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"
65 #define GOT_PROTOMAX 64
66 #define GOT_HOSTMAX 256
67 #define GOT_PATHMAX 512
68 #define GOT_REPOMAX 256
69 #define GOT_PORTMAX 16
70 #define GOT_URIMAX 1024
73 hassuffix(char *base, char *suf)
79 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
84 static const struct got_error *
85 dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
86 const char *direction)
88 const struct got_error *error = NULL;
95 return got_error_from_errno("pipe");
99 error = got_error_from_errno("fork");
103 } else if (pid == 0) {
108 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
109 if (n < 0 || n >= sizeof(cmd))
111 if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
113 abort(); /* not reached */
121 static const struct got_error *
122 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
123 const char *direction)
125 const struct got_error *err = NULL;
126 struct addrinfo hints, *servinfo, *p;
127 char *cmd = NULL, *pkt = NULL;
128 int fd = -1, totlen, r, eaicode;
132 memset(&hints, 0, sizeof hints);
133 hints.ai_family = AF_UNSPEC;
134 hints.ai_socktype = SOCK_STREAM;
135 eaicode = getaddrinfo(host, port, &hints, &servinfo);
137 return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
139 for (p = servinfo; p != NULL; p = p->ai_next) {
140 if ((fd = socket(p->ai_family, p->ai_socktype,
141 p->ai_protocol)) == -1)
143 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
145 err = got_error_from_errno("connect");
151 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
152 err = got_error_from_errno("asprintf");
155 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
156 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
157 err = got_error_from_errno("asprintf");
160 r = write(fd, pkt, strlen(pkt) + 1);
162 err = got_error_from_errno("write");
165 if (asprintf(&pkt, "host=%s", host) == -1) {
166 err = got_error_from_errno("asprintf");
169 r = write(fd, pkt, strlen(pkt) + 1);
171 err = got_error_from_errno("write");
185 const struct got_error *
186 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
187 const char *port, const char *server_path)
189 const struct got_error *err = NULL;
193 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
194 err = dial_ssh(fetchfd, host, port, server_path, "upload");
195 else if (strcmp(proto, "git") == 0)
196 err = dial_git(fetchfd, host, port, server_path, "upload");
197 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
198 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
200 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
204 const struct got_error *
205 got_fetch_parse_uri(char **proto, char **host, char **port,
206 char **server_path, char **repo_name, const char *uri)
208 const struct got_error *err = NULL;
212 *proto = *host = *port = *server_path = *repo_name = NULL;
214 p = strstr(uri, "://");
216 return got_error(GOT_ERR_PARSE_URI);
218 *proto = strndup(uri, p - uri);
220 err = got_error_from_errno("strndup");
224 hasport = (strcmp(*proto, "git") == 0 ||
225 strstr(*proto, "http") == *proto);
235 if (p == NULL || strlen(p) == 1) {
236 err = got_error(GOT_ERR_PARSE_URI);
240 q = memchr(s, ':', p - s);
242 *host = strndup(s, q - s);
244 err = got_error_from_errno("strndup");
247 *port = strndup(q + 1, p - (q + 1));
249 err = got_error_from_errno("strndup");
253 *host = strndup(s, p - s);
255 err = got_error_from_errno("strndup");
258 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
259 err = got_error_from_errno("asprintf");
264 *server_path = strdup(p);
265 if (*server_path == NULL) {
266 err = got_error_from_errno("strdup");
270 p = strrchr(p, '/') + 1;
271 if (!p || strlen(p) == 0) {
272 //werrstr("missing repository in uri");
273 err = got_error(GOT_ERR_PARSE_URI);
277 if (hassuffix(p, ".git"))
279 *repo_name = strndup(p, (p + n) - p);
280 if (*repo_name == NULL) {
281 err = got_error_from_errno("strndup");
300 const struct got_error*
301 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
302 struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
303 got_fetch_progress_cb progress_cb, void *progress_arg)
305 int imsg_fetchfds[2], imsg_idxfds[2];
306 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
307 int status, done = 0;
308 const struct got_error *err;
311 char *tmppackpath = NULL, *tmpidxpath = NULL;
312 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
313 const char *repo_path = got_repo_get_path(repo);
314 struct got_pathlist_head have_refs;
315 struct got_pathlist_entry *pe;
320 TAILQ_INIT(&have_refs);
322 if (asprintf(&path, "%s/%s/fetching.pack",
323 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
324 err = got_error_from_errno("asprintf");
327 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
331 npackfd = dup(packfd);
333 err = got_error_from_errno("dup");
336 if (asprintf(&path, "%s/%s/fetching.idx",
337 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
338 err = got_error_from_errno("asprintf");
341 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
347 err = got_error_from_errno("dup");
351 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
352 err = got_error_from_errno("socketpair");
358 err = got_error_from_errno("fork");
360 } else if (pid == 0){
361 got_privsep_exec_child(imsg_fetchfds,
362 GOT_PATH_PROG_FETCH_PACK, ".");
365 if (close(imsg_fetchfds[1]) != 0) {
366 err = got_error_from_errno("close");
369 imsg_init(&ibuf, imsg_fetchfds[0]);
370 nfetchfd = dup(fetchfd);
371 if (nfetchfd == -1) {
372 err = got_error_from_errno("dup");
375 err = got_privsep_send_fetch_req(&ibuf, nfetchfd, &have_refs);
379 err = got_privsep_send_tmpfd(&ibuf, npackfd);
382 npackfd = dup(packfd);
384 err = got_error_from_errno("dup");
389 struct got_object_id *id = NULL;
390 char *refname = NULL;
391 char *server_progress = NULL;
393 err = got_privsep_recv_fetch_progress(&done,
394 &id, &refname, symrefs, &server_progress, &ibuf);
399 else if (refname && id) {
400 err = got_pathlist_append(refs, refname, id);
403 } else if (server_progress) {
404 char *s, *s0 = server_progress;
405 while ((s = strsep(&s0, "\r")) != NULL) {
408 err = progress_cb(progress_arg, s);
412 free(server_progress);
417 if (waitpid(pid, &status, 0) == -1) {
418 err = got_error_from_errno("waitpid");
422 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
423 err = got_error_from_errno("socketpair");
428 err= got_error_from_errno("fork");
431 got_privsep_exec_child(imsg_idxfds,
432 GOT_PATH_PROG_INDEX_PACK, ".");
433 if (close(imsg_idxfds[1]) != 0) {
434 err = got_error_from_errno("close");
437 imsg_init(&ibuf, imsg_idxfds[0]);
439 err = got_privsep_send_index_pack_req(&ibuf, npackfd, *pack_hash);
443 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
447 err = got_privsep_wait_index_pack_done(&ibuf);
451 if (close(imsg_idxfds[0]) == -1) {
452 err = got_error_from_errno("close");
455 if (waitpid(pid, &status, 0) == -1) {
456 err = got_error_from_errno("waitpid");
460 err = got_object_id_str(&id_str, *pack_hash);
463 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
464 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
465 err = got_error_from_errno("asprintf");
469 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
470 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
471 err = got_error_from_errno("asprintf");
475 if (rename(tmppackpath, packpath) == -1) {
476 err = got_error_from_errno3("rename", tmppackpath, packpath);
479 if (rename(tmpidxpath, idxpath) == -1) {
480 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
485 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
486 err = got_error_from_errno("close");
487 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
488 err = got_error_from_errno("close");
489 if (packfd != -1 && close(packfd) == -1 && err == NULL)
490 err = got_error_from_errno("close");
491 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
492 err = got_error_from_errno("close");
501 TAILQ_FOREACH(pe, refs, entry) {
502 free((void *)pe->path);
505 got_pathlist_free(refs);
506 TAILQ_FOREACH(pe, symrefs, entry) {
507 free((void *)pe->path);
510 got_pathlist_free(symrefs);