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