Code

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