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 93658fb9 2020-03-18 stsp
53 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
59 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
60 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
61 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
62 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
63 93658fb9 2020-03-18 stsp
64 93658fb9 2020-03-18 stsp #define GOT_PROTOMAX 64
65 93658fb9 2020-03-18 stsp #define GOT_HOSTMAX 256
66 93658fb9 2020-03-18 stsp #define GOT_PATHMAX 512
67 93658fb9 2020-03-18 stsp #define GOT_REPOMAX 256
68 93658fb9 2020-03-18 stsp #define GOT_PORTMAX 16
69 93658fb9 2020-03-18 stsp #define GOT_URIMAX 1024
70 93658fb9 2020-03-18 stsp
71 93658fb9 2020-03-18 stsp static int
72 93658fb9 2020-03-18 stsp mkpath(char *path)
73 93658fb9 2020-03-18 stsp {
74 93658fb9 2020-03-18 stsp char *p, namebuf[PATH_MAX];
75 93658fb9 2020-03-18 stsp struct stat sb;
76 93658fb9 2020-03-18 stsp int done;
77 93658fb9 2020-03-18 stsp
78 93658fb9 2020-03-18 stsp while (*path == '/')
79 93658fb9 2020-03-18 stsp path++;
80 93658fb9 2020-03-18 stsp if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
81 93658fb9 2020-03-18 stsp errno = ENAMETOOLONG;
82 93658fb9 2020-03-18 stsp return -1;
83 93658fb9 2020-03-18 stsp }
84 93658fb9 2020-03-18 stsp
85 93658fb9 2020-03-18 stsp p = namebuf;
86 93658fb9 2020-03-18 stsp for (;;) {
87 93658fb9 2020-03-18 stsp p += strspn(p, "/");
88 93658fb9 2020-03-18 stsp p += strcspn(p, "/");
89 93658fb9 2020-03-18 stsp done = (*p == '\0');
90 93658fb9 2020-03-18 stsp *p = '\0';
91 93658fb9 2020-03-18 stsp
92 93658fb9 2020-03-18 stsp if (mkdir(namebuf, 0755) != 0) {
93 93658fb9 2020-03-18 stsp int mkdir_errno = errno;
94 93658fb9 2020-03-18 stsp if (stat(path, &sb) == -1) {
95 93658fb9 2020-03-18 stsp /* Not there; use mkdir()s errno */
96 93658fb9 2020-03-18 stsp errno = mkdir_errno;
97 93658fb9 2020-03-18 stsp return -1;
98 93658fb9 2020-03-18 stsp }
99 93658fb9 2020-03-18 stsp if (!S_ISDIR(sb.st_mode)) {
100 93658fb9 2020-03-18 stsp /* Is there, but isn't a directory */
101 93658fb9 2020-03-18 stsp errno = ENOTDIR;
102 93658fb9 2020-03-18 stsp return -1;
103 93658fb9 2020-03-18 stsp }
104 93658fb9 2020-03-18 stsp }
105 93658fb9 2020-03-18 stsp
106 93658fb9 2020-03-18 stsp if (done)
107 93658fb9 2020-03-18 stsp break;
108 93658fb9 2020-03-18 stsp *p = '/';
109 93658fb9 2020-03-18 stsp }
110 93658fb9 2020-03-18 stsp
111 93658fb9 2020-03-18 stsp return 0;
112 93658fb9 2020-03-18 stsp }
113 93658fb9 2020-03-18 stsp
114 93658fb9 2020-03-18 stsp static int
115 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
116 93658fb9 2020-03-18 stsp {
117 93658fb9 2020-03-18 stsp int nb, ns;
118 93658fb9 2020-03-18 stsp
119 93658fb9 2020-03-18 stsp nb = strlen(base);
120 93658fb9 2020-03-18 stsp ns = strlen(suf);
121 93658fb9 2020-03-18 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
122 93658fb9 2020-03-18 stsp return 1;
123 93658fb9 2020-03-18 stsp return 0;
124 93658fb9 2020-03-18 stsp }
125 93658fb9 2020-03-18 stsp
126 93658fb9 2020-03-18 stsp static int
127 93658fb9 2020-03-18 stsp grab(char *dst, int n, char *p, char *e)
128 93658fb9 2020-03-18 stsp {
129 93658fb9 2020-03-18 stsp int l;
130 93658fb9 2020-03-18 stsp
131 93658fb9 2020-03-18 stsp l = e - p;
132 93658fb9 2020-03-18 stsp if (l >= n) {
133 93658fb9 2020-03-18 stsp errno = ENAMETOOLONG;
134 93658fb9 2020-03-18 stsp return -1;
135 93658fb9 2020-03-18 stsp }
136 93658fb9 2020-03-18 stsp return strlcpy(dst, p, l + 1);
137 93658fb9 2020-03-18 stsp }
138 93658fb9 2020-03-18 stsp
139 5cc27ede 2020-03-18 stsp static const struct got_error *
140 5cc27ede 2020-03-18 stsp dial_ssh(int *fetchfd, char *host, char *port, char *path, char *direction)
141 93658fb9 2020-03-18 stsp {
142 5cc27ede 2020-03-18 stsp const struct got_error *error = NULL;
143 93658fb9 2020-03-18 stsp int pid, pfd[2];
144 93658fb9 2020-03-18 stsp char cmd[64];
145 93658fb9 2020-03-18 stsp
146 5cc27ede 2020-03-18 stsp *fetchfd = -1;
147 5cc27ede 2020-03-18 stsp
148 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
149 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
150 5cc27ede 2020-03-18 stsp
151 93658fb9 2020-03-18 stsp pid = fork();
152 5cc27ede 2020-03-18 stsp if (pid == -1) {
153 5cc27ede 2020-03-18 stsp error = got_error_from_errno("fork");
154 5cc27ede 2020-03-18 stsp close(pfd[0]);
155 93658fb9 2020-03-18 stsp close(pfd[1]);
156 5cc27ede 2020-03-18 stsp return error;
157 5cc27ede 2020-03-18 stsp } else if (pid == 0) {
158 5cc27ede 2020-03-18 stsp int n;
159 5cc27ede 2020-03-18 stsp close(pfd[1]);
160 93658fb9 2020-03-18 stsp dup2(pfd[0], 0);
161 93658fb9 2020-03-18 stsp dup2(pfd[0], 1);
162 5cc27ede 2020-03-18 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
163 5cc27ede 2020-03-18 stsp if (n < 0 || n >= sizeof(cmd))
164 5cc27ede 2020-03-18 stsp err(1, "snprintf");
165 5cc27ede 2020-03-18 stsp if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
166 5cc27ede 2020-03-18 stsp err(1, "execlp");
167 5cc27ede 2020-03-18 stsp abort(); /* not reached */
168 5cc27ede 2020-03-18 stsp } else {
169 93658fb9 2020-03-18 stsp close(pfd[0]);
170 5cc27ede 2020-03-18 stsp *fetchfd = pfd[1];
171 5cc27ede 2020-03-18 stsp return NULL;
172 93658fb9 2020-03-18 stsp }
173 93658fb9 2020-03-18 stsp }
174 93658fb9 2020-03-18 stsp
175 5cc27ede 2020-03-18 stsp static const struct got_error *
176 5cc27ede 2020-03-18 stsp dial_git(int *fetchfd, char *host, char *port, char *path, char *direction)
177 93658fb9 2020-03-18 stsp {
178 5cc27ede 2020-03-18 stsp const struct got_error *err = NULL;
179 93658fb9 2020-03-18 stsp struct addrinfo hints, *servinfo, *p;
180 5cc27ede 2020-03-18 stsp char *cmd = NULL, *pkt = NULL;
181 4312a498 2020-03-18 stsp int fd = -1, totlen, r, eaicode;
182 5cc27ede 2020-03-18 stsp
183 5cc27ede 2020-03-18 stsp *fetchfd = -1;
184 93658fb9 2020-03-18 stsp
185 93658fb9 2020-03-18 stsp memset(&hints, 0, sizeof hints);
186 93658fb9 2020-03-18 stsp hints.ai_family = AF_UNSPEC;
187 93658fb9 2020-03-18 stsp hints.ai_socktype = SOCK_STREAM;
188 5cc27ede 2020-03-18 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
189 5cc27ede 2020-03-18 stsp if (eaicode)
190 5cc27ede 2020-03-18 stsp return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
191 93658fb9 2020-03-18 stsp
192 93658fb9 2020-03-18 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
193 93658fb9 2020-03-18 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
194 93658fb9 2020-03-18 stsp p->ai_protocol)) == -1)
195 93658fb9 2020-03-18 stsp continue;
196 93658fb9 2020-03-18 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
197 93658fb9 2020-03-18 stsp break;
198 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
199 93658fb9 2020-03-18 stsp close(fd);
200 93658fb9 2020-03-18 stsp }
201 93658fb9 2020-03-18 stsp if (p == NULL)
202 5cc27ede 2020-03-18 stsp goto done;
203 93658fb9 2020-03-18 stsp
204 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
205 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
206 5cc27ede 2020-03-18 stsp goto done;
207 5cc27ede 2020-03-18 stsp }
208 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
209 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
210 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
211 5cc27ede 2020-03-18 stsp goto done;
212 5cc27ede 2020-03-18 stsp }
213 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
214 4312a498 2020-03-18 stsp if (r == -1) {
215 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
216 4312a498 2020-03-18 stsp goto done;
217 4312a498 2020-03-18 stsp }
218 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
219 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
220 4312a498 2020-03-18 stsp goto done;
221 4312a498 2020-03-18 stsp }
222 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
223 4312a498 2020-03-18 stsp if (r == -1) {
224 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
225 4312a498 2020-03-18 stsp goto done;
226 4312a498 2020-03-18 stsp }
227 5cc27ede 2020-03-18 stsp done:
228 93658fb9 2020-03-18 stsp free(cmd);
229 93658fb9 2020-03-18 stsp free(pkt);
230 5cc27ede 2020-03-18 stsp if (err) {
231 5cc27ede 2020-03-18 stsp if (fd != -1)
232 5cc27ede 2020-03-18 stsp close(fd);
233 5cc27ede 2020-03-18 stsp } else
234 5cc27ede 2020-03-18 stsp *fetchfd = fd;
235 5cc27ede 2020-03-18 stsp return err;
236 93658fb9 2020-03-18 stsp }
237 93658fb9 2020-03-18 stsp
238 93658fb9 2020-03-18 stsp int
239 93658fb9 2020-03-18 stsp got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
240 93658fb9 2020-03-18 stsp {
241 93658fb9 2020-03-18 stsp char *s, *p, *q;
242 93658fb9 2020-03-18 stsp int n, hasport;
243 93658fb9 2020-03-18 stsp
244 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
245 93658fb9 2020-03-18 stsp if (!p) {
246 93658fb9 2020-03-18 stsp //werrstr("missing protocol");
247 93658fb9 2020-03-18 stsp return -1;
248 93658fb9 2020-03-18 stsp }
249 93658fb9 2020-03-18 stsp if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
250 93658fb9 2020-03-18 stsp return -1;
251 93658fb9 2020-03-18 stsp hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
252 93658fb9 2020-03-18 stsp s = p + 3;
253 93658fb9 2020-03-18 stsp p = NULL;
254 93658fb9 2020-03-18 stsp if (!hasport) {
255 93658fb9 2020-03-18 stsp p = strstr(s, ":");
256 93658fb9 2020-03-18 stsp if (p != NULL)
257 93658fb9 2020-03-18 stsp p++;
258 93658fb9 2020-03-18 stsp }
259 93658fb9 2020-03-18 stsp if (p == NULL)
260 93658fb9 2020-03-18 stsp p = strstr(s, "/");
261 93658fb9 2020-03-18 stsp if (p == NULL || strlen(p) == 1) {
262 93658fb9 2020-03-18 stsp //werrstr("missing path");
263 93658fb9 2020-03-18 stsp return -1;
264 93658fb9 2020-03-18 stsp }
265 93658fb9 2020-03-18 stsp
266 93658fb9 2020-03-18 stsp q = memchr(s, ':', p - s);
267 93658fb9 2020-03-18 stsp if (q) {
268 93658fb9 2020-03-18 stsp grab(host, GOT_HOSTMAX, s, q);
269 93658fb9 2020-03-18 stsp grab(port, GOT_PORTMAX, q + 1, p);
270 93658fb9 2020-03-18 stsp }else{
271 93658fb9 2020-03-18 stsp grab(host, GOT_HOSTMAX, s, p);
272 93658fb9 2020-03-18 stsp snprintf(port, GOT_PORTMAX, "9418");
273 93658fb9 2020-03-18 stsp }
274 93658fb9 2020-03-18 stsp
275 93658fb9 2020-03-18 stsp snprintf(path, GOT_PATHMAX, "%s", p);
276 93658fb9 2020-03-18 stsp p = strrchr(p, '/') + 1;
277 93658fb9 2020-03-18 stsp if (!p || strlen(p) == 0) {
278 93658fb9 2020-03-18 stsp //werrstr("missing repository in uri");
279 93658fb9 2020-03-18 stsp return -1;
280 93658fb9 2020-03-18 stsp }
281 93658fb9 2020-03-18 stsp n = strlen(p);
282 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
283 93658fb9 2020-03-18 stsp n -= 4;
284 93658fb9 2020-03-18 stsp grab(repo, GOT_REPOMAX, p, p + n);
285 93658fb9 2020-03-18 stsp return 0;
286 93658fb9 2020-03-18 stsp }
287 93658fb9 2020-03-18 stsp
288 93658fb9 2020-03-18 stsp const struct got_error*
289 84f2fa52 2020-03-18 stsp got_fetch(char *uri, char *branch_filter, char *destdir)
290 93658fb9 2020-03-18 stsp {
291 93658fb9 2020-03-18 stsp char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
292 93658fb9 2020-03-18 stsp char repo[GOT_REPOMAX], path[GOT_PATHMAX];
293 93658fb9 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
294 8f2d01a6 2020-03-18 stsp int packfd = -1, npackfd, idxfd = -1, nidxfd, status, done = 0;
295 8f2d01a6 2020-03-18 stsp struct got_object_id *packhash = NULL;
296 93658fb9 2020-03-18 stsp const struct got_error *err;
297 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
298 93658fb9 2020-03-18 stsp pid_t pid;
299 afa77e03 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL, *default_destdir = NULL;
300 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
301 93658fb9 2020-03-18 stsp
302 93658fb9 2020-03-18 stsp fetchfd = -1;
303 93658fb9 2020-03-18 stsp if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
304 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PARSE_URI);
305 22b6b490 2020-03-18 stsp if (destdir == NULL) {
306 22b6b490 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo) == -1)
307 22b6b490 2020-03-18 stsp return got_error_from_errno("asprintf");
308 22b6b490 2020-03-18 stsp }
309 22b6b490 2020-03-18 stsp err = got_repo_init(destdir ? destdir : default_destdir);
310 93658fb9 2020-03-18 stsp if (err != NULL)
311 93658fb9 2020-03-18 stsp return err;
312 22b6b490 2020-03-18 stsp if (chdir(destdir ? destdir : default_destdir))
313 93658fb9 2020-03-18 stsp return got_error_from_errno("enter new repo");
314 afa77e03 2020-03-18 stsp if (mkpath("objects/pack") == -1)
315 93658fb9 2020-03-18 stsp return got_error_from_errno("mkpath");
316 afa77e03 2020-03-18 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd,
317 afa77e03 2020-03-18 stsp "objects/pack/fetching.pack");
318 fe4e1501 2020-03-18 stsp if (err)
319 fe4e1501 2020-03-18 stsp return err;
320 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
321 93658fb9 2020-03-18 stsp if (npackfd == -1)
322 93658fb9 2020-03-18 stsp return got_error_from_errno("dup");
323 afa77e03 2020-03-18 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd,
324 afa77e03 2020-03-18 stsp "objects/pack/fetching.idx");
325 fe4e1501 2020-03-18 stsp if (err)
326 fe4e1501 2020-03-18 stsp return err;
327 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
328 93658fb9 2020-03-18 stsp if (nidxfd == -1)
329 93658fb9 2020-03-18 stsp return got_error_from_errno("dup");
330 93658fb9 2020-03-18 stsp
331 93658fb9 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
332 5cc27ede 2020-03-18 stsp err = dial_ssh(&fetchfd, host, port, path, "upload");
333 93658fb9 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
334 5cc27ede 2020-03-18 stsp err = dial_git(&fetchfd, host, port, path, "upload");
335 93658fb9 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
336 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PROTO);
337 93658fb9 2020-03-18 stsp else
338 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PROTO);
339 75fd4eb2 2020-03-18 stsp if (err)
340 75fd4eb2 2020-03-18 stsp return err;
341 93658fb9 2020-03-18 stsp
342 93658fb9 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
343 93658fb9 2020-03-18 stsp return got_error_from_errno("socketpair");
344 93658fb9 2020-03-18 stsp
345 93658fb9 2020-03-18 stsp pid = fork();
346 93658fb9 2020-03-18 stsp if (pid == -1)
347 93658fb9 2020-03-18 stsp return got_error_from_errno("fork");
348 93658fb9 2020-03-18 stsp else if (pid == 0){
349 93658fb9 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
350 93658fb9 2020-03-18 stsp }
351 93658fb9 2020-03-18 stsp
352 93658fb9 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0)
353 93658fb9 2020-03-18 stsp return got_error_from_errno("close");
354 93658fb9 2020-03-18 stsp imsg_init(&ibuf, imsg_fetchfds[0]);
355 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_req(&ibuf, fetchfd);
356 93658fb9 2020-03-18 stsp if (err != NULL)
357 93658fb9 2020-03-18 stsp return err;
358 93658fb9 2020-03-18 stsp err = got_privsep_send_tmpfd(&ibuf, npackfd);
359 93658fb9 2020-03-18 stsp if (err != NULL)
360 93658fb9 2020-03-18 stsp return err;
361 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
362 93658fb9 2020-03-18 stsp if (npackfd == -1)
363 93658fb9 2020-03-18 stsp return got_error_from_errno("dup");
364 8f2d01a6 2020-03-18 stsp while (!done) {
365 8f2d01a6 2020-03-18 stsp struct got_object_id *id;
366 8f2d01a6 2020-03-18 stsp char *refname;
367 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
368 8f2d01a6 2020-03-18 stsp &id, &refname, &ibuf);
369 8f2d01a6 2020-03-18 stsp if (err != NULL)
370 8f2d01a6 2020-03-18 stsp return err;
371 8f2d01a6 2020-03-18 stsp if (done) {
372 8f2d01a6 2020-03-18 stsp packhash = got_object_id_dup(id);
373 8f2d01a6 2020-03-18 stsp if (packhash == NULL)
374 8f2d01a6 2020-03-18 stsp return got_error_from_errno(
375 8f2d01a6 2020-03-18 stsp "got_object_id_dup");
376 8f2d01a6 2020-03-18 stsp } else {
377 8f2d01a6 2020-03-18 stsp char *id_str;
378 8f2d01a6 2020-03-18 stsp /* TODO Use a progress callback */
379 8f2d01a6 2020-03-18 stsp err = got_object_id_str(&id_str, id);
380 8f2d01a6 2020-03-18 stsp if (err)
381 8f2d01a6 2020-03-18 stsp return err;
382 8f2d01a6 2020-03-18 stsp printf( "%.12s %s\n", id_str, refname);
383 8f2d01a6 2020-03-18 stsp }
384 8f2d01a6 2020-03-18 stsp }
385 93658fb9 2020-03-18 stsp if (waitpid(pid, &status, 0) == -1)
386 93658fb9 2020-03-18 stsp return got_error_from_errno("child exit");
387 93658fb9 2020-03-18 stsp
388 93658fb9 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
389 93658fb9 2020-03-18 stsp return got_error_from_errno("socketpair");
390 93658fb9 2020-03-18 stsp pid = fork();
391 93658fb9 2020-03-18 stsp if (pid == -1)
392 93658fb9 2020-03-18 stsp return got_error_from_errno("fork");
393 93658fb9 2020-03-18 stsp else if (pid == 0)
394 93658fb9 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
395 93658fb9 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0)
396 93658fb9 2020-03-18 stsp return got_error_from_errno("close");
397 93658fb9 2020-03-18 stsp imsg_init(&ibuf, imsg_idxfds[0]);
398 93658fb9 2020-03-18 stsp
399 8f2d01a6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
400 93658fb9 2020-03-18 stsp if (err != NULL)
401 93658fb9 2020-03-18 stsp return err;
402 93658fb9 2020-03-18 stsp err = got_privsep_send_tmpfd(&ibuf, nidxfd);
403 93658fb9 2020-03-18 stsp if (err != NULL)
404 93658fb9 2020-03-18 stsp return err;
405 93658fb9 2020-03-18 stsp err = got_privsep_wait_index_pack_done(&ibuf);
406 93658fb9 2020-03-18 stsp if (err != NULL)
407 93658fb9 2020-03-18 stsp return err;
408 93658fb9 2020-03-18 stsp imsg_clear(&ibuf);
409 93658fb9 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1)
410 93658fb9 2020-03-18 stsp return got_error_from_errno("close child");
411 93658fb9 2020-03-18 stsp if (waitpid(pid, &status, 0) == -1)
412 93658fb9 2020-03-18 stsp return got_error_from_errno("child exit");
413 93658fb9 2020-03-18 stsp
414 8f2d01a6 2020-03-18 stsp err = got_object_id_str(&id_str, packhash);
415 afa77e03 2020-03-18 stsp if (err)
416 afa77e03 2020-03-18 stsp return err;
417 afa77e03 2020-03-18 stsp if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1)
418 afa77e03 2020-03-18 stsp return got_error_from_errno("asprintf");
419 93658fb9 2020-03-18 stsp
420 afa77e03 2020-03-18 stsp if (asprintf(&idxpath, "objects/pack/pack-%s.idx", id_str) == -1)
421 afa77e03 2020-03-18 stsp return got_error_from_errno("asprintf");
422 afa77e03 2020-03-18 stsp
423 afa77e03 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1)
424 afa77e03 2020-03-18 stsp return got_error_from_errno3("rename", tmppackpath, packpath);
425 afa77e03 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1)
426 afa77e03 2020-03-18 stsp return got_error_from_errno3("rename", tmpidxpath, idxpath);
427 afa77e03 2020-03-18 stsp
428 afa77e03 2020-03-18 stsp free(tmppackpath);
429 afa77e03 2020-03-18 stsp free(tmpidxpath);
430 fe4e1501 2020-03-18 stsp free(idxpath);
431 afa77e03 2020-03-18 stsp free(packpath);
432 22b6b490 2020-03-18 stsp free(default_destdir);
433 8f2d01a6 2020-03-18 stsp free(packhash);
434 fe4e1501 2020-03-18 stsp
435 93658fb9 2020-03-18 stsp return NULL;
436 93658fb9 2020-03-18 stsp }