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:
136/* color sideband */
137safe_write(2, "\033[44;37;1m", 10);
138safe_write(2, buf+1, len);
139safe_write(2, "\033[m", 3);
140continue;
141case 1:
142safe_write(fd[1], buf+1, len);
143continue;
144default:
145die("%s: protocol error: bad band #%d",
146me, (buf[0] & 0xFF));
147}
148}
149exit(0);
150}
151close(xd[0]);
152close(fd[1]);
153fd[1] = xd[1];
154return side_pid;
155}
156157
int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
158{
159int status;
160pid_t pid, side_pid;
161int fd[2];
162163
side_pid = setup_sideband(sideband, me, fd, xd);
164pid = fork();
165if (pid < 0)
166die("%s: unable to fork off git-unpack-objects", me);
167if (!pid) {
168dup2(fd[0], 0);
169close(fd[0]);
170close(fd[1]);
171execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
172die("git-unpack-objects exec failed");
173}
174close(fd[0]);
175close(fd[1]);
176while (waitpid(pid, &status, 0) < 0) {
177if (errno != EINTR)
178die("waiting for git-unpack-objects: %s",
179strerror(errno));
180}
181if (WIFEXITED(status)) {
182int code = WEXITSTATUS(status);
183if (code)
184die("git-unpack-objects died with error code %d",
185code);
186return 0;
187}
188if (WIFSIGNALED(status)) {
189int sig = WTERMSIG(status);
190die("git-unpack-objects died of signal %d", sig);
191}
192die("git-unpack-objects died of unnatural causes %d", status);
193}
194195
/*
196* We average out the download speed over this many "events", where
197* an event is a minimum of about half a second. That way, we get
198* a reasonably stable number.
199*/
200#define NR_AVERAGE (4)
201202
/*
203* A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
204* Keeping the time in that format means that "bytes / msecs" means
205* the same as kB/s (modulo rounding).
206*
207* 1000512 is a magic number (usecs in a second, rounded up by half
208* of 1024, to make "rounding" come out right ;)
209*/
210#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
211212
int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
213{
214char tmpfile[PATH_MAX];
215int ofd, ifd, fd[2];
216unsigned long total;
217static struct timeval prev_tv;
218struct average {
219unsigned long bytes;
220unsigned long time;
221} download[NR_AVERAGE] = { {0, 0}, };
222unsigned long avg_bytes, avg_time;
223int idx = 0;
224225
setup_sideband(sideband, me, fd, xd);
226227
ifd = fd[0];
228snprintf(tmpfile, sizeof(tmpfile),
229"%s/pack/tmp-XXXXXX", get_object_directory());
230ofd = mkstemp(tmpfile);
231if (ofd < 0)
232return error("unable to create temporary file %s", tmpfile);
233234
gettimeofday(&prev_tv, NULL);
235total = 0;
236avg_bytes = 0;
237avg_time = 0;
238while (1) {
239char buf[8192];
240ssize_t sz, wsz, pos;
241sz = read(ifd, buf, sizeof(buf));
242if (sz == 0)
243break;
244if (sz < 0) {
245if (errno != EINTR && errno != EAGAIN) {
246error("error reading pack (%s)", strerror(errno));
247close(ofd);
248unlink(tmpfile);
249return -1;
250}
251sz = 0;
252}
253pos = 0;
254while (pos < sz) {
255wsz = write(ofd, buf + pos, sz - pos);
256if (wsz < 0) {
257error("error writing pack (%s)",
258strerror(errno));
259close(ofd);
260unlink(tmpfile);
261return -1;
262}
263pos += wsz;
264}
265total += sz;
266if (!quiet) {
267static unsigned long last;
268struct timeval tv;
269unsigned long diff = total - last;
270/* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
271unsigned long msecs;
272273
gettimeofday(&tv, NULL);
274msecs = tv.tv_sec - prev_tv.tv_sec;
275msecs <<= 10;
276msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
277278
if (msecs > 500) {
279prev_tv = tv;
280last = total;
281282
/* Update averages ..*/
283avg_bytes += diff;
284avg_time += msecs;
285avg_bytes -= download[idx].bytes;
286avg_time -= download[idx].time;
287download[idx].bytes = diff;
288download[idx].time = msecs;
289idx++;
290if (idx >= NR_AVERAGE)
291idx = 0;
292293
fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
294total >> 20,
2951000*((total >> 10) & 1023)>>10,
296avg_bytes / avg_time );
297}
298}
299}
300close(ofd);
301return finish_pack(tmpfile, me);
302}