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 > 1)
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 > 1)
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 > 1) {
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 > 1) {
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 int
255 match_wanted_ref(const char *refname, const char *wanted_ref)
257 if (strncmp(refname, "refs/", 5) != 0)
258 return 0;
259 refname += 5;
261 /*
262 * Prevent fetching of references that won't make any
263 * sense outside of the remote repository's context.
264 */
265 if (strncmp(refname, "got/", 4) == 0)
266 return 0;
267 if (strncmp(refname, "remotes/", 8) == 0)
268 return 0;
270 if (strncmp(wanted_ref, "refs/", 5) == 0)
271 wanted_ref += 5;
273 /* Allow prefix match. */
274 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
275 return 1;
277 /* Allow exact match. */
278 return (strcmp(refname, wanted_ref) == 0);
281 static const struct got_error *
282 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
284 const struct got_error *err = NULL;
285 char *p;
286 size_t i, n = 0;
288 for (i = 0; i < maxtokens; i++)
289 tokens[i] = NULL;
291 for (i = 0; n < len && i < maxtokens; i++) {
292 while (isspace(*line)) {
293 line++;
294 n++;
296 p = line;
297 while (*line != '\0' &&
298 (!isspace(*line) || i == maxtokens - 1)) {
299 line++;
300 n++;
302 tokens[i] = strndup(p, line - p);
303 if (tokens[i] == NULL) {
304 err = got_error_from_errno("strndup");
305 goto done;
307 /* Skip \0 field-delimiter at end of token. */
308 while (line[0] == '\0' && n < len) {
309 line++;
310 n++;
313 if (i <= 2)
314 err = got_error(GOT_ERR_NOT_REF);
315 done:
316 if (err) {
317 int j;
318 for (j = 0; j < i; j++)
319 free(tokens[j]);
320 tokens[j] = NULL;
322 return err;
325 static const struct got_error *
326 parse_refline(char **id_str, char **refname, char **server_capabilities,
327 char *line, int len)
329 const struct got_error *err = NULL;
330 char *tokens[3];
332 err = tokenize_refline(tokens, line, len, nitems(tokens));
333 if (err)
334 return err;
336 if (tokens[0])
337 *id_str = tokens[0];
338 if (tokens[1])
339 *refname = tokens[1];
340 if (tokens[2]) {
341 char *p;
342 *server_capabilities = tokens[2];
343 p = strrchr(*server_capabilities, '\n');
344 if (p)
345 *p = '\0';
348 return NULL;
351 #define GOT_CAPA_AGENT "agent"
352 #define GOT_CAPA_OFS_DELTA "ofs-delta"
353 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
355 #define GOT_SIDEBAND_PACKFILE_DATA 1
356 #define GOT_SIDEBAND_PROGRESS_INFO 2
357 #define GOT_SIDEBAND_ERROR_INFO 3
360 struct got_capability {
361 const char *key;
362 const char *value;
363 };
364 static const struct got_capability got_capabilities[] = {
365 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
366 { GOT_CAPA_OFS_DELTA, NULL },
367 { GOT_CAPA_SIDE_BAND_64K, NULL },
368 };
370 static const struct got_error *
371 match_capability(char **my_capabilities, const char *capa,
372 const struct got_capability *mycapa)
374 char *equalsign;
375 char *s;
377 equalsign = strchr(capa, '=');
378 if (equalsign) {
379 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
380 return NULL;
381 } else {
382 if (strcmp(capa, mycapa->key) != 0)
383 return NULL;
386 if (asprintf(&s, "%s %s%s%s",
387 *my_capabilities != NULL ? *my_capabilities : "",
388 mycapa->key,
389 mycapa->value != NULL ? "=" : "",
390 mycapa->value != NULL? mycapa->value : "") == -1)
391 return got_error_from_errno("asprintf");
393 free(*my_capabilities);
394 *my_capabilities = s;
395 return NULL;
398 static const struct got_error *
399 add_symref(struct got_pathlist_head *symrefs, char *capa)
401 const struct got_error *err = NULL;
402 char *colon, *name = NULL, *target = NULL;
404 /* Need at least "A:B" */
405 if (strlen(capa) < 3)
406 return NULL;
408 colon = strchr(capa, ':');
409 if (colon == NULL)
410 return NULL;
412 *colon = '\0';
413 name = strdup(capa);
414 if (name == NULL)
415 return got_error_from_errno("strdup");
417 target = strdup(colon + 1);
418 if (target == NULL) {
419 err = got_error_from_errno("strdup");
420 goto done;
423 /* We can't validate the ref itself here. The main process will. */
424 err = got_pathlist_append(symrefs, name, target);
425 done:
426 if (err) {
427 free(name);
428 free(target);
430 return err;
433 static const struct got_error *
434 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
435 char *server_capabilities)
437 const struct got_error *err = NULL;
438 char *capa, *equalsign;
439 int i;
441 *my_capabilities = NULL;
442 do {
443 capa = strsep(&server_capabilities, " ");
444 if (capa == NULL)
445 return NULL;
447 equalsign = strchr(capa, '=');
448 if (equalsign != NULL &&
449 strncmp(capa, "symref", equalsign - capa) == 0) {
450 err = add_symref(symrefs, equalsign + 1);
451 if (err)
452 break;
453 continue;
456 for (i = 0; i < nitems(got_capabilities); i++) {
457 err = match_capability(my_capabilities,
458 capa, &got_capabilities[i]);
459 if (err)
460 break;
462 } while (capa);
464 if (*my_capabilities == NULL) {
465 *my_capabilities = strdup("");
466 if (*my_capabilities == NULL)
467 err = got_error_from_errno("strdup");
469 return err;
472 static const struct got_error *
473 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
475 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
476 return got_error(GOT_ERR_NO_SPACE);
478 if (msglen == 0)
479 return NULL;
481 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
482 msg, msglen) == -1)
483 return got_error_from_errno(
484 "imsg_compose FETCH_SERVER_PROGRESS");
486 return got_privsep_flush_imsg(ibuf);
489 static const struct got_error *
490 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
492 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
493 &bytes, sizeof(bytes)) == -1)
494 return got_error_from_errno(
495 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
497 return got_privsep_flush_imsg(ibuf);
500 static const struct got_error *
501 send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
503 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
504 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
505 return got_error_from_errno("imsg_compose FETCH");
506 return got_privsep_flush_imsg(ibuf);
511 static const struct got_error *
512 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
514 int i;
516 if (len == 0)
517 return NULL;
519 /*
520 * Truncate messages which exceed the maximum imsg payload size.
521 * Server may send up to 64k.
522 */
523 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
524 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
526 /* Only allow printable ASCII. */
527 for (i = 0; i < len; i++) {
528 if (isprint((unsigned char)buf[i]) ||
529 isspace((unsigned char)buf[i]))
530 continue;
531 return got_error_msg(GOT_ERR_BAD_PACKET,
532 "non-printable progress message received from server");
535 return send_fetch_server_progress(ibuf, buf, len);
538 static const struct got_error *
539 fetch_error(const char *buf, size_t len)
541 static char msg[1024];
542 int i;
544 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
545 if (!isprint(buf[i]))
546 return got_error_msg(GOT_ERR_BAD_PACKET,
547 "non-printable error message received from server");
548 msg[i] = buf[i];
550 msg[i] = '\0';
551 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
554 static const struct got_error *
555 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
557 const struct got_error *err = NULL;
558 struct ibuf *wbuf;
559 size_t len, nsymrefs = 0;
560 struct got_pathlist_entry *pe;
562 len = sizeof(struct got_imsg_fetch_symrefs);
563 TAILQ_FOREACH(pe, symrefs, entry) {
564 const char *target = pe->data;
565 len += sizeof(struct got_imsg_fetch_symref) +
566 pe->path_len + strlen(target);
567 nsymrefs++;
570 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
571 return got_error(GOT_ERR_NO_SPACE);
573 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
574 if (wbuf == NULL)
575 return got_error_from_errno("imsg_create FETCH_SYMREFS");
577 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
578 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
579 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
580 ibuf_free(wbuf);
581 return err;
584 TAILQ_FOREACH(pe, symrefs, entry) {
585 const char *name = pe->path;
586 size_t name_len = pe->path_len;
587 const char *target = pe->data;
588 size_t target_len = strlen(target);
590 /* Keep in sync with struct got_imsg_fetch_symref definition! */
591 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
592 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
593 ibuf_free(wbuf);
594 return err;
596 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
597 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
598 ibuf_free(wbuf);
599 return err;
601 if (imsg_add(wbuf, name, name_len) == -1) {
602 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
603 ibuf_free(wbuf);
604 return err;
606 if (imsg_add(wbuf, target, target_len) == -1) {
607 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
608 ibuf_free(wbuf);
609 return err;
613 wbuf->fd = -1;
614 imsg_close(ibuf, wbuf);
615 return got_privsep_flush_imsg(ibuf);
618 static const struct got_error *
619 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
620 const char *refname)
622 const struct got_error *err = NULL;
623 struct ibuf *wbuf;
624 size_t len, reflen = strlen(refname);
626 len = sizeof(struct got_imsg_fetch_ref) + reflen;
627 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
628 return got_error(GOT_ERR_NO_SPACE);
630 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
631 if (wbuf == NULL)
632 return got_error_from_errno("imsg_create FETCH_REF");
634 /* Keep in sync with struct got_imsg_fetch_ref definition! */
635 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
636 err = got_error_from_errno("imsg_add FETCH_REF");
637 ibuf_free(wbuf);
638 return err;
640 if (imsg_add(wbuf, refname, reflen) == -1) {
641 err = got_error_from_errno("imsg_add FETCH_REF");
642 ibuf_free(wbuf);
643 return err;
646 wbuf->fd = -1;
647 imsg_close(ibuf, wbuf);
648 return got_privsep_flush_imsg(ibuf);
652 static const struct got_error *
653 fetch_pack(int fd, int packfd, struct got_object_id *packid,
654 struct got_pathlist_head *have_refs, int fetch_all_branches,
655 struct got_pathlist_head *wanted_branches,
656 struct got_pathlist_head *wanted_refs, int list_refs_only,
657 struct imsgbuf *ibuf)
659 const struct got_error *err = NULL;
660 char buf[GOT_FETCH_PKTMAX];
661 char hashstr[SHA1_DIGEST_STRING_LENGTH];
662 struct got_object_id *have, *want;
663 int is_firstpkt = 1, nref = 0, refsz = 16;
664 int i, n, nwant = 0, nhave = 0, acked = 0;
665 off_t packsz = 0, last_reported_packsz = 0;
666 char *id_str = NULL, *refname = NULL;
667 char *server_capabilities = NULL, *my_capabilities = NULL;
668 const char *default_branch = NULL;
669 struct got_pathlist_head symrefs;
670 struct got_pathlist_entry *pe;
671 int sent_my_capabilites = 0, have_sidebands = 0;
672 int found_branch = 0;
674 TAILQ_INIT(&symrefs);
676 have = malloc(refsz * sizeof(have[0]));
677 if (have == NULL)
678 return got_error_from_errno("malloc");
679 want = malloc(refsz * sizeof(want[0]));
680 if (want == NULL) {
681 err = got_error_from_errno("malloc");
682 goto done;
684 while (1) {
685 err = readpkt(&n, fd, buf, sizeof(buf));
686 if (err)
687 goto done;
688 if (n == 0)
689 break;
690 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
691 err = fetch_error(&buf[4], n - 4);
692 goto done;
694 err = parse_refline(&id_str, &refname, &server_capabilities,
695 buf, n);
696 if (err)
697 goto done;
698 if (is_firstpkt) {
699 if (chattygot && server_capabilities[0] != '\0')
700 fprintf(stderr, "%s: server capabilities: %s\n",
701 getprogname(), server_capabilities);
702 err = match_capabilities(&my_capabilities, &symrefs,
703 server_capabilities);
704 if (err)
705 goto done;
706 if (chattygot)
707 fprintf(stderr, "%s: my capabilities:%s\n",
708 getprogname(), my_capabilities);
709 err = send_fetch_symrefs(ibuf, &symrefs);
710 if (err)
711 goto done;
712 is_firstpkt = 0;
713 if (!fetch_all_branches) {
714 TAILQ_FOREACH(pe, &symrefs, entry) {
715 const char *name = pe->path;
716 const char *symref_target = pe->data;
717 if (strcmp(name, GOT_REF_HEAD) != 0)
718 continue;
719 default_branch = symref_target;
720 break;
723 continue;
725 if (strstr(refname, "^{}")) {
726 if (chattygot) {
727 fprintf(stderr, "%s: ignoring %s\n",
728 getprogname(), refname);
730 continue;
733 if (strncmp(refname, "refs/heads/", 11) == 0) {
734 if (fetch_all_branches || list_refs_only) {
735 found_branch = 1;
736 } else if (!TAILQ_EMPTY(wanted_branches)) {
737 TAILQ_FOREACH(pe, wanted_branches, entry) {
738 if (match_branch(refname, pe->path))
739 break;
741 if (pe == NULL) {
742 if (chattygot) {
743 fprintf(stderr,
744 "%s: ignoring %s\n",
745 getprogname(), refname);
747 continue;
749 found_branch = 1;
750 } else if (default_branch != NULL) {
751 if (!match_branch(refname, default_branch)) {
752 if (chattygot) {
753 fprintf(stderr,
754 "%s: ignoring %s\n",
755 getprogname(), refname);
757 continue;
759 found_branch = 1;
761 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
762 if (!TAILQ_EMPTY(wanted_refs)) {
763 TAILQ_FOREACH(pe, wanted_refs, entry) {
764 if (match_wanted_ref(refname, pe->path))
765 break;
767 if (pe == NULL) {
768 if (chattygot) {
769 fprintf(stderr,
770 "%s: ignoring %s\n",
771 getprogname(), refname);
773 continue;
775 found_branch = 1;
776 } else if (!list_refs_only) {
777 if (chattygot) {
778 fprintf(stderr, "%s: ignoring %s\n",
779 getprogname(), refname);
781 continue;
785 if (refsz == nref + 1) {
786 refsz *= 2;
787 have = reallocarray(have, refsz, sizeof(have[0]));
788 if (have == NULL) {
789 err = got_error_from_errno("reallocarray");
790 goto done;
792 want = reallocarray(want, refsz, sizeof(want[0]));
793 if (want == NULL) {
794 err = got_error_from_errno("reallocarray");
795 goto done;
798 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
799 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
800 goto done;
802 match_remote_ref(have_refs, &have[nref], refname);
803 err = send_fetch_ref(ibuf, &want[nref], refname);
804 if (err)
805 goto done;
807 if (chattygot)
808 fprintf(stderr, "%s: %s will be fetched\n",
809 getprogname(), refname);
810 if (chattygot > 1) {
811 char *theirs, *mine;
812 err = got_object_id_str(&theirs, &want[nref]);
813 if (err)
814 goto done;
815 err = got_object_id_str(&mine, &have[nref]);
816 if (err) {
817 free(theirs);
818 goto done;
820 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
821 getprogname(), theirs, getprogname(), mine);
822 free(theirs);
823 free(mine);
825 nref++;
828 if (list_refs_only)
829 goto done;
831 /* Abort if we haven't found any branch to fetch. */
832 if (!found_branch) {
833 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
834 goto done;
837 for (i = 0; i < nref; i++) {
838 if (got_object_id_cmp(&have[i], &want[i]) == 0)
839 continue;
840 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
841 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
842 sent_my_capabilites ? "" : my_capabilities);
843 if (n >= sizeof(buf)) {
844 err = got_error(GOT_ERR_NO_SPACE);
845 goto done;
847 err = writepkt(fd, buf, n);
848 if (err)
849 goto done;
850 sent_my_capabilites = 1;
851 nwant++;
853 err = flushpkt(fd);
854 if (err)
855 goto done;
857 if (nwant == 0)
858 goto done;
860 for (i = 0; i < nref; i++) {
861 if (got_object_id_cmp(&have[i], &zhash) == 0)
862 continue;
863 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
864 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
865 if (n >= sizeof(buf)) {
866 err = got_error(GOT_ERR_NO_SPACE);
867 goto done;
869 err = writepkt(fd, buf, n);
870 if (err)
871 goto done;
872 nhave++;
875 while (nhave > 0 && !acked) {
876 struct got_object_id common_id;
878 /* The server should ACK the object IDs we need. */
879 err = readpkt(&n, fd, buf, sizeof(buf));
880 if (err)
881 goto done;
882 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
883 err = fetch_error(&buf[4], n - 4);
884 goto done;
886 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
887 /* Server has not located our objects yet. */
888 continue;
890 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
891 strncmp(buf, "ACK ", 4) != 0) {
892 err = got_error_msg(GOT_ERR_BAD_PACKET,
893 "unexpected message from server");
894 goto done;
896 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
897 err = got_error_msg(GOT_ERR_BAD_PACKET,
898 "bad object ID in ACK packet from server");
899 goto done;
901 acked++;
904 n = snprintf(buf, sizeof(buf), "done\n");
905 err = writepkt(fd, buf, n);
906 if (err)
907 goto done;
909 if (nhave == 0) {
910 err = readpkt(&n, fd, buf, sizeof(buf));
911 if (err)
912 goto done;
913 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
914 err = got_error_msg(GOT_ERR_BAD_PACKET,
915 "unexpected message from server");
916 goto done;
920 if (chattygot)
921 fprintf(stderr, "%s: fetching...\n", getprogname());
923 if (my_capabilities != NULL &&
924 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
925 have_sidebands = 1;
927 while (1) {
928 ssize_t r = 0, w;
929 int datalen = -1;
931 if (have_sidebands) {
932 err = read_pkthdr(&datalen, fd);
933 if (err)
934 goto done;
935 if (datalen <= 0)
936 break;
938 /* Read sideband channel ID (one byte). */
939 r = read(fd, buf, 1);
940 if (r == -1) {
941 err = got_error_from_errno("read");
942 goto done;
944 if (r != 1) {
945 err = got_error_msg(GOT_ERR_BAD_PACKET,
946 "short packet");
947 goto done;
949 if (datalen > sizeof(buf) - 5) {
950 err = got_error_msg(GOT_ERR_BAD_PACKET,
951 "bad packet length");
952 goto done;
954 datalen--; /* sideband ID has been read */
955 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
956 /* Read packfile data. */
957 err = readn(&r, fd, buf, datalen);
958 if (err)
959 goto done;
960 if (r != datalen) {
961 err = got_error_msg(GOT_ERR_BAD_PACKET,
962 "packet too short");
963 goto done;
965 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
966 err = readn(&r, fd, buf, datalen);
967 if (err)
968 goto done;
969 if (r != datalen) {
970 err = got_error_msg(GOT_ERR_BAD_PACKET,
971 "packet too short");
972 goto done;
974 err = fetch_progress(ibuf, buf, r);
975 if (err)
976 goto done;
977 continue;
978 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
979 err = readn(&r, fd, buf, datalen);
980 if (err)
981 goto done;
982 if (r != datalen) {
983 err = got_error_msg(GOT_ERR_BAD_PACKET,
984 "packet too short");
985 goto done;
987 err = fetch_error(buf, r);
988 goto done;
989 } else {
990 err = got_error_msg(GOT_ERR_BAD_PACKET,
991 "unknown side-band received from server");
992 goto done;
994 } else {
995 /* No sideband channel. Every byte is packfile data. */
996 err = readn(&r, fd, buf, sizeof buf);
997 if (err)
998 goto done;
999 if (r <= 0)
1000 break;
1003 /* Write packfile data to temporary pack file. */
1004 w = write(packfd, buf, r);
1005 if (w == -1) {
1006 err = got_error_from_errno("write");
1007 goto done;
1009 if (w != r) {
1010 err = got_error(GOT_ERR_IO);
1011 goto done;
1013 packsz += w;
1015 /* Don't send too many progress privsep messages. */
1016 if (packsz > last_reported_packsz + 1024) {
1017 err = send_fetch_download_progress(ibuf, packsz);
1018 if (err)
1019 goto done;
1020 last_reported_packsz = packsz;
1023 err = send_fetch_download_progress(ibuf, packsz);
1024 if (err)
1025 goto done;
1026 done:
1027 TAILQ_FOREACH(pe, &symrefs, entry) {
1028 free((void *)pe->path);
1029 free(pe->data);
1031 got_pathlist_free(&symrefs);
1032 free(have);
1033 free(want);
1034 free(id_str);
1035 free(refname);
1036 free(server_capabilities);
1037 return err;
1041 int
1042 main(int argc, char **argv)
1044 const struct got_error *err = NULL;
1045 int fetchfd, packfd = -1, i;
1046 struct got_object_id packid;
1047 struct imsgbuf ibuf;
1048 struct imsg imsg;
1049 struct got_pathlist_head have_refs;
1050 struct got_pathlist_head wanted_branches;
1051 struct got_pathlist_head wanted_refs;
1052 struct got_pathlist_entry *pe;
1053 struct got_imsg_fetch_request fetch_req;
1054 struct got_imsg_fetch_have_ref href;
1055 struct got_imsg_fetch_wanted_branch wbranch;
1056 struct got_imsg_fetch_wanted_ref wref;
1057 size_t datalen;
1058 #if 0
1059 static int attached;
1060 while (!attached)
1061 sleep (1);
1062 #endif
1064 TAILQ_INIT(&have_refs);
1065 TAILQ_INIT(&wanted_branches);
1066 TAILQ_INIT(&wanted_refs);
1068 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1069 #ifndef PROFILE
1070 /* revoke access to most system calls */
1071 if (pledge("stdio recvfd", NULL) == -1) {
1072 err = got_error_from_errno("pledge");
1073 got_privsep_send_error(&ibuf, err);
1074 return 1;
1076 #endif
1077 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1078 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1079 err = NULL;
1080 goto done;
1082 if (imsg.hdr.type == GOT_IMSG_STOP)
1083 goto done;
1084 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1085 err = got_error(GOT_ERR_PRIVSEP_MSG);
1086 goto done;
1088 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1089 if (datalen < sizeof(fetch_req)) {
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1091 goto done;
1093 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1094 fetchfd = imsg.fd;
1095 imsg_free(&imsg);
1097 if (fetch_req.verbosity > 0)
1098 chattygot += fetch_req.verbosity;
1100 for (i = 0; i < fetch_req.n_have_refs; i++) {
1101 struct got_object_id *id;
1102 char *refname;
1104 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1105 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1106 err = NULL;
1107 goto done;
1109 if (imsg.hdr.type == GOT_IMSG_STOP)
1110 goto done;
1111 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1112 err = got_error(GOT_ERR_PRIVSEP_MSG);
1113 goto done;
1115 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1116 if (datalen < sizeof(href)) {
1117 err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 goto done;
1120 memcpy(&href, imsg.data, sizeof(href));
1121 if (datalen - sizeof(href) < href.name_len) {
1122 err = got_error(GOT_ERR_PRIVSEP_LEN);
1123 goto done;
1125 refname = malloc(href.name_len + 1);
1126 if (refname == NULL) {
1127 err = got_error_from_errno("malloc");
1128 goto done;
1130 memcpy(refname, imsg.data + sizeof(href), href.name_len);
1131 refname[href.name_len] = '\0';
1133 id = malloc(sizeof(*id));
1134 if (id == NULL) {
1135 free(refname);
1136 err = got_error_from_errno("malloc");
1137 goto done;
1139 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1140 err = got_pathlist_append(&have_refs, refname, id);
1141 if (err) {
1142 free(refname);
1143 free(id);
1144 goto done;
1147 imsg_free(&imsg);
1150 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1151 char *refname;
1153 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1154 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1155 err = NULL;
1156 goto done;
1158 if (imsg.hdr.type == GOT_IMSG_STOP)
1159 goto done;
1160 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1161 err = got_error(GOT_ERR_PRIVSEP_MSG);
1162 goto done;
1164 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1165 if (datalen < sizeof(wbranch)) {
1166 err = got_error(GOT_ERR_PRIVSEP_LEN);
1167 goto done;
1169 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1170 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1171 err = got_error(GOT_ERR_PRIVSEP_LEN);
1172 goto done;
1174 refname = malloc(wbranch.name_len + 1);
1175 if (refname == NULL) {
1176 err = got_error_from_errno("malloc");
1177 goto done;
1179 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1180 refname[wbranch.name_len] = '\0';
1182 err = got_pathlist_append(&wanted_branches, refname, NULL);
1183 if (err) {
1184 free(refname);
1185 goto done;
1188 imsg_free(&imsg);
1191 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1192 char *refname;
1194 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1195 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1196 err = NULL;
1197 goto done;
1199 if (imsg.hdr.type == GOT_IMSG_STOP)
1200 goto done;
1201 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1202 err = got_error(GOT_ERR_PRIVSEP_MSG);
1203 goto done;
1205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1206 if (datalen < sizeof(wref)) {
1207 err = got_error(GOT_ERR_PRIVSEP_LEN);
1208 goto done;
1210 memcpy(&wref, imsg.data, sizeof(wref));
1211 if (datalen - sizeof(wref) < wref.name_len) {
1212 err = got_error(GOT_ERR_PRIVSEP_LEN);
1213 goto done;
1215 refname = malloc(wref.name_len + 1);
1216 if (refname == NULL) {
1217 err = got_error_from_errno("malloc");
1218 goto done;
1220 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1221 refname[wref.name_len] = '\0';
1223 err = got_pathlist_append(&wanted_refs, refname, NULL);
1224 if (err) {
1225 free(refname);
1226 goto done;
1229 imsg_free(&imsg);
1232 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1233 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1234 err = NULL;
1235 goto done;
1237 if (imsg.hdr.type == GOT_IMSG_STOP)
1238 goto done;
1239 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1240 err = got_error(GOT_ERR_PRIVSEP_MSG);
1241 goto done;
1243 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1244 err = got_error(GOT_ERR_PRIVSEP_LEN);
1245 goto done;
1247 packfd = imsg.fd;
1249 err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1250 fetch_req.fetch_all_branches, &wanted_branches,
1251 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1252 done:
1253 TAILQ_FOREACH(pe, &have_refs, entry) {
1254 free((char *)pe->path);
1255 free(pe->data);
1257 got_pathlist_free(&have_refs);
1258 TAILQ_FOREACH(pe, &wanted_branches, entry)
1259 free((char *)pe->path);
1260 got_pathlist_free(&wanted_branches);
1261 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1262 err = got_error_from_errno("close");
1263 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1264 err = got_error_from_errno("close");
1265 if (err != NULL)
1266 got_privsep_send_error(&ibuf, err);
1267 else
1268 err = send_fetch_done(&ibuf, packid);
1269 if (err != NULL) {
1270 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1271 got_privsep_send_error(&ibuf, err);
1274 exit(0);