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>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_get_fd(struct got_repository *repo)
86 return repo->gitdir_fd;
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 return repo->gitconfig_author_name;
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 return repo->gitconfig_author_email;
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 get_path_git_child(struct got_repository *repo, const char *basename)
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
214 if (!S_ISDIR(sb.st_mode))
217 if (lstat(path_objects, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_refs, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_head, &sb) == -1)
229 if (!S_ISREG(sb.st_mode))
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 got_ref_close(head_ref);
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, *pack_fds_tmp;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
258 return got_error_from_errno("calloc");
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
268 pack_fds_tmp[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 pack_fds_tmp[i] = got_opentempfd();
272 if (pack_fds_tmp[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(pack_fds_tmp);
278 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
282 const struct got_error *
283 got_repo_pack_fds_close(int *pack_fds)
285 const struct got_error *err = NULL;
288 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
289 if (pack_fds[i] == -1)
291 if (close(pack_fds[i]) == -1) {
292 err = got_error_from_errno("close");
300 const struct got_error *
301 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
302 struct got_object *obj)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->objcache, id, obj);
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
319 got_repo_get_cached_object(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
325 const struct got_error *
326 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
327 struct got_tree_object *tree)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->treecache, id, tree);
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
343 struct got_tree_object *
344 got_repo_get_cached_tree(struct got_repository *repo,
345 struct got_object_id *id)
347 return (struct got_tree_object *)got_object_cache_get(
348 &repo->treecache, id);
351 const struct got_error *
352 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
353 struct got_commit_object *commit)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->commitcache, id, commit);
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
369 struct got_commit_object *
370 got_repo_get_cached_commit(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_commit_object *)got_object_cache_get(
374 &repo->commitcache, id);
377 const struct got_error *
378 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
379 struct got_tag_object *tag)
381 #ifndef GOT_NO_OBJ_CACHE
382 const struct got_error *err = NULL;
383 err = got_object_cache_add(&repo->tagcache, id, tag);
385 if (err->code == GOT_ERR_OBJ_EXISTS ||
386 err->code == GOT_ERR_OBJ_TOO_LARGE)
395 struct got_tag_object *
396 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
398 return (struct got_tag_object *)got_object_cache_get(
399 &repo->tagcache, id);
402 const struct got_error *
403 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
404 struct got_raw_object *raw)
406 #ifndef GOT_NO_OBJ_CACHE
407 const struct got_error *err = NULL;
408 err = got_object_cache_add(&repo->rawcache, id, raw);
410 if (err->code == GOT_ERR_OBJ_EXISTS ||
411 err->code == GOT_ERR_OBJ_TOO_LARGE)
421 struct got_raw_object *
422 got_repo_get_cached_raw_object(struct got_repository *repo,
423 struct got_object_id *id)
425 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
429 static const struct got_error *
430 open_repo(struct got_repository *repo, const char *path)
432 const struct got_error *err = NULL;
434 repo->gitdir_fd = -1;
436 /* bare git repository? */
437 repo->path_git_dir = strdup(path);
438 if (repo->path_git_dir == NULL)
439 return got_error_from_errno("strdup");
440 if (is_git_repo(repo)) {
441 repo->path = strdup(repo->path_git_dir);
442 if (repo->path == NULL) {
443 err = got_error_from_errno("strdup");
446 repo->gitdir_fd = open(repo->path_git_dir,
447 O_DIRECTORY | O_CLOEXEC);
448 if (repo->gitdir_fd == -1) {
449 err = got_error_from_errno2("open",
456 /* git repository with working tree? */
457 free(repo->path_git_dir);
458 repo->path_git_dir = NULL;
459 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
460 err = got_error_from_errno("asprintf");
463 if (is_git_repo(repo)) {
464 repo->path = strdup(path);
465 if (repo->path == NULL) {
466 err = got_error_from_errno("strdup");
469 repo->gitdir_fd = open(repo->path_git_dir,
470 O_DIRECTORY | O_CLOEXEC);
471 if (repo->gitdir_fd == -1) {
472 err = got_error_from_errno2("open",
479 err = got_error(GOT_ERR_NOT_GIT_REPO);
484 free(repo->path_git_dir);
485 repo->path_git_dir = NULL;
486 if (repo->gitdir_fd != -1)
487 close(repo->gitdir_fd);
488 repo->gitdir_fd = -1;
494 static const struct got_error *
495 parse_gitconfig_file(int *gitconfig_repository_format_version,
496 char **gitconfig_author_name, char **gitconfig_author_email,
497 struct got_remote_repo **remotes, int *nremotes,
498 char **gitconfig_owner, char ***extensions, int *nextensions,
499 const char *gitconfig_path)
501 const struct got_error *err = NULL, *child_err = NULL;
503 int imsg_fds[2] = { -1, -1 };
505 struct imsgbuf *ibuf;
507 *gitconfig_repository_format_version = 0;
512 *gitconfig_author_name = NULL;
513 *gitconfig_author_email = NULL;
519 *gitconfig_owner = NULL;
521 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
525 return got_error_from_errno2("open", gitconfig_path);
528 ibuf = calloc(1, sizeof(*ibuf));
530 err = got_error_from_errno("calloc");
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
535 err = got_error_from_errno("socketpair");
541 err = got_error_from_errno("fork");
543 } else if (pid == 0) {
544 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
549 if (close(imsg_fds[1]) == -1) {
550 err = got_error_from_errno("close");
554 imsg_init(ibuf, imsg_fds[0]);
556 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
561 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
565 err = got_privsep_recv_gitconfig_int(
566 gitconfig_repository_format_version, ibuf);
570 if (extensions && nextensions) {
571 err = got_privsep_send_gitconfig_repository_extensions_req(
575 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
578 if (*nextensions > 0) {
580 *extensions = calloc(*nextensions, sizeof(char *));
581 if (*extensions == NULL) {
582 err = got_error_from_errno("calloc");
585 for (i = 0; i < *nextensions; i++) {
587 err = got_privsep_recv_gitconfig_str(&ext,
591 (*extensions)[i] = ext;
596 err = got_privsep_send_gitconfig_author_name_req(ibuf);
600 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
604 err = got_privsep_send_gitconfig_author_email_req(ibuf);
608 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
612 if (remotes && nremotes) {
613 err = got_privsep_send_gitconfig_remotes_req(ibuf);
617 err = got_privsep_recv_gitconfig_remotes(remotes,
623 if (gitconfig_owner) {
624 err = got_privsep_send_gitconfig_owner_req(ibuf);
627 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
632 err = got_privsep_send_stop(imsg_fds[0]);
633 child_err = got_privsep_wait_for_child(pid);
634 if (child_err && err == NULL)
637 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
638 err = got_error_from_errno("close");
639 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
640 err = got_error_from_errno("close");
641 if (fd != -1 && close(fd) == -1 && err == NULL)
642 err = got_error_from_errno2("close", gitconfig_path);
647 static const struct got_error *
648 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
650 const struct got_error *err = NULL;
651 char *repo_gitconfig_path = NULL;
653 if (global_gitconfig_path) {
654 /* Read settings from ~/.gitconfig. */
655 int dummy_repo_version;
656 err = parse_gitconfig_file(&dummy_repo_version,
657 &repo->global_gitconfig_author_name,
658 &repo->global_gitconfig_author_email,
659 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
664 /* Read repository's .git/config file. */
665 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
666 if (repo_gitconfig_path == NULL)
667 return got_error_from_errno("got_repo_get_path_gitconfig");
669 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
670 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
671 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
672 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
673 repo_gitconfig_path);
677 free(repo_gitconfig_path);
681 static const struct got_error *
682 read_gotconfig(struct got_repository *repo)
684 const struct got_error *err = NULL;
685 char *gotconfig_path;
687 gotconfig_path = got_repo_get_path_gotconfig(repo);
688 if (gotconfig_path == NULL)
689 return got_error_from_errno("got_repo_get_path_gotconfig");
691 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
692 free(gotconfig_path);
696 /* Supported repository format extensions. */
697 static const char *const repo_extensions[] = {
698 "noop", /* Got supports repository format version 1. */
699 "preciousObjects", /* Supported by gotadmin cleanup. */
700 "worktreeConfig", /* Got does not care about Git work trees. */
703 const struct got_error *
704 got_repo_open(struct got_repository **repop, const char *path,
705 const char *global_gitconfig_path, int *pack_fds)
707 struct got_repository *repo = NULL;
708 const struct got_error *err = NULL;
709 char *repo_path = NULL;
715 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
716 return got_error_from_errno("getrlimit");
718 repo = calloc(1, sizeof(*repo));
720 return got_error_from_errno("calloc");
722 RB_INIT(&repo->packidx_bloom_filters);
723 TAILQ_INIT(&repo->packidx_paths);
725 for (i = 0; i < nitems(repo->privsep_children); i++) {
726 memset(&repo->privsep_children[i], 0,
727 sizeof(repo->privsep_children[0]));
728 repo->privsep_children[i].imsg_fd = -1;
731 err = got_object_cache_init(&repo->objcache,
732 GOT_OBJECT_CACHE_TYPE_OBJ);
735 err = got_object_cache_init(&repo->treecache,
736 GOT_OBJECT_CACHE_TYPE_TREE);
739 err = got_object_cache_init(&repo->commitcache,
740 GOT_OBJECT_CACHE_TYPE_COMMIT);
743 err = got_object_cache_init(&repo->tagcache,
744 GOT_OBJECT_CACHE_TYPE_TAG);
747 err = got_object_cache_init(&repo->rawcache,
748 GOT_OBJECT_CACHE_TYPE_RAW);
752 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
753 if (repo->pack_cache_size > rl.rlim_cur / 8)
754 repo->pack_cache_size = rl.rlim_cur / 8;
755 for (i = 0; i < nitems(repo->packs); i++) {
756 if (i < repo->pack_cache_size) {
757 repo->packs[i].basefd = pack_fds[j++];
758 repo->packs[i].accumfd = pack_fds[j++];
760 repo->packs[i].basefd = -1;
761 repo->packs[i].accumfd = -1;
764 repo->pinned_pack = -1;
765 repo->pinned_packidx = -1;
766 repo->pinned_pid = 0;
768 repo_path = realpath(path, NULL);
769 if (repo_path == NULL) {
770 err = got_error_from_errno2("realpath", path);
777 err = open_repo(repo, repo_path);
780 if (err->code != GOT_ERR_NOT_GIT_REPO)
782 if (repo_path[0] == '/' && repo_path[1] == '\0') {
783 err = got_error(GOT_ERR_NOT_GIT_REPO);
786 err = got_path_dirname(&parent_path, repo_path);
790 repo_path = parent_path;
793 err = read_gotconfig(repo);
797 err = read_gitconfig(repo, global_gitconfig_path);
800 if (repo->gitconfig_repository_format_version != 0) {
801 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
804 for (i = 0; i < repo->nextensions; i++) {
805 char *ext = repo->extensions[i];
806 int j, supported = 0;
807 for (j = 0; j < nitems(repo_extensions); j++) {
808 if (strcmp(ext, repo_extensions[j]) == 0) {
814 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 err = got_repo_list_packidx(&repo->packidx_paths, repo);
822 got_repo_close(repo);
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 struct got_pathlist_entry *pe;
837 for (i = 0; i < repo->pack_cache_size; i++) {
838 if (repo->packidx_cache[i] == NULL)
840 got_packidx_close(repo->packidx_cache[i]);
843 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
844 &repo->packidx_bloom_filters))) {
845 RB_REMOVE(got_packidx_bloom_filter_tree,
846 &repo->packidx_bloom_filters, 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]);
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)
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 child_err = got_privsep_wait_for_child(
872 repo->privsep_children[i].pid);
873 if (child_err && err == NULL)
875 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
877 err = got_error_from_errno("close");
880 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
882 err = got_error_from_errno("close");
885 got_gotconfig_free(repo->gotconfig);
886 free(repo->gitconfig_author_name);
887 free(repo->gitconfig_author_email);
888 for (i = 0; i < repo->ngitconfig_remotes; i++)
889 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
890 free(repo->gitconfig_remotes);
891 for (i = 0; i < repo->nextensions; i++)
892 free(repo->extensions[i]);
893 free(repo->extensions);
895 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
896 free((void *)pe->path);
897 got_pathlist_free(&repo->packidx_paths);
904 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
910 free(repo->fetch_url);
911 repo->fetch_url = NULL;
912 free(repo->send_url);
913 repo->send_url = NULL;
914 for (i = 0; i < repo->nfetch_branches; i++)
915 free(repo->fetch_branches[i]);
916 free(repo->fetch_branches);
917 repo->fetch_branches = NULL;
918 repo->nfetch_branches = 0;
919 for (i = 0; i < repo->nsend_branches; i++)
920 free(repo->send_branches[i]);
921 free(repo->send_branches);
922 repo->send_branches = NULL;
923 repo->nsend_branches = 0;
926 const struct got_error *
927 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
928 const char *input_path)
930 const struct got_error *err = NULL;
931 const char *repo_abspath = NULL;
933 char *canonpath, *path = NULL;
935 *in_repo_path = NULL;
937 canonpath = strdup(input_path);
938 if (canonpath == NULL) {
939 err = got_error_from_errno("strdup");
942 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
946 repo_abspath = got_repo_get_path(repo);
948 if (canonpath[0] == '\0') {
949 path = strdup(canonpath);
951 err = got_error_from_errno("strdup");
955 path = realpath(canonpath, NULL);
957 if (errno != ENOENT) {
958 err = got_error_from_errno2("realpath",
963 * Path is not on disk.
964 * Assume it is already relative to repository root.
966 path = strdup(canonpath);
968 err = got_error_from_errno("strdup");
973 repolen = strlen(repo_abspath);
977 if (strcmp(path, repo_abspath) == 0) {
981 err = got_error_from_errno("strdup");
984 } else if (len > repolen &&
985 got_path_is_child(path, repo_abspath, repolen)) {
986 /* Matched an on-disk path inside repository. */
987 if (got_repo_is_bare(repo)) {
989 * Matched an on-disk path inside repository
990 * database. Treat input as repository-relative.
997 /* Strip common prefix with repository path. */
998 err = got_path_skip_common_ancestor(&child,
1007 * Matched unrelated on-disk path.
1008 * Treat input as repository-relative.
1016 /* Make in-repository path absolute */
1017 if (path[0] != '/') {
1019 if (asprintf(&abspath, "/%s", path) == -1) {
1020 err = got_error_from_errno("asprintf");
1032 *in_repo_path = path;
1036 static const struct got_error *
1037 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1038 const char *path_packidx)
1040 const struct got_error *err = NULL;
1043 for (i = 0; i < repo->pack_cache_size; i++) {
1044 if (repo->packidx_cache[i] == NULL)
1046 if (strcmp(repo->packidx_cache[i]->path_packidx,
1047 path_packidx) == 0) {
1048 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1051 if (i == repo->pack_cache_size) {
1054 } while (i > 0 && repo->pinned_packidx >= 0 &&
1055 i == repo->pinned_packidx);
1056 err = got_packidx_close(repo->packidx_cache[i]);
1061 repo->packidx_cache[i] = packidx;
1067 got_repo_is_packidx_filename(const char *name, size_t len)
1069 if (len != GOT_PACKIDX_NAMELEN)
1072 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1075 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1076 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1082 static struct got_packidx_bloom_filter *
1083 get_packidx_bloom_filter(struct got_repository *repo,
1084 const char *path, size_t path_len)
1086 struct got_packidx_bloom_filter key;
1088 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1089 return NULL; /* XXX */
1090 key.path_len = path_len;
1092 return RB_FIND(got_packidx_bloom_filter_tree,
1093 &repo->packidx_bloom_filters, &key);
1097 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1098 const char *path_packidx, struct got_object_id *id)
1100 struct got_packidx_bloom_filter *bf;
1102 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1104 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1106 /* No bloom filter means this pack index must be searched. */
1110 static const struct got_error *
1111 add_packidx_bloom_filter(struct got_repository *repo,
1112 struct got_packidx *packidx, const char *path_packidx)
1114 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1115 struct got_packidx_bloom_filter *bf;
1119 * Don't use bloom filters for very large pack index files.
1120 * Large pack files will contain a relatively large fraction
1121 * of our objects so we will likely need to visit them anyway.
1122 * The more objects a pack file contains the higher the probability
1123 * of a false-positive match from the bloom filter. And reading
1124 * all object IDs from a large pack index file can be expensive.
1126 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1129 /* Do we already have a filter for this pack index? */
1130 if (get_packidx_bloom_filter(repo, path_packidx,
1131 strlen(path_packidx)) != NULL)
1134 bf = calloc(1, sizeof(*bf));
1136 return got_error_from_errno("calloc");
1137 bf->bloom = calloc(1, sizeof(*bf->bloom));
1138 if (bf->bloom == NULL) {
1140 return got_error_from_errno("calloc");
1143 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1144 if (len >= sizeof(bf->path)) {
1147 return got_error(GOT_ERR_NO_SPACE);
1151 /* Minimum size supported by our bloom filter is 1000 entries. */
1152 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1153 for (i = 0; i < nobjects; i++) {
1154 struct got_packidx_object_id *id;
1155 id = &packidx->hdr.sorted_ids[i];
1156 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1159 RB_INSERT(got_packidx_bloom_filter_tree,
1160 &repo->packidx_bloom_filters, bf);
1164 const struct got_error *
1165 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1166 struct got_repository *repo, struct got_object_id *id)
1168 const struct got_error *err;
1169 struct got_pathlist_entry *pe;
1172 /* Search pack index cache. */
1173 for (i = 0; i < repo->pack_cache_size; i++) {
1174 if (repo->packidx_cache[i] == NULL)
1176 if (!got_repo_check_packidx_bloom_filter(repo,
1177 repo->packidx_cache[i]->path_packidx, id))
1178 continue; /* object will not be found in this index */
1179 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1181 *packidx = repo->packidx_cache[i];
1183 * Move this cache entry to the front. Repeatedly
1184 * searching a wrong pack index can be expensive.
1187 memmove(&repo->packidx_cache[1],
1188 &repo->packidx_cache[0],
1189 i * sizeof(repo->packidx_cache[0]));
1190 repo->packidx_cache[0] = *packidx;
1191 if (repo->pinned_packidx >= 0 &&
1192 repo->pinned_packidx < i)
1193 repo->pinned_packidx++;
1194 else if (repo->pinned_packidx == i)
1195 repo->pinned_packidx = 0;
1200 /* No luck. Search the filesystem. */
1202 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1203 const char *path_packidx = pe->path;
1206 if (!got_repo_check_packidx_bloom_filter(repo,
1208 continue; /* object will not be found in this index */
1210 for (i = 0; i < repo->pack_cache_size; i++) {
1211 if (repo->packidx_cache[i] == NULL)
1213 if (strcmp(repo->packidx_cache[i]->path_packidx,
1214 path_packidx) == 0) {
1220 continue; /* already searched */
1222 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1227 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1231 err = cache_packidx(repo, *packidx, path_packidx);
1235 *idx = got_packidx_get_object_idx(*packidx, id);
1237 err = NULL; /* found the object */
1242 err = got_error_no_obj(id);
1247 const struct got_error *
1248 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1249 struct got_repository *repo)
1251 const struct got_error *err = NULL;
1252 DIR *packdir = NULL;
1253 struct dirent *dent;
1254 char *path_packidx = NULL;
1257 packdir_fd = openat(got_repo_get_fd(repo),
1258 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1259 if (packdir_fd == -1) {
1260 return got_error_from_errno_fmt("openat: %s/%s",
1261 got_repo_get_path_git_dir(repo),
1262 GOT_OBJECTS_PACK_DIR);
1265 packdir = fdopendir(packdir_fd);
1266 if (packdir == NULL) {
1267 err = got_error_from_errno("fdopendir");
1271 while ((dent = readdir(packdir)) != NULL) {
1272 if (!got_repo_is_packidx_filename(dent->d_name,
1273 strlen(dent->d_name)))
1276 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1277 dent->d_name) == -1) {
1278 err = got_error_from_errno("asprintf");
1279 path_packidx = NULL;
1283 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1290 if (packdir && closedir(packdir) != 0 && err == NULL)
1291 err = got_error_from_errno("closedir");
1295 const struct got_error *
1296 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1297 struct got_repository *repo)
1299 const struct got_error *err;
1304 /* Search pack index cache. */
1305 for (i = 0; i < repo->pack_cache_size; i++) {
1306 if (repo->packidx_cache[i] == NULL)
1308 if (strcmp(repo->packidx_cache[i]->path_packidx,
1309 path_packidx) == 0) {
1310 *packidx = repo->packidx_cache[i];
1314 /* No luck. Search the filesystem. */
1316 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1321 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1325 err = cache_packidx(repo, *packidx, path_packidx);
1328 got_packidx_close(*packidx);
1334 static const struct got_error *
1335 read_packfile_hdr(int fd, struct got_packidx *packidx)
1337 const struct got_error *err = NULL;
1338 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1339 struct got_packfile_hdr hdr;
1342 n = read(fd, &hdr, sizeof(hdr));
1344 return got_error_from_errno("read");
1345 if (n != sizeof(hdr))
1346 return got_error(GOT_ERR_BAD_PACKFILE);
1348 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1349 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1350 be32toh(hdr.nobjects) != totobj)
1351 err = got_error(GOT_ERR_BAD_PACKFILE);
1356 static const struct got_error *
1357 open_packfile(int *fd, struct got_repository *repo,
1358 const char *relpath, struct got_packidx *packidx)
1360 const struct got_error *err = NULL;
1362 *fd = openat(got_repo_get_fd(repo), relpath,
1363 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1365 return got_error_from_errno_fmt("openat: %s/%s",
1366 got_repo_get_path_git_dir(repo), relpath);
1369 err = read_packfile_hdr(*fd, packidx);
1379 const struct got_error *
1380 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1381 const char *path_packfile, struct got_packidx *packidx)
1383 const struct got_error *err = NULL;
1384 struct got_pack *pack = NULL;
1391 for (i = 0; i < repo->pack_cache_size; i++) {
1392 pack = &repo->packs[i];
1393 if (pack->path_packfile == NULL)
1395 if (strcmp(pack->path_packfile, path_packfile) == 0)
1396 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1399 if (i == repo->pack_cache_size) {
1400 struct got_pack tmp;
1403 } while (i > 0 && repo->pinned_pack >= 0 &&
1404 i == repo->pinned_pack);
1405 err = got_pack_close(&repo->packs[i]);
1408 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1409 return got_error_from_errno("ftruncate");
1410 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1411 return got_error_from_errno("ftruncate");
1412 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1413 memcpy(&repo->packs[i], &repo->packs[0],
1414 sizeof(repo->packs[i]));
1415 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1416 if (repo->pinned_pack == 0)
1417 repo->pinned_pack = i;
1418 else if (repo->pinned_pack == i)
1419 repo->pinned_pack = 0;
1423 pack = &repo->packs[i];
1425 pack->path_packfile = strdup(path_packfile);
1426 if (pack->path_packfile == NULL) {
1427 err = got_error_from_errno("strdup");
1431 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1435 if (fstat(pack->fd, &sb) != 0) {
1436 err = got_error_from_errno("fstat");
1439 pack->filesize = sb.st_size;
1441 pack->privsep_child = NULL;
1443 #ifndef GOT_PACK_NO_MMAP
1444 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1446 if (pack->map == MAP_FAILED) {
1447 if (errno != ENOMEM) {
1448 err = got_error_from_errno("mmap");
1451 pack->map = NULL; /* fall back to read(2) */
1457 free(pack->path_packfile);
1458 memset(pack, 0, sizeof(*pack));
1466 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1468 struct got_pack *pack = NULL;
1471 for (i = 0; i < repo->pack_cache_size; i++) {
1472 pack = &repo->packs[i];
1473 if (pack->path_packfile == NULL)
1475 if (strcmp(pack->path_packfile, path_packfile) == 0)
1482 const struct got_error *
1483 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1484 struct got_pack *pack)
1487 int pinned_pack = -1, pinned_packidx = -1;
1489 for (i = 0; i < repo->pack_cache_size; i++) {
1490 if (repo->packidx_cache[i] &&
1491 strcmp(repo->packidx_cache[i]->path_packidx,
1492 packidx->path_packidx) == 0)
1494 if (repo->packs[i].path_packfile &&
1495 strcmp(repo->packs[i].path_packfile,
1496 pack->path_packfile) == 0)
1500 if (pinned_packidx == -1 || pinned_pack == -1)
1501 return got_error(GOT_ERR_PIN_PACK);
1503 repo->pinned_pack = pinned_pack;
1504 repo->pinned_packidx = pinned_packidx;
1505 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1510 got_repo_get_pinned_pack(struct got_repository *repo)
1512 if (repo->pinned_pack >= 0 &&
1513 repo->pinned_pack < repo->pack_cache_size)
1514 return &repo->packs[repo->pinned_pack];
1520 got_repo_unpin_pack(struct got_repository *repo)
1522 repo->pinned_packidx = -1;
1523 repo->pinned_pack = -1;
1524 repo->pinned_pid = 0;
1527 const struct got_error *
1528 got_repo_init(const char *repo_path)
1530 const struct got_error *err = NULL;
1531 const char *dirnames[] = {
1533 GOT_OBJECTS_PACK_DIR,
1536 const char *description_str = "Unnamed repository; "
1537 "edit this file 'description' to name the repository.";
1538 const char *headref_str = "ref: refs/heads/main";
1539 const char *gitconfig_str = "[core]\n"
1540 "\trepositoryformatversion = 0\n"
1541 "\tfilemode = true\n"
1546 if (!got_path_dir_is_empty(repo_path))
1547 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1549 for (i = 0; i < nitems(dirnames); i++) {
1550 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1551 return got_error_from_errno("asprintf");
1553 err = got_path_mkdir(path);
1559 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1560 return got_error_from_errno("asprintf");
1561 err = got_path_create_file(path, description_str);
1566 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1567 return got_error_from_errno("asprintf");
1568 err = got_path_create_file(path, headref_str);
1573 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1574 return got_error_from_errno("asprintf");
1575 err = got_path_create_file(path, gitconfig_str);
1583 static const struct got_error *
1584 match_packed_object(struct got_object_id **unique_id,
1585 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1587 const struct got_error *err = NULL;
1588 struct got_object_id_queue matched_ids;
1589 struct got_pathlist_entry *pe;
1591 STAILQ_INIT(&matched_ids);
1593 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1594 const char *path_packidx = pe->path;
1595 struct got_packidx *packidx;
1596 struct got_object_qid *qid;
1598 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1603 err = got_packidx_match_id_str_prefix(&matched_ids,
1604 packidx, id_str_prefix);
1606 got_packidx_close(packidx);
1609 err = got_packidx_close(packidx);
1613 STAILQ_FOREACH(qid, &matched_ids, entry) {
1614 if (obj_type != GOT_OBJ_TYPE_ANY) {
1616 err = got_object_get_type(&matched_type, repo,
1620 if (matched_type != obj_type)
1623 if (*unique_id == NULL) {
1624 *unique_id = got_object_id_dup(&qid->id);
1625 if (*unique_id == NULL) {
1626 err = got_error_from_errno("malloc");
1630 if (got_object_id_cmp(*unique_id,
1632 continue; /* packed multiple times */
1633 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1639 got_object_id_queue_free(&matched_ids);
1647 static const struct got_error *
1648 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1649 const char *object_dir, const char *id_str_prefix, int obj_type,
1650 struct got_repository *repo)
1652 const struct got_error *err = NULL;
1655 struct dirent *dent;
1656 struct got_object_id id;
1658 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1659 err = got_error_from_errno("asprintf");
1663 dir = opendir(path);
1665 if (errno == ENOENT) {
1669 err = got_error_from_errno2("opendir", path);
1672 while ((dent = readdir(dir)) != NULL) {
1676 if (strcmp(dent->d_name, ".") == 0 ||
1677 strcmp(dent->d_name, "..") == 0)
1680 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1681 err = got_error_from_errno("asprintf");
1685 if (!got_parse_sha1_digest(id.sha1, id_str))
1689 * Directory entries do not necessarily appear in
1690 * sorted order, so we must iterate over all of them.
1692 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1698 if (*unique_id == NULL) {
1699 if (obj_type != GOT_OBJ_TYPE_ANY) {
1701 err = got_object_get_type(&matched_type, repo,
1705 if (matched_type != obj_type)
1708 *unique_id = got_object_id_dup(&id);
1709 if (*unique_id == NULL) {
1710 err = got_error_from_errno("got_object_id_dup");
1715 if (got_object_id_cmp(*unique_id, &id) == 0)
1716 continue; /* both packed and loose */
1717 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1723 if (dir && closedir(dir) != 0 && err == NULL)
1724 err = got_error_from_errno("closedir");
1733 const struct got_error *
1734 got_repo_match_object_id_prefix(struct got_object_id **id,
1735 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1737 const struct got_error *err = NULL;
1738 char *path_objects = got_repo_get_path_objects(repo);
1739 char *object_dir = NULL;
1745 len = strlen(id_str_prefix);
1746 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1747 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1749 for (i = 0; i < len; i++) {
1750 if (isxdigit((unsigned char)id_str_prefix[i]))
1752 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1756 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1759 object_dir = strndup(id_str_prefix, 2);
1760 if (object_dir == NULL) {
1761 err = got_error_from_errno("strdup");
1764 err = match_loose_object(id, path_objects, object_dir,
1765 id_str_prefix, obj_type, repo);
1766 } else if (len == 1) {
1768 for (i = 0; i < 0xf; i++) {
1769 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1771 err = got_error_from_errno("asprintf");
1774 err = match_packed_object(id, repo, object_dir,
1778 err = match_loose_object(id, path_objects, object_dir,
1779 id_str_prefix, obj_type, repo);
1784 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1792 } else if (*id == NULL) {
1794 case GOT_OBJ_TYPE_BLOB:
1795 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1796 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1798 case GOT_OBJ_TYPE_TREE:
1799 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1800 GOT_OBJ_LABEL_TREE, id_str_prefix);
1802 case GOT_OBJ_TYPE_COMMIT:
1803 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1804 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1806 case GOT_OBJ_TYPE_TAG:
1807 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1808 GOT_OBJ_LABEL_TAG, id_str_prefix);
1811 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1819 const struct got_error *
1820 got_repo_match_object_id(struct got_object_id **id, char **label,
1821 const char *id_str, int obj_type, struct got_reflist_head *refs,
1822 struct got_repository *repo)
1824 const struct got_error *err;
1825 struct got_tag_object *tag;
1826 struct got_reference *ref = NULL;
1833 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1836 *id = got_object_id_dup(
1837 got_object_tag_get_object_id(tag));
1839 err = got_error_from_errno("got_object_id_dup");
1840 else if (label && asprintf(label, "refs/tags/%s",
1841 got_object_tag_get_name(tag)) == -1) {
1842 err = got_error_from_errno("asprintf");
1846 got_object_tag_close(tag);
1848 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1849 err->code != GOT_ERR_NO_OBJ)
1853 err = got_ref_open(&ref, repo, id_str, 0);
1855 err = got_ref_resolve(id, repo, ref);
1859 *label = strdup(got_ref_get_name(ref));
1860 if (*label == NULL) {
1861 err = got_error_from_errno("strdup");
1866 if (err->code != GOT_ERR_NOT_REF &&
1867 err->code != GOT_ERR_BAD_REF_NAME)
1869 err = got_repo_match_object_id_prefix(id, id_str,
1872 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1873 err = got_error_not_ref(id_str);
1877 err = got_object_id_str(label, *id);
1878 if (*label == NULL) {
1879 err = got_error_from_errno("strdup");
1890 const struct got_error *
1891 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1892 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1894 const struct got_error *err = NULL;
1895 struct got_reflist_entry *re;
1896 struct got_object_id *tag_id;
1897 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1901 TAILQ_FOREACH(re, refs, entry) {
1902 const char *refname;
1903 refname = got_ref_get_name(re->ref);
1904 if (got_ref_is_symbolic(re->ref))
1906 if (strncmp(refname, "refs/tags/", 10) != 0)
1908 if (!name_is_absolute)
1909 refname += strlen("refs/tags/");
1910 if (strcmp(refname, name) != 0)
1912 err = got_ref_resolve(&tag_id, repo, re->ref);
1915 err = got_object_open_as_tag(tag, repo, tag_id);
1919 if (obj_type == GOT_OBJ_TYPE_ANY ||
1920 got_object_tag_get_object_type(*tag) == obj_type)
1922 got_object_tag_close(*tag);
1926 if (err == NULL && *tag == NULL)
1927 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1928 GOT_OBJ_LABEL_TAG, name);
1932 static const struct got_error *
1933 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1934 const char *name, mode_t mode, struct got_object_id *blob_id)
1936 const struct got_error *err = NULL;
1940 *new_te = calloc(1, sizeof(**new_te));
1941 if (*new_te == NULL)
1942 return got_error_from_errno("calloc");
1944 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1945 sizeof((*new_te)->name)) {
1946 err = got_error(GOT_ERR_NO_SPACE);
1950 if (S_ISLNK(mode)) {
1951 (*new_te)->mode = S_IFLNK;
1953 (*new_te)->mode = S_IFREG;
1954 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1956 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1958 if (err && *new_te) {
1965 static const struct got_error *
1966 import_file(struct got_tree_entry **new_te, struct dirent *de,
1967 const char *path, struct got_repository *repo)
1969 const struct got_error *err;
1970 struct got_object_id *blob_id = NULL;
1974 if (asprintf(&filepath, "%s%s%s", path,
1975 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1976 return got_error_from_errno("asprintf");
1978 if (lstat(filepath, &sb) != 0) {
1979 err = got_error_from_errno2("lstat", path);
1983 err = got_object_blob_create(&blob_id, filepath, repo);
1987 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1996 static const struct got_error *
1997 insert_tree_entry(struct got_tree_entry *new_te,
1998 struct got_pathlist_head *paths)
2000 const struct got_error *err = NULL;
2001 struct got_pathlist_entry *new_pe;
2003 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2007 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2011 static const struct got_error *write_tree(struct got_object_id **,
2012 const char *, struct got_pathlist_head *, struct got_repository *,
2013 got_repo_import_cb progress_cb, void *progress_arg);
2015 static const struct got_error *
2016 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2017 const char *path, struct got_pathlist_head *ignores,
2018 struct got_repository *repo,
2019 got_repo_import_cb progress_cb, void *progress_arg)
2021 const struct got_error *err;
2022 struct got_object_id *id = NULL;
2025 if (asprintf(&subdirpath, "%s%s%s", path,
2026 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2027 return got_error_from_errno("asprintf");
2029 (*new_te) = calloc(1, sizeof(**new_te));
2030 if (*new_te == NULL)
2031 return got_error_from_errno("calloc");
2032 (*new_te)->mode = S_IFDIR;
2033 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2034 sizeof((*new_te)->name)) {
2035 err = got_error(GOT_ERR_NO_SPACE);
2038 err = write_tree(&id, subdirpath, ignores, repo,
2039 progress_cb, progress_arg);
2042 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2054 static const struct got_error *
2055 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2056 struct got_pathlist_head *ignores, struct got_repository *repo,
2057 got_repo_import_cb progress_cb, void *progress_arg)
2059 const struct got_error *err = NULL;
2063 struct got_tree_entry *new_te = NULL;
2064 struct got_pathlist_head paths;
2065 struct got_pathlist_entry *pe;
2067 *new_tree_id = NULL;
2071 dir = opendir(path_dir);
2073 err = got_error_from_errno2("opendir", path_dir);
2078 while ((de = readdir(dir)) != NULL) {
2082 if (strcmp(de->d_name, ".") == 0 ||
2083 strcmp(de->d_name, "..") == 0)
2086 TAILQ_FOREACH(pe, ignores, entry) {
2087 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2095 err = got_path_dirent_type(&type, path_dir, de);
2099 if (type == DT_DIR) {
2100 err = import_subdir(&new_te, de, path_dir,
2101 ignores, repo, progress_cb, progress_arg);
2103 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2108 } else if (type == DT_REG || type == DT_LNK) {
2109 err = import_file(&new_te, de, path_dir, repo);
2115 err = insert_tree_entry(new_te, &paths);
2121 if (TAILQ_EMPTY(&paths)) {
2122 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2123 "cannot create tree without any entries");
2127 TAILQ_FOREACH(pe, &paths, entry) {
2128 struct got_tree_entry *te = pe->data;
2130 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2132 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2133 err = got_error_from_errno("asprintf");
2136 err = (*progress_cb)(progress_arg, path);
2142 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2146 got_pathlist_free(&paths);
2150 const struct got_error *
2151 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2152 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2153 struct got_repository *repo, got_repo_import_cb progress_cb,
2156 const struct got_error *err;
2157 struct got_object_id *new_tree_id;
2159 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2160 progress_cb, progress_arg);
2164 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2165 author, time(NULL), author, time(NULL), logmsg, repo);
2170 const struct got_error *
2171 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 char *path_objects = NULL, *path = NULL;
2177 struct got_object_id id;
2183 path_objects = got_repo_get_path_objects(repo);
2184 if (path_objects == NULL)
2185 return got_error_from_errno("got_repo_get_path_objects");
2187 for (i = 0; i <= 0xff; i++) {
2188 struct dirent *dent;
2190 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2191 err = got_error_from_errno("asprintf");
2195 dir = opendir(path);
2197 if (errno == ENOENT) {
2201 err = got_error_from_errno2("opendir", path);
2205 while ((dent = readdir(dir)) != NULL) {
2210 if (strcmp(dent->d_name, ".") == 0 ||
2211 strcmp(dent->d_name, "..") == 0)
2214 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2215 err = got_error_from_errno("asprintf");
2219 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2225 err = got_object_open_loose_fd(&fd, &id, repo);
2229 if (fstat(fd, &sb) == -1) {
2230 err = got_error_from_errno("fstat");
2235 (*ondisk_size) += sb.st_size;
2237 if (close(fd) == -1) {
2238 err = got_error_from_errno("close");
2243 if (closedir(dir) != 0) {
2244 err = got_error_from_errno("closedir");
2253 if (dir && closedir(dir) != 0 && err == NULL)
2254 err = got_error_from_errno("closedir");
2265 const struct got_error *
2266 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2267 off_t *total_packsize, struct got_repository *repo)
2269 const struct got_error *err = NULL;
2270 DIR *packdir = NULL;
2271 struct dirent *dent;
2272 struct got_packidx *packidx = NULL;
2274 char *path_packfile;
2280 *total_packsize = 0;
2282 packdir_fd = openat(got_repo_get_fd(repo),
2283 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2284 if (packdir_fd == -1) {
2285 return got_error_from_errno_fmt("openat: %s/%s",
2286 got_repo_get_path_git_dir(repo),
2287 GOT_OBJECTS_PACK_DIR);
2290 packdir = fdopendir(packdir_fd);
2291 if (packdir == NULL) {
2292 err = got_error_from_errno("fdopendir");
2296 while ((dent = readdir(packdir)) != NULL) {
2297 if (!got_repo_is_packidx_filename(dent->d_name,
2298 strlen(dent->d_name)))
2301 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2302 dent->d_name) == -1) {
2303 err = got_error_from_errno("asprintf");
2307 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2313 if (fstat(packidx->fd, &sb) == -1)
2315 *total_packsize += sb.st_size;
2317 err = got_packidx_get_packfile_path(&path_packfile,
2318 packidx->path_packidx);
2322 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2324 free(path_packfile);
2327 free(path_packfile);
2328 *total_packsize += sb.st_size;
2330 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2334 got_packidx_close(packidx);
2339 got_packidx_close(packidx);
2340 if (packdir && closedir(packdir) != 0 && err == NULL)
2341 err = got_error_from_errno("closedir");
2345 *total_packsize = 0;
2350 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2351 got_packidx_bloom_filter_cmp);