2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.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"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
74 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
78 hassuffix(char *base, char *suf)
84 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
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)
93 const struct got_error *err = NULL;
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);
107 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
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;
119 *proto = *host = *port = *server_path = *repo_name = NULL;
121 p = strstr(uri, "://");
123 /* Try parsing Git's "scp" style URL syntax. */
124 *proto = strdup("ssh");
126 err = got_error_from_errno("strdup");
132 err = got_error(GOT_ERR_PARSE_URI);
135 /* No slashes allowed before first colon. */
138 err = got_error(GOT_ERR_PARSE_URI);
141 *host = strndup(s, q - s);
143 err = got_error_from_errno("strndup");
148 *proto = strndup(uri, p - uri);
150 err = got_error_from_errno("strndup");
156 if (p == NULL || strlen(p) == 1) {
157 err = got_error(GOT_ERR_PARSE_URI);
161 q = memchr(s, ':', p - s);
163 *host = strndup(s, q - s);
165 err = got_error_from_errno("strndup");
168 *port = strndup(q + 1, p - (q + 1));
170 err = got_error_from_errno("strndup");
174 *host = strndup(s, p - s);
176 err = got_error_from_errno("strndup");
182 while (p[0] == '/' && p[1] == '/')
184 *server_path = strdup(p);
185 if (*server_path == NULL) {
186 err = got_error_from_errno("strdup");
189 got_path_strip_trailing_slashes(*server_path);
192 if (!p || strlen(p) <= 1) {
193 err = got_error(GOT_ERR_PARSE_URI);
199 err = got_error(GOT_ERR_PARSE_URI);
202 if (hassuffix(p, ".git"))
204 *repo_name = strndup(p, (p + n) - p);
205 if (*repo_name == NULL) {
206 err = got_error_from_errno("strndup");
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)
235 int imsg_fetchfds[2], imsg_idxfds[2];
236 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
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;
252 char *ref_prefix = NULL;
253 size_t ref_prefixlen = 0;
255 char *progress = NULL;
260 * Prevent fetching of references that won't make any
261 * sense outside of the remote repository's context.
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);
273 repo_path = got_repo_get_path_git_dir(repo);
275 for (i = 0; i < nitems(tmpfds); i++)
278 TAILQ_INIT(&have_refs);
279 TAILQ_INIT(&my_refs);
281 if (!mirror_references) {
282 if (asprintf(&ref_prefix, "refs/remotes/%s/",
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);
295 TAILQ_FOREACH(re, &my_refs, entry) {
296 struct got_object_id *id;
299 if (got_ref_is_symbolic(re->ref))
302 refname = got_ref_get_name(re->ref);
304 if (mirror_references) {
306 err = got_ref_resolve(&id, repo, re->ref);
309 name = strdup(refname);
311 err = got_error_from_errno("strdup");
314 err = got_pathlist_append(&have_refs, name, id);
320 if (strncmp("refs/tags/", refname, 10) == 0) {
323 err = got_ref_resolve(&id, repo, re->ref);
326 tagname = strdup(refname);
327 if (tagname == NULL) {
328 err = got_error_from_errno("strdup");
331 err = got_pathlist_append(&have_refs, tagname, id);
338 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
341 err = got_ref_resolve(&id, repo, re->ref);
345 if (asprintf(&branchname, "refs/heads/%s",
346 refname + ref_prefixlen) == -1) {
347 err = got_error_from_errno("asprintf");
350 err = got_pathlist_append(&have_refs, branchname, id);
358 if (list_refs_only) {
359 packfd = got_opentempfd();
361 err = got_error_from_errno("got_opentempfd");
365 if (asprintf(&path, "%s/%s/fetching.pack",
366 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
367 err = got_error_from_errno("asprintf");
370 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
374 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
375 err = got_error_from_errno2("fchmod", tmppackpath);
379 if (list_refs_only) {
380 idxfd = got_opentempfd();
382 err = got_error_from_errno("got_opentempfd");
386 if (asprintf(&path, "%s/%s/fetching.idx",
387 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
388 err = got_error_from_errno("asprintf");
391 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
395 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
396 err = got_error_from_errno2("fchmod", tmpidxpath);
402 err = got_error_from_errno("dup");
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");
414 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
415 err = got_error_from_errno("socketpair");
420 if (fetchpid == -1) {
421 err = got_error_from_errno("fork");
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");
432 imsg_init(&fetchibuf, imsg_fetchfds[0]);
433 nfetchfd = dup(fetchfd);
434 if (nfetchfd == -1) {
435 err = got_error_from_errno("dup");
438 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
439 fetch_all_branches, wanted_branches, wanted_refs,
440 list_refs_only, verbosity);
444 npackfd = dup(packfd);
446 err = got_error_from_errno("dup");
449 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
455 progress = calloc(GOT_FETCH_PKTMAX, 1);
456 if (progress == NULL) {
457 err = got_error_from_errno("calloc");
461 *pack_hash = calloc(1, sizeof(**pack_hash));
462 if (*pack_hash == NULL) {
463 err = got_error_from_errno("calloc");
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);
478 if (!done && refname && id) {
479 err = got_pathlist_insert(NULL, refs, refname, id);
482 } else if (!done && server_progress) {
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.
490 if (strlcat(progress, server_progress,
491 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
492 progress[0] = '\0'; /* discard */
495 while ((p = strchr(progress, '\r')) != NULL ||
496 (p = strchr(progress, '\n')) != NULL) {
501 if (asprintf(&s, "%s%s", progress,
502 c == '\n' ? "\n" : "") == -1) {
503 err = got_error_from_errno("asprintf");
506 err = progress_cb(progress_arg, s,
507 packfile_size_cur, 0, 0, 0, 0);
511 n = strlen(progress);
512 if (n < GOT_FETCH_PKTMAX - 1) {
513 memmove(progress, &progress[n + 1],
514 GOT_FETCH_PKTMAX - n - 1);
518 free(server_progress);
521 } else if (!done && packfile_size_cur != packfile_size) {
522 err = progress_cb(progress_arg, NULL,
523 packfile_size_cur, 0, 0, 0, 0);
526 packfile_size = packfile_size_cur;
529 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
530 err = got_error_from_errno("waitpid");
534 if (lseek(packfd, 0, SEEK_SET) == -1) {
535 err = got_error_from_errno("lseek");
539 /* If zero data was fetched without error we are already up-to-date. */
540 if (packfile_size == 0) {
544 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
545 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
550 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
552 err = got_error_from_errno("read");
555 if (n != ssizeof(pack_hdr)) {
556 err = got_error(GOT_ERR_IO);
559 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
560 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
561 "bad pack file signature");
564 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
565 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
566 "bad pack file version");
569 nobj = be32toh(pack_hdr.nobjects);
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");
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");
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.
587 if (lseek(packfd, 0, SEEK_SET) == -1) {
588 err = got_error_from_errno("lseek");
592 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
593 err = got_error_from_errno("socketpair");
598 err= got_error_from_errno("fork");
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");
607 imsg_init(&idxibuf, imsg_idxfds[0]);
609 npackfd = dup(packfd);
611 err = got_error_from_errno("dup");
614 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
619 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
623 for (i = 0; i < nitems(tmpfds); i++) {
624 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
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,
638 if (nobj_indexed != 0) {
639 err = progress_cb(progress_arg, NULL,
640 packfile_size, nobj_total,
641 nobj_indexed, nobj_loose, nobj_resolved);
645 imsg_clear(&idxibuf);
647 if (close(imsg_idxfds[0]) == -1) {
648 err = got_error_from_errno("close");
651 if (waitpid(idxpid, &idxstatus, 0) == -1) {
652 err = got_error_from_errno("waitpid");
656 err = got_object_id_str(&id_str, *pack_hash);
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");
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");
671 if (rename(tmppackpath, packpath) == -1) {
672 err = got_error_from_errno3("rename", tmppackpath, packpath);
677 if (rename(tmpidxpath, idxpath) == -1) {
678 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
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");
708 TAILQ_FOREACH(pe, &have_refs, entry) {
709 free((char *)pe->path);
712 got_pathlist_free(&have_refs);
713 got_ref_list_free(&my_refs);
717 TAILQ_FOREACH(pe, refs, entry) {
718 free((void *)pe->path);
721 got_pathlist_free(refs);
722 TAILQ_FOREACH(pe, symrefs, entry) {
723 free((void *)pe->path);
726 got_pathlist_free(symrefs);