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