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"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
64 #define GOT_PROTOMAX 64
65 #define GOT_HOSTMAX 256
66 #define GOT_PATHMAX 512
67 #define GOT_REPOMAX 256
68 #define GOT_PORTMAX 16
69 #define GOT_URIMAX 1024
74 char *p, namebuf[PATH_MAX];
80 if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
92 if (mkdir(namebuf, 0755) != 0) {
93 int mkdir_errno = errno;
94 if (stat(path, &sb) == -1) {
95 /* Not there; use mkdir()s errno */
99 if (!S_ISDIR(sb.st_mode)) {
100 /* Is there, but isn't a directory */
115 hassuffix(char *base, char *suf)
121 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
127 grab(char *dst, int n, char *p, char *e)
133 errno = ENAMETOOLONG;
136 return strlcpy(dst, p, l + 1);
139 static const struct got_error *
140 dial_ssh(int *fetchfd, char *host, char *port, char *path, char *direction)
142 const struct got_error *error = NULL;
149 return got_error_from_errno("pipe");
153 error = got_error_from_errno("fork");
157 } else if (pid == 0) {
162 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
163 if (n < 0 || n >= sizeof(cmd))
165 if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
167 abort(); /* not reached */
175 static const struct got_error *
176 dial_git(int *fetchfd, char *host, char *port, char *path, char *direction)
178 const struct got_error *err = NULL;
179 struct addrinfo hints, *servinfo, *p;
180 char *cmd = NULL, *pkt = NULL;
181 int fd = -1, totlen, r, eaicode;
185 memset(&hints, 0, sizeof hints);
186 hints.ai_family = AF_UNSPEC;
187 hints.ai_socktype = SOCK_STREAM;
188 eaicode = getaddrinfo(host, port, &hints, &servinfo);
190 return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
192 for (p = servinfo; p != NULL; p = p->ai_next) {
193 if ((fd = socket(p->ai_family, p->ai_socktype,
194 p->ai_protocol)) == -1)
196 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
198 err = got_error_from_errno("connect");
204 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
205 err = got_error_from_errno("asprintf");
208 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
209 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
210 err = got_error_from_errno("asprintf");
213 r = write(fd, pkt, strlen(pkt) + 1);
215 err = got_error_from_errno("write");
218 if (asprintf(&pkt, "host=%s", host) == -1) {
219 err = got_error_from_errno("asprintf");
222 r = write(fd, pkt, strlen(pkt) + 1);
224 err = got_error_from_errno("write");
239 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
244 p = strstr(uri, "://");
246 //werrstr("missing protocol");
249 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
251 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
261 if (p == NULL || strlen(p) == 1) {
262 //werrstr("missing path");
266 q = memchr(s, ':', p - s);
268 grab(host, GOT_HOSTMAX, s, q);
269 grab(port, GOT_PORTMAX, q + 1, p);
271 grab(host, GOT_HOSTMAX, s, p);
272 snprintf(port, GOT_PORTMAX, "9418");
275 snprintf(path, GOT_PATHMAX, "%s", p);
276 p = strrchr(p, '/') + 1;
277 if (!p || strlen(p) == 0) {
278 //werrstr("missing repository in uri");
282 if (hassuffix(p, ".git"))
284 grab(repo, GOT_REPOMAX, p, p + n);
288 const struct got_error*
289 got_fetch(char *uri, char *branch_filter, char *destdir)
291 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
292 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
293 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
294 int packfd = -1, npackfd, idxfd = -1, nidxfd, status, done = 0;
295 struct got_object_id *packhash = NULL;
296 const struct got_error *err;
299 char *tmppackpath = NULL, *tmpidxpath = NULL, *default_destdir = NULL;
300 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
303 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
304 return got_error(GOT_ERR_PARSE_URI);
305 if (destdir == NULL) {
306 if (asprintf(&default_destdir, "%s.git", repo) == -1)
307 return got_error_from_errno("asprintf");
309 err = got_repo_init(destdir ? destdir : default_destdir);
312 if (chdir(destdir ? destdir : default_destdir))
313 return got_error_from_errno("enter new repo");
314 if (mkpath("objects/pack") == -1)
315 return got_error_from_errno("mkpath");
316 err = got_opentemp_named_fd(&tmppackpath, &packfd,
317 "objects/pack/fetching.pack");
320 npackfd = dup(packfd);
322 return got_error_from_errno("dup");
323 err = got_opentemp_named_fd(&tmpidxpath, &idxfd,
324 "objects/pack/fetching.idx");
329 return got_error_from_errno("dup");
331 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
332 err = dial_ssh(&fetchfd, host, port, path, "upload");
333 else if (strcmp(proto, "git") == 0)
334 err = dial_git(&fetchfd, host, port, path, "upload");
335 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
336 err = got_error(GOT_ERR_BAD_PROTO);
338 err = got_error(GOT_ERR_BAD_PROTO);
342 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
343 return got_error_from_errno("socketpair");
347 return got_error_from_errno("fork");
349 got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
352 if (close(imsg_fetchfds[1]) != 0)
353 return got_error_from_errno("close");
354 imsg_init(&ibuf, imsg_fetchfds[0]);
355 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
358 err = got_privsep_send_tmpfd(&ibuf, npackfd);
361 npackfd = dup(packfd);
363 return got_error_from_errno("dup");
365 struct got_object_id *id;
367 err = got_privsep_recv_fetch_progress(&done,
368 &id, &refname, &ibuf);
372 packhash = got_object_id_dup(id);
373 if (packhash == NULL)
374 return got_error_from_errno(
375 "got_object_id_dup");
378 /* TODO Use a progress callback */
379 err = got_object_id_str(&id_str, id);
382 printf( "%.12s %s\n", id_str, refname);
385 if (waitpid(pid, &status, 0) == -1)
386 return got_error_from_errno("child exit");
388 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
389 return got_error_from_errno("socketpair");
392 return got_error_from_errno("fork");
394 got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
395 if (close(imsg_idxfds[1]) != 0)
396 return got_error_from_errno("close");
397 imsg_init(&ibuf, imsg_idxfds[0]);
399 err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
402 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
405 err = got_privsep_wait_index_pack_done(&ibuf);
409 if (close(imsg_idxfds[0]) == -1)
410 return got_error_from_errno("close child");
411 if (waitpid(pid, &status, 0) == -1)
412 return got_error_from_errno("child exit");
414 err = got_object_id_str(&id_str, packhash);
417 if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1)
418 return got_error_from_errno("asprintf");
420 if (asprintf(&idxpath, "objects/pack/pack-%s.idx", id_str) == -1)
421 return got_error_from_errno("asprintf");
423 if (rename(tmppackpath, packpath) == -1)
424 return got_error_from_errno3("rename", tmppackpath, packpath);
425 if (rename(tmpidxpath, idxpath) == -1)
426 return got_error_from_errno3("rename", tmpidxpath, idxpath);
432 free(default_destdir);