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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, *pack_fds_tmp;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
257 free(pack_fds_tmp);
258 return got_error_from_errno("calloc");
261 /*
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
266 */
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
268 pack_fds_tmp[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 pack_fds_tmp[i] = got_opentempfd();
272 if (pack_fds_tmp[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(pack_fds_tmp);
275 return err;
278 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
279 return err;
282 const struct got_error *
283 got_repo_pack_fds_close(int *pack_fds)
285 const struct got_error *err = NULL;
286 int i;
288 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
289 if (pack_fds[i] == -1)
290 continue;
291 if (close(pack_fds[i]) == -1) {
292 err = got_error_from_errno("close");
293 break;
296 free(pack_fds);
297 return err;
300 const struct got_error *
301 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
302 struct got_object *obj)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->objcache, id, obj);
307 if (err) {
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 err = NULL;
311 return err;
313 obj->refcnt++;
314 #endif
315 return NULL;
318 struct got_object *
319 got_repo_get_cached_object(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
325 const struct got_error *
326 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
327 struct got_tree_object *tree)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->treecache, id, tree);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tree->refcnt++;
339 #endif
340 return NULL;
343 struct got_tree_object *
344 got_repo_get_cached_tree(struct got_repository *repo,
345 struct got_object_id *id)
347 return (struct got_tree_object *)got_object_cache_get(
348 &repo->treecache, id);
351 const struct got_error *
352 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
353 struct got_commit_object *commit)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->commitcache, id, commit);
358 if (err) {
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
361 err = NULL;
362 return err;
364 commit->refcnt++;
365 #endif
366 return NULL;
369 struct got_commit_object *
370 got_repo_get_cached_commit(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_commit_object *)got_object_cache_get(
374 &repo->commitcache, id);
377 const struct got_error *
378 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
379 struct got_tag_object *tag)
381 #ifndef GOT_NO_OBJ_CACHE
382 const struct got_error *err = NULL;
383 err = got_object_cache_add(&repo->tagcache, id, tag);
384 if (err) {
385 if (err->code == GOT_ERR_OBJ_EXISTS ||
386 err->code == GOT_ERR_OBJ_TOO_LARGE)
387 err = NULL;
388 return err;
390 tag->refcnt++;
391 #endif
392 return NULL;
395 struct got_tag_object *
396 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
398 return (struct got_tag_object *)got_object_cache_get(
399 &repo->tagcache, id);
402 const struct got_error *
403 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
404 struct got_raw_object *raw)
406 #ifndef GOT_NO_OBJ_CACHE
407 const struct got_error *err = NULL;
408 err = got_object_cache_add(&repo->rawcache, id, raw);
409 if (err) {
410 if (err->code == GOT_ERR_OBJ_EXISTS ||
411 err->code == GOT_ERR_OBJ_TOO_LARGE)
412 err = NULL;
413 return err;
415 raw->refcnt++;
416 #endif
417 return NULL;
421 struct got_raw_object *
422 got_repo_get_cached_raw_object(struct got_repository *repo,
423 struct got_object_id *id)
425 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
429 static const struct got_error *
430 open_repo(struct got_repository *repo, const char *path)
432 const struct got_error *err = NULL;
434 repo->gitdir_fd = -1;
436 /* bare git repository? */
437 repo->path_git_dir = strdup(path);
438 if (repo->path_git_dir == NULL)
439 return got_error_from_errno("strdup");
440 if (is_git_repo(repo)) {
441 repo->path = strdup(repo->path_git_dir);
442 if (repo->path == NULL) {
443 err = got_error_from_errno("strdup");
444 goto done;
446 repo->gitdir_fd = open(repo->path_git_dir,
447 O_DIRECTORY | O_CLOEXEC);
448 if (repo->gitdir_fd == -1) {
449 err = got_error_from_errno2("open",
450 repo->path_git_dir);
451 goto done;
453 return NULL;
456 /* git repository with working tree? */
457 free(repo->path_git_dir);
458 repo->path_git_dir = NULL;
459 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
463 if (is_git_repo(repo)) {
464 repo->path = strdup(path);
465 if (repo->path == NULL) {
466 err = got_error_from_errno("strdup");
467 goto done;
469 repo->gitdir_fd = open(repo->path_git_dir,
470 O_DIRECTORY | O_CLOEXEC);
471 if (repo->gitdir_fd == -1) {
472 err = got_error_from_errno2("open",
473 repo->path_git_dir);
474 goto done;
476 return NULL;
479 err = got_error(GOT_ERR_NOT_GIT_REPO);
480 done:
481 if (err) {
482 free(repo->path);
483 repo->path = NULL;
484 free(repo->path_git_dir);
485 repo->path_git_dir = NULL;
486 if (repo->gitdir_fd != -1)
487 close(repo->gitdir_fd);
488 repo->gitdir_fd = -1;
491 return err;
494 static const struct got_error *
495 parse_gitconfig_file(int *gitconfig_repository_format_version,
496 char **gitconfig_author_name, char **gitconfig_author_email,
497 struct got_remote_repo **remotes, int *nremotes,
498 char **gitconfig_owner, char ***extensions, int *nextensions,
499 const char *gitconfig_path)
501 const struct got_error *err = NULL, *child_err = NULL;
502 int fd = -1;
503 int imsg_fds[2] = { -1, -1 };
504 pid_t pid;
505 struct imsgbuf *ibuf;
507 *gitconfig_repository_format_version = 0;
508 if (extensions)
509 *extensions = NULL;
510 if (nextensions)
511 *nextensions = 0;
512 *gitconfig_author_name = NULL;
513 *gitconfig_author_email = NULL;
514 if (remotes)
515 *remotes = NULL;
516 if (nremotes)
517 *nremotes = 0;
518 if (gitconfig_owner)
519 *gitconfig_owner = NULL;
521 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
522 if (fd == -1) {
523 if (errno == ENOENT)
524 return NULL;
525 return got_error_from_errno2("open", gitconfig_path);
528 ibuf = calloc(1, sizeof(*ibuf));
529 if (ibuf == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
535 err = got_error_from_errno("socketpair");
536 goto done;
539 pid = fork();
540 if (pid == -1) {
541 err = got_error_from_errno("fork");
542 goto done;
543 } else if (pid == 0) {
544 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
545 gitconfig_path);
546 /* not reached */
549 if (close(imsg_fds[1]) == -1) {
550 err = got_error_from_errno("close");
551 goto done;
553 imsg_fds[1] = -1;
554 imsg_init(ibuf, imsg_fds[0]);
556 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
557 if (err)
558 goto done;
559 fd = -1;
561 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
562 if (err)
563 goto done;
565 err = got_privsep_recv_gitconfig_int(
566 gitconfig_repository_format_version, ibuf);
567 if (err)
568 goto done;
570 if (extensions && nextensions) {
571 err = got_privsep_send_gitconfig_repository_extensions_req(
572 ibuf);
573 if (err)
574 goto done;
575 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
576 if (err)
577 goto done;
578 if (*nextensions > 0) {
579 int i;
580 *extensions = calloc(*nextensions, sizeof(char *));
581 if (*extensions == NULL) {
582 err = got_error_from_errno("calloc");
583 goto done;
585 for (i = 0; i < *nextensions; i++) {
586 char *ext;
587 err = got_privsep_recv_gitconfig_str(&ext,
588 ibuf);
589 if (err)
590 goto done;
591 (*extensions)[i] = ext;
596 err = got_privsep_send_gitconfig_author_name_req(ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_send_gitconfig_author_email_req(ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
609 if (err)
610 goto done;
612 if (remotes && nremotes) {
613 err = got_privsep_send_gitconfig_remotes_req(ibuf);
614 if (err)
615 goto done;
617 err = got_privsep_recv_gitconfig_remotes(remotes,
618 nremotes, ibuf);
619 if (err)
620 goto done;
623 if (gitconfig_owner) {
624 err = got_privsep_send_gitconfig_owner_req(ibuf);
625 if (err)
626 goto done;
627 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
628 if (err)
629 goto done;
632 err = got_privsep_send_stop(imsg_fds[0]);
633 child_err = got_privsep_wait_for_child(pid);
634 if (child_err && err == NULL)
635 err = child_err;
636 done:
637 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
638 err = got_error_from_errno("close");
639 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
640 err = got_error_from_errno("close");
641 if (fd != -1 && close(fd) == -1 && err == NULL)
642 err = got_error_from_errno2("close", gitconfig_path);
643 free(ibuf);
644 return err;
647 static const struct got_error *
648 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
650 const struct got_error *err = NULL;
651 char *repo_gitconfig_path = NULL;
653 if (global_gitconfig_path) {
654 /* Read settings from ~/.gitconfig. */
655 int dummy_repo_version;
656 err = parse_gitconfig_file(&dummy_repo_version,
657 &repo->global_gitconfig_author_name,
658 &repo->global_gitconfig_author_email,
659 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
660 if (err)
661 return err;
664 /* Read repository's .git/config file. */
665 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
666 if (repo_gitconfig_path == NULL)
667 return got_error_from_errno("got_repo_get_path_gitconfig");
669 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
670 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
671 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
672 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
673 repo_gitconfig_path);
674 if (err)
675 goto done;
677 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
678 int i;
680 for (i = 0; i < repo->ngitconfig_remotes; i++) {
681 got_repo_free_remote_repo_data(
682 &repo->gitconfig_remotes[i]);
684 free(repo->gitconfig_remotes);
685 repo->gitconfig_remotes = NULL;
686 repo->ngitconfig_remotes = 0;
688 free(repo->gitconfig_author_name);
689 repo->gitconfig_author_name = NULL;
690 free(repo->gitconfig_author_email);
691 repo->gitconfig_author_email = NULL;
693 free(repo->global_gitconfig_author_name);
694 repo->global_gitconfig_author_name = NULL;
695 free(repo->global_gitconfig_author_email);
696 repo->global_gitconfig_author_email = NULL;
699 done:
700 free(repo_gitconfig_path);
701 return err;
704 static const struct got_error *
705 read_gotconfig(struct got_repository *repo)
707 const struct got_error *err = NULL;
708 char *gotconfig_path;
710 gotconfig_path = got_repo_get_path_gotconfig(repo);
711 if (gotconfig_path == NULL)
712 return got_error_from_errno("got_repo_get_path_gotconfig");
714 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
715 free(gotconfig_path);
716 return err;
719 /* Supported repository format extensions. */
720 static const char *const repo_extensions[] = {
721 "noop", /* Got supports repository format version 1. */
722 "preciousObjects", /* Supported by gotadmin cleanup. */
723 "worktreeConfig", /* Got does not care about Git work trees. */
724 };
726 const struct got_error *
727 got_repo_open(struct got_repository **repop, const char *path,
728 const char *global_gitconfig_path, int *pack_fds)
730 struct got_repository *repo = NULL;
731 const struct got_error *err = NULL;
732 char *repo_path = NULL;
733 size_t i, j = 0;
734 struct rlimit rl;
736 *repop = NULL;
738 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
739 return got_error_from_errno("getrlimit");
741 repo = calloc(1, sizeof(*repo));
742 if (repo == NULL)
743 return got_error_from_errno("calloc");
745 RB_INIT(&repo->packidx_bloom_filters);
746 TAILQ_INIT(&repo->packidx_paths);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 memset(&repo->privsep_children[i], 0,
750 sizeof(repo->privsep_children[0]));
751 repo->privsep_children[i].imsg_fd = -1;
754 err = got_object_cache_init(&repo->objcache,
755 GOT_OBJECT_CACHE_TYPE_OBJ);
756 if (err)
757 goto done;
758 err = got_object_cache_init(&repo->treecache,
759 GOT_OBJECT_CACHE_TYPE_TREE);
760 if (err)
761 goto done;
762 err = got_object_cache_init(&repo->commitcache,
763 GOT_OBJECT_CACHE_TYPE_COMMIT);
764 if (err)
765 goto done;
766 err = got_object_cache_init(&repo->tagcache,
767 GOT_OBJECT_CACHE_TYPE_TAG);
768 if (err)
769 goto done;
770 err = got_object_cache_init(&repo->rawcache,
771 GOT_OBJECT_CACHE_TYPE_RAW);
772 if (err)
773 goto done;
775 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
776 if (repo->pack_cache_size > rl.rlim_cur / 8)
777 repo->pack_cache_size = rl.rlim_cur / 8;
778 for (i = 0; i < nitems(repo->packs); i++) {
779 if (i < repo->pack_cache_size) {
780 repo->packs[i].basefd = pack_fds[j++];
781 repo->packs[i].accumfd = pack_fds[j++];
782 } else {
783 repo->packs[i].basefd = -1;
784 repo->packs[i].accumfd = -1;
787 repo->pinned_pack = -1;
788 repo->pinned_packidx = -1;
789 repo->pinned_pid = 0;
791 repo_path = realpath(path, NULL);
792 if (repo_path == NULL) {
793 err = got_error_from_errno2("realpath", path);
794 goto done;
797 for (;;) {
798 char *parent_path;
800 err = open_repo(repo, repo_path);
801 if (err == NULL)
802 break;
803 if (err->code != GOT_ERR_NOT_GIT_REPO)
804 goto done;
805 if (repo_path[0] == '/' && repo_path[1] == '\0') {
806 err = got_error(GOT_ERR_NOT_GIT_REPO);
807 goto done;
809 err = got_path_dirname(&parent_path, repo_path);
810 if (err)
811 goto done;
812 free(repo_path);
813 repo_path = parent_path;
816 err = read_gotconfig(repo);
817 if (err)
818 goto done;
820 err = read_gitconfig(repo, global_gitconfig_path);
821 if (err)
822 goto done;
823 if (repo->gitconfig_repository_format_version != 0) {
824 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
825 goto done;
827 for (i = 0; i < repo->nextensions; i++) {
828 char *ext = repo->extensions[i];
829 int j, supported = 0;
830 for (j = 0; j < nitems(repo_extensions); j++) {
831 if (strcmp(ext, repo_extensions[j]) == 0) {
832 supported = 1;
833 break;
836 if (!supported) {
837 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
838 goto done;
842 err = got_repo_list_packidx(&repo->packidx_paths, repo);
843 done:
844 if (err)
845 got_repo_close(repo);
846 else
847 *repop = repo;
848 free(repo_path);
849 return err;
852 const struct got_error *
853 got_repo_close(struct got_repository *repo)
855 const struct got_error *err = NULL, *child_err;
856 struct got_packidx_bloom_filter *bf;
857 struct got_pathlist_entry *pe;
858 size_t i;
860 for (i = 0; i < repo->pack_cache_size; i++) {
861 if (repo->packidx_cache[i] == NULL)
862 break;
863 got_packidx_close(repo->packidx_cache[i]);
866 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
867 &repo->packidx_bloom_filters))) {
868 RB_REMOVE(got_packidx_bloom_filter_tree,
869 &repo->packidx_bloom_filters, bf);
870 free(bf->bloom);
871 free(bf);
874 for (i = 0; i < repo->pack_cache_size; i++)
875 if (repo->packs[i].path_packfile)
876 if (repo->packs[i].path_packfile)
877 got_pack_close(&repo->packs[i]);
879 free(repo->path);
880 free(repo->path_git_dir);
882 got_object_cache_close(&repo->objcache);
883 got_object_cache_close(&repo->treecache);
884 got_object_cache_close(&repo->commitcache);
885 got_object_cache_close(&repo->tagcache);
886 got_object_cache_close(&repo->rawcache);
888 for (i = 0; i < nitems(repo->privsep_children); i++) {
889 if (repo->privsep_children[i].imsg_fd == -1)
890 continue;
891 imsg_clear(repo->privsep_children[i].ibuf);
892 free(repo->privsep_children[i].ibuf);
893 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
894 child_err = got_privsep_wait_for_child(
895 repo->privsep_children[i].pid);
896 if (child_err && err == NULL)
897 err = child_err;
898 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
899 err == NULL)
900 err = got_error_from_errno("close");
903 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
904 err == NULL)
905 err = got_error_from_errno("close");
907 if (repo->gotconfig)
908 got_gotconfig_free(repo->gotconfig);
909 free(repo->gitconfig_author_name);
910 free(repo->gitconfig_author_email);
911 for (i = 0; i < repo->ngitconfig_remotes; i++)
912 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
913 free(repo->gitconfig_remotes);
914 for (i = 0; i < repo->nextensions; i++)
915 free(repo->extensions[i]);
916 free(repo->extensions);
918 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
919 free((void *)pe->path);
920 got_pathlist_free(&repo->packidx_paths);
921 free(repo);
923 return err;
926 void
927 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
929 int i;
931 free(repo->name);
932 repo->name = NULL;
933 free(repo->fetch_url);
934 repo->fetch_url = NULL;
935 free(repo->send_url);
936 repo->send_url = NULL;
937 for (i = 0; i < repo->nfetch_branches; i++)
938 free(repo->fetch_branches[i]);
939 free(repo->fetch_branches);
940 repo->fetch_branches = NULL;
941 repo->nfetch_branches = 0;
942 for (i = 0; i < repo->nsend_branches; i++)
943 free(repo->send_branches[i]);
944 free(repo->send_branches);
945 repo->send_branches = NULL;
946 repo->nsend_branches = 0;
949 const struct got_error *
950 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
951 const char *input_path)
953 const struct got_error *err = NULL;
954 const char *repo_abspath = NULL;
955 size_t repolen, len;
956 char *canonpath, *path = NULL;
958 *in_repo_path = NULL;
960 canonpath = strdup(input_path);
961 if (canonpath == NULL) {
962 err = got_error_from_errno("strdup");
963 goto done;
965 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
966 if (err)
967 goto done;
969 repo_abspath = got_repo_get_path(repo);
971 if (canonpath[0] == '\0') {
972 path = strdup(canonpath);
973 if (path == NULL) {
974 err = got_error_from_errno("strdup");
975 goto done;
977 } else {
978 path = realpath(canonpath, NULL);
979 if (path == NULL) {
980 if (errno != ENOENT) {
981 err = got_error_from_errno2("realpath",
982 canonpath);
983 goto done;
985 /*
986 * Path is not on disk.
987 * Assume it is already relative to repository root.
988 */
989 path = strdup(canonpath);
990 if (path == NULL) {
991 err = got_error_from_errno("strdup");
992 goto done;
996 repolen = strlen(repo_abspath);
997 len = strlen(path);
1000 if (strcmp(path, repo_abspath) == 0) {
1001 free(path);
1002 path = strdup("");
1003 if (path == NULL) {
1004 err = got_error_from_errno("strdup");
1005 goto done;
1007 } else if (len > repolen &&
1008 got_path_is_child(path, repo_abspath, repolen)) {
1009 /* Matched an on-disk path inside repository. */
1010 if (got_repo_is_bare(repo)) {
1012 * Matched an on-disk path inside repository
1013 * database. Treat input as repository-relative.
1015 free(path);
1016 path = canonpath;
1017 canonpath = NULL;
1018 } else {
1019 char *child;
1020 /* Strip common prefix with repository path. */
1021 err = got_path_skip_common_ancestor(&child,
1022 repo_abspath, path);
1023 if (err)
1024 goto done;
1025 free(path);
1026 path = child;
1028 } else {
1030 * Matched unrelated on-disk path.
1031 * Treat input as repository-relative.
1033 free(path);
1034 path = canonpath;
1035 canonpath = NULL;
1039 /* Make in-repository path absolute */
1040 if (path[0] != '/') {
1041 char *abspath;
1042 if (asprintf(&abspath, "/%s", path) == -1) {
1043 err = got_error_from_errno("asprintf");
1044 goto done;
1046 free(path);
1047 path = abspath;
1050 done:
1051 free(canonpath);
1052 if (err)
1053 free(path);
1054 else
1055 *in_repo_path = path;
1056 return err;
1059 static const struct got_error *
1060 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1061 const char *path_packidx)
1063 const struct got_error *err = NULL;
1064 size_t i;
1066 for (i = 0; i < repo->pack_cache_size; i++) {
1067 if (repo->packidx_cache[i] == NULL)
1068 break;
1069 if (strcmp(repo->packidx_cache[i]->path_packidx,
1070 path_packidx) == 0) {
1071 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1074 if (i == repo->pack_cache_size) {
1075 do {
1076 i--;
1077 } while (i > 0 && repo->pinned_packidx >= 0 &&
1078 i == repo->pinned_packidx);
1079 err = got_packidx_close(repo->packidx_cache[i]);
1080 if (err)
1081 return err;
1084 repo->packidx_cache[i] = packidx;
1086 return NULL;
1089 int
1090 got_repo_is_packidx_filename(const char *name, size_t len)
1092 if (len != GOT_PACKIDX_NAMELEN)
1093 return 0;
1095 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1096 return 0;
1098 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1099 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1100 return 0;
1102 return 1;
1105 static struct got_packidx_bloom_filter *
1106 get_packidx_bloom_filter(struct got_repository *repo,
1107 const char *path, size_t path_len)
1109 struct got_packidx_bloom_filter key;
1111 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1112 return NULL; /* XXX */
1113 key.path_len = path_len;
1115 return RB_FIND(got_packidx_bloom_filter_tree,
1116 &repo->packidx_bloom_filters, &key);
1119 int
1120 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1121 const char *path_packidx, struct got_object_id *id)
1123 struct got_packidx_bloom_filter *bf;
1125 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1126 if (bf)
1127 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1129 /* No bloom filter means this pack index must be searched. */
1130 return 1;
1133 static const struct got_error *
1134 add_packidx_bloom_filter(struct got_repository *repo,
1135 struct got_packidx *packidx, const char *path_packidx)
1137 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1138 struct got_packidx_bloom_filter *bf;
1139 size_t len;
1142 * Don't use bloom filters for very large pack index files.
1143 * Large pack files will contain a relatively large fraction
1144 * of our objects so we will likely need to visit them anyway.
1145 * The more objects a pack file contains the higher the probability
1146 * of a false-positive match from the bloom filter. And reading
1147 * all object IDs from a large pack index file can be expensive.
1149 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1150 return NULL;
1152 /* Do we already have a filter for this pack index? */
1153 if (get_packidx_bloom_filter(repo, path_packidx,
1154 strlen(path_packidx)) != NULL)
1155 return NULL;
1157 bf = calloc(1, sizeof(*bf));
1158 if (bf == NULL)
1159 return got_error_from_errno("calloc");
1160 bf->bloom = calloc(1, sizeof(*bf->bloom));
1161 if (bf->bloom == NULL) {
1162 free(bf);
1163 return got_error_from_errno("calloc");
1166 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1167 if (len >= sizeof(bf->path)) {
1168 free(bf->bloom);
1169 free(bf);
1170 return got_error(GOT_ERR_NO_SPACE);
1172 bf->path_len = len;
1174 /* Minimum size supported by our bloom filter is 1000 entries. */
1175 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1176 for (i = 0; i < nobjects; i++) {
1177 struct got_packidx_object_id *id;
1178 id = &packidx->hdr.sorted_ids[i];
1179 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1182 RB_INSERT(got_packidx_bloom_filter_tree,
1183 &repo->packidx_bloom_filters, bf);
1184 return NULL;
1187 const struct got_error *
1188 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1189 struct got_repository *repo, struct got_object_id *id)
1191 const struct got_error *err;
1192 struct got_pathlist_entry *pe;
1193 size_t i;
1195 /* Search pack index cache. */
1196 for (i = 0; i < repo->pack_cache_size; i++) {
1197 if (repo->packidx_cache[i] == NULL)
1198 break;
1199 if (!got_repo_check_packidx_bloom_filter(repo,
1200 repo->packidx_cache[i]->path_packidx, id))
1201 continue; /* object will not be found in this index */
1202 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1203 if (*idx != -1) {
1204 *packidx = repo->packidx_cache[i];
1206 * Move this cache entry to the front. Repeatedly
1207 * searching a wrong pack index can be expensive.
1209 if (i > 0) {
1210 memmove(&repo->packidx_cache[1],
1211 &repo->packidx_cache[0],
1212 i * sizeof(repo->packidx_cache[0]));
1213 repo->packidx_cache[0] = *packidx;
1214 if (repo->pinned_packidx >= 0 &&
1215 repo->pinned_packidx < i)
1216 repo->pinned_packidx++;
1217 else if (repo->pinned_packidx == i)
1218 repo->pinned_packidx = 0;
1220 return NULL;
1223 /* No luck. Search the filesystem. */
1225 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1226 const char *path_packidx = pe->path;
1227 int is_cached = 0;
1229 if (!got_repo_check_packidx_bloom_filter(repo,
1230 pe->path, id))
1231 continue; /* object will not be found in this index */
1233 for (i = 0; i < repo->pack_cache_size; i++) {
1234 if (repo->packidx_cache[i] == NULL)
1235 break;
1236 if (strcmp(repo->packidx_cache[i]->path_packidx,
1237 path_packidx) == 0) {
1238 is_cached = 1;
1239 break;
1242 if (is_cached)
1243 continue; /* already searched */
1245 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1246 path_packidx, 0);
1247 if (err)
1248 goto done;
1250 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1251 if (err)
1252 goto done;
1254 err = cache_packidx(repo, *packidx, path_packidx);
1255 if (err)
1256 goto done;
1258 *idx = got_packidx_get_object_idx(*packidx, id);
1259 if (*idx != -1) {
1260 err = NULL; /* found the object */
1261 goto done;
1265 err = got_error_no_obj(id);
1266 done:
1267 return err;
1270 const struct got_error *
1271 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1272 struct got_repository *repo)
1274 const struct got_error *err = NULL;
1275 DIR *packdir = NULL;
1276 struct dirent *dent;
1277 char *path_packidx = NULL;
1278 int packdir_fd;
1280 packdir_fd = openat(got_repo_get_fd(repo),
1281 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1282 if (packdir_fd == -1) {
1283 return got_error_from_errno_fmt("openat: %s/%s",
1284 got_repo_get_path_git_dir(repo),
1285 GOT_OBJECTS_PACK_DIR);
1288 packdir = fdopendir(packdir_fd);
1289 if (packdir == NULL) {
1290 err = got_error_from_errno("fdopendir");
1291 goto done;
1294 while ((dent = readdir(packdir)) != NULL) {
1295 if (!got_repo_is_packidx_filename(dent->d_name,
1296 strlen(dent->d_name)))
1297 continue;
1299 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1300 dent->d_name) == -1) {
1301 err = got_error_from_errno("asprintf");
1302 path_packidx = NULL;
1303 break;
1306 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1307 if (err)
1308 break;
1310 done:
1311 if (err)
1312 free(path_packidx);
1313 if (packdir && closedir(packdir) != 0 && err == NULL)
1314 err = got_error_from_errno("closedir");
1315 return err;
1318 const struct got_error *
1319 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1320 struct got_repository *repo)
1322 const struct got_error *err;
1323 size_t i;
1325 *packidx = NULL;
1327 /* Search pack index cache. */
1328 for (i = 0; i < repo->pack_cache_size; i++) {
1329 if (repo->packidx_cache[i] == NULL)
1330 break;
1331 if (strcmp(repo->packidx_cache[i]->path_packidx,
1332 path_packidx) == 0) {
1333 *packidx = repo->packidx_cache[i];
1334 return NULL;
1337 /* No luck. Search the filesystem. */
1339 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1340 path_packidx, 0);
1341 if (err)
1342 return err;
1344 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1345 if (err)
1346 goto done;
1348 err = cache_packidx(repo, *packidx, path_packidx);
1349 done:
1350 if (err) {
1351 got_packidx_close(*packidx);
1352 *packidx = NULL;
1354 return err;
1357 static const struct got_error *
1358 read_packfile_hdr(int fd, struct got_packidx *packidx)
1360 const struct got_error *err = NULL;
1361 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1362 struct got_packfile_hdr hdr;
1363 ssize_t n;
1365 n = read(fd, &hdr, sizeof(hdr));
1366 if (n < 0)
1367 return got_error_from_errno("read");
1368 if (n != sizeof(hdr))
1369 return got_error(GOT_ERR_BAD_PACKFILE);
1371 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1372 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1373 be32toh(hdr.nobjects) != totobj)
1374 err = got_error(GOT_ERR_BAD_PACKFILE);
1376 return err;
1379 static const struct got_error *
1380 open_packfile(int *fd, struct got_repository *repo,
1381 const char *relpath, struct got_packidx *packidx)
1383 const struct got_error *err = NULL;
1385 *fd = openat(got_repo_get_fd(repo), relpath,
1386 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1387 if (*fd == -1)
1388 return got_error_from_errno_fmt("openat: %s/%s",
1389 got_repo_get_path_git_dir(repo), relpath);
1391 if (packidx) {
1392 err = read_packfile_hdr(*fd, packidx);
1393 if (err) {
1394 close(*fd);
1395 *fd = -1;
1399 return err;
1402 const struct got_error *
1403 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1404 const char *path_packfile, struct got_packidx *packidx)
1406 const struct got_error *err = NULL;
1407 struct got_pack *pack = NULL;
1408 struct stat sb;
1409 size_t i;
1411 if (packp)
1412 *packp = NULL;
1414 for (i = 0; i < repo->pack_cache_size; i++) {
1415 pack = &repo->packs[i];
1416 if (pack->path_packfile == NULL)
1417 break;
1418 if (strcmp(pack->path_packfile, path_packfile) == 0)
1419 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1422 if (i == repo->pack_cache_size) {
1423 struct got_pack tmp;
1424 do {
1425 i--;
1426 } while (i > 0 && repo->pinned_pack >= 0 &&
1427 i == repo->pinned_pack);
1428 err = got_pack_close(&repo->packs[i]);
1429 if (err)
1430 return err;
1431 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1432 return got_error_from_errno("ftruncate");
1433 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1434 return got_error_from_errno("ftruncate");
1435 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1436 memcpy(&repo->packs[i], &repo->packs[0],
1437 sizeof(repo->packs[i]));
1438 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1439 if (repo->pinned_pack == 0)
1440 repo->pinned_pack = i;
1441 else if (repo->pinned_pack == i)
1442 repo->pinned_pack = 0;
1443 i = 0;
1446 pack = &repo->packs[i];
1448 pack->path_packfile = strdup(path_packfile);
1449 if (pack->path_packfile == NULL) {
1450 err = got_error_from_errno("strdup");
1451 goto done;
1454 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1455 if (err)
1456 goto done;
1458 if (fstat(pack->fd, &sb) != 0) {
1459 err = got_error_from_errno("fstat");
1460 goto done;
1462 pack->filesize = sb.st_size;
1464 pack->privsep_child = NULL;
1466 #ifndef GOT_PACK_NO_MMAP
1467 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1468 pack->fd, 0);
1469 if (pack->map == MAP_FAILED) {
1470 if (errno != ENOMEM) {
1471 err = got_error_from_errno("mmap");
1472 goto done;
1474 pack->map = NULL; /* fall back to read(2) */
1476 #endif
1477 done:
1478 if (err) {
1479 if (pack) {
1480 free(pack->path_packfile);
1481 memset(pack, 0, sizeof(*pack));
1483 } else if (packp)
1484 *packp = pack;
1485 return err;
1488 struct got_pack *
1489 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1491 struct got_pack *pack = NULL;
1492 size_t i;
1494 for (i = 0; i < repo->pack_cache_size; i++) {
1495 pack = &repo->packs[i];
1496 if (pack->path_packfile == NULL)
1497 break;
1498 if (strcmp(pack->path_packfile, path_packfile) == 0)
1499 return pack;
1502 return NULL;
1505 const struct got_error *
1506 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1507 struct got_pack *pack)
1509 size_t i;
1510 int pinned_pack = -1, pinned_packidx = -1;
1512 for (i = 0; i < repo->pack_cache_size; i++) {
1513 if (repo->packidx_cache[i] &&
1514 strcmp(repo->packidx_cache[i]->path_packidx,
1515 packidx->path_packidx) == 0)
1516 pinned_packidx = i;
1517 if (repo->packs[i].path_packfile &&
1518 strcmp(repo->packs[i].path_packfile,
1519 pack->path_packfile) == 0)
1520 pinned_pack = i;
1523 if (pinned_packidx == -1 || pinned_pack == -1)
1524 return got_error(GOT_ERR_PIN_PACK);
1526 repo->pinned_pack = pinned_pack;
1527 repo->pinned_packidx = pinned_packidx;
1528 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1529 return NULL;
1532 struct got_pack *
1533 got_repo_get_pinned_pack(struct got_repository *repo)
1535 if (repo->pinned_pack >= 0 &&
1536 repo->pinned_pack < repo->pack_cache_size)
1537 return &repo->packs[repo->pinned_pack];
1539 return NULL;
1542 void
1543 got_repo_unpin_pack(struct got_repository *repo)
1545 repo->pinned_packidx = -1;
1546 repo->pinned_pack = -1;
1547 repo->pinned_pid = 0;
1550 const struct got_error *
1551 got_repo_init(const char *repo_path)
1553 const struct got_error *err = NULL;
1554 const char *dirnames[] = {
1555 GOT_OBJECTS_DIR,
1556 GOT_OBJECTS_PACK_DIR,
1557 GOT_REFS_DIR,
1559 const char *description_str = "Unnamed repository; "
1560 "edit this file 'description' to name the repository.";
1561 const char *headref_str = "ref: refs/heads/main";
1562 const char *gitconfig_str = "[core]\n"
1563 "\trepositoryformatversion = 0\n"
1564 "\tfilemode = true\n"
1565 "\tbare = true\n";
1566 char *path;
1567 size_t i;
1569 if (!got_path_dir_is_empty(repo_path))
1570 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1572 for (i = 0; i < nitems(dirnames); i++) {
1573 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1574 return got_error_from_errno("asprintf");
1576 err = got_path_mkdir(path);
1577 free(path);
1578 if (err)
1579 return err;
1582 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1583 return got_error_from_errno("asprintf");
1584 err = got_path_create_file(path, description_str);
1585 free(path);
1586 if (err)
1587 return err;
1589 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1590 return got_error_from_errno("asprintf");
1591 err = got_path_create_file(path, headref_str);
1592 free(path);
1593 if (err)
1594 return err;
1596 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1597 return got_error_from_errno("asprintf");
1598 err = got_path_create_file(path, gitconfig_str);
1599 free(path);
1600 if (err)
1601 return err;
1603 return NULL;
1606 static const struct got_error *
1607 match_packed_object(struct got_object_id **unique_id,
1608 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1610 const struct got_error *err = NULL;
1611 struct got_object_id_queue matched_ids;
1612 struct got_pathlist_entry *pe;
1614 STAILQ_INIT(&matched_ids);
1616 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1617 const char *path_packidx = pe->path;
1618 struct got_packidx *packidx;
1619 struct got_object_qid *qid;
1621 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1622 path_packidx, 0);
1623 if (err)
1624 break;
1626 err = got_packidx_match_id_str_prefix(&matched_ids,
1627 packidx, id_str_prefix);
1628 if (err) {
1629 got_packidx_close(packidx);
1630 break;
1632 err = got_packidx_close(packidx);
1633 if (err)
1634 break;
1636 STAILQ_FOREACH(qid, &matched_ids, entry) {
1637 if (obj_type != GOT_OBJ_TYPE_ANY) {
1638 int matched_type;
1639 err = got_object_get_type(&matched_type, repo,
1640 &qid->id);
1641 if (err)
1642 goto done;
1643 if (matched_type != obj_type)
1644 continue;
1646 if (*unique_id == NULL) {
1647 *unique_id = got_object_id_dup(&qid->id);
1648 if (*unique_id == NULL) {
1649 err = got_error_from_errno("malloc");
1650 goto done;
1652 } else {
1653 if (got_object_id_cmp(*unique_id,
1654 &qid->id) == 0)
1655 continue; /* packed multiple times */
1656 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1657 goto done;
1661 done:
1662 got_object_id_queue_free(&matched_ids);
1663 if (err) {
1664 free(*unique_id);
1665 *unique_id = NULL;
1667 return err;
1670 static const struct got_error *
1671 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1672 const char *object_dir, const char *id_str_prefix, int obj_type,
1673 struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 char *path;
1677 DIR *dir = NULL;
1678 struct dirent *dent;
1679 struct got_object_id id;
1681 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1682 err = got_error_from_errno("asprintf");
1683 goto done;
1686 dir = opendir(path);
1687 if (dir == NULL) {
1688 if (errno == ENOENT) {
1689 err = NULL;
1690 goto done;
1692 err = got_error_from_errno2("opendir", path);
1693 goto done;
1695 while ((dent = readdir(dir)) != NULL) {
1696 char *id_str;
1697 int cmp;
1699 if (strcmp(dent->d_name, ".") == 0 ||
1700 strcmp(dent->d_name, "..") == 0)
1701 continue;
1703 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1704 err = got_error_from_errno("asprintf");
1705 goto done;
1708 if (!got_parse_sha1_digest(id.sha1, id_str))
1709 continue;
1712 * Directory entries do not necessarily appear in
1713 * sorted order, so we must iterate over all of them.
1715 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1716 if (cmp != 0) {
1717 free(id_str);
1718 continue;
1721 if (*unique_id == NULL) {
1722 if (obj_type != GOT_OBJ_TYPE_ANY) {
1723 int matched_type;
1724 err = got_object_get_type(&matched_type, repo,
1725 &id);
1726 if (err)
1727 goto done;
1728 if (matched_type != obj_type)
1729 continue;
1731 *unique_id = got_object_id_dup(&id);
1732 if (*unique_id == NULL) {
1733 err = got_error_from_errno("got_object_id_dup");
1734 free(id_str);
1735 goto done;
1737 } else {
1738 if (got_object_id_cmp(*unique_id, &id) == 0)
1739 continue; /* both packed and loose */
1740 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1741 free(id_str);
1742 goto done;
1745 done:
1746 if (dir && closedir(dir) != 0 && err == NULL)
1747 err = got_error_from_errno("closedir");
1748 if (err) {
1749 free(*unique_id);
1750 *unique_id = NULL;
1752 free(path);
1753 return err;
1756 const struct got_error *
1757 got_repo_match_object_id_prefix(struct got_object_id **id,
1758 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1760 const struct got_error *err = NULL;
1761 char *path_objects = got_repo_get_path_objects(repo);
1762 char *object_dir = NULL;
1763 size_t len;
1764 int i;
1766 *id = NULL;
1768 len = strlen(id_str_prefix);
1769 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1770 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1772 for (i = 0; i < len; i++) {
1773 if (isxdigit((unsigned char)id_str_prefix[i]))
1774 continue;
1775 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1778 if (len >= 2) {
1779 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1780 if (err)
1781 goto done;
1782 object_dir = strndup(id_str_prefix, 2);
1783 if (object_dir == NULL) {
1784 err = got_error_from_errno("strdup");
1785 goto done;
1787 err = match_loose_object(id, path_objects, object_dir,
1788 id_str_prefix, obj_type, repo);
1789 } else if (len == 1) {
1790 int i;
1791 for (i = 0; i < 0xf; i++) {
1792 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1793 == -1) {
1794 err = got_error_from_errno("asprintf");
1795 goto done;
1797 err = match_packed_object(id, repo, object_dir,
1798 obj_type);
1799 if (err)
1800 goto done;
1801 err = match_loose_object(id, path_objects, object_dir,
1802 id_str_prefix, obj_type, repo);
1803 if (err)
1804 goto done;
1806 } else {
1807 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1808 goto done;
1810 done:
1811 free(object_dir);
1812 if (err) {
1813 free(*id);
1814 *id = NULL;
1815 } else if (*id == NULL) {
1816 switch (obj_type) {
1817 case GOT_OBJ_TYPE_BLOB:
1818 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1819 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1820 break;
1821 case GOT_OBJ_TYPE_TREE:
1822 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1823 GOT_OBJ_LABEL_TREE, id_str_prefix);
1824 break;
1825 case GOT_OBJ_TYPE_COMMIT:
1826 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1827 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1828 break;
1829 case GOT_OBJ_TYPE_TAG:
1830 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1831 GOT_OBJ_LABEL_TAG, id_str_prefix);
1832 break;
1833 default:
1834 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1835 break;
1839 return err;
1842 const struct got_error *
1843 got_repo_match_object_id(struct got_object_id **id, char **label,
1844 const char *id_str, int obj_type, struct got_reflist_head *refs,
1845 struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_tag_object *tag;
1849 struct got_reference *ref = NULL;
1851 *id = NULL;
1852 if (label)
1853 *label = NULL;
1855 if (refs) {
1856 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1857 refs, repo);
1858 if (err == NULL) {
1859 *id = got_object_id_dup(
1860 got_object_tag_get_object_id(tag));
1861 if (*id == NULL)
1862 err = got_error_from_errno("got_object_id_dup");
1863 else if (label && asprintf(label, "refs/tags/%s",
1864 got_object_tag_get_name(tag)) == -1) {
1865 err = got_error_from_errno("asprintf");
1866 free(*id);
1867 *id = NULL;
1869 got_object_tag_close(tag);
1870 return err;
1871 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1872 err->code != GOT_ERR_NO_OBJ)
1873 return err;
1876 err = got_ref_open(&ref, repo, id_str, 0);
1877 if (err == NULL) {
1878 err = got_ref_resolve(id, repo, ref);
1879 if (err)
1880 goto done;
1881 if (label) {
1882 *label = strdup(got_ref_get_name(ref));
1883 if (*label == NULL) {
1884 err = got_error_from_errno("strdup");
1885 goto done;
1888 } else {
1889 if (err->code != GOT_ERR_NOT_REF &&
1890 err->code != GOT_ERR_BAD_REF_NAME)
1891 goto done;
1892 err = got_repo_match_object_id_prefix(id, id_str,
1893 obj_type, repo);
1894 if (err) {
1895 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1896 err = got_error_not_ref(id_str);
1897 goto done;
1899 if (label) {
1900 err = got_object_id_str(label, *id);
1901 if (*label == NULL) {
1902 err = got_error_from_errno("strdup");
1903 goto done;
1907 done:
1908 if (ref)
1909 got_ref_close(ref);
1910 return err;
1913 const struct got_error *
1914 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1915 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1917 const struct got_error *err = NULL;
1918 struct got_reflist_entry *re;
1919 struct got_object_id *tag_id;
1920 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1922 *tag = NULL;
1924 TAILQ_FOREACH(re, refs, entry) {
1925 const char *refname;
1926 refname = got_ref_get_name(re->ref);
1927 if (got_ref_is_symbolic(re->ref))
1928 continue;
1929 if (strncmp(refname, "refs/tags/", 10) != 0)
1930 continue;
1931 if (!name_is_absolute)
1932 refname += strlen("refs/tags/");
1933 if (strcmp(refname, name) != 0)
1934 continue;
1935 err = got_ref_resolve(&tag_id, repo, re->ref);
1936 if (err)
1937 break;
1938 err = got_object_open_as_tag(tag, repo, tag_id);
1939 free(tag_id);
1940 if (err)
1941 break;
1942 if (obj_type == GOT_OBJ_TYPE_ANY ||
1943 got_object_tag_get_object_type(*tag) == obj_type)
1944 break;
1945 got_object_tag_close(*tag);
1946 *tag = NULL;
1949 if (err == NULL && *tag == NULL)
1950 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1951 GOT_OBJ_LABEL_TAG, name);
1952 return err;
1955 static const struct got_error *
1956 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1957 const char *name, mode_t mode, struct got_object_id *blob_id)
1959 const struct got_error *err = NULL;
1961 *new_te = NULL;
1963 *new_te = calloc(1, sizeof(**new_te));
1964 if (*new_te == NULL)
1965 return got_error_from_errno("calloc");
1967 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1968 sizeof((*new_te)->name)) {
1969 err = got_error(GOT_ERR_NO_SPACE);
1970 goto done;
1973 if (S_ISLNK(mode)) {
1974 (*new_te)->mode = S_IFLNK;
1975 } else {
1976 (*new_te)->mode = S_IFREG;
1977 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1979 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1980 done:
1981 if (err && *new_te) {
1982 free(*new_te);
1983 *new_te = NULL;
1985 return err;
1988 static const struct got_error *
1989 import_file(struct got_tree_entry **new_te, struct dirent *de,
1990 const char *path, struct got_repository *repo)
1992 const struct got_error *err;
1993 struct got_object_id *blob_id = NULL;
1994 char *filepath;
1995 struct stat sb;
1997 if (asprintf(&filepath, "%s%s%s", path,
1998 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1999 return got_error_from_errno("asprintf");
2001 if (lstat(filepath, &sb) != 0) {
2002 err = got_error_from_errno2("lstat", path);
2003 goto done;
2006 err = got_object_blob_create(&blob_id, filepath, repo);
2007 if (err)
2008 goto done;
2010 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2011 blob_id);
2012 done:
2013 free(filepath);
2014 if (err)
2015 free(blob_id);
2016 return err;
2019 static const struct got_error *
2020 insert_tree_entry(struct got_tree_entry *new_te,
2021 struct got_pathlist_head *paths)
2023 const struct got_error *err = NULL;
2024 struct got_pathlist_entry *new_pe;
2026 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2027 if (err)
2028 return err;
2029 if (new_pe == NULL)
2030 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2031 return NULL;
2034 static const struct got_error *write_tree(struct got_object_id **,
2035 const char *, struct got_pathlist_head *, struct got_repository *,
2036 got_repo_import_cb progress_cb, void *progress_arg);
2038 static const struct got_error *
2039 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2040 const char *path, struct got_pathlist_head *ignores,
2041 struct got_repository *repo,
2042 got_repo_import_cb progress_cb, void *progress_arg)
2044 const struct got_error *err;
2045 struct got_object_id *id = NULL;
2046 char *subdirpath;
2048 if (asprintf(&subdirpath, "%s%s%s", path,
2049 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2050 return got_error_from_errno("asprintf");
2052 (*new_te) = calloc(1, sizeof(**new_te));
2053 if (*new_te == NULL)
2054 return got_error_from_errno("calloc");
2055 (*new_te)->mode = S_IFDIR;
2056 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2057 sizeof((*new_te)->name)) {
2058 err = got_error(GOT_ERR_NO_SPACE);
2059 goto done;
2061 err = write_tree(&id, subdirpath, ignores, repo,
2062 progress_cb, progress_arg);
2063 if (err)
2064 goto done;
2065 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2067 done:
2068 free(id);
2069 free(subdirpath);
2070 if (err) {
2071 free(*new_te);
2072 *new_te = NULL;
2074 return err;
2077 static const struct got_error *
2078 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2079 struct got_pathlist_head *ignores, struct got_repository *repo,
2080 got_repo_import_cb progress_cb, void *progress_arg)
2082 const struct got_error *err = NULL;
2083 DIR *dir;
2084 struct dirent *de;
2085 int nentries;
2086 struct got_tree_entry *new_te = NULL;
2087 struct got_pathlist_head paths;
2088 struct got_pathlist_entry *pe;
2090 *new_tree_id = NULL;
2092 TAILQ_INIT(&paths);
2094 dir = opendir(path_dir);
2095 if (dir == NULL) {
2096 err = got_error_from_errno2("opendir", path_dir);
2097 goto done;
2100 nentries = 0;
2101 while ((de = readdir(dir)) != NULL) {
2102 int ignore = 0;
2103 int type;
2105 if (strcmp(de->d_name, ".") == 0 ||
2106 strcmp(de->d_name, "..") == 0)
2107 continue;
2109 TAILQ_FOREACH(pe, ignores, entry) {
2110 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2111 ignore = 1;
2112 break;
2115 if (ignore)
2116 continue;
2118 err = got_path_dirent_type(&type, path_dir, de);
2119 if (err)
2120 goto done;
2122 if (type == DT_DIR) {
2123 err = import_subdir(&new_te, de, path_dir,
2124 ignores, repo, progress_cb, progress_arg);
2125 if (err) {
2126 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2127 goto done;
2128 err = NULL;
2129 continue;
2131 } else if (type == DT_REG || type == DT_LNK) {
2132 err = import_file(&new_te, de, path_dir, repo);
2133 if (err)
2134 goto done;
2135 } else
2136 continue;
2138 err = insert_tree_entry(new_te, &paths);
2139 if (err)
2140 goto done;
2141 nentries++;
2144 if (TAILQ_EMPTY(&paths)) {
2145 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2146 "cannot create tree without any entries");
2147 goto done;
2150 TAILQ_FOREACH(pe, &paths, entry) {
2151 struct got_tree_entry *te = pe->data;
2152 char *path;
2153 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2154 continue;
2155 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2156 err = got_error_from_errno("asprintf");
2157 goto done;
2159 err = (*progress_cb)(progress_arg, path);
2160 free(path);
2161 if (err)
2162 goto done;
2165 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2166 done:
2167 if (dir)
2168 closedir(dir);
2169 got_pathlist_free(&paths);
2170 return err;
2173 const struct got_error *
2174 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2175 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2176 struct got_repository *repo, got_repo_import_cb progress_cb,
2177 void *progress_arg)
2179 const struct got_error *err;
2180 struct got_object_id *new_tree_id;
2182 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2183 progress_cb, progress_arg);
2184 if (err)
2185 return err;
2187 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2188 author, time(NULL), author, time(NULL), logmsg, repo);
2189 free(new_tree_id);
2190 return err;
2193 const struct got_error *
2194 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2195 struct got_repository *repo)
2197 const struct got_error *err = NULL;
2198 char *path_objects = NULL, *path = NULL;
2199 DIR *dir = NULL;
2200 struct got_object_id id;
2201 int i;
2203 *nobjects = 0;
2204 *ondisk_size = 0;
2206 path_objects = got_repo_get_path_objects(repo);
2207 if (path_objects == NULL)
2208 return got_error_from_errno("got_repo_get_path_objects");
2210 for (i = 0; i <= 0xff; i++) {
2211 struct dirent *dent;
2213 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2214 err = got_error_from_errno("asprintf");
2215 break;
2218 dir = opendir(path);
2219 if (dir == NULL) {
2220 if (errno == ENOENT) {
2221 err = NULL;
2222 continue;
2224 err = got_error_from_errno2("opendir", path);
2225 break;
2228 while ((dent = readdir(dir)) != NULL) {
2229 char *id_str;
2230 int fd;
2231 struct stat sb;
2233 if (strcmp(dent->d_name, ".") == 0 ||
2234 strcmp(dent->d_name, "..") == 0)
2235 continue;
2237 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 goto done;
2242 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2243 free(id_str);
2244 continue;
2246 free(id_str);
2248 err = got_object_open_loose_fd(&fd, &id, repo);
2249 if (err)
2250 goto done;
2252 if (fstat(fd, &sb) == -1) {
2253 err = got_error_from_errno("fstat");
2254 close(fd);
2255 goto done;
2257 (*nobjects)++;
2258 (*ondisk_size) += sb.st_size;
2260 if (close(fd) == -1) {
2261 err = got_error_from_errno("close");
2262 goto done;
2266 if (closedir(dir) != 0) {
2267 err = got_error_from_errno("closedir");
2268 goto done;
2270 dir = NULL;
2272 free(path);
2273 path = NULL;
2275 done:
2276 if (dir && closedir(dir) != 0 && err == NULL)
2277 err = got_error_from_errno("closedir");
2279 if (err) {
2280 *nobjects = 0;
2281 *ondisk_size = 0;
2283 free(path_objects);
2284 free(path);
2285 return err;
2288 const struct got_error *
2289 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2290 off_t *total_packsize, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 DIR *packdir = NULL;
2294 struct dirent *dent;
2295 struct got_packidx *packidx = NULL;
2296 char *path_packidx;
2297 char *path_packfile;
2298 int packdir_fd;
2299 struct stat sb;
2301 *npackfiles = 0;
2302 *nobjects = 0;
2303 *total_packsize = 0;
2305 packdir_fd = openat(got_repo_get_fd(repo),
2306 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2307 if (packdir_fd == -1) {
2308 return got_error_from_errno_fmt("openat: %s/%s",
2309 got_repo_get_path_git_dir(repo),
2310 GOT_OBJECTS_PACK_DIR);
2313 packdir = fdopendir(packdir_fd);
2314 if (packdir == NULL) {
2315 err = got_error_from_errno("fdopendir");
2316 goto done;
2319 while ((dent = readdir(packdir)) != NULL) {
2320 if (!got_repo_is_packidx_filename(dent->d_name,
2321 strlen(dent->d_name)))
2322 continue;
2324 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2325 dent->d_name) == -1) {
2326 err = got_error_from_errno("asprintf");
2327 goto done;
2330 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2331 path_packidx, 0);
2332 free(path_packidx);
2333 if (err)
2334 goto done;
2336 if (fstat(packidx->fd, &sb) == -1)
2337 goto done;
2338 *total_packsize += sb.st_size;
2340 err = got_packidx_get_packfile_path(&path_packfile,
2341 packidx->path_packidx);
2342 if (err)
2343 goto done;
2345 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2346 0) == -1) {
2347 free(path_packfile);
2348 goto done;
2350 free(path_packfile);
2351 *total_packsize += sb.st_size;
2353 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2355 (*npackfiles)++;
2357 got_packidx_close(packidx);
2358 packidx = NULL;
2360 done:
2361 if (packidx)
2362 got_packidx_close(packidx);
2363 if (packdir && closedir(packdir) != 0 && err == NULL)
2364 err = got_error_from_errno("closedir");
2365 if (err) {
2366 *npackfiles = 0;
2367 *nobjects = 0;
2368 *total_packsize = 0;
2370 return err;
2373 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2374 got_packidx_bloom_filter_cmp);