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>
41 #include <netinet/in.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
62 #define GOT_PROTOMAX 64
63 #define GOT_HOSTMAX 256
64 #define GOT_PATHMAX 512
65 #define GOT_REPOMAX 256
66 #define GOT_PORTMAX 16
67 #define GOT_URIMAX 1024
72 char *p, namebuf[PATH_MAX];
78 if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
90 if (mkdir(namebuf, 0755) != 0) {
91 int mkdir_errno = errno;
92 if (stat(path, &sb) == -1) {
93 /* Not there; use mkdir()s errno */
97 if (!S_ISDIR(sb.st_mode)) {
98 /* Is there, but isn't a directory */
113 hassuffix(char *base, char *suf)
119 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
125 grab(char *dst, int n, char *p, char *e)
131 errno = ENAMETOOLONG;
134 return strlcpy(dst, p, l + 1);
138 got_dial_ssh(char *host, char *port, char *path, char *direction)
152 snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
153 execlp("ssh", "ssh", host, cmd, path, NULL);
162 got_dial_git(char *host, char *port, char *path, char *direction)
164 struct addrinfo hints, *servinfo, *p;
168 memset(&hints, 0, sizeof hints);
169 hints.ai_family = AF_UNSPEC;
170 hints.ai_socktype = SOCK_STREAM;
171 if (getaddrinfo(host, port, &hints, &servinfo) != 0)
174 for (p = servinfo; p != NULL; p = p->ai_next) {
175 if ((fd = socket(p->ai_family, p->ai_socktype,
176 p->ai_protocol)) == -1)
178 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
185 if ((l = asprintf(&cmd, "git-%s-pack %s\n", direction, path)) == -1)
187 if ((l = asprintf(&pkt, "%04x%s", l+4, cmd)) == -1)
189 r = write(fd, pkt, l);
200 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
205 p = strstr(uri, "://");
207 //werrstr("missing protocol");
210 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
212 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
222 if (p == NULL || strlen(p) == 1) {
223 //werrstr("missing path");
227 q = memchr(s, ':', p - s);
229 grab(host, GOT_HOSTMAX, s, q);
230 grab(port, GOT_PORTMAX, q + 1, p);
232 grab(host, GOT_HOSTMAX, s, p);
233 snprintf(port, GOT_PORTMAX, "9418");
236 snprintf(path, GOT_PATHMAX, "%s", p);
237 p = strrchr(p, '/') + 1;
238 if (!p || strlen(p) == 0) {
239 //werrstr("missing repository in uri");
243 if (hassuffix(p, ".git"))
245 grab(repo, GOT_REPOMAX, p, p + n);
249 const struct got_error*
250 got_clone(char *uri, char *branch_filter, char *dirname)
252 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
253 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
254 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
255 int packfd, npackfd, idxfd, nidxfd, status;
256 struct got_object_id packhash;
257 const struct got_error *err;
262 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
263 return got_error(GOT_ERR_PARSE_URI);
266 err = got_repo_init(dirname);
270 return got_error_from_errno("enter new repo");
271 if (mkpath(".git/objects/pack") == -1)
272 return got_error_from_errno("mkpath");
273 packfd = open(".git/objects/pack/fetching.pack", O_CREAT|O_RDWR, 0644);
275 return got_error_from_errno("open pack");
276 npackfd = dup(packfd);
278 return got_error_from_errno("dup");
279 idxfd = open(".git/objects/pack/fetching.idx", O_CREAT|O_RDWR, 0644);
281 return got_error_from_errno("open pack");
284 return got_error_from_errno("dup");
286 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
287 fetchfd = got_dial_ssh(host, port, path, "upload");
288 else if (strcmp(proto, "git") == 0)
289 fetchfd = got_dial_git(host, port, path, "upload");
290 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
291 err = got_error(GOT_ERR_BAD_PROTO);
293 err = got_error(GOT_ERR_BAD_PROTO);
296 err = got_error_from_errno("dial uri");
297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
298 return got_error_from_errno("socketpair");
302 return got_error_from_errno("fork");
304 got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
307 if (close(imsg_fetchfds[1]) != 0)
308 return got_error_from_errno("close");
309 imsg_init(&ibuf, imsg_fetchfds[0]);
310 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
313 err = got_privsep_wait_ack(&ibuf);
316 err = got_privsep_send_tmpfd(&ibuf, npackfd);
319 npackfd = dup(packfd);
321 return got_error_from_errno("dup");
322 err = got_privsep_wait_fetch_done(&ibuf, &packhash);
325 if (waitpid(pid, &status, 0) == -1)
326 return got_error_from_errno("child exit");
328 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
329 return got_error_from_errno("socketpair");
332 return got_error_from_errno("fork");
334 got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
335 if (close(imsg_idxfds[1]) != 0)
336 return got_error_from_errno("close");
337 imsg_init(&ibuf, imsg_idxfds[0]);
339 err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
342 err = got_privsep_wait_ack(&ibuf);
345 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
348 err = got_privsep_wait_index_pack_done(&ibuf);
352 if (close(imsg_idxfds[0]) == -1)
353 return got_error_from_errno("close child");
354 if (waitpid(pid, &status, 0) == -1)
355 return got_error_from_errno("child exit");