Blame


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