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, 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, *default_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 int found_branch = 0;
352 SHA1_CTX sha1_ctx;
353 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
354 size_t sha1_buf_len = 0;
355 ssize_t w;
356 struct got_ratelimit rl;
358 TAILQ_INIT(&symrefs);
359 SHA1Init(&sha1_ctx);
360 got_ratelimit_init(&rl, 0, 500);
362 have = malloc(refsz * sizeof(have[0]));
363 if (have == NULL)
364 return got_error_from_errno("malloc");
365 want = malloc(refsz * sizeof(want[0]));
366 if (want == NULL) {
367 err = got_error_from_errno("malloc");
368 goto done;
370 while (1) {
371 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
372 if (err)
373 goto done;
374 if (n == 0)
375 break;
376 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
377 err = fetch_error(&buf[4], n - 4);
378 goto done;
380 free(id_str);
381 free(refname);
382 err = got_gitproto_parse_refline(&id_str, &refname,
383 &server_capabilities, buf, n);
384 if (err)
385 goto done;
387 if (refsz == nref + 1) {
388 struct got_object_id *h, *w;
390 refsz *= 2;
391 h = reallocarray(have, refsz, sizeof(have[0]));
392 if (h == NULL) {
393 err = got_error_from_errno("reallocarray");
394 goto done;
396 have = h;
397 w = reallocarray(want, refsz, sizeof(want[0]));
398 if (w == NULL) {
399 err = got_error_from_errno("reallocarray");
400 goto done;
402 want = w;
405 if (is_firstpkt) {
406 if (chattygot && server_capabilities[0] != '\0')
407 fprintf(stderr, "%s: server capabilities: %s\n",
408 getprogname(), server_capabilities);
409 err = got_gitproto_match_capabilities(&my_capabilities,
410 &symrefs, server_capabilities,
411 got_capabilities, nitems(got_capabilities));
412 if (err)
413 goto done;
414 if (chattygot)
415 fprintf(stderr, "%s: my capabilities:%s\n",
416 getprogname(), my_capabilities != NULL ?
417 my_capabilities : "");
418 err = send_fetch_symrefs(ibuf, &symrefs);
419 if (err)
420 goto done;
421 is_firstpkt = 0;
422 if (!fetch_all_branches) {
423 TAILQ_FOREACH(pe, &symrefs, entry) {
424 const char *name = pe->path;
425 const char *symref_target = pe->data;
426 if (strcmp(name, GOT_REF_HEAD) != 0)
427 continue;
428 default_branch = symref_target;
429 break;
432 if (default_branch)
433 continue;
435 if (strstr(refname, "^{}")) {
436 if (chattygot) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
440 continue;
442 if (default_branch && default_id_str == NULL &&
443 strcmp(refname, default_branch) == 0) {
444 default_id_str = strdup(id_str);
445 if (default_id_str == NULL) {
446 err = got_error_from_errno("strdup");
447 goto done;
451 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
452 err = fetch_ref(ibuf, have_refs, &have[nref],
453 &want[nref], refname, id_str);
454 if (err)
455 goto done;
456 nref++;
457 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
458 if (fetch_all_branches) {
459 err = fetch_ref(ibuf, have_refs, &have[nref],
460 &want[nref], refname, id_str);
461 if (err)
462 goto done;
463 nref++;
464 found_branch = 1;
465 continue;
467 TAILQ_FOREACH(pe, wanted_branches, entry) {
468 if (match_branch(refname, pe->path))
469 break;
471 if (pe != NULL || (worktree_branch != NULL &&
472 match_branch(refname, worktree_branch))) {
473 err = fetch_ref(ibuf, have_refs, &have[nref],
474 &want[nref], refname, id_str);
475 if (err)
476 goto done;
477 nref++;
478 found_branch = 1;
479 } else if (chattygot) {
480 fprintf(stderr, "%s: ignoring %s\n",
481 getprogname(), refname);
483 } else {
484 TAILQ_FOREACH(pe, wanted_refs, entry) {
485 if (match_wanted_ref(refname, pe->path))
486 break;
488 if (pe != NULL) {
489 err = fetch_ref(ibuf, have_refs, &have[nref],
490 &want[nref], refname, id_str);
491 if (err)
492 goto done;
493 nref++;
494 } else if (chattygot) {
495 fprintf(stderr, "%s: ignoring %s\n",
496 getprogname(), refname);
501 if (list_refs_only)
502 goto done;
504 if (!found_branch && default_branch && default_id_str &&
505 strncmp(default_branch, "refs/heads/", 11) == 0) {
506 err = fetch_ref(ibuf, have_refs, &have[nref],
507 &want[nref], default_branch, default_id_str);
508 if (err)
509 goto done;
510 nref++;
511 found_branch = 1;
514 /* Abort if we haven't found anything to fetch. */
515 if (nref == 0) {
516 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
517 goto done;
520 for (i = 0; i < nref; i++) {
521 if (got_object_id_cmp(&have[i], &want[i]) == 0)
522 continue;
523 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
524 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
525 sent_my_capabilites || my_capabilities == NULL ?
526 "" : my_capabilities);
527 if (n < 0 || (size_t)n >= sizeof(buf)) {
528 err = got_error(GOT_ERR_NO_SPACE);
529 goto done;
531 err = got_pkt_writepkt(fd, buf, n, chattygot);
532 if (err)
533 goto done;
534 sent_my_capabilites = 1;
535 nwant++;
537 err = got_pkt_flushpkt(fd, chattygot);
538 if (err)
539 goto done;
541 if (nwant == 0)
542 goto done;
544 TAILQ_FOREACH(pe, have_refs, entry) {
545 struct got_object_id *id = pe->data;
546 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
547 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
548 if (n < 0 || (size_t)n >= sizeof(buf)) {
549 err = got_error(GOT_ERR_NO_SPACE);
550 goto done;
552 err = got_pkt_writepkt(fd, buf, n, chattygot);
553 if (err)
554 goto done;
555 nhave++;
558 while (nhave > 0 && !acked) {
559 struct got_object_id common_id;
561 /* The server should ACK the object IDs we need. */
562 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
563 if (err)
564 goto done;
565 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
566 err = fetch_error(&buf[4], n - 4);
567 goto done;
569 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
570 /* Server has not located our objects yet. */
571 continue;
573 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
574 strncmp(buf, "ACK ", 4) != 0) {
575 err = got_error_msg(GOT_ERR_BAD_PACKET,
576 "unexpected message from server");
577 goto done;
579 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
580 err = got_error_msg(GOT_ERR_BAD_PACKET,
581 "bad object ID in ACK packet from server");
582 goto done;
584 acked++;
587 n = strlcpy(buf, "done\n", sizeof(buf));
588 err = got_pkt_writepkt(fd, buf, n, chattygot);
589 if (err)
590 goto done;
592 if (nhave == 0) {
593 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
594 if (err)
595 goto done;
596 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
597 err = got_error_msg(GOT_ERR_BAD_PACKET,
598 "unexpected message from server");
599 goto done;
603 if (chattygot)
604 fprintf(stderr, "%s: fetching...\n", getprogname());
606 if (my_capabilities != NULL &&
607 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
608 have_sidebands = 1;
610 while (1) {
611 ssize_t r = 0;
612 int datalen = -1;
614 if (have_sidebands) {
615 err = got_pkt_readhdr(&datalen, fd, chattygot);
616 if (err)
617 goto done;
618 if (datalen <= 0)
619 break;
621 /* Read sideband channel ID (one byte). */
622 r = read(fd, buf, 1);
623 if (r == -1) {
624 err = got_error_from_errno("read");
625 goto done;
627 if (r != 1) {
628 err = got_error_msg(GOT_ERR_BAD_PACKET,
629 "short packet");
630 goto done;
632 if (datalen > sizeof(buf) - 5) {
633 err = got_error_msg(GOT_ERR_BAD_PACKET,
634 "bad packet length");
635 goto done;
637 datalen--; /* sideband ID has been read */
638 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
639 /* Read packfile data. */
640 err = got_pkt_readn(&r, fd, buf, datalen);
641 if (err)
642 goto done;
643 if (r != datalen) {
644 err = got_error_msg(GOT_ERR_BAD_PACKET,
645 "packet too short");
646 goto done;
648 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
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 err = fetch_progress(ibuf, buf, r);
658 if (err)
659 goto done;
660 continue;
661 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
662 err = got_pkt_readn(&r, fd, buf, datalen);
663 if (err)
664 goto done;
665 if (r != datalen) {
666 err = got_error_msg(GOT_ERR_BAD_PACKET,
667 "packet too short");
668 goto done;
670 err = fetch_error(buf, r);
671 goto done;
672 } else if (buf[0] == 'A') {
673 err = got_pkt_readn(&r, fd, buf, datalen);
674 if (err)
675 goto done;
676 if (r != datalen) {
677 err = got_error_msg(GOT_ERR_BAD_PACKET,
678 "packet too short");
679 goto done;
681 /*
682 * Git server responds with ACK after 'done'
683 * even though multi_ack is disabled?!?
684 */
685 buf[r] = '\0';
686 if (strncmp(buf, "CK ", 3) == 0)
687 continue; /* ignore */
688 err = got_error_msg(GOT_ERR_BAD_PACKET,
689 "unexpected message from server");
690 goto done;
691 } else {
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
693 "unknown side-band received from server");
694 goto done;
696 } else {
697 /* No sideband channel. Every byte is packfile data. */
698 err = got_pkt_readn(&r, fd, buf, sizeof buf);
699 if (err)
700 goto done;
701 if (r <= 0)
702 break;
705 /*
706 * An expected SHA1 checksum sits at the end of the pack file.
707 * Since we don't know the file size ahead of time we have to
708 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
709 * those bytes into our SHA1 checksum computation until we
710 * know for sure that additional pack file data bytes follow.
712 * We can assume r > 0 since otherwise the loop would exit.
713 */
714 if (r < SHA1_DIGEST_LENGTH) {
715 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
716 /*
717 * If there's enough buffered + read data to
718 * fill up the buffer then shift a sufficient
719 * amount of bytes out at the front to make
720 * room, mixing those bytes into the checksum.
721 */
722 if (sha1_buf_len > 0 &&
723 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
724 size_t nshift = MIN(sha1_buf_len + r -
725 SHA1_DIGEST_LENGTH, sha1_buf_len);
726 SHA1Update(&sha1_ctx, sha1_buf, nshift);
727 memmove(sha1_buf, sha1_buf + nshift,
728 sha1_buf_len - nshift);
729 sha1_buf_len -= nshift;
732 /* Buffer potential checksum bytes. */
733 memcpy(sha1_buf + sha1_buf_len, buf, r);
734 sha1_buf_len += r;
735 } else {
736 /*
737 * Mix in previously buffered bytes which
738 * are not part of the checksum after all.
739 */
740 SHA1Update(&sha1_ctx, sha1_buf, r);
742 /* Update potential checksum buffer. */
743 memmove(sha1_buf, sha1_buf + r,
744 sha1_buf_len - r);
745 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
747 } else {
748 /* Mix in any previously buffered bytes. */
749 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
751 /* Mix in bytes read minus potential checksum bytes. */
752 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
754 /* Buffer potential checksum bytes. */
755 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
756 SHA1_DIGEST_LENGTH);
757 sha1_buf_len = SHA1_DIGEST_LENGTH;
760 /* Write packfile data to temporary pack file. */
761 w = write(packfd, buf, r);
762 if (w == -1) {
763 err = got_error_from_errno("write");
764 goto done;
766 if (w != r) {
767 err = got_error(GOT_ERR_IO);
768 goto done;
770 packsz += w;
772 /* Don't send too many progress privsep messages. */
773 if (packsz > last_reported_packsz + 1024) {
774 err = send_fetch_download_progress(ibuf, packsz, &rl);
775 if (err)
776 goto done;
777 last_reported_packsz = packsz;
780 err = send_fetch_download_progress(ibuf, packsz, NULL);
781 if (err)
782 goto done;
784 SHA1Final(pack_sha1, &sha1_ctx);
785 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
786 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
787 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
788 "pack file checksum mismatch");
790 done:
791 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
792 free(have);
793 free(want);
794 free(id_str);
795 free(default_id_str);
796 free(refname);
797 free(server_capabilities);
798 return err;
802 int
803 main(int argc, char **argv)
805 const struct got_error *err = NULL;
806 int fetchfd = -1, packfd = -1;
807 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
808 struct imsgbuf ibuf;
809 struct imsg imsg;
810 struct got_pathlist_head have_refs;
811 struct got_pathlist_head wanted_branches;
812 struct got_pathlist_head wanted_refs;
813 struct got_imsg_fetch_request fetch_req;
814 struct got_imsg_fetch_have_ref href;
815 struct got_imsg_fetch_wanted_branch wbranch;
816 struct got_imsg_fetch_wanted_ref wref;
817 size_t datalen, i;
818 char *worktree_branch = NULL;
819 #if 0
820 static int attached;
821 while (!attached)
822 sleep (1);
823 #endif
825 TAILQ_INIT(&have_refs);
826 TAILQ_INIT(&wanted_branches);
827 TAILQ_INIT(&wanted_refs);
829 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
830 #ifndef PROFILE
831 /* revoke access to most system calls */
832 if (pledge("stdio recvfd", NULL) == -1) {
833 err = got_error_from_errno("pledge");
834 got_privsep_send_error(&ibuf, err);
835 return 1;
837 #endif
838 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
839 if (err) {
840 if (err->code == GOT_ERR_PRIVSEP_PIPE)
841 err = NULL;
842 goto done;
844 if (imsg.hdr.type == GOT_IMSG_STOP)
845 goto done;
846 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
847 err = got_error(GOT_ERR_PRIVSEP_MSG);
848 goto done;
850 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
851 if (datalen < sizeof(fetch_req)) {
852 err = got_error(GOT_ERR_PRIVSEP_LEN);
853 goto done;
855 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
856 fetchfd = imsg.fd;
858 if (datalen != sizeof(fetch_req) +
859 fetch_req.worktree_branch_len) {
860 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 goto done;
864 if (fetch_req.worktree_branch_len != 0) {
865 worktree_branch = strndup(imsg.data +
866 sizeof(fetch_req), fetch_req.worktree_branch_len);
867 if (worktree_branch == NULL) {
868 err = got_error_from_errno("strndup");
869 goto done;
873 imsg_free(&imsg);
875 if (fetch_req.verbosity > 0)
876 chattygot += fetch_req.verbosity;
878 for (i = 0; i < fetch_req.n_have_refs; i++) {
879 struct got_object_id *id;
880 char *refname;
882 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
883 if (err) {
884 if (err->code == GOT_ERR_PRIVSEP_PIPE)
885 err = NULL;
886 goto done;
888 if (imsg.hdr.type == GOT_IMSG_STOP)
889 goto done;
890 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
891 err = got_error(GOT_ERR_PRIVSEP_MSG);
892 goto done;
894 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
895 if (datalen < sizeof(href)) {
896 err = got_error(GOT_ERR_PRIVSEP_LEN);
897 goto done;
899 memcpy(&href, imsg.data, sizeof(href));
900 if (datalen - sizeof(href) < href.name_len) {
901 err = got_error(GOT_ERR_PRIVSEP_LEN);
902 goto done;
904 refname = strndup(imsg.data + sizeof(href), href.name_len);
905 if (refname == NULL) {
906 err = got_error_from_errno("strndup");
907 goto done;
910 id = malloc(sizeof(*id));
911 if (id == NULL) {
912 free(refname);
913 err = got_error_from_errno("malloc");
914 goto done;
916 memcpy(id, &href.id, sizeof(*id));
917 err = got_pathlist_append(&have_refs, refname, id);
918 if (err) {
919 free(refname);
920 free(id);
921 goto done;
924 imsg_free(&imsg);
927 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
928 char *refname;
930 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
931 if (err) {
932 if (err->code == GOT_ERR_PRIVSEP_PIPE)
933 err = NULL;
934 goto done;
936 if (imsg.hdr.type == GOT_IMSG_STOP)
937 goto done;
938 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
940 goto done;
942 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
943 if (datalen < sizeof(wbranch)) {
944 err = got_error(GOT_ERR_PRIVSEP_LEN);
945 goto done;
947 memcpy(&wbranch, imsg.data, sizeof(wbranch));
948 if (datalen - sizeof(wbranch) < wbranch.name_len) {
949 err = got_error(GOT_ERR_PRIVSEP_LEN);
950 goto done;
952 refname = strndup(imsg.data + sizeof(wbranch),
953 wbranch.name_len);
954 if (refname == NULL) {
955 err = got_error_from_errno("strndup");
956 goto done;
959 err = got_pathlist_append(&wanted_branches, refname, NULL);
960 if (err) {
961 free(refname);
962 goto done;
965 imsg_free(&imsg);
968 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
969 char *refname;
971 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
972 if (err) {
973 if (err->code == GOT_ERR_PRIVSEP_PIPE)
974 err = NULL;
975 goto done;
977 if (imsg.hdr.type == GOT_IMSG_STOP)
978 goto done;
979 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
980 err = got_error(GOT_ERR_PRIVSEP_MSG);
981 goto done;
983 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
984 if (datalen < sizeof(wref)) {
985 err = got_error(GOT_ERR_PRIVSEP_LEN);
986 goto done;
988 memcpy(&wref, imsg.data, sizeof(wref));
989 if (datalen - sizeof(wref) < wref.name_len) {
990 err = got_error(GOT_ERR_PRIVSEP_LEN);
991 goto done;
993 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
994 if (refname == NULL) {
995 err = got_error_from_errno("strndup");
996 goto done;
999 err = got_pathlist_append(&wanted_refs, refname, NULL);
1000 if (err) {
1001 free(refname);
1002 goto done;
1005 imsg_free(&imsg);
1008 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1009 if (err) {
1010 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1011 err = NULL;
1012 goto done;
1014 if (imsg.hdr.type == GOT_IMSG_STOP)
1015 goto done;
1016 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1017 err = got_error(GOT_ERR_PRIVSEP_MSG);
1018 goto done;
1020 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1021 err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 goto done;
1024 packfd = imsg.fd;
1026 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1027 fetch_req.fetch_all_branches, &wanted_branches,
1028 &wanted_refs, fetch_req.list_refs_only,
1029 worktree_branch, &ibuf);
1030 done:
1031 free(worktree_branch);
1032 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1033 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1034 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1035 err = got_error_from_errno("close");
1036 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1037 err = got_error_from_errno("close");
1038 if (err != NULL)
1039 got_privsep_send_error(&ibuf, err);
1040 else
1041 err = send_fetch_done(&ibuf, pack_sha1);
1042 if (err != NULL) {
1043 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1044 got_privsep_send_error(&ibuf, err);
1047 exit(0);