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>
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++) {
326 static const struct got_error *
327 parse_refline(char **id_str, char **refname, char **server_capabilities,
330 const struct got_error *err = NULL;
333 err = tokenize_refline(tokens, line, len, nitems(tokens));
340 *refname = tokens[1];
343 *server_capabilities = tokens[2];
344 p = strrchr(*server_capabilities, '\n');
352 #define GOT_CAPA_AGENT "agent"
353 #define GOT_CAPA_OFS_DELTA "ofs-delta"
354 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
356 #define GOT_SIDEBAND_PACKFILE_DATA 1
357 #define GOT_SIDEBAND_PROGRESS_INFO 2
358 #define GOT_SIDEBAND_ERROR_INFO 3
361 struct got_capability {
365 static const struct got_capability got_capabilities[] = {
366 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
367 { GOT_CAPA_OFS_DELTA, NULL },
368 { GOT_CAPA_SIDE_BAND_64K, NULL },
371 static const struct got_error *
372 match_capability(char **my_capabilities, const char *capa,
373 const struct got_capability *mycapa)
378 equalsign = strchr(capa, '=');
380 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
383 if (strcmp(capa, mycapa->key) != 0)
387 if (asprintf(&s, "%s %s%s%s",
388 *my_capabilities != NULL ? *my_capabilities : "",
390 mycapa->value != NULL ? "=" : "",
391 mycapa->value != NULL? mycapa->value : "") == -1)
392 return got_error_from_errno("asprintf");
394 free(*my_capabilities);
395 *my_capabilities = s;
399 static const struct got_error *
400 add_symref(struct got_pathlist_head *symrefs, char *capa)
402 const struct got_error *err = NULL;
403 char *colon, *name = NULL, *target = NULL;
405 /* Need at least "A:B" */
406 if (strlen(capa) < 3)
409 colon = strchr(capa, ':');
416 return got_error_from_errno("strdup");
418 target = strdup(colon + 1);
419 if (target == NULL) {
420 err = got_error_from_errno("strdup");
424 /* We can't validate the ref itself here. The main process will. */
425 err = got_pathlist_append(symrefs, name, target);
434 static const struct got_error *
435 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
436 char *server_capabilities)
438 const struct got_error *err = NULL;
439 char *capa, *equalsign;
442 *my_capabilities = NULL;
444 capa = strsep(&server_capabilities, " ");
448 equalsign = strchr(capa, '=');
449 if (equalsign != NULL &&
450 strncmp(capa, "symref", equalsign - capa) == 0) {
451 err = add_symref(symrefs, equalsign + 1);
457 for (i = 0; i < nitems(got_capabilities); i++) {
458 err = match_capability(my_capabilities,
459 capa, &got_capabilities[i]);
465 if (*my_capabilities == NULL) {
466 *my_capabilities = strdup("");
467 if (*my_capabilities == NULL)
468 err = got_error_from_errno("strdup");
473 static const struct got_error *
474 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
476 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
477 return got_error(GOT_ERR_NO_SPACE);
482 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
484 return got_error_from_errno(
485 "imsg_compose FETCH_SERVER_PROGRESS");
487 return got_privsep_flush_imsg(ibuf);
490 static const struct got_error *
491 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
493 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
494 &bytes, sizeof(bytes)) == -1)
495 return got_error_from_errno(
496 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
498 return got_privsep_flush_imsg(ibuf);
501 static const struct got_error *
502 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
504 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
505 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
506 return got_error_from_errno("imsg_compose FETCH");
507 return got_privsep_flush_imsg(ibuf);
512 static const struct got_error *
513 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
521 * Truncate messages which exceed the maximum imsg payload size.
522 * Server may send up to 64k.
524 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
525 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
527 /* Only allow printable ASCII. */
528 for (i = 0; i < len; i++) {
529 if (isprint((unsigned char)buf[i]) ||
530 isspace((unsigned char)buf[i]))
532 return got_error_msg(GOT_ERR_BAD_PACKET,
533 "non-printable progress message received from server");
536 return send_fetch_server_progress(ibuf, buf, len);
539 static const struct got_error *
540 fetch_error(const char *buf, size_t len)
542 static char msg[1024];
545 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
546 if (!isprint(buf[i]))
547 return got_error_msg(GOT_ERR_BAD_PACKET,
548 "non-printable error message received from server");
552 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
555 static const struct got_error *
556 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
558 const struct got_error *err = NULL;
560 size_t len, nsymrefs = 0;
561 struct got_pathlist_entry *pe;
563 len = sizeof(struct got_imsg_fetch_symrefs);
564 TAILQ_FOREACH(pe, symrefs, entry) {
565 const char *target = pe->data;
566 len += sizeof(struct got_imsg_fetch_symref) +
567 pe->path_len + strlen(target);
571 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
572 return got_error(GOT_ERR_NO_SPACE);
574 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
576 return got_error_from_errno("imsg_create FETCH_SYMREFS");
578 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
579 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
580 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
585 TAILQ_FOREACH(pe, symrefs, entry) {
586 const char *name = pe->path;
587 size_t name_len = pe->path_len;
588 const char *target = pe->data;
589 size_t target_len = strlen(target);
591 /* Keep in sync with struct got_imsg_fetch_symref definition! */
592 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
593 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
597 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
598 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
602 if (imsg_add(wbuf, name, name_len) == -1) {
603 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
607 if (imsg_add(wbuf, target, target_len) == -1) {
608 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
615 imsg_close(ibuf, wbuf);
616 return got_privsep_flush_imsg(ibuf);
619 static const struct got_error *
620 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
623 const struct got_error *err = NULL;
625 size_t len, reflen = strlen(refname);
627 len = sizeof(struct got_imsg_fetch_ref) + reflen;
628 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
629 return got_error(GOT_ERR_NO_SPACE);
631 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
633 return got_error_from_errno("imsg_create FETCH_REF");
635 /* Keep in sync with struct got_imsg_fetch_ref definition! */
636 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
637 err = got_error_from_errno("imsg_add FETCH_REF");
641 if (imsg_add(wbuf, refname, reflen) == -1) {
642 err = got_error_from_errno("imsg_add FETCH_REF");
648 imsg_close(ibuf, wbuf);
649 return got_privsep_flush_imsg(ibuf);
652 static const struct got_error *
653 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
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 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
675 size_t sha1_buf_len = 0;
678 TAILQ_INIT(&symrefs);
681 have = malloc(refsz * sizeof(have[0]));
683 return got_error_from_errno("malloc");
684 want = malloc(refsz * sizeof(want[0]));
686 err = got_error_from_errno("malloc");
690 err = readpkt(&n, fd, buf, sizeof(buf));
695 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
696 err = fetch_error(&buf[4], n - 4);
699 err = parse_refline(&id_str, &refname, &server_capabilities,
704 if (chattygot && server_capabilities[0] != '\0')
705 fprintf(stderr, "%s: server capabilities: %s\n",
706 getprogname(), server_capabilities);
707 err = match_capabilities(&my_capabilities, &symrefs,
708 server_capabilities);
712 fprintf(stderr, "%s: my capabilities:%s\n",
713 getprogname(), my_capabilities);
714 err = send_fetch_symrefs(ibuf, &symrefs);
718 if (!fetch_all_branches) {
719 TAILQ_FOREACH(pe, &symrefs, entry) {
720 const char *name = pe->path;
721 const char *symref_target = pe->data;
722 if (strcmp(name, GOT_REF_HEAD) != 0)
724 default_branch = symref_target;
730 if (strstr(refname, "^{}")) {
732 fprintf(stderr, "%s: ignoring %s\n",
733 getprogname(), refname);
738 if (strncmp(refname, "refs/heads/", 11) == 0) {
739 if (fetch_all_branches || list_refs_only) {
741 } else if (!TAILQ_EMPTY(wanted_branches)) {
742 TAILQ_FOREACH(pe, wanted_branches, entry) {
743 if (match_branch(refname, pe->path))
750 getprogname(), refname);
755 } else if (default_branch != NULL) {
756 if (!match_branch(refname, default_branch)) {
760 getprogname(), refname);
766 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
767 if (!TAILQ_EMPTY(wanted_refs)) {
768 TAILQ_FOREACH(pe, wanted_refs, entry) {
769 if (match_wanted_ref(refname, pe->path))
776 getprogname(), refname);
781 } else if (!list_refs_only) {
783 fprintf(stderr, "%s: ignoring %s\n",
784 getprogname(), refname);
790 if (refsz == nref + 1) {
792 have = reallocarray(have, refsz, sizeof(have[0]));
794 err = got_error_from_errno("reallocarray");
797 want = reallocarray(want, refsz, sizeof(want[0]));
799 err = got_error_from_errno("reallocarray");
803 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
804 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
807 match_remote_ref(have_refs, &have[nref], refname);
808 err = send_fetch_ref(ibuf, &want[nref], refname);
813 fprintf(stderr, "%s: %s will be fetched\n",
814 getprogname(), refname);
817 err = got_object_id_str(&theirs, &want[nref]);
820 err = got_object_id_str(&mine, &have[nref]);
825 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
826 getprogname(), theirs, getprogname(), mine);
836 /* Abort if we haven't found any branch to fetch. */
838 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
842 for (i = 0; i < nref; i++) {
843 if (got_object_id_cmp(&have[i], &want[i]) == 0)
845 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
846 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
847 sent_my_capabilites ? "" : my_capabilities);
848 if (n >= sizeof(buf)) {
849 err = got_error(GOT_ERR_NO_SPACE);
852 err = writepkt(fd, buf, n);
855 sent_my_capabilites = 1;
865 for (i = 0; i < nref; i++) {
866 if (got_object_id_cmp(&have[i], &zhash) == 0)
868 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
869 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
870 if (n >= sizeof(buf)) {
871 err = got_error(GOT_ERR_NO_SPACE);
874 err = writepkt(fd, buf, n);
880 while (nhave > 0 && !acked) {
881 struct got_object_id common_id;
883 /* The server should ACK the object IDs we need. */
884 err = readpkt(&n, fd, buf, sizeof(buf));
887 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
888 err = fetch_error(&buf[4], n - 4);
891 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
892 /* Server has not located our objects yet. */
895 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
896 strncmp(buf, "ACK ", 4) != 0) {
897 err = got_error_msg(GOT_ERR_BAD_PACKET,
898 "unexpected message from server");
901 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
902 err = got_error_msg(GOT_ERR_BAD_PACKET,
903 "bad object ID in ACK packet from server");
909 n = snprintf(buf, sizeof(buf), "done\n");
910 err = writepkt(fd, buf, n);
915 err = readpkt(&n, fd, buf, sizeof(buf));
918 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
919 err = got_error_msg(GOT_ERR_BAD_PACKET,
920 "unexpected message from server");
926 fprintf(stderr, "%s: fetching...\n", getprogname());
928 if (my_capabilities != NULL &&
929 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
936 if (have_sidebands) {
937 err = read_pkthdr(&datalen, fd);
943 /* Read sideband channel ID (one byte). */
944 r = read(fd, buf, 1);
946 err = got_error_from_errno("read");
950 err = got_error_msg(GOT_ERR_BAD_PACKET,
954 if (datalen > sizeof(buf) - 5) {
955 err = got_error_msg(GOT_ERR_BAD_PACKET,
956 "bad packet length");
959 datalen--; /* sideband ID has been read */
960 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
961 /* Read packfile data. */
962 err = readn(&r, fd, buf, datalen);
966 err = got_error_msg(GOT_ERR_BAD_PACKET,
970 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
971 err = readn(&r, fd, buf, datalen);
975 err = got_error_msg(GOT_ERR_BAD_PACKET,
979 err = fetch_progress(ibuf, buf, r);
983 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
984 err = readn(&r, fd, buf, datalen);
988 err = got_error_msg(GOT_ERR_BAD_PACKET,
992 err = fetch_error(buf, r);
994 } else if (buf[0] == 'A') {
995 err = readn(&r, fd, buf, datalen);
999 err = got_error_msg(GOT_ERR_BAD_PACKET,
1000 "packet too short");
1004 * Git server responds with ACK after 'done'
1005 * even though multi_ack is disabled?!?
1008 if (strncmp(buf, "CK ", 3) == 0)
1009 continue; /* ignore */
1010 err = got_error_msg(GOT_ERR_BAD_PACKET,
1011 "unexpected message from server");
1014 err = got_error_msg(GOT_ERR_BAD_PACKET,
1015 "unknown side-band received from server");
1019 /* No sideband channel. Every byte is packfile data. */
1020 err = readn(&r, fd, buf, sizeof buf);
1028 * An expected SHA1 checksum sits at the end of the pack file.
1029 * Since we don't know the file size ahead of time we have to
1030 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
1031 * those bytes into our SHA1 checksum computation until we
1032 * know for sure that additional pack file data bytes follow.
1034 * We can assume r > 0 since otherwise the loop would exit.
1036 if (r < SHA1_DIGEST_LENGTH) {
1037 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
1039 * If there's enough buffered + read data to
1040 * fill up the buffer then shift a sufficient
1041 * amount of bytes out at the front to make
1042 * room, mixing those bytes into the checksum.
1044 while (sha1_buf_len > 0 &&
1045 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
1046 SHA1Update(&sha1_ctx, sha1_buf, 1);
1047 memmove(sha1_buf, sha1_buf + 1, 1);
1051 /* Buffer potential checksum bytes. */
1052 memcpy(sha1_buf + sha1_buf_len, buf, r);
1056 * Mix in previously buffered bytes which
1057 * are not part of the checksum after all.
1059 SHA1Update(&sha1_ctx, sha1_buf, r);
1061 /* Update potential checksum buffer. */
1062 memmove(sha1_buf, sha1_buf + r,
1064 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
1067 /* Mix in any previously buffered bytes. */
1068 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
1070 /* Mix in bytes read minus potential checksum bytes. */
1071 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
1073 /* Buffer potential checksum bytes. */
1074 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
1075 SHA1_DIGEST_LENGTH);
1076 sha1_buf_len = SHA1_DIGEST_LENGTH;
1079 /* Write packfile data to temporary pack file. */
1080 w = write(packfd, buf, r);
1082 err = got_error_from_errno("write");
1086 err = got_error(GOT_ERR_IO);
1091 /* Don't send too many progress privsep messages. */
1092 if (packsz > last_reported_packsz + 1024) {
1093 err = send_fetch_download_progress(ibuf, packsz);
1096 last_reported_packsz = packsz;
1099 err = send_fetch_download_progress(ibuf, packsz);
1103 SHA1Final(pack_sha1, &sha1_ctx);
1104 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
1105 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
1106 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1107 "pack file checksum mismatch");
1110 TAILQ_FOREACH(pe, &symrefs, entry) {
1111 free((void *)pe->path);
1114 got_pathlist_free(&symrefs);
1119 free(server_capabilities);
1125 main(int argc, char **argv)
1127 const struct got_error *err = NULL;
1128 int fetchfd, packfd = -1, i;
1129 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
1130 struct imsgbuf ibuf;
1132 struct got_pathlist_head have_refs;
1133 struct got_pathlist_head wanted_branches;
1134 struct got_pathlist_head wanted_refs;
1135 struct got_pathlist_entry *pe;
1136 struct got_imsg_fetch_request fetch_req;
1137 struct got_imsg_fetch_have_ref href;
1138 struct got_imsg_fetch_wanted_branch wbranch;
1139 struct got_imsg_fetch_wanted_ref wref;
1142 static int attached;
1147 TAILQ_INIT(&have_refs);
1148 TAILQ_INIT(&wanted_branches);
1149 TAILQ_INIT(&wanted_refs);
1151 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1153 /* revoke access to most system calls */
1154 if (pledge("stdio recvfd", NULL) == -1) {
1155 err = got_error_from_errno("pledge");
1156 got_privsep_send_error(&ibuf, err);
1160 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1161 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1165 if (imsg.hdr.type == GOT_IMSG_STOP)
1167 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1168 err = got_error(GOT_ERR_PRIVSEP_MSG);
1171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1172 if (datalen < sizeof(fetch_req)) {
1173 err = got_error(GOT_ERR_PRIVSEP_LEN);
1176 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1180 if (fetch_req.verbosity > 0)
1181 chattygot += fetch_req.verbosity;
1183 for (i = 0; i < fetch_req.n_have_refs; i++) {
1184 struct got_object_id *id;
1187 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1188 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1192 if (imsg.hdr.type == GOT_IMSG_STOP)
1194 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1195 err = got_error(GOT_ERR_PRIVSEP_MSG);
1198 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1199 if (datalen < sizeof(href)) {
1200 err = got_error(GOT_ERR_PRIVSEP_LEN);
1203 memcpy(&href, imsg.data, sizeof(href));
1204 if (datalen - sizeof(href) < href.name_len) {
1205 err = got_error(GOT_ERR_PRIVSEP_LEN);
1208 refname = malloc(href.name_len + 1);
1209 if (refname == NULL) {
1210 err = got_error_from_errno("malloc");
1213 memcpy(refname, imsg.data + sizeof(href), href.name_len);
1214 refname[href.name_len] = '\0';
1216 id = malloc(sizeof(*id));
1219 err = got_error_from_errno("malloc");
1222 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1223 err = got_pathlist_append(&have_refs, refname, id);
1233 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1236 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1237 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1241 if (imsg.hdr.type == GOT_IMSG_STOP)
1243 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1244 err = got_error(GOT_ERR_PRIVSEP_MSG);
1247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1248 if (datalen < sizeof(wbranch)) {
1249 err = got_error(GOT_ERR_PRIVSEP_LEN);
1252 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1253 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1254 err = got_error(GOT_ERR_PRIVSEP_LEN);
1257 refname = malloc(wbranch.name_len + 1);
1258 if (refname == NULL) {
1259 err = got_error_from_errno("malloc");
1262 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1263 refname[wbranch.name_len] = '\0';
1265 err = got_pathlist_append(&wanted_branches, refname, NULL);
1274 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1277 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1278 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1282 if (imsg.hdr.type == GOT_IMSG_STOP)
1284 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1285 err = got_error(GOT_ERR_PRIVSEP_MSG);
1288 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1289 if (datalen < sizeof(wref)) {
1290 err = got_error(GOT_ERR_PRIVSEP_LEN);
1293 memcpy(&wref, imsg.data, sizeof(wref));
1294 if (datalen - sizeof(wref) < wref.name_len) {
1295 err = got_error(GOT_ERR_PRIVSEP_LEN);
1298 refname = malloc(wref.name_len + 1);
1299 if (refname == NULL) {
1300 err = got_error_from_errno("malloc");
1303 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1304 refname[wref.name_len] = '\0';
1306 err = got_pathlist_append(&wanted_refs, refname, NULL);
1315 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1316 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1320 if (imsg.hdr.type == GOT_IMSG_STOP)
1322 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1323 err = got_error(GOT_ERR_PRIVSEP_MSG);
1326 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1327 err = got_error(GOT_ERR_PRIVSEP_LEN);
1332 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1333 fetch_req.fetch_all_branches, &wanted_branches,
1334 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1336 TAILQ_FOREACH(pe, &have_refs, entry) {
1337 free((char *)pe->path);
1340 got_pathlist_free(&have_refs);
1341 TAILQ_FOREACH(pe, &wanted_branches, entry)
1342 free((char *)pe->path);
1343 got_pathlist_free(&wanted_branches);
1344 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1345 err = got_error_from_errno("close");
1346 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1347 err = got_error_from_errno("close");
1349 got_privsep_send_error(&ibuf, err);
1351 err = send_fetch_done(&ibuf, pack_sha1);
1353 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1354 got_privsep_send_error(&ibuf, err);