2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
22 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.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"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 struct got_object *indexed;
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)
67 r = read(fd, buf + *off, n - *off);
69 return got_error_from_errno("read");
77 static const struct got_error *
83 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
85 w = write(fd, "0000", 4);
87 return got_error_from_errno("write");
89 return got_error(GOT_ERR_IO);
94 * Packet header contains a 4-byte hexstring which specifies the length
95 * of data which follows.
97 static const struct got_error *
98 read_pkthdr(int *datalen, int fd)
100 static const struct got_error *err = NULL;
109 err = readn(&r, fd, lenstr, 4);
113 /* implicit "0000" */
115 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
119 return got_error_msg(GOT_ERR_BAD_PACKET,
120 "wrong packet header length");
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])) {
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");
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");
149 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
156 static const struct got_error *
157 readpkt(int *outlen, int fd, char *buf, int buflen)
159 const struct got_error *err = NULL;
163 err = read_pkthdr(&datalen, fd);
167 if (datalen > buflen)
168 return got_error(GOT_ERR_NO_SPACE);
170 err = readn(&n, fd, buf, datalen);
174 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
177 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 for (i = 0; i < n; i++) {
180 fputc(buf[i], stderr);
182 fprintf(stderr, "[0x%.2x]", buf[i]);
191 static const struct got_error *
192 writepkt(int fd, char *buf, int nbuf)
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);
202 return got_error_from_errno("write");
204 return got_error(GOT_ERR_IO);
205 w = write(fd, buf, nbuf);
207 return got_error_from_errno("write");
209 return got_error(GOT_ERR_IO);
211 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 for (i = 0; i < nbuf; i++) {
214 fputc(buf[i], stderr);
216 fprintf(stderr, "[0x%.2x]", buf[i]);
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));
243 match_branch(const char *branch, const char *wanted_branch)
245 if (strncmp(branch, "refs/heads/", 11) != 0)
248 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
251 return (strcmp(branch + 11, wanted_branch) == 0);
255 match_wanted_ref(const char *refname, const char *wanted_ref)
257 if (strncmp(refname, "refs/", 5) != 0)
262 * Prevent fetching of references that won't make any
263 * sense outside of the remote repository's context.
265 if (strncmp(refname, "got/", 4) == 0)
267 if (strncmp(refname, "remotes/", 8) == 0)
270 if (strncmp(wanted_ref, "refs/", 5) == 0)
273 /* Allow prefix match. */
274 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
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;
288 for (i = 0; i < maxtokens; i++)
291 for (i = 0; n < len && i < maxtokens; i++) {
292 while (isspace(*line)) {
297 while (*line != '\0' &&
298 (!isspace(*line) || i == maxtokens - 1)) {
302 tokens[i] = strndup(p, line - p);
303 if (tokens[i] == NULL) {
304 err = got_error_from_errno("strndup");
307 /* Skip \0 field-delimiter at end of token. */
308 while (line[0] == '\0' && n < len) {
314 err = got_error(GOT_ERR_NOT_REF);
318 for (j = 0; j < i; j++)
325 static const struct got_error *
326 parse_refline(char **id_str, char **refname, char **server_capabilities,
329 const struct got_error *err = NULL;
332 err = tokenize_refline(tokens, line, len, nitems(tokens));
339 *refname = tokens[1];
342 *server_capabilities = tokens[2];
343 p = strrchr(*server_capabilities, '\n');
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 {
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 },
370 static const struct got_error *
371 match_capability(char **my_capabilities, const char *capa,
372 const struct got_capability *mycapa)
377 equalsign = strchr(capa, '=');
379 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
382 if (strcmp(capa, mycapa->key) != 0)
386 if (asprintf(&s, "%s %s%s%s",
387 *my_capabilities != NULL ? *my_capabilities : "",
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;
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)
408 colon = strchr(capa, ':');
415 return got_error_from_errno("strdup");
417 target = strdup(colon + 1);
418 if (target == NULL) {
419 err = got_error_from_errno("strdup");
423 /* We can't validate the ref itself here. The main process will. */
424 err = got_pathlist_append(symrefs, name, target);
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;
441 *my_capabilities = NULL;
443 capa = strsep(&server_capabilities, " ");
447 equalsign = strchr(capa, '=');
448 if (equalsign != NULL &&
449 strncmp(capa, "symref", equalsign - capa) == 0) {
450 err = add_symref(symrefs, equalsign + 1);
456 for (i = 0; i < nitems(got_capabilities); i++) {
457 err = match_capability(my_capabilities,
458 capa, &got_capabilities[i]);
464 if (*my_capabilities == NULL) {
465 *my_capabilities = strdup("");
466 if (*my_capabilities == NULL)
467 err = got_error_from_errno("strdup");
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);
481 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -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)
520 * Truncate messages which exceed the maximum imsg payload size.
521 * Server may send up to 64k.
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]))
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];
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");
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;
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);
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);
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");
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");
596 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
597 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
601 if (imsg_add(wbuf, name, name_len) == -1) {
602 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
606 if (imsg_add(wbuf, target, target_len) == -1) {
607 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
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,
622 const struct got_error *err = NULL;
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);
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");
640 if (imsg_add(wbuf, refname, reflen) == -1) {
641 err = got_error_from_errno("imsg_add FETCH_REF");
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]));
678 return got_error_from_errno("malloc");
679 want = malloc(refsz * sizeof(want[0]));
681 err = got_error_from_errno("malloc");
685 err = readpkt(&n, fd, buf, sizeof(buf));
690 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
691 err = fetch_error(&buf[4], n - 4);
694 err = parse_refline(&id_str, &refname, &server_capabilities,
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);
707 fprintf(stderr, "%s: my capabilities:%s\n",
708 getprogname(), my_capabilities);
709 err = send_fetch_symrefs(ibuf, &symrefs);
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)
719 default_branch = symref_target;
725 if (strstr(refname, "^{}")) {
727 fprintf(stderr, "%s: ignoring %s\n",
728 getprogname(), refname);
733 if (strncmp(refname, "refs/heads/", 11) == 0) {
734 if (fetch_all_branches || list_refs_only) {
736 } else if (!TAILQ_EMPTY(wanted_branches)) {
737 TAILQ_FOREACH(pe, wanted_branches, entry) {
738 if (match_branch(refname, pe->path))
745 getprogname(), refname);
750 } else if (default_branch != NULL) {
751 if (!match_branch(refname, default_branch)) {
755 getprogname(), refname);
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))
771 getprogname(), refname);
776 } else if (!list_refs_only) {
778 fprintf(stderr, "%s: ignoring %s\n",
779 getprogname(), refname);
785 if (refsz == nref + 1) {
787 have = reallocarray(have, refsz, sizeof(have[0]));
789 err = got_error_from_errno("reallocarray");
792 want = reallocarray(want, refsz, sizeof(want[0]));
794 err = got_error_from_errno("reallocarray");
798 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
799 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
802 match_remote_ref(have_refs, &have[nref], refname);
803 err = send_fetch_ref(ibuf, &want[nref], refname);
808 fprintf(stderr, "%s: %s will be fetched\n",
809 getprogname(), refname);
812 err = got_object_id_str(&theirs, &want[nref]);
815 err = got_object_id_str(&mine, &have[nref]);
820 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
821 getprogname(), theirs, getprogname(), mine);
831 /* Abort if we haven't found any branch to fetch. */
833 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
837 for (i = 0; i < nref; i++) {
838 if (got_object_id_cmp(&have[i], &want[i]) == 0)
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);
847 err = writepkt(fd, buf, n);
850 sent_my_capabilites = 1;
860 for (i = 0; i < nref; i++) {
861 if (got_object_id_cmp(&have[i], &zhash) == 0)
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);
869 err = writepkt(fd, buf, n);
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));
882 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
883 err = fetch_error(&buf[4], n - 4);
886 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
887 /* Server has not located our objects yet. */
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");
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");
904 n = snprintf(buf, sizeof(buf), "done\n");
905 err = writepkt(fd, buf, n);
910 err = readpkt(&n, fd, buf, sizeof(buf));
913 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
914 err = got_error_msg(GOT_ERR_BAD_PACKET,
915 "unexpected message from server");
921 fprintf(stderr, "%s: fetching...\n", getprogname());
923 if (my_capabilities != NULL &&
924 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
931 if (have_sidebands) {
932 err = read_pkthdr(&datalen, fd);
938 /* Read sideband channel ID (one byte). */
939 r = read(fd, buf, 1);
941 err = got_error_from_errno("read");
945 err = got_error_msg(GOT_ERR_BAD_PACKET,
949 if (datalen > sizeof(buf) - 5) {
950 err = got_error_msg(GOT_ERR_BAD_PACKET,
951 "bad packet length");
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);
961 err = got_error_msg(GOT_ERR_BAD_PACKET,
965 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
966 err = readn(&r, fd, buf, datalen);
970 err = got_error_msg(GOT_ERR_BAD_PACKET,
974 err = fetch_progress(ibuf, buf, r);
978 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
979 err = readn(&r, fd, buf, datalen);
983 err = got_error_msg(GOT_ERR_BAD_PACKET,
987 err = fetch_error(buf, r);
990 err = got_error_msg(GOT_ERR_BAD_PACKET,
991 "unknown side-band received from server");
995 /* No sideband channel. Every byte is packfile data. */
996 err = readn(&r, fd, buf, sizeof buf);
1003 /* Write packfile data to temporary pack file. */
1004 w = write(packfd, buf, r);
1006 err = got_error_from_errno("write");
1010 err = got_error(GOT_ERR_IO);
1015 /* Don't send too many progress privsep messages. */
1016 if (packsz > last_reported_packsz + 1024) {
1017 err = send_fetch_download_progress(ibuf, packsz);
1020 last_reported_packsz = packsz;
1023 err = send_fetch_download_progress(ibuf, packsz);
1027 TAILQ_FOREACH(pe, &symrefs, entry) {
1028 free((void *)pe->path);
1031 got_pathlist_free(&symrefs);
1036 free(server_capabilities);
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;
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;
1059 static int attached;
1064 TAILQ_INIT(&have_refs);
1065 TAILQ_INIT(&wanted_branches);
1066 TAILQ_INIT(&wanted_refs);
1068 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
1077 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1078 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1082 if (imsg.hdr.type == GOT_IMSG_STOP)
1084 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1085 err = got_error(GOT_ERR_PRIVSEP_MSG);
1088 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1089 if (datalen < sizeof(fetch_req)) {
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1093 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
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;
1104 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1105 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1109 if (imsg.hdr.type == GOT_IMSG_STOP)
1111 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1112 err = got_error(GOT_ERR_PRIVSEP_MSG);
1115 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1116 if (datalen < sizeof(href)) {
1117 err = got_error(GOT_ERR_PRIVSEP_LEN);
1120 memcpy(&href, imsg.data, sizeof(href));
1121 if (datalen - sizeof(href) < href.name_len) {
1122 err = got_error(GOT_ERR_PRIVSEP_LEN);
1125 refname = malloc(href.name_len + 1);
1126 if (refname == NULL) {
1127 err = got_error_from_errno("malloc");
1130 memcpy(refname, imsg.data + sizeof(href), href.name_len);
1131 refname[href.name_len] = '\0';
1133 id = malloc(sizeof(*id));
1136 err = got_error_from_errno("malloc");
1139 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1140 err = got_pathlist_append(&have_refs, refname, id);
1150 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1153 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1154 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1158 if (imsg.hdr.type == GOT_IMSG_STOP)
1160 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1161 err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1165 if (datalen < sizeof(wbranch)) {
1166 err = got_error(GOT_ERR_PRIVSEP_LEN);
1169 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1170 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1171 err = got_error(GOT_ERR_PRIVSEP_LEN);
1174 refname = malloc(wbranch.name_len + 1);
1175 if (refname == NULL) {
1176 err = got_error_from_errno("malloc");
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);
1191 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1194 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1195 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1199 if (imsg.hdr.type == GOT_IMSG_STOP)
1201 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1202 err = got_error(GOT_ERR_PRIVSEP_MSG);
1205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1206 if (datalen < sizeof(wref)) {
1207 err = got_error(GOT_ERR_PRIVSEP_LEN);
1210 memcpy(&wref, imsg.data, sizeof(wref));
1211 if (datalen - sizeof(wref) < wref.name_len) {
1212 err = got_error(GOT_ERR_PRIVSEP_LEN);
1215 refname = malloc(wref.name_len + 1);
1216 if (refname == NULL) {
1217 err = got_error_from_errno("malloc");
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);
1232 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1233 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1237 if (imsg.hdr.type == GOT_IMSG_STOP)
1239 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1240 err = got_error(GOT_ERR_PRIVSEP_MSG);
1243 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1244 err = got_error(GOT_ERR_PRIVSEP_LEN);
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);
1253 TAILQ_FOREACH(pe, &have_refs, entry) {
1254 free((char *)pe->path);
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");
1266 got_privsep_send_error(&ibuf, err);
1268 err = send_fetch_done(&ibuf, packid);
1270 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1271 got_privsep_send_error(&ibuf, err);