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>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_version.h"
43 #include "got_fetch.h"
44 #include "got_reference.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 struct got_object *indexed;
59 static struct got_object_id zhash = {.sha1={0}};
61 static const struct got_error *
62 readn(ssize_t *off, int fd, void *buf, size_t n)
68 r = read(fd, buf + *off, n - *off);
70 return got_error_from_errno("read");
78 static const struct got_error *
84 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
86 w = write(fd, "0000", 4);
88 return got_error_from_errno("write");
90 return got_error(GOT_ERR_IO);
95 * Packet header contains a 4-byte hexstring which specifies the length
96 * of data which follows.
98 static const struct got_error *
99 read_pkthdr(int *datalen, int fd)
101 static const struct got_error *err = NULL;
110 err = readn(&r, fd, lenstr, 4);
114 /* implicit "0000" */
116 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
120 return got_error_msg(GOT_ERR_BAD_PACKET,
121 "wrong packet header length");
124 for (i = 0; i < 4; i++) {
125 if (!isprint((unsigned char)lenstr[i]))
126 return got_error_msg(GOT_ERR_BAD_PACKET,
127 "unprintable character in packet length field");
129 for (i = 0; i < 4; i++) {
130 if (!isxdigit((unsigned char)lenstr[i])) {
132 fprintf(stderr, "%s: bad length: '%s'\n",
133 getprogname(), lenstr);
134 return got_error_msg(GOT_ERR_BAD_PACKET,
135 "packet length not specified in hex");
139 len = strtol(lenstr, &e, 16);
140 if (lenstr[0] == '\0' || *e != '\0')
141 return got_error(GOT_ERR_BAD_PACKET);
142 if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
143 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
144 if (len > INT_MAX || len < INT_MIN)
145 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
150 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
157 static const struct got_error *
158 readpkt(int *outlen, int fd, char *buf, int buflen)
160 const struct got_error *err = NULL;
164 err = read_pkthdr(&datalen, fd);
168 if (datalen > buflen)
169 return got_error(GOT_ERR_NO_SPACE);
171 err = readn(&n, fd, buf, datalen);
175 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
178 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
179 for (i = 0; i < n; i++) {
181 fputc(buf[i], stderr);
183 fprintf(stderr, "[0x%.2x]", buf[i]);
192 static const struct got_error *
193 writepkt(int fd, char *buf, int nbuf)
199 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
200 return got_error(GOT_ERR_NO_SPACE);
201 w = write(fd, len, 4);
203 return got_error_from_errno("write");
205 return got_error(GOT_ERR_IO);
206 w = write(fd, buf, nbuf);
208 return got_error_from_errno("write");
210 return got_error(GOT_ERR_IO);
212 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
213 for (i = 0; i < nbuf; i++) {
215 fputc(buf[i], stderr);
217 fprintf(stderr, "[0x%.2x]", buf[i]);
225 match_remote_ref(struct got_pathlist_head *have_refs,
226 struct got_object_id *my_id, char *refname)
228 struct got_pathlist_entry *pe;
230 /* XXX zero-hash signifies we don't have this ref;
231 * we should use a flag instead */
232 memset(my_id, 0, sizeof(*my_id));
234 TAILQ_FOREACH(pe, have_refs, entry) {
235 struct got_object_id *id = pe->data;
236 if (strcmp(pe->path, refname) == 0) {
237 memcpy(my_id, id, sizeof(*my_id));
244 match_branch(const char *branch, const char *wanted_branch)
246 if (strncmp(branch, "refs/heads/", 11) != 0)
249 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
252 return (strcmp(branch + 11, wanted_branch) == 0);
256 match_wanted_ref(const char *refname, const char *wanted_ref)
258 if (strncmp(refname, "refs/", 5) != 0)
263 * Prevent fetching of references that won't make any
264 * sense outside of the remote repository's context.
266 if (strncmp(refname, "got/", 4) == 0)
268 if (strncmp(refname, "remotes/", 8) == 0)
271 if (strncmp(wanted_ref, "refs/", 5) == 0)
274 /* Allow prefix match. */
275 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
278 /* Allow exact match. */
279 return (strcmp(refname, wanted_ref) == 0);
282 static const struct got_error *
283 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
285 const struct got_error *err = NULL;
289 for (i = 0; i < maxtokens; i++)
292 for (i = 0; n < len && i < maxtokens; i++) {
293 while (isspace(*line)) {
298 while (*line != '\0' &&
299 (!isspace(*line) || i == maxtokens - 1)) {
303 tokens[i] = strndup(p, line - p);
304 if (tokens[i] == NULL) {
305 err = got_error_from_errno("strndup");
308 /* Skip \0 field-delimiter at end of token. */
309 while (line[0] == '\0' && n < len) {
315 err = got_error(GOT_ERR_NOT_REF);
319 for (j = 0; j < i; j++) {
327 static const struct got_error *
328 parse_refline(char **id_str, char **refname, char **server_capabilities,
331 const struct got_error *err = NULL;
334 err = tokenize_refline(tokens, line, len, nitems(tokens));
341 *refname = tokens[1];
344 *server_capabilities = tokens[2];
345 p = strrchr(*server_capabilities, '\n');
353 #define GOT_CAPA_AGENT "agent"
354 #define GOT_CAPA_OFS_DELTA "ofs-delta"
355 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
357 #define GOT_SIDEBAND_PACKFILE_DATA 1
358 #define GOT_SIDEBAND_PROGRESS_INFO 2
359 #define GOT_SIDEBAND_ERROR_INFO 3
362 struct got_capability {
366 static const struct got_capability got_capabilities[] = {
367 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
368 { GOT_CAPA_OFS_DELTA, NULL },
369 { GOT_CAPA_SIDE_BAND_64K, NULL },
372 static const struct got_error *
373 match_capability(char **my_capabilities, const char *capa,
374 const struct got_capability *mycapa)
379 equalsign = strchr(capa, '=');
381 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
384 if (strcmp(capa, mycapa->key) != 0)
388 if (asprintf(&s, "%s %s%s%s",
389 *my_capabilities != NULL ? *my_capabilities : "",
391 mycapa->value != NULL ? "=" : "",
392 mycapa->value != NULL? mycapa->value : "") == -1)
393 return got_error_from_errno("asprintf");
395 free(*my_capabilities);
396 *my_capabilities = s;
400 static const struct got_error *
401 add_symref(struct got_pathlist_head *symrefs, char *capa)
403 const struct got_error *err = NULL;
404 char *colon, *name = NULL, *target = NULL;
406 /* Need at least "A:B" */
407 if (strlen(capa) < 3)
410 colon = strchr(capa, ':');
417 return got_error_from_errno("strdup");
419 target = strdup(colon + 1);
420 if (target == NULL) {
421 err = got_error_from_errno("strdup");
425 /* We can't validate the ref itself here. The main process will. */
426 err = got_pathlist_append(symrefs, name, target);
435 static const struct got_error *
436 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
437 char *server_capabilities)
439 const struct got_error *err = NULL;
440 char *capa, *equalsign;
443 *my_capabilities = NULL;
445 capa = strsep(&server_capabilities, " ");
449 equalsign = strchr(capa, '=');
450 if (equalsign != NULL &&
451 strncmp(capa, "symref", equalsign - capa) == 0) {
452 err = add_symref(symrefs, equalsign + 1);
458 for (i = 0; i < nitems(got_capabilities); i++) {
459 err = match_capability(my_capabilities,
460 capa, &got_capabilities[i]);
466 if (*my_capabilities == NULL) {
467 *my_capabilities = strdup("");
468 if (*my_capabilities == NULL)
469 err = got_error_from_errno("strdup");
474 static const struct got_error *
475 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
477 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
478 return got_error(GOT_ERR_NO_SPACE);
483 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
485 return got_error_from_errno(
486 "imsg_compose FETCH_SERVER_PROGRESS");
488 return got_privsep_flush_imsg(ibuf);
491 static const struct got_error *
492 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
494 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
495 &bytes, sizeof(bytes)) == -1)
496 return got_error_from_errno(
497 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
499 return got_privsep_flush_imsg(ibuf);
502 static const struct got_error *
503 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
505 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
506 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
507 return got_error_from_errno("imsg_compose FETCH");
508 return got_privsep_flush_imsg(ibuf);
513 static const struct got_error *
514 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
522 * Truncate messages which exceed the maximum imsg payload size.
523 * Server may send up to 64k.
525 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
526 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
528 /* Only allow printable ASCII. */
529 for (i = 0; i < len; i++) {
530 if (isprint((unsigned char)buf[i]) ||
531 isspace((unsigned char)buf[i]))
533 return got_error_msg(GOT_ERR_BAD_PACKET,
534 "non-printable progress message received from server");
537 return send_fetch_server_progress(ibuf, buf, len);
540 static const struct got_error *
541 fetch_error(const char *buf, size_t len)
543 static char msg[1024];
546 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
547 if (!isprint(buf[i]))
548 return got_error_msg(GOT_ERR_BAD_PACKET,
549 "non-printable error message received from server");
553 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
556 static const struct got_error *
557 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
559 const struct got_error *err = NULL;
561 size_t len, nsymrefs = 0;
562 struct got_pathlist_entry *pe;
564 len = sizeof(struct got_imsg_fetch_symrefs);
565 TAILQ_FOREACH(pe, symrefs, entry) {
566 const char *target = pe->data;
567 len += sizeof(struct got_imsg_fetch_symref) +
568 pe->path_len + strlen(target);
572 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
573 return got_error(GOT_ERR_NO_SPACE);
575 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
577 return got_error_from_errno("imsg_create FETCH_SYMREFS");
579 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
580 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
581 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
586 TAILQ_FOREACH(pe, symrefs, entry) {
587 const char *name = pe->path;
588 size_t name_len = pe->path_len;
589 const char *target = pe->data;
590 size_t target_len = strlen(target);
592 /* Keep in sync with struct got_imsg_fetch_symref definition! */
593 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
594 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
598 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
599 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
603 if (imsg_add(wbuf, name, name_len) == -1) {
604 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
608 if (imsg_add(wbuf, target, target_len) == -1) {
609 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
616 imsg_close(ibuf, wbuf);
617 return got_privsep_flush_imsg(ibuf);
620 static const struct got_error *
621 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
624 const struct got_error *err = NULL;
626 size_t len, reflen = strlen(refname);
628 len = sizeof(struct got_imsg_fetch_ref) + reflen;
629 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
630 return got_error(GOT_ERR_NO_SPACE);
632 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
634 return got_error_from_errno("imsg_create FETCH_REF");
636 /* Keep in sync with struct got_imsg_fetch_ref definition! */
637 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
638 err = got_error_from_errno("imsg_add FETCH_REF");
642 if (imsg_add(wbuf, refname, reflen) == -1) {
643 err = got_error_from_errno("imsg_add FETCH_REF");
649 imsg_close(ibuf, wbuf);
650 return got_privsep_flush_imsg(ibuf);
653 static const struct got_error *
654 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
655 struct got_pathlist_head *have_refs, int fetch_all_branches,
656 struct got_pathlist_head *wanted_branches,
657 struct got_pathlist_head *wanted_refs, int list_refs_only,
658 struct imsgbuf *ibuf)
660 const struct got_error *err = NULL;
661 char buf[GOT_FETCH_PKTMAX];
662 char hashstr[SHA1_DIGEST_STRING_LENGTH];
663 struct got_object_id *have, *want;
664 int is_firstpkt = 1, nref = 0, refsz = 16;
665 int i, n, nwant = 0, nhave = 0, acked = 0;
666 off_t packsz = 0, last_reported_packsz = 0;
667 char *id_str = NULL, *refname = NULL;
668 char *server_capabilities = NULL, *my_capabilities = NULL;
669 const char *default_branch = NULL;
670 struct got_pathlist_head symrefs;
671 struct got_pathlist_entry *pe;
672 int sent_my_capabilites = 0, have_sidebands = 0;
673 int found_branch = 0;
675 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
676 size_t sha1_buf_len = 0;
679 TAILQ_INIT(&symrefs);
682 have = malloc(refsz * sizeof(have[0]));
684 return got_error_from_errno("malloc");
685 want = malloc(refsz * sizeof(want[0]));
687 err = got_error_from_errno("malloc");
691 err = readpkt(&n, fd, buf, sizeof(buf));
696 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
697 err = fetch_error(&buf[4], n - 4);
700 err = parse_refline(&id_str, &refname, &server_capabilities,
705 if (chattygot && server_capabilities[0] != '\0')
706 fprintf(stderr, "%s: server capabilities: %s\n",
707 getprogname(), server_capabilities);
708 err = match_capabilities(&my_capabilities, &symrefs,
709 server_capabilities);
713 fprintf(stderr, "%s: my capabilities:%s\n",
714 getprogname(), my_capabilities);
715 err = send_fetch_symrefs(ibuf, &symrefs);
719 if (!fetch_all_branches) {
720 TAILQ_FOREACH(pe, &symrefs, entry) {
721 const char *name = pe->path;
722 const char *symref_target = pe->data;
723 if (strcmp(name, GOT_REF_HEAD) != 0)
725 default_branch = symref_target;
731 if (strstr(refname, "^{}")) {
733 fprintf(stderr, "%s: ignoring %s\n",
734 getprogname(), refname);
739 if (strncmp(refname, "refs/heads/", 11) == 0) {
740 if (fetch_all_branches || list_refs_only) {
742 } else if (!TAILQ_EMPTY(wanted_branches)) {
743 TAILQ_FOREACH(pe, wanted_branches, entry) {
744 if (match_branch(refname, pe->path))
751 getprogname(), refname);
756 } else if (default_branch != NULL) {
757 if (!match_branch(refname, default_branch)) {
761 getprogname(), refname);
767 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
768 if (!TAILQ_EMPTY(wanted_refs)) {
769 TAILQ_FOREACH(pe, wanted_refs, entry) {
770 if (match_wanted_ref(refname, pe->path))
777 getprogname(), refname);
782 } else if (!list_refs_only) {
784 fprintf(stderr, "%s: ignoring %s\n",
785 getprogname(), refname);
791 if (refsz == nref + 1) {
793 have = reallocarray(have, refsz, sizeof(have[0]));
795 err = got_error_from_errno("reallocarray");
798 want = reallocarray(want, refsz, sizeof(want[0]));
800 err = got_error_from_errno("reallocarray");
804 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
805 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
808 match_remote_ref(have_refs, &have[nref], refname);
809 err = send_fetch_ref(ibuf, &want[nref], refname);
814 fprintf(stderr, "%s: %s will be fetched\n",
815 getprogname(), refname);
818 err = got_object_id_str(&theirs, &want[nref]);
821 err = got_object_id_str(&mine, &have[nref]);
826 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
827 getprogname(), theirs, getprogname(), mine);
837 /* Abort if we haven't found any branch to fetch. */
839 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
843 for (i = 0; i < nref; i++) {
844 if (got_object_id_cmp(&have[i], &want[i]) == 0)
846 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
847 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
848 sent_my_capabilites ? "" : my_capabilities);
849 if (n >= sizeof(buf)) {
850 err = got_error(GOT_ERR_NO_SPACE);
853 err = writepkt(fd, buf, n);
856 sent_my_capabilites = 1;
866 for (i = 0; i < nref; i++) {
867 if (got_object_id_cmp(&have[i], &zhash) == 0)
869 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
870 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
871 if (n >= sizeof(buf)) {
872 err = got_error(GOT_ERR_NO_SPACE);
875 err = writepkt(fd, buf, n);
881 while (nhave > 0 && !acked) {
882 struct got_object_id common_id;
884 /* The server should ACK the object IDs we need. */
885 err = readpkt(&n, fd, buf, sizeof(buf));
888 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
889 err = fetch_error(&buf[4], n - 4);
892 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
893 /* Server has not located our objects yet. */
896 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
897 strncmp(buf, "ACK ", 4) != 0) {
898 err = got_error_msg(GOT_ERR_BAD_PACKET,
899 "unexpected message from server");
902 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
903 err = got_error_msg(GOT_ERR_BAD_PACKET,
904 "bad object ID in ACK packet from server");
910 n = snprintf(buf, sizeof(buf), "done\n");
911 err = writepkt(fd, buf, n);
916 err = readpkt(&n, fd, buf, sizeof(buf));
919 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
920 err = got_error_msg(GOT_ERR_BAD_PACKET,
921 "unexpected message from server");
927 fprintf(stderr, "%s: fetching...\n", getprogname());
929 if (my_capabilities != NULL &&
930 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
937 if (have_sidebands) {
938 err = read_pkthdr(&datalen, fd);
944 /* Read sideband channel ID (one byte). */
945 r = read(fd, buf, 1);
947 err = got_error_from_errno("read");
951 err = got_error_msg(GOT_ERR_BAD_PACKET,
955 if (datalen > sizeof(buf) - 5) {
956 err = got_error_msg(GOT_ERR_BAD_PACKET,
957 "bad packet length");
960 datalen--; /* sideband ID has been read */
961 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
962 /* Read packfile data. */
963 err = readn(&r, fd, buf, datalen);
967 err = got_error_msg(GOT_ERR_BAD_PACKET,
971 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
972 err = readn(&r, fd, buf, datalen);
976 err = got_error_msg(GOT_ERR_BAD_PACKET,
980 err = fetch_progress(ibuf, buf, r);
984 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
985 err = readn(&r, fd, buf, datalen);
989 err = got_error_msg(GOT_ERR_BAD_PACKET,
993 err = fetch_error(buf, r);
996 err = got_error_msg(GOT_ERR_BAD_PACKET,
997 "unknown side-band received from server");
1001 /* No sideband channel. Every byte is packfile data. */
1002 err = readn(&r, fd, buf, sizeof buf);
1010 * An expected SHA1 checksum sits at the end of the pack file.
1011 * Since we don't know the file size ahead of time we have to
1012 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
1013 * those bytes into our SHA1 checksum computation until we
1014 * know for sure that additional pack file data bytes follow.
1016 * We can assume r > 0 since otherwise the loop would exit.
1018 if (r < SHA1_DIGEST_LENGTH) {
1019 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
1021 * If there's enough buffered + read data to
1022 * fill up the buffer then shift a sufficient
1023 * amount of bytes out at the front to make
1024 * room, mixing those bytes into the checksum.
1026 while (sha1_buf_len > 0 &&
1027 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
1028 SHA1Update(&sha1_ctx, sha1_buf, 1);
1029 memmove(sha1_buf, sha1_buf + 1, 1);
1033 /* Buffer potential checksum bytes. */
1034 memcpy(sha1_buf + sha1_buf_len, buf, r);
1038 * Mix in previously buffered bytes which
1039 * are not part of the checksum after all.
1041 SHA1Update(&sha1_ctx, sha1_buf, r);
1043 /* Update potential checksum buffer. */
1044 memmove(sha1_buf, sha1_buf + r,
1046 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
1049 /* Mix in any previously buffered bytes. */
1050 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
1052 /* Mix in bytes read minus potential checksum bytes. */
1053 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
1055 /* Buffer potential checksum bytes. */
1056 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
1057 SHA1_DIGEST_LENGTH);
1058 sha1_buf_len = SHA1_DIGEST_LENGTH;
1061 /* Write packfile data to temporary pack file. */
1062 w = write(packfd, buf, r);
1064 err = got_error_from_errno("write");
1068 err = got_error(GOT_ERR_IO);
1073 /* Don't send too many progress privsep messages. */
1074 if (packsz > last_reported_packsz + 1024) {
1075 err = send_fetch_download_progress(ibuf, packsz);
1078 last_reported_packsz = packsz;
1081 err = send_fetch_download_progress(ibuf, packsz);
1085 SHA1Final(pack_sha1, &sha1_ctx);
1086 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
1087 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
1088 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1089 "pack file checksum mismatch");
1092 TAILQ_FOREACH(pe, &symrefs, entry) {
1093 free((void *)pe->path);
1096 got_pathlist_free(&symrefs);
1101 free(server_capabilities);
1107 main(int argc, char **argv)
1109 const struct got_error *err = NULL;
1110 int fetchfd, packfd = -1, i;
1111 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
1112 struct imsgbuf ibuf;
1114 struct got_pathlist_head have_refs;
1115 struct got_pathlist_head wanted_branches;
1116 struct got_pathlist_head wanted_refs;
1117 struct got_pathlist_entry *pe;
1118 struct got_imsg_fetch_request fetch_req;
1119 struct got_imsg_fetch_have_ref href;
1120 struct got_imsg_fetch_wanted_branch wbranch;
1121 struct got_imsg_fetch_wanted_ref wref;
1124 static int attached;
1129 TAILQ_INIT(&have_refs);
1130 TAILQ_INIT(&wanted_branches);
1131 TAILQ_INIT(&wanted_refs);
1133 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1135 /* revoke access to most system calls */
1136 if (pledge("stdio recvfd", NULL) == -1) {
1137 err = got_error_from_errno("pledge");
1138 got_privsep_send_error(&ibuf, err);
1142 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1143 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1147 if (imsg.hdr.type == GOT_IMSG_STOP)
1149 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1150 err = got_error(GOT_ERR_PRIVSEP_MSG);
1153 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1154 if (datalen < sizeof(fetch_req)) {
1155 err = got_error(GOT_ERR_PRIVSEP_LEN);
1158 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1162 if (fetch_req.verbosity > 0)
1163 chattygot += fetch_req.verbosity;
1165 for (i = 0; i < fetch_req.n_have_refs; i++) {
1166 struct got_object_id *id;
1169 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1170 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1174 if (imsg.hdr.type == GOT_IMSG_STOP)
1176 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1177 err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1181 if (datalen < sizeof(href)) {
1182 err = got_error(GOT_ERR_PRIVSEP_LEN);
1185 memcpy(&href, imsg.data, sizeof(href));
1186 if (datalen - sizeof(href) < href.name_len) {
1187 err = got_error(GOT_ERR_PRIVSEP_LEN);
1190 refname = malloc(href.name_len + 1);
1191 if (refname == NULL) {
1192 err = got_error_from_errno("malloc");
1195 memcpy(refname, imsg.data + sizeof(href), href.name_len);
1196 refname[href.name_len] = '\0';
1198 id = malloc(sizeof(*id));
1201 err = got_error_from_errno("malloc");
1204 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1205 err = got_pathlist_append(&have_refs, refname, id);
1215 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1218 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1219 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1223 if (imsg.hdr.type == GOT_IMSG_STOP)
1225 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1226 err = got_error(GOT_ERR_PRIVSEP_MSG);
1229 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1230 if (datalen < sizeof(wbranch)) {
1231 err = got_error(GOT_ERR_PRIVSEP_LEN);
1234 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1235 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1236 err = got_error(GOT_ERR_PRIVSEP_LEN);
1239 refname = malloc(wbranch.name_len + 1);
1240 if (refname == NULL) {
1241 err = got_error_from_errno("malloc");
1244 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1245 refname[wbranch.name_len] = '\0';
1247 err = got_pathlist_append(&wanted_branches, refname, NULL);
1256 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1259 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1260 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1264 if (imsg.hdr.type == GOT_IMSG_STOP)
1266 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1267 err = got_error(GOT_ERR_PRIVSEP_MSG);
1270 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1271 if (datalen < sizeof(wref)) {
1272 err = got_error(GOT_ERR_PRIVSEP_LEN);
1275 memcpy(&wref, imsg.data, sizeof(wref));
1276 if (datalen - sizeof(wref) < wref.name_len) {
1277 err = got_error(GOT_ERR_PRIVSEP_LEN);
1280 refname = malloc(wref.name_len + 1);
1281 if (refname == NULL) {
1282 err = got_error_from_errno("malloc");
1285 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1286 refname[wref.name_len] = '\0';
1288 err = got_pathlist_append(&wanted_refs, refname, NULL);
1297 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1298 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1302 if (imsg.hdr.type == GOT_IMSG_STOP)
1304 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1305 err = got_error(GOT_ERR_PRIVSEP_MSG);
1308 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1309 err = got_error(GOT_ERR_PRIVSEP_LEN);
1314 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1315 fetch_req.fetch_all_branches, &wanted_branches,
1316 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1318 TAILQ_FOREACH(pe, &have_refs, entry) {
1319 free((char *)pe->path);
1322 got_pathlist_free(&have_refs);
1323 TAILQ_FOREACH(pe, &wanted_branches, entry)
1324 free((char *)pe->path);
1325 got_pathlist_free(&wanted_branches);
1326 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1327 err = got_error_from_errno("close");
1328 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1329 err = got_error_from_errno("close");
1331 got_privsep_send_error(&ibuf, err);
1333 err = send_fetch_done(&ibuf, pack_sha1);
1335 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1336 got_privsep_send_error(&ibuf, err);