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;
95 *fetchpid = -1;
96 *fetchfd = -1;
98 if (port == NULL)
99 port = "22";
101 argv[0] = GOT_FETCH_PATH_SSH;
102 argv[1] = "-p";
103 argv[2] = (char *)port;
104 if (verbosity == -1) {
105 argv[3 + i++] = "-q";
106 } else {
107 /* ssh(1) allows up to 3 "-v" options. */
108 for (i = 0; i < MIN(3, verbosity); i++)
109 argv[3 + i] = "-v";
111 argv[3 + i] = "--";
112 argv[4 + i] = (char *)host;
113 argv[5 + i] = (char *)cmd;
114 argv[6 + i] = (char *)path;
115 argv[7 + 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 *server_path = strdup(p);
309 if (*server_path == NULL) {
310 err = got_error_from_errno("strdup");
311 goto done;
314 p = strrchr(p, '/');
315 if (!p || strlen(p) <= 1) {
316 err = got_error(GOT_ERR_PARSE_URI);
317 goto done;
319 p++;
320 n = strlen(p);
321 if (n == 0) {
322 err = got_error(GOT_ERR_PARSE_URI);
323 goto done;
325 if (hassuffix(p, ".git"))
326 n -= 4;
327 *repo_name = strndup(p, (p + n) - p);
328 if (*repo_name == NULL) {
329 err = got_error_from_errno("strndup");
330 goto done;
332 done:
333 if (err) {
334 free(*proto);
335 *proto = NULL;
336 free(*host);
337 *host = NULL;
338 free(*port);
339 *port = NULL;
340 free(*server_path);
341 *server_path = NULL;
342 free(*repo_name);
343 *repo_name = NULL;
345 return err;
348 const struct got_error*
349 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
350 struct got_pathlist_head *symrefs, const char *remote_name,
351 int mirror_references, int fetch_all_branches,
352 struct got_pathlist_head *wanted_branches,
353 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
354 int fetchfd, struct got_repository *repo,
355 got_fetch_progress_cb progress_cb, void *progress_arg)
357 int imsg_fetchfds[2], imsg_idxfds[2];
358 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
359 int tmpfds[3], i;
360 int fetchstatus, idxstatus, done = 0;
361 const struct got_error *err;
362 struct imsgbuf fetchibuf, idxibuf;
363 pid_t fetchpid, idxpid;
364 char *tmppackpath = NULL, *tmpidxpath = NULL;
365 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
366 const char *repo_path = NULL;
367 struct got_pathlist_head have_refs;
368 struct got_pathlist_entry *pe;
369 struct got_reflist_head my_refs;
370 struct got_reflist_entry *re;
371 off_t packfile_size = 0;
372 struct got_packfile_hdr pack_hdr;
373 uint32_t nobj = 0;
374 char *ref_prefix = NULL;
375 size_t ref_prefixlen = 0;
376 char *path;
377 char *progress = NULL;
379 *pack_hash = NULL;
381 /*
382 * Prevent fetching of references that won't make any
383 * sense outside of the remote repository's context.
384 */
385 TAILQ_FOREACH(pe, wanted_refs, entry) {
386 const char *refname = pe->path;
387 if (strncmp(refname, "refs/got/", 9) == 0 ||
388 strncmp(refname, "got/", 4) == 0 ||
389 strncmp(refname, "refs/remotes/", 13) == 0 ||
390 strncmp(refname, "remotes/", 8) == 0)
391 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
394 if (!list_refs_only)
395 repo_path = got_repo_get_path_git_dir(repo);
397 for (i = 0; i < nitems(tmpfds); i++)
398 tmpfds[i] = -1;
400 TAILQ_INIT(&have_refs);
401 SIMPLEQ_INIT(&my_refs);
403 if (!mirror_references) {
404 if (asprintf(&ref_prefix, "refs/remotes/%s/",
405 remote_name) == -1)
406 return got_error_from_errno("asprintf");
407 ref_prefixlen = strlen(ref_prefix);
410 if (!list_refs_only) {
411 err = got_ref_list(&my_refs, repo, NULL,
412 got_ref_cmp_by_name, NULL);
413 if (err)
414 goto done;
417 SIMPLEQ_FOREACH(re, &my_refs, entry) {
418 struct got_object_id *id;
419 const char *refname;
421 if (got_ref_is_symbolic(re->ref))
422 continue;
424 refname = got_ref_get_name(re->ref);
426 if (mirror_references) {
427 char *name;
428 err = got_ref_resolve(&id, repo, re->ref);
429 if (err)
430 goto done;
431 name = strdup(refname);
432 if (name == NULL) {
433 err = got_error_from_errno("strdup");
434 goto done;
436 err = got_pathlist_append(&have_refs, name, id);
437 if (err)
438 goto done;
439 continue;
442 if (strncmp("refs/tags/", refname, 10) == 0) {
443 char *tagname;
445 err = got_ref_resolve(&id, repo, re->ref);
446 if (err)
447 goto done;
448 tagname = strdup(refname);
449 if (tagname == NULL) {
450 err = got_error_from_errno("strdup");
451 goto done;
453 err = got_pathlist_append(&have_refs, tagname, id);
454 if (err) {
455 free(tagname);
456 goto done;
460 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
461 char *branchname;
463 err = got_ref_resolve(&id, repo, re->ref);
464 if (err)
465 goto done;
467 if (asprintf(&branchname, "refs/heads/%s",
468 refname + ref_prefixlen) == -1) {
469 err = got_error_from_errno("asprintf");
470 goto done;
472 err = got_pathlist_append(&have_refs, branchname, id);
473 if (err) {
474 free(branchname);
475 goto done;
480 if (list_refs_only) {
481 packfd = got_opentempfd();
482 if (packfd == -1) {
483 err = got_error_from_errno("got_opentempfd");
484 goto done;
486 } else {
487 if (asprintf(&path, "%s/%s/fetching.pack",
488 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
489 err = got_error_from_errno("asprintf");
490 goto done;
492 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
493 free(path);
494 if (err)
495 goto done;
497 if (list_refs_only) {
498 idxfd = got_opentempfd();
499 if (idxfd == -1) {
500 err = got_error_from_errno("got_opentempfd");
501 goto done;
503 } else {
504 if (asprintf(&path, "%s/%s/fetching.idx",
505 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
506 err = got_error_from_errno("asprintf");
507 goto done;
509 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
510 free(path);
511 if (err)
512 goto done;
514 nidxfd = dup(idxfd);
515 if (nidxfd == -1) {
516 err = got_error_from_errno("dup");
517 goto done;
520 for (i = 0; i < nitems(tmpfds); i++) {
521 tmpfds[i] = got_opentempfd();
522 if (tmpfds[i] == -1) {
523 err = got_error_from_errno("got_opentempfd");
524 goto done;
528 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
529 err = got_error_from_errno("socketpair");
530 goto done;
533 fetchpid = fork();
534 if (fetchpid == -1) {
535 err = got_error_from_errno("fork");
536 goto done;
537 } else if (fetchpid == 0){
538 got_privsep_exec_child(imsg_fetchfds,
539 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
542 if (close(imsg_fetchfds[1]) != 0) {
543 err = got_error_from_errno("close");
544 goto done;
546 imsg_init(&fetchibuf, imsg_fetchfds[0]);
547 nfetchfd = dup(fetchfd);
548 if (nfetchfd == -1) {
549 err = got_error_from_errno("dup");
550 goto done;
552 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
553 fetch_all_branches, wanted_branches, wanted_refs,
554 list_refs_only, verbosity);
555 if (err != NULL)
556 goto done;
557 nfetchfd = -1;
558 npackfd = dup(packfd);
559 if (npackfd == -1) {
560 err = got_error_from_errno("dup");
561 goto done;
563 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
564 if (err != NULL)
565 goto done;
566 npackfd = -1;
568 packfile_size = 0;
569 progress = calloc(GOT_FETCH_PKTMAX, 1);
570 if (progress == NULL) {
571 err = got_error_from_errno("calloc");
572 goto done;
575 *pack_hash = calloc(1, sizeof(**pack_hash));
576 if (*pack_hash == NULL) {
577 err = got_error_from_errno("calloc");
578 goto done;
581 while (!done) {
582 struct got_object_id *id = NULL;
583 char *refname = NULL;
584 char *server_progress = NULL;
585 off_t packfile_size_cur = 0;
587 err = got_privsep_recv_fetch_progress(&done,
588 &id, &refname, symrefs, &server_progress,
589 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
590 if (err != NULL)
591 goto done;
592 if (!done && refname && id) {
593 err = got_pathlist_insert(NULL, refs, refname, id);
594 if (err)
595 goto done;
596 } else if (!done && server_progress) {
597 char *p;
598 /*
599 * XXX git-daemon tends to send batched output with
600 * lines spanning separate packets. Buffer progress
601 * output until we see a CR or LF to avoid giving
602 * partial lines of progress output to the callback.
603 */
604 if (strlcat(progress, server_progress,
605 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
606 progress[0] = '\0'; /* discard */
607 continue;
609 while ((p = strchr(progress, '\r')) != NULL ||
610 (p = strchr(progress, '\n')) != NULL) {
611 char *s;
612 size_t n;
613 char c = *p;
614 *p = '\0';
615 if (asprintf(&s, "%s%s", progress,
616 c == '\n' ? "\n" : "") == -1) {
617 err = got_error_from_errno("asprintf");
618 goto done;
620 err = progress_cb(progress_arg, s,
621 packfile_size_cur, 0, 0, 0, 0);
622 free(s);
623 if (err)
624 break;
625 n = strlen(progress);
626 if (n < GOT_FETCH_PKTMAX - 1) {
627 memmove(progress, &progress[n + 1],
628 GOT_FETCH_PKTMAX - n - 1);
629 } else
630 progress[0] = '\0';
632 free(server_progress);
633 if (err)
634 goto done;
635 } else if (!done && packfile_size_cur != packfile_size) {
636 err = progress_cb(progress_arg, NULL,
637 packfile_size_cur, 0, 0, 0, 0);
638 if (err)
639 break;
640 packfile_size = packfile_size_cur;
643 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
644 err = got_error_from_errno("waitpid");
645 goto done;
648 if (lseek(packfd, 0, SEEK_SET) == -1) {
649 err = got_error_from_errno("lseek");
650 goto done;
653 /* If zero data was fetched without error we are already up-to-date. */
654 if (packfile_size == 0) {
655 free(*pack_hash);
656 *pack_hash = NULL;
657 goto done;
658 } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
659 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
660 goto done;
661 } else {
662 ssize_t n;
664 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
665 if (n == -1) {
666 err = got_error_from_errno("read");
667 goto done;
669 if (n != sizeof(pack_hdr)) {
670 err = got_error(GOT_ERR_IO);
671 goto done;
673 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
674 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
675 "bad pack file signature");
676 goto done;
678 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
679 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
680 "bad pack file version");
681 goto done;
683 nobj = betoh32(pack_hdr.nobjects);
684 if (nobj == 0 &&
685 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
686 return got_error_msg(GOT_ERR_BAD_PACKFILE,
687 "bad pack file with zero objects");
688 if (nobj != 0 &&
689 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
690 return got_error_msg(GOT_ERR_BAD_PACKFILE,
691 "empty pack file with non-zero object count");
694 /*
695 * If the pack file contains no objects, we may only need to update
696 * references in our repository. The caller will take care of that.
697 */
698 if (nobj == 0)
699 goto done;
701 if (lseek(packfd, 0, SEEK_SET) == -1) {
702 err = got_error_from_errno("lseek");
703 goto done;
706 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
707 err = got_error_from_errno("socketpair");
708 goto done;
710 idxpid = fork();
711 if (idxpid == -1) {
712 err= got_error_from_errno("fork");
713 goto done;
714 } else if (idxpid == 0)
715 got_privsep_exec_child(imsg_idxfds,
716 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
717 if (close(imsg_idxfds[1]) != 0) {
718 err = got_error_from_errno("close");
719 goto done;
721 imsg_init(&idxibuf, imsg_idxfds[0]);
723 npackfd = dup(packfd);
724 if (npackfd == -1) {
725 err = got_error_from_errno("dup");
726 goto done;
728 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
729 npackfd);
730 if (err != NULL)
731 goto done;
732 npackfd = -1;
733 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
734 if (err != NULL)
735 goto done;
736 nidxfd = -1;
737 for (i = 0; i < nitems(tmpfds); i++) {
738 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
739 if (err != NULL)
740 goto done;
741 tmpfds[i] = -1;
743 done = 0;
744 while (!done) {
745 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
747 err = got_privsep_recv_index_progress(&done, &nobj_total,
748 &nobj_indexed, &nobj_loose, &nobj_resolved,
749 &idxibuf);
750 if (err != NULL)
751 goto done;
752 if (nobj_indexed != 0) {
753 err = progress_cb(progress_arg, NULL,
754 packfile_size, nobj_total,
755 nobj_indexed, nobj_loose, nobj_resolved);
756 if (err)
757 break;
759 imsg_clear(&idxibuf);
761 if (close(imsg_idxfds[0]) == -1) {
762 err = got_error_from_errno("close");
763 goto done;
765 if (waitpid(idxpid, &idxstatus, 0) == -1) {
766 err = got_error_from_errno("waitpid");
767 goto done;
770 err = got_object_id_str(&id_str, *pack_hash);
771 if (err)
772 goto done;
773 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
774 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
775 err = got_error_from_errno("asprintf");
776 goto done;
779 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
780 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
781 err = got_error_from_errno("asprintf");
782 goto done;
785 if (rename(tmppackpath, packpath) == -1) {
786 err = got_error_from_errno3("rename", tmppackpath, packpath);
787 goto done;
789 free(tmppackpath);
790 tmppackpath = NULL;
791 if (rename(tmpidxpath, idxpath) == -1) {
792 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
793 goto done;
795 free(tmpidxpath);
796 tmpidxpath = NULL;
798 done:
799 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
800 err = got_error_from_errno2("unlink", tmppackpath);
801 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
802 err = got_error_from_errno2("unlink", tmpidxpath);
803 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
804 err = got_error_from_errno("close");
805 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
806 err = got_error_from_errno("close");
807 if (packfd != -1 && close(packfd) == -1 && err == NULL)
808 err = got_error_from_errno("close");
809 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
810 err = got_error_from_errno("close");
811 for (i = 0; i < nitems(tmpfds); i++) {
812 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
813 err = got_error_from_errno("close");
815 free(tmppackpath);
816 free(tmpidxpath);
817 free(idxpath);
818 free(packpath);
819 free(ref_prefix);
820 free(progress);
822 TAILQ_FOREACH(pe, &have_refs, entry) {
823 free((char *)pe->path);
824 free(pe->data);
826 got_pathlist_free(&have_refs);
827 got_ref_list_free(&my_refs);
828 if (err) {
829 free(*pack_hash);
830 *pack_hash = NULL;
831 TAILQ_FOREACH(pe, refs, entry) {
832 free((void *)pe->path);
833 free(pe->data);
835 got_pathlist_free(refs);
836 TAILQ_FOREACH(pe, symrefs, entry) {
837 free((void *)pe->path);
838 free(pe->data);
840 got_pathlist_free(symrefs);
842 return err;