let 'got fetch' send all references to the server to avoid redundant downloads
[got-portable.git] / libexec / got-fetch-pack / got-fetch-pack.c
blob9d5b83b348179ccd30d944a9e9e50183e0a4a458
1 /*
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/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;
60 static const struct got_capability got_capabilities[] = {
61 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
62 { GOT_CAPA_OFS_DELTA, NULL },
63 { GOT_CAPA_SIDE_BAND_64K, NULL },
66 static void
67 match_remote_ref(struct got_pathlist_head *have_refs,
68 struct got_object_id *my_id, char *refname)
70 struct got_pathlist_entry *pe;
72 /* XXX zero-hash signifies we don't have this ref;
73 * we should use a flag instead */
74 memset(my_id, 0, sizeof(*my_id));
76 TAILQ_FOREACH(pe, have_refs, entry) {
77 struct got_object_id *id = pe->data;
78 if (strcmp(pe->path, refname) == 0) {
79 memcpy(my_id, id, sizeof(*my_id));
80 break;
85 static int
86 match_branch(const char *branch, const char *wanted_branch)
88 if (strncmp(branch, "refs/heads/", 11) != 0)
89 return 0;
91 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
92 wanted_branch += 11;
94 return (strcmp(branch + 11, wanted_branch) == 0);
97 static int
98 match_wanted_ref(const char *refname, const char *wanted_ref)
100 if (strncmp(refname, "refs/", 5) != 0)
101 return 0;
102 refname += 5;
105 * Prevent fetching of references that won't make any
106 * sense outside of the remote repository's context.
108 if (strncmp(refname, "got/", 4) == 0)
109 return 0;
110 if (strncmp(refname, "remotes/", 8) == 0)
111 return 0;
113 if (strncmp(wanted_ref, "refs/", 5) == 0)
114 wanted_ref += 5;
116 /* Allow prefix match. */
117 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
118 return 1;
120 /* Allow exact match. */
121 return (strcmp(refname, wanted_ref) == 0);
124 static const struct got_error *
125 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
127 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
128 return got_error(GOT_ERR_NO_SPACE);
130 if (msglen == 0)
131 return NULL;
133 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
134 msg, msglen) == -1)
135 return got_error_from_errno(
136 "imsg_compose FETCH_SERVER_PROGRESS");
138 return got_privsep_flush_imsg(ibuf);
141 static const struct got_error *
142 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
144 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
145 &bytes, sizeof(bytes)) == -1)
146 return got_error_from_errno(
147 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
149 return got_privsep_flush_imsg(ibuf);
152 static const struct got_error *
153 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
155 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
156 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
157 return got_error_from_errno("imsg_compose FETCH");
158 return got_privsep_flush_imsg(ibuf);
161 static const struct got_error *
162 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
164 size_t i;
166 if (len == 0)
167 return NULL;
170 * Truncate messages which exceed the maximum imsg payload size.
171 * Server may send up to 64k.
173 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
174 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
176 /* Only allow printable ASCII. */
177 for (i = 0; i < len; i++) {
178 if (isprint((unsigned char)buf[i]) ||
179 isspace((unsigned char)buf[i]))
180 continue;
181 return got_error_msg(GOT_ERR_BAD_PACKET,
182 "non-printable progress message received from server");
185 return send_fetch_server_progress(ibuf, buf, len);
188 static const struct got_error *
189 fetch_error(const char *buf, size_t len)
191 static char msg[1024];
192 size_t i;
194 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
195 if (!isprint(buf[i]))
196 return got_error_msg(GOT_ERR_BAD_PACKET,
197 "non-printable error message received from server");
198 msg[i] = buf[i];
200 msg[i] = '\0';
201 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
204 static const struct got_error *
205 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
207 const struct got_error *err = NULL;
208 struct ibuf *wbuf;
209 size_t len, nsymrefs = 0;
210 struct got_pathlist_entry *pe;
212 len = sizeof(struct got_imsg_fetch_symrefs);
213 TAILQ_FOREACH(pe, symrefs, entry) {
214 const char *target = pe->data;
215 len += sizeof(struct got_imsg_fetch_symref) +
216 pe->path_len + strlen(target);
217 nsymrefs++;
220 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 return got_error(GOT_ERR_NO_SPACE);
223 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
224 if (wbuf == NULL)
225 return got_error_from_errno("imsg_create FETCH_SYMREFS");
227 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
228 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
229 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
230 ibuf_free(wbuf);
231 return err;
234 TAILQ_FOREACH(pe, symrefs, entry) {
235 const char *name = pe->path;
236 size_t name_len = pe->path_len;
237 const char *target = pe->data;
238 size_t target_len = strlen(target);
240 /* Keep in sync with struct got_imsg_fetch_symref definition! */
241 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
242 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
243 ibuf_free(wbuf);
244 return err;
246 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
247 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
248 ibuf_free(wbuf);
249 return err;
251 if (imsg_add(wbuf, name, name_len) == -1) {
252 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
253 ibuf_free(wbuf);
254 return err;
256 if (imsg_add(wbuf, target, target_len) == -1) {
257 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
258 ibuf_free(wbuf);
259 return err;
263 wbuf->fd = -1;
264 imsg_close(ibuf, wbuf);
265 return got_privsep_flush_imsg(ibuf);
268 static const struct got_error *
269 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
270 const char *refname)
272 const struct got_error *err = NULL;
273 struct ibuf *wbuf;
274 size_t len, reflen = strlen(refname);
276 len = sizeof(struct got_imsg_fetch_ref) + reflen;
277 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
278 return got_error(GOT_ERR_NO_SPACE);
280 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
281 if (wbuf == NULL)
282 return got_error_from_errno("imsg_create FETCH_REF");
284 /* Keep in sync with struct got_imsg_fetch_ref definition! */
285 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add FETCH_REF");
287 ibuf_free(wbuf);
288 return err;
290 if (imsg_add(wbuf, refname, reflen) == -1) {
291 err = got_error_from_errno("imsg_add FETCH_REF");
292 ibuf_free(wbuf);
293 return err;
296 wbuf->fd = -1;
297 imsg_close(ibuf, wbuf);
298 return got_privsep_flush_imsg(ibuf);
301 static const struct got_error *
302 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
303 struct got_pathlist_head *have_refs, int fetch_all_branches,
304 struct got_pathlist_head *wanted_branches,
305 struct got_pathlist_head *wanted_refs, int list_refs_only,
306 struct imsgbuf *ibuf)
308 const struct got_error *err = NULL;
309 char buf[GOT_PKT_MAX];
310 char hashstr[SHA1_DIGEST_STRING_LENGTH];
311 struct got_object_id *have, *want;
312 int is_firstpkt = 1, nref = 0, refsz = 16;
313 int i, n, nwant = 0, nhave = 0, acked = 0;
314 off_t packsz = 0, last_reported_packsz = 0;
315 char *id_str = NULL, *refname = NULL;
316 char *server_capabilities = NULL, *my_capabilities = NULL;
317 const char *default_branch = NULL;
318 struct got_pathlist_head symrefs;
319 struct got_pathlist_entry *pe;
320 int sent_my_capabilites = 0, have_sidebands = 0;
321 int found_branch = 0;
322 SHA1_CTX sha1_ctx;
323 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
324 size_t sha1_buf_len = 0;
325 ssize_t w;
327 TAILQ_INIT(&symrefs);
328 SHA1Init(&sha1_ctx);
330 have = malloc(refsz * sizeof(have[0]));
331 if (have == NULL)
332 return got_error_from_errno("malloc");
333 want = malloc(refsz * sizeof(want[0]));
334 if (want == NULL) {
335 err = got_error_from_errno("malloc");
336 goto done;
338 while (1) {
339 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
340 if (err)
341 goto done;
342 if (n == 0)
343 break;
344 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
345 err = fetch_error(&buf[4], n - 4);
346 goto done;
348 err = got_gitproto_parse_refline(&id_str, &refname,
349 &server_capabilities, buf, n);
350 if (err)
351 goto done;
352 if (is_firstpkt) {
353 if (chattygot && server_capabilities[0] != '\0')
354 fprintf(stderr, "%s: server capabilities: %s\n",
355 getprogname(), server_capabilities);
356 err = got_gitproto_match_capabilities(&my_capabilities,
357 &symrefs, server_capabilities,
358 got_capabilities, nitems(got_capabilities));
359 if (err)
360 goto done;
361 if (chattygot)
362 fprintf(stderr, "%s: my capabilities:%s\n",
363 getprogname(), my_capabilities != NULL ?
364 my_capabilities : "");
365 err = send_fetch_symrefs(ibuf, &symrefs);
366 if (err)
367 goto done;
368 is_firstpkt = 0;
369 if (!fetch_all_branches) {
370 TAILQ_FOREACH(pe, &symrefs, entry) {
371 const char *name = pe->path;
372 const char *symref_target = pe->data;
373 if (strcmp(name, GOT_REF_HEAD) != 0)
374 continue;
375 default_branch = symref_target;
376 break;
379 continue;
381 if (strstr(refname, "^{}")) {
382 if (chattygot) {
383 fprintf(stderr, "%s: ignoring %s\n",
384 getprogname(), refname);
386 continue;
389 if (strncmp(refname, "refs/heads/", 11) == 0) {
390 if (fetch_all_branches || list_refs_only) {
391 found_branch = 1;
392 } else if (!TAILQ_EMPTY(wanted_branches)) {
393 TAILQ_FOREACH(pe, wanted_branches, entry) {
394 if (match_branch(refname, pe->path))
395 break;
397 if (pe == NULL) {
398 if (chattygot) {
399 fprintf(stderr,
400 "%s: ignoring %s\n",
401 getprogname(), refname);
403 continue;
405 found_branch = 1;
406 } else if (default_branch != NULL) {
407 if (!match_branch(refname, default_branch)) {
408 if (chattygot) {
409 fprintf(stderr,
410 "%s: ignoring %s\n",
411 getprogname(), refname);
413 continue;
415 found_branch = 1;
417 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
418 if (!TAILQ_EMPTY(wanted_refs)) {
419 TAILQ_FOREACH(pe, wanted_refs, entry) {
420 if (match_wanted_ref(refname, pe->path))
421 break;
423 if (pe == NULL) {
424 if (chattygot) {
425 fprintf(stderr,
426 "%s: ignoring %s\n",
427 getprogname(), refname);
429 continue;
431 found_branch = 1;
432 } else if (!list_refs_only) {
433 if (chattygot) {
434 fprintf(stderr, "%s: ignoring %s\n",
435 getprogname(), refname);
437 continue;
441 if (refsz == nref + 1) {
442 refsz *= 2;
443 have = reallocarray(have, refsz, sizeof(have[0]));
444 if (have == NULL) {
445 err = got_error_from_errno("reallocarray");
446 goto done;
448 want = reallocarray(want, refsz, sizeof(want[0]));
449 if (want == NULL) {
450 err = got_error_from_errno("reallocarray");
451 goto done;
454 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
455 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
456 goto done;
458 match_remote_ref(have_refs, &have[nref], refname);
459 err = send_fetch_ref(ibuf, &want[nref], refname);
460 if (err)
461 goto done;
463 if (chattygot)
464 fprintf(stderr, "%s: %s will be fetched\n",
465 getprogname(), refname);
466 if (chattygot > 1) {
467 char *theirs, *mine;
468 err = got_object_id_str(&theirs, &want[nref]);
469 if (err)
470 goto done;
471 err = got_object_id_str(&mine, &have[nref]);
472 if (err) {
473 free(theirs);
474 goto done;
476 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
477 getprogname(), theirs, getprogname(), mine);
478 free(theirs);
479 free(mine);
481 nref++;
484 if (list_refs_only)
485 goto done;
487 /* Abort if we haven't found any branch to fetch. */
488 if (!found_branch) {
489 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
490 goto done;
493 for (i = 0; i < nref; i++) {
494 if (got_object_id_cmp(&have[i], &want[i]) == 0)
495 continue;
496 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
497 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
498 sent_my_capabilites || my_capabilities == NULL ?
499 "" : my_capabilities);
500 if (n >= sizeof(buf)) {
501 err = got_error(GOT_ERR_NO_SPACE);
502 goto done;
504 err = got_pkt_writepkt(fd, buf, n, chattygot);
505 if (err)
506 goto done;
507 sent_my_capabilites = 1;
508 nwant++;
510 err = got_pkt_flushpkt(fd, chattygot);
511 if (err)
512 goto done;
514 if (nwant == 0)
515 goto done;
517 TAILQ_FOREACH(pe, have_refs, entry) {
518 struct got_object_id *id = pe->data;
519 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
520 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
521 if (n >= sizeof(buf)) {
522 err = got_error(GOT_ERR_NO_SPACE);
523 goto done;
525 err = got_pkt_writepkt(fd, buf, n, chattygot);
526 if (err)
527 goto done;
528 nhave++;
531 while (nhave > 0 && !acked) {
532 struct got_object_id common_id;
534 /* The server should ACK the object IDs we need. */
535 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
536 if (err)
537 goto done;
538 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
539 err = fetch_error(&buf[4], n - 4);
540 goto done;
542 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
543 /* Server has not located our objects yet. */
544 continue;
546 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
547 strncmp(buf, "ACK ", 4) != 0) {
548 err = got_error_msg(GOT_ERR_BAD_PACKET,
549 "unexpected message from server");
550 goto done;
552 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
553 err = got_error_msg(GOT_ERR_BAD_PACKET,
554 "bad object ID in ACK packet from server");
555 goto done;
557 acked++;
560 n = snprintf(buf, sizeof(buf), "done\n");
561 err = got_pkt_writepkt(fd, buf, n, chattygot);
562 if (err)
563 goto done;
565 if (nhave == 0) {
566 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
567 if (err)
568 goto done;
569 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
570 err = got_error_msg(GOT_ERR_BAD_PACKET,
571 "unexpected message from server");
572 goto done;
576 if (chattygot)
577 fprintf(stderr, "%s: fetching...\n", getprogname());
579 if (my_capabilities != NULL &&
580 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
581 have_sidebands = 1;
583 while (1) {
584 ssize_t r = 0;
585 int datalen = -1;
587 if (have_sidebands) {
588 err = got_pkt_readhdr(&datalen, fd, chattygot);
589 if (err)
590 goto done;
591 if (datalen <= 0)
592 break;
594 /* Read sideband channel ID (one byte). */
595 r = read(fd, buf, 1);
596 if (r == -1) {
597 err = got_error_from_errno("read");
598 goto done;
600 if (r != 1) {
601 err = got_error_msg(GOT_ERR_BAD_PACKET,
602 "short packet");
603 goto done;
605 if (datalen > sizeof(buf) - 5) {
606 err = got_error_msg(GOT_ERR_BAD_PACKET,
607 "bad packet length");
608 goto done;
610 datalen--; /* sideband ID has been read */
611 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
612 /* Read packfile data. */
613 err = got_pkt_readn(&r, fd, buf, datalen);
614 if (err)
615 goto done;
616 if (r != datalen) {
617 err = got_error_msg(GOT_ERR_BAD_PACKET,
618 "packet too short");
619 goto done;
621 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
622 err = got_pkt_readn(&r, fd, buf, datalen);
623 if (err)
624 goto done;
625 if (r != datalen) {
626 err = got_error_msg(GOT_ERR_BAD_PACKET,
627 "packet too short");
628 goto done;
630 err = fetch_progress(ibuf, buf, r);
631 if (err)
632 goto done;
633 continue;
634 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
635 err = got_pkt_readn(&r, fd, buf, datalen);
636 if (err)
637 goto done;
638 if (r != datalen) {
639 err = got_error_msg(GOT_ERR_BAD_PACKET,
640 "packet too short");
641 goto done;
643 err = fetch_error(buf, r);
644 goto done;
645 } else if (buf[0] == 'A') {
646 err = got_pkt_readn(&r, fd, buf, datalen);
647 if (err)
648 goto done;
649 if (r != datalen) {
650 err = got_error_msg(GOT_ERR_BAD_PACKET,
651 "packet too short");
652 goto done;
655 * Git server responds with ACK after 'done'
656 * even though multi_ack is disabled?!?
658 buf[r] = '\0';
659 if (strncmp(buf, "CK ", 3) == 0)
660 continue; /* ignore */
661 err = got_error_msg(GOT_ERR_BAD_PACKET,
662 "unexpected message from server");
663 goto done;
664 } else {
665 err = got_error_msg(GOT_ERR_BAD_PACKET,
666 "unknown side-band received from server");
667 goto done;
669 } else {
670 /* No sideband channel. Every byte is packfile data. */
671 err = got_pkt_readn(&r, fd, buf, sizeof buf);
672 if (err)
673 goto done;
674 if (r <= 0)
675 break;
679 * An expected SHA1 checksum sits at the end of the pack file.
680 * Since we don't know the file size ahead of time we have to
681 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
682 * those bytes into our SHA1 checksum computation until we
683 * know for sure that additional pack file data bytes follow.
685 * We can assume r > 0 since otherwise the loop would exit.
687 if (r < SHA1_DIGEST_LENGTH) {
688 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
690 * If there's enough buffered + read data to
691 * fill up the buffer then shift a sufficient
692 * amount of bytes out at the front to make
693 * room, mixing those bytes into the checksum.
695 while (sha1_buf_len > 0 &&
696 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
697 SHA1Update(&sha1_ctx, sha1_buf, 1);
698 memmove(sha1_buf, sha1_buf + 1, 1);
699 sha1_buf_len--;
702 /* Buffer potential checksum bytes. */
703 memcpy(sha1_buf + sha1_buf_len, buf, r);
704 sha1_buf_len += r;
705 } else {
707 * Mix in previously buffered bytes which
708 * are not part of the checksum after all.
710 SHA1Update(&sha1_ctx, sha1_buf, r);
712 /* Update potential checksum buffer. */
713 memmove(sha1_buf, sha1_buf + r,
714 sha1_buf_len - r);
715 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
717 } else {
718 /* Mix in any previously buffered bytes. */
719 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
721 /* Mix in bytes read minus potential checksum bytes. */
722 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
724 /* Buffer potential checksum bytes. */
725 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
726 SHA1_DIGEST_LENGTH);
727 sha1_buf_len = SHA1_DIGEST_LENGTH;
730 /* Write packfile data to temporary pack file. */
731 w = write(packfd, buf, r);
732 if (w == -1) {
733 err = got_error_from_errno("write");
734 goto done;
736 if (w != r) {
737 err = got_error(GOT_ERR_IO);
738 goto done;
740 packsz += w;
742 /* Don't send too many progress privsep messages. */
743 if (packsz > last_reported_packsz + 1024) {
744 err = send_fetch_download_progress(ibuf, packsz);
745 if (err)
746 goto done;
747 last_reported_packsz = packsz;
750 err = send_fetch_download_progress(ibuf, packsz);
751 if (err)
752 goto done;
754 SHA1Final(pack_sha1, &sha1_ctx);
755 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
756 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
757 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
758 "pack file checksum mismatch");
760 done:
761 TAILQ_FOREACH(pe, &symrefs, entry) {
762 free((void *)pe->path);
763 free(pe->data);
765 got_pathlist_free(&symrefs);
766 free(have);
767 free(want);
768 free(id_str);
769 free(refname);
770 free(server_capabilities);
771 return err;
776 main(int argc, char **argv)
778 const struct got_error *err = NULL;
779 int fetchfd, packfd = -1;
780 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
781 struct imsgbuf ibuf;
782 struct imsg imsg;
783 struct got_pathlist_head have_refs;
784 struct got_pathlist_head wanted_branches;
785 struct got_pathlist_head wanted_refs;
786 struct got_pathlist_entry *pe;
787 struct got_imsg_fetch_request fetch_req;
788 struct got_imsg_fetch_have_ref href;
789 struct got_imsg_fetch_wanted_branch wbranch;
790 struct got_imsg_fetch_wanted_ref wref;
791 size_t datalen, i;
792 #if 0
793 static int attached;
794 while (!attached)
795 sleep (1);
796 #endif
798 TAILQ_INIT(&have_refs);
799 TAILQ_INIT(&wanted_branches);
800 TAILQ_INIT(&wanted_refs);
802 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
803 #ifndef PROFILE
804 /* revoke access to most system calls */
805 if (pledge("stdio recvfd", NULL) == -1) {
806 err = got_error_from_errno("pledge");
807 got_privsep_send_error(&ibuf, err);
808 return 1;
810 #endif
811 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
812 if (err) {
813 if (err->code == GOT_ERR_PRIVSEP_PIPE)
814 err = NULL;
815 goto done;
817 if (imsg.hdr.type == GOT_IMSG_STOP)
818 goto done;
819 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
820 err = got_error(GOT_ERR_PRIVSEP_MSG);
821 goto done;
823 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
824 if (datalen < sizeof(fetch_req)) {
825 err = got_error(GOT_ERR_PRIVSEP_LEN);
826 goto done;
828 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
829 fetchfd = imsg.fd;
830 imsg_free(&imsg);
832 if (fetch_req.verbosity > 0)
833 chattygot += fetch_req.verbosity;
835 for (i = 0; i < fetch_req.n_have_refs; i++) {
836 struct got_object_id *id;
837 char *refname;
839 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
840 if (err) {
841 if (err->code == GOT_ERR_PRIVSEP_PIPE)
842 err = NULL;
843 goto done;
845 if (imsg.hdr.type == GOT_IMSG_STOP)
846 goto done;
847 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
848 err = got_error(GOT_ERR_PRIVSEP_MSG);
849 goto done;
851 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
852 if (datalen < sizeof(href)) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
854 goto done;
856 memcpy(&href, imsg.data, sizeof(href));
857 if (datalen - sizeof(href) < href.name_len) {
858 err = got_error(GOT_ERR_PRIVSEP_LEN);
859 goto done;
861 refname = malloc(href.name_len + 1);
862 if (refname == NULL) {
863 err = got_error_from_errno("malloc");
864 goto done;
866 memcpy(refname, imsg.data + sizeof(href), href.name_len);
867 refname[href.name_len] = '\0';
869 id = malloc(sizeof(*id));
870 if (id == NULL) {
871 free(refname);
872 err = got_error_from_errno("malloc");
873 goto done;
875 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
876 err = got_pathlist_append(&have_refs, refname, id);
877 if (err) {
878 free(refname);
879 free(id);
880 goto done;
883 imsg_free(&imsg);
886 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
887 char *refname;
889 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
890 if (err) {
891 if (err->code == GOT_ERR_PRIVSEP_PIPE)
892 err = NULL;
893 goto done;
895 if (imsg.hdr.type == GOT_IMSG_STOP)
896 goto done;
897 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
898 err = got_error(GOT_ERR_PRIVSEP_MSG);
899 goto done;
901 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
902 if (datalen < sizeof(wbranch)) {
903 err = got_error(GOT_ERR_PRIVSEP_LEN);
904 goto done;
906 memcpy(&wbranch, imsg.data, sizeof(wbranch));
907 if (datalen - sizeof(wbranch) < wbranch.name_len) {
908 err = got_error(GOT_ERR_PRIVSEP_LEN);
909 goto done;
911 refname = malloc(wbranch.name_len + 1);
912 if (refname == NULL) {
913 err = got_error_from_errno("malloc");
914 goto done;
916 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
917 refname[wbranch.name_len] = '\0';
919 err = got_pathlist_append(&wanted_branches, refname, NULL);
920 if (err) {
921 free(refname);
922 goto done;
925 imsg_free(&imsg);
928 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
929 char *refname;
931 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
932 if (err) {
933 if (err->code == GOT_ERR_PRIVSEP_PIPE)
934 err = NULL;
935 goto done;
937 if (imsg.hdr.type == GOT_IMSG_STOP)
938 goto done;
939 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
941 goto done;
943 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
944 if (datalen < sizeof(wref)) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
946 goto done;
948 memcpy(&wref, imsg.data, sizeof(wref));
949 if (datalen - sizeof(wref) < wref.name_len) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 goto done;
953 refname = malloc(wref.name_len + 1);
954 if (refname == NULL) {
955 err = got_error_from_errno("malloc");
956 goto done;
958 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
959 refname[wref.name_len] = '\0';
961 err = got_pathlist_append(&wanted_refs, refname, NULL);
962 if (err) {
963 free(refname);
964 goto done;
967 imsg_free(&imsg);
970 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
971 if (err) {
972 if (err->code == GOT_ERR_PRIVSEP_PIPE)
973 err = NULL;
974 goto done;
976 if (imsg.hdr.type == GOT_IMSG_STOP)
977 goto done;
978 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
979 err = got_error(GOT_ERR_PRIVSEP_MSG);
980 goto done;
982 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
983 err = got_error(GOT_ERR_PRIVSEP_LEN);
984 goto done;
986 packfd = imsg.fd;
988 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
989 fetch_req.fetch_all_branches, &wanted_branches,
990 &wanted_refs, fetch_req.list_refs_only, &ibuf);
991 done:
992 TAILQ_FOREACH(pe, &have_refs, entry) {
993 free((char *)pe->path);
994 free(pe->data);
996 got_pathlist_free(&have_refs);
997 TAILQ_FOREACH(pe, &wanted_branches, entry)
998 free((char *)pe->path);
999 got_pathlist_free(&wanted_branches);
1000 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1001 err = got_error_from_errno("close");
1002 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1003 err = got_error_from_errno("close");
1004 if (err != NULL)
1005 got_privsep_send_error(&ibuf, err);
1006 else
1007 err = send_fetch_done(&ibuf, pack_sha1);
1008 if (err != NULL) {
1009 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1010 got_privsep_send_error(&ibuf, err);
1013 exit(0);