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 break;
176 err = got_error_from_errno("connect");
177 close(fd);
179 if (p == NULL)
180 goto done;
182 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
183 err = got_error_from_errno("asprintf");
184 goto done;
186 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
187 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
188 err = got_error_from_errno("asprintf");
189 goto done;
191 r = write(fd, pkt, strlen(pkt) + 1);
192 if (r == -1) {
193 err = got_error_from_errno("write");
194 goto done;
196 if (asprintf(&pkt, "host=%s", host) == -1) {
197 err = got_error_from_errno("asprintf");
198 goto done;
200 r = write(fd, pkt, strlen(pkt) + 1);
201 if (r == -1) {
202 err = got_error_from_errno("write");
203 goto done;
205 done:
206 free(cmd);
207 free(pkt);
208 if (err) {
209 if (fd != -1)
210 close(fd);
211 } else
212 *fetchfd = fd;
213 return err;
216 const struct got_error *
217 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
218 const char *host, const char *port, const char *server_path, int verbosity)
220 const struct got_error *err = NULL;
222 *fetchpid = -1;
223 *fetchfd = -1;
225 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
226 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
227 "upload", verbosity);
228 else if (strcmp(proto, "git") == 0)
229 err = dial_git(fetchfd, host, port, server_path, "upload");
230 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
231 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
232 else
233 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
234 return err;
237 const struct got_error *
238 got_fetch_parse_uri(char **proto, char **host, char **port,
239 char **server_path, char **repo_name, const char *uri)
241 const struct got_error *err = NULL;
242 char *s, *p, *q;
243 int n;
245 *proto = *host = *port = *server_path = *repo_name = NULL;
247 p = strstr(uri, "://");
248 if (!p) {
249 /* Try parsing Git's "scp" style URL syntax. */
250 *proto = strdup("ssh");
251 if (proto == NULL) {
252 err = got_error_from_errno("strdup");
253 goto done;
255 s = (char *)uri;
256 q = strchr(s, ':');
257 if (q == NULL) {
258 err = got_error(GOT_ERR_PARSE_URI);
259 goto done;
261 /* No slashes allowed before first colon. */
262 p = strchr(s, '/');
263 if (p && q > p) {
264 err = got_error(GOT_ERR_PARSE_URI);
265 goto done;
267 *host = strndup(s, q - s);
268 if (*host == NULL) {
269 err = got_error_from_errno("strndup");
270 goto done;
272 p = q + 1;
273 } else {
274 *proto = strndup(uri, p - uri);
275 if (proto == NULL) {
276 err = got_error_from_errno("strndup");
277 goto done;
279 s = p + 3;
281 p = strstr(s, "/");
282 if (p == NULL || strlen(p) == 1) {
283 err = got_error(GOT_ERR_PARSE_URI);
284 goto done;
287 q = memchr(s, ':', p - s);
288 if (q) {
289 *host = strndup(s, q - s);
290 if (*host == NULL) {
291 err = got_error_from_errno("strndup");
292 goto done;
294 *port = strndup(q + 1, p - (q + 1));
295 if (*port == NULL) {
296 err = got_error_from_errno("strndup");
297 goto done;
299 } else {
300 *host = strndup(s, p - s);
301 if (*host == NULL) {
302 err = got_error_from_errno("strndup");
303 goto done;
308 while (p[0] == '/' && p[1] == '/')
309 p++;
310 *server_path = strdup(p);
311 if (*server_path == NULL) {
312 err = got_error_from_errno("strdup");
313 goto done;
315 got_path_strip_trailing_slashes(*server_path);
317 p = strrchr(p, '/');
318 if (!p || strlen(p) <= 1) {
319 err = got_error(GOT_ERR_PARSE_URI);
320 goto done;
322 p++;
323 n = strlen(p);
324 if (n == 0) {
325 err = got_error(GOT_ERR_PARSE_URI);
326 goto done;
328 if (hassuffix(p, ".git"))
329 n -= 4;
330 *repo_name = strndup(p, (p + n) - p);
331 if (*repo_name == NULL) {
332 err = got_error_from_errno("strndup");
333 goto done;
335 done:
336 if (err) {
337 free(*proto);
338 *proto = NULL;
339 free(*host);
340 *host = NULL;
341 free(*port);
342 *port = NULL;
343 free(*server_path);
344 *server_path = NULL;
345 free(*repo_name);
346 *repo_name = NULL;
348 return err;
351 const struct got_error*
352 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
353 struct got_pathlist_head *symrefs, const char *remote_name,
354 int mirror_references, int fetch_all_branches,
355 struct got_pathlist_head *wanted_branches,
356 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
357 int fetchfd, struct got_repository *repo,
358 got_fetch_progress_cb progress_cb, void *progress_arg)
360 int imsg_fetchfds[2], imsg_idxfds[2];
361 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
362 int tmpfds[3], i;
363 int fetchstatus, idxstatus, done = 0;
364 const struct got_error *err;
365 struct imsgbuf fetchibuf, idxibuf;
366 pid_t fetchpid, idxpid;
367 char *tmppackpath = NULL, *tmpidxpath = NULL;
368 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
369 const char *repo_path = NULL;
370 struct got_pathlist_head have_refs;
371 struct got_pathlist_entry *pe;
372 struct got_reflist_head my_refs;
373 struct got_reflist_entry *re;
374 off_t packfile_size = 0;
375 struct got_packfile_hdr pack_hdr;
376 uint32_t nobj = 0;
377 char *ref_prefix = NULL;
378 size_t ref_prefixlen = 0;
379 char *path;
380 char *progress = NULL;
382 *pack_hash = NULL;
384 /*
385 * Prevent fetching of references that won't make any
386 * sense outside of the remote repository's context.
387 */
388 TAILQ_FOREACH(pe, wanted_refs, entry) {
389 const char *refname = pe->path;
390 if (strncmp(refname, "refs/got/", 9) == 0 ||
391 strncmp(refname, "got/", 4) == 0 ||
392 strncmp(refname, "refs/remotes/", 13) == 0 ||
393 strncmp(refname, "remotes/", 8) == 0)
394 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
397 if (!list_refs_only)
398 repo_path = got_repo_get_path_git_dir(repo);
400 for (i = 0; i < nitems(tmpfds); i++)
401 tmpfds[i] = -1;
403 TAILQ_INIT(&have_refs);
404 SIMPLEQ_INIT(&my_refs);
406 if (!mirror_references) {
407 if (asprintf(&ref_prefix, "refs/remotes/%s/",
408 remote_name) == -1)
409 return got_error_from_errno("asprintf");
410 ref_prefixlen = strlen(ref_prefix);
413 if (!list_refs_only) {
414 err = got_ref_list(&my_refs, repo, NULL,
415 got_ref_cmp_by_name, NULL);
416 if (err)
417 goto done;
420 SIMPLEQ_FOREACH(re, &my_refs, entry) {
421 struct got_object_id *id;
422 const char *refname;
424 if (got_ref_is_symbolic(re->ref))
425 continue;
427 refname = got_ref_get_name(re->ref);
429 if (mirror_references) {
430 char *name;
431 err = got_ref_resolve(&id, repo, re->ref);
432 if (err)
433 goto done;
434 name = strdup(refname);
435 if (name == NULL) {
436 err = got_error_from_errno("strdup");
437 goto done;
439 err = got_pathlist_append(&have_refs, name, id);
440 if (err)
441 goto done;
442 continue;
445 if (strncmp("refs/tags/", refname, 10) == 0) {
446 char *tagname;
448 err = got_ref_resolve(&id, repo, re->ref);
449 if (err)
450 goto done;
451 tagname = strdup(refname);
452 if (tagname == NULL) {
453 err = got_error_from_errno("strdup");
454 goto done;
456 err = got_pathlist_append(&have_refs, tagname, id);
457 if (err) {
458 free(tagname);
459 goto done;
463 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
464 char *branchname;
466 err = got_ref_resolve(&id, repo, re->ref);
467 if (err)
468 goto done;
470 if (asprintf(&branchname, "refs/heads/%s",
471 refname + ref_prefixlen) == -1) {
472 err = got_error_from_errno("asprintf");
473 goto done;
475 err = got_pathlist_append(&have_refs, branchname, id);
476 if (err) {
477 free(branchname);
478 goto done;
483 if (list_refs_only) {
484 packfd = got_opentempfd();
485 if (packfd == -1) {
486 err = got_error_from_errno("got_opentempfd");
487 goto done;
489 } else {
490 if (asprintf(&path, "%s/%s/fetching.pack",
491 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
492 err = got_error_from_errno("asprintf");
493 goto done;
495 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
496 free(path);
497 if (err)
498 goto done;
500 if (list_refs_only) {
501 idxfd = got_opentempfd();
502 if (idxfd == -1) {
503 err = got_error_from_errno("got_opentempfd");
504 goto done;
506 } else {
507 if (asprintf(&path, "%s/%s/fetching.idx",
508 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
509 err = got_error_from_errno("asprintf");
510 goto done;
512 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
513 free(path);
514 if (err)
515 goto done;
517 nidxfd = dup(idxfd);
518 if (nidxfd == -1) {
519 err = got_error_from_errno("dup");
520 goto done;
523 for (i = 0; i < nitems(tmpfds); i++) {
524 tmpfds[i] = got_opentempfd();
525 if (tmpfds[i] == -1) {
526 err = got_error_from_errno("got_opentempfd");
527 goto done;
531 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
532 err = got_error_from_errno("socketpair");
533 goto done;
536 fetchpid = fork();
537 if (fetchpid == -1) {
538 err = got_error_from_errno("fork");
539 goto done;
540 } else if (fetchpid == 0){
541 got_privsep_exec_child(imsg_fetchfds,
542 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
545 if (close(imsg_fetchfds[1]) != 0) {
546 err = got_error_from_errno("close");
547 goto done;
549 imsg_init(&fetchibuf, imsg_fetchfds[0]);
550 nfetchfd = dup(fetchfd);
551 if (nfetchfd == -1) {
552 err = got_error_from_errno("dup");
553 goto done;
555 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
556 fetch_all_branches, wanted_branches, wanted_refs,
557 list_refs_only, verbosity);
558 if (err != NULL)
559 goto done;
560 nfetchfd = -1;
561 npackfd = dup(packfd);
562 if (npackfd == -1) {
563 err = got_error_from_errno("dup");
564 goto done;
566 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
567 if (err != NULL)
568 goto done;
569 npackfd = -1;
571 packfile_size = 0;
572 progress = calloc(GOT_FETCH_PKTMAX, 1);
573 if (progress == NULL) {
574 err = got_error_from_errno("calloc");
575 goto done;
578 *pack_hash = calloc(1, sizeof(**pack_hash));
579 if (*pack_hash == NULL) {
580 err = got_error_from_errno("calloc");
581 goto done;
584 while (!done) {
585 struct got_object_id *id = NULL;
586 char *refname = NULL;
587 char *server_progress = NULL;
588 off_t packfile_size_cur = 0;
590 err = got_privsep_recv_fetch_progress(&done,
591 &id, &refname, symrefs, &server_progress,
592 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
593 if (err != NULL)
594 goto done;
595 if (!done && refname && id) {
596 err = got_pathlist_insert(NULL, refs, refname, id);
597 if (err)
598 goto done;
599 } else if (!done && server_progress) {
600 char *p;
601 /*
602 * XXX git-daemon tends to send batched output with
603 * lines spanning separate packets. Buffer progress
604 * output until we see a CR or LF to avoid giving
605 * partial lines of progress output to the callback.
606 */
607 if (strlcat(progress, server_progress,
608 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
609 progress[0] = '\0'; /* discard */
610 continue;
612 while ((p = strchr(progress, '\r')) != NULL ||
613 (p = strchr(progress, '\n')) != NULL) {
614 char *s;
615 size_t n;
616 char c = *p;
617 *p = '\0';
618 if (asprintf(&s, "%s%s", progress,
619 c == '\n' ? "\n" : "") == -1) {
620 err = got_error_from_errno("asprintf");
621 goto done;
623 err = progress_cb(progress_arg, s,
624 packfile_size_cur, 0, 0, 0, 0);
625 free(s);
626 if (err)
627 break;
628 n = strlen(progress);
629 if (n < GOT_FETCH_PKTMAX - 1) {
630 memmove(progress, &progress[n + 1],
631 GOT_FETCH_PKTMAX - n - 1);
632 } else
633 progress[0] = '\0';
635 free(server_progress);
636 if (err)
637 goto done;
638 } else if (!done && packfile_size_cur != packfile_size) {
639 err = progress_cb(progress_arg, NULL,
640 packfile_size_cur, 0, 0, 0, 0);
641 if (err)
642 break;
643 packfile_size = packfile_size_cur;
646 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
647 err = got_error_from_errno("waitpid");
648 goto done;
651 if (lseek(packfd, 0, SEEK_SET) == -1) {
652 err = got_error_from_errno("lseek");
653 goto done;
656 /* If zero data was fetched without error we are already up-to-date. */
657 if (packfile_size == 0) {
658 free(*pack_hash);
659 *pack_hash = NULL;
660 goto done;
661 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
662 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
663 goto done;
664 } else {
665 ssize_t n;
667 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
668 if (n == -1) {
669 err = got_error_from_errno("read");
670 goto done;
672 if (n != sizeof(pack_hdr)) {
673 err = got_error(GOT_ERR_IO);
674 goto done;
676 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
677 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
678 "bad pack file signature");
679 goto done;
681 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
682 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
683 "bad pack file version");
684 goto done;
686 nobj = be32toh(pack_hdr.nobjects);
687 if (nobj == 0 &&
688 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
689 return got_error_msg(GOT_ERR_BAD_PACKFILE,
690 "bad pack file with zero objects");
691 if (nobj != 0 &&
692 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
693 return got_error_msg(GOT_ERR_BAD_PACKFILE,
694 "empty pack file with non-zero object count");
697 /*
698 * If the pack file contains no objects, we may only need to update
699 * references in our repository. The caller will take care of that.
700 */
701 if (nobj == 0)
702 goto done;
704 if (lseek(packfd, 0, SEEK_SET) == -1) {
705 err = got_error_from_errno("lseek");
706 goto done;
709 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
710 err = got_error_from_errno("socketpair");
711 goto done;
713 idxpid = fork();
714 if (idxpid == -1) {
715 err= got_error_from_errno("fork");
716 goto done;
717 } else if (idxpid == 0)
718 got_privsep_exec_child(imsg_idxfds,
719 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
720 if (close(imsg_idxfds[1]) != 0) {
721 err = got_error_from_errno("close");
722 goto done;
724 imsg_init(&idxibuf, imsg_idxfds[0]);
726 npackfd = dup(packfd);
727 if (npackfd == -1) {
728 err = got_error_from_errno("dup");
729 goto done;
731 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
732 npackfd);
733 if (err != NULL)
734 goto done;
735 npackfd = -1;
736 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
737 if (err != NULL)
738 goto done;
739 nidxfd = -1;
740 for (i = 0; i < nitems(tmpfds); i++) {
741 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
742 if (err != NULL)
743 goto done;
744 tmpfds[i] = -1;
746 done = 0;
747 while (!done) {
748 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
750 err = got_privsep_recv_index_progress(&done, &nobj_total,
751 &nobj_indexed, &nobj_loose, &nobj_resolved,
752 &idxibuf);
753 if (err != NULL)
754 goto done;
755 if (nobj_indexed != 0) {
756 err = progress_cb(progress_arg, NULL,
757 packfile_size, nobj_total,
758 nobj_indexed, nobj_loose, nobj_resolved);
759 if (err)
760 break;
762 imsg_clear(&idxibuf);
764 if (close(imsg_idxfds[0]) == -1) {
765 err = got_error_from_errno("close");
766 goto done;
768 if (waitpid(idxpid, &idxstatus, 0) == -1) {
769 err = got_error_from_errno("waitpid");
770 goto done;
773 err = got_object_id_str(&id_str, *pack_hash);
774 if (err)
775 goto done;
776 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
777 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
778 err = got_error_from_errno("asprintf");
779 goto done;
782 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
783 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
784 err = got_error_from_errno("asprintf");
785 goto done;
788 if (rename(tmppackpath, packpath) == -1) {
789 err = got_error_from_errno3("rename", tmppackpath, packpath);
790 goto done;
792 free(tmppackpath);
793 tmppackpath = NULL;
794 if (rename(tmpidxpath, idxpath) == -1) {
795 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
796 goto done;
798 free(tmpidxpath);
799 tmpidxpath = NULL;
801 done:
802 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
803 err = got_error_from_errno2("unlink", tmppackpath);
804 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
805 err = got_error_from_errno2("unlink", tmpidxpath);
806 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 if (packfd != -1 && close(packfd) == -1 && err == NULL)
811 err = got_error_from_errno("close");
812 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
813 err = got_error_from_errno("close");
814 for (i = 0; i < nitems(tmpfds); i++) {
815 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
816 err = got_error_from_errno("close");
818 free(tmppackpath);
819 free(tmpidxpath);
820 free(idxpath);
821 free(packpath);
822 free(ref_prefix);
823 free(progress);
825 TAILQ_FOREACH(pe, &have_refs, entry) {
826 free((char *)pe->path);
827 free(pe->data);
829 got_pathlist_free(&have_refs);
830 got_ref_list_free(&my_refs);
831 if (err) {
832 free(*pack_hash);
833 *pack_hash = NULL;
834 TAILQ_FOREACH(pe, refs, entry) {
835 free((void *)pe->path);
836 free(pe->data);
838 got_pathlist_free(refs);
839 TAILQ_FOREACH(pe, symrefs, entry) {
840 free((void *)pe->path);
841 free(pe->data);
843 got_pathlist_free(symrefs);
845 return err;