upload-pack: fix timeout in create_pack_file
[git.git] / upload-pack.c
blob638e257c948a16bc152276a65e5e34a3cab9a7eb
1 #include <signal.h>
2 #include <sys/wait.h>
3 #include <sys/poll.h>
4 #include "cache.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "tag.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "exec_cmd.h"
12 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
14 #define THEY_HAVE (1U << 0)
15 #define OUR_REF (1U << 1)
16 #define WANTED (1U << 2)
17 #define MAX_HAS 256
18 #define MAX_NEEDS 256
19 static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
20 static int use_thin_pack = 0;
21 static unsigned char has_sha1[MAX_HAS][20];
22 static unsigned char needs_sha1[MAX_NEEDS][20];
23 static unsigned int timeout = 0;
24 static int use_sideband = 0;
26 static void reset_timeout(void)
28 alarm(timeout);
31 static int strip(char *line, int len)
33 if (len && line[len-1] == '\n')
34 line[--len] = 0;
35 return len;
38 #define PACKET_MAX 1000
39 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
41 ssize_t ssz;
42 const char *p;
44 if (!data) {
45 if (!use_sideband)
46 return 0;
47 packet_flush(1);
50 if (!use_sideband) {
51 if (fd == 3)
52 /* emergency quit */
53 fd = 2;
54 return safe_write(fd, data, sz);
56 p = data;
57 ssz = sz;
58 while (sz) {
59 unsigned n;
60 char hdr[5];
62 n = sz;
63 if (PACKET_MAX - 5 < n)
64 n = PACKET_MAX - 5;
65 sprintf(hdr, "%04x", n + 5);
66 hdr[4] = fd;
67 safe_write(1, hdr, 5);
68 safe_write(1, p, n);
69 p += n;
70 sz -= n;
72 return ssz;
75 static void create_pack_file(void)
77 /* Pipes between rev-list to pack-objects, pack-objects to us
78 * and pack-objects error stream for progress bar.
80 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
81 pid_t pid_rev_list, pid_pack_objects;
82 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
83 char data[8193], progress[128];
84 char abort_msg[] = "aborting due to possible repository "
85 "corruption on the remote side.";
86 int buffered = -1;
88 if (pipe(lp_pipe) < 0)
89 die("git-upload-pack: unable to create pipe");
90 pid_rev_list = fork();
91 if (pid_rev_list < 0)
92 die("git-upload-pack: unable to fork git-rev-list");
94 if (!pid_rev_list) {
95 int i;
96 int args;
97 const char **argv;
98 const char **p;
99 char *buf;
101 if (create_full_pack) {
102 args = 10;
103 use_thin_pack = 0; /* no point doing it */
105 else
106 args = nr_has + nr_needs + 5;
107 p = xmalloc(args * sizeof(char *));
108 argv = (const char **) p;
109 buf = xmalloc(args * 45);
111 dup2(lp_pipe[1], 1);
112 close(0);
113 close(lp_pipe[0]);
114 close(lp_pipe[1]);
115 *p++ = "rev-list";
116 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
117 if (create_full_pack || MAX_NEEDS <= nr_needs)
118 *p++ = "--all";
119 else {
120 for (i = 0; i < nr_needs; i++) {
121 *p++ = buf;
122 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
123 buf += 41;
126 if (!create_full_pack)
127 for (i = 0; i < nr_has; i++) {
128 *p++ = buf;
129 *buf++ = '^';
130 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
131 buf += 41;
133 *p++ = NULL;
134 execv_git_cmd(argv);
135 die("git-upload-pack: unable to exec git-rev-list");
138 if (pipe(pu_pipe) < 0)
139 die("git-upload-pack: unable to create pipe");
140 if (pipe(pe_pipe) < 0)
141 die("git-upload-pack: unable to create pipe");
142 pid_pack_objects = fork();
143 if (pid_pack_objects < 0) {
144 /* daemon sets things up to ignore TERM */
145 kill(pid_rev_list, SIGKILL);
146 die("git-upload-pack: unable to fork git-pack-objects");
148 if (!pid_pack_objects) {
149 dup2(lp_pipe[0], 0);
150 dup2(pu_pipe[1], 1);
151 dup2(pe_pipe[1], 2);
153 close(lp_pipe[0]);
154 close(lp_pipe[1]);
155 close(pu_pipe[0]);
156 close(pu_pipe[1]);
157 close(pe_pipe[0]);
158 close(pe_pipe[1]);
159 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
160 kill(pid_rev_list, SIGKILL);
161 die("git-upload-pack: unable to exec git-pack-objects");
164 close(lp_pipe[0]);
165 close(lp_pipe[1]);
167 /* We read from pe_pipe[0] to capture stderr output for
168 * progress bar, and pu_pipe[0] to capture the pack data.
170 close(pe_pipe[1]);
171 close(pu_pipe[1]);
173 while (1) {
174 const char *who;
175 struct pollfd pfd[2];
176 pid_t pid;
177 int status;
178 ssize_t sz;
179 int pe, pu, pollsize;
181 reset_timeout();
183 pollsize = 0;
184 pe = pu = -1;
186 if (0 <= pu_pipe[0]) {
187 pfd[pollsize].fd = pu_pipe[0];
188 pfd[pollsize].events = POLLIN;
189 pu = pollsize;
190 pollsize++;
192 if (0 <= pe_pipe[0]) {
193 pfd[pollsize].fd = pe_pipe[0];
194 pfd[pollsize].events = POLLIN;
195 pe = pollsize;
196 pollsize++;
199 if (pollsize) {
200 if (poll(pfd, pollsize, -1) < 0) {
201 if (errno != EINTR) {
202 error("poll failed, resuming: %s",
203 strerror(errno));
204 sleep(1);
206 continue;
208 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
209 /* Data ready; we keep the last byte
210 * to ourselves in case we detect
211 * broken rev-list, so that we can
212 * leave the stream corrupted. This
213 * is unfortunate -- unpack-objects
214 * would happily accept a valid pack
215 * data with trailing garbage, so
216 * appending garbage after we pass all
217 * the pack data is not good enough to
218 * signal breakage to downstream.
220 char *cp = data;
221 ssize_t outsz = 0;
222 if (0 <= buffered) {
223 *cp++ = buffered;
224 outsz++;
226 sz = read(pu_pipe[0], cp,
227 sizeof(data) - outsz);
228 if (0 < sz)
230 else if (sz == 0) {
231 close(pu_pipe[0]);
232 pu_pipe[0] = -1;
234 else
235 goto fail;
236 sz += outsz;
237 if (1 < sz) {
238 buffered = data[sz-1] & 0xFF;
239 sz--;
241 else
242 buffered = -1;
243 sz = send_client_data(1, data, sz);
244 if (sz < 0)
245 goto fail;
247 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
248 /* Status ready; we ship that in the side-band
249 * or dump to the standard error.
251 sz = read(pe_pipe[0], progress,
252 sizeof(progress));
253 if (0 < sz)
254 send_client_data(2, progress, sz);
255 else if (sz == 0) {
256 close(pe_pipe[0]);
257 pe_pipe[0] = -1;
259 else
260 goto fail;
264 /* See if the children are still there */
265 if (pid_rev_list || pid_pack_objects) {
266 pid = waitpid(-1, &status, WNOHANG);
267 if (!pid)
268 continue;
269 who = ((pid == pid_rev_list) ? "git-rev-list" :
270 (pid == pid_pack_objects) ? "git-pack-objects" :
271 NULL);
272 if (!who) {
273 if (pid < 0) {
274 error("git-upload-pack: %s",
275 strerror(errno));
276 goto fail;
278 error("git-upload-pack: we weren't "
279 "waiting for %d", pid);
280 continue;
282 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
283 error("git-upload-pack: %s died with error.",
284 who);
285 goto fail;
287 if (pid == pid_rev_list)
288 pid_rev_list = 0;
289 if (pid == pid_pack_objects)
290 pid_pack_objects = 0;
291 if (pid_rev_list || pid_pack_objects)
292 continue;
295 /* both died happily */
296 if (pollsize)
297 continue;
299 /* flush the data */
300 if (0 <= buffered) {
301 data[0] = buffered;
302 sz = send_client_data(1, data, 1);
303 if (sz < 0)
304 goto fail;
305 fprintf(stderr, "flushed.\n");
307 send_client_data(1, NULL, 0);
308 return;
310 fail:
311 if (pid_pack_objects)
312 kill(pid_pack_objects, SIGKILL);
313 if (pid_rev_list)
314 kill(pid_rev_list, SIGKILL);
315 send_client_data(3, abort_msg, sizeof(abort_msg));
316 die("git-upload-pack: %s", abort_msg);
319 static int got_sha1(char *hex, unsigned char *sha1)
321 if (get_sha1_hex(hex, sha1))
322 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
323 if (!has_sha1_file(sha1))
324 return 0;
325 if (nr_has < MAX_HAS) {
326 struct object *o = lookup_object(sha1);
327 if (!(o && o->parsed))
328 o = parse_object(sha1);
329 if (!o)
330 die("oops (%s)", sha1_to_hex(sha1));
331 if (o->type == TYPE_COMMIT) {
332 struct commit_list *parents;
333 if (o->flags & THEY_HAVE)
334 return 0;
335 o->flags |= THEY_HAVE;
336 for (parents = ((struct commit*)o)->parents;
337 parents;
338 parents = parents->next)
339 parents->item->object.flags |= THEY_HAVE;
341 memcpy(has_sha1[nr_has++], sha1, 20);
343 return 1;
346 static int get_common_commits(void)
348 static char line[1000];
349 unsigned char sha1[20], last_sha1[20];
350 int len;
352 track_object_refs = 0;
353 save_commit_buffer = 0;
355 for(;;) {
356 len = packet_read_line(0, line, sizeof(line));
357 reset_timeout();
359 if (!len) {
360 if (nr_has == 0 || multi_ack)
361 packet_write(1, "NAK\n");
362 continue;
364 len = strip(line, len);
365 if (!strncmp(line, "have ", 5)) {
366 if (got_sha1(line+5, sha1) &&
367 (multi_ack || nr_has == 1)) {
368 if (nr_has >= MAX_HAS)
369 multi_ack = 0;
370 packet_write(1, "ACK %s%s\n",
371 sha1_to_hex(sha1),
372 multi_ack ? " continue" : "");
373 if (multi_ack)
374 memcpy(last_sha1, sha1, 20);
376 continue;
378 if (!strcmp(line, "done")) {
379 if (nr_has > 0) {
380 if (multi_ack)
381 packet_write(1, "ACK %s\n",
382 sha1_to_hex(last_sha1));
383 return 0;
385 packet_write(1, "NAK\n");
386 return -1;
388 die("git-upload-pack: expected SHA1 list, got '%s'", line);
392 static int receive_needs(void)
394 static char line[1000];
395 int len, needs;
397 needs = 0;
398 for (;;) {
399 struct object *o;
400 unsigned char dummy[20], *sha1_buf;
401 len = packet_read_line(0, line, sizeof(line));
402 reset_timeout();
403 if (!len)
404 return needs;
406 sha1_buf = dummy;
407 if (needs == MAX_NEEDS) {
408 fprintf(stderr,
409 "warning: supporting only a max of %d requests. "
410 "sending everything instead.\n",
411 MAX_NEEDS);
413 else if (needs < MAX_NEEDS)
414 sha1_buf = needs_sha1[needs];
416 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
417 die("git-upload-pack: protocol error, "
418 "expected to get sha, not '%s'", line);
419 if (strstr(line+45, "multi_ack"))
420 multi_ack = 1;
421 if (strstr(line+45, "thin-pack"))
422 use_thin_pack = 1;
423 if (strstr(line+45, "side-band"))
424 use_sideband = 1;
426 /* We have sent all our refs already, and the other end
427 * should have chosen out of them; otherwise they are
428 * asking for nonsense.
430 * Hmph. We may later want to allow "want" line that
431 * asks for something like "master~10" (symbolic)...
432 * would it make sense? I don't know.
434 o = lookup_object(sha1_buf);
435 if (!o || !(o->flags & OUR_REF))
436 die("git-upload-pack: not our ref %s", line+5);
437 if (!(o->flags & WANTED)) {
438 o->flags |= WANTED;
439 needs++;
444 static int send_ref(const char *refname, const unsigned char *sha1)
446 static const char *capabilities = "multi_ack thin-pack side-band";
447 struct object *o = parse_object(sha1);
449 if (!o)
450 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
452 if (capabilities)
453 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
454 0, capabilities);
455 else
456 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
457 capabilities = NULL;
458 if (!(o->flags & OUR_REF)) {
459 o->flags |= OUR_REF;
460 nr_our_refs++;
462 if (o->type == TYPE_TAG) {
463 o = deref_tag(o, refname, 0);
464 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
466 return 0;
469 static int upload_pack(void)
471 reset_timeout();
472 head_ref(send_ref);
473 for_each_ref(send_ref);
474 packet_flush(1);
475 nr_needs = receive_needs();
476 if (!nr_needs)
477 return 0;
478 get_common_commits();
479 create_pack_file();
480 return 0;
483 int main(int argc, char **argv)
485 char *dir;
486 int i;
487 int strict = 0;
489 for (i = 1; i < argc; i++) {
490 char *arg = argv[i];
492 if (arg[0] != '-')
493 break;
494 if (!strcmp(arg, "--strict")) {
495 strict = 1;
496 continue;
498 if (!strncmp(arg, "--timeout=", 10)) {
499 timeout = atoi(arg+10);
500 continue;
502 if (!strcmp(arg, "--")) {
503 i++;
504 break;
508 if (i != argc-1)
509 usage(upload_pack_usage);
510 dir = argv[i];
512 if (!enter_repo(dir, strict))
513 die("'%s': unable to chdir or not a git archive", dir);
515 upload_pack();
516 return 0;