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;
97 argv[0] = GOT_FETCH_PATH_SSH;
98 if (verbosity == -1) {
101 /* ssh(1) allows up to 3 "-v" options. */
102 for (i = 0; i < MIN(3, verbosity); i++)
106 argv[2 + i] = (char *)host;
107 argv[3 + i] = (char *)cmd;
108 argv[4 + i] = (char *)path;
112 return got_error_from_errno("pipe");
116 error = got_error_from_errno("fork");
120 } else if (pid == 0) {
125 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
126 if (n < 0 || n >= sizeof(cmd))
128 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
130 abort(); /* not reached */
138 static const struct got_error *
139 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
140 const char *direction)
142 const struct got_error *err = NULL;
143 struct addrinfo hints, *servinfo, *p;
144 char *cmd = NULL, *pkt = NULL;
145 int fd = -1, totlen, r, eaicode;
149 memset(&hints, 0, sizeof hints);
150 hints.ai_family = AF_UNSPEC;
151 hints.ai_socktype = SOCK_STREAM;
152 eaicode = getaddrinfo(host, port, &hints, &servinfo);
155 snprintf(msg, sizeof(msg), "%s: %s", host,
156 gai_strerror(eaicode));
157 return got_error_msg(GOT_ERR_ADDRINFO, msg);
160 for (p = servinfo; p != NULL; p = p->ai_next) {
161 if ((fd = socket(p->ai_family, p->ai_socktype,
162 p->ai_protocol)) == -1)
164 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
166 err = got_error_from_errno("connect");
172 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
173 err = got_error_from_errno("asprintf");
176 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
177 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
178 err = got_error_from_errno("asprintf");
181 r = write(fd, pkt, strlen(pkt) + 1);
183 err = got_error_from_errno("write");
186 if (asprintf(&pkt, "host=%s", host) == -1) {
187 err = got_error_from_errno("asprintf");
190 r = write(fd, pkt, strlen(pkt) + 1);
192 err = got_error_from_errno("write");
206 const struct got_error *
207 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
208 const char *port, const char *server_path, int verbosity)
210 const struct got_error *err = NULL;
214 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
215 err = dial_ssh(fetchfd, host, port, server_path, "upload",
217 else if (strcmp(proto, "git") == 0)
218 err = dial_git(fetchfd, host, port, server_path, "upload");
219 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
220 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
222 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
226 const struct got_error *
227 got_fetch_parse_uri(char **proto, char **host, char **port,
228 char **server_path, char **repo_name, const char *uri)
230 const struct got_error *err = NULL;
234 *proto = *host = *port = *server_path = *repo_name = NULL;
236 p = strstr(uri, "://");
238 /* Try parsing Git's "scp" style URL syntax. */
239 *proto = strdup("ssh");
241 err = got_error_from_errno("strdup");
244 *port = strdup("22");
246 err = got_error_from_errno("strdup");
252 err = got_error(GOT_ERR_PARSE_URI);
255 /* No slashes allowed before first colon. */
258 err = got_error(GOT_ERR_PARSE_URI);
261 *host = strndup(s, q - s);
263 err = got_error_from_errno("strndup");
268 *proto = strndup(uri, p - uri);
270 err = got_error_from_errno("strndup");
276 if (p == NULL || strlen(p) == 1) {
277 err = got_error(GOT_ERR_PARSE_URI);
281 q = memchr(s, ':', p - s);
283 *host = strndup(s, q - s);
285 err = got_error_from_errno("strndup");
288 *port = strndup(q + 1, p - (q + 1));
290 err = got_error_from_errno("strndup");
294 *host = strndup(s, p - s);
296 err = got_error_from_errno("strndup");
299 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
300 err = got_error_from_errno("asprintf");
306 *server_path = strdup(p);
307 if (*server_path == NULL) {
308 err = got_error_from_errno("strdup");
313 if (!p || strlen(p) <= 1) {
314 err = got_error(GOT_ERR_PARSE_URI);
320 err = got_error(GOT_ERR_PARSE_URI);
323 if (hassuffix(p, ".git"))
325 *repo_name = strndup(p, (p + n) - p);
326 if (*repo_name == NULL) {
327 err = got_error_from_errno("strndup");
346 static const struct got_error *
347 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
350 uint8_t hexpect[SHA1_DIGEST_LENGTH];
351 uint8_t buf[32 * 1024];
354 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
355 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
359 while (n < sz - 20) {
361 if (sz - n - 20 < sizeof(buf))
363 r = read(fd, buf, nr);
365 return got_error_from_errno("read");
367 return got_error_msg(GOT_ERR_BAD_PACKFILE,
369 SHA1Update(&ctx, buf, nr);
372 SHA1Final(hcomp, &ctx);
374 r = read(fd, hexpect, sizeof(hexpect));
376 return got_error_from_errno("read");
377 if (r != sizeof(hexpect))
378 return got_error_msg(GOT_ERR_BAD_PACKFILE,
381 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
382 return got_error_msg(GOT_ERR_BAD_PACKFILE,
383 "packfile checksum mismatch");
388 const struct got_error*
389 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
390 struct got_pathlist_head *symrefs, const char *remote_name, int fetchfd,
391 struct got_repository *repo, got_fetch_progress_cb progress_cb,
394 int imsg_fetchfds[2], imsg_idxfds[2];
395 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
397 int fetchstatus, idxstatus, done = 0;
398 const struct got_error *err;
399 struct imsgbuf fetchibuf, idxibuf;
400 pid_t fetchpid, idxpid;
401 char *tmppackpath = NULL, *tmpidxpath = NULL;
402 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
403 const char *repo_path = got_repo_get_path(repo);
404 struct got_pathlist_head have_refs;
405 struct got_pathlist_entry *pe;
406 struct got_reflist_head my_refs;
407 struct got_reflist_entry *re;
408 off_t packfile_size = 0;
409 char *ref_prefix = NULL;
410 size_t ref_prefixlen = 0;
414 for (i = 0; i < nitems(tmpfds); i++)
417 TAILQ_INIT(&have_refs);
418 SIMPLEQ_INIT(&my_refs);
420 if (asprintf(&ref_prefix, "refs/remotes/%s/", remote_name) == -1)
421 return got_error_from_errno("asprintf");
422 ref_prefixlen = strlen(ref_prefix);
424 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
428 SIMPLEQ_FOREACH(re, &my_refs, entry) {
429 struct got_object_id *id;
432 if (got_ref_is_symbolic(re->ref))
435 refname = got_ref_get_name(re->ref);
436 if (strncmp("refs/tags/", refname, 10) == 0) {
439 err = got_ref_resolve(&id, repo, re->ref);
442 tagname = strdup(refname);
443 if (tagname == NULL) {
444 err = got_error_from_errno("strdup");
447 err = got_pathlist_append(&have_refs, tagname, id);
454 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
457 err = got_ref_resolve(&id, repo, re->ref);
461 if (asprintf(&branchname, "refs/heads/%s",
462 refname + ref_prefixlen) == -1) {
463 err = got_error_from_errno("asprintf");
466 err = got_pathlist_append(&have_refs, branchname, id);
474 if (asprintf(&path, "%s/%s/fetching.pack",
475 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
476 err = got_error_from_errno("asprintf");
479 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
483 npackfd = dup(packfd);
485 err = got_error_from_errno("dup");
488 if (asprintf(&path, "%s/%s/fetching.idx",
489 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
490 err = got_error_from_errno("asprintf");
493 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
499 err = got_error_from_errno("dup");
503 for (i = 0; i < nitems(tmpfds); i++) {
504 tmpfds[i] = got_opentempfd();
505 if (tmpfds[i] == -1) {
506 err = got_error_from_errno("got_opentempfd");
511 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
512 err = got_error_from_errno("socketpair");
517 if (fetchpid == -1) {
518 err = got_error_from_errno("fork");
520 } else if (fetchpid == 0){
521 got_privsep_exec_child(imsg_fetchfds,
522 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
525 if (close(imsg_fetchfds[1]) != 0) {
526 err = got_error_from_errno("close");
529 imsg_init(&fetchibuf, imsg_fetchfds[0]);
530 nfetchfd = dup(fetchfd);
531 if (nfetchfd == -1) {
532 err = got_error_from_errno("dup");
535 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
539 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
542 npackfd = dup(packfd);
544 err = got_error_from_errno("dup");
550 struct got_object_id *id = NULL;
551 char *refname = NULL;
552 char *server_progress = NULL;
553 off_t packfile_size_cur = 0;
555 err = got_privsep_recv_fetch_progress(&done,
556 &id, &refname, symrefs, &server_progress,
557 &packfile_size_cur, &fetchibuf);
561 if (packfile_size > 0)
566 else if (refname && id) {
567 err = got_pathlist_append(refs, refname, id);
570 } else if (server_progress) {
571 char *s, *s0 = server_progress;
572 while ((s = strsep(&s0, "\r")) != NULL) {
575 err = progress_cb(progress_arg, s,
576 packfile_size_cur, 0, 0, 0, 0);
580 free(server_progress);
583 } else if (packfile_size_cur != packfile_size) {
584 err = progress_cb(progress_arg, NULL,
585 packfile_size_cur, 0, 0, 0, 0);
588 packfile_size = packfile_size_cur;
591 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
592 err = got_error_from_errno("waitpid");
596 /* If zero data was fetched without error we are already up-to-date. */
597 if (packfile_size == 0)
600 if (lseek(packfd, 0, SEEK_SET) == -1) {
601 err = got_error_from_errno("lseek");
604 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
608 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
609 err = got_error_from_errno("socketpair");
614 err= got_error_from_errno("fork");
616 } else if (idxpid == 0)
617 got_privsep_exec_child(imsg_idxfds,
618 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
619 if (close(imsg_idxfds[1]) != 0) {
620 err = got_error_from_errno("close");
623 imsg_init(&idxibuf, imsg_idxfds[0]);
625 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
630 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
634 for (i = 0; i < nitems(tmpfds); i++) {
635 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
642 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
644 err = got_privsep_recv_index_progress(&done, &nobj_total,
645 &nobj_indexed, &nobj_loose, &nobj_resolved,
649 if (nobj_indexed != 0) {
650 err = progress_cb(progress_arg, NULL,
651 packfile_size, nobj_total,
652 nobj_indexed, nobj_loose, nobj_resolved);
656 imsg_clear(&idxibuf);
658 if (close(imsg_idxfds[0]) == -1) {
659 err = got_error_from_errno("close");
662 if (waitpid(idxpid, &idxstatus, 0) == -1) {
663 err = got_error_from_errno("waitpid");
667 err = got_object_id_str(&id_str, *pack_hash);
670 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
671 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
672 err = got_error_from_errno("asprintf");
676 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
677 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
678 err = got_error_from_errno("asprintf");
682 if (rename(tmppackpath, packpath) == -1) {
683 err = got_error_from_errno3("rename", tmppackpath, packpath);
688 if (rename(tmpidxpath, idxpath) == -1) {
689 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
696 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
697 err = got_error_from_errno2("unlink", tmppackpath);
698 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
699 err = got_error_from_errno2("unlink", tmpidxpath);
700 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
701 err = got_error_from_errno("close");
702 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
703 err = got_error_from_errno("close");
704 if (packfd != -1 && close(packfd) == -1 && err == NULL)
705 err = got_error_from_errno("close");
706 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
707 err = got_error_from_errno("close");
708 for (i = 0; i < nitems(tmpfds); i++) {
709 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
710 err = got_error_from_errno("close");
718 TAILQ_FOREACH(pe, &have_refs, entry) {
719 free((char *)pe->path);
722 got_pathlist_free(&have_refs);
723 got_ref_list_free(&my_refs);
727 TAILQ_FOREACH(pe, refs, entry) {
728 free((void *)pe->path);
731 got_pathlist_free(refs);
732 TAILQ_FOREACH(pe, symrefs, entry) {
733 free((void *)pe->path);
736 got_pathlist_free(symrefs);