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;
97 got_repo_get_gitconfig_author_name(struct got_repository *repo)
99 return repo->gitconfig_author_name;
103 got_repo_get_gitconfig_author_email(struct got_repository *repo)
105 return repo->gitconfig_author_email;
109 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
111 return repo->global_gitconfig_author_name;
115 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
117 return repo->global_gitconfig_author_email;
121 got_repo_get_gitconfig_owner(struct got_repository *repo)
123 return repo->gitconfig_owner;
127 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
128 struct got_repository *repo)
130 *extensions = repo->extensions;
131 *nextensions = repo->nextensions;
135 got_repo_is_bare(struct got_repository *repo)
137 return (strcmp(repo->path, repo->path_git_dir) == 0);
141 get_path_git_child(struct got_repository *repo, const char *basename)
145 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
153 got_repo_get_path_objects(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_OBJECTS_DIR);
159 got_repo_get_path_objects_pack(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
165 got_repo_get_path_refs(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_REFS_DIR);
171 got_repo_get_path_packed_refs(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
177 get_path_head(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_HEAD_FILE);
183 got_repo_get_path_gitconfig(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_GITCONFIG);
189 got_repo_get_path_gotconfig(struct got_repository *repo)
191 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
194 const struct got_gotconfig *
195 got_repo_get_gotconfig(struct got_repository *repo)
197 return repo->gotconfig;
201 got_repo_get_gitconfig_remotes(int *nremotes,
202 const struct got_remote_repo **remotes, struct got_repository *repo)
204 *nremotes = repo->ngitconfig_remotes;
205 *remotes = repo->gitconfig_remotes;
209 is_git_repo(struct got_repository *repo)
211 const char *path_git = got_repo_get_path_git_dir(repo);
212 char *path_objects = got_repo_get_path_objects(repo);
213 char *path_refs = got_repo_get_path_refs(repo);
214 char *path_head = get_path_head(repo);
217 struct got_reference *head_ref;
219 if (lstat(path_git, &sb) == -1)
221 if (!S_ISDIR(sb.st_mode))
224 if (lstat(path_objects, &sb) == -1)
226 if (!S_ISDIR(sb.st_mode))
229 if (lstat(path_refs, &sb) == -1)
231 if (!S_ISDIR(sb.st_mode))
234 if (lstat(path_head, &sb) == -1)
236 if (!S_ISREG(sb.st_mode))
239 /* Check if the HEAD reference can be opened. */
240 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
242 got_ref_close(head_ref);
253 static const struct got_error *
254 close_tempfiles(int *fds, size_t nfds)
256 const struct got_error *err = NULL;
259 for (i = 0; i < nfds; i++) {
262 if (close(fds[i]) == -1) {
263 err = got_error_from_errno("close");
271 static const struct got_error *
272 open_tempfiles(int **fds, size_t array_size, size_t nfds)
274 const struct got_error *err = NULL;
277 *fds = calloc(array_size, sizeof(**fds));
279 return got_error_from_errno("calloc");
281 for (i = 0; i < array_size; i++)
284 for (i = 0; i < nfds; i++) {
285 (*fds)[i] = got_opentempfd();
286 if ((*fds)[i] == -1) {
287 err = got_error_from_errno("got_opentempfd");
288 close_tempfiles(*fds, nfds);
297 static const struct got_error *
298 get_pack_cache_size(int *pack_cache_size)
302 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
303 return got_error_from_errno("getrlimit");
305 *pack_cache_size = GOT_PACK_CACHE_SIZE;
306 if (*pack_cache_size > rl.rlim_cur / 8)
307 *pack_cache_size = rl.rlim_cur / 8;
312 const struct got_error *
313 got_repo_pack_fds_open(int **pack_fds)
315 const struct got_error *err;
318 err = get_pack_cache_size(&nfds);
323 * We need one basefd and one accumfd per cached pack.
324 * Our constants should be set up in a way such that
325 * this error never triggers.
327 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
328 return got_error(GOT_ERR_NO_SPACE);
330 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
333 const struct got_error *
334 got_repo_pack_fds_close(int *pack_fds)
336 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
339 const struct got_error *
340 got_repo_temp_fds_open(int **temp_fds)
342 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
343 GOT_REPO_NUM_TEMPFILES);
347 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
351 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
352 repo->tempfiles[i] = temp_fds[i];
355 const struct got_error *
356 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
363 for (i = 0; i < nitems(repo->tempfiles); i++) {
364 if (repo->tempfile_use_mask & (1 << i))
366 if (repo->tempfiles[i] != -1) {
367 if (ftruncate(repo->tempfiles[i], 0L) == -1)
368 return got_error_from_errno("ftruncate");
369 *fd = repo->tempfiles[i];
371 repo->tempfile_use_mask |= (1 << i);
376 return got_error(GOT_ERR_REPO_TEMPFILE);
380 got_repo_temp_fds_put(int idx, struct got_repository *repo)
382 repo->tempfile_use_mask &= ~(1 << idx);
385 const struct got_error *
386 got_repo_temp_fds_close(int *temp_fds)
388 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
391 const struct got_error *
392 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
393 struct got_object *obj)
395 #ifndef GOT_NO_OBJ_CACHE
396 const struct got_error *err = NULL;
397 err = got_object_cache_add(&repo->objcache, id, obj);
399 if (err->code == GOT_ERR_OBJ_EXISTS ||
400 err->code == GOT_ERR_OBJ_TOO_LARGE)
410 got_repo_get_cached_object(struct got_repository *repo,
411 struct got_object_id *id)
413 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
416 const struct got_error *
417 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
418 struct got_tree_object *tree)
420 #ifndef GOT_NO_OBJ_CACHE
421 const struct got_error *err = NULL;
422 err = got_object_cache_add(&repo->treecache, id, tree);
424 if (err->code == GOT_ERR_OBJ_EXISTS ||
425 err->code == GOT_ERR_OBJ_TOO_LARGE)
434 struct got_tree_object *
435 got_repo_get_cached_tree(struct got_repository *repo,
436 struct got_object_id *id)
438 return (struct got_tree_object *)got_object_cache_get(
439 &repo->treecache, id);
442 const struct got_error *
443 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
444 struct got_commit_object *commit)
446 #ifndef GOT_NO_OBJ_CACHE
447 const struct got_error *err = NULL;
448 err = got_object_cache_add(&repo->commitcache, id, commit);
450 if (err->code == GOT_ERR_OBJ_EXISTS ||
451 err->code == GOT_ERR_OBJ_TOO_LARGE)
460 struct got_commit_object *
461 got_repo_get_cached_commit(struct got_repository *repo,
462 struct got_object_id *id)
464 return (struct got_commit_object *)got_object_cache_get(
465 &repo->commitcache, id);
468 const struct got_error *
469 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
470 struct got_tag_object *tag)
472 #ifndef GOT_NO_OBJ_CACHE
473 const struct got_error *err = NULL;
474 err = got_object_cache_add(&repo->tagcache, id, tag);
476 if (err->code == GOT_ERR_OBJ_EXISTS ||
477 err->code == GOT_ERR_OBJ_TOO_LARGE)
486 struct got_tag_object *
487 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
489 return (struct got_tag_object *)got_object_cache_get(
490 &repo->tagcache, id);
493 const struct got_error *
494 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
495 struct got_raw_object *raw)
497 #ifndef GOT_NO_OBJ_CACHE
498 const struct got_error *err = NULL;
499 err = got_object_cache_add(&repo->rawcache, id, raw);
501 if (err->code == GOT_ERR_OBJ_EXISTS ||
502 err->code == GOT_ERR_OBJ_TOO_LARGE)
512 struct got_raw_object *
513 got_repo_get_cached_raw_object(struct got_repository *repo,
514 struct got_object_id *id)
516 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
520 static const struct got_error *
521 open_repo(struct got_repository *repo, const char *path)
523 const struct got_error *err = NULL;
525 repo->gitdir_fd = -1;
527 /* bare git repository? */
528 repo->path_git_dir = strdup(path);
529 if (repo->path_git_dir == NULL)
530 return got_error_from_errno("strdup");
531 if (is_git_repo(repo)) {
532 repo->path = strdup(repo->path_git_dir);
533 if (repo->path == NULL) {
534 err = got_error_from_errno("strdup");
537 repo->gitdir_fd = open(repo->path_git_dir,
538 O_DIRECTORY | O_CLOEXEC);
539 if (repo->gitdir_fd == -1) {
540 err = got_error_from_errno2("open",
547 /* git repository with working tree? */
548 free(repo->path_git_dir);
549 repo->path_git_dir = NULL;
550 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
551 err = got_error_from_errno("asprintf");
554 if (is_git_repo(repo)) {
555 repo->path = strdup(path);
556 if (repo->path == NULL) {
557 err = got_error_from_errno("strdup");
560 repo->gitdir_fd = open(repo->path_git_dir,
561 O_DIRECTORY | O_CLOEXEC);
562 if (repo->gitdir_fd == -1) {
563 err = got_error_from_errno2("open",
570 err = got_error(GOT_ERR_NOT_GIT_REPO);
575 free(repo->path_git_dir);
576 repo->path_git_dir = NULL;
577 if (repo->gitdir_fd != -1)
578 close(repo->gitdir_fd);
579 repo->gitdir_fd = -1;
585 static const struct got_error *
586 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
588 const struct got_error *err = NULL;
589 char *repo_gitconfig_path = NULL;
590 char *object_format = NULL;
592 if (global_gitconfig_path) {
593 /* Read settings from ~/.gitconfig. */
594 int dummy_repo_version;
595 err = got_repo_read_gitconfig(&dummy_repo_version,
596 &repo->global_gitconfig_author_name,
597 &repo->global_gitconfig_author_email,
598 NULL, NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
603 /* Read repository's .git/config file. */
604 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
605 if (repo_gitconfig_path == NULL)
606 return got_error_from_errno("got_repo_get_path_gitconfig");
608 err = got_repo_read_gitconfig(
609 &repo->gitconfig_repository_format_version,
610 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
611 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
612 &repo->gitconfig_owner, &object_format,
613 &repo->extensions, &repo->nextensions,
614 repo_gitconfig_path);
619 repo->algo = GOT_HASH_SHA1;
621 if (!strcmp(object_format, "sha256"))
622 repo->algo = GOT_HASH_SHA256;
624 err = got_error_msg(GOT_ERR_OBJECT_FORMAT,
630 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
633 for (i = 0; i < repo->ngitconfig_remotes; i++) {
634 got_repo_free_remote_repo_data(
635 &repo->gitconfig_remotes[i]);
637 free(repo->gitconfig_remotes);
638 repo->gitconfig_remotes = NULL;
639 repo->ngitconfig_remotes = 0;
641 free(repo->gitconfig_author_name);
642 repo->gitconfig_author_name = NULL;
643 free(repo->gitconfig_author_email);
644 repo->gitconfig_author_email = NULL;
646 free(repo->global_gitconfig_author_name);
647 repo->global_gitconfig_author_name = NULL;
648 free(repo->global_gitconfig_author_email);
649 repo->global_gitconfig_author_email = NULL;
654 free(repo_gitconfig_path);
658 static const struct got_error *
659 read_gotconfig(struct got_repository *repo)
661 const struct got_error *err = NULL;
662 char *gotconfig_path;
664 gotconfig_path = got_repo_get_path_gotconfig(repo);
665 if (gotconfig_path == NULL)
666 return got_error_from_errno("got_repo_get_path_gotconfig");
668 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
669 free(gotconfig_path);
673 /* Supported repository format extensions. */
674 static const char *const repo_extensions[] = {
675 "noop", /* Got supports repository format version 1. */
676 "preciousObjects", /* Supported by gotadmin cleanup. */
677 "worktreeConfig", /* Got does not care about Git work trees. */
680 const struct got_error *
681 got_repo_open(struct got_repository **repop, const char *path,
682 const char *global_gitconfig_path, int *pack_fds)
684 struct got_repository *repo = NULL;
685 const struct got_error *err = NULL;
686 char *repo_path = NULL;
691 repo = calloc(1, sizeof(*repo));
693 return got_error_from_errno("calloc");
695 RB_INIT(&repo->packidx_bloom_filters);
696 TAILQ_INIT(&repo->packidx_paths);
698 for (i = 0; i < nitems(repo->privsep_children); i++) {
699 memset(&repo->privsep_children[i], 0,
700 sizeof(repo->privsep_children[0]));
701 repo->privsep_children[i].imsg_fd = -1;
704 err = got_object_cache_init(&repo->objcache,
705 GOT_OBJECT_CACHE_TYPE_OBJ);
708 err = got_object_cache_init(&repo->treecache,
709 GOT_OBJECT_CACHE_TYPE_TREE);
712 err = got_object_cache_init(&repo->commitcache,
713 GOT_OBJECT_CACHE_TYPE_COMMIT);
716 err = got_object_cache_init(&repo->tagcache,
717 GOT_OBJECT_CACHE_TYPE_TAG);
720 err = got_object_cache_init(&repo->rawcache,
721 GOT_OBJECT_CACHE_TYPE_RAW);
725 err = get_pack_cache_size(&repo->pack_cache_size);
728 for (i = 0; i < nitems(repo->packs); i++) {
729 if (pack_fds != NULL && i < repo->pack_cache_size) {
730 repo->packs[i].basefd = pack_fds[j++];
731 repo->packs[i].accumfd = pack_fds[j++];
733 repo->packs[i].basefd = -1;
734 repo->packs[i].accumfd = -1;
737 for (i = 0; i < nitems(repo->tempfiles); i++)
738 repo->tempfiles[i] = -1;
739 repo->pinned_pack = -1;
740 repo->pinned_packidx = -1;
741 repo->pinned_pid = 0;
743 repo_path = realpath(path, NULL);
744 if (repo_path == NULL) {
745 err = got_error_from_errno2("realpath", path);
752 err = open_repo(repo, repo_path);
755 if (err->code != GOT_ERR_NOT_GIT_REPO)
757 if (repo_path[0] == '/' && repo_path[1] == '\0') {
758 err = got_error(GOT_ERR_NOT_GIT_REPO);
761 err = got_path_dirname(&parent_path, repo_path);
765 repo_path = parent_path;
768 err = read_gotconfig(repo);
772 err = read_gitconfig(repo, global_gitconfig_path);
775 if (repo->gitconfig_repository_format_version > 1) {
776 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
779 if (repo->gitconfig_repository_format_version == 0 &&
780 repo->algo != GOT_HASH_SHA1) {
781 err = got_error(GOT_ERR_OBJECT_FORMAT);
784 for (i = 0; i < repo->nextensions; i++) {
785 char *ext = repo->extensions[i];
786 int j, supported = 0;
787 for (j = 0; j < nitems(repo_extensions); j++) {
788 if (strcmp(ext, repo_extensions[j]) == 0) {
794 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
799 err = got_repo_list_packidx(&repo->packidx_paths, repo);
802 got_repo_close(repo);
809 const struct got_error *
810 got_repo_close(struct got_repository *repo)
812 const struct got_error *err = NULL, *child_err;
813 struct got_packidx_bloom_filter *bf;
816 for (i = 0; i < repo->pack_cache_size; i++) {
817 if (repo->packidx_cache[i] == NULL)
819 got_packidx_close(repo->packidx_cache[i]);
822 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
823 &repo->packidx_bloom_filters))) {
824 RB_REMOVE(got_packidx_bloom_filter_tree,
825 &repo->packidx_bloom_filters, bf);
826 bloom_free(bf->bloom);
831 for (i = 0; i < repo->pack_cache_size; i++)
832 if (repo->packs[i].path_packfile)
833 if (repo->packs[i].path_packfile)
834 got_pack_close(&repo->packs[i]);
837 free(repo->path_git_dir);
839 got_object_cache_close(&repo->objcache);
840 got_object_cache_close(&repo->treecache);
841 got_object_cache_close(&repo->commitcache);
842 got_object_cache_close(&repo->tagcache);
843 got_object_cache_close(&repo->rawcache);
845 for (i = 0; i < nitems(repo->privsep_children); i++) {
846 if (repo->privsep_children[i].imsg_fd == -1)
848 imsg_clear(repo->privsep_children[i].ibuf);
849 free(repo->privsep_children[i].ibuf);
850 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
851 child_err = got_privsep_wait_for_child(
852 repo->privsep_children[i].pid);
853 if (child_err && err == NULL)
855 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
857 err = got_error_from_errno("close");
860 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
862 err = got_error_from_errno("close");
865 got_gotconfig_free(repo->gotconfig);
866 free(repo->gitconfig_author_name);
867 free(repo->gitconfig_author_email);
868 for (i = 0; i < repo->ngitconfig_remotes; i++)
869 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
870 free(repo->gitconfig_remotes);
871 for (i = 0; i < repo->nextensions; i++)
872 free(repo->extensions[i]);
873 free(repo->extensions);
875 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
882 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
888 free(repo->fetch_url);
889 repo->fetch_url = NULL;
890 free(repo->send_url);
891 repo->send_url = NULL;
892 for (i = 0; i < repo->nfetch_branches; i++)
893 free(repo->fetch_branches[i]);
894 free(repo->fetch_branches);
895 repo->fetch_branches = NULL;
896 repo->nfetch_branches = 0;
897 for (i = 0; i < repo->nsend_branches; i++)
898 free(repo->send_branches[i]);
899 free(repo->send_branches);
900 repo->send_branches = NULL;
901 repo->nsend_branches = 0;
904 const struct got_error *
905 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
906 const char *input_path)
908 const struct got_error *err = NULL;
909 const char *repo_abspath = NULL;
911 char *canonpath, *path = NULL;
913 *in_repo_path = NULL;
915 canonpath = strdup(input_path);
916 if (canonpath == NULL) {
917 err = got_error_from_errno("strdup");
920 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
924 repo_abspath = got_repo_get_path(repo);
926 if (canonpath[0] == '\0') {
927 path = strdup(canonpath);
929 err = got_error_from_errno("strdup");
933 path = realpath(canonpath, NULL);
935 if (errno != ENOENT) {
936 err = got_error_from_errno2("realpath",
941 * Path is not on disk.
942 * Assume it is already relative to repository root.
944 path = strdup(canonpath);
946 err = got_error_from_errno("strdup");
951 repolen = strlen(repo_abspath);
955 if (strcmp(path, repo_abspath) == 0) {
959 err = got_error_from_errno("strdup");
962 } else if (len > repolen &&
963 got_path_is_child(path, repo_abspath, repolen)) {
964 /* Matched an on-disk path inside repository. */
965 if (got_repo_is_bare(repo)) {
967 * Matched an on-disk path inside repository
968 * database. Treat input as repository-relative.
975 /* Strip common prefix with repository path. */
976 err = got_path_skip_common_ancestor(&child,
985 * Matched unrelated on-disk path.
986 * Treat input as repository-relative.
994 /* Make in-repository path absolute */
995 if (path[0] != '/') {
997 if (asprintf(&abspath, "/%s", path) == -1) {
998 err = got_error_from_errno("asprintf");
1010 *in_repo_path = path;
1014 static const struct got_error *
1015 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1016 const char *path_packidx)
1018 const struct got_error *err = NULL;
1021 for (i = 0; i < repo->pack_cache_size; i++) {
1022 if (repo->packidx_cache[i] == NULL)
1024 if (strcmp(repo->packidx_cache[i]->path_packidx,
1025 path_packidx) == 0) {
1026 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1029 if (i == repo->pack_cache_size) {
1032 } while (i > 0 && repo->pinned_packidx >= 0 &&
1033 i == repo->pinned_packidx);
1034 err = got_packidx_close(repo->packidx_cache[i]);
1039 repo->packidx_cache[i] = packidx;
1045 got_repo_is_packidx_filename(const char *name, size_t len)
1047 if (len != GOT_PACKIDX_NAMELEN)
1050 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1053 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1054 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1060 static struct got_packidx_bloom_filter *
1061 get_packidx_bloom_filter(struct got_repository *repo,
1062 const char *path, size_t path_len)
1064 struct got_packidx_bloom_filter key;
1066 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1067 return NULL; /* XXX */
1068 key.path_len = path_len;
1070 return RB_FIND(got_packidx_bloom_filter_tree,
1071 &repo->packidx_bloom_filters, &key);
1075 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1076 const char *path_packidx, struct got_object_id *id)
1078 struct got_packidx_bloom_filter *bf;
1080 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1082 return bloom_check(bf->bloom, id->hash, sizeof(id->hash));
1084 /* No bloom filter means this pack index must be searched. */
1088 static const struct got_error *
1089 add_packidx_bloom_filter(struct got_repository *repo,
1090 struct got_packidx *packidx, const char *path_packidx)
1092 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1093 struct got_packidx_bloom_filter *bf;
1097 * Don't use bloom filters for very large pack index files.
1098 * Large pack files will contain a relatively large fraction
1099 * of our objects so we will likely need to visit them anyway.
1100 * The more objects a pack file contains the higher the probability
1101 * of a false-positive match from the bloom filter. And reading
1102 * all object IDs from a large pack index file can be expensive.
1104 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1107 /* Do we already have a filter for this pack index? */
1108 if (get_packidx_bloom_filter(repo, path_packidx,
1109 strlen(path_packidx)) != NULL)
1112 bf = calloc(1, sizeof(*bf));
1114 return got_error_from_errno("calloc");
1115 bf->bloom = calloc(1, sizeof(*bf->bloom));
1116 if (bf->bloom == NULL) {
1118 return got_error_from_errno("calloc");
1121 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1122 if (len >= sizeof(bf->path)) {
1125 return got_error(GOT_ERR_NO_SPACE);
1129 /* Minimum size supported by our bloom filter is 1000 entries. */
1130 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1131 for (i = 0; i < nobjects; i++) {
1132 struct got_packidx_object_id *id;
1133 id = &packidx->hdr.sorted_ids[i];
1134 bloom_add(bf->bloom, id->hash, sizeof(id->hash));
1137 RB_INSERT(got_packidx_bloom_filter_tree,
1138 &repo->packidx_bloom_filters, bf);
1143 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1145 struct got_pathlist_entry *pe;
1147 while (!TAILQ_EMPTY(packidx_paths)) {
1148 pe = TAILQ_FIRST(packidx_paths);
1149 TAILQ_REMOVE(packidx_paths, pe, entry);
1150 free((char *)pe->path);
1155 static const struct got_error *
1156 refresh_packidx_paths(struct got_repository *repo)
1158 const struct got_error *err = NULL;
1159 char *objects_pack_dir = NULL;
1162 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1163 if (objects_pack_dir == NULL)
1164 return got_error_from_errno("got_repo_get_path_objects_pack");
1166 if (stat(objects_pack_dir, &sb) == -1) {
1167 if (errno != ENOENT) {
1168 err = got_error_from_errno2("stat", objects_pack_dir);
1171 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1172 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1173 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1174 purge_packidx_paths(&repo->packidx_paths);
1175 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1180 free(objects_pack_dir);
1184 const struct got_error *
1185 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1186 struct got_repository *repo, struct got_object_id *id)
1188 const struct got_error *err;
1189 struct got_pathlist_entry *pe;
1192 /* Search pack index cache. */
1193 for (i = 0; i < repo->pack_cache_size; i++) {
1194 if (repo->packidx_cache[i] == NULL)
1196 if (!got_repo_check_packidx_bloom_filter(repo,
1197 repo->packidx_cache[i]->path_packidx, id))
1198 continue; /* object will not be found in this index */
1199 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1201 *packidx = repo->packidx_cache[i];
1203 * Move this cache entry to the front. Repeatedly
1204 * searching a wrong pack index can be expensive.
1207 memmove(&repo->packidx_cache[1],
1208 &repo->packidx_cache[0],
1209 i * sizeof(repo->packidx_cache[0]));
1210 repo->packidx_cache[0] = *packidx;
1211 if (repo->pinned_packidx >= 0 &&
1212 repo->pinned_packidx < i)
1213 repo->pinned_packidx++;
1214 else if (repo->pinned_packidx == i)
1215 repo->pinned_packidx = 0;
1220 /* No luck. Search the filesystem. */
1222 err = refresh_packidx_paths(repo);
1226 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1227 const char *path_packidx = pe->path;
1230 if (!got_repo_check_packidx_bloom_filter(repo,
1232 continue; /* object will not be found in this index */
1234 for (i = 0; i < repo->pack_cache_size; i++) {
1235 if (repo->packidx_cache[i] == NULL)
1237 if (strcmp(repo->packidx_cache[i]->path_packidx,
1238 path_packidx) == 0) {
1244 continue; /* already searched */
1246 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1251 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1255 err = cache_packidx(repo, *packidx, path_packidx);
1259 *idx = got_packidx_get_object_idx(*packidx, id);
1261 err = NULL; /* found the object */
1266 err = got_error_no_obj(id);
1271 const struct got_error *
1272 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 DIR *packdir = NULL;
1277 struct dirent *dent;
1278 char *path_packidx = NULL;
1282 packdir_fd = openat(got_repo_get_fd(repo),
1283 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1284 if (packdir_fd == -1) {
1285 return got_error_from_errno_fmt("openat: %s/%s",
1286 got_repo_get_path_git_dir(repo),
1287 GOT_OBJECTS_PACK_DIR);
1290 packdir = fdopendir(packdir_fd);
1291 if (packdir == NULL) {
1292 err = got_error_from_errno("fdopendir");
1296 if (fstat(packdir_fd, &sb) == -1) {
1297 err = got_error_from_errno("fstat");
1300 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1301 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1303 while ((dent = readdir(packdir)) != NULL) {
1304 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1307 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1308 dent->d_name) == -1) {
1309 err = got_error_from_errno("asprintf");
1310 path_packidx = NULL;
1314 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1321 if (packdir && closedir(packdir) != 0 && err == NULL)
1322 err = got_error_from_errno("closedir");
1326 const struct got_error *
1327 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1328 struct got_repository *repo)
1330 const struct got_error *err;
1335 /* Search pack index cache. */
1336 for (i = 0; i < repo->pack_cache_size; i++) {
1337 if (repo->packidx_cache[i] == NULL)
1339 if (strcmp(repo->packidx_cache[i]->path_packidx,
1340 path_packidx) == 0) {
1341 *packidx = repo->packidx_cache[i];
1345 /* No luck. Search the filesystem. */
1347 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1352 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1356 err = cache_packidx(repo, *packidx, path_packidx);
1359 got_packidx_close(*packidx);
1365 static const struct got_error *
1366 read_packfile_hdr(int fd, struct got_packidx *packidx)
1368 const struct got_error *err = NULL;
1369 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1370 struct got_packfile_hdr hdr;
1373 n = read(fd, &hdr, sizeof(hdr));
1375 return got_error_from_errno("read");
1376 if (n != sizeof(hdr))
1377 return got_error(GOT_ERR_BAD_PACKFILE);
1379 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1380 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1381 be32toh(hdr.nobjects) != totobj)
1382 err = got_error(GOT_ERR_BAD_PACKFILE);
1387 static const struct got_error *
1388 open_packfile(int *fd, struct got_repository *repo,
1389 const char *relpath, struct got_packidx *packidx)
1391 const struct got_error *err = NULL;
1393 *fd = openat(got_repo_get_fd(repo), relpath,
1394 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1396 return got_error_from_errno_fmt("openat: %s/%s",
1397 got_repo_get_path_git_dir(repo), relpath);
1400 err = read_packfile_hdr(*fd, packidx);
1410 const struct got_error *
1411 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1412 const char *path_packfile, struct got_packidx *packidx)
1414 const struct got_error *err = NULL;
1415 struct got_pack *pack = NULL;
1422 for (i = 0; i < repo->pack_cache_size; i++) {
1423 pack = &repo->packs[i];
1424 if (pack->path_packfile == NULL)
1426 if (strcmp(pack->path_packfile, path_packfile) == 0)
1427 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1430 if (i == repo->pack_cache_size) {
1431 struct got_pack tmp;
1434 } while (i > 0 && repo->pinned_pack >= 0 &&
1435 i == repo->pinned_pack);
1436 err = got_pack_close(&repo->packs[i]);
1439 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1440 return got_error_from_errno("ftruncate");
1441 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1442 return got_error_from_errno("ftruncate");
1443 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1444 memcpy(&repo->packs[i], &repo->packs[0],
1445 sizeof(repo->packs[i]));
1446 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1447 if (repo->pinned_pack == 0)
1448 repo->pinned_pack = i;
1449 else if (repo->pinned_pack == i)
1450 repo->pinned_pack = 0;
1454 pack = &repo->packs[i];
1456 pack->path_packfile = strdup(path_packfile);
1457 if (pack->path_packfile == NULL) {
1458 err = got_error_from_errno("strdup");
1462 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1466 if (fstat(pack->fd, &sb) != 0) {
1467 err = got_error_from_errno("fstat");
1470 pack->filesize = sb.st_size;
1472 pack->privsep_child = NULL;
1474 err = got_delta_cache_alloc(&pack->delta_cache);
1478 #ifndef GOT_PACK_NO_MMAP
1479 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1480 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1482 if (pack->map == MAP_FAILED) {
1483 if (errno != ENOMEM) {
1484 err = got_error_from_errno("mmap");
1487 pack->map = NULL; /* fall back to read(2) */
1494 got_pack_close(pack);
1501 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1503 struct got_pack *pack = NULL;
1506 for (i = 0; i < repo->pack_cache_size; i++) {
1507 pack = &repo->packs[i];
1508 if (pack->path_packfile == NULL)
1510 if (strcmp(pack->path_packfile, path_packfile) == 0)
1517 const struct got_error *
1518 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1519 struct got_pack *pack)
1522 int pinned_pack = -1, pinned_packidx = -1;
1524 for (i = 0; i < repo->pack_cache_size; i++) {
1525 if (repo->packidx_cache[i] &&
1526 strcmp(repo->packidx_cache[i]->path_packidx,
1527 packidx->path_packidx) == 0)
1529 if (repo->packs[i].path_packfile &&
1530 strcmp(repo->packs[i].path_packfile,
1531 pack->path_packfile) == 0)
1535 if (pinned_packidx == -1 || pinned_pack == -1)
1536 return got_error(GOT_ERR_PIN_PACK);
1538 repo->pinned_pack = pinned_pack;
1539 repo->pinned_packidx = pinned_packidx;
1540 if (repo->packs[pinned_pack].privsep_child)
1541 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1546 got_repo_get_pinned_pack(struct got_repository *repo)
1548 if (repo->pinned_pack >= 0 &&
1549 repo->pinned_pack < repo->pack_cache_size)
1550 return &repo->packs[repo->pinned_pack];
1556 got_repo_unpin_pack(struct got_repository *repo)
1558 repo->pinned_packidx = -1;
1559 repo->pinned_pack = -1;
1560 repo->pinned_pid = 0;
1563 const struct got_error *
1564 got_repo_init(const char *repo_path, const char *head_name)
1566 const struct got_error *err = NULL;
1567 const char *dirnames[] = {
1569 GOT_OBJECTS_PACK_DIR,
1572 const char *description_str = "Unnamed repository; "
1573 "edit this file 'description' to name the repository.";
1574 const char *headref = "ref: refs/heads/";
1575 const char *gitconfig_str = "[core]\n"
1576 "\trepositoryformatversion = 0\n"
1577 "\tfilemode = true\n"
1579 char *headref_str, *path;
1582 if (!got_path_dir_is_empty(repo_path))
1583 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1585 for (i = 0; i < nitems(dirnames); i++) {
1586 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1587 return got_error_from_errno("asprintf");
1589 err = got_path_mkdir(path);
1595 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1596 return got_error_from_errno("asprintf");
1597 err = got_path_create_file(path, description_str);
1602 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1603 return got_error_from_errno("asprintf");
1604 if (asprintf(&headref_str, "%s%s", headref,
1605 head_name ? head_name : "main") == -1) {
1607 return got_error_from_errno("asprintf");
1609 err = got_path_create_file(path, headref_str);
1615 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1616 return got_error_from_errno("asprintf");
1617 err = got_path_create_file(path, gitconfig_str);
1625 static const struct got_error *
1626 match_packed_object(struct got_object_id **unique_id,
1627 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1629 const struct got_error *err = NULL;
1630 struct got_object_id_queue matched_ids;
1631 struct got_pathlist_entry *pe;
1633 STAILQ_INIT(&matched_ids);
1635 err = refresh_packidx_paths(repo);
1639 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1640 const char *path_packidx = pe->path;
1641 struct got_packidx *packidx;
1642 struct got_object_qid *qid;
1644 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1649 err = got_packidx_match_id_str_prefix(&matched_ids,
1650 packidx, id_str_prefix);
1652 got_packidx_close(packidx);
1655 err = got_packidx_close(packidx);
1659 STAILQ_FOREACH(qid, &matched_ids, entry) {
1660 if (obj_type != GOT_OBJ_TYPE_ANY) {
1662 err = got_object_get_type(&matched_type, repo,
1666 if (matched_type != obj_type)
1669 if (*unique_id == NULL) {
1670 *unique_id = got_object_id_dup(&qid->id);
1671 if (*unique_id == NULL) {
1672 err = got_error_from_errno("malloc");
1676 if (got_object_id_cmp(*unique_id,
1678 continue; /* packed multiple times */
1679 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1685 got_object_id_queue_free(&matched_ids);
1693 static const struct got_error *
1694 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1695 const char *object_dir, const char *id_str_prefix, int obj_type,
1696 struct got_repository *repo)
1698 const struct got_error *err = NULL;
1699 char *path, *id_str = NULL;
1701 struct dirent *dent;
1702 struct got_object_id id;
1704 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1705 err = got_error_from_errno("asprintf");
1709 dir = opendir(path);
1711 if (errno == ENOENT) {
1715 err = got_error_from_errno2("opendir", path);
1718 while ((dent = readdir(dir)) != NULL) {
1724 if (strcmp(dent->d_name, ".") == 0 ||
1725 strcmp(dent->d_name, "..") == 0)
1728 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1729 err = got_error_from_errno("asprintf");
1733 if (!got_parse_sha1_digest(id.hash, id_str))
1737 * Directory entries do not necessarily appear in
1738 * sorted order, so we must iterate over all of them.
1740 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1744 if (*unique_id == NULL) {
1745 if (obj_type != GOT_OBJ_TYPE_ANY) {
1747 err = got_object_get_type(&matched_type, repo,
1751 if (matched_type != obj_type)
1754 *unique_id = got_object_id_dup(&id);
1755 if (*unique_id == NULL) {
1756 err = got_error_from_errno("got_object_id_dup");
1760 if (got_object_id_cmp(*unique_id, &id) == 0)
1761 continue; /* both packed and loose */
1762 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1767 if (dir && closedir(dir) != 0 && err == NULL)
1768 err = got_error_from_errno("closedir");
1778 const struct got_error *
1779 got_repo_match_object_id_prefix(struct got_object_id **id,
1780 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1782 const struct got_error *err = NULL;
1783 char *path_objects = NULL, *object_dir = NULL;
1789 path_objects = got_repo_get_path_objects(repo);
1791 len = strlen(id_str_prefix);
1792 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1793 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1797 for (i = 0; i < len; i++) {
1798 if (isxdigit((unsigned char)id_str_prefix[i]))
1800 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1805 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1808 object_dir = strndup(id_str_prefix, 2);
1809 if (object_dir == NULL) {
1810 err = got_error_from_errno("strdup");
1813 err = match_loose_object(id, path_objects, object_dir,
1814 id_str_prefix, obj_type, repo);
1815 } else if (len == 1) {
1817 for (i = 0; i < 0xf; i++) {
1818 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1820 err = got_error_from_errno("asprintf");
1823 err = match_packed_object(id, repo, object_dir,
1827 err = match_loose_object(id, path_objects, object_dir,
1828 id_str_prefix, obj_type, repo);
1833 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1842 } else if (*id == NULL) {
1844 case GOT_OBJ_TYPE_BLOB:
1845 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1846 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1848 case GOT_OBJ_TYPE_TREE:
1849 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1850 GOT_OBJ_LABEL_TREE, id_str_prefix);
1852 case GOT_OBJ_TYPE_COMMIT:
1853 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1854 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1856 case GOT_OBJ_TYPE_TAG:
1857 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1858 GOT_OBJ_LABEL_TAG, id_str_prefix);
1861 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1869 const struct got_error *
1870 got_repo_match_object_id(struct got_object_id **id, char **label,
1871 const char *id_str, int obj_type, struct got_reflist_head *refs,
1872 struct got_repository *repo)
1874 const struct got_error *err;
1875 struct got_tag_object *tag;
1876 struct got_reference *ref = NULL;
1883 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1886 *id = got_object_id_dup(
1887 got_object_tag_get_object_id(tag));
1889 err = got_error_from_errno("got_object_id_dup");
1890 else if (label && asprintf(label, "refs/tags/%s",
1891 got_object_tag_get_name(tag)) == -1) {
1892 err = got_error_from_errno("asprintf");
1896 got_object_tag_close(tag);
1898 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1899 err->code != GOT_ERR_NO_OBJ)
1903 err = got_ref_open(&ref, repo, id_str, 0);
1905 err = got_ref_resolve(id, repo, ref);
1909 *label = strdup(got_ref_get_name(ref));
1910 if (*label == NULL) {
1911 err = got_error_from_errno("strdup");
1916 if (err->code != GOT_ERR_NOT_REF &&
1917 err->code != GOT_ERR_BAD_REF_NAME)
1919 err = got_repo_match_object_id_prefix(id, id_str,
1922 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1923 err = got_error_not_ref(id_str);
1927 err = got_object_id_str(label, *id);
1928 if (*label == NULL) {
1929 err = got_error_from_errno("strdup");
1940 const struct got_error *
1941 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1942 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1944 const struct got_error *err = NULL;
1945 struct got_reflist_entry *re;
1946 struct got_object_id *tag_id;
1947 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1951 TAILQ_FOREACH(re, refs, entry) {
1952 const char *refname;
1953 refname = got_ref_get_name(re->ref);
1954 if (got_ref_is_symbolic(re->ref))
1956 if (strncmp(refname, "refs/tags/", 10) != 0)
1958 if (!name_is_absolute)
1959 refname += strlen("refs/tags/");
1960 if (strcmp(refname, name) != 0)
1962 err = got_ref_resolve(&tag_id, repo, re->ref);
1965 err = got_object_open_as_tag(tag, repo, tag_id);
1969 if (obj_type == GOT_OBJ_TYPE_ANY ||
1970 got_object_tag_get_object_type(*tag) == obj_type)
1972 got_object_tag_close(*tag);
1976 if (err == NULL && *tag == NULL)
1977 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1978 GOT_OBJ_LABEL_TAG, name);
1982 static const struct got_error *
1983 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1984 const char *name, mode_t mode, struct got_object_id *blob_id)
1986 const struct got_error *err = NULL;
1990 *new_te = calloc(1, sizeof(**new_te));
1991 if (*new_te == NULL)
1992 return got_error_from_errno("calloc");
1994 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1995 sizeof((*new_te)->name)) {
1996 err = got_error(GOT_ERR_NO_SPACE);
2000 if (S_ISLNK(mode)) {
2001 (*new_te)->mode = S_IFLNK;
2003 (*new_te)->mode = S_IFREG;
2004 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2006 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2008 if (err && *new_te) {
2015 static const struct got_error *
2016 import_file(struct got_tree_entry **new_te, struct dirent *de,
2017 const char *path, struct got_repository *repo)
2019 const struct got_error *err;
2020 struct got_object_id *blob_id = NULL;
2024 if (asprintf(&filepath, "%s%s%s", path,
2025 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2026 return got_error_from_errno("asprintf");
2028 if (lstat(filepath, &sb) != 0) {
2029 err = got_error_from_errno2("lstat", path);
2033 err = got_object_blob_create(&blob_id, filepath, repo);
2037 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2046 static const struct got_error *
2047 insert_tree_entry(struct got_tree_entry *new_te,
2048 struct got_pathlist_head *paths)
2050 const struct got_error *err = NULL;
2051 struct got_pathlist_entry *new_pe;
2053 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2057 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2061 static const struct got_error *write_tree(struct got_object_id **,
2062 const char *, struct got_pathlist_head *, struct got_repository *,
2063 got_repo_import_cb progress_cb, void *progress_arg);
2065 static const struct got_error *
2066 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2067 const char *path, struct got_pathlist_head *ignores,
2068 struct got_repository *repo,
2069 got_repo_import_cb progress_cb, void *progress_arg)
2071 const struct got_error *err;
2072 struct got_object_id *id = NULL;
2075 if (asprintf(&subdirpath, "%s%s%s", path,
2076 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2077 return got_error_from_errno("asprintf");
2079 (*new_te) = calloc(1, sizeof(**new_te));
2080 if (*new_te == NULL)
2081 return got_error_from_errno("calloc");
2082 (*new_te)->mode = S_IFDIR;
2083 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2084 sizeof((*new_te)->name)) {
2085 err = got_error(GOT_ERR_NO_SPACE);
2088 err = write_tree(&id, subdirpath, ignores, repo,
2089 progress_cb, progress_arg);
2092 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2104 static const struct got_error *
2105 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2106 struct got_pathlist_head *ignores, struct got_repository *repo,
2107 got_repo_import_cb progress_cb, void *progress_arg)
2109 const struct got_error *err = NULL;
2113 struct got_tree_entry *new_te = NULL;
2114 struct got_pathlist_head paths;
2115 struct got_pathlist_entry *pe;
2117 *new_tree_id = NULL;
2121 dir = opendir(path_dir);
2123 err = got_error_from_errno2("opendir", path_dir);
2128 while ((de = readdir(dir)) != NULL) {
2132 if (strcmp(de->d_name, ".") == 0 ||
2133 strcmp(de->d_name, "..") == 0)
2136 TAILQ_FOREACH(pe, ignores, entry) {
2137 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2145 err = got_path_dirent_type(&type, path_dir, de);
2149 if (type == DT_DIR) {
2150 err = import_subdir(&new_te, de, path_dir,
2151 ignores, repo, progress_cb, progress_arg);
2153 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2158 } else if (type == DT_REG || type == DT_LNK) {
2159 err = import_file(&new_te, de, path_dir, repo);
2165 err = insert_tree_entry(new_te, &paths);
2171 if (TAILQ_EMPTY(&paths)) {
2172 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2173 "cannot create tree without any entries");
2177 TAILQ_FOREACH(pe, &paths, entry) {
2178 struct got_tree_entry *te = pe->data;
2180 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2182 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2183 err = got_error_from_errno("asprintf");
2186 err = (*progress_cb)(progress_arg, path);
2192 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2196 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2200 const struct got_error *
2201 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2202 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2203 struct got_repository *repo, got_repo_import_cb progress_cb,
2206 const struct got_error *err;
2207 struct got_object_id *new_tree_id;
2209 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2210 progress_cb, progress_arg);
2214 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2215 author, time(NULL), author, time(NULL), logmsg, repo);
2220 const struct got_error *
2221 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2222 struct got_repository *repo)
2224 const struct got_error *err = NULL;
2225 char *path_objects = NULL, *path = NULL;
2227 struct got_object_id id;
2233 path_objects = got_repo_get_path_objects(repo);
2234 if (path_objects == NULL)
2235 return got_error_from_errno("got_repo_get_path_objects");
2237 for (i = 0; i <= 0xff; i++) {
2238 struct dirent *dent;
2240 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2241 err = got_error_from_errno("asprintf");
2245 dir = opendir(path);
2247 if (errno == ENOENT) {
2251 err = got_error_from_errno2("opendir", path);
2255 while ((dent = readdir(dir)) != NULL) {
2260 if (strcmp(dent->d_name, ".") == 0 ||
2261 strcmp(dent->d_name, "..") == 0)
2264 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2265 err = got_error_from_errno("asprintf");
2269 if (!got_parse_sha1_digest(id.hash, id_str)) {
2275 err = got_object_open_loose_fd(&fd, &id, repo);
2279 if (fstat(fd, &sb) == -1) {
2280 err = got_error_from_errno("fstat");
2285 (*ondisk_size) += sb.st_size;
2287 if (close(fd) == -1) {
2288 err = got_error_from_errno("close");
2293 if (closedir(dir) != 0) {
2294 err = got_error_from_errno("closedir");
2303 if (dir && closedir(dir) != 0 && err == NULL)
2304 err = got_error_from_errno("closedir");
2315 const struct got_error *
2316 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2317 off_t *total_packsize, struct got_repository *repo)
2319 const struct got_error *err = NULL;
2320 DIR *packdir = NULL;
2321 struct dirent *dent;
2322 struct got_packidx *packidx = NULL;
2324 char *path_packfile;
2330 *total_packsize = 0;
2332 packdir_fd = openat(got_repo_get_fd(repo),
2333 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2334 if (packdir_fd == -1) {
2335 return got_error_from_errno_fmt("openat: %s/%s",
2336 got_repo_get_path_git_dir(repo),
2337 GOT_OBJECTS_PACK_DIR);
2340 packdir = fdopendir(packdir_fd);
2341 if (packdir == NULL) {
2342 err = got_error_from_errno("fdopendir");
2346 while ((dent = readdir(packdir)) != NULL) {
2347 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2350 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2351 dent->d_name) == -1) {
2352 err = got_error_from_errno("asprintf");
2356 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2362 if (fstat(packidx->fd, &sb) == -1)
2364 *total_packsize += sb.st_size;
2366 err = got_packidx_get_packfile_path(&path_packfile,
2367 packidx->path_packidx);
2371 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2373 free(path_packfile);
2376 free(path_packfile);
2377 *total_packsize += sb.st_size;
2379 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2383 got_packidx_close(packidx);
2388 got_packidx_close(packidx);
2389 if (packdir && closedir(packdir) != 0 && err == NULL)
2390 err = got_error_from_errno("closedir");
2394 *total_packsize = 0;
2399 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2400 got_packidx_bloom_filter_cmp);