8 static int finish_pack(const char *pack_tmp_name
, const char *me
)
15 unsigned char sha1
[20];
19 if (pipe(pipe_fd
) < 0)
20 die("%s: unable to set up pipe", me
);
22 strcpy(idx
, pack_tmp_name
); /* ".git/objects/pack-XXXXXX" */
23 cp
= strrchr(idx
, '/');
24 memcpy(cp
, "/pidx", 5);
28 die("%s: unable to fork off git-index-pack", me
);
34 execl_git_cmd("index-pack", "-o", idx
, pack_tmp_name
, NULL
);
35 error("cannot exec git-index-pack <%s> <%s>",
40 if (read(pipe_fd
[0], hash
, 40) != 40) {
41 error("%s: unable to read from git-index-pack", me
);
49 if (waitpid(pid
, &status
, 0) < 0) {
52 error("waitpid failed (%s)", strerror(errno
));
55 if (WIFSIGNALED(status
)) {
56 int sig
= WTERMSIG(status
);
57 error("git-index-pack died of signal %d", sig
);
60 if (!WIFEXITED(status
)) {
61 error("git-index-pack died of unnatural causes %d",
65 code
= WEXITSTATUS(status
);
67 error("git-index-pack died with error code %d", code
);
75 if (get_sha1_hex(hash
, sha1
)) {
76 error("git-index-pack reported nonsense '%s'", hash
);
79 /* Now we have pack in pack_tmp_name[], and
80 * idx in idx[]; rename them to their final names.
82 snprintf(final
, sizeof(final
),
83 "%s/pack/pack-%s.pack", get_object_directory(), hash
);
84 move_temp_to_file(pack_tmp_name
, final
);
86 snprintf(final
, sizeof(final
),
87 "%s/pack/pack-%s.idx", get_object_directory(), hash
);
88 move_temp_to_file(idx
, final
);
94 unlink(pack_tmp_name
);
98 static pid_t
setup_sideband(int sideband
, const char *me
, int fd
[2], int xd
[2])
107 /* xd[] is talking with upload-pack; subprocess reads from
108 * xd[0], spits out band#2 to stderr, and feeds us band#1
112 die("%s: unable to set up pipe", me
);
115 die("%s: unable to fork off sideband demultiplexer", me
);
121 if (recv_sideband(me
, xd
[0], fd
[1], 2))
131 int receive_unpack_pack(int xd
[2], const char *me
, int quiet
, int sideband
)
137 side_pid
= setup_sideband(sideband
, me
, fd
, xd
);
140 die("%s: unable to fork off git-unpack-objects", me
);
145 execl_git_cmd("unpack-objects", quiet
? "-q" : NULL
, NULL
);
146 die("git-unpack-objects exec failed");
150 while (waitpid(pid
, &status
, 0) < 0) {
152 die("waiting for git-unpack-objects: %s",
155 if (WIFEXITED(status
)) {
156 int code
= WEXITSTATUS(status
);
158 die("git-unpack-objects died with error code %d",
162 if (WIFSIGNALED(status
)) {
163 int sig
= WTERMSIG(status
);
164 die("git-unpack-objects died of signal %d", sig
);
166 die("git-unpack-objects died of unnatural causes %d", status
);
170 * We average out the download speed over this many "events", where
171 * an event is a minimum of about half a second. That way, we get
172 * a reasonably stable number.
174 #define NR_AVERAGE (4)
177 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
178 * Keeping the time in that format means that "bytes / msecs" means
179 * the same as kB/s (modulo rounding).
181 * 1000512 is a magic number (usecs in a second, rounded up by half
182 * of 1024, to make "rounding" come out right ;)
184 #define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
186 int receive_keep_pack(int xd
[2], const char *me
, int quiet
, int sideband
)
188 char tmpfile
[PATH_MAX
];
191 static struct timeval prev_tv
;
195 } download
[NR_AVERAGE
] = { {0, 0}, };
196 unsigned long avg_bytes
, avg_time
;
199 setup_sideband(sideband
, me
, fd
, xd
);
202 snprintf(tmpfile
, sizeof(tmpfile
),
203 "%s/pack/tmp-XXXXXX", get_object_directory());
204 ofd
= mkstemp(tmpfile
);
206 return error("unable to create temporary file %s", tmpfile
);
208 gettimeofday(&prev_tv
, NULL
);
214 ssize_t sz
, wsz
, pos
;
215 sz
= read(ifd
, buf
, sizeof(buf
));
219 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
220 error("error reading pack (%s)", strerror(errno
));
229 wsz
= write(ofd
, buf
+ pos
, sz
- pos
);
231 error("error writing pack (%s)",
241 static unsigned long last
;
243 unsigned long diff
= total
- last
;
244 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
247 gettimeofday(&tv
, NULL
);
248 msecs
= tv
.tv_sec
- prev_tv
.tv_sec
;
250 msecs
+= usec_to_binarymsec(tv
.tv_usec
- prev_tv
.tv_usec
);
256 /* Update averages ..*/
259 avg_bytes
-= download
[idx
].bytes
;
260 avg_time
-= download
[idx
].time
;
261 download
[idx
].bytes
= diff
;
262 download
[idx
].time
= msecs
;
264 if (idx
>= NR_AVERAGE
)
267 fprintf(stderr
, "%4lu.%03luMB (%lu kB/s) \r",
269 1000*((total
>> 10) & 1023)>>10,
270 avg_bytes
/ avg_time
);
275 return finish_pack(tmpfile
, me
);