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