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;
159 char *cmd = NULL, *pkt = NULL;
160 int fd = -1, totlen, 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 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
197 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
198 err = got_error_from_errno("asprintf");
201 r = write(fd, pkt, strlen(pkt) + 1);
203 err = got_error_from_errno("write");
206 if (asprintf(&pkt, "host=%s", host) == -1) {
207 err = got_error_from_errno("asprintf");
210 r = write(fd, pkt, strlen(pkt) + 1);
212 err = got_error_from_errno("write");
226 const struct got_error *
227 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
228 const char *host, const char *port, const char *server_path, int verbosity)
230 const struct got_error *err = NULL;
235 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
236 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
237 "upload", verbosity);
238 else if (strcmp(proto, "git") == 0)
239 err = dial_git(fetchfd, host, port, server_path, "upload");
240 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
241 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
243 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
247 const struct got_error *
248 got_fetch_parse_uri(char **proto, char **host, char **port,
249 char **server_path, char **repo_name, const char *uri)
251 const struct got_error *err = NULL;
255 *proto = *host = *port = *server_path = *repo_name = NULL;
257 p = strstr(uri, "://");
259 /* Try parsing Git's "scp" style URL syntax. */
260 *proto = strdup("ssh");
262 err = got_error_from_errno("strdup");
268 err = got_error(GOT_ERR_PARSE_URI);
271 /* No slashes allowed before first colon. */
274 err = got_error(GOT_ERR_PARSE_URI);
277 *host = strndup(s, q - s);
279 err = got_error_from_errno("strndup");
284 *proto = strndup(uri, p - uri);
286 err = got_error_from_errno("strndup");
292 if (p == NULL || strlen(p) == 1) {
293 err = got_error(GOT_ERR_PARSE_URI);
297 q = memchr(s, ':', p - s);
299 *host = strndup(s, q - s);
301 err = got_error_from_errno("strndup");
304 *port = strndup(q + 1, p - (q + 1));
306 err = got_error_from_errno("strndup");
310 *host = strndup(s, p - s);
312 err = got_error_from_errno("strndup");
318 while (p[0] == '/' && p[1] == '/')
320 *server_path = strdup(p);
321 if (*server_path == NULL) {
322 err = got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(*server_path);
328 if (!p || strlen(p) <= 1) {
329 err = got_error(GOT_ERR_PARSE_URI);
335 err = got_error(GOT_ERR_PARSE_URI);
338 if (hassuffix(p, ".git"))
340 *repo_name = strndup(p, (p + n) - p);
341 if (*repo_name == NULL) {
342 err = got_error_from_errno("strndup");
361 const struct got_error*
362 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
363 struct got_pathlist_head *symrefs, const char *remote_name,
364 int mirror_references, int fetch_all_branches,
365 struct got_pathlist_head *wanted_branches,
366 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
367 int fetchfd, struct got_repository *repo,
368 got_fetch_progress_cb progress_cb, void *progress_arg)
371 int imsg_fetchfds[2], imsg_idxfds[2];
372 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
374 int fetchstatus, idxstatus, done = 0;
375 const struct got_error *err;
376 struct imsgbuf fetchibuf, idxibuf;
377 pid_t fetchpid, idxpid;
378 char *tmppackpath = NULL, *tmpidxpath = NULL;
379 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
380 const char *repo_path = NULL;
381 struct got_pathlist_head have_refs;
382 struct got_pathlist_entry *pe;
383 struct got_reflist_head my_refs;
384 struct got_reflist_entry *re;
385 off_t packfile_size = 0;
386 struct got_packfile_hdr pack_hdr;
388 char *ref_prefix = NULL;
389 size_t ref_prefixlen = 0;
391 char *progress = NULL;
396 * Prevent fetching of references that won't make any
397 * sense outside of the remote repository's context.
399 TAILQ_FOREACH(pe, wanted_refs, entry) {
400 const char *refname = pe->path;
401 if (strncmp(refname, "refs/got/", 9) == 0 ||
402 strncmp(refname, "got/", 4) == 0 ||
403 strncmp(refname, "refs/remotes/", 13) == 0 ||
404 strncmp(refname, "remotes/", 8) == 0)
405 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
409 repo_path = got_repo_get_path_git_dir(repo);
411 for (i = 0; i < nitems(tmpfds); i++)
414 TAILQ_INIT(&have_refs);
415 TAILQ_INIT(&my_refs);
417 if (!mirror_references) {
418 if (asprintf(&ref_prefix, "refs/remotes/%s/",
420 return got_error_from_errno("asprintf");
421 ref_prefixlen = strlen(ref_prefix);
424 if (!list_refs_only) {
425 err = got_ref_list(&my_refs, repo, NULL,
426 got_ref_cmp_by_name, NULL);
431 TAILQ_FOREACH(re, &my_refs, entry) {
432 struct got_object_id *id;
435 if (got_ref_is_symbolic(re->ref))
438 refname = got_ref_get_name(re->ref);
440 if (mirror_references) {
442 err = got_ref_resolve(&id, repo, re->ref);
445 name = strdup(refname);
447 err = got_error_from_errno("strdup");
450 err = got_pathlist_append(&have_refs, name, id);
456 if (strncmp("refs/tags/", refname, 10) == 0) {
459 err = got_ref_resolve(&id, repo, re->ref);
462 tagname = strdup(refname);
463 if (tagname == NULL) {
464 err = got_error_from_errno("strdup");
467 err = got_pathlist_append(&have_refs, tagname, id);
474 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
477 err = got_ref_resolve(&id, repo, re->ref);
481 if (asprintf(&branchname, "refs/heads/%s",
482 refname + ref_prefixlen) == -1) {
483 err = got_error_from_errno("asprintf");
486 err = got_pathlist_append(&have_refs, branchname, id);
494 if (list_refs_only) {
495 packfd = got_opentempfd();
497 err = got_error_from_errno("got_opentempfd");
501 if (asprintf(&path, "%s/%s/fetching.pack",
502 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
503 err = got_error_from_errno("asprintf");
506 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
510 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
511 err = got_error_from_errno2("fchmod", tmppackpath);
515 if (list_refs_only) {
516 idxfd = got_opentempfd();
518 err = got_error_from_errno("got_opentempfd");
522 if (asprintf(&path, "%s/%s/fetching.idx",
523 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
527 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
531 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
532 err = got_error_from_errno2("fchmod", tmpidxpath);
538 err = got_error_from_errno("dup");
542 for (i = 0; i < nitems(tmpfds); i++) {
543 tmpfds[i] = got_opentempfd();
544 if (tmpfds[i] == -1) {
545 err = got_error_from_errno("got_opentempfd");
550 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
551 err = got_error_from_errno("socketpair");
556 if (fetchpid == -1) {
557 err = got_error_from_errno("fork");
559 } else if (fetchpid == 0){
560 got_privsep_exec_child(imsg_fetchfds,
561 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
564 if (close(imsg_fetchfds[1]) == -1) {
565 err = got_error_from_errno("close");
568 imsg_init(&fetchibuf, imsg_fetchfds[0]);
569 nfetchfd = dup(fetchfd);
570 if (nfetchfd == -1) {
571 err = got_error_from_errno("dup");
574 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
575 fetch_all_branches, wanted_branches, wanted_refs,
576 list_refs_only, verbosity);
580 npackfd = dup(packfd);
582 err = got_error_from_errno("dup");
585 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
591 progress = calloc(GOT_FETCH_PKTMAX, 1);
592 if (progress == NULL) {
593 err = got_error_from_errno("calloc");
597 *pack_hash = calloc(1, sizeof(**pack_hash));
598 if (*pack_hash == NULL) {
599 err = got_error_from_errno("calloc");
604 struct got_object_id *id = NULL;
605 char *refname = NULL;
606 char *server_progress = NULL;
607 off_t packfile_size_cur = 0;
609 err = got_privsep_recv_fetch_progress(&done,
610 &id, &refname, symrefs, &server_progress,
611 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
614 if (!done && refname && id) {
615 err = got_pathlist_insert(NULL, refs, refname, id);
618 } else if (!done && server_progress) {
621 * XXX git-daemon tends to send batched output with
622 * lines spanning separate packets. Buffer progress
623 * output until we see a CR or LF to avoid giving
624 * partial lines of progress output to the callback.
626 if (strlcat(progress, server_progress,
627 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
628 progress[0] = '\0'; /* discard */
631 while ((p = strchr(progress, '\r')) != NULL ||
632 (p = strchr(progress, '\n')) != NULL) {
637 if (asprintf(&s, "%s%s", progress,
638 c == '\n' ? "\n" : "") == -1) {
639 err = got_error_from_errno("asprintf");
642 err = progress_cb(progress_arg, s,
643 packfile_size_cur, 0, 0, 0, 0);
647 n = strlen(progress);
648 if (n < GOT_FETCH_PKTMAX - 1) {
649 memmove(progress, &progress[n + 1],
650 GOT_FETCH_PKTMAX - n - 1);
654 free(server_progress);
657 } else if (!done && packfile_size_cur != packfile_size) {
658 err = progress_cb(progress_arg, NULL,
659 packfile_size_cur, 0, 0, 0, 0);
662 packfile_size = packfile_size_cur;
665 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
666 err = got_error_from_errno("waitpid");
670 if (lseek(packfd, 0, SEEK_SET) == -1) {
671 err = got_error_from_errno("lseek");
675 /* If zero data was fetched without error we are already up-to-date. */
676 if (packfile_size == 0) {
680 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
681 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
686 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
688 err = got_error_from_errno("read");
691 if (n != ssizeof(pack_hdr)) {
692 err = got_error(GOT_ERR_IO);
695 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
696 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
697 "bad pack file signature");
700 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
701 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
702 "bad pack file version");
705 nobj = be32toh(pack_hdr.nobjects);
707 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
708 return got_error_msg(GOT_ERR_BAD_PACKFILE,
709 "bad pack file with zero objects");
711 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
712 return got_error_msg(GOT_ERR_BAD_PACKFILE,
713 "empty pack file with non-zero object count");
717 * If the pack file contains no objects, we may only need to update
718 * references in our repository. The caller will take care of that.
723 if (lseek(packfd, 0, SEEK_SET) == -1) {
724 err = got_error_from_errno("lseek");
728 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
729 err = got_error_from_errno("socketpair");
734 err= got_error_from_errno("fork");
736 } else if (idxpid == 0)
737 got_privsep_exec_child(imsg_idxfds,
738 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
739 if (close(imsg_idxfds[1]) == -1) {
740 err = got_error_from_errno("close");
743 imsg_init(&idxibuf, imsg_idxfds[0]);
745 npackfd = dup(packfd);
747 err = got_error_from_errno("dup");
750 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
755 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
759 for (i = 0; i < nitems(tmpfds); i++) {
760 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
767 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
769 err = got_privsep_recv_index_progress(&done, &nobj_total,
770 &nobj_indexed, &nobj_loose, &nobj_resolved,
774 if (nobj_indexed != 0) {
775 err = progress_cb(progress_arg, NULL,
776 packfile_size, nobj_total,
777 nobj_indexed, nobj_loose, nobj_resolved);
781 imsg_clear(&idxibuf);
783 if (close(imsg_idxfds[0]) == -1) {
784 err = got_error_from_errno("close");
787 if (waitpid(idxpid, &idxstatus, 0) == -1) {
788 err = got_error_from_errno("waitpid");
792 err = got_object_id_str(&id_str, *pack_hash);
795 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
796 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
797 err = got_error_from_errno("asprintf");
801 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
802 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
803 err = got_error_from_errno("asprintf");
807 if (rename(tmppackpath, packpath) == -1) {
808 err = got_error_from_errno3("rename", tmppackpath, packpath);
813 if (rename(tmpidxpath, idxpath) == -1) {
814 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
821 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
822 err = got_error_from_errno2("unlink", tmppackpath);
823 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
824 err = got_error_from_errno2("unlink", tmpidxpath);
825 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
826 err = got_error_from_errno("close");
827 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
828 err = got_error_from_errno("close");
829 if (packfd != -1 && close(packfd) == -1 && err == NULL)
830 err = got_error_from_errno("close");
831 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
832 err = got_error_from_errno("close");
833 for (i = 0; i < nitems(tmpfds); i++) {
834 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
835 err = got_error_from_errno("close");
844 TAILQ_FOREACH(pe, &have_refs, entry) {
845 free((char *)pe->path);
848 got_pathlist_free(&have_refs);
849 got_ref_list_free(&my_refs);
853 TAILQ_FOREACH(pe, refs, entry) {
854 free((void *)pe->path);
857 got_pathlist_free(refs);
858 TAILQ_FOREACH(pe, symrefs, entry) {
859 free((void *)pe->path);
862 got_pathlist_free(symrefs);