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 <sha2.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <err.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_version.h"
43 #include "got_fetch.h"
44 #include "got_reference.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pkt.h"
53 #include "got_lib_gitproto.h"
54 #include "got_lib_ratelimit.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 struct got_object *indexed;
65 static int chattygot;
67 static const struct got_capability got_capabilities[] = {
68 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
69 { GOT_CAPA_OFS_DELTA, NULL },
70 { GOT_CAPA_SIDE_BAND_64K, NULL },
71 };
73 static void
74 match_remote_ref(struct got_pathlist_head *have_refs,
75 struct got_object_id *my_id, const char *refname)
76 {
77 struct got_pathlist_entry *pe;
79 /* XXX zero-hash signifies we don't have this ref;
80 * we should use a flag instead */
81 memset(my_id, 0, sizeof(*my_id));
83 TAILQ_FOREACH(pe, have_refs, entry) {
84 struct got_object_id *id = pe->data;
85 if (strcmp(pe->path, refname) == 0) {
86 memcpy(my_id, id, sizeof(*my_id));
87 break;
88 }
89 }
90 }
92 static int
93 match_branch(const char *branch, const char *wanted_branch)
94 {
95 if (strncmp(branch, "refs/heads/", 11) != 0)
96 return 0;
98 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
99 wanted_branch += 11;
101 return (strcmp(branch + 11, wanted_branch) == 0);
104 static int
105 match_wanted_ref(const char *refname, const char *wanted_ref)
107 if (strncmp(refname, "refs/", 5) != 0)
108 return 0;
109 refname += 5;
111 /*
112 * Prevent fetching of references that won't make any
113 * sense outside of the remote repository's context.
114 */
115 if (strncmp(refname, "got/", 4) == 0)
116 return 0;
117 if (strncmp(refname, "remotes/", 8) == 0)
118 return 0;
120 if (strncmp(wanted_ref, "refs/", 5) == 0)
121 wanted_ref += 5;
123 /* Allow prefix match. */
124 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
125 return 1;
127 /* Allow exact match. */
128 return (strcmp(refname, wanted_ref) == 0);
131 static const struct got_error *
132 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
134 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
135 return got_error(GOT_ERR_NO_SPACE);
137 if (msglen == 0)
138 return NULL;
140 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
141 msg, msglen) == -1)
142 return got_error_from_errno(
143 "imsg_compose FETCH_SERVER_PROGRESS");
145 return got_privsep_flush_imsg(ibuf);
148 static const struct got_error *
149 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
150 struct got_ratelimit *rl)
152 const struct got_error *err;
153 int elapsed = 0;
155 if (rl) {
156 err = got_ratelimit_check(&elapsed, rl);
157 if (err || !elapsed)
158 return err;
161 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
162 &bytes, sizeof(bytes)) == -1)
163 return got_error_from_errno(
164 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
166 return got_privsep_flush_imsg(ibuf);
169 static const struct got_error *
170 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
172 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
173 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
174 return got_error_from_errno("imsg_compose FETCH");
175 return got_privsep_flush_imsg(ibuf);
178 static const struct got_error *
179 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
181 size_t i;
183 if (len == 0)
184 return NULL;
186 /*
187 * Truncate messages which exceed the maximum imsg payload size.
188 * Server may send up to 64k.
189 */
190 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
191 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
193 /* Only allow printable ASCII. */
194 for (i = 0; i < len; i++) {
195 if (isprint((unsigned char)buf[i]) ||
196 isspace((unsigned char)buf[i]))
197 continue;
198 return got_error_msg(GOT_ERR_BAD_PACKET,
199 "non-printable progress message received from server");
202 return send_fetch_server_progress(ibuf, buf, len);
205 static const struct got_error *
206 fetch_error(const char *buf, size_t len)
208 static char msg[1024];
209 size_t i;
211 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
212 if (!isprint((unsigned char)buf[i]))
213 return got_error_msg(GOT_ERR_BAD_PACKET,
214 "non-printable error message received from server");
215 msg[i] = buf[i];
217 msg[i] = '\0';
218 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
221 static const struct got_error *
222 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
224 struct ibuf *wbuf;
225 size_t len, nsymrefs = 0;
226 struct got_pathlist_entry *pe;
228 len = sizeof(struct got_imsg_fetch_symrefs);
229 TAILQ_FOREACH(pe, symrefs, entry) {
230 const char *target = pe->data;
231 len += sizeof(struct got_imsg_fetch_symref) +
232 pe->path_len + strlen(target);
233 nsymrefs++;
236 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
237 return got_error(GOT_ERR_NO_SPACE);
239 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
240 if (wbuf == NULL)
241 return got_error_from_errno("imsg_create FETCH_SYMREFS");
243 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
244 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
245 return got_error_from_errno("imsg_add FETCH_SYMREFS");
247 TAILQ_FOREACH(pe, symrefs, entry) {
248 const char *name = pe->path;
249 size_t name_len = pe->path_len;
250 const char *target = pe->data;
251 size_t target_len = strlen(target);
253 /* Keep in sync with struct got_imsg_fetch_symref definition! */
254 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
255 return got_error_from_errno("imsg_add FETCH_SYMREFS");
256 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
257 return got_error_from_errno("imsg_add FETCH_SYMREFS");
258 if (imsg_add(wbuf, name, name_len) == -1)
259 return got_error_from_errno("imsg_add FETCH_SYMREFS");
260 if (imsg_add(wbuf, target, target_len) == -1)
261 return got_error_from_errno("imsg_add FETCH_SYMREFS");
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 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, sizeof(*refid)) == -1)
286 return got_error_from_errno("imsg_add FETCH_REF");
287 if (imsg_add(wbuf, refname, reflen) == -1)
288 return got_error_from_errno("imsg_add FETCH_REF");
290 wbuf->fd = -1;
291 imsg_close(ibuf, wbuf);
292 return got_privsep_flush_imsg(ibuf);
295 static const struct got_error *
296 fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
297 struct got_object_id *have, struct got_object_id *want,
298 const char *refname, const char *id_str)
300 const struct got_error *err;
301 char *theirs = NULL, *mine = NULL;
303 memset(want, 0, sizeof(*want));
304 if (!got_parse_sha1_digest(want->hash, id_str)) {
305 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
306 goto done;
309 match_remote_ref(have_refs, have, refname);
310 err = send_fetch_ref(ibuf, want, refname);
311 if (err)
312 goto done;
314 if (chattygot)
315 fprintf(stderr, "%s: %s will be fetched\n",
316 getprogname(), refname);
317 if (chattygot > 1) {
318 err = got_object_id_str(&theirs, want);
319 if (err)
320 goto done;
321 err = got_object_id_str(&mine, have);
322 if (err)
323 goto done;
324 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
325 getprogname(), theirs, getprogname(), mine);
327 done:
328 free(theirs);
329 free(mine);
330 return err;
333 static const struct got_error *
334 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
335 struct got_pathlist_head *have_refs, int fetch_all_branches,
336 struct got_pathlist_head *wanted_branches,
337 struct got_pathlist_head *wanted_refs, int list_refs_only,
338 const char *worktree_branch, struct imsgbuf *ibuf)
340 const struct got_error *err = NULL;
341 char buf[GOT_PKT_MAX];
342 char hashstr[SHA1_DIGEST_STRING_LENGTH];
343 struct got_object_id *have, *want;
344 int is_firstpkt = 1, nref = 0, refsz = 16;
345 int i, n, nwant = 0, nhave = 0, acked = 0;
346 off_t packsz = 0, last_reported_packsz = 0;
347 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
348 char *server_capabilities = NULL, *my_capabilities = NULL;
349 const char *default_branch = NULL;
350 struct got_pathlist_head symrefs;
351 struct got_pathlist_entry *pe;
352 int sent_my_capabilites = 0, have_sidebands = 0;
353 int found_branch = 0;
354 SHA1_CTX sha1_ctx;
355 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
356 size_t sha1_buf_len = 0;
357 ssize_t w;
358 struct got_ratelimit rl;
360 TAILQ_INIT(&symrefs);
361 SHA1Init(&sha1_ctx);
362 got_ratelimit_init(&rl, 0, 500);
364 have = malloc(refsz * sizeof(have[0]));
365 if (have == NULL)
366 return got_error_from_errno("malloc");
367 want = malloc(refsz * sizeof(want[0]));
368 if (want == NULL) {
369 err = got_error_from_errno("malloc");
370 goto done;
372 while (1) {
373 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
374 if (err)
375 goto done;
376 if (n == 0)
377 break;
378 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
379 err = fetch_error(&buf[4], n - 4);
380 goto done;
382 free(id_str);
383 free(refname);
384 err = got_gitproto_parse_refline(&id_str, &refname,
385 &server_capabilities, buf, n);
386 if (err)
387 goto done;
389 if (refsz == nref + 1) {
390 struct got_object_id *h, *w;
392 refsz *= 2;
393 h = reallocarray(have, refsz, sizeof(have[0]));
394 if (h == NULL) {
395 err = got_error_from_errno("reallocarray");
396 goto done;
398 have = h;
399 w = reallocarray(want, refsz, sizeof(want[0]));
400 if (w == NULL) {
401 err = got_error_from_errno("reallocarray");
402 goto done;
404 want = w;
407 if (is_firstpkt) {
408 if (chattygot && server_capabilities[0] != '\0')
409 fprintf(stderr, "%s: server capabilities: %s\n",
410 getprogname(), server_capabilities);
411 err = got_gitproto_match_capabilities(&my_capabilities,
412 &symrefs, server_capabilities,
413 got_capabilities, nitems(got_capabilities));
414 if (err)
415 goto done;
416 if (chattygot)
417 fprintf(stderr, "%s: my capabilities:%s\n",
418 getprogname(), my_capabilities != NULL ?
419 my_capabilities : "");
420 err = send_fetch_symrefs(ibuf, &symrefs);
421 if (err)
422 goto done;
423 is_firstpkt = 0;
424 if (!fetch_all_branches) {
425 TAILQ_FOREACH(pe, &symrefs, entry) {
426 const char *name = pe->path;
427 const char *symref_target = pe->data;
428 if (strcmp(name, GOT_REF_HEAD) != 0)
429 continue;
430 default_branch = symref_target;
431 break;
434 if (default_branch)
435 continue;
437 if (strstr(refname, "^{}")) {
438 if (chattygot) {
439 fprintf(stderr, "%s: ignoring %s\n",
440 getprogname(), refname);
442 continue;
444 if (default_branch && default_id_str == NULL &&
445 strcmp(refname, default_branch) == 0) {
446 default_id_str = strdup(id_str);
447 if (default_id_str == NULL) {
448 err = got_error_from_errno("strdup");
449 goto done;
453 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
454 err = fetch_ref(ibuf, have_refs, &have[nref],
455 &want[nref], refname, id_str);
456 if (err)
457 goto done;
458 nref++;
459 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
460 if (fetch_all_branches) {
461 err = fetch_ref(ibuf, have_refs, &have[nref],
462 &want[nref], refname, id_str);
463 if (err)
464 goto done;
465 nref++;
466 found_branch = 1;
467 continue;
469 TAILQ_FOREACH(pe, wanted_branches, entry) {
470 if (match_branch(refname, pe->path))
471 break;
473 if (pe != NULL || (worktree_branch != NULL &&
474 match_branch(refname, worktree_branch))) {
475 err = fetch_ref(ibuf, have_refs, &have[nref],
476 &want[nref], refname, id_str);
477 if (err)
478 goto done;
479 nref++;
480 found_branch = 1;
481 } else if (chattygot) {
482 fprintf(stderr, "%s: ignoring %s\n",
483 getprogname(), refname);
485 } else {
486 TAILQ_FOREACH(pe, wanted_refs, entry) {
487 if (match_wanted_ref(refname, pe->path))
488 break;
490 if (pe != NULL) {
491 err = fetch_ref(ibuf, have_refs, &have[nref],
492 &want[nref], refname, id_str);
493 if (err)
494 goto done;
495 nref++;
496 } else if (chattygot) {
497 fprintf(stderr, "%s: ignoring %s\n",
498 getprogname(), refname);
503 if (list_refs_only)
504 goto done;
506 if (!found_branch && default_branch && default_id_str &&
507 strncmp(default_branch, "refs/heads/", 11) == 0) {
508 err = fetch_ref(ibuf, have_refs, &have[nref],
509 &want[nref], default_branch, default_id_str);
510 if (err)
511 goto done;
512 nref++;
513 found_branch = 1;
516 /* Abort if we haven't found anything to fetch. */
517 if (nref == 0) {
518 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
519 goto done;
522 for (i = 0; i < nref; i++) {
523 if (got_object_id_cmp(&have[i], &want[i]) == 0)
524 continue;
525 got_sha1_digest_to_str(want[i].hash, hashstr, sizeof(hashstr));
526 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
527 sent_my_capabilites || my_capabilities == NULL ?
528 "" : my_capabilities);
529 if (n < 0 || (size_t)n >= sizeof(buf)) {
530 err = got_error(GOT_ERR_NO_SPACE);
531 goto done;
533 err = got_pkt_writepkt(fd, buf, n, chattygot);
534 if (err)
535 goto done;
536 sent_my_capabilites = 1;
537 nwant++;
539 err = got_pkt_flushpkt(fd, chattygot);
540 if (err)
541 goto done;
543 if (nwant == 0)
544 goto done;
546 TAILQ_FOREACH(pe, have_refs, entry) {
547 struct got_object_id *id = pe->data;
548 got_sha1_digest_to_str(id->hash, hashstr, sizeof(hashstr));
549 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
550 if (n < 0 || (size_t)n >= sizeof(buf)) {
551 err = got_error(GOT_ERR_NO_SPACE);
552 goto done;
554 err = got_pkt_writepkt(fd, buf, n, chattygot);
555 if (err)
556 goto done;
557 nhave++;
560 while (nhave > 0 && !acked) {
561 struct got_object_id common_id;
563 /* The server should ACK the object IDs we need. */
564 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
565 if (err)
566 goto done;
567 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
568 err = fetch_error(&buf[4], n - 4);
569 goto done;
571 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
572 /* Server has not located our objects yet. */
573 continue;
575 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
576 strncmp(buf, "ACK ", 4) != 0) {
577 err = got_error_msg(GOT_ERR_BAD_PACKET,
578 "unexpected message from server");
579 goto done;
582 memset(&common_id, 0, sizeof(common_id));
583 if (!got_parse_sha1_digest(common_id.hash, buf + 4)) {
584 err = got_error_msg(GOT_ERR_BAD_PACKET,
585 "bad object ID in ACK packet from server");
586 goto done;
588 acked++;
591 n = strlcpy(buf, "done\n", sizeof(buf));
592 err = got_pkt_writepkt(fd, buf, n, chattygot);
593 if (err)
594 goto done;
596 if (nhave == 0) {
597 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
598 if (err)
599 goto done;
600 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
601 err = got_error_msg(GOT_ERR_BAD_PACKET,
602 "unexpected message from server");
603 goto done;
607 if (chattygot)
608 fprintf(stderr, "%s: fetching...\n", getprogname());
610 if (my_capabilities != NULL &&
611 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
612 have_sidebands = 1;
614 while (1) {
615 ssize_t r = 0;
616 int datalen = -1;
618 if (have_sidebands) {
619 err = got_pkt_readhdr(&datalen, fd, chattygot);
620 if (err)
621 goto done;
622 if (datalen <= 0)
623 break;
625 /* Read sideband channel ID (one byte). */
626 r = read(fd, buf, 1);
627 if (r == -1) {
628 err = got_error_from_errno("read");
629 goto done;
631 if (r != 1) {
632 err = got_error_msg(GOT_ERR_BAD_PACKET,
633 "short packet");
634 goto done;
636 if (datalen > sizeof(buf) - 5) {
637 err = got_error_msg(GOT_ERR_BAD_PACKET,
638 "bad packet length");
639 goto done;
641 datalen--; /* sideband ID has been read */
642 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
643 /* Read packfile data. */
644 err = got_pkt_readn(&r, fd, buf, datalen);
645 if (err)
646 goto done;
647 if (r != datalen) {
648 err = got_error_msg(GOT_ERR_BAD_PACKET,
649 "packet too short");
650 goto done;
652 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
653 err = got_pkt_readn(&r, fd, buf, datalen);
654 if (err)
655 goto done;
656 if (r != datalen) {
657 err = got_error_msg(GOT_ERR_BAD_PACKET,
658 "packet too short");
659 goto done;
661 err = fetch_progress(ibuf, buf, r);
662 if (err)
663 goto done;
664 continue;
665 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
666 err = got_pkt_readn(&r, fd, buf, datalen);
667 if (err)
668 goto done;
669 if (r != datalen) {
670 err = got_error_msg(GOT_ERR_BAD_PACKET,
671 "packet too short");
672 goto done;
674 err = fetch_error(buf, r);
675 goto done;
676 } else if (buf[0] == 'A') {
677 err = got_pkt_readn(&r, fd, buf, datalen);
678 if (err)
679 goto done;
680 if (r != datalen) {
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 "packet too short");
683 goto done;
685 /*
686 * Git server responds with ACK after 'done'
687 * even though multi_ack is disabled?!?
688 */
689 buf[r] = '\0';
690 if (strncmp(buf, "CK ", 3) == 0)
691 continue; /* ignore */
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
693 "unexpected message from server");
694 goto done;
695 } else {
696 err = got_error_msg(GOT_ERR_BAD_PACKET,
697 "unknown side-band received from server");
698 goto done;
700 } else {
701 /* No sideband channel. Every byte is packfile data. */
702 err = got_pkt_readn(&r, fd, buf, sizeof buf);
703 if (err)
704 goto done;
705 if (r <= 0)
706 break;
709 /*
710 * An expected SHA1 checksum sits at the end of the pack file.
711 * Since we don't know the file size ahead of time we have to
712 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
713 * those bytes into our SHA1 checksum computation until we
714 * know for sure that additional pack file data bytes follow.
716 * We can assume r > 0 since otherwise the loop would exit.
717 */
718 if (r < SHA1_DIGEST_LENGTH) {
719 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
720 /*
721 * If there's enough buffered + read data to
722 * fill up the buffer then shift a sufficient
723 * amount of bytes out at the front to make
724 * room, mixing those bytes into the checksum.
725 */
726 if (sha1_buf_len > 0 &&
727 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
728 size_t nshift = MIN(sha1_buf_len + r -
729 SHA1_DIGEST_LENGTH, sha1_buf_len);
730 SHA1Update(&sha1_ctx, sha1_buf, nshift);
731 memmove(sha1_buf, sha1_buf + nshift,
732 sha1_buf_len - nshift);
733 sha1_buf_len -= nshift;
736 /* Buffer potential checksum bytes. */
737 memcpy(sha1_buf + sha1_buf_len, buf, r);
738 sha1_buf_len += r;
739 } else {
740 /*
741 * Mix in previously buffered bytes which
742 * are not part of the checksum after all.
743 */
744 SHA1Update(&sha1_ctx, sha1_buf, r);
746 /* Update potential checksum buffer. */
747 memmove(sha1_buf, sha1_buf + r,
748 sha1_buf_len - r);
749 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
751 } else {
752 /* Mix in any previously buffered bytes. */
753 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
755 /* Mix in bytes read minus potential checksum bytes. */
756 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
758 /* Buffer potential checksum bytes. */
759 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
760 SHA1_DIGEST_LENGTH);
761 sha1_buf_len = SHA1_DIGEST_LENGTH;
764 /* Write packfile data to temporary pack file. */
765 w = write(packfd, buf, r);
766 if (w == -1) {
767 err = got_error_from_errno("write");
768 goto done;
770 if (w != r) {
771 err = got_error(GOT_ERR_IO);
772 goto done;
774 packsz += w;
776 /* Don't send too many progress privsep messages. */
777 if (packsz > last_reported_packsz + 1024) {
778 err = send_fetch_download_progress(ibuf, packsz, &rl);
779 if (err)
780 goto done;
781 last_reported_packsz = packsz;
784 err = send_fetch_download_progress(ibuf, packsz, NULL);
785 if (err)
786 goto done;
788 SHA1Final(pack_sha1, &sha1_ctx);
789 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
790 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
791 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
792 "pack file checksum mismatch");
794 done:
795 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
796 free(have);
797 free(want);
798 free(id_str);
799 free(default_id_str);
800 free(refname);
801 free(server_capabilities);
802 return err;
806 int
807 main(int argc, char **argv)
809 const struct got_error *err = NULL;
810 int fetchfd = -1, packfd = -1;
811 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
812 struct imsgbuf ibuf;
813 struct imsg imsg;
814 struct got_pathlist_head have_refs;
815 struct got_pathlist_head wanted_branches;
816 struct got_pathlist_head wanted_refs;
817 struct got_imsg_fetch_request fetch_req;
818 struct got_imsg_fetch_have_ref href;
819 struct got_imsg_fetch_wanted_branch wbranch;
820 struct got_imsg_fetch_wanted_ref wref;
821 size_t datalen, i;
822 char *worktree_branch = NULL;
823 #if 0
824 static int attached;
825 while (!attached)
826 sleep (1);
827 #endif
829 TAILQ_INIT(&have_refs);
830 TAILQ_INIT(&wanted_branches);
831 TAILQ_INIT(&wanted_refs);
833 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
834 #ifndef PROFILE
835 /* revoke access to most system calls */
836 if (pledge("stdio recvfd", NULL) == -1) {
837 err = got_error_from_errno("pledge");
838 got_privsep_send_error(&ibuf, err);
839 return 1;
841 #endif
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_REQUEST) {
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 goto done;
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 if (datalen < sizeof(fetch_req)) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
857 goto done;
859 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
860 fetchfd = imsg.fd;
862 if (datalen != sizeof(fetch_req) +
863 fetch_req.worktree_branch_len) {
864 err = got_error(GOT_ERR_PRIVSEP_LEN);
865 goto done;
868 if (fetch_req.worktree_branch_len != 0) {
869 worktree_branch = strndup(imsg.data +
870 sizeof(fetch_req), fetch_req.worktree_branch_len);
871 if (worktree_branch == NULL) {
872 err = got_error_from_errno("strndup");
873 goto done;
877 imsg_free(&imsg);
879 if (fetch_req.verbosity > 0)
880 chattygot += fetch_req.verbosity;
882 for (i = 0; i < fetch_req.n_have_refs; i++) {
883 struct got_object_id *id;
884 char *refname;
886 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
887 if (err) {
888 if (err->code == GOT_ERR_PRIVSEP_PIPE)
889 err = NULL;
890 goto done;
892 if (imsg.hdr.type == GOT_IMSG_STOP)
893 goto done;
894 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
895 err = got_error(GOT_ERR_PRIVSEP_MSG);
896 goto done;
898 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
899 if (datalen < sizeof(href)) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
901 goto done;
903 memcpy(&href, imsg.data, sizeof(href));
904 if (datalen - sizeof(href) < href.name_len) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
906 goto done;
908 refname = strndup(imsg.data + sizeof(href), href.name_len);
909 if (refname == NULL) {
910 err = got_error_from_errno("strndup");
911 goto done;
914 id = malloc(sizeof(*id));
915 if (id == NULL) {
916 free(refname);
917 err = got_error_from_errno("malloc");
918 goto done;
920 memcpy(id, &href.id, sizeof(*id));
921 err = got_pathlist_append(&have_refs, refname, id);
922 if (err) {
923 free(refname);
924 free(id);
925 goto done;
928 imsg_free(&imsg);
931 for (i = 0; i < fetch_req.n_wanted_branches; 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_BRANCH) {
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
944 goto done;
946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 if (datalen < sizeof(wbranch)) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 goto done;
951 memcpy(&wbranch, imsg.data, sizeof(wbranch));
952 if (datalen - sizeof(wbranch) < wbranch.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_LEN);
954 goto done;
956 refname = strndup(imsg.data + sizeof(wbranch),
957 wbranch.name_len);
958 if (refname == NULL) {
959 err = got_error_from_errno("strndup");
960 goto done;
963 err = got_pathlist_append(&wanted_branches, refname, NULL);
964 if (err) {
965 free(refname);
966 goto done;
969 imsg_free(&imsg);
972 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
973 char *refname;
975 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
976 if (err) {
977 if (err->code == GOT_ERR_PRIVSEP_PIPE)
978 err = NULL;
979 goto done;
981 if (imsg.hdr.type == GOT_IMSG_STOP)
982 goto done;
983 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
984 err = got_error(GOT_ERR_PRIVSEP_MSG);
985 goto done;
987 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
988 if (datalen < sizeof(wref)) {
989 err = got_error(GOT_ERR_PRIVSEP_LEN);
990 goto done;
992 memcpy(&wref, imsg.data, sizeof(wref));
993 if (datalen - sizeof(wref) < wref.name_len) {
994 err = got_error(GOT_ERR_PRIVSEP_LEN);
995 goto done;
997 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
998 if (refname == NULL) {
999 err = got_error_from_errno("strndup");
1000 goto done;
1003 err = got_pathlist_append(&wanted_refs, refname, NULL);
1004 if (err) {
1005 free(refname);
1006 goto done;
1009 imsg_free(&imsg);
1012 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1013 if (err) {
1014 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1015 err = NULL;
1016 goto done;
1018 if (imsg.hdr.type == GOT_IMSG_STOP)
1019 goto done;
1020 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1021 err = got_error(GOT_ERR_PRIVSEP_MSG);
1022 goto done;
1024 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1025 err = got_error(GOT_ERR_PRIVSEP_LEN);
1026 goto done;
1028 packfd = imsg.fd;
1030 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1031 fetch_req.fetch_all_branches, &wanted_branches,
1032 &wanted_refs, fetch_req.list_refs_only,
1033 worktree_branch, &ibuf);
1034 done:
1035 free(worktree_branch);
1036 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1037 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1038 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1039 err = got_error_from_errno("close");
1040 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1041 err = got_error_from_errno("close");
1042 if (err != NULL)
1043 got_privsep_send_error(&ibuf, err);
1044 else
1045 err = send_fetch_done(&ibuf, pack_sha1);
1046 if (err != NULL) {
1047 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1048 got_privsep_send_error(&ibuf, err);
1051 exit(0);