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(int *fetchfd, const char *host, const char *port, const char *path,
87 const char *direction, int verbosity)
89 const struct got_error *error = NULL;
100 argv[0] = GOT_FETCH_PATH_SSH;
102 argv[2] = (char *)port;
103 if (verbosity == -1) {
104 argv[3 + i++] = "-q";
106 /* ssh(1) allows up to 3 "-v" options. */
107 for (i = 0; i < MIN(3, verbosity); i++)
111 argv[4 + i] = (char *)host;
112 argv[5 + i] = (char *)cmd;
113 argv[6 + 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 */
143 static const struct got_error *
144 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
145 const char *direction)
147 const struct got_error *err = NULL;
148 struct addrinfo hints, *servinfo, *p;
149 char *cmd = NULL, *pkt = NULL;
150 int fd = -1, totlen, r, eaicode;
155 port = GOT_DEFAULT_GIT_PORT_STR;
157 memset(&hints, 0, sizeof hints);
158 hints.ai_family = AF_UNSPEC;
159 hints.ai_socktype = SOCK_STREAM;
160 eaicode = getaddrinfo(host, port, &hints, &servinfo);
163 snprintf(msg, sizeof(msg), "%s: %s", host,
164 gai_strerror(eaicode));
165 return got_error_msg(GOT_ERR_ADDRINFO, msg);
168 for (p = servinfo; p != NULL; p = p->ai_next) {
169 if ((fd = socket(p->ai_family, p->ai_socktype,
170 p->ai_protocol)) == -1)
172 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
174 err = got_error_from_errno("connect");
180 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
181 err = got_error_from_errno("asprintf");
184 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
185 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
186 err = got_error_from_errno("asprintf");
189 r = write(fd, pkt, strlen(pkt) + 1);
191 err = got_error_from_errno("write");
194 if (asprintf(&pkt, "host=%s", host) == -1) {
195 err = got_error_from_errno("asprintf");
198 r = write(fd, pkt, strlen(pkt) + 1);
200 err = got_error_from_errno("write");
214 const struct got_error *
215 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
216 const char *port, const char *server_path, int verbosity)
218 const struct got_error *err = NULL;
222 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
223 err = dial_ssh(fetchfd, host, port, server_path, "upload",
225 else if (strcmp(proto, "git") == 0)
226 err = dial_git(fetchfd, host, port, server_path, "upload");
227 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
228 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
230 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
234 const struct got_error *
235 got_fetch_parse_uri(char **proto, char **host, char **port,
236 char **server_path, char **repo_name, const char *uri)
238 const struct got_error *err = NULL;
242 *proto = *host = *port = *server_path = *repo_name = NULL;
244 p = strstr(uri, "://");
246 /* Try parsing Git's "scp" style URL syntax. */
247 *proto = strdup("ssh");
249 err = got_error_from_errno("strdup");
255 err = got_error(GOT_ERR_PARSE_URI);
258 /* No slashes allowed before first colon. */
261 err = got_error(GOT_ERR_PARSE_URI);
264 *host = strndup(s, q - s);
266 err = got_error_from_errno("strndup");
271 *proto = strndup(uri, p - uri);
273 err = got_error_from_errno("strndup");
279 if (p == NULL || strlen(p) == 1) {
280 err = got_error(GOT_ERR_PARSE_URI);
284 q = memchr(s, ':', p - s);
286 *host = strndup(s, q - s);
288 err = got_error_from_errno("strndup");
291 *port = strndup(q + 1, p - (q + 1));
293 err = got_error_from_errno("strndup");
297 *host = strndup(s, p - s);
299 err = got_error_from_errno("strndup");
305 *server_path = strdup(p);
306 if (*server_path == NULL) {
307 err = got_error_from_errno("strdup");
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 static const struct got_error *
346 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
349 uint8_t hexpect[SHA1_DIGEST_LENGTH];
350 uint8_t buf[32 * 1024];
353 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
354 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
358 while (n < sz - 20) {
360 if (sz - n - 20 < sizeof(buf))
362 r = read(fd, buf, nr);
364 return got_error_from_errno("read");
366 return got_error_msg(GOT_ERR_BAD_PACKFILE,
368 SHA1Update(&ctx, buf, nr);
371 SHA1Final(hcomp, &ctx);
373 r = read(fd, hexpect, sizeof(hexpect));
375 return got_error_from_errno("read");
376 if (r != sizeof(hexpect))
377 return got_error_msg(GOT_ERR_BAD_PACKFILE,
380 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
381 return got_error_msg(GOT_ERR_BAD_PACKFILE,
382 "packfile checksum mismatch");
387 const struct got_error*
388 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
389 struct got_pathlist_head *symrefs, const char *remote_name,
390 int mirror_references, int fetchfd, struct got_repository *repo,
391 got_fetch_progress_cb progress_cb, void *progress_arg)
393 int imsg_fetchfds[2], imsg_idxfds[2];
394 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
396 int fetchstatus, idxstatus, done = 0;
397 const struct got_error *err;
398 struct imsgbuf fetchibuf, idxibuf;
399 pid_t fetchpid, idxpid;
400 char *tmppackpath = NULL, *tmpidxpath = NULL;
401 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
402 const char *repo_path = got_repo_get_path_git_dir(repo);
403 struct got_pathlist_head have_refs;
404 struct got_pathlist_entry *pe;
405 struct got_reflist_head my_refs;
406 struct got_reflist_entry *re;
407 off_t packfile_size = 0;
408 char *ref_prefix = NULL;
409 size_t ref_prefixlen = 0;
411 char *progress = NULL;
414 for (i = 0; i < nitems(tmpfds); i++)
417 TAILQ_INIT(&have_refs);
418 SIMPLEQ_INIT(&my_refs);
420 if (!mirror_references) {
421 if (asprintf(&ref_prefix, "refs/remotes/%s/",
423 return got_error_from_errno("asprintf");
424 ref_prefixlen = strlen(ref_prefix);
427 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
431 SIMPLEQ_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 (asprintf(&path, "%s/%s/fetching.pack",
495 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
496 err = got_error_from_errno("asprintf");
499 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
503 npackfd = dup(packfd);
505 err = got_error_from_errno("dup");
508 if (asprintf(&path, "%s/%s/fetching.idx",
509 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
510 err = got_error_from_errno("asprintf");
513 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
519 err = got_error_from_errno("dup");
523 for (i = 0; i < nitems(tmpfds); i++) {
524 tmpfds[i] = got_opentempfd();
525 if (tmpfds[i] == -1) {
526 err = got_error_from_errno("got_opentempfd");
531 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
532 err = got_error_from_errno("socketpair");
537 if (fetchpid == -1) {
538 err = got_error_from_errno("fork");
540 } else if (fetchpid == 0){
541 got_privsep_exec_child(imsg_fetchfds,
542 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
545 if (close(imsg_fetchfds[1]) != 0) {
546 err = got_error_from_errno("close");
549 imsg_init(&fetchibuf, imsg_fetchfds[0]);
550 nfetchfd = dup(fetchfd);
551 if (nfetchfd == -1) {
552 err = got_error_from_errno("dup");
555 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
559 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
562 npackfd = dup(packfd);
564 err = got_error_from_errno("dup");
569 progress = calloc(GOT_FETCH_PKTMAX, 1);
570 if (progress == NULL) {
571 err = got_error_from_errno("calloc");
575 struct got_object_id *id = NULL;
576 char *refname = NULL;
577 char *server_progress = NULL;
578 off_t packfile_size_cur = 0;
580 err = got_privsep_recv_fetch_progress(&done,
581 &id, &refname, symrefs, &server_progress,
582 &packfile_size_cur, &fetchibuf);
586 if (packfile_size > 0)
590 } else if (refname && id) {
591 err = got_pathlist_append(refs, refname, id);
594 } else if (server_progress) {
597 * XXX git-daemon tends to send batched output with
598 * lines spanning separate packets. Buffer progress
599 * output until we see a CR or LF to avoid giving
600 * partial lines of progress output to the callback.
602 if (strlcat(progress, server_progress,
603 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
604 progress[0] = '\0'; /* discard */
607 while ((p = strchr(progress, '\r')) != NULL ||
608 (p = strchr(progress, '\n')) != NULL) {
613 if (asprintf(&s, "%s%s", progress,
614 c == '\n' ? "\n" : "") == -1) {
615 err = got_error_from_errno("asprintf");
618 err = progress_cb(progress_arg, s,
619 packfile_size_cur, 0, 0, 0, 0);
623 n = strlen(progress);
624 if (n < GOT_FETCH_PKTMAX - 1) {
625 memmove(progress, &progress[n + 1],
626 GOT_FETCH_PKTMAX - n - 1);
630 free(server_progress);
633 } else if (packfile_size_cur != packfile_size) {
634 err = progress_cb(progress_arg, NULL,
635 packfile_size_cur, 0, 0, 0, 0);
638 packfile_size = packfile_size_cur;
641 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
642 err = got_error_from_errno("waitpid");
646 /* If zero data was fetched without error we are already up-to-date. */
647 if (packfile_size == 0)
650 if (lseek(packfd, 0, SEEK_SET) == -1) {
651 err = got_error_from_errno("lseek");
654 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
658 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
659 err = got_error_from_errno("socketpair");
664 err= got_error_from_errno("fork");
666 } else if (idxpid == 0)
667 got_privsep_exec_child(imsg_idxfds,
668 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
669 if (close(imsg_idxfds[1]) != 0) {
670 err = got_error_from_errno("close");
673 imsg_init(&idxibuf, imsg_idxfds[0]);
675 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
680 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
684 for (i = 0; i < nitems(tmpfds); i++) {
685 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
692 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
694 err = got_privsep_recv_index_progress(&done, &nobj_total,
695 &nobj_indexed, &nobj_loose, &nobj_resolved,
699 if (nobj_indexed != 0) {
700 err = progress_cb(progress_arg, NULL,
701 packfile_size, nobj_total,
702 nobj_indexed, nobj_loose, nobj_resolved);
706 imsg_clear(&idxibuf);
708 if (close(imsg_idxfds[0]) == -1) {
709 err = got_error_from_errno("close");
712 if (waitpid(idxpid, &idxstatus, 0) == -1) {
713 err = got_error_from_errno("waitpid");
717 err = got_object_id_str(&id_str, *pack_hash);
720 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
721 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
722 err = got_error_from_errno("asprintf");
726 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
727 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
728 err = got_error_from_errno("asprintf");
732 if (rename(tmppackpath, packpath) == -1) {
733 err = got_error_from_errno3("rename", tmppackpath, packpath);
738 if (rename(tmpidxpath, idxpath) == -1) {
739 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
746 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
747 err = got_error_from_errno2("unlink", tmppackpath);
748 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
749 err = got_error_from_errno2("unlink", tmpidxpath);
750 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
751 err = got_error_from_errno("close");
752 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
753 err = got_error_from_errno("close");
754 if (packfd != -1 && close(packfd) == -1 && err == NULL)
755 err = got_error_from_errno("close");
756 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
757 err = got_error_from_errno("close");
758 for (i = 0; i < nitems(tmpfds); i++) {
759 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
760 err = got_error_from_errno("close");
769 TAILQ_FOREACH(pe, &have_refs, entry) {
770 free((char *)pe->path);
773 got_pathlist_free(&have_refs);
774 got_ref_list_free(&my_refs);
778 TAILQ_FOREACH(pe, refs, entry) {
779 free((void *)pe->path);
782 got_pathlist_free(refs);
783 TAILQ_FOREACH(pe, symrefs, entry) {
784 free((void *)pe->path);
787 got_pathlist_free(symrefs);