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 free(bf->bloom);
876 free(bf);
879 for (i = 0; i < repo->pack_cache_size; i++)
880 if (repo->packs[i].path_packfile)
881 if (repo->packs[i].path_packfile)
882 got_pack_close(&repo->packs[i]);
884 free(repo->path);
885 free(repo->path_git_dir);
887 got_object_cache_close(&repo->objcache);
888 got_object_cache_close(&repo->treecache);
889 got_object_cache_close(&repo->commitcache);
890 got_object_cache_close(&repo->tagcache);
891 got_object_cache_close(&repo->rawcache);
893 for (i = 0; i < nitems(repo->privsep_children); i++) {
894 if (repo->privsep_children[i].imsg_fd == -1)
895 continue;
896 imsg_clear(repo->privsep_children[i].ibuf);
897 free(repo->privsep_children[i].ibuf);
898 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
899 child_err = got_privsep_wait_for_child(
900 repo->privsep_children[i].pid);
901 if (child_err && err == NULL)
902 err = child_err;
903 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
904 err == NULL)
905 err = got_error_from_errno("close");
908 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
909 err == NULL)
910 err = got_error_from_errno("close");
912 if (repo->gotconfig)
913 got_gotconfig_free(repo->gotconfig);
914 free(repo->gitconfig_author_name);
915 free(repo->gitconfig_author_email);
916 for (i = 0; i < repo->ngitconfig_remotes; i++)
917 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
918 free(repo->gitconfig_remotes);
919 for (i = 0; i < repo->nextensions; i++)
920 free(repo->extensions[i]);
921 free(repo->extensions);
923 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
924 free((void *)pe->path);
925 got_pathlist_free(&repo->packidx_paths);
926 free(repo);
928 return err;
931 void
932 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
934 int i;
936 free(repo->name);
937 repo->name = NULL;
938 free(repo->fetch_url);
939 repo->fetch_url = NULL;
940 free(repo->send_url);
941 repo->send_url = NULL;
942 for (i = 0; i < repo->nfetch_branches; i++)
943 free(repo->fetch_branches[i]);
944 free(repo->fetch_branches);
945 repo->fetch_branches = NULL;
946 repo->nfetch_branches = 0;
947 for (i = 0; i < repo->nsend_branches; i++)
948 free(repo->send_branches[i]);
949 free(repo->send_branches);
950 repo->send_branches = NULL;
951 repo->nsend_branches = 0;
954 const struct got_error *
955 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
956 const char *input_path)
958 const struct got_error *err = NULL;
959 const char *repo_abspath = NULL;
960 size_t repolen, len;
961 char *canonpath, *path = NULL;
963 *in_repo_path = NULL;
965 canonpath = strdup(input_path);
966 if (canonpath == NULL) {
967 err = got_error_from_errno("strdup");
968 goto done;
970 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
971 if (err)
972 goto done;
974 repo_abspath = got_repo_get_path(repo);
976 if (canonpath[0] == '\0') {
977 path = strdup(canonpath);
978 if (path == NULL) {
979 err = got_error_from_errno("strdup");
980 goto done;
982 } else {
983 path = realpath(canonpath, NULL);
984 if (path == NULL) {
985 if (errno != ENOENT) {
986 err = got_error_from_errno2("realpath",
987 canonpath);
988 goto done;
990 /*
991 * Path is not on disk.
992 * Assume it is already relative to repository root.
993 */
994 path = strdup(canonpath);
995 if (path == NULL) {
996 err = got_error_from_errno("strdup");
997 goto done;
1001 repolen = strlen(repo_abspath);
1002 len = strlen(path);
1005 if (strcmp(path, repo_abspath) == 0) {
1006 free(path);
1007 path = strdup("");
1008 if (path == NULL) {
1009 err = got_error_from_errno("strdup");
1010 goto done;
1012 } else if (len > repolen &&
1013 got_path_is_child(path, repo_abspath, repolen)) {
1014 /* Matched an on-disk path inside repository. */
1015 if (got_repo_is_bare(repo)) {
1017 * Matched an on-disk path inside repository
1018 * database. Treat input as repository-relative.
1020 free(path);
1021 path = canonpath;
1022 canonpath = NULL;
1023 } else {
1024 char *child;
1025 /* Strip common prefix with repository path. */
1026 err = got_path_skip_common_ancestor(&child,
1027 repo_abspath, path);
1028 if (err)
1029 goto done;
1030 free(path);
1031 path = child;
1033 } else {
1035 * Matched unrelated on-disk path.
1036 * Treat input as repository-relative.
1038 free(path);
1039 path = canonpath;
1040 canonpath = NULL;
1044 /* Make in-repository path absolute */
1045 if (path[0] != '/') {
1046 char *abspath;
1047 if (asprintf(&abspath, "/%s", path) == -1) {
1048 err = got_error_from_errno("asprintf");
1049 goto done;
1051 free(path);
1052 path = abspath;
1055 done:
1056 free(canonpath);
1057 if (err)
1058 free(path);
1059 else
1060 *in_repo_path = path;
1061 return err;
1064 static const struct got_error *
1065 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1066 const char *path_packidx)
1068 const struct got_error *err = NULL;
1069 size_t i;
1071 for (i = 0; i < repo->pack_cache_size; i++) {
1072 if (repo->packidx_cache[i] == NULL)
1073 break;
1074 if (strcmp(repo->packidx_cache[i]->path_packidx,
1075 path_packidx) == 0) {
1076 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1079 if (i == repo->pack_cache_size) {
1080 do {
1081 i--;
1082 } while (i > 0 && repo->pinned_packidx >= 0 &&
1083 i == repo->pinned_packidx);
1084 err = got_packidx_close(repo->packidx_cache[i]);
1085 if (err)
1086 return err;
1089 repo->packidx_cache[i] = packidx;
1091 return NULL;
1094 int
1095 got_repo_is_packidx_filename(const char *name, size_t len)
1097 if (len != GOT_PACKIDX_NAMELEN)
1098 return 0;
1100 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1101 return 0;
1103 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1104 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1105 return 0;
1107 return 1;
1110 static struct got_packidx_bloom_filter *
1111 get_packidx_bloom_filter(struct got_repository *repo,
1112 const char *path, size_t path_len)
1114 struct got_packidx_bloom_filter key;
1116 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1117 return NULL; /* XXX */
1118 key.path_len = path_len;
1120 return RB_FIND(got_packidx_bloom_filter_tree,
1121 &repo->packidx_bloom_filters, &key);
1124 int
1125 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1126 const char *path_packidx, struct got_object_id *id)
1128 struct got_packidx_bloom_filter *bf;
1130 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1131 if (bf)
1132 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1134 /* No bloom filter means this pack index must be searched. */
1135 return 1;
1138 static const struct got_error *
1139 add_packidx_bloom_filter(struct got_repository *repo,
1140 struct got_packidx *packidx, const char *path_packidx)
1142 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1143 struct got_packidx_bloom_filter *bf;
1144 size_t len;
1147 * Don't use bloom filters for very large pack index files.
1148 * Large pack files will contain a relatively large fraction
1149 * of our objects so we will likely need to visit them anyway.
1150 * The more objects a pack file contains the higher the probability
1151 * of a false-positive match from the bloom filter. And reading
1152 * all object IDs from a large pack index file can be expensive.
1154 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1155 return NULL;
1157 /* Do we already have a filter for this pack index? */
1158 if (get_packidx_bloom_filter(repo, path_packidx,
1159 strlen(path_packidx)) != NULL)
1160 return NULL;
1162 bf = calloc(1, sizeof(*bf));
1163 if (bf == NULL)
1164 return got_error_from_errno("calloc");
1165 bf->bloom = calloc(1, sizeof(*bf->bloom));
1166 if (bf->bloom == NULL) {
1167 free(bf);
1168 return got_error_from_errno("calloc");
1171 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1172 if (len >= sizeof(bf->path)) {
1173 free(bf->bloom);
1174 free(bf);
1175 return got_error(GOT_ERR_NO_SPACE);
1177 bf->path_len = len;
1179 /* Minimum size supported by our bloom filter is 1000 entries. */
1180 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1181 for (i = 0; i < nobjects; i++) {
1182 struct got_packidx_object_id *id;
1183 id = &packidx->hdr.sorted_ids[i];
1184 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1187 RB_INSERT(got_packidx_bloom_filter_tree,
1188 &repo->packidx_bloom_filters, bf);
1189 return NULL;
1192 const struct got_error *
1193 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1194 struct got_repository *repo, struct got_object_id *id)
1196 const struct got_error *err;
1197 struct got_pathlist_entry *pe;
1198 size_t i;
1200 /* Search pack index cache. */
1201 for (i = 0; i < repo->pack_cache_size; i++) {
1202 if (repo->packidx_cache[i] == NULL)
1203 break;
1204 if (!got_repo_check_packidx_bloom_filter(repo,
1205 repo->packidx_cache[i]->path_packidx, id))
1206 continue; /* object will not be found in this index */
1207 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1208 if (*idx != -1) {
1209 *packidx = repo->packidx_cache[i];
1211 * Move this cache entry to the front. Repeatedly
1212 * searching a wrong pack index can be expensive.
1214 if (i > 0) {
1215 memmove(&repo->packidx_cache[1],
1216 &repo->packidx_cache[0],
1217 i * sizeof(repo->packidx_cache[0]));
1218 repo->packidx_cache[0] = *packidx;
1219 if (repo->pinned_packidx >= 0 &&
1220 repo->pinned_packidx < i)
1221 repo->pinned_packidx++;
1222 else if (repo->pinned_packidx == i)
1223 repo->pinned_packidx = 0;
1225 return NULL;
1228 /* No luck. Search the filesystem. */
1230 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1231 const char *path_packidx = pe->path;
1232 int is_cached = 0;
1234 if (!got_repo_check_packidx_bloom_filter(repo,
1235 pe->path, id))
1236 continue; /* object will not be found in this index */
1238 for (i = 0; i < repo->pack_cache_size; i++) {
1239 if (repo->packidx_cache[i] == NULL)
1240 break;
1241 if (strcmp(repo->packidx_cache[i]->path_packidx,
1242 path_packidx) == 0) {
1243 is_cached = 1;
1244 break;
1247 if (is_cached)
1248 continue; /* already searched */
1250 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1251 path_packidx, 0);
1252 if (err)
1253 goto done;
1255 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1256 if (err)
1257 goto done;
1259 err = cache_packidx(repo, *packidx, path_packidx);
1260 if (err)
1261 goto done;
1263 *idx = got_packidx_get_object_idx(*packidx, id);
1264 if (*idx != -1) {
1265 err = NULL; /* found the object */
1266 goto done;
1270 err = got_error_no_obj(id);
1271 done:
1272 return err;
1275 const struct got_error *
1276 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1277 struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 DIR *packdir = NULL;
1281 struct dirent *dent;
1282 char *path_packidx = NULL;
1283 int packdir_fd;
1285 packdir_fd = openat(got_repo_get_fd(repo),
1286 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1287 if (packdir_fd == -1) {
1288 return got_error_from_errno_fmt("openat: %s/%s",
1289 got_repo_get_path_git_dir(repo),
1290 GOT_OBJECTS_PACK_DIR);
1293 packdir = fdopendir(packdir_fd);
1294 if (packdir == NULL) {
1295 err = got_error_from_errno("fdopendir");
1296 goto done;
1299 while ((dent = readdir(packdir)) != NULL) {
1300 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1301 continue;
1303 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1304 dent->d_name) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 path_packidx = NULL;
1307 break;
1310 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1311 if (err)
1312 break;
1314 done:
1315 if (err)
1316 free(path_packidx);
1317 if (packdir && closedir(packdir) != 0 && err == NULL)
1318 err = got_error_from_errno("closedir");
1319 return err;
1322 const struct got_error *
1323 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1324 struct got_repository *repo)
1326 const struct got_error *err;
1327 size_t i;
1329 *packidx = NULL;
1331 /* Search pack index cache. */
1332 for (i = 0; i < repo->pack_cache_size; i++) {
1333 if (repo->packidx_cache[i] == NULL)
1334 break;
1335 if (strcmp(repo->packidx_cache[i]->path_packidx,
1336 path_packidx) == 0) {
1337 *packidx = repo->packidx_cache[i];
1338 return NULL;
1341 /* No luck. Search the filesystem. */
1343 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1344 path_packidx, 0);
1345 if (err)
1346 return err;
1348 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1349 if (err)
1350 goto done;
1352 err = cache_packidx(repo, *packidx, path_packidx);
1353 done:
1354 if (err) {
1355 got_packidx_close(*packidx);
1356 *packidx = NULL;
1358 return err;
1361 static const struct got_error *
1362 read_packfile_hdr(int fd, struct got_packidx *packidx)
1364 const struct got_error *err = NULL;
1365 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1366 struct got_packfile_hdr hdr;
1367 ssize_t n;
1369 n = read(fd, &hdr, sizeof(hdr));
1370 if (n < 0)
1371 return got_error_from_errno("read");
1372 if (n != sizeof(hdr))
1373 return got_error(GOT_ERR_BAD_PACKFILE);
1375 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1376 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1377 be32toh(hdr.nobjects) != totobj)
1378 err = got_error(GOT_ERR_BAD_PACKFILE);
1380 return err;
1383 static const struct got_error *
1384 open_packfile(int *fd, struct got_repository *repo,
1385 const char *relpath, struct got_packidx *packidx)
1387 const struct got_error *err = NULL;
1389 *fd = openat(got_repo_get_fd(repo), relpath,
1390 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1391 if (*fd == -1)
1392 return got_error_from_errno_fmt("openat: %s/%s",
1393 got_repo_get_path_git_dir(repo), relpath);
1395 if (packidx) {
1396 err = read_packfile_hdr(*fd, packidx);
1397 if (err) {
1398 close(*fd);
1399 *fd = -1;
1403 return err;
1406 const struct got_error *
1407 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1408 const char *path_packfile, struct got_packidx *packidx)
1410 const struct got_error *err = NULL;
1411 struct got_pack *pack = NULL;
1412 struct stat sb;
1413 size_t i;
1415 if (packp)
1416 *packp = NULL;
1418 for (i = 0; i < repo->pack_cache_size; i++) {
1419 pack = &repo->packs[i];
1420 if (pack->path_packfile == NULL)
1421 break;
1422 if (strcmp(pack->path_packfile, path_packfile) == 0)
1423 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1426 if (i == repo->pack_cache_size) {
1427 struct got_pack tmp;
1428 do {
1429 i--;
1430 } while (i > 0 && repo->pinned_pack >= 0 &&
1431 i == repo->pinned_pack);
1432 err = got_pack_close(&repo->packs[i]);
1433 if (err)
1434 return err;
1435 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1436 return got_error_from_errno("ftruncate");
1437 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1438 return got_error_from_errno("ftruncate");
1439 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1440 memcpy(&repo->packs[i], &repo->packs[0],
1441 sizeof(repo->packs[i]));
1442 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1443 if (repo->pinned_pack == 0)
1444 repo->pinned_pack = i;
1445 else if (repo->pinned_pack == i)
1446 repo->pinned_pack = 0;
1447 i = 0;
1450 pack = &repo->packs[i];
1452 pack->path_packfile = strdup(path_packfile);
1453 if (pack->path_packfile == NULL) {
1454 err = got_error_from_errno("strdup");
1455 goto done;
1458 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1459 if (err)
1460 goto done;
1462 if (fstat(pack->fd, &sb) != 0) {
1463 err = got_error_from_errno("fstat");
1464 goto done;
1466 pack->filesize = sb.st_size;
1468 pack->privsep_child = NULL;
1470 #ifndef GOT_PACK_NO_MMAP
1471 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1472 pack->fd, 0);
1473 if (pack->map == MAP_FAILED) {
1474 if (errno != ENOMEM) {
1475 err = got_error_from_errno("mmap");
1476 goto done;
1478 pack->map = NULL; /* fall back to read(2) */
1480 #endif
1481 done:
1482 if (err) {
1483 if (pack) {
1484 free(pack->path_packfile);
1485 memset(pack, 0, sizeof(*pack));
1487 } else if (packp)
1488 *packp = pack;
1489 return err;
1492 struct got_pack *
1493 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1495 struct got_pack *pack = NULL;
1496 size_t i;
1498 for (i = 0; i < repo->pack_cache_size; i++) {
1499 pack = &repo->packs[i];
1500 if (pack->path_packfile == NULL)
1501 break;
1502 if (strcmp(pack->path_packfile, path_packfile) == 0)
1503 return pack;
1506 return NULL;
1509 const struct got_error *
1510 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1511 struct got_pack *pack)
1513 size_t i;
1514 int pinned_pack = -1, pinned_packidx = -1;
1516 for (i = 0; i < repo->pack_cache_size; i++) {
1517 if (repo->packidx_cache[i] &&
1518 strcmp(repo->packidx_cache[i]->path_packidx,
1519 packidx->path_packidx) == 0)
1520 pinned_packidx = i;
1521 if (repo->packs[i].path_packfile &&
1522 strcmp(repo->packs[i].path_packfile,
1523 pack->path_packfile) == 0)
1524 pinned_pack = i;
1527 if (pinned_packidx == -1 || pinned_pack == -1)
1528 return got_error(GOT_ERR_PIN_PACK);
1530 repo->pinned_pack = pinned_pack;
1531 repo->pinned_packidx = pinned_packidx;
1532 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1533 return NULL;
1536 struct got_pack *
1537 got_repo_get_pinned_pack(struct got_repository *repo)
1539 if (repo->pinned_pack >= 0 &&
1540 repo->pinned_pack < repo->pack_cache_size)
1541 return &repo->packs[repo->pinned_pack];
1543 return NULL;
1546 void
1547 got_repo_unpin_pack(struct got_repository *repo)
1549 repo->pinned_packidx = -1;
1550 repo->pinned_pack = -1;
1551 repo->pinned_pid = 0;
1554 const struct got_error *
1555 got_repo_init(const char *repo_path)
1557 const struct got_error *err = NULL;
1558 const char *dirnames[] = {
1559 GOT_OBJECTS_DIR,
1560 GOT_OBJECTS_PACK_DIR,
1561 GOT_REFS_DIR,
1563 const char *description_str = "Unnamed repository; "
1564 "edit this file 'description' to name the repository.";
1565 const char *headref_str = "ref: refs/heads/main";
1566 const char *gitconfig_str = "[core]\n"
1567 "\trepositoryformatversion = 0\n"
1568 "\tfilemode = true\n"
1569 "\tbare = true\n";
1570 char *path;
1571 size_t i;
1573 if (!got_path_dir_is_empty(repo_path))
1574 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1576 for (i = 0; i < nitems(dirnames); i++) {
1577 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1578 return got_error_from_errno("asprintf");
1580 err = got_path_mkdir(path);
1581 free(path);
1582 if (err)
1583 return err;
1586 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1587 return got_error_from_errno("asprintf");
1588 err = got_path_create_file(path, description_str);
1589 free(path);
1590 if (err)
1591 return err;
1593 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1594 return got_error_from_errno("asprintf");
1595 err = got_path_create_file(path, headref_str);
1596 free(path);
1597 if (err)
1598 return err;
1600 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1601 return got_error_from_errno("asprintf");
1602 err = got_path_create_file(path, gitconfig_str);
1603 free(path);
1604 if (err)
1605 return err;
1607 return NULL;
1610 static const struct got_error *
1611 match_packed_object(struct got_object_id **unique_id,
1612 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1614 const struct got_error *err = NULL;
1615 struct got_object_id_queue matched_ids;
1616 struct got_pathlist_entry *pe;
1618 STAILQ_INIT(&matched_ids);
1620 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1621 const char *path_packidx = pe->path;
1622 struct got_packidx *packidx;
1623 struct got_object_qid *qid;
1625 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1626 path_packidx, 0);
1627 if (err)
1628 break;
1630 err = got_packidx_match_id_str_prefix(&matched_ids,
1631 packidx, id_str_prefix);
1632 if (err) {
1633 got_packidx_close(packidx);
1634 break;
1636 err = got_packidx_close(packidx);
1637 if (err)
1638 break;
1640 STAILQ_FOREACH(qid, &matched_ids, entry) {
1641 if (obj_type != GOT_OBJ_TYPE_ANY) {
1642 int matched_type;
1643 err = got_object_get_type(&matched_type, repo,
1644 &qid->id);
1645 if (err)
1646 goto done;
1647 if (matched_type != obj_type)
1648 continue;
1650 if (*unique_id == NULL) {
1651 *unique_id = got_object_id_dup(&qid->id);
1652 if (*unique_id == NULL) {
1653 err = got_error_from_errno("malloc");
1654 goto done;
1656 } else {
1657 if (got_object_id_cmp(*unique_id,
1658 &qid->id) == 0)
1659 continue; /* packed multiple times */
1660 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1661 goto done;
1665 done:
1666 got_object_id_queue_free(&matched_ids);
1667 if (err) {
1668 free(*unique_id);
1669 *unique_id = NULL;
1671 return err;
1674 static const struct got_error *
1675 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1676 const char *object_dir, const char *id_str_prefix, int obj_type,
1677 struct got_repository *repo)
1679 const struct got_error *err = NULL;
1680 char *path;
1681 DIR *dir = NULL;
1682 struct dirent *dent;
1683 struct got_object_id id;
1685 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1686 err = got_error_from_errno("asprintf");
1687 goto done;
1690 dir = opendir(path);
1691 if (dir == NULL) {
1692 if (errno == ENOENT) {
1693 err = NULL;
1694 goto done;
1696 err = got_error_from_errno2("opendir", path);
1697 goto done;
1699 while ((dent = readdir(dir)) != NULL) {
1700 char *id_str;
1701 int cmp;
1703 if (strcmp(dent->d_name, ".") == 0 ||
1704 strcmp(dent->d_name, "..") == 0)
1705 continue;
1707 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1708 err = got_error_from_errno("asprintf");
1709 goto done;
1712 if (!got_parse_sha1_digest(id.sha1, id_str))
1713 continue;
1716 * Directory entries do not necessarily appear in
1717 * sorted order, so we must iterate over all of them.
1719 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1720 if (cmp != 0) {
1721 free(id_str);
1722 continue;
1725 if (*unique_id == NULL) {
1726 if (obj_type != GOT_OBJ_TYPE_ANY) {
1727 int matched_type;
1728 err = got_object_get_type(&matched_type, repo,
1729 &id);
1730 if (err)
1731 goto done;
1732 if (matched_type != obj_type)
1733 continue;
1735 *unique_id = got_object_id_dup(&id);
1736 if (*unique_id == NULL) {
1737 err = got_error_from_errno("got_object_id_dup");
1738 free(id_str);
1739 goto done;
1741 } else {
1742 if (got_object_id_cmp(*unique_id, &id) == 0)
1743 continue; /* both packed and loose */
1744 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1745 free(id_str);
1746 goto done;
1749 done:
1750 if (dir && closedir(dir) != 0 && err == NULL)
1751 err = got_error_from_errno("closedir");
1752 if (err) {
1753 free(*unique_id);
1754 *unique_id = NULL;
1756 free(path);
1757 return err;
1760 const struct got_error *
1761 got_repo_match_object_id_prefix(struct got_object_id **id,
1762 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1764 const struct got_error *err = NULL;
1765 char *path_objects = got_repo_get_path_objects(repo);
1766 char *object_dir = NULL;
1767 size_t len;
1768 int i;
1770 *id = NULL;
1772 len = strlen(id_str_prefix);
1773 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1774 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 for (i = 0; i < len; i++) {
1777 if (isxdigit((unsigned char)id_str_prefix[i]))
1778 continue;
1779 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1782 if (len >= 2) {
1783 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1784 if (err)
1785 goto done;
1786 object_dir = strndup(id_str_prefix, 2);
1787 if (object_dir == NULL) {
1788 err = got_error_from_errno("strdup");
1789 goto done;
1791 err = match_loose_object(id, path_objects, object_dir,
1792 id_str_prefix, obj_type, repo);
1793 } else if (len == 1) {
1794 int i;
1795 for (i = 0; i < 0xf; i++) {
1796 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1797 == -1) {
1798 err = got_error_from_errno("asprintf");
1799 goto done;
1801 err = match_packed_object(id, repo, object_dir,
1802 obj_type);
1803 if (err)
1804 goto done;
1805 err = match_loose_object(id, path_objects, object_dir,
1806 id_str_prefix, obj_type, repo);
1807 if (err)
1808 goto done;
1810 } else {
1811 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1812 goto done;
1814 done:
1815 free(object_dir);
1816 if (err) {
1817 free(*id);
1818 *id = NULL;
1819 } else if (*id == NULL) {
1820 switch (obj_type) {
1821 case GOT_OBJ_TYPE_BLOB:
1822 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1823 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1824 break;
1825 case GOT_OBJ_TYPE_TREE:
1826 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1827 GOT_OBJ_LABEL_TREE, id_str_prefix);
1828 break;
1829 case GOT_OBJ_TYPE_COMMIT:
1830 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1831 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1832 break;
1833 case GOT_OBJ_TYPE_TAG:
1834 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1835 GOT_OBJ_LABEL_TAG, id_str_prefix);
1836 break;
1837 default:
1838 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1839 break;
1843 return err;
1846 const struct got_error *
1847 got_repo_match_object_id(struct got_object_id **id, char **label,
1848 const char *id_str, int obj_type, struct got_reflist_head *refs,
1849 struct got_repository *repo)
1851 const struct got_error *err;
1852 struct got_tag_object *tag;
1853 struct got_reference *ref = NULL;
1855 *id = NULL;
1856 if (label)
1857 *label = NULL;
1859 if (refs) {
1860 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1861 refs, repo);
1862 if (err == NULL) {
1863 *id = got_object_id_dup(
1864 got_object_tag_get_object_id(tag));
1865 if (*id == NULL)
1866 err = got_error_from_errno("got_object_id_dup");
1867 else if (label && asprintf(label, "refs/tags/%s",
1868 got_object_tag_get_name(tag)) == -1) {
1869 err = got_error_from_errno("asprintf");
1870 free(*id);
1871 *id = NULL;
1873 got_object_tag_close(tag);
1874 return err;
1875 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1876 err->code != GOT_ERR_NO_OBJ)
1877 return err;
1880 err = got_ref_open(&ref, repo, id_str, 0);
1881 if (err == NULL) {
1882 err = got_ref_resolve(id, repo, ref);
1883 if (err)
1884 goto done;
1885 if (label) {
1886 *label = strdup(got_ref_get_name(ref));
1887 if (*label == NULL) {
1888 err = got_error_from_errno("strdup");
1889 goto done;
1892 } else {
1893 if (err->code != GOT_ERR_NOT_REF &&
1894 err->code != GOT_ERR_BAD_REF_NAME)
1895 goto done;
1896 err = got_repo_match_object_id_prefix(id, id_str,
1897 obj_type, repo);
1898 if (err) {
1899 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1900 err = got_error_not_ref(id_str);
1901 goto done;
1903 if (label) {
1904 err = got_object_id_str(label, *id);
1905 if (*label == NULL) {
1906 err = got_error_from_errno("strdup");
1907 goto done;
1911 done:
1912 if (ref)
1913 got_ref_close(ref);
1914 return err;
1917 const struct got_error *
1918 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1919 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1921 const struct got_error *err = NULL;
1922 struct got_reflist_entry *re;
1923 struct got_object_id *tag_id;
1924 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1926 *tag = NULL;
1928 TAILQ_FOREACH(re, refs, entry) {
1929 const char *refname;
1930 refname = got_ref_get_name(re->ref);
1931 if (got_ref_is_symbolic(re->ref))
1932 continue;
1933 if (strncmp(refname, "refs/tags/", 10) != 0)
1934 continue;
1935 if (!name_is_absolute)
1936 refname += strlen("refs/tags/");
1937 if (strcmp(refname, name) != 0)
1938 continue;
1939 err = got_ref_resolve(&tag_id, repo, re->ref);
1940 if (err)
1941 break;
1942 err = got_object_open_as_tag(tag, repo, tag_id);
1943 free(tag_id);
1944 if (err)
1945 break;
1946 if (obj_type == GOT_OBJ_TYPE_ANY ||
1947 got_object_tag_get_object_type(*tag) == obj_type)
1948 break;
1949 got_object_tag_close(*tag);
1950 *tag = NULL;
1953 if (err == NULL && *tag == NULL)
1954 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1955 GOT_OBJ_LABEL_TAG, name);
1956 return err;
1959 static const struct got_error *
1960 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1961 const char *name, mode_t mode, struct got_object_id *blob_id)
1963 const struct got_error *err = NULL;
1965 *new_te = NULL;
1967 *new_te = calloc(1, sizeof(**new_te));
1968 if (*new_te == NULL)
1969 return got_error_from_errno("calloc");
1971 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1972 sizeof((*new_te)->name)) {
1973 err = got_error(GOT_ERR_NO_SPACE);
1974 goto done;
1977 if (S_ISLNK(mode)) {
1978 (*new_te)->mode = S_IFLNK;
1979 } else {
1980 (*new_te)->mode = S_IFREG;
1981 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1983 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1984 done:
1985 if (err && *new_te) {
1986 free(*new_te);
1987 *new_te = NULL;
1989 return err;
1992 static const struct got_error *
1993 import_file(struct got_tree_entry **new_te, struct dirent *de,
1994 const char *path, struct got_repository *repo)
1996 const struct got_error *err;
1997 struct got_object_id *blob_id = NULL;
1998 char *filepath;
1999 struct stat sb;
2001 if (asprintf(&filepath, "%s%s%s", path,
2002 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (lstat(filepath, &sb) != 0) {
2006 err = got_error_from_errno2("lstat", path);
2007 goto done;
2010 err = got_object_blob_create(&blob_id, filepath, repo);
2011 if (err)
2012 goto done;
2014 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2015 blob_id);
2016 done:
2017 free(filepath);
2018 if (err)
2019 free(blob_id);
2020 return err;
2023 static const struct got_error *
2024 insert_tree_entry(struct got_tree_entry *new_te,
2025 struct got_pathlist_head *paths)
2027 const struct got_error *err = NULL;
2028 struct got_pathlist_entry *new_pe;
2030 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2031 if (err)
2032 return err;
2033 if (new_pe == NULL)
2034 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2035 return NULL;
2038 static const struct got_error *write_tree(struct got_object_id **,
2039 const char *, struct got_pathlist_head *, struct got_repository *,
2040 got_repo_import_cb progress_cb, void *progress_arg);
2042 static const struct got_error *
2043 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2044 const char *path, struct got_pathlist_head *ignores,
2045 struct got_repository *repo,
2046 got_repo_import_cb progress_cb, void *progress_arg)
2048 const struct got_error *err;
2049 struct got_object_id *id = NULL;
2050 char *subdirpath;
2052 if (asprintf(&subdirpath, "%s%s%s", path,
2053 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2054 return got_error_from_errno("asprintf");
2056 (*new_te) = calloc(1, sizeof(**new_te));
2057 if (*new_te == NULL)
2058 return got_error_from_errno("calloc");
2059 (*new_te)->mode = S_IFDIR;
2060 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2061 sizeof((*new_te)->name)) {
2062 err = got_error(GOT_ERR_NO_SPACE);
2063 goto done;
2065 err = write_tree(&id, subdirpath, ignores, repo,
2066 progress_cb, progress_arg);
2067 if (err)
2068 goto done;
2069 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2071 done:
2072 free(id);
2073 free(subdirpath);
2074 if (err) {
2075 free(*new_te);
2076 *new_te = NULL;
2078 return err;
2081 static const struct got_error *
2082 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2083 struct got_pathlist_head *ignores, struct got_repository *repo,
2084 got_repo_import_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 DIR *dir;
2088 struct dirent *de;
2089 int nentries;
2090 struct got_tree_entry *new_te = NULL;
2091 struct got_pathlist_head paths;
2092 struct got_pathlist_entry *pe;
2094 *new_tree_id = NULL;
2096 TAILQ_INIT(&paths);
2098 dir = opendir(path_dir);
2099 if (dir == NULL) {
2100 err = got_error_from_errno2("opendir", path_dir);
2101 goto done;
2104 nentries = 0;
2105 while ((de = readdir(dir)) != NULL) {
2106 int ignore = 0;
2107 int type;
2109 if (strcmp(de->d_name, ".") == 0 ||
2110 strcmp(de->d_name, "..") == 0)
2111 continue;
2113 TAILQ_FOREACH(pe, ignores, entry) {
2114 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2115 ignore = 1;
2116 break;
2119 if (ignore)
2120 continue;
2122 err = got_path_dirent_type(&type, path_dir, de);
2123 if (err)
2124 goto done;
2126 if (type == DT_DIR) {
2127 err = import_subdir(&new_te, de, path_dir,
2128 ignores, repo, progress_cb, progress_arg);
2129 if (err) {
2130 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2131 goto done;
2132 err = NULL;
2133 continue;
2135 } else if (type == DT_REG || type == DT_LNK) {
2136 err = import_file(&new_te, de, path_dir, repo);
2137 if (err)
2138 goto done;
2139 } else
2140 continue;
2142 err = insert_tree_entry(new_te, &paths);
2143 if (err)
2144 goto done;
2145 nentries++;
2148 if (TAILQ_EMPTY(&paths)) {
2149 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2150 "cannot create tree without any entries");
2151 goto done;
2154 TAILQ_FOREACH(pe, &paths, entry) {
2155 struct got_tree_entry *te = pe->data;
2156 char *path;
2157 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2158 continue;
2159 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2160 err = got_error_from_errno("asprintf");
2161 goto done;
2163 err = (*progress_cb)(progress_arg, path);
2164 free(path);
2165 if (err)
2166 goto done;
2169 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2170 done:
2171 if (dir)
2172 closedir(dir);
2173 got_pathlist_free(&paths);
2174 return err;
2177 const struct got_error *
2178 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2179 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2180 struct got_repository *repo, got_repo_import_cb progress_cb,
2181 void *progress_arg)
2183 const struct got_error *err;
2184 struct got_object_id *new_tree_id;
2186 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2187 progress_cb, progress_arg);
2188 if (err)
2189 return err;
2191 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2192 author, time(NULL), author, time(NULL), logmsg, repo);
2193 free(new_tree_id);
2194 return err;
2197 const struct got_error *
2198 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2199 struct got_repository *repo)
2201 const struct got_error *err = NULL;
2202 char *path_objects = NULL, *path = NULL;
2203 DIR *dir = NULL;
2204 struct got_object_id id;
2205 int i;
2207 *nobjects = 0;
2208 *ondisk_size = 0;
2210 path_objects = got_repo_get_path_objects(repo);
2211 if (path_objects == NULL)
2212 return got_error_from_errno("got_repo_get_path_objects");
2214 for (i = 0; i <= 0xff; i++) {
2215 struct dirent *dent;
2217 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 break;
2222 dir = opendir(path);
2223 if (dir == NULL) {
2224 if (errno == ENOENT) {
2225 err = NULL;
2226 continue;
2228 err = got_error_from_errno2("opendir", path);
2229 break;
2232 while ((dent = readdir(dir)) != NULL) {
2233 char *id_str;
2234 int fd;
2235 struct stat sb;
2237 if (strcmp(dent->d_name, ".") == 0 ||
2238 strcmp(dent->d_name, "..") == 0)
2239 continue;
2241 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 goto done;
2246 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2247 free(id_str);
2248 continue;
2250 free(id_str);
2252 err = got_object_open_loose_fd(&fd, &id, repo);
2253 if (err)
2254 goto done;
2256 if (fstat(fd, &sb) == -1) {
2257 err = got_error_from_errno("fstat");
2258 close(fd);
2259 goto done;
2261 (*nobjects)++;
2262 (*ondisk_size) += sb.st_size;
2264 if (close(fd) == -1) {
2265 err = got_error_from_errno("close");
2266 goto done;
2270 if (closedir(dir) != 0) {
2271 err = got_error_from_errno("closedir");
2272 goto done;
2274 dir = NULL;
2276 free(path);
2277 path = NULL;
2279 done:
2280 if (dir && closedir(dir) != 0 && err == NULL)
2281 err = got_error_from_errno("closedir");
2283 if (err) {
2284 *nobjects = 0;
2285 *ondisk_size = 0;
2287 free(path_objects);
2288 free(path);
2289 return err;
2292 const struct got_error *
2293 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2294 off_t *total_packsize, struct got_repository *repo)
2296 const struct got_error *err = NULL;
2297 DIR *packdir = NULL;
2298 struct dirent *dent;
2299 struct got_packidx *packidx = NULL;
2300 char *path_packidx;
2301 char *path_packfile;
2302 int packdir_fd;
2303 struct stat sb;
2305 *npackfiles = 0;
2306 *nobjects = 0;
2307 *total_packsize = 0;
2309 packdir_fd = openat(got_repo_get_fd(repo),
2310 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2311 if (packdir_fd == -1) {
2312 return got_error_from_errno_fmt("openat: %s/%s",
2313 got_repo_get_path_git_dir(repo),
2314 GOT_OBJECTS_PACK_DIR);
2317 packdir = fdopendir(packdir_fd);
2318 if (packdir == NULL) {
2319 err = got_error_from_errno("fdopendir");
2320 goto done;
2323 while ((dent = readdir(packdir)) != NULL) {
2324 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2325 continue;
2327 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2328 dent->d_name) == -1) {
2329 err = got_error_from_errno("asprintf");
2330 goto done;
2333 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2334 path_packidx, 0);
2335 free(path_packidx);
2336 if (err)
2337 goto done;
2339 if (fstat(packidx->fd, &sb) == -1)
2340 goto done;
2341 *total_packsize += sb.st_size;
2343 err = got_packidx_get_packfile_path(&path_packfile,
2344 packidx->path_packidx);
2345 if (err)
2346 goto done;
2348 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2349 0) == -1) {
2350 free(path_packfile);
2351 goto done;
2353 free(path_packfile);
2354 *total_packsize += sb.st_size;
2356 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2358 (*npackfiles)++;
2360 got_packidx_close(packidx);
2361 packidx = NULL;
2363 done:
2364 if (packidx)
2365 got_packidx_close(packidx);
2366 if (packdir && closedir(packdir) != 0 && err == NULL)
2367 err = got_error_from_errno("closedir");
2368 if (err) {
2369 *npackfiles = 0;
2370 *nobjects = 0;
2371 *total_packsize = 0;
2373 return err;
2376 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2377 got_packidx_bloom_filter_cmp);