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 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 imsg_close(ibuf, wbuf);
290 return got_privsep_flush_imsg(ibuf);
293 static const struct got_error *
294 fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
295 struct got_object_id *have, struct got_object_id *want,
296 const char *refname, const char *id_str)
298 const struct got_error *err;
299 char *theirs = NULL, *mine = NULL;
301 if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
302 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
303 goto done;
306 match_remote_ref(have_refs, have, refname);
307 err = send_fetch_ref(ibuf, want, refname);
308 if (err)
309 goto done;
311 if (chattygot)
312 fprintf(stderr, "%s: %s will be fetched\n",
313 getprogname(), refname);
314 if (chattygot > 1) {
315 err = got_object_id_str(&theirs, want);
316 if (err)
317 goto done;
318 err = got_object_id_str(&mine, have);
319 if (err)
320 goto done;
321 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
322 getprogname(), theirs, getprogname(), mine);
324 done:
325 free(theirs);
326 free(mine);
327 return err;
330 static const struct got_error *
331 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
332 struct got_pathlist_head *have_refs, int fetch_all_branches,
333 struct got_pathlist_head *wanted_branches,
334 struct got_pathlist_head *wanted_refs, int list_refs_only,
335 const char *worktree_branch, const char *remote_head,
336 int no_head, struct imsgbuf *ibuf)
338 const struct got_error *err = NULL;
339 char buf[GOT_PKT_MAX];
340 char hashstr[SHA1_DIGEST_STRING_LENGTH];
341 struct got_object_id *have, *want;
342 int is_firstpkt = 1, nref = 0, refsz = 16;
343 int i, n, nwant = 0, nhave = 0, acked = 0;
344 off_t packsz = 0, last_reported_packsz = 0;
345 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
346 char *server_capabilities = NULL, *my_capabilities = NULL;
347 const char *default_branch = NULL;
348 struct got_pathlist_head symrefs;
349 struct got_pathlist_entry *pe;
350 int sent_my_capabilites = 0, have_sidebands = 0;
351 int found_branch = 0;
352 struct got_hash ctx;
353 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
354 size_t sha1_buf_len = 0;
355 ssize_t w;
356 struct got_ratelimit rl;
358 TAILQ_INIT(&symrefs);
359 got_hash_init(&ctx, GOT_HASH_SHA1);
360 got_ratelimit_init(&rl, 0, 500);
362 have = malloc(refsz * sizeof(have[0]));
363 if (have == NULL)
364 return got_error_from_errno("malloc");
365 want = malloc(refsz * sizeof(want[0]));
366 if (want == NULL) {
367 err = got_error_from_errno("malloc");
368 goto done;
370 while (1) {
371 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
372 if (err)
373 goto done;
374 if (n == 0)
375 break;
376 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
377 err = fetch_error(&buf[4], n - 4);
378 goto done;
380 free(id_str);
381 free(refname);
382 err = got_gitproto_parse_refline(&id_str, &refname,
383 &server_capabilities, buf, n);
384 if (err)
385 goto done;
387 if (refsz == nref + 1) {
388 struct got_object_id *h, *w;
390 refsz *= 2;
391 h = reallocarray(have, refsz, sizeof(have[0]));
392 if (h == NULL) {
393 err = got_error_from_errno("reallocarray");
394 goto done;
396 have = h;
397 w = reallocarray(want, refsz, sizeof(want[0]));
398 if (w == NULL) {
399 err = got_error_from_errno("reallocarray");
400 goto done;
402 want = w;
405 if (is_firstpkt) {
406 if (chattygot && server_capabilities[0] != '\0')
407 fprintf(stderr, "%s: server capabilities: %s\n",
408 getprogname(), server_capabilities);
409 err = got_gitproto_match_capabilities(&my_capabilities,
410 &symrefs, server_capabilities,
411 got_capabilities, nitems(got_capabilities));
412 if (err)
413 goto done;
414 if (chattygot)
415 fprintf(stderr, "%s: my capabilities:%s\n",
416 getprogname(), my_capabilities != NULL ?
417 my_capabilities : "");
418 err = send_fetch_symrefs(ibuf, &symrefs);
419 if (err)
420 goto done;
421 is_firstpkt = 0;
422 if (!fetch_all_branches) {
423 TAILQ_FOREACH(pe, &symrefs, entry) {
424 const char *name = pe->path;
425 const char *symref_target = pe->data;
426 if (strcmp(name, GOT_REF_HEAD) != 0)
427 continue;
428 default_branch = symref_target;
429 break;
432 if (default_branch)
433 continue;
435 if (strstr(refname, "^{}")) {
436 if (chattygot) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
440 continue;
442 if (default_branch && default_id_str == NULL &&
443 strcmp(refname, default_branch) == 0) {
444 default_id_str = strdup(id_str);
445 if (default_id_str == NULL) {
446 err = got_error_from_errno("strdup");
447 goto done;
451 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
452 err = fetch_ref(ibuf, have_refs, &have[nref],
453 &want[nref], refname, id_str);
454 if (err)
455 goto done;
456 nref++;
457 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
458 if (fetch_all_branches) {
459 err = fetch_ref(ibuf, have_refs, &have[nref],
460 &want[nref], refname, id_str);
461 if (err)
462 goto done;
463 nref++;
464 found_branch = 1;
465 continue;
467 TAILQ_FOREACH(pe, wanted_branches, entry) {
468 if (match_branch(refname, pe->path))
469 break;
471 if (pe != NULL || (worktree_branch != NULL &&
472 match_branch(refname, worktree_branch))) {
473 err = fetch_ref(ibuf, have_refs, &have[nref],
474 &want[nref], refname, id_str);
475 if (err)
476 goto done;
477 nref++;
478 found_branch = 1;
479 } else if (chattygot) {
480 fprintf(stderr, "%s: ignoring %s\n",
481 getprogname(), refname);
483 } else {
484 TAILQ_FOREACH(pe, wanted_refs, entry) {
485 if (match_wanted_ref(refname, pe->path))
486 break;
488 if (pe != NULL) {
489 err = fetch_ref(ibuf, have_refs, &have[nref],
490 &want[nref], refname, id_str);
491 if (err)
492 goto done;
493 nref++;
494 } else if (chattygot) {
495 fprintf(stderr, "%s: ignoring %s\n",
496 getprogname(), refname);
501 if (list_refs_only)
502 goto done;
504 /*
505 * If -b was not used and either none of the requested branches
506 * (got.conf, worktree) were found or the client already has the
507 * remote HEAD symref but its target changed, fetch remote's HEAD.
508 */
509 if (!no_head && default_branch && default_id_str &&
510 strncmp(default_branch, "refs/heads/", 11) == 0) {
511 int remote_head_changed = 0;
513 if (remote_head) {
514 if (strcmp(remote_head, default_branch + 11) != 0)
515 remote_head_changed = 1;
518 if (!found_branch || remote_head_changed) {
519 err = fetch_ref(ibuf, have_refs, &have[nref],
520 &want[nref], default_branch, default_id_str);
521 if (err)
522 goto done;
523 nref++;
527 /* Abort if we haven't found anything to fetch. */
528 if (nref == 0) {
529 struct got_pathlist_entry *pe;
530 static char msg[PATH_MAX + 33];
532 pe = TAILQ_FIRST(wanted_branches);
533 if (pe) {
534 snprintf(msg, sizeof(msg),
535 "branch \"%s\" not found on server", pe->path);
536 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
537 goto done;
540 pe = TAILQ_FIRST(wanted_refs);
541 if (pe) {
542 snprintf(msg, sizeof(msg),
543 "reference \"%s\" not found on server", pe->path);
544 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
545 goto done;
548 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
549 goto done;
552 for (i = 0; i < nref; i++) {
553 if (got_object_id_cmp(&have[i], &want[i]) == 0)
554 continue;
555 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
556 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
557 sent_my_capabilites || my_capabilities == NULL ?
558 "" : my_capabilities);
559 if (n < 0 || (size_t)n >= sizeof(buf)) {
560 err = got_error(GOT_ERR_NO_SPACE);
561 goto done;
563 err = got_pkt_writepkt(fd, buf, n, chattygot);
564 if (err)
565 goto done;
566 sent_my_capabilites = 1;
567 nwant++;
569 err = got_pkt_flushpkt(fd, chattygot);
570 if (err)
571 goto done;
573 if (nwant == 0)
574 goto done;
576 TAILQ_FOREACH(pe, have_refs, entry) {
577 struct got_object_id *id = pe->data;
578 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
579 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
580 if (n < 0 || (size_t)n >= sizeof(buf)) {
581 err = got_error(GOT_ERR_NO_SPACE);
582 goto done;
584 err = got_pkt_writepkt(fd, buf, n, chattygot);
585 if (err)
586 goto done;
587 nhave++;
590 n = strlcpy(buf, "done\n", sizeof(buf));
591 err = got_pkt_writepkt(fd, buf, n, chattygot);
592 if (err)
593 goto done;
595 while (nhave > 0 && !acked) {
596 struct got_object_id common_id;
598 /* The server should ACK the object IDs we need. */
599 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
600 if (err)
601 goto done;
602 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
603 err = fetch_error(&buf[4], n - 4);
604 goto done;
606 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
607 /*
608 * Server could not find a common ancestor.
609 * Perhaps it is an out-of-date mirror, or there
610 * is a repository with unrelated history.
611 */
612 break;
614 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
615 strncmp(buf, "ACK ", 4) != 0) {
616 err = got_error_msg(GOT_ERR_BAD_PACKET,
617 "unexpected message from server");
618 goto done;
620 if (!got_parse_object_id(&common_id, buf + 4,
621 GOT_HASH_SHA1)) {
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 got_hash_update(&ctx, sha1_buf,
764 nshift);
765 memmove(sha1_buf, sha1_buf + nshift,
766 sha1_buf_len - nshift);
767 sha1_buf_len -= nshift;
770 /* Buffer potential checksum bytes. */
771 memcpy(sha1_buf + sha1_buf_len, buf, r);
772 sha1_buf_len += r;
773 } else {
774 /*
775 * Mix in previously buffered bytes which
776 * are not part of the checksum after all.
777 */
778 got_hash_update(&ctx, sha1_buf, r);
780 /* Update potential checksum buffer. */
781 memmove(sha1_buf, sha1_buf + r,
782 sha1_buf_len - r);
783 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
785 } else {
786 /* Mix in any previously buffered bytes. */
787 got_hash_update(&ctx, sha1_buf, sha1_buf_len);
789 /* Mix in bytes read minus potential checksum bytes. */
790 got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
792 /* Buffer potential checksum bytes. */
793 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
794 SHA1_DIGEST_LENGTH);
795 sha1_buf_len = SHA1_DIGEST_LENGTH;
798 /* Write packfile data to temporary pack file. */
799 w = write(packfd, buf, r);
800 if (w == -1) {
801 err = got_error_from_errno("write");
802 goto done;
804 if (w != r) {
805 err = got_error(GOT_ERR_IO);
806 goto done;
808 packsz += w;
810 /* Don't send too many progress privsep messages. */
811 if (packsz > last_reported_packsz + 1024) {
812 err = send_fetch_download_progress(ibuf, packsz, &rl);
813 if (err)
814 goto done;
815 last_reported_packsz = packsz;
818 err = send_fetch_download_progress(ibuf, packsz, NULL);
819 if (err)
820 goto done;
822 got_hash_final(&ctx, pack_sha1);
823 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
824 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
825 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
826 "pack file checksum mismatch");
828 done:
829 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
830 free(have);
831 free(want);
832 free(id_str);
833 free(default_id_str);
834 free(refname);
835 free(server_capabilities);
836 return err;
840 int
841 main(int argc, char **argv)
843 const struct got_error *err = NULL;
844 int fetchfd = -1, packfd = -1;
845 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
846 struct imsgbuf ibuf;
847 struct imsg imsg;
848 struct got_pathlist_head have_refs;
849 struct got_pathlist_head wanted_branches;
850 struct got_pathlist_head wanted_refs;
851 struct got_imsg_fetch_request fetch_req;
852 struct got_imsg_fetch_have_ref href;
853 struct got_imsg_fetch_wanted_branch wbranch;
854 struct got_imsg_fetch_wanted_ref wref;
855 size_t datalen, i;
856 char *remote_head = NULL, *worktree_branch = NULL;
857 #if 0
858 static int attached;
859 while (!attached)
860 sleep (1);
861 #endif
863 TAILQ_INIT(&have_refs);
864 TAILQ_INIT(&wanted_branches);
865 TAILQ_INIT(&wanted_refs);
867 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
868 #ifndef PROFILE
869 /* revoke access to most system calls */
870 if (pledge("stdio recvfd", NULL) == -1) {
871 err = got_error_from_errno("pledge");
872 got_privsep_send_error(&ibuf, err);
873 return 1;
875 #endif
876 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
877 if (err) {
878 if (err->code == GOT_ERR_PRIVSEP_PIPE)
879 err = NULL;
880 goto done;
882 if (imsg.hdr.type == GOT_IMSG_STOP)
883 goto done;
884 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
885 err = got_error(GOT_ERR_PRIVSEP_MSG);
886 goto done;
888 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
889 if (datalen < sizeof(fetch_req)) {
890 err = got_error(GOT_ERR_PRIVSEP_LEN);
891 goto done;
893 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
894 fetchfd = imsg_get_fd(&imsg);
896 if (datalen != sizeof(fetch_req) +
897 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
898 err = got_error(GOT_ERR_PRIVSEP_LEN);
899 goto done;
902 if (fetch_req.worktree_branch_len != 0) {
903 worktree_branch = strndup(imsg.data +
904 sizeof(fetch_req), fetch_req.worktree_branch_len);
905 if (worktree_branch == NULL) {
906 err = got_error_from_errno("strndup");
907 goto done;
911 if (fetch_req.remote_head_len != 0) {
912 remote_head = strndup(imsg.data + sizeof(fetch_req) +
913 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
914 if (remote_head == NULL) {
915 err = got_error_from_errno("strndup");
916 goto done;
920 imsg_free(&imsg);
922 if (fetch_req.verbosity > 0)
923 chattygot += fetch_req.verbosity;
925 for (i = 0; i < fetch_req.n_have_refs; i++) {
926 struct got_object_id *id;
927 char *refname;
929 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
930 if (err) {
931 if (err->code == GOT_ERR_PRIVSEP_PIPE)
932 err = NULL;
933 goto done;
935 if (imsg.hdr.type == GOT_IMSG_STOP)
936 goto done;
937 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
938 err = got_error(GOT_ERR_PRIVSEP_MSG);
939 goto done;
941 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
942 if (datalen < sizeof(href)) {
943 err = got_error(GOT_ERR_PRIVSEP_LEN);
944 goto done;
946 memcpy(&href, imsg.data, sizeof(href));
947 if (datalen - sizeof(href) < href.name_len) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 goto done;
951 refname = strndup(imsg.data + sizeof(href), href.name_len);
952 if (refname == NULL) {
953 err = got_error_from_errno("strndup");
954 goto done;
957 id = malloc(sizeof(*id));
958 if (id == NULL) {
959 free(refname);
960 err = got_error_from_errno("malloc");
961 goto done;
963 memcpy(id, &href.id, sizeof(*id));
964 err = got_pathlist_append(&have_refs, refname, id);
965 if (err) {
966 free(refname);
967 free(id);
968 goto done;
971 imsg_free(&imsg);
974 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
975 char *refname;
977 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
978 if (err) {
979 if (err->code == GOT_ERR_PRIVSEP_PIPE)
980 err = NULL;
981 goto done;
983 if (imsg.hdr.type == GOT_IMSG_STOP)
984 goto done;
985 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
987 goto done;
989 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
990 if (datalen < sizeof(wbranch)) {
991 err = got_error(GOT_ERR_PRIVSEP_LEN);
992 goto done;
994 memcpy(&wbranch, imsg.data, sizeof(wbranch));
995 if (datalen - sizeof(wbranch) < wbranch.name_len) {
996 err = got_error(GOT_ERR_PRIVSEP_LEN);
997 goto done;
999 refname = strndup(imsg.data + sizeof(wbranch),
1000 wbranch.name_len);
1001 if (refname == NULL) {
1002 err = got_error_from_errno("strndup");
1003 goto done;
1006 err = got_pathlist_append(&wanted_branches, refname, NULL);
1007 if (err) {
1008 free(refname);
1009 goto done;
1012 imsg_free(&imsg);
1015 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1016 char *refname;
1018 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1019 if (err) {
1020 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1021 err = NULL;
1022 goto done;
1024 if (imsg.hdr.type == GOT_IMSG_STOP)
1025 goto done;
1026 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1028 goto done;
1030 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1031 if (datalen < sizeof(wref)) {
1032 err = got_error(GOT_ERR_PRIVSEP_LEN);
1033 goto done;
1035 memcpy(&wref, imsg.data, sizeof(wref));
1036 if (datalen - sizeof(wref) < wref.name_len) {
1037 err = got_error(GOT_ERR_PRIVSEP_LEN);
1038 goto done;
1040 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1041 if (refname == NULL) {
1042 err = got_error_from_errno("strndup");
1043 goto done;
1046 err = got_pathlist_append(&wanted_refs, refname, NULL);
1047 if (err) {
1048 free(refname);
1049 goto done;
1052 imsg_free(&imsg);
1055 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1056 if (err) {
1057 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1058 err = NULL;
1059 goto done;
1061 if (imsg.hdr.type == GOT_IMSG_STOP)
1062 goto done;
1063 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1064 err = got_error(GOT_ERR_PRIVSEP_MSG);
1065 goto done;
1067 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1069 goto done;
1071 packfd = imsg_get_fd(&imsg);
1073 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1074 fetch_req.fetch_all_branches, &wanted_branches,
1075 &wanted_refs, fetch_req.list_refs_only,
1076 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1077 done:
1078 free(worktree_branch);
1079 free(remote_head);
1080 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1081 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1082 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1083 err = got_error_from_errno("close");
1084 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1086 if (err != NULL)
1087 got_privsep_send_error(&ibuf, err);
1088 else
1089 err = send_fetch_done(&ibuf, pack_sha1);
1090 if (err != NULL) {
1091 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1092 got_privsep_send_error(&ibuf, err);
1095 exit(0);