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/syslimits.h>
24 #include <sys/resource.h>
25 #include <sys/socket.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 <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
40 #include <uuid.h>
41 #include <netdb.h>
42 #include <netinet/in.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
51 #include "got_opentemp.h"
52 #include "got_fetch.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 #ifndef MIN
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
71 #endif
73 static int
74 hassuffix(char *base, char *suf)
75 {
76 int nb, ns;
78 nb = strlen(base);
79 ns = strlen(suf);
80 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
81 return 1;
82 return 0;
83 }
85 static const struct got_error *
86 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
87 const char *path, const char *direction, int verbosity)
88 {
89 const struct got_error *error = NULL;
90 int pid, pfd[2];
91 char cmd[64];
92 char *argv[11];
93 int i = 0, j;
95 *fetchpid = -1;
96 *fetchfd = -1;
98 argv[i++] = GOT_FETCH_PATH_SSH;
99 if (port != NULL) {
100 argv[i++] = "-p";
101 argv[i++] = (char *)port;
103 if (verbosity == -1) {
104 argv[i++] = "-q";
105 } else {
106 /* ssh(1) allows up to 3 "-v" options. */
107 for (j = 0; j < MIN(3, verbosity); j++)
108 argv[i++] = "-v";
110 argv[i++] = "--";
111 argv[i++] = (char *)host;
112 argv[i++] = (char *)cmd;
113 argv[i++] = (char *)path;
114 argv[i++] = NULL;
116 if (pipe(pfd) == -1)
117 return got_error_from_errno("pipe");
119 pid = fork();
120 if (pid == -1) {
121 error = got_error_from_errno("fork");
122 close(pfd[0]);
123 close(pfd[1]);
124 return error;
125 } else if (pid == 0) {
126 int n;
127 close(pfd[1]);
128 dup2(pfd[0], 0);
129 dup2(pfd[0], 1);
130 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
131 if (n < 0 || n >= sizeof(cmd))
132 err(1, "snprintf");
133 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
134 err(1, "execl");
135 abort(); /* not reached */
136 } else {
137 close(pfd[0]);
138 *fetchpid = pid;
139 *fetchfd = pfd[1];
140 return NULL;
144 static const struct got_error *
145 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
146 const char *direction)
148 const struct got_error *err = NULL;
149 struct addrinfo hints, *servinfo, *p;
150 char *cmd = NULL, *pkt = NULL;
151 int fd = -1, totlen, r, eaicode;
153 *fetchfd = -1;
155 if (port == NULL)
156 port = GOT_DEFAULT_GIT_PORT_STR;
158 memset(&hints, 0, sizeof hints);
159 hints.ai_family = AF_UNSPEC;
160 hints.ai_socktype = SOCK_STREAM;
161 eaicode = getaddrinfo(host, port, &hints, &servinfo);
162 if (eaicode) {
163 char msg[512];
164 snprintf(msg, sizeof(msg), "%s: %s", host,
165 gai_strerror(eaicode));
166 return got_error_msg(GOT_ERR_ADDRINFO, msg);
169 for (p = servinfo; p != NULL; p = p->ai_next) {
170 if ((fd = socket(p->ai_family, p->ai_socktype,
171 p->ai_protocol)) == -1)
172 continue;
173 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
174 break;
175 err = got_error_from_errno("connect");
176 close(fd);
178 if (p == NULL)
179 goto done;
181 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
186 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
187 err = got_error_from_errno("asprintf");
188 goto done;
190 r = write(fd, pkt, strlen(pkt) + 1);
191 if (r == -1) {
192 err = got_error_from_errno("write");
193 goto done;
195 if (asprintf(&pkt, "host=%s", host) == -1) {
196 err = got_error_from_errno("asprintf");
197 goto done;
199 r = write(fd, pkt, strlen(pkt) + 1);
200 if (r == -1) {
201 err = got_error_from_errno("write");
202 goto done;
204 done:
205 free(cmd);
206 free(pkt);
207 if (err) {
208 if (fd != -1)
209 close(fd);
210 } else
211 *fetchfd = fd;
212 return err;
215 const struct got_error *
216 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
217 const char *host, const char *port, const char *server_path, int verbosity)
219 const struct got_error *err = NULL;
221 *fetchpid = -1;
222 *fetchfd = -1;
224 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
225 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
226 "upload", verbosity);
227 else if (strcmp(proto, "git") == 0)
228 err = dial_git(fetchfd, host, port, server_path, "upload");
229 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
230 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
231 else
232 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
233 return err;
236 const struct got_error *
237 got_fetch_parse_uri(char **proto, char **host, char **port,
238 char **server_path, char **repo_name, const char *uri)
240 const struct got_error *err = NULL;
241 char *s, *p, *q;
242 int n;
244 *proto = *host = *port = *server_path = *repo_name = NULL;
246 p = strstr(uri, "://");
247 if (!p) {
248 /* Try parsing Git's "scp" style URL syntax. */
249 *proto = strdup("ssh");
250 if (proto == NULL) {
251 err = got_error_from_errno("strdup");
252 goto done;
254 s = (char *)uri;
255 q = strchr(s, ':');
256 if (q == NULL) {
257 err = got_error(GOT_ERR_PARSE_URI);
258 goto done;
260 /* No slashes allowed before first colon. */
261 p = strchr(s, '/');
262 if (p && q > p) {
263 err = got_error(GOT_ERR_PARSE_URI);
264 goto done;
266 *host = strndup(s, q - s);
267 if (*host == NULL) {
268 err = got_error_from_errno("strndup");
269 goto done;
271 p = q + 1;
272 } else {
273 *proto = strndup(uri, p - uri);
274 if (proto == NULL) {
275 err = got_error_from_errno("strndup");
276 goto done;
278 s = p + 3;
280 p = strstr(s, "/");
281 if (p == NULL || strlen(p) == 1) {
282 err = got_error(GOT_ERR_PARSE_URI);
283 goto done;
286 q = memchr(s, ':', p - s);
287 if (q) {
288 *host = strndup(s, q - s);
289 if (*host == NULL) {
290 err = got_error_from_errno("strndup");
291 goto done;
293 *port = strndup(q + 1, p - (q + 1));
294 if (*port == NULL) {
295 err = got_error_from_errno("strndup");
296 goto done;
298 } else {
299 *host = strndup(s, p - s);
300 if (*host == NULL) {
301 err = got_error_from_errno("strndup");
302 goto done;
307 *server_path = strdup(p);
308 if (*server_path == NULL) {
309 err = got_error_from_errno("strdup");
310 goto done;
313 p = strrchr(p, '/');
314 if (!p || strlen(p) <= 1) {
315 err = got_error(GOT_ERR_PARSE_URI);
316 goto done;
318 p++;
319 n = strlen(p);
320 if (n == 0) {
321 err = got_error(GOT_ERR_PARSE_URI);
322 goto done;
324 if (hassuffix(p, ".git"))
325 n -= 4;
326 *repo_name = strndup(p, (p + n) - p);
327 if (*repo_name == NULL) {
328 err = got_error_from_errno("strndup");
329 goto done;
331 done:
332 if (err) {
333 free(*proto);
334 *proto = NULL;
335 free(*host);
336 *host = NULL;
337 free(*port);
338 *port = NULL;
339 free(*server_path);
340 *server_path = NULL;
341 free(*repo_name);
342 *repo_name = NULL;
344 return err;
347 const struct got_error*
348 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
349 struct got_pathlist_head *symrefs, const char *remote_name,
350 int mirror_references, int fetch_all_branches,
351 struct got_pathlist_head *wanted_branches,
352 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
353 int fetchfd, struct got_repository *repo,
354 got_fetch_progress_cb progress_cb, void *progress_arg)
356 int imsg_fetchfds[2], imsg_idxfds[2];
357 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
358 int tmpfds[3], i;
359 int fetchstatus, idxstatus, done = 0;
360 const struct got_error *err;
361 struct imsgbuf fetchibuf, idxibuf;
362 pid_t fetchpid, idxpid;
363 char *tmppackpath = NULL, *tmpidxpath = NULL;
364 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
365 const char *repo_path = NULL;
366 struct got_pathlist_head have_refs;
367 struct got_pathlist_entry *pe;
368 struct got_reflist_head my_refs;
369 struct got_reflist_entry *re;
370 off_t packfile_size = 0;
371 struct got_packfile_hdr pack_hdr;
372 uint32_t nobj = 0;
373 char *ref_prefix = NULL;
374 size_t ref_prefixlen = 0;
375 char *path;
376 char *progress = NULL;
378 *pack_hash = NULL;
380 /*
381 * Prevent fetching of references that won't make any
382 * sense outside of the remote repository's context.
383 */
384 TAILQ_FOREACH(pe, wanted_refs, entry) {
385 const char *refname = pe->path;
386 if (strncmp(refname, "refs/got/", 9) == 0 ||
387 strncmp(refname, "got/", 4) == 0 ||
388 strncmp(refname, "refs/remotes/", 13) == 0 ||
389 strncmp(refname, "remotes/", 8) == 0)
390 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
393 if (!list_refs_only)
394 repo_path = got_repo_get_path_git_dir(repo);
396 for (i = 0; i < nitems(tmpfds); i++)
397 tmpfds[i] = -1;
399 TAILQ_INIT(&have_refs);
400 SIMPLEQ_INIT(&my_refs);
402 if (!mirror_references) {
403 if (asprintf(&ref_prefix, "refs/remotes/%s/",
404 remote_name) == -1)
405 return got_error_from_errno("asprintf");
406 ref_prefixlen = strlen(ref_prefix);
409 if (!list_refs_only) {
410 err = got_ref_list(&my_refs, repo, NULL,
411 got_ref_cmp_by_name, NULL);
412 if (err)
413 goto done;
416 SIMPLEQ_FOREACH(re, &my_refs, entry) {
417 struct got_object_id *id;
418 const char *refname;
420 if (got_ref_is_symbolic(re->ref))
421 continue;
423 refname = got_ref_get_name(re->ref);
425 if (mirror_references) {
426 char *name;
427 err = got_ref_resolve(&id, repo, re->ref);
428 if (err)
429 goto done;
430 name = strdup(refname);
431 if (name == NULL) {
432 err = got_error_from_errno("strdup");
433 goto done;
435 err = got_pathlist_append(&have_refs, name, id);
436 if (err)
437 goto done;
438 continue;
441 if (strncmp("refs/tags/", refname, 10) == 0) {
442 char *tagname;
444 err = got_ref_resolve(&id, repo, re->ref);
445 if (err)
446 goto done;
447 tagname = strdup(refname);
448 if (tagname == NULL) {
449 err = got_error_from_errno("strdup");
450 goto done;
452 err = got_pathlist_append(&have_refs, tagname, id);
453 if (err) {
454 free(tagname);
455 goto done;
459 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
460 char *branchname;
462 err = got_ref_resolve(&id, repo, re->ref);
463 if (err)
464 goto done;
466 if (asprintf(&branchname, "refs/heads/%s",
467 refname + ref_prefixlen) == -1) {
468 err = got_error_from_errno("asprintf");
469 goto done;
471 err = got_pathlist_append(&have_refs, branchname, id);
472 if (err) {
473 free(branchname);
474 goto done;
479 if (list_refs_only) {
480 packfd = got_opentempfd();
481 if (packfd == -1) {
482 err = got_error_from_errno("got_opentempfd");
483 goto done;
485 } else {
486 if (asprintf(&path, "%s/%s/fetching.pack",
487 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
488 err = got_error_from_errno("asprintf");
489 goto done;
491 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
492 free(path);
493 if (err)
494 goto done;
496 if (list_refs_only) {
497 idxfd = got_opentempfd();
498 if (idxfd == -1) {
499 err = got_error_from_errno("got_opentempfd");
500 goto done;
502 } else {
503 if (asprintf(&path, "%s/%s/fetching.idx",
504 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
505 err = got_error_from_errno("asprintf");
506 goto done;
508 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
509 free(path);
510 if (err)
511 goto done;
513 nidxfd = dup(idxfd);
514 if (nidxfd == -1) {
515 err = got_error_from_errno("dup");
516 goto done;
519 for (i = 0; i < nitems(tmpfds); i++) {
520 tmpfds[i] = got_opentempfd();
521 if (tmpfds[i] == -1) {
522 err = got_error_from_errno("got_opentempfd");
523 goto done;
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
528 err = got_error_from_errno("socketpair");
529 goto done;
532 fetchpid = fork();
533 if (fetchpid == -1) {
534 err = got_error_from_errno("fork");
535 goto done;
536 } else if (fetchpid == 0){
537 got_privsep_exec_child(imsg_fetchfds,
538 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
541 if (close(imsg_fetchfds[1]) != 0) {
542 err = got_error_from_errno("close");
543 goto done;
545 imsg_init(&fetchibuf, imsg_fetchfds[0]);
546 nfetchfd = dup(fetchfd);
547 if (nfetchfd == -1) {
548 err = got_error_from_errno("dup");
549 goto done;
551 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
552 fetch_all_branches, wanted_branches, wanted_refs,
553 list_refs_only, verbosity);
554 if (err != NULL)
555 goto done;
556 nfetchfd = -1;
557 npackfd = dup(packfd);
558 if (npackfd == -1) {
559 err = got_error_from_errno("dup");
560 goto done;
562 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
563 if (err != NULL)
564 goto done;
565 npackfd = -1;
567 packfile_size = 0;
568 progress = calloc(GOT_FETCH_PKTMAX, 1);
569 if (progress == NULL) {
570 err = got_error_from_errno("calloc");
571 goto done;
574 *pack_hash = calloc(1, sizeof(**pack_hash));
575 if (*pack_hash == NULL) {
576 err = got_error_from_errno("calloc");
577 goto done;
580 while (!done) {
581 struct got_object_id *id = NULL;
582 char *refname = NULL;
583 char *server_progress = NULL;
584 off_t packfile_size_cur = 0;
586 err = got_privsep_recv_fetch_progress(&done,
587 &id, &refname, symrefs, &server_progress,
588 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
589 if (err != NULL)
590 goto done;
591 if (!done && refname && id) {
592 err = got_pathlist_insert(NULL, refs, refname, id);
593 if (err)
594 goto done;
595 } else if (!done && server_progress) {
596 char *p;
597 /*
598 * XXX git-daemon tends to send batched output with
599 * lines spanning separate packets. Buffer progress
600 * output until we see a CR or LF to avoid giving
601 * partial lines of progress output to the callback.
602 */
603 if (strlcat(progress, server_progress,
604 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
605 progress[0] = '\0'; /* discard */
606 continue;
608 while ((p = strchr(progress, '\r')) != NULL ||
609 (p = strchr(progress, '\n')) != NULL) {
610 char *s;
611 size_t n;
612 char c = *p;
613 *p = '\0';
614 if (asprintf(&s, "%s%s", progress,
615 c == '\n' ? "\n" : "") == -1) {
616 err = got_error_from_errno("asprintf");
617 goto done;
619 err = progress_cb(progress_arg, s,
620 packfile_size_cur, 0, 0, 0, 0);
621 free(s);
622 if (err)
623 break;
624 n = strlen(progress);
625 if (n < GOT_FETCH_PKTMAX - 1) {
626 memmove(progress, &progress[n + 1],
627 GOT_FETCH_PKTMAX - n - 1);
628 } else
629 progress[0] = '\0';
631 free(server_progress);
632 if (err)
633 goto done;
634 } else if (!done && packfile_size_cur != packfile_size) {
635 err = progress_cb(progress_arg, NULL,
636 packfile_size_cur, 0, 0, 0, 0);
637 if (err)
638 break;
639 packfile_size = packfile_size_cur;
642 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
643 err = got_error_from_errno("waitpid");
644 goto done;
647 if (lseek(packfd, 0, SEEK_SET) == -1) {
648 err = got_error_from_errno("lseek");
649 goto done;
652 /* If zero data was fetched without error we are already up-to-date. */
653 if (packfile_size == 0) {
654 free(*pack_hash);
655 *pack_hash = NULL;
656 goto done;
657 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
658 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
659 goto done;
660 } else {
661 ssize_t n;
663 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
664 if (n == -1) {
665 err = got_error_from_errno("read");
666 goto done;
668 if (n != sizeof(pack_hdr)) {
669 err = got_error(GOT_ERR_IO);
670 goto done;
672 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
673 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
674 "bad pack file signature");
675 goto done;
677 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
678 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
679 "bad pack file version");
680 goto done;
682 nobj = betoh32(pack_hdr.nobjects);
683 if (nobj == 0 &&
684 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
685 return got_error_msg(GOT_ERR_BAD_PACKFILE,
686 "bad pack file with zero objects");
687 if (nobj != 0 &&
688 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
689 return got_error_msg(GOT_ERR_BAD_PACKFILE,
690 "empty pack file with non-zero object count");
693 /*
694 * If the pack file contains no objects, we may only need to update
695 * references in our repository. The caller will take care of that.
696 */
697 if (nobj == 0)
698 goto done;
700 if (lseek(packfd, 0, SEEK_SET) == -1) {
701 err = got_error_from_errno("lseek");
702 goto done;
705 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
706 err = got_error_from_errno("socketpair");
707 goto done;
709 idxpid = fork();
710 if (idxpid == -1) {
711 err= got_error_from_errno("fork");
712 goto done;
713 } else if (idxpid == 0)
714 got_privsep_exec_child(imsg_idxfds,
715 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
716 if (close(imsg_idxfds[1]) != 0) {
717 err = got_error_from_errno("close");
718 goto done;
720 imsg_init(&idxibuf, imsg_idxfds[0]);
722 npackfd = dup(packfd);
723 if (npackfd == -1) {
724 err = got_error_from_errno("dup");
725 goto done;
727 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
728 npackfd);
729 if (err != NULL)
730 goto done;
731 npackfd = -1;
732 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
733 if (err != NULL)
734 goto done;
735 nidxfd = -1;
736 for (i = 0; i < nitems(tmpfds); i++) {
737 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
738 if (err != NULL)
739 goto done;
740 tmpfds[i] = -1;
742 done = 0;
743 while (!done) {
744 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
746 err = got_privsep_recv_index_progress(&done, &nobj_total,
747 &nobj_indexed, &nobj_loose, &nobj_resolved,
748 &idxibuf);
749 if (err != NULL)
750 goto done;
751 if (nobj_indexed != 0) {
752 err = progress_cb(progress_arg, NULL,
753 packfile_size, nobj_total,
754 nobj_indexed, nobj_loose, nobj_resolved);
755 if (err)
756 break;
758 imsg_clear(&idxibuf);
760 if (close(imsg_idxfds[0]) == -1) {
761 err = got_error_from_errno("close");
762 goto done;
764 if (waitpid(idxpid, &idxstatus, 0) == -1) {
765 err = got_error_from_errno("waitpid");
766 goto done;
769 err = got_object_id_str(&id_str, *pack_hash);
770 if (err)
771 goto done;
772 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
773 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
779 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
780 err = got_error_from_errno("asprintf");
781 goto done;
784 if (rename(tmppackpath, packpath) == -1) {
785 err = got_error_from_errno3("rename", tmppackpath, packpath);
786 goto done;
788 free(tmppackpath);
789 tmppackpath = NULL;
790 if (rename(tmpidxpath, idxpath) == -1) {
791 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
792 goto done;
794 free(tmpidxpath);
795 tmpidxpath = NULL;
797 done:
798 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
799 err = got_error_from_errno2("unlink", tmppackpath);
800 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
801 err = got_error_from_errno2("unlink", tmpidxpath);
802 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
803 err = got_error_from_errno("close");
804 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 if (packfd != -1 && close(packfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 for (i = 0; i < nitems(tmpfds); i++) {
811 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
812 err = got_error_from_errno("close");
814 free(tmppackpath);
815 free(tmpidxpath);
816 free(idxpath);
817 free(packpath);
818 free(ref_prefix);
819 free(progress);
821 TAILQ_FOREACH(pe, &have_refs, entry) {
822 free((char *)pe->path);
823 free(pe->data);
825 got_pathlist_free(&have_refs);
826 got_ref_list_free(&my_refs);
827 if (err) {
828 free(*pack_hash);
829 *pack_hash = NULL;
830 TAILQ_FOREACH(pe, refs, entry) {
831 free((void *)pe->path);
832 free(pe->data);
834 got_pathlist_free(refs);
835 TAILQ_FOREACH(pe, symrefs, entry) {
836 free((void *)pe->path);
837 free(pe->data);
839 got_pathlist_free(symrefs);
841 return err;