1#include "cache.h"
2#include "exec_cmd.h"
3#include "pkt-line.h"
4#include <sys/wait.h>
5#include <sys/time.h>
67
static int finish_pack(const char *pack_tmp_name, const char *me)
8{
9int pipe_fd[2];
10pid_t pid;
11char idx[PATH_MAX];
12char final[PATH_MAX];
13char hash[41];
14unsigned char sha1[20];
15char *cp;
16int err = 0;
1718
if (pipe(pipe_fd) < 0)
19die("%s: unable to set up pipe", me);
2021
strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
22cp = strrchr(idx, '/');
23memcpy(cp, "/pidx", 5);
2425
pid = fork();
26if (pid < 0)
27die("%s: unable to fork off git-index-pack", me);
28if (!pid) {
29close(0);
30dup2(pipe_fd[1], 1);
31close(pipe_fd[0]);
32close(pipe_fd[1]);
33execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
34error("cannot exec git-index-pack <%s> <%s>",
35idx, pack_tmp_name);
36exit(1);
37}
38close(pipe_fd[1]);
39if (read(pipe_fd[0], hash, 40) != 40) {
40error("%s: unable to read from git-index-pack", me);
41err = 1;
42}
43close(pipe_fd[0]);
4445
for (;;) {
46int status, code;
47int retval = waitpid(pid, &status, 0);
4849
if (retval < 0) {
50if (errno == EINTR)
51continue;
52error("waitpid failed (%s)", strerror(errno));
53goto error_die;
54}
55if (WIFSIGNALED(status)) {
56int sig = WTERMSIG(status);
57error("git-index-pack died of signal %d", sig);
58goto error_die;
59}
60if (!WIFEXITED(status)) {
61error("git-index-pack died of unnatural causes %d",
62status);
63goto error_die;
64}
65code = WEXITSTATUS(status);
66if (code) {
67error("git-index-pack died with error code %d", code);
68goto error_die;
69}
70if (err)
71goto error_die;
72break;
73}
74hash[40] = 0;
75if (get_sha1_hex(hash, sha1)) {
76error("git-index-pack reported nonsense '%s'", hash);
77goto error_die;
78}
79/* Now we have pack in pack_tmp_name[], and
80* idx in idx[]; rename them to their final names.
81*/
82snprintf(final, sizeof(final),
83"%s/pack/pack-%s.pack", get_object_directory(), hash);
84move_temp_to_file(pack_tmp_name, final);
85chmod(final, 0444);
86snprintf(final, sizeof(final),
87"%s/pack/pack-%s.idx", get_object_directory(), hash);
88move_temp_to_file(idx, final);
89chmod(final, 0444);
90return 0;
9192
error_die:
93unlink(idx);
94unlink(pack_tmp_name);
95exit(1);
96}
9798
static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
99{
100pid_t side_pid;
101102
if (!sideband) {
103fd[0] = xd[0];
104fd[1] = xd[1];
105return 0;
106}
107/* xd[] is talking with upload-pack; subprocess reads from
108* xd[0], spits out band#2 to stderr, and feeds us band#1
109* through our fd[0].
110*/
111if (pipe(fd) < 0)
112die("%s: unable to set up pipe", me);
113side_pid = fork();
114if (side_pid < 0)
115die("%s: unable to fork off sideband demultiplexer", me);
116if (!side_pid) {
117/* subprocess */
118close(fd[0]);
119if (xd[0] != xd[1])
120close(xd[1]);
121while (1) {
122char buf[1024];
123int len = packet_read_line(xd[0], buf, sizeof(buf));
124if (len == 0)
125break;
126if (len < 1)
127die("%s: protocol error: no band designator",
128me);
129len--;
130switch (buf[0] & 0xFF) {
131case 3:
132safe_write(2, buf+1, len);
133fprintf(stderr, "\n");
134exit(1);
135case 2:
136safe_write(2, buf+1, len);
137continue;
138case 1:
139safe_write(fd[1], buf+1, len);
140continue;
141default:
142die("%s: protocol error: bad band #%d",
143me, (buf[0] & 0xFF));
144}
145}
146exit(0);
147}
148close(xd[0]);
149close(fd[1]);
150fd[1] = xd[1];
151return side_pid;
152}
153154
int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
155{
156int status;
157pid_t pid, side_pid;
158int fd[2];
159160
side_pid = setup_sideband(sideband, me, fd, xd);
161pid = fork();
162if (pid < 0)
163die("%s: unable to fork off git-unpack-objects", me);
164if (!pid) {
165dup2(fd[0], 0);
166close(fd[0]);
167close(fd[1]);
168execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
169die("git-unpack-objects exec failed");
170}
171close(fd[0]);
172close(fd[1]);
173while (waitpid(pid, &status, 0) < 0) {
174if (errno != EINTR)
175die("waiting for git-unpack-objects: %s",
176strerror(errno));
177}
178if (WIFEXITED(status)) {
179int code = WEXITSTATUS(status);
180if (code)
181die("git-unpack-objects died with error code %d",
182code);
183return 0;
184}
185if (WIFSIGNALED(status)) {
186int sig = WTERMSIG(status);
187die("git-unpack-objects died of signal %d", sig);
188}
189die("git-unpack-objects died of unnatural causes %d", status);
190}
191192
/*
193* We average out the download speed over this many "events", where
194* an event is a minimum of about half a second. That way, we get
195* a reasonably stable number.
196*/
197#define NR_AVERAGE (4)
198199
/*
200* A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
201* Keeing the time in that format means that "bytes / msecs" means
202* is the same as kB/s (modulo rounding).
203*
204* 1000512 is a magic number (usecs in a second, rounded up by half
205* of 1024, to make "rounding" come out right ;)
206*/
207#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
208209
int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
210{
211char tmpfile[PATH_MAX];
212int ofd, ifd, fd[2];
213unsigned long total;
214static struct timeval prev_tv;
215struct average {
216unsigned long bytes;
217unsigned long time;
218} download[NR_AVERAGE] = { {0, 0}, };
219unsigned long avg_bytes, avg_time;
220int idx = 0;
221222
setup_sideband(sideband, me, fd, xd);
223224
ifd = fd[0];
225snprintf(tmpfile, sizeof(tmpfile),
226"%s/pack/tmp-XXXXXX", get_object_directory());
227ofd = mkstemp(tmpfile);
228if (ofd < 0)
229return error("unable to create temporary file %s", tmpfile);
230231
gettimeofday(&prev_tv, NULL);
232total = 0;
233avg_bytes = 0;
234avg_time = 0;
235while (1) {
236char buf[8192];
237ssize_t sz, wsz, pos;
238sz = read(ifd, buf, sizeof(buf));
239if (sz == 0)
240break;
241if (sz < 0) {
242if (errno != EINTR && errno != EAGAIN) {
243error("error reading pack (%s)", strerror(errno));
244close(ofd);
245unlink(tmpfile);
246return -1;
247}
248sz = 0;
249}
250pos = 0;
251while (pos < sz) {
252wsz = write(ofd, buf + pos, sz - pos);
253if (wsz < 0) {
254error("error writing pack (%s)",
255strerror(errno));
256close(ofd);
257unlink(tmpfile);
258return -1;
259}
260pos += wsz;
261}
262total += sz;
263if (!quiet) {
264static unsigned long last;
265struct timeval tv;
266unsigned long diff = total - last;
267/* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
268unsigned long msecs;
269270
gettimeofday(&tv, NULL);
271msecs = tv.tv_sec - prev_tv.tv_sec;
272msecs <<= 10;
273msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
274275
if (msecs > 500) {
276prev_tv = tv;
277last = total;
278279
/* Update averages ..*/
280avg_bytes += diff;
281avg_time += msecs;
282avg_bytes -= download[idx].bytes;
283avg_time -= download[idx].time;
284download[idx].bytes = diff;
285download[idx].time = msecs;
286idx++;
287if (idx >= NR_AVERAGE)
288idx = 0;
289290
fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
291total >> 20,
2921000*((total >> 10) & 1023)>>10,
293avg_bytes / avg_time );
294}
295}
296}
297close(ofd);
298return finish_pack(tmpfile, me);
299}