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