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 <poll.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <err.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_version.h"
44 #include "got_fetch.h"
45 #include "got_reference.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_pkt.h"
54 #include "got_lib_gitproto.h"
55 #include "got_lib_ratelimit.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct got_object *indexed;
66 static int chattygot;
68 static const struct got_capability got_capabilities[] = {
69 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
70 { GOT_CAPA_OFS_DELTA, NULL },
71 { GOT_CAPA_SIDE_BAND_64K, NULL },
72 };
74 static void
75 match_remote_ref(struct got_pathlist_head *have_refs,
76 struct got_object_id *my_id, const char *refname)
77 {
78 struct got_pathlist_entry *pe;
80 /* XXX zero-hash signifies we don't have this ref;
81 * we should use a flag instead */
82 memset(my_id, 0, sizeof(*my_id));
84 TAILQ_FOREACH(pe, have_refs, entry) {
85 struct got_object_id *id = pe->data;
86 if (strcmp(pe->path, refname) == 0) {
87 memcpy(my_id, id, sizeof(*my_id));
88 break;
89 }
90 }
91 }
93 static int
94 match_branch(const char *branch, const char *wanted_branch)
95 {
96 if (strncmp(branch, "refs/heads/", 11) != 0)
97 return 0;
99 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
100 wanted_branch += 11;
102 return (strcmp(branch + 11, wanted_branch) == 0);
105 static int
106 match_wanted_ref(const char *refname, const char *wanted_ref)
108 if (strncmp(refname, "refs/", 5) != 0)
109 return 0;
110 refname += 5;
112 /*
113 * Prevent fetching of references that won't make any
114 * sense outside of the remote repository's context.
115 */
116 if (strncmp(refname, "got/", 4) == 0)
117 return 0;
118 if (strncmp(refname, "remotes/", 8) == 0)
119 return 0;
121 if (strncmp(wanted_ref, "refs/", 5) == 0)
122 wanted_ref += 5;
124 /* Allow prefix match. */
125 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
126 return 1;
128 /* Allow exact match. */
129 return (strcmp(refname, wanted_ref) == 0);
132 static const struct got_error *
133 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
135 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
136 return got_error(GOT_ERR_NO_SPACE);
138 if (msglen == 0)
139 return NULL;
141 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
142 msg, msglen) == -1)
143 return got_error_from_errno(
144 "imsg_compose FETCH_SERVER_PROGRESS");
146 return got_privsep_flush_imsg(ibuf);
149 static const struct got_error *
150 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
151 struct got_ratelimit *rl)
153 const struct got_error *err;
154 int elapsed = 0;
156 if (rl) {
157 err = got_ratelimit_check(&elapsed, rl);
158 if (err || !elapsed)
159 return err;
162 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
163 &bytes, sizeof(bytes)) == -1)
164 return got_error_from_errno(
165 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
167 return got_privsep_flush_imsg(ibuf);
170 static const struct got_error *
171 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
173 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
174 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
175 return got_error_from_errno("imsg_compose FETCH");
176 return got_privsep_flush_imsg(ibuf);
179 static const struct got_error *
180 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
182 size_t i;
184 if (len == 0)
185 return NULL;
187 /*
188 * Truncate messages which exceed the maximum imsg payload size.
189 * Server may send up to 64k.
190 */
191 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
192 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
194 /* Only allow printable ASCII. */
195 for (i = 0; i < len; i++) {
196 if (isprint((unsigned char)buf[i]) ||
197 isspace((unsigned char)buf[i]))
198 continue;
199 return got_error_msg(GOT_ERR_BAD_PACKET,
200 "non-printable progress message received from server");
203 return send_fetch_server_progress(ibuf, buf, len);
206 static const struct got_error *
207 fetch_error(const char *buf, size_t len)
209 static char msg[1024];
210 size_t i;
212 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
213 if (!isprint((unsigned char)buf[i]))
214 return got_error_msg(GOT_ERR_BAD_PACKET,
215 "non-printable error message received from server");
216 msg[i] = buf[i];
218 msg[i] = '\0';
219 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
222 static const struct got_error *
223 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
225 struct ibuf *wbuf;
226 size_t len, nsymrefs = 0;
227 struct got_pathlist_entry *pe;
229 len = sizeof(struct got_imsg_fetch_symrefs);
230 TAILQ_FOREACH(pe, symrefs, entry) {
231 const char *target = pe->data;
232 len += sizeof(struct got_imsg_fetch_symref) +
233 pe->path_len + strlen(target);
234 nsymrefs++;
237 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
238 return got_error(GOT_ERR_NO_SPACE);
240 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
241 if (wbuf == NULL)
242 return got_error_from_errno("imsg_create FETCH_SYMREFS");
244 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
245 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
246 return got_error_from_errno("imsg_add FETCH_SYMREFS");
248 TAILQ_FOREACH(pe, symrefs, entry) {
249 const char *name = pe->path;
250 size_t name_len = pe->path_len;
251 const char *target = pe->data;
252 size_t target_len = strlen(target);
254 /* Keep in sync with struct got_imsg_fetch_symref definition! */
255 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
256 return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
258 return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 if (imsg_add(wbuf, name, name_len) == -1)
260 return got_error_from_errno("imsg_add FETCH_SYMREFS");
261 if (imsg_add(wbuf, target, target_len) == -1)
262 return got_error_from_errno("imsg_add FETCH_SYMREFS");
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 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_object_id(want, id_str, GOT_HASH_SHA1)) {
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, const char *remote_head,
337 int no_head, struct imsgbuf *ibuf)
339 const struct got_error *err = NULL;
340 char buf[GOT_PKT_MAX];
341 char hashstr[SHA1_DIGEST_STRING_LENGTH];
342 struct got_object_id *have, *want;
343 int is_firstpkt = 1, nref = 0, refsz = 16;
344 int i, n, nwant = 0, nhave = 0, acked = 0;
345 off_t packsz = 0, last_reported_packsz = 0;
346 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
347 char *server_capabilities = NULL, *my_capabilities = NULL;
348 const char *default_branch = NULL;
349 struct got_pathlist_head symrefs;
350 struct got_pathlist_entry *pe;
351 int sent_my_capabilites = 0, have_sidebands = 0;
352 int found_branch = 0;
353 struct got_hash ctx;
354 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
355 size_t sha1_buf_len = 0;
356 ssize_t w;
357 struct got_ratelimit rl;
359 TAILQ_INIT(&symrefs);
360 got_hash_init(&ctx, GOT_HASH_SHA1);
361 got_ratelimit_init(&rl, 0, 500);
363 have = malloc(refsz * sizeof(have[0]));
364 if (have == NULL)
365 return got_error_from_errno("malloc");
366 want = malloc(refsz * sizeof(want[0]));
367 if (want == NULL) {
368 err = got_error_from_errno("malloc");
369 goto done;
371 while (1) {
372 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
373 INFTIM);
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 /*
507 * If -b was not used and either none of the requested branches
508 * (got.conf, worktree) were found or the client already has the
509 * remote HEAD symref but its target changed, fetch remote's HEAD.
510 */
511 if (!no_head && default_branch && default_id_str &&
512 strncmp(default_branch, "refs/heads/", 11) == 0) {
513 int remote_head_changed = 0;
515 if (remote_head) {
516 if (strcmp(remote_head, default_branch + 11) != 0)
517 remote_head_changed = 1;
520 if (!found_branch || remote_head_changed) {
521 err = fetch_ref(ibuf, have_refs, &have[nref],
522 &want[nref], default_branch, default_id_str);
523 if (err)
524 goto done;
525 nref++;
529 /* Abort if we haven't found anything to fetch. */
530 if (nref == 0) {
531 struct got_pathlist_entry *pe;
532 static char msg[PATH_MAX + 33];
534 pe = TAILQ_FIRST(wanted_branches);
535 if (pe) {
536 snprintf(msg, sizeof(msg),
537 "branch \"%s\" not found on server", pe->path);
538 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
539 goto done;
542 pe = TAILQ_FIRST(wanted_refs);
543 if (pe) {
544 snprintf(msg, sizeof(msg),
545 "reference \"%s\" not found on server", pe->path);
546 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
547 goto done;
550 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
551 goto done;
554 for (i = 0; i < nref; i++) {
555 if (got_object_id_cmp(&have[i], &want[i]) == 0)
556 continue;
557 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
558 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
559 sent_my_capabilites || my_capabilities == NULL ?
560 "" : my_capabilities);
561 if (n < 0 || (size_t)n >= sizeof(buf)) {
562 err = got_error(GOT_ERR_NO_SPACE);
563 goto done;
565 err = got_pkt_writepkt(fd, buf, n, chattygot);
566 if (err)
567 goto done;
568 sent_my_capabilites = 1;
569 nwant++;
571 err = got_pkt_flushpkt(fd, chattygot);
572 if (err)
573 goto done;
575 if (nwant == 0)
576 goto done;
578 TAILQ_FOREACH(pe, have_refs, entry) {
579 struct got_object_id *id = pe->data;
580 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
581 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
582 if (n < 0 || (size_t)n >= sizeof(buf)) {
583 err = got_error(GOT_ERR_NO_SPACE);
584 goto done;
586 err = got_pkt_writepkt(fd, buf, n, chattygot);
587 if (err)
588 goto done;
589 nhave++;
592 n = strlcpy(buf, "done\n", sizeof(buf));
593 err = got_pkt_writepkt(fd, buf, n, chattygot);
594 if (err)
595 goto done;
597 while (nhave > 0 && !acked) {
598 struct got_object_id common_id;
600 /* The server should ACK the object IDs we need. */
601 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
602 INFTIM);
603 if (err)
604 goto done;
605 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
606 err = fetch_error(&buf[4], n - 4);
607 goto done;
609 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
610 /*
611 * Server could not find a common ancestor.
612 * Perhaps it is an out-of-date mirror, or there
613 * is a repository with unrelated history.
614 */
615 break;
617 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
618 strncmp(buf, "ACK ", 4) != 0) {
619 err = got_error_msg(GOT_ERR_BAD_PACKET,
620 "unexpected message from server");
621 goto done;
623 if (!got_parse_object_id(&common_id, buf + 4,
624 GOT_HASH_SHA1)) {
625 err = got_error_msg(GOT_ERR_BAD_PACKET,
626 "bad object ID in ACK packet from server");
627 goto done;
629 acked++;
632 if (nhave == 0) {
633 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
634 INFTIM);
635 if (err)
636 goto done;
637 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
638 err = got_error_msg(GOT_ERR_BAD_PACKET,
639 "unexpected message from server");
640 goto done;
644 if (chattygot)
645 fprintf(stderr, "%s: fetching...\n", getprogname());
647 if (my_capabilities != NULL &&
648 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
649 have_sidebands = 1;
651 while (1) {
652 ssize_t r = 0;
653 int datalen = -1;
655 if (have_sidebands) {
656 err = got_pkt_readhdr(&datalen, fd, chattygot, INFTIM);
657 if (err)
658 goto done;
659 if (datalen <= 0)
660 break;
662 /* Read sideband channel ID (one byte). */
663 r = read(fd, buf, 1);
664 if (r == -1) {
665 err = got_error_from_errno("read");
666 goto done;
668 if (r != 1) {
669 err = got_error_msg(GOT_ERR_BAD_PACKET,
670 "short packet");
671 goto done;
673 if (datalen > sizeof(buf) - 5) {
674 err = got_error_msg(GOT_ERR_BAD_PACKET,
675 "bad packet length");
676 goto done;
678 datalen--; /* sideband ID has been read */
679 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
680 /* Read packfile data. */
681 err = got_pkt_readn(&r, fd, buf, datalen,
682 INFTIM);
683 if (err)
684 goto done;
685 if (r != datalen) {
686 err = got_error_msg(GOT_ERR_BAD_PACKET,
687 "packet too short");
688 goto done;
690 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
691 err = got_pkt_readn(&r, fd, buf, datalen,
692 INFTIM);
693 if (err)
694 goto done;
695 if (r != datalen) {
696 err = got_error_msg(GOT_ERR_BAD_PACKET,
697 "packet too short");
698 goto done;
700 err = fetch_progress(ibuf, buf, r);
701 if (err)
702 goto done;
703 continue;
704 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
705 err = got_pkt_readn(&r, fd, buf, datalen,
706 INFTIM);
707 if (err)
708 goto done;
709 if (r != datalen) {
710 err = got_error_msg(GOT_ERR_BAD_PACKET,
711 "packet too short");
712 goto done;
714 err = fetch_error(buf, r);
715 goto done;
716 } else if (buf[0] == 'A') {
717 err = got_pkt_readn(&r, fd, buf, datalen,
718 INFTIM);
719 if (err)
720 goto done;
721 if (r != datalen) {
722 err = got_error_msg(GOT_ERR_BAD_PACKET,
723 "packet too short");
724 goto done;
726 /*
727 * Git server responds with ACK after 'done'
728 * even though multi_ack is disabled?!?
729 */
730 buf[r] = '\0';
731 if (strncmp(buf, "CK ", 3) == 0)
732 continue; /* ignore */
733 err = got_error_msg(GOT_ERR_BAD_PACKET,
734 "unexpected message from server");
735 goto done;
736 } else {
737 err = got_error_msg(GOT_ERR_BAD_PACKET,
738 "unknown side-band received from server");
739 goto done;
741 } else {
742 /* No sideband channel. Every byte is packfile data. */
743 err = got_pkt_readn(&r, fd, buf, sizeof buf, INFTIM);
744 if (err)
745 goto done;
746 if (r <= 0)
747 break;
750 /*
751 * An expected SHA1 checksum sits at the end of the pack file.
752 * Since we don't know the file size ahead of time we have to
753 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
754 * those bytes into our SHA1 checksum computation until we
755 * know for sure that additional pack file data bytes follow.
757 * We can assume r > 0 since otherwise the loop would exit.
758 */
759 if (r < SHA1_DIGEST_LENGTH) {
760 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
761 /*
762 * If there's enough buffered + read data to
763 * fill up the buffer then shift a sufficient
764 * amount of bytes out at the front to make
765 * room, mixing those bytes into the checksum.
766 */
767 if (sha1_buf_len > 0 &&
768 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
769 size_t nshift = MIN(sha1_buf_len + r -
770 SHA1_DIGEST_LENGTH, sha1_buf_len);
771 got_hash_update(&ctx, sha1_buf,
772 nshift);
773 memmove(sha1_buf, sha1_buf + nshift,
774 sha1_buf_len - nshift);
775 sha1_buf_len -= nshift;
778 /* Buffer potential checksum bytes. */
779 memcpy(sha1_buf + sha1_buf_len, buf, r);
780 sha1_buf_len += r;
781 } else {
782 /*
783 * Mix in previously buffered bytes which
784 * are not part of the checksum after all.
785 */
786 got_hash_update(&ctx, sha1_buf, r);
788 /* Update potential checksum buffer. */
789 memmove(sha1_buf, sha1_buf + r,
790 sha1_buf_len - r);
791 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
793 } else {
794 /* Mix in any previously buffered bytes. */
795 got_hash_update(&ctx, sha1_buf, sha1_buf_len);
797 /* Mix in bytes read minus potential checksum bytes. */
798 got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
800 /* Buffer potential checksum bytes. */
801 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
802 SHA1_DIGEST_LENGTH);
803 sha1_buf_len = SHA1_DIGEST_LENGTH;
806 /* Write packfile data to temporary pack file. */
807 w = write(packfd, buf, r);
808 if (w == -1) {
809 err = got_error_from_errno("write");
810 goto done;
812 if (w != r) {
813 err = got_error(GOT_ERR_IO);
814 goto done;
816 packsz += w;
818 /* Don't send too many progress privsep messages. */
819 if (packsz > last_reported_packsz + 1024) {
820 err = send_fetch_download_progress(ibuf, packsz, &rl);
821 if (err)
822 goto done;
823 last_reported_packsz = packsz;
826 err = send_fetch_download_progress(ibuf, packsz, NULL);
827 if (err)
828 goto done;
830 got_hash_final(&ctx, pack_sha1);
831 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
832 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
833 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
834 "pack file checksum mismatch");
836 done:
837 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
838 free(have);
839 free(want);
840 free(id_str);
841 free(default_id_str);
842 free(refname);
843 free(server_capabilities);
844 return err;
848 int
849 main(int argc, char **argv)
851 const struct got_error *err = NULL;
852 int fetchfd = -1, packfd = -1;
853 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
854 struct imsgbuf ibuf;
855 struct imsg imsg;
856 struct got_pathlist_head have_refs;
857 struct got_pathlist_head wanted_branches;
858 struct got_pathlist_head wanted_refs;
859 struct got_imsg_fetch_request fetch_req;
860 struct got_imsg_fetch_have_ref href;
861 struct got_imsg_fetch_wanted_branch wbranch;
862 struct got_imsg_fetch_wanted_ref wref;
863 size_t datalen, i;
864 char *remote_head = NULL, *worktree_branch = NULL;
865 #if 0
866 static int attached;
867 while (!attached)
868 sleep (1);
869 #endif
871 TAILQ_INIT(&have_refs);
872 TAILQ_INIT(&wanted_branches);
873 TAILQ_INIT(&wanted_refs);
875 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
876 #ifndef PROFILE
877 /* revoke access to most system calls */
878 if (pledge("stdio recvfd", NULL) == -1) {
879 err = got_error_from_errno("pledge");
880 got_privsep_send_error(&ibuf, err);
881 return 1;
883 #endif
884 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
885 if (err) {
886 if (err->code == GOT_ERR_PRIVSEP_PIPE)
887 err = NULL;
888 goto done;
890 if (imsg.hdr.type == GOT_IMSG_STOP)
891 goto done;
892 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
893 err = got_error(GOT_ERR_PRIVSEP_MSG);
894 goto done;
896 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
897 if (datalen < sizeof(fetch_req)) {
898 err = got_error(GOT_ERR_PRIVSEP_LEN);
899 goto done;
901 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
902 fetchfd = imsg_get_fd(&imsg);
904 if (datalen != sizeof(fetch_req) +
905 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
906 err = got_error(GOT_ERR_PRIVSEP_LEN);
907 goto done;
910 if (fetch_req.worktree_branch_len != 0) {
911 worktree_branch = strndup(imsg.data +
912 sizeof(fetch_req), fetch_req.worktree_branch_len);
913 if (worktree_branch == NULL) {
914 err = got_error_from_errno("strndup");
915 goto done;
919 if (fetch_req.remote_head_len != 0) {
920 remote_head = strndup(imsg.data + sizeof(fetch_req) +
921 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
922 if (remote_head == NULL) {
923 err = got_error_from_errno("strndup");
924 goto done;
928 imsg_free(&imsg);
930 if (fetch_req.verbosity > 0)
931 chattygot += fetch_req.verbosity;
933 for (i = 0; i < fetch_req.n_have_refs; i++) {
934 struct got_object_id *id;
935 char *refname;
937 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
938 if (err) {
939 if (err->code == GOT_ERR_PRIVSEP_PIPE)
940 err = NULL;
941 goto done;
943 if (imsg.hdr.type == GOT_IMSG_STOP)
944 goto done;
945 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
946 err = got_error(GOT_ERR_PRIVSEP_MSG);
947 goto done;
949 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
950 if (datalen < sizeof(href)) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 goto done;
954 memcpy(&href, imsg.data, sizeof(href));
955 if (datalen - sizeof(href) < href.name_len) {
956 err = got_error(GOT_ERR_PRIVSEP_LEN);
957 goto done;
959 refname = strndup(imsg.data + sizeof(href), href.name_len);
960 if (refname == NULL) {
961 err = got_error_from_errno("strndup");
962 goto done;
965 id = malloc(sizeof(*id));
966 if (id == NULL) {
967 free(refname);
968 err = got_error_from_errno("malloc");
969 goto done;
971 memcpy(id, &href.id, sizeof(*id));
972 err = got_pathlist_append(&have_refs, refname, id);
973 if (err) {
974 free(refname);
975 free(id);
976 goto done;
979 imsg_free(&imsg);
982 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
983 char *refname;
985 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
986 if (err) {
987 if (err->code == GOT_ERR_PRIVSEP_PIPE)
988 err = NULL;
989 goto done;
991 if (imsg.hdr.type == GOT_IMSG_STOP)
992 goto done;
993 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
994 err = got_error(GOT_ERR_PRIVSEP_MSG);
995 goto done;
997 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
998 if (datalen < sizeof(wbranch)) {
999 err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 goto done;
1002 memcpy(&wbranch, imsg.data, sizeof(wbranch));
1003 if (datalen - sizeof(wbranch) < wbranch.name_len) {
1004 err = got_error(GOT_ERR_PRIVSEP_LEN);
1005 goto done;
1007 refname = strndup(imsg.data + sizeof(wbranch),
1008 wbranch.name_len);
1009 if (refname == NULL) {
1010 err = got_error_from_errno("strndup");
1011 goto done;
1014 err = got_pathlist_append(&wanted_branches, refname, NULL);
1015 if (err) {
1016 free(refname);
1017 goto done;
1020 imsg_free(&imsg);
1023 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1024 char *refname;
1026 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1027 if (err) {
1028 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1029 err = NULL;
1030 goto done;
1032 if (imsg.hdr.type == GOT_IMSG_STOP)
1033 goto done;
1034 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1035 err = got_error(GOT_ERR_PRIVSEP_MSG);
1036 goto done;
1038 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1039 if (datalen < sizeof(wref)) {
1040 err = got_error(GOT_ERR_PRIVSEP_LEN);
1041 goto done;
1043 memcpy(&wref, imsg.data, sizeof(wref));
1044 if (datalen - sizeof(wref) < wref.name_len) {
1045 err = got_error(GOT_ERR_PRIVSEP_LEN);
1046 goto done;
1048 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1049 if (refname == NULL) {
1050 err = got_error_from_errno("strndup");
1051 goto done;
1054 err = got_pathlist_append(&wanted_refs, refname, NULL);
1055 if (err) {
1056 free(refname);
1057 goto done;
1060 imsg_free(&imsg);
1063 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1064 if (err) {
1065 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1066 err = NULL;
1067 goto done;
1069 if (imsg.hdr.type == GOT_IMSG_STOP)
1070 goto done;
1071 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1072 err = got_error(GOT_ERR_PRIVSEP_MSG);
1073 goto done;
1075 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1076 err = got_error(GOT_ERR_PRIVSEP_LEN);
1077 goto done;
1079 packfd = imsg_get_fd(&imsg);
1081 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1082 fetch_req.fetch_all_branches, &wanted_branches,
1083 &wanted_refs, fetch_req.list_refs_only,
1084 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1085 done:
1086 free(worktree_branch);
1087 free(remote_head);
1088 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1089 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1090 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1091 err = got_error_from_errno("close");
1092 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1093 err = got_error_from_errno("close");
1094 if (err != NULL)
1095 got_privsep_send_error(&ibuf, err);
1096 else
1097 err = send_fetch_done(&ibuf, pack_sha1);
1098 if (err != NULL) {
1099 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1100 got_privsep_send_error(&ibuf, err);
1103 exit(0);