2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
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/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/socket.h>
21 93658fb9 2020-03-18 stsp #include <sys/wait.h>
22 93658fb9 2020-03-18 stsp #include <sys/resource.h>
23 93658fb9 2020-03-18 stsp #include <sys/socket.h>
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 5cc27ede 2020-03-18 stsp #include <err.h>
27 93658fb9 2020-03-18 stsp #include <fcntl.h>
28 93658fb9 2020-03-18 stsp #include <stdio.h>
29 93658fb9 2020-03-18 stsp #include <stdlib.h>
30 93658fb9 2020-03-18 stsp #include <string.h>
31 93658fb9 2020-03-18 stsp #include <stdint.h>
32 81a12da5 2020-09-09 naddy #include <unistd.h>
33 93658fb9 2020-03-18 stsp #include <zlib.h>
34 93658fb9 2020-03-18 stsp #include <ctype.h>
35 93658fb9 2020-03-18 stsp #include <limits.h>
36 93658fb9 2020-03-18 stsp #include <time.h>
37 93658fb9 2020-03-18 stsp #include <uuid.h>
39 93658fb9 2020-03-18 stsp #include "got_error.h"
40 93658fb9 2020-03-18 stsp #include "got_reference.h"
41 93658fb9 2020-03-18 stsp #include "got_repository.h"
42 93658fb9 2020-03-18 stsp #include "got_path.h"
43 93658fb9 2020-03-18 stsp #include "got_cancel.h"
44 93658fb9 2020-03-18 stsp #include "got_worktree.h"
45 93658fb9 2020-03-18 stsp #include "got_object.h"
46 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
47 82ebf666 2020-03-18 stsp #include "got_fetch.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
52 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
53 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
59 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
60 77d7d3bb 2021-09-05 stsp #include "got_lib_pkt.h"
62 d582f26c 2020-03-18 stsp #ifndef nitems
63 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 ccf6dd5e 2020-12-19 stsp #ifndef ssizeof
67 ccf6dd5e 2020-12-19 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 68999b92 2020-03-18 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 20eb36d0 2020-03-18 stsp const struct got_error *
75 9c52365f 2020-03-21 stsp got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
76 9c52365f 2020-03-21 stsp const char *host, const char *port, const char *server_path, int verbosity)
78 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
80 9c52365f 2020-03-21 stsp *fetchpid = -1;
81 20eb36d0 2020-03-18 stsp *fetchfd = -1;
83 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
84 d65a88a2 2021-09-05 stsp err = got_dial_ssh(fetchpid, fetchfd, host, port,
85 d65a88a2 2021-09-05 stsp server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
86 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
87 d65a88a2 2021-09-05 stsp err = got_dial_git(fetchfd, host, port, server_path,
88 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_FETCH);
89 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
90 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
92 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
96 b235acd4 2021-12-31 thomas const struct got_error *
97 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
98 469dd726 2020-03-20 stsp struct got_pathlist_head *symrefs, const char *remote_name,
99 4ba14133 2020-03-20 stsp int mirror_references, int fetch_all_branches,
100 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
101 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
102 0e4002ca 2020-03-21 stsp int fetchfd, struct got_repository *repo,
103 469dd726 2020-03-20 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
106 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
107 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
108 16aeacf7 2020-11-26 stsp int tmpfds[3];
109 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
110 93658fb9 2020-03-18 stsp const struct got_error *err;
111 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
112 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
113 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
114 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
115 41b0de12 2020-03-21 stsp const char *repo_path = NULL;
116 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
117 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
118 7848a0e1 2020-03-19 stsp struct got_reflist_head my_refs;
119 7848a0e1 2020-03-19 stsp struct got_reflist_entry *re;
120 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
121 393fb88d 2020-03-21 stsp struct got_packfile_hdr pack_hdr;
122 393fb88d 2020-03-21 stsp uint32_t nobj = 0;
123 ee61b6d3 2020-03-18 stsp char *path;
124 f1c6967f 2020-03-19 stsp char *progress = NULL;
126 92dc95a8 2020-03-24 stsp *pack_hash = NULL;
129 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
130 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
132 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
133 0e4002ca 2020-03-21 stsp const char *refname = pe->path;
134 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
135 0e4002ca 2020-03-21 stsp strncmp(refname, "got/", 4) == 0 ||
136 0e4002ca 2020-03-21 stsp strncmp(refname, "refs/remotes/", 13) == 0 ||
137 0e4002ca 2020-03-21 stsp strncmp(refname, "remotes/", 8) == 0)
138 0e4002ca 2020-03-21 stsp return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
141 41b0de12 2020-03-21 stsp if (!list_refs_only)
142 41b0de12 2020-03-21 stsp repo_path = got_repo_get_path_git_dir(repo);
144 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
145 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
147 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
148 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
150 41b0de12 2020-03-21 stsp if (!list_refs_only) {
151 41b0de12 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL,
152 41b0de12 2020-03-21 stsp got_ref_cmp_by_name, NULL);
157 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
158 7848a0e1 2020-03-19 stsp struct got_object_id *id;
159 7848a0e1 2020-03-19 stsp const char *refname;
161 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(re->ref))
164 726fb900 2021-09-28 thomas err = got_ref_resolve(&id, repo, re->ref);
166 726fb900 2021-09-28 thomas goto done;
167 726fb900 2021-09-28 thomas refname = strdup(got_ref_get_name(re->ref));
168 726fb900 2021-09-28 thomas if (refname == NULL) {
169 726fb900 2021-09-28 thomas err = got_error_from_errno("strdup");
170 726fb900 2021-09-28 thomas goto done;
172 726fb900 2021-09-28 thomas err = got_pathlist_append(&have_refs, refname, id);
174 726fb900 2021-09-28 thomas goto done;
177 41b0de12 2020-03-21 stsp if (list_refs_only) {
178 41b0de12 2020-03-21 stsp packfd = got_opentempfd();
179 41b0de12 2020-03-21 stsp if (packfd == -1) {
180 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
184 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.pack",
185 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
186 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
189 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
190 41b0de12 2020-03-21 stsp free(path);
192 0843a4ce 2020-10-31 semarie goto done;
193 0843a4ce 2020-10-31 semarie if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
194 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmppackpath);
198 41b0de12 2020-03-21 stsp if (list_refs_only) {
199 41b0de12 2020-03-21 stsp idxfd = got_opentempfd();
200 41b0de12 2020-03-21 stsp if (idxfd == -1) {
201 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
205 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.idx",
206 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
207 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
210 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
211 41b0de12 2020-03-21 stsp free(path);
213 0843a4ce 2020-10-31 semarie goto done;
214 0843a4ce 2020-10-31 semarie if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
215 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmpidxpath);
219 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
220 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
221 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
225 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
226 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
227 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
228 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
233 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
234 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
238 85e8591f 2020-03-18 stsp fetchpid = fork();
239 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
240 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
242 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
243 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
244 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
247 08578a35 2021-01-22 stsp if (close(imsg_fetchfds[1]) == -1) {
248 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
251 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
252 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
253 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
254 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
257 659e7fbd 2020-03-20 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
258 0e4002ca 2020-03-21 stsp fetch_all_branches, wanted_branches, wanted_refs,
259 0e4002ca 2020-03-21 stsp list_refs_only, verbosity);
260 93658fb9 2020-03-18 stsp if (err != NULL)
262 20eb36d0 2020-03-18 stsp nfetchfd = -1;
263 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
264 8e278d17 2020-03-18 stsp if (npackfd == -1) {
265 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
268 393fb88d 2020-03-21 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
269 393fb88d 2020-03-21 stsp if (err != NULL)
271 393fb88d 2020-03-21 stsp npackfd = -1;
273 d2cdc636 2020-03-18 stsp packfile_size = 0;
274 77d7d3bb 2021-09-05 stsp progress = calloc(GOT_PKT_MAX, 1);
275 f1c6967f 2020-03-19 stsp if (progress == NULL) {
276 f1c6967f 2020-03-19 stsp err = got_error_from_errno("calloc");
280 1d72a2a0 2020-03-24 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
281 1d72a2a0 2020-03-24 stsp if (*pack_hash == NULL) {
282 1d72a2a0 2020-03-24 stsp err = got_error_from_errno("calloc");
286 8f2d01a6 2020-03-18 stsp while (!done) {
287 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
288 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
289 531c3985 2020-03-18 stsp char *server_progress = NULL;
290 7848a0e1 2020-03-19 stsp off_t packfile_size_cur = 0;
292 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
293 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
294 1d72a2a0 2020-03-24 stsp &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
295 8f2d01a6 2020-03-18 stsp if (err != NULL)
297 ea83355f 2021-10-08 thomas /* Don't report size progress for an empty pack file. */
298 ea83355f 2021-10-08 thomas if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
299 ea83355f 2021-10-08 thomas packfile_size_cur = 0;
300 1d72a2a0 2020-03-24 stsp if (!done && refname && id) {
301 41b0de12 2020-03-21 stsp err = got_pathlist_insert(NULL, refs, refname, id);
304 1d72a2a0 2020-03-24 stsp } else if (!done && server_progress) {
307 f1c6967f 2020-03-19 stsp * XXX git-daemon tends to send batched output with
308 f1c6967f 2020-03-19 stsp * lines spanning separate packets. Buffer progress
309 f1c6967f 2020-03-19 stsp * output until we see a CR or LF to avoid giving
310 f1c6967f 2020-03-19 stsp * partial lines of progress output to the callback.
312 f1c6967f 2020-03-19 stsp if (strlcat(progress, server_progress,
313 77d7d3bb 2021-09-05 stsp GOT_PKT_MAX) >= GOT_PKT_MAX) {
314 f1c6967f 2020-03-19 stsp progress[0] = '\0'; /* discard */
317 f1c6967f 2020-03-19 stsp while ((p = strchr(progress, '\r')) != NULL ||
318 f1c6967f 2020-03-19 stsp (p = strchr(progress, '\n')) != NULL) {
321 f1c6967f 2020-03-19 stsp char c = *p;
323 f1c6967f 2020-03-19 stsp if (asprintf(&s, "%s%s", progress,
324 f1c6967f 2020-03-19 stsp c == '\n' ? "\n" : "") == -1) {
325 f1c6967f 2020-03-19 stsp err = got_error_from_errno("asprintf");
328 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
329 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
333 f1c6967f 2020-03-19 stsp n = strlen(progress);
334 77d7d3bb 2021-09-05 stsp if (n < GOT_PKT_MAX - 1) {
335 f1c6967f 2020-03-19 stsp memmove(progress, &progress[n + 1],
336 77d7d3bb 2021-09-05 stsp GOT_PKT_MAX - n - 1);
338 f1c6967f 2020-03-19 stsp progress[0] = '\0';
340 531c3985 2020-03-18 stsp free(server_progress);
343 1d72a2a0 2020-03-24 stsp } else if (!done && packfile_size_cur != packfile_size) {
344 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
345 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
348 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
351 4049b748 2022-02-16 thomas if (close(imsg_fetchfds[0]) == -1) {
352 4049b748 2022-02-16 thomas err = got_error_from_errno("close");
353 4049b748 2022-02-16 thomas goto done;
355 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
356 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
360 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
361 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
365 7848a0e1 2020-03-19 stsp /* If zero data was fetched without error we are already up-to-date. */
366 1d72a2a0 2020-03-24 stsp if (packfile_size == 0) {
367 1d72a2a0 2020-03-24 stsp free(*pack_hash);
368 1d72a2a0 2020-03-24 stsp *pack_hash = NULL;
370 ccf6dd5e 2020-12-19 stsp } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
371 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
376 ccf6dd5e 2020-12-19 stsp n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
377 393fb88d 2020-03-21 stsp if (n == -1) {
378 393fb88d 2020-03-21 stsp err = got_error_from_errno("read");
381 ccf6dd5e 2020-12-19 stsp if (n != ssizeof(pack_hdr)) {
382 393fb88d 2020-03-21 stsp err = got_error(GOT_ERR_IO);
385 393fb88d 2020-03-21 stsp if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
386 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
387 393fb88d 2020-03-21 stsp "bad pack file signature");
390 393fb88d 2020-03-21 stsp if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
391 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
392 393fb88d 2020-03-21 stsp "bad pack file version");
395 78fb0967 2020-09-09 naddy nobj = be32toh(pack_hdr.nobjects);
396 393fb88d 2020-03-21 stsp if (nobj == 0 &&
397 ccf6dd5e 2020-12-19 stsp packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
398 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
399 393fb88d 2020-03-21 stsp "bad pack file with zero objects");
400 393fb88d 2020-03-21 stsp if (nobj != 0 &&
401 ccf6dd5e 2020-12-19 stsp packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
402 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
403 393fb88d 2020-03-21 stsp "empty pack file with non-zero object count");
407 393fb88d 2020-03-21 stsp * If the pack file contains no objects, we may only need to update
408 393fb88d 2020-03-21 stsp * references in our repository. The caller will take care of that.
410 ea83355f 2021-10-08 thomas if (nobj == 0) {
411 ea83355f 2021-10-08 thomas free(*pack_hash);
412 ea83355f 2021-10-08 thomas *pack_hash = NULL;
416 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
417 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
421 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
422 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
425 85e8591f 2020-03-18 stsp idxpid = fork();
426 85e8591f 2020-03-18 stsp if (idxpid == -1) {
427 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
429 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
430 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
431 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
432 08578a35 2021-01-22 stsp if (close(imsg_idxfds[1]) == -1) {
433 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
436 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
438 393fb88d 2020-03-21 stsp npackfd = dup(packfd);
439 393fb88d 2020-03-21 stsp if (npackfd == -1) {
440 393fb88d 2020-03-21 stsp err = got_error_from_errno("dup");
443 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
445 93658fb9 2020-03-18 stsp if (err != NULL)
447 8e278d17 2020-03-18 stsp npackfd = -1;
448 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
449 93658fb9 2020-03-18 stsp if (err != NULL)
451 8e278d17 2020-03-18 stsp nidxfd = -1;
452 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
453 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
454 d582f26c 2020-03-18 stsp if (err != NULL)
456 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
459 baa9fea0 2020-03-18 stsp while (!done) {
460 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
462 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
463 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
465 baa9fea0 2020-03-18 stsp if (err != NULL)
467 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
468 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
469 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
470 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
475 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
476 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
479 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
480 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
484 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
487 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
488 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
489 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
493 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
494 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
495 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
499 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
500 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
503 7848a0e1 2020-03-19 stsp free(tmppackpath);
504 7848a0e1 2020-03-19 stsp tmppackpath = NULL;
505 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
506 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
509 7848a0e1 2020-03-19 stsp free(tmpidxpath);
510 7848a0e1 2020-03-19 stsp tmpidxpath = NULL;
513 7848a0e1 2020-03-19 stsp if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
514 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmppackpath);
515 7848a0e1 2020-03-19 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
516 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmpidxpath);
517 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
518 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
519 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
520 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
521 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
522 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
523 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
524 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
525 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
526 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
527 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
529 afa77e03 2020-03-18 stsp free(tmppackpath);
530 afa77e03 2020-03-18 stsp free(tmpidxpath);
531 fe4e1501 2020-03-18 stsp free(idxpath);
532 afa77e03 2020-03-18 stsp free(packpath);
533 f1c6967f 2020-03-19 stsp free(progress);
535 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
536 7848a0e1 2020-03-19 stsp free((char *)pe->path);
537 7848a0e1 2020-03-19 stsp free(pe->data);
539 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
540 7848a0e1 2020-03-19 stsp got_ref_list_free(&my_refs);
542 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
543 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
544 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
545 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
546 d9b4d0c0 2020-03-18 stsp free(pe->data);
548 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
549 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
550 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
551 d9b4d0c0 2020-03-18 stsp free(pe->data);
553 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
555 8e278d17 2020-03-18 stsp return err;