Blame


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