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 MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 hassuffix(char *base, char *suf)
81 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
86 static const struct got_error *
87 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
88 const char *path, const char *direction, int verbosity)
90 const struct got_error *error = NULL;
99 argv[i++] = GOT_FETCH_PATH_SSH;
102 argv[i++] = (char *)port;
104 if (verbosity == -1) {
107 /* ssh(1) allows up to 3 "-v" options. */
108 for (j = 0; j < MIN(3, verbosity); j++)
112 argv[i++] = (char *)host;
113 argv[i++] = (char *)cmd;
114 argv[i++] = (char *)path;
118 return got_error_from_errno("pipe");
122 error = got_error_from_errno("fork");
126 } else if (pid == 0) {
131 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
132 if (n < 0 || n >= sizeof(cmd))
134 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
136 abort(); /* not reached */
145 static const struct got_error *
146 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
147 const char *direction)
149 const struct got_error *err = NULL;
150 struct addrinfo hints, *servinfo, *p;
151 char *cmd = NULL, *pkt = NULL;
152 int fd = -1, totlen, r, eaicode;
157 port = GOT_DEFAULT_GIT_PORT_STR;
159 memset(&hints, 0, sizeof hints);
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 eaicode = getaddrinfo(host, port, &hints, &servinfo);
165 snprintf(msg, sizeof(msg), "%s: %s", host,
166 gai_strerror(eaicode));
167 return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 for (p = servinfo; p != NULL; p = p->ai_next) {
171 if ((fd = socket(p->ai_family, p->ai_socktype,
172 p->ai_protocol)) == -1)
174 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
178 err = got_error_from_errno("connect");
184 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
185 err = got_error_from_errno("asprintf");
188 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
189 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
190 err = got_error_from_errno("asprintf");
193 r = write(fd, pkt, strlen(pkt) + 1);
195 err = got_error_from_errno("write");
198 if (asprintf(&pkt, "host=%s", host) == -1) {
199 err = got_error_from_errno("asprintf");
202 r = write(fd, pkt, strlen(pkt) + 1);
204 err = got_error_from_errno("write");
218 const struct got_error *
219 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
220 const char *host, const char *port, const char *server_path, int verbosity)
222 const struct got_error *err = NULL;
227 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
228 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
229 "upload", verbosity);
230 else if (strcmp(proto, "git") == 0)
231 err = dial_git(fetchfd, host, port, server_path, "upload");
232 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
233 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
235 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
239 const struct got_error *
240 got_fetch_parse_uri(char **proto, char **host, char **port,
241 char **server_path, char **repo_name, const char *uri)
243 const struct got_error *err = NULL;
247 *proto = *host = *port = *server_path = *repo_name = NULL;
249 p = strstr(uri, "://");
251 /* Try parsing Git's "scp" style URL syntax. */
252 *proto = strdup("ssh");
254 err = got_error_from_errno("strdup");
260 err = got_error(GOT_ERR_PARSE_URI);
263 /* No slashes allowed before first colon. */
266 err = got_error(GOT_ERR_PARSE_URI);
269 *host = strndup(s, q - s);
271 err = got_error_from_errno("strndup");
276 *proto = strndup(uri, p - uri);
278 err = got_error_from_errno("strndup");
284 if (p == NULL || strlen(p) == 1) {
285 err = got_error(GOT_ERR_PARSE_URI);
289 q = memchr(s, ':', p - s);
291 *host = strndup(s, q - s);
293 err = got_error_from_errno("strndup");
296 *port = strndup(q + 1, p - (q + 1));
298 err = got_error_from_errno("strndup");
302 *host = strndup(s, p - s);
304 err = got_error_from_errno("strndup");
310 while (p[0] == '/' && p[1] == '/')
312 *server_path = strdup(p);
313 if (*server_path == NULL) {
314 err = got_error_from_errno("strdup");
317 got_path_strip_trailing_slashes(*server_path);
320 if (!p || strlen(p) <= 1) {
321 err = got_error(GOT_ERR_PARSE_URI);
327 err = got_error(GOT_ERR_PARSE_URI);
330 if (hassuffix(p, ".git"))
332 *repo_name = strndup(p, (p + n) - p);
333 if (*repo_name == NULL) {
334 err = got_error_from_errno("strndup");
353 const struct got_error*
354 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
355 struct got_pathlist_head *symrefs, const char *remote_name,
356 int mirror_references, int fetch_all_branches,
357 struct got_pathlist_head *wanted_branches,
358 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
359 int fetchfd, struct got_repository *repo,
360 got_fetch_progress_cb progress_cb, void *progress_arg)
363 int imsg_fetchfds[2], imsg_idxfds[2];
364 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
366 int fetchstatus, idxstatus, done = 0;
367 const struct got_error *err;
368 struct imsgbuf fetchibuf, idxibuf;
369 pid_t fetchpid, idxpid;
370 char *tmppackpath = NULL, *tmpidxpath = NULL;
371 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
372 const char *repo_path = NULL;
373 struct got_pathlist_head have_refs;
374 struct got_pathlist_entry *pe;
375 struct got_reflist_head my_refs;
376 struct got_reflist_entry *re;
377 off_t packfile_size = 0;
378 struct got_packfile_hdr pack_hdr;
380 char *ref_prefix = NULL;
381 size_t ref_prefixlen = 0;
383 char *progress = NULL;
388 * Prevent fetching of references that won't make any
389 * sense outside of the remote repository's context.
391 TAILQ_FOREACH(pe, wanted_refs, entry) {
392 const char *refname = pe->path;
393 if (strncmp(refname, "refs/got/", 9) == 0 ||
394 strncmp(refname, "got/", 4) == 0 ||
395 strncmp(refname, "refs/remotes/", 13) == 0 ||
396 strncmp(refname, "remotes/", 8) == 0)
397 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
401 repo_path = got_repo_get_path_git_dir(repo);
403 for (i = 0; i < nitems(tmpfds); i++)
406 TAILQ_INIT(&have_refs);
407 SIMPLEQ_INIT(&my_refs);
409 if (!mirror_references) {
410 if (asprintf(&ref_prefix, "refs/remotes/%s/",
412 return got_error_from_errno("asprintf");
413 ref_prefixlen = strlen(ref_prefix);
416 if (!list_refs_only) {
417 err = got_ref_list(&my_refs, repo, NULL,
418 got_ref_cmp_by_name, NULL);
423 SIMPLEQ_FOREACH(re, &my_refs, entry) {
424 struct got_object_id *id;
427 if (got_ref_is_symbolic(re->ref))
430 refname = got_ref_get_name(re->ref);
432 if (mirror_references) {
434 err = got_ref_resolve(&id, repo, re->ref);
437 name = strdup(refname);
439 err = got_error_from_errno("strdup");
442 err = got_pathlist_append(&have_refs, name, id);
448 if (strncmp("refs/tags/", refname, 10) == 0) {
451 err = got_ref_resolve(&id, repo, re->ref);
454 tagname = strdup(refname);
455 if (tagname == NULL) {
456 err = got_error_from_errno("strdup");
459 err = got_pathlist_append(&have_refs, tagname, id);
466 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
469 err = got_ref_resolve(&id, repo, re->ref);
473 if (asprintf(&branchname, "refs/heads/%s",
474 refname + ref_prefixlen) == -1) {
475 err = got_error_from_errno("asprintf");
478 err = got_pathlist_append(&have_refs, branchname, id);
486 if (list_refs_only) {
487 packfd = got_opentempfd();
489 err = got_error_from_errno("got_opentempfd");
493 if (asprintf(&path, "%s/%s/fetching.pack",
494 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
495 err = got_error_from_errno("asprintf");
498 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
502 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
503 err = got_error_from_errno2("fchmod", tmppackpath);
507 if (list_refs_only) {
508 idxfd = got_opentempfd();
510 err = got_error_from_errno("got_opentempfd");
514 if (asprintf(&path, "%s/%s/fetching.idx",
515 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
516 err = got_error_from_errno("asprintf");
519 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
523 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
524 err = got_error_from_errno2("fchmod", tmpidxpath);
530 err = got_error_from_errno("dup");
534 for (i = 0; i < nitems(tmpfds); i++) {
535 tmpfds[i] = got_opentempfd();
536 if (tmpfds[i] == -1) {
537 err = got_error_from_errno("got_opentempfd");
542 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
543 err = got_error_from_errno("socketpair");
548 if (fetchpid == -1) {
549 err = got_error_from_errno("fork");
551 } else if (fetchpid == 0){
552 got_privsep_exec_child(imsg_fetchfds,
553 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
556 if (close(imsg_fetchfds[1]) != 0) {
557 err = got_error_from_errno("close");
560 imsg_init(&fetchibuf, imsg_fetchfds[0]);
561 nfetchfd = dup(fetchfd);
562 if (nfetchfd == -1) {
563 err = got_error_from_errno("dup");
566 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
567 fetch_all_branches, wanted_branches, wanted_refs,
568 list_refs_only, verbosity);
572 npackfd = dup(packfd);
574 err = got_error_from_errno("dup");
577 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
583 progress = calloc(GOT_FETCH_PKTMAX, 1);
584 if (progress == NULL) {
585 err = got_error_from_errno("calloc");
589 *pack_hash = calloc(1, sizeof(**pack_hash));
590 if (*pack_hash == NULL) {
591 err = got_error_from_errno("calloc");
596 struct got_object_id *id = NULL;
597 char *refname = NULL;
598 char *server_progress = NULL;
599 off_t packfile_size_cur = 0;
601 err = got_privsep_recv_fetch_progress(&done,
602 &id, &refname, symrefs, &server_progress,
603 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
606 if (!done && refname && id) {
607 err = got_pathlist_insert(NULL, refs, refname, id);
610 } else if (!done && server_progress) {
613 * XXX git-daemon tends to send batched output with
614 * lines spanning separate packets. Buffer progress
615 * output until we see a CR or LF to avoid giving
616 * partial lines of progress output to the callback.
618 if (strlcat(progress, server_progress,
619 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
620 progress[0] = '\0'; /* discard */
623 while ((p = strchr(progress, '\r')) != NULL ||
624 (p = strchr(progress, '\n')) != NULL) {
629 if (asprintf(&s, "%s%s", progress,
630 c == '\n' ? "\n" : "") == -1) {
631 err = got_error_from_errno("asprintf");
634 err = progress_cb(progress_arg, s,
635 packfile_size_cur, 0, 0, 0, 0);
639 n = strlen(progress);
640 if (n < GOT_FETCH_PKTMAX - 1) {
641 memmove(progress, &progress[n + 1],
642 GOT_FETCH_PKTMAX - n - 1);
646 free(server_progress);
649 } else if (!done && packfile_size_cur != packfile_size) {
650 err = progress_cb(progress_arg, NULL,
651 packfile_size_cur, 0, 0, 0, 0);
654 packfile_size = packfile_size_cur;
657 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
658 err = got_error_from_errno("waitpid");
662 if (lseek(packfd, 0, SEEK_SET) == -1) {
663 err = got_error_from_errno("lseek");
667 /* If zero data was fetched without error we are already up-to-date. */
668 if (packfile_size == 0) {
672 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
673 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
678 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
680 err = got_error_from_errno("read");
683 if (n != sizeof(pack_hdr)) {
684 err = got_error(GOT_ERR_IO);
687 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
688 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
689 "bad pack file signature");
692 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
693 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
694 "bad pack file version");
697 nobj = be32toh(pack_hdr.nobjects);
699 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
700 return got_error_msg(GOT_ERR_BAD_PACKFILE,
701 "bad pack file with zero objects");
703 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
704 return got_error_msg(GOT_ERR_BAD_PACKFILE,
705 "empty pack file with non-zero object count");
709 * If the pack file contains no objects, we may only need to update
710 * references in our repository. The caller will take care of that.
715 if (lseek(packfd, 0, SEEK_SET) == -1) {
716 err = got_error_from_errno("lseek");
720 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
721 err = got_error_from_errno("socketpair");
726 err= got_error_from_errno("fork");
728 } else if (idxpid == 0)
729 got_privsep_exec_child(imsg_idxfds,
730 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
731 if (close(imsg_idxfds[1]) != 0) {
732 err = got_error_from_errno("close");
735 imsg_init(&idxibuf, imsg_idxfds[0]);
737 npackfd = dup(packfd);
739 err = got_error_from_errno("dup");
742 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
747 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
751 for (i = 0; i < nitems(tmpfds); i++) {
752 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
759 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
761 err = got_privsep_recv_index_progress(&done, &nobj_total,
762 &nobj_indexed, &nobj_loose, &nobj_resolved,
766 if (nobj_indexed != 0) {
767 err = progress_cb(progress_arg, NULL,
768 packfile_size, nobj_total,
769 nobj_indexed, nobj_loose, nobj_resolved);
773 imsg_clear(&idxibuf);
775 if (close(imsg_idxfds[0]) == -1) {
776 err = got_error_from_errno("close");
779 if (waitpid(idxpid, &idxstatus, 0) == -1) {
780 err = got_error_from_errno("waitpid");
784 err = got_object_id_str(&id_str, *pack_hash);
787 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
788 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
793 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
794 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
795 err = got_error_from_errno("asprintf");
799 if (rename(tmppackpath, packpath) == -1) {
800 err = got_error_from_errno3("rename", tmppackpath, packpath);
805 if (rename(tmpidxpath, idxpath) == -1) {
806 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
813 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
814 err = got_error_from_errno2("unlink", tmppackpath);
815 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
816 err = got_error_from_errno2("unlink", tmpidxpath);
817 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
818 err = got_error_from_errno("close");
819 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
820 err = got_error_from_errno("close");
821 if (packfd != -1 && close(packfd) == -1 && err == NULL)
822 err = got_error_from_errno("close");
823 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
825 for (i = 0; i < nitems(tmpfds); i++) {
826 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
827 err = got_error_from_errno("close");
836 TAILQ_FOREACH(pe, &have_refs, entry) {
837 free((char *)pe->path);
840 got_pathlist_free(&have_refs);
841 got_ref_list_free(&my_refs);
845 TAILQ_FOREACH(pe, refs, entry) {
846 free((void *)pe->path);
849 got_pathlist_free(refs);
850 TAILQ_FOREACH(pe, symrefs, entry) {
851 free((void *)pe->path);
854 got_pathlist_free(symrefs);