2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_get_fd(struct got_repository *repo)
86 return repo->gitdir_fd;
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 return repo->gitconfig_author_name;
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 return repo->gitconfig_author_email;
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 get_path_git_child(struct got_repository *repo, const char *basename)
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
214 if (!S_ISDIR(sb.st_mode))
217 if (lstat(path_objects, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_refs, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_head, &sb) == -1)
229 if (!S_ISREG(sb.st_mode))
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 got_ref_close(head_ref);
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
257 * got_repo_pack_fds_close will try to close all of the
258 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
259 * a value from got_opentempfd(), which would result in a close(0) if
260 * we do not initialize to -1 here.
262 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
265 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
266 (*pack_fds)[i] = got_opentempfd();
267 if ((*pack_fds)[i] == -1) {
268 err = got_error_from_errno("got_opentempfd");
269 got_repo_pack_fds_close(*pack_fds);
278 const struct got_error *
279 got_repo_pack_fds_close(int *pack_fds)
281 const struct got_error *err = NULL;
284 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
285 if (pack_fds[i] == -1)
287 if (close(pack_fds[i]) == -1) {
288 err = got_error_from_errno("close");
296 const struct got_error *
297 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
298 struct got_object *obj)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->objcache, id, obj);
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
315 got_repo_get_cached_object(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
321 const struct got_error *
322 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
323 struct got_tree_object *tree)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->treecache, id, tree);
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
339 struct got_tree_object *
340 got_repo_get_cached_tree(struct got_repository *repo,
341 struct got_object_id *id)
343 return (struct got_tree_object *)got_object_cache_get(
344 &repo->treecache, id);
347 const struct got_error *
348 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
349 struct got_commit_object *commit)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->commitcache, id, commit);
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
365 struct got_commit_object *
366 got_repo_get_cached_commit(struct got_repository *repo,
367 struct got_object_id *id)
369 return (struct got_commit_object *)got_object_cache_get(
370 &repo->commitcache, id);
373 const struct got_error *
374 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
375 struct got_tag_object *tag)
377 #ifndef GOT_NO_OBJ_CACHE
378 const struct got_error *err = NULL;
379 err = got_object_cache_add(&repo->tagcache, id, tag);
381 if (err->code == GOT_ERR_OBJ_EXISTS ||
382 err->code == GOT_ERR_OBJ_TOO_LARGE)
391 struct got_tag_object *
392 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
394 return (struct got_tag_object *)got_object_cache_get(
395 &repo->tagcache, id);
398 const struct got_error *
399 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
400 struct got_raw_object *raw)
402 #ifndef GOT_NO_OBJ_CACHE
403 const struct got_error *err = NULL;
404 err = got_object_cache_add(&repo->rawcache, id, raw);
406 if (err->code == GOT_ERR_OBJ_EXISTS ||
407 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 struct got_raw_object *
418 got_repo_get_cached_raw_object(struct got_repository *repo,
419 struct got_object_id *id)
421 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
425 static const struct got_error *
426 open_repo(struct got_repository *repo, const char *path)
428 const struct got_error *err = NULL;
430 repo->gitdir_fd = -1;
432 /* bare git repository? */
433 repo->path_git_dir = strdup(path);
434 if (repo->path_git_dir == NULL)
435 return got_error_from_errno("strdup");
436 if (is_git_repo(repo)) {
437 repo->path = strdup(repo->path_git_dir);
438 if (repo->path == NULL) {
439 err = got_error_from_errno("strdup");
442 repo->gitdir_fd = open(repo->path_git_dir,
443 O_DIRECTORY | O_CLOEXEC);
444 if (repo->gitdir_fd == -1) {
445 err = got_error_from_errno2("open",
452 /* git repository with working tree? */
453 free(repo->path_git_dir);
454 repo->path_git_dir = NULL;
455 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
456 err = got_error_from_errno("asprintf");
459 if (is_git_repo(repo)) {
460 repo->path = strdup(path);
461 if (repo->path == NULL) {
462 err = got_error_from_errno("strdup");
465 repo->gitdir_fd = open(repo->path_git_dir,
466 O_DIRECTORY | O_CLOEXEC);
467 if (repo->gitdir_fd == -1) {
468 err = got_error_from_errno2("open",
475 err = got_error(GOT_ERR_NOT_GIT_REPO);
480 free(repo->path_git_dir);
481 repo->path_git_dir = NULL;
482 if (repo->gitdir_fd != -1)
483 close(repo->gitdir_fd);
484 repo->gitdir_fd = -1;
490 static const struct got_error *
491 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
493 const struct got_error *err = NULL;
494 char *repo_gitconfig_path = NULL;
496 if (global_gitconfig_path) {
497 /* Read settings from ~/.gitconfig. */
498 int dummy_repo_version;
499 err = got_repo_read_gitconfig(&dummy_repo_version,
500 &repo->global_gitconfig_author_name,
501 &repo->global_gitconfig_author_email,
502 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
507 /* Read repository's .git/config file. */
508 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
509 if (repo_gitconfig_path == NULL)
510 return got_error_from_errno("got_repo_get_path_gitconfig");
512 err = got_repo_read_gitconfig(
513 &repo->gitconfig_repository_format_version,
514 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
515 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
516 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
517 repo_gitconfig_path);
521 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
524 for (i = 0; i < repo->ngitconfig_remotes; i++) {
525 got_repo_free_remote_repo_data(
526 &repo->gitconfig_remotes[i]);
528 free(repo->gitconfig_remotes);
529 repo->gitconfig_remotes = NULL;
530 repo->ngitconfig_remotes = 0;
532 free(repo->gitconfig_author_name);
533 repo->gitconfig_author_name = NULL;
534 free(repo->gitconfig_author_email);
535 repo->gitconfig_author_email = NULL;
537 free(repo->global_gitconfig_author_name);
538 repo->global_gitconfig_author_name = NULL;
539 free(repo->global_gitconfig_author_email);
540 repo->global_gitconfig_author_email = NULL;
544 free(repo_gitconfig_path);
548 static const struct got_error *
549 read_gotconfig(struct got_repository *repo)
551 const struct got_error *err = NULL;
552 char *gotconfig_path;
554 gotconfig_path = got_repo_get_path_gotconfig(repo);
555 if (gotconfig_path == NULL)
556 return got_error_from_errno("got_repo_get_path_gotconfig");
558 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
559 free(gotconfig_path);
563 /* Supported repository format extensions. */
564 static const char *const repo_extensions[] = {
565 "noop", /* Got supports repository format version 1. */
566 "preciousObjects", /* Supported by gotadmin cleanup. */
567 "worktreeConfig", /* Got does not care about Git work trees. */
570 const struct got_error *
571 got_repo_open(struct got_repository **repop, const char *path,
572 const char *global_gitconfig_path, int *pack_fds)
574 struct got_repository *repo = NULL;
575 const struct got_error *err = NULL;
576 char *repo_path = NULL;
582 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
583 return got_error_from_errno("getrlimit");
585 repo = calloc(1, sizeof(*repo));
587 return got_error_from_errno("calloc");
589 RB_INIT(&repo->packidx_bloom_filters);
590 TAILQ_INIT(&repo->packidx_paths);
592 for (i = 0; i < nitems(repo->privsep_children); i++) {
593 memset(&repo->privsep_children[i], 0,
594 sizeof(repo->privsep_children[0]));
595 repo->privsep_children[i].imsg_fd = -1;
598 err = got_object_cache_init(&repo->objcache,
599 GOT_OBJECT_CACHE_TYPE_OBJ);
602 err = got_object_cache_init(&repo->treecache,
603 GOT_OBJECT_CACHE_TYPE_TREE);
606 err = got_object_cache_init(&repo->commitcache,
607 GOT_OBJECT_CACHE_TYPE_COMMIT);
610 err = got_object_cache_init(&repo->tagcache,
611 GOT_OBJECT_CACHE_TYPE_TAG);
614 err = got_object_cache_init(&repo->rawcache,
615 GOT_OBJECT_CACHE_TYPE_RAW);
619 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
620 if (repo->pack_cache_size > rl.rlim_cur / 8)
621 repo->pack_cache_size = rl.rlim_cur / 8;
622 for (i = 0; i < nitems(repo->packs); i++) {
623 if (i < repo->pack_cache_size) {
624 repo->packs[i].basefd = pack_fds[j++];
625 repo->packs[i].accumfd = pack_fds[j++];
627 repo->packs[i].basefd = -1;
628 repo->packs[i].accumfd = -1;
631 repo->pinned_pack = -1;
632 repo->pinned_packidx = -1;
633 repo->pinned_pid = 0;
635 repo_path = realpath(path, NULL);
636 if (repo_path == NULL) {
637 err = got_error_from_errno2("realpath", path);
644 err = open_repo(repo, repo_path);
647 if (err->code != GOT_ERR_NOT_GIT_REPO)
649 if (repo_path[0] == '/' && repo_path[1] == '\0') {
650 err = got_error(GOT_ERR_NOT_GIT_REPO);
653 err = got_path_dirname(&parent_path, repo_path);
657 repo_path = parent_path;
660 err = read_gotconfig(repo);
664 err = read_gitconfig(repo, global_gitconfig_path);
667 if (repo->gitconfig_repository_format_version != 0) {
668 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
671 for (i = 0; i < repo->nextensions; i++) {
672 char *ext = repo->extensions[i];
673 int j, supported = 0;
674 for (j = 0; j < nitems(repo_extensions); j++) {
675 if (strcmp(ext, repo_extensions[j]) == 0) {
681 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
686 err = got_repo_list_packidx(&repo->packidx_paths, repo);
689 got_repo_close(repo);
696 const struct got_error *
697 got_repo_close(struct got_repository *repo)
699 const struct got_error *err = NULL, *child_err;
700 struct got_packidx_bloom_filter *bf;
701 struct got_pathlist_entry *pe;
704 for (i = 0; i < repo->pack_cache_size; i++) {
705 if (repo->packidx_cache[i] == NULL)
707 got_packidx_close(repo->packidx_cache[i]);
710 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
711 &repo->packidx_bloom_filters))) {
712 RB_REMOVE(got_packidx_bloom_filter_tree,
713 &repo->packidx_bloom_filters, bf);
714 bloom_free(bf->bloom);
719 for (i = 0; i < repo->pack_cache_size; i++)
720 if (repo->packs[i].path_packfile)
721 if (repo->packs[i].path_packfile)
722 got_pack_close(&repo->packs[i]);
725 free(repo->path_git_dir);
727 got_object_cache_close(&repo->objcache);
728 got_object_cache_close(&repo->treecache);
729 got_object_cache_close(&repo->commitcache);
730 got_object_cache_close(&repo->tagcache);
731 got_object_cache_close(&repo->rawcache);
733 for (i = 0; i < nitems(repo->privsep_children); i++) {
734 if (repo->privsep_children[i].imsg_fd == -1)
736 imsg_clear(repo->privsep_children[i].ibuf);
737 free(repo->privsep_children[i].ibuf);
738 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
739 child_err = got_privsep_wait_for_child(
740 repo->privsep_children[i].pid);
741 if (child_err && err == NULL)
743 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
745 err = got_error_from_errno("close");
748 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
750 err = got_error_from_errno("close");
753 got_gotconfig_free(repo->gotconfig);
754 free(repo->gitconfig_author_name);
755 free(repo->gitconfig_author_email);
756 for (i = 0; i < repo->ngitconfig_remotes; i++)
757 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
758 free(repo->gitconfig_remotes);
759 for (i = 0; i < repo->nextensions; i++)
760 free(repo->extensions[i]);
761 free(repo->extensions);
763 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
764 free((void *)pe->path);
765 got_pathlist_free(&repo->packidx_paths);
772 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
778 free(repo->fetch_url);
779 repo->fetch_url = NULL;
780 free(repo->send_url);
781 repo->send_url = NULL;
782 for (i = 0; i < repo->nfetch_branches; i++)
783 free(repo->fetch_branches[i]);
784 free(repo->fetch_branches);
785 repo->fetch_branches = NULL;
786 repo->nfetch_branches = 0;
787 for (i = 0; i < repo->nsend_branches; i++)
788 free(repo->send_branches[i]);
789 free(repo->send_branches);
790 repo->send_branches = NULL;
791 repo->nsend_branches = 0;
794 const struct got_error *
795 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
796 const char *input_path)
798 const struct got_error *err = NULL;
799 const char *repo_abspath = NULL;
801 char *canonpath, *path = NULL;
803 *in_repo_path = NULL;
805 canonpath = strdup(input_path);
806 if (canonpath == NULL) {
807 err = got_error_from_errno("strdup");
810 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
814 repo_abspath = got_repo_get_path(repo);
816 if (canonpath[0] == '\0') {
817 path = strdup(canonpath);
819 err = got_error_from_errno("strdup");
823 path = realpath(canonpath, NULL);
825 if (errno != ENOENT) {
826 err = got_error_from_errno2("realpath",
831 * Path is not on disk.
832 * Assume it is already relative to repository root.
834 path = strdup(canonpath);
836 err = got_error_from_errno("strdup");
841 repolen = strlen(repo_abspath);
845 if (strcmp(path, repo_abspath) == 0) {
849 err = got_error_from_errno("strdup");
852 } else if (len > repolen &&
853 got_path_is_child(path, repo_abspath, repolen)) {
854 /* Matched an on-disk path inside repository. */
855 if (got_repo_is_bare(repo)) {
857 * Matched an on-disk path inside repository
858 * database. Treat input as repository-relative.
865 /* Strip common prefix with repository path. */
866 err = got_path_skip_common_ancestor(&child,
875 * Matched unrelated on-disk path.
876 * Treat input as repository-relative.
884 /* Make in-repository path absolute */
885 if (path[0] != '/') {
887 if (asprintf(&abspath, "/%s", path) == -1) {
888 err = got_error_from_errno("asprintf");
900 *in_repo_path = path;
904 static const struct got_error *
905 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
906 const char *path_packidx)
908 const struct got_error *err = NULL;
911 for (i = 0; i < repo->pack_cache_size; i++) {
912 if (repo->packidx_cache[i] == NULL)
914 if (strcmp(repo->packidx_cache[i]->path_packidx,
915 path_packidx) == 0) {
916 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
919 if (i == repo->pack_cache_size) {
922 } while (i > 0 && repo->pinned_packidx >= 0 &&
923 i == repo->pinned_packidx);
924 err = got_packidx_close(repo->packidx_cache[i]);
929 repo->packidx_cache[i] = packidx;
935 got_repo_is_packidx_filename(const char *name, size_t len)
937 if (len != GOT_PACKIDX_NAMELEN)
940 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
943 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
944 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
950 static struct got_packidx_bloom_filter *
951 get_packidx_bloom_filter(struct got_repository *repo,
952 const char *path, size_t path_len)
954 struct got_packidx_bloom_filter key;
956 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
957 return NULL; /* XXX */
958 key.path_len = path_len;
960 return RB_FIND(got_packidx_bloom_filter_tree,
961 &repo->packidx_bloom_filters, &key);
965 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
966 const char *path_packidx, struct got_object_id *id)
968 struct got_packidx_bloom_filter *bf;
970 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
972 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
974 /* No bloom filter means this pack index must be searched. */
978 static const struct got_error *
979 add_packidx_bloom_filter(struct got_repository *repo,
980 struct got_packidx *packidx, const char *path_packidx)
982 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
983 struct got_packidx_bloom_filter *bf;
987 * Don't use bloom filters for very large pack index files.
988 * Large pack files will contain a relatively large fraction
989 * of our objects so we will likely need to visit them anyway.
990 * The more objects a pack file contains the higher the probability
991 * of a false-positive match from the bloom filter. And reading
992 * all object IDs from a large pack index file can be expensive.
994 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
997 /* Do we already have a filter for this pack index? */
998 if (get_packidx_bloom_filter(repo, path_packidx,
999 strlen(path_packidx)) != NULL)
1002 bf = calloc(1, sizeof(*bf));
1004 return got_error_from_errno("calloc");
1005 bf->bloom = calloc(1, sizeof(*bf->bloom));
1006 if (bf->bloom == NULL) {
1008 return got_error_from_errno("calloc");
1011 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1012 if (len >= sizeof(bf->path)) {
1015 return got_error(GOT_ERR_NO_SPACE);
1019 /* Minimum size supported by our bloom filter is 1000 entries. */
1020 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1021 for (i = 0; i < nobjects; i++) {
1022 struct got_packidx_object_id *id;
1023 id = &packidx->hdr.sorted_ids[i];
1024 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1027 RB_INSERT(got_packidx_bloom_filter_tree,
1028 &repo->packidx_bloom_filters, bf);
1032 const struct got_error *
1033 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1034 struct got_repository *repo, struct got_object_id *id)
1036 const struct got_error *err;
1037 struct got_pathlist_entry *pe;
1040 /* Search pack index cache. */
1041 for (i = 0; i < repo->pack_cache_size; i++) {
1042 if (repo->packidx_cache[i] == NULL)
1044 if (!got_repo_check_packidx_bloom_filter(repo,
1045 repo->packidx_cache[i]->path_packidx, id))
1046 continue; /* object will not be found in this index */
1047 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1049 *packidx = repo->packidx_cache[i];
1051 * Move this cache entry to the front. Repeatedly
1052 * searching a wrong pack index can be expensive.
1055 memmove(&repo->packidx_cache[1],
1056 &repo->packidx_cache[0],
1057 i * sizeof(repo->packidx_cache[0]));
1058 repo->packidx_cache[0] = *packidx;
1059 if (repo->pinned_packidx >= 0 &&
1060 repo->pinned_packidx < i)
1061 repo->pinned_packidx++;
1062 else if (repo->pinned_packidx == i)
1063 repo->pinned_packidx = 0;
1068 /* No luck. Search the filesystem. */
1070 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1071 const char *path_packidx = pe->path;
1074 if (!got_repo_check_packidx_bloom_filter(repo,
1076 continue; /* object will not be found in this index */
1078 for (i = 0; i < repo->pack_cache_size; i++) {
1079 if (repo->packidx_cache[i] == NULL)
1081 if (strcmp(repo->packidx_cache[i]->path_packidx,
1082 path_packidx) == 0) {
1088 continue; /* already searched */
1090 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1095 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1099 err = cache_packidx(repo, *packidx, path_packidx);
1103 *idx = got_packidx_get_object_idx(*packidx, id);
1105 err = NULL; /* found the object */
1110 err = got_error_no_obj(id);
1115 const struct got_error *
1116 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1117 struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 DIR *packdir = NULL;
1121 struct dirent *dent;
1122 char *path_packidx = NULL;
1126 packdir_fd = openat(got_repo_get_fd(repo),
1127 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1128 if (packdir_fd == -1) {
1129 return got_error_from_errno_fmt("openat: %s/%s",
1130 got_repo_get_path_git_dir(repo),
1131 GOT_OBJECTS_PACK_DIR);
1134 packdir = fdopendir(packdir_fd);
1135 if (packdir == NULL) {
1136 err = got_error_from_errno("fdopendir");
1140 if (fstat(packdir_fd, &sb) == -1) {
1141 err = got_error_from_errno("fstat");
1144 repo->pack_path_mtime = sb.st_mtime;
1146 while ((dent = readdir(packdir)) != NULL) {
1147 if (!got_repo_is_packidx_filename(dent->d_name,
1148 strlen(dent->d_name)))
1151 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1152 dent->d_name) == -1) {
1153 err = got_error_from_errno("asprintf");
1154 path_packidx = NULL;
1158 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1165 if (packdir && closedir(packdir) != 0 && err == NULL)
1166 err = got_error_from_errno("closedir");
1170 const struct got_error *
1171 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1172 struct got_repository *repo)
1174 const struct got_error *err;
1179 /* Search pack index cache. */
1180 for (i = 0; i < repo->pack_cache_size; i++) {
1181 if (repo->packidx_cache[i] == NULL)
1183 if (strcmp(repo->packidx_cache[i]->path_packidx,
1184 path_packidx) == 0) {
1185 *packidx = repo->packidx_cache[i];
1189 /* No luck. Search the filesystem. */
1191 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1196 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1200 err = cache_packidx(repo, *packidx, path_packidx);
1203 got_packidx_close(*packidx);
1209 static const struct got_error *
1210 read_packfile_hdr(int fd, struct got_packidx *packidx)
1212 const struct got_error *err = NULL;
1213 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1214 struct got_packfile_hdr hdr;
1217 n = read(fd, &hdr, sizeof(hdr));
1219 return got_error_from_errno("read");
1220 if (n != sizeof(hdr))
1221 return got_error(GOT_ERR_BAD_PACKFILE);
1223 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1224 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1225 be32toh(hdr.nobjects) != totobj)
1226 err = got_error(GOT_ERR_BAD_PACKFILE);
1231 static const struct got_error *
1232 open_packfile(int *fd, struct got_repository *repo,
1233 const char *relpath, struct got_packidx *packidx)
1235 const struct got_error *err = NULL;
1237 *fd = openat(got_repo_get_fd(repo), relpath,
1238 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1240 return got_error_from_errno_fmt("openat: %s/%s",
1241 got_repo_get_path_git_dir(repo), relpath);
1244 err = read_packfile_hdr(*fd, packidx);
1254 const struct got_error *
1255 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1256 const char *path_packfile, struct got_packidx *packidx)
1258 const struct got_error *err = NULL;
1259 struct got_pack *pack = NULL;
1266 for (i = 0; i < repo->pack_cache_size; i++) {
1267 pack = &repo->packs[i];
1268 if (pack->path_packfile == NULL)
1270 if (strcmp(pack->path_packfile, path_packfile) == 0)
1271 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1274 if (i == repo->pack_cache_size) {
1275 struct got_pack tmp;
1278 } while (i > 0 && repo->pinned_pack >= 0 &&
1279 i == repo->pinned_pack);
1280 err = got_pack_close(&repo->packs[i]);
1283 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1284 return got_error_from_errno("ftruncate");
1285 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1286 return got_error_from_errno("ftruncate");
1287 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1288 memcpy(&repo->packs[i], &repo->packs[0],
1289 sizeof(repo->packs[i]));
1290 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1291 if (repo->pinned_pack == 0)
1292 repo->pinned_pack = i;
1293 else if (repo->pinned_pack == i)
1294 repo->pinned_pack = 0;
1298 pack = &repo->packs[i];
1300 pack->path_packfile = strdup(path_packfile);
1301 if (pack->path_packfile == NULL) {
1302 err = got_error_from_errno("strdup");
1306 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1310 if (fstat(pack->fd, &sb) != 0) {
1311 err = got_error_from_errno("fstat");
1314 pack->filesize = sb.st_size;
1316 pack->privsep_child = NULL;
1318 #ifndef GOT_PACK_NO_MMAP
1319 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1321 if (pack->map == MAP_FAILED) {
1322 if (errno != ENOMEM) {
1323 err = got_error_from_errno("mmap");
1326 pack->map = NULL; /* fall back to read(2) */
1332 free(pack->path_packfile);
1333 memset(pack, 0, sizeof(*pack));
1341 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1343 struct got_pack *pack = NULL;
1346 for (i = 0; i < repo->pack_cache_size; i++) {
1347 pack = &repo->packs[i];
1348 if (pack->path_packfile == NULL)
1350 if (strcmp(pack->path_packfile, path_packfile) == 0)
1357 const struct got_error *
1358 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1359 struct got_pack *pack)
1362 int pinned_pack = -1, pinned_packidx = -1;
1364 for (i = 0; i < repo->pack_cache_size; i++) {
1365 if (repo->packidx_cache[i] &&
1366 strcmp(repo->packidx_cache[i]->path_packidx,
1367 packidx->path_packidx) == 0)
1369 if (repo->packs[i].path_packfile &&
1370 strcmp(repo->packs[i].path_packfile,
1371 pack->path_packfile) == 0)
1375 if (pinned_packidx == -1 || pinned_pack == -1)
1376 return got_error(GOT_ERR_PIN_PACK);
1378 repo->pinned_pack = pinned_pack;
1379 repo->pinned_packidx = pinned_packidx;
1380 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1385 got_repo_get_pinned_pack(struct got_repository *repo)
1387 if (repo->pinned_pack >= 0 &&
1388 repo->pinned_pack < repo->pack_cache_size)
1389 return &repo->packs[repo->pinned_pack];
1395 got_repo_unpin_pack(struct got_repository *repo)
1397 repo->pinned_packidx = -1;
1398 repo->pinned_pack = -1;
1399 repo->pinned_pid = 0;
1402 const struct got_error *
1403 got_repo_init(const char *repo_path, const char *head_name)
1405 const struct got_error *err = NULL;
1406 const char *dirnames[] = {
1408 GOT_OBJECTS_PACK_DIR,
1411 const char *description_str = "Unnamed repository; "
1412 "edit this file 'description' to name the repository.";
1413 const char *headref = "ref: refs/heads/";
1414 const char *gitconfig_str = "[core]\n"
1415 "\trepositoryformatversion = 0\n"
1416 "\tfilemode = true\n"
1418 char *headref_str, *path;
1421 if (!got_path_dir_is_empty(repo_path))
1422 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1424 for (i = 0; i < nitems(dirnames); i++) {
1425 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1426 return got_error_from_errno("asprintf");
1428 err = got_path_mkdir(path);
1434 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1435 return got_error_from_errno("asprintf");
1436 err = got_path_create_file(path, description_str);
1441 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1442 return got_error_from_errno("asprintf");
1443 if (asprintf(&headref_str, "%s%s", headref,
1444 head_name ? head_name : "main") == -1) {
1446 return got_error_from_errno("asprintf");
1448 err = got_path_create_file(path, headref_str);
1454 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1455 return got_error_from_errno("asprintf");
1456 err = got_path_create_file(path, gitconfig_str);
1465 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1467 struct got_pathlist_entry *pe;
1469 while (!TAILQ_EMPTY(packidx_paths)) {
1470 pe = TAILQ_FIRST(packidx_paths);
1471 TAILQ_REMOVE(packidx_paths, pe, entry);
1472 free((char *)pe->path);
1477 static const struct got_error *
1478 match_packed_object(struct got_object_id **unique_id,
1479 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1481 const struct got_error *err = NULL;
1482 char *objects_pack_dir = NULL;
1483 struct got_object_id_queue matched_ids;
1484 struct got_pathlist_entry *pe;
1487 STAILQ_INIT(&matched_ids);
1489 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1490 if (objects_pack_dir == NULL)
1491 return got_error_from_errno("got_repo_get_path_objects_pack");
1493 if (stat(objects_pack_dir, &sb) == -1) {
1494 if (errno != ENOENT) {
1495 err = got_error_from_errno2("stat", objects_pack_dir);
1498 } else if (sb.st_mtime != repo->pack_path_mtime) {
1499 purge_packidx_paths(&repo->packidx_paths);
1500 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1505 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1506 const char *path_packidx = pe->path;
1507 struct got_packidx *packidx;
1508 struct got_object_qid *qid;
1510 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1515 err = got_packidx_match_id_str_prefix(&matched_ids,
1516 packidx, id_str_prefix);
1518 got_packidx_close(packidx);
1521 err = got_packidx_close(packidx);
1525 STAILQ_FOREACH(qid, &matched_ids, entry) {
1526 if (obj_type != GOT_OBJ_TYPE_ANY) {
1528 err = got_object_get_type(&matched_type, repo,
1532 if (matched_type != obj_type)
1535 if (*unique_id == NULL) {
1536 *unique_id = got_object_id_dup(&qid->id);
1537 if (*unique_id == NULL) {
1538 err = got_error_from_errno("malloc");
1542 if (got_object_id_cmp(*unique_id,
1544 continue; /* packed multiple times */
1545 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1551 free(objects_pack_dir);
1552 got_object_id_queue_free(&matched_ids);
1560 static const struct got_error *
1561 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1562 const char *object_dir, const char *id_str_prefix, int obj_type,
1563 struct got_repository *repo)
1565 const struct got_error *err = NULL;
1566 char *path, *id_str = NULL;
1568 struct dirent *dent;
1569 struct got_object_id id;
1571 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1572 err = got_error_from_errno("asprintf");
1576 dir = opendir(path);
1578 if (errno == ENOENT) {
1582 err = got_error_from_errno2("opendir", path);
1585 while ((dent = readdir(dir)) != NULL) {
1591 if (strcmp(dent->d_name, ".") == 0 ||
1592 strcmp(dent->d_name, "..") == 0)
1595 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1596 err = got_error_from_errno("asprintf");
1600 if (!got_parse_sha1_digest(id.sha1, id_str))
1604 * Directory entries do not necessarily appear in
1605 * sorted order, so we must iterate over all of them.
1607 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1611 if (*unique_id == NULL) {
1612 if (obj_type != GOT_OBJ_TYPE_ANY) {
1614 err = got_object_get_type(&matched_type, repo,
1618 if (matched_type != obj_type)
1621 *unique_id = got_object_id_dup(&id);
1622 if (*unique_id == NULL) {
1623 err = got_error_from_errno("got_object_id_dup");
1627 if (got_object_id_cmp(*unique_id, &id) == 0)
1628 continue; /* both packed and loose */
1629 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1634 if (dir && closedir(dir) != 0 && err == NULL)
1635 err = got_error_from_errno("closedir");
1645 const struct got_error *
1646 got_repo_match_object_id_prefix(struct got_object_id **id,
1647 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1649 const struct got_error *err = NULL;
1650 char *path_objects = NULL, *object_dir = NULL;
1656 path_objects = got_repo_get_path_objects(repo);
1658 len = strlen(id_str_prefix);
1659 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1660 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1664 for (i = 0; i < len; i++) {
1665 if (isxdigit((unsigned char)id_str_prefix[i]))
1667 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1672 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1675 object_dir = strndup(id_str_prefix, 2);
1676 if (object_dir == NULL) {
1677 err = got_error_from_errno("strdup");
1680 err = match_loose_object(id, path_objects, object_dir,
1681 id_str_prefix, obj_type, repo);
1682 } else if (len == 1) {
1684 for (i = 0; i < 0xf; i++) {
1685 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1687 err = got_error_from_errno("asprintf");
1690 err = match_packed_object(id, repo, object_dir,
1694 err = match_loose_object(id, path_objects, object_dir,
1695 id_str_prefix, obj_type, repo);
1700 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1709 } else if (*id == NULL) {
1711 case GOT_OBJ_TYPE_BLOB:
1712 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1713 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1715 case GOT_OBJ_TYPE_TREE:
1716 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1717 GOT_OBJ_LABEL_TREE, id_str_prefix);
1719 case GOT_OBJ_TYPE_COMMIT:
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1723 case GOT_OBJ_TYPE_TAG:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_TAG, id_str_prefix);
1728 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1736 const struct got_error *
1737 got_repo_match_object_id(struct got_object_id **id, char **label,
1738 const char *id_str, int obj_type, struct got_reflist_head *refs,
1739 struct got_repository *repo)
1741 const struct got_error *err;
1742 struct got_tag_object *tag;
1743 struct got_reference *ref = NULL;
1750 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1753 *id = got_object_id_dup(
1754 got_object_tag_get_object_id(tag));
1756 err = got_error_from_errno("got_object_id_dup");
1757 else if (label && asprintf(label, "refs/tags/%s",
1758 got_object_tag_get_name(tag)) == -1) {
1759 err = got_error_from_errno("asprintf");
1763 got_object_tag_close(tag);
1765 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1766 err->code != GOT_ERR_NO_OBJ)
1770 err = got_ref_open(&ref, repo, id_str, 0);
1772 err = got_ref_resolve(id, repo, ref);
1776 *label = strdup(got_ref_get_name(ref));
1777 if (*label == NULL) {
1778 err = got_error_from_errno("strdup");
1783 if (err->code != GOT_ERR_NOT_REF &&
1784 err->code != GOT_ERR_BAD_REF_NAME)
1786 err = got_repo_match_object_id_prefix(id, id_str,
1789 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1790 err = got_error_not_ref(id_str);
1794 err = got_object_id_str(label, *id);
1795 if (*label == NULL) {
1796 err = got_error_from_errno("strdup");
1807 const struct got_error *
1808 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1809 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_reflist_entry *re;
1813 struct got_object_id *tag_id;
1814 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1818 TAILQ_FOREACH(re, refs, entry) {
1819 const char *refname;
1820 refname = got_ref_get_name(re->ref);
1821 if (got_ref_is_symbolic(re->ref))
1823 if (strncmp(refname, "refs/tags/", 10) != 0)
1825 if (!name_is_absolute)
1826 refname += strlen("refs/tags/");
1827 if (strcmp(refname, name) != 0)
1829 err = got_ref_resolve(&tag_id, repo, re->ref);
1832 err = got_object_open_as_tag(tag, repo, tag_id);
1836 if (obj_type == GOT_OBJ_TYPE_ANY ||
1837 got_object_tag_get_object_type(*tag) == obj_type)
1839 got_object_tag_close(*tag);
1843 if (err == NULL && *tag == NULL)
1844 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1845 GOT_OBJ_LABEL_TAG, name);
1849 static const struct got_error *
1850 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1851 const char *name, mode_t mode, struct got_object_id *blob_id)
1853 const struct got_error *err = NULL;
1857 *new_te = calloc(1, sizeof(**new_te));
1858 if (*new_te == NULL)
1859 return got_error_from_errno("calloc");
1861 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1862 sizeof((*new_te)->name)) {
1863 err = got_error(GOT_ERR_NO_SPACE);
1867 if (S_ISLNK(mode)) {
1868 (*new_te)->mode = S_IFLNK;
1870 (*new_te)->mode = S_IFREG;
1871 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1873 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1875 if (err && *new_te) {
1882 static const struct got_error *
1883 import_file(struct got_tree_entry **new_te, struct dirent *de,
1884 const char *path, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_object_id *blob_id = NULL;
1891 if (asprintf(&filepath, "%s%s%s", path,
1892 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1893 return got_error_from_errno("asprintf");
1895 if (lstat(filepath, &sb) != 0) {
1896 err = got_error_from_errno2("lstat", path);
1900 err = got_object_blob_create(&blob_id, filepath, repo);
1904 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1913 static const struct got_error *
1914 insert_tree_entry(struct got_tree_entry *new_te,
1915 struct got_pathlist_head *paths)
1917 const struct got_error *err = NULL;
1918 struct got_pathlist_entry *new_pe;
1920 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1924 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1928 static const struct got_error *write_tree(struct got_object_id **,
1929 const char *, struct got_pathlist_head *, struct got_repository *,
1930 got_repo_import_cb progress_cb, void *progress_arg);
1932 static const struct got_error *
1933 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1934 const char *path, struct got_pathlist_head *ignores,
1935 struct got_repository *repo,
1936 got_repo_import_cb progress_cb, void *progress_arg)
1938 const struct got_error *err;
1939 struct got_object_id *id = NULL;
1942 if (asprintf(&subdirpath, "%s%s%s", path,
1943 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1944 return got_error_from_errno("asprintf");
1946 (*new_te) = calloc(1, sizeof(**new_te));
1947 if (*new_te == NULL)
1948 return got_error_from_errno("calloc");
1949 (*new_te)->mode = S_IFDIR;
1950 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1951 sizeof((*new_te)->name)) {
1952 err = got_error(GOT_ERR_NO_SPACE);
1955 err = write_tree(&id, subdirpath, ignores, repo,
1956 progress_cb, progress_arg);
1959 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1971 static const struct got_error *
1972 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1973 struct got_pathlist_head *ignores, struct got_repository *repo,
1974 got_repo_import_cb progress_cb, void *progress_arg)
1976 const struct got_error *err = NULL;
1980 struct got_tree_entry *new_te = NULL;
1981 struct got_pathlist_head paths;
1982 struct got_pathlist_entry *pe;
1984 *new_tree_id = NULL;
1988 dir = opendir(path_dir);
1990 err = got_error_from_errno2("opendir", path_dir);
1995 while ((de = readdir(dir)) != NULL) {
1999 if (strcmp(de->d_name, ".") == 0 ||
2000 strcmp(de->d_name, "..") == 0)
2003 TAILQ_FOREACH(pe, ignores, entry) {
2004 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2012 err = got_path_dirent_type(&type, path_dir, de);
2016 if (type == DT_DIR) {
2017 err = import_subdir(&new_te, de, path_dir,
2018 ignores, repo, progress_cb, progress_arg);
2020 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2025 } else if (type == DT_REG || type == DT_LNK) {
2026 err = import_file(&new_te, de, path_dir, repo);
2032 err = insert_tree_entry(new_te, &paths);
2038 if (TAILQ_EMPTY(&paths)) {
2039 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2040 "cannot create tree without any entries");
2044 TAILQ_FOREACH(pe, &paths, entry) {
2045 struct got_tree_entry *te = pe->data;
2047 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2049 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2050 err = got_error_from_errno("asprintf");
2053 err = (*progress_cb)(progress_arg, path);
2059 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2063 got_pathlist_free(&paths);
2067 const struct got_error *
2068 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2069 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2070 struct got_repository *repo, got_repo_import_cb progress_cb,
2073 const struct got_error *err;
2074 struct got_object_id *new_tree_id;
2076 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2077 progress_cb, progress_arg);
2081 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2082 author, time(NULL), author, time(NULL), logmsg, repo);
2087 const struct got_error *
2088 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2089 struct got_repository *repo)
2091 const struct got_error *err = NULL;
2092 char *path_objects = NULL, *path = NULL;
2094 struct got_object_id id;
2100 path_objects = got_repo_get_path_objects(repo);
2101 if (path_objects == NULL)
2102 return got_error_from_errno("got_repo_get_path_objects");
2104 for (i = 0; i <= 0xff; i++) {
2105 struct dirent *dent;
2107 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2108 err = got_error_from_errno("asprintf");
2112 dir = opendir(path);
2114 if (errno == ENOENT) {
2118 err = got_error_from_errno2("opendir", path);
2122 while ((dent = readdir(dir)) != NULL) {
2127 if (strcmp(dent->d_name, ".") == 0 ||
2128 strcmp(dent->d_name, "..") == 0)
2131 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2132 err = got_error_from_errno("asprintf");
2136 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2142 err = got_object_open_loose_fd(&fd, &id, repo);
2146 if (fstat(fd, &sb) == -1) {
2147 err = got_error_from_errno("fstat");
2152 (*ondisk_size) += sb.st_size;
2154 if (close(fd) == -1) {
2155 err = got_error_from_errno("close");
2160 if (closedir(dir) != 0) {
2161 err = got_error_from_errno("closedir");
2170 if (dir && closedir(dir) != 0 && err == NULL)
2171 err = got_error_from_errno("closedir");
2182 const struct got_error *
2183 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2184 off_t *total_packsize, struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 DIR *packdir = NULL;
2188 struct dirent *dent;
2189 struct got_packidx *packidx = NULL;
2191 char *path_packfile;
2197 *total_packsize = 0;
2199 packdir_fd = openat(got_repo_get_fd(repo),
2200 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2201 if (packdir_fd == -1) {
2202 return got_error_from_errno_fmt("openat: %s/%s",
2203 got_repo_get_path_git_dir(repo),
2204 GOT_OBJECTS_PACK_DIR);
2207 packdir = fdopendir(packdir_fd);
2208 if (packdir == NULL) {
2209 err = got_error_from_errno("fdopendir");
2213 while ((dent = readdir(packdir)) != NULL) {
2214 if (!got_repo_is_packidx_filename(dent->d_name,
2215 strlen(dent->d_name)))
2218 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2219 dent->d_name) == -1) {
2220 err = got_error_from_errno("asprintf");
2224 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2230 if (fstat(packidx->fd, &sb) == -1)
2232 *total_packsize += sb.st_size;
2234 err = got_packidx_get_packfile_path(&path_packfile,
2235 packidx->path_packidx);
2239 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2241 free(path_packfile);
2244 free(path_packfile);
2245 *total_packsize += sb.st_size;
2247 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2251 got_packidx_close(packidx);
2256 got_packidx_close(packidx);
2257 if (packdir && closedir(packdir) != 0 && err == NULL)
2258 err = got_error_from_errno("closedir");
2262 *total_packsize = 0;
2267 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2268 got_packidx_bloom_filter_cmp);