Blob


1 /*
2 * Copyright (c) 2018, 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/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <sha1.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
41 #include <uuid.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
51 #include "got_fetch.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_dial.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 #ifndef ssizeof
70 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 #endif
73 #ifndef MIN
74 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 #endif
77 static int
78 hassuffix(char *base, char *suf)
79 {
80 int nb, ns;
82 nb = strlen(base);
83 ns = strlen(suf);
84 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
85 return 1;
86 return 0;
87 }
89 const struct got_error *
90 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
91 const char *host, const char *port, const char *server_path, int verbosity)
92 {
93 const struct got_error *err = NULL;
95 *fetchpid = -1;
96 *fetchfd = -1;
98 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
99 err = got_dial_ssh(fetchpid, fetchfd, host, port,
100 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
101 else if (strcmp(proto, "git") == 0)
102 err = got_dial_git(fetchfd, host, port, server_path,
103 GOT_DIAL_DIRECTION_FETCH);
104 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
105 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
106 else
107 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
108 return err;
111 const struct got_error *
112 got_fetch_parse_uri(char **proto, char **host, char **port,
113 char **server_path, char **repo_name, const char *uri)
115 const struct got_error *err = NULL;
116 char *s, *p, *q;
117 int n;
119 *proto = *host = *port = *server_path = *repo_name = NULL;
121 p = strstr(uri, "://");
122 if (!p) {
123 /* Try parsing Git's "scp" style URL syntax. */
124 *proto = strdup("ssh");
125 if (proto == NULL) {
126 err = got_error_from_errno("strdup");
127 goto done;
129 s = (char *)uri;
130 q = strchr(s, ':');
131 if (q == NULL) {
132 err = got_error(GOT_ERR_PARSE_URI);
133 goto done;
135 /* No slashes allowed before first colon. */
136 p = strchr(s, '/');
137 if (p && q > p) {
138 err = got_error(GOT_ERR_PARSE_URI);
139 goto done;
141 *host = strndup(s, q - s);
142 if (*host == NULL) {
143 err = got_error_from_errno("strndup");
144 goto done;
146 p = q + 1;
147 } else {
148 *proto = strndup(uri, p - uri);
149 if (proto == NULL) {
150 err = got_error_from_errno("strndup");
151 goto done;
153 s = p + 3;
155 p = strstr(s, "/");
156 if (p == NULL || strlen(p) == 1) {
157 err = got_error(GOT_ERR_PARSE_URI);
158 goto done;
161 q = memchr(s, ':', p - s);
162 if (q) {
163 *host = strndup(s, q - s);
164 if (*host == NULL) {
165 err = got_error_from_errno("strndup");
166 goto done;
168 *port = strndup(q + 1, p - (q + 1));
169 if (*port == NULL) {
170 err = got_error_from_errno("strndup");
171 goto done;
173 } else {
174 *host = strndup(s, p - s);
175 if (*host == NULL) {
176 err = got_error_from_errno("strndup");
177 goto done;
182 while (p[0] == '/' && p[1] == '/')
183 p++;
184 *server_path = strdup(p);
185 if (*server_path == NULL) {
186 err = got_error_from_errno("strdup");
187 goto done;
189 got_path_strip_trailing_slashes(*server_path);
191 p = strrchr(p, '/');
192 if (!p || strlen(p) <= 1) {
193 err = got_error(GOT_ERR_PARSE_URI);
194 goto done;
196 p++;
197 n = strlen(p);
198 if (n == 0) {
199 err = got_error(GOT_ERR_PARSE_URI);
200 goto done;
202 if (hassuffix(p, ".git"))
203 n -= 4;
204 *repo_name = strndup(p, (p + n) - p);
205 if (*repo_name == NULL) {
206 err = got_error_from_errno("strndup");
207 goto done;
209 done:
210 if (err) {
211 free(*proto);
212 *proto = NULL;
213 free(*host);
214 *host = NULL;
215 free(*port);
216 *port = NULL;
217 free(*server_path);
218 *server_path = NULL;
219 free(*repo_name);
220 *repo_name = NULL;
222 return err;
225 const struct got_error*
226 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
227 struct got_pathlist_head *symrefs, const char *remote_name,
228 int mirror_references, int fetch_all_branches,
229 struct got_pathlist_head *wanted_branches,
230 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
231 int fetchfd, struct got_repository *repo,
232 got_fetch_progress_cb progress_cb, void *progress_arg)
234 size_t i;
235 int imsg_fetchfds[2], imsg_idxfds[2];
236 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
237 int tmpfds[3];
238 int fetchstatus, idxstatus, done = 0;
239 const struct got_error *err;
240 struct imsgbuf fetchibuf, idxibuf;
241 pid_t fetchpid, idxpid;
242 char *tmppackpath = NULL, *tmpidxpath = NULL;
243 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
244 const char *repo_path = NULL;
245 struct got_pathlist_head have_refs;
246 struct got_pathlist_entry *pe;
247 struct got_reflist_head my_refs;
248 struct got_reflist_entry *re;
249 off_t packfile_size = 0;
250 struct got_packfile_hdr pack_hdr;
251 uint32_t nobj = 0;
252 char *ref_prefix = NULL;
253 size_t ref_prefixlen = 0;
254 char *path;
255 char *progress = NULL;
257 *pack_hash = NULL;
259 /*
260 * Prevent fetching of references that won't make any
261 * sense outside of the remote repository's context.
262 */
263 TAILQ_FOREACH(pe, wanted_refs, entry) {
264 const char *refname = pe->path;
265 if (strncmp(refname, "refs/got/", 9) == 0 ||
266 strncmp(refname, "got/", 4) == 0 ||
267 strncmp(refname, "refs/remotes/", 13) == 0 ||
268 strncmp(refname, "remotes/", 8) == 0)
269 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
272 if (!list_refs_only)
273 repo_path = got_repo_get_path_git_dir(repo);
275 for (i = 0; i < nitems(tmpfds); i++)
276 tmpfds[i] = -1;
278 TAILQ_INIT(&have_refs);
279 TAILQ_INIT(&my_refs);
281 if (!mirror_references) {
282 if (asprintf(&ref_prefix, "refs/remotes/%s/",
283 remote_name) == -1)
284 return got_error_from_errno("asprintf");
285 ref_prefixlen = strlen(ref_prefix);
288 if (!list_refs_only) {
289 err = got_ref_list(&my_refs, repo, NULL,
290 got_ref_cmp_by_name, NULL);
291 if (err)
292 goto done;
295 TAILQ_FOREACH(re, &my_refs, entry) {
296 struct got_object_id *id;
297 const char *refname;
299 if (got_ref_is_symbolic(re->ref))
300 continue;
302 refname = got_ref_get_name(re->ref);
304 if (mirror_references) {
305 char *name;
306 err = got_ref_resolve(&id, repo, re->ref);
307 if (err)
308 goto done;
309 name = strdup(refname);
310 if (name == NULL) {
311 err = got_error_from_errno("strdup");
312 goto done;
314 err = got_pathlist_append(&have_refs, name, id);
315 if (err)
316 goto done;
317 continue;
320 if (strncmp("refs/tags/", refname, 10) == 0) {
321 char *tagname;
323 err = got_ref_resolve(&id, repo, re->ref);
324 if (err)
325 goto done;
326 tagname = strdup(refname);
327 if (tagname == NULL) {
328 err = got_error_from_errno("strdup");
329 goto done;
331 err = got_pathlist_append(&have_refs, tagname, id);
332 if (err) {
333 free(tagname);
334 goto done;
338 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
339 char *branchname;
341 err = got_ref_resolve(&id, repo, re->ref);
342 if (err)
343 goto done;
345 if (asprintf(&branchname, "refs/heads/%s",
346 refname + ref_prefixlen) == -1) {
347 err = got_error_from_errno("asprintf");
348 goto done;
350 err = got_pathlist_append(&have_refs, branchname, id);
351 if (err) {
352 free(branchname);
353 goto done;
358 if (list_refs_only) {
359 packfd = got_opentempfd();
360 if (packfd == -1) {
361 err = got_error_from_errno("got_opentempfd");
362 goto done;
364 } else {
365 if (asprintf(&path, "%s/%s/fetching.pack",
366 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
367 err = got_error_from_errno("asprintf");
368 goto done;
370 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
371 free(path);
372 if (err)
373 goto done;
374 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
375 err = got_error_from_errno2("fchmod", tmppackpath);
376 goto done;
379 if (list_refs_only) {
380 idxfd = got_opentempfd();
381 if (idxfd == -1) {
382 err = got_error_from_errno("got_opentempfd");
383 goto done;
385 } else {
386 if (asprintf(&path, "%s/%s/fetching.idx",
387 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
388 err = got_error_from_errno("asprintf");
389 goto done;
391 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
392 free(path);
393 if (err)
394 goto done;
395 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
396 err = got_error_from_errno2("fchmod", tmpidxpath);
397 goto done;
400 nidxfd = dup(idxfd);
401 if (nidxfd == -1) {
402 err = got_error_from_errno("dup");
403 goto done;
406 for (i = 0; i < nitems(tmpfds); i++) {
407 tmpfds[i] = got_opentempfd();
408 if (tmpfds[i] == -1) {
409 err = got_error_from_errno("got_opentempfd");
410 goto done;
414 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
415 err = got_error_from_errno("socketpair");
416 goto done;
419 fetchpid = fork();
420 if (fetchpid == -1) {
421 err = got_error_from_errno("fork");
422 goto done;
423 } else if (fetchpid == 0){
424 got_privsep_exec_child(imsg_fetchfds,
425 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
428 if (close(imsg_fetchfds[1]) == -1) {
429 err = got_error_from_errno("close");
430 goto done;
432 imsg_init(&fetchibuf, imsg_fetchfds[0]);
433 nfetchfd = dup(fetchfd);
434 if (nfetchfd == -1) {
435 err = got_error_from_errno("dup");
436 goto done;
438 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
439 fetch_all_branches, wanted_branches, wanted_refs,
440 list_refs_only, verbosity);
441 if (err != NULL)
442 goto done;
443 nfetchfd = -1;
444 npackfd = dup(packfd);
445 if (npackfd == -1) {
446 err = got_error_from_errno("dup");
447 goto done;
449 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
450 if (err != NULL)
451 goto done;
452 npackfd = -1;
454 packfile_size = 0;
455 progress = calloc(GOT_FETCH_PKTMAX, 1);
456 if (progress == NULL) {
457 err = got_error_from_errno("calloc");
458 goto done;
461 *pack_hash = calloc(1, sizeof(**pack_hash));
462 if (*pack_hash == NULL) {
463 err = got_error_from_errno("calloc");
464 goto done;
467 while (!done) {
468 struct got_object_id *id = NULL;
469 char *refname = NULL;
470 char *server_progress = NULL;
471 off_t packfile_size_cur = 0;
473 err = got_privsep_recv_fetch_progress(&done,
474 &id, &refname, symrefs, &server_progress,
475 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
476 if (err != NULL)
477 goto done;
478 if (!done && refname && id) {
479 err = got_pathlist_insert(NULL, refs, refname, id);
480 if (err)
481 goto done;
482 } else if (!done && server_progress) {
483 char *p;
484 /*
485 * XXX git-daemon tends to send batched output with
486 * lines spanning separate packets. Buffer progress
487 * output until we see a CR or LF to avoid giving
488 * partial lines of progress output to the callback.
489 */
490 if (strlcat(progress, server_progress,
491 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
492 progress[0] = '\0'; /* discard */
493 continue;
495 while ((p = strchr(progress, '\r')) != NULL ||
496 (p = strchr(progress, '\n')) != NULL) {
497 char *s;
498 size_t n;
499 char c = *p;
500 *p = '\0';
501 if (asprintf(&s, "%s%s", progress,
502 c == '\n' ? "\n" : "") == -1) {
503 err = got_error_from_errno("asprintf");
504 goto done;
506 err = progress_cb(progress_arg, s,
507 packfile_size_cur, 0, 0, 0, 0);
508 free(s);
509 if (err)
510 break;
511 n = strlen(progress);
512 if (n < GOT_FETCH_PKTMAX - 1) {
513 memmove(progress, &progress[n + 1],
514 GOT_FETCH_PKTMAX - n - 1);
515 } else
516 progress[0] = '\0';
518 free(server_progress);
519 if (err)
520 goto done;
521 } else if (!done && packfile_size_cur != packfile_size) {
522 err = progress_cb(progress_arg, NULL,
523 packfile_size_cur, 0, 0, 0, 0);
524 if (err)
525 break;
526 packfile_size = packfile_size_cur;
529 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
530 err = got_error_from_errno("waitpid");
531 goto done;
534 if (lseek(packfd, 0, SEEK_SET) == -1) {
535 err = got_error_from_errno("lseek");
536 goto done;
539 /* If zero data was fetched without error we are already up-to-date. */
540 if (packfile_size == 0) {
541 free(*pack_hash);
542 *pack_hash = NULL;
543 goto done;
544 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
545 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
546 goto done;
547 } else {
548 ssize_t n;
550 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
551 if (n == -1) {
552 err = got_error_from_errno("read");
553 goto done;
555 if (n != ssizeof(pack_hdr)) {
556 err = got_error(GOT_ERR_IO);
557 goto done;
559 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
560 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
561 "bad pack file signature");
562 goto done;
564 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
565 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
566 "bad pack file version");
567 goto done;
569 nobj = be32toh(pack_hdr.nobjects);
570 if (nobj == 0 &&
571 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
572 return got_error_msg(GOT_ERR_BAD_PACKFILE,
573 "bad pack file with zero objects");
574 if (nobj != 0 &&
575 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
576 return got_error_msg(GOT_ERR_BAD_PACKFILE,
577 "empty pack file with non-zero object count");
580 /*
581 * If the pack file contains no objects, we may only need to update
582 * references in our repository. The caller will take care of that.
583 */
584 if (nobj == 0)
585 goto done;
587 if (lseek(packfd, 0, SEEK_SET) == -1) {
588 err = got_error_from_errno("lseek");
589 goto done;
592 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
593 err = got_error_from_errno("socketpair");
594 goto done;
596 idxpid = fork();
597 if (idxpid == -1) {
598 err= got_error_from_errno("fork");
599 goto done;
600 } else if (idxpid == 0)
601 got_privsep_exec_child(imsg_idxfds,
602 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
603 if (close(imsg_idxfds[1]) == -1) {
604 err = got_error_from_errno("close");
605 goto done;
607 imsg_init(&idxibuf, imsg_idxfds[0]);
609 npackfd = dup(packfd);
610 if (npackfd == -1) {
611 err = got_error_from_errno("dup");
612 goto done;
614 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
615 npackfd);
616 if (err != NULL)
617 goto done;
618 npackfd = -1;
619 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
620 if (err != NULL)
621 goto done;
622 nidxfd = -1;
623 for (i = 0; i < nitems(tmpfds); i++) {
624 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
625 if (err != NULL)
626 goto done;
627 tmpfds[i] = -1;
629 done = 0;
630 while (!done) {
631 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
633 err = got_privsep_recv_index_progress(&done, &nobj_total,
634 &nobj_indexed, &nobj_loose, &nobj_resolved,
635 &idxibuf);
636 if (err != NULL)
637 goto done;
638 if (nobj_indexed != 0) {
639 err = progress_cb(progress_arg, NULL,
640 packfile_size, nobj_total,
641 nobj_indexed, nobj_loose, nobj_resolved);
642 if (err)
643 break;
645 imsg_clear(&idxibuf);
647 if (close(imsg_idxfds[0]) == -1) {
648 err = got_error_from_errno("close");
649 goto done;
651 if (waitpid(idxpid, &idxstatus, 0) == -1) {
652 err = got_error_from_errno("waitpid");
653 goto done;
656 err = got_object_id_str(&id_str, *pack_hash);
657 if (err)
658 goto done;
659 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
660 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
666 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
667 err = got_error_from_errno("asprintf");
668 goto done;
671 if (rename(tmppackpath, packpath) == -1) {
672 err = got_error_from_errno3("rename", tmppackpath, packpath);
673 goto done;
675 free(tmppackpath);
676 tmppackpath = NULL;
677 if (rename(tmpidxpath, idxpath) == -1) {
678 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
679 goto done;
681 free(tmpidxpath);
682 tmpidxpath = NULL;
684 done:
685 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
686 err = got_error_from_errno2("unlink", tmppackpath);
687 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
688 err = got_error_from_errno2("unlink", tmpidxpath);
689 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
690 err = got_error_from_errno("close");
691 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
692 err = got_error_from_errno("close");
693 if (packfd != -1 && close(packfd) == -1 && err == NULL)
694 err = got_error_from_errno("close");
695 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
696 err = got_error_from_errno("close");
697 for (i = 0; i < nitems(tmpfds); i++) {
698 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
699 err = got_error_from_errno("close");
701 free(tmppackpath);
702 free(tmpidxpath);
703 free(idxpath);
704 free(packpath);
705 free(ref_prefix);
706 free(progress);
708 TAILQ_FOREACH(pe, &have_refs, entry) {
709 free((char *)pe->path);
710 free(pe->data);
712 got_pathlist_free(&have_refs);
713 got_ref_list_free(&my_refs);
714 if (err) {
715 free(*pack_hash);
716 *pack_hash = NULL;
717 TAILQ_FOREACH(pe, refs, entry) {
718 free((void *)pe->path);
719 free(pe->data);
721 got_pathlist_free(refs);
722 TAILQ_FOREACH(pe, symrefs, entry) {
723 free((void *)pe->path);
724 free(pe->data);
726 got_pathlist_free(symrefs);
728 return err;