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 if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
304 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
305 goto done;
308 match_remote_ref(have_refs, have, refname);
309 err = send_fetch_ref(ibuf, want, refname);
310 if (err)
311 goto done;
313 if (chattygot)
314 fprintf(stderr, "%s: %s will be fetched\n",
315 getprogname(), refname);
316 if (chattygot > 1) {
317 err = got_object_id_str(&theirs, want);
318 if (err)
319 goto done;
320 err = got_object_id_str(&mine, have);
321 if (err)
322 goto done;
323 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
324 getprogname(), theirs, getprogname(), mine);
326 done:
327 free(theirs);
328 free(mine);
329 return err;
332 static const struct got_error *
333 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
334 struct got_pathlist_head *have_refs, int fetch_all_branches,
335 struct got_pathlist_head *wanted_branches,
336 struct got_pathlist_head *wanted_refs, int list_refs_only,
337 const char *worktree_branch, const char *remote_head,
338 int no_head, 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 /*
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 if (err)
603 goto done;
604 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
605 err = fetch_error(&buf[4], n - 4);
606 goto done;
608 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
609 /*
610 * Server could not find a common ancestor.
611 * Perhaps it is an out-of-date mirror, or there
612 * is a repository with unrelated history.
613 */
614 break;
616 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
617 strncmp(buf, "ACK ", 4) != 0) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "unexpected message from server");
620 goto done;
622 if (!got_parse_object_id(&common_id, buf + 4,
623 GOT_HASH_SHA1)) {
624 err = got_error_msg(GOT_ERR_BAD_PACKET,
625 "bad object ID in ACK packet from server");
626 goto done;
628 acked++;
631 if (nhave == 0) {
632 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
633 if (err)
634 goto done;
635 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
636 err = got_error_msg(GOT_ERR_BAD_PACKET,
637 "unexpected message from server");
638 goto done;
642 if (chattygot)
643 fprintf(stderr, "%s: fetching...\n", getprogname());
645 if (my_capabilities != NULL &&
646 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
647 have_sidebands = 1;
649 while (1) {
650 ssize_t r = 0;
651 int datalen = -1;
653 if (have_sidebands) {
654 err = got_pkt_readhdr(&datalen, fd, chattygot);
655 if (err)
656 goto done;
657 if (datalen <= 0)
658 break;
660 /* Read sideband channel ID (one byte). */
661 r = read(fd, buf, 1);
662 if (r == -1) {
663 err = got_error_from_errno("read");
664 goto done;
666 if (r != 1) {
667 err = got_error_msg(GOT_ERR_BAD_PACKET,
668 "short packet");
669 goto done;
671 if (datalen > sizeof(buf) - 5) {
672 err = got_error_msg(GOT_ERR_BAD_PACKET,
673 "bad packet length");
674 goto done;
676 datalen--; /* sideband ID has been read */
677 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
678 /* Read packfile data. */
679 err = got_pkt_readn(&r, fd, buf, datalen);
680 if (err)
681 goto done;
682 if (r != datalen) {
683 err = got_error_msg(GOT_ERR_BAD_PACKET,
684 "packet too short");
685 goto done;
687 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
688 err = got_pkt_readn(&r, fd, buf, datalen);
689 if (err)
690 goto done;
691 if (r != datalen) {
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
693 "packet too short");
694 goto done;
696 err = fetch_progress(ibuf, buf, r);
697 if (err)
698 goto done;
699 continue;
700 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
701 err = got_pkt_readn(&r, fd, buf, datalen);
702 if (err)
703 goto done;
704 if (r != datalen) {
705 err = got_error_msg(GOT_ERR_BAD_PACKET,
706 "packet too short");
707 goto done;
709 err = fetch_error(buf, r);
710 goto done;
711 } else if (buf[0] == 'A') {
712 err = got_pkt_readn(&r, fd, buf, datalen);
713 if (err)
714 goto done;
715 if (r != datalen) {
716 err = got_error_msg(GOT_ERR_BAD_PACKET,
717 "packet too short");
718 goto done;
720 /*
721 * Git server responds with ACK after 'done'
722 * even though multi_ack is disabled?!?
723 */
724 buf[r] = '\0';
725 if (strncmp(buf, "CK ", 3) == 0)
726 continue; /* ignore */
727 err = got_error_msg(GOT_ERR_BAD_PACKET,
728 "unexpected message from server");
729 goto done;
730 } else {
731 err = got_error_msg(GOT_ERR_BAD_PACKET,
732 "unknown side-band received from server");
733 goto done;
735 } else {
736 /* No sideband channel. Every byte is packfile data. */
737 err = got_pkt_readn(&r, fd, buf, sizeof buf);
738 if (err)
739 goto done;
740 if (r <= 0)
741 break;
744 /*
745 * An expected SHA1 checksum sits at the end of the pack file.
746 * Since we don't know the file size ahead of time we have to
747 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
748 * those bytes into our SHA1 checksum computation until we
749 * know for sure that additional pack file data bytes follow.
751 * We can assume r > 0 since otherwise the loop would exit.
752 */
753 if (r < SHA1_DIGEST_LENGTH) {
754 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
755 /*
756 * If there's enough buffered + read data to
757 * fill up the buffer then shift a sufficient
758 * amount of bytes out at the front to make
759 * room, mixing those bytes into the checksum.
760 */
761 if (sha1_buf_len > 0 &&
762 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
763 size_t nshift = MIN(sha1_buf_len + r -
764 SHA1_DIGEST_LENGTH, sha1_buf_len);
765 SHA1Update(&sha1_ctx, sha1_buf, nshift);
766 memmove(sha1_buf, sha1_buf + nshift,
767 sha1_buf_len - nshift);
768 sha1_buf_len -= nshift;
771 /* Buffer potential checksum bytes. */
772 memcpy(sha1_buf + sha1_buf_len, buf, r);
773 sha1_buf_len += r;
774 } else {
775 /*
776 * Mix in previously buffered bytes which
777 * are not part of the checksum after all.
778 */
779 SHA1Update(&sha1_ctx, sha1_buf, r);
781 /* Update potential checksum buffer. */
782 memmove(sha1_buf, sha1_buf + r,
783 sha1_buf_len - r);
784 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
786 } else {
787 /* Mix in any previously buffered bytes. */
788 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
790 /* Mix in bytes read minus potential checksum bytes. */
791 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
793 /* Buffer potential checksum bytes. */
794 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
795 SHA1_DIGEST_LENGTH);
796 sha1_buf_len = SHA1_DIGEST_LENGTH;
799 /* Write packfile data to temporary pack file. */
800 w = write(packfd, buf, r);
801 if (w == -1) {
802 err = got_error_from_errno("write");
803 goto done;
805 if (w != r) {
806 err = got_error(GOT_ERR_IO);
807 goto done;
809 packsz += w;
811 /* Don't send too many progress privsep messages. */
812 if (packsz > last_reported_packsz + 1024) {
813 err = send_fetch_download_progress(ibuf, packsz, &rl);
814 if (err)
815 goto done;
816 last_reported_packsz = packsz;
819 err = send_fetch_download_progress(ibuf, packsz, NULL);
820 if (err)
821 goto done;
823 SHA1Final(pack_sha1, &sha1_ctx);
824 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
825 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
826 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
827 "pack file checksum mismatch");
829 done:
830 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
831 free(have);
832 free(want);
833 free(id_str);
834 free(default_id_str);
835 free(refname);
836 free(server_capabilities);
837 return err;
841 int
842 main(int argc, char **argv)
844 const struct got_error *err = NULL;
845 int fetchfd = -1, packfd = -1;
846 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
847 struct imsgbuf ibuf;
848 struct imsg imsg;
849 struct got_pathlist_head have_refs;
850 struct got_pathlist_head wanted_branches;
851 struct got_pathlist_head wanted_refs;
852 struct got_imsg_fetch_request fetch_req;
853 struct got_imsg_fetch_have_ref href;
854 struct got_imsg_fetch_wanted_branch wbranch;
855 struct got_imsg_fetch_wanted_ref wref;
856 size_t datalen, i;
857 char *remote_head = NULL, *worktree_branch = NULL;
858 #if 0
859 static int attached;
860 while (!attached)
861 sleep (1);
862 #endif
864 TAILQ_INIT(&have_refs);
865 TAILQ_INIT(&wanted_branches);
866 TAILQ_INIT(&wanted_refs);
868 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
869 #ifndef PROFILE
870 /* revoke access to most system calls */
871 if (pledge("stdio recvfd", NULL) == -1) {
872 err = got_error_from_errno("pledge");
873 got_privsep_send_error(&ibuf, err);
874 return 1;
876 #endif
877 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
878 if (err) {
879 if (err->code == GOT_ERR_PRIVSEP_PIPE)
880 err = NULL;
881 goto done;
883 if (imsg.hdr.type == GOT_IMSG_STOP)
884 goto done;
885 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
886 err = got_error(GOT_ERR_PRIVSEP_MSG);
887 goto done;
889 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
890 if (datalen < sizeof(fetch_req)) {
891 err = got_error(GOT_ERR_PRIVSEP_LEN);
892 goto done;
894 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
895 fetchfd = imsg.fd;
897 if (datalen != sizeof(fetch_req) +
898 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
899 err = got_error(GOT_ERR_PRIVSEP_LEN);
900 goto done;
903 if (fetch_req.worktree_branch_len != 0) {
904 worktree_branch = strndup(imsg.data +
905 sizeof(fetch_req), fetch_req.worktree_branch_len);
906 if (worktree_branch == NULL) {
907 err = got_error_from_errno("strndup");
908 goto done;
912 if (fetch_req.remote_head_len != 0) {
913 remote_head = strndup(imsg.data + sizeof(fetch_req) +
914 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
915 if (remote_head == NULL) {
916 err = got_error_from_errno("strndup");
917 goto done;
921 imsg_free(&imsg);
923 if (fetch_req.verbosity > 0)
924 chattygot += fetch_req.verbosity;
926 for (i = 0; i < fetch_req.n_have_refs; i++) {
927 struct got_object_id *id;
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_HAVE_REF) {
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
940 goto done;
942 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
943 if (datalen < sizeof(href)) {
944 err = got_error(GOT_ERR_PRIVSEP_LEN);
945 goto done;
947 memcpy(&href, imsg.data, sizeof(href));
948 if (datalen - sizeof(href) < href.name_len) {
949 err = got_error(GOT_ERR_PRIVSEP_LEN);
950 goto done;
952 refname = strndup(imsg.data + sizeof(href), href.name_len);
953 if (refname == NULL) {
954 err = got_error_from_errno("strndup");
955 goto done;
958 id = malloc(sizeof(*id));
959 if (id == NULL) {
960 free(refname);
961 err = got_error_from_errno("malloc");
962 goto done;
964 memcpy(id, &href.id, sizeof(*id));
965 err = got_pathlist_append(&have_refs, refname, id);
966 if (err) {
967 free(refname);
968 free(id);
969 goto done;
972 imsg_free(&imsg);
975 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
976 char *refname;
978 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
979 if (err) {
980 if (err->code == GOT_ERR_PRIVSEP_PIPE)
981 err = NULL;
982 goto done;
984 if (imsg.hdr.type == GOT_IMSG_STOP)
985 goto done;
986 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
987 err = got_error(GOT_ERR_PRIVSEP_MSG);
988 goto done;
990 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
991 if (datalen < sizeof(wbranch)) {
992 err = got_error(GOT_ERR_PRIVSEP_LEN);
993 goto done;
995 memcpy(&wbranch, imsg.data, sizeof(wbranch));
996 if (datalen - sizeof(wbranch) < wbranch.name_len) {
997 err = got_error(GOT_ERR_PRIVSEP_LEN);
998 goto done;
1000 refname = strndup(imsg.data + sizeof(wbranch),
1001 wbranch.name_len);
1002 if (refname == NULL) {
1003 err = got_error_from_errno("strndup");
1004 goto done;
1007 err = got_pathlist_append(&wanted_branches, refname, NULL);
1008 if (err) {
1009 free(refname);
1010 goto done;
1013 imsg_free(&imsg);
1016 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1017 char *refname;
1019 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1020 if (err) {
1021 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1022 err = NULL;
1023 goto done;
1025 if (imsg.hdr.type == GOT_IMSG_STOP)
1026 goto done;
1027 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 goto done;
1031 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1032 if (datalen < sizeof(wref)) {
1033 err = got_error(GOT_ERR_PRIVSEP_LEN);
1034 goto done;
1036 memcpy(&wref, imsg.data, sizeof(wref));
1037 if (datalen - sizeof(wref) < wref.name_len) {
1038 err = got_error(GOT_ERR_PRIVSEP_LEN);
1039 goto done;
1041 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1042 if (refname == NULL) {
1043 err = got_error_from_errno("strndup");
1044 goto done;
1047 err = got_pathlist_append(&wanted_refs, refname, NULL);
1048 if (err) {
1049 free(refname);
1050 goto done;
1053 imsg_free(&imsg);
1056 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1057 if (err) {
1058 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1059 err = NULL;
1060 goto done;
1062 if (imsg.hdr.type == GOT_IMSG_STOP)
1063 goto done;
1064 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1065 err = got_error(GOT_ERR_PRIVSEP_MSG);
1066 goto done;
1068 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1069 err = got_error(GOT_ERR_PRIVSEP_LEN);
1070 goto done;
1072 packfd = imsg.fd;
1074 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1075 fetch_req.fetch_all_branches, &wanted_branches,
1076 &wanted_refs, fetch_req.list_refs_only,
1077 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1078 done:
1079 free(worktree_branch);
1080 free(remote_head);
1081 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1082 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1083 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1084 err = got_error_from_errno("close");
1085 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1086 err = got_error_from_errno("close");
1087 if (err != NULL)
1088 got_privsep_send_error(&ibuf, err);
1089 else
1090 err = send_fetch_done(&ibuf, pack_sha1);
1091 if (err != NULL) {
1092 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1093 got_privsep_send_error(&ibuf, err);
1096 exit(0);