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 static const struct got_error *
349 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
351 SHA1_CTX ctx;
352 uint8_t hexpect[SHA1_DIGEST_LENGTH];
353 uint8_t buf[32 * 1024];
354 ssize_t n, r, nr;
356 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
357 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
359 n = 0;
360 SHA1Init(&ctx);
361 while (n < sz - 20) {
362 nr = sizeof(buf);
363 if (sz - n - 20 < sizeof(buf))
364 nr = sz - n - 20;
365 r = read(fd, buf, nr);
366 if (r == -1)
367 return got_error_from_errno("read");
368 if (r != nr)
369 return got_error_msg(GOT_ERR_BAD_PACKFILE,
370 "short pack file");
371 SHA1Update(&ctx, buf, nr);
372 n += r;
374 SHA1Final(hcomp, &ctx);
376 r = read(fd, hexpect, sizeof(hexpect));
377 if (r == -1)
378 return got_error_from_errno("read");
379 if (r != sizeof(hexpect))
380 return got_error_msg(GOT_ERR_BAD_PACKFILE,
381 "short pack file");
383 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
384 return got_error_msg(GOT_ERR_BAD_PACKFILE,
385 "packfile checksum mismatch");
387 return NULL;
390 const struct got_error*
391 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
392 struct got_pathlist_head *symrefs, const char *remote_name,
393 int mirror_references, int fetch_all_branches,
394 struct got_pathlist_head *wanted_branches, int list_refs_only,
395 int fetchfd, struct got_repository *repo,
396 got_fetch_progress_cb progress_cb, void *progress_arg)
398 int imsg_fetchfds[2], imsg_idxfds[2];
399 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
400 int tmpfds[3], i;
401 int fetchstatus, idxstatus, done = 0;
402 const struct got_error *err;
403 struct imsgbuf fetchibuf, idxibuf;
404 pid_t fetchpid, idxpid;
405 char *tmppackpath = NULL, *tmpidxpath = NULL;
406 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
407 const char *repo_path = NULL;
408 struct got_pathlist_head have_refs;
409 struct got_pathlist_entry *pe;
410 struct got_reflist_head my_refs;
411 struct got_reflist_entry *re;
412 off_t packfile_size = 0;
413 struct got_packfile_hdr pack_hdr;
414 uint32_t nobj = 0;
415 char *ref_prefix = NULL;
416 size_t ref_prefixlen = 0;
417 char *path;
418 char *progress = NULL;
420 if (!list_refs_only)
421 repo_path = got_repo_get_path_git_dir(repo);
423 *pack_hash = NULL;
424 for (i = 0; i < nitems(tmpfds); i++)
425 tmpfds[i] = -1;
427 TAILQ_INIT(&have_refs);
428 SIMPLEQ_INIT(&my_refs);
430 if (!mirror_references) {
431 if (asprintf(&ref_prefix, "refs/remotes/%s/",
432 remote_name) == -1)
433 return got_error_from_errno("asprintf");
434 ref_prefixlen = strlen(ref_prefix);
437 if (!list_refs_only) {
438 err = got_ref_list(&my_refs, repo, NULL,
439 got_ref_cmp_by_name, NULL);
440 if (err)
441 goto done;
444 SIMPLEQ_FOREACH(re, &my_refs, entry) {
445 struct got_object_id *id;
446 const char *refname;
448 if (got_ref_is_symbolic(re->ref))
449 continue;
451 refname = got_ref_get_name(re->ref);
453 if (mirror_references) {
454 char *name;
455 err = got_ref_resolve(&id, repo, re->ref);
456 if (err)
457 goto done;
458 name = strdup(refname);
459 if (name == NULL) {
460 err = got_error_from_errno("strdup");
461 goto done;
463 err = got_pathlist_append(&have_refs, name, id);
464 if (err)
465 goto done;
466 continue;
469 if (strncmp("refs/tags/", refname, 10) == 0) {
470 char *tagname;
472 err = got_ref_resolve(&id, repo, re->ref);
473 if (err)
474 goto done;
475 tagname = strdup(refname);
476 if (tagname == NULL) {
477 err = got_error_from_errno("strdup");
478 goto done;
480 err = got_pathlist_append(&have_refs, tagname, id);
481 if (err) {
482 free(tagname);
483 goto done;
487 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
488 char *branchname;
490 err = got_ref_resolve(&id, repo, re->ref);
491 if (err)
492 goto done;
494 if (asprintf(&branchname, "refs/heads/%s",
495 refname + ref_prefixlen) == -1) {
496 err = got_error_from_errno("asprintf");
497 goto done;
499 err = got_pathlist_append(&have_refs, branchname, id);
500 if (err) {
501 free(branchname);
502 goto done;
507 if (list_refs_only) {
508 packfd = got_opentempfd();
509 if (packfd == -1) {
510 err = got_error_from_errno("got_opentempfd");
511 goto done;
513 } else {
514 if (asprintf(&path, "%s/%s/fetching.pack",
515 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
516 err = got_error_from_errno("asprintf");
517 goto done;
519 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
520 free(path);
521 if (err)
522 goto done;
524 if (list_refs_only) {
525 idxfd = got_opentempfd();
526 if (idxfd == -1) {
527 err = got_error_from_errno("got_opentempfd");
528 goto done;
530 } else {
531 if (asprintf(&path, "%s/%s/fetching.idx",
532 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
533 err = got_error_from_errno("asprintf");
534 goto done;
536 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
537 free(path);
538 if (err)
539 goto done;
541 nidxfd = dup(idxfd);
542 if (nidxfd == -1) {
543 err = got_error_from_errno("dup");
544 goto done;
547 for (i = 0; i < nitems(tmpfds); i++) {
548 tmpfds[i] = got_opentempfd();
549 if (tmpfds[i] == -1) {
550 err = got_error_from_errno("got_opentempfd");
551 goto done;
555 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
556 err = got_error_from_errno("socketpair");
557 goto done;
560 fetchpid = fork();
561 if (fetchpid == -1) {
562 err = got_error_from_errno("fork");
563 goto done;
564 } else if (fetchpid == 0){
565 got_privsep_exec_child(imsg_fetchfds,
566 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
569 if (close(imsg_fetchfds[1]) != 0) {
570 err = got_error_from_errno("close");
571 goto done;
573 imsg_init(&fetchibuf, imsg_fetchfds[0]);
574 nfetchfd = dup(fetchfd);
575 if (nfetchfd == -1) {
576 err = got_error_from_errno("dup");
577 goto done;
579 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
580 fetch_all_branches, wanted_branches, list_refs_only);
581 if (err != NULL)
582 goto done;
583 nfetchfd = -1;
584 npackfd = dup(packfd);
585 if (npackfd == -1) {
586 err = got_error_from_errno("dup");
587 goto done;
589 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
590 if (err != NULL)
591 goto done;
592 npackfd = -1;
594 packfile_size = 0;
595 progress = calloc(GOT_FETCH_PKTMAX, 1);
596 if (progress == NULL) {
597 err = got_error_from_errno("calloc");
598 goto done;
600 while (!done) {
601 struct got_object_id *id = NULL;
602 char *refname = NULL;
603 char *server_progress = NULL;
604 off_t packfile_size_cur = 0;
606 err = got_privsep_recv_fetch_progress(&done,
607 &id, &refname, symrefs, &server_progress,
608 &packfile_size_cur, &fetchibuf);
609 if (err != NULL)
610 goto done;
611 if (done) {
612 if (packfile_size > 0)
613 *pack_hash = id;
614 else
615 free(id);
616 } else if (refname && id) {
617 err = got_pathlist_insert(NULL, refs, refname, id);
618 if (err)
619 goto done;
620 } else if (server_progress) {
621 char *p;
622 /*
623 * XXX git-daemon tends to send batched output with
624 * lines spanning separate packets. Buffer progress
625 * output until we see a CR or LF to avoid giving
626 * partial lines of progress output to the callback.
627 */
628 if (strlcat(progress, server_progress,
629 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
630 progress[0] = '\0'; /* discard */
631 continue;
633 while ((p = strchr(progress, '\r')) != NULL ||
634 (p = strchr(progress, '\n')) != NULL) {
635 char *s;
636 size_t n;
637 char c = *p;
638 *p = '\0';
639 if (asprintf(&s, "%s%s", progress,
640 c == '\n' ? "\n" : "") == -1) {
641 err = got_error_from_errno("asprintf");
642 goto done;
644 err = progress_cb(progress_arg, s,
645 packfile_size_cur, 0, 0, 0, 0);
646 free(s);
647 if (err)
648 break;
649 n = strlen(progress);
650 if (n < GOT_FETCH_PKTMAX - 1) {
651 memmove(progress, &progress[n + 1],
652 GOT_FETCH_PKTMAX - n - 1);
653 } else
654 progress[0] = '\0';
656 free(server_progress);
657 if (err)
658 goto done;
659 } else if (packfile_size_cur != packfile_size) {
660 err = progress_cb(progress_arg, NULL,
661 packfile_size_cur, 0, 0, 0, 0);
662 if (err)
663 break;
664 packfile_size = packfile_size_cur;
667 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
668 err = got_error_from_errno("waitpid");
669 goto done;
672 if (lseek(packfd, 0, SEEK_SET) == -1) {
673 err = got_error_from_errno("lseek");
674 goto done;
677 /* If zero data was fetched without error we are already up-to-date. */
678 if (packfile_size == 0)
679 goto done;
680 else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
681 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
682 goto done;
683 } else {
684 ssize_t n;
686 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
687 if (n == -1) {
688 err = got_error_from_errno("read");
689 goto done;
691 if (n != sizeof(pack_hdr)) {
692 err = got_error(GOT_ERR_IO);
693 goto done;
695 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
696 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
697 "bad pack file signature");
698 goto done;
700 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
701 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
702 "bad pack file version");
703 goto done;
705 nobj = betoh32(pack_hdr.nobjects);
706 if (nobj == 0 &&
707 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
708 return got_error_msg(GOT_ERR_BAD_PACKFILE,
709 "bad pack file with zero objects");
710 if (nobj != 0 &&
711 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
712 return got_error_msg(GOT_ERR_BAD_PACKFILE,
713 "empty pack file with non-zero object count");
716 /*
717 * If the pack file contains no objects, we may only need to update
718 * references in our repository. The caller will take care of that.
719 */
720 if (nobj == 0)
721 goto done;
723 if (lseek(packfd, 0, SEEK_SET) == -1) {
724 err = got_error_from_errno("lseek");
725 goto done;
728 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
729 if (err)
730 goto done;
732 if (lseek(packfd, 0, SEEK_SET) == -1) {
733 err = got_error_from_errno("lseek");
734 goto done;
737 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
738 err = got_error_from_errno("socketpair");
739 goto done;
741 idxpid = fork();
742 if (idxpid == -1) {
743 err= got_error_from_errno("fork");
744 goto done;
745 } else if (idxpid == 0)
746 got_privsep_exec_child(imsg_idxfds,
747 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
748 if (close(imsg_idxfds[1]) != 0) {
749 err = got_error_from_errno("close");
750 goto done;
752 imsg_init(&idxibuf, imsg_idxfds[0]);
754 npackfd = dup(packfd);
755 if (npackfd == -1) {
756 err = got_error_from_errno("dup");
757 goto done;
759 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
760 npackfd);
761 if (err != NULL)
762 goto done;
763 npackfd = -1;
764 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
765 if (err != NULL)
766 goto done;
767 nidxfd = -1;
768 for (i = 0; i < nitems(tmpfds); i++) {
769 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
770 if (err != NULL)
771 goto done;
772 tmpfds[i] = -1;
774 done = 0;
775 while (!done) {
776 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
778 err = got_privsep_recv_index_progress(&done, &nobj_total,
779 &nobj_indexed, &nobj_loose, &nobj_resolved,
780 &idxibuf);
781 if (err != NULL)
782 goto done;
783 if (nobj_indexed != 0) {
784 err = progress_cb(progress_arg, NULL,
785 packfile_size, nobj_total,
786 nobj_indexed, nobj_loose, nobj_resolved);
787 if (err)
788 break;
790 imsg_clear(&idxibuf);
792 if (close(imsg_idxfds[0]) == -1) {
793 err = got_error_from_errno("close");
794 goto done;
796 if (waitpid(idxpid, &idxstatus, 0) == -1) {
797 err = got_error_from_errno("waitpid");
798 goto done;
801 err = got_object_id_str(&id_str, *pack_hash);
802 if (err)
803 goto done;
804 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
805 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
806 err = got_error_from_errno("asprintf");
807 goto done;
810 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
811 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
812 err = got_error_from_errno("asprintf");
813 goto done;
816 if (rename(tmppackpath, packpath) == -1) {
817 err = got_error_from_errno3("rename", tmppackpath, packpath);
818 goto done;
820 free(tmppackpath);
821 tmppackpath = NULL;
822 if (rename(tmpidxpath, idxpath) == -1) {
823 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
824 goto done;
826 free(tmpidxpath);
827 tmpidxpath = NULL;
829 done:
830 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
831 err = got_error_from_errno2("unlink", tmppackpath);
832 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
833 err = got_error_from_errno2("unlink", tmpidxpath);
834 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
835 err = got_error_from_errno("close");
836 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
837 err = got_error_from_errno("close");
838 if (packfd != -1 && close(packfd) == -1 && err == NULL)
839 err = got_error_from_errno("close");
840 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
841 err = got_error_from_errno("close");
842 for (i = 0; i < nitems(tmpfds); i++) {
843 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
844 err = got_error_from_errno("close");
846 free(tmppackpath);
847 free(tmpidxpath);
848 free(idxpath);
849 free(packpath);
850 free(ref_prefix);
851 free(progress);
853 TAILQ_FOREACH(pe, &have_refs, entry) {
854 free((char *)pe->path);
855 free(pe->data);
857 got_pathlist_free(&have_refs);
858 got_ref_list_free(&my_refs);
859 if (err) {
860 free(*pack_hash);
861 *pack_hash = NULL;
862 TAILQ_FOREACH(pe, refs, entry) {
863 free((void *)pe->path);
864 free(pe->data);
866 got_pathlist_free(refs);
867 TAILQ_FOREACH(pe, symrefs, entry) {
868 free((void *)pe->path);
869 free(pe->data);
871 got_pathlist_free(symrefs);
873 return err;