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 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
481 * Truncate messages which exceed the maximum imsg payload size.
482 * Server may send up to 64k.
484 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
485 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
487 /* Only allow printable ASCII. */
488 for (i = 0; i < len; i++) {
489 if (isprint((unsigned char)buf[i]) ||
490 isspace((unsigned char)buf[i]))
492 return got_error_msg(GOT_ERR_BAD_PACKET,
493 "non-printable progress message received from server");
496 return got_privsep_send_fetch_server_progress(ibuf, buf, len);
499 static const struct got_error *
500 fetch_error(const char *buf, size_t len)
502 static char msg[1024];
505 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
506 if (!isprint(buf[i]))
507 return got_error_msg(GOT_ERR_BAD_PACKET,
508 "non-printable error message received from server");
512 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
515 static const struct got_error *
516 fetch_pack(int fd, int packfd, struct got_object_id *packid,
517 struct got_pathlist_head *have_refs, int fetch_all_branches,
518 struct got_pathlist_head *wanted_branches,
519 struct got_pathlist_head *wanted_refs, int list_refs_only,
520 struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
523 char buf[GOT_FETCH_PKTMAX];
524 char hashstr[SHA1_DIGEST_STRING_LENGTH];
525 struct got_object_id *have, *want;
526 int is_firstpkt = 1, nref = 0, refsz = 16;
527 int i, n, nwant = 0, nhave = 0, acked = 0;
528 off_t packsz = 0, last_reported_packsz = 0;
529 char *id_str = NULL, *refname = NULL;
530 char *server_capabilities = NULL, *my_capabilities = NULL;
531 const char *default_branch = NULL;
532 struct got_pathlist_head symrefs;
533 struct got_pathlist_entry *pe;
534 int sent_my_capabilites = 0, have_sidebands = 0;
535 int found_branch = 0;
537 TAILQ_INIT(&symrefs);
539 have = malloc(refsz * sizeof(have[0]));
541 return got_error_from_errno("malloc");
542 want = malloc(refsz * sizeof(want[0]));
544 err = got_error_from_errno("malloc");
548 err = readpkt(&n, fd, buf, sizeof(buf));
553 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 err = fetch_error(&buf[4], n - 4);
557 err = parse_refline(&id_str, &refname, &server_capabilities,
562 if (chattygot && server_capabilities[0] != '\0')
563 fprintf(stderr, "%s: server capabilities: %s\n",
564 getprogname(), server_capabilities);
565 err = match_capabilities(&my_capabilities, &symrefs,
566 server_capabilities);
570 fprintf(stderr, "%s: my capabilities:%s\n",
571 getprogname(), my_capabilities);
572 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
576 if (!fetch_all_branches) {
577 TAILQ_FOREACH(pe, &symrefs, entry) {
578 const char *name = pe->path;
579 const char *symref_target = pe->data;
580 if (strcmp(name, GOT_REF_HEAD) != 0)
582 default_branch = symref_target;
588 if (strstr(refname, "^{}")) {
590 fprintf(stderr, "%s: ignoring %s\n",
591 getprogname(), refname);
596 if (strncmp(refname, "refs/heads/", 11) == 0) {
597 if (fetch_all_branches || list_refs_only) {
599 } else if (!TAILQ_EMPTY(wanted_branches)) {
600 TAILQ_FOREACH(pe, wanted_branches, entry) {
601 if (match_branch(refname, pe->path))
608 getprogname(), refname);
613 } else if (default_branch != NULL) {
614 if (!match_branch(refname, default_branch)) {
618 getprogname(), refname);
624 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
625 if (!TAILQ_EMPTY(wanted_refs)) {
626 TAILQ_FOREACH(pe, wanted_refs, entry) {
627 if (match_wanted_ref(refname, pe->path))
634 getprogname(), refname);
639 } else if (!list_refs_only) {
641 fprintf(stderr, "%s: ignoring %s\n",
642 getprogname(), refname);
648 if (refsz == nref + 1) {
650 have = reallocarray(have, refsz, sizeof(have[0]));
652 err = got_error_from_errno("reallocarray");
655 want = reallocarray(want, refsz, sizeof(want[0]));
657 err = got_error_from_errno("reallocarray");
661 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
662 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
665 match_remote_ref(have_refs, &have[nref], refname);
666 err = got_privsep_send_fetch_ref(ibuf, &want[nref],
672 fprintf(stderr, "%s: %s will be fetched\n",
673 getprogname(), refname);
676 err = got_object_id_str(&theirs, &want[nref]);
679 err = got_object_id_str(&mine, &have[nref]);
684 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
685 getprogname(), theirs, getprogname(), mine);
695 /* Abort if we haven't found any branch to fetch. */
697 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
701 for (i = 0; i < nref; i++) {
702 if (got_object_id_cmp(&have[i], &want[i]) == 0)
704 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
705 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
706 sent_my_capabilites ? "" : my_capabilities);
707 if (n >= sizeof(buf)) {
708 err = got_error(GOT_ERR_NO_SPACE);
711 err = writepkt(fd, buf, n);
714 sent_my_capabilites = 1;
724 for (i = 0; i < nref; i++) {
725 if (got_object_id_cmp(&have[i], &zhash) == 0)
727 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
728 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
729 if (n >= sizeof(buf)) {
730 err = got_error(GOT_ERR_NO_SPACE);
733 err = writepkt(fd, buf, n);
739 while (nhave > 0 && !acked) {
740 struct got_object_id common_id;
742 /* The server should ACK the object IDs we need. */
743 err = readpkt(&n, fd, buf, sizeof(buf));
746 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
747 err = fetch_error(&buf[4], n - 4);
750 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
751 /* Server has not located our objects yet. */
754 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
755 strncmp(buf, "ACK ", 4) != 0) {
756 err = got_error_msg(GOT_ERR_BAD_PACKET,
757 "unexpected message from server");
760 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
761 err = got_error_msg(GOT_ERR_BAD_PACKET,
762 "bad object ID in ACK packet from server");
768 n = snprintf(buf, sizeof(buf), "done\n");
769 err = writepkt(fd, buf, n);
774 err = readpkt(&n, fd, buf, sizeof(buf));
777 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
778 err = got_error_msg(GOT_ERR_BAD_PACKET,
779 "unexpected message from server");
785 fprintf(stderr, "%s: fetching...\n", getprogname());
787 if (my_capabilities != NULL &&
788 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
795 if (have_sidebands) {
796 err = read_pkthdr(&datalen, fd);
802 /* Read sideband channel ID (one byte). */
803 r = read(fd, buf, 1);
805 err = got_error_from_errno("read");
809 err = got_error_msg(GOT_ERR_BAD_PACKET,
813 if (datalen > sizeof(buf) - 5) {
814 err = got_error_msg(GOT_ERR_BAD_PACKET,
815 "bad packet length");
818 datalen--; /* sideband ID has been read */
819 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
820 /* Read packfile data. */
821 err = readn(&r, fd, buf, datalen);
825 err = got_error_msg(GOT_ERR_BAD_PACKET,
829 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
830 err = readn(&r, fd, buf, datalen);
834 err = got_error_msg(GOT_ERR_BAD_PACKET,
838 err = fetch_progress(ibuf, buf, r);
842 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
843 err = readn(&r, fd, buf, datalen);
847 err = got_error_msg(GOT_ERR_BAD_PACKET,
851 err = fetch_error(buf, r);
854 err = got_error_msg(GOT_ERR_BAD_PACKET,
855 "unknown side-band received from server");
859 /* No sideband channel. Every byte is packfile data. */
860 err = readn(&r, fd, buf, sizeof buf);
867 /* Write packfile data to temporary pack file. */
868 w = write(packfd, buf, r);
870 err = got_error_from_errno("write");
874 err = got_error(GOT_ERR_IO);
879 /* Don't send too many progress privsep messages. */
880 if (packsz > last_reported_packsz + 1024) {
881 err = got_privsep_send_fetch_download_progress(ibuf,
885 last_reported_packsz = packsz;
888 err = got_privsep_send_fetch_download_progress(ibuf, packsz);
892 TAILQ_FOREACH(pe, &symrefs, entry) {
893 free((void *)pe->path);
896 got_pathlist_free(&symrefs);
901 free(server_capabilities);
907 main(int argc, char **argv)
909 const struct got_error *err = NULL;
910 int fetchfd, packfd = -1, i;
911 struct got_object_id packid;
914 struct got_pathlist_head have_refs;
915 struct got_pathlist_head wanted_branches;
916 struct got_pathlist_head wanted_refs;
917 struct got_pathlist_entry *pe;
918 struct got_imsg_fetch_request fetch_req;
919 struct got_imsg_fetch_have_ref href;
920 struct got_imsg_fetch_wanted_branch wbranch;
921 struct got_imsg_fetch_wanted_ref wref;
929 TAILQ_INIT(&have_refs);
930 TAILQ_INIT(&wanted_branches);
931 TAILQ_INIT(&wanted_refs);
933 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
935 /* revoke access to most system calls */
936 if (pledge("stdio recvfd", NULL) == -1) {
937 err = got_error_from_errno("pledge");
938 got_privsep_send_error(&ibuf, err);
942 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
943 if (err->code == GOT_ERR_PRIVSEP_PIPE)
947 if (imsg.hdr.type == GOT_IMSG_STOP)
949 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
950 err = got_error(GOT_ERR_PRIVSEP_MSG);
953 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 if (datalen < sizeof(fetch_req)) {
955 err = got_error(GOT_ERR_PRIVSEP_LEN);
958 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
962 if (fetch_req.verbosity > 0)
963 chattygot += fetch_req.verbosity;
965 for (i = 0; i < fetch_req.n_have_refs; i++) {
966 struct got_object_id *id;
969 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
970 if (err->code == GOT_ERR_PRIVSEP_PIPE)
974 if (imsg.hdr.type == GOT_IMSG_STOP)
976 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
977 err = got_error(GOT_ERR_PRIVSEP_MSG);
980 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
981 if (datalen < sizeof(href)) {
982 err = got_error(GOT_ERR_PRIVSEP_LEN);
985 memcpy(&href, imsg.data, sizeof(href));
986 if (datalen - sizeof(href) < href.name_len) {
987 err = got_error(GOT_ERR_PRIVSEP_LEN);
990 refname = malloc(href.name_len + 1);
991 if (refname == NULL) {
992 err = got_error_from_errno("malloc");
995 memcpy(refname, imsg.data + sizeof(href), href.name_len);
996 refname[href.name_len] = '\0';
998 id = malloc(sizeof(*id));
1001 err = got_error_from_errno("malloc");
1004 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1005 err = got_pathlist_append(&have_refs, refname, id);
1015 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1018 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1019 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1023 if (imsg.hdr.type == GOT_IMSG_STOP)
1025 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1026 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 if (datalen < sizeof(wbranch)) {
1031 err = got_error(GOT_ERR_PRIVSEP_LEN);
1034 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1035 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1036 err = got_error(GOT_ERR_PRIVSEP_LEN);
1039 refname = malloc(wbranch.name_len + 1);
1040 if (refname == NULL) {
1041 err = got_error_from_errno("malloc");
1044 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1045 refname[wbranch.name_len] = '\0';
1047 err = got_pathlist_append(&wanted_branches, refname, NULL);
1056 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1059 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1060 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1064 if (imsg.hdr.type == GOT_IMSG_STOP)
1066 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1067 err = got_error(GOT_ERR_PRIVSEP_MSG);
1070 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1071 if (datalen < sizeof(wref)) {
1072 err = got_error(GOT_ERR_PRIVSEP_LEN);
1075 memcpy(&wref, imsg.data, sizeof(wref));
1076 if (datalen - sizeof(wref) < wref.name_len) {
1077 err = got_error(GOT_ERR_PRIVSEP_LEN);
1080 refname = malloc(wref.name_len + 1);
1081 if (refname == NULL) {
1082 err = got_error_from_errno("malloc");
1085 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1086 refname[wref.name_len] = '\0';
1088 err = got_pathlist_append(&wanted_refs, refname, NULL);
1097 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1098 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1102 if (imsg.hdr.type == GOT_IMSG_STOP)
1104 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1105 err = got_error(GOT_ERR_PRIVSEP_MSG);
1108 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1109 err = got_error(GOT_ERR_PRIVSEP_LEN);
1114 err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1115 fetch_req.fetch_all_branches, &wanted_branches,
1116 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1118 TAILQ_FOREACH(pe, &have_refs, entry) {
1119 free((char *)pe->path);
1122 got_pathlist_free(&have_refs);
1123 TAILQ_FOREACH(pe, &wanted_branches, entry)
1124 free((char *)pe->path);
1125 got_pathlist_free(&wanted_branches);
1126 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1127 err = got_error_from_errno("close");
1128 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1129 err = got_error_from_errno("close");
1131 got_privsep_send_error(&ibuf, err);
1133 err = got_privsep_send_fetch_done(&ibuf, packid);
1135 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1136 got_privsep_send_error(&ibuf, err);