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 ssizeof
71 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
72 #endif
74 #ifndef MIN
75 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
76 #endif
78 static int
79 hassuffix(char *base, char *suf)
80 {
81 int nb, ns;
83 nb = strlen(base);
84 ns = strlen(suf);
85 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
86 return 1;
87 return 0;
88 }
90 static const struct got_error *
91 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
92 const char *path, const char *direction, int verbosity)
93 {
94 const struct got_error *error = NULL;
95 int pid, pfd[2];
96 char cmd[64];
97 char *argv[11];
98 int i = 0, j;
100 *fetchpid = -1;
101 *fetchfd = -1;
103 argv[i++] = GOT_FETCH_PATH_SSH;
104 if (port != NULL) {
105 argv[i++] = "-p";
106 argv[i++] = (char *)port;
108 if (verbosity == -1) {
109 argv[i++] = "-q";
110 } else {
111 /* ssh(1) allows up to 3 "-v" options. */
112 for (j = 0; j < MIN(3, verbosity); j++)
113 argv[i++] = "-v";
115 argv[i++] = "--";
116 argv[i++] = (char *)host;
117 argv[i++] = (char *)cmd;
118 argv[i++] = (char *)path;
119 argv[i++] = NULL;
121 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
122 return got_error_from_errno("socketpair");
124 pid = fork();
125 if (pid == -1) {
126 error = got_error_from_errno("fork");
127 close(pfd[0]);
128 close(pfd[1]);
129 return error;
130 } else if (pid == 0) {
131 int n;
132 if (close(pfd[1]) == -1)
133 err(1, "close");
134 if (dup2(pfd[0], 0) == -1)
135 err(1, "dup2");
136 if (dup2(pfd[0], 1) == -1)
137 err(1, "dup2");
138 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
139 if (n < 0 || n >= ssizeof(cmd))
140 err(1, "snprintf");
141 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
142 err(1, "execv");
143 abort(); /* not reached */
144 } else {
145 if (close(pfd[0]) == -1)
146 return got_error_from_errno("close");
147 *fetchpid = pid;
148 *fetchfd = pfd[1];
149 return NULL;
153 static const struct got_error *
154 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
155 const char *direction)
157 const struct got_error *err = NULL;
158 struct addrinfo hints, *servinfo, *p;
159 char *cmd = NULL;
160 int fd = -1, len, r, eaicode;
162 *fetchfd = -1;
164 if (port == NULL)
165 port = GOT_DEFAULT_GIT_PORT_STR;
167 memset(&hints, 0, sizeof hints);
168 hints.ai_family = AF_UNSPEC;
169 hints.ai_socktype = SOCK_STREAM;
170 eaicode = getaddrinfo(host, port, &hints, &servinfo);
171 if (eaicode) {
172 char msg[512];
173 snprintf(msg, sizeof(msg), "%s: %s", host,
174 gai_strerror(eaicode));
175 return got_error_msg(GOT_ERR_ADDRINFO, msg);
178 for (p = servinfo; p != NULL; p = p->ai_next) {
179 if ((fd = socket(p->ai_family, p->ai_socktype,
180 p->ai_protocol)) == -1)
181 continue;
182 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
183 err = NULL;
184 break;
186 err = got_error_from_errno("connect");
187 close(fd);
189 if (p == NULL)
190 goto done;
192 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
197 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
198 if (r < 0)
199 err = got_error_from_errno("dprintf");
200 done:
201 free(cmd);
202 if (err) {
203 if (fd != -1)
204 close(fd);
205 } else
206 *fetchfd = fd;
207 return err;
210 const struct got_error *
211 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
212 const char *host, const char *port, const char *server_path, int verbosity)
214 const struct got_error *err = NULL;
216 *fetchpid = -1;
217 *fetchfd = -1;
219 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
220 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
221 "upload", verbosity);
222 else if (strcmp(proto, "git") == 0)
223 err = dial_git(fetchfd, host, port, server_path, "upload");
224 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
225 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
226 else
227 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
228 return err;
231 const struct got_error *
232 got_fetch_parse_uri(char **proto, char **host, char **port,
233 char **server_path, char **repo_name, const char *uri)
235 const struct got_error *err = NULL;
236 char *s, *p, *q;
237 int n;
239 *proto = *host = *port = *server_path = *repo_name = NULL;
241 p = strstr(uri, "://");
242 if (!p) {
243 /* Try parsing Git's "scp" style URL syntax. */
244 *proto = strdup("ssh");
245 if (proto == NULL) {
246 err = got_error_from_errno("strdup");
247 goto done;
249 s = (char *)uri;
250 q = strchr(s, ':');
251 if (q == NULL) {
252 err = got_error(GOT_ERR_PARSE_URI);
253 goto done;
255 /* No slashes allowed before first colon. */
256 p = strchr(s, '/');
257 if (p && q > p) {
258 err = got_error(GOT_ERR_PARSE_URI);
259 goto done;
261 *host = strndup(s, q - s);
262 if (*host == NULL) {
263 err = got_error_from_errno("strndup");
264 goto done;
266 p = q + 1;
267 } else {
268 *proto = strndup(uri, p - uri);
269 if (proto == NULL) {
270 err = got_error_from_errno("strndup");
271 goto done;
273 s = p + 3;
275 p = strstr(s, "/");
276 if (p == NULL || strlen(p) == 1) {
277 err = got_error(GOT_ERR_PARSE_URI);
278 goto done;
281 q = memchr(s, ':', p - s);
282 if (q) {
283 *host = strndup(s, q - s);
284 if (*host == NULL) {
285 err = got_error_from_errno("strndup");
286 goto done;
288 *port = strndup(q + 1, p - (q + 1));
289 if (*port == NULL) {
290 err = got_error_from_errno("strndup");
291 goto done;
293 } else {
294 *host = strndup(s, p - s);
295 if (*host == NULL) {
296 err = got_error_from_errno("strndup");
297 goto done;
302 while (p[0] == '/' && p[1] == '/')
303 p++;
304 *server_path = strdup(p);
305 if (*server_path == NULL) {
306 err = got_error_from_errno("strdup");
307 goto done;
309 got_path_strip_trailing_slashes(*server_path);
311 p = strrchr(p, '/');
312 if (!p || strlen(p) <= 1) {
313 err = got_error(GOT_ERR_PARSE_URI);
314 goto done;
316 p++;
317 n = strlen(p);
318 if (n == 0) {
319 err = got_error(GOT_ERR_PARSE_URI);
320 goto done;
322 if (hassuffix(p, ".git"))
323 n -= 4;
324 *repo_name = strndup(p, (p + n) - p);
325 if (*repo_name == NULL) {
326 err = got_error_from_errno("strndup");
327 goto done;
329 done:
330 if (err) {
331 free(*proto);
332 *proto = NULL;
333 free(*host);
334 *host = NULL;
335 free(*port);
336 *port = NULL;
337 free(*server_path);
338 *server_path = NULL;
339 free(*repo_name);
340 *repo_name = NULL;
342 return err;
345 const struct got_error*
346 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
347 struct got_pathlist_head *symrefs, const char *remote_name,
348 int mirror_references, int fetch_all_branches,
349 struct got_pathlist_head *wanted_branches,
350 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
351 int fetchfd, struct got_repository *repo,
352 got_fetch_progress_cb progress_cb, void *progress_arg)
354 size_t i;
355 int imsg_fetchfds[2], imsg_idxfds[2];
356 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
357 int tmpfds[3];
358 int fetchstatus, idxstatus, done = 0;
359 const struct got_error *err;
360 struct imsgbuf fetchibuf, idxibuf;
361 pid_t fetchpid, idxpid;
362 char *tmppackpath = NULL, *tmpidxpath = NULL;
363 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
364 const char *repo_path = NULL;
365 struct got_pathlist_head have_refs;
366 struct got_pathlist_entry *pe;
367 struct got_reflist_head my_refs;
368 struct got_reflist_entry *re;
369 off_t packfile_size = 0;
370 struct got_packfile_hdr pack_hdr;
371 uint32_t nobj = 0;
372 char *ref_prefix = NULL;
373 size_t ref_prefixlen = 0;
374 char *path;
375 char *progress = NULL;
377 *pack_hash = NULL;
379 /*
380 * Prevent fetching of references that won't make any
381 * sense outside of the remote repository's context.
382 */
383 TAILQ_FOREACH(pe, wanted_refs, entry) {
384 const char *refname = pe->path;
385 if (strncmp(refname, "refs/got/", 9) == 0 ||
386 strncmp(refname, "got/", 4) == 0 ||
387 strncmp(refname, "refs/remotes/", 13) == 0 ||
388 strncmp(refname, "remotes/", 8) == 0)
389 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
392 if (!list_refs_only)
393 repo_path = got_repo_get_path_git_dir(repo);
395 for (i = 0; i < nitems(tmpfds); i++)
396 tmpfds[i] = -1;
398 TAILQ_INIT(&have_refs);
399 TAILQ_INIT(&my_refs);
401 if (!mirror_references) {
402 if (asprintf(&ref_prefix, "refs/remotes/%s/",
403 remote_name) == -1)
404 return got_error_from_errno("asprintf");
405 ref_prefixlen = strlen(ref_prefix);
408 if (!list_refs_only) {
409 err = got_ref_list(&my_refs, repo, NULL,
410 got_ref_cmp_by_name, NULL);
411 if (err)
412 goto done;
415 TAILQ_FOREACH(re, &my_refs, entry) {
416 struct got_object_id *id;
417 const char *refname;
419 if (got_ref_is_symbolic(re->ref))
420 continue;
422 refname = got_ref_get_name(re->ref);
424 if (mirror_references) {
425 char *name;
426 err = got_ref_resolve(&id, repo, re->ref);
427 if (err)
428 goto done;
429 name = strdup(refname);
430 if (name == NULL) {
431 err = got_error_from_errno("strdup");
432 goto done;
434 err = got_pathlist_append(&have_refs, name, id);
435 if (err)
436 goto done;
437 continue;
440 if (strncmp("refs/tags/", refname, 10) == 0) {
441 char *tagname;
443 err = got_ref_resolve(&id, repo, re->ref);
444 if (err)
445 goto done;
446 tagname = strdup(refname);
447 if (tagname == NULL) {
448 err = got_error_from_errno("strdup");
449 goto done;
451 err = got_pathlist_append(&have_refs, tagname, id);
452 if (err) {
453 free(tagname);
454 goto done;
458 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
459 char *branchname;
461 err = got_ref_resolve(&id, repo, re->ref);
462 if (err)
463 goto done;
465 if (asprintf(&branchname, "refs/heads/%s",
466 refname + ref_prefixlen) == -1) {
467 err = got_error_from_errno("asprintf");
468 goto done;
470 err = got_pathlist_append(&have_refs, branchname, id);
471 if (err) {
472 free(branchname);
473 goto done;
478 if (list_refs_only) {
479 packfd = got_opentempfd();
480 if (packfd == -1) {
481 err = got_error_from_errno("got_opentempfd");
482 goto done;
484 } else {
485 if (asprintf(&path, "%s/%s/fetching.pack",
486 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
487 err = got_error_from_errno("asprintf");
488 goto done;
490 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
491 free(path);
492 if (err)
493 goto done;
494 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
495 err = got_error_from_errno2("fchmod", tmppackpath);
496 goto done;
499 if (list_refs_only) {
500 idxfd = got_opentempfd();
501 if (idxfd == -1) {
502 err = got_error_from_errno("got_opentempfd");
503 goto done;
505 } else {
506 if (asprintf(&path, "%s/%s/fetching.idx",
507 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
508 err = got_error_from_errno("asprintf");
509 goto done;
511 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
512 free(path);
513 if (err)
514 goto done;
515 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
516 err = got_error_from_errno2("fchmod", tmpidxpath);
517 goto done;
520 nidxfd = dup(idxfd);
521 if (nidxfd == -1) {
522 err = got_error_from_errno("dup");
523 goto done;
526 for (i = 0; i < nitems(tmpfds); i++) {
527 tmpfds[i] = got_opentempfd();
528 if (tmpfds[i] == -1) {
529 err = got_error_from_errno("got_opentempfd");
530 goto done;
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
535 err = got_error_from_errno("socketpair");
536 goto done;
539 fetchpid = fork();
540 if (fetchpid == -1) {
541 err = got_error_from_errno("fork");
542 goto done;
543 } else if (fetchpid == 0){
544 got_privsep_exec_child(imsg_fetchfds,
545 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
548 if (close(imsg_fetchfds[1]) == -1) {
549 err = got_error_from_errno("close");
550 goto done;
552 imsg_init(&fetchibuf, imsg_fetchfds[0]);
553 nfetchfd = dup(fetchfd);
554 if (nfetchfd == -1) {
555 err = got_error_from_errno("dup");
556 goto done;
558 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
559 fetch_all_branches, wanted_branches, wanted_refs,
560 list_refs_only, verbosity);
561 if (err != NULL)
562 goto done;
563 nfetchfd = -1;
564 npackfd = dup(packfd);
565 if (npackfd == -1) {
566 err = got_error_from_errno("dup");
567 goto done;
569 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
570 if (err != NULL)
571 goto done;
572 npackfd = -1;
574 packfile_size = 0;
575 progress = calloc(GOT_FETCH_PKTMAX, 1);
576 if (progress == NULL) {
577 err = got_error_from_errno("calloc");
578 goto done;
581 *pack_hash = calloc(1, sizeof(**pack_hash));
582 if (*pack_hash == NULL) {
583 err = got_error_from_errno("calloc");
584 goto done;
587 while (!done) {
588 struct got_object_id *id = NULL;
589 char *refname = NULL;
590 char *server_progress = NULL;
591 off_t packfile_size_cur = 0;
593 err = got_privsep_recv_fetch_progress(&done,
594 &id, &refname, symrefs, &server_progress,
595 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
596 if (err != NULL)
597 goto done;
598 if (!done && refname && id) {
599 err = got_pathlist_insert(NULL, refs, refname, id);
600 if (err)
601 goto done;
602 } else if (!done && server_progress) {
603 char *p;
604 /*
605 * XXX git-daemon tends to send batched output with
606 * lines spanning separate packets. Buffer progress
607 * output until we see a CR or LF to avoid giving
608 * partial lines of progress output to the callback.
609 */
610 if (strlcat(progress, server_progress,
611 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
612 progress[0] = '\0'; /* discard */
613 continue;
615 while ((p = strchr(progress, '\r')) != NULL ||
616 (p = strchr(progress, '\n')) != NULL) {
617 char *s;
618 size_t n;
619 char c = *p;
620 *p = '\0';
621 if (asprintf(&s, "%s%s", progress,
622 c == '\n' ? "\n" : "") == -1) {
623 err = got_error_from_errno("asprintf");
624 goto done;
626 err = progress_cb(progress_arg, s,
627 packfile_size_cur, 0, 0, 0, 0);
628 free(s);
629 if (err)
630 break;
631 n = strlen(progress);
632 if (n < GOT_FETCH_PKTMAX - 1) {
633 memmove(progress, &progress[n + 1],
634 GOT_FETCH_PKTMAX - n - 1);
635 } else
636 progress[0] = '\0';
638 free(server_progress);
639 if (err)
640 goto done;
641 } else if (!done && packfile_size_cur != packfile_size) {
642 err = progress_cb(progress_arg, NULL,
643 packfile_size_cur, 0, 0, 0, 0);
644 if (err)
645 break;
646 packfile_size = packfile_size_cur;
649 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
650 err = got_error_from_errno("waitpid");
651 goto done;
654 if (lseek(packfd, 0, SEEK_SET) == -1) {
655 err = got_error_from_errno("lseek");
656 goto done;
659 /* If zero data was fetched without error we are already up-to-date. */
660 if (packfile_size == 0) {
661 free(*pack_hash);
662 *pack_hash = NULL;
663 goto done;
664 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
665 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
666 goto done;
667 } else {
668 ssize_t n;
670 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
671 if (n == -1) {
672 err = got_error_from_errno("read");
673 goto done;
675 if (n != ssizeof(pack_hdr)) {
676 err = got_error(GOT_ERR_IO);
677 goto done;
679 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
680 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
681 "bad pack file signature");
682 goto done;
684 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
685 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
686 "bad pack file version");
687 goto done;
689 nobj = be32toh(pack_hdr.nobjects);
690 if (nobj == 0 &&
691 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
692 return got_error_msg(GOT_ERR_BAD_PACKFILE,
693 "bad pack file with zero objects");
694 if (nobj != 0 &&
695 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
696 return got_error_msg(GOT_ERR_BAD_PACKFILE,
697 "empty pack file with non-zero object count");
700 /*
701 * If the pack file contains no objects, we may only need to update
702 * references in our repository. The caller will take care of that.
703 */
704 if (nobj == 0)
705 goto done;
707 if (lseek(packfd, 0, SEEK_SET) == -1) {
708 err = got_error_from_errno("lseek");
709 goto done;
712 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
713 err = got_error_from_errno("socketpair");
714 goto done;
716 idxpid = fork();
717 if (idxpid == -1) {
718 err= got_error_from_errno("fork");
719 goto done;
720 } else if (idxpid == 0)
721 got_privsep_exec_child(imsg_idxfds,
722 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
723 if (close(imsg_idxfds[1]) == -1) {
724 err = got_error_from_errno("close");
725 goto done;
727 imsg_init(&idxibuf, imsg_idxfds[0]);
729 npackfd = dup(packfd);
730 if (npackfd == -1) {
731 err = got_error_from_errno("dup");
732 goto done;
734 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
735 npackfd);
736 if (err != NULL)
737 goto done;
738 npackfd = -1;
739 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
740 if (err != NULL)
741 goto done;
742 nidxfd = -1;
743 for (i = 0; i < nitems(tmpfds); i++) {
744 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
745 if (err != NULL)
746 goto done;
747 tmpfds[i] = -1;
749 done = 0;
750 while (!done) {
751 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
753 err = got_privsep_recv_index_progress(&done, &nobj_total,
754 &nobj_indexed, &nobj_loose, &nobj_resolved,
755 &idxibuf);
756 if (err != NULL)
757 goto done;
758 if (nobj_indexed != 0) {
759 err = progress_cb(progress_arg, NULL,
760 packfile_size, nobj_total,
761 nobj_indexed, nobj_loose, nobj_resolved);
762 if (err)
763 break;
765 imsg_clear(&idxibuf);
767 if (close(imsg_idxfds[0]) == -1) {
768 err = got_error_from_errno("close");
769 goto done;
771 if (waitpid(idxpid, &idxstatus, 0) == -1) {
772 err = got_error_from_errno("waitpid");
773 goto done;
776 err = got_object_id_str(&id_str, *pack_hash);
777 if (err)
778 goto done;
779 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
780 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
781 err = got_error_from_errno("asprintf");
782 goto done;
785 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
786 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
787 err = got_error_from_errno("asprintf");
788 goto done;
791 if (rename(tmppackpath, packpath) == -1) {
792 err = got_error_from_errno3("rename", tmppackpath, packpath);
793 goto done;
795 free(tmppackpath);
796 tmppackpath = NULL;
797 if (rename(tmpidxpath, idxpath) == -1) {
798 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
799 goto done;
801 free(tmpidxpath);
802 tmpidxpath = NULL;
804 done:
805 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
806 err = got_error_from_errno2("unlink", tmppackpath);
807 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
808 err = got_error_from_errno2("unlink", tmpidxpath);
809 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
810 err = got_error_from_errno("close");
811 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
812 err = got_error_from_errno("close");
813 if (packfd != -1 && close(packfd) == -1 && err == NULL)
814 err = got_error_from_errno("close");
815 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 for (i = 0; i < nitems(tmpfds); i++) {
818 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
819 err = got_error_from_errno("close");
821 free(tmppackpath);
822 free(tmpidxpath);
823 free(idxpath);
824 free(packpath);
825 free(ref_prefix);
826 free(progress);
828 TAILQ_FOREACH(pe, &have_refs, entry) {
829 free((char *)pe->path);
830 free(pe->data);
832 got_pathlist_free(&have_refs);
833 got_ref_list_free(&my_refs);
834 if (err) {
835 free(*pack_hash);
836 *pack_hash = NULL;
837 TAILQ_FOREACH(pe, refs, entry) {
838 free((void *)pe->path);
839 free(pe->data);
841 got_pathlist_free(refs);
842 TAILQ_FOREACH(pe, symrefs, entry) {
843 free((void *)pe->path);
844 free(pe->data);
846 got_pathlist_free(symrefs);
848 return err;