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(int *fetchfd, const char *host, const char *port, const char *path,
87 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;
95 *fetchfd = -1;
97 if (port == NULL)
98 port = "22";
100 argv[0] = GOT_FETCH_PATH_SSH;
101 argv[1] = "-p";
102 argv[2] = (char *)port;
103 if (verbosity == -1) {
104 argv[3 + i++] = "-q";
105 } else {
106 /* ssh(1) allows up to 3 "-v" options. */
107 for (i = 0; i < MIN(3, verbosity); i++)
108 argv[3 + i] = "-v";
110 argv[3 + i] = "--";
111 argv[4 + i] = (char *)host;
112 argv[5 + i] = (char *)cmd;
113 argv[6 + i] = (char *)path;
114 argv[7 + 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 *fetchfd = pfd[1];
139 return NULL;
143 static const struct got_error *
144 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
145 const char *direction)
147 const struct got_error *err = NULL;
148 struct addrinfo hints, *servinfo, *p;
149 char *cmd = NULL, *pkt = NULL;
150 int fd = -1, totlen, r, eaicode;
152 *fetchfd = -1;
154 if (port == NULL)
155 port = GOT_DEFAULT_GIT_PORT_STR;
157 memset(&hints, 0, sizeof hints);
158 hints.ai_family = AF_UNSPEC;
159 hints.ai_socktype = SOCK_STREAM;
160 eaicode = getaddrinfo(host, port, &hints, &servinfo);
161 if (eaicode) {
162 char msg[512];
163 snprintf(msg, sizeof(msg), "%s: %s", host,
164 gai_strerror(eaicode));
165 return got_error_msg(GOT_ERR_ADDRINFO, msg);
168 for (p = servinfo; p != NULL; p = p->ai_next) {
169 if ((fd = socket(p->ai_family, p->ai_socktype,
170 p->ai_protocol)) == -1)
171 continue;
172 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
173 break;
174 err = got_error_from_errno("connect");
175 close(fd);
177 if (p == NULL)
178 goto done;
180 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
181 err = got_error_from_errno("asprintf");
182 goto done;
184 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
185 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
186 err = got_error_from_errno("asprintf");
187 goto done;
189 r = write(fd, pkt, strlen(pkt) + 1);
190 if (r == -1) {
191 err = got_error_from_errno("write");
192 goto done;
194 if (asprintf(&pkt, "host=%s", host) == -1) {
195 err = got_error_from_errno("asprintf");
196 goto done;
198 r = write(fd, pkt, strlen(pkt) + 1);
199 if (r == -1) {
200 err = got_error_from_errno("write");
201 goto done;
203 done:
204 free(cmd);
205 free(pkt);
206 if (err) {
207 if (fd != -1)
208 close(fd);
209 } else
210 *fetchfd = fd;
211 return err;
214 const struct got_error *
215 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
216 const char *port, const char *server_path, int verbosity)
218 const struct got_error *err = NULL;
220 *fetchfd = -1;
222 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
223 err = dial_ssh(fetchfd, host, port, server_path, "upload",
224 verbosity);
225 else if (strcmp(proto, "git") == 0)
226 err = dial_git(fetchfd, host, port, server_path, "upload");
227 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
228 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
229 else
230 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
231 return err;
234 const struct got_error *
235 got_fetch_parse_uri(char **proto, char **host, char **port,
236 char **server_path, char **repo_name, const char *uri)
238 const struct got_error *err = NULL;
239 char *s, *p, *q;
240 int n;
242 *proto = *host = *port = *server_path = *repo_name = NULL;
244 p = strstr(uri, "://");
245 if (!p) {
246 /* Try parsing Git's "scp" style URL syntax. */
247 *proto = strdup("ssh");
248 if (proto == NULL) {
249 err = got_error_from_errno("strdup");
250 goto done;
252 s = (char *)uri;
253 q = strchr(s, ':');
254 if (q == NULL) {
255 err = got_error(GOT_ERR_PARSE_URI);
256 goto done;
258 /* No slashes allowed before first colon. */
259 p = strchr(s, '/');
260 if (p && q > p) {
261 err = got_error(GOT_ERR_PARSE_URI);
262 goto done;
264 *host = strndup(s, q - s);
265 if (*host == NULL) {
266 err = got_error_from_errno("strndup");
267 goto done;
269 p = q + 1;
270 } else {
271 *proto = strndup(uri, p - uri);
272 if (proto == NULL) {
273 err = got_error_from_errno("strndup");
274 goto done;
276 s = p + 3;
278 p = strstr(s, "/");
279 if (p == NULL || strlen(p) == 1) {
280 err = got_error(GOT_ERR_PARSE_URI);
281 goto done;
284 q = memchr(s, ':', p - s);
285 if (q) {
286 *host = strndup(s, q - s);
287 if (*host == NULL) {
288 err = got_error_from_errno("strndup");
289 goto done;
291 *port = strndup(q + 1, p - (q + 1));
292 if (*port == NULL) {
293 err = got_error_from_errno("strndup");
294 goto done;
296 } else {
297 *host = strndup(s, p - s);
298 if (*host == NULL) {
299 err = got_error_from_errno("strndup");
300 goto done;
305 *server_path = strdup(p);
306 if (*server_path == NULL) {
307 err = got_error_from_errno("strdup");
308 goto done;
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 static const struct got_error *
346 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
348 SHA1_CTX ctx;
349 uint8_t hexpect[SHA1_DIGEST_LENGTH];
350 uint8_t buf[32 * 1024];
351 ssize_t n, r, nr;
353 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
354 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
356 n = 0;
357 SHA1Init(&ctx);
358 while (n < sz - 20) {
359 nr = sizeof(buf);
360 if (sz - n - 20 < sizeof(buf))
361 nr = sz - n - 20;
362 r = read(fd, buf, nr);
363 if (r == -1)
364 return got_error_from_errno("read");
365 if (r != nr)
366 return got_error_msg(GOT_ERR_BAD_PACKFILE,
367 "short pack file");
368 SHA1Update(&ctx, buf, nr);
369 n += r;
371 SHA1Final(hcomp, &ctx);
373 r = read(fd, hexpect, sizeof(hexpect));
374 if (r == -1)
375 return got_error_from_errno("read");
376 if (r != sizeof(hexpect))
377 return got_error_msg(GOT_ERR_BAD_PACKFILE,
378 "short pack file");
380 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
381 return got_error_msg(GOT_ERR_BAD_PACKFILE,
382 "packfile checksum mismatch");
384 return NULL;
387 const struct got_error*
388 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
389 struct got_pathlist_head *symrefs, const char *remote_name,
390 int mirror_references, int fetch_all_branches,
391 struct got_pathlist_head *wanted_branches, int fetchfd,
392 struct got_repository *repo,
393 got_fetch_progress_cb progress_cb, void *progress_arg)
395 int imsg_fetchfds[2], imsg_idxfds[2];
396 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
397 int tmpfds[3], i;
398 int fetchstatus, idxstatus, done = 0;
399 const struct got_error *err;
400 struct imsgbuf fetchibuf, idxibuf;
401 pid_t fetchpid, idxpid;
402 char *tmppackpath = NULL, *tmpidxpath = NULL;
403 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
404 const char *repo_path = got_repo_get_path_git_dir(repo);
405 struct got_pathlist_head have_refs;
406 struct got_pathlist_entry *pe;
407 struct got_reflist_head my_refs;
408 struct got_reflist_entry *re;
409 off_t packfile_size = 0;
410 char *ref_prefix = NULL;
411 size_t ref_prefixlen = 0;
412 char *path;
413 char *progress = NULL;
415 *pack_hash = NULL;
416 for (i = 0; i < nitems(tmpfds); i++)
417 tmpfds[i] = -1;
419 TAILQ_INIT(&have_refs);
420 SIMPLEQ_INIT(&my_refs);
422 if (!mirror_references) {
423 if (asprintf(&ref_prefix, "refs/remotes/%s/",
424 remote_name) == -1)
425 return got_error_from_errno("asprintf");
426 ref_prefixlen = strlen(ref_prefix);
429 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
430 if (err)
431 goto done;
433 SIMPLEQ_FOREACH(re, &my_refs, entry) {
434 struct got_object_id *id;
435 const char *refname;
437 if (got_ref_is_symbolic(re->ref))
438 continue;
440 refname = got_ref_get_name(re->ref);
442 if (mirror_references) {
443 char *name;
444 err = got_ref_resolve(&id, repo, re->ref);
445 if (err)
446 goto done;
447 name = strdup(refname);
448 if (name == NULL) {
449 err = got_error_from_errno("strdup");
450 goto done;
452 err = got_pathlist_append(&have_refs, name, id);
453 if (err)
454 goto done;
455 continue;
458 if (strncmp("refs/tags/", refname, 10) == 0) {
459 char *tagname;
461 err = got_ref_resolve(&id, repo, re->ref);
462 if (err)
463 goto done;
464 tagname = strdup(refname);
465 if (tagname == NULL) {
466 err = got_error_from_errno("strdup");
467 goto done;
469 err = got_pathlist_append(&have_refs, tagname, id);
470 if (err) {
471 free(tagname);
472 goto done;
476 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
477 char *branchname;
479 err = got_ref_resolve(&id, repo, re->ref);
480 if (err)
481 goto done;
483 if (asprintf(&branchname, "refs/heads/%s",
484 refname + ref_prefixlen) == -1) {
485 err = got_error_from_errno("asprintf");
486 goto done;
488 err = got_pathlist_append(&have_refs, branchname, id);
489 if (err) {
490 free(branchname);
491 goto done;
496 if (asprintf(&path, "%s/%s/fetching.pack",
497 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
498 err = got_error_from_errno("asprintf");
499 goto done;
501 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
502 free(path);
503 if (err)
504 goto done;
505 npackfd = dup(packfd);
506 if (npackfd == -1) {
507 err = got_error_from_errno("dup");
508 goto done;
510 if (asprintf(&path, "%s/%s/fetching.idx",
511 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
512 err = got_error_from_errno("asprintf");
513 goto done;
515 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
516 free(path);
517 if (err)
518 goto done;
519 nidxfd = dup(idxfd);
520 if (nidxfd == -1) {
521 err = got_error_from_errno("dup");
522 goto done;
525 for (i = 0; i < nitems(tmpfds); i++) {
526 tmpfds[i] = got_opentempfd();
527 if (tmpfds[i] == -1) {
528 err = got_error_from_errno("got_opentempfd");
529 goto done;
533 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
534 err = got_error_from_errno("socketpair");
535 goto done;
538 fetchpid = fork();
539 if (fetchpid == -1) {
540 err = got_error_from_errno("fork");
541 goto done;
542 } else if (fetchpid == 0){
543 got_privsep_exec_child(imsg_fetchfds,
544 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
547 if (close(imsg_fetchfds[1]) != 0) {
548 err = got_error_from_errno("close");
549 goto done;
551 imsg_init(&fetchibuf, imsg_fetchfds[0]);
552 nfetchfd = dup(fetchfd);
553 if (nfetchfd == -1) {
554 err = got_error_from_errno("dup");
555 goto done;
557 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
558 fetch_all_branches, wanted_branches);
559 if (err != NULL)
560 goto done;
561 nfetchfd = -1;
562 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
563 if (err != NULL)
564 goto done;
565 npackfd = dup(packfd);
566 if (npackfd == -1) {
567 err = got_error_from_errno("dup");
568 goto done;
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;
577 while (!done) {
578 struct got_object_id *id = NULL;
579 char *refname = NULL;
580 char *server_progress = NULL;
581 off_t packfile_size_cur = 0;
583 err = got_privsep_recv_fetch_progress(&done,
584 &id, &refname, symrefs, &server_progress,
585 &packfile_size_cur, &fetchibuf);
586 if (err != NULL)
587 goto done;
588 if (done) {
589 if (packfile_size > 0)
590 *pack_hash = id;
591 else
592 free(id);
593 } else if (refname && id) {
594 err = got_pathlist_append(refs, refname, id);
595 if (err)
596 goto done;
597 } else if (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 (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 zero data was fetched without error we are already up-to-date. */
650 if (packfile_size == 0)
651 goto done;
653 if (lseek(packfd, 0, SEEK_SET) == -1) {
654 err = got_error_from_errno("lseek");
655 goto done;
657 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
658 if (err)
659 goto done;
661 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
662 err = got_error_from_errno("socketpair");
663 goto done;
665 idxpid = fork();
666 if (idxpid == -1) {
667 err= got_error_from_errno("fork");
668 goto done;
669 } else if (idxpid == 0)
670 got_privsep_exec_child(imsg_idxfds,
671 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
672 if (close(imsg_idxfds[1]) != 0) {
673 err = got_error_from_errno("close");
674 goto done;
676 imsg_init(&idxibuf, imsg_idxfds[0]);
678 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
679 npackfd);
680 if (err != NULL)
681 goto done;
682 npackfd = -1;
683 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
684 if (err != NULL)
685 goto done;
686 nidxfd = -1;
687 for (i = 0; i < nitems(tmpfds); i++) {
688 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
689 if (err != NULL)
690 goto done;
691 tmpfds[i] = -1;
693 done = 0;
694 while (!done) {
695 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
697 err = got_privsep_recv_index_progress(&done, &nobj_total,
698 &nobj_indexed, &nobj_loose, &nobj_resolved,
699 &idxibuf);
700 if (err != NULL)
701 goto done;
702 if (nobj_indexed != 0) {
703 err = progress_cb(progress_arg, NULL,
704 packfile_size, nobj_total,
705 nobj_indexed, nobj_loose, nobj_resolved);
706 if (err)
707 break;
709 imsg_clear(&idxibuf);
711 if (close(imsg_idxfds[0]) == -1) {
712 err = got_error_from_errno("close");
713 goto done;
715 if (waitpid(idxpid, &idxstatus, 0) == -1) {
716 err = got_error_from_errno("waitpid");
717 goto done;
720 err = got_object_id_str(&id_str, *pack_hash);
721 if (err)
722 goto done;
723 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
724 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
725 err = got_error_from_errno("asprintf");
726 goto done;
729 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
730 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
731 err = got_error_from_errno("asprintf");
732 goto done;
735 if (rename(tmppackpath, packpath) == -1) {
736 err = got_error_from_errno3("rename", tmppackpath, packpath);
737 goto done;
739 free(tmppackpath);
740 tmppackpath = NULL;
741 if (rename(tmpidxpath, idxpath) == -1) {
742 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
743 goto done;
745 free(tmpidxpath);
746 tmpidxpath = NULL;
748 done:
749 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
750 err = got_error_from_errno2("unlink", tmppackpath);
751 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
752 err = got_error_from_errno2("unlink", tmpidxpath);
753 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
754 err = got_error_from_errno("close");
755 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
756 err = got_error_from_errno("close");
757 if (packfd != -1 && close(packfd) == -1 && err == NULL)
758 err = got_error_from_errno("close");
759 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
760 err = got_error_from_errno("close");
761 for (i = 0; i < nitems(tmpfds); i++) {
762 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
763 err = got_error_from_errno("close");
765 free(tmppackpath);
766 free(tmpidxpath);
767 free(idxpath);
768 free(packpath);
769 free(ref_prefix);
770 free(progress);
772 TAILQ_FOREACH(pe, &have_refs, entry) {
773 free((char *)pe->path);
774 free(pe->data);
776 got_pathlist_free(&have_refs);
777 got_ref_list_free(&my_refs);
778 if (err) {
779 free(*pack_hash);
780 *pack_hash = NULL;
781 TAILQ_FOREACH(pe, refs, entry) {
782 free((void *)pe->path);
783 free(pe->data);
785 got_pathlist_free(refs);
786 TAILQ_FOREACH(pe, symrefs, entry) {
787 free((void *)pe->path);
788 free(pe->data);
790 got_pathlist_free(symrefs);
792 return err;