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 (pipe(pfd) == -1)
122 return got_error_from_errno("pipe");
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 close(pfd[1]);
133 dup2(pfd[0], 0);
134 dup2(pfd[0], 1);
135 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
136 if (n < 0 || n >= ssizeof(cmd))
137 err(1, "snprintf");
138 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
139 err(1, "execl");
140 abort(); /* not reached */
141 } else {
142 close(pfd[0]);
143 *fetchpid = pid;
144 *fetchfd = pfd[1];
145 return NULL;
149 static const struct got_error *
150 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
151 const char *direction)
153 const struct got_error *err = NULL;
154 struct addrinfo hints, *servinfo, *p;
155 char *cmd = NULL, *pkt = NULL;
156 int fd = -1, totlen, r, eaicode;
158 *fetchfd = -1;
160 if (port == NULL)
161 port = GOT_DEFAULT_GIT_PORT_STR;
163 memset(&hints, 0, sizeof hints);
164 hints.ai_family = AF_UNSPEC;
165 hints.ai_socktype = SOCK_STREAM;
166 eaicode = getaddrinfo(host, port, &hints, &servinfo);
167 if (eaicode) {
168 char msg[512];
169 snprintf(msg, sizeof(msg), "%s: %s", host,
170 gai_strerror(eaicode));
171 return got_error_msg(GOT_ERR_ADDRINFO, msg);
174 for (p = servinfo; p != NULL; p = p->ai_next) {
175 if ((fd = socket(p->ai_family, p->ai_socktype,
176 p->ai_protocol)) == -1)
177 continue;
178 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
179 err = NULL;
180 break;
182 err = got_error_from_errno("connect");
183 close(fd);
185 if (p == NULL)
186 goto done;
188 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
189 err = got_error_from_errno("asprintf");
190 goto done;
192 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
193 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
194 err = got_error_from_errno("asprintf");
195 goto done;
197 r = write(fd, pkt, strlen(pkt) + 1);
198 if (r == -1) {
199 err = got_error_from_errno("write");
200 goto done;
202 if (asprintf(&pkt, "host=%s", host) == -1) {
203 err = got_error_from_errno("asprintf");
204 goto done;
206 r = write(fd, pkt, strlen(pkt) + 1);
207 if (r == -1) {
208 err = got_error_from_errno("write");
209 goto done;
211 done:
212 free(cmd);
213 free(pkt);
214 if (err) {
215 if (fd != -1)
216 close(fd);
217 } else
218 *fetchfd = fd;
219 return err;
222 const struct got_error *
223 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
224 const char *host, const char *port, const char *server_path, int verbosity)
226 const struct got_error *err = NULL;
228 *fetchpid = -1;
229 *fetchfd = -1;
231 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
232 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
233 "upload", verbosity);
234 else if (strcmp(proto, "git") == 0)
235 err = dial_git(fetchfd, host, port, server_path, "upload");
236 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
237 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
238 else
239 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
240 return err;
243 const struct got_error *
244 got_fetch_parse_uri(char **proto, char **host, char **port,
245 char **server_path, char **repo_name, const char *uri)
247 const struct got_error *err = NULL;
248 char *s, *p, *q;
249 int n;
251 *proto = *host = *port = *server_path = *repo_name = NULL;
253 p = strstr(uri, "://");
254 if (!p) {
255 /* Try parsing Git's "scp" style URL syntax. */
256 *proto = strdup("ssh");
257 if (proto == NULL) {
258 err = got_error_from_errno("strdup");
259 goto done;
261 s = (char *)uri;
262 q = strchr(s, ':');
263 if (q == NULL) {
264 err = got_error(GOT_ERR_PARSE_URI);
265 goto done;
267 /* No slashes allowed before first colon. */
268 p = strchr(s, '/');
269 if (p && q > p) {
270 err = got_error(GOT_ERR_PARSE_URI);
271 goto done;
273 *host = strndup(s, q - s);
274 if (*host == NULL) {
275 err = got_error_from_errno("strndup");
276 goto done;
278 p = q + 1;
279 } else {
280 *proto = strndup(uri, p - uri);
281 if (proto == NULL) {
282 err = got_error_from_errno("strndup");
283 goto done;
285 s = p + 3;
287 p = strstr(s, "/");
288 if (p == NULL || strlen(p) == 1) {
289 err = got_error(GOT_ERR_PARSE_URI);
290 goto done;
293 q = memchr(s, ':', p - s);
294 if (q) {
295 *host = strndup(s, q - s);
296 if (*host == NULL) {
297 err = got_error_from_errno("strndup");
298 goto done;
300 *port = strndup(q + 1, p - (q + 1));
301 if (*port == NULL) {
302 err = got_error_from_errno("strndup");
303 goto done;
305 } else {
306 *host = strndup(s, p - s);
307 if (*host == NULL) {
308 err = got_error_from_errno("strndup");
309 goto done;
314 while (p[0] == '/' && p[1] == '/')
315 p++;
316 *server_path = strdup(p);
317 if (*server_path == NULL) {
318 err = got_error_from_errno("strdup");
319 goto done;
321 got_path_strip_trailing_slashes(*server_path);
323 p = strrchr(p, '/');
324 if (!p || strlen(p) <= 1) {
325 err = got_error(GOT_ERR_PARSE_URI);
326 goto done;
328 p++;
329 n = strlen(p);
330 if (n == 0) {
331 err = got_error(GOT_ERR_PARSE_URI);
332 goto done;
334 if (hassuffix(p, ".git"))
335 n -= 4;
336 *repo_name = strndup(p, (p + n) - p);
337 if (*repo_name == NULL) {
338 err = got_error_from_errno("strndup");
339 goto done;
341 done:
342 if (err) {
343 free(*proto);
344 *proto = NULL;
345 free(*host);
346 *host = NULL;
347 free(*port);
348 *port = NULL;
349 free(*server_path);
350 *server_path = NULL;
351 free(*repo_name);
352 *repo_name = NULL;
354 return err;
357 const struct got_error*
358 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
359 struct got_pathlist_head *symrefs, const char *remote_name,
360 int mirror_references, int fetch_all_branches,
361 struct got_pathlist_head *wanted_branches,
362 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
363 int fetchfd, struct got_repository *repo,
364 got_fetch_progress_cb progress_cb, void *progress_arg)
366 size_t i;
367 int imsg_fetchfds[2], imsg_idxfds[2];
368 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
369 int tmpfds[3];
370 int fetchstatus, idxstatus, done = 0;
371 const struct got_error *err;
372 struct imsgbuf fetchibuf, idxibuf;
373 pid_t fetchpid, idxpid;
374 char *tmppackpath = NULL, *tmpidxpath = NULL;
375 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
376 const char *repo_path = NULL;
377 struct got_pathlist_head have_refs;
378 struct got_pathlist_entry *pe;
379 struct got_reflist_head my_refs;
380 struct got_reflist_entry *re;
381 off_t packfile_size = 0;
382 struct got_packfile_hdr pack_hdr;
383 uint32_t nobj = 0;
384 char *ref_prefix = NULL;
385 size_t ref_prefixlen = 0;
386 char *path;
387 char *progress = NULL;
389 *pack_hash = NULL;
391 /*
392 * Prevent fetching of references that won't make any
393 * sense outside of the remote repository's context.
394 */
395 TAILQ_FOREACH(pe, wanted_refs, entry) {
396 const char *refname = pe->path;
397 if (strncmp(refname, "refs/got/", 9) == 0 ||
398 strncmp(refname, "got/", 4) == 0 ||
399 strncmp(refname, "refs/remotes/", 13) == 0 ||
400 strncmp(refname, "remotes/", 8) == 0)
401 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
404 if (!list_refs_only)
405 repo_path = got_repo_get_path_git_dir(repo);
407 for (i = 0; i < nitems(tmpfds); i++)
408 tmpfds[i] = -1;
410 TAILQ_INIT(&have_refs);
411 TAILQ_INIT(&my_refs);
413 if (!mirror_references) {
414 if (asprintf(&ref_prefix, "refs/remotes/%s/",
415 remote_name) == -1)
416 return got_error_from_errno("asprintf");
417 ref_prefixlen = strlen(ref_prefix);
420 if (!list_refs_only) {
421 err = got_ref_list(&my_refs, repo, NULL,
422 got_ref_cmp_by_name, NULL);
423 if (err)
424 goto done;
427 TAILQ_FOREACH(re, &my_refs, entry) {
428 struct got_object_id *id;
429 const char *refname;
431 if (got_ref_is_symbolic(re->ref))
432 continue;
434 refname = got_ref_get_name(re->ref);
436 if (mirror_references) {
437 char *name;
438 err = got_ref_resolve(&id, repo, re->ref);
439 if (err)
440 goto done;
441 name = strdup(refname);
442 if (name == NULL) {
443 err = got_error_from_errno("strdup");
444 goto done;
446 err = got_pathlist_append(&have_refs, name, id);
447 if (err)
448 goto done;
449 continue;
452 if (strncmp("refs/tags/", refname, 10) == 0) {
453 char *tagname;
455 err = got_ref_resolve(&id, repo, re->ref);
456 if (err)
457 goto done;
458 tagname = strdup(refname);
459 if (tagname == NULL) {
460 err = got_error_from_errno("strdup");
461 goto done;
463 err = got_pathlist_append(&have_refs, tagname, id);
464 if (err) {
465 free(tagname);
466 goto done;
470 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
471 char *branchname;
473 err = got_ref_resolve(&id, repo, re->ref);
474 if (err)
475 goto done;
477 if (asprintf(&branchname, "refs/heads/%s",
478 refname + ref_prefixlen) == -1) {
479 err = got_error_from_errno("asprintf");
480 goto done;
482 err = got_pathlist_append(&have_refs, branchname, id);
483 if (err) {
484 free(branchname);
485 goto done;
490 if (list_refs_only) {
491 packfd = got_opentempfd();
492 if (packfd == -1) {
493 err = got_error_from_errno("got_opentempfd");
494 goto done;
496 } else {
497 if (asprintf(&path, "%s/%s/fetching.pack",
498 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
499 err = got_error_from_errno("asprintf");
500 goto done;
502 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
503 free(path);
504 if (err)
505 goto done;
506 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
507 err = got_error_from_errno2("fchmod", tmppackpath);
508 goto done;
511 if (list_refs_only) {
512 idxfd = got_opentempfd();
513 if (idxfd == -1) {
514 err = got_error_from_errno("got_opentempfd");
515 goto done;
517 } else {
518 if (asprintf(&path, "%s/%s/fetching.idx",
519 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
521 goto done;
523 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
524 free(path);
525 if (err)
526 goto done;
527 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
528 err = got_error_from_errno2("fchmod", tmpidxpath);
529 goto done;
532 nidxfd = dup(idxfd);
533 if (nidxfd == -1) {
534 err = got_error_from_errno("dup");
535 goto done;
538 for (i = 0; i < nitems(tmpfds); i++) {
539 tmpfds[i] = got_opentempfd();
540 if (tmpfds[i] == -1) {
541 err = got_error_from_errno("got_opentempfd");
542 goto done;
546 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
547 err = got_error_from_errno("socketpair");
548 goto done;
551 fetchpid = fork();
552 if (fetchpid == -1) {
553 err = got_error_from_errno("fork");
554 goto done;
555 } else if (fetchpid == 0){
556 got_privsep_exec_child(imsg_fetchfds,
557 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
560 if (close(imsg_fetchfds[1]) == -1) {
561 err = got_error_from_errno("close");
562 goto done;
564 imsg_init(&fetchibuf, imsg_fetchfds[0]);
565 nfetchfd = dup(fetchfd);
566 if (nfetchfd == -1) {
567 err = got_error_from_errno("dup");
568 goto done;
570 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
571 fetch_all_branches, wanted_branches, wanted_refs,
572 list_refs_only, verbosity);
573 if (err != NULL)
574 goto done;
575 nfetchfd = -1;
576 npackfd = dup(packfd);
577 if (npackfd == -1) {
578 err = got_error_from_errno("dup");
579 goto done;
581 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
582 if (err != NULL)
583 goto done;
584 npackfd = -1;
586 packfile_size = 0;
587 progress = calloc(GOT_FETCH_PKTMAX, 1);
588 if (progress == NULL) {
589 err = got_error_from_errno("calloc");
590 goto done;
593 *pack_hash = calloc(1, sizeof(**pack_hash));
594 if (*pack_hash == NULL) {
595 err = got_error_from_errno("calloc");
596 goto done;
599 while (!done) {
600 struct got_object_id *id = NULL;
601 char *refname = NULL;
602 char *server_progress = NULL;
603 off_t packfile_size_cur = 0;
605 err = got_privsep_recv_fetch_progress(&done,
606 &id, &refname, symrefs, &server_progress,
607 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
608 if (err != NULL)
609 goto done;
610 if (!done && refname && id) {
611 err = got_pathlist_insert(NULL, refs, refname, id);
612 if (err)
613 goto done;
614 } else if (!done && server_progress) {
615 char *p;
616 /*
617 * XXX git-daemon tends to send batched output with
618 * lines spanning separate packets. Buffer progress
619 * output until we see a CR or LF to avoid giving
620 * partial lines of progress output to the callback.
621 */
622 if (strlcat(progress, server_progress,
623 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
624 progress[0] = '\0'; /* discard */
625 continue;
627 while ((p = strchr(progress, '\r')) != NULL ||
628 (p = strchr(progress, '\n')) != NULL) {
629 char *s;
630 size_t n;
631 char c = *p;
632 *p = '\0';
633 if (asprintf(&s, "%s%s", progress,
634 c == '\n' ? "\n" : "") == -1) {
635 err = got_error_from_errno("asprintf");
636 goto done;
638 err = progress_cb(progress_arg, s,
639 packfile_size_cur, 0, 0, 0, 0);
640 free(s);
641 if (err)
642 break;
643 n = strlen(progress);
644 if (n < GOT_FETCH_PKTMAX - 1) {
645 memmove(progress, &progress[n + 1],
646 GOT_FETCH_PKTMAX - n - 1);
647 } else
648 progress[0] = '\0';
650 free(server_progress);
651 if (err)
652 goto done;
653 } else if (!done && packfile_size_cur != packfile_size) {
654 err = progress_cb(progress_arg, NULL,
655 packfile_size_cur, 0, 0, 0, 0);
656 if (err)
657 break;
658 packfile_size = packfile_size_cur;
661 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
662 err = got_error_from_errno("waitpid");
663 goto done;
666 if (lseek(packfd, 0, SEEK_SET) == -1) {
667 err = got_error_from_errno("lseek");
668 goto done;
671 /* If zero data was fetched without error we are already up-to-date. */
672 if (packfile_size == 0) {
673 free(*pack_hash);
674 *pack_hash = NULL;
675 goto done;
676 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
677 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
678 goto done;
679 } else {
680 ssize_t n;
682 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
683 if (n == -1) {
684 err = got_error_from_errno("read");
685 goto done;
687 if (n != ssizeof(pack_hdr)) {
688 err = got_error(GOT_ERR_IO);
689 goto done;
691 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
692 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
693 "bad pack file signature");
694 goto done;
696 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
697 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
698 "bad pack file version");
699 goto done;
701 nobj = be32toh(pack_hdr.nobjects);
702 if (nobj == 0 &&
703 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
704 return got_error_msg(GOT_ERR_BAD_PACKFILE,
705 "bad pack file with zero objects");
706 if (nobj != 0 &&
707 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
708 return got_error_msg(GOT_ERR_BAD_PACKFILE,
709 "empty pack file with non-zero object count");
712 /*
713 * If the pack file contains no objects, we may only need to update
714 * references in our repository. The caller will take care of that.
715 */
716 if (nobj == 0)
717 goto done;
719 if (lseek(packfd, 0, SEEK_SET) == -1) {
720 err = got_error_from_errno("lseek");
721 goto done;
724 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
725 err = got_error_from_errno("socketpair");
726 goto done;
728 idxpid = fork();
729 if (idxpid == -1) {
730 err= got_error_from_errno("fork");
731 goto done;
732 } else if (idxpid == 0)
733 got_privsep_exec_child(imsg_idxfds,
734 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
735 if (close(imsg_idxfds[1]) == -1) {
736 err = got_error_from_errno("close");
737 goto done;
739 imsg_init(&idxibuf, imsg_idxfds[0]);
741 npackfd = dup(packfd);
742 if (npackfd == -1) {
743 err = got_error_from_errno("dup");
744 goto done;
746 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
747 npackfd);
748 if (err != NULL)
749 goto done;
750 npackfd = -1;
751 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
752 if (err != NULL)
753 goto done;
754 nidxfd = -1;
755 for (i = 0; i < nitems(tmpfds); i++) {
756 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
757 if (err != NULL)
758 goto done;
759 tmpfds[i] = -1;
761 done = 0;
762 while (!done) {
763 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
765 err = got_privsep_recv_index_progress(&done, &nobj_total,
766 &nobj_indexed, &nobj_loose, &nobj_resolved,
767 &idxibuf);
768 if (err != NULL)
769 goto done;
770 if (nobj_indexed != 0) {
771 err = progress_cb(progress_arg, NULL,
772 packfile_size, nobj_total,
773 nobj_indexed, nobj_loose, nobj_resolved);
774 if (err)
775 break;
777 imsg_clear(&idxibuf);
779 if (close(imsg_idxfds[0]) == -1) {
780 err = got_error_from_errno("close");
781 goto done;
783 if (waitpid(idxpid, &idxstatus, 0) == -1) {
784 err = got_error_from_errno("waitpid");
785 goto done;
788 err = got_object_id_str(&id_str, *pack_hash);
789 if (err)
790 goto done;
791 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
792 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
793 err = got_error_from_errno("asprintf");
794 goto done;
797 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
798 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
799 err = got_error_from_errno("asprintf");
800 goto done;
803 if (rename(tmppackpath, packpath) == -1) {
804 err = got_error_from_errno3("rename", tmppackpath, packpath);
805 goto done;
807 free(tmppackpath);
808 tmppackpath = NULL;
809 if (rename(tmpidxpath, idxpath) == -1) {
810 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
811 goto done;
813 free(tmpidxpath);
814 tmpidxpath = NULL;
816 done:
817 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
818 err = got_error_from_errno2("unlink", tmppackpath);
819 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
820 err = got_error_from_errno2("unlink", tmpidxpath);
821 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
822 err = got_error_from_errno("close");
823 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
825 if (packfd != -1 && close(packfd) == -1 && err == NULL)
826 err = got_error_from_errno("close");
827 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
828 err = got_error_from_errno("close");
829 for (i = 0; i < nitems(tmpfds); i++) {
830 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
831 err = got_error_from_errno("close");
833 free(tmppackpath);
834 free(tmpidxpath);
835 free(idxpath);
836 free(packpath);
837 free(ref_prefix);
838 free(progress);
840 TAILQ_FOREACH(pe, &have_refs, entry) {
841 free((char *)pe->path);
842 free(pe->data);
844 got_pathlist_free(&have_refs);
845 got_ref_list_free(&my_refs);
846 if (err) {
847 free(*pack_hash);
848 *pack_hash = NULL;
849 TAILQ_FOREACH(pe, refs, entry) {
850 free((void *)pe->path);
851 free(pe->data);
853 got_pathlist_free(refs);
854 TAILQ_FOREACH(pe, symrefs, entry) {
855 free((void *)pe->path);
856 free(pe->data);
858 got_pathlist_free(symrefs);
860 return err;