Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
22 #include <stdint.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <err.h>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_version.h"
41 #include "got_fetch.h"
42 #include "got_reference.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pkt.h"
51 #include "got_lib_gitproto.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct got_object *indexed;
58 static int chattygot;
59 static struct got_object_id zhash = {.sha1={0}};
61 static const struct got_capability got_capabilities[] = {
62 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
63 { GOT_CAPA_OFS_DELTA, NULL },
64 { GOT_CAPA_SIDE_BAND_64K, NULL },
65 };
67 static void
68 match_remote_ref(struct got_pathlist_head *have_refs,
69 struct got_object_id *my_id, char *refname)
70 {
71 struct got_pathlist_entry *pe;
73 /* XXX zero-hash signifies we don't have this ref;
74 * we should use a flag instead */
75 memset(my_id, 0, sizeof(*my_id));
77 TAILQ_FOREACH(pe, have_refs, entry) {
78 struct got_object_id *id = pe->data;
79 if (strcmp(pe->path, refname) == 0) {
80 memcpy(my_id, id, sizeof(*my_id));
81 break;
82 }
83 }
84 }
86 static int
87 match_branch(const char *branch, const char *wanted_branch)
88 {
89 if (strncmp(branch, "refs/heads/", 11) != 0)
90 return 0;
92 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
93 wanted_branch += 11;
95 return (strcmp(branch + 11, wanted_branch) == 0);
96 }
98 static int
99 match_wanted_ref(const char *refname, const char *wanted_ref)
101 if (strncmp(refname, "refs/", 5) != 0)
102 return 0;
103 refname += 5;
105 /*
106 * Prevent fetching of references that won't make any
107 * sense outside of the remote repository's context.
108 */
109 if (strncmp(refname, "got/", 4) == 0)
110 return 0;
111 if (strncmp(refname, "remotes/", 8) == 0)
112 return 0;
114 if (strncmp(wanted_ref, "refs/", 5) == 0)
115 wanted_ref += 5;
117 /* Allow prefix match. */
118 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
119 return 1;
121 /* Allow exact match. */
122 return (strcmp(refname, wanted_ref) == 0);
125 static const struct got_error *
126 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
128 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
129 return got_error(GOT_ERR_NO_SPACE);
131 if (msglen == 0)
132 return NULL;
134 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
135 msg, msglen) == -1)
136 return got_error_from_errno(
137 "imsg_compose FETCH_SERVER_PROGRESS");
139 return got_privsep_flush_imsg(ibuf);
142 static const struct got_error *
143 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
145 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
146 &bytes, sizeof(bytes)) == -1)
147 return got_error_from_errno(
148 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
150 return got_privsep_flush_imsg(ibuf);
153 static const struct got_error *
154 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
156 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
157 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
158 return got_error_from_errno("imsg_compose FETCH");
159 return got_privsep_flush_imsg(ibuf);
162 static const struct got_error *
163 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
165 size_t i;
167 if (len == 0)
168 return NULL;
170 /*
171 * Truncate messages which exceed the maximum imsg payload size.
172 * Server may send up to 64k.
173 */
174 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
175 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
177 /* Only allow printable ASCII. */
178 for (i = 0; i < len; i++) {
179 if (isprint((unsigned char)buf[i]) ||
180 isspace((unsigned char)buf[i]))
181 continue;
182 return got_error_msg(GOT_ERR_BAD_PACKET,
183 "non-printable progress message received from server");
186 return send_fetch_server_progress(ibuf, buf, len);
189 static const struct got_error *
190 fetch_error(const char *buf, size_t len)
192 static char msg[1024];
193 size_t i;
195 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
196 if (!isprint(buf[i]))
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable error message received from server");
199 msg[i] = buf[i];
201 msg[i] = '\0';
202 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
205 static const struct got_error *
206 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
208 const struct got_error *err = NULL;
209 struct ibuf *wbuf;
210 size_t len, nsymrefs = 0;
211 struct got_pathlist_entry *pe;
213 len = sizeof(struct got_imsg_fetch_symrefs);
214 TAILQ_FOREACH(pe, symrefs, entry) {
215 const char *target = pe->data;
216 len += sizeof(struct got_imsg_fetch_symref) +
217 pe->path_len + strlen(target);
218 nsymrefs++;
221 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
225 if (wbuf == NULL)
226 return got_error_from_errno("imsg_create FETCH_SYMREFS");
228 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
229 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
230 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
231 ibuf_free(wbuf);
232 return err;
235 TAILQ_FOREACH(pe, symrefs, entry) {
236 const char *name = pe->path;
237 size_t name_len = pe->path_len;
238 const char *target = pe->data;
239 size_t target_len = strlen(target);
241 /* Keep in sync with struct got_imsg_fetch_symref definition! */
242 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
243 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
244 ibuf_free(wbuf);
245 return err;
247 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
248 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
249 ibuf_free(wbuf);
250 return err;
252 if (imsg_add(wbuf, name, name_len) == -1) {
253 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
254 ibuf_free(wbuf);
255 return err;
257 if (imsg_add(wbuf, target, target_len) == -1) {
258 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
259 ibuf_free(wbuf);
260 return err;
264 wbuf->fd = -1;
265 imsg_close(ibuf, wbuf);
266 return got_privsep_flush_imsg(ibuf);
269 static const struct got_error *
270 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
271 const char *refname)
273 const struct got_error *err = NULL;
274 struct ibuf *wbuf;
275 size_t len, reflen = strlen(refname);
277 len = sizeof(struct got_imsg_fetch_ref) + reflen;
278 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 return got_error(GOT_ERR_NO_SPACE);
281 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
282 if (wbuf == NULL)
283 return got_error_from_errno("imsg_create FETCH_REF");
285 /* Keep in sync with struct got_imsg_fetch_ref definition! */
286 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
287 err = got_error_from_errno("imsg_add FETCH_REF");
288 ibuf_free(wbuf);
289 return err;
291 if (imsg_add(wbuf, refname, reflen) == -1) {
292 err = got_error_from_errno("imsg_add FETCH_REF");
293 ibuf_free(wbuf);
294 return err;
297 wbuf->fd = -1;
298 imsg_close(ibuf, wbuf);
299 return got_privsep_flush_imsg(ibuf);
302 static const struct got_error *
303 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
304 struct got_pathlist_head *have_refs, int fetch_all_branches,
305 struct got_pathlist_head *wanted_branches,
306 struct got_pathlist_head *wanted_refs, int list_refs_only,
307 struct imsgbuf *ibuf)
309 const struct got_error *err = NULL;
310 char buf[GOT_PKT_MAX];
311 char hashstr[SHA1_DIGEST_STRING_LENGTH];
312 struct got_object_id *have, *want;
313 int is_firstpkt = 1, nref = 0, refsz = 16;
314 int i, n, nwant = 0, nhave = 0, acked = 0;
315 off_t packsz = 0, last_reported_packsz = 0;
316 char *id_str = NULL, *refname = NULL;
317 char *server_capabilities = NULL, *my_capabilities = NULL;
318 const char *default_branch = NULL;
319 struct got_pathlist_head symrefs;
320 struct got_pathlist_entry *pe;
321 int sent_my_capabilites = 0, have_sidebands = 0;
322 int found_branch = 0;
323 SHA1_CTX sha1_ctx;
324 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
325 size_t sha1_buf_len = 0;
326 ssize_t w;
328 TAILQ_INIT(&symrefs);
329 SHA1Init(&sha1_ctx);
331 have = malloc(refsz * sizeof(have[0]));
332 if (have == NULL)
333 return got_error_from_errno("malloc");
334 want = malloc(refsz * sizeof(want[0]));
335 if (want == NULL) {
336 err = got_error_from_errno("malloc");
337 goto done;
339 while (1) {
340 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
341 if (err)
342 goto done;
343 if (n == 0)
344 break;
345 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
346 err = fetch_error(&buf[4], n - 4);
347 goto done;
349 err = got_gitproto_parse_refline(&id_str, &refname,
350 &server_capabilities, buf, n);
351 if (err)
352 goto done;
353 if (is_firstpkt) {
354 if (chattygot && server_capabilities[0] != '\0')
355 fprintf(stderr, "%s: server capabilities: %s\n",
356 getprogname(), server_capabilities);
357 err = got_gitproto_match_capabilities(&my_capabilities,
358 &symrefs, server_capabilities,
359 got_capabilities, nitems(got_capabilities));
360 if (err)
361 goto done;
362 if (chattygot)
363 fprintf(stderr, "%s: my capabilities:%s\n",
364 getprogname(), my_capabilities != NULL ?
365 my_capabilities : "");
366 err = send_fetch_symrefs(ibuf, &symrefs);
367 if (err)
368 goto done;
369 is_firstpkt = 0;
370 if (!fetch_all_branches) {
371 TAILQ_FOREACH(pe, &symrefs, entry) {
372 const char *name = pe->path;
373 const char *symref_target = pe->data;
374 if (strcmp(name, GOT_REF_HEAD) != 0)
375 continue;
376 default_branch = symref_target;
377 break;
380 continue;
382 if (strstr(refname, "^{}")) {
383 if (chattygot) {
384 fprintf(stderr, "%s: ignoring %s\n",
385 getprogname(), refname);
387 continue;
390 if (strncmp(refname, "refs/heads/", 11) == 0) {
391 if (fetch_all_branches || list_refs_only) {
392 found_branch = 1;
393 } else if (!TAILQ_EMPTY(wanted_branches)) {
394 TAILQ_FOREACH(pe, wanted_branches, entry) {
395 if (match_branch(refname, pe->path))
396 break;
398 if (pe == NULL) {
399 if (chattygot) {
400 fprintf(stderr,
401 "%s: ignoring %s\n",
402 getprogname(), refname);
404 continue;
406 found_branch = 1;
407 } else if (default_branch != NULL) {
408 if (!match_branch(refname, default_branch)) {
409 if (chattygot) {
410 fprintf(stderr,
411 "%s: ignoring %s\n",
412 getprogname(), refname);
414 continue;
416 found_branch = 1;
418 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
419 if (!TAILQ_EMPTY(wanted_refs)) {
420 TAILQ_FOREACH(pe, wanted_refs, entry) {
421 if (match_wanted_ref(refname, pe->path))
422 break;
424 if (pe == NULL) {
425 if (chattygot) {
426 fprintf(stderr,
427 "%s: ignoring %s\n",
428 getprogname(), refname);
430 continue;
432 found_branch = 1;
433 } else if (!list_refs_only) {
434 if (chattygot) {
435 fprintf(stderr, "%s: ignoring %s\n",
436 getprogname(), refname);
438 continue;
442 if (refsz == nref + 1) {
443 refsz *= 2;
444 have = reallocarray(have, refsz, sizeof(have[0]));
445 if (have == NULL) {
446 err = got_error_from_errno("reallocarray");
447 goto done;
449 want = reallocarray(want, refsz, sizeof(want[0]));
450 if (want == NULL) {
451 err = got_error_from_errno("reallocarray");
452 goto done;
455 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
456 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
457 goto done;
459 match_remote_ref(have_refs, &have[nref], refname);
460 err = send_fetch_ref(ibuf, &want[nref], refname);
461 if (err)
462 goto done;
464 if (chattygot)
465 fprintf(stderr, "%s: %s will be fetched\n",
466 getprogname(), refname);
467 if (chattygot > 1) {
468 char *theirs, *mine;
469 err = got_object_id_str(&theirs, &want[nref]);
470 if (err)
471 goto done;
472 err = got_object_id_str(&mine, &have[nref]);
473 if (err) {
474 free(theirs);
475 goto done;
477 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
478 getprogname(), theirs, getprogname(), mine);
479 free(theirs);
480 free(mine);
482 nref++;
485 if (list_refs_only)
486 goto done;
488 /* Abort if we haven't found any branch to fetch. */
489 if (!found_branch) {
490 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
491 goto done;
494 for (i = 0; i < nref; i++) {
495 if (got_object_id_cmp(&have[i], &want[i]) == 0)
496 continue;
497 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
498 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
499 sent_my_capabilites || my_capabilities == NULL ?
500 "" : my_capabilities);
501 if (n >= sizeof(buf)) {
502 err = got_error(GOT_ERR_NO_SPACE);
503 goto done;
505 err = got_pkt_writepkt(fd, buf, n, chattygot);
506 if (err)
507 goto done;
508 sent_my_capabilites = 1;
509 nwant++;
511 err = got_pkt_flushpkt(fd, chattygot);
512 if (err)
513 goto done;
515 if (nwant == 0)
516 goto done;
518 for (i = 0; i < nref; i++) {
519 if (got_object_id_cmp(&have[i], &zhash) == 0)
520 continue;
521 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
522 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
523 if (n >= sizeof(buf)) {
524 err = got_error(GOT_ERR_NO_SPACE);
525 goto done;
527 err = got_pkt_writepkt(fd, buf, n, chattygot);
528 if (err)
529 goto done;
530 nhave++;
533 while (nhave > 0 && !acked) {
534 struct got_object_id common_id;
536 /* The server should ACK the object IDs we need. */
537 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
538 if (err)
539 goto done;
540 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
541 err = fetch_error(&buf[4], n - 4);
542 goto done;
544 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
545 /* Server has not located our objects yet. */
546 continue;
548 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
549 strncmp(buf, "ACK ", 4) != 0) {
550 err = got_error_msg(GOT_ERR_BAD_PACKET,
551 "unexpected message from server");
552 goto done;
554 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
555 err = got_error_msg(GOT_ERR_BAD_PACKET,
556 "bad object ID in ACK packet from server");
557 goto done;
559 acked++;
562 n = snprintf(buf, sizeof(buf), "done\n");
563 err = got_pkt_writepkt(fd, buf, n, chattygot);
564 if (err)
565 goto done;
567 if (nhave == 0) {
568 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
569 if (err)
570 goto done;
571 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
572 err = got_error_msg(GOT_ERR_BAD_PACKET,
573 "unexpected message from server");
574 goto done;
578 if (chattygot)
579 fprintf(stderr, "%s: fetching...\n", getprogname());
581 if (my_capabilities != NULL &&
582 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
583 have_sidebands = 1;
585 while (1) {
586 ssize_t r = 0;
587 int datalen = -1;
589 if (have_sidebands) {
590 err = got_pkt_readhdr(&datalen, fd, chattygot);
591 if (err)
592 goto done;
593 if (datalen <= 0)
594 break;
596 /* Read sideband channel ID (one byte). */
597 r = read(fd, buf, 1);
598 if (r == -1) {
599 err = got_error_from_errno("read");
600 goto done;
602 if (r != 1) {
603 err = got_error_msg(GOT_ERR_BAD_PACKET,
604 "short packet");
605 goto done;
607 if (datalen > sizeof(buf) - 5) {
608 err = got_error_msg(GOT_ERR_BAD_PACKET,
609 "bad packet length");
610 goto done;
612 datalen--; /* sideband ID has been read */
613 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
614 /* Read packfile data. */
615 err = got_pkt_readn(&r, fd, buf, datalen);
616 if (err)
617 goto done;
618 if (r != datalen) {
619 err = got_error_msg(GOT_ERR_BAD_PACKET,
620 "packet too short");
621 goto done;
623 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
624 err = got_pkt_readn(&r, fd, buf, datalen);
625 if (err)
626 goto done;
627 if (r != datalen) {
628 err = got_error_msg(GOT_ERR_BAD_PACKET,
629 "packet too short");
630 goto done;
632 err = fetch_progress(ibuf, buf, r);
633 if (err)
634 goto done;
635 continue;
636 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
637 err = got_pkt_readn(&r, fd, buf, datalen);
638 if (err)
639 goto done;
640 if (r != datalen) {
641 err = got_error_msg(GOT_ERR_BAD_PACKET,
642 "packet too short");
643 goto done;
645 err = fetch_error(buf, r);
646 goto done;
647 } else if (buf[0] == 'A') {
648 err = got_pkt_readn(&r, fd, buf, datalen);
649 if (err)
650 goto done;
651 if (r != datalen) {
652 err = got_error_msg(GOT_ERR_BAD_PACKET,
653 "packet too short");
654 goto done;
656 /*
657 * Git server responds with ACK after 'done'
658 * even though multi_ack is disabled?!?
659 */
660 buf[r] = '\0';
661 if (strncmp(buf, "CK ", 3) == 0)
662 continue; /* ignore */
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "unexpected message from server");
665 goto done;
666 } else {
667 err = got_error_msg(GOT_ERR_BAD_PACKET,
668 "unknown side-band received from server");
669 goto done;
671 } else {
672 /* No sideband channel. Every byte is packfile data. */
673 err = got_pkt_readn(&r, fd, buf, sizeof buf);
674 if (err)
675 goto done;
676 if (r <= 0)
677 break;
680 /*
681 * An expected SHA1 checksum sits at the end of the pack file.
682 * Since we don't know the file size ahead of time we have to
683 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
684 * those bytes into our SHA1 checksum computation until we
685 * know for sure that additional pack file data bytes follow.
687 * We can assume r > 0 since otherwise the loop would exit.
688 */
689 if (r < SHA1_DIGEST_LENGTH) {
690 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
691 /*
692 * If there's enough buffered + read data to
693 * fill up the buffer then shift a sufficient
694 * amount of bytes out at the front to make
695 * room, mixing those bytes into the checksum.
696 */
697 while (sha1_buf_len > 0 &&
698 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
699 SHA1Update(&sha1_ctx, sha1_buf, 1);
700 memmove(sha1_buf, sha1_buf + 1, 1);
701 sha1_buf_len--;
704 /* Buffer potential checksum bytes. */
705 memcpy(sha1_buf + sha1_buf_len, buf, r);
706 sha1_buf_len += r;
707 } else {
708 /*
709 * Mix in previously buffered bytes which
710 * are not part of the checksum after all.
711 */
712 SHA1Update(&sha1_ctx, sha1_buf, r);
714 /* Update potential checksum buffer. */
715 memmove(sha1_buf, sha1_buf + r,
716 sha1_buf_len - r);
717 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
719 } else {
720 /* Mix in any previously buffered bytes. */
721 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
723 /* Mix in bytes read minus potential checksum bytes. */
724 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
726 /* Buffer potential checksum bytes. */
727 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
728 SHA1_DIGEST_LENGTH);
729 sha1_buf_len = SHA1_DIGEST_LENGTH;
732 /* Write packfile data to temporary pack file. */
733 w = write(packfd, buf, r);
734 if (w == -1) {
735 err = got_error_from_errno("write");
736 goto done;
738 if (w != r) {
739 err = got_error(GOT_ERR_IO);
740 goto done;
742 packsz += w;
744 /* Don't send too many progress privsep messages. */
745 if (packsz > last_reported_packsz + 1024) {
746 err = send_fetch_download_progress(ibuf, packsz);
747 if (err)
748 goto done;
749 last_reported_packsz = packsz;
752 err = send_fetch_download_progress(ibuf, packsz);
753 if (err)
754 goto done;
756 SHA1Final(pack_sha1, &sha1_ctx);
757 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
758 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
759 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
760 "pack file checksum mismatch");
762 done:
763 TAILQ_FOREACH(pe, &symrefs, entry) {
764 free((void *)pe->path);
765 free(pe->data);
767 got_pathlist_free(&symrefs);
768 free(have);
769 free(want);
770 free(id_str);
771 free(refname);
772 free(server_capabilities);
773 return err;
777 int
778 main(int argc, char **argv)
780 const struct got_error *err = NULL;
781 int fetchfd, packfd = -1;
782 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
783 struct imsgbuf ibuf;
784 struct imsg imsg;
785 struct got_pathlist_head have_refs;
786 struct got_pathlist_head wanted_branches;
787 struct got_pathlist_head wanted_refs;
788 struct got_pathlist_entry *pe;
789 struct got_imsg_fetch_request fetch_req;
790 struct got_imsg_fetch_have_ref href;
791 struct got_imsg_fetch_wanted_branch wbranch;
792 struct got_imsg_fetch_wanted_ref wref;
793 size_t datalen, i;
794 #if 0
795 static int attached;
796 while (!attached)
797 sleep (1);
798 #endif
800 TAILQ_INIT(&have_refs);
801 TAILQ_INIT(&wanted_branches);
802 TAILQ_INIT(&wanted_refs);
804 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
805 #ifndef PROFILE
806 /* revoke access to most system calls */
807 if (pledge("stdio recvfd", NULL) == -1) {
808 err = got_error_from_errno("pledge");
809 got_privsep_send_error(&ibuf, err);
810 return 1;
812 #endif
813 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
814 if (err) {
815 if (err->code == GOT_ERR_PRIVSEP_PIPE)
816 err = NULL;
817 goto done;
819 if (imsg.hdr.type == GOT_IMSG_STOP)
820 goto done;
821 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
822 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 goto done;
825 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
826 if (datalen < sizeof(fetch_req)) {
827 err = got_error(GOT_ERR_PRIVSEP_LEN);
828 goto done;
830 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
831 fetchfd = imsg.fd;
832 imsg_free(&imsg);
834 if (fetch_req.verbosity > 0)
835 chattygot += fetch_req.verbosity;
837 for (i = 0; i < fetch_req.n_have_refs; i++) {
838 struct got_object_id *id;
839 char *refname;
841 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
842 if (err) {
843 if (err->code == GOT_ERR_PRIVSEP_PIPE)
844 err = NULL;
845 goto done;
847 if (imsg.hdr.type == GOT_IMSG_STOP)
848 goto done;
849 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
850 err = got_error(GOT_ERR_PRIVSEP_MSG);
851 goto done;
853 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
854 if (datalen < sizeof(href)) {
855 err = got_error(GOT_ERR_PRIVSEP_LEN);
856 goto done;
858 memcpy(&href, imsg.data, sizeof(href));
859 if (datalen - sizeof(href) < href.name_len) {
860 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 goto done;
863 refname = malloc(href.name_len + 1);
864 if (refname == NULL) {
865 err = got_error_from_errno("malloc");
866 goto done;
868 memcpy(refname, imsg.data + sizeof(href), href.name_len);
869 refname[href.name_len] = '\0';
871 id = malloc(sizeof(*id));
872 if (id == NULL) {
873 free(refname);
874 err = got_error_from_errno("malloc");
875 goto done;
877 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
878 err = got_pathlist_append(&have_refs, refname, id);
879 if (err) {
880 free(refname);
881 free(id);
882 goto done;
885 imsg_free(&imsg);
888 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
889 char *refname;
891 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
892 if (err) {
893 if (err->code == GOT_ERR_PRIVSEP_PIPE)
894 err = NULL;
895 goto done;
897 if (imsg.hdr.type == GOT_IMSG_STOP)
898 goto done;
899 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
900 err = got_error(GOT_ERR_PRIVSEP_MSG);
901 goto done;
903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
904 if (datalen < sizeof(wbranch)) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
906 goto done;
908 memcpy(&wbranch, imsg.data, sizeof(wbranch));
909 if (datalen - sizeof(wbranch) < wbranch.name_len) {
910 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 goto done;
913 refname = malloc(wbranch.name_len + 1);
914 if (refname == NULL) {
915 err = got_error_from_errno("malloc");
916 goto done;
918 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
919 refname[wbranch.name_len] = '\0';
921 err = got_pathlist_append(&wanted_branches, refname, NULL);
922 if (err) {
923 free(refname);
924 goto done;
927 imsg_free(&imsg);
930 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
931 char *refname;
933 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
934 if (err) {
935 if (err->code == GOT_ERR_PRIVSEP_PIPE)
936 err = NULL;
937 goto done;
939 if (imsg.hdr.type == GOT_IMSG_STOP)
940 goto done;
941 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
942 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 goto done;
945 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
946 if (datalen < sizeof(wref)) {
947 err = got_error(GOT_ERR_PRIVSEP_LEN);
948 goto done;
950 memcpy(&wref, imsg.data, sizeof(wref));
951 if (datalen - sizeof(wref) < wref.name_len) {
952 err = got_error(GOT_ERR_PRIVSEP_LEN);
953 goto done;
955 refname = malloc(wref.name_len + 1);
956 if (refname == NULL) {
957 err = got_error_from_errno("malloc");
958 goto done;
960 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
961 refname[wref.name_len] = '\0';
963 err = got_pathlist_append(&wanted_refs, refname, NULL);
964 if (err) {
965 free(refname);
966 goto done;
969 imsg_free(&imsg);
972 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
973 if (err) {
974 if (err->code == GOT_ERR_PRIVSEP_PIPE)
975 err = NULL;
976 goto done;
978 if (imsg.hdr.type == GOT_IMSG_STOP)
979 goto done;
980 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
982 goto done;
984 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
985 err = got_error(GOT_ERR_PRIVSEP_LEN);
986 goto done;
988 packfd = imsg.fd;
990 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
991 fetch_req.fetch_all_branches, &wanted_branches,
992 &wanted_refs, fetch_req.list_refs_only, &ibuf);
993 done:
994 TAILQ_FOREACH(pe, &have_refs, entry) {
995 free((char *)pe->path);
996 free(pe->data);
998 got_pathlist_free(&have_refs);
999 TAILQ_FOREACH(pe, &wanted_branches, entry)
1000 free((char *)pe->path);
1001 got_pathlist_free(&wanted_branches);
1002 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1003 err = got_error_from_errno("close");
1004 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1006 if (err != NULL)
1007 got_privsep_send_error(&ibuf, err);
1008 else
1009 err = send_fetch_done(&ibuf, pack_sha1);
1010 if (err != NULL) {
1011 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1012 got_privsep_send_error(&ibuf, err);
1015 exit(0);