7 static int finish_pack(const char *pack_tmp_name, const char *me)
14 unsigned char sha1[20];
18 if (pipe(pipe_fd) < 0)
19 die("%s: unable to set up pipe", me);
21 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
22 cp = strrchr(idx, '/');
23 memcpy(cp, "/pidx", 5);
27 die("%s: unable to fork off git-index-pack", me);
33 execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
34 error("cannot exec git-index-pack <%s> <%s>",
39 if (read(pipe_fd[0], hash, 40) != 40) {
40 error("%s: unable to read from git-index-pack", me);
48 if (waitpid(pid, &status, 0) < 0) {
51 error("waitpid failed (%s)", strerror(errno));
54 if (WIFSIGNALED(status)) {
55 int sig = WTERMSIG(status);
56 error("git-index-pack died of signal %d", sig);
59 if (!WIFEXITED(status)) {
60 error("git-index-pack died of unnatural causes %d",
64 code = WEXITSTATUS(status);
66 error("git-index-pack died with error code %d", code);
74 if (get_sha1_hex(hash, sha1)) {
75 error("git-index-pack reported nonsense '%s'", hash);
78 /* Now we have pack in pack_tmp_name[], and
79 * idx in idx[]; rename them to their final names.
81 snprintf(final, sizeof(final),
82 "%s/pack/pack-%s.pack", get_object_directory(), hash);
83 move_temp_to_file(pack_tmp_name, final);
85 snprintf(final, sizeof(final),
86 "%s/pack/pack-%s.idx", get_object_directory(), hash);
87 move_temp_to_file(idx, final);
93 unlink(pack_tmp_name);
97 static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
106 /* xd[] is talking with upload-pack; subprocess reads from
107 * xd[0], spits out band#2 to stderr, and feeds us band#1
111 die("%s: unable to set up pipe", me);
114 die("%s: unable to fork off sideband demultiplexer", me);
122 int len = packet_read_line(xd[0], buf, sizeof(buf));
126 die("%s: protocol error: no band designator",
129 switch (buf[0] & 0xFF) {
131 safe_write(2, "remote: ", 8);
132 safe_write(2, buf+1, len);
133 safe_write(2, "\n", 1);
136 safe_write(2, "remote: ", 8);
137 safe_write(2, buf+1, len);
140 safe_write(fd[1], buf+1, len);
143 die("%s: protocol error: bad band #%d",
144 me, (buf[0] & 0xFF));
155 int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
161 side_pid = setup_sideband(sideband, me, fd, xd);
164 die("%s: unable to fork off git-unpack-objects", me);
169 execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
170 die("git-unpack-objects exec failed");
174 while (waitpid(pid, &status, 0) < 0) {
176 die("waiting for git-unpack-objects: %s",
179 if (WIFEXITED(status)) {
180 int code = WEXITSTATUS(status);
182 die("git-unpack-objects died with error code %d",
186 if (WIFSIGNALED(status)) {
187 int sig = WTERMSIG(status);
188 die("git-unpack-objects died of signal %d", sig);
190 die("git-unpack-objects died of unnatural causes %d", status);
194 * We average out the download speed over this many "events", where
195 * an event is a minimum of about half a second. That way, we get
196 * a reasonably stable number.
198 #define NR_AVERAGE (4)
201 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
202 * Keeping the time in that format means that "bytes / msecs" means
203 * the same as kB/s (modulo rounding).
205 * 1000512 is a magic number (usecs in a second, rounded up by half
206 * of 1024, to make "rounding" come out right ;)
208 #define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
210 int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
212 char tmpfile[PATH_MAX];
215 static struct timeval prev_tv;
219 } download[NR_AVERAGE] = { {0, 0}, };
220 unsigned long avg_bytes, avg_time;
223 setup_sideband(sideband, me, fd, xd);
226 snprintf(tmpfile, sizeof(tmpfile),
227 "%s/pack/tmp-XXXXXX", get_object_directory());
228 ofd = mkstemp(tmpfile);
230 return error("unable to create temporary file %s", tmpfile);
232 gettimeofday(&prev_tv, NULL);
238 ssize_t sz, wsz, pos;
239 sz = read(ifd, buf, sizeof(buf));
243 if (errno != EINTR && errno != EAGAIN) {
244 error("error reading pack (%s)", strerror(errno));
253 wsz = write(ofd, buf + pos, sz - pos);
255 error("error writing pack (%s)",
265 static unsigned long last;
267 unsigned long diff = total - last;
268 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
271 gettimeofday(&tv, NULL);
272 msecs = tv.tv_sec - prev_tv.tv_sec;
274 msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
280 /* Update averages ..*/
283 avg_bytes -= download[idx].bytes;
284 avg_time -= download[idx].time;
285 download[idx].bytes = diff;
286 download[idx].time = msecs;
288 if (idx >= NR_AVERAGE)
291 fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
293 1000*((total >> 10) & 1023)>>10,
294 avg_bytes / avg_time );
299 return finish_pack(tmpfile, me);