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, 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 SHA1_CTX sha1_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 SHA1Init(&sha1_ctx);
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 if (err)
374 goto done;
375 if (n == 0)
376 break;
377 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
378 err = fetch_error(&buf[4], n - 4);
379 goto done;
381 free(id_str);
382 free(refname);
383 err = got_gitproto_parse_refline(&id_str, &refname,
384 &server_capabilities, buf, n);
385 if (err)
386 goto done;
388 if (refsz == nref + 1) {
389 struct got_object_id *h, *w;
391 refsz *= 2;
392 h = reallocarray(have, refsz, sizeof(have[0]));
393 if (h == NULL) {
394 err = got_error_from_errno("reallocarray");
395 goto done;
397 have = h;
398 w = reallocarray(want, refsz, sizeof(want[0]));
399 if (w == NULL) {
400 err = got_error_from_errno("reallocarray");
401 goto done;
403 want = w;
406 if (is_firstpkt) {
407 if (chattygot && server_capabilities[0] != '\0')
408 fprintf(stderr, "%s: server capabilities: %s\n",
409 getprogname(), server_capabilities);
410 err = got_gitproto_match_capabilities(&my_capabilities,
411 &symrefs, server_capabilities,
412 got_capabilities, nitems(got_capabilities));
413 if (err)
414 goto done;
415 if (chattygot)
416 fprintf(stderr, "%s: my capabilities:%s\n",
417 getprogname(), my_capabilities != NULL ?
418 my_capabilities : "");
419 err = send_fetch_symrefs(ibuf, &symrefs);
420 if (err)
421 goto done;
422 is_firstpkt = 0;
423 if (!fetch_all_branches) {
424 TAILQ_FOREACH(pe, &symrefs, entry) {
425 const char *name = pe->path;
426 const char *symref_target = pe->data;
427 if (strcmp(name, GOT_REF_HEAD) != 0)
428 continue;
429 default_branch = symref_target;
430 break;
433 if (default_branch)
434 continue;
436 if (strstr(refname, "^{}")) {
437 if (chattygot) {
438 fprintf(stderr, "%s: ignoring %s\n",
439 getprogname(), refname);
441 continue;
443 if (default_branch && default_id_str == NULL &&
444 strcmp(refname, default_branch) == 0) {
445 default_id_str = strdup(id_str);
446 if (default_id_str == NULL) {
447 err = got_error_from_errno("strdup");
448 goto done;
452 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
453 err = fetch_ref(ibuf, have_refs, &have[nref],
454 &want[nref], refname, id_str);
455 if (err)
456 goto done;
457 nref++;
458 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
459 if (fetch_all_branches) {
460 err = fetch_ref(ibuf, have_refs, &have[nref],
461 &want[nref], refname, id_str);
462 if (err)
463 goto done;
464 nref++;
465 found_branch = 1;
466 continue;
468 TAILQ_FOREACH(pe, wanted_branches, entry) {
469 if (match_branch(refname, pe->path))
470 break;
472 if (pe != NULL || (worktree_branch != NULL &&
473 match_branch(refname, worktree_branch))) {
474 err = fetch_ref(ibuf, have_refs, &have[nref],
475 &want[nref], refname, id_str);
476 if (err)
477 goto done;
478 nref++;
479 found_branch = 1;
480 } else if (chattygot) {
481 fprintf(stderr, "%s: ignoring %s\n",
482 getprogname(), refname);
484 } else {
485 TAILQ_FOREACH(pe, wanted_refs, entry) {
486 if (match_wanted_ref(refname, pe->path))
487 break;
489 if (pe != NULL) {
490 err = fetch_ref(ibuf, have_refs, &have[nref],
491 &want[nref], refname, id_str);
492 if (err)
493 goto done;
494 nref++;
495 } else if (chattygot) {
496 fprintf(stderr, "%s: ignoring %s\n",
497 getprogname(), refname);
502 if (list_refs_only)
503 goto done;
505 /*
506 * If -b was not used and either none of the requested branches
507 * (got.conf, worktree) were found or the client already has the
508 * remote HEAD symref but its target changed, fetch remote's HEAD.
509 */
510 if (!no_head && default_branch && default_id_str &&
511 strncmp(default_branch, "refs/heads/", 11) == 0) {
512 int remote_head_changed = 0;
514 if (remote_head) {
515 if (strcmp(remote_head, default_branch + 11) != 0)
516 remote_head_changed = 1;
519 if (!found_branch || remote_head_changed) {
520 err = fetch_ref(ibuf, have_refs, &have[nref],
521 &want[nref], default_branch, default_id_str);
522 if (err)
523 goto done;
524 nref++;
528 /* Abort if we haven't found anything to fetch. */
529 if (nref == 0) {
530 struct got_pathlist_entry *pe;
531 static char msg[PATH_MAX + 33];
533 pe = TAILQ_FIRST(wanted_branches);
534 if (pe) {
535 snprintf(msg, sizeof(msg),
536 "branch \"%s\" not found on server", pe->path);
537 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
538 goto done;
541 pe = TAILQ_FIRST(wanted_refs);
542 if (pe) {
543 snprintf(msg, sizeof(msg),
544 "reference \"%s\" not found on server", pe->path);
545 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
546 goto done;
549 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
550 goto done;
553 for (i = 0; i < nref; i++) {
554 if (got_object_id_cmp(&have[i], &want[i]) == 0)
555 continue;
556 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
557 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
558 sent_my_capabilites || my_capabilities == NULL ?
559 "" : my_capabilities);
560 if (n < 0 || (size_t)n >= sizeof(buf)) {
561 err = got_error(GOT_ERR_NO_SPACE);
562 goto done;
564 err = got_pkt_writepkt(fd, buf, n, chattygot);
565 if (err)
566 goto done;
567 sent_my_capabilites = 1;
568 nwant++;
570 err = got_pkt_flushpkt(fd, chattygot);
571 if (err)
572 goto done;
574 if (nwant == 0)
575 goto done;
577 TAILQ_FOREACH(pe, have_refs, entry) {
578 struct got_object_id *id = pe->data;
579 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
580 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
581 if (n < 0 || (size_t)n >= sizeof(buf)) {
582 err = got_error(GOT_ERR_NO_SPACE);
583 goto done;
585 err = got_pkt_writepkt(fd, buf, n, chattygot);
586 if (err)
587 goto done;
588 nhave++;
591 n = strlcpy(buf, "done\n", sizeof(buf));
592 err = got_pkt_writepkt(fd, buf, n, chattygot);
593 if (err)
594 goto done;
596 while (nhave > 0 && !acked) {
597 struct got_object_id common_id;
599 /* The server should ACK the object IDs we need. */
600 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
601 if (err)
602 goto done;
603 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
604 err = fetch_error(&buf[4], n - 4);
605 goto done;
607 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
608 /*
609 * Server could not find a common ancestor.
610 * Perhaps it is an out-of-date mirror, or there
611 * is a repository with unrelated history.
612 */
613 break;
615 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
616 strncmp(buf, "ACK ", 4) != 0) {
617 err = got_error_msg(GOT_ERR_BAD_PACKET,
618 "unexpected message from server");
619 goto done;
621 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
622 err = got_error_msg(GOT_ERR_BAD_PACKET,
623 "bad object ID in ACK packet from server");
624 goto done;
626 acked++;
629 if (nhave == 0) {
630 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
631 if (err)
632 goto done;
633 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
634 err = got_error_msg(GOT_ERR_BAD_PACKET,
635 "unexpected message from server");
636 goto done;
640 if (chattygot)
641 fprintf(stderr, "%s: fetching...\n", getprogname());
643 if (my_capabilities != NULL &&
644 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
645 have_sidebands = 1;
647 while (1) {
648 ssize_t r = 0;
649 int datalen = -1;
651 if (have_sidebands) {
652 err = got_pkt_readhdr(&datalen, fd, chattygot);
653 if (err)
654 goto done;
655 if (datalen <= 0)
656 break;
658 /* Read sideband channel ID (one byte). */
659 r = read(fd, buf, 1);
660 if (r == -1) {
661 err = got_error_from_errno("read");
662 goto done;
664 if (r != 1) {
665 err = got_error_msg(GOT_ERR_BAD_PACKET,
666 "short packet");
667 goto done;
669 if (datalen > sizeof(buf) - 5) {
670 err = got_error_msg(GOT_ERR_BAD_PACKET,
671 "bad packet length");
672 goto done;
674 datalen--; /* sideband ID has been read */
675 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
676 /* Read packfile data. */
677 err = got_pkt_readn(&r, fd, buf, datalen);
678 if (err)
679 goto done;
680 if (r != datalen) {
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 "packet too short");
683 goto done;
685 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
686 err = got_pkt_readn(&r, fd, buf, datalen);
687 if (err)
688 goto done;
689 if (r != datalen) {
690 err = got_error_msg(GOT_ERR_BAD_PACKET,
691 "packet too short");
692 goto done;
694 err = fetch_progress(ibuf, buf, r);
695 if (err)
696 goto done;
697 continue;
698 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
699 err = got_pkt_readn(&r, fd, buf, datalen);
700 if (err)
701 goto done;
702 if (r != datalen) {
703 err = got_error_msg(GOT_ERR_BAD_PACKET,
704 "packet too short");
705 goto done;
707 err = fetch_error(buf, r);
708 goto done;
709 } else if (buf[0] == 'A') {
710 err = got_pkt_readn(&r, fd, buf, datalen);
711 if (err)
712 goto done;
713 if (r != datalen) {
714 err = got_error_msg(GOT_ERR_BAD_PACKET,
715 "packet too short");
716 goto done;
718 /*
719 * Git server responds with ACK after 'done'
720 * even though multi_ack is disabled?!?
721 */
722 buf[r] = '\0';
723 if (strncmp(buf, "CK ", 3) == 0)
724 continue; /* ignore */
725 err = got_error_msg(GOT_ERR_BAD_PACKET,
726 "unexpected message from server");
727 goto done;
728 } else {
729 err = got_error_msg(GOT_ERR_BAD_PACKET,
730 "unknown side-band received from server");
731 goto done;
733 } else {
734 /* No sideband channel. Every byte is packfile data. */
735 err = got_pkt_readn(&r, fd, buf, sizeof buf);
736 if (err)
737 goto done;
738 if (r <= 0)
739 break;
742 /*
743 * An expected SHA1 checksum sits at the end of the pack file.
744 * Since we don't know the file size ahead of time we have to
745 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
746 * those bytes into our SHA1 checksum computation until we
747 * know for sure that additional pack file data bytes follow.
749 * We can assume r > 0 since otherwise the loop would exit.
750 */
751 if (r < SHA1_DIGEST_LENGTH) {
752 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
753 /*
754 * If there's enough buffered + read data to
755 * fill up the buffer then shift a sufficient
756 * amount of bytes out at the front to make
757 * room, mixing those bytes into the checksum.
758 */
759 if (sha1_buf_len > 0 &&
760 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
761 size_t nshift = MIN(sha1_buf_len + r -
762 SHA1_DIGEST_LENGTH, sha1_buf_len);
763 SHA1Update(&sha1_ctx, sha1_buf, nshift);
764 memmove(sha1_buf, sha1_buf + nshift,
765 sha1_buf_len - nshift);
766 sha1_buf_len -= nshift;
769 /* Buffer potential checksum bytes. */
770 memcpy(sha1_buf + sha1_buf_len, buf, r);
771 sha1_buf_len += r;
772 } else {
773 /*
774 * Mix in previously buffered bytes which
775 * are not part of the checksum after all.
776 */
777 SHA1Update(&sha1_ctx, sha1_buf, r);
779 /* Update potential checksum buffer. */
780 memmove(sha1_buf, sha1_buf + r,
781 sha1_buf_len - r);
782 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
784 } else {
785 /* Mix in any previously buffered bytes. */
786 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
788 /* Mix in bytes read minus potential checksum bytes. */
789 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
791 /* Buffer potential checksum bytes. */
792 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
793 SHA1_DIGEST_LENGTH);
794 sha1_buf_len = SHA1_DIGEST_LENGTH;
797 /* Write packfile data to temporary pack file. */
798 w = write(packfd, buf, r);
799 if (w == -1) {
800 err = got_error_from_errno("write");
801 goto done;
803 if (w != r) {
804 err = got_error(GOT_ERR_IO);
805 goto done;
807 packsz += w;
809 /* Don't send too many progress privsep messages. */
810 if (packsz > last_reported_packsz + 1024) {
811 err = send_fetch_download_progress(ibuf, packsz, &rl);
812 if (err)
813 goto done;
814 last_reported_packsz = packsz;
817 err = send_fetch_download_progress(ibuf, packsz, NULL);
818 if (err)
819 goto done;
821 SHA1Final(pack_sha1, &sha1_ctx);
822 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
823 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
824 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
825 "pack file checksum mismatch");
827 done:
828 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
829 free(have);
830 free(want);
831 free(id_str);
832 free(default_id_str);
833 free(refname);
834 free(server_capabilities);
835 return err;
839 int
840 main(int argc, char **argv)
842 const struct got_error *err = NULL;
843 int fetchfd = -1, packfd = -1;
844 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
845 struct imsgbuf ibuf;
846 struct imsg imsg;
847 struct got_pathlist_head have_refs;
848 struct got_pathlist_head wanted_branches;
849 struct got_pathlist_head wanted_refs;
850 struct got_imsg_fetch_request fetch_req;
851 struct got_imsg_fetch_have_ref href;
852 struct got_imsg_fetch_wanted_branch wbranch;
853 struct got_imsg_fetch_wanted_ref wref;
854 size_t datalen, i;
855 char *remote_head = NULL, *worktree_branch = NULL;
856 #if 0
857 static int attached;
858 while (!attached)
859 sleep (1);
860 #endif
862 TAILQ_INIT(&have_refs);
863 TAILQ_INIT(&wanted_branches);
864 TAILQ_INIT(&wanted_refs);
866 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
867 #ifndef PROFILE
868 /* revoke access to most system calls */
869 if (pledge("stdio recvfd", NULL) == -1) {
870 err = got_error_from_errno("pledge");
871 got_privsep_send_error(&ibuf, err);
872 return 1;
874 #endif
875 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
876 if (err) {
877 if (err->code == GOT_ERR_PRIVSEP_PIPE)
878 err = NULL;
879 goto done;
881 if (imsg.hdr.type == GOT_IMSG_STOP)
882 goto done;
883 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
884 err = got_error(GOT_ERR_PRIVSEP_MSG);
885 goto done;
887 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
888 if (datalen < sizeof(fetch_req)) {
889 err = got_error(GOT_ERR_PRIVSEP_LEN);
890 goto done;
892 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
893 fetchfd = imsg.fd;
895 if (datalen != sizeof(fetch_req) +
896 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
897 err = got_error(GOT_ERR_PRIVSEP_LEN);
898 goto done;
901 if (fetch_req.worktree_branch_len != 0) {
902 worktree_branch = strndup(imsg.data +
903 sizeof(fetch_req), fetch_req.worktree_branch_len);
904 if (worktree_branch == NULL) {
905 err = got_error_from_errno("strndup");
906 goto done;
910 if (fetch_req.remote_head_len != 0) {
911 remote_head = strndup(imsg.data + sizeof(fetch_req) +
912 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
913 if (remote_head == NULL) {
914 err = got_error_from_errno("strndup");
915 goto done;
919 imsg_free(&imsg);
921 if (fetch_req.verbosity > 0)
922 chattygot += fetch_req.verbosity;
924 for (i = 0; i < fetch_req.n_have_refs; i++) {
925 struct got_object_id *id;
926 char *refname;
928 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
929 if (err) {
930 if (err->code == GOT_ERR_PRIVSEP_PIPE)
931 err = NULL;
932 goto done;
934 if (imsg.hdr.type == GOT_IMSG_STOP)
935 goto done;
936 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
937 err = got_error(GOT_ERR_PRIVSEP_MSG);
938 goto done;
940 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
941 if (datalen < sizeof(href)) {
942 err = got_error(GOT_ERR_PRIVSEP_LEN);
943 goto done;
945 memcpy(&href, imsg.data, sizeof(href));
946 if (datalen - sizeof(href) < href.name_len) {
947 err = got_error(GOT_ERR_PRIVSEP_LEN);
948 goto done;
950 refname = strndup(imsg.data + sizeof(href), href.name_len);
951 if (refname == NULL) {
952 err = got_error_from_errno("strndup");
953 goto done;
956 id = malloc(sizeof(*id));
957 if (id == NULL) {
958 free(refname);
959 err = got_error_from_errno("malloc");
960 goto done;
962 memcpy(id, &href.id, sizeof(*id));
963 err = got_pathlist_append(&have_refs, refname, id);
964 if (err) {
965 free(refname);
966 free(id);
967 goto done;
970 imsg_free(&imsg);
973 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
974 char *refname;
976 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
977 if (err) {
978 if (err->code == GOT_ERR_PRIVSEP_PIPE)
979 err = NULL;
980 goto done;
982 if (imsg.hdr.type == GOT_IMSG_STOP)
983 goto done;
984 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
985 err = got_error(GOT_ERR_PRIVSEP_MSG);
986 goto done;
988 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
989 if (datalen < sizeof(wbranch)) {
990 err = got_error(GOT_ERR_PRIVSEP_LEN);
991 goto done;
993 memcpy(&wbranch, imsg.data, sizeof(wbranch));
994 if (datalen - sizeof(wbranch) < wbranch.name_len) {
995 err = got_error(GOT_ERR_PRIVSEP_LEN);
996 goto done;
998 refname = strndup(imsg.data + sizeof(wbranch),
999 wbranch.name_len);
1000 if (refname == NULL) {
1001 err = got_error_from_errno("strndup");
1002 goto done;
1005 err = got_pathlist_append(&wanted_branches, refname, NULL);
1006 if (err) {
1007 free(refname);
1008 goto done;
1011 imsg_free(&imsg);
1014 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1015 char *refname;
1017 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1018 if (err) {
1019 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1020 err = NULL;
1021 goto done;
1023 if (imsg.hdr.type == GOT_IMSG_STOP)
1024 goto done;
1025 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1026 err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 goto done;
1029 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 if (datalen < sizeof(wref)) {
1031 err = got_error(GOT_ERR_PRIVSEP_LEN);
1032 goto done;
1034 memcpy(&wref, imsg.data, sizeof(wref));
1035 if (datalen - sizeof(wref) < wref.name_len) {
1036 err = got_error(GOT_ERR_PRIVSEP_LEN);
1037 goto done;
1039 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1040 if (refname == NULL) {
1041 err = got_error_from_errno("strndup");
1042 goto done;
1045 err = got_pathlist_append(&wanted_refs, refname, NULL);
1046 if (err) {
1047 free(refname);
1048 goto done;
1051 imsg_free(&imsg);
1054 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1055 if (err) {
1056 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1057 err = NULL;
1058 goto done;
1060 if (imsg.hdr.type == GOT_IMSG_STOP)
1061 goto done;
1062 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1063 err = got_error(GOT_ERR_PRIVSEP_MSG);
1064 goto done;
1066 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1067 err = got_error(GOT_ERR_PRIVSEP_LEN);
1068 goto done;
1070 packfd = imsg.fd;
1072 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1073 fetch_req.fetch_all_branches, &wanted_branches,
1074 &wanted_refs, fetch_req.list_refs_only,
1075 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1076 done:
1077 free(worktree_branch);
1078 free(remote_head);
1079 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1080 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1081 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1082 err = got_error_from_errno("close");
1083 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1084 err = got_error_from_errno("close");
1085 if (err != NULL)
1086 got_privsep_send_error(&ibuf, err);
1087 else
1088 err = send_fetch_done(&ibuf, pack_sha1);
1089 if (err != NULL) {
1090 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1091 got_privsep_send_error(&ibuf, err);
1094 exit(0);