Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@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/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.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_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, *pack_fds_tmp;
257 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 if (pack_fds_tmp == NULL)
259 return got_error_from_errno("calloc");
260 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 if (*pack_fds == NULL) {
262 free(pack_fds_tmp);
263 return got_error_from_errno("calloc");
266 /*
267 * got_repo_pack_fds_close will try to close all of the
268 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 * a value from got_opentempfd(), which would result in a close(0) if
270 * we do not initialize to -1 here.
271 */
272 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
273 pack_fds_tmp[i] = -1;
275 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
276 pack_fds_tmp[i] = got_opentempfd();
277 if (pack_fds_tmp[i] == -1) {
278 err = got_error_from_errno("got_opentempfd");
279 got_repo_pack_fds_close(pack_fds_tmp);
280 return err;
283 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
284 return err;
287 const struct got_error *
288 got_repo_pack_fds_close(int *pack_fds)
290 const struct got_error *err = NULL;
291 int i;
293 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
294 if (pack_fds[i] == -1)
295 continue;
296 if (close(pack_fds[i]) == -1) {
297 err = got_error_from_errno("close");
298 break;
301 free(pack_fds);
302 return err;
305 const struct got_error *
306 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
307 struct got_object *obj)
309 #ifndef GOT_NO_OBJ_CACHE
310 const struct got_error *err = NULL;
311 err = got_object_cache_add(&repo->objcache, id, obj);
312 if (err) {
313 if (err->code == GOT_ERR_OBJ_EXISTS ||
314 err->code == GOT_ERR_OBJ_TOO_LARGE)
315 err = NULL;
316 return err;
318 obj->refcnt++;
319 #endif
320 return NULL;
323 struct got_object *
324 got_repo_get_cached_object(struct got_repository *repo,
325 struct got_object_id *id)
327 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
330 const struct got_error *
331 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
332 struct got_tree_object *tree)
334 #ifndef GOT_NO_OBJ_CACHE
335 const struct got_error *err = NULL;
336 err = got_object_cache_add(&repo->treecache, id, tree);
337 if (err) {
338 if (err->code == GOT_ERR_OBJ_EXISTS ||
339 err->code == GOT_ERR_OBJ_TOO_LARGE)
340 err = NULL;
341 return err;
343 tree->refcnt++;
344 #endif
345 return NULL;
348 struct got_tree_object *
349 got_repo_get_cached_tree(struct got_repository *repo,
350 struct got_object_id *id)
352 return (struct got_tree_object *)got_object_cache_get(
353 &repo->treecache, id);
356 const struct got_error *
357 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
358 struct got_commit_object *commit)
360 #ifndef GOT_NO_OBJ_CACHE
361 const struct got_error *err = NULL;
362 err = got_object_cache_add(&repo->commitcache, id, commit);
363 if (err) {
364 if (err->code == GOT_ERR_OBJ_EXISTS ||
365 err->code == GOT_ERR_OBJ_TOO_LARGE)
366 err = NULL;
367 return err;
369 commit->refcnt++;
370 #endif
371 return NULL;
374 struct got_commit_object *
375 got_repo_get_cached_commit(struct got_repository *repo,
376 struct got_object_id *id)
378 return (struct got_commit_object *)got_object_cache_get(
379 &repo->commitcache, id);
382 const struct got_error *
383 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
384 struct got_tag_object *tag)
386 #ifndef GOT_NO_OBJ_CACHE
387 const struct got_error *err = NULL;
388 err = got_object_cache_add(&repo->tagcache, id, tag);
389 if (err) {
390 if (err->code == GOT_ERR_OBJ_EXISTS ||
391 err->code == GOT_ERR_OBJ_TOO_LARGE)
392 err = NULL;
393 return err;
395 tag->refcnt++;
396 #endif
397 return NULL;
400 struct got_tag_object *
401 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
403 return (struct got_tag_object *)got_object_cache_get(
404 &repo->tagcache, id);
407 const struct got_error *
408 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
409 struct got_raw_object *raw)
411 #ifndef GOT_NO_OBJ_CACHE
412 const struct got_error *err = NULL;
413 err = got_object_cache_add(&repo->rawcache, id, raw);
414 if (err) {
415 if (err->code == GOT_ERR_OBJ_EXISTS ||
416 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 err = NULL;
418 return err;
420 raw->refcnt++;
421 #endif
422 return NULL;
426 struct got_raw_object *
427 got_repo_get_cached_raw_object(struct got_repository *repo,
428 struct got_object_id *id)
430 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
434 static const struct got_error *
435 open_repo(struct got_repository *repo, const char *path)
437 const struct got_error *err = NULL;
439 repo->gitdir_fd = -1;
441 /* bare git repository? */
442 repo->path_git_dir = strdup(path);
443 if (repo->path_git_dir == NULL)
444 return got_error_from_errno("strdup");
445 if (is_git_repo(repo)) {
446 repo->path = strdup(repo->path_git_dir);
447 if (repo->path == NULL) {
448 err = got_error_from_errno("strdup");
449 goto done;
451 repo->gitdir_fd = open(repo->path_git_dir,
452 O_DIRECTORY | O_CLOEXEC);
453 if (repo->gitdir_fd == -1) {
454 err = got_error_from_errno2("open",
455 repo->path_git_dir);
456 goto done;
458 return NULL;
461 /* git repository with working tree? */
462 free(repo->path_git_dir);
463 repo->path_git_dir = NULL;
464 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
465 err = got_error_from_errno("asprintf");
466 goto done;
468 if (is_git_repo(repo)) {
469 repo->path = strdup(path);
470 if (repo->path == NULL) {
471 err = got_error_from_errno("strdup");
472 goto done;
474 repo->gitdir_fd = open(repo->path_git_dir,
475 O_DIRECTORY | O_CLOEXEC);
476 if (repo->gitdir_fd == -1) {
477 err = got_error_from_errno2("open",
478 repo->path_git_dir);
479 goto done;
481 return NULL;
484 err = got_error(GOT_ERR_NOT_GIT_REPO);
485 done:
486 if (err) {
487 free(repo->path);
488 repo->path = NULL;
489 free(repo->path_git_dir);
490 repo->path_git_dir = NULL;
491 if (repo->gitdir_fd != -1)
492 close(repo->gitdir_fd);
493 repo->gitdir_fd = -1;
496 return err;
499 static const struct got_error *
500 parse_gitconfig_file(int *gitconfig_repository_format_version,
501 char **gitconfig_author_name, char **gitconfig_author_email,
502 struct got_remote_repo **remotes, int *nremotes,
503 char **gitconfig_owner, char ***extensions, int *nextensions,
504 const char *gitconfig_path)
506 const struct got_error *err = NULL, *child_err = NULL;
507 int fd = -1;
508 int imsg_fds[2] = { -1, -1 };
509 pid_t pid;
510 struct imsgbuf *ibuf;
512 *gitconfig_repository_format_version = 0;
513 if (extensions)
514 *extensions = NULL;
515 if (nextensions)
516 *nextensions = 0;
517 *gitconfig_author_name = NULL;
518 *gitconfig_author_email = NULL;
519 if (remotes)
520 *remotes = NULL;
521 if (nremotes)
522 *nremotes = 0;
523 if (gitconfig_owner)
524 *gitconfig_owner = NULL;
526 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
527 if (fd == -1) {
528 if (errno == ENOENT)
529 return NULL;
530 return got_error_from_errno2("open", gitconfig_path);
533 ibuf = calloc(1, sizeof(*ibuf));
534 if (ibuf == NULL) {
535 err = got_error_from_errno("calloc");
536 goto done;
539 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
540 err = got_error_from_errno("socketpair");
541 goto done;
544 pid = fork();
545 if (pid == -1) {
546 err = got_error_from_errno("fork");
547 goto done;
548 } else if (pid == 0) {
549 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
550 gitconfig_path);
551 /* not reached */
554 if (close(imsg_fds[1]) == -1) {
555 err = got_error_from_errno("close");
556 goto done;
558 imsg_fds[1] = -1;
559 imsg_init(ibuf, imsg_fds[0]);
561 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
562 if (err)
563 goto done;
564 fd = -1;
566 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
567 if (err)
568 goto done;
570 err = got_privsep_recv_gitconfig_int(
571 gitconfig_repository_format_version, ibuf);
572 if (err)
573 goto done;
575 if (extensions && nextensions) {
576 err = got_privsep_send_gitconfig_repository_extensions_req(
577 ibuf);
578 if (err)
579 goto done;
580 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
581 if (err)
582 goto done;
583 if (*nextensions > 0) {
584 int i;
585 *extensions = calloc(*nextensions, sizeof(char *));
586 if (*extensions == NULL) {
587 err = got_error_from_errno("calloc");
588 goto done;
590 for (i = 0; i < *nextensions; i++) {
591 char *ext;
592 err = got_privsep_recv_gitconfig_str(&ext,
593 ibuf);
594 if (err)
595 goto done;
596 (*extensions)[i] = ext;
601 err = got_privsep_send_gitconfig_author_name_req(ibuf);
602 if (err)
603 goto done;
605 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
606 if (err)
607 goto done;
609 err = got_privsep_send_gitconfig_author_email_req(ibuf);
610 if (err)
611 goto done;
613 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
614 if (err)
615 goto done;
617 if (remotes && nremotes) {
618 err = got_privsep_send_gitconfig_remotes_req(ibuf);
619 if (err)
620 goto done;
622 err = got_privsep_recv_gitconfig_remotes(remotes,
623 nremotes, ibuf);
624 if (err)
625 goto done;
628 if (gitconfig_owner) {
629 err = got_privsep_send_gitconfig_owner_req(ibuf);
630 if (err)
631 goto done;
632 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
633 if (err)
634 goto done;
637 err = got_privsep_send_stop(imsg_fds[0]);
638 child_err = got_privsep_wait_for_child(pid);
639 if (child_err && err == NULL)
640 err = child_err;
641 done:
642 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
643 err = got_error_from_errno("close");
644 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
645 err = got_error_from_errno("close");
646 if (fd != -1 && close(fd) == -1 && err == NULL)
647 err = got_error_from_errno2("close", gitconfig_path);
648 free(ibuf);
649 return err;
652 static const struct got_error *
653 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
655 const struct got_error *err = NULL;
656 char *repo_gitconfig_path = NULL;
658 if (global_gitconfig_path) {
659 /* Read settings from ~/.gitconfig. */
660 int dummy_repo_version;
661 err = parse_gitconfig_file(&dummy_repo_version,
662 &repo->global_gitconfig_author_name,
663 &repo->global_gitconfig_author_email,
664 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
665 if (err)
666 return err;
669 /* Read repository's .git/config file. */
670 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
671 if (repo_gitconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gitconfig");
674 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
675 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
676 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
677 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
678 repo_gitconfig_path);
679 if (err)
680 goto done;
682 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
683 int i;
685 for (i = 0; i < repo->ngitconfig_remotes; i++) {
686 got_repo_free_remote_repo_data(
687 &repo->gitconfig_remotes[i]);
689 free(repo->gitconfig_remotes);
690 repo->gitconfig_remotes = NULL;
691 repo->ngitconfig_remotes = 0;
693 free(repo->gitconfig_author_name);
694 repo->gitconfig_author_name = NULL;
695 free(repo->gitconfig_author_email);
696 repo->gitconfig_author_email = NULL;
698 free(repo->global_gitconfig_author_name);
699 repo->global_gitconfig_author_name = NULL;
700 free(repo->global_gitconfig_author_email);
701 repo->global_gitconfig_author_email = NULL;
704 done:
705 free(repo_gitconfig_path);
706 return err;
709 static const struct got_error *
710 read_gotconfig(struct got_repository *repo)
712 const struct got_error *err = NULL;
713 char *gotconfig_path;
715 gotconfig_path = got_repo_get_path_gotconfig(repo);
716 if (gotconfig_path == NULL)
717 return got_error_from_errno("got_repo_get_path_gotconfig");
719 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
720 free(gotconfig_path);
721 return err;
724 /* Supported repository format extensions. */
725 static const char *const repo_extensions[] = {
726 "noop", /* Got supports repository format version 1. */
727 "preciousObjects", /* Supported by gotadmin cleanup. */
728 "worktreeConfig", /* Got does not care about Git work trees. */
729 };
731 const struct got_error *
732 got_repo_open(struct got_repository **repop, const char *path,
733 const char *global_gitconfig_path, int *pack_fds)
735 struct got_repository *repo = NULL;
736 const struct got_error *err = NULL;
737 char *repo_path = NULL;
738 size_t i, j = 0;
739 struct rlimit rl;
741 *repop = NULL;
743 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
744 return got_error_from_errno("getrlimit");
746 repo = calloc(1, sizeof(*repo));
747 if (repo == NULL)
748 return got_error_from_errno("calloc");
750 RB_INIT(&repo->packidx_bloom_filters);
751 TAILQ_INIT(&repo->packidx_paths);
753 for (i = 0; i < nitems(repo->privsep_children); i++) {
754 memset(&repo->privsep_children[i], 0,
755 sizeof(repo->privsep_children[0]));
756 repo->privsep_children[i].imsg_fd = -1;
759 err = got_object_cache_init(&repo->objcache,
760 GOT_OBJECT_CACHE_TYPE_OBJ);
761 if (err)
762 goto done;
763 err = got_object_cache_init(&repo->treecache,
764 GOT_OBJECT_CACHE_TYPE_TREE);
765 if (err)
766 goto done;
767 err = got_object_cache_init(&repo->commitcache,
768 GOT_OBJECT_CACHE_TYPE_COMMIT);
769 if (err)
770 goto done;
771 err = got_object_cache_init(&repo->tagcache,
772 GOT_OBJECT_CACHE_TYPE_TAG);
773 if (err)
774 goto done;
775 err = got_object_cache_init(&repo->rawcache,
776 GOT_OBJECT_CACHE_TYPE_RAW);
777 if (err)
778 goto done;
780 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
781 if (repo->pack_cache_size > rl.rlim_cur / 8)
782 repo->pack_cache_size = rl.rlim_cur / 8;
783 for (i = 0; i < nitems(repo->packs); i++) {
784 if (i < repo->pack_cache_size) {
785 repo->packs[i].basefd = pack_fds[j++];
786 repo->packs[i].accumfd = pack_fds[j++];
787 } else {
788 repo->packs[i].basefd = -1;
789 repo->packs[i].accumfd = -1;
792 repo->pinned_pack = -1;
793 repo->pinned_packidx = -1;
794 repo->pinned_pid = 0;
796 repo_path = realpath(path, NULL);
797 if (repo_path == NULL) {
798 err = got_error_from_errno2("realpath", path);
799 goto done;
802 for (;;) {
803 char *parent_path;
805 err = open_repo(repo, repo_path);
806 if (err == NULL)
807 break;
808 if (err->code != GOT_ERR_NOT_GIT_REPO)
809 goto done;
810 if (repo_path[0] == '/' && repo_path[1] == '\0') {
811 err = got_error(GOT_ERR_NOT_GIT_REPO);
812 goto done;
814 err = got_path_dirname(&parent_path, repo_path);
815 if (err)
816 goto done;
817 free(repo_path);
818 repo_path = parent_path;
821 err = read_gotconfig(repo);
822 if (err)
823 goto done;
825 err = read_gitconfig(repo, global_gitconfig_path);
826 if (err)
827 goto done;
828 if (repo->gitconfig_repository_format_version != 0) {
829 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
830 goto done;
832 for (i = 0; i < repo->nextensions; i++) {
833 char *ext = repo->extensions[i];
834 int j, supported = 0;
835 for (j = 0; j < nitems(repo_extensions); j++) {
836 if (strcmp(ext, repo_extensions[j]) == 0) {
837 supported = 1;
838 break;
841 if (!supported) {
842 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
843 goto done;
847 err = got_repo_list_packidx(&repo->packidx_paths, repo);
848 done:
849 if (err)
850 got_repo_close(repo);
851 else
852 *repop = repo;
853 free(repo_path);
854 return err;
857 const struct got_error *
858 got_repo_close(struct got_repository *repo)
860 const struct got_error *err = NULL, *child_err;
861 struct got_packidx_bloom_filter *bf;
862 struct got_pathlist_entry *pe;
863 size_t i;
865 for (i = 0; i < repo->pack_cache_size; i++) {
866 if (repo->packidx_cache[i] == NULL)
867 break;
868 got_packidx_close(repo->packidx_cache[i]);
871 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
872 &repo->packidx_bloom_filters))) {
873 RB_REMOVE(got_packidx_bloom_filter_tree,
874 &repo->packidx_bloom_filters, bf);
875 bloom_free(bf->bloom);
876 free(bf->bloom);
877 free(bf);
880 for (i = 0; i < repo->pack_cache_size; i++)
881 if (repo->packs[i].path_packfile)
882 if (repo->packs[i].path_packfile)
883 got_pack_close(&repo->packs[i]);
885 free(repo->path);
886 free(repo->path_git_dir);
888 got_object_cache_close(&repo->objcache);
889 got_object_cache_close(&repo->treecache);
890 got_object_cache_close(&repo->commitcache);
891 got_object_cache_close(&repo->tagcache);
892 got_object_cache_close(&repo->rawcache);
894 for (i = 0; i < nitems(repo->privsep_children); i++) {
895 if (repo->privsep_children[i].imsg_fd == -1)
896 continue;
897 imsg_clear(repo->privsep_children[i].ibuf);
898 free(repo->privsep_children[i].ibuf);
899 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
900 child_err = got_privsep_wait_for_child(
901 repo->privsep_children[i].pid);
902 if (child_err && err == NULL)
903 err = child_err;
904 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
905 err == NULL)
906 err = got_error_from_errno("close");
909 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
910 err == NULL)
911 err = got_error_from_errno("close");
913 if (repo->gotconfig)
914 got_gotconfig_free(repo->gotconfig);
915 free(repo->gitconfig_author_name);
916 free(repo->gitconfig_author_email);
917 for (i = 0; i < repo->ngitconfig_remotes; i++)
918 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
919 free(repo->gitconfig_remotes);
920 for (i = 0; i < repo->nextensions; i++)
921 free(repo->extensions[i]);
922 free(repo->extensions);
924 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
925 free((void *)pe->path);
926 got_pathlist_free(&repo->packidx_paths);
927 free(repo);
929 return err;
932 void
933 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
935 int i;
937 free(repo->name);
938 repo->name = NULL;
939 free(repo->fetch_url);
940 repo->fetch_url = NULL;
941 free(repo->send_url);
942 repo->send_url = NULL;
943 for (i = 0; i < repo->nfetch_branches; i++)
944 free(repo->fetch_branches[i]);
945 free(repo->fetch_branches);
946 repo->fetch_branches = NULL;
947 repo->nfetch_branches = 0;
948 for (i = 0; i < repo->nsend_branches; i++)
949 free(repo->send_branches[i]);
950 free(repo->send_branches);
951 repo->send_branches = NULL;
952 repo->nsend_branches = 0;
955 const struct got_error *
956 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
957 const char *input_path)
959 const struct got_error *err = NULL;
960 const char *repo_abspath = NULL;
961 size_t repolen, len;
962 char *canonpath, *path = NULL;
964 *in_repo_path = NULL;
966 canonpath = strdup(input_path);
967 if (canonpath == NULL) {
968 err = got_error_from_errno("strdup");
969 goto done;
971 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
972 if (err)
973 goto done;
975 repo_abspath = got_repo_get_path(repo);
977 if (canonpath[0] == '\0') {
978 path = strdup(canonpath);
979 if (path == NULL) {
980 err = got_error_from_errno("strdup");
981 goto done;
983 } else {
984 path = realpath(canonpath, NULL);
985 if (path == NULL) {
986 if (errno != ENOENT) {
987 err = got_error_from_errno2("realpath",
988 canonpath);
989 goto done;
991 /*
992 * Path is not on disk.
993 * Assume it is already relative to repository root.
994 */
995 path = strdup(canonpath);
996 if (path == NULL) {
997 err = got_error_from_errno("strdup");
998 goto done;
1002 repolen = strlen(repo_abspath);
1003 len = strlen(path);
1006 if (strcmp(path, repo_abspath) == 0) {
1007 free(path);
1008 path = strdup("");
1009 if (path == NULL) {
1010 err = got_error_from_errno("strdup");
1011 goto done;
1013 } else if (len > repolen &&
1014 got_path_is_child(path, repo_abspath, repolen)) {
1015 /* Matched an on-disk path inside repository. */
1016 if (got_repo_is_bare(repo)) {
1018 * Matched an on-disk path inside repository
1019 * database. Treat input as repository-relative.
1021 free(path);
1022 path = canonpath;
1023 canonpath = NULL;
1024 } else {
1025 char *child;
1026 /* Strip common prefix with repository path. */
1027 err = got_path_skip_common_ancestor(&child,
1028 repo_abspath, path);
1029 if (err)
1030 goto done;
1031 free(path);
1032 path = child;
1034 } else {
1036 * Matched unrelated on-disk path.
1037 * Treat input as repository-relative.
1039 free(path);
1040 path = canonpath;
1041 canonpath = NULL;
1045 /* Make in-repository path absolute */
1046 if (path[0] != '/') {
1047 char *abspath;
1048 if (asprintf(&abspath, "/%s", path) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 goto done;
1052 free(path);
1053 path = abspath;
1056 done:
1057 free(canonpath);
1058 if (err)
1059 free(path);
1060 else
1061 *in_repo_path = path;
1062 return err;
1065 static const struct got_error *
1066 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1067 const char *path_packidx)
1069 const struct got_error *err = NULL;
1070 size_t i;
1072 for (i = 0; i < repo->pack_cache_size; i++) {
1073 if (repo->packidx_cache[i] == NULL)
1074 break;
1075 if (strcmp(repo->packidx_cache[i]->path_packidx,
1076 path_packidx) == 0) {
1077 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1080 if (i == repo->pack_cache_size) {
1081 do {
1082 i--;
1083 } while (i > 0 && repo->pinned_packidx >= 0 &&
1084 i == repo->pinned_packidx);
1085 err = got_packidx_close(repo->packidx_cache[i]);
1086 if (err)
1087 return err;
1090 repo->packidx_cache[i] = packidx;
1092 return NULL;
1095 int
1096 got_repo_is_packidx_filename(const char *name, size_t len)
1098 if (len != GOT_PACKIDX_NAMELEN)
1099 return 0;
1101 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1102 return 0;
1104 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1105 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1106 return 0;
1108 return 1;
1111 static struct got_packidx_bloom_filter *
1112 get_packidx_bloom_filter(struct got_repository *repo,
1113 const char *path, size_t path_len)
1115 struct got_packidx_bloom_filter key;
1117 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1118 return NULL; /* XXX */
1119 key.path_len = path_len;
1121 return RB_FIND(got_packidx_bloom_filter_tree,
1122 &repo->packidx_bloom_filters, &key);
1125 int
1126 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1127 const char *path_packidx, struct got_object_id *id)
1129 struct got_packidx_bloom_filter *bf;
1131 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1132 if (bf)
1133 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1135 /* No bloom filter means this pack index must be searched. */
1136 return 1;
1139 static const struct got_error *
1140 add_packidx_bloom_filter(struct got_repository *repo,
1141 struct got_packidx *packidx, const char *path_packidx)
1143 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1144 struct got_packidx_bloom_filter *bf;
1145 size_t len;
1148 * Don't use bloom filters for very large pack index files.
1149 * Large pack files will contain a relatively large fraction
1150 * of our objects so we will likely need to visit them anyway.
1151 * The more objects a pack file contains the higher the probability
1152 * of a false-positive match from the bloom filter. And reading
1153 * all object IDs from a large pack index file can be expensive.
1155 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1156 return NULL;
1158 /* Do we already have a filter for this pack index? */
1159 if (get_packidx_bloom_filter(repo, path_packidx,
1160 strlen(path_packidx)) != NULL)
1161 return NULL;
1163 bf = calloc(1, sizeof(*bf));
1164 if (bf == NULL)
1165 return got_error_from_errno("calloc");
1166 bf->bloom = calloc(1, sizeof(*bf->bloom));
1167 if (bf->bloom == NULL) {
1168 free(bf);
1169 return got_error_from_errno("calloc");
1172 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1173 if (len >= sizeof(bf->path)) {
1174 free(bf->bloom);
1175 free(bf);
1176 return got_error(GOT_ERR_NO_SPACE);
1178 bf->path_len = len;
1180 /* Minimum size supported by our bloom filter is 1000 entries. */
1181 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1182 for (i = 0; i < nobjects; i++) {
1183 struct got_packidx_object_id *id;
1184 id = &packidx->hdr.sorted_ids[i];
1185 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1188 RB_INSERT(got_packidx_bloom_filter_tree,
1189 &repo->packidx_bloom_filters, bf);
1190 return NULL;
1193 const struct got_error *
1194 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1195 struct got_repository *repo, struct got_object_id *id)
1197 const struct got_error *err;
1198 struct got_pathlist_entry *pe;
1199 size_t i;
1201 /* Search pack index cache. */
1202 for (i = 0; i < repo->pack_cache_size; i++) {
1203 if (repo->packidx_cache[i] == NULL)
1204 break;
1205 if (!got_repo_check_packidx_bloom_filter(repo,
1206 repo->packidx_cache[i]->path_packidx, id))
1207 continue; /* object will not be found in this index */
1208 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1209 if (*idx != -1) {
1210 *packidx = repo->packidx_cache[i];
1212 * Move this cache entry to the front. Repeatedly
1213 * searching a wrong pack index can be expensive.
1215 if (i > 0) {
1216 memmove(&repo->packidx_cache[1],
1217 &repo->packidx_cache[0],
1218 i * sizeof(repo->packidx_cache[0]));
1219 repo->packidx_cache[0] = *packidx;
1220 if (repo->pinned_packidx >= 0 &&
1221 repo->pinned_packidx < i)
1222 repo->pinned_packidx++;
1223 else if (repo->pinned_packidx == i)
1224 repo->pinned_packidx = 0;
1226 return NULL;
1229 /* No luck. Search the filesystem. */
1231 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1232 const char *path_packidx = pe->path;
1233 int is_cached = 0;
1235 if (!got_repo_check_packidx_bloom_filter(repo,
1236 pe->path, id))
1237 continue; /* object will not be found in this index */
1239 for (i = 0; i < repo->pack_cache_size; i++) {
1240 if (repo->packidx_cache[i] == NULL)
1241 break;
1242 if (strcmp(repo->packidx_cache[i]->path_packidx,
1243 path_packidx) == 0) {
1244 is_cached = 1;
1245 break;
1248 if (is_cached)
1249 continue; /* already searched */
1251 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1252 path_packidx, 0);
1253 if (err)
1254 goto done;
1256 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1257 if (err)
1258 goto done;
1260 err = cache_packidx(repo, *packidx, path_packidx);
1261 if (err)
1262 goto done;
1264 *idx = got_packidx_get_object_idx(*packidx, id);
1265 if (*idx != -1) {
1266 err = NULL; /* found the object */
1267 goto done;
1271 err = got_error_no_obj(id);
1272 done:
1273 return err;
1276 const struct got_error *
1277 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1278 struct got_repository *repo)
1280 const struct got_error *err = NULL;
1281 DIR *packdir = NULL;
1282 struct dirent *dent;
1283 char *path_packidx = NULL;
1284 int packdir_fd;
1286 packdir_fd = openat(got_repo_get_fd(repo),
1287 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1288 if (packdir_fd == -1) {
1289 return got_error_from_errno_fmt("openat: %s/%s",
1290 got_repo_get_path_git_dir(repo),
1291 GOT_OBJECTS_PACK_DIR);
1294 packdir = fdopendir(packdir_fd);
1295 if (packdir == NULL) {
1296 err = got_error_from_errno("fdopendir");
1297 goto done;
1300 while ((dent = readdir(packdir)) != NULL) {
1301 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1302 continue;
1304 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1305 dent->d_name) == -1) {
1306 err = got_error_from_errno("asprintf");
1307 path_packidx = NULL;
1308 break;
1311 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1312 if (err)
1313 break;
1315 done:
1316 if (err)
1317 free(path_packidx);
1318 if (packdir && closedir(packdir) != 0 && err == NULL)
1319 err = got_error_from_errno("closedir");
1320 return err;
1323 const struct got_error *
1324 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1325 struct got_repository *repo)
1327 const struct got_error *err;
1328 size_t i;
1330 *packidx = NULL;
1332 /* Search pack index cache. */
1333 for (i = 0; i < repo->pack_cache_size; i++) {
1334 if (repo->packidx_cache[i] == NULL)
1335 break;
1336 if (strcmp(repo->packidx_cache[i]->path_packidx,
1337 path_packidx) == 0) {
1338 *packidx = repo->packidx_cache[i];
1339 return NULL;
1342 /* No luck. Search the filesystem. */
1344 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1345 path_packidx, 0);
1346 if (err)
1347 return err;
1349 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1350 if (err)
1351 goto done;
1353 err = cache_packidx(repo, *packidx, path_packidx);
1354 done:
1355 if (err) {
1356 got_packidx_close(*packidx);
1357 *packidx = NULL;
1359 return err;
1362 static const struct got_error *
1363 read_packfile_hdr(int fd, struct got_packidx *packidx)
1365 const struct got_error *err = NULL;
1366 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1367 struct got_packfile_hdr hdr;
1368 ssize_t n;
1370 n = read(fd, &hdr, sizeof(hdr));
1371 if (n < 0)
1372 return got_error_from_errno("read");
1373 if (n != sizeof(hdr))
1374 return got_error(GOT_ERR_BAD_PACKFILE);
1376 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1377 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1378 be32toh(hdr.nobjects) != totobj)
1379 err = got_error(GOT_ERR_BAD_PACKFILE);
1381 return err;
1384 static const struct got_error *
1385 open_packfile(int *fd, struct got_repository *repo,
1386 const char *relpath, struct got_packidx *packidx)
1388 const struct got_error *err = NULL;
1390 *fd = openat(got_repo_get_fd(repo), relpath,
1391 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1392 if (*fd == -1)
1393 return got_error_from_errno_fmt("openat: %s/%s",
1394 got_repo_get_path_git_dir(repo), relpath);
1396 if (packidx) {
1397 err = read_packfile_hdr(*fd, packidx);
1398 if (err) {
1399 close(*fd);
1400 *fd = -1;
1404 return err;
1407 const struct got_error *
1408 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1409 const char *path_packfile, struct got_packidx *packidx)
1411 const struct got_error *err = NULL;
1412 struct got_pack *pack = NULL;
1413 struct stat sb;
1414 size_t i;
1416 if (packp)
1417 *packp = NULL;
1419 for (i = 0; i < repo->pack_cache_size; i++) {
1420 pack = &repo->packs[i];
1421 if (pack->path_packfile == NULL)
1422 break;
1423 if (strcmp(pack->path_packfile, path_packfile) == 0)
1424 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1427 if (i == repo->pack_cache_size) {
1428 struct got_pack tmp;
1429 do {
1430 i--;
1431 } while (i > 0 && repo->pinned_pack >= 0 &&
1432 i == repo->pinned_pack);
1433 err = got_pack_close(&repo->packs[i]);
1434 if (err)
1435 return err;
1436 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1437 return got_error_from_errno("ftruncate");
1438 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1439 return got_error_from_errno("ftruncate");
1440 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1441 memcpy(&repo->packs[i], &repo->packs[0],
1442 sizeof(repo->packs[i]));
1443 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1444 if (repo->pinned_pack == 0)
1445 repo->pinned_pack = i;
1446 else if (repo->pinned_pack == i)
1447 repo->pinned_pack = 0;
1448 i = 0;
1451 pack = &repo->packs[i];
1453 pack->path_packfile = strdup(path_packfile);
1454 if (pack->path_packfile == NULL) {
1455 err = got_error_from_errno("strdup");
1456 goto done;
1459 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1460 if (err)
1461 goto done;
1463 if (fstat(pack->fd, &sb) != 0) {
1464 err = got_error_from_errno("fstat");
1465 goto done;
1467 pack->filesize = sb.st_size;
1469 pack->privsep_child = NULL;
1471 #ifndef GOT_PACK_NO_MMAP
1472 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1473 pack->fd, 0);
1474 if (pack->map == MAP_FAILED) {
1475 if (errno != ENOMEM) {
1476 err = got_error_from_errno("mmap");
1477 goto done;
1479 pack->map = NULL; /* fall back to read(2) */
1481 #endif
1482 done:
1483 if (err) {
1484 if (pack) {
1485 free(pack->path_packfile);
1486 memset(pack, 0, sizeof(*pack));
1488 } else if (packp)
1489 *packp = pack;
1490 return err;
1493 struct got_pack *
1494 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1496 struct got_pack *pack = NULL;
1497 size_t i;
1499 for (i = 0; i < repo->pack_cache_size; i++) {
1500 pack = &repo->packs[i];
1501 if (pack->path_packfile == NULL)
1502 break;
1503 if (strcmp(pack->path_packfile, path_packfile) == 0)
1504 return pack;
1507 return NULL;
1510 const struct got_error *
1511 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1512 struct got_pack *pack)
1514 size_t i;
1515 int pinned_pack = -1, pinned_packidx = -1;
1517 for (i = 0; i < repo->pack_cache_size; i++) {
1518 if (repo->packidx_cache[i] &&
1519 strcmp(repo->packidx_cache[i]->path_packidx,
1520 packidx->path_packidx) == 0)
1521 pinned_packidx = i;
1522 if (repo->packs[i].path_packfile &&
1523 strcmp(repo->packs[i].path_packfile,
1524 pack->path_packfile) == 0)
1525 pinned_pack = i;
1528 if (pinned_packidx == -1 || pinned_pack == -1)
1529 return got_error(GOT_ERR_PIN_PACK);
1531 repo->pinned_pack = pinned_pack;
1532 repo->pinned_packidx = pinned_packidx;
1533 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1534 return NULL;
1537 struct got_pack *
1538 got_repo_get_pinned_pack(struct got_repository *repo)
1540 if (repo->pinned_pack >= 0 &&
1541 repo->pinned_pack < repo->pack_cache_size)
1542 return &repo->packs[repo->pinned_pack];
1544 return NULL;
1547 void
1548 got_repo_unpin_pack(struct got_repository *repo)
1550 repo->pinned_packidx = -1;
1551 repo->pinned_pack = -1;
1552 repo->pinned_pid = 0;
1555 const struct got_error *
1556 got_repo_init(const char *repo_path)
1558 const struct got_error *err = NULL;
1559 const char *dirnames[] = {
1560 GOT_OBJECTS_DIR,
1561 GOT_OBJECTS_PACK_DIR,
1562 GOT_REFS_DIR,
1564 const char *description_str = "Unnamed repository; "
1565 "edit this file 'description' to name the repository.";
1566 const char *headref_str = "ref: refs/heads/main";
1567 const char *gitconfig_str = "[core]\n"
1568 "\trepositoryformatversion = 0\n"
1569 "\tfilemode = true\n"
1570 "\tbare = true\n";
1571 char *path;
1572 size_t i;
1574 if (!got_path_dir_is_empty(repo_path))
1575 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1577 for (i = 0; i < nitems(dirnames); i++) {
1578 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1579 return got_error_from_errno("asprintf");
1581 err = got_path_mkdir(path);
1582 free(path);
1583 if (err)
1584 return err;
1587 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1588 return got_error_from_errno("asprintf");
1589 err = got_path_create_file(path, description_str);
1590 free(path);
1591 if (err)
1592 return err;
1594 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, headref_str);
1597 free(path);
1598 if (err)
1599 return err;
1601 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1602 return got_error_from_errno("asprintf");
1603 err = got_path_create_file(path, gitconfig_str);
1604 free(path);
1605 if (err)
1606 return err;
1608 return NULL;
1611 static const struct got_error *
1612 match_packed_object(struct got_object_id **unique_id,
1613 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1615 const struct got_error *err = NULL;
1616 struct got_object_id_queue matched_ids;
1617 struct got_pathlist_entry *pe;
1619 STAILQ_INIT(&matched_ids);
1621 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1622 const char *path_packidx = pe->path;
1623 struct got_packidx *packidx;
1624 struct got_object_qid *qid;
1626 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1627 path_packidx, 0);
1628 if (err)
1629 break;
1631 err = got_packidx_match_id_str_prefix(&matched_ids,
1632 packidx, id_str_prefix);
1633 if (err) {
1634 got_packidx_close(packidx);
1635 break;
1637 err = got_packidx_close(packidx);
1638 if (err)
1639 break;
1641 STAILQ_FOREACH(qid, &matched_ids, entry) {
1642 if (obj_type != GOT_OBJ_TYPE_ANY) {
1643 int matched_type;
1644 err = got_object_get_type(&matched_type, repo,
1645 &qid->id);
1646 if (err)
1647 goto done;
1648 if (matched_type != obj_type)
1649 continue;
1651 if (*unique_id == NULL) {
1652 *unique_id = got_object_id_dup(&qid->id);
1653 if (*unique_id == NULL) {
1654 err = got_error_from_errno("malloc");
1655 goto done;
1657 } else {
1658 if (got_object_id_cmp(*unique_id,
1659 &qid->id) == 0)
1660 continue; /* packed multiple times */
1661 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1662 goto done;
1666 done:
1667 got_object_id_queue_free(&matched_ids);
1668 if (err) {
1669 free(*unique_id);
1670 *unique_id = NULL;
1672 return err;
1675 static const struct got_error *
1676 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1677 const char *object_dir, const char *id_str_prefix, int obj_type,
1678 struct got_repository *repo)
1680 const struct got_error *err = NULL;
1681 char *path;
1682 DIR *dir = NULL;
1683 struct dirent *dent;
1684 struct got_object_id id;
1686 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1687 err = got_error_from_errno("asprintf");
1688 goto done;
1691 dir = opendir(path);
1692 if (dir == NULL) {
1693 if (errno == ENOENT) {
1694 err = NULL;
1695 goto done;
1697 err = got_error_from_errno2("opendir", path);
1698 goto done;
1700 while ((dent = readdir(dir)) != NULL) {
1701 char *id_str;
1702 int cmp;
1704 if (strcmp(dent->d_name, ".") == 0 ||
1705 strcmp(dent->d_name, "..") == 0)
1706 continue;
1708 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1709 err = got_error_from_errno("asprintf");
1710 goto done;
1713 if (!got_parse_sha1_digest(id.sha1, id_str))
1714 continue;
1717 * Directory entries do not necessarily appear in
1718 * sorted order, so we must iterate over all of them.
1720 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1721 if (cmp != 0) {
1722 free(id_str);
1723 continue;
1726 if (*unique_id == NULL) {
1727 if (obj_type != GOT_OBJ_TYPE_ANY) {
1728 int matched_type;
1729 err = got_object_get_type(&matched_type, repo,
1730 &id);
1731 if (err)
1732 goto done;
1733 if (matched_type != obj_type)
1734 continue;
1736 *unique_id = got_object_id_dup(&id);
1737 if (*unique_id == NULL) {
1738 err = got_error_from_errno("got_object_id_dup");
1739 free(id_str);
1740 goto done;
1742 } else {
1743 if (got_object_id_cmp(*unique_id, &id) == 0)
1744 continue; /* both packed and loose */
1745 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1746 free(id_str);
1747 goto done;
1750 done:
1751 if (dir && closedir(dir) != 0 && err == NULL)
1752 err = got_error_from_errno("closedir");
1753 if (err) {
1754 free(*unique_id);
1755 *unique_id = NULL;
1757 free(path);
1758 return err;
1761 const struct got_error *
1762 got_repo_match_object_id_prefix(struct got_object_id **id,
1763 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1765 const struct got_error *err = NULL;
1766 char *path_objects = got_repo_get_path_objects(repo);
1767 char *object_dir = NULL;
1768 size_t len;
1769 int i;
1771 *id = NULL;
1773 len = strlen(id_str_prefix);
1774 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1775 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1777 for (i = 0; i < len; i++) {
1778 if (isxdigit((unsigned char)id_str_prefix[i]))
1779 continue;
1780 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1783 if (len >= 2) {
1784 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1785 if (err)
1786 goto done;
1787 object_dir = strndup(id_str_prefix, 2);
1788 if (object_dir == NULL) {
1789 err = got_error_from_errno("strdup");
1790 goto done;
1792 err = match_loose_object(id, path_objects, object_dir,
1793 id_str_prefix, obj_type, repo);
1794 } else if (len == 1) {
1795 int i;
1796 for (i = 0; i < 0xf; i++) {
1797 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1798 == -1) {
1799 err = got_error_from_errno("asprintf");
1800 goto done;
1802 err = match_packed_object(id, repo, object_dir,
1803 obj_type);
1804 if (err)
1805 goto done;
1806 err = match_loose_object(id, path_objects, object_dir,
1807 id_str_prefix, obj_type, repo);
1808 if (err)
1809 goto done;
1811 } else {
1812 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1813 goto done;
1815 done:
1816 free(object_dir);
1817 if (err) {
1818 free(*id);
1819 *id = NULL;
1820 } else if (*id == NULL) {
1821 switch (obj_type) {
1822 case GOT_OBJ_TYPE_BLOB:
1823 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1824 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1825 break;
1826 case GOT_OBJ_TYPE_TREE:
1827 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1828 GOT_OBJ_LABEL_TREE, id_str_prefix);
1829 break;
1830 case GOT_OBJ_TYPE_COMMIT:
1831 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1832 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1833 break;
1834 case GOT_OBJ_TYPE_TAG:
1835 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1836 GOT_OBJ_LABEL_TAG, id_str_prefix);
1837 break;
1838 default:
1839 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1840 break;
1844 return err;
1847 const struct got_error *
1848 got_repo_match_object_id(struct got_object_id **id, char **label,
1849 const char *id_str, int obj_type, struct got_reflist_head *refs,
1850 struct got_repository *repo)
1852 const struct got_error *err;
1853 struct got_tag_object *tag;
1854 struct got_reference *ref = NULL;
1856 *id = NULL;
1857 if (label)
1858 *label = NULL;
1860 if (refs) {
1861 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1862 refs, repo);
1863 if (err == NULL) {
1864 *id = got_object_id_dup(
1865 got_object_tag_get_object_id(tag));
1866 if (*id == NULL)
1867 err = got_error_from_errno("got_object_id_dup");
1868 else if (label && asprintf(label, "refs/tags/%s",
1869 got_object_tag_get_name(tag)) == -1) {
1870 err = got_error_from_errno("asprintf");
1871 free(*id);
1872 *id = NULL;
1874 got_object_tag_close(tag);
1875 return err;
1876 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1877 err->code != GOT_ERR_NO_OBJ)
1878 return err;
1881 err = got_ref_open(&ref, repo, id_str, 0);
1882 if (err == NULL) {
1883 err = got_ref_resolve(id, repo, ref);
1884 if (err)
1885 goto done;
1886 if (label) {
1887 *label = strdup(got_ref_get_name(ref));
1888 if (*label == NULL) {
1889 err = got_error_from_errno("strdup");
1890 goto done;
1893 } else {
1894 if (err->code != GOT_ERR_NOT_REF &&
1895 err->code != GOT_ERR_BAD_REF_NAME)
1896 goto done;
1897 err = got_repo_match_object_id_prefix(id, id_str,
1898 obj_type, repo);
1899 if (err) {
1900 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1901 err = got_error_not_ref(id_str);
1902 goto done;
1904 if (label) {
1905 err = got_object_id_str(label, *id);
1906 if (*label == NULL) {
1907 err = got_error_from_errno("strdup");
1908 goto done;
1912 done:
1913 if (ref)
1914 got_ref_close(ref);
1915 return err;
1918 const struct got_error *
1919 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1920 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1922 const struct got_error *err = NULL;
1923 struct got_reflist_entry *re;
1924 struct got_object_id *tag_id;
1925 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1927 *tag = NULL;
1929 TAILQ_FOREACH(re, refs, entry) {
1930 const char *refname;
1931 refname = got_ref_get_name(re->ref);
1932 if (got_ref_is_symbolic(re->ref))
1933 continue;
1934 if (strncmp(refname, "refs/tags/", 10) != 0)
1935 continue;
1936 if (!name_is_absolute)
1937 refname += strlen("refs/tags/");
1938 if (strcmp(refname, name) != 0)
1939 continue;
1940 err = got_ref_resolve(&tag_id, repo, re->ref);
1941 if (err)
1942 break;
1943 err = got_object_open_as_tag(tag, repo, tag_id);
1944 free(tag_id);
1945 if (err)
1946 break;
1947 if (obj_type == GOT_OBJ_TYPE_ANY ||
1948 got_object_tag_get_object_type(*tag) == obj_type)
1949 break;
1950 got_object_tag_close(*tag);
1951 *tag = NULL;
1954 if (err == NULL && *tag == NULL)
1955 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1956 GOT_OBJ_LABEL_TAG, name);
1957 return err;
1960 static const struct got_error *
1961 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1962 const char *name, mode_t mode, struct got_object_id *blob_id)
1964 const struct got_error *err = NULL;
1966 *new_te = NULL;
1968 *new_te = calloc(1, sizeof(**new_te));
1969 if (*new_te == NULL)
1970 return got_error_from_errno("calloc");
1972 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1973 sizeof((*new_te)->name)) {
1974 err = got_error(GOT_ERR_NO_SPACE);
1975 goto done;
1978 if (S_ISLNK(mode)) {
1979 (*new_te)->mode = S_IFLNK;
1980 } else {
1981 (*new_te)->mode = S_IFREG;
1982 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1984 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1985 done:
1986 if (err && *new_te) {
1987 free(*new_te);
1988 *new_te = NULL;
1990 return err;
1993 static const struct got_error *
1994 import_file(struct got_tree_entry **new_te, struct dirent *de,
1995 const char *path, struct got_repository *repo)
1997 const struct got_error *err;
1998 struct got_object_id *blob_id = NULL;
1999 char *filepath;
2000 struct stat sb;
2002 if (asprintf(&filepath, "%s%s%s", path,
2003 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2004 return got_error_from_errno("asprintf");
2006 if (lstat(filepath, &sb) != 0) {
2007 err = got_error_from_errno2("lstat", path);
2008 goto done;
2011 err = got_object_blob_create(&blob_id, filepath, repo);
2012 if (err)
2013 goto done;
2015 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2016 blob_id);
2017 done:
2018 free(filepath);
2019 if (err)
2020 free(blob_id);
2021 return err;
2024 static const struct got_error *
2025 insert_tree_entry(struct got_tree_entry *new_te,
2026 struct got_pathlist_head *paths)
2028 const struct got_error *err = NULL;
2029 struct got_pathlist_entry *new_pe;
2031 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2032 if (err)
2033 return err;
2034 if (new_pe == NULL)
2035 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2036 return NULL;
2039 static const struct got_error *write_tree(struct got_object_id **,
2040 const char *, struct got_pathlist_head *, struct got_repository *,
2041 got_repo_import_cb progress_cb, void *progress_arg);
2043 static const struct got_error *
2044 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2045 const char *path, struct got_pathlist_head *ignores,
2046 struct got_repository *repo,
2047 got_repo_import_cb progress_cb, void *progress_arg)
2049 const struct got_error *err;
2050 struct got_object_id *id = NULL;
2051 char *subdirpath;
2053 if (asprintf(&subdirpath, "%s%s%s", path,
2054 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2055 return got_error_from_errno("asprintf");
2057 (*new_te) = calloc(1, sizeof(**new_te));
2058 if (*new_te == NULL)
2059 return got_error_from_errno("calloc");
2060 (*new_te)->mode = S_IFDIR;
2061 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2062 sizeof((*new_te)->name)) {
2063 err = got_error(GOT_ERR_NO_SPACE);
2064 goto done;
2066 err = write_tree(&id, subdirpath, ignores, repo,
2067 progress_cb, progress_arg);
2068 if (err)
2069 goto done;
2070 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2072 done:
2073 free(id);
2074 free(subdirpath);
2075 if (err) {
2076 free(*new_te);
2077 *new_te = NULL;
2079 return err;
2082 static const struct got_error *
2083 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2084 struct got_pathlist_head *ignores, struct got_repository *repo,
2085 got_repo_import_cb progress_cb, void *progress_arg)
2087 const struct got_error *err = NULL;
2088 DIR *dir;
2089 struct dirent *de;
2090 int nentries;
2091 struct got_tree_entry *new_te = NULL;
2092 struct got_pathlist_head paths;
2093 struct got_pathlist_entry *pe;
2095 *new_tree_id = NULL;
2097 TAILQ_INIT(&paths);
2099 dir = opendir(path_dir);
2100 if (dir == NULL) {
2101 err = got_error_from_errno2("opendir", path_dir);
2102 goto done;
2105 nentries = 0;
2106 while ((de = readdir(dir)) != NULL) {
2107 int ignore = 0;
2108 int type;
2110 if (strcmp(de->d_name, ".") == 0 ||
2111 strcmp(de->d_name, "..") == 0)
2112 continue;
2114 TAILQ_FOREACH(pe, ignores, entry) {
2115 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2116 ignore = 1;
2117 break;
2120 if (ignore)
2121 continue;
2123 err = got_path_dirent_type(&type, path_dir, de);
2124 if (err)
2125 goto done;
2127 if (type == DT_DIR) {
2128 err = import_subdir(&new_te, de, path_dir,
2129 ignores, repo, progress_cb, progress_arg);
2130 if (err) {
2131 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2132 goto done;
2133 err = NULL;
2134 continue;
2136 } else if (type == DT_REG || type == DT_LNK) {
2137 err = import_file(&new_te, de, path_dir, repo);
2138 if (err)
2139 goto done;
2140 } else
2141 continue;
2143 err = insert_tree_entry(new_te, &paths);
2144 if (err)
2145 goto done;
2146 nentries++;
2149 if (TAILQ_EMPTY(&paths)) {
2150 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2151 "cannot create tree without any entries");
2152 goto done;
2155 TAILQ_FOREACH(pe, &paths, entry) {
2156 struct got_tree_entry *te = pe->data;
2157 char *path;
2158 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2159 continue;
2160 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2161 err = got_error_from_errno("asprintf");
2162 goto done;
2164 err = (*progress_cb)(progress_arg, path);
2165 free(path);
2166 if (err)
2167 goto done;
2170 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2171 done:
2172 if (dir)
2173 closedir(dir);
2174 got_pathlist_free(&paths);
2175 return err;
2178 const struct got_error *
2179 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2180 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2181 struct got_repository *repo, got_repo_import_cb progress_cb,
2182 void *progress_arg)
2184 const struct got_error *err;
2185 struct got_object_id *new_tree_id;
2187 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2188 progress_cb, progress_arg);
2189 if (err)
2190 return err;
2192 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2193 author, time(NULL), author, time(NULL), logmsg, repo);
2194 free(new_tree_id);
2195 return err;
2198 const struct got_error *
2199 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2200 struct got_repository *repo)
2202 const struct got_error *err = NULL;
2203 char *path_objects = NULL, *path = NULL;
2204 DIR *dir = NULL;
2205 struct got_object_id id;
2206 int i;
2208 *nobjects = 0;
2209 *ondisk_size = 0;
2211 path_objects = got_repo_get_path_objects(repo);
2212 if (path_objects == NULL)
2213 return got_error_from_errno("got_repo_get_path_objects");
2215 for (i = 0; i <= 0xff; i++) {
2216 struct dirent *dent;
2218 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2219 err = got_error_from_errno("asprintf");
2220 break;
2223 dir = opendir(path);
2224 if (dir == NULL) {
2225 if (errno == ENOENT) {
2226 err = NULL;
2227 continue;
2229 err = got_error_from_errno2("opendir", path);
2230 break;
2233 while ((dent = readdir(dir)) != NULL) {
2234 char *id_str;
2235 int fd;
2236 struct stat sb;
2238 if (strcmp(dent->d_name, ".") == 0 ||
2239 strcmp(dent->d_name, "..") == 0)
2240 continue;
2242 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2243 err = got_error_from_errno("asprintf");
2244 goto done;
2247 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2248 free(id_str);
2249 continue;
2251 free(id_str);
2253 err = got_object_open_loose_fd(&fd, &id, repo);
2254 if (err)
2255 goto done;
2257 if (fstat(fd, &sb) == -1) {
2258 err = got_error_from_errno("fstat");
2259 close(fd);
2260 goto done;
2262 (*nobjects)++;
2263 (*ondisk_size) += sb.st_size;
2265 if (close(fd) == -1) {
2266 err = got_error_from_errno("close");
2267 goto done;
2271 if (closedir(dir) != 0) {
2272 err = got_error_from_errno("closedir");
2273 goto done;
2275 dir = NULL;
2277 free(path);
2278 path = NULL;
2280 done:
2281 if (dir && closedir(dir) != 0 && err == NULL)
2282 err = got_error_from_errno("closedir");
2284 if (err) {
2285 *nobjects = 0;
2286 *ondisk_size = 0;
2288 free(path_objects);
2289 free(path);
2290 return err;
2293 const struct got_error *
2294 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2295 off_t *total_packsize, struct got_repository *repo)
2297 const struct got_error *err = NULL;
2298 DIR *packdir = NULL;
2299 struct dirent *dent;
2300 struct got_packidx *packidx = NULL;
2301 char *path_packidx;
2302 char *path_packfile;
2303 int packdir_fd;
2304 struct stat sb;
2306 *npackfiles = 0;
2307 *nobjects = 0;
2308 *total_packsize = 0;
2310 packdir_fd = openat(got_repo_get_fd(repo),
2311 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2312 if (packdir_fd == -1) {
2313 return got_error_from_errno_fmt("openat: %s/%s",
2314 got_repo_get_path_git_dir(repo),
2315 GOT_OBJECTS_PACK_DIR);
2318 packdir = fdopendir(packdir_fd);
2319 if (packdir == NULL) {
2320 err = got_error_from_errno("fdopendir");
2321 goto done;
2324 while ((dent = readdir(packdir)) != NULL) {
2325 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2326 continue;
2328 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2329 dent->d_name) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 goto done;
2334 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2335 path_packidx, 0);
2336 free(path_packidx);
2337 if (err)
2338 goto done;
2340 if (fstat(packidx->fd, &sb) == -1)
2341 goto done;
2342 *total_packsize += sb.st_size;
2344 err = got_packidx_get_packfile_path(&path_packfile,
2345 packidx->path_packidx);
2346 if (err)
2347 goto done;
2349 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2350 0) == -1) {
2351 free(path_packfile);
2352 goto done;
2354 free(path_packfile);
2355 *total_packsize += sb.st_size;
2357 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2359 (*npackfiles)++;
2361 got_packidx_close(packidx);
2362 packidx = NULL;
2364 done:
2365 if (packidx)
2366 got_packidx_close(packidx);
2367 if (packdir && closedir(packdir) != 0 && err == NULL)
2368 err = got_error_from_errno("closedir");
2369 if (err) {
2370 *npackfiles = 0;
2371 *nobjects = 0;
2372 *total_packsize = 0;
2374 return err;
2377 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2378 got_packidx_bloom_filter_cmp);