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->ngitconfig_remotes = 0;
692 free(repo->gitconfig_author_name);
693 repo->gitconfig_author_name = NULL;
694 free(repo->gitconfig_author_email);
695 repo->gitconfig_author_email = NULL;
698 done:
699 free(repo_gitconfig_path);
700 return err;
703 static const struct got_error *
704 read_gotconfig(struct got_repository *repo)
706 const struct got_error *err = NULL;
707 char *gotconfig_path;
709 gotconfig_path = got_repo_get_path_gotconfig(repo);
710 if (gotconfig_path == NULL)
711 return got_error_from_errno("got_repo_get_path_gotconfig");
713 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
714 free(gotconfig_path);
715 return err;
718 /* Supported repository format extensions. */
719 static const char *const repo_extensions[] = {
720 "noop", /* Got supports repository format version 1. */
721 "preciousObjects", /* Supported by gotadmin cleanup. */
722 "worktreeConfig", /* Got does not care about Git work trees. */
723 };
725 const struct got_error *
726 got_repo_open(struct got_repository **repop, const char *path,
727 const char *global_gitconfig_path, int *pack_fds)
729 struct got_repository *repo = NULL;
730 const struct got_error *err = NULL;
731 char *repo_path = NULL;
732 size_t i, j = 0;
733 struct rlimit rl;
735 *repop = NULL;
737 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
738 return got_error_from_errno("getrlimit");
740 repo = calloc(1, sizeof(*repo));
741 if (repo == NULL)
742 return got_error_from_errno("calloc");
744 RB_INIT(&repo->packidx_bloom_filters);
745 TAILQ_INIT(&repo->packidx_paths);
747 for (i = 0; i < nitems(repo->privsep_children); i++) {
748 memset(&repo->privsep_children[i], 0,
749 sizeof(repo->privsep_children[0]));
750 repo->privsep_children[i].imsg_fd = -1;
753 err = got_object_cache_init(&repo->objcache,
754 GOT_OBJECT_CACHE_TYPE_OBJ);
755 if (err)
756 goto done;
757 err = got_object_cache_init(&repo->treecache,
758 GOT_OBJECT_CACHE_TYPE_TREE);
759 if (err)
760 goto done;
761 err = got_object_cache_init(&repo->commitcache,
762 GOT_OBJECT_CACHE_TYPE_COMMIT);
763 if (err)
764 goto done;
765 err = got_object_cache_init(&repo->tagcache,
766 GOT_OBJECT_CACHE_TYPE_TAG);
767 if (err)
768 goto done;
769 err = got_object_cache_init(&repo->rawcache,
770 GOT_OBJECT_CACHE_TYPE_RAW);
771 if (err)
772 goto done;
774 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
775 if (repo->pack_cache_size > rl.rlim_cur / 8)
776 repo->pack_cache_size = rl.rlim_cur / 8;
777 for (i = 0; i < nitems(repo->packs); i++) {
778 if (i < repo->pack_cache_size) {
779 repo->packs[i].basefd = pack_fds[j++];
780 repo->packs[i].accumfd = pack_fds[j++];
781 } else {
782 repo->packs[i].basefd = -1;
783 repo->packs[i].accumfd = -1;
786 repo->pinned_pack = -1;
787 repo->pinned_packidx = -1;
788 repo->pinned_pid = 0;
790 repo_path = realpath(path, NULL);
791 if (repo_path == NULL) {
792 err = got_error_from_errno2("realpath", path);
793 goto done;
796 for (;;) {
797 char *parent_path;
799 err = open_repo(repo, repo_path);
800 if (err == NULL)
801 break;
802 if (err->code != GOT_ERR_NOT_GIT_REPO)
803 goto done;
804 if (repo_path[0] == '/' && repo_path[1] == '\0') {
805 err = got_error(GOT_ERR_NOT_GIT_REPO);
806 goto done;
808 err = got_path_dirname(&parent_path, repo_path);
809 if (err)
810 goto done;
811 free(repo_path);
812 repo_path = parent_path;
815 err = read_gotconfig(repo);
816 if (err)
817 goto done;
819 err = read_gitconfig(repo, global_gitconfig_path);
820 if (err)
821 goto done;
822 if (repo->gitconfig_repository_format_version != 0) {
823 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
824 goto done;
826 for (i = 0; i < repo->nextensions; i++) {
827 char *ext = repo->extensions[i];
828 int j, supported = 0;
829 for (j = 0; j < nitems(repo_extensions); j++) {
830 if (strcmp(ext, repo_extensions[j]) == 0) {
831 supported = 1;
832 break;
835 if (!supported) {
836 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
837 goto done;
841 err = got_repo_list_packidx(&repo->packidx_paths, repo);
842 done:
843 if (err)
844 got_repo_close(repo);
845 else
846 *repop = repo;
847 free(repo_path);
848 return err;
851 const struct got_error *
852 got_repo_close(struct got_repository *repo)
854 const struct got_error *err = NULL, *child_err;
855 struct got_packidx_bloom_filter *bf;
856 struct got_pathlist_entry *pe;
857 size_t i;
859 for (i = 0; i < repo->pack_cache_size; i++) {
860 if (repo->packidx_cache[i] == NULL)
861 break;
862 got_packidx_close(repo->packidx_cache[i]);
865 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
866 &repo->packidx_bloom_filters))) {
867 RB_REMOVE(got_packidx_bloom_filter_tree,
868 &repo->packidx_bloom_filters, bf);
869 free(bf->bloom);
870 free(bf);
873 for (i = 0; i < repo->pack_cache_size; i++)
874 if (repo->packs[i].path_packfile)
875 if (repo->packs[i].path_packfile)
876 got_pack_close(&repo->packs[i]);
878 free(repo->path);
879 free(repo->path_git_dir);
881 got_object_cache_close(&repo->objcache);
882 got_object_cache_close(&repo->treecache);
883 got_object_cache_close(&repo->commitcache);
884 got_object_cache_close(&repo->tagcache);
885 got_object_cache_close(&repo->rawcache);
887 for (i = 0; i < nitems(repo->privsep_children); i++) {
888 if (repo->privsep_children[i].imsg_fd == -1)
889 continue;
890 imsg_clear(repo->privsep_children[i].ibuf);
891 free(repo->privsep_children[i].ibuf);
892 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
893 child_err = got_privsep_wait_for_child(
894 repo->privsep_children[i].pid);
895 if (child_err && err == NULL)
896 err = child_err;
897 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
898 err == NULL)
899 err = got_error_from_errno("close");
902 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
903 err == NULL)
904 err = got_error_from_errno("close");
906 if (repo->gotconfig)
907 got_gotconfig_free(repo->gotconfig);
908 free(repo->gitconfig_author_name);
909 free(repo->gitconfig_author_email);
910 for (i = 0; i < repo->ngitconfig_remotes; i++)
911 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
912 free(repo->gitconfig_remotes);
913 for (i = 0; i < repo->nextensions; i++)
914 free(repo->extensions[i]);
915 free(repo->extensions);
917 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
918 free((void *)pe->path);
919 got_pathlist_free(&repo->packidx_paths);
920 free(repo);
922 return err;
925 void
926 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
928 int i;
930 free(repo->name);
931 repo->name = NULL;
932 free(repo->fetch_url);
933 repo->fetch_url = NULL;
934 free(repo->send_url);
935 repo->send_url = NULL;
936 for (i = 0; i < repo->nfetch_branches; i++)
937 free(repo->fetch_branches[i]);
938 free(repo->fetch_branches);
939 repo->fetch_branches = NULL;
940 repo->nfetch_branches = 0;
941 for (i = 0; i < repo->nsend_branches; i++)
942 free(repo->send_branches[i]);
943 free(repo->send_branches);
944 repo->send_branches = NULL;
945 repo->nsend_branches = 0;
948 const struct got_error *
949 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
950 const char *input_path)
952 const struct got_error *err = NULL;
953 const char *repo_abspath = NULL;
954 size_t repolen, len;
955 char *canonpath, *path = NULL;
957 *in_repo_path = NULL;
959 canonpath = strdup(input_path);
960 if (canonpath == NULL) {
961 err = got_error_from_errno("strdup");
962 goto done;
964 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
965 if (err)
966 goto done;
968 repo_abspath = got_repo_get_path(repo);
970 if (canonpath[0] == '\0') {
971 path = strdup(canonpath);
972 if (path == NULL) {
973 err = got_error_from_errno("strdup");
974 goto done;
976 } else {
977 path = realpath(canonpath, NULL);
978 if (path == NULL) {
979 if (errno != ENOENT) {
980 err = got_error_from_errno2("realpath",
981 canonpath);
982 goto done;
984 /*
985 * Path is not on disk.
986 * Assume it is already relative to repository root.
987 */
988 path = strdup(canonpath);
989 if (path == NULL) {
990 err = got_error_from_errno("strdup");
991 goto done;
995 repolen = strlen(repo_abspath);
996 len = strlen(path);
999 if (strcmp(path, repo_abspath) == 0) {
1000 free(path);
1001 path = strdup("");
1002 if (path == NULL) {
1003 err = got_error_from_errno("strdup");
1004 goto done;
1006 } else if (len > repolen &&
1007 got_path_is_child(path, repo_abspath, repolen)) {
1008 /* Matched an on-disk path inside repository. */
1009 if (got_repo_is_bare(repo)) {
1011 * Matched an on-disk path inside repository
1012 * database. Treat input as repository-relative.
1014 free(path);
1015 path = canonpath;
1016 canonpath = NULL;
1017 } else {
1018 char *child;
1019 /* Strip common prefix with repository path. */
1020 err = got_path_skip_common_ancestor(&child,
1021 repo_abspath, path);
1022 if (err)
1023 goto done;
1024 free(path);
1025 path = child;
1027 } else {
1029 * Matched unrelated on-disk path.
1030 * Treat input as repository-relative.
1032 free(path);
1033 path = canonpath;
1034 canonpath = NULL;
1038 /* Make in-repository path absolute */
1039 if (path[0] != '/') {
1040 char *abspath;
1041 if (asprintf(&abspath, "/%s", path) == -1) {
1042 err = got_error_from_errno("asprintf");
1043 goto done;
1045 free(path);
1046 path = abspath;
1049 done:
1050 free(canonpath);
1051 if (err)
1052 free(path);
1053 else
1054 *in_repo_path = path;
1055 return err;
1058 static const struct got_error *
1059 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1060 const char *path_packidx)
1062 const struct got_error *err = NULL;
1063 size_t i;
1065 for (i = 0; i < repo->pack_cache_size; i++) {
1066 if (repo->packidx_cache[i] == NULL)
1067 break;
1068 if (strcmp(repo->packidx_cache[i]->path_packidx,
1069 path_packidx) == 0) {
1070 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1073 if (i == repo->pack_cache_size) {
1074 do {
1075 i--;
1076 } while (i > 0 && repo->pinned_packidx >= 0 &&
1077 i == repo->pinned_packidx);
1078 err = got_packidx_close(repo->packidx_cache[i]);
1079 if (err)
1080 return err;
1083 repo->packidx_cache[i] = packidx;
1085 return NULL;
1088 int
1089 got_repo_is_packidx_filename(const char *name, size_t len)
1091 if (len != GOT_PACKIDX_NAMELEN)
1092 return 0;
1094 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1095 return 0;
1097 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1098 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1099 return 0;
1101 return 1;
1104 static struct got_packidx_bloom_filter *
1105 get_packidx_bloom_filter(struct got_repository *repo,
1106 const char *path, size_t path_len)
1108 struct got_packidx_bloom_filter key;
1110 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1111 return NULL; /* XXX */
1112 key.path_len = path_len;
1114 return RB_FIND(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, &key);
1118 int
1119 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1120 const char *path_packidx, struct got_object_id *id)
1122 struct got_packidx_bloom_filter *bf;
1124 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1125 if (bf)
1126 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1128 /* No bloom filter means this pack index must be searched. */
1129 return 1;
1132 static const struct got_error *
1133 add_packidx_bloom_filter(struct got_repository *repo,
1134 struct got_packidx *packidx, const char *path_packidx)
1136 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1137 struct got_packidx_bloom_filter *bf;
1138 size_t len;
1141 * Don't use bloom filters for very large pack index files.
1142 * Large pack files will contain a relatively large fraction
1143 * of our objects so we will likely need to visit them anyway.
1144 * The more objects a pack file contains the higher the probability
1145 * of a false-positive match from the bloom filter. And reading
1146 * all object IDs from a large pack index file can be expensive.
1148 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1149 return NULL;
1151 /* Do we already have a filter for this pack index? */
1152 if (get_packidx_bloom_filter(repo, path_packidx,
1153 strlen(path_packidx)) != NULL)
1154 return NULL;
1156 bf = calloc(1, sizeof(*bf));
1157 if (bf == NULL)
1158 return got_error_from_errno("calloc");
1159 bf->bloom = calloc(1, sizeof(*bf->bloom));
1160 if (bf->bloom == NULL) {
1161 free(bf);
1162 return got_error_from_errno("calloc");
1165 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1166 if (len >= sizeof(bf->path)) {
1167 free(bf->bloom);
1168 free(bf);
1169 return got_error(GOT_ERR_NO_SPACE);
1171 bf->path_len = len;
1173 /* Minimum size supported by our bloom filter is 1000 entries. */
1174 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1175 for (i = 0; i < nobjects; i++) {
1176 struct got_packidx_object_id *id;
1177 id = &packidx->hdr.sorted_ids[i];
1178 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1181 RB_INSERT(got_packidx_bloom_filter_tree,
1182 &repo->packidx_bloom_filters, bf);
1183 return NULL;
1186 const struct got_error *
1187 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1188 struct got_repository *repo, struct got_object_id *id)
1190 const struct got_error *err;
1191 struct got_pathlist_entry *pe;
1192 size_t i;
1194 /* Search pack index cache. */
1195 for (i = 0; i < repo->pack_cache_size; i++) {
1196 if (repo->packidx_cache[i] == NULL)
1197 break;
1198 if (!got_repo_check_packidx_bloom_filter(repo,
1199 repo->packidx_cache[i]->path_packidx, id))
1200 continue; /* object will not be found in this index */
1201 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1202 if (*idx != -1) {
1203 *packidx = repo->packidx_cache[i];
1205 * Move this cache entry to the front. Repeatedly
1206 * searching a wrong pack index can be expensive.
1208 if (i > 0) {
1209 memmove(&repo->packidx_cache[1],
1210 &repo->packidx_cache[0],
1211 i * sizeof(repo->packidx_cache[0]));
1212 repo->packidx_cache[0] = *packidx;
1213 if (repo->pinned_packidx >= 0 &&
1214 repo->pinned_packidx < i)
1215 repo->pinned_packidx++;
1216 else if (repo->pinned_packidx == i)
1217 repo->pinned_packidx = 0;
1219 return NULL;
1222 /* No luck. Search the filesystem. */
1224 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1225 const char *path_packidx = pe->path;
1226 int is_cached = 0;
1228 if (!got_repo_check_packidx_bloom_filter(repo,
1229 pe->path, id))
1230 continue; /* object will not be found in this index */
1232 for (i = 0; i < repo->pack_cache_size; i++) {
1233 if (repo->packidx_cache[i] == NULL)
1234 break;
1235 if (strcmp(repo->packidx_cache[i]->path_packidx,
1236 path_packidx) == 0) {
1237 is_cached = 1;
1238 break;
1241 if (is_cached)
1242 continue; /* already searched */
1244 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1245 path_packidx, 0);
1246 if (err)
1247 goto done;
1249 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1250 if (err)
1251 goto done;
1253 err = cache_packidx(repo, *packidx, path_packidx);
1254 if (err)
1255 goto done;
1257 *idx = got_packidx_get_object_idx(*packidx, id);
1258 if (*idx != -1) {
1259 err = NULL; /* found the object */
1260 goto done;
1264 err = got_error_no_obj(id);
1265 done:
1266 return err;
1269 const struct got_error *
1270 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1271 struct got_repository *repo)
1273 const struct got_error *err = NULL;
1274 DIR *packdir = NULL;
1275 struct dirent *dent;
1276 char *path_packidx = NULL;
1277 int packdir_fd;
1279 packdir_fd = openat(got_repo_get_fd(repo),
1280 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1281 if (packdir_fd == -1) {
1282 return got_error_from_errno_fmt("openat: %s/%s",
1283 got_repo_get_path_git_dir(repo),
1284 GOT_OBJECTS_PACK_DIR);
1287 packdir = fdopendir(packdir_fd);
1288 if (packdir == NULL) {
1289 err = got_error_from_errno("fdopendir");
1290 goto done;
1293 while ((dent = readdir(packdir)) != NULL) {
1294 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1295 continue;
1297 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1298 dent->d_name) == -1) {
1299 err = got_error_from_errno("asprintf");
1300 path_packidx = NULL;
1301 break;
1304 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1305 if (err)
1306 break;
1308 done:
1309 if (err)
1310 free(path_packidx);
1311 if (packdir && closedir(packdir) != 0 && err == NULL)
1312 err = got_error_from_errno("closedir");
1313 return err;
1316 const struct got_error *
1317 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1318 struct got_repository *repo)
1320 const struct got_error *err;
1321 size_t i;
1323 *packidx = NULL;
1325 /* Search pack index cache. */
1326 for (i = 0; i < repo->pack_cache_size; i++) {
1327 if (repo->packidx_cache[i] == NULL)
1328 break;
1329 if (strcmp(repo->packidx_cache[i]->path_packidx,
1330 path_packidx) == 0) {
1331 *packidx = repo->packidx_cache[i];
1332 return NULL;
1335 /* No luck. Search the filesystem. */
1337 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1338 path_packidx, 0);
1339 if (err)
1340 return err;
1342 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1343 if (err)
1344 goto done;
1346 err = cache_packidx(repo, *packidx, path_packidx);
1347 done:
1348 if (err) {
1349 got_packidx_close(*packidx);
1350 *packidx = NULL;
1352 return err;
1355 static const struct got_error *
1356 read_packfile_hdr(int fd, struct got_packidx *packidx)
1358 const struct got_error *err = NULL;
1359 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1360 struct got_packfile_hdr hdr;
1361 ssize_t n;
1363 n = read(fd, &hdr, sizeof(hdr));
1364 if (n < 0)
1365 return got_error_from_errno("read");
1366 if (n != sizeof(hdr))
1367 return got_error(GOT_ERR_BAD_PACKFILE);
1369 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1370 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1371 be32toh(hdr.nobjects) != totobj)
1372 err = got_error(GOT_ERR_BAD_PACKFILE);
1374 return err;
1377 static const struct got_error *
1378 open_packfile(int *fd, struct got_repository *repo,
1379 const char *relpath, struct got_packidx *packidx)
1381 const struct got_error *err = NULL;
1383 *fd = openat(got_repo_get_fd(repo), relpath,
1384 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1385 if (*fd == -1)
1386 return got_error_from_errno_fmt("openat: %s/%s",
1387 got_repo_get_path_git_dir(repo), relpath);
1389 if (packidx) {
1390 err = read_packfile_hdr(*fd, packidx);
1391 if (err) {
1392 close(*fd);
1393 *fd = -1;
1397 return err;
1400 const struct got_error *
1401 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1402 const char *path_packfile, struct got_packidx *packidx)
1404 const struct got_error *err = NULL;
1405 struct got_pack *pack = NULL;
1406 struct stat sb;
1407 size_t i;
1409 if (packp)
1410 *packp = NULL;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1415 break;
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1417 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1420 if (i == repo->pack_cache_size) {
1421 struct got_pack tmp;
1422 do {
1423 i--;
1424 } while (i > 0 && repo->pinned_pack >= 0 &&
1425 i == repo->pinned_pack);
1426 err = got_pack_close(&repo->packs[i]);
1427 if (err)
1428 return err;
1429 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1430 return got_error_from_errno("ftruncate");
1431 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1432 return got_error_from_errno("ftruncate");
1433 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1434 memcpy(&repo->packs[i], &repo->packs[0],
1435 sizeof(repo->packs[i]));
1436 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1437 if (repo->pinned_pack == 0)
1438 repo->pinned_pack = i;
1439 else if (repo->pinned_pack == i)
1440 repo->pinned_pack = 0;
1441 i = 0;
1444 pack = &repo->packs[i];
1446 pack->path_packfile = strdup(path_packfile);
1447 if (pack->path_packfile == NULL) {
1448 err = got_error_from_errno("strdup");
1449 goto done;
1452 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1453 if (err)
1454 goto done;
1456 if (fstat(pack->fd, &sb) != 0) {
1457 err = got_error_from_errno("fstat");
1458 goto done;
1460 pack->filesize = sb.st_size;
1462 pack->privsep_child = NULL;
1464 #ifndef GOT_PACK_NO_MMAP
1465 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1466 pack->fd, 0);
1467 if (pack->map == MAP_FAILED) {
1468 if (errno != ENOMEM) {
1469 err = got_error_from_errno("mmap");
1470 goto done;
1472 pack->map = NULL; /* fall back to read(2) */
1474 #endif
1475 done:
1476 if (err) {
1477 if (pack) {
1478 free(pack->path_packfile);
1479 memset(pack, 0, sizeof(*pack));
1481 } else if (packp)
1482 *packp = pack;
1483 return err;
1486 struct got_pack *
1487 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1489 struct got_pack *pack = NULL;
1490 size_t i;
1492 for (i = 0; i < repo->pack_cache_size; i++) {
1493 pack = &repo->packs[i];
1494 if (pack->path_packfile == NULL)
1495 break;
1496 if (strcmp(pack->path_packfile, path_packfile) == 0)
1497 return pack;
1500 return NULL;
1503 const struct got_error *
1504 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1505 struct got_pack *pack)
1507 size_t i;
1508 int pinned_pack = -1, pinned_packidx = -1;
1510 for (i = 0; i < repo->pack_cache_size; i++) {
1511 if (repo->packidx_cache[i] &&
1512 strcmp(repo->packidx_cache[i]->path_packidx,
1513 packidx->path_packidx) == 0)
1514 pinned_packidx = i;
1515 if (repo->packs[i].path_packfile &&
1516 strcmp(repo->packs[i].path_packfile,
1517 pack->path_packfile) == 0)
1518 pinned_pack = i;
1521 if (pinned_packidx == -1 || pinned_pack == -1)
1522 return got_error(GOT_ERR_PIN_PACK);
1524 repo->pinned_pack = pinned_pack;
1525 repo->pinned_packidx = pinned_packidx;
1526 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1527 return NULL;
1530 struct got_pack *
1531 got_repo_get_pinned_pack(struct got_repository *repo)
1533 if (repo->pinned_pack >= 0 &&
1534 repo->pinned_pack < repo->pack_cache_size)
1535 return &repo->packs[repo->pinned_pack];
1537 return NULL;
1540 void
1541 got_repo_unpin_pack(struct got_repository *repo)
1543 repo->pinned_packidx = -1;
1544 repo->pinned_pack = -1;
1545 repo->pinned_pid = 0;
1548 const struct got_error *
1549 got_repo_init(const char *repo_path)
1551 const struct got_error *err = NULL;
1552 const char *dirnames[] = {
1553 GOT_OBJECTS_DIR,
1554 GOT_OBJECTS_PACK_DIR,
1555 GOT_REFS_DIR,
1557 const char *description_str = "Unnamed repository; "
1558 "edit this file 'description' to name the repository.";
1559 const char *headref_str = "ref: refs/heads/main";
1560 const char *gitconfig_str = "[core]\n"
1561 "\trepositoryformatversion = 0\n"
1562 "\tfilemode = true\n"
1563 "\tbare = true\n";
1564 char *path;
1565 size_t i;
1567 if (!got_path_dir_is_empty(repo_path))
1568 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1570 for (i = 0; i < nitems(dirnames); i++) {
1571 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1572 return got_error_from_errno("asprintf");
1574 err = got_path_mkdir(path);
1575 free(path);
1576 if (err)
1577 return err;
1580 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1581 return got_error_from_errno("asprintf");
1582 err = got_path_create_file(path, description_str);
1583 free(path);
1584 if (err)
1585 return err;
1587 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1588 return got_error_from_errno("asprintf");
1589 err = got_path_create_file(path, headref_str);
1590 free(path);
1591 if (err)
1592 return err;
1594 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, gitconfig_str);
1597 free(path);
1598 if (err)
1599 return err;
1601 return NULL;
1604 static const struct got_error *
1605 match_packed_object(struct got_object_id **unique_id,
1606 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1608 const struct got_error *err = NULL;
1609 struct got_object_id_queue matched_ids;
1610 struct got_pathlist_entry *pe;
1612 STAILQ_INIT(&matched_ids);
1614 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1615 const char *path_packidx = pe->path;
1616 struct got_packidx *packidx;
1617 struct got_object_qid *qid;
1619 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1620 path_packidx, 0);
1621 if (err)
1622 break;
1624 err = got_packidx_match_id_str_prefix(&matched_ids,
1625 packidx, id_str_prefix);
1626 if (err) {
1627 got_packidx_close(packidx);
1628 break;
1630 err = got_packidx_close(packidx);
1631 if (err)
1632 break;
1634 STAILQ_FOREACH(qid, &matched_ids, entry) {
1635 if (obj_type != GOT_OBJ_TYPE_ANY) {
1636 int matched_type;
1637 err = got_object_get_type(&matched_type, repo,
1638 &qid->id);
1639 if (err)
1640 goto done;
1641 if (matched_type != obj_type)
1642 continue;
1644 if (*unique_id == NULL) {
1645 *unique_id = got_object_id_dup(&qid->id);
1646 if (*unique_id == NULL) {
1647 err = got_error_from_errno("malloc");
1648 goto done;
1650 } else {
1651 if (got_object_id_cmp(*unique_id,
1652 &qid->id) == 0)
1653 continue; /* packed multiple times */
1654 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1655 goto done;
1659 done:
1660 got_object_id_queue_free(&matched_ids);
1661 if (err) {
1662 free(*unique_id);
1663 *unique_id = NULL;
1665 return err;
1668 static const struct got_error *
1669 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1670 const char *object_dir, const char *id_str_prefix, int obj_type,
1671 struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 char *path;
1675 DIR *dir = NULL;
1676 struct dirent *dent;
1677 struct got_object_id id;
1679 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1680 err = got_error_from_errno("asprintf");
1681 goto done;
1684 dir = opendir(path);
1685 if (dir == NULL) {
1686 if (errno == ENOENT) {
1687 err = NULL;
1688 goto done;
1690 err = got_error_from_errno2("opendir", path);
1691 goto done;
1693 while ((dent = readdir(dir)) != NULL) {
1694 char *id_str;
1695 int cmp;
1697 if (strcmp(dent->d_name, ".") == 0 ||
1698 strcmp(dent->d_name, "..") == 0)
1699 continue;
1701 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1702 err = got_error_from_errno("asprintf");
1703 goto done;
1706 if (!got_parse_sha1_digest(id.sha1, id_str))
1707 continue;
1710 * Directory entries do not necessarily appear in
1711 * sorted order, so we must iterate over all of them.
1713 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1714 if (cmp != 0) {
1715 free(id_str);
1716 continue;
1719 if (*unique_id == NULL) {
1720 if (obj_type != GOT_OBJ_TYPE_ANY) {
1721 int matched_type;
1722 err = got_object_get_type(&matched_type, repo,
1723 &id);
1724 if (err)
1725 goto done;
1726 if (matched_type != obj_type)
1727 continue;
1729 *unique_id = got_object_id_dup(&id);
1730 if (*unique_id == NULL) {
1731 err = got_error_from_errno("got_object_id_dup");
1732 free(id_str);
1733 goto done;
1735 } else {
1736 if (got_object_id_cmp(*unique_id, &id) == 0)
1737 continue; /* both packed and loose */
1738 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1739 free(id_str);
1740 goto done;
1743 done:
1744 if (dir && closedir(dir) != 0 && err == NULL)
1745 err = got_error_from_errno("closedir");
1746 if (err) {
1747 free(*unique_id);
1748 *unique_id = NULL;
1750 free(path);
1751 return err;
1754 const struct got_error *
1755 got_repo_match_object_id_prefix(struct got_object_id **id,
1756 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1758 const struct got_error *err = NULL;
1759 char *path_objects = got_repo_get_path_objects(repo);
1760 char *object_dir = NULL;
1761 size_t len;
1762 int i;
1764 *id = NULL;
1766 len = strlen(id_str_prefix);
1767 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1768 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1770 for (i = 0; i < len; i++) {
1771 if (isxdigit((unsigned char)id_str_prefix[i]))
1772 continue;
1773 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 if (len >= 2) {
1777 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1778 if (err)
1779 goto done;
1780 object_dir = strndup(id_str_prefix, 2);
1781 if (object_dir == NULL) {
1782 err = got_error_from_errno("strdup");
1783 goto done;
1785 err = match_loose_object(id, path_objects, object_dir,
1786 id_str_prefix, obj_type, repo);
1787 } else if (len == 1) {
1788 int i;
1789 for (i = 0; i < 0xf; i++) {
1790 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1791 == -1) {
1792 err = got_error_from_errno("asprintf");
1793 goto done;
1795 err = match_packed_object(id, repo, object_dir,
1796 obj_type);
1797 if (err)
1798 goto done;
1799 err = match_loose_object(id, path_objects, object_dir,
1800 id_str_prefix, obj_type, repo);
1801 if (err)
1802 goto done;
1804 } else {
1805 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1806 goto done;
1808 done:
1809 free(object_dir);
1810 if (err) {
1811 free(*id);
1812 *id = NULL;
1813 } else if (*id == NULL) {
1814 switch (obj_type) {
1815 case GOT_OBJ_TYPE_BLOB:
1816 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1817 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1818 break;
1819 case GOT_OBJ_TYPE_TREE:
1820 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1821 GOT_OBJ_LABEL_TREE, id_str_prefix);
1822 break;
1823 case GOT_OBJ_TYPE_COMMIT:
1824 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1825 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1826 break;
1827 case GOT_OBJ_TYPE_TAG:
1828 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1829 GOT_OBJ_LABEL_TAG, id_str_prefix);
1830 break;
1831 default:
1832 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1833 break;
1837 return err;
1840 const struct got_error *
1841 got_repo_match_object_id(struct got_object_id **id, char **label,
1842 const char *id_str, int obj_type, struct got_reflist_head *refs,
1843 struct got_repository *repo)
1845 const struct got_error *err;
1846 struct got_tag_object *tag;
1847 struct got_reference *ref = NULL;
1849 *id = NULL;
1850 if (label)
1851 *label = NULL;
1853 if (refs) {
1854 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1855 refs, repo);
1856 if (err == NULL) {
1857 *id = got_object_id_dup(
1858 got_object_tag_get_object_id(tag));
1859 if (*id == NULL)
1860 err = got_error_from_errno("got_object_id_dup");
1861 else if (label && asprintf(label, "refs/tags/%s",
1862 got_object_tag_get_name(tag)) == -1) {
1863 err = got_error_from_errno("asprintf");
1864 free(*id);
1865 *id = NULL;
1867 got_object_tag_close(tag);
1868 return err;
1869 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1870 err->code != GOT_ERR_NO_OBJ)
1871 return err;
1874 err = got_ref_open(&ref, repo, id_str, 0);
1875 if (err == NULL) {
1876 err = got_ref_resolve(id, repo, ref);
1877 if (err)
1878 goto done;
1879 if (label) {
1880 *label = strdup(got_ref_get_name(ref));
1881 if (*label == NULL) {
1882 err = got_error_from_errno("strdup");
1883 goto done;
1886 } else {
1887 if (err->code != GOT_ERR_NOT_REF &&
1888 err->code != GOT_ERR_BAD_REF_NAME)
1889 goto done;
1890 err = got_repo_match_object_id_prefix(id, id_str,
1891 obj_type, repo);
1892 if (err) {
1893 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1894 err = got_error_not_ref(id_str);
1895 goto done;
1897 if (label) {
1898 err = got_object_id_str(label, *id);
1899 if (*label == NULL) {
1900 err = got_error_from_errno("strdup");
1901 goto done;
1905 done:
1906 if (ref)
1907 got_ref_close(ref);
1908 return err;
1911 const struct got_error *
1912 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1913 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_reflist_entry *re;
1917 struct got_object_id *tag_id;
1918 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1920 *tag = NULL;
1922 TAILQ_FOREACH(re, refs, entry) {
1923 const char *refname;
1924 refname = got_ref_get_name(re->ref);
1925 if (got_ref_is_symbolic(re->ref))
1926 continue;
1927 if (strncmp(refname, "refs/tags/", 10) != 0)
1928 continue;
1929 if (!name_is_absolute)
1930 refname += strlen("refs/tags/");
1931 if (strcmp(refname, name) != 0)
1932 continue;
1933 err = got_ref_resolve(&tag_id, repo, re->ref);
1934 if (err)
1935 break;
1936 err = got_object_open_as_tag(tag, repo, tag_id);
1937 free(tag_id);
1938 if (err)
1939 break;
1940 if (obj_type == GOT_OBJ_TYPE_ANY ||
1941 got_object_tag_get_object_type(*tag) == obj_type)
1942 break;
1943 got_object_tag_close(*tag);
1944 *tag = NULL;
1947 if (err == NULL && *tag == NULL)
1948 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1949 GOT_OBJ_LABEL_TAG, name);
1950 return err;
1953 static const struct got_error *
1954 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1955 const char *name, mode_t mode, struct got_object_id *blob_id)
1957 const struct got_error *err = NULL;
1959 *new_te = NULL;
1961 *new_te = calloc(1, sizeof(**new_te));
1962 if (*new_te == NULL)
1963 return got_error_from_errno("calloc");
1965 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1966 sizeof((*new_te)->name)) {
1967 err = got_error(GOT_ERR_NO_SPACE);
1968 goto done;
1971 if (S_ISLNK(mode)) {
1972 (*new_te)->mode = S_IFLNK;
1973 } else {
1974 (*new_te)->mode = S_IFREG;
1975 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1977 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1978 done:
1979 if (err && *new_te) {
1980 free(*new_te);
1981 *new_te = NULL;
1983 return err;
1986 static const struct got_error *
1987 import_file(struct got_tree_entry **new_te, struct dirent *de,
1988 const char *path, struct got_repository *repo)
1990 const struct got_error *err;
1991 struct got_object_id *blob_id = NULL;
1992 char *filepath;
1993 struct stat sb;
1995 if (asprintf(&filepath, "%s%s%s", path,
1996 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (lstat(filepath, &sb) != 0) {
2000 err = got_error_from_errno2("lstat", path);
2001 goto done;
2004 err = got_object_blob_create(&blob_id, filepath, repo);
2005 if (err)
2006 goto done;
2008 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2009 blob_id);
2010 done:
2011 free(filepath);
2012 if (err)
2013 free(blob_id);
2014 return err;
2017 static const struct got_error *
2018 insert_tree_entry(struct got_tree_entry *new_te,
2019 struct got_pathlist_head *paths)
2021 const struct got_error *err = NULL;
2022 struct got_pathlist_entry *new_pe;
2024 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2025 if (err)
2026 return err;
2027 if (new_pe == NULL)
2028 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2029 return NULL;
2032 static const struct got_error *write_tree(struct got_object_id **,
2033 const char *, struct got_pathlist_head *, struct got_repository *,
2034 got_repo_import_cb progress_cb, void *progress_arg);
2036 static const struct got_error *
2037 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2038 const char *path, struct got_pathlist_head *ignores,
2039 struct got_repository *repo,
2040 got_repo_import_cb progress_cb, void *progress_arg)
2042 const struct got_error *err;
2043 struct got_object_id *id = NULL;
2044 char *subdirpath;
2046 if (asprintf(&subdirpath, "%s%s%s", path,
2047 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2048 return got_error_from_errno("asprintf");
2050 (*new_te) = calloc(1, sizeof(**new_te));
2051 if (*new_te == NULL)
2052 return got_error_from_errno("calloc");
2053 (*new_te)->mode = S_IFDIR;
2054 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2055 sizeof((*new_te)->name)) {
2056 err = got_error(GOT_ERR_NO_SPACE);
2057 goto done;
2059 err = write_tree(&id, subdirpath, ignores, repo,
2060 progress_cb, progress_arg);
2061 if (err)
2062 goto done;
2063 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2065 done:
2066 free(id);
2067 free(subdirpath);
2068 if (err) {
2069 free(*new_te);
2070 *new_te = NULL;
2072 return err;
2075 static const struct got_error *
2076 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2077 struct got_pathlist_head *ignores, struct got_repository *repo,
2078 got_repo_import_cb progress_cb, void *progress_arg)
2080 const struct got_error *err = NULL;
2081 DIR *dir;
2082 struct dirent *de;
2083 int nentries;
2084 struct got_tree_entry *new_te = NULL;
2085 struct got_pathlist_head paths;
2086 struct got_pathlist_entry *pe;
2088 *new_tree_id = NULL;
2090 TAILQ_INIT(&paths);
2092 dir = opendir(path_dir);
2093 if (dir == NULL) {
2094 err = got_error_from_errno2("opendir", path_dir);
2095 goto done;
2098 nentries = 0;
2099 while ((de = readdir(dir)) != NULL) {
2100 int ignore = 0;
2101 int type;
2103 if (strcmp(de->d_name, ".") == 0 ||
2104 strcmp(de->d_name, "..") == 0)
2105 continue;
2107 TAILQ_FOREACH(pe, ignores, entry) {
2108 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2109 ignore = 1;
2110 break;
2113 if (ignore)
2114 continue;
2116 err = got_path_dirent_type(&type, path_dir, de);
2117 if (err)
2118 goto done;
2120 if (type == DT_DIR) {
2121 err = import_subdir(&new_te, de, path_dir,
2122 ignores, repo, progress_cb, progress_arg);
2123 if (err) {
2124 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2125 goto done;
2126 err = NULL;
2127 continue;
2129 } else if (type == DT_REG || type == DT_LNK) {
2130 err = import_file(&new_te, de, path_dir, repo);
2131 if (err)
2132 goto done;
2133 } else
2134 continue;
2136 err = insert_tree_entry(new_te, &paths);
2137 if (err)
2138 goto done;
2139 nentries++;
2142 if (TAILQ_EMPTY(&paths)) {
2143 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2144 "cannot create tree without any entries");
2145 goto done;
2148 TAILQ_FOREACH(pe, &paths, entry) {
2149 struct got_tree_entry *te = pe->data;
2150 char *path;
2151 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2152 continue;
2153 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2154 err = got_error_from_errno("asprintf");
2155 goto done;
2157 err = (*progress_cb)(progress_arg, path);
2158 free(path);
2159 if (err)
2160 goto done;
2163 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2164 done:
2165 if (dir)
2166 closedir(dir);
2167 got_pathlist_free(&paths);
2168 return err;
2171 const struct got_error *
2172 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2173 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2174 struct got_repository *repo, got_repo_import_cb progress_cb,
2175 void *progress_arg)
2177 const struct got_error *err;
2178 struct got_object_id *new_tree_id;
2180 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2181 progress_cb, progress_arg);
2182 if (err)
2183 return err;
2185 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2186 author, time(NULL), author, time(NULL), logmsg, repo);
2187 free(new_tree_id);
2188 return err;
2191 const struct got_error *
2192 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2193 struct got_repository *repo)
2195 const struct got_error *err = NULL;
2196 char *path_objects = NULL, *path = NULL;
2197 DIR *dir = NULL;
2198 struct got_object_id id;
2199 int i;
2201 *nobjects = 0;
2202 *ondisk_size = 0;
2204 path_objects = got_repo_get_path_objects(repo);
2205 if (path_objects == NULL)
2206 return got_error_from_errno("got_repo_get_path_objects");
2208 for (i = 0; i <= 0xff; i++) {
2209 struct dirent *dent;
2211 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2212 err = got_error_from_errno("asprintf");
2213 break;
2216 dir = opendir(path);
2217 if (dir == NULL) {
2218 if (errno == ENOENT) {
2219 err = NULL;
2220 continue;
2222 err = got_error_from_errno2("opendir", path);
2223 break;
2226 while ((dent = readdir(dir)) != NULL) {
2227 char *id_str;
2228 int fd;
2229 struct stat sb;
2231 if (strcmp(dent->d_name, ".") == 0 ||
2232 strcmp(dent->d_name, "..") == 0)
2233 continue;
2235 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2236 err = got_error_from_errno("asprintf");
2237 goto done;
2240 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2241 free(id_str);
2242 continue;
2244 free(id_str);
2246 err = got_object_open_loose_fd(&fd, &id, repo);
2247 if (err)
2248 goto done;
2250 if (fstat(fd, &sb) == -1) {
2251 err = got_error_from_errno("fstat");
2252 close(fd);
2253 goto done;
2255 (*nobjects)++;
2256 (*ondisk_size) += sb.st_size;
2258 if (close(fd) == -1) {
2259 err = got_error_from_errno("close");
2260 goto done;
2264 if (closedir(dir) != 0) {
2265 err = got_error_from_errno("closedir");
2266 goto done;
2268 dir = NULL;
2270 free(path);
2271 path = NULL;
2273 done:
2274 if (dir && closedir(dir) != 0 && err == NULL)
2275 err = got_error_from_errno("closedir");
2277 if (err) {
2278 *nobjects = 0;
2279 *ondisk_size = 0;
2281 free(path_objects);
2282 free(path);
2283 return err;
2286 const struct got_error *
2287 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2288 off_t *total_packsize, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 DIR *packdir = NULL;
2292 struct dirent *dent;
2293 struct got_packidx *packidx = NULL;
2294 char *path_packidx;
2295 char *path_packfile;
2296 int packdir_fd;
2297 struct stat sb;
2299 *npackfiles = 0;
2300 *nobjects = 0;
2301 *total_packsize = 0;
2303 packdir_fd = openat(got_repo_get_fd(repo),
2304 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2305 if (packdir_fd == -1) {
2306 return got_error_from_errno_fmt("openat: %s/%s",
2307 got_repo_get_path_git_dir(repo),
2308 GOT_OBJECTS_PACK_DIR);
2311 packdir = fdopendir(packdir_fd);
2312 if (packdir == NULL) {
2313 err = got_error_from_errno("fdopendir");
2314 goto done;
2317 while ((dent = readdir(packdir)) != NULL) {
2318 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2319 continue;
2321 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2322 dent->d_name) == -1) {
2323 err = got_error_from_errno("asprintf");
2324 goto done;
2327 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2328 path_packidx, 0);
2329 free(path_packidx);
2330 if (err)
2331 goto done;
2333 if (fstat(packidx->fd, &sb) == -1)
2334 goto done;
2335 *total_packsize += sb.st_size;
2337 err = got_packidx_get_packfile_path(&path_packfile,
2338 packidx->path_packidx);
2339 if (err)
2340 goto done;
2342 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2343 0) == -1) {
2344 free(path_packfile);
2345 goto done;
2347 free(path_packfile);
2348 *total_packsize += sb.st_size;
2350 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2352 (*npackfiles)++;
2354 got_packidx_close(packidx);
2355 packidx = NULL;
2357 done:
2358 if (packidx)
2359 got_packidx_close(packidx);
2360 if (packdir && closedir(packdir) != 0 && err == NULL)
2361 err = got_error_from_errno("closedir");
2362 if (err) {
2363 *npackfiles = 0;
2364 *nobjects = 0;
2365 *total_packsize = 0;
2367 return err;
2370 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2371 got_packidx_bloom_filter_cmp);