Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
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.
7 93658fb9 2020-03-18 stsp *
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.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
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>
25 93658fb9 2020-03-18 stsp
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>
44 93658fb9 2020-03-18 stsp
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"
54 93658fb9 2020-03-18 stsp
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"
65 d582f26c 2020-03-18 stsp
66 d582f26c 2020-03-18 stsp #ifndef nitems
67 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 d582f26c 2020-03-18 stsp #endif
69 93658fb9 2020-03-18 stsp
70 68999b92 2020-03-18 stsp #ifndef MIN
71 68999b92 2020-03-18 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
72 68999b92 2020-03-18 stsp #endif
73 68999b92 2020-03-18 stsp
74 93658fb9 2020-03-18 stsp static int
75 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
76 93658fb9 2020-03-18 stsp {
77 93658fb9 2020-03-18 stsp int nb, ns;
78 93658fb9 2020-03-18 stsp
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)
82 93658fb9 2020-03-18 stsp return 1;
83 93658fb9 2020-03-18 stsp return 0;
84 93658fb9 2020-03-18 stsp }
85 93658fb9 2020-03-18 stsp
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)
89 93658fb9 2020-03-18 stsp {
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;
95 93658fb9 2020-03-18 stsp
96 9c52365f 2020-03-21 stsp *fetchpid = -1;
97 5cc27ede 2020-03-18 stsp *fetchfd = -1;
98 68999b92 2020-03-18 stsp
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;
103 59d5e252 2020-04-21 semarie }
104 68999b92 2020-03-18 stsp if (verbosity == -1) {
105 59d5e252 2020-04-21 semarie argv[i++] = "-q";
106 68999b92 2020-03-18 stsp } else {
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";
110 68999b92 2020-03-18 stsp }
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;
116 5cc27ede 2020-03-18 stsp
117 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
118 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
119 5cc27ede 2020-03-18 stsp
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) {
127 5cc27ede 2020-03-18 stsp int n;
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 */
137 5cc27ede 2020-03-18 stsp } else {
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;
142 93658fb9 2020-03-18 stsp }
143 93658fb9 2020-03-18 stsp }
144 93658fb9 2020-03-18 stsp
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)
148 93658fb9 2020-03-18 stsp {
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;
153 5cc27ede 2020-03-18 stsp
154 5cc27ede 2020-03-18 stsp *fetchfd = -1;
155 93658fb9 2020-03-18 stsp
156 62a4c94c 2020-03-20 stsp if (port == NULL)
157 62a4c94c 2020-03-20 stsp port = GOT_DEFAULT_GIT_PORT_STR;
158 62a4c94c 2020-03-20 stsp
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);
168 a117fd10 2020-03-18 stsp }
169 93658fb9 2020-03-18 stsp
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)
173 93658fb9 2020-03-18 stsp continue;
174 e03cc834 2020-09-24 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
175 e03cc834 2020-09-24 stsp err = NULL;
176 93658fb9 2020-03-18 stsp break;
177 e03cc834 2020-09-24 stsp }
178 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
179 93658fb9 2020-03-18 stsp close(fd);
180 93658fb9 2020-03-18 stsp }
181 93658fb9 2020-03-18 stsp if (p == NULL)
182 5cc27ede 2020-03-18 stsp goto done;
183 93658fb9 2020-03-18 stsp
184 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
185 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
186 5cc27ede 2020-03-18 stsp goto done;
187 5cc27ede 2020-03-18 stsp }
188 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
189 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
190 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
191 5cc27ede 2020-03-18 stsp goto done;
192 5cc27ede 2020-03-18 stsp }
193 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
194 4312a498 2020-03-18 stsp if (r == -1) {
195 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
196 4312a498 2020-03-18 stsp goto done;
197 4312a498 2020-03-18 stsp }
198 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
199 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
200 4312a498 2020-03-18 stsp goto done;
201 4312a498 2020-03-18 stsp }
202 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
203 4312a498 2020-03-18 stsp if (r == -1) {
204 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
205 4312a498 2020-03-18 stsp goto done;
206 4312a498 2020-03-18 stsp }
207 5cc27ede 2020-03-18 stsp done:
208 93658fb9 2020-03-18 stsp free(cmd);
209 93658fb9 2020-03-18 stsp free(pkt);
210 5cc27ede 2020-03-18 stsp if (err) {
211 5cc27ede 2020-03-18 stsp if (fd != -1)
212 5cc27ede 2020-03-18 stsp close(fd);
213 5cc27ede 2020-03-18 stsp } else
214 5cc27ede 2020-03-18 stsp *fetchfd = fd;
215 20eb36d0 2020-03-18 stsp return err;
216 20eb36d0 2020-03-18 stsp }
217 20eb36d0 2020-03-18 stsp
218 20eb36d0 2020-03-18 stsp const struct got_error *
219 9c52365f 2020-03-21 stsp got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
220 9c52365f 2020-03-21 stsp const char *host, const char *port, const char *server_path, int verbosity)
221 20eb36d0 2020-03-18 stsp {
222 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
223 20eb36d0 2020-03-18 stsp
224 9c52365f 2020-03-21 stsp *fetchpid = -1;
225 20eb36d0 2020-03-18 stsp *fetchfd = -1;
226 20eb36d0 2020-03-18 stsp
227 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
228 9c52365f 2020-03-21 stsp err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
229 9c52365f 2020-03-21 stsp "upload", verbosity);
230 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
231 20eb36d0 2020-03-18 stsp err = dial_git(fetchfd, host, port, server_path, "upload");
232 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
233 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
234 20eb36d0 2020-03-18 stsp else
235 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
236 5cc27ede 2020-03-18 stsp return err;
237 93658fb9 2020-03-18 stsp }
238 93658fb9 2020-03-18 stsp
239 82ebf666 2020-03-18 stsp const struct got_error *
240 82ebf666 2020-03-18 stsp got_fetch_parse_uri(char **proto, char **host, char **port,
241 82ebf666 2020-03-18 stsp char **server_path, char **repo_name, const char *uri)
242 93658fb9 2020-03-18 stsp {
243 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
244 93658fb9 2020-03-18 stsp char *s, *p, *q;
245 9a682fbe 2020-03-19 stsp int n;
246 93658fb9 2020-03-18 stsp
247 82ebf666 2020-03-18 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
248 82ebf666 2020-03-18 stsp
249 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
250 93658fb9 2020-03-18 stsp if (!p) {
251 9a682fbe 2020-03-19 stsp /* Try parsing Git's "scp" style URL syntax. */
252 9a682fbe 2020-03-19 stsp *proto = strdup("ssh");
253 9a682fbe 2020-03-19 stsp if (proto == NULL) {
254 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strdup");
255 9a682fbe 2020-03-19 stsp goto done;
256 9a682fbe 2020-03-19 stsp }
257 9a682fbe 2020-03-19 stsp s = (char *)uri;
258 9a682fbe 2020-03-19 stsp q = strchr(s, ':');
259 9a682fbe 2020-03-19 stsp if (q == NULL) {
260 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
261 9a682fbe 2020-03-19 stsp goto done;
262 9a682fbe 2020-03-19 stsp }
263 9a682fbe 2020-03-19 stsp /* No slashes allowed before first colon. */
264 9a682fbe 2020-03-19 stsp p = strchr(s, '/');
265 9a682fbe 2020-03-19 stsp if (p && q > p) {
266 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
267 9a682fbe 2020-03-19 stsp goto done;
268 9a682fbe 2020-03-19 stsp }
269 82ebf666 2020-03-18 stsp *host = strndup(s, q - s);
270 82ebf666 2020-03-18 stsp if (*host == NULL) {
271 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
272 82ebf666 2020-03-18 stsp goto done;
273 82ebf666 2020-03-18 stsp }
274 9a682fbe 2020-03-19 stsp p = q + 1;
275 82ebf666 2020-03-18 stsp } else {
276 9a682fbe 2020-03-19 stsp *proto = strndup(uri, p - uri);
277 9a682fbe 2020-03-19 stsp if (proto == NULL) {
278 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
279 82ebf666 2020-03-18 stsp goto done;
280 82ebf666 2020-03-18 stsp }
281 9a682fbe 2020-03-19 stsp s = p + 3;
282 9a682fbe 2020-03-19 stsp
283 9a682fbe 2020-03-19 stsp p = strstr(s, "/");
284 9a682fbe 2020-03-19 stsp if (p == NULL || strlen(p) == 1) {
285 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
286 82ebf666 2020-03-18 stsp goto done;
287 82ebf666 2020-03-18 stsp }
288 9a682fbe 2020-03-19 stsp
289 9a682fbe 2020-03-19 stsp q = memchr(s, ':', p - s);
290 9a682fbe 2020-03-19 stsp if (q) {
291 9a682fbe 2020-03-19 stsp *host = strndup(s, q - s);
292 9a682fbe 2020-03-19 stsp if (*host == NULL) {
293 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
294 9a682fbe 2020-03-19 stsp goto done;
295 9a682fbe 2020-03-19 stsp }
296 9a682fbe 2020-03-19 stsp *port = strndup(q + 1, p - (q + 1));
297 9a682fbe 2020-03-19 stsp if (*port == NULL) {
298 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
299 9a682fbe 2020-03-19 stsp goto done;
300 9a682fbe 2020-03-19 stsp }
301 9a682fbe 2020-03-19 stsp } else {
302 9a682fbe 2020-03-19 stsp *host = strndup(s, p - s);
303 9a682fbe 2020-03-19 stsp if (*host == NULL) {
304 9a682fbe 2020-03-19 stsp err = got_error_from_errno("strndup");
305 9a682fbe 2020-03-19 stsp goto done;
306 9a682fbe 2020-03-19 stsp }
307 9a682fbe 2020-03-19 stsp }
308 93658fb9 2020-03-18 stsp }
309 93658fb9 2020-03-18 stsp
310 0921e08f 2020-09-24 stsp while (p[0] == '/' && p[1] == '/')
311 0921e08f 2020-09-24 stsp p++;
312 82ebf666 2020-03-18 stsp *server_path = strdup(p);
313 82ebf666 2020-03-18 stsp if (*server_path == NULL) {
314 82ebf666 2020-03-18 stsp err = got_error_from_errno("strdup");
315 82ebf666 2020-03-18 stsp goto done;
316 82ebf666 2020-03-18 stsp }
317 66cb1a7f 2020-09-24 stsp got_path_strip_trailing_slashes(*server_path);
318 82ebf666 2020-03-18 stsp
319 9a682fbe 2020-03-19 stsp p = strrchr(p, '/');
320 9a682fbe 2020-03-19 stsp if (!p || strlen(p) <= 1) {
321 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
322 82ebf666 2020-03-18 stsp goto done;
323 93658fb9 2020-03-18 stsp }
324 9a682fbe 2020-03-19 stsp p++;
325 93658fb9 2020-03-18 stsp n = strlen(p);
326 9a682fbe 2020-03-19 stsp if (n == 0) {
327 9a682fbe 2020-03-19 stsp err = got_error(GOT_ERR_PARSE_URI);
328 9a682fbe 2020-03-19 stsp goto done;
329 9a682fbe 2020-03-19 stsp }
330 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
331 93658fb9 2020-03-18 stsp n -= 4;
332 82ebf666 2020-03-18 stsp *repo_name = strndup(p, (p + n) - p);
333 82ebf666 2020-03-18 stsp if (*repo_name == NULL) {
334 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
335 82ebf666 2020-03-18 stsp goto done;
336 82ebf666 2020-03-18 stsp }
337 82ebf666 2020-03-18 stsp done:
338 82ebf666 2020-03-18 stsp if (err) {
339 82ebf666 2020-03-18 stsp free(*proto);
340 82ebf666 2020-03-18 stsp *proto = NULL;
341 82ebf666 2020-03-18 stsp free(*host);
342 82ebf666 2020-03-18 stsp *host = NULL;
343 82ebf666 2020-03-18 stsp free(*port);
344 82ebf666 2020-03-18 stsp *port = NULL;
345 82ebf666 2020-03-18 stsp free(*server_path);
346 82ebf666 2020-03-18 stsp *server_path = NULL;
347 82ebf666 2020-03-18 stsp free(*repo_name);
348 82ebf666 2020-03-18 stsp *repo_name = NULL;
349 82ebf666 2020-03-18 stsp }
350 82ebf666 2020-03-18 stsp return err;
351 849f7557 2020-03-18 stsp }
352 849f7557 2020-03-18 stsp
353 93658fb9 2020-03-18 stsp const struct got_error*
354 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
355 469dd726 2020-03-20 stsp struct got_pathlist_head *symrefs, const char *remote_name,
356 4ba14133 2020-03-20 stsp int mirror_references, int fetch_all_branches,
357 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
358 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
359 0e4002ca 2020-03-21 stsp int fetchfd, struct got_repository *repo,
360 469dd726 2020-03-20 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
361 93658fb9 2020-03-18 stsp {
362 16aeacf7 2020-11-26 stsp size_t i;
363 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
364 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
365 16aeacf7 2020-11-26 stsp int tmpfds[3];
366 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
367 93658fb9 2020-03-18 stsp const struct got_error *err;
368 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
369 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
370 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
371 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
372 41b0de12 2020-03-21 stsp const char *repo_path = NULL;
373 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
374 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
375 7848a0e1 2020-03-19 stsp struct got_reflist_head my_refs;
376 7848a0e1 2020-03-19 stsp struct got_reflist_entry *re;
377 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
378 393fb88d 2020-03-21 stsp struct got_packfile_hdr pack_hdr;
379 393fb88d 2020-03-21 stsp uint32_t nobj = 0;
380 7848a0e1 2020-03-19 stsp char *ref_prefix = NULL;
381 7848a0e1 2020-03-19 stsp size_t ref_prefixlen = 0;
382 ee61b6d3 2020-03-18 stsp char *path;
383 f1c6967f 2020-03-19 stsp char *progress = NULL;
384 92dc95a8 2020-03-24 stsp
385 92dc95a8 2020-03-24 stsp *pack_hash = NULL;
386 41b0de12 2020-03-21 stsp
387 0e4002ca 2020-03-21 stsp /*
388 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
389 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
390 0e4002ca 2020-03-21 stsp */
391 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
392 0e4002ca 2020-03-21 stsp const char *refname = pe->path;
393 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
394 0e4002ca 2020-03-21 stsp strncmp(refname, "got/", 4) == 0 ||
395 0e4002ca 2020-03-21 stsp strncmp(refname, "refs/remotes/", 13) == 0 ||
396 0e4002ca 2020-03-21 stsp strncmp(refname, "remotes/", 8) == 0)
397 0e4002ca 2020-03-21 stsp return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
398 0e4002ca 2020-03-21 stsp }
399 0e4002ca 2020-03-21 stsp
400 41b0de12 2020-03-21 stsp if (!list_refs_only)
401 41b0de12 2020-03-21 stsp repo_path = got_repo_get_path_git_dir(repo);
402 abe0f35f 2020-03-18 stsp
403 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
404 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
405 93658fb9 2020-03-18 stsp
406 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
407 7848a0e1 2020-03-19 stsp SIMPLEQ_INIT(&my_refs);
408 7848a0e1 2020-03-19 stsp
409 469dd726 2020-03-20 stsp if (!mirror_references) {
410 469dd726 2020-03-20 stsp if (asprintf(&ref_prefix, "refs/remotes/%s/",
411 469dd726 2020-03-20 stsp remote_name) == -1)
412 469dd726 2020-03-20 stsp return got_error_from_errno("asprintf");
413 469dd726 2020-03-20 stsp ref_prefixlen = strlen(ref_prefix);
414 469dd726 2020-03-20 stsp }
415 7848a0e1 2020-03-19 stsp
416 41b0de12 2020-03-21 stsp if (!list_refs_only) {
417 41b0de12 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL,
418 41b0de12 2020-03-21 stsp got_ref_cmp_by_name, NULL);
419 41b0de12 2020-03-21 stsp if (err)
420 41b0de12 2020-03-21 stsp goto done;
421 41b0de12 2020-03-21 stsp }
422 7848a0e1 2020-03-19 stsp
423 7848a0e1 2020-03-19 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
424 7848a0e1 2020-03-19 stsp struct got_object_id *id;
425 7848a0e1 2020-03-19 stsp const char *refname;
426 7848a0e1 2020-03-19 stsp
427 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(re->ref))
428 7848a0e1 2020-03-19 stsp continue;
429 33501562 2020-03-18 stsp
430 7848a0e1 2020-03-19 stsp refname = got_ref_get_name(re->ref);
431 469dd726 2020-03-20 stsp
432 469dd726 2020-03-20 stsp if (mirror_references) {
433 469dd726 2020-03-20 stsp char *name;
434 469dd726 2020-03-20 stsp err = got_ref_resolve(&id, repo, re->ref);
435 469dd726 2020-03-20 stsp if (err)
436 469dd726 2020-03-20 stsp goto done;
437 469dd726 2020-03-20 stsp name = strdup(refname);
438 469dd726 2020-03-20 stsp if (name == NULL) {
439 469dd726 2020-03-20 stsp err = got_error_from_errno("strdup");
440 469dd726 2020-03-20 stsp goto done;
441 469dd726 2020-03-20 stsp }
442 469dd726 2020-03-20 stsp err = got_pathlist_append(&have_refs, name, id);
443 469dd726 2020-03-20 stsp if (err)
444 469dd726 2020-03-20 stsp goto done;
445 469dd726 2020-03-20 stsp continue;
446 469dd726 2020-03-20 stsp }
447 469dd726 2020-03-20 stsp
448 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
449 7848a0e1 2020-03-19 stsp char *tagname;
450 7848a0e1 2020-03-19 stsp
451 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&id, repo, re->ref);
452 7848a0e1 2020-03-19 stsp if (err)
453 7848a0e1 2020-03-19 stsp goto done;
454 7848a0e1 2020-03-19 stsp tagname = strdup(refname);
455 7848a0e1 2020-03-19 stsp if (tagname == NULL) {
456 7848a0e1 2020-03-19 stsp err = got_error_from_errno("strdup");
457 7848a0e1 2020-03-19 stsp goto done;
458 7848a0e1 2020-03-19 stsp }
459 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, tagname, id);
460 7848a0e1 2020-03-19 stsp if (err) {
461 7848a0e1 2020-03-19 stsp free(tagname);
462 7848a0e1 2020-03-19 stsp goto done;
463 7848a0e1 2020-03-19 stsp }
464 7848a0e1 2020-03-19 stsp }
465 7848a0e1 2020-03-19 stsp
466 7848a0e1 2020-03-19 stsp if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
467 7848a0e1 2020-03-19 stsp char *branchname;
468 7848a0e1 2020-03-19 stsp
469 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&id, repo, re->ref);
470 7848a0e1 2020-03-19 stsp if (err)
471 7848a0e1 2020-03-19 stsp goto done;
472 7848a0e1 2020-03-19 stsp
473 7848a0e1 2020-03-19 stsp if (asprintf(&branchname, "refs/heads/%s",
474 7848a0e1 2020-03-19 stsp refname + ref_prefixlen) == -1) {
475 7848a0e1 2020-03-19 stsp err = got_error_from_errno("asprintf");
476 7848a0e1 2020-03-19 stsp goto done;
477 7848a0e1 2020-03-19 stsp }
478 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, branchname, id);
479 7848a0e1 2020-03-19 stsp if (err) {
480 7848a0e1 2020-03-19 stsp free(branchname);
481 7848a0e1 2020-03-19 stsp goto done;
482 7848a0e1 2020-03-19 stsp }
483 7848a0e1 2020-03-19 stsp }
484 7848a0e1 2020-03-19 stsp }
485 7848a0e1 2020-03-19 stsp
486 41b0de12 2020-03-21 stsp if (list_refs_only) {
487 41b0de12 2020-03-21 stsp packfd = got_opentempfd();
488 41b0de12 2020-03-21 stsp if (packfd == -1) {
489 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
490 41b0de12 2020-03-21 stsp goto done;
491 41b0de12 2020-03-21 stsp }
492 41b0de12 2020-03-21 stsp } else {
493 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.pack",
494 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
495 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
496 41b0de12 2020-03-21 stsp goto done;
497 41b0de12 2020-03-21 stsp }
498 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
499 41b0de12 2020-03-21 stsp free(path);
500 41b0de12 2020-03-21 stsp if (err)
501 0843a4ce 2020-10-31 semarie goto done;
502 0843a4ce 2020-10-31 semarie if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
503 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmppackpath);
504 41b0de12 2020-03-21 stsp goto done;
505 0843a4ce 2020-10-31 semarie }
506 8e278d17 2020-03-18 stsp }
507 41b0de12 2020-03-21 stsp if (list_refs_only) {
508 41b0de12 2020-03-21 stsp idxfd = got_opentempfd();
509 41b0de12 2020-03-21 stsp if (idxfd == -1) {
510 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
511 41b0de12 2020-03-21 stsp goto done;
512 41b0de12 2020-03-21 stsp }
513 41b0de12 2020-03-21 stsp } else {
514 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.idx",
515 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
516 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
517 41b0de12 2020-03-21 stsp goto done;
518 41b0de12 2020-03-21 stsp }
519 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
520 41b0de12 2020-03-21 stsp free(path);
521 41b0de12 2020-03-21 stsp if (err)
522 0843a4ce 2020-10-31 semarie goto done;
523 0843a4ce 2020-10-31 semarie if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
524 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmpidxpath);
525 41b0de12 2020-03-21 stsp goto done;
526 0843a4ce 2020-10-31 semarie }
527 ee61b6d3 2020-03-18 stsp }
528 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
529 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
530 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
531 8e278d17 2020-03-18 stsp goto done;
532 8e278d17 2020-03-18 stsp }
533 93658fb9 2020-03-18 stsp
534 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
535 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
536 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
537 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
538 d582f26c 2020-03-18 stsp goto done;
539 d582f26c 2020-03-18 stsp }
540 4788f1ce 2020-03-18 stsp }
541 4788f1ce 2020-03-18 stsp
542 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
543 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
544 8e278d17 2020-03-18 stsp goto done;
545 8e278d17 2020-03-18 stsp }
546 93658fb9 2020-03-18 stsp
547 85e8591f 2020-03-18 stsp fetchpid = fork();
548 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
549 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
550 8e278d17 2020-03-18 stsp goto done;
551 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
552 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
553 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
554 93658fb9 2020-03-18 stsp }
555 93658fb9 2020-03-18 stsp
556 8e278d17 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0) {
557 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
558 8e278d17 2020-03-18 stsp goto done;
559 8e278d17 2020-03-18 stsp }
560 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
561 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
562 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
563 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
564 20eb36d0 2020-03-18 stsp goto done;
565 20eb36d0 2020-03-18 stsp }
566 659e7fbd 2020-03-20 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
567 0e4002ca 2020-03-21 stsp fetch_all_branches, wanted_branches, wanted_refs,
568 0e4002ca 2020-03-21 stsp list_refs_only, verbosity);
569 93658fb9 2020-03-18 stsp if (err != NULL)
570 8e278d17 2020-03-18 stsp goto done;
571 20eb36d0 2020-03-18 stsp nfetchfd = -1;
572 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
573 8e278d17 2020-03-18 stsp if (npackfd == -1) {
574 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
575 8e278d17 2020-03-18 stsp goto done;
576 8e278d17 2020-03-18 stsp }
577 393fb88d 2020-03-21 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
578 393fb88d 2020-03-21 stsp if (err != NULL)
579 393fb88d 2020-03-21 stsp goto done;
580 393fb88d 2020-03-21 stsp npackfd = -1;
581 8e278d17 2020-03-18 stsp
582 d2cdc636 2020-03-18 stsp packfile_size = 0;
583 f1c6967f 2020-03-19 stsp progress = calloc(GOT_FETCH_PKTMAX, 1);
584 f1c6967f 2020-03-19 stsp if (progress == NULL) {
585 f1c6967f 2020-03-19 stsp err = got_error_from_errno("calloc");
586 f1c6967f 2020-03-19 stsp goto done;
587 f1c6967f 2020-03-19 stsp }
588 1d72a2a0 2020-03-24 stsp
589 1d72a2a0 2020-03-24 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
590 1d72a2a0 2020-03-24 stsp if (*pack_hash == NULL) {
591 1d72a2a0 2020-03-24 stsp err = got_error_from_errno("calloc");
592 1d72a2a0 2020-03-24 stsp goto done;
593 1d72a2a0 2020-03-24 stsp }
594 1d72a2a0 2020-03-24 stsp
595 8f2d01a6 2020-03-18 stsp while (!done) {
596 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
597 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
598 531c3985 2020-03-18 stsp char *server_progress = NULL;
599 7848a0e1 2020-03-19 stsp off_t packfile_size_cur = 0;
600 8e278d17 2020-03-18 stsp
601 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
602 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
603 1d72a2a0 2020-03-24 stsp &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
604 8f2d01a6 2020-03-18 stsp if (err != NULL)
605 8e278d17 2020-03-18 stsp goto done;
606 1d72a2a0 2020-03-24 stsp if (!done && refname && id) {
607 41b0de12 2020-03-21 stsp err = got_pathlist_insert(NULL, refs, refname, id);
608 8f2d01a6 2020-03-18 stsp if (err)
609 8e278d17 2020-03-18 stsp goto done;
610 1d72a2a0 2020-03-24 stsp } else if (!done && server_progress) {
611 f1c6967f 2020-03-19 stsp char *p;
612 f1c6967f 2020-03-19 stsp /*
613 f1c6967f 2020-03-19 stsp * XXX git-daemon tends to send batched output with
614 f1c6967f 2020-03-19 stsp * lines spanning separate packets. Buffer progress
615 f1c6967f 2020-03-19 stsp * output until we see a CR or LF to avoid giving
616 f1c6967f 2020-03-19 stsp * partial lines of progress output to the callback.
617 f1c6967f 2020-03-19 stsp */
618 f1c6967f 2020-03-19 stsp if (strlcat(progress, server_progress,
619 f1c6967f 2020-03-19 stsp GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
620 f1c6967f 2020-03-19 stsp progress[0] = '\0'; /* discard */
621 f1c6967f 2020-03-19 stsp continue;
622 f1c6967f 2020-03-19 stsp }
623 f1c6967f 2020-03-19 stsp while ((p = strchr(progress, '\r')) != NULL ||
624 f1c6967f 2020-03-19 stsp (p = strchr(progress, '\n')) != NULL) {
625 f1c6967f 2020-03-19 stsp char *s;
626 f1c6967f 2020-03-19 stsp size_t n;
627 f1c6967f 2020-03-19 stsp char c = *p;
628 f1c6967f 2020-03-19 stsp *p = '\0';
629 f1c6967f 2020-03-19 stsp if (asprintf(&s, "%s%s", progress,
630 f1c6967f 2020-03-19 stsp c == '\n' ? "\n" : "") == -1) {
631 f1c6967f 2020-03-19 stsp err = got_error_from_errno("asprintf");
632 f1c6967f 2020-03-19 stsp goto done;
633 f1c6967f 2020-03-19 stsp }
634 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
635 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
636 f1c6967f 2020-03-19 stsp free(s);
637 531c3985 2020-03-18 stsp if (err)
638 531c3985 2020-03-18 stsp break;
639 f1c6967f 2020-03-19 stsp n = strlen(progress);
640 f1c6967f 2020-03-19 stsp if (n < GOT_FETCH_PKTMAX - 1) {
641 f1c6967f 2020-03-19 stsp memmove(progress, &progress[n + 1],
642 f1c6967f 2020-03-19 stsp GOT_FETCH_PKTMAX - n - 1);
643 f1c6967f 2020-03-19 stsp } else
644 f1c6967f 2020-03-19 stsp progress[0] = '\0';
645 531c3985 2020-03-18 stsp }
646 531c3985 2020-03-18 stsp free(server_progress);
647 531c3985 2020-03-18 stsp if (err)
648 531c3985 2020-03-18 stsp goto done;
649 1d72a2a0 2020-03-24 stsp } else if (!done && packfile_size_cur != packfile_size) {
650 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
651 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
652 d2cdc636 2020-03-18 stsp if (err)
653 d2cdc636 2020-03-18 stsp break;
654 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
655 8f2d01a6 2020-03-18 stsp }
656 8f2d01a6 2020-03-18 stsp }
657 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
658 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
659 393fb88d 2020-03-21 stsp goto done;
660 393fb88d 2020-03-21 stsp }
661 393fb88d 2020-03-21 stsp
662 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
663 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
664 8e278d17 2020-03-18 stsp goto done;
665 8e278d17 2020-03-18 stsp }
666 849f7557 2020-03-18 stsp
667 7848a0e1 2020-03-19 stsp /* If zero data was fetched without error we are already up-to-date. */
668 1d72a2a0 2020-03-24 stsp if (packfile_size == 0) {
669 1d72a2a0 2020-03-24 stsp free(*pack_hash);
670 1d72a2a0 2020-03-24 stsp *pack_hash = NULL;
671 393fb88d 2020-03-21 stsp goto done;
672 1d72a2a0 2020-03-24 stsp } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
673 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
674 393fb88d 2020-03-21 stsp goto done;
675 393fb88d 2020-03-21 stsp } else {
676 393fb88d 2020-03-21 stsp ssize_t n;
677 393fb88d 2020-03-21 stsp
678 393fb88d 2020-03-21 stsp n = read(packfd, &pack_hdr, sizeof(pack_hdr));
679 393fb88d 2020-03-21 stsp if (n == -1) {
680 393fb88d 2020-03-21 stsp err = got_error_from_errno("read");
681 393fb88d 2020-03-21 stsp goto done;
682 393fb88d 2020-03-21 stsp }
683 393fb88d 2020-03-21 stsp if (n != sizeof(pack_hdr)) {
684 393fb88d 2020-03-21 stsp err = got_error(GOT_ERR_IO);
685 393fb88d 2020-03-21 stsp goto done;
686 393fb88d 2020-03-21 stsp }
687 393fb88d 2020-03-21 stsp if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
688 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
689 393fb88d 2020-03-21 stsp "bad pack file signature");
690 393fb88d 2020-03-21 stsp goto done;
691 393fb88d 2020-03-21 stsp }
692 393fb88d 2020-03-21 stsp if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
693 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
694 393fb88d 2020-03-21 stsp "bad pack file version");
695 393fb88d 2020-03-21 stsp goto done;
696 393fb88d 2020-03-21 stsp }
697 78fb0967 2020-09-09 naddy nobj = be32toh(pack_hdr.nobjects);
698 393fb88d 2020-03-21 stsp if (nobj == 0 &&
699 393fb88d 2020-03-21 stsp packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
700 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
701 393fb88d 2020-03-21 stsp "bad pack file with zero objects");
702 393fb88d 2020-03-21 stsp if (nobj != 0 &&
703 393fb88d 2020-03-21 stsp packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
704 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
705 393fb88d 2020-03-21 stsp "empty pack file with non-zero object count");
706 393fb88d 2020-03-21 stsp }
707 393fb88d 2020-03-21 stsp
708 393fb88d 2020-03-21 stsp /*
709 393fb88d 2020-03-21 stsp * If the pack file contains no objects, we may only need to update
710 393fb88d 2020-03-21 stsp * references in our repository. The caller will take care of that.
711 393fb88d 2020-03-21 stsp */
712 393fb88d 2020-03-21 stsp if (nobj == 0)
713 849f7557 2020-03-18 stsp goto done;
714 393fb88d 2020-03-21 stsp
715 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
716 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
717 393fb88d 2020-03-21 stsp goto done;
718 393fb88d 2020-03-21 stsp }
719 531c3985 2020-03-18 stsp
720 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
721 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
722 8e278d17 2020-03-18 stsp goto done;
723 8e278d17 2020-03-18 stsp }
724 85e8591f 2020-03-18 stsp idxpid = fork();
725 85e8591f 2020-03-18 stsp if (idxpid == -1) {
726 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
727 8e278d17 2020-03-18 stsp goto done;
728 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
729 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
730 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
731 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0) {
732 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
733 8e278d17 2020-03-18 stsp goto done;
734 8e278d17 2020-03-18 stsp }
735 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
736 93658fb9 2020-03-18 stsp
737 393fb88d 2020-03-21 stsp npackfd = dup(packfd);
738 393fb88d 2020-03-21 stsp if (npackfd == -1) {
739 393fb88d 2020-03-21 stsp err = got_error_from_errno("dup");
740 393fb88d 2020-03-21 stsp goto done;
741 393fb88d 2020-03-21 stsp }
742 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
743 668a20f6 2020-03-18 stsp npackfd);
744 93658fb9 2020-03-18 stsp if (err != NULL)
745 8e278d17 2020-03-18 stsp goto done;
746 8e278d17 2020-03-18 stsp npackfd = -1;
747 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
748 93658fb9 2020-03-18 stsp if (err != NULL)
749 8e278d17 2020-03-18 stsp goto done;
750 8e278d17 2020-03-18 stsp nidxfd = -1;
751 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
752 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
753 d582f26c 2020-03-18 stsp if (err != NULL)
754 d582f26c 2020-03-18 stsp goto done;
755 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
756 d582f26c 2020-03-18 stsp }
757 baa9fea0 2020-03-18 stsp done = 0;
758 baa9fea0 2020-03-18 stsp while (!done) {
759 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
760 668a20f6 2020-03-18 stsp
761 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
762 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
763 668a20f6 2020-03-18 stsp &idxibuf);
764 baa9fea0 2020-03-18 stsp if (err != NULL)
765 baa9fea0 2020-03-18 stsp goto done;
766 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
767 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
768 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
769 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
770 baa9fea0 2020-03-18 stsp if (err)
771 baa9fea0 2020-03-18 stsp break;
772 baa9fea0 2020-03-18 stsp }
773 baa9fea0 2020-03-18 stsp imsg_clear(&idxibuf);
774 baa9fea0 2020-03-18 stsp }
775 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
776 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
777 8e278d17 2020-03-18 stsp goto done;
778 8e278d17 2020-03-18 stsp }
779 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
780 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
781 8e278d17 2020-03-18 stsp goto done;
782 8e278d17 2020-03-18 stsp }
783 93658fb9 2020-03-18 stsp
784 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
785 afa77e03 2020-03-18 stsp if (err)
786 8e278d17 2020-03-18 stsp goto done;
787 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
788 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
789 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
790 8e278d17 2020-03-18 stsp goto done;
791 8e278d17 2020-03-18 stsp }
792 93658fb9 2020-03-18 stsp
793 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
794 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
795 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
796 8e278d17 2020-03-18 stsp goto done;
797 8e278d17 2020-03-18 stsp }
798 afa77e03 2020-03-18 stsp
799 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
800 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
801 8e278d17 2020-03-18 stsp goto done;
802 8e278d17 2020-03-18 stsp }
803 7848a0e1 2020-03-19 stsp free(tmppackpath);
804 7848a0e1 2020-03-19 stsp tmppackpath = NULL;
805 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
806 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
807 8e278d17 2020-03-18 stsp goto done;
808 8e278d17 2020-03-18 stsp }
809 7848a0e1 2020-03-19 stsp free(tmpidxpath);
810 7848a0e1 2020-03-19 stsp tmpidxpath = NULL;
811 ee61b6d3 2020-03-18 stsp
812 8e278d17 2020-03-18 stsp done:
813 7848a0e1 2020-03-19 stsp if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
814 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmppackpath);
815 7848a0e1 2020-03-19 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
816 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmpidxpath);
817 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
818 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
819 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
820 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
821 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
822 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
823 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
824 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
825 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
826 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
827 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
828 d582f26c 2020-03-18 stsp }
829 afa77e03 2020-03-18 stsp free(tmppackpath);
830 afa77e03 2020-03-18 stsp free(tmpidxpath);
831 fe4e1501 2020-03-18 stsp free(idxpath);
832 afa77e03 2020-03-18 stsp free(packpath);
833 7848a0e1 2020-03-19 stsp free(ref_prefix);
834 f1c6967f 2020-03-19 stsp free(progress);
835 fe4e1501 2020-03-18 stsp
836 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
837 7848a0e1 2020-03-19 stsp free((char *)pe->path);
838 7848a0e1 2020-03-19 stsp free(pe->data);
839 7848a0e1 2020-03-19 stsp }
840 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
841 7848a0e1 2020-03-19 stsp got_ref_list_free(&my_refs);
842 d9b4d0c0 2020-03-18 stsp if (err) {
843 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
844 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
845 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
846 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
847 d9b4d0c0 2020-03-18 stsp free(pe->data);
848 d9b4d0c0 2020-03-18 stsp }
849 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
850 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
851 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
852 d9b4d0c0 2020-03-18 stsp free(pe->data);
853 d9b4d0c0 2020-03-18 stsp }
854 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
855 d9b4d0c0 2020-03-18 stsp }
856 8e278d17 2020-03-18 stsp return err;
857 93658fb9 2020-03-18 stsp }