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>
42 #include <netinet/in.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
51 #include "got_opentemp.h"
52 #include "got_fetch.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 hassuffix(char *base, char *suf)
80 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
85 static const struct got_error *
86 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
87 const char *path, const char *direction, int verbosity)
89 const struct got_error *error = NULL;
98 argv[i++] = GOT_FETCH_PATH_SSH;
101 argv[i++] = (char *)port;
103 if (verbosity == -1) {
106 /* ssh(1) allows up to 3 "-v" options. */
107 for (j = 0; j < MIN(3, verbosity); j++)
111 argv[i++] = (char *)host;
112 argv[i++] = (char *)cmd;
113 argv[i++] = (char *)path;
117 return got_error_from_errno("pipe");
121 error = got_error_from_errno("fork");
125 } else if (pid == 0) {
130 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
131 if (n < 0 || n >= sizeof(cmd))
133 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
135 abort(); /* not reached */
144 static const struct got_error *
145 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
146 const char *direction)
148 const struct got_error *err = NULL;
149 struct addrinfo hints, *servinfo, *p;
150 char *cmd = NULL, *pkt = NULL;
151 int fd = -1, totlen, r, eaicode;
156 port = GOT_DEFAULT_GIT_PORT_STR;
158 memset(&hints, 0, sizeof hints);
159 hints.ai_family = AF_UNSPEC;
160 hints.ai_socktype = SOCK_STREAM;
161 eaicode = getaddrinfo(host, port, &hints, &servinfo);
164 snprintf(msg, sizeof(msg), "%s: %s", host,
165 gai_strerror(eaicode));
166 return got_error_msg(GOT_ERR_ADDRINFO, msg);
169 for (p = servinfo; p != NULL; p = p->ai_next) {
170 if ((fd = socket(p->ai_family, p->ai_socktype,
171 p->ai_protocol)) == -1)
173 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
175 err = got_error_from_errno("connect");
181 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
182 err = got_error_from_errno("asprintf");
185 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
186 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
187 err = got_error_from_errno("asprintf");
190 r = write(fd, pkt, strlen(pkt) + 1);
192 err = got_error_from_errno("write");
195 if (asprintf(&pkt, "host=%s", host) == -1) {
196 err = got_error_from_errno("asprintf");
199 r = write(fd, pkt, strlen(pkt) + 1);
201 err = got_error_from_errno("write");
215 const struct got_error *
216 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
217 const char *host, const char *port, const char *server_path, int verbosity)
219 const struct got_error *err = NULL;
224 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
225 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
226 "upload", verbosity);
227 else if (strcmp(proto, "git") == 0)
228 err = dial_git(fetchfd, host, port, server_path, "upload");
229 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
230 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
232 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
236 const struct got_error *
237 got_fetch_parse_uri(char **proto, char **host, char **port,
238 char **server_path, char **repo_name, const char *uri)
240 const struct got_error *err = NULL;
244 *proto = *host = *port = *server_path = *repo_name = NULL;
246 p = strstr(uri, "://");
248 /* Try parsing Git's "scp" style URL syntax. */
249 *proto = strdup("ssh");
251 err = got_error_from_errno("strdup");
257 err = got_error(GOT_ERR_PARSE_URI);
260 /* No slashes allowed before first colon. */
263 err = got_error(GOT_ERR_PARSE_URI);
266 *host = strndup(s, q - s);
268 err = got_error_from_errno("strndup");
273 *proto = strndup(uri, p - uri);
275 err = got_error_from_errno("strndup");
281 if (p == NULL || strlen(p) == 1) {
282 err = got_error(GOT_ERR_PARSE_URI);
286 q = memchr(s, ':', p - s);
288 *host = strndup(s, q - s);
290 err = got_error_from_errno("strndup");
293 *port = strndup(q + 1, p - (q + 1));
295 err = got_error_from_errno("strndup");
299 *host = strndup(s, p - s);
301 err = got_error_from_errno("strndup");
307 *server_path = strdup(p);
308 if (*server_path == NULL) {
309 err = got_error_from_errno("strdup");
314 if (!p || strlen(p) <= 1) {
315 err = got_error(GOT_ERR_PARSE_URI);
321 err = got_error(GOT_ERR_PARSE_URI);
324 if (hassuffix(p, ".git"))
326 *repo_name = strndup(p, (p + n) - p);
327 if (*repo_name == NULL) {
328 err = got_error_from_errno("strndup");
347 const struct got_error*
348 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
349 struct got_pathlist_head *symrefs, const char *remote_name,
350 int mirror_references, int fetch_all_branches,
351 struct got_pathlist_head *wanted_branches,
352 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
353 int fetchfd, struct got_repository *repo,
354 got_fetch_progress_cb progress_cb, void *progress_arg)
356 int imsg_fetchfds[2], imsg_idxfds[2];
357 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
359 int fetchstatus, idxstatus, done = 0;
360 const struct got_error *err;
361 struct imsgbuf fetchibuf, idxibuf;
362 pid_t fetchpid, idxpid;
363 char *tmppackpath = NULL, *tmpidxpath = NULL;
364 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
365 const char *repo_path = NULL;
366 struct got_pathlist_head have_refs;
367 struct got_pathlist_entry *pe;
368 struct got_reflist_head my_refs;
369 struct got_reflist_entry *re;
370 off_t packfile_size = 0;
371 struct got_packfile_hdr pack_hdr;
373 char *ref_prefix = NULL;
374 size_t ref_prefixlen = 0;
376 char *progress = NULL;
381 * Prevent fetching of references that won't make any
382 * sense outside of the remote repository's context.
384 TAILQ_FOREACH(pe, wanted_refs, entry) {
385 const char *refname = pe->path;
386 if (strncmp(refname, "refs/got/", 9) == 0 ||
387 strncmp(refname, "got/", 4) == 0 ||
388 strncmp(refname, "refs/remotes/", 13) == 0 ||
389 strncmp(refname, "remotes/", 8) == 0)
390 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
394 repo_path = got_repo_get_path_git_dir(repo);
396 for (i = 0; i < nitems(tmpfds); i++)
399 TAILQ_INIT(&have_refs);
400 SIMPLEQ_INIT(&my_refs);
402 if (!mirror_references) {
403 if (asprintf(&ref_prefix, "refs/remotes/%s/",
405 return got_error_from_errno("asprintf");
406 ref_prefixlen = strlen(ref_prefix);
409 if (!list_refs_only) {
410 err = got_ref_list(&my_refs, repo, NULL,
411 got_ref_cmp_by_name, NULL);
416 SIMPLEQ_FOREACH(re, &my_refs, entry) {
417 struct got_object_id *id;
420 if (got_ref_is_symbolic(re->ref))
423 refname = got_ref_get_name(re->ref);
425 if (mirror_references) {
427 err = got_ref_resolve(&id, repo, re->ref);
430 name = strdup(refname);
432 err = got_error_from_errno("strdup");
435 err = got_pathlist_append(&have_refs, name, id);
441 if (strncmp("refs/tags/", refname, 10) == 0) {
444 err = got_ref_resolve(&id, repo, re->ref);
447 tagname = strdup(refname);
448 if (tagname == NULL) {
449 err = got_error_from_errno("strdup");
452 err = got_pathlist_append(&have_refs, tagname, id);
459 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
462 err = got_ref_resolve(&id, repo, re->ref);
466 if (asprintf(&branchname, "refs/heads/%s",
467 refname + ref_prefixlen) == -1) {
468 err = got_error_from_errno("asprintf");
471 err = got_pathlist_append(&have_refs, branchname, id);
479 if (list_refs_only) {
480 packfd = got_opentempfd();
482 err = got_error_from_errno("got_opentempfd");
486 if (asprintf(&path, "%s/%s/fetching.pack",
487 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
488 err = got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
496 if (list_refs_only) {
497 idxfd = got_opentempfd();
499 err = got_error_from_errno("got_opentempfd");
503 if (asprintf(&path, "%s/%s/fetching.idx",
504 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
505 err = got_error_from_errno("asprintf");
508 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
515 err = got_error_from_errno("dup");
519 for (i = 0; i < nitems(tmpfds); i++) {
520 tmpfds[i] = got_opentempfd();
521 if (tmpfds[i] == -1) {
522 err = got_error_from_errno("got_opentempfd");
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
528 err = got_error_from_errno("socketpair");
533 if (fetchpid == -1) {
534 err = got_error_from_errno("fork");
536 } else if (fetchpid == 0){
537 got_privsep_exec_child(imsg_fetchfds,
538 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
541 if (close(imsg_fetchfds[1]) != 0) {
542 err = got_error_from_errno("close");
545 imsg_init(&fetchibuf, imsg_fetchfds[0]);
546 nfetchfd = dup(fetchfd);
547 if (nfetchfd == -1) {
548 err = got_error_from_errno("dup");
551 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
552 fetch_all_branches, wanted_branches, wanted_refs,
553 list_refs_only, verbosity);
557 npackfd = dup(packfd);
559 err = got_error_from_errno("dup");
562 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
568 progress = calloc(GOT_FETCH_PKTMAX, 1);
569 if (progress == NULL) {
570 err = got_error_from_errno("calloc");
574 *pack_hash = calloc(1, sizeof(**pack_hash));
575 if (*pack_hash == NULL) {
576 err = got_error_from_errno("calloc");
581 struct got_object_id *id = NULL;
582 char *refname = NULL;
583 char *server_progress = NULL;
584 off_t packfile_size_cur = 0;
586 err = got_privsep_recv_fetch_progress(&done,
587 &id, &refname, symrefs, &server_progress,
588 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
591 if (!done && refname && id) {
592 err = got_pathlist_insert(NULL, refs, refname, id);
595 } else if (!done && server_progress) {
598 * XXX git-daemon tends to send batched output with
599 * lines spanning separate packets. Buffer progress
600 * output until we see a CR or LF to avoid giving
601 * partial lines of progress output to the callback.
603 if (strlcat(progress, server_progress,
604 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
605 progress[0] = '\0'; /* discard */
608 while ((p = strchr(progress, '\r')) != NULL ||
609 (p = strchr(progress, '\n')) != NULL) {
614 if (asprintf(&s, "%s%s", progress,
615 c == '\n' ? "\n" : "") == -1) {
616 err = got_error_from_errno("asprintf");
619 err = progress_cb(progress_arg, s,
620 packfile_size_cur, 0, 0, 0, 0);
624 n = strlen(progress);
625 if (n < GOT_FETCH_PKTMAX - 1) {
626 memmove(progress, &progress[n + 1],
627 GOT_FETCH_PKTMAX - n - 1);
631 free(server_progress);
634 } else if (!done && packfile_size_cur != packfile_size) {
635 err = progress_cb(progress_arg, NULL,
636 packfile_size_cur, 0, 0, 0, 0);
639 packfile_size = packfile_size_cur;
642 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
643 err = got_error_from_errno("waitpid");
647 if (lseek(packfd, 0, SEEK_SET) == -1) {
648 err = got_error_from_errno("lseek");
652 /* If zero data was fetched without error we are already up-to-date. */
653 if (packfile_size == 0) {
657 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
658 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
663 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
665 err = got_error_from_errno("read");
668 if (n != sizeof(pack_hdr)) {
669 err = got_error(GOT_ERR_IO);
672 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
673 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
674 "bad pack file signature");
677 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
678 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
679 "bad pack file version");
682 nobj = betoh32(pack_hdr.nobjects);
684 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
685 return got_error_msg(GOT_ERR_BAD_PACKFILE,
686 "bad pack file with zero objects");
688 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
689 return got_error_msg(GOT_ERR_BAD_PACKFILE,
690 "empty pack file with non-zero object count");
694 * If the pack file contains no objects, we may only need to update
695 * references in our repository. The caller will take care of that.
700 if (lseek(packfd, 0, SEEK_SET) == -1) {
701 err = got_error_from_errno("lseek");
705 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
706 err = got_error_from_errno("socketpair");
711 err= got_error_from_errno("fork");
713 } else if (idxpid == 0)
714 got_privsep_exec_child(imsg_idxfds,
715 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
716 if (close(imsg_idxfds[1]) != 0) {
717 err = got_error_from_errno("close");
720 imsg_init(&idxibuf, imsg_idxfds[0]);
722 npackfd = dup(packfd);
724 err = got_error_from_errno("dup");
727 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
732 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
736 for (i = 0; i < nitems(tmpfds); i++) {
737 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
744 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
746 err = got_privsep_recv_index_progress(&done, &nobj_total,
747 &nobj_indexed, &nobj_loose, &nobj_resolved,
751 if (nobj_indexed != 0) {
752 err = progress_cb(progress_arg, NULL,
753 packfile_size, nobj_total,
754 nobj_indexed, nobj_loose, nobj_resolved);
758 imsg_clear(&idxibuf);
760 if (close(imsg_idxfds[0]) == -1) {
761 err = got_error_from_errno("close");
764 if (waitpid(idxpid, &idxstatus, 0) == -1) {
765 err = got_error_from_errno("waitpid");
769 err = got_object_id_str(&id_str, *pack_hash);
772 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
773 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
778 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
779 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
780 err = got_error_from_errno("asprintf");
784 if (rename(tmppackpath, packpath) == -1) {
785 err = got_error_from_errno3("rename", tmppackpath, packpath);
790 if (rename(tmpidxpath, idxpath) == -1) {
791 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
798 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
799 err = got_error_from_errno2("unlink", tmppackpath);
800 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
801 err = got_error_from_errno2("unlink", tmpidxpath);
802 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
803 err = got_error_from_errno("close");
804 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 if (packfd != -1 && close(packfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 for (i = 0; i < nitems(tmpfds); i++) {
811 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
812 err = got_error_from_errno("close");
821 TAILQ_FOREACH(pe, &have_refs, entry) {
822 free((char *)pe->path);
825 got_pathlist_free(&have_refs);
826 got_ref_list_free(&my_refs);
830 TAILQ_FOREACH(pe, refs, entry) {
831 free((void *)pe->path);
834 got_pathlist_free(refs);
835 TAILQ_FOREACH(pe, symrefs, entry) {
836 free((void *)pe->path);
839 got_pathlist_free(symrefs);