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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
23 #include <stdint.h>
24 #include <errno.h>
25 #include <imsg.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <sha1.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <err.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.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"
51 #include "got_lib_pkt.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_ratelimit.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 struct got_object *indexed;
60 static int chattygot;
62 static const struct got_capability got_capabilities[] = {
63 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
64 { GOT_CAPA_OFS_DELTA, NULL },
65 { GOT_CAPA_SIDE_BAND_64K, NULL },
66 };
68 static void
69 match_remote_ref(struct got_pathlist_head *have_refs,
70 struct got_object_id *my_id, char *refname)
71 {
72 struct got_pathlist_entry *pe;
74 /* XXX zero-hash signifies we don't have this ref;
75 * we should use a flag instead */
76 memset(my_id, 0, sizeof(*my_id));
78 TAILQ_FOREACH(pe, have_refs, entry) {
79 struct got_object_id *id = pe->data;
80 if (strcmp(pe->path, refname) == 0) {
81 memcpy(my_id, id, sizeof(*my_id));
82 break;
83 }
84 }
85 }
87 static int
88 match_branch(const char *branch, const char *wanted_branch)
89 {
90 if (strncmp(branch, "refs/heads/", 11) != 0)
91 return 0;
93 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
94 wanted_branch += 11;
96 return (strcmp(branch + 11, wanted_branch) == 0);
97 }
99 static int
100 match_wanted_ref(const char *refname, const char *wanted_ref)
102 if (strncmp(refname, "refs/", 5) != 0)
103 return 0;
104 refname += 5;
106 /*
107 * Prevent fetching of references that won't make any
108 * sense outside of the remote repository's context.
109 */
110 if (strncmp(refname, "got/", 4) == 0)
111 return 0;
112 if (strncmp(refname, "remotes/", 8) == 0)
113 return 0;
115 if (strncmp(wanted_ref, "refs/", 5) == 0)
116 wanted_ref += 5;
118 /* Allow prefix match. */
119 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
120 return 1;
122 /* Allow exact match. */
123 return (strcmp(refname, wanted_ref) == 0);
126 static const struct got_error *
127 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
129 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
130 return got_error(GOT_ERR_NO_SPACE);
132 if (msglen == 0)
133 return NULL;
135 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
136 msg, msglen) == -1)
137 return got_error_from_errno(
138 "imsg_compose FETCH_SERVER_PROGRESS");
140 return got_privsep_flush_imsg(ibuf);
143 static const struct got_error *
144 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
145 struct got_ratelimit *rl)
147 const struct got_error *err;
148 int elapsed = 0;
150 if (rl) {
151 err = got_ratelimit_check(&elapsed, rl);
152 if (err || !elapsed)
153 return err;
156 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
157 &bytes, sizeof(bytes)) == -1)
158 return got_error_from_errno(
159 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
161 return got_privsep_flush_imsg(ibuf);
164 static const struct got_error *
165 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
167 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
168 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
169 return got_error_from_errno("imsg_compose FETCH");
170 return got_privsep_flush_imsg(ibuf);
173 static const struct got_error *
174 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
176 size_t i;
178 if (len == 0)
179 return NULL;
181 /*
182 * Truncate messages which exceed the maximum imsg payload size.
183 * Server may send up to 64k.
184 */
185 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
186 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
188 /* Only allow printable ASCII. */
189 for (i = 0; i < len; i++) {
190 if (isprint((unsigned char)buf[i]) ||
191 isspace((unsigned char)buf[i]))
192 continue;
193 return got_error_msg(GOT_ERR_BAD_PACKET,
194 "non-printable progress message received from server");
197 return send_fetch_server_progress(ibuf, buf, len);
200 static const struct got_error *
201 fetch_error(const char *buf, size_t len)
203 static char msg[1024];
204 size_t i;
206 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
207 if (!isprint(buf[i]))
208 return got_error_msg(GOT_ERR_BAD_PACKET,
209 "non-printable error message received from server");
210 msg[i] = buf[i];
212 msg[i] = '\0';
213 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
216 static const struct got_error *
217 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
219 const struct got_error *err = NULL;
220 struct ibuf *wbuf;
221 size_t len, nsymrefs = 0;
222 struct got_pathlist_entry *pe;
224 len = sizeof(struct got_imsg_fetch_symrefs);
225 TAILQ_FOREACH(pe, symrefs, entry) {
226 const char *target = pe->data;
227 len += sizeof(struct got_imsg_fetch_symref) +
228 pe->path_len + strlen(target);
229 nsymrefs++;
232 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
233 return got_error(GOT_ERR_NO_SPACE);
235 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
236 if (wbuf == NULL)
237 return got_error_from_errno("imsg_create FETCH_SYMREFS");
239 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
240 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
241 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
242 ibuf_free(wbuf);
243 return err;
246 TAILQ_FOREACH(pe, symrefs, entry) {
247 const char *name = pe->path;
248 size_t name_len = pe->path_len;
249 const char *target = pe->data;
250 size_t target_len = strlen(target);
252 /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
254 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
255 ibuf_free(wbuf);
256 return err;
258 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
259 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
260 ibuf_free(wbuf);
261 return err;
263 if (imsg_add(wbuf, name, name_len) == -1) {
264 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
265 ibuf_free(wbuf);
266 return err;
268 if (imsg_add(wbuf, target, target_len) == -1) {
269 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
270 ibuf_free(wbuf);
271 return err;
275 wbuf->fd = -1;
276 imsg_close(ibuf, wbuf);
277 return got_privsep_flush_imsg(ibuf);
280 static const struct got_error *
281 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
282 const char *refname)
284 const struct got_error *err = NULL;
285 struct ibuf *wbuf;
286 size_t len, reflen = strlen(refname);
288 len = sizeof(struct got_imsg_fetch_ref) + reflen;
289 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
290 return got_error(GOT_ERR_NO_SPACE);
292 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
293 if (wbuf == NULL)
294 return got_error_from_errno("imsg_create FETCH_REF");
296 /* Keep in sync with struct got_imsg_fetch_ref definition! */
297 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
298 err = got_error_from_errno("imsg_add FETCH_REF");
299 ibuf_free(wbuf);
300 return err;
302 if (imsg_add(wbuf, refname, reflen) == -1) {
303 err = got_error_from_errno("imsg_add FETCH_REF");
304 ibuf_free(wbuf);
305 return err;
308 wbuf->fd = -1;
309 imsg_close(ibuf, wbuf);
310 return got_privsep_flush_imsg(ibuf);
313 static const struct got_error *
314 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
315 struct got_pathlist_head *have_refs, int fetch_all_branches,
316 struct got_pathlist_head *wanted_branches,
317 struct got_pathlist_head *wanted_refs, int list_refs_only,
318 struct imsgbuf *ibuf)
320 const struct got_error *err = NULL;
321 char buf[GOT_PKT_MAX];
322 char hashstr[SHA1_DIGEST_STRING_LENGTH];
323 struct got_object_id *have, *want;
324 int is_firstpkt = 1, nref = 0, refsz = 16;
325 int i, n, nwant = 0, nhave = 0, acked = 0;
326 off_t packsz = 0, last_reported_packsz = 0;
327 char *id_str = NULL, *refname = NULL;
328 char *server_capabilities = NULL, *my_capabilities = NULL;
329 const char *default_branch = NULL;
330 struct got_pathlist_head symrefs;
331 struct got_pathlist_entry *pe;
332 int sent_my_capabilites = 0, have_sidebands = 0;
333 int found_branch = 0;
334 SHA1_CTX sha1_ctx;
335 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
336 size_t sha1_buf_len = 0;
337 ssize_t w;
338 struct got_ratelimit rl;
340 TAILQ_INIT(&symrefs);
341 SHA1Init(&sha1_ctx);
342 got_ratelimit_init(&rl, 0, 500);
344 have = malloc(refsz * sizeof(have[0]));
345 if (have == NULL)
346 return got_error_from_errno("malloc");
347 want = malloc(refsz * sizeof(want[0]));
348 if (want == NULL) {
349 err = got_error_from_errno("malloc");
350 goto done;
352 while (1) {
353 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
354 if (err)
355 goto done;
356 if (n == 0)
357 break;
358 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
359 err = fetch_error(&buf[4], n - 4);
360 goto done;
362 free(id_str);
363 free(refname);
364 err = got_gitproto_parse_refline(&id_str, &refname,
365 &server_capabilities, buf, n);
366 if (err)
367 goto done;
368 if (is_firstpkt) {
369 if (chattygot && server_capabilities[0] != '\0')
370 fprintf(stderr, "%s: server capabilities: %s\n",
371 getprogname(), server_capabilities);
372 err = got_gitproto_match_capabilities(&my_capabilities,
373 &symrefs, server_capabilities,
374 got_capabilities, nitems(got_capabilities));
375 if (err)
376 goto done;
377 if (chattygot)
378 fprintf(stderr, "%s: my capabilities:%s\n",
379 getprogname(), my_capabilities != NULL ?
380 my_capabilities : "");
381 err = send_fetch_symrefs(ibuf, &symrefs);
382 if (err)
383 goto done;
384 is_firstpkt = 0;
385 if (!fetch_all_branches) {
386 TAILQ_FOREACH(pe, &symrefs, entry) {
387 const char *name = pe->path;
388 const char *symref_target = pe->data;
389 if (strcmp(name, GOT_REF_HEAD) != 0)
390 continue;
391 default_branch = symref_target;
392 break;
395 continue;
397 if (strstr(refname, "^{}")) {
398 if (chattygot) {
399 fprintf(stderr, "%s: ignoring %s\n",
400 getprogname(), refname);
402 continue;
405 if (strncmp(refname, "refs/heads/", 11) == 0) {
406 if (fetch_all_branches || list_refs_only) {
407 found_branch = 1;
408 } else if (!TAILQ_EMPTY(wanted_branches)) {
409 TAILQ_FOREACH(pe, wanted_branches, entry) {
410 if (match_branch(refname, pe->path))
411 break;
413 if (pe == NULL) {
414 if (chattygot) {
415 fprintf(stderr,
416 "%s: ignoring %s\n",
417 getprogname(), refname);
419 continue;
421 found_branch = 1;
422 } else if (default_branch != NULL) {
423 if (!match_branch(refname, default_branch)) {
424 if (chattygot) {
425 fprintf(stderr,
426 "%s: ignoring %s\n",
427 getprogname(), refname);
429 continue;
431 found_branch = 1;
433 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
434 if (!TAILQ_EMPTY(wanted_refs)) {
435 TAILQ_FOREACH(pe, wanted_refs, entry) {
436 if (match_wanted_ref(refname, pe->path))
437 break;
439 if (pe == NULL) {
440 if (chattygot) {
441 fprintf(stderr,
442 "%s: ignoring %s\n",
443 getprogname(), refname);
445 continue;
447 found_branch = 1;
448 } else if (!list_refs_only) {
449 if (chattygot) {
450 fprintf(stderr, "%s: ignoring %s\n",
451 getprogname(), refname);
453 continue;
457 if (refsz == nref + 1) {
458 refsz *= 2;
459 have = reallocarray(have, refsz, sizeof(have[0]));
460 if (have == NULL) {
461 err = got_error_from_errno("reallocarray");
462 goto done;
464 want = reallocarray(want, refsz, sizeof(want[0]));
465 if (want == NULL) {
466 err = got_error_from_errno("reallocarray");
467 goto done;
470 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
471 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
472 goto done;
474 match_remote_ref(have_refs, &have[nref], refname);
475 err = send_fetch_ref(ibuf, &want[nref], refname);
476 if (err)
477 goto done;
479 if (chattygot)
480 fprintf(stderr, "%s: %s will be fetched\n",
481 getprogname(), refname);
482 if (chattygot > 1) {
483 char *theirs, *mine;
484 err = got_object_id_str(&theirs, &want[nref]);
485 if (err)
486 goto done;
487 err = got_object_id_str(&mine, &have[nref]);
488 if (err) {
489 free(theirs);
490 goto done;
492 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
493 getprogname(), theirs, getprogname(), mine);
494 free(theirs);
495 free(mine);
497 nref++;
500 if (list_refs_only)
501 goto done;
503 /* Abort if we haven't found any branch to fetch. */
504 if (!found_branch) {
505 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
506 goto done;
509 for (i = 0; i < nref; i++) {
510 if (got_object_id_cmp(&have[i], &want[i]) == 0)
511 continue;
512 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
513 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
514 sent_my_capabilites || my_capabilities == NULL ?
515 "" : my_capabilities);
516 if (n >= sizeof(buf)) {
517 err = got_error(GOT_ERR_NO_SPACE);
518 goto done;
520 err = got_pkt_writepkt(fd, buf, n, chattygot);
521 if (err)
522 goto done;
523 sent_my_capabilites = 1;
524 nwant++;
526 err = got_pkt_flushpkt(fd, chattygot);
527 if (err)
528 goto done;
530 if (nwant == 0)
531 goto done;
533 TAILQ_FOREACH(pe, have_refs, entry) {
534 struct got_object_id *id = pe->data;
535 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
536 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
537 if (n >= sizeof(buf)) {
538 err = got_error(GOT_ERR_NO_SPACE);
539 goto done;
541 err = got_pkt_writepkt(fd, buf, n, chattygot);
542 if (err)
543 goto done;
544 nhave++;
547 while (nhave > 0 && !acked) {
548 struct got_object_id common_id;
550 /* The server should ACK the object IDs we need. */
551 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
552 if (err)
553 goto done;
554 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
555 err = fetch_error(&buf[4], n - 4);
556 goto done;
558 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
559 /* Server has not located our objects yet. */
560 continue;
562 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
563 strncmp(buf, "ACK ", 4) != 0) {
564 err = got_error_msg(GOT_ERR_BAD_PACKET,
565 "unexpected message from server");
566 goto done;
568 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
569 err = got_error_msg(GOT_ERR_BAD_PACKET,
570 "bad object ID in ACK packet from server");
571 goto done;
573 acked++;
576 n = snprintf(buf, sizeof(buf), "done\n");
577 err = got_pkt_writepkt(fd, buf, n, chattygot);
578 if (err)
579 goto done;
581 if (nhave == 0) {
582 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
583 if (err)
584 goto done;
585 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
586 err = got_error_msg(GOT_ERR_BAD_PACKET,
587 "unexpected message from server");
588 goto done;
592 if (chattygot)
593 fprintf(stderr, "%s: fetching...\n", getprogname());
595 if (my_capabilities != NULL &&
596 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
597 have_sidebands = 1;
599 while (1) {
600 ssize_t r = 0;
601 int datalen = -1;
603 if (have_sidebands) {
604 err = got_pkt_readhdr(&datalen, fd, chattygot);
605 if (err)
606 goto done;
607 if (datalen <= 0)
608 break;
610 /* Read sideband channel ID (one byte). */
611 r = read(fd, buf, 1);
612 if (r == -1) {
613 err = got_error_from_errno("read");
614 goto done;
616 if (r != 1) {
617 err = got_error_msg(GOT_ERR_BAD_PACKET,
618 "short packet");
619 goto done;
621 if (datalen > sizeof(buf) - 5) {
622 err = got_error_msg(GOT_ERR_BAD_PACKET,
623 "bad packet length");
624 goto done;
626 datalen--; /* sideband ID has been read */
627 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
628 /* Read packfile data. */
629 err = got_pkt_readn(&r, fd, buf, datalen);
630 if (err)
631 goto done;
632 if (r != datalen) {
633 err = got_error_msg(GOT_ERR_BAD_PACKET,
634 "packet too short");
635 goto done;
637 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
638 err = got_pkt_readn(&r, fd, buf, datalen);
639 if (err)
640 goto done;
641 if (r != datalen) {
642 err = got_error_msg(GOT_ERR_BAD_PACKET,
643 "packet too short");
644 goto done;
646 err = fetch_progress(ibuf, buf, r);
647 if (err)
648 goto done;
649 continue;
650 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
651 err = got_pkt_readn(&r, fd, buf, datalen);
652 if (err)
653 goto done;
654 if (r != datalen) {
655 err = got_error_msg(GOT_ERR_BAD_PACKET,
656 "packet too short");
657 goto done;
659 err = fetch_error(buf, r);
660 goto done;
661 } else if (buf[0] == 'A') {
662 err = got_pkt_readn(&r, fd, buf, datalen);
663 if (err)
664 goto done;
665 if (r != datalen) {
666 err = got_error_msg(GOT_ERR_BAD_PACKET,
667 "packet too short");
668 goto done;
670 /*
671 * Git server responds with ACK after 'done'
672 * even though multi_ack is disabled?!?
673 */
674 buf[r] = '\0';
675 if (strncmp(buf, "CK ", 3) == 0)
676 continue; /* ignore */
677 err = got_error_msg(GOT_ERR_BAD_PACKET,
678 "unexpected message from server");
679 goto done;
680 } else {
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 "unknown side-band received from server");
683 goto done;
685 } else {
686 /* No sideband channel. Every byte is packfile data. */
687 err = got_pkt_readn(&r, fd, buf, sizeof buf);
688 if (err)
689 goto done;
690 if (r <= 0)
691 break;
694 /*
695 * An expected SHA1 checksum sits at the end of the pack file.
696 * Since we don't know the file size ahead of time we have to
697 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
698 * those bytes into our SHA1 checksum computation until we
699 * know for sure that additional pack file data bytes follow.
701 * We can assume r > 0 since otherwise the loop would exit.
702 */
703 if (r < SHA1_DIGEST_LENGTH) {
704 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
705 /*
706 * If there's enough buffered + read data to
707 * fill up the buffer then shift a sufficient
708 * amount of bytes out at the front to make
709 * room, mixing those bytes into the checksum.
710 */
711 while (sha1_buf_len > 0 &&
712 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
713 SHA1Update(&sha1_ctx, sha1_buf, 1);
714 memmove(sha1_buf, sha1_buf + 1, 1);
715 sha1_buf_len--;
718 /* Buffer potential checksum bytes. */
719 memcpy(sha1_buf + sha1_buf_len, buf, r);
720 sha1_buf_len += r;
721 } else {
722 /*
723 * Mix in previously buffered bytes which
724 * are not part of the checksum after all.
725 */
726 SHA1Update(&sha1_ctx, sha1_buf, r);
728 /* Update potential checksum buffer. */
729 memmove(sha1_buf, sha1_buf + r,
730 sha1_buf_len - r);
731 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
733 } else {
734 /* Mix in any previously buffered bytes. */
735 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
737 /* Mix in bytes read minus potential checksum bytes. */
738 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
740 /* Buffer potential checksum bytes. */
741 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
742 SHA1_DIGEST_LENGTH);
743 sha1_buf_len = SHA1_DIGEST_LENGTH;
746 /* Write packfile data to temporary pack file. */
747 w = write(packfd, buf, r);
748 if (w == -1) {
749 err = got_error_from_errno("write");
750 goto done;
752 if (w != r) {
753 err = got_error(GOT_ERR_IO);
754 goto done;
756 packsz += w;
758 /* Don't send too many progress privsep messages. */
759 if (packsz > last_reported_packsz + 1024) {
760 err = send_fetch_download_progress(ibuf, packsz, &rl);
761 if (err)
762 goto done;
763 last_reported_packsz = packsz;
766 err = send_fetch_download_progress(ibuf, packsz, NULL);
767 if (err)
768 goto done;
770 SHA1Final(pack_sha1, &sha1_ctx);
771 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
772 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
773 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
774 "pack file checksum mismatch");
776 done:
777 TAILQ_FOREACH(pe, &symrefs, entry) {
778 free((void *)pe->path);
779 free(pe->data);
781 got_pathlist_free(&symrefs);
782 free(have);
783 free(want);
784 free(id_str);
785 free(refname);
786 free(server_capabilities);
787 return err;
791 int
792 main(int argc, char **argv)
794 const struct got_error *err = NULL;
795 int fetchfd, packfd = -1;
796 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
797 struct imsgbuf ibuf;
798 struct imsg imsg;
799 struct got_pathlist_head have_refs;
800 struct got_pathlist_head wanted_branches;
801 struct got_pathlist_head wanted_refs;
802 struct got_pathlist_entry *pe;
803 struct got_imsg_fetch_request fetch_req;
804 struct got_imsg_fetch_have_ref href;
805 struct got_imsg_fetch_wanted_branch wbranch;
806 struct got_imsg_fetch_wanted_ref wref;
807 size_t datalen, i;
808 #if 0
809 static int attached;
810 while (!attached)
811 sleep (1);
812 #endif
814 TAILQ_INIT(&have_refs);
815 TAILQ_INIT(&wanted_branches);
816 TAILQ_INIT(&wanted_refs);
818 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
819 #ifndef PROFILE
820 /* revoke access to most system calls */
821 if (pledge("stdio recvfd", NULL) == -1) {
822 err = got_error_from_errno("pledge");
823 got_privsep_send_error(&ibuf, err);
824 return 1;
826 #endif
827 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
828 if (err) {
829 if (err->code == GOT_ERR_PRIVSEP_PIPE)
830 err = NULL;
831 goto done;
833 if (imsg.hdr.type == GOT_IMSG_STOP)
834 goto done;
835 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
836 err = got_error(GOT_ERR_PRIVSEP_MSG);
837 goto done;
839 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
840 if (datalen < sizeof(fetch_req)) {
841 err = got_error(GOT_ERR_PRIVSEP_LEN);
842 goto done;
844 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
845 fetchfd = imsg.fd;
846 imsg_free(&imsg);
848 if (fetch_req.verbosity > 0)
849 chattygot += fetch_req.verbosity;
851 for (i = 0; i < fetch_req.n_have_refs; i++) {
852 struct got_object_id *id;
853 char *refname;
855 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
856 if (err) {
857 if (err->code == GOT_ERR_PRIVSEP_PIPE)
858 err = NULL;
859 goto done;
861 if (imsg.hdr.type == GOT_IMSG_STOP)
862 goto done;
863 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
864 err = got_error(GOT_ERR_PRIVSEP_MSG);
865 goto done;
867 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
868 if (datalen < sizeof(href)) {
869 err = got_error(GOT_ERR_PRIVSEP_LEN);
870 goto done;
872 memcpy(&href, imsg.data, sizeof(href));
873 if (datalen - sizeof(href) < href.name_len) {
874 err = got_error(GOT_ERR_PRIVSEP_LEN);
875 goto done;
877 refname = malloc(href.name_len + 1);
878 if (refname == NULL) {
879 err = got_error_from_errno("malloc");
880 goto done;
882 memcpy(refname, imsg.data + sizeof(href), href.name_len);
883 refname[href.name_len] = '\0';
885 id = malloc(sizeof(*id));
886 if (id == NULL) {
887 free(refname);
888 err = got_error_from_errno("malloc");
889 goto done;
891 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
892 err = got_pathlist_append(&have_refs, refname, id);
893 if (err) {
894 free(refname);
895 free(id);
896 goto done;
899 imsg_free(&imsg);
902 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
903 char *refname;
905 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
906 if (err) {
907 if (err->code == GOT_ERR_PRIVSEP_PIPE)
908 err = NULL;
909 goto done;
911 if (imsg.hdr.type == GOT_IMSG_STOP)
912 goto done;
913 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
914 err = got_error(GOT_ERR_PRIVSEP_MSG);
915 goto done;
917 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
918 if (datalen < sizeof(wbranch)) {
919 err = got_error(GOT_ERR_PRIVSEP_LEN);
920 goto done;
922 memcpy(&wbranch, imsg.data, sizeof(wbranch));
923 if (datalen - sizeof(wbranch) < wbranch.name_len) {
924 err = got_error(GOT_ERR_PRIVSEP_LEN);
925 goto done;
927 refname = malloc(wbranch.name_len + 1);
928 if (refname == NULL) {
929 err = got_error_from_errno("malloc");
930 goto done;
932 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
933 refname[wbranch.name_len] = '\0';
935 err = got_pathlist_append(&wanted_branches, refname, NULL);
936 if (err) {
937 free(refname);
938 goto done;
941 imsg_free(&imsg);
944 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
945 char *refname;
947 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
948 if (err) {
949 if (err->code == GOT_ERR_PRIVSEP_PIPE)
950 err = NULL;
951 goto done;
953 if (imsg.hdr.type == GOT_IMSG_STOP)
954 goto done;
955 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
956 err = got_error(GOT_ERR_PRIVSEP_MSG);
957 goto done;
959 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
960 if (datalen < sizeof(wref)) {
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
962 goto done;
964 memcpy(&wref, imsg.data, sizeof(wref));
965 if (datalen - sizeof(wref) < wref.name_len) {
966 err = got_error(GOT_ERR_PRIVSEP_LEN);
967 goto done;
969 refname = malloc(wref.name_len + 1);
970 if (refname == NULL) {
971 err = got_error_from_errno("malloc");
972 goto done;
974 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
975 refname[wref.name_len] = '\0';
977 err = got_pathlist_append(&wanted_refs, refname, NULL);
978 if (err) {
979 free(refname);
980 goto done;
983 imsg_free(&imsg);
986 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
987 if (err) {
988 if (err->code == GOT_ERR_PRIVSEP_PIPE)
989 err = NULL;
990 goto done;
992 if (imsg.hdr.type == GOT_IMSG_STOP)
993 goto done;
994 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
995 err = got_error(GOT_ERR_PRIVSEP_MSG);
996 goto done;
998 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
999 err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 goto done;
1002 packfd = imsg.fd;
1004 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1005 fetch_req.fetch_all_branches, &wanted_branches,
1006 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1007 done:
1008 TAILQ_FOREACH(pe, &have_refs, entry) {
1009 free((char *)pe->path);
1010 free(pe->data);
1012 got_pathlist_free(&have_refs);
1013 TAILQ_FOREACH(pe, &wanted_branches, entry)
1014 free((char *)pe->path);
1015 got_pathlist_free(&wanted_branches);
1016 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1017 err = got_error_from_errno("close");
1018 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1019 err = got_error_from_errno("close");
1020 if (err != NULL)
1021 got_privsep_send_error(&ibuf, err);
1022 else
1023 err = send_fetch_done(&ibuf, pack_sha1);
1024 if (err != NULL) {
1025 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1026 got_privsep_send_error(&ibuf, err);
1029 exit(0);