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 struct got_hash 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 got_hash_init(&ctx, GOT_HASH_SHA1);
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 got_hash_update(&ctx, sha1_buf,
766 nshift);
767 memmove(sha1_buf, sha1_buf + nshift,
768 sha1_buf_len - nshift);
769 sha1_buf_len -= nshift;
772 /* Buffer potential checksum bytes. */
773 memcpy(sha1_buf + sha1_buf_len, buf, r);
774 sha1_buf_len += r;
775 } else {
776 /*
777 * Mix in previously buffered bytes which
778 * are not part of the checksum after all.
779 */
780 got_hash_update(&ctx, sha1_buf, r);
782 /* Update potential checksum buffer. */
783 memmove(sha1_buf, sha1_buf + r,
784 sha1_buf_len - r);
785 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
787 } else {
788 /* Mix in any previously buffered bytes. */
789 got_hash_update(&ctx, sha1_buf, sha1_buf_len);
791 /* Mix in bytes read minus potential checksum bytes. */
792 got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
794 /* Buffer potential checksum bytes. */
795 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
796 SHA1_DIGEST_LENGTH);
797 sha1_buf_len = SHA1_DIGEST_LENGTH;
800 /* Write packfile data to temporary pack file. */
801 w = write(packfd, buf, r);
802 if (w == -1) {
803 err = got_error_from_errno("write");
804 goto done;
806 if (w != r) {
807 err = got_error(GOT_ERR_IO);
808 goto done;
810 packsz += w;
812 /* Don't send too many progress privsep messages. */
813 if (packsz > last_reported_packsz + 1024) {
814 err = send_fetch_download_progress(ibuf, packsz, &rl);
815 if (err)
816 goto done;
817 last_reported_packsz = packsz;
820 err = send_fetch_download_progress(ibuf, packsz, NULL);
821 if (err)
822 goto done;
824 got_hash_final(&ctx, pack_sha1);
825 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
826 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
827 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
828 "pack file checksum mismatch");
830 done:
831 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
832 free(have);
833 free(want);
834 free(id_str);
835 free(default_id_str);
836 free(refname);
837 free(server_capabilities);
838 return err;
842 int
843 main(int argc, char **argv)
845 const struct got_error *err = NULL;
846 int fetchfd = -1, packfd = -1;
847 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
848 struct imsgbuf ibuf;
849 struct imsg imsg;
850 struct got_pathlist_head have_refs;
851 struct got_pathlist_head wanted_branches;
852 struct got_pathlist_head wanted_refs;
853 struct got_imsg_fetch_request fetch_req;
854 struct got_imsg_fetch_have_ref href;
855 struct got_imsg_fetch_wanted_branch wbranch;
856 struct got_imsg_fetch_wanted_ref wref;
857 size_t datalen, i;
858 char *remote_head = NULL, *worktree_branch = NULL;
859 #if 0
860 static int attached;
861 while (!attached)
862 sleep (1);
863 #endif
865 TAILQ_INIT(&have_refs);
866 TAILQ_INIT(&wanted_branches);
867 TAILQ_INIT(&wanted_refs);
869 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
870 #ifndef PROFILE
871 /* revoke access to most system calls */
872 if (pledge("stdio recvfd", NULL) == -1) {
873 err = got_error_from_errno("pledge");
874 got_privsep_send_error(&ibuf, err);
875 return 1;
877 #endif
878 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
879 if (err) {
880 if (err->code == GOT_ERR_PRIVSEP_PIPE)
881 err = NULL;
882 goto done;
884 if (imsg.hdr.type == GOT_IMSG_STOP)
885 goto done;
886 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
887 err = got_error(GOT_ERR_PRIVSEP_MSG);
888 goto done;
890 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
891 if (datalen < sizeof(fetch_req)) {
892 err = got_error(GOT_ERR_PRIVSEP_LEN);
893 goto done;
895 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
896 fetchfd = imsg.fd;
898 if (datalen != sizeof(fetch_req) +
899 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
901 goto done;
904 if (fetch_req.worktree_branch_len != 0) {
905 worktree_branch = strndup(imsg.data +
906 sizeof(fetch_req), fetch_req.worktree_branch_len);
907 if (worktree_branch == NULL) {
908 err = got_error_from_errno("strndup");
909 goto done;
913 if (fetch_req.remote_head_len != 0) {
914 remote_head = strndup(imsg.data + sizeof(fetch_req) +
915 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
916 if (remote_head == NULL) {
917 err = got_error_from_errno("strndup");
918 goto done;
922 imsg_free(&imsg);
924 if (fetch_req.verbosity > 0)
925 chattygot += fetch_req.verbosity;
927 for (i = 0; i < fetch_req.n_have_refs; i++) {
928 struct got_object_id *id;
929 char *refname;
931 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
932 if (err) {
933 if (err->code == GOT_ERR_PRIVSEP_PIPE)
934 err = NULL;
935 goto done;
937 if (imsg.hdr.type == GOT_IMSG_STOP)
938 goto done;
939 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
941 goto done;
943 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
944 if (datalen < sizeof(href)) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
946 goto done;
948 memcpy(&href, imsg.data, sizeof(href));
949 if (datalen - sizeof(href) < href.name_len) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 goto done;
953 refname = strndup(imsg.data + sizeof(href), href.name_len);
954 if (refname == NULL) {
955 err = got_error_from_errno("strndup");
956 goto done;
959 id = malloc(sizeof(*id));
960 if (id == NULL) {
961 free(refname);
962 err = got_error_from_errno("malloc");
963 goto done;
965 memcpy(id, &href.id, sizeof(*id));
966 err = got_pathlist_append(&have_refs, refname, id);
967 if (err) {
968 free(refname);
969 free(id);
970 goto done;
973 imsg_free(&imsg);
976 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
977 char *refname;
979 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
980 if (err) {
981 if (err->code == GOT_ERR_PRIVSEP_PIPE)
982 err = NULL;
983 goto done;
985 if (imsg.hdr.type == GOT_IMSG_STOP)
986 goto done;
987 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
988 err = got_error(GOT_ERR_PRIVSEP_MSG);
989 goto done;
991 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
992 if (datalen < sizeof(wbranch)) {
993 err = got_error(GOT_ERR_PRIVSEP_LEN);
994 goto done;
996 memcpy(&wbranch, imsg.data, sizeof(wbranch));
997 if (datalen - sizeof(wbranch) < wbranch.name_len) {
998 err = got_error(GOT_ERR_PRIVSEP_LEN);
999 goto done;
1001 refname = strndup(imsg.data + sizeof(wbranch),
1002 wbranch.name_len);
1003 if (refname == NULL) {
1004 err = got_error_from_errno("strndup");
1005 goto done;
1008 err = got_pathlist_append(&wanted_branches, refname, NULL);
1009 if (err) {
1010 free(refname);
1011 goto done;
1014 imsg_free(&imsg);
1017 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1018 char *refname;
1020 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1021 if (err) {
1022 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1023 err = NULL;
1024 goto done;
1026 if (imsg.hdr.type == GOT_IMSG_STOP)
1027 goto done;
1028 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1030 goto done;
1032 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1033 if (datalen < sizeof(wref)) {
1034 err = got_error(GOT_ERR_PRIVSEP_LEN);
1035 goto done;
1037 memcpy(&wref, imsg.data, sizeof(wref));
1038 if (datalen - sizeof(wref) < wref.name_len) {
1039 err = got_error(GOT_ERR_PRIVSEP_LEN);
1040 goto done;
1042 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1043 if (refname == NULL) {
1044 err = got_error_from_errno("strndup");
1045 goto done;
1048 err = got_pathlist_append(&wanted_refs, refname, NULL);
1049 if (err) {
1050 free(refname);
1051 goto done;
1054 imsg_free(&imsg);
1057 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1058 if (err) {
1059 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1060 err = NULL;
1061 goto done;
1063 if (imsg.hdr.type == GOT_IMSG_STOP)
1064 goto done;
1065 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1066 err = got_error(GOT_ERR_PRIVSEP_MSG);
1067 goto done;
1069 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1070 err = got_error(GOT_ERR_PRIVSEP_LEN);
1071 goto done;
1073 packfd = imsg.fd;
1075 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1076 fetch_req.fetch_all_branches, &wanted_branches,
1077 &wanted_refs, fetch_req.list_refs_only,
1078 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1079 done:
1080 free(worktree_branch);
1081 free(remote_head);
1082 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1083 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1084 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1086 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1087 err = got_error_from_errno("close");
1088 if (err != NULL)
1089 got_privsep_send_error(&ibuf, err);
1090 else
1091 err = send_fetch_done(&ibuf, pack_sha1);
1092 if (err != NULL) {
1093 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1094 got_privsep_send_error(&ibuf, err);
1097 exit(0);