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