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, const 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((unsigned char)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, sizeof(*refid)) == -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_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 struct got_object_id *have, struct got_object_id *want,
297 const char *refname, const char *id_str)
299 const struct got_error *err;
300 char *theirs = NULL, *mine = NULL;
302 if (!got_parse_sha1_digest(want->sha1, id_str)) {
303 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
304 goto done;
307 match_remote_ref(have_refs, have, refname);
308 err = send_fetch_ref(ibuf, want, refname);
309 if (err)
310 goto done;
312 if (chattygot)
313 fprintf(stderr, "%s: %s will be fetched\n",
314 getprogname(), refname);
315 if (chattygot > 1) {
316 err = got_object_id_str(&theirs, want);
317 if (err)
318 goto done;
319 err = got_object_id_str(&mine, have);
320 if (err)
321 goto done;
322 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 getprogname(), theirs, getprogname(), mine);
325 done:
326 free(theirs);
327 free(mine);
328 return err;
331 static const struct got_error *
332 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
333 struct got_pathlist_head *have_refs, int fetch_all_branches,
334 struct got_pathlist_head *wanted_branches,
335 struct got_pathlist_head *wanted_refs, int list_refs_only,
336 const char *worktree_branch, int no_head, struct imsgbuf *ibuf)
338 const struct got_error *err = NULL;
339 char buf[GOT_PKT_MAX];
340 char hashstr[SHA1_DIGEST_STRING_LENGTH];
341 struct got_object_id *have, *want;
342 int is_firstpkt = 1, nref = 0, refsz = 16;
343 int i, n, nwant = 0, nhave = 0, acked = 0;
344 off_t packsz = 0, last_reported_packsz = 0;
345 char *id_str = NULL, *refname = NULL;
346 char *server_capabilities = NULL, *my_capabilities = NULL;
347 const char *default_branch = NULL;
348 struct got_pathlist_head symrefs;
349 struct got_pathlist_entry *pe;
350 int sent_my_capabilites = 0, have_sidebands = 0;
351 SHA1_CTX sha1_ctx;
352 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
353 size_t sha1_buf_len = 0;
354 ssize_t w;
355 struct got_ratelimit rl;
357 TAILQ_INIT(&symrefs);
358 SHA1Init(&sha1_ctx);
359 got_ratelimit_init(&rl, 0, 500);
361 have = malloc(refsz * sizeof(have[0]));
362 if (have == NULL)
363 return got_error_from_errno("malloc");
364 want = malloc(refsz * sizeof(want[0]));
365 if (want == NULL) {
366 err = got_error_from_errno("malloc");
367 goto done;
369 while (1) {
370 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
371 if (err)
372 goto done;
373 if (n == 0)
374 break;
375 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
376 err = fetch_error(&buf[4], n - 4);
377 goto done;
379 free(id_str);
380 free(refname);
381 err = got_gitproto_parse_refline(&id_str, &refname,
382 &server_capabilities, buf, n);
383 if (err)
384 goto done;
386 if (refsz == nref + 1) {
387 struct got_object_id *h, *w;
389 refsz *= 2;
390 h = reallocarray(have, refsz, sizeof(have[0]));
391 if (h == NULL) {
392 err = got_error_from_errno("reallocarray");
393 goto done;
395 have = h;
396 w = reallocarray(want, refsz, sizeof(want[0]));
397 if (w == NULL) {
398 err = got_error_from_errno("reallocarray");
399 goto done;
401 want = w;
404 if (is_firstpkt) {
405 if (chattygot && server_capabilities[0] != '\0')
406 fprintf(stderr, "%s: server capabilities: %s\n",
407 getprogname(), server_capabilities);
408 err = got_gitproto_match_capabilities(&my_capabilities,
409 &symrefs, server_capabilities,
410 got_capabilities, nitems(got_capabilities));
411 if (err)
412 goto done;
413 if (chattygot)
414 fprintf(stderr, "%s: my capabilities:%s\n",
415 getprogname(), my_capabilities != NULL ?
416 my_capabilities : "");
417 err = send_fetch_symrefs(ibuf, &symrefs);
418 if (err)
419 goto done;
420 is_firstpkt = 0;
421 if (!fetch_all_branches) {
422 TAILQ_FOREACH(pe, &symrefs, entry) {
423 const char *name = pe->path;
424 const char *symref_target = pe->data;
425 if (strcmp(name, GOT_REF_HEAD) != 0)
426 continue;
427 default_branch = symref_target;
428 break;
431 if (default_branch)
432 continue;
434 if (strstr(refname, "^{}")) {
435 if (chattygot) {
436 fprintf(stderr, "%s: ignoring %s\n",
437 getprogname(), refname);
439 continue;
442 /* always fetch remote HEAD unless -b was specified */
443 if (!no_head && default_branch &&
444 strcmp(refname, default_branch) == 0 &&
445 strncmp(default_branch, "refs/heads/", 11) == 0) {
446 err = fetch_ref(ibuf, have_refs, &have[nref],
447 &want[nref], default_branch, id_str);
448 if (err)
449 goto done;
450 nref++;
451 continue;
454 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
455 err = fetch_ref(ibuf, have_refs, &have[nref],
456 &want[nref], refname, id_str);
457 if (err)
458 goto done;
459 nref++;
460 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
461 if (fetch_all_branches) {
462 err = fetch_ref(ibuf, have_refs, &have[nref],
463 &want[nref], refname, id_str);
464 if (err)
465 goto done;
466 nref++;
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 } else if (chattygot) {
481 fprintf(stderr, "%s: ignoring %s\n",
482 getprogname(), refname);
484 } else {
485 TAILQ_FOREACH(pe, wanted_refs, entry) {
486 if (match_wanted_ref(refname, pe->path))
487 break;
489 if (pe != NULL) {
490 err = fetch_ref(ibuf, have_refs, &have[nref],
491 &want[nref], refname, id_str);
492 if (err)
493 goto done;
494 nref++;
495 } else if (chattygot) {
496 fprintf(stderr, "%s: ignoring %s\n",
497 getprogname(), refname);
502 if (list_refs_only)
503 goto done;
505 /* Abort if we haven't found anything to fetch. */
506 if (nref == 0) {
507 struct got_pathlist_entry *pe;
508 static char msg[PATH_MAX + 33];
510 pe = TAILQ_FIRST(wanted_branches);
511 if (pe) {
512 snprintf(msg, sizeof(msg),
513 "branch \"%s\" not found on server", pe->path);
514 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
515 goto done;
518 pe = TAILQ_FIRST(wanted_refs);
519 if (pe) {
520 snprintf(msg, sizeof(msg),
521 "reference \"%s\" not found on server", pe->path);
522 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
523 goto done;
526 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
527 goto done;
530 for (i = 0; i < nref; i++) {
531 if (got_object_id_cmp(&have[i], &want[i]) == 0)
532 continue;
533 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
534 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
535 sent_my_capabilites || my_capabilities == NULL ?
536 "" : my_capabilities);
537 if (n < 0 || (size_t)n >= sizeof(buf)) {
538 err = got_error(GOT_ERR_NO_SPACE);
539 goto done;
541 err = got_pkt_writepkt(fd, buf, n, chattygot);
542 if (err)
543 goto done;
544 sent_my_capabilites = 1;
545 nwant++;
547 err = got_pkt_flushpkt(fd, chattygot);
548 if (err)
549 goto done;
551 if (nwant == 0)
552 goto done;
554 TAILQ_FOREACH(pe, have_refs, entry) {
555 struct got_object_id *id = pe->data;
556 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
557 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
558 if (n < 0 || (size_t)n >= sizeof(buf)) {
559 err = got_error(GOT_ERR_NO_SPACE);
560 goto done;
562 err = got_pkt_writepkt(fd, buf, n, chattygot);
563 if (err)
564 goto done;
565 nhave++;
568 while (nhave > 0 && !acked) {
569 struct got_object_id common_id;
571 /* The server should ACK the object IDs we need. */
572 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
573 if (err)
574 goto done;
575 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
576 err = fetch_error(&buf[4], n - 4);
577 goto done;
579 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
580 /* Server has not located our objects yet. */
581 continue;
583 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
584 strncmp(buf, "ACK ", 4) != 0) {
585 err = got_error_msg(GOT_ERR_BAD_PACKET,
586 "unexpected message from server");
587 goto done;
589 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
590 err = got_error_msg(GOT_ERR_BAD_PACKET,
591 "bad object ID in ACK packet from server");
592 goto done;
594 acked++;
597 n = strlcpy(buf, "done\n", sizeof(buf));
598 err = got_pkt_writepkt(fd, buf, n, chattygot);
599 if (err)
600 goto done;
602 if (nhave == 0) {
603 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
604 if (err)
605 goto done;
606 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
607 err = got_error_msg(GOT_ERR_BAD_PACKET,
608 "unexpected message from server");
609 goto done;
613 if (chattygot)
614 fprintf(stderr, "%s: fetching...\n", getprogname());
616 if (my_capabilities != NULL &&
617 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
618 have_sidebands = 1;
620 while (1) {
621 ssize_t r = 0;
622 int datalen = -1;
624 if (have_sidebands) {
625 err = got_pkt_readhdr(&datalen, fd, chattygot);
626 if (err)
627 goto done;
628 if (datalen <= 0)
629 break;
631 /* Read sideband channel ID (one byte). */
632 r = read(fd, buf, 1);
633 if (r == -1) {
634 err = got_error_from_errno("read");
635 goto done;
637 if (r != 1) {
638 err = got_error_msg(GOT_ERR_BAD_PACKET,
639 "short packet");
640 goto done;
642 if (datalen > sizeof(buf) - 5) {
643 err = got_error_msg(GOT_ERR_BAD_PACKET,
644 "bad packet length");
645 goto done;
647 datalen--; /* sideband ID has been read */
648 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
649 /* Read packfile data. */
650 err = got_pkt_readn(&r, fd, buf, datalen);
651 if (err)
652 goto done;
653 if (r != datalen) {
654 err = got_error_msg(GOT_ERR_BAD_PACKET,
655 "packet too short");
656 goto done;
658 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
659 err = got_pkt_readn(&r, fd, buf, datalen);
660 if (err)
661 goto done;
662 if (r != datalen) {
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "packet too short");
665 goto done;
667 err = fetch_progress(ibuf, buf, r);
668 if (err)
669 goto done;
670 continue;
671 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
672 err = got_pkt_readn(&r, fd, buf, datalen);
673 if (err)
674 goto done;
675 if (r != datalen) {
676 err = got_error_msg(GOT_ERR_BAD_PACKET,
677 "packet too short");
678 goto done;
680 err = fetch_error(buf, r);
681 goto done;
682 } else if (buf[0] == 'A') {
683 err = got_pkt_readn(&r, fd, buf, datalen);
684 if (err)
685 goto done;
686 if (r != datalen) {
687 err = got_error_msg(GOT_ERR_BAD_PACKET,
688 "packet too short");
689 goto done;
691 /*
692 * Git server responds with ACK after 'done'
693 * even though multi_ack is disabled?!?
694 */
695 buf[r] = '\0';
696 if (strncmp(buf, "CK ", 3) == 0)
697 continue; /* ignore */
698 err = got_error_msg(GOT_ERR_BAD_PACKET,
699 "unexpected message from server");
700 goto done;
701 } else {
702 err = got_error_msg(GOT_ERR_BAD_PACKET,
703 "unknown side-band received from server");
704 goto done;
706 } else {
707 /* No sideband channel. Every byte is packfile data. */
708 err = got_pkt_readn(&r, fd, buf, sizeof buf);
709 if (err)
710 goto done;
711 if (r <= 0)
712 break;
715 /*
716 * An expected SHA1 checksum sits at the end of the pack file.
717 * Since we don't know the file size ahead of time we have to
718 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
719 * those bytes into our SHA1 checksum computation until we
720 * know for sure that additional pack file data bytes follow.
722 * We can assume r > 0 since otherwise the loop would exit.
723 */
724 if (r < SHA1_DIGEST_LENGTH) {
725 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
726 /*
727 * If there's enough buffered + read data to
728 * fill up the buffer then shift a sufficient
729 * amount of bytes out at the front to make
730 * room, mixing those bytes into the checksum.
731 */
732 if (sha1_buf_len > 0 &&
733 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
734 size_t nshift = MIN(sha1_buf_len + r -
735 SHA1_DIGEST_LENGTH, sha1_buf_len);
736 SHA1Update(&sha1_ctx, sha1_buf, nshift);
737 memmove(sha1_buf, sha1_buf + nshift,
738 sha1_buf_len - nshift);
739 sha1_buf_len -= nshift;
742 /* Buffer potential checksum bytes. */
743 memcpy(sha1_buf + sha1_buf_len, buf, r);
744 sha1_buf_len += r;
745 } else {
746 /*
747 * Mix in previously buffered bytes which
748 * are not part of the checksum after all.
749 */
750 SHA1Update(&sha1_ctx, sha1_buf, r);
752 /* Update potential checksum buffer. */
753 memmove(sha1_buf, sha1_buf + r,
754 sha1_buf_len - r);
755 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
757 } else {
758 /* Mix in any previously buffered bytes. */
759 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
761 /* Mix in bytes read minus potential checksum bytes. */
762 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
764 /* Buffer potential checksum bytes. */
765 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
766 SHA1_DIGEST_LENGTH);
767 sha1_buf_len = SHA1_DIGEST_LENGTH;
770 /* Write packfile data to temporary pack file. */
771 w = write(packfd, buf, r);
772 if (w == -1) {
773 err = got_error_from_errno("write");
774 goto done;
776 if (w != r) {
777 err = got_error(GOT_ERR_IO);
778 goto done;
780 packsz += w;
782 /* Don't send too many progress privsep messages. */
783 if (packsz > last_reported_packsz + 1024) {
784 err = send_fetch_download_progress(ibuf, packsz, &rl);
785 if (err)
786 goto done;
787 last_reported_packsz = packsz;
790 err = send_fetch_download_progress(ibuf, packsz, NULL);
791 if (err)
792 goto done;
794 SHA1Final(pack_sha1, &sha1_ctx);
795 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
796 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
797 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
798 "pack file checksum mismatch");
800 done:
801 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
802 free(have);
803 free(want);
804 free(id_str);
805 free(refname);
806 free(server_capabilities);
807 return err;
811 int
812 main(int argc, char **argv)
814 const struct got_error *err = NULL;
815 int fetchfd = -1, packfd = -1;
816 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
817 struct imsgbuf ibuf;
818 struct imsg imsg;
819 struct got_pathlist_head have_refs;
820 struct got_pathlist_head wanted_branches;
821 struct got_pathlist_head wanted_refs;
822 struct got_imsg_fetch_request fetch_req;
823 struct got_imsg_fetch_have_ref href;
824 struct got_imsg_fetch_wanted_branch wbranch;
825 struct got_imsg_fetch_wanted_ref wref;
826 size_t datalen, i;
827 char *worktree_branch = NULL;
828 #if 0
829 static int attached;
830 while (!attached)
831 sleep (1);
832 #endif
834 TAILQ_INIT(&have_refs);
835 TAILQ_INIT(&wanted_branches);
836 TAILQ_INIT(&wanted_refs);
838 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
839 #ifndef PROFILE
840 /* revoke access to most system calls */
841 if (pledge("stdio recvfd", NULL) == -1) {
842 err = got_error_from_errno("pledge");
843 got_privsep_send_error(&ibuf, err);
844 return 1;
846 #endif
847 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
848 if (err) {
849 if (err->code == GOT_ERR_PRIVSEP_PIPE)
850 err = NULL;
851 goto done;
853 if (imsg.hdr.type == GOT_IMSG_STOP)
854 goto done;
855 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
856 err = got_error(GOT_ERR_PRIVSEP_MSG);
857 goto done;
859 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
860 if (datalen < sizeof(fetch_req)) {
861 err = got_error(GOT_ERR_PRIVSEP_LEN);
862 goto done;
864 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
865 fetchfd = imsg.fd;
867 if (datalen != sizeof(fetch_req) +
868 fetch_req.worktree_branch_len) {
869 err = got_error(GOT_ERR_PRIVSEP_LEN);
870 goto done;
873 if (fetch_req.worktree_branch_len != 0) {
874 worktree_branch = strndup(imsg.data +
875 sizeof(fetch_req), fetch_req.worktree_branch_len);
876 if (worktree_branch == NULL) {
877 err = got_error_from_errno("strndup");
878 goto done;
882 imsg_free(&imsg);
884 if (fetch_req.verbosity > 0)
885 chattygot += fetch_req.verbosity;
887 for (i = 0; i < fetch_req.n_have_refs; i++) {
888 struct got_object_id *id;
889 char *refname;
891 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
892 if (err) {
893 if (err->code == GOT_ERR_PRIVSEP_PIPE)
894 err = NULL;
895 goto done;
897 if (imsg.hdr.type == GOT_IMSG_STOP)
898 goto done;
899 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
900 err = got_error(GOT_ERR_PRIVSEP_MSG);
901 goto done;
903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
904 if (datalen < sizeof(href)) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
906 goto done;
908 memcpy(&href, imsg.data, sizeof(href));
909 if (datalen - sizeof(href) < href.name_len) {
910 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 goto done;
913 refname = strndup(imsg.data + sizeof(href), href.name_len);
914 if (refname == NULL) {
915 err = got_error_from_errno("strndup");
916 goto done;
919 id = malloc(sizeof(*id));
920 if (id == NULL) {
921 free(refname);
922 err = got_error_from_errno("malloc");
923 goto done;
925 memcpy(id, &href.id, sizeof(*id));
926 err = got_pathlist_append(&have_refs, refname, id);
927 if (err) {
928 free(refname);
929 free(id);
930 goto done;
933 imsg_free(&imsg);
936 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
937 char *refname;
939 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
940 if (err) {
941 if (err->code == GOT_ERR_PRIVSEP_PIPE)
942 err = NULL;
943 goto done;
945 if (imsg.hdr.type == GOT_IMSG_STOP)
946 goto done;
947 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
948 err = got_error(GOT_ERR_PRIVSEP_MSG);
949 goto done;
951 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
952 if (datalen < sizeof(wbranch)) {
953 err = got_error(GOT_ERR_PRIVSEP_LEN);
954 goto done;
956 memcpy(&wbranch, imsg.data, sizeof(wbranch));
957 if (datalen - sizeof(wbranch) < wbranch.name_len) {
958 err = got_error(GOT_ERR_PRIVSEP_LEN);
959 goto done;
961 refname = strndup(imsg.data + sizeof(wbranch),
962 wbranch.name_len);
963 if (refname == NULL) {
964 err = got_error_from_errno("strndup");
965 goto done;
968 err = got_pathlist_append(&wanted_branches, refname, NULL);
969 if (err) {
970 free(refname);
971 goto done;
974 imsg_free(&imsg);
977 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
978 char *refname;
980 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
981 if (err) {
982 if (err->code == GOT_ERR_PRIVSEP_PIPE)
983 err = NULL;
984 goto done;
986 if (imsg.hdr.type == GOT_IMSG_STOP)
987 goto done;
988 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
989 err = got_error(GOT_ERR_PRIVSEP_MSG);
990 goto done;
992 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
993 if (datalen < sizeof(wref)) {
994 err = got_error(GOT_ERR_PRIVSEP_LEN);
995 goto done;
997 memcpy(&wref, imsg.data, sizeof(wref));
998 if (datalen - sizeof(wref) < wref.name_len) {
999 err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 goto done;
1002 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1003 if (refname == NULL) {
1004 err = got_error_from_errno("strndup");
1005 goto done;
1008 err = got_pathlist_append(&wanted_refs, refname, NULL);
1009 if (err) {
1010 free(refname);
1011 goto done;
1014 imsg_free(&imsg);
1017 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1018 if (err) {
1019 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1020 err = NULL;
1021 goto done;
1023 if (imsg.hdr.type == GOT_IMSG_STOP)
1024 goto done;
1025 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1026 err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 goto done;
1029 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1030 err = got_error(GOT_ERR_PRIVSEP_LEN);
1031 goto done;
1033 packfd = imsg.fd;
1035 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1036 fetch_req.fetch_all_branches, &wanted_branches,
1037 &wanted_refs, fetch_req.list_refs_only,
1038 worktree_branch, fetch_req.no_head, &ibuf);
1039 done:
1040 free(worktree_branch);
1041 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1042 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1043 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1044 err = got_error_from_errno("close");
1045 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1046 err = got_error_from_errno("close");
1047 if (err != NULL)
1048 got_privsep_send_error(&ibuf, err);
1049 else
1050 err = send_fetch_done(&ibuf, pack_sha1);
1051 if (err != NULL) {
1052 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1053 got_privsep_send_error(&ibuf, err);
1056 exit(0);