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/syslimits.h>
24 93658fb9 2020-03-18 stsp #include <sys/resource.h>
25 93658fb9 2020-03-18 stsp #include <sys/socket.h>
26 93658fb9 2020-03-18 stsp
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 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <ctype.h>
37 93658fb9 2020-03-18 stsp #include <limits.h>
38 93658fb9 2020-03-18 stsp #include <imsg.h>
39 93658fb9 2020-03-18 stsp #include <time.h>
40 93658fb9 2020-03-18 stsp #include <uuid.h>
41 93658fb9 2020-03-18 stsp #include <netdb.h>
42 93658fb9 2020-03-18 stsp #include <netinet/in.h>
43 93658fb9 2020-03-18 stsp
44 93658fb9 2020-03-18 stsp #include "got_error.h"
45 93658fb9 2020-03-18 stsp #include "got_reference.h"
46 93658fb9 2020-03-18 stsp #include "got_repository.h"
47 93658fb9 2020-03-18 stsp #include "got_path.h"
48 93658fb9 2020-03-18 stsp #include "got_cancel.h"
49 93658fb9 2020-03-18 stsp #include "got_worktree.h"
50 93658fb9 2020-03-18 stsp #include "got_object.h"
51 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
52 82ebf666 2020-03-18 stsp #include "got_fetch.h"
53 93658fb9 2020-03-18 stsp
54 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
59 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
60 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
61 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
62 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
63 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
64 d582f26c 2020-03-18 stsp
65 d582f26c 2020-03-18 stsp #ifndef nitems
66 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 d582f26c 2020-03-18 stsp #endif
68 93658fb9 2020-03-18 stsp
69 93658fb9 2020-03-18 stsp static int
70 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
71 93658fb9 2020-03-18 stsp {
72 93658fb9 2020-03-18 stsp int nb, ns;
73 93658fb9 2020-03-18 stsp
74 93658fb9 2020-03-18 stsp nb = strlen(base);
75 93658fb9 2020-03-18 stsp ns = strlen(suf);
76 93658fb9 2020-03-18 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
77 93658fb9 2020-03-18 stsp return 1;
78 93658fb9 2020-03-18 stsp return 0;
79 93658fb9 2020-03-18 stsp }
80 93658fb9 2020-03-18 stsp
81 5cc27ede 2020-03-18 stsp static const struct got_error *
82 09838ffc 2020-03-18 stsp dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
83 09838ffc 2020-03-18 stsp const char *direction)
84 93658fb9 2020-03-18 stsp {
85 5cc27ede 2020-03-18 stsp const struct got_error *error = NULL;
86 93658fb9 2020-03-18 stsp int pid, pfd[2];
87 93658fb9 2020-03-18 stsp char cmd[64];
88 93658fb9 2020-03-18 stsp
89 5cc27ede 2020-03-18 stsp *fetchfd = -1;
90 5cc27ede 2020-03-18 stsp
91 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
92 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
93 5cc27ede 2020-03-18 stsp
94 93658fb9 2020-03-18 stsp pid = fork();
95 5cc27ede 2020-03-18 stsp if (pid == -1) {
96 5cc27ede 2020-03-18 stsp error = got_error_from_errno("fork");
97 5cc27ede 2020-03-18 stsp close(pfd[0]);
98 93658fb9 2020-03-18 stsp close(pfd[1]);
99 5cc27ede 2020-03-18 stsp return error;
100 5cc27ede 2020-03-18 stsp } else if (pid == 0) {
101 5cc27ede 2020-03-18 stsp int n;
102 5cc27ede 2020-03-18 stsp close(pfd[1]);
103 93658fb9 2020-03-18 stsp dup2(pfd[0], 0);
104 93658fb9 2020-03-18 stsp dup2(pfd[0], 1);
105 5cc27ede 2020-03-18 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
106 5cc27ede 2020-03-18 stsp if (n < 0 || n >= sizeof(cmd))
107 5cc27ede 2020-03-18 stsp err(1, "snprintf");
108 3af5bc7c 2020-03-18 stsp if (execl(GOT_FETCH_PATH_SSH, GOT_FETCH_PATH_SSH, "--",
109 ee448f5f 2020-03-18 stsp host, cmd, path, NULL) == -1)
110 ee448f5f 2020-03-18 stsp err(1, "execl");
111 5cc27ede 2020-03-18 stsp abort(); /* not reached */
112 5cc27ede 2020-03-18 stsp } else {
113 93658fb9 2020-03-18 stsp close(pfd[0]);
114 5cc27ede 2020-03-18 stsp *fetchfd = pfd[1];
115 5cc27ede 2020-03-18 stsp return NULL;
116 93658fb9 2020-03-18 stsp }
117 93658fb9 2020-03-18 stsp }
118 93658fb9 2020-03-18 stsp
119 5cc27ede 2020-03-18 stsp static const struct got_error *
120 09838ffc 2020-03-18 stsp dial_git(int *fetchfd, const char *host, const char *port, const char *path,
121 09838ffc 2020-03-18 stsp const char *direction)
122 93658fb9 2020-03-18 stsp {
123 5cc27ede 2020-03-18 stsp const struct got_error *err = NULL;
124 93658fb9 2020-03-18 stsp struct addrinfo hints, *servinfo, *p;
125 5cc27ede 2020-03-18 stsp char *cmd = NULL, *pkt = NULL;
126 4312a498 2020-03-18 stsp int fd = -1, totlen, r, eaicode;
127 5cc27ede 2020-03-18 stsp
128 5cc27ede 2020-03-18 stsp *fetchfd = -1;
129 93658fb9 2020-03-18 stsp
130 93658fb9 2020-03-18 stsp memset(&hints, 0, sizeof hints);
131 93658fb9 2020-03-18 stsp hints.ai_family = AF_UNSPEC;
132 93658fb9 2020-03-18 stsp hints.ai_socktype = SOCK_STREAM;
133 5cc27ede 2020-03-18 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
134 a117fd10 2020-03-18 stsp if (eaicode) {
135 a117fd10 2020-03-18 stsp char msg[512];
136 a117fd10 2020-03-18 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
137 a117fd10 2020-03-18 stsp gai_strerror(eaicode));
138 a117fd10 2020-03-18 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
139 a117fd10 2020-03-18 stsp }
140 93658fb9 2020-03-18 stsp
141 93658fb9 2020-03-18 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
142 93658fb9 2020-03-18 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
143 93658fb9 2020-03-18 stsp p->ai_protocol)) == -1)
144 93658fb9 2020-03-18 stsp continue;
145 93658fb9 2020-03-18 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
146 93658fb9 2020-03-18 stsp break;
147 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
148 93658fb9 2020-03-18 stsp close(fd);
149 93658fb9 2020-03-18 stsp }
150 93658fb9 2020-03-18 stsp if (p == NULL)
151 5cc27ede 2020-03-18 stsp goto done;
152 93658fb9 2020-03-18 stsp
153 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
154 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
155 5cc27ede 2020-03-18 stsp goto done;
156 5cc27ede 2020-03-18 stsp }
157 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
158 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
159 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
160 5cc27ede 2020-03-18 stsp goto done;
161 5cc27ede 2020-03-18 stsp }
162 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
163 4312a498 2020-03-18 stsp if (r == -1) {
164 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
165 4312a498 2020-03-18 stsp goto done;
166 4312a498 2020-03-18 stsp }
167 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
168 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
169 4312a498 2020-03-18 stsp goto done;
170 4312a498 2020-03-18 stsp }
171 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
172 4312a498 2020-03-18 stsp if (r == -1) {
173 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
174 4312a498 2020-03-18 stsp goto done;
175 4312a498 2020-03-18 stsp }
176 5cc27ede 2020-03-18 stsp done:
177 93658fb9 2020-03-18 stsp free(cmd);
178 93658fb9 2020-03-18 stsp free(pkt);
179 5cc27ede 2020-03-18 stsp if (err) {
180 5cc27ede 2020-03-18 stsp if (fd != -1)
181 5cc27ede 2020-03-18 stsp close(fd);
182 5cc27ede 2020-03-18 stsp } else
183 5cc27ede 2020-03-18 stsp *fetchfd = fd;
184 20eb36d0 2020-03-18 stsp return err;
185 20eb36d0 2020-03-18 stsp }
186 20eb36d0 2020-03-18 stsp
187 20eb36d0 2020-03-18 stsp const struct got_error *
188 20eb36d0 2020-03-18 stsp got_fetch_connect(int *fetchfd, const char *proto, const char *host,
189 20eb36d0 2020-03-18 stsp const char *port, const char *server_path)
190 20eb36d0 2020-03-18 stsp {
191 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
192 20eb36d0 2020-03-18 stsp
193 20eb36d0 2020-03-18 stsp *fetchfd = -1;
194 20eb36d0 2020-03-18 stsp
195 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
196 20eb36d0 2020-03-18 stsp err = dial_ssh(fetchfd, host, port, server_path, "upload");
197 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
198 20eb36d0 2020-03-18 stsp err = dial_git(fetchfd, host, port, server_path, "upload");
199 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
200 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
201 20eb36d0 2020-03-18 stsp else
202 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
203 5cc27ede 2020-03-18 stsp return err;
204 93658fb9 2020-03-18 stsp }
205 93658fb9 2020-03-18 stsp
206 82ebf666 2020-03-18 stsp const struct got_error *
207 82ebf666 2020-03-18 stsp got_fetch_parse_uri(char **proto, char **host, char **port,
208 82ebf666 2020-03-18 stsp char **server_path, char **repo_name, const char *uri)
209 93658fb9 2020-03-18 stsp {
210 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
211 93658fb9 2020-03-18 stsp char *s, *p, *q;
212 93658fb9 2020-03-18 stsp int n, hasport;
213 93658fb9 2020-03-18 stsp
214 82ebf666 2020-03-18 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
215 82ebf666 2020-03-18 stsp
216 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
217 93658fb9 2020-03-18 stsp if (!p) {
218 82ebf666 2020-03-18 stsp return got_error(GOT_ERR_PARSE_URI);
219 93658fb9 2020-03-18 stsp }
220 82ebf666 2020-03-18 stsp *proto = strndup(uri, p - uri);
221 82ebf666 2020-03-18 stsp if (proto == NULL) {
222 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
223 82ebf666 2020-03-18 stsp goto done;
224 82ebf666 2020-03-18 stsp }
225 82ebf666 2020-03-18 stsp
226 82ebf666 2020-03-18 stsp hasport = (strcmp(*proto, "git") == 0 ||
227 82ebf666 2020-03-18 stsp strstr(*proto, "http") == *proto);
228 93658fb9 2020-03-18 stsp s = p + 3;
229 93658fb9 2020-03-18 stsp p = NULL;
230 93658fb9 2020-03-18 stsp if (!hasport) {
231 93658fb9 2020-03-18 stsp p = strstr(s, ":");
232 93658fb9 2020-03-18 stsp if (p != NULL)
233 93658fb9 2020-03-18 stsp p++;
234 93658fb9 2020-03-18 stsp }
235 93658fb9 2020-03-18 stsp if (p == NULL)
236 93658fb9 2020-03-18 stsp p = strstr(s, "/");
237 93658fb9 2020-03-18 stsp if (p == NULL || strlen(p) == 1) {
238 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
239 82ebf666 2020-03-18 stsp goto done;
240 93658fb9 2020-03-18 stsp }
241 93658fb9 2020-03-18 stsp
242 93658fb9 2020-03-18 stsp q = memchr(s, ':', p - s);
243 93658fb9 2020-03-18 stsp if (q) {
244 82ebf666 2020-03-18 stsp *host = strndup(s, q - s);
245 82ebf666 2020-03-18 stsp if (*host == NULL) {
246 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
247 82ebf666 2020-03-18 stsp goto done;
248 82ebf666 2020-03-18 stsp }
249 82ebf666 2020-03-18 stsp *port = strndup(q + 1, p - (q + 1));
250 82ebf666 2020-03-18 stsp if (*port == NULL) {
251 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
252 82ebf666 2020-03-18 stsp goto done;
253 82ebf666 2020-03-18 stsp }
254 82ebf666 2020-03-18 stsp } else {
255 82ebf666 2020-03-18 stsp *host = strndup(s, p - s);
256 82ebf666 2020-03-18 stsp if (*host == NULL) {
257 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
258 82ebf666 2020-03-18 stsp goto done;
259 82ebf666 2020-03-18 stsp }
260 82ebf666 2020-03-18 stsp if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
261 82ebf666 2020-03-18 stsp err = got_error_from_errno("asprintf");
262 82ebf666 2020-03-18 stsp goto done;
263 82ebf666 2020-03-18 stsp }
264 93658fb9 2020-03-18 stsp }
265 93658fb9 2020-03-18 stsp
266 82ebf666 2020-03-18 stsp *server_path = strdup(p);
267 82ebf666 2020-03-18 stsp if (*server_path == NULL) {
268 82ebf666 2020-03-18 stsp err = got_error_from_errno("strdup");
269 82ebf666 2020-03-18 stsp goto done;
270 82ebf666 2020-03-18 stsp }
271 82ebf666 2020-03-18 stsp
272 93658fb9 2020-03-18 stsp p = strrchr(p, '/') + 1;
273 93658fb9 2020-03-18 stsp if (!p || strlen(p) == 0) {
274 93658fb9 2020-03-18 stsp //werrstr("missing repository in uri");
275 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
276 82ebf666 2020-03-18 stsp goto done;
277 93658fb9 2020-03-18 stsp }
278 93658fb9 2020-03-18 stsp n = strlen(p);
279 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
280 93658fb9 2020-03-18 stsp n -= 4;
281 82ebf666 2020-03-18 stsp *repo_name = strndup(p, (p + n) - p);
282 82ebf666 2020-03-18 stsp if (*repo_name == NULL) {
283 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
284 82ebf666 2020-03-18 stsp goto done;
285 82ebf666 2020-03-18 stsp }
286 82ebf666 2020-03-18 stsp done:
287 82ebf666 2020-03-18 stsp if (err) {
288 82ebf666 2020-03-18 stsp free(*proto);
289 82ebf666 2020-03-18 stsp *proto = NULL;
290 82ebf666 2020-03-18 stsp free(*host);
291 82ebf666 2020-03-18 stsp *host = NULL;
292 82ebf666 2020-03-18 stsp free(*port);
293 82ebf666 2020-03-18 stsp *port = NULL;
294 82ebf666 2020-03-18 stsp free(*server_path);
295 82ebf666 2020-03-18 stsp *server_path = NULL;
296 82ebf666 2020-03-18 stsp free(*repo_name);
297 82ebf666 2020-03-18 stsp *repo_name = NULL;
298 82ebf666 2020-03-18 stsp }
299 82ebf666 2020-03-18 stsp return err;
300 849f7557 2020-03-18 stsp }
301 849f7557 2020-03-18 stsp
302 849f7557 2020-03-18 stsp static const struct got_error *
303 849f7557 2020-03-18 stsp check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
304 849f7557 2020-03-18 stsp {
305 849f7557 2020-03-18 stsp SHA1_CTX ctx;
306 849f7557 2020-03-18 stsp uint8_t hexpect[SHA1_DIGEST_LENGTH];
307 849f7557 2020-03-18 stsp uint8_t buf[32 * 1024];
308 849f7557 2020-03-18 stsp ssize_t n, r, nr;
309 849f7557 2020-03-18 stsp
310 849f7557 2020-03-18 stsp if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
311 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
312 849f7557 2020-03-18 stsp
313 849f7557 2020-03-18 stsp n = 0;
314 849f7557 2020-03-18 stsp SHA1Init(&ctx);
315 849f7557 2020-03-18 stsp while (n < sz - 20) {
316 849f7557 2020-03-18 stsp nr = sizeof(buf);
317 849f7557 2020-03-18 stsp if (sz - n - 20 < sizeof(buf))
318 849f7557 2020-03-18 stsp nr = sz - n - 20;
319 849f7557 2020-03-18 stsp r = read(fd, buf, nr);
320 849f7557 2020-03-18 stsp if (r == -1)
321 849f7557 2020-03-18 stsp return got_error_from_errno("read");
322 849f7557 2020-03-18 stsp if (r != nr)
323 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
324 849f7557 2020-03-18 stsp "short pack file");
325 849f7557 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
326 849f7557 2020-03-18 stsp n += r;
327 849f7557 2020-03-18 stsp }
328 849f7557 2020-03-18 stsp SHA1Final(hcomp, &ctx);
329 849f7557 2020-03-18 stsp
330 849f7557 2020-03-18 stsp r = read(fd, hexpect, sizeof(hexpect));
331 849f7557 2020-03-18 stsp if (r == -1)
332 849f7557 2020-03-18 stsp return got_error_from_errno("read");
333 849f7557 2020-03-18 stsp if (r != sizeof(hexpect))
334 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
335 849f7557 2020-03-18 stsp "short pack file");
336 849f7557 2020-03-18 stsp
337 849f7557 2020-03-18 stsp if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
338 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
339 849f7557 2020-03-18 stsp "packfile checksum mismatch");
340 849f7557 2020-03-18 stsp
341 849f7557 2020-03-18 stsp return NULL;
342 93658fb9 2020-03-18 stsp }
343 93658fb9 2020-03-18 stsp
344 93658fb9 2020-03-18 stsp const struct got_error*
345 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
346 531c3985 2020-03-18 stsp struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
347 531c3985 2020-03-18 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
348 93658fb9 2020-03-18 stsp {
349 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
350 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
351 d582f26c 2020-03-18 stsp int tmpfds[3], i;
352 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
353 93658fb9 2020-03-18 stsp const struct got_error *err;
354 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
355 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
356 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
357 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
358 bb64b798 2020-03-18 stsp const char *repo_path = got_repo_get_path(repo);
359 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
360 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
361 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
362 ee61b6d3 2020-03-18 stsp char *path;
363 abe0f35f 2020-03-18 stsp
364 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
365 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
366 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
367 93658fb9 2020-03-18 stsp
368 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
369 33501562 2020-03-18 stsp
370 66cba96f 2020-03-18 stsp if (asprintf(&path, "%s/%s/fetching.pack",
371 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
372 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
373 ee61b6d3 2020-03-18 stsp goto done;
374 8e278d17 2020-03-18 stsp }
375 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
376 ee61b6d3 2020-03-18 stsp free(path);
377 fe4e1501 2020-03-18 stsp if (err)
378 8e278d17 2020-03-18 stsp goto done;
379 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
380 8e278d17 2020-03-18 stsp if (npackfd == -1) {
381 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
382 8e278d17 2020-03-18 stsp goto done;
383 8e278d17 2020-03-18 stsp }
384 66cba96f 2020-03-18 stsp if (asprintf(&path, "%s/%s/fetching.idx",
385 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
386 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
387 ee61b6d3 2020-03-18 stsp goto done;
388 ee61b6d3 2020-03-18 stsp }
389 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
390 ee61b6d3 2020-03-18 stsp free(path);
391 fe4e1501 2020-03-18 stsp if (err)
392 8e278d17 2020-03-18 stsp goto done;
393 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
394 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
395 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
396 8e278d17 2020-03-18 stsp goto done;
397 8e278d17 2020-03-18 stsp }
398 93658fb9 2020-03-18 stsp
399 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
400 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
401 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
402 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
403 d582f26c 2020-03-18 stsp goto done;
404 d582f26c 2020-03-18 stsp }
405 4788f1ce 2020-03-18 stsp }
406 4788f1ce 2020-03-18 stsp
407 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
408 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
409 8e278d17 2020-03-18 stsp goto done;
410 8e278d17 2020-03-18 stsp }
411 93658fb9 2020-03-18 stsp
412 85e8591f 2020-03-18 stsp fetchpid = fork();
413 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
414 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
415 8e278d17 2020-03-18 stsp goto done;
416 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
417 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
418 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
419 93658fb9 2020-03-18 stsp }
420 93658fb9 2020-03-18 stsp
421 8e278d17 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0) {
422 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
423 8e278d17 2020-03-18 stsp goto done;
424 8e278d17 2020-03-18 stsp }
425 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
426 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
427 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
428 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
429 20eb36d0 2020-03-18 stsp goto done;
430 20eb36d0 2020-03-18 stsp }
431 85e8591f 2020-03-18 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
432 93658fb9 2020-03-18 stsp if (err != NULL)
433 8e278d17 2020-03-18 stsp goto done;
434 20eb36d0 2020-03-18 stsp nfetchfd = -1;
435 f826addf 2020-03-18 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
436 93658fb9 2020-03-18 stsp if (err != NULL)
437 8e278d17 2020-03-18 stsp goto done;
438 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
439 8e278d17 2020-03-18 stsp if (npackfd == -1) {
440 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
441 8e278d17 2020-03-18 stsp goto done;
442 8e278d17 2020-03-18 stsp }
443 8e278d17 2020-03-18 stsp
444 d2cdc636 2020-03-18 stsp packfile_size = 0;
445 8f2d01a6 2020-03-18 stsp while (!done) {
446 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
447 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
448 531c3985 2020-03-18 stsp char *server_progress = NULL;
449 d2cdc636 2020-03-18 stsp off_t packfile_size_cur;
450 8e278d17 2020-03-18 stsp
451 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
452 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
453 85e8591f 2020-03-18 stsp &packfile_size_cur, &fetchibuf);
454 8f2d01a6 2020-03-18 stsp if (err != NULL)
455 8e278d17 2020-03-18 stsp goto done;
456 d9b4d0c0 2020-03-18 stsp if (done)
457 d9b4d0c0 2020-03-18 stsp *pack_hash = id;
458 d9b4d0c0 2020-03-18 stsp else if (refname && id) {
459 d9b4d0c0 2020-03-18 stsp err = got_pathlist_append(refs, refname, id);
460 8f2d01a6 2020-03-18 stsp if (err)
461 8e278d17 2020-03-18 stsp goto done;
462 531c3985 2020-03-18 stsp } else if (server_progress) {
463 531c3985 2020-03-18 stsp char *s, *s0 = server_progress;
464 531c3985 2020-03-18 stsp while ((s = strsep(&s0, "\r")) != NULL) {
465 531c3985 2020-03-18 stsp if (*s == '\0')
466 531c3985 2020-03-18 stsp continue;
467 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
468 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
469 531c3985 2020-03-18 stsp if (err)
470 531c3985 2020-03-18 stsp break;
471 531c3985 2020-03-18 stsp }
472 531c3985 2020-03-18 stsp free(server_progress);
473 531c3985 2020-03-18 stsp if (err)
474 531c3985 2020-03-18 stsp goto done;
475 d2cdc636 2020-03-18 stsp } else if (packfile_size_cur != packfile_size) {
476 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
477 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
478 d2cdc636 2020-03-18 stsp if (err)
479 d2cdc636 2020-03-18 stsp break;
480 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
481 8f2d01a6 2020-03-18 stsp }
482 8f2d01a6 2020-03-18 stsp }
483 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
484 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
485 8e278d17 2020-03-18 stsp goto done;
486 8e278d17 2020-03-18 stsp }
487 849f7557 2020-03-18 stsp
488 849f7557 2020-03-18 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
489 849f7557 2020-03-18 stsp err = got_error_from_errno("lseek");
490 849f7557 2020-03-18 stsp goto done;
491 849f7557 2020-03-18 stsp }
492 849f7557 2020-03-18 stsp err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
493 849f7557 2020-03-18 stsp if (err)
494 849f7557 2020-03-18 stsp goto done;
495 531c3985 2020-03-18 stsp
496 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
497 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
498 8e278d17 2020-03-18 stsp goto done;
499 8e278d17 2020-03-18 stsp }
500 85e8591f 2020-03-18 stsp idxpid = fork();
501 85e8591f 2020-03-18 stsp if (idxpid == -1) {
502 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
503 8e278d17 2020-03-18 stsp goto done;
504 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
505 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
506 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
507 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0) {
508 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
509 8e278d17 2020-03-18 stsp goto done;
510 8e278d17 2020-03-18 stsp }
511 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
512 93658fb9 2020-03-18 stsp
513 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
514 668a20f6 2020-03-18 stsp npackfd);
515 93658fb9 2020-03-18 stsp if (err != NULL)
516 8e278d17 2020-03-18 stsp goto done;
517 8e278d17 2020-03-18 stsp npackfd = -1;
518 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
519 93658fb9 2020-03-18 stsp if (err != NULL)
520 8e278d17 2020-03-18 stsp goto done;
521 8e278d17 2020-03-18 stsp nidxfd = -1;
522 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
523 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
524 d582f26c 2020-03-18 stsp if (err != NULL)
525 d582f26c 2020-03-18 stsp goto done;
526 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
527 d582f26c 2020-03-18 stsp }
528 baa9fea0 2020-03-18 stsp done = 0;
529 baa9fea0 2020-03-18 stsp while (!done) {
530 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
531 668a20f6 2020-03-18 stsp
532 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
533 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
534 668a20f6 2020-03-18 stsp &idxibuf);
535 baa9fea0 2020-03-18 stsp if (err != NULL)
536 baa9fea0 2020-03-18 stsp goto done;
537 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
538 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
539 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
540 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
541 baa9fea0 2020-03-18 stsp if (err)
542 baa9fea0 2020-03-18 stsp break;
543 baa9fea0 2020-03-18 stsp }
544 baa9fea0 2020-03-18 stsp imsg_clear(&idxibuf);
545 baa9fea0 2020-03-18 stsp }
546 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
547 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
548 8e278d17 2020-03-18 stsp goto done;
549 8e278d17 2020-03-18 stsp }
550 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
551 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
552 8e278d17 2020-03-18 stsp goto done;
553 8e278d17 2020-03-18 stsp }
554 93658fb9 2020-03-18 stsp
555 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
556 afa77e03 2020-03-18 stsp if (err)
557 8e278d17 2020-03-18 stsp goto done;
558 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
559 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
560 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
561 8e278d17 2020-03-18 stsp goto done;
562 8e278d17 2020-03-18 stsp }
563 93658fb9 2020-03-18 stsp
564 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
565 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
566 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
567 8e278d17 2020-03-18 stsp goto done;
568 8e278d17 2020-03-18 stsp }
569 afa77e03 2020-03-18 stsp
570 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
571 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
572 8e278d17 2020-03-18 stsp goto done;
573 8e278d17 2020-03-18 stsp }
574 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
575 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
576 8e278d17 2020-03-18 stsp goto done;
577 8e278d17 2020-03-18 stsp }
578 ee61b6d3 2020-03-18 stsp
579 8e278d17 2020-03-18 stsp done:
580 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
581 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
582 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
583 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
584 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
585 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
586 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
587 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
588 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
589 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
590 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
591 d582f26c 2020-03-18 stsp }
592 afa77e03 2020-03-18 stsp free(tmppackpath);
593 afa77e03 2020-03-18 stsp free(tmpidxpath);
594 fe4e1501 2020-03-18 stsp free(idxpath);
595 afa77e03 2020-03-18 stsp free(packpath);
596 fe4e1501 2020-03-18 stsp
597 d9b4d0c0 2020-03-18 stsp if (err) {
598 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
599 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
600 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
601 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
602 d9b4d0c0 2020-03-18 stsp free(pe->data);
603 d9b4d0c0 2020-03-18 stsp }
604 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
605 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
606 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
607 d9b4d0c0 2020-03-18 stsp free(pe->data);
608 d9b4d0c0 2020-03-18 stsp }
609 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
610 d9b4d0c0 2020-03-18 stsp }
611 8e278d17 2020-03-18 stsp return err;
612 93658fb9 2020-03-18 stsp }