Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <sha1.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
41 #include <uuid.h>
42 #include <netdb.h>
43 #include <netinet/in.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_object.h"
52 #include "got_opentemp.h"
53 #include "got_fetch.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 #ifndef MIN
71 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
72 #endif
74 static int
75 hassuffix(char *base, char *suf)
76 {
77 int nb, ns;
79 nb = strlen(base);
80 ns = strlen(suf);
81 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
82 return 1;
83 return 0;
84 }
86 static const struct got_error *
87 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
88 const char *path, const char *direction, int verbosity)
89 {
90 const struct got_error *error = NULL;
91 int pid, pfd[2];
92 char cmd[64];
93 char *argv[11];
94 int i = 0, j;
96 *fetchpid = -1;
97 *fetchfd = -1;
99 argv[i++] = GOT_FETCH_PATH_SSH;
100 if (port != NULL) {
101 argv[i++] = "-p";
102 argv[i++] = (char *)port;
104 if (verbosity == -1) {
105 argv[i++] = "-q";
106 } else {
107 /* ssh(1) allows up to 3 "-v" options. */
108 for (j = 0; j < MIN(3, verbosity); j++)
109 argv[i++] = "-v";
111 argv[i++] = "--";
112 argv[i++] = (char *)host;
113 argv[i++] = (char *)cmd;
114 argv[i++] = (char *)path;
115 argv[i++] = NULL;
117 if (pipe(pfd) == -1)
118 return got_error_from_errno("pipe");
120 pid = fork();
121 if (pid == -1) {
122 error = got_error_from_errno("fork");
123 close(pfd[0]);
124 close(pfd[1]);
125 return error;
126 } else if (pid == 0) {
127 int n;
128 close(pfd[1]);
129 dup2(pfd[0], 0);
130 dup2(pfd[0], 1);
131 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
132 if (n < 0 || n >= sizeof(cmd))
133 err(1, "snprintf");
134 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
135 err(1, "execl");
136 abort(); /* not reached */
137 } else {
138 close(pfd[0]);
139 *fetchpid = pid;
140 *fetchfd = pfd[1];
141 return NULL;
145 static const struct got_error *
146 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
147 const char *direction)
149 const struct got_error *err = NULL;
150 struct addrinfo hints, *servinfo, *p;
151 char *cmd = NULL, *pkt = NULL;
152 int fd = -1, totlen, r, eaicode;
154 *fetchfd = -1;
156 if (port == NULL)
157 port = GOT_DEFAULT_GIT_PORT_STR;
159 memset(&hints, 0, sizeof hints);
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 eaicode = getaddrinfo(host, port, &hints, &servinfo);
163 if (eaicode) {
164 char msg[512];
165 snprintf(msg, sizeof(msg), "%s: %s", host,
166 gai_strerror(eaicode));
167 return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 for (p = servinfo; p != NULL; p = p->ai_next) {
171 if ((fd = socket(p->ai_family, p->ai_socktype,
172 p->ai_protocol)) == -1)
173 continue;
174 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
175 err = NULL;
176 break;
178 err = got_error_from_errno("connect");
179 close(fd);
181 if (p == NULL)
182 goto done;
184 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
189 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 r = write(fd, pkt, strlen(pkt) + 1);
194 if (r == -1) {
195 err = got_error_from_errno("write");
196 goto done;
198 if (asprintf(&pkt, "host=%s", host) == -1) {
199 err = got_error_from_errno("asprintf");
200 goto done;
202 r = write(fd, pkt, strlen(pkt) + 1);
203 if (r == -1) {
204 err = got_error_from_errno("write");
205 goto done;
207 done:
208 free(cmd);
209 free(pkt);
210 if (err) {
211 if (fd != -1)
212 close(fd);
213 } else
214 *fetchfd = fd;
215 return err;
218 const struct got_error *
219 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
220 const char *host, const char *port, const char *server_path, int verbosity)
222 const struct got_error *err = NULL;
224 *fetchpid = -1;
225 *fetchfd = -1;
227 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
228 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
229 "upload", verbosity);
230 else if (strcmp(proto, "git") == 0)
231 err = dial_git(fetchfd, host, port, server_path, "upload");
232 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
233 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
234 else
235 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
236 return err;
239 const struct got_error *
240 got_fetch_parse_uri(char **proto, char **host, char **port,
241 char **server_path, char **repo_name, const char *uri)
243 const struct got_error *err = NULL;
244 char *s, *p, *q;
245 int n;
247 *proto = *host = *port = *server_path = *repo_name = NULL;
249 p = strstr(uri, "://");
250 if (!p) {
251 /* Try parsing Git's "scp" style URL syntax. */
252 *proto = strdup("ssh");
253 if (proto == NULL) {
254 err = got_error_from_errno("strdup");
255 goto done;
257 s = (char *)uri;
258 q = strchr(s, ':');
259 if (q == NULL) {
260 err = got_error(GOT_ERR_PARSE_URI);
261 goto done;
263 /* No slashes allowed before first colon. */
264 p = strchr(s, '/');
265 if (p && q > p) {
266 err = got_error(GOT_ERR_PARSE_URI);
267 goto done;
269 *host = strndup(s, q - s);
270 if (*host == NULL) {
271 err = got_error_from_errno("strndup");
272 goto done;
274 p = q + 1;
275 } else {
276 *proto = strndup(uri, p - uri);
277 if (proto == NULL) {
278 err = got_error_from_errno("strndup");
279 goto done;
281 s = p + 3;
283 p = strstr(s, "/");
284 if (p == NULL || strlen(p) == 1) {
285 err = got_error(GOT_ERR_PARSE_URI);
286 goto done;
289 q = memchr(s, ':', p - s);
290 if (q) {
291 *host = strndup(s, q - s);
292 if (*host == NULL) {
293 err = got_error_from_errno("strndup");
294 goto done;
296 *port = strndup(q + 1, p - (q + 1));
297 if (*port == NULL) {
298 err = got_error_from_errno("strndup");
299 goto done;
301 } else {
302 *host = strndup(s, p - s);
303 if (*host == NULL) {
304 err = got_error_from_errno("strndup");
305 goto done;
310 while (p[0] == '/' && p[1] == '/')
311 p++;
312 *server_path = strdup(p);
313 if (*server_path == NULL) {
314 err = got_error_from_errno("strdup");
315 goto done;
317 got_path_strip_trailing_slashes(*server_path);
319 p = strrchr(p, '/');
320 if (!p || strlen(p) <= 1) {
321 err = got_error(GOT_ERR_PARSE_URI);
322 goto done;
324 p++;
325 n = strlen(p);
326 if (n == 0) {
327 err = got_error(GOT_ERR_PARSE_URI);
328 goto done;
330 if (hassuffix(p, ".git"))
331 n -= 4;
332 *repo_name = strndup(p, (p + n) - p);
333 if (*repo_name == NULL) {
334 err = got_error_from_errno("strndup");
335 goto done;
337 done:
338 if (err) {
339 free(*proto);
340 *proto = NULL;
341 free(*host);
342 *host = NULL;
343 free(*port);
344 *port = NULL;
345 free(*server_path);
346 *server_path = NULL;
347 free(*repo_name);
348 *repo_name = NULL;
350 return err;
353 const struct got_error*
354 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
355 struct got_pathlist_head *symrefs, const char *remote_name,
356 int mirror_references, int fetch_all_branches,
357 struct got_pathlist_head *wanted_branches,
358 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
359 int fetchfd, struct got_repository *repo,
360 got_fetch_progress_cb progress_cb, void *progress_arg)
362 int imsg_fetchfds[2], imsg_idxfds[2];
363 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
364 int tmpfds[3], i;
365 int fetchstatus, idxstatus, done = 0;
366 const struct got_error *err;
367 struct imsgbuf fetchibuf, idxibuf;
368 pid_t fetchpid, idxpid;
369 char *tmppackpath = NULL, *tmpidxpath = NULL;
370 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
371 const char *repo_path = NULL;
372 struct got_pathlist_head have_refs;
373 struct got_pathlist_entry *pe;
374 struct got_reflist_head my_refs;
375 struct got_reflist_entry *re;
376 off_t packfile_size = 0;
377 struct got_packfile_hdr pack_hdr;
378 uint32_t nobj = 0;
379 char *ref_prefix = NULL;
380 size_t ref_prefixlen = 0;
381 char *path;
382 char *progress = NULL;
384 *pack_hash = NULL;
386 /*
387 * Prevent fetching of references that won't make any
388 * sense outside of the remote repository's context.
389 */
390 TAILQ_FOREACH(pe, wanted_refs, entry) {
391 const char *refname = pe->path;
392 if (strncmp(refname, "refs/got/", 9) == 0 ||
393 strncmp(refname, "got/", 4) == 0 ||
394 strncmp(refname, "refs/remotes/", 13) == 0 ||
395 strncmp(refname, "remotes/", 8) == 0)
396 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
399 if (!list_refs_only)
400 repo_path = got_repo_get_path_git_dir(repo);
402 for (i = 0; i < nitems(tmpfds); i++)
403 tmpfds[i] = -1;
405 TAILQ_INIT(&have_refs);
406 SIMPLEQ_INIT(&my_refs);
408 if (!mirror_references) {
409 if (asprintf(&ref_prefix, "refs/remotes/%s/",
410 remote_name) == -1)
411 return got_error_from_errno("asprintf");
412 ref_prefixlen = strlen(ref_prefix);
415 if (!list_refs_only) {
416 err = got_ref_list(&my_refs, repo, NULL,
417 got_ref_cmp_by_name, NULL);
418 if (err)
419 goto done;
422 SIMPLEQ_FOREACH(re, &my_refs, entry) {
423 struct got_object_id *id;
424 const char *refname;
426 if (got_ref_is_symbolic(re->ref))
427 continue;
429 refname = got_ref_get_name(re->ref);
431 if (mirror_references) {
432 char *name;
433 err = got_ref_resolve(&id, repo, re->ref);
434 if (err)
435 goto done;
436 name = strdup(refname);
437 if (name == NULL) {
438 err = got_error_from_errno("strdup");
439 goto done;
441 err = got_pathlist_append(&have_refs, name, id);
442 if (err)
443 goto done;
444 continue;
447 if (strncmp("refs/tags/", refname, 10) == 0) {
448 char *tagname;
450 err = got_ref_resolve(&id, repo, re->ref);
451 if (err)
452 goto done;
453 tagname = strdup(refname);
454 if (tagname == NULL) {
455 err = got_error_from_errno("strdup");
456 goto done;
458 err = got_pathlist_append(&have_refs, tagname, id);
459 if (err) {
460 free(tagname);
461 goto done;
465 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
466 char *branchname;
468 err = got_ref_resolve(&id, repo, re->ref);
469 if (err)
470 goto done;
472 if (asprintf(&branchname, "refs/heads/%s",
473 refname + ref_prefixlen) == -1) {
474 err = got_error_from_errno("asprintf");
475 goto done;
477 err = got_pathlist_append(&have_refs, branchname, id);
478 if (err) {
479 free(branchname);
480 goto done;
485 if (list_refs_only) {
486 packfd = got_opentempfd();
487 if (packfd == -1) {
488 err = got_error_from_errno("got_opentempfd");
489 goto done;
491 } else {
492 if (asprintf(&path, "%s/%s/fetching.pack",
493 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
494 err = got_error_from_errno("asprintf");
495 goto done;
497 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
498 free(path);
499 if (err)
500 goto done;
501 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
502 err = got_error_from_errno2("fchmod", tmppackpath);
503 goto done;
506 if (list_refs_only) {
507 idxfd = got_opentempfd();
508 if (idxfd == -1) {
509 err = got_error_from_errno("got_opentempfd");
510 goto done;
512 } else {
513 if (asprintf(&path, "%s/%s/fetching.idx",
514 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
515 err = got_error_from_errno("asprintf");
516 goto done;
518 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
519 free(path);
520 if (err)
521 goto done;
522 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
523 err = got_error_from_errno2("fchmod", tmpidxpath);
524 goto done;
527 nidxfd = dup(idxfd);
528 if (nidxfd == -1) {
529 err = got_error_from_errno("dup");
530 goto done;
533 for (i = 0; i < nitems(tmpfds); i++) {
534 tmpfds[i] = got_opentempfd();
535 if (tmpfds[i] == -1) {
536 err = got_error_from_errno("got_opentempfd");
537 goto done;
541 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
542 err = got_error_from_errno("socketpair");
543 goto done;
546 fetchpid = fork();
547 if (fetchpid == -1) {
548 err = got_error_from_errno("fork");
549 goto done;
550 } else if (fetchpid == 0){
551 got_privsep_exec_child(imsg_fetchfds,
552 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
555 if (close(imsg_fetchfds[1]) != 0) {
556 err = got_error_from_errno("close");
557 goto done;
559 imsg_init(&fetchibuf, imsg_fetchfds[0]);
560 nfetchfd = dup(fetchfd);
561 if (nfetchfd == -1) {
562 err = got_error_from_errno("dup");
563 goto done;
565 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
566 fetch_all_branches, wanted_branches, wanted_refs,
567 list_refs_only, verbosity);
568 if (err != NULL)
569 goto done;
570 nfetchfd = -1;
571 npackfd = dup(packfd);
572 if (npackfd == -1) {
573 err = got_error_from_errno("dup");
574 goto done;
576 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
577 if (err != NULL)
578 goto done;
579 npackfd = -1;
581 packfile_size = 0;
582 progress = calloc(GOT_FETCH_PKTMAX, 1);
583 if (progress == NULL) {
584 err = got_error_from_errno("calloc");
585 goto done;
588 *pack_hash = calloc(1, sizeof(**pack_hash));
589 if (*pack_hash == NULL) {
590 err = got_error_from_errno("calloc");
591 goto done;
594 while (!done) {
595 struct got_object_id *id = NULL;
596 char *refname = NULL;
597 char *server_progress = NULL;
598 off_t packfile_size_cur = 0;
600 err = got_privsep_recv_fetch_progress(&done,
601 &id, &refname, symrefs, &server_progress,
602 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
603 if (err != NULL)
604 goto done;
605 if (!done && refname && id) {
606 err = got_pathlist_insert(NULL, refs, refname, id);
607 if (err)
608 goto done;
609 } else if (!done && server_progress) {
610 char *p;
611 /*
612 * XXX git-daemon tends to send batched output with
613 * lines spanning separate packets. Buffer progress
614 * output until we see a CR or LF to avoid giving
615 * partial lines of progress output to the callback.
616 */
617 if (strlcat(progress, server_progress,
618 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
619 progress[0] = '\0'; /* discard */
620 continue;
622 while ((p = strchr(progress, '\r')) != NULL ||
623 (p = strchr(progress, '\n')) != NULL) {
624 char *s;
625 size_t n;
626 char c = *p;
627 *p = '\0';
628 if (asprintf(&s, "%s%s", progress,
629 c == '\n' ? "\n" : "") == -1) {
630 err = got_error_from_errno("asprintf");
631 goto done;
633 err = progress_cb(progress_arg, s,
634 packfile_size_cur, 0, 0, 0, 0);
635 free(s);
636 if (err)
637 break;
638 n = strlen(progress);
639 if (n < GOT_FETCH_PKTMAX - 1) {
640 memmove(progress, &progress[n + 1],
641 GOT_FETCH_PKTMAX - n - 1);
642 } else
643 progress[0] = '\0';
645 free(server_progress);
646 if (err)
647 goto done;
648 } else if (!done && packfile_size_cur != packfile_size) {
649 err = progress_cb(progress_arg, NULL,
650 packfile_size_cur, 0, 0, 0, 0);
651 if (err)
652 break;
653 packfile_size = packfile_size_cur;
656 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
657 err = got_error_from_errno("waitpid");
658 goto done;
661 if (lseek(packfd, 0, SEEK_SET) == -1) {
662 err = got_error_from_errno("lseek");
663 goto done;
666 /* If zero data was fetched without error we are already up-to-date. */
667 if (packfile_size == 0) {
668 free(*pack_hash);
669 *pack_hash = NULL;
670 goto done;
671 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
672 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
673 goto done;
674 } else {
675 ssize_t n;
677 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
678 if (n == -1) {
679 err = got_error_from_errno("read");
680 goto done;
682 if (n != sizeof(pack_hdr)) {
683 err = got_error(GOT_ERR_IO);
684 goto done;
686 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
687 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
688 "bad pack file signature");
689 goto done;
691 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
692 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
693 "bad pack file version");
694 goto done;
696 nobj = be32toh(pack_hdr.nobjects);
697 if (nobj == 0 &&
698 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
699 return got_error_msg(GOT_ERR_BAD_PACKFILE,
700 "bad pack file with zero objects");
701 if (nobj != 0 &&
702 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
703 return got_error_msg(GOT_ERR_BAD_PACKFILE,
704 "empty pack file with non-zero object count");
707 /*
708 * If the pack file contains no objects, we may only need to update
709 * references in our repository. The caller will take care of that.
710 */
711 if (nobj == 0)
712 goto done;
714 if (lseek(packfd, 0, SEEK_SET) == -1) {
715 err = got_error_from_errno("lseek");
716 goto done;
719 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
720 err = got_error_from_errno("socketpair");
721 goto done;
723 idxpid = fork();
724 if (idxpid == -1) {
725 err= got_error_from_errno("fork");
726 goto done;
727 } else if (idxpid == 0)
728 got_privsep_exec_child(imsg_idxfds,
729 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
730 if (close(imsg_idxfds[1]) != 0) {
731 err = got_error_from_errno("close");
732 goto done;
734 imsg_init(&idxibuf, imsg_idxfds[0]);
736 npackfd = dup(packfd);
737 if (npackfd == -1) {
738 err = got_error_from_errno("dup");
739 goto done;
741 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
742 npackfd);
743 if (err != NULL)
744 goto done;
745 npackfd = -1;
746 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
747 if (err != NULL)
748 goto done;
749 nidxfd = -1;
750 for (i = 0; i < nitems(tmpfds); i++) {
751 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
752 if (err != NULL)
753 goto done;
754 tmpfds[i] = -1;
756 done = 0;
757 while (!done) {
758 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
760 err = got_privsep_recv_index_progress(&done, &nobj_total,
761 &nobj_indexed, &nobj_loose, &nobj_resolved,
762 &idxibuf);
763 if (err != NULL)
764 goto done;
765 if (nobj_indexed != 0) {
766 err = progress_cb(progress_arg, NULL,
767 packfile_size, nobj_total,
768 nobj_indexed, nobj_loose, nobj_resolved);
769 if (err)
770 break;
772 imsg_clear(&idxibuf);
774 if (close(imsg_idxfds[0]) == -1) {
775 err = got_error_from_errno("close");
776 goto done;
778 if (waitpid(idxpid, &idxstatus, 0) == -1) {
779 err = got_error_from_errno("waitpid");
780 goto done;
783 err = got_object_id_str(&id_str, *pack_hash);
784 if (err)
785 goto done;
786 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
787 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
788 err = got_error_from_errno("asprintf");
789 goto done;
792 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
793 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
794 err = got_error_from_errno("asprintf");
795 goto done;
798 if (rename(tmppackpath, packpath) == -1) {
799 err = got_error_from_errno3("rename", tmppackpath, packpath);
800 goto done;
802 free(tmppackpath);
803 tmppackpath = NULL;
804 if (rename(tmpidxpath, idxpath) == -1) {
805 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
806 goto done;
808 free(tmpidxpath);
809 tmpidxpath = NULL;
811 done:
812 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
813 err = got_error_from_errno2("unlink", tmppackpath);
814 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
815 err = got_error_from_errno2("unlink", tmpidxpath);
816 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
817 err = got_error_from_errno("close");
818 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
819 err = got_error_from_errno("close");
820 if (packfd != -1 && close(packfd) == -1 && err == NULL)
821 err = got_error_from_errno("close");
822 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
823 err = got_error_from_errno("close");
824 for (i = 0; i < nitems(tmpfds); i++) {
825 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
826 err = got_error_from_errno("close");
828 free(tmppackpath);
829 free(tmpidxpath);
830 free(idxpath);
831 free(packpath);
832 free(ref_prefix);
833 free(progress);
835 TAILQ_FOREACH(pe, &have_refs, entry) {
836 free((char *)pe->path);
837 free(pe->data);
839 got_pathlist_free(&have_refs);
840 got_ref_list_free(&my_refs);
841 if (err) {
842 free(*pack_hash);
843 *pack_hash = NULL;
844 TAILQ_FOREACH(pe, refs, entry) {
845 free((void *)pe->path);
846 free(pe->data);
848 got_pathlist_free(refs);
849 TAILQ_FOREACH(pe, symrefs, entry) {
850 free((void *)pe->path);
851 free(pe->data);
853 got_pathlist_free(symrefs);
855 return err;