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"
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 struct got_object *indexed;
59 static int chattygot;
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 free(id_str);
350 free(refname);
351 err = got_gitproto_parse_refline(&id_str, &refname,
352 &server_capabilities, buf, n);
353 if (err)
354 goto done;
355 if (is_firstpkt) {
356 if (chattygot && server_capabilities[0] != '\0')
357 fprintf(stderr, "%s: server capabilities: %s\n",
358 getprogname(), server_capabilities);
359 err = got_gitproto_match_capabilities(&my_capabilities,
360 &symrefs, server_capabilities,
361 got_capabilities, nitems(got_capabilities));
362 if (err)
363 goto done;
364 if (chattygot)
365 fprintf(stderr, "%s: my capabilities:%s\n",
366 getprogname(), my_capabilities != NULL ?
367 my_capabilities : "");
368 err = send_fetch_symrefs(ibuf, &symrefs);
369 if (err)
370 goto done;
371 is_firstpkt = 0;
372 if (!fetch_all_branches) {
373 TAILQ_FOREACH(pe, &symrefs, entry) {
374 const char *name = pe->path;
375 const char *symref_target = pe->data;
376 if (strcmp(name, GOT_REF_HEAD) != 0)
377 continue;
378 default_branch = symref_target;
379 break;
382 continue;
384 if (strstr(refname, "^{}")) {
385 if (chattygot) {
386 fprintf(stderr, "%s: ignoring %s\n",
387 getprogname(), refname);
389 continue;
392 if (strncmp(refname, "refs/heads/", 11) == 0) {
393 if (fetch_all_branches || list_refs_only) {
394 found_branch = 1;
395 } else if (!TAILQ_EMPTY(wanted_branches)) {
396 TAILQ_FOREACH(pe, wanted_branches, entry) {
397 if (match_branch(refname, pe->path))
398 break;
400 if (pe == NULL) {
401 if (chattygot) {
402 fprintf(stderr,
403 "%s: ignoring %s\n",
404 getprogname(), refname);
406 continue;
408 found_branch = 1;
409 } else if (default_branch != NULL) {
410 if (!match_branch(refname, default_branch)) {
411 if (chattygot) {
412 fprintf(stderr,
413 "%s: ignoring %s\n",
414 getprogname(), refname);
416 continue;
418 found_branch = 1;
420 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
421 if (!TAILQ_EMPTY(wanted_refs)) {
422 TAILQ_FOREACH(pe, wanted_refs, entry) {
423 if (match_wanted_ref(refname, pe->path))
424 break;
426 if (pe == NULL) {
427 if (chattygot) {
428 fprintf(stderr,
429 "%s: ignoring %s\n",
430 getprogname(), refname);
432 continue;
434 found_branch = 1;
435 } else if (!list_refs_only) {
436 if (chattygot) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
440 continue;
444 if (refsz == nref + 1) {
445 refsz *= 2;
446 have = reallocarray(have, refsz, sizeof(have[0]));
447 if (have == NULL) {
448 err = got_error_from_errno("reallocarray");
449 goto done;
451 want = reallocarray(want, refsz, sizeof(want[0]));
452 if (want == NULL) {
453 err = got_error_from_errno("reallocarray");
454 goto done;
457 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
458 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
459 goto done;
461 match_remote_ref(have_refs, &have[nref], refname);
462 err = send_fetch_ref(ibuf, &want[nref], refname);
463 if (err)
464 goto done;
466 if (chattygot)
467 fprintf(stderr, "%s: %s will be fetched\n",
468 getprogname(), refname);
469 if (chattygot > 1) {
470 char *theirs, *mine;
471 err = got_object_id_str(&theirs, &want[nref]);
472 if (err)
473 goto done;
474 err = got_object_id_str(&mine, &have[nref]);
475 if (err) {
476 free(theirs);
477 goto done;
479 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
480 getprogname(), theirs, getprogname(), mine);
481 free(theirs);
482 free(mine);
484 nref++;
487 if (list_refs_only)
488 goto done;
490 /* Abort if we haven't found any branch to fetch. */
491 if (!found_branch) {
492 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
493 goto done;
496 for (i = 0; i < nref; i++) {
497 if (got_object_id_cmp(&have[i], &want[i]) == 0)
498 continue;
499 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
500 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
501 sent_my_capabilites || my_capabilities == NULL ?
502 "" : my_capabilities);
503 if (n >= sizeof(buf)) {
504 err = got_error(GOT_ERR_NO_SPACE);
505 goto done;
507 err = got_pkt_writepkt(fd, buf, n, chattygot);
508 if (err)
509 goto done;
510 sent_my_capabilites = 1;
511 nwant++;
513 err = got_pkt_flushpkt(fd, chattygot);
514 if (err)
515 goto done;
517 if (nwant == 0)
518 goto done;
520 TAILQ_FOREACH(pe, have_refs, entry) {
521 struct got_object_id *id = pe->data;
522 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
523 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
524 if (n >= sizeof(buf)) {
525 err = got_error(GOT_ERR_NO_SPACE);
526 goto done;
528 err = got_pkt_writepkt(fd, buf, n, chattygot);
529 if (err)
530 goto done;
531 nhave++;
534 while (nhave > 0 && !acked) {
535 struct got_object_id common_id;
537 /* The server should ACK the object IDs we need. */
538 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
539 if (err)
540 goto done;
541 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
542 err = fetch_error(&buf[4], n - 4);
543 goto done;
545 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
546 /* Server has not located our objects yet. */
547 continue;
549 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
550 strncmp(buf, "ACK ", 4) != 0) {
551 err = got_error_msg(GOT_ERR_BAD_PACKET,
552 "unexpected message from server");
553 goto done;
555 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
556 err = got_error_msg(GOT_ERR_BAD_PACKET,
557 "bad object ID in ACK packet from server");
558 goto done;
560 acked++;
563 n = snprintf(buf, sizeof(buf), "done\n");
564 err = got_pkt_writepkt(fd, buf, n, chattygot);
565 if (err)
566 goto done;
568 if (nhave == 0) {
569 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
570 if (err)
571 goto done;
572 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
573 err = got_error_msg(GOT_ERR_BAD_PACKET,
574 "unexpected message from server");
575 goto done;
579 if (chattygot)
580 fprintf(stderr, "%s: fetching...\n", getprogname());
582 if (my_capabilities != NULL &&
583 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
584 have_sidebands = 1;
586 while (1) {
587 ssize_t r = 0;
588 int datalen = -1;
590 if (have_sidebands) {
591 err = got_pkt_readhdr(&datalen, fd, chattygot);
592 if (err)
593 goto done;
594 if (datalen <= 0)
595 break;
597 /* Read sideband channel ID (one byte). */
598 r = read(fd, buf, 1);
599 if (r == -1) {
600 err = got_error_from_errno("read");
601 goto done;
603 if (r != 1) {
604 err = got_error_msg(GOT_ERR_BAD_PACKET,
605 "short packet");
606 goto done;
608 if (datalen > sizeof(buf) - 5) {
609 err = got_error_msg(GOT_ERR_BAD_PACKET,
610 "bad packet length");
611 goto done;
613 datalen--; /* sideband ID has been read */
614 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
615 /* Read packfile data. */
616 err = got_pkt_readn(&r, fd, buf, datalen);
617 if (err)
618 goto done;
619 if (r != datalen) {
620 err = got_error_msg(GOT_ERR_BAD_PACKET,
621 "packet too short");
622 goto done;
624 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
625 err = got_pkt_readn(&r, fd, buf, datalen);
626 if (err)
627 goto done;
628 if (r != datalen) {
629 err = got_error_msg(GOT_ERR_BAD_PACKET,
630 "packet too short");
631 goto done;
633 err = fetch_progress(ibuf, buf, r);
634 if (err)
635 goto done;
636 continue;
637 } else if (buf[0] == GOT_SIDEBAND_ERROR_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_error(buf, r);
647 goto done;
648 } else if (buf[0] == 'A') {
649 err = got_pkt_readn(&r, fd, buf, datalen);
650 if (err)
651 goto done;
652 if (r != datalen) {
653 err = got_error_msg(GOT_ERR_BAD_PACKET,
654 "packet too short");
655 goto done;
657 /*
658 * Git server responds with ACK after 'done'
659 * even though multi_ack is disabled?!?
660 */
661 buf[r] = '\0';
662 if (strncmp(buf, "CK ", 3) == 0)
663 continue; /* ignore */
664 err = got_error_msg(GOT_ERR_BAD_PACKET,
665 "unexpected message from server");
666 goto done;
667 } else {
668 err = got_error_msg(GOT_ERR_BAD_PACKET,
669 "unknown side-band received from server");
670 goto done;
672 } else {
673 /* No sideband channel. Every byte is packfile data. */
674 err = got_pkt_readn(&r, fd, buf, sizeof buf);
675 if (err)
676 goto done;
677 if (r <= 0)
678 break;
681 /*
682 * An expected SHA1 checksum sits at the end of the pack file.
683 * Since we don't know the file size ahead of time we have to
684 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
685 * those bytes into our SHA1 checksum computation until we
686 * know for sure that additional pack file data bytes follow.
688 * We can assume r > 0 since otherwise the loop would exit.
689 */
690 if (r < SHA1_DIGEST_LENGTH) {
691 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
692 /*
693 * If there's enough buffered + read data to
694 * fill up the buffer then shift a sufficient
695 * amount of bytes out at the front to make
696 * room, mixing those bytes into the checksum.
697 */
698 while (sha1_buf_len > 0 &&
699 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
700 SHA1Update(&sha1_ctx, sha1_buf, 1);
701 memmove(sha1_buf, sha1_buf + 1, 1);
702 sha1_buf_len--;
705 /* Buffer potential checksum bytes. */
706 memcpy(sha1_buf + sha1_buf_len, buf, r);
707 sha1_buf_len += r;
708 } else {
709 /*
710 * Mix in previously buffered bytes which
711 * are not part of the checksum after all.
712 */
713 SHA1Update(&sha1_ctx, sha1_buf, r);
715 /* Update potential checksum buffer. */
716 memmove(sha1_buf, sha1_buf + r,
717 sha1_buf_len - r);
718 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
720 } else {
721 /* Mix in any previously buffered bytes. */
722 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
724 /* Mix in bytes read minus potential checksum bytes. */
725 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
727 /* Buffer potential checksum bytes. */
728 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
729 SHA1_DIGEST_LENGTH);
730 sha1_buf_len = SHA1_DIGEST_LENGTH;
733 /* Write packfile data to temporary pack file. */
734 w = write(packfd, buf, r);
735 if (w == -1) {
736 err = got_error_from_errno("write");
737 goto done;
739 if (w != r) {
740 err = got_error(GOT_ERR_IO);
741 goto done;
743 packsz += w;
745 /* Don't send too many progress privsep messages. */
746 if (packsz > last_reported_packsz + 1024) {
747 err = send_fetch_download_progress(ibuf, packsz);
748 if (err)
749 goto done;
750 last_reported_packsz = packsz;
753 err = send_fetch_download_progress(ibuf, packsz);
754 if (err)
755 goto done;
757 SHA1Final(pack_sha1, &sha1_ctx);
758 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
759 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
760 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
761 "pack file checksum mismatch");
763 done:
764 TAILQ_FOREACH(pe, &symrefs, entry) {
765 free((void *)pe->path);
766 free(pe->data);
768 got_pathlist_free(&symrefs);
769 free(have);
770 free(want);
771 free(id_str);
772 free(refname);
773 free(server_capabilities);
774 return err;
778 int
779 main(int argc, char **argv)
781 const struct got_error *err = NULL;
782 int fetchfd, packfd = -1;
783 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
784 struct imsgbuf ibuf;
785 struct imsg imsg;
786 struct got_pathlist_head have_refs;
787 struct got_pathlist_head wanted_branches;
788 struct got_pathlist_head wanted_refs;
789 struct got_pathlist_entry *pe;
790 struct got_imsg_fetch_request fetch_req;
791 struct got_imsg_fetch_have_ref href;
792 struct got_imsg_fetch_wanted_branch wbranch;
793 struct got_imsg_fetch_wanted_ref wref;
794 size_t datalen, i;
795 #if 0
796 static int attached;
797 while (!attached)
798 sleep (1);
799 #endif
801 TAILQ_INIT(&have_refs);
802 TAILQ_INIT(&wanted_branches);
803 TAILQ_INIT(&wanted_refs);
805 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
806 #ifndef PROFILE
807 /* revoke access to most system calls */
808 if (pledge("stdio recvfd", NULL) == -1) {
809 err = got_error_from_errno("pledge");
810 got_privsep_send_error(&ibuf, err);
811 return 1;
813 #endif
814 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
815 if (err) {
816 if (err->code == GOT_ERR_PRIVSEP_PIPE)
817 err = NULL;
818 goto done;
820 if (imsg.hdr.type == GOT_IMSG_STOP)
821 goto done;
822 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 goto done;
826 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
827 if (datalen < sizeof(fetch_req)) {
828 err = got_error(GOT_ERR_PRIVSEP_LEN);
829 goto done;
831 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
832 fetchfd = imsg.fd;
833 imsg_free(&imsg);
835 if (fetch_req.verbosity > 0)
836 chattygot += fetch_req.verbosity;
838 for (i = 0; i < fetch_req.n_have_refs; i++) {
839 struct got_object_id *id;
840 char *refname;
842 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
843 if (err) {
844 if (err->code == GOT_ERR_PRIVSEP_PIPE)
845 err = NULL;
846 goto done;
848 if (imsg.hdr.type == GOT_IMSG_STOP)
849 goto done;
850 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 goto done;
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 if (datalen < sizeof(href)) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
857 goto done;
859 memcpy(&href, imsg.data, sizeof(href));
860 if (datalen - sizeof(href) < href.name_len) {
861 err = got_error(GOT_ERR_PRIVSEP_LEN);
862 goto done;
864 refname = malloc(href.name_len + 1);
865 if (refname == NULL) {
866 err = got_error_from_errno("malloc");
867 goto done;
869 memcpy(refname, imsg.data + sizeof(href), href.name_len);
870 refname[href.name_len] = '\0';
872 id = malloc(sizeof(*id));
873 if (id == NULL) {
874 free(refname);
875 err = got_error_from_errno("malloc");
876 goto done;
878 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
879 err = got_pathlist_append(&have_refs, refname, id);
880 if (err) {
881 free(refname);
882 free(id);
883 goto done;
886 imsg_free(&imsg);
889 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
890 char *refname;
892 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
893 if (err) {
894 if (err->code == GOT_ERR_PRIVSEP_PIPE)
895 err = NULL;
896 goto done;
898 if (imsg.hdr.type == GOT_IMSG_STOP)
899 goto done;
900 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
901 err = got_error(GOT_ERR_PRIVSEP_MSG);
902 goto done;
904 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
905 if (datalen < sizeof(wbranch)) {
906 err = got_error(GOT_ERR_PRIVSEP_LEN);
907 goto done;
909 memcpy(&wbranch, imsg.data, sizeof(wbranch));
910 if (datalen - sizeof(wbranch) < wbranch.name_len) {
911 err = got_error(GOT_ERR_PRIVSEP_LEN);
912 goto done;
914 refname = malloc(wbranch.name_len + 1);
915 if (refname == NULL) {
916 err = got_error_from_errno("malloc");
917 goto done;
919 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
920 refname[wbranch.name_len] = '\0';
922 err = got_pathlist_append(&wanted_branches, refname, NULL);
923 if (err) {
924 free(refname);
925 goto done;
928 imsg_free(&imsg);
931 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
932 char *refname;
934 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
935 if (err) {
936 if (err->code == GOT_ERR_PRIVSEP_PIPE)
937 err = NULL;
938 goto done;
940 if (imsg.hdr.type == GOT_IMSG_STOP)
941 goto done;
942 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
944 goto done;
946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 if (datalen < sizeof(wref)) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 goto done;
951 memcpy(&wref, imsg.data, sizeof(wref));
952 if (datalen - sizeof(wref) < wref.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_LEN);
954 goto done;
956 refname = malloc(wref.name_len + 1);
957 if (refname == NULL) {
958 err = got_error_from_errno("malloc");
959 goto done;
961 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
962 refname[wref.name_len] = '\0';
964 err = got_pathlist_append(&wanted_refs, refname, NULL);
965 if (err) {
966 free(refname);
967 goto done;
970 imsg_free(&imsg);
973 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
974 if (err) {
975 if (err->code == GOT_ERR_PRIVSEP_PIPE)
976 err = NULL;
977 goto done;
979 if (imsg.hdr.type == GOT_IMSG_STOP)
980 goto done;
981 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
982 err = got_error(GOT_ERR_PRIVSEP_MSG);
983 goto done;
985 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
986 err = got_error(GOT_ERR_PRIVSEP_LEN);
987 goto done;
989 packfd = imsg.fd;
991 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
992 fetch_req.fetch_all_branches, &wanted_branches,
993 &wanted_refs, fetch_req.list_refs_only, &ibuf);
994 done:
995 TAILQ_FOREACH(pe, &have_refs, entry) {
996 free((char *)pe->path);
997 free(pe->data);
999 got_pathlist_free(&have_refs);
1000 TAILQ_FOREACH(pe, &wanted_branches, entry)
1001 free((char *)pe->path);
1002 got_pathlist_free(&wanted_branches);
1003 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1004 err = got_error_from_errno("close");
1005 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1006 err = got_error_from_errno("close");
1007 if (err != NULL)
1008 got_privsep_send_error(&ibuf, err);
1009 else
1010 err = send_fetch_done(&ibuf, pack_sha1);
1011 if (err != NULL) {
1012 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1013 got_privsep_send_error(&ibuf, err);
1016 exit(0);