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) {
132 if (close(pfd[1]) == -1)
134 if (dup2(pfd[0], 0) == -1)
136 if (dup2(pfd[0], 1) == -1)
138 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
139 if (n < 0 || n >= ssizeof(cmd))
141 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
143 abort(); /* not reached */
145 if (close(pfd[0]) == -1)
146 return got_error_from_errno("close");
153 static const struct got_error *
154 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
155 const char *direction)
157 const struct got_error *err = NULL;
158 struct addrinfo hints, *servinfo, *p;
160 int fd = -1, len, r, eaicode;
165 port = GOT_DEFAULT_GIT_PORT_STR;
167 memset(&hints, 0, sizeof hints);
168 hints.ai_family = AF_UNSPEC;
169 hints.ai_socktype = SOCK_STREAM;
170 eaicode = getaddrinfo(host, port, &hints, &servinfo);
173 snprintf(msg, sizeof(msg), "%s: %s", host,
174 gai_strerror(eaicode));
175 return got_error_msg(GOT_ERR_ADDRINFO, msg);
178 for (p = servinfo; p != NULL; p = p->ai_next) {
179 if ((fd = socket(p->ai_family, p->ai_socktype,
180 p->ai_protocol)) == -1)
182 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
186 err = got_error_from_errno("connect");
192 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
193 err = got_error_from_errno("asprintf");
196 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
197 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
199 err = got_error_from_errno("dprintf");
210 const struct got_error *
211 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
212 const char *host, const char *port, const char *server_path, int verbosity)
214 const struct got_error *err = NULL;
219 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
220 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
221 "upload", verbosity);
222 else if (strcmp(proto, "git") == 0)
223 err = dial_git(fetchfd, host, port, server_path, "upload");
224 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
225 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
227 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
231 const struct got_error *
232 got_fetch_parse_uri(char **proto, char **host, char **port,
233 char **server_path, char **repo_name, const char *uri)
235 const struct got_error *err = NULL;
239 *proto = *host = *port = *server_path = *repo_name = NULL;
241 p = strstr(uri, "://");
243 /* Try parsing Git's "scp" style URL syntax. */
244 *proto = strdup("ssh");
246 err = got_error_from_errno("strdup");
252 err = got_error(GOT_ERR_PARSE_URI);
255 /* No slashes allowed before first colon. */
258 err = got_error(GOT_ERR_PARSE_URI);
261 *host = strndup(s, q - s);
263 err = got_error_from_errno("strndup");
268 *proto = strndup(uri, p - uri);
270 err = got_error_from_errno("strndup");
276 if (p == NULL || strlen(p) == 1) {
277 err = got_error(GOT_ERR_PARSE_URI);
281 q = memchr(s, ':', p - s);
283 *host = strndup(s, q - s);
285 err = got_error_from_errno("strndup");
288 *port = strndup(q + 1, p - (q + 1));
290 err = got_error_from_errno("strndup");
294 *host = strndup(s, p - s);
296 err = got_error_from_errno("strndup");
302 while (p[0] == '/' && p[1] == '/')
304 *server_path = strdup(p);
305 if (*server_path == NULL) {
306 err = got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(*server_path);
312 if (!p || strlen(p) <= 1) {
313 err = got_error(GOT_ERR_PARSE_URI);
319 err = got_error(GOT_ERR_PARSE_URI);
322 if (hassuffix(p, ".git"))
324 *repo_name = strndup(p, (p + n) - p);
325 if (*repo_name == NULL) {
326 err = got_error_from_errno("strndup");
345 const struct got_error*
346 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
347 struct got_pathlist_head *symrefs, const char *remote_name,
348 int mirror_references, int fetch_all_branches,
349 struct got_pathlist_head *wanted_branches,
350 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
351 int fetchfd, struct got_repository *repo,
352 got_fetch_progress_cb progress_cb, void *progress_arg)
355 int imsg_fetchfds[2], imsg_idxfds[2];
356 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
358 int fetchstatus, idxstatus, done = 0;
359 const struct got_error *err;
360 struct imsgbuf fetchibuf, idxibuf;
361 pid_t fetchpid, idxpid;
362 char *tmppackpath = NULL, *tmpidxpath = NULL;
363 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
364 const char *repo_path = NULL;
365 struct got_pathlist_head have_refs;
366 struct got_pathlist_entry *pe;
367 struct got_reflist_head my_refs;
368 struct got_reflist_entry *re;
369 off_t packfile_size = 0;
370 struct got_packfile_hdr pack_hdr;
372 char *ref_prefix = NULL;
373 size_t ref_prefixlen = 0;
375 char *progress = NULL;
380 * Prevent fetching of references that won't make any
381 * sense outside of the remote repository's context.
383 TAILQ_FOREACH(pe, wanted_refs, entry) {
384 const char *refname = pe->path;
385 if (strncmp(refname, "refs/got/", 9) == 0 ||
386 strncmp(refname, "got/", 4) == 0 ||
387 strncmp(refname, "refs/remotes/", 13) == 0 ||
388 strncmp(refname, "remotes/", 8) == 0)
389 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
393 repo_path = got_repo_get_path_git_dir(repo);
395 for (i = 0; i < nitems(tmpfds); i++)
398 TAILQ_INIT(&have_refs);
399 TAILQ_INIT(&my_refs);
401 if (!mirror_references) {
402 if (asprintf(&ref_prefix, "refs/remotes/%s/",
404 return got_error_from_errno("asprintf");
405 ref_prefixlen = strlen(ref_prefix);
408 if (!list_refs_only) {
409 err = got_ref_list(&my_refs, repo, NULL,
410 got_ref_cmp_by_name, NULL);
415 TAILQ_FOREACH(re, &my_refs, entry) {
416 struct got_object_id *id;
419 if (got_ref_is_symbolic(re->ref))
422 refname = got_ref_get_name(re->ref);
424 if (mirror_references) {
426 err = got_ref_resolve(&id, repo, re->ref);
429 name = strdup(refname);
431 err = got_error_from_errno("strdup");
434 err = got_pathlist_append(&have_refs, name, id);
440 if (strncmp("refs/tags/", refname, 10) == 0) {
443 err = got_ref_resolve(&id, repo, re->ref);
446 tagname = strdup(refname);
447 if (tagname == NULL) {
448 err = got_error_from_errno("strdup");
451 err = got_pathlist_append(&have_refs, tagname, id);
458 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
461 err = got_ref_resolve(&id, repo, re->ref);
465 if (asprintf(&branchname, "refs/heads/%s",
466 refname + ref_prefixlen) == -1) {
467 err = got_error_from_errno("asprintf");
470 err = got_pathlist_append(&have_refs, branchname, id);
478 if (list_refs_only) {
479 packfd = got_opentempfd();
481 err = got_error_from_errno("got_opentempfd");
485 if (asprintf(&path, "%s/%s/fetching.pack",
486 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
487 err = got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
494 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
495 err = got_error_from_errno2("fchmod", tmppackpath);
499 if (list_refs_only) {
500 idxfd = got_opentempfd();
502 err = got_error_from_errno("got_opentempfd");
506 if (asprintf(&path, "%s/%s/fetching.idx",
507 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
508 err = got_error_from_errno("asprintf");
511 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
515 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
516 err = got_error_from_errno2("fchmod", tmpidxpath);
522 err = got_error_from_errno("dup");
526 for (i = 0; i < nitems(tmpfds); i++) {
527 tmpfds[i] = got_opentempfd();
528 if (tmpfds[i] == -1) {
529 err = got_error_from_errno("got_opentempfd");
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
535 err = got_error_from_errno("socketpair");
540 if (fetchpid == -1) {
541 err = got_error_from_errno("fork");
543 } else if (fetchpid == 0){
544 got_privsep_exec_child(imsg_fetchfds,
545 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
548 if (close(imsg_fetchfds[1]) == -1) {
549 err = got_error_from_errno("close");
552 imsg_init(&fetchibuf, imsg_fetchfds[0]);
553 nfetchfd = dup(fetchfd);
554 if (nfetchfd == -1) {
555 err = got_error_from_errno("dup");
558 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
559 fetch_all_branches, wanted_branches, wanted_refs,
560 list_refs_only, verbosity);
564 npackfd = dup(packfd);
566 err = got_error_from_errno("dup");
569 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
575 progress = calloc(GOT_FETCH_PKTMAX, 1);
576 if (progress == NULL) {
577 err = got_error_from_errno("calloc");
581 *pack_hash = calloc(1, sizeof(**pack_hash));
582 if (*pack_hash == NULL) {
583 err = got_error_from_errno("calloc");
588 struct got_object_id *id = NULL;
589 char *refname = NULL;
590 char *server_progress = NULL;
591 off_t packfile_size_cur = 0;
593 err = got_privsep_recv_fetch_progress(&done,
594 &id, &refname, symrefs, &server_progress,
595 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
598 if (!done && refname && id) {
599 err = got_pathlist_insert(NULL, refs, refname, id);
602 } else if (!done && server_progress) {
605 * XXX git-daemon tends to send batched output with
606 * lines spanning separate packets. Buffer progress
607 * output until we see a CR or LF to avoid giving
608 * partial lines of progress output to the callback.
610 if (strlcat(progress, server_progress,
611 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
612 progress[0] = '\0'; /* discard */
615 while ((p = strchr(progress, '\r')) != NULL ||
616 (p = strchr(progress, '\n')) != NULL) {
621 if (asprintf(&s, "%s%s", progress,
622 c == '\n' ? "\n" : "") == -1) {
623 err = got_error_from_errno("asprintf");
626 err = progress_cb(progress_arg, s,
627 packfile_size_cur, 0, 0, 0, 0);
631 n = strlen(progress);
632 if (n < GOT_FETCH_PKTMAX - 1) {
633 memmove(progress, &progress[n + 1],
634 GOT_FETCH_PKTMAX - n - 1);
638 free(server_progress);
641 } else if (!done && packfile_size_cur != packfile_size) {
642 err = progress_cb(progress_arg, NULL,
643 packfile_size_cur, 0, 0, 0, 0);
646 packfile_size = packfile_size_cur;
649 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
650 err = got_error_from_errno("waitpid");
654 if (lseek(packfd, 0, SEEK_SET) == -1) {
655 err = got_error_from_errno("lseek");
659 /* If zero data was fetched without error we are already up-to-date. */
660 if (packfile_size == 0) {
664 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
665 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
670 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
672 err = got_error_from_errno("read");
675 if (n != ssizeof(pack_hdr)) {
676 err = got_error(GOT_ERR_IO);
679 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
680 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
681 "bad pack file signature");
684 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
685 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
686 "bad pack file version");
689 nobj = be32toh(pack_hdr.nobjects);
691 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
692 return got_error_msg(GOT_ERR_BAD_PACKFILE,
693 "bad pack file with zero objects");
695 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
696 return got_error_msg(GOT_ERR_BAD_PACKFILE,
697 "empty pack file with non-zero object count");
701 * If the pack file contains no objects, we may only need to update
702 * references in our repository. The caller will take care of that.
707 if (lseek(packfd, 0, SEEK_SET) == -1) {
708 err = got_error_from_errno("lseek");
712 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
713 err = got_error_from_errno("socketpair");
718 err= got_error_from_errno("fork");
720 } else if (idxpid == 0)
721 got_privsep_exec_child(imsg_idxfds,
722 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
723 if (close(imsg_idxfds[1]) == -1) {
724 err = got_error_from_errno("close");
727 imsg_init(&idxibuf, imsg_idxfds[0]);
729 npackfd = dup(packfd);
731 err = got_error_from_errno("dup");
734 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
739 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
743 for (i = 0; i < nitems(tmpfds); i++) {
744 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
751 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
753 err = got_privsep_recv_index_progress(&done, &nobj_total,
754 &nobj_indexed, &nobj_loose, &nobj_resolved,
758 if (nobj_indexed != 0) {
759 err = progress_cb(progress_arg, NULL,
760 packfile_size, nobj_total,
761 nobj_indexed, nobj_loose, nobj_resolved);
765 imsg_clear(&idxibuf);
767 if (close(imsg_idxfds[0]) == -1) {
768 err = got_error_from_errno("close");
771 if (waitpid(idxpid, &idxstatus, 0) == -1) {
772 err = got_error_from_errno("waitpid");
776 err = got_object_id_str(&id_str, *pack_hash);
779 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
780 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
781 err = got_error_from_errno("asprintf");
785 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
786 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
787 err = got_error_from_errno("asprintf");
791 if (rename(tmppackpath, packpath) == -1) {
792 err = got_error_from_errno3("rename", tmppackpath, packpath);
797 if (rename(tmpidxpath, idxpath) == -1) {
798 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
805 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
806 err = got_error_from_errno2("unlink", tmppackpath);
807 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
808 err = got_error_from_errno2("unlink", tmpidxpath);
809 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
810 err = got_error_from_errno("close");
811 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
812 err = got_error_from_errno("close");
813 if (packfd != -1 && close(packfd) == -1 && err == NULL)
814 err = got_error_from_errno("close");
815 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 for (i = 0; i < nitems(tmpfds); i++) {
818 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
819 err = got_error_from_errno("close");
828 TAILQ_FOREACH(pe, &have_refs, entry) {
829 free((char *)pe->path);
832 got_pathlist_free(&have_refs);
833 got_ref_list_free(&my_refs);
837 TAILQ_FOREACH(pe, refs, entry) {
838 free((void *)pe->path);
841 got_pathlist_free(refs);
842 TAILQ_FOREACH(pe, symrefs, entry) {
843 free((void *)pe->path);
846 got_pathlist_free(symrefs);