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 MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 struct got_object *indexed;
64 static int chattygot;
66 static const struct got_capability got_capabilities[] = {
67 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 { GOT_CAPA_OFS_DELTA, NULL },
69 { GOT_CAPA_SIDE_BAND_64K, NULL },
70 };
72 static void
73 match_remote_ref(struct got_pathlist_head *have_refs,
74 struct got_object_id *my_id, char *refname)
75 {
76 struct got_pathlist_entry *pe;
78 /* XXX zero-hash signifies we don't have this ref;
79 * we should use a flag instead */
80 memset(my_id, 0, sizeof(*my_id));
82 TAILQ_FOREACH(pe, have_refs, entry) {
83 struct got_object_id *id = pe->data;
84 if (strcmp(pe->path, refname) == 0) {
85 memcpy(my_id, id, sizeof(*my_id));
86 break;
87 }
88 }
89 }
91 static int
92 match_branch(const char *branch, const char *wanted_branch)
93 {
94 if (strncmp(branch, "refs/heads/", 11) != 0)
95 return 0;
97 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
98 wanted_branch += 11;
100 return (strcmp(branch + 11, wanted_branch) == 0);
103 static int
104 match_wanted_ref(const char *refname, const char *wanted_ref)
106 if (strncmp(refname, "refs/", 5) != 0)
107 return 0;
108 refname += 5;
110 /*
111 * Prevent fetching of references that won't make any
112 * sense outside of the remote repository's context.
113 */
114 if (strncmp(refname, "got/", 4) == 0)
115 return 0;
116 if (strncmp(refname, "remotes/", 8) == 0)
117 return 0;
119 if (strncmp(wanted_ref, "refs/", 5) == 0)
120 wanted_ref += 5;
122 /* Allow prefix match. */
123 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
124 return 1;
126 /* Allow exact match. */
127 return (strcmp(refname, wanted_ref) == 0);
130 static const struct got_error *
131 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
133 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
134 return got_error(GOT_ERR_NO_SPACE);
136 if (msglen == 0)
137 return NULL;
139 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
140 msg, msglen) == -1)
141 return got_error_from_errno(
142 "imsg_compose FETCH_SERVER_PROGRESS");
144 return got_privsep_flush_imsg(ibuf);
147 static const struct got_error *
148 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 struct got_ratelimit *rl)
151 const struct got_error *err;
152 int elapsed = 0;
154 if (rl) {
155 err = got_ratelimit_check(&elapsed, rl);
156 if (err || !elapsed)
157 return err;
160 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
161 &bytes, sizeof(bytes)) == -1)
162 return got_error_from_errno(
163 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
165 return got_privsep_flush_imsg(ibuf);
168 static const struct got_error *
169 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
171 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
172 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
173 return got_error_from_errno("imsg_compose FETCH");
174 return got_privsep_flush_imsg(ibuf);
177 static const struct got_error *
178 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
180 size_t i;
182 if (len == 0)
183 return NULL;
185 /*
186 * Truncate messages which exceed the maximum imsg payload size.
187 * Server may send up to 64k.
188 */
189 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
192 /* Only allow printable ASCII. */
193 for (i = 0; i < len; i++) {
194 if (isprint((unsigned char)buf[i]) ||
195 isspace((unsigned char)buf[i]))
196 continue;
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable progress message received from server");
201 return send_fetch_server_progress(ibuf, buf, len);
204 static const struct got_error *
205 fetch_error(const char *buf, size_t len)
207 static char msg[1024];
208 size_t i;
210 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
211 if (!isprint(buf[i]))
212 return got_error_msg(GOT_ERR_BAD_PACKET,
213 "non-printable error message received from server");
214 msg[i] = buf[i];
216 msg[i] = '\0';
217 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
220 static const struct got_error *
221 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
223 struct ibuf *wbuf;
224 size_t len, nsymrefs = 0;
225 struct got_pathlist_entry *pe;
227 len = sizeof(struct got_imsg_fetch_symrefs);
228 TAILQ_FOREACH(pe, symrefs, entry) {
229 const char *target = pe->data;
230 len += sizeof(struct got_imsg_fetch_symref) +
231 pe->path_len + strlen(target);
232 nsymrefs++;
235 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
236 return got_error(GOT_ERR_NO_SPACE);
238 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
239 if (wbuf == NULL)
240 return got_error_from_errno("imsg_create FETCH_SYMREFS");
242 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
243 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 return got_error_from_errno("imsg_add FETCH_SYMREFS");
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 return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, name, name_len) == -1)
258 return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 if (imsg_add(wbuf, target, target_len) == -1)
260 return got_error_from_errno("imsg_add FETCH_SYMREFS");
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 struct ibuf *wbuf;
273 size_t len, reflen = strlen(refname);
275 len = sizeof(struct got_imsg_fetch_ref) + reflen;
276 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
277 return got_error(GOT_ERR_NO_SPACE);
279 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
280 if (wbuf == NULL)
281 return got_error_from_errno("imsg_create FETCH_REF");
283 /* Keep in sync with struct got_imsg_fetch_ref definition! */
284 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
285 return got_error_from_errno("imsg_add FETCH_REF");
286 if (imsg_add(wbuf, refname, reflen) == -1)
287 return got_error_from_errno("imsg_add FETCH_REF");
289 wbuf->fd = -1;
290 imsg_close(ibuf, wbuf);
291 return got_privsep_flush_imsg(ibuf);
294 static const struct got_error *
295 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
296 struct got_pathlist_head *have_refs, int fetch_all_branches,
297 struct got_pathlist_head *wanted_branches,
298 struct got_pathlist_head *wanted_refs, int list_refs_only,
299 struct imsgbuf *ibuf)
301 const struct got_error *err = NULL;
302 char buf[GOT_PKT_MAX];
303 char hashstr[SHA1_DIGEST_STRING_LENGTH];
304 struct got_object_id *have, *want;
305 int is_firstpkt = 1, nref = 0, refsz = 16;
306 int i, n, nwant = 0, nhave = 0, acked = 0;
307 off_t packsz = 0, last_reported_packsz = 0;
308 char *id_str = NULL, *refname = NULL;
309 char *server_capabilities = NULL, *my_capabilities = NULL;
310 const char *default_branch = NULL;
311 struct got_pathlist_head symrefs;
312 struct got_pathlist_entry *pe;
313 int sent_my_capabilites = 0, have_sidebands = 0;
314 int found_branch = 0;
315 SHA1_CTX sha1_ctx;
316 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
317 size_t sha1_buf_len = 0;
318 ssize_t w;
319 struct got_ratelimit rl;
321 TAILQ_INIT(&symrefs);
322 SHA1Init(&sha1_ctx);
323 got_ratelimit_init(&rl, 0, 500);
325 have = malloc(refsz * sizeof(have[0]));
326 if (have == NULL)
327 return got_error_from_errno("malloc");
328 want = malloc(refsz * sizeof(want[0]));
329 if (want == NULL) {
330 err = got_error_from_errno("malloc");
331 goto done;
333 while (1) {
334 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
335 if (err)
336 goto done;
337 if (n == 0)
338 break;
339 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
340 err = fetch_error(&buf[4], n - 4);
341 goto done;
343 free(id_str);
344 free(refname);
345 err = got_gitproto_parse_refline(&id_str, &refname,
346 &server_capabilities, buf, n);
347 if (err)
348 goto done;
349 if (is_firstpkt) {
350 if (chattygot && server_capabilities[0] != '\0')
351 fprintf(stderr, "%s: server capabilities: %s\n",
352 getprogname(), server_capabilities);
353 err = got_gitproto_match_capabilities(&my_capabilities,
354 &symrefs, server_capabilities,
355 got_capabilities, nitems(got_capabilities));
356 if (err)
357 goto done;
358 if (chattygot)
359 fprintf(stderr, "%s: my capabilities:%s\n",
360 getprogname(), my_capabilities != NULL ?
361 my_capabilities : "");
362 err = send_fetch_symrefs(ibuf, &symrefs);
363 if (err)
364 goto done;
365 is_firstpkt = 0;
366 if (!fetch_all_branches) {
367 TAILQ_FOREACH(pe, &symrefs, entry) {
368 const char *name = pe->path;
369 const char *symref_target = pe->data;
370 if (strcmp(name, GOT_REF_HEAD) != 0)
371 continue;
372 default_branch = symref_target;
373 break;
376 continue;
378 if (strstr(refname, "^{}")) {
379 if (chattygot) {
380 fprintf(stderr, "%s: ignoring %s\n",
381 getprogname(), refname);
383 continue;
386 if (strncmp(refname, "refs/heads/", 11) == 0) {
387 if (fetch_all_branches || list_refs_only) {
388 found_branch = 1;
389 } else if (!TAILQ_EMPTY(wanted_branches)) {
390 TAILQ_FOREACH(pe, wanted_branches, entry) {
391 if (match_branch(refname, pe->path))
392 break;
394 if (pe == NULL) {
395 if (chattygot) {
396 fprintf(stderr,
397 "%s: ignoring %s\n",
398 getprogname(), refname);
400 continue;
402 found_branch = 1;
403 } else if (default_branch != NULL) {
404 if (!match_branch(refname, default_branch)) {
405 if (chattygot) {
406 fprintf(stderr,
407 "%s: ignoring %s\n",
408 getprogname(), refname);
410 continue;
412 found_branch = 1;
414 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
415 if (!TAILQ_EMPTY(wanted_refs)) {
416 TAILQ_FOREACH(pe, wanted_refs, entry) {
417 if (match_wanted_ref(refname, pe->path))
418 break;
420 if (pe == NULL) {
421 if (chattygot) {
422 fprintf(stderr,
423 "%s: ignoring %s\n",
424 getprogname(), refname);
426 continue;
428 found_branch = 1;
429 } else if (!list_refs_only) {
430 if (chattygot) {
431 fprintf(stderr, "%s: ignoring %s\n",
432 getprogname(), refname);
434 continue;
438 if (refsz == nref + 1) {
439 refsz *= 2;
440 have = reallocarray(have, refsz, sizeof(have[0]));
441 if (have == NULL) {
442 err = got_error_from_errno("reallocarray");
443 goto done;
445 want = reallocarray(want, refsz, sizeof(want[0]));
446 if (want == NULL) {
447 err = got_error_from_errno("reallocarray");
448 goto done;
451 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
452 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
453 goto done;
455 match_remote_ref(have_refs, &have[nref], refname);
456 err = send_fetch_ref(ibuf, &want[nref], refname);
457 if (err)
458 goto done;
460 if (chattygot)
461 fprintf(stderr, "%s: %s will be fetched\n",
462 getprogname(), refname);
463 if (chattygot > 1) {
464 char *theirs, *mine;
465 err = got_object_id_str(&theirs, &want[nref]);
466 if (err)
467 goto done;
468 err = got_object_id_str(&mine, &have[nref]);
469 if (err) {
470 free(theirs);
471 goto done;
473 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
474 getprogname(), theirs, getprogname(), mine);
475 free(theirs);
476 free(mine);
478 nref++;
481 if (list_refs_only)
482 goto done;
484 /* Abort if we haven't found any branch to fetch. */
485 if (!found_branch) {
486 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
487 goto done;
490 for (i = 0; i < nref; i++) {
491 if (got_object_id_cmp(&have[i], &want[i]) == 0)
492 continue;
493 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
494 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
495 sent_my_capabilites || my_capabilities == NULL ?
496 "" : my_capabilities);
497 if (n < 0 || (size_t)n >= sizeof(buf)) {
498 err = got_error(GOT_ERR_NO_SPACE);
499 goto done;
501 err = got_pkt_writepkt(fd, buf, n, chattygot);
502 if (err)
503 goto done;
504 sent_my_capabilites = 1;
505 nwant++;
507 err = got_pkt_flushpkt(fd, chattygot);
508 if (err)
509 goto done;
511 if (nwant == 0)
512 goto done;
514 TAILQ_FOREACH(pe, have_refs, entry) {
515 struct got_object_id *id = pe->data;
516 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
517 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
518 if (n < 0 || (size_t)n >= sizeof(buf)) {
519 err = got_error(GOT_ERR_NO_SPACE);
520 goto done;
522 err = got_pkt_writepkt(fd, buf, n, chattygot);
523 if (err)
524 goto done;
525 nhave++;
528 while (nhave > 0 && !acked) {
529 struct got_object_id common_id;
531 /* The server should ACK the object IDs we need. */
532 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
533 if (err)
534 goto done;
535 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
536 err = fetch_error(&buf[4], n - 4);
537 goto done;
539 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
540 /* Server has not located our objects yet. */
541 continue;
543 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
544 strncmp(buf, "ACK ", 4) != 0) {
545 err = got_error_msg(GOT_ERR_BAD_PACKET,
546 "unexpected message from server");
547 goto done;
549 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
550 err = got_error_msg(GOT_ERR_BAD_PACKET,
551 "bad object ID in ACK packet from server");
552 goto done;
554 acked++;
557 n = strlcpy(buf, "done\n", sizeof(buf));
558 err = got_pkt_writepkt(fd, buf, n, chattygot);
559 if (err)
560 goto done;
562 if (nhave == 0) {
563 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
564 if (err)
565 goto done;
566 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
567 err = got_error_msg(GOT_ERR_BAD_PACKET,
568 "unexpected message from server");
569 goto done;
573 if (chattygot)
574 fprintf(stderr, "%s: fetching...\n", getprogname());
576 if (my_capabilities != NULL &&
577 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
578 have_sidebands = 1;
580 while (1) {
581 ssize_t r = 0;
582 int datalen = -1;
584 if (have_sidebands) {
585 err = got_pkt_readhdr(&datalen, fd, chattygot);
586 if (err)
587 goto done;
588 if (datalen <= 0)
589 break;
591 /* Read sideband channel ID (one byte). */
592 r = read(fd, buf, 1);
593 if (r == -1) {
594 err = got_error_from_errno("read");
595 goto done;
597 if (r != 1) {
598 err = got_error_msg(GOT_ERR_BAD_PACKET,
599 "short packet");
600 goto done;
602 if (datalen > sizeof(buf) - 5) {
603 err = got_error_msg(GOT_ERR_BAD_PACKET,
604 "bad packet length");
605 goto done;
607 datalen--; /* sideband ID has been read */
608 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
609 /* Read packfile data. */
610 err = got_pkt_readn(&r, fd, buf, datalen);
611 if (err)
612 goto done;
613 if (r != datalen) {
614 err = got_error_msg(GOT_ERR_BAD_PACKET,
615 "packet too short");
616 goto done;
618 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
619 err = got_pkt_readn(&r, fd, buf, datalen);
620 if (err)
621 goto done;
622 if (r != datalen) {
623 err = got_error_msg(GOT_ERR_BAD_PACKET,
624 "packet too short");
625 goto done;
627 err = fetch_progress(ibuf, buf, r);
628 if (err)
629 goto done;
630 continue;
631 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
632 err = got_pkt_readn(&r, fd, buf, datalen);
633 if (err)
634 goto done;
635 if (r != datalen) {
636 err = got_error_msg(GOT_ERR_BAD_PACKET,
637 "packet too short");
638 goto done;
640 err = fetch_error(buf, r);
641 goto done;
642 } else if (buf[0] == 'A') {
643 err = got_pkt_readn(&r, fd, buf, datalen);
644 if (err)
645 goto done;
646 if (r != datalen) {
647 err = got_error_msg(GOT_ERR_BAD_PACKET,
648 "packet too short");
649 goto done;
651 /*
652 * Git server responds with ACK after 'done'
653 * even though multi_ack is disabled?!?
654 */
655 buf[r] = '\0';
656 if (strncmp(buf, "CK ", 3) == 0)
657 continue; /* ignore */
658 err = got_error_msg(GOT_ERR_BAD_PACKET,
659 "unexpected message from server");
660 goto done;
661 } else {
662 err = got_error_msg(GOT_ERR_BAD_PACKET,
663 "unknown side-band received from server");
664 goto done;
666 } else {
667 /* No sideband channel. Every byte is packfile data. */
668 err = got_pkt_readn(&r, fd, buf, sizeof buf);
669 if (err)
670 goto done;
671 if (r <= 0)
672 break;
675 /*
676 * An expected SHA1 checksum sits at the end of the pack file.
677 * Since we don't know the file size ahead of time we have to
678 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
679 * those bytes into our SHA1 checksum computation until we
680 * know for sure that additional pack file data bytes follow.
682 * We can assume r > 0 since otherwise the loop would exit.
683 */
684 if (r < SHA1_DIGEST_LENGTH) {
685 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
686 /*
687 * If there's enough buffered + read data to
688 * fill up the buffer then shift a sufficient
689 * amount of bytes out at the front to make
690 * room, mixing those bytes into the checksum.
691 */
692 if (sha1_buf_len > 0 &&
693 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
694 size_t nshift = MIN(sha1_buf_len + r -
695 SHA1_DIGEST_LENGTH, sha1_buf_len);
696 SHA1Update(&sha1_ctx, sha1_buf, nshift);
697 memmove(sha1_buf, sha1_buf + nshift,
698 sha1_buf_len - nshift);
699 sha1_buf_len -= nshift;
702 /* Buffer potential checksum bytes. */
703 memcpy(sha1_buf + sha1_buf_len, buf, r);
704 sha1_buf_len += r;
705 } else {
706 /*
707 * Mix in previously buffered bytes which
708 * are not part of the checksum after all.
709 */
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, &rl);
745 if (err)
746 goto done;
747 last_reported_packsz = packsz;
750 err = send_fetch_download_progress(ibuf, packsz, NULL);
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;
775 int
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);