]>
Commit | Line | Data |
---|---|---|
ad897215 | 1 | #include "cache.h" |
77cb17e9 | 2 | #include "exec_cmd.h" |
583b7ea3 | 3 | #include "pkt-line.h" |
ad897215 | 4 | #include <sys/wait.h> |
5ee2ad65 | 5 | #include <sys/time.h> |
ad897215 JH |
6 | |
7 | static int finish_pack(const char *pack_tmp_name, const char *me) | |
8 | { | |
9 | int pipe_fd[2]; | |
10 | pid_t pid; | |
11 | char idx[PATH_MAX]; | |
12 | char final[PATH_MAX]; | |
13 | char hash[41]; | |
14 | unsigned char sha1[20]; | |
15 | char *cp; | |
16 | int err = 0; | |
17 | ||
18 | if (pipe(pipe_fd) < 0) | |
19 | die("%s: unable to set up pipe", me); | |
20 | ||
21 | strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */ | |
22 | cp = strrchr(idx, '/'); | |
23 | memcpy(cp, "/pidx", 5); | |
24 | ||
25 | pid = fork(); | |
26 | if (pid < 0) | |
583b7ea3 | 27 | die("%s: unable to fork off git-index-pack", me); |
ad897215 JH |
28 | if (!pid) { |
29 | close(0); | |
30 | dup2(pipe_fd[1], 1); | |
31 | close(pipe_fd[0]); | |
32 | close(pipe_fd[1]); | |
77cb17e9 | 33 | execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL); |
ad897215 JH |
34 | error("cannot exec git-index-pack <%s> <%s>", |
35 | idx, pack_tmp_name); | |
36 | exit(1); | |
37 | } | |
38 | close(pipe_fd[1]); | |
39 | if (read(pipe_fd[0], hash, 40) != 40) { | |
40 | error("%s: unable to read from git-index-pack", me); | |
41 | err = 1; | |
42 | } | |
43 | close(pipe_fd[0]); | |
44 | ||
45 | for (;;) { | |
46 | int status, code; | |
47 | int retval = waitpid(pid, &status, 0); | |
48 | ||
49 | if (retval < 0) { | |
50 | if (errno == EINTR) | |
51 | continue; | |
7f272ca8 | 52 | error("waitpid failed (%s)", strerror(errno)); |
ad897215 JH |
53 | goto error_die; |
54 | } | |
55 | if (WIFSIGNALED(status)) { | |
56 | int sig = WTERMSIG(status); | |
57 | error("git-index-pack died of signal %d", sig); | |
58 | goto error_die; | |
59 | } | |
60 | if (!WIFEXITED(status)) { | |
61 | error("git-index-pack died of unnatural causes %d", | |
62 | status); | |
63 | goto error_die; | |
64 | } | |
65 | code = WEXITSTATUS(status); | |
66 | if (code) { | |
67 | error("git-index-pack died with error code %d", code); | |
68 | goto error_die; | |
69 | } | |
70 | if (err) | |
71 | goto error_die; | |
72 | break; | |
73 | } | |
74 | hash[40] = 0; | |
75 | if (get_sha1_hex(hash, sha1)) { | |
76 | error("git-index-pack reported nonsense '%s'", hash); | |
77 | goto error_die; | |
78 | } | |
79 | /* Now we have pack in pack_tmp_name[], and | |
80 | * idx in idx[]; rename them to their final names. | |
81 | */ | |
82 | snprintf(final, sizeof(final), | |
83 | "%s/pack/pack-%s.pack", get_object_directory(), hash); | |
84 | move_temp_to_file(pack_tmp_name, final); | |
85 | chmod(final, 0444); | |
86 | snprintf(final, sizeof(final), | |
87 | "%s/pack/pack-%s.idx", get_object_directory(), hash); | |
88 | move_temp_to_file(idx, final); | |
89 | chmod(final, 0444); | |
90 | return 0; | |
91 | ||
92 | error_die: | |
93 | unlink(idx); | |
94 | unlink(pack_tmp_name); | |
95 | exit(1); | |
96 | } | |
97 | ||
583b7ea3 JH |
98 | static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2]) |
99 | { | |
100 | pid_t side_pid; | |
101 | ||
102 | if (!sideband) { | |
103 | fd[0] = xd[0]; | |
104 | fd[1] = xd[1]; | |
105 | return 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 | */ | |
111 | if (pipe(fd) < 0) | |
112 | die("%s: unable to set up pipe", me); | |
113 | side_pid = fork(); | |
114 | if (side_pid < 0) | |
115 | die("%s: unable to fork off sideband demultiplexer", me); | |
116 | if (!side_pid) { | |
117 | /* subprocess */ | |
118 | close(fd[0]); | |
119 | if (xd[0] != xd[1]) | |
120 | close(xd[1]); | |
121 | while (1) { | |
122 | char buf[1024]; | |
123 | int len = packet_read_line(xd[0], buf, sizeof(buf)); | |
124 | if (len == 0) | |
125 | break; | |
126 | if (len < 1) | |
127 | die("%s: protocol error: no band designator", | |
128 | me); | |
129 | len--; | |
130 | switch (buf[0] & 0xFF) { | |
131 | case 3: | |
132 | safe_write(2, buf+1, len); | |
133 | fprintf(stderr, "\n"); | |
134 | exit(1); | |
135 | case 2: | |
136 | safe_write(2, buf+1, len); | |
137 | continue; | |
138 | case 1: | |
139 | safe_write(fd[1], buf+1, len); | |
140 | continue; | |
141 | default: | |
142 | die("%s: protocol error: bad band #%d", | |
143 | me, (buf[0] & 0xFF)); | |
144 | } | |
145 | } | |
146 | exit(0); | |
147 | } | |
148 | close(xd[0]); | |
149 | close(fd[1]); | |
150 | fd[1] = xd[1]; | |
151 | return side_pid; | |
152 | } | |
153 | ||
154 | int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband) | |
ad897215 JH |
155 | { |
156 | int status; | |
583b7ea3 JH |
157 | pid_t pid, side_pid; |
158 | int fd[2]; | |
ad897215 | 159 | |
583b7ea3 | 160 | side_pid = setup_sideband(sideband, me, fd, xd); |
ad897215 JH |
161 | pid = fork(); |
162 | if (pid < 0) | |
163 | die("%s: unable to fork off git-unpack-objects", me); | |
164 | if (!pid) { | |
165 | dup2(fd[0], 0); | |
166 | close(fd[0]); | |
167 | close(fd[1]); | |
77cb17e9 | 168 | execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL); |
ad897215 JH |
169 | die("git-unpack-objects exec failed"); |
170 | } | |
171 | close(fd[0]); | |
172 | close(fd[1]); | |
173 | while (waitpid(pid, &status, 0) < 0) { | |
174 | if (errno != EINTR) | |
175 | die("waiting for git-unpack-objects: %s", | |
176 | strerror(errno)); | |
177 | } | |
178 | if (WIFEXITED(status)) { | |
179 | int code = WEXITSTATUS(status); | |
180 | if (code) | |
181 | die("git-unpack-objects died with error code %d", | |
182 | code); | |
183 | return 0; | |
184 | } | |
185 | if (WIFSIGNALED(status)) { | |
186 | int sig = WTERMSIG(status); | |
187 | die("git-unpack-objects died of signal %d", sig); | |
188 | } | |
189 | die("git-unpack-objects died of unnatural causes %d", status); | |
190 | } | |
191 | ||
c548cf4e LT |
192 | /* |
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) | |
198 | ||
199 | /* | |
200 | * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second. | |
82e5a82f PR |
201 | * Keeping the time in that format means that "bytes / msecs" means |
202 | * the same as kB/s (modulo rounding). | |
c548cf4e LT |
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)) | |
208 | ||
583b7ea3 | 209 | int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband) |
ad897215 JH |
210 | { |
211 | char tmpfile[PATH_MAX]; | |
583b7ea3 | 212 | int ofd, ifd, fd[2]; |
5ee2ad65 LT |
213 | unsigned long total; |
214 | static struct timeval prev_tv; | |
c548cf4e LT |
215 | struct average { |
216 | unsigned long bytes; | |
217 | unsigned long time; | |
218 | } download[NR_AVERAGE] = { {0, 0}, }; | |
219 | unsigned long avg_bytes, avg_time; | |
220 | int idx = 0; | |
ad897215 | 221 | |
583b7ea3 JH |
222 | setup_sideband(sideband, me, fd, xd); |
223 | ||
ad897215 JH |
224 | ifd = fd[0]; |
225 | snprintf(tmpfile, sizeof(tmpfile), | |
226 | "%s/pack/tmp-XXXXXX", get_object_directory()); | |
227 | ofd = mkstemp(tmpfile); | |
228 | if (ofd < 0) | |
229 | return error("unable to create temporary file %s", tmpfile); | |
230 | ||
5ee2ad65 LT |
231 | gettimeofday(&prev_tv, NULL); |
232 | total = 0; | |
c548cf4e LT |
233 | avg_bytes = 0; |
234 | avg_time = 0; | |
ad897215 JH |
235 | while (1) { |
236 | char buf[8192]; | |
237 | ssize_t sz, wsz, pos; | |
238 | sz = read(ifd, buf, sizeof(buf)); | |
239 | if (sz == 0) | |
240 | break; | |
241 | if (sz < 0) { | |
98deeaa8 LT |
242 | if (errno != EINTR && errno != EAGAIN) { |
243 | error("error reading pack (%s)", strerror(errno)); | |
244 | close(ofd); | |
245 | unlink(tmpfile); | |
246 | return -1; | |
247 | } | |
248 | sz = 0; | |
ad897215 JH |
249 | } |
250 | pos = 0; | |
251 | while (pos < sz) { | |
252 | wsz = write(ofd, buf + pos, sz - pos); | |
253 | if (wsz < 0) { | |
254 | error("error writing pack (%s)", | |
255 | strerror(errno)); | |
256 | close(ofd); | |
257 | unlink(tmpfile); | |
258 | return -1; | |
259 | } | |
260 | pos += wsz; | |
261 | } | |
5ee2ad65 LT |
262 | total += sz; |
263 | if (!quiet) { | |
264 | static unsigned long last; | |
265 | struct timeval tv; | |
266 | unsigned long diff = total - last; | |
267 | /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */ | |
268 | unsigned long msecs; | |
269 | ||
270 | gettimeofday(&tv, NULL); | |
271 | msecs = tv.tv_sec - prev_tv.tv_sec; | |
272 | msecs <<= 10; | |
c548cf4e LT |
273 | msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec); |
274 | ||
5ee2ad65 LT |
275 | if (msecs > 500) { |
276 | prev_tv = tv; | |
277 | last = total; | |
c548cf4e LT |
278 | |
279 | /* Update averages ..*/ | |
280 | avg_bytes += diff; | |
281 | avg_time += msecs; | |
282 | avg_bytes -= download[idx].bytes; | |
283 | avg_time -= download[idx].time; | |
284 | download[idx].bytes = diff; | |
285 | download[idx].time = msecs; | |
286 | idx++; | |
287 | if (idx >= NR_AVERAGE) | |
288 | idx = 0; | |
289 | ||
290 | fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r", | |
5ee2ad65 LT |
291 | total >> 20, |
292 | 1000*((total >> 10) & 1023)>>10, | |
c548cf4e | 293 | avg_bytes / avg_time ); |
5ee2ad65 LT |
294 | } |
295 | } | |
ad897215 JH |
296 | } |
297 | close(ofd); | |
298 | return finish_pack(tmpfile, me); | |
299 | } |