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 <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_delta_cache.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_gotconfig.h"
69 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
72 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
74 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
75 got_packidx_bloom_filter_cmp);
78 got_repo_get_path(struct got_repository *repo)
84 got_repo_get_path_git_dir(struct got_repository *repo)
86 return repo->path_git_dir;
90 got_repo_get_fd(struct got_repository *repo)
92 return repo->gitdir_fd;
96 got_repo_get_gitconfig_author_name(struct got_repository *repo)
98 return repo->gitconfig_author_name;
102 got_repo_get_gitconfig_author_email(struct got_repository *repo)
104 return repo->gitconfig_author_email;
108 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
110 return repo->global_gitconfig_author_name;
114 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
116 return repo->global_gitconfig_author_email;
120 got_repo_get_gitconfig_owner(struct got_repository *repo)
122 return repo->gitconfig_owner;
126 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
127 struct got_repository *repo)
129 *extensions = repo->extensions;
130 *nextensions = repo->nextensions;
134 got_repo_is_bare(struct got_repository *repo)
136 return (strcmp(repo->path, repo->path_git_dir) == 0);
140 get_path_git_child(struct got_repository *repo, const char *basename)
144 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
152 got_repo_get_path_objects(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_DIR);
158 got_repo_get_path_objects_pack(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
164 got_repo_get_path_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_REFS_DIR);
170 got_repo_get_path_packed_refs(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
176 get_path_head(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_HEAD_FILE);
182 got_repo_get_path_gitconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GITCONFIG);
188 got_repo_get_path_gotconfig(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
193 const struct got_gotconfig *
194 got_repo_get_gotconfig(struct got_repository *repo)
196 return repo->gotconfig;
200 got_repo_get_gitconfig_remotes(int *nremotes,
201 const struct got_remote_repo **remotes, struct got_repository *repo)
203 *nremotes = repo->ngitconfig_remotes;
204 *remotes = repo->gitconfig_remotes;
208 is_git_repo(struct got_repository *repo)
210 const char *path_git = got_repo_get_path_git_dir(repo);
211 char *path_objects = got_repo_get_path_objects(repo);
212 char *path_refs = got_repo_get_path_refs(repo);
213 char *path_head = get_path_head(repo);
216 struct got_reference *head_ref;
218 if (lstat(path_git, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_objects, &sb) == -1)
225 if (!S_ISDIR(sb.st_mode))
228 if (lstat(path_refs, &sb) == -1)
230 if (!S_ISDIR(sb.st_mode))
233 if (lstat(path_head, &sb) == -1)
235 if (!S_ISREG(sb.st_mode))
238 /* Check if the HEAD reference can be opened. */
239 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
241 got_ref_close(head_ref);
252 static const struct got_error *
253 close_tempfiles(int *fds, size_t nfds)
255 const struct got_error *err = NULL;
258 for (i = 0; i < nfds; i++) {
261 if (close(fds[i]) == -1) {
262 err = got_error_from_errno("close");
270 static const struct got_error *
271 open_tempfiles(int **fds, size_t array_size, size_t nfds)
273 const struct got_error *err = NULL;
276 *fds = calloc(array_size, sizeof(**fds));
278 return got_error_from_errno("calloc");
280 for (i = 0; i < array_size; i++)
283 for (i = 0; i < nfds; i++) {
284 (*fds)[i] = got_opentempfd();
285 if ((*fds)[i] == -1) {
286 err = got_error_from_errno("got_opentempfd");
287 close_tempfiles(*fds, nfds);
296 static const struct got_error *
297 get_pack_cache_size(int *pack_cache_size)
301 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
302 return got_error_from_errno("getrlimit");
304 *pack_cache_size = GOT_PACK_CACHE_SIZE;
305 if (*pack_cache_size > rl.rlim_cur / 8)
306 *pack_cache_size = rl.rlim_cur / 8;
311 const struct got_error *
312 got_repo_pack_fds_open(int **pack_fds)
314 const struct got_error *err;
317 err = get_pack_cache_size(&nfds);
322 * We need one basefd and one accumfd per cached pack.
323 * Our constants should be set up in a way such that
324 * this error never triggers.
326 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
327 return got_error(GOT_ERR_NO_SPACE);
329 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
332 const struct got_error *
333 got_repo_pack_fds_close(int *pack_fds)
335 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
338 const struct got_error *
339 got_repo_temp_fds_open(int **temp_fds)
341 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
342 GOT_REPO_NUM_TEMPFILES);
346 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
350 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
351 repo->tempfiles[i] = temp_fds[i];
354 const struct got_error *
355 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
362 for (i = 0; i < nitems(repo->tempfiles); i++) {
363 if (repo->tempfile_use_mask & (1 << i))
365 if (repo->tempfiles[i] != -1) {
366 if (ftruncate(repo->tempfiles[i], 0L) == -1)
367 return got_error_from_errno("ftruncate");
368 *fd = repo->tempfiles[i];
370 repo->tempfile_use_mask |= (1 << i);
375 return got_error(GOT_ERR_REPO_TEMPFILE);
379 got_repo_temp_fds_put(int idx, struct got_repository *repo)
381 repo->tempfile_use_mask &= ~(1 << idx);
384 const struct got_error *
385 got_repo_temp_fds_close(int *temp_fds)
387 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
390 const struct got_error *
391 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
392 struct got_object *obj)
394 #ifndef GOT_NO_OBJ_CACHE
395 const struct got_error *err = NULL;
396 err = got_object_cache_add(&repo->objcache, id, obj);
398 if (err->code == GOT_ERR_OBJ_EXISTS ||
399 err->code == GOT_ERR_OBJ_TOO_LARGE)
409 got_repo_get_cached_object(struct got_repository *repo,
410 struct got_object_id *id)
412 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
415 const struct got_error *
416 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
417 struct got_tree_object *tree)
419 #ifndef GOT_NO_OBJ_CACHE
420 const struct got_error *err = NULL;
421 err = got_object_cache_add(&repo->treecache, id, tree);
423 if (err->code == GOT_ERR_OBJ_EXISTS ||
424 err->code == GOT_ERR_OBJ_TOO_LARGE)
433 struct got_tree_object *
434 got_repo_get_cached_tree(struct got_repository *repo,
435 struct got_object_id *id)
437 return (struct got_tree_object *)got_object_cache_get(
438 &repo->treecache, id);
441 const struct got_error *
442 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
443 struct got_commit_object *commit)
445 #ifndef GOT_NO_OBJ_CACHE
446 const struct got_error *err = NULL;
447 err = got_object_cache_add(&repo->commitcache, id, commit);
449 if (err->code == GOT_ERR_OBJ_EXISTS ||
450 err->code == GOT_ERR_OBJ_TOO_LARGE)
459 struct got_commit_object *
460 got_repo_get_cached_commit(struct got_repository *repo,
461 struct got_object_id *id)
463 return (struct got_commit_object *)got_object_cache_get(
464 &repo->commitcache, id);
467 const struct got_error *
468 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
469 struct got_tag_object *tag)
471 #ifndef GOT_NO_OBJ_CACHE
472 const struct got_error *err = NULL;
473 err = got_object_cache_add(&repo->tagcache, id, tag);
475 if (err->code == GOT_ERR_OBJ_EXISTS ||
476 err->code == GOT_ERR_OBJ_TOO_LARGE)
485 struct got_tag_object *
486 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
488 return (struct got_tag_object *)got_object_cache_get(
489 &repo->tagcache, id);
492 const struct got_error *
493 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
494 struct got_raw_object *raw)
496 #ifndef GOT_NO_OBJ_CACHE
497 const struct got_error *err = NULL;
498 err = got_object_cache_add(&repo->rawcache, id, raw);
500 if (err->code == GOT_ERR_OBJ_EXISTS ||
501 err->code == GOT_ERR_OBJ_TOO_LARGE)
511 struct got_raw_object *
512 got_repo_get_cached_raw_object(struct got_repository *repo,
513 struct got_object_id *id)
515 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
519 static const struct got_error *
520 open_repo(struct got_repository *repo, const char *path)
522 const struct got_error *err = NULL;
524 repo->gitdir_fd = -1;
526 /* bare git repository? */
527 repo->path_git_dir = strdup(path);
528 if (repo->path_git_dir == NULL)
529 return got_error_from_errno("strdup");
530 if (is_git_repo(repo)) {
531 repo->path = strdup(repo->path_git_dir);
532 if (repo->path == NULL) {
533 err = got_error_from_errno("strdup");
536 repo->gitdir_fd = open(repo->path_git_dir,
537 O_DIRECTORY | O_CLOEXEC);
538 if (repo->gitdir_fd == -1) {
539 err = got_error_from_errno2("open",
546 /* git repository with working tree? */
547 free(repo->path_git_dir);
548 repo->path_git_dir = NULL;
549 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
550 err = got_error_from_errno("asprintf");
553 if (is_git_repo(repo)) {
554 repo->path = strdup(path);
555 if (repo->path == NULL) {
556 err = got_error_from_errno("strdup");
559 repo->gitdir_fd = open(repo->path_git_dir,
560 O_DIRECTORY | O_CLOEXEC);
561 if (repo->gitdir_fd == -1) {
562 err = got_error_from_errno2("open",
569 err = got_error(GOT_ERR_NOT_GIT_REPO);
574 free(repo->path_git_dir);
575 repo->path_git_dir = NULL;
576 if (repo->gitdir_fd != -1)
577 close(repo->gitdir_fd);
578 repo->gitdir_fd = -1;
584 static const struct got_error *
585 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
587 const struct got_error *err = NULL;
588 char *repo_gitconfig_path = NULL;
590 if (global_gitconfig_path) {
591 /* Read settings from ~/.gitconfig. */
592 int dummy_repo_version;
593 err = got_repo_read_gitconfig(&dummy_repo_version,
594 &repo->global_gitconfig_author_name,
595 &repo->global_gitconfig_author_email,
596 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
601 /* Read repository's .git/config file. */
602 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
603 if (repo_gitconfig_path == NULL)
604 return got_error_from_errno("got_repo_get_path_gitconfig");
606 err = got_repo_read_gitconfig(
607 &repo->gitconfig_repository_format_version,
608 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
609 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
610 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
611 repo_gitconfig_path);
615 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
618 for (i = 0; i < repo->ngitconfig_remotes; i++) {
619 got_repo_free_remote_repo_data(
620 &repo->gitconfig_remotes[i]);
622 free(repo->gitconfig_remotes);
623 repo->gitconfig_remotes = NULL;
624 repo->ngitconfig_remotes = 0;
626 free(repo->gitconfig_author_name);
627 repo->gitconfig_author_name = NULL;
628 free(repo->gitconfig_author_email);
629 repo->gitconfig_author_email = NULL;
631 free(repo->global_gitconfig_author_name);
632 repo->global_gitconfig_author_name = NULL;
633 free(repo->global_gitconfig_author_email);
634 repo->global_gitconfig_author_email = NULL;
638 free(repo_gitconfig_path);
642 static const struct got_error *
643 read_gotconfig(struct got_repository *repo)
645 const struct got_error *err = NULL;
646 char *gotconfig_path;
648 gotconfig_path = got_repo_get_path_gotconfig(repo);
649 if (gotconfig_path == NULL)
650 return got_error_from_errno("got_repo_get_path_gotconfig");
652 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
653 free(gotconfig_path);
657 /* Supported repository format extensions. */
658 static const char *const repo_extensions[] = {
659 "noop", /* Got supports repository format version 1. */
660 "preciousObjects", /* Supported by gotadmin cleanup. */
661 "worktreeConfig", /* Got does not care about Git work trees. */
664 const struct got_error *
665 got_repo_open(struct got_repository **repop, const char *path,
666 const char *global_gitconfig_path, int *pack_fds)
668 struct got_repository *repo = NULL;
669 const struct got_error *err = NULL;
670 char *repo_path = NULL;
675 repo = calloc(1, sizeof(*repo));
677 return got_error_from_errno("calloc");
679 RB_INIT(&repo->packidx_bloom_filters);
680 TAILQ_INIT(&repo->packidx_paths);
682 for (i = 0; i < nitems(repo->privsep_children); i++) {
683 memset(&repo->privsep_children[i], 0,
684 sizeof(repo->privsep_children[0]));
685 repo->privsep_children[i].imsg_fd = -1;
688 err = got_object_cache_init(&repo->objcache,
689 GOT_OBJECT_CACHE_TYPE_OBJ);
692 err = got_object_cache_init(&repo->treecache,
693 GOT_OBJECT_CACHE_TYPE_TREE);
696 err = got_object_cache_init(&repo->commitcache,
697 GOT_OBJECT_CACHE_TYPE_COMMIT);
700 err = got_object_cache_init(&repo->tagcache,
701 GOT_OBJECT_CACHE_TYPE_TAG);
704 err = got_object_cache_init(&repo->rawcache,
705 GOT_OBJECT_CACHE_TYPE_RAW);
709 err = get_pack_cache_size(&repo->pack_cache_size);
712 for (i = 0; i < nitems(repo->packs); i++) {
713 if (pack_fds != NULL && i < repo->pack_cache_size) {
714 repo->packs[i].basefd = pack_fds[j++];
715 repo->packs[i].accumfd = pack_fds[j++];
717 repo->packs[i].basefd = -1;
718 repo->packs[i].accumfd = -1;
721 for (i = 0; i < nitems(repo->tempfiles); i++)
722 repo->tempfiles[i] = -1;
723 repo->pinned_pack = -1;
724 repo->pinned_packidx = -1;
725 repo->pinned_pid = 0;
727 repo_path = realpath(path, NULL);
728 if (repo_path == NULL) {
729 err = got_error_from_errno2("realpath", path);
736 err = open_repo(repo, repo_path);
739 if (err->code != GOT_ERR_NOT_GIT_REPO)
741 if (repo_path[0] == '/' && repo_path[1] == '\0') {
742 err = got_error(GOT_ERR_NOT_GIT_REPO);
745 err = got_path_dirname(&parent_path, repo_path);
749 repo_path = parent_path;
752 err = read_gotconfig(repo);
756 err = read_gitconfig(repo, global_gitconfig_path);
759 if (repo->gitconfig_repository_format_version != 0) {
760 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
763 for (i = 0; i < repo->nextensions; i++) {
764 char *ext = repo->extensions[i];
765 int j, supported = 0;
766 for (j = 0; j < nitems(repo_extensions); j++) {
767 if (strcmp(ext, repo_extensions[j]) == 0) {
773 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
778 err = got_repo_list_packidx(&repo->packidx_paths, repo);
781 got_repo_close(repo);
788 const struct got_error *
789 got_repo_close(struct got_repository *repo)
791 const struct got_error *err = NULL, *child_err;
792 struct got_packidx_bloom_filter *bf;
795 for (i = 0; i < repo->pack_cache_size; i++) {
796 if (repo->packidx_cache[i] == NULL)
798 got_packidx_close(repo->packidx_cache[i]);
801 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
802 &repo->packidx_bloom_filters))) {
803 RB_REMOVE(got_packidx_bloom_filter_tree,
804 &repo->packidx_bloom_filters, bf);
805 bloom_free(bf->bloom);
810 for (i = 0; i < repo->pack_cache_size; i++)
811 if (repo->packs[i].path_packfile)
812 if (repo->packs[i].path_packfile)
813 got_pack_close(&repo->packs[i]);
816 free(repo->path_git_dir);
818 got_object_cache_close(&repo->objcache);
819 got_object_cache_close(&repo->treecache);
820 got_object_cache_close(&repo->commitcache);
821 got_object_cache_close(&repo->tagcache);
822 got_object_cache_close(&repo->rawcache);
824 for (i = 0; i < nitems(repo->privsep_children); i++) {
825 if (repo->privsep_children[i].imsg_fd == -1)
827 imsg_clear(repo->privsep_children[i].ibuf);
828 free(repo->privsep_children[i].ibuf);
829 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
830 child_err = got_privsep_wait_for_child(
831 repo->privsep_children[i].pid);
832 if (child_err && err == NULL)
834 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
836 err = got_error_from_errno("close");
839 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
841 err = got_error_from_errno("close");
844 got_gotconfig_free(repo->gotconfig);
845 free(repo->gitconfig_author_name);
846 free(repo->gitconfig_author_email);
847 for (i = 0; i < repo->ngitconfig_remotes; i++)
848 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
849 free(repo->gitconfig_remotes);
850 for (i = 0; i < repo->nextensions; i++)
851 free(repo->extensions[i]);
852 free(repo->extensions);
854 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
861 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
867 free(repo->fetch_url);
868 repo->fetch_url = NULL;
869 free(repo->send_url);
870 repo->send_url = NULL;
871 for (i = 0; i < repo->nfetch_branches; i++)
872 free(repo->fetch_branches[i]);
873 free(repo->fetch_branches);
874 repo->fetch_branches = NULL;
875 repo->nfetch_branches = 0;
876 for (i = 0; i < repo->nsend_branches; i++)
877 free(repo->send_branches[i]);
878 free(repo->send_branches);
879 repo->send_branches = NULL;
880 repo->nsend_branches = 0;
883 const struct got_error *
884 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
885 const char *input_path)
887 const struct got_error *err = NULL;
888 const char *repo_abspath = NULL;
890 char *canonpath, *path = NULL;
892 *in_repo_path = NULL;
894 canonpath = strdup(input_path);
895 if (canonpath == NULL) {
896 err = got_error_from_errno("strdup");
899 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
903 repo_abspath = got_repo_get_path(repo);
905 if (canonpath[0] == '\0') {
906 path = strdup(canonpath);
908 err = got_error_from_errno("strdup");
912 path = realpath(canonpath, NULL);
914 if (errno != ENOENT) {
915 err = got_error_from_errno2("realpath",
920 * Path is not on disk.
921 * Assume it is already relative to repository root.
923 path = strdup(canonpath);
925 err = got_error_from_errno("strdup");
930 repolen = strlen(repo_abspath);
934 if (strcmp(path, repo_abspath) == 0) {
938 err = got_error_from_errno("strdup");
941 } else if (len > repolen &&
942 got_path_is_child(path, repo_abspath, repolen)) {
943 /* Matched an on-disk path inside repository. */
944 if (got_repo_is_bare(repo)) {
946 * Matched an on-disk path inside repository
947 * database. Treat input as repository-relative.
954 /* Strip common prefix with repository path. */
955 err = got_path_skip_common_ancestor(&child,
964 * Matched unrelated on-disk path.
965 * Treat input as repository-relative.
973 /* Make in-repository path absolute */
974 if (path[0] != '/') {
976 if (asprintf(&abspath, "/%s", path) == -1) {
977 err = got_error_from_errno("asprintf");
989 *in_repo_path = path;
993 static const struct got_error *
994 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
995 const char *path_packidx)
997 const struct got_error *err = NULL;
1000 for (i = 0; i < repo->pack_cache_size; i++) {
1001 if (repo->packidx_cache[i] == NULL)
1003 if (strcmp(repo->packidx_cache[i]->path_packidx,
1004 path_packidx) == 0) {
1005 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1008 if (i == repo->pack_cache_size) {
1011 } while (i > 0 && repo->pinned_packidx >= 0 &&
1012 i == repo->pinned_packidx);
1013 err = got_packidx_close(repo->packidx_cache[i]);
1018 repo->packidx_cache[i] = packidx;
1024 got_repo_is_packidx_filename(const char *name, size_t len)
1026 if (len != GOT_PACKIDX_NAMELEN)
1029 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1032 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1033 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1039 static struct got_packidx_bloom_filter *
1040 get_packidx_bloom_filter(struct got_repository *repo,
1041 const char *path, size_t path_len)
1043 struct got_packidx_bloom_filter key;
1045 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1046 return NULL; /* XXX */
1047 key.path_len = path_len;
1049 return RB_FIND(got_packidx_bloom_filter_tree,
1050 &repo->packidx_bloom_filters, &key);
1054 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1055 const char *path_packidx, struct got_object_id *id)
1057 struct got_packidx_bloom_filter *bf;
1059 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1061 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1063 /* No bloom filter means this pack index must be searched. */
1067 static const struct got_error *
1068 add_packidx_bloom_filter(struct got_repository *repo,
1069 struct got_packidx *packidx, const char *path_packidx)
1071 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1072 struct got_packidx_bloom_filter *bf;
1076 * Don't use bloom filters for very large pack index files.
1077 * Large pack files will contain a relatively large fraction
1078 * of our objects so we will likely need to visit them anyway.
1079 * The more objects a pack file contains the higher the probability
1080 * of a false-positive match from the bloom filter. And reading
1081 * all object IDs from a large pack index file can be expensive.
1083 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1086 /* Do we already have a filter for this pack index? */
1087 if (get_packidx_bloom_filter(repo, path_packidx,
1088 strlen(path_packidx)) != NULL)
1091 bf = calloc(1, sizeof(*bf));
1093 return got_error_from_errno("calloc");
1094 bf->bloom = calloc(1, sizeof(*bf->bloom));
1095 if (bf->bloom == NULL) {
1097 return got_error_from_errno("calloc");
1100 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1101 if (len >= sizeof(bf->path)) {
1104 return got_error(GOT_ERR_NO_SPACE);
1108 /* Minimum size supported by our bloom filter is 1000 entries. */
1109 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1110 for (i = 0; i < nobjects; i++) {
1111 struct got_packidx_object_id *id;
1112 id = &packidx->hdr.sorted_ids[i];
1113 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1116 RB_INSERT(got_packidx_bloom_filter_tree,
1117 &repo->packidx_bloom_filters, bf);
1122 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1124 struct got_pathlist_entry *pe;
1126 while (!TAILQ_EMPTY(packidx_paths)) {
1127 pe = TAILQ_FIRST(packidx_paths);
1128 TAILQ_REMOVE(packidx_paths, pe, entry);
1129 free((char *)pe->path);
1134 static const struct got_error *
1135 refresh_packidx_paths(struct got_repository *repo)
1137 const struct got_error *err = NULL;
1138 char *objects_pack_dir = NULL;
1141 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1142 if (objects_pack_dir == NULL)
1143 return got_error_from_errno("got_repo_get_path_objects_pack");
1145 if (stat(objects_pack_dir, &sb) == -1) {
1146 if (errno != ENOENT) {
1147 err = got_error_from_errno2("stat", objects_pack_dir);
1150 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1151 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1152 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1153 purge_packidx_paths(&repo->packidx_paths);
1154 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1159 free(objects_pack_dir);
1163 const struct got_error *
1164 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1165 struct got_repository *repo, struct got_object_id *id)
1167 const struct got_error *err;
1168 struct got_pathlist_entry *pe;
1171 /* Search pack index cache. */
1172 for (i = 0; i < repo->pack_cache_size; i++) {
1173 if (repo->packidx_cache[i] == NULL)
1175 if (!got_repo_check_packidx_bloom_filter(repo,
1176 repo->packidx_cache[i]->path_packidx, id))
1177 continue; /* object will not be found in this index */
1178 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1180 *packidx = repo->packidx_cache[i];
1182 * Move this cache entry to the front. Repeatedly
1183 * searching a wrong pack index can be expensive.
1186 memmove(&repo->packidx_cache[1],
1187 &repo->packidx_cache[0],
1188 i * sizeof(repo->packidx_cache[0]));
1189 repo->packidx_cache[0] = *packidx;
1190 if (repo->pinned_packidx >= 0 &&
1191 repo->pinned_packidx < i)
1192 repo->pinned_packidx++;
1193 else if (repo->pinned_packidx == i)
1194 repo->pinned_packidx = 0;
1199 /* No luck. Search the filesystem. */
1201 err = refresh_packidx_paths(repo);
1205 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1206 const char *path_packidx = pe->path;
1209 if (!got_repo_check_packidx_bloom_filter(repo,
1211 continue; /* object will not be found in this index */
1213 for (i = 0; i < repo->pack_cache_size; i++) {
1214 if (repo->packidx_cache[i] == NULL)
1216 if (strcmp(repo->packidx_cache[i]->path_packidx,
1217 path_packidx) == 0) {
1223 continue; /* already searched */
1225 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1230 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1234 err = cache_packidx(repo, *packidx, path_packidx);
1238 *idx = got_packidx_get_object_idx(*packidx, id);
1240 err = NULL; /* found the object */
1245 err = got_error_no_obj(id);
1250 const struct got_error *
1251 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1252 struct got_repository *repo)
1254 const struct got_error *err = NULL;
1255 DIR *packdir = NULL;
1256 struct dirent *dent;
1257 char *path_packidx = NULL;
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1263 if (packdir_fd == -1) {
1264 return got_error_from_errno_fmt("openat: %s/%s",
1265 got_repo_get_path_git_dir(repo),
1266 GOT_OBJECTS_PACK_DIR);
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1275 if (fstat(packdir_fd, &sb) == -1) {
1276 err = got_error_from_errno("fstat");
1279 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1280 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1282 while ((dent = readdir(packdir)) != NULL) {
1283 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1286 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1287 dent->d_name) == -1) {
1288 err = got_error_from_errno("asprintf");
1289 path_packidx = NULL;
1293 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1300 if (packdir && closedir(packdir) != 0 && err == NULL)
1301 err = got_error_from_errno("closedir");
1305 const struct got_error *
1306 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1307 struct got_repository *repo)
1309 const struct got_error *err;
1314 /* Search pack index cache. */
1315 for (i = 0; i < repo->pack_cache_size; i++) {
1316 if (repo->packidx_cache[i] == NULL)
1318 if (strcmp(repo->packidx_cache[i]->path_packidx,
1319 path_packidx) == 0) {
1320 *packidx = repo->packidx_cache[i];
1324 /* No luck. Search the filesystem. */
1326 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1331 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1335 err = cache_packidx(repo, *packidx, path_packidx);
1338 got_packidx_close(*packidx);
1344 static const struct got_error *
1345 read_packfile_hdr(int fd, struct got_packidx *packidx)
1347 const struct got_error *err = NULL;
1348 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1349 struct got_packfile_hdr hdr;
1352 n = read(fd, &hdr, sizeof(hdr));
1354 return got_error_from_errno("read");
1355 if (n != sizeof(hdr))
1356 return got_error(GOT_ERR_BAD_PACKFILE);
1358 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1359 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1360 be32toh(hdr.nobjects) != totobj)
1361 err = got_error(GOT_ERR_BAD_PACKFILE);
1366 static const struct got_error *
1367 open_packfile(int *fd, struct got_repository *repo,
1368 const char *relpath, struct got_packidx *packidx)
1370 const struct got_error *err = NULL;
1372 *fd = openat(got_repo_get_fd(repo), relpath,
1373 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1375 return got_error_from_errno_fmt("openat: %s/%s",
1376 got_repo_get_path_git_dir(repo), relpath);
1379 err = read_packfile_hdr(*fd, packidx);
1389 const struct got_error *
1390 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1391 const char *path_packfile, struct got_packidx *packidx)
1393 const struct got_error *err = NULL;
1394 struct got_pack *pack = NULL;
1401 for (i = 0; i < repo->pack_cache_size; i++) {
1402 pack = &repo->packs[i];
1403 if (pack->path_packfile == NULL)
1405 if (strcmp(pack->path_packfile, path_packfile) == 0)
1406 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1409 if (i == repo->pack_cache_size) {
1410 struct got_pack tmp;
1413 } while (i > 0 && repo->pinned_pack >= 0 &&
1414 i == repo->pinned_pack);
1415 err = got_pack_close(&repo->packs[i]);
1418 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1419 return got_error_from_errno("ftruncate");
1420 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1421 return got_error_from_errno("ftruncate");
1422 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1423 memcpy(&repo->packs[i], &repo->packs[0],
1424 sizeof(repo->packs[i]));
1425 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1426 if (repo->pinned_pack == 0)
1427 repo->pinned_pack = i;
1428 else if (repo->pinned_pack == i)
1429 repo->pinned_pack = 0;
1433 pack = &repo->packs[i];
1435 pack->path_packfile = strdup(path_packfile);
1436 if (pack->path_packfile == NULL) {
1437 err = got_error_from_errno("strdup");
1441 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1445 if (fstat(pack->fd, &sb) != 0) {
1446 err = got_error_from_errno("fstat");
1449 pack->filesize = sb.st_size;
1451 pack->privsep_child = NULL;
1453 err = got_delta_cache_alloc(&pack->delta_cache);
1457 #ifndef GOT_PACK_NO_MMAP
1458 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1459 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1461 if (pack->map == MAP_FAILED) {
1462 if (errno != ENOMEM) {
1463 err = got_error_from_errno("mmap");
1466 pack->map = NULL; /* fall back to read(2) */
1473 got_pack_close(pack);
1480 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1482 struct got_pack *pack = NULL;
1485 for (i = 0; i < repo->pack_cache_size; i++) {
1486 pack = &repo->packs[i];
1487 if (pack->path_packfile == NULL)
1489 if (strcmp(pack->path_packfile, path_packfile) == 0)
1496 const struct got_error *
1497 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1498 struct got_pack *pack)
1501 int pinned_pack = -1, pinned_packidx = -1;
1503 for (i = 0; i < repo->pack_cache_size; i++) {
1504 if (repo->packidx_cache[i] &&
1505 strcmp(repo->packidx_cache[i]->path_packidx,
1506 packidx->path_packidx) == 0)
1508 if (repo->packs[i].path_packfile &&
1509 strcmp(repo->packs[i].path_packfile,
1510 pack->path_packfile) == 0)
1514 if (pinned_packidx == -1 || pinned_pack == -1)
1515 return got_error(GOT_ERR_PIN_PACK);
1517 repo->pinned_pack = pinned_pack;
1518 repo->pinned_packidx = pinned_packidx;
1519 if (repo->packs[pinned_pack].privsep_child)
1520 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1525 got_repo_get_pinned_pack(struct got_repository *repo)
1527 if (repo->pinned_pack >= 0 &&
1528 repo->pinned_pack < repo->pack_cache_size)
1529 return &repo->packs[repo->pinned_pack];
1535 got_repo_unpin_pack(struct got_repository *repo)
1537 repo->pinned_packidx = -1;
1538 repo->pinned_pack = -1;
1539 repo->pinned_pid = 0;
1542 const struct got_error *
1543 got_repo_init(const char *repo_path, const char *head_name)
1545 const struct got_error *err = NULL;
1546 const char *dirnames[] = {
1548 GOT_OBJECTS_PACK_DIR,
1551 const char *description_str = "Unnamed repository; "
1552 "edit this file 'description' to name the repository.";
1553 const char *headref = "ref: refs/heads/";
1554 const char *gitconfig_str = "[core]\n"
1555 "\trepositoryformatversion = 0\n"
1556 "\tfilemode = true\n"
1558 char *headref_str, *path;
1561 if (!got_path_dir_is_empty(repo_path))
1562 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1564 for (i = 0; i < nitems(dirnames); i++) {
1565 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1566 return got_error_from_errno("asprintf");
1568 err = got_path_mkdir(path);
1574 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1575 return got_error_from_errno("asprintf");
1576 err = got_path_create_file(path, description_str);
1581 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1582 return got_error_from_errno("asprintf");
1583 if (asprintf(&headref_str, "%s%s", headref,
1584 head_name ? head_name : "main") == -1) {
1586 return got_error_from_errno("asprintf");
1588 err = got_path_create_file(path, headref_str);
1594 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, gitconfig_str);
1604 static const struct got_error *
1605 match_packed_object(struct got_object_id **unique_id,
1606 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1608 const struct got_error *err = NULL;
1609 struct got_object_id_queue matched_ids;
1610 struct got_pathlist_entry *pe;
1612 STAILQ_INIT(&matched_ids);
1614 err = refresh_packidx_paths(repo);
1618 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1619 const char *path_packidx = pe->path;
1620 struct got_packidx *packidx;
1621 struct got_object_qid *qid;
1623 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1628 err = got_packidx_match_id_str_prefix(&matched_ids,
1629 packidx, id_str_prefix);
1631 got_packidx_close(packidx);
1634 err = got_packidx_close(packidx);
1638 STAILQ_FOREACH(qid, &matched_ids, entry) {
1639 if (obj_type != GOT_OBJ_TYPE_ANY) {
1641 err = got_object_get_type(&matched_type, repo,
1645 if (matched_type != obj_type)
1648 if (*unique_id == NULL) {
1649 *unique_id = got_object_id_dup(&qid->id);
1650 if (*unique_id == NULL) {
1651 err = got_error_from_errno("malloc");
1655 if (got_object_id_cmp(*unique_id,
1657 continue; /* packed multiple times */
1658 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1664 got_object_id_queue_free(&matched_ids);
1672 static const struct got_error *
1673 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1674 const char *object_dir, const char *id_str_prefix, int obj_type,
1675 struct got_repository *repo)
1677 const struct got_error *err = NULL;
1678 char *path, *id_str = NULL;
1680 struct dirent *dent;
1681 struct got_object_id id;
1683 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1684 err = got_error_from_errno("asprintf");
1688 dir = opendir(path);
1690 if (errno == ENOENT) {
1694 err = got_error_from_errno2("opendir", path);
1697 while ((dent = readdir(dir)) != NULL) {
1703 if (strcmp(dent->d_name, ".") == 0 ||
1704 strcmp(dent->d_name, "..") == 0)
1707 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1708 err = got_error_from_errno("asprintf");
1712 if (!got_parse_sha1_digest(id.sha1, id_str))
1716 * Directory entries do not necessarily appear in
1717 * sorted order, so we must iterate over all of them.
1719 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1723 if (*unique_id == NULL) {
1724 if (obj_type != GOT_OBJ_TYPE_ANY) {
1726 err = got_object_get_type(&matched_type, repo,
1730 if (matched_type != obj_type)
1733 *unique_id = got_object_id_dup(&id);
1734 if (*unique_id == NULL) {
1735 err = got_error_from_errno("got_object_id_dup");
1739 if (got_object_id_cmp(*unique_id, &id) == 0)
1740 continue; /* both packed and loose */
1741 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1746 if (dir && closedir(dir) != 0 && err == NULL)
1747 err = got_error_from_errno("closedir");
1757 const struct got_error *
1758 got_repo_match_object_id_prefix(struct got_object_id **id,
1759 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1761 const struct got_error *err = NULL;
1762 char *path_objects = NULL, *object_dir = NULL;
1768 path_objects = got_repo_get_path_objects(repo);
1770 len = strlen(id_str_prefix);
1771 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1772 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 for (i = 0; i < len; i++) {
1777 if (isxdigit((unsigned char)id_str_prefix[i]))
1779 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1784 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1787 object_dir = strndup(id_str_prefix, 2);
1788 if (object_dir == NULL) {
1789 err = got_error_from_errno("strdup");
1792 err = match_loose_object(id, path_objects, object_dir,
1793 id_str_prefix, obj_type, repo);
1794 } else if (len == 1) {
1796 for (i = 0; i < 0xf; i++) {
1797 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1799 err = got_error_from_errno("asprintf");
1802 err = match_packed_object(id, repo, object_dir,
1806 err = match_loose_object(id, path_objects, object_dir,
1807 id_str_prefix, obj_type, repo);
1812 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1821 } else if (*id == NULL) {
1823 case GOT_OBJ_TYPE_BLOB:
1824 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1825 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1827 case GOT_OBJ_TYPE_TREE:
1828 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1829 GOT_OBJ_LABEL_TREE, id_str_prefix);
1831 case GOT_OBJ_TYPE_COMMIT:
1832 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1833 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1835 case GOT_OBJ_TYPE_TAG:
1836 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1837 GOT_OBJ_LABEL_TAG, id_str_prefix);
1840 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1848 const struct got_error *
1849 got_repo_match_object_id(struct got_object_id **id, char **label,
1850 const char *id_str, int obj_type, struct got_reflist_head *refs,
1851 struct got_repository *repo)
1853 const struct got_error *err;
1854 struct got_tag_object *tag;
1855 struct got_reference *ref = NULL;
1862 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1865 *id = got_object_id_dup(
1866 got_object_tag_get_object_id(tag));
1868 err = got_error_from_errno("got_object_id_dup");
1869 else if (label && asprintf(label, "refs/tags/%s",
1870 got_object_tag_get_name(tag)) == -1) {
1871 err = got_error_from_errno("asprintf");
1875 got_object_tag_close(tag);
1877 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1878 err->code != GOT_ERR_NO_OBJ)
1882 err = got_ref_open(&ref, repo, id_str, 0);
1884 err = got_ref_resolve(id, repo, ref);
1888 *label = strdup(got_ref_get_name(ref));
1889 if (*label == NULL) {
1890 err = got_error_from_errno("strdup");
1895 if (err->code != GOT_ERR_NOT_REF &&
1896 err->code != GOT_ERR_BAD_REF_NAME)
1898 err = got_repo_match_object_id_prefix(id, id_str,
1901 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1902 err = got_error_not_ref(id_str);
1906 err = got_object_id_str(label, *id);
1907 if (*label == NULL) {
1908 err = got_error_from_errno("strdup");
1919 const struct got_error *
1920 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1921 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1923 const struct got_error *err = NULL;
1924 struct got_reflist_entry *re;
1925 struct got_object_id *tag_id;
1926 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1930 TAILQ_FOREACH(re, refs, entry) {
1931 const char *refname;
1932 refname = got_ref_get_name(re->ref);
1933 if (got_ref_is_symbolic(re->ref))
1935 if (strncmp(refname, "refs/tags/", 10) != 0)
1937 if (!name_is_absolute)
1938 refname += strlen("refs/tags/");
1939 if (strcmp(refname, name) != 0)
1941 err = got_ref_resolve(&tag_id, repo, re->ref);
1944 err = got_object_open_as_tag(tag, repo, tag_id);
1948 if (obj_type == GOT_OBJ_TYPE_ANY ||
1949 got_object_tag_get_object_type(*tag) == obj_type)
1951 got_object_tag_close(*tag);
1955 if (err == NULL && *tag == NULL)
1956 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1957 GOT_OBJ_LABEL_TAG, name);
1961 static const struct got_error *
1962 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1963 const char *name, mode_t mode, struct got_object_id *blob_id)
1965 const struct got_error *err = NULL;
1969 *new_te = calloc(1, sizeof(**new_te));
1970 if (*new_te == NULL)
1971 return got_error_from_errno("calloc");
1973 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1974 sizeof((*new_te)->name)) {
1975 err = got_error(GOT_ERR_NO_SPACE);
1979 if (S_ISLNK(mode)) {
1980 (*new_te)->mode = S_IFLNK;
1982 (*new_te)->mode = S_IFREG;
1983 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1985 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1987 if (err && *new_te) {
1994 static const struct got_error *
1995 import_file(struct got_tree_entry **new_te, struct dirent *de,
1996 const char *path, struct got_repository *repo)
1998 const struct got_error *err;
1999 struct got_object_id *blob_id = NULL;
2003 if (asprintf(&filepath, "%s%s%s", path,
2004 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (lstat(filepath, &sb) != 0) {
2008 err = got_error_from_errno2("lstat", path);
2012 err = got_object_blob_create(&blob_id, filepath, repo);
2016 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2025 static const struct got_error *
2026 insert_tree_entry(struct got_tree_entry *new_te,
2027 struct got_pathlist_head *paths)
2029 const struct got_error *err = NULL;
2030 struct got_pathlist_entry *new_pe;
2032 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2036 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2040 static const struct got_error *write_tree(struct got_object_id **,
2041 const char *, struct got_pathlist_head *, struct got_repository *,
2042 got_repo_import_cb progress_cb, void *progress_arg);
2044 static const struct got_error *
2045 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2046 const char *path, struct got_pathlist_head *ignores,
2047 struct got_repository *repo,
2048 got_repo_import_cb progress_cb, void *progress_arg)
2050 const struct got_error *err;
2051 struct got_object_id *id = NULL;
2054 if (asprintf(&subdirpath, "%s%s%s", path,
2055 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2056 return got_error_from_errno("asprintf");
2058 (*new_te) = calloc(1, sizeof(**new_te));
2059 if (*new_te == NULL)
2060 return got_error_from_errno("calloc");
2061 (*new_te)->mode = S_IFDIR;
2062 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2063 sizeof((*new_te)->name)) {
2064 err = got_error(GOT_ERR_NO_SPACE);
2067 err = write_tree(&id, subdirpath, ignores, repo,
2068 progress_cb, progress_arg);
2071 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2083 static const struct got_error *
2084 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2085 struct got_pathlist_head *ignores, struct got_repository *repo,
2086 got_repo_import_cb progress_cb, void *progress_arg)
2088 const struct got_error *err = NULL;
2092 struct got_tree_entry *new_te = NULL;
2093 struct got_pathlist_head paths;
2094 struct got_pathlist_entry *pe;
2096 *new_tree_id = NULL;
2100 dir = opendir(path_dir);
2102 err = got_error_from_errno2("opendir", path_dir);
2107 while ((de = readdir(dir)) != NULL) {
2111 if (strcmp(de->d_name, ".") == 0 ||
2112 strcmp(de->d_name, "..") == 0)
2115 TAILQ_FOREACH(pe, ignores, entry) {
2116 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2124 err = got_path_dirent_type(&type, path_dir, de);
2128 if (type == DT_DIR) {
2129 err = import_subdir(&new_te, de, path_dir,
2130 ignores, repo, progress_cb, progress_arg);
2132 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2137 } else if (type == DT_REG || type == DT_LNK) {
2138 err = import_file(&new_te, de, path_dir, repo);
2144 err = insert_tree_entry(new_te, &paths);
2150 if (TAILQ_EMPTY(&paths)) {
2151 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2152 "cannot create tree without any entries");
2156 TAILQ_FOREACH(pe, &paths, entry) {
2157 struct got_tree_entry *te = pe->data;
2159 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2161 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2162 err = got_error_from_errno("asprintf");
2165 err = (*progress_cb)(progress_arg, path);
2171 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2175 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2179 const struct got_error *
2180 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2181 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2182 struct got_repository *repo, got_repo_import_cb progress_cb,
2185 const struct got_error *err;
2186 struct got_object_id *new_tree_id;
2188 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2189 progress_cb, progress_arg);
2193 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2194 author, time(NULL), author, time(NULL), logmsg, repo);
2199 const struct got_error *
2200 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2201 struct got_repository *repo)
2203 const struct got_error *err = NULL;
2204 char *path_objects = NULL, *path = NULL;
2206 struct got_object_id id;
2212 path_objects = got_repo_get_path_objects(repo);
2213 if (path_objects == NULL)
2214 return got_error_from_errno("got_repo_get_path_objects");
2216 for (i = 0; i <= 0xff; i++) {
2217 struct dirent *dent;
2219 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2220 err = got_error_from_errno("asprintf");
2224 dir = opendir(path);
2226 if (errno == ENOENT) {
2230 err = got_error_from_errno2("opendir", path);
2234 while ((dent = readdir(dir)) != NULL) {
2239 if (strcmp(dent->d_name, ".") == 0 ||
2240 strcmp(dent->d_name, "..") == 0)
2243 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2244 err = got_error_from_errno("asprintf");
2248 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2254 err = got_object_open_loose_fd(&fd, &id, repo);
2258 if (fstat(fd, &sb) == -1) {
2259 err = got_error_from_errno("fstat");
2264 (*ondisk_size) += sb.st_size;
2266 if (close(fd) == -1) {
2267 err = got_error_from_errno("close");
2272 if (closedir(dir) != 0) {
2273 err = got_error_from_errno("closedir");
2282 if (dir && closedir(dir) != 0 && err == NULL)
2283 err = got_error_from_errno("closedir");
2294 const struct got_error *
2295 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2296 off_t *total_packsize, struct got_repository *repo)
2298 const struct got_error *err = NULL;
2299 DIR *packdir = NULL;
2300 struct dirent *dent;
2301 struct got_packidx *packidx = NULL;
2303 char *path_packfile;
2309 *total_packsize = 0;
2311 packdir_fd = openat(got_repo_get_fd(repo),
2312 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2313 if (packdir_fd == -1) {
2314 return got_error_from_errno_fmt("openat: %s/%s",
2315 got_repo_get_path_git_dir(repo),
2316 GOT_OBJECTS_PACK_DIR);
2319 packdir = fdopendir(packdir_fd);
2320 if (packdir == NULL) {
2321 err = got_error_from_errno("fdopendir");
2325 while ((dent = readdir(packdir)) != NULL) {
2326 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2329 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2330 dent->d_name) == -1) {
2331 err = got_error_from_errno("asprintf");
2335 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2341 if (fstat(packidx->fd, &sb) == -1)
2343 *total_packsize += sb.st_size;
2345 err = got_packidx_get_packfile_path(&path_packfile,
2346 packidx->path_packidx);
2350 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2352 free(path_packfile);
2355 free(path_packfile);
2356 *total_packsize += sb.st_size;
2358 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2362 got_packidx_close(packidx);
2367 got_packidx_close(packidx);
2368 if (packdir && closedir(packdir) != 0 && err == NULL)
2369 err = got_error_from_errno("closedir");
2373 *total_packsize = 0;
2378 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2379 got_packidx_bloom_filter_cmp);