portable: handle gotd/auth
[got-portable.git] / lib / repository.c
blob23dbb7bc21c9b25bb0c764457101d99b91e7795e
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25 #include <sys/resource.h>
27 #include <ctype.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 <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <errno.h>
39 #include <libgen.h>
40 #include <stdint.h>
42 #include "bloom.h"
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_delta_cache.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_hash.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #endif
69 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
71 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
74 static inline int
75 is_boolean_val(const char *val)
77 return (strcasecmp(val, "true") == 0 ||
78 strcasecmp(val, "false") == 0 ||
79 strcasecmp(val, "on") == 0 ||
80 strcasecmp(val, "off") == 0 ||
81 strcasecmp(val, "yes") == 0 ||
82 strcasecmp(val, "no") == 0 ||
83 strcasecmp(val, "1") == 0 ||
84 strcasecmp(val, "0") == 0);
87 static inline int
88 get_boolean_val(const char *val)
90 return (strcasecmp(val, "true") == 0 ||
91 strcasecmp(val, "on") == 0 ||
92 strcasecmp(val, "yes") == 0 ||
93 strcasecmp(val, "1") == 0);
96 const char *
97 got_repo_get_path(struct got_repository *repo)
99 return repo->path;
102 const char *
103 got_repo_get_path_git_dir(struct got_repository *repo)
105 return repo->path_git_dir;
109 got_repo_get_fd(struct got_repository *repo)
111 return repo->gitdir_fd;
114 enum got_hash_algorithm
115 got_repo_get_object_format(struct got_repository *repo)
117 return repo->algo;
120 const char *
121 got_repo_get_gitconfig_author_name(struct got_repository *repo)
123 return repo->gitconfig_author_name;
126 const char *
127 got_repo_get_gitconfig_author_email(struct got_repository *repo)
129 return repo->gitconfig_author_email;
132 const char *
133 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
135 return repo->global_gitconfig_author_name;
138 const char *
139 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
141 return repo->global_gitconfig_author_email;
144 const char *
145 got_repo_get_gitconfig_owner(struct got_repository *repo)
147 return repo->gitconfig_owner;
151 got_repo_has_extension(struct got_repository *repo, const char *ext)
153 int i;
155 for (i = 0; i < repo->nextensions; ++i) {
156 if (!strcasecmp(ext, repo->extnames[i]))
157 return get_boolean_val(repo->extvals[i]);
160 return 0;
164 got_repo_is_bare(struct got_repository *repo)
166 return (strcmp(repo->path, repo->path_git_dir) == 0);
169 static char *
170 get_path_git_child(struct got_repository *repo, const char *basename)
172 char *path_child;
174 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
175 basename) == -1)
176 return NULL;
178 return path_child;
181 char *
182 got_repo_get_path_objects(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_OBJECTS_DIR);
187 char *
188 got_repo_get_path_objects_pack(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
193 char *
194 got_repo_get_path_refs(struct got_repository *repo)
196 return get_path_git_child(repo, GOT_REFS_DIR);
199 char *
200 got_repo_get_path_packed_refs(struct got_repository *repo)
202 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
205 static char *
206 get_path_head(struct got_repository *repo)
208 return get_path_git_child(repo, GOT_HEAD_FILE);
211 char *
212 got_repo_get_path_gitconfig(struct got_repository *repo)
214 return get_path_git_child(repo, GOT_GITCONFIG);
217 char *
218 got_repo_get_path_gotconfig(struct got_repository *repo)
220 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
223 const struct got_gotconfig *
224 got_repo_get_gotconfig(struct got_repository *repo)
226 return repo->gotconfig;
229 void
230 got_repo_get_gitconfig_remotes(int *nremotes,
231 const struct got_remote_repo **remotes, struct got_repository *repo)
233 *nremotes = repo->ngitconfig_remotes;
234 *remotes = repo->gitconfig_remotes;
237 static int
238 is_git_repo(struct got_repository *repo)
240 const char *path_git = got_repo_get_path_git_dir(repo);
241 char *path_objects = got_repo_get_path_objects(repo);
242 char *path_refs = got_repo_get_path_refs(repo);
243 char *path_head = get_path_head(repo);
244 int ret = 0;
245 struct stat sb;
246 struct got_reference *head_ref;
248 if (lstat(path_git, &sb) == -1)
249 goto done;
250 if (!S_ISDIR(sb.st_mode))
251 goto done;
253 if (lstat(path_objects, &sb) == -1)
254 goto done;
255 if (!S_ISDIR(sb.st_mode))
256 goto done;
258 if (lstat(path_refs, &sb) == -1)
259 goto done;
260 if (!S_ISDIR(sb.st_mode))
261 goto done;
263 if (lstat(path_head, &sb) == -1)
264 goto done;
265 if (!S_ISREG(sb.st_mode))
266 goto done;
268 /* Check if the HEAD reference can be opened. */
269 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
270 goto done;
271 got_ref_close(head_ref);
273 ret = 1;
274 done:
275 free(path_objects);
276 free(path_refs);
277 free(path_head);
278 return ret;
282 static const struct got_error *
283 close_tempfiles(int *fds, size_t nfds)
285 const struct got_error *err = NULL;
286 int i;
288 for (i = 0; i < nfds; i++) {
289 if (fds[i] == -1)
290 continue;
291 if (close(fds[i]) == -1) {
292 err = got_error_from_errno("close");
293 break;
296 free(fds);
297 return err;
300 static const struct got_error *
301 open_tempfiles(int **fds, size_t array_size, size_t nfds)
303 const struct got_error *err = NULL;
304 int i;
306 *fds = calloc(array_size, sizeof(**fds));
307 if (*fds == NULL)
308 return got_error_from_errno("calloc");
310 for (i = 0; i < array_size; i++)
311 (*fds)[i] = -1;
313 for (i = 0; i < nfds; i++) {
314 (*fds)[i] = got_opentempfd();
315 if ((*fds)[i] == -1) {
316 err = got_error_from_errno("got_opentempfd");
317 close_tempfiles(*fds, nfds);
318 *fds = NULL;
319 return err;
323 return NULL;
326 static const struct got_error *
327 get_pack_cache_size(int *pack_cache_size)
329 struct rlimit rl;
331 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
332 return got_error_from_errno("getrlimit");
334 *pack_cache_size = GOT_PACK_CACHE_SIZE;
335 if (*pack_cache_size > rl.rlim_cur / 8)
336 *pack_cache_size = rl.rlim_cur / 8;
338 return NULL;
341 const struct got_error *
342 got_repo_pack_fds_open(int **pack_fds)
344 const struct got_error *err;
345 int nfds;
347 err = get_pack_cache_size(&nfds);
348 if (err)
349 return err;
352 * We need one basefd and one accumfd per cached pack.
353 * Our constants should be set up in a way such that
354 * this error never triggers.
356 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
357 return got_error(GOT_ERR_NO_SPACE);
359 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
362 const struct got_error *
363 got_repo_pack_fds_close(int *pack_fds)
365 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
368 const struct got_error *
369 got_repo_temp_fds_open(int **temp_fds)
371 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
372 GOT_REPO_NUM_TEMPFILES);
375 void
376 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
378 int i;
380 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
381 repo->tempfiles[i] = temp_fds[i];
384 const struct got_error *
385 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
387 int i;
389 *fd = -1;
390 *idx = -1;
392 for (i = 0; i < nitems(repo->tempfiles); i++) {
393 if (repo->tempfile_use_mask & (1 << i))
394 continue;
395 if (repo->tempfiles[i] != -1) {
396 if (ftruncate(repo->tempfiles[i], 0L) == -1)
397 return got_error_from_errno("ftruncate");
398 *fd = repo->tempfiles[i];
399 *idx = i;
400 repo->tempfile_use_mask |= (1 << i);
401 return NULL;
405 return got_error(GOT_ERR_REPO_TEMPFILE);
408 void
409 got_repo_temp_fds_put(int idx, struct got_repository *repo)
411 repo->tempfile_use_mask &= ~(1 << idx);
414 const struct got_error *
415 got_repo_temp_fds_close(int *temp_fds)
417 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
420 const struct got_error *
421 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
422 struct got_object *obj)
424 #ifndef GOT_NO_OBJ_CACHE
425 const struct got_error *err = NULL;
426 err = got_object_cache_add(&repo->objcache, id, obj);
427 if (err) {
428 if (err->code == GOT_ERR_OBJ_EXISTS ||
429 err->code == GOT_ERR_OBJ_TOO_LARGE)
430 err = NULL;
431 return err;
433 obj->refcnt++;
434 #endif
435 return NULL;
438 struct got_object *
439 got_repo_get_cached_object(struct got_repository *repo,
440 struct got_object_id *id)
442 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
445 const struct got_error *
446 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
447 struct got_tree_object *tree)
449 #ifndef GOT_NO_OBJ_CACHE
450 const struct got_error *err = NULL;
451 err = got_object_cache_add(&repo->treecache, id, tree);
452 if (err) {
453 if (err->code == GOT_ERR_OBJ_EXISTS ||
454 err->code == GOT_ERR_OBJ_TOO_LARGE)
455 err = NULL;
456 return err;
458 tree->refcnt++;
459 #endif
460 return NULL;
463 struct got_tree_object *
464 got_repo_get_cached_tree(struct got_repository *repo,
465 struct got_object_id *id)
467 return (struct got_tree_object *)got_object_cache_get(
468 &repo->treecache, id);
471 const struct got_error *
472 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
473 struct got_commit_object *commit)
475 #ifndef GOT_NO_OBJ_CACHE
476 const struct got_error *err = NULL;
477 err = got_object_cache_add(&repo->commitcache, id, commit);
478 if (err) {
479 if (err->code == GOT_ERR_OBJ_EXISTS ||
480 err->code == GOT_ERR_OBJ_TOO_LARGE)
481 err = NULL;
482 return err;
484 commit->refcnt++;
485 #endif
486 return NULL;
489 struct got_commit_object *
490 got_repo_get_cached_commit(struct got_repository *repo,
491 struct got_object_id *id)
493 return (struct got_commit_object *)got_object_cache_get(
494 &repo->commitcache, id);
497 const struct got_error *
498 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
499 struct got_tag_object *tag)
501 #ifndef GOT_NO_OBJ_CACHE
502 const struct got_error *err = NULL;
503 err = got_object_cache_add(&repo->tagcache, id, tag);
504 if (err) {
505 if (err->code == GOT_ERR_OBJ_EXISTS ||
506 err->code == GOT_ERR_OBJ_TOO_LARGE)
507 err = NULL;
508 return err;
510 tag->refcnt++;
511 #endif
512 return NULL;
515 struct got_tag_object *
516 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
518 return (struct got_tag_object *)got_object_cache_get(
519 &repo->tagcache, id);
522 const struct got_error *
523 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
524 struct got_raw_object *raw)
526 #ifndef GOT_NO_OBJ_CACHE
527 const struct got_error *err = NULL;
528 err = got_object_cache_add(&repo->rawcache, id, raw);
529 if (err) {
530 if (err->code == GOT_ERR_OBJ_EXISTS ||
531 err->code == GOT_ERR_OBJ_TOO_LARGE)
532 err = NULL;
533 return err;
535 raw->refcnt++;
536 #endif
537 return NULL;
541 struct got_raw_object *
542 got_repo_get_cached_raw_object(struct got_repository *repo,
543 struct got_object_id *id)
545 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
549 static const struct got_error *
550 open_repo(struct got_repository *repo, const char *path)
552 const struct got_error *err = NULL;
554 repo->gitdir_fd = -1;
556 /* bare git repository? */
557 repo->path_git_dir = strdup(path);
558 if (repo->path_git_dir == NULL)
559 return got_error_from_errno("strdup");
560 if (is_git_repo(repo)) {
561 repo->path = strdup(repo->path_git_dir);
562 if (repo->path == NULL) {
563 err = got_error_from_errno("strdup");
564 goto done;
566 repo->gitdir_fd = open(repo->path_git_dir,
567 O_DIRECTORY | O_CLOEXEC);
568 if (repo->gitdir_fd == -1) {
569 err = got_error_from_errno2("open",
570 repo->path_git_dir);
571 goto done;
573 return NULL;
576 /* git repository with working tree? */
577 free(repo->path_git_dir);
578 repo->path_git_dir = NULL;
579 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
580 err = got_error_from_errno("asprintf");
581 goto done;
583 if (is_git_repo(repo)) {
584 repo->path = strdup(path);
585 if (repo->path == NULL) {
586 err = got_error_from_errno("strdup");
587 goto done;
589 repo->gitdir_fd = open(repo->path_git_dir,
590 O_DIRECTORY | O_CLOEXEC);
591 if (repo->gitdir_fd == -1) {
592 err = got_error_from_errno2("open",
593 repo->path_git_dir);
594 goto done;
596 return NULL;
599 err = got_error(GOT_ERR_NOT_GIT_REPO);
600 done:
601 if (err) {
602 free(repo->path);
603 repo->path = NULL;
604 free(repo->path_git_dir);
605 repo->path_git_dir = NULL;
606 if (repo->gitdir_fd != -1)
607 close(repo->gitdir_fd);
608 repo->gitdir_fd = -1;
611 return err;
614 static const struct got_error *
615 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
617 const struct got_error *err = NULL;
618 char *repo_gitconfig_path = NULL;
620 if (global_gitconfig_path) {
621 /* Read settings from ~/.gitconfig. */
622 int dummy_repo_version;
623 err = got_repo_read_gitconfig(&dummy_repo_version,
624 &repo->global_gitconfig_author_name,
625 &repo->global_gitconfig_author_email,
626 NULL, NULL, NULL, NULL, NULL, NULL,
627 global_gitconfig_path);
628 if (err)
629 return err;
632 /* Read repository's .git/config file. */
633 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
634 if (repo_gitconfig_path == NULL)
635 return got_error_from_errno("got_repo_get_path_gitconfig");
637 err = got_repo_read_gitconfig(
638 &repo->gitconfig_repository_format_version,
639 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
640 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
641 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
642 &repo->nextensions, repo_gitconfig_path);
643 if (err)
644 goto done;
646 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
647 int i;
649 for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 got_repo_free_remote_repo_data(
651 &repo->gitconfig_remotes[i]);
653 free(repo->gitconfig_remotes);
654 repo->gitconfig_remotes = NULL;
655 repo->ngitconfig_remotes = 0;
657 free(repo->gitconfig_author_name);
658 repo->gitconfig_author_name = NULL;
659 free(repo->gitconfig_author_email);
660 repo->gitconfig_author_email = NULL;
662 free(repo->global_gitconfig_author_name);
663 repo->global_gitconfig_author_name = NULL;
664 free(repo->global_gitconfig_author_email);
665 repo->global_gitconfig_author_email = NULL;
668 done:
669 free(repo_gitconfig_path);
670 return err;
673 static const struct got_error *
674 read_gotconfig(struct got_repository *repo)
676 const struct got_error *err = NULL;
677 char *gotconfig_path;
679 gotconfig_path = got_repo_get_path_gotconfig(repo);
680 if (gotconfig_path == NULL)
681 return got_error_from_errno("got_repo_get_path_gotconfig");
683 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
684 free(gotconfig_path);
685 return err;
688 /* Supported repository format extensions. */
689 static const char *const repo_extensions[] = {
690 "noop", /* Got supports repository format version 1. */
691 "preciousObjects", /* Supported by gotadmin cleanup. */
692 "worktreeConfig", /* Got does not care about Git work trees. */
695 const struct got_error *
696 got_repo_open(struct got_repository **repop, const char *path,
697 const char *global_gitconfig_path, int *pack_fds)
699 struct got_repository *repo = NULL;
700 const struct got_error *err = NULL;
701 char *repo_path = NULL;
702 size_t i, j = 0;
704 *repop = NULL;
706 repo = calloc(1, sizeof(*repo));
707 if (repo == NULL)
708 return got_error_from_errno("calloc");
710 RB_INIT(&repo->packidx_bloom_filters);
711 TAILQ_INIT(&repo->packidx_paths);
713 for (i = 0; i < nitems(repo->privsep_children); i++) {
714 memset(&repo->privsep_children[i], 0,
715 sizeof(repo->privsep_children[0]));
716 repo->privsep_children[i].imsg_fd = -1;
719 err = got_object_cache_init(&repo->objcache,
720 GOT_OBJECT_CACHE_TYPE_OBJ);
721 if (err)
722 goto done;
723 err = got_object_cache_init(&repo->treecache,
724 GOT_OBJECT_CACHE_TYPE_TREE);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->commitcache,
728 GOT_OBJECT_CACHE_TYPE_COMMIT);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->tagcache,
732 GOT_OBJECT_CACHE_TYPE_TAG);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->rawcache,
736 GOT_OBJECT_CACHE_TYPE_RAW);
737 if (err)
738 goto done;
740 err = get_pack_cache_size(&repo->pack_cache_size);
741 if (err)
742 goto done;
743 for (i = 0; i < nitems(repo->packs); i++) {
744 if (pack_fds != NULL && i < repo->pack_cache_size) {
745 repo->packs[i].basefd = pack_fds[j++];
746 repo->packs[i].accumfd = pack_fds[j++];
747 } else {
748 repo->packs[i].basefd = -1;
749 repo->packs[i].accumfd = -1;
752 for (i = 0; i < nitems(repo->tempfiles); i++)
753 repo->tempfiles[i] = -1;
754 repo->pinned_pack = -1;
755 repo->pinned_packidx = -1;
756 repo->pinned_pid = 0;
758 repo_path = realpath(path, NULL);
759 if (repo_path == NULL) {
760 err = got_error_from_errno2("realpath", path);
761 goto done;
764 for (;;) {
765 char *parent_path;
767 err = open_repo(repo, repo_path);
768 if (err == NULL)
769 break;
770 if (err->code != GOT_ERR_NOT_GIT_REPO)
771 goto done;
772 if (repo_path[0] == '/' && repo_path[1] == '\0') {
773 err = got_error(GOT_ERR_NOT_GIT_REPO);
774 goto done;
776 err = got_path_dirname(&parent_path, repo_path);
777 if (err)
778 goto done;
779 free(repo_path);
780 repo_path = parent_path;
783 err = read_gotconfig(repo);
784 if (err)
785 goto done;
787 err = read_gitconfig(repo, global_gitconfig_path);
788 if (err)
789 goto done;
790 if (repo->gitconfig_repository_format_version != 0) {
791 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
792 goto done;
794 for (i = 0; i < repo->nextensions; i++) {
795 char *ext = repo->extnames[i];
796 char *val = repo->extvals[i];
797 int j, supported = 0;
799 if (!is_boolean_val(val)) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 goto done;
804 if (!get_boolean_val(val))
805 continue;
807 for (j = 0; j < nitems(repo_extensions); j++) {
808 if (strcmp(ext, repo_extensions[j]) == 0) {
809 supported = 1;
810 break;
813 if (!supported) {
814 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
815 goto done;
819 err = got_repo_list_packidx(&repo->packidx_paths, repo);
820 done:
821 if (err)
822 got_repo_close(repo);
823 else
824 *repop = repo;
825 free(repo_path);
826 return err;
829 const struct got_error *
830 got_repo_close(struct got_repository *repo)
832 const struct got_error *err = NULL, *child_err;
833 struct got_packidx_bloom_filter *bf;
834 size_t i;
836 for (i = 0; i < repo->pack_cache_size; i++) {
837 if (repo->packidx_cache[i] == NULL)
838 break;
839 got_packidx_close(repo->packidx_cache[i]);
842 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters))) {
844 RB_REMOVE(got_packidx_bloom_filter_tree,
845 &repo->packidx_bloom_filters, bf);
846 bloom_free(bf->bloom);
847 free(bf->bloom);
848 free(bf);
851 for (i = 0; i < repo->pack_cache_size; i++)
852 if (repo->packs[i].path_packfile)
853 if (repo->packs[i].path_packfile)
854 got_pack_close(&repo->packs[i]);
856 free(repo->path);
857 free(repo->path_git_dir);
859 got_object_cache_close(&repo->objcache);
860 got_object_cache_close(&repo->treecache);
861 got_object_cache_close(&repo->commitcache);
862 got_object_cache_close(&repo->tagcache);
863 got_object_cache_close(&repo->rawcache);
865 for (i = 0; i < nitems(repo->privsep_children); i++) {
866 if (repo->privsep_children[i].imsg_fd == -1)
867 continue;
868 imsg_clear(repo->privsep_children[i].ibuf);
869 free(repo->privsep_children[i].ibuf);
870 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
871 if (err && err->code == GOT_ERR_EOF)
872 err = NULL;
873 child_err = got_privsep_wait_for_child(
874 repo->privsep_children[i].pid);
875 if (child_err && err == NULL)
876 err = child_err;
877 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
878 err == NULL)
879 err = got_error_from_errno("close");
882 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
883 err == NULL)
884 err = got_error_from_errno("close");
886 if (repo->gotconfig)
887 got_gotconfig_free(repo->gotconfig);
888 free(repo->gitconfig_author_name);
889 free(repo->gitconfig_author_email);
890 for (i = 0; i < repo->ngitconfig_remotes; i++)
891 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
892 free(repo->gitconfig_remotes);
893 for (i = 0; i < repo->nextensions; i++) {
894 free(repo->extnames[i]);
895 free(repo->extvals[i]);
897 free(repo->extnames);
898 free(repo->extvals);
900 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
901 free(repo);
903 return err;
906 void
907 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
909 int i;
911 free(repo->name);
912 repo->name = NULL;
913 free(repo->fetch_url);
914 repo->fetch_url = NULL;
915 free(repo->send_url);
916 repo->send_url = NULL;
917 for (i = 0; i < repo->nfetch_branches; i++)
918 free(repo->fetch_branches[i]);
919 free(repo->fetch_branches);
920 repo->fetch_branches = NULL;
921 repo->nfetch_branches = 0;
922 for (i = 0; i < repo->nsend_branches; i++)
923 free(repo->send_branches[i]);
924 free(repo->send_branches);
925 repo->send_branches = NULL;
926 repo->nsend_branches = 0;
929 const struct got_error *
930 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
931 const char *input_path)
933 const struct got_error *err = NULL;
934 const char *repo_abspath = NULL;
935 size_t repolen, len;
936 char *canonpath, *path = NULL;
938 *in_repo_path = NULL;
940 canonpath = strdup(input_path);
941 if (canonpath == NULL) {
942 err = got_error_from_errno("strdup");
943 goto done;
945 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
946 if (err)
947 goto done;
949 repo_abspath = got_repo_get_path(repo);
951 if (canonpath[0] == '\0') {
952 path = strdup(canonpath);
953 if (path == NULL) {
954 err = got_error_from_errno("strdup");
955 goto done;
957 } else {
958 path = realpath(canonpath, NULL);
959 if (path == NULL) {
960 if (errno != ENOENT) {
961 err = got_error_from_errno2("realpath",
962 canonpath);
963 goto done;
966 * Path is not on disk.
967 * Assume it is already relative to repository root.
969 path = strdup(canonpath);
970 if (path == NULL) {
971 err = got_error_from_errno("strdup");
972 goto done;
976 repolen = strlen(repo_abspath);
977 len = strlen(path);
980 if (strcmp(path, repo_abspath) == 0) {
981 free(path);
982 path = strdup("");
983 if (path == NULL) {
984 err = got_error_from_errno("strdup");
985 goto done;
987 } else if (len > repolen &&
988 got_path_is_child(path, repo_abspath, repolen)) {
989 /* Matched an on-disk path inside repository. */
990 if (got_repo_is_bare(repo)) {
992 * Matched an on-disk path inside repository
993 * database. Treat input as repository-relative.
995 free(path);
996 path = canonpath;
997 canonpath = NULL;
998 } else {
999 char *child;
1000 /* Strip common prefix with repository path. */
1001 err = got_path_skip_common_ancestor(&child,
1002 repo_abspath, path);
1003 if (err)
1004 goto done;
1005 free(path);
1006 path = child;
1008 } else {
1010 * Matched unrelated on-disk path.
1011 * Treat input as repository-relative.
1013 free(path);
1014 path = canonpath;
1015 canonpath = NULL;
1019 /* Make in-repository path absolute */
1020 if (path[0] != '/') {
1021 char *abspath;
1022 if (asprintf(&abspath, "/%s", path) == -1) {
1023 err = got_error_from_errno("asprintf");
1024 goto done;
1026 free(path);
1027 path = abspath;
1030 done:
1031 free(canonpath);
1032 if (err)
1033 free(path);
1034 else
1035 *in_repo_path = path;
1036 return err;
1039 static const struct got_error *
1040 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1041 const char *path_packidx)
1043 const struct got_error *err = NULL;
1044 size_t i;
1046 for (i = 0; i < repo->pack_cache_size; i++) {
1047 if (repo->packidx_cache[i] == NULL)
1048 break;
1049 if (strcmp(repo->packidx_cache[i]->path_packidx,
1050 path_packidx) == 0) {
1051 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1054 if (i == repo->pack_cache_size) {
1055 do {
1056 i--;
1057 } while (i > 0 && repo->pinned_packidx >= 0 &&
1058 i == repo->pinned_packidx);
1059 err = got_packidx_close(repo->packidx_cache[i]);
1060 if (err)
1061 return err;
1064 repo->packidx_cache[i] = packidx;
1066 return NULL;
1070 got_repo_is_packidx_filename(const char *name, size_t len)
1072 if (len != GOT_PACKIDX_NAMELEN)
1073 return 0;
1075 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1076 return 0;
1078 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1079 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1080 return 0;
1082 return 1;
1085 static struct got_packidx_bloom_filter *
1086 get_packidx_bloom_filter(struct got_repository *repo,
1087 const char *path, size_t path_len)
1089 struct got_packidx_bloom_filter key;
1091 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1092 return NULL; /* XXX */
1093 key.path_len = path_len;
1095 return RB_FIND(got_packidx_bloom_filter_tree,
1096 &repo->packidx_bloom_filters, &key);
1100 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1101 const char *path_packidx, struct got_object_id *id)
1103 struct got_packidx_bloom_filter *bf;
1105 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1106 if (bf)
1107 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1109 /* No bloom filter means this pack index must be searched. */
1110 return 1;
1113 static const struct got_error *
1114 add_packidx_bloom_filter(struct got_repository *repo,
1115 struct got_packidx *packidx, const char *path_packidx)
1117 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1118 struct got_packidx_bloom_filter *bf;
1119 size_t len;
1122 * Don't use bloom filters for very large pack index files.
1123 * Large pack files will contain a relatively large fraction
1124 * of our objects so we will likely need to visit them anyway.
1125 * The more objects a pack file contains the higher the probability
1126 * of a false-positive match from the bloom filter. And reading
1127 * all object IDs from a large pack index file can be expensive.
1129 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1130 return NULL;
1132 /* Do we already have a filter for this pack index? */
1133 if (get_packidx_bloom_filter(repo, path_packidx,
1134 strlen(path_packidx)) != NULL)
1135 return NULL;
1137 bf = calloc(1, sizeof(*bf));
1138 if (bf == NULL)
1139 return got_error_from_errno("calloc");
1140 bf->bloom = calloc(1, sizeof(*bf->bloom));
1141 if (bf->bloom == NULL) {
1142 free(bf);
1143 return got_error_from_errno("calloc");
1146 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1147 if (len >= sizeof(bf->path)) {
1148 free(bf->bloom);
1149 free(bf);
1150 return got_error(GOT_ERR_NO_SPACE);
1152 bf->path_len = len;
1154 /* Minimum size supported by our bloom filter is 1000 entries. */
1155 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1156 for (i = 0; i < nobjects; i++) {
1157 struct got_packidx_object_id *id;
1158 id = &packidx->hdr.sorted_ids[i];
1159 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1162 RB_INSERT(got_packidx_bloom_filter_tree,
1163 &repo->packidx_bloom_filters, bf);
1164 return NULL;
1167 static void
1168 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1170 struct got_pathlist_entry *pe;
1172 while (!TAILQ_EMPTY(packidx_paths)) {
1173 pe = TAILQ_FIRST(packidx_paths);
1174 TAILQ_REMOVE(packidx_paths, pe, entry);
1175 free((char *)pe->path);
1176 free(pe);
1180 static const struct got_error *
1181 refresh_packidx_paths(struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 char *objects_pack_dir = NULL;
1185 struct stat sb;
1187 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1188 if (objects_pack_dir == NULL)
1189 return got_error_from_errno("got_repo_get_path_objects_pack");
1191 if (stat(objects_pack_dir, &sb) == -1) {
1192 if (errno != ENOENT) {
1193 err = got_error_from_errno2("stat", objects_pack_dir);
1194 goto done;
1196 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1197 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1198 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1199 purge_packidx_paths(&repo->packidx_paths);
1200 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1201 if (err)
1202 goto done;
1204 done:
1205 free(objects_pack_dir);
1206 return err;
1209 const struct got_error *
1210 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1211 struct got_repository *repo, struct got_object_id *id)
1213 const struct got_error *err;
1214 struct got_pathlist_entry *pe;
1215 size_t i;
1217 /* Search pack index cache. */
1218 for (i = 0; i < repo->pack_cache_size; i++) {
1219 if (repo->packidx_cache[i] == NULL)
1220 break;
1221 if (!got_repo_check_packidx_bloom_filter(repo,
1222 repo->packidx_cache[i]->path_packidx, id))
1223 continue; /* object will not be found in this index */
1224 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1225 if (*idx != -1) {
1226 *packidx = repo->packidx_cache[i];
1228 * Move this cache entry to the front. Repeatedly
1229 * searching a wrong pack index can be expensive.
1231 if (i > 0) {
1232 memmove(&repo->packidx_cache[1],
1233 &repo->packidx_cache[0],
1234 i * sizeof(repo->packidx_cache[0]));
1235 repo->packidx_cache[0] = *packidx;
1236 if (repo->pinned_packidx >= 0 &&
1237 repo->pinned_packidx < i)
1238 repo->pinned_packidx++;
1239 else if (repo->pinned_packidx == i)
1240 repo->pinned_packidx = 0;
1242 return NULL;
1245 /* No luck. Search the filesystem. */
1247 err = refresh_packidx_paths(repo);
1248 if (err)
1249 return err;
1251 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1252 const char *path_packidx = pe->path;
1253 int is_cached = 0;
1255 if (!got_repo_check_packidx_bloom_filter(repo,
1256 pe->path, id))
1257 continue; /* object will not be found in this index */
1259 for (i = 0; i < repo->pack_cache_size; i++) {
1260 if (repo->packidx_cache[i] == NULL)
1261 break;
1262 if (strcmp(repo->packidx_cache[i]->path_packidx,
1263 path_packidx) == 0) {
1264 is_cached = 1;
1265 break;
1268 if (is_cached)
1269 continue; /* already searched */
1271 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1272 path_packidx, 0);
1273 if (err)
1274 goto done;
1276 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1277 if (err)
1278 goto done;
1280 err = cache_packidx(repo, *packidx, path_packidx);
1281 if (err)
1282 goto done;
1284 *idx = got_packidx_get_object_idx(*packidx, id);
1285 if (*idx != -1) {
1286 err = NULL; /* found the object */
1287 goto done;
1291 err = got_error_no_obj(id);
1292 done:
1293 return err;
1296 const struct got_error *
1297 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1298 struct got_repository *repo)
1300 const struct got_error *err = NULL;
1301 DIR *packdir = NULL;
1302 struct dirent *dent;
1303 char *path_packidx = NULL;
1304 int packdir_fd;
1305 struct stat sb;
1307 packdir_fd = openat(got_repo_get_fd(repo),
1308 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1309 if (packdir_fd == -1) {
1310 return got_error_from_errno_fmt("openat: %s/%s",
1311 got_repo_get_path_git_dir(repo),
1312 GOT_OBJECTS_PACK_DIR);
1315 packdir = fdopendir(packdir_fd);
1316 if (packdir == NULL) {
1317 err = got_error_from_errno("fdopendir");
1318 goto done;
1321 if (fstat(packdir_fd, &sb) == -1) {
1322 err = got_error_from_errno("fstat");
1323 goto done;
1325 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1326 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1328 while ((dent = readdir(packdir)) != NULL) {
1329 if (!got_repo_is_packidx_filename(dent->d_name,
1330 strlen(dent->d_name)))
1331 continue;
1333 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1334 dent->d_name) == -1) {
1335 err = got_error_from_errno("asprintf");
1336 path_packidx = NULL;
1337 break;
1340 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1341 if (err)
1342 break;
1344 done:
1345 if (err)
1346 free(path_packidx);
1347 if (packdir && closedir(packdir) != 0 && err == NULL)
1348 err = got_error_from_errno("closedir");
1349 return err;
1352 const struct got_error *
1353 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1354 struct got_repository *repo)
1356 const struct got_error *err;
1357 size_t i;
1359 *packidx = NULL;
1361 /* Search pack index cache. */
1362 for (i = 0; i < repo->pack_cache_size; i++) {
1363 if (repo->packidx_cache[i] == NULL)
1364 break;
1365 if (strcmp(repo->packidx_cache[i]->path_packidx,
1366 path_packidx) == 0) {
1367 *packidx = repo->packidx_cache[i];
1368 return NULL;
1371 /* No luck. Search the filesystem. */
1373 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1374 path_packidx, 0);
1375 if (err)
1376 return err;
1378 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1379 if (err)
1380 goto done;
1382 err = cache_packidx(repo, *packidx, path_packidx);
1383 done:
1384 if (err) {
1385 got_packidx_close(*packidx);
1386 *packidx = NULL;
1388 return err;
1391 static const struct got_error *
1392 read_packfile_hdr(int fd, struct got_packidx *packidx)
1394 const struct got_error *err = NULL;
1395 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1396 struct got_packfile_hdr hdr;
1397 ssize_t n;
1399 n = read(fd, &hdr, sizeof(hdr));
1400 if (n < 0)
1401 return got_error_from_errno("read");
1402 if (n != sizeof(hdr))
1403 return got_error(GOT_ERR_BAD_PACKFILE);
1405 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1406 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1407 be32toh(hdr.nobjects) != totobj)
1408 err = got_error(GOT_ERR_BAD_PACKFILE);
1410 return err;
1413 static const struct got_error *
1414 open_packfile(int *fd, struct got_repository *repo,
1415 const char *relpath, struct got_packidx *packidx)
1417 const struct got_error *err = NULL;
1419 *fd = openat(got_repo_get_fd(repo), relpath,
1420 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1421 if (*fd == -1)
1422 return got_error_from_errno_fmt("openat: %s/%s",
1423 got_repo_get_path_git_dir(repo), relpath);
1425 if (packidx) {
1426 err = read_packfile_hdr(*fd, packidx);
1427 if (err) {
1428 close(*fd);
1429 *fd = -1;
1433 return err;
1436 const struct got_error *
1437 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1438 const char *path_packfile, struct got_packidx *packidx)
1440 const struct got_error *err = NULL;
1441 struct got_pack *pack = NULL;
1442 struct stat sb;
1443 size_t i;
1445 if (packp)
1446 *packp = NULL;
1448 for (i = 0; i < repo->pack_cache_size; i++) {
1449 pack = &repo->packs[i];
1450 if (pack->path_packfile == NULL)
1451 break;
1452 if (strcmp(pack->path_packfile, path_packfile) == 0)
1453 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1456 if (i == repo->pack_cache_size) {
1457 struct got_pack tmp;
1458 do {
1459 i--;
1460 } while (i > 0 && repo->pinned_pack >= 0 &&
1461 i == repo->pinned_pack);
1462 err = got_pack_close(&repo->packs[i]);
1463 if (err)
1464 return err;
1465 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1466 return got_error_from_errno("ftruncate");
1467 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1468 return got_error_from_errno("ftruncate");
1469 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1470 memcpy(&repo->packs[i], &repo->packs[0],
1471 sizeof(repo->packs[i]));
1472 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1473 if (repo->pinned_pack == 0)
1474 repo->pinned_pack = i;
1475 else if (repo->pinned_pack == i)
1476 repo->pinned_pack = 0;
1477 i = 0;
1480 pack = &repo->packs[i];
1482 pack->path_packfile = strdup(path_packfile);
1483 if (pack->path_packfile == NULL) {
1484 err = got_error_from_errno("strdup");
1485 goto done;
1488 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1489 if (err)
1490 goto done;
1492 if (fstat(pack->fd, &sb) != 0) {
1493 err = got_error_from_errno("fstat");
1494 goto done;
1496 pack->filesize = sb.st_size;
1498 pack->privsep_child = NULL;
1500 err = got_delta_cache_alloc(&pack->delta_cache);
1501 if (err)
1502 goto done;
1504 #ifndef GOT_PACK_NO_MMAP
1505 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1506 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1507 pack->fd, 0);
1508 if (pack->map == MAP_FAILED) {
1509 if (errno != ENOMEM) {
1510 err = got_error_from_errno("mmap");
1511 goto done;
1513 pack->map = NULL; /* fall back to read(2) */
1516 #endif
1517 done:
1518 if (err) {
1519 if (pack)
1520 got_pack_close(pack);
1521 } else if (packp)
1522 *packp = pack;
1523 return err;
1526 struct got_pack *
1527 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1529 struct got_pack *pack = NULL;
1530 size_t i;
1532 for (i = 0; i < repo->pack_cache_size; i++) {
1533 pack = &repo->packs[i];
1534 if (pack->path_packfile == NULL)
1535 break;
1536 if (strcmp(pack->path_packfile, path_packfile) == 0)
1537 return pack;
1540 return NULL;
1543 const struct got_error *
1544 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1545 struct got_pack *pack)
1547 size_t i;
1548 int pinned_pack = -1, pinned_packidx = -1;
1550 for (i = 0; i < repo->pack_cache_size; i++) {
1551 if (repo->packidx_cache[i] &&
1552 strcmp(repo->packidx_cache[i]->path_packidx,
1553 packidx->path_packidx) == 0)
1554 pinned_packidx = i;
1555 if (repo->packs[i].path_packfile &&
1556 strcmp(repo->packs[i].path_packfile,
1557 pack->path_packfile) == 0)
1558 pinned_pack = i;
1561 if (pinned_packidx == -1 || pinned_pack == -1)
1562 return got_error(GOT_ERR_PIN_PACK);
1564 repo->pinned_pack = pinned_pack;
1565 repo->pinned_packidx = pinned_packidx;
1566 if (repo->packs[pinned_pack].privsep_child)
1567 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1568 return NULL;
1571 struct got_pack *
1572 got_repo_get_pinned_pack(struct got_repository *repo)
1574 if (repo->pinned_pack >= 0 &&
1575 repo->pinned_pack < repo->pack_cache_size)
1576 return &repo->packs[repo->pinned_pack];
1578 return NULL;
1581 void
1582 got_repo_unpin_pack(struct got_repository *repo)
1584 repo->pinned_packidx = -1;
1585 repo->pinned_pack = -1;
1586 repo->pinned_pid = 0;
1589 const struct got_error *
1590 got_repo_init(const char *repo_path, const char *head_name)
1592 const struct got_error *err = NULL;
1593 const char *dirnames[] = {
1594 GOT_OBJECTS_DIR,
1595 GOT_OBJECTS_PACK_DIR,
1596 GOT_REFS_DIR,
1598 const char *description_str = "Unnamed repository; "
1599 "edit this file 'description' to name the repository.";
1600 const char *headref = "ref: refs/heads/";
1601 const char *gitconfig_str = "[core]\n"
1602 "\trepositoryformatversion = 0\n"
1603 "\tfilemode = true\n"
1604 "\tbare = true\n";
1605 char *headref_str, *path;
1606 size_t i;
1608 if (!got_path_dir_is_empty(repo_path))
1609 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1611 for (i = 0; i < nitems(dirnames); i++) {
1612 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1613 return got_error_from_errno("asprintf");
1615 err = got_path_mkdir(path);
1616 free(path);
1617 if (err)
1618 return err;
1621 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1622 return got_error_from_errno("asprintf");
1623 err = got_path_create_file(path, description_str);
1624 free(path);
1625 if (err)
1626 return err;
1628 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1629 return got_error_from_errno("asprintf");
1630 if (asprintf(&headref_str, "%s%s", headref,
1631 head_name ? head_name : "main") == -1) {
1632 free(path);
1633 return got_error_from_errno("asprintf");
1635 err = got_path_create_file(path, headref_str);
1636 free(headref_str);
1637 free(path);
1638 if (err)
1639 return err;
1641 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1642 return got_error_from_errno("asprintf");
1643 err = got_path_create_file(path, gitconfig_str);
1644 free(path);
1645 if (err)
1646 return err;
1648 return NULL;
1651 static const struct got_error *
1652 match_packed_object(struct got_object_id **unique_id,
1653 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1655 const struct got_error *err = NULL;
1656 struct got_object_id_queue matched_ids;
1657 struct got_pathlist_entry *pe;
1659 STAILQ_INIT(&matched_ids);
1661 err = refresh_packidx_paths(repo);
1662 if (err)
1663 return err;
1665 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1666 const char *path_packidx = pe->path;
1667 struct got_packidx *packidx;
1668 struct got_object_qid *qid;
1670 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1671 path_packidx, 0);
1672 if (err)
1673 break;
1675 err = got_packidx_match_id_str_prefix(&matched_ids,
1676 packidx, id_str_prefix);
1677 if (err) {
1678 got_packidx_close(packidx);
1679 break;
1681 err = got_packidx_close(packidx);
1682 if (err)
1683 break;
1685 STAILQ_FOREACH(qid, &matched_ids, entry) {
1686 if (obj_type != GOT_OBJ_TYPE_ANY) {
1687 int matched_type;
1688 err = got_object_get_type(&matched_type, repo,
1689 &qid->id);
1690 if (err)
1691 goto done;
1692 if (matched_type != obj_type)
1693 continue;
1695 if (*unique_id == NULL) {
1696 *unique_id = got_object_id_dup(&qid->id);
1697 if (*unique_id == NULL) {
1698 err = got_error_from_errno("malloc");
1699 goto done;
1701 } else {
1702 if (got_object_id_cmp(*unique_id,
1703 &qid->id) == 0)
1704 continue; /* packed multiple times */
1705 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1706 goto done;
1710 done:
1711 got_object_id_queue_free(&matched_ids);
1712 if (err) {
1713 free(*unique_id);
1714 *unique_id = NULL;
1716 return err;
1719 static const struct got_error *
1720 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1721 const char *object_dir, const char *id_str_prefix, int obj_type,
1722 struct got_repository *repo)
1724 const struct got_error *err = NULL;
1725 char *path, *id_str = NULL;
1726 DIR *dir = NULL;
1727 struct dirent *dent;
1728 struct got_object_id id;
1730 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1731 err = got_error_from_errno("asprintf");
1732 goto done;
1735 dir = opendir(path);
1736 if (dir == NULL) {
1737 if (errno == ENOENT) {
1738 err = NULL;
1739 goto done;
1741 err = got_error_from_errno2("opendir", path);
1742 goto done;
1744 while ((dent = readdir(dir)) != NULL) {
1745 int cmp;
1747 free(id_str);
1748 id_str = NULL;
1750 if (strcmp(dent->d_name, ".") == 0 ||
1751 strcmp(dent->d_name, "..") == 0)
1752 continue;
1754 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1755 err = got_error_from_errno("asprintf");
1756 goto done;
1759 if (!got_parse_object_id(&id, id_str, repo->algo))
1760 continue;
1763 * Directory entries do not necessarily appear in
1764 * sorted order, so we must iterate over all of them.
1766 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1767 if (cmp != 0)
1768 continue;
1770 if (*unique_id == NULL) {
1771 if (obj_type != GOT_OBJ_TYPE_ANY) {
1772 int matched_type;
1773 err = got_object_get_type(&matched_type, repo,
1774 &id);
1775 if (err)
1776 goto done;
1777 if (matched_type != obj_type)
1778 continue;
1780 *unique_id = got_object_id_dup(&id);
1781 if (*unique_id == NULL) {
1782 err = got_error_from_errno("got_object_id_dup");
1783 goto done;
1785 } else {
1786 if (got_object_id_cmp(*unique_id, &id) == 0)
1787 continue; /* both packed and loose */
1788 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1789 goto done;
1792 done:
1793 if (dir && closedir(dir) != 0 && err == NULL)
1794 err = got_error_from_errno("closedir");
1795 if (err) {
1796 free(*unique_id);
1797 *unique_id = NULL;
1799 free(id_str);
1800 free(path);
1801 return err;
1804 const struct got_error *
1805 got_repo_match_object_id_prefix(struct got_object_id **id,
1806 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1808 const struct got_error *err = NULL;
1809 char *path_objects = NULL, *object_dir = NULL;
1810 size_t len;
1811 int i;
1813 *id = NULL;
1815 path_objects = got_repo_get_path_objects(repo);
1817 len = strlen(id_str_prefix);
1818 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1819 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1820 goto done;
1823 for (i = 0; i < len; i++) {
1824 if (isxdigit((unsigned char)id_str_prefix[i]))
1825 continue;
1826 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1827 goto done;
1830 if (len >= 2) {
1831 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1832 if (err)
1833 goto done;
1834 object_dir = strndup(id_str_prefix, 2);
1835 if (object_dir == NULL) {
1836 err = got_error_from_errno("strdup");
1837 goto done;
1839 err = match_loose_object(id, path_objects, object_dir,
1840 id_str_prefix, obj_type, repo);
1841 } else if (len == 1) {
1842 int i;
1843 for (i = 0; i < 0xf; i++) {
1844 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1845 == -1) {
1846 err = got_error_from_errno("asprintf");
1847 goto done;
1849 err = match_packed_object(id, repo, object_dir,
1850 obj_type);
1851 if (err)
1852 goto done;
1853 err = match_loose_object(id, path_objects, object_dir,
1854 id_str_prefix, obj_type, repo);
1855 if (err)
1856 goto done;
1858 } else {
1859 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1860 goto done;
1862 done:
1863 free(path_objects);
1864 free(object_dir);
1865 if (err) {
1866 free(*id);
1867 *id = NULL;
1868 } else if (*id == NULL) {
1869 switch (obj_type) {
1870 case GOT_OBJ_TYPE_BLOB:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1873 break;
1874 case GOT_OBJ_TYPE_TREE:
1875 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1876 GOT_OBJ_LABEL_TREE, id_str_prefix);
1877 break;
1878 case GOT_OBJ_TYPE_COMMIT:
1879 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1880 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1881 break;
1882 case GOT_OBJ_TYPE_TAG:
1883 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1884 GOT_OBJ_LABEL_TAG, id_str_prefix);
1885 break;
1886 default:
1887 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1888 break;
1892 return err;
1895 const struct got_error *
1896 got_repo_match_object_id(struct got_object_id **id, char **label,
1897 const char *id_str, int obj_type, struct got_reflist_head *refs,
1898 struct got_repository *repo)
1900 const struct got_error *err;
1901 struct got_tag_object *tag;
1902 struct got_reference *ref = NULL;
1904 *id = NULL;
1905 if (label)
1906 *label = NULL;
1908 if (refs) {
1909 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1910 refs, repo);
1911 if (err == NULL) {
1912 *id = got_object_id_dup(
1913 got_object_tag_get_object_id(tag));
1914 if (*id == NULL)
1915 err = got_error_from_errno("got_object_id_dup");
1916 else if (label && asprintf(label, "refs/tags/%s",
1917 got_object_tag_get_name(tag)) == -1) {
1918 err = got_error_from_errno("asprintf");
1919 free(*id);
1920 *id = NULL;
1922 got_object_tag_close(tag);
1923 return err;
1924 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1925 err->code != GOT_ERR_NO_OBJ)
1926 return err;
1929 err = got_ref_open(&ref, repo, id_str, 0);
1930 if (err == NULL) {
1931 err = got_ref_resolve(id, repo, ref);
1932 if (err)
1933 goto done;
1934 if (label) {
1935 *label = strdup(got_ref_get_name(ref));
1936 if (*label == NULL) {
1937 err = got_error_from_errno("strdup");
1938 goto done;
1941 } else {
1942 if (err->code != GOT_ERR_NOT_REF &&
1943 err->code != GOT_ERR_BAD_REF_NAME)
1944 goto done;
1945 err = got_repo_match_object_id_prefix(id, id_str,
1946 obj_type, repo);
1947 if (err) {
1948 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1949 err = got_error_not_ref(id_str);
1950 goto done;
1952 if (label) {
1953 err = got_object_id_str(label, *id);
1954 if (*label == NULL) {
1955 err = got_error_from_errno("strdup");
1956 goto done;
1960 done:
1961 if (ref)
1962 got_ref_close(ref);
1963 return err;
1966 const struct got_error *
1967 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1968 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1970 const struct got_error *err = NULL;
1971 struct got_reflist_entry *re;
1972 struct got_object_id *tag_id;
1973 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1975 *tag = NULL;
1977 TAILQ_FOREACH(re, refs, entry) {
1978 const char *refname;
1979 refname = got_ref_get_name(re->ref);
1980 if (got_ref_is_symbolic(re->ref))
1981 continue;
1982 if (strncmp(refname, "refs/tags/", 10) != 0)
1983 continue;
1984 if (!name_is_absolute)
1985 refname += strlen("refs/tags/");
1986 if (strcmp(refname, name) != 0)
1987 continue;
1988 err = got_ref_resolve(&tag_id, repo, re->ref);
1989 if (err)
1990 break;
1991 err = got_object_open_as_tag(tag, repo, tag_id);
1992 free(tag_id);
1993 if (err)
1994 break;
1995 if (obj_type == GOT_OBJ_TYPE_ANY ||
1996 got_object_tag_get_object_type(*tag) == obj_type)
1997 break;
1998 got_object_tag_close(*tag);
1999 *tag = NULL;
2002 if (err == NULL && *tag == NULL)
2003 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2004 GOT_OBJ_LABEL_TAG, name);
2005 return err;
2008 static const struct got_error *
2009 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2010 const char *name, mode_t mode, struct got_object_id *blob_id)
2012 const struct got_error *err = NULL;
2014 *new_te = NULL;
2016 *new_te = calloc(1, sizeof(**new_te));
2017 if (*new_te == NULL)
2018 return got_error_from_errno("calloc");
2020 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2021 sizeof((*new_te)->name)) {
2022 err = got_error(GOT_ERR_NO_SPACE);
2023 goto done;
2026 if (S_ISLNK(mode)) {
2027 (*new_te)->mode = S_IFLNK;
2028 } else {
2029 (*new_te)->mode = S_IFREG;
2030 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2032 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2033 done:
2034 if (err && *new_te) {
2035 free(*new_te);
2036 *new_te = NULL;
2038 return err;
2041 static const struct got_error *
2042 import_file(struct got_tree_entry **new_te, struct dirent *de,
2043 const char *path, struct got_repository *repo)
2045 const struct got_error *err;
2046 struct got_object_id *blob_id = NULL;
2047 char *filepath;
2048 struct stat sb;
2050 if (asprintf(&filepath, "%s%s%s", path,
2051 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2052 return got_error_from_errno("asprintf");
2054 if (lstat(filepath, &sb) != 0) {
2055 err = got_error_from_errno2("lstat", path);
2056 goto done;
2059 err = got_object_blob_create(&blob_id, filepath, repo);
2060 if (err)
2061 goto done;
2063 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2064 blob_id);
2065 done:
2066 free(filepath);
2067 if (err)
2068 free(blob_id);
2069 return err;
2072 static const struct got_error *
2073 insert_tree_entry(struct got_tree_entry *new_te,
2074 struct got_pathlist_head *paths)
2076 const struct got_error *err = NULL;
2077 struct got_pathlist_entry *new_pe;
2079 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2080 if (err)
2081 return err;
2082 if (new_pe == NULL)
2083 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2084 return NULL;
2087 static const struct got_error *write_tree(struct got_object_id **,
2088 const char *, struct got_pathlist_head *, struct got_repository *,
2089 got_repo_import_cb progress_cb, void *progress_arg);
2091 static const struct got_error *
2092 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2093 const char *path, struct got_pathlist_head *ignores,
2094 struct got_repository *repo,
2095 got_repo_import_cb progress_cb, void *progress_arg)
2097 const struct got_error *err;
2098 struct got_object_id *id = NULL;
2099 char *subdirpath;
2101 if (asprintf(&subdirpath, "%s%s%s", path,
2102 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2103 return got_error_from_errno("asprintf");
2105 (*new_te) = calloc(1, sizeof(**new_te));
2106 if (*new_te == NULL)
2107 return got_error_from_errno("calloc");
2108 (*new_te)->mode = S_IFDIR;
2109 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2110 sizeof((*new_te)->name)) {
2111 err = got_error(GOT_ERR_NO_SPACE);
2112 goto done;
2114 err = write_tree(&id, subdirpath, ignores, repo,
2115 progress_cb, progress_arg);
2116 if (err)
2117 goto done;
2118 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2120 done:
2121 free(id);
2122 free(subdirpath);
2123 if (err) {
2124 free(*new_te);
2125 *new_te = NULL;
2127 return err;
2130 static const struct got_error *
2131 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2132 struct got_pathlist_head *ignores, struct got_repository *repo,
2133 got_repo_import_cb progress_cb, void *progress_arg)
2135 const struct got_error *err = NULL;
2136 DIR *dir;
2137 struct dirent *de;
2138 int nentries;
2139 struct got_tree_entry *new_te = NULL;
2140 struct got_pathlist_head paths;
2141 struct got_pathlist_entry *pe;
2143 *new_tree_id = NULL;
2145 TAILQ_INIT(&paths);
2147 dir = opendir(path_dir);
2148 if (dir == NULL) {
2149 err = got_error_from_errno2("opendir", path_dir);
2150 goto done;
2153 nentries = 0;
2154 while ((de = readdir(dir)) != NULL) {
2155 int ignore = 0;
2156 int type;
2158 if (strcmp(de->d_name, ".") == 0 ||
2159 strcmp(de->d_name, "..") == 0)
2160 continue;
2162 err = got_path_dirent_type(&type, path_dir, de);
2163 if (err)
2164 goto done;
2166 TAILQ_FOREACH(pe, ignores, entry) {
2167 if (type == DT_DIR && pe->path_len > 0 &&
2168 pe->path[pe->path_len - 1] == '/') {
2169 char stripped[PATH_MAX];
2171 if (strlcpy(stripped, pe->path,
2172 sizeof(stripped)) >= sizeof(stripped)) {
2173 err = got_error(GOT_ERR_NO_SPACE);
2174 goto done;
2176 got_path_strip_trailing_slashes(stripped);
2177 if (fnmatch(stripped, de->d_name, 0) == 0) {
2178 ignore = 1;
2179 break;
2181 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2182 ignore = 1;
2183 break;
2186 if (ignore)
2187 continue;
2189 if (type == DT_DIR) {
2190 err = import_subdir(&new_te, de, path_dir,
2191 ignores, repo, progress_cb, progress_arg);
2192 if (err) {
2193 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2194 goto done;
2195 err = NULL;
2196 continue;
2198 } else if (type == DT_REG || type == DT_LNK) {
2199 err = import_file(&new_te, de, path_dir, repo);
2200 if (err)
2201 goto done;
2202 } else
2203 continue;
2205 err = insert_tree_entry(new_te, &paths);
2206 if (err)
2207 goto done;
2208 nentries++;
2211 if (TAILQ_EMPTY(&paths)) {
2212 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2213 "cannot create tree without any entries");
2214 goto done;
2217 TAILQ_FOREACH(pe, &paths, entry) {
2218 struct got_tree_entry *te = pe->data;
2219 char *path;
2220 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2221 continue;
2222 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2223 err = got_error_from_errno("asprintf");
2224 goto done;
2226 err = (*progress_cb)(progress_arg, path);
2227 free(path);
2228 if (err)
2229 goto done;
2232 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2233 done:
2234 if (dir)
2235 closedir(dir);
2236 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2237 return err;
2240 const struct got_error *
2241 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2242 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2243 struct got_repository *repo, got_repo_import_cb progress_cb,
2244 void *progress_arg)
2246 const struct got_error *err;
2247 struct got_object_id *new_tree_id;
2249 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2250 progress_cb, progress_arg);
2251 if (err)
2252 return err;
2254 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2255 author, time(NULL), author, time(NULL), logmsg, repo);
2256 free(new_tree_id);
2257 return err;
2260 const struct got_error *
2261 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2262 struct got_repository *repo)
2264 const struct got_error *err = NULL;
2265 char *path_objects = NULL, *path = NULL;
2266 DIR *dir = NULL;
2267 struct got_object_id id;
2268 int i;
2270 *nobjects = 0;
2271 *ondisk_size = 0;
2273 path_objects = got_repo_get_path_objects(repo);
2274 if (path_objects == NULL)
2275 return got_error_from_errno("got_repo_get_path_objects");
2277 for (i = 0; i <= 0xff; i++) {
2278 struct dirent *dent;
2280 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2281 err = got_error_from_errno("asprintf");
2282 break;
2285 dir = opendir(path);
2286 if (dir == NULL) {
2287 if (errno == ENOENT) {
2288 err = NULL;
2289 continue;
2291 err = got_error_from_errno2("opendir", path);
2292 break;
2295 while ((dent = readdir(dir)) != NULL) {
2296 char *id_str;
2297 int fd;
2298 struct stat sb;
2300 if (strcmp(dent->d_name, ".") == 0 ||
2301 strcmp(dent->d_name, "..") == 0)
2302 continue;
2304 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2305 err = got_error_from_errno("asprintf");
2306 goto done;
2309 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2310 free(id_str);
2311 continue;
2313 free(id_str);
2315 err = got_object_open_loose_fd(&fd, &id, repo);
2316 if (err)
2317 goto done;
2319 if (fstat(fd, &sb) == -1) {
2320 err = got_error_from_errno("fstat");
2321 close(fd);
2322 goto done;
2324 (*nobjects)++;
2325 (*ondisk_size) += sb.st_size;
2327 if (close(fd) == -1) {
2328 err = got_error_from_errno("close");
2329 goto done;
2333 if (closedir(dir) != 0) {
2334 err = got_error_from_errno("closedir");
2335 goto done;
2337 dir = NULL;
2339 free(path);
2340 path = NULL;
2342 done:
2343 if (dir && closedir(dir) != 0 && err == NULL)
2344 err = got_error_from_errno("closedir");
2346 if (err) {
2347 *nobjects = 0;
2348 *ondisk_size = 0;
2350 free(path_objects);
2351 free(path);
2352 return err;
2355 const struct got_error *
2356 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2357 off_t *total_packsize, struct got_repository *repo)
2359 const struct got_error *err = NULL;
2360 DIR *packdir = NULL;
2361 struct dirent *dent;
2362 struct got_packidx *packidx = NULL;
2363 char *path_packidx;
2364 char *path_packfile;
2365 int packdir_fd;
2366 struct stat sb;
2368 *npackfiles = 0;
2369 *nobjects = 0;
2370 *total_packsize = 0;
2372 packdir_fd = openat(got_repo_get_fd(repo),
2373 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2374 if (packdir_fd == -1) {
2375 return got_error_from_errno_fmt("openat: %s/%s",
2376 got_repo_get_path_git_dir(repo),
2377 GOT_OBJECTS_PACK_DIR);
2380 packdir = fdopendir(packdir_fd);
2381 if (packdir == NULL) {
2382 err = got_error_from_errno("fdopendir");
2383 goto done;
2386 while ((dent = readdir(packdir)) != NULL) {
2387 if (!got_repo_is_packidx_filename(dent->d_name,
2388 strlen(dent->d_name)))
2389 continue;
2391 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2392 dent->d_name) == -1) {
2393 err = got_error_from_errno("asprintf");
2394 goto done;
2397 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2398 path_packidx, 0);
2399 free(path_packidx);
2400 if (err)
2401 goto done;
2403 if (fstat(packidx->fd, &sb) == -1)
2404 goto done;
2405 *total_packsize += sb.st_size;
2407 err = got_packidx_get_packfile_path(&path_packfile,
2408 packidx->path_packidx);
2409 if (err)
2410 goto done;
2412 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2413 0) == -1) {
2414 free(path_packfile);
2415 goto done;
2417 free(path_packfile);
2418 *total_packsize += sb.st_size;
2420 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2422 (*npackfiles)++;
2424 got_packidx_close(packidx);
2425 packidx = NULL;
2427 done:
2428 if (packidx)
2429 got_packidx_close(packidx);
2430 if (packdir && closedir(packdir) != 0 && err == NULL)
2431 err = got_error_from_errno("closedir");
2432 if (err) {
2433 *npackfiles = 0;
2434 *nobjects = 0;
2435 *total_packsize = 0;
2437 return err;
2440 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2441 got_packidx_bloom_filter_cmp);