2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/queue.h>
20 93658fb9 2020-03-18 stsp #include <sys/uio.h>
21 93658fb9 2020-03-18 stsp #include <sys/socket.h>
22 93658fb9 2020-03-18 stsp #include <sys/wait.h>
23 93658fb9 2020-03-18 stsp #include <sys/resource.h>
24 93658fb9 2020-03-18 stsp #include <sys/socket.h>
26 78fb0967 2020-09-09 naddy #include <endian.h>
27 93658fb9 2020-03-18 stsp #include <errno.h>
28 5cc27ede 2020-03-18 stsp #include <err.h>
29 93658fb9 2020-03-18 stsp #include <fcntl.h>
30 93658fb9 2020-03-18 stsp #include <stdio.h>
31 93658fb9 2020-03-18 stsp #include <stdlib.h>
32 93658fb9 2020-03-18 stsp #include <string.h>
33 93658fb9 2020-03-18 stsp #include <stdint.h>
34 93658fb9 2020-03-18 stsp #include <sha1.h>
35 81a12da5 2020-09-09 naddy #include <unistd.h>
36 93658fb9 2020-03-18 stsp #include <zlib.h>
37 93658fb9 2020-03-18 stsp #include <ctype.h>
38 93658fb9 2020-03-18 stsp #include <limits.h>
39 93658fb9 2020-03-18 stsp #include <imsg.h>
40 93658fb9 2020-03-18 stsp #include <time.h>
41 93658fb9 2020-03-18 stsp #include <uuid.h>
42 93658fb9 2020-03-18 stsp #include <netdb.h>
43 93658fb9 2020-03-18 stsp #include <netinet/in.h>
45 93658fb9 2020-03-18 stsp #include "got_error.h"
46 93658fb9 2020-03-18 stsp #include "got_reference.h"
47 93658fb9 2020-03-18 stsp #include "got_repository.h"
48 93658fb9 2020-03-18 stsp #include "got_path.h"
49 93658fb9 2020-03-18 stsp #include "got_cancel.h"
50 93658fb9 2020-03-18 stsp #include "got_worktree.h"
51 93658fb9 2020-03-18 stsp #include "got_object.h"
52 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
53 82ebf666 2020-03-18 stsp #include "got_fetch.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
59 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
60 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
61 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
62 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
63 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
64 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
66 d582f26c 2020-03-18 stsp #ifndef nitems
67 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 68999b92 2020-03-18 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
79 93658fb9 2020-03-18 stsp nb = strlen(base);
80 93658fb9 2020-03-18 stsp ns = strlen(suf);
81 93658fb9 2020-03-18 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
86 5cc27ede 2020-03-18 stsp static const struct got_error *
87 9c52365f 2020-03-21 stsp dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
88 9c52365f 2020-03-21 stsp const char *path, const char *direction, int verbosity)
90 5cc27ede 2020-03-18 stsp const struct got_error *error = NULL;
91 93658fb9 2020-03-18 stsp int pid, pfd[2];
92 93658fb9 2020-03-18 stsp char cmd[64];
93 62a4c94c 2020-03-20 stsp char *argv[11];
94 59d5e252 2020-04-21 semarie int i = 0, j;
96 9c52365f 2020-03-21 stsp *fetchpid = -1;
97 5cc27ede 2020-03-18 stsp *fetchfd = -1;
99 59d5e252 2020-04-21 semarie argv[i++] = GOT_FETCH_PATH_SSH;
100 59d5e252 2020-04-21 semarie if (port != NULL) {
101 59d5e252 2020-04-21 semarie argv[i++] = "-p";
102 59d5e252 2020-04-21 semarie argv[i++] = (char *)port;
104 68999b92 2020-03-18 stsp if (verbosity == -1) {
105 59d5e252 2020-04-21 semarie argv[i++] = "-q";
107 68999b92 2020-03-18 stsp /* ssh(1) allows up to 3 "-v" options. */
108 59d5e252 2020-04-21 semarie for (j = 0; j < MIN(3, verbosity); j++)
109 59d5e252 2020-04-21 semarie argv[i++] = "-v";
111 59d5e252 2020-04-21 semarie argv[i++] = "--";
112 59d5e252 2020-04-21 semarie argv[i++] = (char *)host;
113 59d5e252 2020-04-21 semarie argv[i++] = (char *)cmd;
114 59d5e252 2020-04-21 semarie argv[i++] = (char *)path;
115 59d5e252 2020-04-21 semarie argv[i++] = NULL;
117 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
118 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
120 93658fb9 2020-03-18 stsp pid = fork();
121 5cc27ede 2020-03-18 stsp if (pid == -1) {
122 5cc27ede 2020-03-18 stsp error = got_error_from_errno("fork");
123 5cc27ede 2020-03-18 stsp close(pfd[0]);
124 93658fb9 2020-03-18 stsp close(pfd[1]);
125 5cc27ede 2020-03-18 stsp return error;
126 5cc27ede 2020-03-18 stsp } else if (pid == 0) {
128 5cc27ede 2020-03-18 stsp close(pfd[1]);
129 93658fb9 2020-03-18 stsp dup2(pfd[0], 0);
130 93658fb9 2020-03-18 stsp dup2(pfd[0], 1);
131 5cc27ede 2020-03-18 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
132 5cc27ede 2020-03-18 stsp if (n < 0 || n >= sizeof(cmd))
133 5cc27ede 2020-03-18 stsp err(1, "snprintf");
134 68999b92 2020-03-18 stsp if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
135 ee448f5f 2020-03-18 stsp err(1, "execl");
136 5cc27ede 2020-03-18 stsp abort(); /* not reached */
138 93658fb9 2020-03-18 stsp close(pfd[0]);
139 9c52365f 2020-03-21 stsp *fetchpid = pid;
140 5cc27ede 2020-03-18 stsp *fetchfd = pfd[1];
141 5cc27ede 2020-03-18 stsp return NULL;
145 5cc27ede 2020-03-18 stsp static const struct got_error *
146 09838ffc 2020-03-18 stsp dial_git(int *fetchfd, const char *host, const char *port, const char *path,
147 09838ffc 2020-03-18 stsp const char *direction)
149 5cc27ede 2020-03-18 stsp const struct got_error *err = NULL;
150 93658fb9 2020-03-18 stsp struct addrinfo hints, *servinfo, *p;
151 5cc27ede 2020-03-18 stsp char *cmd = NULL, *pkt = NULL;
152 4312a498 2020-03-18 stsp int fd = -1, totlen, r, eaicode;
154 5cc27ede 2020-03-18 stsp *fetchfd = -1;
156 62a4c94c 2020-03-20 stsp if (port == NULL)
157 62a4c94c 2020-03-20 stsp port = GOT_DEFAULT_GIT_PORT_STR;
159 93658fb9 2020-03-18 stsp memset(&hints, 0, sizeof hints);
160 93658fb9 2020-03-18 stsp hints.ai_family = AF_UNSPEC;
161 93658fb9 2020-03-18 stsp hints.ai_socktype = SOCK_STREAM;
162 5cc27ede 2020-03-18 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
163 a117fd10 2020-03-18 stsp if (eaicode) {
164 a117fd10 2020-03-18 stsp char msg[512];
165 a117fd10 2020-03-18 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
166 a117fd10 2020-03-18 stsp gai_strerror(eaicode));
167 a117fd10 2020-03-18 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 93658fb9 2020-03-18 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
171 93658fb9 2020-03-18 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
172 93658fb9 2020-03-18 stsp p->ai_protocol)) == -1)
174 93658fb9 2020-03-18 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
176 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
179 93658fb9 2020-03-18 stsp if (p == NULL)
182 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
183 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
186 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
187 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
188 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
191 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
192 4312a498 2020-03-18 stsp if (r == -1) {
193 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
196 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
197 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
200 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
201 4312a498 2020-03-18 stsp if (r == -1) {
202 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
209 5cc27ede 2020-03-18 stsp if (fd != -1)
212 5cc27ede 2020-03-18 stsp *fetchfd = fd;
213 20eb36d0 2020-03-18 stsp return err;
216 20eb36d0 2020-03-18 stsp const struct got_error *
217 9c52365f 2020-03-21 stsp got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
218 9c52365f 2020-03-21 stsp const char *host, const char *port, const char *server_path, int verbosity)
220 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
222 9c52365f 2020-03-21 stsp *fetchpid = -1;
223 20eb36d0 2020-03-18 stsp *fetchfd = -1;
225 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
226 9c52365f 2020-03-21 stsp err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
227 9c52365f 2020-03-21 stsp "upload", verbosity);
228 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
229 20eb36d0 2020-03-18 stsp err = dial_git(fetchfd, host, port, server_path, "upload");
230 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
231 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
233 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
234 5cc27ede 2020-03-18 stsp return err;
237 82ebf666 2020-03-18 stsp const struct got_error *
238 82ebf666 2020-03-18 stsp got_fetch_parse_uri(char **proto, char **host, char **port,
239 82ebf666 2020-03-18 stsp char **server_path, char **repo_name, const char *uri)
241 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
242 93658fb9 2020-03-18 stsp char *s, *p, *q;
245 82ebf666 2020-03-18 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
247 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
249 9a682fbe 2020-03-19 stsp /* Try parsing Git's "scp" style URL syntax. */
250 9a682fbe 2020-03-19 stsp *proto = strdup("ssh");
251 9a682fbe 2020-03-19 stsp if (proto == NULL) {
252 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strdup");
255 9a682fbe 2020-03-19 stsp s = (char *)uri;
256 9a682fbe 2020-03-19 stsp q = strchr(s, ':');
257 9a682fbe 2020-03-19 stsp if (q == NULL) {
258 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
261 9a682fbe 2020-03-19 stsp /* No slashes allowed before first colon. */
262 9a682fbe 2020-03-19 stsp p = strchr(s, '/');
263 9a682fbe 2020-03-19 stsp if (p && q > p) {
264 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
267 82ebf666 2020-03-18 stsp *host = strndup(s, q - s);
268 82ebf666 2020-03-18 stsp if (*host == NULL) {
269 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
274 9a682fbe 2020-03-19 stsp *proto = strndup(uri, p - uri);
275 9a682fbe 2020-03-19 stsp if (proto == NULL) {
276 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
281 9a682fbe 2020-03-19 stsp p = strstr(s, "/");
282 9a682fbe 2020-03-19 stsp if (p == NULL || strlen(p) == 1) {
283 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
287 9a682fbe 2020-03-19 stsp q = memchr(s, ':', p - s);
289 9a682fbe 2020-03-19 stsp *host = strndup(s, q - s);
290 9a682fbe 2020-03-19 stsp if (*host == NULL) {
291 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
294 9a682fbe 2020-03-19 stsp *port = strndup(q + 1, p - (q + 1));
295 9a682fbe 2020-03-19 stsp if (*port == NULL) {
296 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
300 9a682fbe 2020-03-19 stsp *host = strndup(s, p - s);
301 9a682fbe 2020-03-19 stsp if (*host == NULL) {
302 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
308 0921e08f 2020-09-24 stsp while (p[0] == '/' && p[1] == '/')
310 82ebf666 2020-03-18 stsp *server_path = strdup(p);
311 82ebf666 2020-03-18 stsp if (*server_path == NULL) {
312 82ebf666 2020-03-18 stsp err = got_error_from_errno("strdup");
315 66cb1a7f 2020-09-24 stsp got_path_strip_trailing_slashes(*server_path);
317 9a682fbe 2020-03-19 stsp p = strrchr(p, '/');
318 9a682fbe 2020-03-19 stsp if (!p || strlen(p) <= 1) {
319 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
323 93658fb9 2020-03-18 stsp n = strlen(p);
324 9a682fbe 2020-03-19 stsp if (n == 0) {
325 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
328 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
330 82ebf666 2020-03-18 stsp *repo_name = strndup(p, (p + n) - p);
331 82ebf666 2020-03-18 stsp if (*repo_name == NULL) {
332 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
337 82ebf666 2020-03-18 stsp free(*proto);
338 82ebf666 2020-03-18 stsp *proto = NULL;
339 82ebf666 2020-03-18 stsp free(*host);
340 82ebf666 2020-03-18 stsp *host = NULL;
341 82ebf666 2020-03-18 stsp free(*port);
342 82ebf666 2020-03-18 stsp *port = NULL;
343 82ebf666 2020-03-18 stsp free(*server_path);
344 82ebf666 2020-03-18 stsp *server_path = NULL;
345 82ebf666 2020-03-18 stsp free(*repo_name);
346 82ebf666 2020-03-18 stsp *repo_name = NULL;
348 82ebf666 2020-03-18 stsp return err;
351 93658fb9 2020-03-18 stsp const struct got_error*
352 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
353 469dd726 2020-03-20 stsp struct got_pathlist_head *symrefs, const char *remote_name,
354 4ba14133 2020-03-20 stsp int mirror_references, int fetch_all_branches,
355 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
356 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
357 0e4002ca 2020-03-21 stsp int fetchfd, struct got_repository *repo,
358 469dd726 2020-03-20 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
360 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
361 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
362 d582f26c 2020-03-18 stsp int tmpfds[3], i;
363 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
364 93658fb9 2020-03-18 stsp const struct got_error *err;
365 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
366 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
367 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
368 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
369 41b0de12 2020-03-21 stsp const char *repo_path = NULL;
370 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
371 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
372 7848a0e1 2020-03-19 stsp struct got_reflist_head my_refs;
373 7848a0e1 2020-03-19 stsp struct got_reflist_entry *re;
374 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
375 393fb88d 2020-03-21 stsp struct got_packfile_hdr pack_hdr;
376 393fb88d 2020-03-21 stsp uint32_t nobj = 0;
377 7848a0e1 2020-03-19 stsp char *ref_prefix = NULL;
378 7848a0e1 2020-03-19 stsp size_t ref_prefixlen = 0;
379 ee61b6d3 2020-03-18 stsp char *path;
380 f1c6967f 2020-03-19 stsp char *progress = NULL;
382 92dc95a8 2020-03-24 stsp *pack_hash = NULL;
385 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
386 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
388 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
389 0e4002ca 2020-03-21 stsp const char *refname = pe->path;
390 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
391 0e4002ca 2020-03-21 stsp strncmp(refname, "got/", 4) == 0 ||
392 0e4002ca 2020-03-21 stsp strncmp(refname, "refs/remotes/", 13) == 0 ||
393 0e4002ca 2020-03-21 stsp strncmp(refname, "remotes/", 8) == 0)
394 0e4002ca 2020-03-21 stsp return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
397 41b0de12 2020-03-21 stsp if (!list_refs_only)
398 41b0de12 2020-03-21 stsp repo_path = got_repo_get_path_git_dir(repo);
400 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
401 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
403 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
404 7848a0e1 2020-03-19 stsp SIMPLEQ_INIT(&my_refs);
406 469dd726 2020-03-20 stsp if (!mirror_references) {
407 469dd726 2020-03-20 stsp if (asprintf(&ref_prefix, "refs/remotes/%s/",
408 469dd726 2020-03-20 stsp remote_name) == -1)
409 469dd726 2020-03-20 stsp return got_error_from_errno("asprintf");
410 469dd726 2020-03-20 stsp ref_prefixlen = strlen(ref_prefix);
413 41b0de12 2020-03-21 stsp if (!list_refs_only) {
414 41b0de12 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL,
415 41b0de12 2020-03-21 stsp got_ref_cmp_by_name, NULL);
420 7848a0e1 2020-03-19 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
421 7848a0e1 2020-03-19 stsp struct got_object_id *id;
422 7848a0e1 2020-03-19 stsp const char *refname;
424 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(re->ref))
427 7848a0e1 2020-03-19 stsp refname = got_ref_get_name(re->ref);
429 469dd726 2020-03-20 stsp if (mirror_references) {
430 469dd726 2020-03-20 stsp char *name;
431 469dd726 2020-03-20 stsp err = got_ref_resolve(&id, repo, re->ref);
434 469dd726 2020-03-20 stsp name = strdup(refname);
435 469dd726 2020-03-20 stsp if (name == NULL) {
436 469dd726 2020-03-20 stsp err = got_error_from_errno("strdup");
439 469dd726 2020-03-20 stsp err = got_pathlist_append(&have_refs, name, id);
445 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
446 7848a0e1 2020-03-19 stsp char *tagname;
448 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&id, repo, re->ref);
451 7848a0e1 2020-03-19 stsp tagname = strdup(refname);
452 7848a0e1 2020-03-19 stsp if (tagname == NULL) {
453 7848a0e1 2020-03-19 stsp err = got_error_from_errno("strdup");
456 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, tagname, id);
458 7848a0e1 2020-03-19 stsp free(tagname);
463 7848a0e1 2020-03-19 stsp if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
464 7848a0e1 2020-03-19 stsp char *branchname;
466 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&id, repo, re->ref);
470 7848a0e1 2020-03-19 stsp if (asprintf(&branchname, "refs/heads/%s",
471 7848a0e1 2020-03-19 stsp refname + ref_prefixlen) == -1) {
472 7848a0e1 2020-03-19 stsp err = got_error_from_errno("asprintf");
475 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, branchname, id);
477 7848a0e1 2020-03-19 stsp free(branchname);
483 41b0de12 2020-03-21 stsp if (list_refs_only) {
484 41b0de12 2020-03-21 stsp packfd = got_opentempfd();
485 41b0de12 2020-03-21 stsp if (packfd == -1) {
486 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
490 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.pack",
491 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
492 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
495 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
496 41b0de12 2020-03-21 stsp free(path);
500 41b0de12 2020-03-21 stsp if (list_refs_only) {
501 41b0de12 2020-03-21 stsp idxfd = got_opentempfd();
502 41b0de12 2020-03-21 stsp if (idxfd == -1) {
503 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
507 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.idx",
508 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
509 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
512 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
513 41b0de12 2020-03-21 stsp free(path);
517 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
518 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
519 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
523 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
524 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
525 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
526 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
531 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
532 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
536 85e8591f 2020-03-18 stsp fetchpid = fork();
537 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
538 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
540 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
541 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
542 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
545 8e278d17 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0) {
546 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
549 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
550 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
551 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
552 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
555 659e7fbd 2020-03-20 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
556 0e4002ca 2020-03-21 stsp fetch_all_branches, wanted_branches, wanted_refs,
557 0e4002ca 2020-03-21 stsp list_refs_only, verbosity);
558 93658fb9 2020-03-18 stsp if (err != NULL)
560 20eb36d0 2020-03-18 stsp nfetchfd = -1;
561 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
562 8e278d17 2020-03-18 stsp if (npackfd == -1) {
563 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
566 393fb88d 2020-03-21 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
567 393fb88d 2020-03-21 stsp if (err != NULL)
569 393fb88d 2020-03-21 stsp npackfd = -1;
571 d2cdc636 2020-03-18 stsp packfile_size = 0;
572 f1c6967f 2020-03-19 stsp progress = calloc(GOT_FETCH_PKTMAX, 1);
573 f1c6967f 2020-03-19 stsp if (progress == NULL) {
574 f1c6967f 2020-03-19 stsp err = got_error_from_errno("calloc");
578 1d72a2a0 2020-03-24 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
579 1d72a2a0 2020-03-24 stsp if (*pack_hash == NULL) {
580 1d72a2a0 2020-03-24 stsp err = got_error_from_errno("calloc");
584 8f2d01a6 2020-03-18 stsp while (!done) {
585 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
586 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
587 531c3985 2020-03-18 stsp char *server_progress = NULL;
588 7848a0e1 2020-03-19 stsp off_t packfile_size_cur = 0;
590 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
591 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
592 1d72a2a0 2020-03-24 stsp &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
593 8f2d01a6 2020-03-18 stsp if (err != NULL)
595 1d72a2a0 2020-03-24 stsp if (!done && refname && id) {
596 41b0de12 2020-03-21 stsp err = got_pathlist_insert(NULL, refs, refname, id);
599 1d72a2a0 2020-03-24 stsp } else if (!done && server_progress) {
602 f1c6967f 2020-03-19 stsp * XXX git-daemon tends to send batched output with
603 f1c6967f 2020-03-19 stsp * lines spanning separate packets. Buffer progress
604 f1c6967f 2020-03-19 stsp * output until we see a CR or LF to avoid giving
605 f1c6967f 2020-03-19 stsp * partial lines of progress output to the callback.
607 f1c6967f 2020-03-19 stsp if (strlcat(progress, server_progress,
608 f1c6967f 2020-03-19 stsp GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
609 f1c6967f 2020-03-19 stsp progress[0] = '\0'; /* discard */
612 f1c6967f 2020-03-19 stsp while ((p = strchr(progress, '\r')) != NULL ||
613 f1c6967f 2020-03-19 stsp (p = strchr(progress, '\n')) != NULL) {
616 f1c6967f 2020-03-19 stsp char c = *p;
618 f1c6967f 2020-03-19 stsp if (asprintf(&s, "%s%s", progress,
619 f1c6967f 2020-03-19 stsp c == '\n' ? "\n" : "") == -1) {
620 f1c6967f 2020-03-19 stsp err = got_error_from_errno("asprintf");
623 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
624 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
628 f1c6967f 2020-03-19 stsp n = strlen(progress);
629 f1c6967f 2020-03-19 stsp if (n < GOT_FETCH_PKTMAX - 1) {
630 f1c6967f 2020-03-19 stsp memmove(progress, &progress[n + 1],
631 f1c6967f 2020-03-19 stsp GOT_FETCH_PKTMAX - n - 1);
633 f1c6967f 2020-03-19 stsp progress[0] = '\0';
635 531c3985 2020-03-18 stsp free(server_progress);
638 1d72a2a0 2020-03-24 stsp } else if (!done && packfile_size_cur != packfile_size) {
639 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
640 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
643 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
646 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
647 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
651 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
652 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
656 7848a0e1 2020-03-19 stsp /* If zero data was fetched without error we are already up-to-date. */
657 1d72a2a0 2020-03-24 stsp if (packfile_size == 0) {
658 1d72a2a0 2020-03-24 stsp free(*pack_hash);
659 1d72a2a0 2020-03-24 stsp *pack_hash = NULL;
661 1d72a2a0 2020-03-24 stsp } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
662 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
667 393fb88d 2020-03-21 stsp n = read(packfd, &pack_hdr, sizeof(pack_hdr));
668 393fb88d 2020-03-21 stsp if (n == -1) {
669 393fb88d 2020-03-21 stsp err = got_error_from_errno("read");
672 393fb88d 2020-03-21 stsp if (n != sizeof(pack_hdr)) {
673 393fb88d 2020-03-21 stsp err = got_error(GOT_ERR_IO);
676 393fb88d 2020-03-21 stsp if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
677 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
678 393fb88d 2020-03-21 stsp "bad pack file signature");
681 393fb88d 2020-03-21 stsp if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
682 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
683 393fb88d 2020-03-21 stsp "bad pack file version");
686 78fb0967 2020-09-09 naddy nobj = be32toh(pack_hdr.nobjects);
687 393fb88d 2020-03-21 stsp if (nobj == 0 &&
688 393fb88d 2020-03-21 stsp packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
689 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
690 393fb88d 2020-03-21 stsp "bad pack file with zero objects");
691 393fb88d 2020-03-21 stsp if (nobj != 0 &&
692 393fb88d 2020-03-21 stsp packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
693 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
694 393fb88d 2020-03-21 stsp "empty pack file with non-zero object count");
698 393fb88d 2020-03-21 stsp * If the pack file contains no objects, we may only need to update
699 393fb88d 2020-03-21 stsp * references in our repository. The caller will take care of that.
701 393fb88d 2020-03-21 stsp if (nobj == 0)
704 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
705 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
709 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
710 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
713 85e8591f 2020-03-18 stsp idxpid = fork();
714 85e8591f 2020-03-18 stsp if (idxpid == -1) {
715 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
717 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
718 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
719 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
720 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0) {
721 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
724 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
726 393fb88d 2020-03-21 stsp npackfd = dup(packfd);
727 393fb88d 2020-03-21 stsp if (npackfd == -1) {
728 393fb88d 2020-03-21 stsp err = got_error_from_errno("dup");
731 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
733 93658fb9 2020-03-18 stsp if (err != NULL)
735 8e278d17 2020-03-18 stsp npackfd = -1;
736 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
737 93658fb9 2020-03-18 stsp if (err != NULL)
739 8e278d17 2020-03-18 stsp nidxfd = -1;
740 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
741 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
742 d582f26c 2020-03-18 stsp if (err != NULL)
744 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
747 baa9fea0 2020-03-18 stsp while (!done) {
748 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
750 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
751 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
753 baa9fea0 2020-03-18 stsp if (err != NULL)
755 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
756 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
757 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
758 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
762 baa9fea0 2020-03-18 stsp imsg_clear(&idxibuf);
764 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
765 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
768 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
769 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
773 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
776 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
777 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
778 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
782 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
783 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
784 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
788 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
789 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
792 7848a0e1 2020-03-19 stsp free(tmppackpath);
793 7848a0e1 2020-03-19 stsp tmppackpath = NULL;
794 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
795 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
798 7848a0e1 2020-03-19 stsp free(tmpidxpath);
799 7848a0e1 2020-03-19 stsp tmpidxpath = NULL;
802 7848a0e1 2020-03-19 stsp if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
803 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmppackpath);
804 7848a0e1 2020-03-19 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
805 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmpidxpath);
806 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
807 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
808 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
809 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
810 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
811 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
812 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
813 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
814 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
815 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
816 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
818 afa77e03 2020-03-18 stsp free(tmppackpath);
819 afa77e03 2020-03-18 stsp free(tmpidxpath);
820 fe4e1501 2020-03-18 stsp free(idxpath);
821 afa77e03 2020-03-18 stsp free(packpath);
822 7848a0e1 2020-03-19 stsp free(ref_prefix);
823 f1c6967f 2020-03-19 stsp free(progress);
825 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
826 7848a0e1 2020-03-19 stsp free((char *)pe->path);
827 7848a0e1 2020-03-19 stsp free(pe->data);
829 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
830 7848a0e1 2020-03-19 stsp got_ref_list_free(&my_refs);
832 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
833 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
834 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
835 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
836 d9b4d0c0 2020-03-18 stsp free(pe->data);
838 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
839 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
840 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
841 d9b4d0c0 2020-03-18 stsp free(pe->data);
843 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
845 8e278d17 2020-03-18 stsp return err;