Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/syslimits.h>
24 #include <stdint.h>
25 #include <errno.h>
26 #include <imsg.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sha1.h>
34 #include <fcntl.h>
35 #include <zlib.h>
36 #include <err.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_version.h"
42 #include "got_fetch.h"
43 #include "got_reference.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_pack.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct got_object *indexed;
57 static int chattygot;
58 static struct got_object_id zhash = {.sha1={0}};
60 static const struct got_error *
61 readn(ssize_t *off, int fd, void *buf, size_t n)
62 {
63 ssize_t r;
65 *off = 0;
66 while (*off != n) {
67 r = read(fd, buf + *off, n - *off);
68 if (r == -1)
69 return got_error_from_errno("read");
70 if (r == 0)
71 return NULL;
72 *off += r;
73 }
74 return NULL;
75 }
77 static const struct got_error *
78 flushpkt(int fd)
79 {
80 ssize_t w;
82 if (chattygot)
83 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
85 w = write(fd, "0000", 4);
86 if (w == -1)
87 return got_error_from_errno("write");
88 if (w != 4)
89 return got_error(GOT_ERR_IO);
90 return NULL;
91 }
93 /*
94 * Packet header contains a 4-byte hexstring which specifies the length
95 * of data which follows.
96 */
97 static const struct got_error *
98 read_pkthdr(int *datalen, int fd)
99 {
100 static const struct got_error *err = NULL;
101 char lenstr[5];
102 long len;
103 char *e;
104 int n, i;
105 ssize_t r;
107 *datalen = 0;
109 err = readn(&r, fd, lenstr, 4);
110 if (err)
111 return err;
112 if (r == 0) {
113 /* implicit "0000" */
114 if (chattygot)
115 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 return NULL;
118 if (r != 4)
119 return got_error_msg(GOT_ERR_BAD_PACKET,
120 "wrong packet header length");
122 lenstr[4] = '\0';
123 for (i = 0; i < 4; i++) {
124 if (!isprint((unsigned char)lenstr[i]))
125 return got_error_msg(GOT_ERR_BAD_PACKET,
126 "unprintable character in packet length field");
128 for (i = 0; i < 4; i++) {
129 if (!isxdigit((unsigned char)lenstr[i])) {
130 if (chattygot)
131 fprintf(stderr, "%s: bad length: '%s'\n",
132 getprogname(), lenstr);
133 return got_error_msg(GOT_ERR_BAD_PACKET,
134 "packet length not specified in hex");
137 errno = 0;
138 len = strtol(lenstr, &e, 16);
139 if (lenstr[0] == '\0' || *e != '\0')
140 return got_error(GOT_ERR_BAD_PACKET);
141 if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 if (len > INT_MAX || len < INT_MIN)
144 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
145 n = len;
146 if (n == 0)
147 return NULL;
148 if (n <= 4)
149 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
150 n -= 4;
152 *datalen = n;
153 return NULL;
156 static const struct got_error *
157 readpkt(int *outlen, int fd, char *buf, int buflen)
159 const struct got_error *err = NULL;
160 int datalen, i;
161 ssize_t n;
163 err = read_pkthdr(&datalen, fd);
164 if (err)
165 return err;
167 if (datalen > buflen)
168 return got_error(GOT_ERR_NO_SPACE);
170 err = readn(&n, fd, buf, datalen);
171 if (err)
172 return err;
173 if (n != datalen)
174 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
176 if (chattygot) {
177 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 for (i = 0; i < n; i++) {
179 if (isprint(buf[i]))
180 fputc(buf[i], stderr);
181 else
182 fprintf(stderr, "[0x%.2x]", buf[i]);
184 fputc('\n', stderr);
187 *outlen = n;
188 return NULL;
191 static const struct got_error *
192 writepkt(int fd, char *buf, int nbuf)
194 char len[5];
195 int i;
196 ssize_t w;
198 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 return got_error(GOT_ERR_NO_SPACE);
200 w = write(fd, len, 4);
201 if (w == -1)
202 return got_error_from_errno("write");
203 if (w != 4)
204 return got_error(GOT_ERR_IO);
205 w = write(fd, buf, nbuf);
206 if (w == -1)
207 return got_error_from_errno("write");
208 if (w != nbuf)
209 return got_error(GOT_ERR_IO);
210 if (chattygot) {
211 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 for (i = 0; i < nbuf; i++) {
213 if (isprint(buf[i]))
214 fputc(buf[i], stderr);
215 else
216 fprintf(stderr, "[0x%.2x]", buf[i]);
218 fputc('\n', stderr);
220 return NULL;
223 static void
224 match_remote_ref(struct got_pathlist_head *have_refs,
225 struct got_object_id *my_id, char *refname)
227 struct got_pathlist_entry *pe;
229 /* XXX zero-hash signifies we don't have this ref;
230 * we should use a flag instead */
231 memset(my_id, 0, sizeof(*my_id));
233 TAILQ_FOREACH(pe, have_refs, entry) {
234 struct got_object_id *id = pe->data;
235 if (strcmp(pe->path, refname) == 0) {
236 memcpy(my_id, id, sizeof(*my_id));
237 break;
242 static int
243 match_branch(const char *branch, const char *wanted_branch)
245 if (strncmp(branch, "refs/heads/", 11) != 0)
246 return 0;
248 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
249 wanted_branch += 11;
251 return (strcmp(branch + 11, wanted_branch) == 0);
254 static const struct got_error *
255 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
257 const struct got_error *err = NULL;
258 char *p;
259 size_t i, n = 0;
261 for (i = 0; i < maxtokens; i++)
262 tokens[i] = NULL;
264 for (i = 0; n < len && i < maxtokens; i++) {
265 while (isspace(*line)) {
266 line++;
267 n++;
269 p = line;
270 while (*line != '\0' &&
271 (!isspace(*line) || i == maxtokens - 1)) {
272 line++;
273 n++;
275 tokens[i] = strndup(p, line - p);
276 if (tokens[i] == NULL) {
277 err = got_error_from_errno("strndup");
278 goto done;
280 /* Skip \0 field-delimiter at end of token. */
281 while (line[0] == '\0' && n < len) {
282 line++;
283 n++;
286 if (i <= 2)
287 err = got_error(GOT_ERR_NOT_REF);
288 done:
289 if (err) {
290 int j;
291 for (j = 0; j < i; j++)
292 free(tokens[j]);
293 tokens[j] = NULL;
295 return err;
298 static const struct got_error *
299 parse_refline(char **id_str, char **refname, char **server_capabilities,
300 char *line, int len)
302 const struct got_error *err = NULL;
303 char *tokens[3];
305 err = tokenize_refline(tokens, line, len, nitems(tokens));
306 if (err)
307 return err;
309 if (tokens[0])
310 *id_str = tokens[0];
311 if (tokens[1])
312 *refname = tokens[1];
313 if (tokens[2])
314 *server_capabilities = tokens[2];
316 return NULL;
319 #define GOT_CAPA_AGENT "agent"
320 #define GOT_CAPA_OFS_DELTA "ofs-delta"
321 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
323 #define GOT_SIDEBAND_PACKFILE_DATA 1
324 #define GOT_SIDEBAND_PROGRESS_INFO 2
325 #define GOT_SIDEBAND_ERROR_INFO 3
328 struct got_capability {
329 const char *key;
330 const char *value;
331 };
332 static const struct got_capability got_capabilities[] = {
333 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
334 { GOT_CAPA_OFS_DELTA, NULL },
335 { GOT_CAPA_SIDE_BAND_64K, NULL },
336 };
338 static const struct got_error *
339 match_capability(char **my_capabilities, const char *capa,
340 const struct got_capability *mycapa)
342 char *equalsign;
343 char *s;
345 equalsign = strchr(capa, '=');
346 if (equalsign) {
347 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
348 return NULL;
349 } else {
350 if (strcmp(capa, mycapa->key) != 0)
351 return NULL;
354 if (asprintf(&s, "%s %s%s%s",
355 *my_capabilities != NULL ? *my_capabilities : "",
356 mycapa->key,
357 mycapa->value != NULL ? "=" : "",
358 mycapa->value != NULL? mycapa->value : "") == -1)
359 return got_error_from_errno("asprintf");
361 free(*my_capabilities);
362 *my_capabilities = s;
363 return NULL;
366 static const struct got_error *
367 add_symref(struct got_pathlist_head *symrefs, char *capa)
369 const struct got_error *err = NULL;
370 char *colon, *name = NULL, *target = NULL;
372 /* Need at least "A:B" */
373 if (strlen(capa) < 3)
374 return NULL;
376 colon = strchr(capa, ':');
377 if (colon == NULL)
378 return NULL;
380 *colon = '\0';
381 name = strdup(capa);
382 if (name == NULL)
383 return got_error_from_errno("strdup");
385 target = strdup(colon + 1);
386 if (target == NULL) {
387 err = got_error_from_errno("strdup");
388 goto done;
391 /* We can't validate the ref itself here. The main process will. */
392 err = got_pathlist_append(symrefs, name, target);
393 done:
394 if (err) {
395 free(name);
396 free(target);
398 return err;
401 static const struct got_error *
402 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
403 char *server_capabilities)
405 const struct got_error *err = NULL;
406 char *capa, *equalsign;
407 int i;
409 *my_capabilities = NULL;
410 do {
411 capa = strsep(&server_capabilities, " ");
412 if (capa == NULL)
413 return NULL;
415 equalsign = strchr(capa, '=');
416 if (equalsign != NULL &&
417 strncmp(capa, "symref", equalsign - capa) == 0) {
418 err = add_symref(symrefs, equalsign + 1);
419 if (err)
420 break;
421 continue;
424 for (i = 0; i < nitems(got_capabilities); i++) {
425 err = match_capability(my_capabilities,
426 capa, &got_capabilities[i]);
427 if (err)
428 break;
430 } while (capa);
432 if (*my_capabilities == NULL) {
433 *my_capabilities = strdup("");
434 if (*my_capabilities == NULL)
435 err = got_error_from_errno("strdup");
437 return err;
440 static const struct got_error *
441 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
443 int i;
445 if (len == 0)
446 return NULL;
448 /*
449 * Truncate messages which exceed the maximum imsg payload size.
450 * Server may send up to 64k.
451 */
452 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
453 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
455 /* Only allow printable ASCII. */
456 for (i = 0; i < len; i++) {
457 if (isprint((unsigned char)buf[i]) ||
458 isspace((unsigned char)buf[i]))
459 continue;
460 return got_error_msg(GOT_ERR_BAD_PACKET,
461 "non-printable progress message received from server");
464 return got_privsep_send_fetch_server_progress(ibuf, buf, len);
467 static const struct got_error *
468 fetch_error(const char *buf, size_t len)
470 static char msg[1024];
471 int i;
473 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
474 if (!isprint(buf[i]))
475 return got_error_msg(GOT_ERR_BAD_PACKET,
476 "non-printable error message received from server");
477 msg[i] = buf[i];
479 msg[i] = '\0';
480 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
483 static const struct got_error *
484 fetch_pack(int fd, int packfd, struct got_object_id *packid,
485 struct got_pathlist_head *have_refs, int fetch_all_branches,
486 struct imsgbuf *ibuf)
488 const struct got_error *err = NULL;
489 char buf[GOT_FETCH_PKTMAX];
490 char hashstr[SHA1_DIGEST_STRING_LENGTH];
491 struct got_object_id *have, *want;
492 int is_firstpkt = 1, nref = 0, refsz = 16;
493 int i, n, nwant = 0, nhave = 0, acked = 0;
494 off_t packsz = 0, last_reported_packsz = 0;
495 char *id_str = NULL, *refname = NULL;
496 char *server_capabilities = NULL, *my_capabilities = NULL;
497 const char *default_branch = NULL;
498 struct got_pathlist_head symrefs;
499 struct got_pathlist_entry *pe;
500 int sent_my_capabilites = 0, have_sidebands = 0;
501 int found_branch = 0;
503 TAILQ_INIT(&symrefs);
505 have = malloc(refsz * sizeof(have[0]));
506 if (have == NULL)
507 return got_error_from_errno("malloc");
508 want = malloc(refsz * sizeof(want[0]));
509 if (want == NULL) {
510 err = got_error_from_errno("malloc");
511 goto done;
513 if (chattygot)
514 fprintf(stderr, "%s: starting fetch\n", getprogname());
515 while (1) {
516 err = readpkt(&n, fd, buf, sizeof(buf));
517 if (err)
518 goto done;
519 if (n == 0)
520 break;
521 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
522 err = fetch_error(&buf[4], n - 4);
523 goto done;
525 err = parse_refline(&id_str, &refname, &server_capabilities,
526 buf, n);
527 if (err)
528 goto done;
529 if (is_firstpkt) {
530 if (chattygot && server_capabilities[0] != '\0')
531 fprintf(stderr, "%s: server capabilities: %s\n",
532 getprogname(), server_capabilities);
533 err = match_capabilities(&my_capabilities, &symrefs,
534 server_capabilities);
535 if (err)
536 goto done;
537 if (chattygot)
538 fprintf(stderr, "%s: my capabilities: %s\n",
539 getprogname(), my_capabilities);
540 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
541 if (err)
542 goto done;
543 is_firstpkt = 0;
544 if (!fetch_all_branches) {
545 TAILQ_FOREACH(pe, &symrefs, entry) {
546 const char *name = pe->path;
547 const char *symref_target = pe->data;
548 if (strcmp(name, GOT_REF_HEAD) != 0)
549 continue;
550 default_branch = symref_target;
551 break;
554 continue;
556 if (strstr(refname, "^{}"))
557 continue;
559 if (chattygot)
560 fprintf(stderr, "%s: discovered remote ref %s\n",
561 getprogname(), refname);
563 if (strncmp(refname, "refs/heads/", 11) == 0) {
564 if (default_branch != NULL &&
565 !match_branch(refname, default_branch))
566 continue;
567 found_branch = 1;
568 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
569 if (chattygot) {
570 fprintf(stderr, "%s: ignoring '%s' which is "
571 "neither a branch nor a tag\n",
572 getprogname(), refname);
574 continue;
577 if (refsz == nref + 1) {
578 refsz *= 2;
579 have = reallocarray(have, refsz, sizeof(have[0]));
580 if (have == NULL) {
581 err = got_error_from_errno("reallocarray");
582 goto done;
584 want = reallocarray(want, refsz, sizeof(want[0]));
585 if (want == NULL) {
586 err = got_error_from_errno("reallocarray");
587 goto done;
590 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
591 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
592 goto done;
594 match_remote_ref(have_refs, &have[nref], refname);
595 err = got_privsep_send_fetch_ref(ibuf, &want[nref],
596 refname);
597 if (err)
598 goto done;
600 if (chattygot) {
601 char *theirs, *mine;
602 err = got_object_id_str(&theirs, &want[nref]);
603 if (err)
604 goto done;
605 err = got_object_id_str(&mine, &have[nref]);
606 if (err) {
607 free(theirs);
608 goto done;
610 fprintf(stderr, "%s: %s will be fetched\n",
611 getprogname(), refname);
612 fprintf(stderr, "%s: theirs=%s\n%s: mine=%s\n",
613 getprogname(), theirs, getprogname(), mine);
614 free(theirs);
615 free(mine);
617 nref++;
620 /* Abort if we haven't found any branch to fetch. */
621 if (!found_branch) {
622 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
623 goto done;
626 for (i = 0; i < nref; i++) {
627 if (got_object_id_cmp(&have[i], &want[i]) == 0)
628 continue;
629 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
630 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
631 sent_my_capabilites ? "" : my_capabilities);
632 if (n >= sizeof(buf)) {
633 err = got_error(GOT_ERR_NO_SPACE);
634 goto done;
636 err = writepkt(fd, buf, n);
637 if (err)
638 goto done;
639 sent_my_capabilites = 1;
640 nwant++;
642 err = flushpkt(fd);
643 if (err)
644 goto done;
646 if (nwant == 0)
647 goto done;
649 for (i = 0; i < nref; i++) {
650 if (got_object_id_cmp(&have[i], &zhash) == 0)
651 continue;
652 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
653 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
654 if (n >= sizeof(buf)) {
655 err = got_error(GOT_ERR_NO_SPACE);
656 goto done;
658 err = writepkt(fd, buf, n);
659 if (err)
660 goto done;
661 nhave++;
664 while (nhave > 0 && !acked) {
665 struct got_object_id common_id;
667 /* The server should ACK the object IDs we need. */
668 err = readpkt(&n, fd, buf, sizeof(buf));
669 if (err)
670 goto done;
671 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
672 err = fetch_error(&buf[4], n - 4);
673 goto done;
675 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
676 /* Server has not located our objects yet. */
677 continue;
679 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
680 strncmp(buf, "ACK ", 4) != 0) {
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 "unexpected message from server");
683 goto done;
685 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
686 err = got_error_msg(GOT_ERR_BAD_PACKET,
687 "bad object ID in ACK packet from server");
688 goto done;
690 acked++;
693 n = snprintf(buf, sizeof(buf), "done\n");
694 err = writepkt(fd, buf, n);
695 if (err)
696 goto done;
698 if (nhave == 0) {
699 err = readpkt(&n, fd, buf, sizeof(buf));
700 if (err)
701 goto done;
702 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
703 err = got_error_msg(GOT_ERR_BAD_PACKET,
704 "unexpected message from server");
705 goto done;
709 if (chattygot)
710 fprintf(stderr, "%s: fetching...\n", getprogname());
712 if (my_capabilities != NULL &&
713 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
714 have_sidebands = 1;
716 while (1) {
717 ssize_t r = 0, w;
718 int datalen = -1;
720 if (have_sidebands) {
721 err = read_pkthdr(&datalen, fd);
722 if (err)
723 goto done;
724 if (datalen <= 0)
725 break;
727 /* Read sideband channel ID (one byte). */
728 r = read(fd, buf, 1);
729 if (r == -1) {
730 err = got_error_from_errno("read");
731 goto done;
733 if (r != 1) {
734 err = got_error_msg(GOT_ERR_BAD_PACKET,
735 "short packet");
736 goto done;
738 if (datalen > sizeof(buf) - 5) {
739 err = got_error_msg(GOT_ERR_BAD_PACKET,
740 "bad packet length");
741 goto done;
743 datalen--; /* sideband ID has been read */
744 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
745 /* Read packfile data. */
746 err = readn(&r, fd, buf, datalen);
747 if (err)
748 goto done;
749 if (r != datalen) {
750 err = got_error_msg(GOT_ERR_BAD_PACKET,
751 "packet too short");
752 goto done;
754 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
755 err = readn(&r, fd, buf, datalen);
756 if (err)
757 goto done;
758 if (r != datalen) {
759 err = got_error_msg(GOT_ERR_BAD_PACKET,
760 "packet too short");
761 goto done;
763 err = fetch_progress(ibuf, buf, r);
764 if (err)
765 goto done;
766 continue;
767 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
768 err = readn(&r, fd, buf, datalen);
769 if (err)
770 goto done;
771 if (r != datalen) {
772 err = got_error_msg(GOT_ERR_BAD_PACKET,
773 "packet too short");
774 goto done;
776 err = fetch_error(buf, r);
777 goto done;
778 } else {
779 err = got_error_msg(GOT_ERR_BAD_PACKET,
780 "unknown side-band received from server");
781 goto done;
783 } else {
784 /* No sideband channel. Every byte is packfile data. */
785 err = readn(&r, fd, buf, sizeof buf);
786 if (err)
787 goto done;
788 if (r <= 0)
789 break;
792 /* Write packfile data to temporary pack file. */
793 w = write(packfd, buf, r);
794 if (w == -1) {
795 err = got_error_from_errno("write");
796 goto done;
798 if (w != r) {
799 err = got_error(GOT_ERR_IO);
800 goto done;
802 packsz += w;
804 /* Don't send too many progress privsep messages. */
805 if (packsz > last_reported_packsz + 1024) {
806 err = got_privsep_send_fetch_download_progress(ibuf,
807 packsz);
808 if (err)
809 goto done;
810 last_reported_packsz = packsz;
813 err = got_privsep_send_fetch_download_progress(ibuf, packsz);
814 if (err)
815 goto done;
816 done:
817 TAILQ_FOREACH(pe, &symrefs, entry) {
818 free((void *)pe->path);
819 free(pe->data);
821 got_pathlist_free(&symrefs);
822 free(have);
823 free(want);
824 free(id_str);
825 free(refname);
826 free(server_capabilities);
827 return err;
831 int
832 main(int argc, char **argv)
834 const struct got_error *err = NULL;
835 int fetchfd, packfd = -1, i;
836 struct got_object_id packid;
837 struct imsgbuf ibuf;
838 struct imsg imsg;
839 struct got_pathlist_head have_refs;
840 struct got_pathlist_entry *pe;
841 struct got_imsg_fetch_request *fetch_req = NULL;
842 struct got_imsg_fetch_have_ref href;
843 size_t datalen, remain;
844 size_t offset;
845 #if 0
846 static int attached;
847 while (!attached)
848 sleep (1);
849 #endif
851 TAILQ_INIT(&have_refs);
853 if (getenv("GOT_FETCH_DEBUG") != NULL) {
854 fprintf(stderr, "%s being chatty!\n", getprogname());
855 chattygot = 1;
858 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
859 #ifndef PROFILE
860 /* revoke access to most system calls */
861 if (pledge("stdio recvfd", NULL) == -1) {
862 err = got_error_from_errno("pledge");
863 got_privsep_send_error(&ibuf, err);
864 return 1;
866 #endif
867 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
868 if (err->code == GOT_ERR_PRIVSEP_PIPE)
869 err = NULL;
870 goto done;
872 if (imsg.hdr.type == GOT_IMSG_STOP)
873 goto done;
874 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
875 err = got_error(GOT_ERR_PRIVSEP_MSG);
876 goto done;
878 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
879 if (datalen < sizeof(struct got_imsg_fetch_request)) {
880 err = got_error(GOT_ERR_PRIVSEP_LEN);
881 goto done;
883 fetch_req = (struct got_imsg_fetch_request *)imsg.data;
884 if (datalen < sizeof(*fetch_req) +
885 sizeof(struct got_imsg_fetch_have_ref) *
886 fetch_req->n_have_refs) {
887 err = got_error(GOT_ERR_PRIVSEP_LEN);
888 goto done;
890 offset = sizeof(*fetch_req);
891 remain = datalen;
892 for (i = 0; i < fetch_req->n_have_refs; i++) {
893 struct got_object_id *id;
894 char *refname;
896 if (remain < sizeof(href) || offset > datalen) {
897 err = got_error(GOT_ERR_PRIVSEP_LEN);
898 goto done;
900 memcpy(&href, imsg.data + offset, sizeof(href));
901 remain -= sizeof(href);
902 if (remain < href.name_len) {
903 err = got_error(GOT_ERR_PRIVSEP_LEN);
904 goto done;
906 remain -= href.name_len;
907 refname = malloc(href.name_len + 1);
908 if (refname == NULL) {
909 err = got_error_from_errno("malloc");
910 goto done;
912 offset += sizeof(href);
913 memcpy(refname, imsg.data + offset, href.name_len);
914 refname[href.name_len] = '\0';
915 offset += href.name_len;
917 id = malloc(sizeof(*id));
918 if (id == NULL) {
919 free(refname);
920 err = got_error_from_errno("malloc");
921 goto done;
923 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
924 err = got_pathlist_append(&have_refs, refname, id);
925 if (err) {
926 free(refname);
927 free(id);
928 goto done;
931 fetchfd = imsg.fd;
933 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
935 err = NULL;
936 goto done;
938 if (imsg.hdr.type == GOT_IMSG_STOP)
939 goto done;
940 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 goto done;
944 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
946 goto done;
948 packfd = imsg.fd;
950 err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
951 fetch_req->fetch_all_branches, &ibuf);
952 done:
953 TAILQ_FOREACH(pe, &have_refs, entry) {
954 free((char *)pe->path);
955 free(pe->data);
957 got_pathlist_free(&have_refs);
958 if (packfd != -1 && close(packfd) == -1 && err == NULL)
959 err = got_error_from_errno("close");
960 if (err != NULL)
961 got_privsep_send_error(&ibuf, err);
962 else
963 err = got_privsep_send_fetch_done(&ibuf, packid);
964 if (err != NULL) {
965 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
966 got_privsep_send_error(&ibuf, err);
969 exit(0);