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/syslimits.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
54 #include "got_fetch.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
76 hassuffix(char *base, char *suf)
82 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
87 static const struct got_error *
88 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
89 const char *path, const char *direction, int verbosity)
91 const struct got_error *error = NULL;
100 argv[i++] = GOT_FETCH_PATH_SSH;
103 argv[i++] = (char *)port;
105 if (verbosity == -1) {
108 /* ssh(1) allows up to 3 "-v" options. */
109 for (j = 0; j < MIN(3, verbosity); j++)
113 argv[i++] = (char *)host;
114 argv[i++] = (char *)cmd;
115 argv[i++] = (char *)path;
119 return got_error_from_errno("pipe");
123 error = got_error_from_errno("fork");
127 } else if (pid == 0) {
132 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
133 if (n < 0 || n >= sizeof(cmd))
135 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
137 abort(); /* not reached */
146 static const struct got_error *
147 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
148 const char *direction)
150 const struct got_error *err = NULL;
151 struct addrinfo hints, *servinfo, *p;
152 char *cmd = NULL, *pkt = NULL;
153 int fd = -1, totlen, r, eaicode;
158 port = GOT_DEFAULT_GIT_PORT_STR;
160 memset(&hints, 0, sizeof hints);
161 hints.ai_family = AF_UNSPEC;
162 hints.ai_socktype = SOCK_STREAM;
163 eaicode = getaddrinfo(host, port, &hints, &servinfo);
166 snprintf(msg, sizeof(msg), "%s: %s", host,
167 gai_strerror(eaicode));
168 return got_error_msg(GOT_ERR_ADDRINFO, msg);
171 for (p = servinfo; p != NULL; p = p->ai_next) {
172 if ((fd = socket(p->ai_family, p->ai_socktype,
173 p->ai_protocol)) == -1)
175 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
177 err = got_error_from_errno("connect");
183 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
184 err = got_error_from_errno("asprintf");
187 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
188 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
189 err = got_error_from_errno("asprintf");
192 r = write(fd, pkt, strlen(pkt) + 1);
194 err = got_error_from_errno("write");
197 if (asprintf(&pkt, "host=%s", host) == -1) {
198 err = got_error_from_errno("asprintf");
201 r = write(fd, pkt, strlen(pkt) + 1);
203 err = got_error_from_errno("write");
217 const struct got_error *
218 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
219 const char *host, const char *port, const char *server_path, int verbosity)
221 const struct got_error *err = NULL;
226 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
227 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
228 "upload", verbosity);
229 else if (strcmp(proto, "git") == 0)
230 err = dial_git(fetchfd, host, port, server_path, "upload");
231 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
232 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
234 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
238 const struct got_error *
239 got_fetch_parse_uri(char **proto, char **host, char **port,
240 char **server_path, char **repo_name, const char *uri)
242 const struct got_error *err = NULL;
246 *proto = *host = *port = *server_path = *repo_name = NULL;
248 p = strstr(uri, "://");
250 /* Try parsing Git's "scp" style URL syntax. */
251 *proto = strdup("ssh");
253 err = got_error_from_errno("strdup");
259 err = got_error(GOT_ERR_PARSE_URI);
262 /* No slashes allowed before first colon. */
265 err = got_error(GOT_ERR_PARSE_URI);
268 *host = strndup(s, q - s);
270 err = got_error_from_errno("strndup");
275 *proto = strndup(uri, p - uri);
277 err = got_error_from_errno("strndup");
283 if (p == NULL || strlen(p) == 1) {
284 err = got_error(GOT_ERR_PARSE_URI);
288 q = memchr(s, ':', p - s);
290 *host = strndup(s, q - s);
292 err = got_error_from_errno("strndup");
295 *port = strndup(q + 1, p - (q + 1));
297 err = got_error_from_errno("strndup");
301 *host = strndup(s, p - s);
303 err = got_error_from_errno("strndup");
309 *server_path = strdup(p);
310 if (*server_path == NULL) {
311 err = got_error_from_errno("strdup");
316 if (!p || strlen(p) <= 1) {
317 err = got_error(GOT_ERR_PARSE_URI);
323 err = got_error(GOT_ERR_PARSE_URI);
326 if (hassuffix(p, ".git"))
328 *repo_name = strndup(p, (p + n) - p);
329 if (*repo_name == NULL) {
330 err = got_error_from_errno("strndup");
349 const struct got_error*
350 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
351 struct got_pathlist_head *symrefs, const char *remote_name,
352 int mirror_references, int fetch_all_branches,
353 struct got_pathlist_head *wanted_branches,
354 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
355 int fetchfd, struct got_repository *repo,
356 got_fetch_progress_cb progress_cb, void *progress_arg)
358 int imsg_fetchfds[2], imsg_idxfds[2];
359 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
361 int fetchstatus, idxstatus, done = 0;
362 const struct got_error *err;
363 struct imsgbuf fetchibuf, idxibuf;
364 pid_t fetchpid, idxpid;
365 char *tmppackpath = NULL, *tmpidxpath = NULL;
366 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
367 const char *repo_path = NULL;
368 struct got_pathlist_head have_refs;
369 struct got_pathlist_entry *pe;
370 struct got_reflist_head my_refs;
371 struct got_reflist_entry *re;
372 off_t packfile_size = 0;
373 struct got_packfile_hdr pack_hdr;
375 char *ref_prefix = NULL;
376 size_t ref_prefixlen = 0;
378 char *progress = NULL;
383 * Prevent fetching of references that won't make any
384 * sense outside of the remote repository's context.
386 TAILQ_FOREACH(pe, wanted_refs, entry) {
387 const char *refname = pe->path;
388 if (strncmp(refname, "refs/got/", 9) == 0 ||
389 strncmp(refname, "got/", 4) == 0 ||
390 strncmp(refname, "refs/remotes/", 13) == 0 ||
391 strncmp(refname, "remotes/", 8) == 0)
392 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
396 repo_path = got_repo_get_path_git_dir(repo);
398 for (i = 0; i < nitems(tmpfds); i++)
401 TAILQ_INIT(&have_refs);
402 SIMPLEQ_INIT(&my_refs);
404 if (!mirror_references) {
405 if (asprintf(&ref_prefix, "refs/remotes/%s/",
407 return got_error_from_errno("asprintf");
408 ref_prefixlen = strlen(ref_prefix);
411 if (!list_refs_only) {
412 err = got_ref_list(&my_refs, repo, NULL,
413 got_ref_cmp_by_name, NULL);
418 SIMPLEQ_FOREACH(re, &my_refs, entry) {
419 struct got_object_id *id;
422 if (got_ref_is_symbolic(re->ref))
425 refname = got_ref_get_name(re->ref);
427 if (mirror_references) {
429 err = got_ref_resolve(&id, repo, re->ref);
432 name = strdup(refname);
434 err = got_error_from_errno("strdup");
437 err = got_pathlist_append(&have_refs, name, id);
443 if (strncmp("refs/tags/", refname, 10) == 0) {
446 err = got_ref_resolve(&id, repo, re->ref);
449 tagname = strdup(refname);
450 if (tagname == NULL) {
451 err = got_error_from_errno("strdup");
454 err = got_pathlist_append(&have_refs, tagname, id);
461 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
464 err = got_ref_resolve(&id, repo, re->ref);
468 if (asprintf(&branchname, "refs/heads/%s",
469 refname + ref_prefixlen) == -1) {
470 err = got_error_from_errno("asprintf");
473 err = got_pathlist_append(&have_refs, branchname, id);
481 if (list_refs_only) {
482 packfd = got_opentempfd();
484 err = got_error_from_errno("got_opentempfd");
488 if (asprintf(&path, "%s/%s/fetching.pack",
489 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
490 err = got_error_from_errno("asprintf");
493 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
498 if (list_refs_only) {
499 idxfd = got_opentempfd();
501 err = got_error_from_errno("got_opentempfd");
505 if (asprintf(&path, "%s/%s/fetching.idx",
506 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
507 err = got_error_from_errno("asprintf");
510 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
517 err = got_error_from_errno("dup");
521 for (i = 0; i < nitems(tmpfds); i++) {
522 tmpfds[i] = got_opentempfd();
523 if (tmpfds[i] == -1) {
524 err = got_error_from_errno("got_opentempfd");
529 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
530 err = got_error_from_errno("socketpair");
535 if (fetchpid == -1) {
536 err = got_error_from_errno("fork");
538 } else if (fetchpid == 0){
539 got_privsep_exec_child(imsg_fetchfds,
540 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
543 if (close(imsg_fetchfds[1]) != 0) {
544 err = got_error_from_errno("close");
547 imsg_init(&fetchibuf, imsg_fetchfds[0]);
548 nfetchfd = dup(fetchfd);
549 if (nfetchfd == -1) {
550 err = got_error_from_errno("dup");
553 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
554 fetch_all_branches, wanted_branches, wanted_refs,
555 list_refs_only, verbosity);
559 npackfd = dup(packfd);
561 err = got_error_from_errno("dup");
564 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
570 progress = calloc(GOT_FETCH_PKTMAX, 1);
571 if (progress == NULL) {
572 err = got_error_from_errno("calloc");
576 *pack_hash = calloc(1, sizeof(**pack_hash));
577 if (*pack_hash == NULL) {
578 err = got_error_from_errno("calloc");
583 struct got_object_id *id = NULL;
584 char *refname = NULL;
585 char *server_progress = NULL;
586 off_t packfile_size_cur = 0;
588 err = got_privsep_recv_fetch_progress(&done,
589 &id, &refname, symrefs, &server_progress,
590 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
593 if (!done && refname && id) {
594 err = got_pathlist_insert(NULL, refs, refname, id);
597 } else if (!done && server_progress) {
600 * XXX git-daemon tends to send batched output with
601 * lines spanning separate packets. Buffer progress
602 * output until we see a CR or LF to avoid giving
603 * partial lines of progress output to the callback.
605 if (strlcat(progress, server_progress,
606 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
607 progress[0] = '\0'; /* discard */
610 while ((p = strchr(progress, '\r')) != NULL ||
611 (p = strchr(progress, '\n')) != NULL) {
616 if (asprintf(&s, "%s%s", progress,
617 c == '\n' ? "\n" : "") == -1) {
618 err = got_error_from_errno("asprintf");
621 err = progress_cb(progress_arg, s,
622 packfile_size_cur, 0, 0, 0, 0);
626 n = strlen(progress);
627 if (n < GOT_FETCH_PKTMAX - 1) {
628 memmove(progress, &progress[n + 1],
629 GOT_FETCH_PKTMAX - n - 1);
633 free(server_progress);
636 } else if (!done && packfile_size_cur != packfile_size) {
637 err = progress_cb(progress_arg, NULL,
638 packfile_size_cur, 0, 0, 0, 0);
641 packfile_size = packfile_size_cur;
644 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
645 err = got_error_from_errno("waitpid");
649 if (lseek(packfd, 0, SEEK_SET) == -1) {
650 err = got_error_from_errno("lseek");
654 /* If zero data was fetched without error we are already up-to-date. */
655 if (packfile_size == 0) {
659 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
660 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
665 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
667 err = got_error_from_errno("read");
670 if (n != sizeof(pack_hdr)) {
671 err = got_error(GOT_ERR_IO);
674 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
675 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
676 "bad pack file signature");
679 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
680 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
681 "bad pack file version");
684 nobj = be32toh(pack_hdr.nobjects);
686 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
687 return got_error_msg(GOT_ERR_BAD_PACKFILE,
688 "bad pack file with zero objects");
690 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
691 return got_error_msg(GOT_ERR_BAD_PACKFILE,
692 "empty pack file with non-zero object count");
696 * If the pack file contains no objects, we may only need to update
697 * references in our repository. The caller will take care of that.
702 if (lseek(packfd, 0, SEEK_SET) == -1) {
703 err = got_error_from_errno("lseek");
707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
708 err = got_error_from_errno("socketpair");
713 err= got_error_from_errno("fork");
715 } else if (idxpid == 0)
716 got_privsep_exec_child(imsg_idxfds,
717 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
718 if (close(imsg_idxfds[1]) != 0) {
719 err = got_error_from_errno("close");
722 imsg_init(&idxibuf, imsg_idxfds[0]);
724 npackfd = dup(packfd);
726 err = got_error_from_errno("dup");
729 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
734 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
738 for (i = 0; i < nitems(tmpfds); i++) {
739 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
746 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
748 err = got_privsep_recv_index_progress(&done, &nobj_total,
749 &nobj_indexed, &nobj_loose, &nobj_resolved,
753 if (nobj_indexed != 0) {
754 err = progress_cb(progress_arg, NULL,
755 packfile_size, nobj_total,
756 nobj_indexed, nobj_loose, nobj_resolved);
760 imsg_clear(&idxibuf);
762 if (close(imsg_idxfds[0]) == -1) {
763 err = got_error_from_errno("close");
766 if (waitpid(idxpid, &idxstatus, 0) == -1) {
767 err = got_error_from_errno("waitpid");
771 err = got_object_id_str(&id_str, *pack_hash);
774 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
775 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
776 err = got_error_from_errno("asprintf");
780 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
781 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
782 err = got_error_from_errno("asprintf");
786 if (rename(tmppackpath, packpath) == -1) {
787 err = got_error_from_errno3("rename", tmppackpath, packpath);
792 if (rename(tmpidxpath, idxpath) == -1) {
793 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
800 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
801 err = got_error_from_errno2("unlink", tmppackpath);
802 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
803 err = got_error_from_errno2("unlink", tmpidxpath);
804 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 if (packfd != -1 && close(packfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
811 err = got_error_from_errno("close");
812 for (i = 0; i < nitems(tmpfds); i++) {
813 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
814 err = got_error_from_errno("close");
823 TAILQ_FOREACH(pe, &have_refs, entry) {
824 free((char *)pe->path);
827 got_pathlist_free(&have_refs);
828 got_ref_list_free(&my_refs);
832 TAILQ_FOREACH(pe, refs, entry) {
833 free((void *)pe->path);
836 got_pathlist_free(refs);
837 TAILQ_FOREACH(pe, symrefs, entry) {
838 free((void *)pe->path);
841 got_pathlist_free(symrefs);