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 <endian.h>
28 #include <errno.h>
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <sha1.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <imsg.h>
41 #include <time.h>
42 #include <uuid.h>
43 #include <netdb.h>
44 #include <netinet/in.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
54 #include "got_fetch.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 #ifndef MIN
72 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
73 #endif
75 static int
76 hassuffix(char *base, char *suf)
77 {
78 int nb, ns;
80 nb = strlen(base);
81 ns = strlen(suf);
82 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
83 return 1;
84 return 0;
85 }
87 static const struct got_error *
88 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
89 const char *path, const char *direction, int verbosity)
90 {
91 const struct got_error *error = NULL;
92 int pid, pfd[2];
93 char cmd[64];
94 char *argv[11];
95 int i = 0, j;
97 *fetchpid = -1;
98 *fetchfd = -1;
100 argv[i++] = GOT_FETCH_PATH_SSH;
101 if (port != NULL) {
102 argv[i++] = "-p";
103 argv[i++] = (char *)port;
105 if (verbosity == -1) {
106 argv[i++] = "-q";
107 } else {
108 /* ssh(1) allows up to 3 "-v" options. */
109 for (j = 0; j < MIN(3, verbosity); j++)
110 argv[i++] = "-v";
112 argv[i++] = "--";
113 argv[i++] = (char *)host;
114 argv[i++] = (char *)cmd;
115 argv[i++] = (char *)path;
116 argv[i++] = NULL;
118 if (pipe(pfd) == -1)
119 return got_error_from_errno("pipe");
121 pid = fork();
122 if (pid == -1) {
123 error = got_error_from_errno("fork");
124 close(pfd[0]);
125 close(pfd[1]);
126 return error;
127 } else if (pid == 0) {
128 int n;
129 close(pfd[1]);
130 dup2(pfd[0], 0);
131 dup2(pfd[0], 1);
132 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
133 if (n < 0 || n >= sizeof(cmd))
134 err(1, "snprintf");
135 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
136 err(1, "execl");
137 abort(); /* not reached */
138 } else {
139 close(pfd[0]);
140 *fetchpid = pid;
141 *fetchfd = pfd[1];
142 return NULL;
146 static const struct got_error *
147 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
148 const char *direction)
150 const struct got_error *err = NULL;
151 struct addrinfo hints, *servinfo, *p;
152 char *cmd = NULL, *pkt = NULL;
153 int fd = -1, totlen, r, eaicode;
155 *fetchfd = -1;
157 if (port == NULL)
158 port = GOT_DEFAULT_GIT_PORT_STR;
160 memset(&hints, 0, sizeof hints);
161 hints.ai_family = AF_UNSPEC;
162 hints.ai_socktype = SOCK_STREAM;
163 eaicode = getaddrinfo(host, port, &hints, &servinfo);
164 if (eaicode) {
165 char msg[512];
166 snprintf(msg, sizeof(msg), "%s: %s", host,
167 gai_strerror(eaicode));
168 return got_error_msg(GOT_ERR_ADDRINFO, msg);
171 for (p = servinfo; p != NULL; p = p->ai_next) {
172 if ((fd = socket(p->ai_family, p->ai_socktype,
173 p->ai_protocol)) == -1)
174 continue;
175 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
176 break;
177 err = got_error_from_errno("connect");
178 close(fd);
180 if (p == NULL)
181 goto done;
183 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
188 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
189 err = got_error_from_errno("asprintf");
190 goto done;
192 r = write(fd, pkt, strlen(pkt) + 1);
193 if (r == -1) {
194 err = got_error_from_errno("write");
195 goto done;
197 if (asprintf(&pkt, "host=%s", host) == -1) {
198 err = got_error_from_errno("asprintf");
199 goto done;
201 r = write(fd, pkt, strlen(pkt) + 1);
202 if (r == -1) {
203 err = got_error_from_errno("write");
204 goto done;
206 done:
207 free(cmd);
208 free(pkt);
209 if (err) {
210 if (fd != -1)
211 close(fd);
212 } else
213 *fetchfd = fd;
214 return err;
217 const struct got_error *
218 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
219 const char *host, const char *port, const char *server_path, int verbosity)
221 const struct got_error *err = NULL;
223 *fetchpid = -1;
224 *fetchfd = -1;
226 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
227 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
228 "upload", verbosity);
229 else if (strcmp(proto, "git") == 0)
230 err = dial_git(fetchfd, host, port, server_path, "upload");
231 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
232 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
233 else
234 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
235 return err;
238 const struct got_error *
239 got_fetch_parse_uri(char **proto, char **host, char **port,
240 char **server_path, char **repo_name, const char *uri)
242 const struct got_error *err = NULL;
243 char *s, *p, *q;
244 int n;
246 *proto = *host = *port = *server_path = *repo_name = NULL;
248 p = strstr(uri, "://");
249 if (!p) {
250 /* Try parsing Git's "scp" style URL syntax. */
251 *proto = strdup("ssh");
252 if (proto == NULL) {
253 err = got_error_from_errno("strdup");
254 goto done;
256 s = (char *)uri;
257 q = strchr(s, ':');
258 if (q == NULL) {
259 err = got_error(GOT_ERR_PARSE_URI);
260 goto done;
262 /* No slashes allowed before first colon. */
263 p = strchr(s, '/');
264 if (p && q > p) {
265 err = got_error(GOT_ERR_PARSE_URI);
266 goto done;
268 *host = strndup(s, q - s);
269 if (*host == NULL) {
270 err = got_error_from_errno("strndup");
271 goto done;
273 p = q + 1;
274 } else {
275 *proto = strndup(uri, p - uri);
276 if (proto == NULL) {
277 err = got_error_from_errno("strndup");
278 goto done;
280 s = p + 3;
282 p = strstr(s, "/");
283 if (p == NULL || strlen(p) == 1) {
284 err = got_error(GOT_ERR_PARSE_URI);
285 goto done;
288 q = memchr(s, ':', p - s);
289 if (q) {
290 *host = strndup(s, q - s);
291 if (*host == NULL) {
292 err = got_error_from_errno("strndup");
293 goto done;
295 *port = strndup(q + 1, p - (q + 1));
296 if (*port == NULL) {
297 err = got_error_from_errno("strndup");
298 goto done;
300 } else {
301 *host = strndup(s, p - s);
302 if (*host == NULL) {
303 err = got_error_from_errno("strndup");
304 goto done;
309 *server_path = strdup(p);
310 if (*server_path == NULL) {
311 err = got_error_from_errno("strdup");
312 goto done;
315 p = strrchr(p, '/');
316 if (!p || strlen(p) <= 1) {
317 err = got_error(GOT_ERR_PARSE_URI);
318 goto done;
320 p++;
321 n = strlen(p);
322 if (n == 0) {
323 err = got_error(GOT_ERR_PARSE_URI);
324 goto done;
326 if (hassuffix(p, ".git"))
327 n -= 4;
328 *repo_name = strndup(p, (p + n) - p);
329 if (*repo_name == NULL) {
330 err = got_error_from_errno("strndup");
331 goto done;
333 done:
334 if (err) {
335 free(*proto);
336 *proto = NULL;
337 free(*host);
338 *host = NULL;
339 free(*port);
340 *port = NULL;
341 free(*server_path);
342 *server_path = NULL;
343 free(*repo_name);
344 *repo_name = NULL;
346 return err;
349 const struct got_error*
350 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
351 struct got_pathlist_head *symrefs, const char *remote_name,
352 int mirror_references, int fetch_all_branches,
353 struct got_pathlist_head *wanted_branches,
354 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
355 int fetchfd, struct got_repository *repo,
356 got_fetch_progress_cb progress_cb, void *progress_arg)
358 int imsg_fetchfds[2], imsg_idxfds[2];
359 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
360 int tmpfds[3], i;
361 int fetchstatus, idxstatus, done = 0;
362 const struct got_error *err;
363 struct imsgbuf fetchibuf, idxibuf;
364 pid_t fetchpid, idxpid;
365 char *tmppackpath = NULL, *tmpidxpath = NULL;
366 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
367 const char *repo_path = NULL;
368 struct got_pathlist_head have_refs;
369 struct got_pathlist_entry *pe;
370 struct got_reflist_head my_refs;
371 struct got_reflist_entry *re;
372 off_t packfile_size = 0;
373 struct got_packfile_hdr pack_hdr;
374 uint32_t nobj = 0;
375 char *ref_prefix = NULL;
376 size_t ref_prefixlen = 0;
377 char *path;
378 char *progress = NULL;
380 *pack_hash = NULL;
382 /*
383 * Prevent fetching of references that won't make any
384 * sense outside of the remote repository's context.
385 */
386 TAILQ_FOREACH(pe, wanted_refs, entry) {
387 const char *refname = pe->path;
388 if (strncmp(refname, "refs/got/", 9) == 0 ||
389 strncmp(refname, "got/", 4) == 0 ||
390 strncmp(refname, "refs/remotes/", 13) == 0 ||
391 strncmp(refname, "remotes/", 8) == 0)
392 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
395 if (!list_refs_only)
396 repo_path = got_repo_get_path_git_dir(repo);
398 for (i = 0; i < nitems(tmpfds); i++)
399 tmpfds[i] = -1;
401 TAILQ_INIT(&have_refs);
402 SIMPLEQ_INIT(&my_refs);
404 if (!mirror_references) {
405 if (asprintf(&ref_prefix, "refs/remotes/%s/",
406 remote_name) == -1)
407 return got_error_from_errno("asprintf");
408 ref_prefixlen = strlen(ref_prefix);
411 if (!list_refs_only) {
412 err = got_ref_list(&my_refs, repo, NULL,
413 got_ref_cmp_by_name, NULL);
414 if (err)
415 goto done;
418 SIMPLEQ_FOREACH(re, &my_refs, entry) {
419 struct got_object_id *id;
420 const char *refname;
422 if (got_ref_is_symbolic(re->ref))
423 continue;
425 refname = got_ref_get_name(re->ref);
427 if (mirror_references) {
428 char *name;
429 err = got_ref_resolve(&id, repo, re->ref);
430 if (err)
431 goto done;
432 name = strdup(refname);
433 if (name == NULL) {
434 err = got_error_from_errno("strdup");
435 goto done;
437 err = got_pathlist_append(&have_refs, name, id);
438 if (err)
439 goto done;
440 continue;
443 if (strncmp("refs/tags/", refname, 10) == 0) {
444 char *tagname;
446 err = got_ref_resolve(&id, repo, re->ref);
447 if (err)
448 goto done;
449 tagname = strdup(refname);
450 if (tagname == NULL) {
451 err = got_error_from_errno("strdup");
452 goto done;
454 err = got_pathlist_append(&have_refs, tagname, id);
455 if (err) {
456 free(tagname);
457 goto done;
461 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
462 char *branchname;
464 err = got_ref_resolve(&id, repo, re->ref);
465 if (err)
466 goto done;
468 if (asprintf(&branchname, "refs/heads/%s",
469 refname + ref_prefixlen) == -1) {
470 err = got_error_from_errno("asprintf");
471 goto done;
473 err = got_pathlist_append(&have_refs, branchname, id);
474 if (err) {
475 free(branchname);
476 goto done;
481 if (list_refs_only) {
482 packfd = got_opentempfd();
483 if (packfd == -1) {
484 err = got_error_from_errno("got_opentempfd");
485 goto done;
487 } else {
488 if (asprintf(&path, "%s/%s/fetching.pack",
489 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
490 err = got_error_from_errno("asprintf");
491 goto done;
493 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
494 free(path);
495 if (err)
496 goto done;
498 if (list_refs_only) {
499 idxfd = got_opentempfd();
500 if (idxfd == -1) {
501 err = got_error_from_errno("got_opentempfd");
502 goto done;
504 } else {
505 if (asprintf(&path, "%s/%s/fetching.idx",
506 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
507 err = got_error_from_errno("asprintf");
508 goto done;
510 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
511 free(path);
512 if (err)
513 goto done;
515 nidxfd = dup(idxfd);
516 if (nidxfd == -1) {
517 err = got_error_from_errno("dup");
518 goto done;
521 for (i = 0; i < nitems(tmpfds); i++) {
522 tmpfds[i] = got_opentempfd();
523 if (tmpfds[i] == -1) {
524 err = got_error_from_errno("got_opentempfd");
525 goto done;
529 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
530 err = got_error_from_errno("socketpair");
531 goto done;
534 fetchpid = fork();
535 if (fetchpid == -1) {
536 err = got_error_from_errno("fork");
537 goto done;
538 } else if (fetchpid == 0){
539 got_privsep_exec_child(imsg_fetchfds,
540 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
543 if (close(imsg_fetchfds[1]) != 0) {
544 err = got_error_from_errno("close");
545 goto done;
547 imsg_init(&fetchibuf, imsg_fetchfds[0]);
548 nfetchfd = dup(fetchfd);
549 if (nfetchfd == -1) {
550 err = got_error_from_errno("dup");
551 goto done;
553 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
554 fetch_all_branches, wanted_branches, wanted_refs,
555 list_refs_only, verbosity);
556 if (err != NULL)
557 goto done;
558 nfetchfd = -1;
559 npackfd = dup(packfd);
560 if (npackfd == -1) {
561 err = got_error_from_errno("dup");
562 goto done;
564 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
565 if (err != NULL)
566 goto done;
567 npackfd = -1;
569 packfile_size = 0;
570 progress = calloc(GOT_FETCH_PKTMAX, 1);
571 if (progress == NULL) {
572 err = got_error_from_errno("calloc");
573 goto done;
576 *pack_hash = calloc(1, sizeof(**pack_hash));
577 if (*pack_hash == NULL) {
578 err = got_error_from_errno("calloc");
579 goto done;
582 while (!done) {
583 struct got_object_id *id = NULL;
584 char *refname = NULL;
585 char *server_progress = NULL;
586 off_t packfile_size_cur = 0;
588 err = got_privsep_recv_fetch_progress(&done,
589 &id, &refname, symrefs, &server_progress,
590 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
591 if (err != NULL)
592 goto done;
593 if (!done && refname && id) {
594 err = got_pathlist_insert(NULL, refs, refname, id);
595 if (err)
596 goto done;
597 } else if (!done && server_progress) {
598 char *p;
599 /*
600 * XXX git-daemon tends to send batched output with
601 * lines spanning separate packets. Buffer progress
602 * output until we see a CR or LF to avoid giving
603 * partial lines of progress output to the callback.
604 */
605 if (strlcat(progress, server_progress,
606 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
607 progress[0] = '\0'; /* discard */
608 continue;
610 while ((p = strchr(progress, '\r')) != NULL ||
611 (p = strchr(progress, '\n')) != NULL) {
612 char *s;
613 size_t n;
614 char c = *p;
615 *p = '\0';
616 if (asprintf(&s, "%s%s", progress,
617 c == '\n' ? "\n" : "") == -1) {
618 err = got_error_from_errno("asprintf");
619 goto done;
621 err = progress_cb(progress_arg, s,
622 packfile_size_cur, 0, 0, 0, 0);
623 free(s);
624 if (err)
625 break;
626 n = strlen(progress);
627 if (n < GOT_FETCH_PKTMAX - 1) {
628 memmove(progress, &progress[n + 1],
629 GOT_FETCH_PKTMAX - n - 1);
630 } else
631 progress[0] = '\0';
633 free(server_progress);
634 if (err)
635 goto done;
636 } else if (!done && packfile_size_cur != packfile_size) {
637 err = progress_cb(progress_arg, NULL,
638 packfile_size_cur, 0, 0, 0, 0);
639 if (err)
640 break;
641 packfile_size = packfile_size_cur;
644 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
645 err = got_error_from_errno("waitpid");
646 goto done;
649 if (lseek(packfd, 0, SEEK_SET) == -1) {
650 err = got_error_from_errno("lseek");
651 goto done;
654 /* If zero data was fetched without error we are already up-to-date. */
655 if (packfile_size == 0) {
656 free(*pack_hash);
657 *pack_hash = NULL;
658 goto done;
659 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
660 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
661 goto done;
662 } else {
663 ssize_t n;
665 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
666 if (n == -1) {
667 err = got_error_from_errno("read");
668 goto done;
670 if (n != sizeof(pack_hdr)) {
671 err = got_error(GOT_ERR_IO);
672 goto done;
674 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
675 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
676 "bad pack file signature");
677 goto done;
679 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
680 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
681 "bad pack file version");
682 goto done;
684 nobj = be32toh(pack_hdr.nobjects);
685 if (nobj == 0 &&
686 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
687 return got_error_msg(GOT_ERR_BAD_PACKFILE,
688 "bad pack file with zero objects");
689 if (nobj != 0 &&
690 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
691 return got_error_msg(GOT_ERR_BAD_PACKFILE,
692 "empty pack file with non-zero object count");
695 /*
696 * If the pack file contains no objects, we may only need to update
697 * references in our repository. The caller will take care of that.
698 */
699 if (nobj == 0)
700 goto done;
702 if (lseek(packfd, 0, SEEK_SET) == -1) {
703 err = got_error_from_errno("lseek");
704 goto done;
707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
708 err = got_error_from_errno("socketpair");
709 goto done;
711 idxpid = fork();
712 if (idxpid == -1) {
713 err= got_error_from_errno("fork");
714 goto done;
715 } else if (idxpid == 0)
716 got_privsep_exec_child(imsg_idxfds,
717 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
718 if (close(imsg_idxfds[1]) != 0) {
719 err = got_error_from_errno("close");
720 goto done;
722 imsg_init(&idxibuf, imsg_idxfds[0]);
724 npackfd = dup(packfd);
725 if (npackfd == -1) {
726 err = got_error_from_errno("dup");
727 goto done;
729 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
730 npackfd);
731 if (err != NULL)
732 goto done;
733 npackfd = -1;
734 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
735 if (err != NULL)
736 goto done;
737 nidxfd = -1;
738 for (i = 0; i < nitems(tmpfds); i++) {
739 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
740 if (err != NULL)
741 goto done;
742 tmpfds[i] = -1;
744 done = 0;
745 while (!done) {
746 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
748 err = got_privsep_recv_index_progress(&done, &nobj_total,
749 &nobj_indexed, &nobj_loose, &nobj_resolved,
750 &idxibuf);
751 if (err != NULL)
752 goto done;
753 if (nobj_indexed != 0) {
754 err = progress_cb(progress_arg, NULL,
755 packfile_size, nobj_total,
756 nobj_indexed, nobj_loose, nobj_resolved);
757 if (err)
758 break;
760 imsg_clear(&idxibuf);
762 if (close(imsg_idxfds[0]) == -1) {
763 err = got_error_from_errno("close");
764 goto done;
766 if (waitpid(idxpid, &idxstatus, 0) == -1) {
767 err = got_error_from_errno("waitpid");
768 goto done;
771 err = got_object_id_str(&id_str, *pack_hash);
772 if (err)
773 goto done;
774 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
775 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
776 err = got_error_from_errno("asprintf");
777 goto done;
780 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
781 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
782 err = got_error_from_errno("asprintf");
783 goto done;
786 if (rename(tmppackpath, packpath) == -1) {
787 err = got_error_from_errno3("rename", tmppackpath, packpath);
788 goto done;
790 free(tmppackpath);
791 tmppackpath = NULL;
792 if (rename(tmpidxpath, idxpath) == -1) {
793 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
794 goto done;
796 free(tmpidxpath);
797 tmpidxpath = NULL;
799 done:
800 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
801 err = got_error_from_errno2("unlink", tmppackpath);
802 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
803 err = got_error_from_errno2("unlink", tmpidxpath);
804 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 if (packfd != -1 && close(packfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
811 err = got_error_from_errno("close");
812 for (i = 0; i < nitems(tmpfds); i++) {
813 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
814 err = got_error_from_errno("close");
816 free(tmppackpath);
817 free(tmpidxpath);
818 free(idxpath);
819 free(packpath);
820 free(ref_prefix);
821 free(progress);
823 TAILQ_FOREACH(pe, &have_refs, entry) {
824 free((char *)pe->path);
825 free(pe->data);
827 got_pathlist_free(&have_refs);
828 got_ref_list_free(&my_refs);
829 if (err) {
830 free(*pack_hash);
831 *pack_hash = NULL;
832 TAILQ_FOREACH(pe, refs, entry) {
833 free((void *)pe->path);
834 free(pe->data);
836 got_pathlist_free(refs);
837 TAILQ_FOREACH(pe, symrefs, entry) {
838 free((void *)pe->path);
839 free(pe->data);
841 got_pathlist_free(symrefs);
843 return err;