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>
48 #include "got_error.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_cancel.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_delta_cache.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_gotconfig.h"
70 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
73 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
75 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
76 got_packidx_bloom_filter_cmp);
79 got_repo_get_path(struct got_repository *repo)
85 got_repo_get_path_git_dir(struct got_repository *repo)
87 return repo->path_git_dir;
91 got_repo_get_fd(struct got_repository *repo)
93 return repo->gitdir_fd;
96 enum got_hash_algorithm
97 got_repo_get_hash_algorithm(struct got_repository *repo)
103 got_repo_get_gitconfig_author_name(struct got_repository *repo)
105 return repo->gitconfig_author_name;
109 got_repo_get_gitconfig_author_email(struct got_repository *repo)
111 return repo->gitconfig_author_email;
115 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
117 return repo->global_gitconfig_author_name;
121 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
123 return repo->global_gitconfig_author_email;
127 got_repo_get_gitconfig_owner(struct got_repository *repo)
129 return repo->gitconfig_owner;
133 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
134 struct got_repository *repo)
136 *extensions = repo->extensions;
137 *nextensions = repo->nextensions;
141 got_repo_is_bare(struct got_repository *repo)
143 return (strcmp(repo->path, repo->path_git_dir) == 0);
147 get_path_git_child(struct got_repository *repo, const char *basename)
151 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
159 got_repo_get_path_objects(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_OBJECTS_DIR);
165 got_repo_get_path_objects_pack(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
171 got_repo_get_path_refs(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_REFS_DIR);
177 got_repo_get_path_packed_refs(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
183 get_path_head(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_HEAD_FILE);
189 got_repo_get_path_gitconfig(struct got_repository *repo)
191 return get_path_git_child(repo, GOT_GITCONFIG);
195 got_repo_get_path_gotconfig(struct got_repository *repo)
197 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
200 const struct got_gotconfig *
201 got_repo_get_gotconfig(struct got_repository *repo)
203 return repo->gotconfig;
207 got_repo_get_gitconfig_remotes(int *nremotes,
208 const struct got_remote_repo **remotes, struct got_repository *repo)
210 *nremotes = repo->ngitconfig_remotes;
211 *remotes = repo->gitconfig_remotes;
215 is_git_repo(struct got_repository *repo)
217 const char *path_git = got_repo_get_path_git_dir(repo);
218 char *path_objects = got_repo_get_path_objects(repo);
219 char *path_refs = got_repo_get_path_refs(repo);
220 char *path_head = get_path_head(repo);
223 struct got_reference *head_ref;
225 if (lstat(path_git, &sb) == -1)
227 if (!S_ISDIR(sb.st_mode))
230 if (lstat(path_objects, &sb) == -1)
232 if (!S_ISDIR(sb.st_mode))
235 if (lstat(path_refs, &sb) == -1)
237 if (!S_ISDIR(sb.st_mode))
240 if (lstat(path_head, &sb) == -1)
242 if (!S_ISREG(sb.st_mode))
245 /* Check if the HEAD reference can be opened. */
246 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
248 got_ref_close(head_ref);
259 static const struct got_error *
260 close_tempfiles(int *fds, size_t nfds)
262 const struct got_error *err = NULL;
265 for (i = 0; i < nfds; i++) {
268 if (close(fds[i]) == -1) {
269 err = got_error_from_errno("close");
277 static const struct got_error *
278 open_tempfiles(int **fds, size_t array_size, size_t nfds)
280 const struct got_error *err = NULL;
283 *fds = calloc(array_size, sizeof(**fds));
285 return got_error_from_errno("calloc");
287 for (i = 0; i < array_size; i++)
290 for (i = 0; i < nfds; i++) {
291 (*fds)[i] = got_opentempfd();
292 if ((*fds)[i] == -1) {
293 err = got_error_from_errno("got_opentempfd");
294 close_tempfiles(*fds, nfds);
303 static const struct got_error *
304 get_pack_cache_size(int *pack_cache_size)
308 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
309 return got_error_from_errno("getrlimit");
311 *pack_cache_size = GOT_PACK_CACHE_SIZE;
312 if (*pack_cache_size > rl.rlim_cur / 8)
313 *pack_cache_size = rl.rlim_cur / 8;
318 const struct got_error *
319 got_repo_pack_fds_open(int **pack_fds)
321 const struct got_error *err;
324 err = get_pack_cache_size(&nfds);
329 * We need one basefd and one accumfd per cached pack.
330 * Our constants should be set up in a way such that
331 * this error never triggers.
333 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
334 return got_error(GOT_ERR_NO_SPACE);
336 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
339 const struct got_error *
340 got_repo_pack_fds_close(int *pack_fds)
342 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
345 const struct got_error *
346 got_repo_temp_fds_open(int **temp_fds)
348 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
349 GOT_REPO_NUM_TEMPFILES);
353 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
357 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
358 repo->tempfiles[i] = temp_fds[i];
361 const struct got_error *
362 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
369 for (i = 0; i < nitems(repo->tempfiles); i++) {
370 if (repo->tempfile_use_mask & (1 << i))
372 if (repo->tempfiles[i] != -1) {
373 if (ftruncate(repo->tempfiles[i], 0L) == -1)
374 return got_error_from_errno("ftruncate");
375 *fd = repo->tempfiles[i];
377 repo->tempfile_use_mask |= (1 << i);
382 return got_error(GOT_ERR_REPO_TEMPFILE);
386 got_repo_temp_fds_put(int idx, struct got_repository *repo)
388 repo->tempfile_use_mask &= ~(1 << idx);
391 const struct got_error *
392 got_repo_temp_fds_close(int *temp_fds)
394 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
397 const struct got_error *
398 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
399 struct got_object *obj)
401 #ifndef GOT_NO_OBJ_CACHE
402 const struct got_error *err = NULL;
403 err = got_object_cache_add(&repo->objcache, id, obj);
405 if (err->code == GOT_ERR_OBJ_EXISTS ||
406 err->code == GOT_ERR_OBJ_TOO_LARGE)
416 got_repo_get_cached_object(struct got_repository *repo,
417 struct got_object_id *id)
419 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
422 const struct got_error *
423 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
424 struct got_tree_object *tree)
426 #ifndef GOT_NO_OBJ_CACHE
427 const struct got_error *err = NULL;
428 err = got_object_cache_add(&repo->treecache, id, tree);
430 if (err->code == GOT_ERR_OBJ_EXISTS ||
431 err->code == GOT_ERR_OBJ_TOO_LARGE)
440 struct got_tree_object *
441 got_repo_get_cached_tree(struct got_repository *repo,
442 struct got_object_id *id)
444 return (struct got_tree_object *)got_object_cache_get(
445 &repo->treecache, id);
448 const struct got_error *
449 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
450 struct got_commit_object *commit)
452 #ifndef GOT_NO_OBJ_CACHE
453 const struct got_error *err = NULL;
454 err = got_object_cache_add(&repo->commitcache, id, commit);
456 if (err->code == GOT_ERR_OBJ_EXISTS ||
457 err->code == GOT_ERR_OBJ_TOO_LARGE)
466 struct got_commit_object *
467 got_repo_get_cached_commit(struct got_repository *repo,
468 struct got_object_id *id)
470 return (struct got_commit_object *)got_object_cache_get(
471 &repo->commitcache, id);
474 const struct got_error *
475 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
476 struct got_tag_object *tag)
478 #ifndef GOT_NO_OBJ_CACHE
479 const struct got_error *err = NULL;
480 err = got_object_cache_add(&repo->tagcache, id, tag);
482 if (err->code == GOT_ERR_OBJ_EXISTS ||
483 err->code == GOT_ERR_OBJ_TOO_LARGE)
492 struct got_tag_object *
493 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
495 return (struct got_tag_object *)got_object_cache_get(
496 &repo->tagcache, id);
499 const struct got_error *
500 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
501 struct got_raw_object *raw)
503 #ifndef GOT_NO_OBJ_CACHE
504 const struct got_error *err = NULL;
505 err = got_object_cache_add(&repo->rawcache, id, raw);
507 if (err->code == GOT_ERR_OBJ_EXISTS ||
508 err->code == GOT_ERR_OBJ_TOO_LARGE)
518 struct got_raw_object *
519 got_repo_get_cached_raw_object(struct got_repository *repo,
520 struct got_object_id *id)
522 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
526 static const struct got_error *
527 open_repo(struct got_repository *repo, const char *path)
529 const struct got_error *err = NULL;
531 repo->gitdir_fd = -1;
533 /* bare git repository? */
534 repo->path_git_dir = strdup(path);
535 if (repo->path_git_dir == NULL)
536 return got_error_from_errno("strdup");
537 if (is_git_repo(repo)) {
538 repo->path = strdup(repo->path_git_dir);
539 if (repo->path == NULL) {
540 err = got_error_from_errno("strdup");
543 repo->gitdir_fd = open(repo->path_git_dir,
544 O_DIRECTORY | O_CLOEXEC);
545 if (repo->gitdir_fd == -1) {
546 err = got_error_from_errno2("open",
553 /* git repository with working tree? */
554 free(repo->path_git_dir);
555 repo->path_git_dir = NULL;
556 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
557 err = got_error_from_errno("asprintf");
560 if (is_git_repo(repo)) {
561 repo->path = strdup(path);
562 if (repo->path == NULL) {
563 err = got_error_from_errno("strdup");
566 repo->gitdir_fd = open(repo->path_git_dir,
567 O_DIRECTORY | O_CLOEXEC);
568 if (repo->gitdir_fd == -1) {
569 err = got_error_from_errno2("open",
576 err = got_error(GOT_ERR_NOT_GIT_REPO);
581 free(repo->path_git_dir);
582 repo->path_git_dir = NULL;
583 if (repo->gitdir_fd != -1)
584 close(repo->gitdir_fd);
585 repo->gitdir_fd = -1;
591 static const struct got_error *
592 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
594 const struct got_error *err = NULL;
595 char *repo_gitconfig_path = NULL;
596 char *object_format = NULL;
598 if (global_gitconfig_path) {
599 /* Read settings from ~/.gitconfig. */
600 int dummy_repo_version;
601 err = got_repo_read_gitconfig(&dummy_repo_version,
602 &repo->global_gitconfig_author_name,
603 &repo->global_gitconfig_author_email,
604 NULL, NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
609 /* Read repository's .git/config file. */
610 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
611 if (repo_gitconfig_path == NULL)
612 return got_error_from_errno("got_repo_get_path_gitconfig");
614 err = got_repo_read_gitconfig(
615 &repo->gitconfig_repository_format_version,
616 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
617 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
618 &repo->gitconfig_owner, &object_format,
619 &repo->extensions, &repo->nextensions,
620 repo_gitconfig_path);
625 repo->algo = GOT_HASH_SHA1;
627 if (!strcmp(object_format, "sha256"))
628 repo->algo = GOT_HASH_SHA256;
630 err = got_error_msg(GOT_ERR_OBJECT_FORMAT,
636 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
639 for (i = 0; i < repo->ngitconfig_remotes; i++) {
640 got_repo_free_remote_repo_data(
641 &repo->gitconfig_remotes[i]);
643 free(repo->gitconfig_remotes);
644 repo->gitconfig_remotes = NULL;
645 repo->ngitconfig_remotes = 0;
647 free(repo->gitconfig_author_name);
648 repo->gitconfig_author_name = NULL;
649 free(repo->gitconfig_author_email);
650 repo->gitconfig_author_email = NULL;
652 free(repo->global_gitconfig_author_name);
653 repo->global_gitconfig_author_name = NULL;
654 free(repo->global_gitconfig_author_email);
655 repo->global_gitconfig_author_email = NULL;
660 free(repo_gitconfig_path);
664 static const struct got_error *
665 read_gotconfig(struct got_repository *repo)
667 const struct got_error *err = NULL;
668 char *gotconfig_path;
670 gotconfig_path = got_repo_get_path_gotconfig(repo);
671 if (gotconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gotconfig");
674 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
675 free(gotconfig_path);
679 /* Supported repository format extensions. */
680 static const char *const repo_extensions[] = {
681 "noop", /* Got supports repository format version 1. */
682 "preciousObjects", /* Supported by gotadmin cleanup. */
683 "worktreeConfig", /* Got does not care about Git work trees. */
686 const struct got_error *
687 got_repo_open(struct got_repository **repop, const char *path,
688 const char *global_gitconfig_path, int *pack_fds)
690 struct got_repository *repo = NULL;
691 const struct got_error *err = NULL;
692 char *repo_path = NULL;
697 repo = calloc(1, sizeof(*repo));
699 return got_error_from_errno("calloc");
701 RB_INIT(&repo->packidx_bloom_filters);
702 TAILQ_INIT(&repo->packidx_paths);
704 for (i = 0; i < nitems(repo->privsep_children); i++) {
705 memset(&repo->privsep_children[i], 0,
706 sizeof(repo->privsep_children[0]));
707 repo->privsep_children[i].imsg_fd = -1;
710 err = got_object_cache_init(&repo->objcache,
711 GOT_OBJECT_CACHE_TYPE_OBJ);
714 err = got_object_cache_init(&repo->treecache,
715 GOT_OBJECT_CACHE_TYPE_TREE);
718 err = got_object_cache_init(&repo->commitcache,
719 GOT_OBJECT_CACHE_TYPE_COMMIT);
722 err = got_object_cache_init(&repo->tagcache,
723 GOT_OBJECT_CACHE_TYPE_TAG);
726 err = got_object_cache_init(&repo->rawcache,
727 GOT_OBJECT_CACHE_TYPE_RAW);
731 err = get_pack_cache_size(&repo->pack_cache_size);
734 for (i = 0; i < nitems(repo->packs); i++) {
735 if (pack_fds != NULL && i < repo->pack_cache_size) {
736 repo->packs[i].basefd = pack_fds[j++];
737 repo->packs[i].accumfd = pack_fds[j++];
739 repo->packs[i].basefd = -1;
740 repo->packs[i].accumfd = -1;
743 for (i = 0; i < nitems(repo->tempfiles); i++)
744 repo->tempfiles[i] = -1;
745 repo->pinned_pack = -1;
746 repo->pinned_packidx = -1;
747 repo->pinned_pid = 0;
749 repo_path = realpath(path, NULL);
750 if (repo_path == NULL) {
751 err = got_error_from_errno2("realpath", path);
758 err = open_repo(repo, repo_path);
761 if (err->code != GOT_ERR_NOT_GIT_REPO)
763 if (repo_path[0] == '/' && repo_path[1] == '\0') {
764 err = got_error(GOT_ERR_NOT_GIT_REPO);
767 err = got_path_dirname(&parent_path, repo_path);
771 repo_path = parent_path;
774 err = read_gotconfig(repo);
778 err = read_gitconfig(repo, global_gitconfig_path);
781 if (repo->gitconfig_repository_format_version > 1) {
782 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
785 if (repo->gitconfig_repository_format_version == 0 &&
786 repo->algo != GOT_HASH_SHA1) {
787 err = got_error(GOT_ERR_OBJECT_FORMAT);
790 for (i = 0; i < repo->nextensions; i++) {
791 char *ext = repo->extensions[i];
792 int j, supported = 0;
793 for (j = 0; j < nitems(repo_extensions); j++) {
794 if (strcmp(ext, repo_extensions[j]) == 0) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
805 err = got_repo_list_packidx(&repo->packidx_paths, repo);
808 got_repo_close(repo);
815 const struct got_error *
816 got_repo_close(struct got_repository *repo)
818 const struct got_error *err = NULL, *child_err;
819 struct got_packidx_bloom_filter *bf;
822 for (i = 0; i < repo->pack_cache_size; i++) {
823 if (repo->packidx_cache[i] == NULL)
825 got_packidx_close(repo->packidx_cache[i]);
828 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
829 &repo->packidx_bloom_filters))) {
830 RB_REMOVE(got_packidx_bloom_filter_tree,
831 &repo->packidx_bloom_filters, bf);
832 bloom_free(bf->bloom);
837 for (i = 0; i < repo->pack_cache_size; i++)
838 if (repo->packs[i].path_packfile)
839 if (repo->packs[i].path_packfile)
840 got_pack_close(&repo->packs[i]);
843 free(repo->path_git_dir);
845 got_object_cache_close(&repo->objcache);
846 got_object_cache_close(&repo->treecache);
847 got_object_cache_close(&repo->commitcache);
848 got_object_cache_close(&repo->tagcache);
849 got_object_cache_close(&repo->rawcache);
851 for (i = 0; i < nitems(repo->privsep_children); i++) {
852 if (repo->privsep_children[i].imsg_fd == -1)
854 imsg_clear(repo->privsep_children[i].ibuf);
855 free(repo->privsep_children[i].ibuf);
856 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
857 child_err = got_privsep_wait_for_child(
858 repo->privsep_children[i].pid);
859 if (child_err && err == NULL)
861 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
863 err = got_error_from_errno("close");
866 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
868 err = got_error_from_errno("close");
871 got_gotconfig_free(repo->gotconfig);
872 free(repo->gitconfig_author_name);
873 free(repo->gitconfig_author_email);
874 for (i = 0; i < repo->ngitconfig_remotes; i++)
875 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
876 free(repo->gitconfig_remotes);
877 for (i = 0; i < repo->nextensions; i++)
878 free(repo->extensions[i]);
879 free(repo->extensions);
881 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
888 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
894 free(repo->fetch_url);
895 repo->fetch_url = NULL;
896 free(repo->send_url);
897 repo->send_url = NULL;
898 for (i = 0; i < repo->nfetch_branches; i++)
899 free(repo->fetch_branches[i]);
900 free(repo->fetch_branches);
901 repo->fetch_branches = NULL;
902 repo->nfetch_branches = 0;
903 for (i = 0; i < repo->nsend_branches; i++)
904 free(repo->send_branches[i]);
905 free(repo->send_branches);
906 repo->send_branches = NULL;
907 repo->nsend_branches = 0;
910 const struct got_error *
911 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
912 const char *input_path)
914 const struct got_error *err = NULL;
915 const char *repo_abspath = NULL;
917 char *canonpath, *path = NULL;
919 *in_repo_path = NULL;
921 canonpath = strdup(input_path);
922 if (canonpath == NULL) {
923 err = got_error_from_errno("strdup");
926 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
930 repo_abspath = got_repo_get_path(repo);
932 if (canonpath[0] == '\0') {
933 path = strdup(canonpath);
935 err = got_error_from_errno("strdup");
939 path = realpath(canonpath, NULL);
941 if (errno != ENOENT) {
942 err = got_error_from_errno2("realpath",
947 * Path is not on disk.
948 * Assume it is already relative to repository root.
950 path = strdup(canonpath);
952 err = got_error_from_errno("strdup");
957 repolen = strlen(repo_abspath);
961 if (strcmp(path, repo_abspath) == 0) {
965 err = got_error_from_errno("strdup");
968 } else if (len > repolen &&
969 got_path_is_child(path, repo_abspath, repolen)) {
970 /* Matched an on-disk path inside repository. */
971 if (got_repo_is_bare(repo)) {
973 * Matched an on-disk path inside repository
974 * database. Treat input as repository-relative.
981 /* Strip common prefix with repository path. */
982 err = got_path_skip_common_ancestor(&child,
991 * Matched unrelated on-disk path.
992 * Treat input as repository-relative.
1000 /* Make in-repository path absolute */
1001 if (path[0] != '/') {
1003 if (asprintf(&abspath, "/%s", path) == -1) {
1004 err = got_error_from_errno("asprintf");
1016 *in_repo_path = path;
1020 static const struct got_error *
1021 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1022 const char *path_packidx)
1024 const struct got_error *err = NULL;
1027 for (i = 0; i < repo->pack_cache_size; i++) {
1028 if (repo->packidx_cache[i] == NULL)
1030 if (strcmp(repo->packidx_cache[i]->path_packidx,
1031 path_packidx) == 0) {
1032 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1035 if (i == repo->pack_cache_size) {
1038 } while (i > 0 && repo->pinned_packidx >= 0 &&
1039 i == repo->pinned_packidx);
1040 err = got_packidx_close(repo->packidx_cache[i]);
1045 repo->packidx_cache[i] = packidx;
1051 got_repo_is_packidx_filename(const char *name, size_t len)
1053 if (len != GOT_PACKIDX_NAMELEN)
1056 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1059 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1060 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1066 static struct got_packidx_bloom_filter *
1067 get_packidx_bloom_filter(struct got_repository *repo,
1068 const char *path, size_t path_len)
1070 struct got_packidx_bloom_filter key;
1072 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1073 return NULL; /* XXX */
1074 key.path_len = path_len;
1076 return RB_FIND(got_packidx_bloom_filter_tree,
1077 &repo->packidx_bloom_filters, &key);
1081 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path_packidx, struct got_object_id *id)
1084 struct got_packidx_bloom_filter *bf;
1086 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1088 return bloom_check(bf->bloom, id->hash, sizeof(id->hash));
1090 /* No bloom filter means this pack index must be searched. */
1094 static const struct got_error *
1095 add_packidx_bloom_filter(struct got_repository *repo,
1096 struct got_packidx *packidx, const char *path_packidx)
1098 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1099 struct got_packidx_bloom_filter *bf;
1103 * Don't use bloom filters for very large pack index files.
1104 * Large pack files will contain a relatively large fraction
1105 * of our objects so we will likely need to visit them anyway.
1106 * The more objects a pack file contains the higher the probability
1107 * of a false-positive match from the bloom filter. And reading
1108 * all object IDs from a large pack index file can be expensive.
1110 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1113 /* Do we already have a filter for this pack index? */
1114 if (get_packidx_bloom_filter(repo, path_packidx,
1115 strlen(path_packidx)) != NULL)
1118 bf = calloc(1, sizeof(*bf));
1120 return got_error_from_errno("calloc");
1121 bf->bloom = calloc(1, sizeof(*bf->bloom));
1122 if (bf->bloom == NULL) {
1124 return got_error_from_errno("calloc");
1127 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1128 if (len >= sizeof(bf->path)) {
1131 return got_error(GOT_ERR_NO_SPACE);
1135 /* Minimum size supported by our bloom filter is 1000 entries. */
1136 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1137 for (i = 0; i < nobjects; i++) {
1138 struct got_packidx_object_id *id;
1139 id = &packidx->hdr.sorted_ids[i];
1140 bloom_add(bf->bloom, id->hash, sizeof(id->hash));
1143 RB_INSERT(got_packidx_bloom_filter_tree,
1144 &repo->packidx_bloom_filters, bf);
1149 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1151 struct got_pathlist_entry *pe;
1153 while (!TAILQ_EMPTY(packidx_paths)) {
1154 pe = TAILQ_FIRST(packidx_paths);
1155 TAILQ_REMOVE(packidx_paths, pe, entry);
1156 free((char *)pe->path);
1161 static const struct got_error *
1162 refresh_packidx_paths(struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 char *objects_pack_dir = NULL;
1168 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1169 if (objects_pack_dir == NULL)
1170 return got_error_from_errno("got_repo_get_path_objects_pack");
1172 if (stat(objects_pack_dir, &sb) == -1) {
1173 if (errno != ENOENT) {
1174 err = got_error_from_errno2("stat", objects_pack_dir);
1177 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1178 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1179 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1180 purge_packidx_paths(&repo->packidx_paths);
1181 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1186 free(objects_pack_dir);
1190 const struct got_error *
1191 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1192 struct got_repository *repo, struct got_object_id *id)
1194 const struct got_error *err;
1195 struct got_pathlist_entry *pe;
1198 /* Search pack index cache. */
1199 for (i = 0; i < repo->pack_cache_size; i++) {
1200 if (repo->packidx_cache[i] == NULL)
1202 if (!got_repo_check_packidx_bloom_filter(repo,
1203 repo->packidx_cache[i]->path_packidx, id))
1204 continue; /* object will not be found in this index */
1205 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1207 *packidx = repo->packidx_cache[i];
1209 * Move this cache entry to the front. Repeatedly
1210 * searching a wrong pack index can be expensive.
1213 memmove(&repo->packidx_cache[1],
1214 &repo->packidx_cache[0],
1215 i * sizeof(repo->packidx_cache[0]));
1216 repo->packidx_cache[0] = *packidx;
1217 if (repo->pinned_packidx >= 0 &&
1218 repo->pinned_packidx < i)
1219 repo->pinned_packidx++;
1220 else if (repo->pinned_packidx == i)
1221 repo->pinned_packidx = 0;
1226 /* No luck. Search the filesystem. */
1228 err = refresh_packidx_paths(repo);
1232 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1233 const char *path_packidx = pe->path;
1236 if (!got_repo_check_packidx_bloom_filter(repo,
1238 continue; /* object will not be found in this index */
1240 for (i = 0; i < repo->pack_cache_size; i++) {
1241 if (repo->packidx_cache[i] == NULL)
1243 if (strcmp(repo->packidx_cache[i]->path_packidx,
1244 path_packidx) == 0) {
1250 continue; /* already searched */
1252 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1257 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1261 err = cache_packidx(repo, *packidx, path_packidx);
1265 *idx = got_packidx_get_object_idx(*packidx, id);
1267 err = NULL; /* found the object */
1272 err = got_error_no_obj(id);
1277 const struct got_error *
1278 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1279 struct got_repository *repo)
1281 const struct got_error *err = NULL;
1282 DIR *packdir = NULL;
1283 struct dirent *dent;
1284 char *path_packidx = NULL;
1288 packdir_fd = openat(got_repo_get_fd(repo),
1289 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1290 if (packdir_fd == -1) {
1291 return got_error_from_errno_fmt("openat: %s/%s",
1292 got_repo_get_path_git_dir(repo),
1293 GOT_OBJECTS_PACK_DIR);
1296 packdir = fdopendir(packdir_fd);
1297 if (packdir == NULL) {
1298 err = got_error_from_errno("fdopendir");
1302 if (fstat(packdir_fd, &sb) == -1) {
1303 err = got_error_from_errno("fstat");
1306 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1307 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1309 while ((dent = readdir(packdir)) != NULL) {
1310 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1313 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1314 dent->d_name) == -1) {
1315 err = got_error_from_errno("asprintf");
1316 path_packidx = NULL;
1320 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1327 if (packdir && closedir(packdir) != 0 && err == NULL)
1328 err = got_error_from_errno("closedir");
1332 const struct got_error *
1333 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1334 struct got_repository *repo)
1336 const struct got_error *err;
1341 /* Search pack index cache. */
1342 for (i = 0; i < repo->pack_cache_size; i++) {
1343 if (repo->packidx_cache[i] == NULL)
1345 if (strcmp(repo->packidx_cache[i]->path_packidx,
1346 path_packidx) == 0) {
1347 *packidx = repo->packidx_cache[i];
1351 /* No luck. Search the filesystem. */
1353 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1358 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1362 err = cache_packidx(repo, *packidx, path_packidx);
1365 got_packidx_close(*packidx);
1371 static const struct got_error *
1372 read_packfile_hdr(int fd, struct got_packidx *packidx)
1374 const struct got_error *err = NULL;
1375 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1376 struct got_packfile_hdr hdr;
1379 n = read(fd, &hdr, sizeof(hdr));
1381 return got_error_from_errno("read");
1382 if (n != sizeof(hdr))
1383 return got_error(GOT_ERR_BAD_PACKFILE);
1385 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1386 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1387 be32toh(hdr.nobjects) != totobj)
1388 err = got_error(GOT_ERR_BAD_PACKFILE);
1393 static const struct got_error *
1394 open_packfile(int *fd, struct got_repository *repo,
1395 const char *relpath, struct got_packidx *packidx)
1397 const struct got_error *err = NULL;
1399 *fd = openat(got_repo_get_fd(repo), relpath,
1400 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1402 return got_error_from_errno_fmt("openat: %s/%s",
1403 got_repo_get_path_git_dir(repo), relpath);
1406 err = read_packfile_hdr(*fd, packidx);
1416 const struct got_error *
1417 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1418 const char *path_packfile, struct got_packidx *packidx)
1420 const struct got_error *err = NULL;
1421 struct got_pack *pack = NULL;
1428 for (i = 0; i < repo->pack_cache_size; i++) {
1429 pack = &repo->packs[i];
1430 if (pack->path_packfile == NULL)
1432 if (strcmp(pack->path_packfile, path_packfile) == 0)
1433 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1436 if (i == repo->pack_cache_size) {
1437 struct got_pack tmp;
1440 } while (i > 0 && repo->pinned_pack >= 0 &&
1441 i == repo->pinned_pack);
1442 err = got_pack_close(&repo->packs[i]);
1445 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1446 return got_error_from_errno("ftruncate");
1447 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1448 return got_error_from_errno("ftruncate");
1449 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1450 memcpy(&repo->packs[i], &repo->packs[0],
1451 sizeof(repo->packs[i]));
1452 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1453 if (repo->pinned_pack == 0)
1454 repo->pinned_pack = i;
1455 else if (repo->pinned_pack == i)
1456 repo->pinned_pack = 0;
1460 pack = &repo->packs[i];
1462 pack->path_packfile = strdup(path_packfile);
1463 if (pack->path_packfile == NULL) {
1464 err = got_error_from_errno("strdup");
1468 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1472 if (fstat(pack->fd, &sb) != 0) {
1473 err = got_error_from_errno("fstat");
1476 pack->filesize = sb.st_size;
1478 pack->privsep_child = NULL;
1480 err = got_delta_cache_alloc(&pack->delta_cache);
1484 #ifndef GOT_PACK_NO_MMAP
1485 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1486 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1488 if (pack->map == MAP_FAILED) {
1489 if (errno != ENOMEM) {
1490 err = got_error_from_errno("mmap");
1493 pack->map = NULL; /* fall back to read(2) */
1500 got_pack_close(pack);
1507 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1509 struct got_pack *pack = NULL;
1512 for (i = 0; i < repo->pack_cache_size; i++) {
1513 pack = &repo->packs[i];
1514 if (pack->path_packfile == NULL)
1516 if (strcmp(pack->path_packfile, path_packfile) == 0)
1523 const struct got_error *
1524 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1525 struct got_pack *pack)
1528 int pinned_pack = -1, pinned_packidx = -1;
1530 for (i = 0; i < repo->pack_cache_size; i++) {
1531 if (repo->packidx_cache[i] &&
1532 strcmp(repo->packidx_cache[i]->path_packidx,
1533 packidx->path_packidx) == 0)
1535 if (repo->packs[i].path_packfile &&
1536 strcmp(repo->packs[i].path_packfile,
1537 pack->path_packfile) == 0)
1541 if (pinned_packidx == -1 || pinned_pack == -1)
1542 return got_error(GOT_ERR_PIN_PACK);
1544 repo->pinned_pack = pinned_pack;
1545 repo->pinned_packidx = pinned_packidx;
1546 if (repo->packs[pinned_pack].privsep_child)
1547 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1552 got_repo_get_pinned_pack(struct got_repository *repo)
1554 if (repo->pinned_pack >= 0 &&
1555 repo->pinned_pack < repo->pack_cache_size)
1556 return &repo->packs[repo->pinned_pack];
1562 got_repo_unpin_pack(struct got_repository *repo)
1564 repo->pinned_packidx = -1;
1565 repo->pinned_pack = -1;
1566 repo->pinned_pid = 0;
1569 const struct got_error *
1570 got_repo_init(const char *repo_path, const char *head_name)
1572 const struct got_error *err = NULL;
1573 const char *dirnames[] = {
1575 GOT_OBJECTS_PACK_DIR,
1578 const char *description_str = "Unnamed repository; "
1579 "edit this file 'description' to name the repository.";
1580 const char *headref = "ref: refs/heads/";
1581 const char *gitconfig_str = "[core]\n"
1582 "\trepositoryformatversion = 0\n"
1583 "\tfilemode = true\n"
1585 char *headref_str, *path;
1588 if (!got_path_dir_is_empty(repo_path))
1589 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1591 for (i = 0; i < nitems(dirnames); i++) {
1592 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1593 return got_error_from_errno("asprintf");
1595 err = got_path_mkdir(path);
1601 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1602 return got_error_from_errno("asprintf");
1603 err = got_path_create_file(path, description_str);
1608 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1609 return got_error_from_errno("asprintf");
1610 if (asprintf(&headref_str, "%s%s", headref,
1611 head_name ? head_name : "main") == -1) {
1613 return got_error_from_errno("asprintf");
1615 err = got_path_create_file(path, headref_str);
1621 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1622 return got_error_from_errno("asprintf");
1623 err = got_path_create_file(path, gitconfig_str);
1631 static const struct got_error *
1632 match_packed_object(struct got_object_id **unique_id,
1633 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1635 const struct got_error *err = NULL;
1636 struct got_object_id_queue matched_ids;
1637 struct got_pathlist_entry *pe;
1639 STAILQ_INIT(&matched_ids);
1641 err = refresh_packidx_paths(repo);
1645 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1646 const char *path_packidx = pe->path;
1647 struct got_packidx *packidx;
1648 struct got_object_qid *qid;
1650 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1655 err = got_packidx_match_id_str_prefix(&matched_ids,
1656 packidx, id_str_prefix);
1658 got_packidx_close(packidx);
1661 err = got_packidx_close(packidx);
1665 STAILQ_FOREACH(qid, &matched_ids, entry) {
1666 if (obj_type != GOT_OBJ_TYPE_ANY) {
1668 err = got_object_get_type(&matched_type, repo,
1672 if (matched_type != obj_type)
1675 if (*unique_id == NULL) {
1676 *unique_id = got_object_id_dup(&qid->id);
1677 if (*unique_id == NULL) {
1678 err = got_error_from_errno("malloc");
1682 if (got_object_id_cmp(*unique_id,
1684 continue; /* packed multiple times */
1685 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1691 got_object_id_queue_free(&matched_ids);
1699 static const struct got_error *
1700 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1701 const char *object_dir, const char *id_str_prefix, int obj_type,
1702 struct got_repository *repo)
1704 const struct got_error *err = NULL;
1705 char *path, *id_str = NULL;
1707 struct dirent *dent;
1708 struct got_object_id id;
1710 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1711 err = got_error_from_errno("asprintf");
1715 dir = opendir(path);
1717 if (errno == ENOENT) {
1721 err = got_error_from_errno2("opendir", path);
1724 while ((dent = readdir(dir)) != NULL) {
1730 if (strcmp(dent->d_name, ".") == 0 ||
1731 strcmp(dent->d_name, "..") == 0)
1734 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1735 err = got_error_from_errno("asprintf");
1739 if (!got_parse_sha1_digest(id.hash, id_str))
1743 * Directory entries do not necessarily appear in
1744 * sorted order, so we must iterate over all of them.
1746 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1750 if (*unique_id == NULL) {
1751 if (obj_type != GOT_OBJ_TYPE_ANY) {
1753 err = got_object_get_type(&matched_type, repo,
1757 if (matched_type != obj_type)
1760 *unique_id = got_object_id_dup(&id);
1761 if (*unique_id == NULL) {
1762 err = got_error_from_errno("got_object_id_dup");
1766 if (got_object_id_cmp(*unique_id, &id) == 0)
1767 continue; /* both packed and loose */
1768 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1773 if (dir && closedir(dir) != 0 && err == NULL)
1774 err = got_error_from_errno("closedir");
1784 const struct got_error *
1785 got_repo_match_object_id_prefix(struct got_object_id **id,
1786 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1788 const struct got_error *err = NULL;
1789 char *path_objects = NULL, *object_dir = NULL;
1795 path_objects = got_repo_get_path_objects(repo);
1797 len = strlen(id_str_prefix);
1798 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1799 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1803 for (i = 0; i < len; i++) {
1804 if (isxdigit((unsigned char)id_str_prefix[i]))
1806 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1811 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1814 object_dir = strndup(id_str_prefix, 2);
1815 if (object_dir == NULL) {
1816 err = got_error_from_errno("strdup");
1819 err = match_loose_object(id, path_objects, object_dir,
1820 id_str_prefix, obj_type, repo);
1821 } else if (len == 1) {
1823 for (i = 0; i < 0xf; i++) {
1824 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1826 err = got_error_from_errno("asprintf");
1829 err = match_packed_object(id, repo, object_dir,
1833 err = match_loose_object(id, path_objects, object_dir,
1834 id_str_prefix, obj_type, repo);
1839 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1848 } else if (*id == NULL) {
1850 case GOT_OBJ_TYPE_BLOB:
1851 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1852 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1854 case GOT_OBJ_TYPE_TREE:
1855 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1856 GOT_OBJ_LABEL_TREE, id_str_prefix);
1858 case GOT_OBJ_TYPE_COMMIT:
1859 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1860 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1862 case GOT_OBJ_TYPE_TAG:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_TAG, id_str_prefix);
1867 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1875 const struct got_error *
1876 got_repo_match_object_id(struct got_object_id **id, char **label,
1877 const char *id_str, int obj_type, struct got_reflist_head *refs,
1878 struct got_repository *repo)
1880 const struct got_error *err;
1881 struct got_tag_object *tag;
1882 struct got_reference *ref = NULL;
1889 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1892 *id = got_object_id_dup(
1893 got_object_tag_get_object_id(tag));
1895 err = got_error_from_errno("got_object_id_dup");
1896 else if (label && asprintf(label, "refs/tags/%s",
1897 got_object_tag_get_name(tag)) == -1) {
1898 err = got_error_from_errno("asprintf");
1902 got_object_tag_close(tag);
1904 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1905 err->code != GOT_ERR_NO_OBJ)
1909 err = got_ref_open(&ref, repo, id_str, 0);
1911 err = got_ref_resolve(id, repo, ref);
1915 *label = strdup(got_ref_get_name(ref));
1916 if (*label == NULL) {
1917 err = got_error_from_errno("strdup");
1922 if (err->code != GOT_ERR_NOT_REF &&
1923 err->code != GOT_ERR_BAD_REF_NAME)
1925 err = got_repo_match_object_id_prefix(id, id_str,
1928 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1929 err = got_error_not_ref(id_str);
1933 err = got_object_id_str(label, *id);
1934 if (*label == NULL) {
1935 err = got_error_from_errno("strdup");
1946 const struct got_error *
1947 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1948 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1950 const struct got_error *err = NULL;
1951 struct got_reflist_entry *re;
1952 struct got_object_id *tag_id;
1953 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1957 TAILQ_FOREACH(re, refs, entry) {
1958 const char *refname;
1959 refname = got_ref_get_name(re->ref);
1960 if (got_ref_is_symbolic(re->ref))
1962 if (strncmp(refname, "refs/tags/", 10) != 0)
1964 if (!name_is_absolute)
1965 refname += strlen("refs/tags/");
1966 if (strcmp(refname, name) != 0)
1968 err = got_ref_resolve(&tag_id, repo, re->ref);
1971 err = got_object_open_as_tag(tag, repo, tag_id);
1975 if (obj_type == GOT_OBJ_TYPE_ANY ||
1976 got_object_tag_get_object_type(*tag) == obj_type)
1978 got_object_tag_close(*tag);
1982 if (err == NULL && *tag == NULL)
1983 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1984 GOT_OBJ_LABEL_TAG, name);
1988 static const struct got_error *
1989 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1990 const char *name, mode_t mode, struct got_object_id *blob_id)
1992 const struct got_error *err = NULL;
1996 *new_te = calloc(1, sizeof(**new_te));
1997 if (*new_te == NULL)
1998 return got_error_from_errno("calloc");
2000 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2001 sizeof((*new_te)->name)) {
2002 err = got_error(GOT_ERR_NO_SPACE);
2006 if (S_ISLNK(mode)) {
2007 (*new_te)->mode = S_IFLNK;
2009 (*new_te)->mode = S_IFREG;
2010 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2012 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2014 if (err && *new_te) {
2021 static const struct got_error *
2022 import_file(struct got_tree_entry **new_te, struct dirent *de,
2023 const char *path, struct got_repository *repo)
2025 const struct got_error *err;
2026 struct got_object_id *blob_id = NULL;
2030 if (asprintf(&filepath, "%s%s%s", path,
2031 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2032 return got_error_from_errno("asprintf");
2034 if (lstat(filepath, &sb) != 0) {
2035 err = got_error_from_errno2("lstat", path);
2039 err = got_object_blob_create(&blob_id, filepath, repo);
2043 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2052 static const struct got_error *
2053 insert_tree_entry(struct got_tree_entry *new_te,
2054 struct got_pathlist_head *paths)
2056 const struct got_error *err = NULL;
2057 struct got_pathlist_entry *new_pe;
2059 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2063 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2067 static const struct got_error *write_tree(struct got_object_id **,
2068 const char *, struct got_pathlist_head *, struct got_repository *,
2069 got_repo_import_cb progress_cb, void *progress_arg);
2071 static const struct got_error *
2072 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2073 const char *path, struct got_pathlist_head *ignores,
2074 struct got_repository *repo,
2075 got_repo_import_cb progress_cb, void *progress_arg)
2077 const struct got_error *err;
2078 struct got_object_id *id = NULL;
2081 if (asprintf(&subdirpath, "%s%s%s", path,
2082 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2083 return got_error_from_errno("asprintf");
2085 (*new_te) = calloc(1, sizeof(**new_te));
2086 if (*new_te == NULL)
2087 return got_error_from_errno("calloc");
2088 (*new_te)->mode = S_IFDIR;
2089 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2090 sizeof((*new_te)->name)) {
2091 err = got_error(GOT_ERR_NO_SPACE);
2094 err = write_tree(&id, subdirpath, ignores, repo,
2095 progress_cb, progress_arg);
2098 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2110 static const struct got_error *
2111 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2112 struct got_pathlist_head *ignores, struct got_repository *repo,
2113 got_repo_import_cb progress_cb, void *progress_arg)
2115 const struct got_error *err = NULL;
2119 struct got_tree_entry *new_te = NULL;
2120 struct got_pathlist_head paths;
2121 struct got_pathlist_entry *pe;
2123 *new_tree_id = NULL;
2127 dir = opendir(path_dir);
2129 err = got_error_from_errno2("opendir", path_dir);
2134 while ((de = readdir(dir)) != NULL) {
2138 if (strcmp(de->d_name, ".") == 0 ||
2139 strcmp(de->d_name, "..") == 0)
2142 TAILQ_FOREACH(pe, ignores, entry) {
2143 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2151 err = got_path_dirent_type(&type, path_dir, de);
2155 if (type == DT_DIR) {
2156 err = import_subdir(&new_te, de, path_dir,
2157 ignores, repo, progress_cb, progress_arg);
2159 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2164 } else if (type == DT_REG || type == DT_LNK) {
2165 err = import_file(&new_te, de, path_dir, repo);
2171 err = insert_tree_entry(new_te, &paths);
2177 if (TAILQ_EMPTY(&paths)) {
2178 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2179 "cannot create tree without any entries");
2183 TAILQ_FOREACH(pe, &paths, entry) {
2184 struct got_tree_entry *te = pe->data;
2186 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2188 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2189 err = got_error_from_errno("asprintf");
2192 err = (*progress_cb)(progress_arg, path);
2198 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2202 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2206 const struct got_error *
2207 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2208 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2209 struct got_repository *repo, got_repo_import_cb progress_cb,
2212 const struct got_error *err;
2213 struct got_object_id *new_tree_id;
2215 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2216 progress_cb, progress_arg);
2220 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2221 author, time(NULL), author, time(NULL), logmsg, repo);
2226 const struct got_error *
2227 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2228 struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 char *path_objects = NULL, *path = NULL;
2233 struct got_object_id id;
2239 path_objects = got_repo_get_path_objects(repo);
2240 if (path_objects == NULL)
2241 return got_error_from_errno("got_repo_get_path_objects");
2243 for (i = 0; i <= 0xff; i++) {
2244 struct dirent *dent;
2246 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2247 err = got_error_from_errno("asprintf");
2251 dir = opendir(path);
2253 if (errno == ENOENT) {
2257 err = got_error_from_errno2("opendir", path);
2261 while ((dent = readdir(dir)) != NULL) {
2266 if (strcmp(dent->d_name, ".") == 0 ||
2267 strcmp(dent->d_name, "..") == 0)
2270 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2271 err = got_error_from_errno("asprintf");
2275 if (!got_parse_sha1_digest(id.hash, id_str)) {
2281 err = got_object_open_loose_fd(&fd, &id, repo);
2285 if (fstat(fd, &sb) == -1) {
2286 err = got_error_from_errno("fstat");
2291 (*ondisk_size) += sb.st_size;
2293 if (close(fd) == -1) {
2294 err = got_error_from_errno("close");
2299 if (closedir(dir) != 0) {
2300 err = got_error_from_errno("closedir");
2309 if (dir && closedir(dir) != 0 && err == NULL)
2310 err = got_error_from_errno("closedir");
2321 const struct got_error *
2322 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2323 off_t *total_packsize, struct got_repository *repo)
2325 const struct got_error *err = NULL;
2326 DIR *packdir = NULL;
2327 struct dirent *dent;
2328 struct got_packidx *packidx = NULL;
2330 char *path_packfile;
2336 *total_packsize = 0;
2338 packdir_fd = openat(got_repo_get_fd(repo),
2339 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2340 if (packdir_fd == -1) {
2341 return got_error_from_errno_fmt("openat: %s/%s",
2342 got_repo_get_path_git_dir(repo),
2343 GOT_OBJECTS_PACK_DIR);
2346 packdir = fdopendir(packdir_fd);
2347 if (packdir == NULL) {
2348 err = got_error_from_errno("fdopendir");
2352 while ((dent = readdir(packdir)) != NULL) {
2353 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2356 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2357 dent->d_name) == -1) {
2358 err = got_error_from_errno("asprintf");
2362 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2368 if (fstat(packidx->fd, &sb) == -1)
2370 *total_packsize += sb.st_size;
2372 err = got_packidx_get_packfile_path(&path_packfile,
2373 packidx->path_packidx);
2377 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2379 free(path_packfile);
2382 free(path_packfile);
2383 *total_packsize += sb.st_size;
2385 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2389 got_packidx_close(packidx);
2394 got_packidx_close(packidx);
2395 if (packdir && closedir(packdir) != 0 && err == NULL)
2396 err = got_error_from_errno("closedir");
2400 *total_packsize = 0;
2405 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2406 got_packidx_bloom_filter_cmp);