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