Prepare larger packet buffer for upload-pack protocol.
[git/dkf.git] / upload-pack.c
blobb673d8cb9748de0758d1c5269178e70a018f43a1
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 "sideband.h"
8 #include "tag.h"
9 #include "object.h"
10 #include "commit.h"
11 #include "exec_cmd.h"
13 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
15 #define THEY_HAVE (1U << 0)
16 #define OUR_REF (1U << 1)
17 #define WANTED (1U << 2)
18 static int multi_ack, nr_our_refs;
19 static int use_thin_pack;
20 static struct object_array have_obj;
21 static struct object_array want_obj;
22 static unsigned int timeout;
23 /* 0 for no sideband,
24 * otherwise maximum packet size (up to 65520 bytes).
26 static int use_sideband;
28 static void reset_timeout(void)
30 alarm(timeout);
33 static int strip(char *line, int len)
35 if (len && line[len-1] == '\n')
36 line[--len] = 0;
37 return len;
40 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
42 if (use_sideband)
43 return send_sideband(1, fd, data, sz, use_sideband);
44 if (fd == 3)
45 /* emergency quit */
46 fd = 2;
47 if (fd == 2) {
48 xwrite(fd, data, sz);
49 return sz;
51 return safe_write(fd, data, sz);
54 static void create_pack_file(void)
56 /* Pipes between rev-list to pack-objects, pack-objects to us
57 * and pack-objects error stream for progress bar.
59 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
60 pid_t pid_rev_list, pid_pack_objects;
61 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
62 char data[8193], progress[128];
63 char abort_msg[] = "aborting due to possible repository "
64 "corruption on the remote side.";
65 int buffered = -1;
67 if (pipe(lp_pipe) < 0)
68 die("git-upload-pack: unable to create pipe");
69 pid_rev_list = fork();
70 if (pid_rev_list < 0)
71 die("git-upload-pack: unable to fork git-rev-list");
73 if (!pid_rev_list) {
74 int i;
75 int args;
76 const char **argv;
77 const char **p;
78 char *buf;
80 if (create_full_pack) {
81 args = 10;
82 use_thin_pack = 0; /* no point doing it */
84 else
85 args = have_obj.nr + want_obj.nr + 5;
86 p = xmalloc(args * sizeof(char *));
87 argv = (const char **) p;
88 buf = xmalloc(args * 45);
90 dup2(lp_pipe[1], 1);
91 close(0);
92 close(lp_pipe[0]);
93 close(lp_pipe[1]);
94 *p++ = "rev-list";
95 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
96 if (create_full_pack)
97 *p++ = "--all";
98 else {
99 for (i = 0; i < want_obj.nr; i++) {
100 struct object *o = want_obj.objects[i].item;
101 *p++ = buf;
102 memcpy(buf, sha1_to_hex(o->sha1), 41);
103 buf += 41;
106 if (!create_full_pack)
107 for (i = 0; i < have_obj.nr; i++) {
108 struct object *o = have_obj.objects[i].item;
109 *p++ = buf;
110 *buf++ = '^';
111 memcpy(buf, sha1_to_hex(o->sha1), 41);
112 buf += 41;
114 *p++ = NULL;
115 execv_git_cmd(argv);
116 die("git-upload-pack: unable to exec git-rev-list");
119 if (pipe(pu_pipe) < 0)
120 die("git-upload-pack: unable to create pipe");
121 if (pipe(pe_pipe) < 0)
122 die("git-upload-pack: unable to create pipe");
123 pid_pack_objects = fork();
124 if (pid_pack_objects < 0) {
125 /* daemon sets things up to ignore TERM */
126 kill(pid_rev_list, SIGKILL);
127 die("git-upload-pack: unable to fork git-pack-objects");
129 if (!pid_pack_objects) {
130 dup2(lp_pipe[0], 0);
131 dup2(pu_pipe[1], 1);
132 dup2(pe_pipe[1], 2);
134 close(lp_pipe[0]);
135 close(lp_pipe[1]);
136 close(pu_pipe[0]);
137 close(pu_pipe[1]);
138 close(pe_pipe[0]);
139 close(pe_pipe[1]);
140 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
141 kill(pid_rev_list, SIGKILL);
142 die("git-upload-pack: unable to exec git-pack-objects");
145 close(lp_pipe[0]);
146 close(lp_pipe[1]);
148 /* We read from pe_pipe[0] to capture stderr output for
149 * progress bar, and pu_pipe[0] to capture the pack data.
151 close(pe_pipe[1]);
152 close(pu_pipe[1]);
154 while (1) {
155 const char *who;
156 struct pollfd pfd[2];
157 pid_t pid;
158 int status;
159 ssize_t sz;
160 int pe, pu, pollsize;
162 reset_timeout();
164 pollsize = 0;
165 pe = pu = -1;
167 if (0 <= pu_pipe[0]) {
168 pfd[pollsize].fd = pu_pipe[0];
169 pfd[pollsize].events = POLLIN;
170 pu = pollsize;
171 pollsize++;
173 if (0 <= pe_pipe[0]) {
174 pfd[pollsize].fd = pe_pipe[0];
175 pfd[pollsize].events = POLLIN;
176 pe = pollsize;
177 pollsize++;
180 if (pollsize) {
181 if (poll(pfd, pollsize, -1) < 0) {
182 if (errno != EINTR) {
183 error("poll failed, resuming: %s",
184 strerror(errno));
185 sleep(1);
187 continue;
189 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
190 /* Data ready; we keep the last byte
191 * to ourselves in case we detect
192 * broken rev-list, so that we can
193 * leave the stream corrupted. This
194 * is unfortunate -- unpack-objects
195 * would happily accept a valid pack
196 * data with trailing garbage, so
197 * appending garbage after we pass all
198 * the pack data is not good enough to
199 * signal breakage to downstream.
201 char *cp = data;
202 ssize_t outsz = 0;
203 if (0 <= buffered) {
204 *cp++ = buffered;
205 outsz++;
207 sz = read(pu_pipe[0], cp,
208 sizeof(data) - outsz);
209 if (0 < sz)
211 else if (sz == 0) {
212 close(pu_pipe[0]);
213 pu_pipe[0] = -1;
215 else
216 goto fail;
217 sz += outsz;
218 if (1 < sz) {
219 buffered = data[sz-1] & 0xFF;
220 sz--;
222 else
223 buffered = -1;
224 sz = send_client_data(1, data, sz);
225 if (sz < 0)
226 goto fail;
228 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
229 /* Status ready; we ship that in the side-band
230 * or dump to the standard error.
232 sz = read(pe_pipe[0], progress,
233 sizeof(progress));
234 if (0 < sz)
235 send_client_data(2, progress, sz);
236 else if (sz == 0) {
237 close(pe_pipe[0]);
238 pe_pipe[0] = -1;
240 else
241 goto fail;
245 /* See if the children are still there */
246 if (pid_rev_list || pid_pack_objects) {
247 pid = waitpid(-1, &status, WNOHANG);
248 if (!pid)
249 continue;
250 who = ((pid == pid_rev_list) ? "git-rev-list" :
251 (pid == pid_pack_objects) ? "git-pack-objects" :
252 NULL);
253 if (!who) {
254 if (pid < 0) {
255 error("git-upload-pack: %s",
256 strerror(errno));
257 goto fail;
259 error("git-upload-pack: we weren't "
260 "waiting for %d", pid);
261 continue;
263 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
264 error("git-upload-pack: %s died with error.",
265 who);
266 goto fail;
268 if (pid == pid_rev_list)
269 pid_rev_list = 0;
270 if (pid == pid_pack_objects)
271 pid_pack_objects = 0;
272 if (pid_rev_list || pid_pack_objects)
273 continue;
276 /* both died happily */
277 if (pollsize)
278 continue;
280 /* flush the data */
281 if (0 <= buffered) {
282 data[0] = buffered;
283 sz = send_client_data(1, data, 1);
284 if (sz < 0)
285 goto fail;
286 fprintf(stderr, "flushed.\n");
288 if (use_sideband)
289 packet_flush(1);
290 return;
292 fail:
293 if (pid_pack_objects)
294 kill(pid_pack_objects, SIGKILL);
295 if (pid_rev_list)
296 kill(pid_rev_list, SIGKILL);
297 send_client_data(3, abort_msg, sizeof(abort_msg));
298 die("git-upload-pack: %s", abort_msg);
301 static int got_sha1(char *hex, unsigned char *sha1)
303 struct object *o;
305 if (get_sha1_hex(hex, sha1))
306 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
307 if (!has_sha1_file(sha1))
308 return 0;
310 o = lookup_object(sha1);
311 if (!(o && o->parsed))
312 o = parse_object(sha1);
313 if (!o)
314 die("oops (%s)", sha1_to_hex(sha1));
315 if (o->type == OBJ_COMMIT) {
316 struct commit_list *parents;
317 if (o->flags & THEY_HAVE)
318 return 0;
319 o->flags |= THEY_HAVE;
320 for (parents = ((struct commit*)o)->parents;
321 parents;
322 parents = parents->next)
323 parents->item->object.flags |= THEY_HAVE;
325 add_object_array(o, NULL, &have_obj);
326 return 1;
329 static int get_common_commits(void)
331 static char line[1000];
332 unsigned char sha1[20], last_sha1[20];
333 int len;
335 track_object_refs = 0;
336 save_commit_buffer = 0;
338 for(;;) {
339 len = packet_read_line(0, line, sizeof(line));
340 reset_timeout();
342 if (!len) {
343 if (have_obj.nr == 0 || multi_ack)
344 packet_write(1, "NAK\n");
345 continue;
347 len = strip(line, len);
348 if (!strncmp(line, "have ", 5)) {
349 if (got_sha1(line+5, sha1) &&
350 (multi_ack || have_obj.nr == 1)) {
351 packet_write(1, "ACK %s%s\n",
352 sha1_to_hex(sha1),
353 multi_ack ? " continue" : "");
354 if (multi_ack)
355 hashcpy(last_sha1, sha1);
357 continue;
359 if (!strcmp(line, "done")) {
360 if (have_obj.nr > 0) {
361 if (multi_ack)
362 packet_write(1, "ACK %s\n",
363 sha1_to_hex(last_sha1));
364 return 0;
366 packet_write(1, "NAK\n");
367 return -1;
369 die("git-upload-pack: expected SHA1 list, got '%s'", line);
373 static void receive_needs(void)
375 static char line[1000];
376 int len;
378 for (;;) {
379 struct object *o;
380 unsigned char sha1_buf[20];
381 len = packet_read_line(0, line, sizeof(line));
382 reset_timeout();
383 if (!len)
384 return;
386 if (strncmp("want ", line, 5) ||
387 get_sha1_hex(line+5, sha1_buf))
388 die("git-upload-pack: protocol error, "
389 "expected to get sha, not '%s'", line);
390 if (strstr(line+45, "multi_ack"))
391 multi_ack = 1;
392 if (strstr(line+45, "thin-pack"))
393 use_thin_pack = 1;
394 if (strstr(line+45, "side-band-64k"))
395 use_sideband = LARGE_PACKET_MAX;
396 else if (strstr(line+45, "side-band"))
397 use_sideband = DEFAULT_PACKET_MAX;
399 /* We have sent all our refs already, and the other end
400 * should have chosen out of them; otherwise they are
401 * asking for nonsense.
403 * Hmph. We may later want to allow "want" line that
404 * asks for something like "master~10" (symbolic)...
405 * would it make sense? I don't know.
407 o = lookup_object(sha1_buf);
408 if (!o || !(o->flags & OUR_REF))
409 die("git-upload-pack: not our ref %s", line+5);
410 if (!(o->flags & WANTED)) {
411 o->flags |= WANTED;
412 add_object_array(o, NULL, &want_obj);
417 static int send_ref(const char *refname, const unsigned char *sha1)
419 static const char *capabilities = "multi_ack thin-pack side-band side-band-64k";
420 struct object *o = parse_object(sha1);
422 if (!o)
423 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
425 if (capabilities)
426 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
427 0, capabilities);
428 else
429 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
430 capabilities = NULL;
431 if (!(o->flags & OUR_REF)) {
432 o->flags |= OUR_REF;
433 nr_our_refs++;
435 if (o->type == OBJ_TAG) {
436 o = deref_tag(o, refname, 0);
437 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
439 return 0;
442 static void upload_pack(void)
444 reset_timeout();
445 head_ref(send_ref);
446 for_each_ref(send_ref);
447 packet_flush(1);
448 receive_needs();
449 if (want_obj.nr) {
450 get_common_commits();
451 create_pack_file();
455 int main(int argc, char **argv)
457 char *dir;
458 int i;
459 int strict = 0;
461 for (i = 1; i < argc; i++) {
462 char *arg = argv[i];
464 if (arg[0] != '-')
465 break;
466 if (!strcmp(arg, "--strict")) {
467 strict = 1;
468 continue;
470 if (!strncmp(arg, "--timeout=", 10)) {
471 timeout = atoi(arg+10);
472 continue;
474 if (!strcmp(arg, "--")) {
475 i++;
476 break;
480 if (i != argc-1)
481 usage(upload_pack_usage);
482 dir = argv[i];
484 if (!enter_repo(dir, strict))
485 die("'%s': unable to chdir or not a git archive", dir);
487 upload_pack();
488 return 0;