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