2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
77 got_repo_get_path(struct got_repository *repo)
83 got_repo_get_path_git_dir(struct got_repository *repo)
85 return repo->path_git_dir;
89 got_repo_get_fd(struct got_repository *repo)
91 return repo->gitdir_fd;
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 return repo->gitconfig_author_name;
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 get_path_git_child(struct got_repository *repo, const char *basename)
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_objects, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_refs, &sb) == -1)
229 if (!S_ISDIR(sb.st_mode))
232 if (lstat(path_head, &sb) == -1)
234 if (!S_ISREG(sb.st_mode))
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 got_ref_close(head_ref);
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 (*pack_fds)[i] = got_opentempfd();
272 if ((*pack_fds)[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(*pack_fds);
283 const struct got_error *
284 got_repo_pack_fds_close(int *pack_fds)
286 const struct got_error *err = NULL;
289 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
290 if (pack_fds[i] == -1)
292 if (close(pack_fds[i]) == -1) {
293 err = got_error_from_errno("close");
301 const struct got_error *
302 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
303 struct got_object *obj)
305 #ifndef GOT_NO_OBJ_CACHE
306 const struct got_error *err = NULL;
307 err = got_object_cache_add(&repo->objcache, id, obj);
309 if (err->code == GOT_ERR_OBJ_EXISTS ||
310 err->code == GOT_ERR_OBJ_TOO_LARGE)
320 got_repo_get_cached_object(struct got_repository *repo,
321 struct got_object_id *id)
323 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
326 const struct got_error *
327 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
328 struct got_tree_object *tree)
330 #ifndef GOT_NO_OBJ_CACHE
331 const struct got_error *err = NULL;
332 err = got_object_cache_add(&repo->treecache, id, tree);
334 if (err->code == GOT_ERR_OBJ_EXISTS ||
335 err->code == GOT_ERR_OBJ_TOO_LARGE)
344 struct got_tree_object *
345 got_repo_get_cached_tree(struct got_repository *repo,
346 struct got_object_id *id)
348 return (struct got_tree_object *)got_object_cache_get(
349 &repo->treecache, id);
352 const struct got_error *
353 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
354 struct got_commit_object *commit)
356 #ifndef GOT_NO_OBJ_CACHE
357 const struct got_error *err = NULL;
358 err = got_object_cache_add(&repo->commitcache, id, commit);
360 if (err->code == GOT_ERR_OBJ_EXISTS ||
361 err->code == GOT_ERR_OBJ_TOO_LARGE)
370 struct got_commit_object *
371 got_repo_get_cached_commit(struct got_repository *repo,
372 struct got_object_id *id)
374 return (struct got_commit_object *)got_object_cache_get(
375 &repo->commitcache, id);
378 const struct got_error *
379 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
380 struct got_tag_object *tag)
382 #ifndef GOT_NO_OBJ_CACHE
383 const struct got_error *err = NULL;
384 err = got_object_cache_add(&repo->tagcache, id, tag);
386 if (err->code == GOT_ERR_OBJ_EXISTS ||
387 err->code == GOT_ERR_OBJ_TOO_LARGE)
396 struct got_tag_object *
397 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
399 return (struct got_tag_object *)got_object_cache_get(
400 &repo->tagcache, id);
403 const struct got_error *
404 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
405 struct got_raw_object *raw)
407 #ifndef GOT_NO_OBJ_CACHE
408 const struct got_error *err = NULL;
409 err = got_object_cache_add(&repo->rawcache, id, raw);
411 if (err->code == GOT_ERR_OBJ_EXISTS ||
412 err->code == GOT_ERR_OBJ_TOO_LARGE)
422 struct got_raw_object *
423 got_repo_get_cached_raw_object(struct got_repository *repo,
424 struct got_object_id *id)
426 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
430 static const struct got_error *
431 open_repo(struct got_repository *repo, const char *path)
433 const struct got_error *err = NULL;
435 repo->gitdir_fd = -1;
437 /* bare git repository? */
438 repo->path_git_dir = strdup(path);
439 if (repo->path_git_dir == NULL)
440 return got_error_from_errno("strdup");
441 if (is_git_repo(repo)) {
442 repo->path = strdup(repo->path_git_dir);
443 if (repo->path == NULL) {
444 err = got_error_from_errno("strdup");
447 repo->gitdir_fd = open(repo->path_git_dir,
448 O_DIRECTORY | O_CLOEXEC);
449 if (repo->gitdir_fd == -1) {
450 err = got_error_from_errno2("open",
457 /* git repository with working tree? */
458 free(repo->path_git_dir);
459 repo->path_git_dir = NULL;
460 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
461 err = got_error_from_errno("asprintf");
464 if (is_git_repo(repo)) {
465 repo->path = strdup(path);
466 if (repo->path == NULL) {
467 err = got_error_from_errno("strdup");
470 repo->gitdir_fd = open(repo->path_git_dir,
471 O_DIRECTORY | O_CLOEXEC);
472 if (repo->gitdir_fd == -1) {
473 err = got_error_from_errno2("open",
480 err = got_error(GOT_ERR_NOT_GIT_REPO);
485 free(repo->path_git_dir);
486 repo->path_git_dir = NULL;
487 if (repo->gitdir_fd != -1)
488 close(repo->gitdir_fd);
489 repo->gitdir_fd = -1;
495 static const struct got_error *
496 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
498 const struct got_error *err = NULL;
499 char *repo_gitconfig_path = NULL;
501 if (global_gitconfig_path) {
502 /* Read settings from ~/.gitconfig. */
503 int dummy_repo_version;
504 err = got_repo_read_gitconfig(&dummy_repo_version,
505 &repo->global_gitconfig_author_name,
506 &repo->global_gitconfig_author_email,
507 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
512 /* Read repository's .git/config file. */
513 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
514 if (repo_gitconfig_path == NULL)
515 return got_error_from_errno("got_repo_get_path_gitconfig");
517 err = got_repo_read_gitconfig(
518 &repo->gitconfig_repository_format_version,
519 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
520 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
521 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
522 repo_gitconfig_path);
526 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
529 for (i = 0; i < repo->ngitconfig_remotes; i++) {
530 got_repo_free_remote_repo_data(
531 &repo->gitconfig_remotes[i]);
533 free(repo->gitconfig_remotes);
534 repo->gitconfig_remotes = NULL;
535 repo->ngitconfig_remotes = 0;
537 free(repo->gitconfig_author_name);
538 repo->gitconfig_author_name = NULL;
539 free(repo->gitconfig_author_email);
540 repo->gitconfig_author_email = NULL;
542 free(repo->global_gitconfig_author_name);
543 repo->global_gitconfig_author_name = NULL;
544 free(repo->global_gitconfig_author_email);
545 repo->global_gitconfig_author_email = NULL;
549 free(repo_gitconfig_path);
553 static const struct got_error *
554 read_gotconfig(struct got_repository *repo)
556 const struct got_error *err = NULL;
557 char *gotconfig_path;
559 gotconfig_path = got_repo_get_path_gotconfig(repo);
560 if (gotconfig_path == NULL)
561 return got_error_from_errno("got_repo_get_path_gotconfig");
563 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
564 free(gotconfig_path);
568 /* Supported repository format extensions. */
569 static const char *const repo_extensions[] = {
570 "noop", /* Got supports repository format version 1. */
571 "preciousObjects", /* Supported by gotadmin cleanup. */
572 "worktreeConfig", /* Got does not care about Git work trees. */
575 const struct got_error *
576 got_repo_open(struct got_repository **repop, const char *path,
577 const char *global_gitconfig_path, int *pack_fds)
579 struct got_repository *repo = NULL;
580 const struct got_error *err = NULL;
581 char *repo_path = NULL;
587 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
588 return got_error_from_errno("getrlimit");
590 repo = calloc(1, sizeof(*repo));
592 return got_error_from_errno("calloc");
594 RB_INIT(&repo->packidx_bloom_filters);
595 TAILQ_INIT(&repo->packidx_paths);
597 for (i = 0; i < nitems(repo->privsep_children); i++) {
598 memset(&repo->privsep_children[i], 0,
599 sizeof(repo->privsep_children[0]));
600 repo->privsep_children[i].imsg_fd = -1;
603 err = got_object_cache_init(&repo->objcache,
604 GOT_OBJECT_CACHE_TYPE_OBJ);
607 err = got_object_cache_init(&repo->treecache,
608 GOT_OBJECT_CACHE_TYPE_TREE);
611 err = got_object_cache_init(&repo->commitcache,
612 GOT_OBJECT_CACHE_TYPE_COMMIT);
615 err = got_object_cache_init(&repo->tagcache,
616 GOT_OBJECT_CACHE_TYPE_TAG);
619 err = got_object_cache_init(&repo->rawcache,
620 GOT_OBJECT_CACHE_TYPE_RAW);
624 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
625 if (repo->pack_cache_size > rl.rlim_cur / 8)
626 repo->pack_cache_size = rl.rlim_cur / 8;
627 for (i = 0; i < nitems(repo->packs); i++) {
628 if (i < repo->pack_cache_size) {
629 repo->packs[i].basefd = pack_fds[j++];
630 repo->packs[i].accumfd = pack_fds[j++];
632 repo->packs[i].basefd = -1;
633 repo->packs[i].accumfd = -1;
636 repo->pinned_pack = -1;
637 repo->pinned_packidx = -1;
638 repo->pinned_pid = 0;
640 repo_path = realpath(path, NULL);
641 if (repo_path == NULL) {
642 err = got_error_from_errno2("realpath", path);
649 err = open_repo(repo, repo_path);
652 if (err->code != GOT_ERR_NOT_GIT_REPO)
654 if (repo_path[0] == '/' && repo_path[1] == '\0') {
655 err = got_error(GOT_ERR_NOT_GIT_REPO);
658 err = got_path_dirname(&parent_path, repo_path);
662 repo_path = parent_path;
665 err = read_gotconfig(repo);
669 err = read_gitconfig(repo, global_gitconfig_path);
672 if (repo->gitconfig_repository_format_version != 0) {
673 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
676 for (i = 0; i < repo->nextensions; i++) {
677 char *ext = repo->extensions[i];
678 int j, supported = 0;
679 for (j = 0; j < nitems(repo_extensions); j++) {
680 if (strcmp(ext, repo_extensions[j]) == 0) {
686 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
691 err = got_repo_list_packidx(&repo->packidx_paths, repo);
694 got_repo_close(repo);
701 const struct got_error *
702 got_repo_close(struct got_repository *repo)
704 const struct got_error *err = NULL, *child_err;
705 struct got_packidx_bloom_filter *bf;
706 struct got_pathlist_entry *pe;
709 for (i = 0; i < repo->pack_cache_size; i++) {
710 if (repo->packidx_cache[i] == NULL)
712 got_packidx_close(repo->packidx_cache[i]);
715 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
716 &repo->packidx_bloom_filters))) {
717 RB_REMOVE(got_packidx_bloom_filter_tree,
718 &repo->packidx_bloom_filters, bf);
719 bloom_free(bf->bloom);
724 for (i = 0; i < repo->pack_cache_size; i++)
725 if (repo->packs[i].path_packfile)
726 if (repo->packs[i].path_packfile)
727 got_pack_close(&repo->packs[i]);
730 free(repo->path_git_dir);
732 got_object_cache_close(&repo->objcache);
733 got_object_cache_close(&repo->treecache);
734 got_object_cache_close(&repo->commitcache);
735 got_object_cache_close(&repo->tagcache);
736 got_object_cache_close(&repo->rawcache);
738 for (i = 0; i < nitems(repo->privsep_children); i++) {
739 if (repo->privsep_children[i].imsg_fd == -1)
741 imsg_clear(repo->privsep_children[i].ibuf);
742 free(repo->privsep_children[i].ibuf);
743 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
744 child_err = got_privsep_wait_for_child(
745 repo->privsep_children[i].pid);
746 if (child_err && err == NULL)
748 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
750 err = got_error_from_errno("close");
753 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
755 err = got_error_from_errno("close");
758 got_gotconfig_free(repo->gotconfig);
759 free(repo->gitconfig_author_name);
760 free(repo->gitconfig_author_email);
761 for (i = 0; i < repo->ngitconfig_remotes; i++)
762 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
763 free(repo->gitconfig_remotes);
764 for (i = 0; i < repo->nextensions; i++)
765 free(repo->extensions[i]);
766 free(repo->extensions);
768 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
769 free((void *)pe->path);
770 got_pathlist_free(&repo->packidx_paths);
777 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
783 free(repo->fetch_url);
784 repo->fetch_url = NULL;
785 free(repo->send_url);
786 repo->send_url = NULL;
787 for (i = 0; i < repo->nfetch_branches; i++)
788 free(repo->fetch_branches[i]);
789 free(repo->fetch_branches);
790 repo->fetch_branches = NULL;
791 repo->nfetch_branches = 0;
792 for (i = 0; i < repo->nsend_branches; i++)
793 free(repo->send_branches[i]);
794 free(repo->send_branches);
795 repo->send_branches = NULL;
796 repo->nsend_branches = 0;
799 const struct got_error *
800 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
801 const char *input_path)
803 const struct got_error *err = NULL;
804 const char *repo_abspath = NULL;
806 char *canonpath, *path = NULL;
808 *in_repo_path = NULL;
810 canonpath = strdup(input_path);
811 if (canonpath == NULL) {
812 err = got_error_from_errno("strdup");
815 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
819 repo_abspath = got_repo_get_path(repo);
821 if (canonpath[0] == '\0') {
822 path = strdup(canonpath);
824 err = got_error_from_errno("strdup");
828 path = realpath(canonpath, NULL);
830 if (errno != ENOENT) {
831 err = got_error_from_errno2("realpath",
836 * Path is not on disk.
837 * Assume it is already relative to repository root.
839 path = strdup(canonpath);
841 err = got_error_from_errno("strdup");
846 repolen = strlen(repo_abspath);
850 if (strcmp(path, repo_abspath) == 0) {
854 err = got_error_from_errno("strdup");
857 } else if (len > repolen &&
858 got_path_is_child(path, repo_abspath, repolen)) {
859 /* Matched an on-disk path inside repository. */
860 if (got_repo_is_bare(repo)) {
862 * Matched an on-disk path inside repository
863 * database. Treat input as repository-relative.
870 /* Strip common prefix with repository path. */
871 err = got_path_skip_common_ancestor(&child,
880 * Matched unrelated on-disk path.
881 * Treat input as repository-relative.
889 /* Make in-repository path absolute */
890 if (path[0] != '/') {
892 if (asprintf(&abspath, "/%s", path) == -1) {
893 err = got_error_from_errno("asprintf");
905 *in_repo_path = path;
909 static const struct got_error *
910 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
911 const char *path_packidx)
913 const struct got_error *err = NULL;
916 for (i = 0; i < repo->pack_cache_size; i++) {
917 if (repo->packidx_cache[i] == NULL)
919 if (strcmp(repo->packidx_cache[i]->path_packidx,
920 path_packidx) == 0) {
921 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
924 if (i == repo->pack_cache_size) {
927 } while (i > 0 && repo->pinned_packidx >= 0 &&
928 i == repo->pinned_packidx);
929 err = got_packidx_close(repo->packidx_cache[i]);
934 repo->packidx_cache[i] = packidx;
940 got_repo_is_packidx_filename(const char *name, size_t len)
942 if (len != GOT_PACKIDX_NAMELEN)
945 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
948 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
949 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
955 static struct got_packidx_bloom_filter *
956 get_packidx_bloom_filter(struct got_repository *repo,
957 const char *path, size_t path_len)
959 struct got_packidx_bloom_filter key;
961 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
962 return NULL; /* XXX */
963 key.path_len = path_len;
965 return RB_FIND(got_packidx_bloom_filter_tree,
966 &repo->packidx_bloom_filters, &key);
970 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
971 const char *path_packidx, struct got_object_id *id)
973 struct got_packidx_bloom_filter *bf;
975 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
977 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
979 /* No bloom filter means this pack index must be searched. */
983 static const struct got_error *
984 add_packidx_bloom_filter(struct got_repository *repo,
985 struct got_packidx *packidx, const char *path_packidx)
987 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
988 struct got_packidx_bloom_filter *bf;
992 * Don't use bloom filters for very large pack index files.
993 * Large pack files will contain a relatively large fraction
994 * of our objects so we will likely need to visit them anyway.
995 * The more objects a pack file contains the higher the probability
996 * of a false-positive match from the bloom filter. And reading
997 * all object IDs from a large pack index file can be expensive.
999 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1002 /* Do we already have a filter for this pack index? */
1003 if (get_packidx_bloom_filter(repo, path_packidx,
1004 strlen(path_packidx)) != NULL)
1007 bf = calloc(1, sizeof(*bf));
1009 return got_error_from_errno("calloc");
1010 bf->bloom = calloc(1, sizeof(*bf->bloom));
1011 if (bf->bloom == NULL) {
1013 return got_error_from_errno("calloc");
1016 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1017 if (len >= sizeof(bf->path)) {
1020 return got_error(GOT_ERR_NO_SPACE);
1024 /* Minimum size supported by our bloom filter is 1000 entries. */
1025 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1026 for (i = 0; i < nobjects; i++) {
1027 struct got_packidx_object_id *id;
1028 id = &packidx->hdr.sorted_ids[i];
1029 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1032 RB_INSERT(got_packidx_bloom_filter_tree,
1033 &repo->packidx_bloom_filters, bf);
1037 const struct got_error *
1038 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1039 struct got_repository *repo, struct got_object_id *id)
1041 const struct got_error *err;
1042 struct got_pathlist_entry *pe;
1045 /* Search pack index cache. */
1046 for (i = 0; i < repo->pack_cache_size; i++) {
1047 if (repo->packidx_cache[i] == NULL)
1049 if (!got_repo_check_packidx_bloom_filter(repo,
1050 repo->packidx_cache[i]->path_packidx, id))
1051 continue; /* object will not be found in this index */
1052 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1054 *packidx = repo->packidx_cache[i];
1056 * Move this cache entry to the front. Repeatedly
1057 * searching a wrong pack index can be expensive.
1060 memmove(&repo->packidx_cache[1],
1061 &repo->packidx_cache[0],
1062 i * sizeof(repo->packidx_cache[0]));
1063 repo->packidx_cache[0] = *packidx;
1064 if (repo->pinned_packidx >= 0 &&
1065 repo->pinned_packidx < i)
1066 repo->pinned_packidx++;
1067 else if (repo->pinned_packidx == i)
1068 repo->pinned_packidx = 0;
1073 /* No luck. Search the filesystem. */
1075 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1076 const char *path_packidx = pe->path;
1079 if (!got_repo_check_packidx_bloom_filter(repo,
1081 continue; /* object will not be found in this index */
1083 for (i = 0; i < repo->pack_cache_size; i++) {
1084 if (repo->packidx_cache[i] == NULL)
1086 if (strcmp(repo->packidx_cache[i]->path_packidx,
1087 path_packidx) == 0) {
1093 continue; /* already searched */
1095 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1100 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1104 err = cache_packidx(repo, *packidx, path_packidx);
1108 *idx = got_packidx_get_object_idx(*packidx, id);
1110 err = NULL; /* found the object */
1115 err = got_error_no_obj(id);
1120 const struct got_error *
1121 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1122 struct got_repository *repo)
1124 const struct got_error *err = NULL;
1125 DIR *packdir = NULL;
1126 struct dirent *dent;
1127 char *path_packidx = NULL;
1131 packdir_fd = openat(got_repo_get_fd(repo),
1132 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1133 if (packdir_fd == -1) {
1134 return got_error_from_errno_fmt("openat: %s/%s",
1135 got_repo_get_path_git_dir(repo),
1136 GOT_OBJECTS_PACK_DIR);
1139 packdir = fdopendir(packdir_fd);
1140 if (packdir == NULL) {
1141 err = got_error_from_errno("fdopendir");
1145 if (fstat(packdir_fd, &sb) == -1) {
1146 err = got_error_from_errno("fstat");
1149 repo->pack_path_mtime = sb.st_mtime;
1151 while ((dent = readdir(packdir)) != NULL) {
1152 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1155 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1156 dent->d_name) == -1) {
1157 err = got_error_from_errno("asprintf");
1158 path_packidx = NULL;
1162 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1169 if (packdir && closedir(packdir) != 0 && err == NULL)
1170 err = got_error_from_errno("closedir");
1174 const struct got_error *
1175 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1176 struct got_repository *repo)
1178 const struct got_error *err;
1183 /* Search pack index cache. */
1184 for (i = 0; i < repo->pack_cache_size; i++) {
1185 if (repo->packidx_cache[i] == NULL)
1187 if (strcmp(repo->packidx_cache[i]->path_packidx,
1188 path_packidx) == 0) {
1189 *packidx = repo->packidx_cache[i];
1193 /* No luck. Search the filesystem. */
1195 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1200 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1204 err = cache_packidx(repo, *packidx, path_packidx);
1207 got_packidx_close(*packidx);
1213 static const struct got_error *
1214 read_packfile_hdr(int fd, struct got_packidx *packidx)
1216 const struct got_error *err = NULL;
1217 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1218 struct got_packfile_hdr hdr;
1221 n = read(fd, &hdr, sizeof(hdr));
1223 return got_error_from_errno("read");
1224 if (n != sizeof(hdr))
1225 return got_error(GOT_ERR_BAD_PACKFILE);
1227 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1228 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1229 be32toh(hdr.nobjects) != totobj)
1230 err = got_error(GOT_ERR_BAD_PACKFILE);
1235 static const struct got_error *
1236 open_packfile(int *fd, struct got_repository *repo,
1237 const char *relpath, struct got_packidx *packidx)
1239 const struct got_error *err = NULL;
1241 *fd = openat(got_repo_get_fd(repo), relpath,
1242 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1244 return got_error_from_errno_fmt("openat: %s/%s",
1245 got_repo_get_path_git_dir(repo), relpath);
1248 err = read_packfile_hdr(*fd, packidx);
1258 const struct got_error *
1259 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1260 const char *path_packfile, struct got_packidx *packidx)
1262 const struct got_error *err = NULL;
1263 struct got_pack *pack = NULL;
1270 for (i = 0; i < repo->pack_cache_size; i++) {
1271 pack = &repo->packs[i];
1272 if (pack->path_packfile == NULL)
1274 if (strcmp(pack->path_packfile, path_packfile) == 0)
1275 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1278 if (i == repo->pack_cache_size) {
1279 struct got_pack tmp;
1282 } while (i > 0 && repo->pinned_pack >= 0 &&
1283 i == repo->pinned_pack);
1284 err = got_pack_close(&repo->packs[i]);
1287 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1288 return got_error_from_errno("ftruncate");
1289 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1290 return got_error_from_errno("ftruncate");
1291 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1292 memcpy(&repo->packs[i], &repo->packs[0],
1293 sizeof(repo->packs[i]));
1294 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1295 if (repo->pinned_pack == 0)
1296 repo->pinned_pack = i;
1297 else if (repo->pinned_pack == i)
1298 repo->pinned_pack = 0;
1302 pack = &repo->packs[i];
1304 pack->path_packfile = strdup(path_packfile);
1305 if (pack->path_packfile == NULL) {
1306 err = got_error_from_errno("strdup");
1310 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1314 if (fstat(pack->fd, &sb) != 0) {
1315 err = got_error_from_errno("fstat");
1318 pack->filesize = sb.st_size;
1320 pack->privsep_child = NULL;
1322 #ifndef GOT_PACK_NO_MMAP
1323 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1325 if (pack->map == MAP_FAILED) {
1326 if (errno != ENOMEM) {
1327 err = got_error_from_errno("mmap");
1330 pack->map = NULL; /* fall back to read(2) */
1336 free(pack->path_packfile);
1337 memset(pack, 0, sizeof(*pack));
1345 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1347 struct got_pack *pack = NULL;
1350 for (i = 0; i < repo->pack_cache_size; i++) {
1351 pack = &repo->packs[i];
1352 if (pack->path_packfile == NULL)
1354 if (strcmp(pack->path_packfile, path_packfile) == 0)
1361 const struct got_error *
1362 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1363 struct got_pack *pack)
1366 int pinned_pack = -1, pinned_packidx = -1;
1368 for (i = 0; i < repo->pack_cache_size; i++) {
1369 if (repo->packidx_cache[i] &&
1370 strcmp(repo->packidx_cache[i]->path_packidx,
1371 packidx->path_packidx) == 0)
1373 if (repo->packs[i].path_packfile &&
1374 strcmp(repo->packs[i].path_packfile,
1375 pack->path_packfile) == 0)
1379 if (pinned_packidx == -1 || pinned_pack == -1)
1380 return got_error(GOT_ERR_PIN_PACK);
1382 repo->pinned_pack = pinned_pack;
1383 repo->pinned_packidx = pinned_packidx;
1384 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1389 got_repo_get_pinned_pack(struct got_repository *repo)
1391 if (repo->pinned_pack >= 0 &&
1392 repo->pinned_pack < repo->pack_cache_size)
1393 return &repo->packs[repo->pinned_pack];
1399 got_repo_unpin_pack(struct got_repository *repo)
1401 repo->pinned_packidx = -1;
1402 repo->pinned_pack = -1;
1403 repo->pinned_pid = 0;
1406 const struct got_error *
1407 got_repo_init(const char *repo_path, const char *head_name)
1409 const struct got_error *err = NULL;
1410 const char *dirnames[] = {
1412 GOT_OBJECTS_PACK_DIR,
1415 const char *description_str = "Unnamed repository; "
1416 "edit this file 'description' to name the repository.";
1417 const char *headref = "ref: refs/heads/";
1418 const char *gitconfig_str = "[core]\n"
1419 "\trepositoryformatversion = 0\n"
1420 "\tfilemode = true\n"
1422 char *headref_str, *path;
1425 if (!got_path_dir_is_empty(repo_path))
1426 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1428 for (i = 0; i < nitems(dirnames); i++) {
1429 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1430 return got_error_from_errno("asprintf");
1432 err = got_path_mkdir(path);
1438 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1439 return got_error_from_errno("asprintf");
1440 err = got_path_create_file(path, description_str);
1445 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1446 return got_error_from_errno("asprintf");
1447 if (asprintf(&headref_str, "%s%s", headref,
1448 head_name ? head_name : "main") == -1) {
1450 return got_error_from_errno("asprintf");
1452 err = got_path_create_file(path, headref_str);
1458 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1459 return got_error_from_errno("asprintf");
1460 err = got_path_create_file(path, gitconfig_str);
1469 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1471 struct got_pathlist_entry *pe;
1473 while (!TAILQ_EMPTY(packidx_paths)) {
1474 pe = TAILQ_FIRST(packidx_paths);
1475 TAILQ_REMOVE(packidx_paths, pe, entry);
1476 free((char *)pe->path);
1481 static const struct got_error *
1482 match_packed_object(struct got_object_id **unique_id,
1483 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1485 const struct got_error *err = NULL;
1486 char *objects_pack_dir = NULL;
1487 struct got_object_id_queue matched_ids;
1488 struct got_pathlist_entry *pe;
1491 STAILQ_INIT(&matched_ids);
1493 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1494 if (objects_pack_dir == NULL)
1495 return got_error_from_errno("got_repo_get_path_objects_pack");
1497 if (stat(objects_pack_dir, &sb) == -1) {
1498 if (errno != ENOENT) {
1499 err = got_error_from_errno2("stat", objects_pack_dir);
1502 } else if (sb.st_mtime != repo->pack_path_mtime) {
1503 purge_packidx_paths(&repo->packidx_paths);
1504 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1509 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1510 const char *path_packidx = pe->path;
1511 struct got_packidx *packidx;
1512 struct got_object_qid *qid;
1514 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1519 err = got_packidx_match_id_str_prefix(&matched_ids,
1520 packidx, id_str_prefix);
1522 got_packidx_close(packidx);
1525 err = got_packidx_close(packidx);
1529 STAILQ_FOREACH(qid, &matched_ids, entry) {
1530 if (obj_type != GOT_OBJ_TYPE_ANY) {
1532 err = got_object_get_type(&matched_type, repo,
1536 if (matched_type != obj_type)
1539 if (*unique_id == NULL) {
1540 *unique_id = got_object_id_dup(&qid->id);
1541 if (*unique_id == NULL) {
1542 err = got_error_from_errno("malloc");
1546 if (got_object_id_cmp(*unique_id,
1548 continue; /* packed multiple times */
1549 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1555 free(objects_pack_dir);
1556 got_object_id_queue_free(&matched_ids);
1564 static const struct got_error *
1565 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1566 const char *object_dir, const char *id_str_prefix, int obj_type,
1567 struct got_repository *repo)
1569 const struct got_error *err = NULL;
1570 char *path, *id_str = NULL;
1572 struct dirent *dent;
1573 struct got_object_id id;
1575 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1576 err = got_error_from_errno("asprintf");
1580 dir = opendir(path);
1582 if (errno == ENOENT) {
1586 err = got_error_from_errno2("opendir", path);
1589 while ((dent = readdir(dir)) != NULL) {
1595 if (strcmp(dent->d_name, ".") == 0 ||
1596 strcmp(dent->d_name, "..") == 0)
1599 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1600 err = got_error_from_errno("asprintf");
1604 if (!got_parse_sha1_digest(id.sha1, id_str))
1608 * Directory entries do not necessarily appear in
1609 * sorted order, so we must iterate over all of them.
1611 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1615 if (*unique_id == NULL) {
1616 if (obj_type != GOT_OBJ_TYPE_ANY) {
1618 err = got_object_get_type(&matched_type, repo,
1622 if (matched_type != obj_type)
1625 *unique_id = got_object_id_dup(&id);
1626 if (*unique_id == NULL) {
1627 err = got_error_from_errno("got_object_id_dup");
1631 if (got_object_id_cmp(*unique_id, &id) == 0)
1632 continue; /* both packed and loose */
1633 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1638 if (dir && closedir(dir) != 0 && err == NULL)
1639 err = got_error_from_errno("closedir");
1649 const struct got_error *
1650 got_repo_match_object_id_prefix(struct got_object_id **id,
1651 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 char *path_objects = NULL, *object_dir = NULL;
1660 path_objects = got_repo_get_path_objects(repo);
1662 len = strlen(id_str_prefix);
1663 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1664 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1668 for (i = 0; i < len; i++) {
1669 if (isxdigit((unsigned char)id_str_prefix[i]))
1671 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1676 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1679 object_dir = strndup(id_str_prefix, 2);
1680 if (object_dir == NULL) {
1681 err = got_error_from_errno("strdup");
1684 err = match_loose_object(id, path_objects, object_dir,
1685 id_str_prefix, obj_type, repo);
1686 } else if (len == 1) {
1688 for (i = 0; i < 0xf; i++) {
1689 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1691 err = got_error_from_errno("asprintf");
1694 err = match_packed_object(id, repo, object_dir,
1698 err = match_loose_object(id, path_objects, object_dir,
1699 id_str_prefix, obj_type, repo);
1704 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1713 } else if (*id == NULL) {
1715 case GOT_OBJ_TYPE_BLOB:
1716 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1717 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1719 case GOT_OBJ_TYPE_TREE:
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_TREE, id_str_prefix);
1723 case GOT_OBJ_TYPE_COMMIT:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1727 case GOT_OBJ_TYPE_TAG:
1728 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1729 GOT_OBJ_LABEL_TAG, id_str_prefix);
1732 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1740 const struct got_error *
1741 got_repo_match_object_id(struct got_object_id **id, char **label,
1742 const char *id_str, int obj_type, struct got_reflist_head *refs,
1743 struct got_repository *repo)
1745 const struct got_error *err;
1746 struct got_tag_object *tag;
1747 struct got_reference *ref = NULL;
1754 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1757 *id = got_object_id_dup(
1758 got_object_tag_get_object_id(tag));
1760 err = got_error_from_errno("got_object_id_dup");
1761 else if (label && asprintf(label, "refs/tags/%s",
1762 got_object_tag_get_name(tag)) == -1) {
1763 err = got_error_from_errno("asprintf");
1767 got_object_tag_close(tag);
1769 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1770 err->code != GOT_ERR_NO_OBJ)
1774 err = got_ref_open(&ref, repo, id_str, 0);
1776 err = got_ref_resolve(id, repo, ref);
1780 *label = strdup(got_ref_get_name(ref));
1781 if (*label == NULL) {
1782 err = got_error_from_errno("strdup");
1787 if (err->code != GOT_ERR_NOT_REF &&
1788 err->code != GOT_ERR_BAD_REF_NAME)
1790 err = got_repo_match_object_id_prefix(id, id_str,
1793 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1794 err = got_error_not_ref(id_str);
1798 err = got_object_id_str(label, *id);
1799 if (*label == NULL) {
1800 err = got_error_from_errno("strdup");
1811 const struct got_error *
1812 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1813 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1815 const struct got_error *err = NULL;
1816 struct got_reflist_entry *re;
1817 struct got_object_id *tag_id;
1818 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1822 TAILQ_FOREACH(re, refs, entry) {
1823 const char *refname;
1824 refname = got_ref_get_name(re->ref);
1825 if (got_ref_is_symbolic(re->ref))
1827 if (strncmp(refname, "refs/tags/", 10) != 0)
1829 if (!name_is_absolute)
1830 refname += strlen("refs/tags/");
1831 if (strcmp(refname, name) != 0)
1833 err = got_ref_resolve(&tag_id, repo, re->ref);
1836 err = got_object_open_as_tag(tag, repo, tag_id);
1840 if (obj_type == GOT_OBJ_TYPE_ANY ||
1841 got_object_tag_get_object_type(*tag) == obj_type)
1843 got_object_tag_close(*tag);
1847 if (err == NULL && *tag == NULL)
1848 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1849 GOT_OBJ_LABEL_TAG, name);
1853 static const struct got_error *
1854 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1855 const char *name, mode_t mode, struct got_object_id *blob_id)
1857 const struct got_error *err = NULL;
1861 *new_te = calloc(1, sizeof(**new_te));
1862 if (*new_te == NULL)
1863 return got_error_from_errno("calloc");
1865 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1866 sizeof((*new_te)->name)) {
1867 err = got_error(GOT_ERR_NO_SPACE);
1871 if (S_ISLNK(mode)) {
1872 (*new_te)->mode = S_IFLNK;
1874 (*new_te)->mode = S_IFREG;
1875 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1877 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1879 if (err && *new_te) {
1886 static const struct got_error *
1887 import_file(struct got_tree_entry **new_te, struct dirent *de,
1888 const char *path, struct got_repository *repo)
1890 const struct got_error *err;
1891 struct got_object_id *blob_id = NULL;
1895 if (asprintf(&filepath, "%s%s%s", path,
1896 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1897 return got_error_from_errno("asprintf");
1899 if (lstat(filepath, &sb) != 0) {
1900 err = got_error_from_errno2("lstat", path);
1904 err = got_object_blob_create(&blob_id, filepath, repo);
1908 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1917 static const struct got_error *
1918 insert_tree_entry(struct got_tree_entry *new_te,
1919 struct got_pathlist_head *paths)
1921 const struct got_error *err = NULL;
1922 struct got_pathlist_entry *new_pe;
1924 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1928 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1932 static const struct got_error *write_tree(struct got_object_id **,
1933 const char *, struct got_pathlist_head *, struct got_repository *,
1934 got_repo_import_cb progress_cb, void *progress_arg);
1936 static const struct got_error *
1937 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1938 const char *path, struct got_pathlist_head *ignores,
1939 struct got_repository *repo,
1940 got_repo_import_cb progress_cb, void *progress_arg)
1942 const struct got_error *err;
1943 struct got_object_id *id = NULL;
1946 if (asprintf(&subdirpath, "%s%s%s", path,
1947 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1948 return got_error_from_errno("asprintf");
1950 (*new_te) = calloc(1, sizeof(**new_te));
1951 if (*new_te == NULL)
1952 return got_error_from_errno("calloc");
1953 (*new_te)->mode = S_IFDIR;
1954 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1955 sizeof((*new_te)->name)) {
1956 err = got_error(GOT_ERR_NO_SPACE);
1959 err = write_tree(&id, subdirpath, ignores, repo,
1960 progress_cb, progress_arg);
1963 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1975 static const struct got_error *
1976 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1977 struct got_pathlist_head *ignores, struct got_repository *repo,
1978 got_repo_import_cb progress_cb, void *progress_arg)
1980 const struct got_error *err = NULL;
1984 struct got_tree_entry *new_te = NULL;
1985 struct got_pathlist_head paths;
1986 struct got_pathlist_entry *pe;
1988 *new_tree_id = NULL;
1992 dir = opendir(path_dir);
1994 err = got_error_from_errno2("opendir", path_dir);
1999 while ((de = readdir(dir)) != NULL) {
2003 if (strcmp(de->d_name, ".") == 0 ||
2004 strcmp(de->d_name, "..") == 0)
2007 TAILQ_FOREACH(pe, ignores, entry) {
2008 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2016 err = got_path_dirent_type(&type, path_dir, de);
2020 if (type == DT_DIR) {
2021 err = import_subdir(&new_te, de, path_dir,
2022 ignores, repo, progress_cb, progress_arg);
2024 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2029 } else if (type == DT_REG || type == DT_LNK) {
2030 err = import_file(&new_te, de, path_dir, repo);
2036 err = insert_tree_entry(new_te, &paths);
2042 if (TAILQ_EMPTY(&paths)) {
2043 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2044 "cannot create tree without any entries");
2048 TAILQ_FOREACH(pe, &paths, entry) {
2049 struct got_tree_entry *te = pe->data;
2051 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2053 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2054 err = got_error_from_errno("asprintf");
2057 err = (*progress_cb)(progress_arg, path);
2063 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2067 got_pathlist_free(&paths);
2071 const struct got_error *
2072 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2073 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2074 struct got_repository *repo, got_repo_import_cb progress_cb,
2077 const struct got_error *err;
2078 struct got_object_id *new_tree_id;
2080 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2081 progress_cb, progress_arg);
2085 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2086 author, time(NULL), author, time(NULL), logmsg, repo);
2091 const struct got_error *
2092 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2093 struct got_repository *repo)
2095 const struct got_error *err = NULL;
2096 char *path_objects = NULL, *path = NULL;
2098 struct got_object_id id;
2104 path_objects = got_repo_get_path_objects(repo);
2105 if (path_objects == NULL)
2106 return got_error_from_errno("got_repo_get_path_objects");
2108 for (i = 0; i <= 0xff; i++) {
2109 struct dirent *dent;
2111 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2112 err = got_error_from_errno("asprintf");
2116 dir = opendir(path);
2118 if (errno == ENOENT) {
2122 err = got_error_from_errno2("opendir", path);
2126 while ((dent = readdir(dir)) != NULL) {
2131 if (strcmp(dent->d_name, ".") == 0 ||
2132 strcmp(dent->d_name, "..") == 0)
2135 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2136 err = got_error_from_errno("asprintf");
2140 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2146 err = got_object_open_loose_fd(&fd, &id, repo);
2150 if (fstat(fd, &sb) == -1) {
2151 err = got_error_from_errno("fstat");
2156 (*ondisk_size) += sb.st_size;
2158 if (close(fd) == -1) {
2159 err = got_error_from_errno("close");
2164 if (closedir(dir) != 0) {
2165 err = got_error_from_errno("closedir");
2174 if (dir && closedir(dir) != 0 && err == NULL)
2175 err = got_error_from_errno("closedir");
2186 const struct got_error *
2187 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2188 off_t *total_packsize, struct got_repository *repo)
2190 const struct got_error *err = NULL;
2191 DIR *packdir = NULL;
2192 struct dirent *dent;
2193 struct got_packidx *packidx = NULL;
2195 char *path_packfile;
2201 *total_packsize = 0;
2203 packdir_fd = openat(got_repo_get_fd(repo),
2204 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2205 if (packdir_fd == -1) {
2206 return got_error_from_errno_fmt("openat: %s/%s",
2207 got_repo_get_path_git_dir(repo),
2208 GOT_OBJECTS_PACK_DIR);
2211 packdir = fdopendir(packdir_fd);
2212 if (packdir == NULL) {
2213 err = got_error_from_errno("fdopendir");
2217 while ((dent = readdir(packdir)) != NULL) {
2218 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2221 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2222 dent->d_name) == -1) {
2223 err = got_error_from_errno("asprintf");
2227 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2233 if (fstat(packidx->fd, &sb) == -1)
2235 *total_packsize += sb.st_size;
2237 err = got_packidx_get_packfile_path(&path_packfile,
2238 packidx->path_packidx);
2242 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2244 free(path_packfile);
2247 free(path_packfile);
2248 *total_packsize += sb.st_size;
2250 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2254 got_packidx_close(packidx);
2259 got_packidx_close(packidx);
2260 if (packdir && closedir(packdir) != 0 && err == NULL)
2261 err = got_error_from_errno("closedir");
2265 *total_packsize = 0;
2270 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2271 got_packidx_bloom_filter_cmp);