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