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 <netinet/in.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_object.h"
52 #include "got_opentemp.h"
53 #include "got_fetch.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
75 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
79 hassuffix(char *base, char *suf)
85 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
90 static const struct got_error *
91 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
92 const char *path, const char *direction, int verbosity)
94 const struct got_error *error = NULL;
103 argv[i++] = GOT_FETCH_PATH_SSH;
106 argv[i++] = (char *)port;
108 if (verbosity == -1) {
111 /* ssh(1) allows up to 3 "-v" options. */
112 for (j = 0; j < MIN(3, verbosity); j++)
116 argv[i++] = (char *)host;
117 argv[i++] = (char *)cmd;
118 argv[i++] = (char *)path;
122 return got_error_from_errno("pipe");
126 error = got_error_from_errno("fork");
130 } else if (pid == 0) {
135 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
136 if (n < 0 || n >= ssizeof(cmd))
138 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
140 abort(); /* not reached */
149 static const struct got_error *
150 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
151 const char *direction)
153 const struct got_error *err = NULL;
154 struct addrinfo hints, *servinfo, *p;
155 char *cmd = NULL, *pkt = NULL;
156 int fd = -1, totlen, r, eaicode;
161 port = GOT_DEFAULT_GIT_PORT_STR;
163 memset(&hints, 0, sizeof hints);
164 hints.ai_family = AF_UNSPEC;
165 hints.ai_socktype = SOCK_STREAM;
166 eaicode = getaddrinfo(host, port, &hints, &servinfo);
169 snprintf(msg, sizeof(msg), "%s: %s", host,
170 gai_strerror(eaicode));
171 return got_error_msg(GOT_ERR_ADDRINFO, msg);
174 for (p = servinfo; p != NULL; p = p->ai_next) {
175 if ((fd = socket(p->ai_family, p->ai_socktype,
176 p->ai_protocol)) == -1)
178 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
182 err = got_error_from_errno("connect");
188 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
189 err = got_error_from_errno("asprintf");
192 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
193 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
194 err = got_error_from_errno("asprintf");
197 r = write(fd, pkt, strlen(pkt) + 1);
199 err = got_error_from_errno("write");
202 if (asprintf(&pkt, "host=%s", host) == -1) {
203 err = got_error_from_errno("asprintf");
206 r = write(fd, pkt, strlen(pkt) + 1);
208 err = got_error_from_errno("write");
222 const struct got_error *
223 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
224 const char *host, const char *port, const char *server_path, int verbosity)
226 const struct got_error *err = NULL;
231 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
232 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
233 "upload", verbosity);
234 else if (strcmp(proto, "git") == 0)
235 err = dial_git(fetchfd, host, port, server_path, "upload");
236 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
237 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
239 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
243 const struct got_error *
244 got_fetch_parse_uri(char **proto, char **host, char **port,
245 char **server_path, char **repo_name, const char *uri)
247 const struct got_error *err = NULL;
251 *proto = *host = *port = *server_path = *repo_name = NULL;
253 p = strstr(uri, "://");
255 /* Try parsing Git's "scp" style URL syntax. */
256 *proto = strdup("ssh");
258 err = got_error_from_errno("strdup");
264 err = got_error(GOT_ERR_PARSE_URI);
267 /* No slashes allowed before first colon. */
270 err = got_error(GOT_ERR_PARSE_URI);
273 *host = strndup(s, q - s);
275 err = got_error_from_errno("strndup");
280 *proto = strndup(uri, p - uri);
282 err = got_error_from_errno("strndup");
288 if (p == NULL || strlen(p) == 1) {
289 err = got_error(GOT_ERR_PARSE_URI);
293 q = memchr(s, ':', p - s);
295 *host = strndup(s, q - s);
297 err = got_error_from_errno("strndup");
300 *port = strndup(q + 1, p - (q + 1));
302 err = got_error_from_errno("strndup");
306 *host = strndup(s, p - s);
308 err = got_error_from_errno("strndup");
314 while (p[0] == '/' && p[1] == '/')
316 *server_path = strdup(p);
317 if (*server_path == NULL) {
318 err = got_error_from_errno("strdup");
321 got_path_strip_trailing_slashes(*server_path);
324 if (!p || strlen(p) <= 1) {
325 err = got_error(GOT_ERR_PARSE_URI);
331 err = got_error(GOT_ERR_PARSE_URI);
334 if (hassuffix(p, ".git"))
336 *repo_name = strndup(p, (p + n) - p);
337 if (*repo_name == NULL) {
338 err = got_error_from_errno("strndup");
357 const struct got_error*
358 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
359 struct got_pathlist_head *symrefs, const char *remote_name,
360 int mirror_references, int fetch_all_branches,
361 struct got_pathlist_head *wanted_branches,
362 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
363 int fetchfd, struct got_repository *repo,
364 got_fetch_progress_cb progress_cb, void *progress_arg)
367 int imsg_fetchfds[2], imsg_idxfds[2];
368 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
370 int fetchstatus, idxstatus, done = 0;
371 const struct got_error *err;
372 struct imsgbuf fetchibuf, idxibuf;
373 pid_t fetchpid, idxpid;
374 char *tmppackpath = NULL, *tmpidxpath = NULL;
375 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
376 const char *repo_path = NULL;
377 struct got_pathlist_head have_refs;
378 struct got_pathlist_entry *pe;
379 struct got_reflist_head my_refs;
380 struct got_reflist_entry *re;
381 off_t packfile_size = 0;
382 struct got_packfile_hdr pack_hdr;
384 char *ref_prefix = NULL;
385 size_t ref_prefixlen = 0;
387 char *progress = NULL;
392 * Prevent fetching of references that won't make any
393 * sense outside of the remote repository's context.
395 TAILQ_FOREACH(pe, wanted_refs, entry) {
396 const char *refname = pe->path;
397 if (strncmp(refname, "refs/got/", 9) == 0 ||
398 strncmp(refname, "got/", 4) == 0 ||
399 strncmp(refname, "refs/remotes/", 13) == 0 ||
400 strncmp(refname, "remotes/", 8) == 0)
401 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
405 repo_path = got_repo_get_path_git_dir(repo);
407 for (i = 0; i < nitems(tmpfds); i++)
410 TAILQ_INIT(&have_refs);
411 TAILQ_INIT(&my_refs);
413 if (!mirror_references) {
414 if (asprintf(&ref_prefix, "refs/remotes/%s/",
416 return got_error_from_errno("asprintf");
417 ref_prefixlen = strlen(ref_prefix);
420 if (!list_refs_only) {
421 err = got_ref_list(&my_refs, repo, NULL,
422 got_ref_cmp_by_name, NULL);
427 TAILQ_FOREACH(re, &my_refs, entry) {
428 struct got_object_id *id;
431 if (got_ref_is_symbolic(re->ref))
434 refname = got_ref_get_name(re->ref);
436 if (mirror_references) {
438 err = got_ref_resolve(&id, repo, re->ref);
441 name = strdup(refname);
443 err = got_error_from_errno("strdup");
446 err = got_pathlist_append(&have_refs, name, id);
452 if (strncmp("refs/tags/", refname, 10) == 0) {
455 err = got_ref_resolve(&id, repo, re->ref);
458 tagname = strdup(refname);
459 if (tagname == NULL) {
460 err = got_error_from_errno("strdup");
463 err = got_pathlist_append(&have_refs, tagname, id);
470 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
473 err = got_ref_resolve(&id, repo, re->ref);
477 if (asprintf(&branchname, "refs/heads/%s",
478 refname + ref_prefixlen) == -1) {
479 err = got_error_from_errno("asprintf");
482 err = got_pathlist_append(&have_refs, branchname, id);
490 if (list_refs_only) {
491 packfd = got_opentempfd();
493 err = got_error_from_errno("got_opentempfd");
497 if (asprintf(&path, "%s/%s/fetching.pack",
498 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
499 err = got_error_from_errno("asprintf");
502 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
506 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
507 err = got_error_from_errno2("fchmod", tmppackpath);
511 if (list_refs_only) {
512 idxfd = got_opentempfd();
514 err = got_error_from_errno("got_opentempfd");
518 if (asprintf(&path, "%s/%s/fetching.idx",
519 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
523 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
527 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
528 err = got_error_from_errno2("fchmod", tmpidxpath);
534 err = got_error_from_errno("dup");
538 for (i = 0; i < nitems(tmpfds); i++) {
539 tmpfds[i] = got_opentempfd();
540 if (tmpfds[i] == -1) {
541 err = got_error_from_errno("got_opentempfd");
546 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
547 err = got_error_from_errno("socketpair");
552 if (fetchpid == -1) {
553 err = got_error_from_errno("fork");
555 } else if (fetchpid == 0){
556 got_privsep_exec_child(imsg_fetchfds,
557 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
560 if (close(imsg_fetchfds[1]) == -1) {
561 err = got_error_from_errno("close");
564 imsg_init(&fetchibuf, imsg_fetchfds[0]);
565 nfetchfd = dup(fetchfd);
566 if (nfetchfd == -1) {
567 err = got_error_from_errno("dup");
570 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
571 fetch_all_branches, wanted_branches, wanted_refs,
572 list_refs_only, verbosity);
576 npackfd = dup(packfd);
578 err = got_error_from_errno("dup");
581 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
587 progress = calloc(GOT_FETCH_PKTMAX, 1);
588 if (progress == NULL) {
589 err = got_error_from_errno("calloc");
593 *pack_hash = calloc(1, sizeof(**pack_hash));
594 if (*pack_hash == NULL) {
595 err = got_error_from_errno("calloc");
600 struct got_object_id *id = NULL;
601 char *refname = NULL;
602 char *server_progress = NULL;
603 off_t packfile_size_cur = 0;
605 err = got_privsep_recv_fetch_progress(&done,
606 &id, &refname, symrefs, &server_progress,
607 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
610 if (!done && refname && id) {
611 err = got_pathlist_insert(NULL, refs, refname, id);
614 } else if (!done && server_progress) {
617 * XXX git-daemon tends to send batched output with
618 * lines spanning separate packets. Buffer progress
619 * output until we see a CR or LF to avoid giving
620 * partial lines of progress output to the callback.
622 if (strlcat(progress, server_progress,
623 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
624 progress[0] = '\0'; /* discard */
627 while ((p = strchr(progress, '\r')) != NULL ||
628 (p = strchr(progress, '\n')) != NULL) {
633 if (asprintf(&s, "%s%s", progress,
634 c == '\n' ? "\n" : "") == -1) {
635 err = got_error_from_errno("asprintf");
638 err = progress_cb(progress_arg, s,
639 packfile_size_cur, 0, 0, 0, 0);
643 n = strlen(progress);
644 if (n < GOT_FETCH_PKTMAX - 1) {
645 memmove(progress, &progress[n + 1],
646 GOT_FETCH_PKTMAX - n - 1);
650 free(server_progress);
653 } else if (!done && packfile_size_cur != packfile_size) {
654 err = progress_cb(progress_arg, NULL,
655 packfile_size_cur, 0, 0, 0, 0);
658 packfile_size = packfile_size_cur;
661 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
662 err = got_error_from_errno("waitpid");
666 if (lseek(packfd, 0, SEEK_SET) == -1) {
667 err = got_error_from_errno("lseek");
671 /* If zero data was fetched without error we are already up-to-date. */
672 if (packfile_size == 0) {
676 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
677 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
682 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
684 err = got_error_from_errno("read");
687 if (n != ssizeof(pack_hdr)) {
688 err = got_error(GOT_ERR_IO);
691 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
692 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
693 "bad pack file signature");
696 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
697 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
698 "bad pack file version");
701 nobj = be32toh(pack_hdr.nobjects);
703 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
704 return got_error_msg(GOT_ERR_BAD_PACKFILE,
705 "bad pack file with zero objects");
707 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
708 return got_error_msg(GOT_ERR_BAD_PACKFILE,
709 "empty pack file with non-zero object count");
713 * If the pack file contains no objects, we may only need to update
714 * references in our repository. The caller will take care of that.
719 if (lseek(packfd, 0, SEEK_SET) == -1) {
720 err = got_error_from_errno("lseek");
724 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
725 err = got_error_from_errno("socketpair");
730 err= got_error_from_errno("fork");
732 } else if (idxpid == 0)
733 got_privsep_exec_child(imsg_idxfds,
734 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
735 if (close(imsg_idxfds[1]) == -1) {
736 err = got_error_from_errno("close");
739 imsg_init(&idxibuf, imsg_idxfds[0]);
741 npackfd = dup(packfd);
743 err = got_error_from_errno("dup");
746 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
751 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
755 for (i = 0; i < nitems(tmpfds); i++) {
756 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
763 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
765 err = got_privsep_recv_index_progress(&done, &nobj_total,
766 &nobj_indexed, &nobj_loose, &nobj_resolved,
770 if (nobj_indexed != 0) {
771 err = progress_cb(progress_arg, NULL,
772 packfile_size, nobj_total,
773 nobj_indexed, nobj_loose, nobj_resolved);
777 imsg_clear(&idxibuf);
779 if (close(imsg_idxfds[0]) == -1) {
780 err = got_error_from_errno("close");
783 if (waitpid(idxpid, &idxstatus, 0) == -1) {
784 err = got_error_from_errno("waitpid");
788 err = got_object_id_str(&id_str, *pack_hash);
791 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
792 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
793 err = got_error_from_errno("asprintf");
797 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
798 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
799 err = got_error_from_errno("asprintf");
803 if (rename(tmppackpath, packpath) == -1) {
804 err = got_error_from_errno3("rename", tmppackpath, packpath);
809 if (rename(tmpidxpath, idxpath) == -1) {
810 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
817 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
818 err = got_error_from_errno2("unlink", tmppackpath);
819 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
820 err = got_error_from_errno2("unlink", tmpidxpath);
821 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
822 err = got_error_from_errno("close");
823 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
825 if (packfd != -1 && close(packfd) == -1 && err == NULL)
826 err = got_error_from_errno("close");
827 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
828 err = got_error_from_errno("close");
829 for (i = 0; i < nitems(tmpfds); i++) {
830 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
831 err = got_error_from_errno("close");
840 TAILQ_FOREACH(pe, &have_refs, entry) {
841 free((char *)pe->path);
844 got_pathlist_free(&have_refs);
845 got_ref_list_free(&my_refs);
849 TAILQ_FOREACH(pe, refs, entry) {
850 free((void *)pe->path);
853 got_pathlist_free(refs);
854 TAILQ_FOREACH(pe, symrefs, entry) {
855 free((void *)pe->path);
858 got_pathlist_free(symrefs);