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_delta_cache.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
69 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 got_packidx_bloom_filter_cmp);
73 got_repo_get_path(struct got_repository *repo)
79 got_repo_get_path_git_dir(struct got_repository *repo)
81 return repo->path_git_dir;
85 got_repo_get_fd(struct got_repository *repo)
87 return repo->gitdir_fd;
91 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 return repo->gitconfig_author_name;
97 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 return repo->gitconfig_author_email;
103 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
105 return repo->global_gitconfig_author_name;
109 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
111 return repo->global_gitconfig_author_email;
115 got_repo_get_gitconfig_owner(struct got_repository *repo)
117 return repo->gitconfig_owner;
121 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
122 struct got_repository *repo)
124 *extensions = repo->extensions;
125 *nextensions = repo->nextensions;
129 got_repo_is_bare(struct got_repository *repo)
131 return (strcmp(repo->path, repo->path_git_dir) == 0);
135 get_path_git_child(struct got_repository *repo, const char *basename)
139 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
147 got_repo_get_path_objects(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_OBJECTS_DIR);
153 got_repo_get_path_objects_pack(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
159 got_repo_get_path_refs(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_REFS_DIR);
165 got_repo_get_path_packed_refs(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
171 get_path_head(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_HEAD_FILE);
177 got_repo_get_path_gitconfig(struct got_repository *repo)
179 return get_path_git_child(repo, GOT_GITCONFIG);
183 got_repo_get_path_gotconfig(struct got_repository *repo)
185 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
188 const struct got_gotconfig *
189 got_repo_get_gotconfig(struct got_repository *repo)
191 return repo->gotconfig;
195 got_repo_get_gitconfig_remotes(int *nremotes,
196 const struct got_remote_repo **remotes, struct got_repository *repo)
198 *nremotes = repo->ngitconfig_remotes;
199 *remotes = repo->gitconfig_remotes;
203 is_git_repo(struct got_repository *repo)
205 const char *path_git = got_repo_get_path_git_dir(repo);
206 char *path_objects = got_repo_get_path_objects(repo);
207 char *path_refs = got_repo_get_path_refs(repo);
208 char *path_head = get_path_head(repo);
211 struct got_reference *head_ref;
213 if (lstat(path_git, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_objects, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_refs, &sb) == -1)
225 if (!S_ISDIR(sb.st_mode))
228 if (lstat(path_head, &sb) == -1)
230 if (!S_ISREG(sb.st_mode))
233 /* Check if the HEAD reference can be opened. */
234 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
236 got_ref_close(head_ref);
247 static const struct got_error *
248 close_tempfiles(int *fds, size_t nfds)
250 const struct got_error *err = NULL;
253 for (i = 0; i < nfds; i++) {
256 if (close(fds[i]) == -1) {
257 err = got_error_from_errno("close");
265 static const struct got_error *
266 open_tempfiles(int **fds, size_t nfds)
268 const struct got_error *err = NULL;
271 *fds = calloc(nfds, sizeof(**fds));
273 return got_error_from_errno("calloc");
275 for (i = 0; i < nfds; i++)
278 for (i = 0; i < nfds; i++) {
279 (*fds)[i] = got_opentempfd();
280 if ((*fds)[i] == -1) {
281 err = got_error_from_errno("got_opentempfd");
282 close_tempfiles(*fds, nfds);
291 const struct got_error *
292 got_repo_pack_fds_open(int **pack_fds)
294 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
297 const struct got_error *
298 got_repo_pack_fds_close(int *pack_fds)
300 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
303 const struct got_error *
304 got_repo_temp_fds_open(int **temp_fds)
306 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
310 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
314 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
315 repo->tempfiles[i] = temp_fds[i];
318 const struct got_error *
319 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
326 for (i = 0; i < nitems(repo->tempfiles); i++) {
327 if (repo->tempfile_use_mask & (1 << i))
329 if (repo->tempfiles[i] != -1) {
330 if (ftruncate(repo->tempfiles[i], 0L) == -1)
331 return got_error_from_errno("ftruncate");
332 *fd = repo->tempfiles[i];
334 repo->tempfile_use_mask |= (1 << i);
339 return got_error(GOT_ERR_REPO_TEMPFILE);
343 got_repo_temp_fds_put(int idx, struct got_repository *repo)
345 repo->tempfile_use_mask &= ~(1 << idx);
348 const struct got_error *
349 got_repo_temp_fds_close(int *temp_fds)
351 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
354 const struct got_error *
355 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
356 struct got_object *obj)
358 #ifndef GOT_NO_OBJ_CACHE
359 const struct got_error *err = NULL;
360 err = got_object_cache_add(&repo->objcache, id, obj);
362 if (err->code == GOT_ERR_OBJ_EXISTS ||
363 err->code == GOT_ERR_OBJ_TOO_LARGE)
373 got_repo_get_cached_object(struct got_repository *repo,
374 struct got_object_id *id)
376 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
379 const struct got_error *
380 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
381 struct got_tree_object *tree)
383 #ifndef GOT_NO_OBJ_CACHE
384 const struct got_error *err = NULL;
385 err = got_object_cache_add(&repo->treecache, id, tree);
387 if (err->code == GOT_ERR_OBJ_EXISTS ||
388 err->code == GOT_ERR_OBJ_TOO_LARGE)
397 struct got_tree_object *
398 got_repo_get_cached_tree(struct got_repository *repo,
399 struct got_object_id *id)
401 return (struct got_tree_object *)got_object_cache_get(
402 &repo->treecache, id);
405 const struct got_error *
406 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
407 struct got_commit_object *commit)
409 #ifndef GOT_NO_OBJ_CACHE
410 const struct got_error *err = NULL;
411 err = got_object_cache_add(&repo->commitcache, id, commit);
413 if (err->code == GOT_ERR_OBJ_EXISTS ||
414 err->code == GOT_ERR_OBJ_TOO_LARGE)
423 struct got_commit_object *
424 got_repo_get_cached_commit(struct got_repository *repo,
425 struct got_object_id *id)
427 return (struct got_commit_object *)got_object_cache_get(
428 &repo->commitcache, id);
431 const struct got_error *
432 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
433 struct got_tag_object *tag)
435 #ifndef GOT_NO_OBJ_CACHE
436 const struct got_error *err = NULL;
437 err = got_object_cache_add(&repo->tagcache, id, tag);
439 if (err->code == GOT_ERR_OBJ_EXISTS ||
440 err->code == GOT_ERR_OBJ_TOO_LARGE)
449 struct got_tag_object *
450 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
452 return (struct got_tag_object *)got_object_cache_get(
453 &repo->tagcache, id);
456 const struct got_error *
457 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
458 struct got_raw_object *raw)
460 #ifndef GOT_NO_OBJ_CACHE
461 const struct got_error *err = NULL;
462 err = got_object_cache_add(&repo->rawcache, id, raw);
464 if (err->code == GOT_ERR_OBJ_EXISTS ||
465 err->code == GOT_ERR_OBJ_TOO_LARGE)
475 struct got_raw_object *
476 got_repo_get_cached_raw_object(struct got_repository *repo,
477 struct got_object_id *id)
479 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
483 static const struct got_error *
484 open_repo(struct got_repository *repo, const char *path)
486 const struct got_error *err = NULL;
488 repo->gitdir_fd = -1;
490 /* bare git repository? */
491 repo->path_git_dir = strdup(path);
492 if (repo->path_git_dir == NULL)
493 return got_error_from_errno("strdup");
494 if (is_git_repo(repo)) {
495 repo->path = strdup(repo->path_git_dir);
496 if (repo->path == NULL) {
497 err = got_error_from_errno("strdup");
500 repo->gitdir_fd = open(repo->path_git_dir,
501 O_DIRECTORY | O_CLOEXEC);
502 if (repo->gitdir_fd == -1) {
503 err = got_error_from_errno2("open",
510 /* git repository with working tree? */
511 free(repo->path_git_dir);
512 repo->path_git_dir = NULL;
513 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
514 err = got_error_from_errno("asprintf");
517 if (is_git_repo(repo)) {
518 repo->path = strdup(path);
519 if (repo->path == NULL) {
520 err = got_error_from_errno("strdup");
523 repo->gitdir_fd = open(repo->path_git_dir,
524 O_DIRECTORY | O_CLOEXEC);
525 if (repo->gitdir_fd == -1) {
526 err = got_error_from_errno2("open",
533 err = got_error(GOT_ERR_NOT_GIT_REPO);
538 free(repo->path_git_dir);
539 repo->path_git_dir = NULL;
540 if (repo->gitdir_fd != -1)
541 close(repo->gitdir_fd);
542 repo->gitdir_fd = -1;
548 static const struct got_error *
549 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
551 const struct got_error *err = NULL;
552 char *repo_gitconfig_path = NULL;
554 if (global_gitconfig_path) {
555 /* Read settings from ~/.gitconfig. */
556 int dummy_repo_version;
557 err = got_repo_read_gitconfig(&dummy_repo_version,
558 &repo->global_gitconfig_author_name,
559 &repo->global_gitconfig_author_email,
560 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
565 /* Read repository's .git/config file. */
566 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
567 if (repo_gitconfig_path == NULL)
568 return got_error_from_errno("got_repo_get_path_gitconfig");
570 err = got_repo_read_gitconfig(
571 &repo->gitconfig_repository_format_version,
572 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
573 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
574 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
575 repo_gitconfig_path);
579 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
582 for (i = 0; i < repo->ngitconfig_remotes; i++) {
583 got_repo_free_remote_repo_data(
584 &repo->gitconfig_remotes[i]);
586 free(repo->gitconfig_remotes);
587 repo->gitconfig_remotes = NULL;
588 repo->ngitconfig_remotes = 0;
590 free(repo->gitconfig_author_name);
591 repo->gitconfig_author_name = NULL;
592 free(repo->gitconfig_author_email);
593 repo->gitconfig_author_email = NULL;
595 free(repo->global_gitconfig_author_name);
596 repo->global_gitconfig_author_name = NULL;
597 free(repo->global_gitconfig_author_email);
598 repo->global_gitconfig_author_email = NULL;
602 free(repo_gitconfig_path);
606 static const struct got_error *
607 read_gotconfig(struct got_repository *repo)
609 const struct got_error *err = NULL;
610 char *gotconfig_path;
612 gotconfig_path = got_repo_get_path_gotconfig(repo);
613 if (gotconfig_path == NULL)
614 return got_error_from_errno("got_repo_get_path_gotconfig");
616 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
617 free(gotconfig_path);
621 /* Supported repository format extensions. */
622 static const char *const repo_extensions[] = {
623 "noop", /* Got supports repository format version 1. */
624 "preciousObjects", /* Supported by gotadmin cleanup. */
625 "worktreeConfig", /* Got does not care about Git work trees. */
628 const struct got_error *
629 got_repo_open(struct got_repository **repop, const char *path,
630 const char *global_gitconfig_path, int *pack_fds)
632 struct got_repository *repo = NULL;
633 const struct got_error *err = NULL;
634 char *repo_path = NULL;
640 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
641 return got_error_from_errno("getrlimit");
643 repo = calloc(1, sizeof(*repo));
645 return got_error_from_errno("calloc");
647 RB_INIT(&repo->packidx_bloom_filters);
648 TAILQ_INIT(&repo->packidx_paths);
650 for (i = 0; i < nitems(repo->privsep_children); i++) {
651 memset(&repo->privsep_children[i], 0,
652 sizeof(repo->privsep_children[0]));
653 repo->privsep_children[i].imsg_fd = -1;
656 err = got_object_cache_init(&repo->objcache,
657 GOT_OBJECT_CACHE_TYPE_OBJ);
660 err = got_object_cache_init(&repo->treecache,
661 GOT_OBJECT_CACHE_TYPE_TREE);
664 err = got_object_cache_init(&repo->commitcache,
665 GOT_OBJECT_CACHE_TYPE_COMMIT);
668 err = got_object_cache_init(&repo->tagcache,
669 GOT_OBJECT_CACHE_TYPE_TAG);
672 err = got_object_cache_init(&repo->rawcache,
673 GOT_OBJECT_CACHE_TYPE_RAW);
677 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
678 if (repo->pack_cache_size > rl.rlim_cur / 8)
679 repo->pack_cache_size = rl.rlim_cur / 8;
680 for (i = 0; i < nitems(repo->packs); i++) {
681 if (pack_fds != NULL && i < repo->pack_cache_size) {
682 repo->packs[i].basefd = pack_fds[j++];
683 repo->packs[i].accumfd = pack_fds[j++];
685 repo->packs[i].basefd = -1;
686 repo->packs[i].accumfd = -1;
689 for (i = 0; i < nitems(repo->tempfiles); i++)
690 repo->tempfiles[i] = -1;
691 repo->pinned_pack = -1;
692 repo->pinned_packidx = -1;
693 repo->pinned_pid = 0;
695 repo_path = realpath(path, NULL);
696 if (repo_path == NULL) {
697 err = got_error_from_errno2("realpath", path);
704 err = open_repo(repo, repo_path);
707 if (err->code != GOT_ERR_NOT_GIT_REPO)
709 if (repo_path[0] == '/' && repo_path[1] == '\0') {
710 err = got_error(GOT_ERR_NOT_GIT_REPO);
713 err = got_path_dirname(&parent_path, repo_path);
717 repo_path = parent_path;
720 err = read_gotconfig(repo);
724 err = read_gitconfig(repo, global_gitconfig_path);
727 if (repo->gitconfig_repository_format_version != 0) {
728 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
731 for (i = 0; i < repo->nextensions; i++) {
732 char *ext = repo->extensions[i];
733 int j, supported = 0;
734 for (j = 0; j < nitems(repo_extensions); j++) {
735 if (strcmp(ext, repo_extensions[j]) == 0) {
741 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
746 err = got_repo_list_packidx(&repo->packidx_paths, repo);
749 got_repo_close(repo);
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
761 struct got_pathlist_entry *pe;
764 for (i = 0; i < repo->pack_cache_size; i++) {
765 if (repo->packidx_cache[i] == NULL)
767 got_packidx_close(repo->packidx_cache[i]);
770 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
771 &repo->packidx_bloom_filters))) {
772 RB_REMOVE(got_packidx_bloom_filter_tree,
773 &repo->packidx_bloom_filters, bf);
774 bloom_free(bf->bloom);
779 for (i = 0; i < repo->pack_cache_size; i++)
780 if (repo->packs[i].path_packfile)
781 if (repo->packs[i].path_packfile)
782 got_pack_close(&repo->packs[i]);
785 free(repo->path_git_dir);
787 got_object_cache_close(&repo->objcache);
788 got_object_cache_close(&repo->treecache);
789 got_object_cache_close(&repo->commitcache);
790 got_object_cache_close(&repo->tagcache);
791 got_object_cache_close(&repo->rawcache);
793 for (i = 0; i < nitems(repo->privsep_children); i++) {
794 if (repo->privsep_children[i].imsg_fd == -1)
796 imsg_clear(repo->privsep_children[i].ibuf);
797 free(repo->privsep_children[i].ibuf);
798 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
799 child_err = got_privsep_wait_for_child(
800 repo->privsep_children[i].pid);
801 if (child_err && err == NULL)
803 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
805 err = got_error_from_errno("close");
808 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
810 err = got_error_from_errno("close");
813 got_gotconfig_free(repo->gotconfig);
814 free(repo->gitconfig_author_name);
815 free(repo->gitconfig_author_email);
816 for (i = 0; i < repo->ngitconfig_remotes; i++)
817 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
818 free(repo->gitconfig_remotes);
819 for (i = 0; i < repo->nextensions; i++)
820 free(repo->extensions[i]);
821 free(repo->extensions);
823 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
824 free((void *)pe->path);
825 got_pathlist_free(&repo->packidx_paths);
832 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
838 free(repo->fetch_url);
839 repo->fetch_url = NULL;
840 free(repo->send_url);
841 repo->send_url = NULL;
842 for (i = 0; i < repo->nfetch_branches; i++)
843 free(repo->fetch_branches[i]);
844 free(repo->fetch_branches);
845 repo->fetch_branches = NULL;
846 repo->nfetch_branches = 0;
847 for (i = 0; i < repo->nsend_branches; i++)
848 free(repo->send_branches[i]);
849 free(repo->send_branches);
850 repo->send_branches = NULL;
851 repo->nsend_branches = 0;
854 const struct got_error *
855 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
856 const char *input_path)
858 const struct got_error *err = NULL;
859 const char *repo_abspath = NULL;
861 char *canonpath, *path = NULL;
863 *in_repo_path = NULL;
865 canonpath = strdup(input_path);
866 if (canonpath == NULL) {
867 err = got_error_from_errno("strdup");
870 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
874 repo_abspath = got_repo_get_path(repo);
876 if (canonpath[0] == '\0') {
877 path = strdup(canonpath);
879 err = got_error_from_errno("strdup");
883 path = realpath(canonpath, NULL);
885 if (errno != ENOENT) {
886 err = got_error_from_errno2("realpath",
891 * Path is not on disk.
892 * Assume it is already relative to repository root.
894 path = strdup(canonpath);
896 err = got_error_from_errno("strdup");
901 repolen = strlen(repo_abspath);
905 if (strcmp(path, repo_abspath) == 0) {
909 err = got_error_from_errno("strdup");
912 } else if (len > repolen &&
913 got_path_is_child(path, repo_abspath, repolen)) {
914 /* Matched an on-disk path inside repository. */
915 if (got_repo_is_bare(repo)) {
917 * Matched an on-disk path inside repository
918 * database. Treat input as repository-relative.
925 /* Strip common prefix with repository path. */
926 err = got_path_skip_common_ancestor(&child,
935 * Matched unrelated on-disk path.
936 * Treat input as repository-relative.
944 /* Make in-repository path absolute */
945 if (path[0] != '/') {
947 if (asprintf(&abspath, "/%s", path) == -1) {
948 err = got_error_from_errno("asprintf");
960 *in_repo_path = path;
964 static const struct got_error *
965 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
966 const char *path_packidx)
968 const struct got_error *err = NULL;
971 for (i = 0; i < repo->pack_cache_size; i++) {
972 if (repo->packidx_cache[i] == NULL)
974 if (strcmp(repo->packidx_cache[i]->path_packidx,
975 path_packidx) == 0) {
976 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
979 if (i == repo->pack_cache_size) {
982 } while (i > 0 && repo->pinned_packidx >= 0 &&
983 i == repo->pinned_packidx);
984 err = got_packidx_close(repo->packidx_cache[i]);
989 repo->packidx_cache[i] = packidx;
995 got_repo_is_packidx_filename(const char *name, size_t len)
997 if (len != GOT_PACKIDX_NAMELEN)
1000 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1003 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1004 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1010 static struct got_packidx_bloom_filter *
1011 get_packidx_bloom_filter(struct got_repository *repo,
1012 const char *path, size_t path_len)
1014 struct got_packidx_bloom_filter key;
1016 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1017 return NULL; /* XXX */
1018 key.path_len = path_len;
1020 return RB_FIND(got_packidx_bloom_filter_tree,
1021 &repo->packidx_bloom_filters, &key);
1025 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1026 const char *path_packidx, struct got_object_id *id)
1028 struct got_packidx_bloom_filter *bf;
1030 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1032 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1034 /* No bloom filter means this pack index must be searched. */
1038 static const struct got_error *
1039 add_packidx_bloom_filter(struct got_repository *repo,
1040 struct got_packidx *packidx, const char *path_packidx)
1042 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1043 struct got_packidx_bloom_filter *bf;
1047 * Don't use bloom filters for very large pack index files.
1048 * Large pack files will contain a relatively large fraction
1049 * of our objects so we will likely need to visit them anyway.
1050 * The more objects a pack file contains the higher the probability
1051 * of a false-positive match from the bloom filter. And reading
1052 * all object IDs from a large pack index file can be expensive.
1054 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1057 /* Do we already have a filter for this pack index? */
1058 if (get_packidx_bloom_filter(repo, path_packidx,
1059 strlen(path_packidx)) != NULL)
1062 bf = calloc(1, sizeof(*bf));
1064 return got_error_from_errno("calloc");
1065 bf->bloom = calloc(1, sizeof(*bf->bloom));
1066 if (bf->bloom == NULL) {
1068 return got_error_from_errno("calloc");
1071 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1072 if (len >= sizeof(bf->path)) {
1075 return got_error(GOT_ERR_NO_SPACE);
1079 /* Minimum size supported by our bloom filter is 1000 entries. */
1080 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1081 for (i = 0; i < nobjects; i++) {
1082 struct got_packidx_object_id *id;
1083 id = &packidx->hdr.sorted_ids[i];
1084 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1087 RB_INSERT(got_packidx_bloom_filter_tree,
1088 &repo->packidx_bloom_filters, bf);
1093 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1095 struct got_pathlist_entry *pe;
1097 while (!TAILQ_EMPTY(packidx_paths)) {
1098 pe = TAILQ_FIRST(packidx_paths);
1099 TAILQ_REMOVE(packidx_paths, pe, entry);
1100 free((char *)pe->path);
1105 static const struct got_error *
1106 refresh_packidx_paths(struct got_repository *repo)
1108 const struct got_error *err = NULL;
1109 char *objects_pack_dir = NULL;
1112 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1113 if (objects_pack_dir == NULL)
1114 return got_error_from_errno("got_repo_get_path_objects_pack");
1116 if (stat(objects_pack_dir, &sb) == -1) {
1117 if (errno != ENOENT) {
1118 err = got_error_from_errno2("stat", objects_pack_dir);
1121 } else if (sb.st_mtime != repo->pack_path_mtime) {
1122 purge_packidx_paths(&repo->packidx_paths);
1123 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1128 free(objects_pack_dir);
1132 const struct got_error *
1133 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1134 struct got_repository *repo, struct got_object_id *id)
1136 const struct got_error *err;
1137 struct got_pathlist_entry *pe;
1140 /* Search pack index cache. */
1141 for (i = 0; i < repo->pack_cache_size; i++) {
1142 if (repo->packidx_cache[i] == NULL)
1144 if (!got_repo_check_packidx_bloom_filter(repo,
1145 repo->packidx_cache[i]->path_packidx, id))
1146 continue; /* object will not be found in this index */
1147 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1149 *packidx = repo->packidx_cache[i];
1151 * Move this cache entry to the front. Repeatedly
1152 * searching a wrong pack index can be expensive.
1155 memmove(&repo->packidx_cache[1],
1156 &repo->packidx_cache[0],
1157 i * sizeof(repo->packidx_cache[0]));
1158 repo->packidx_cache[0] = *packidx;
1159 if (repo->pinned_packidx >= 0 &&
1160 repo->pinned_packidx < i)
1161 repo->pinned_packidx++;
1162 else if (repo->pinned_packidx == i)
1163 repo->pinned_packidx = 0;
1168 /* No luck. Search the filesystem. */
1170 err = refresh_packidx_paths(repo);
1174 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1175 const char *path_packidx = pe->path;
1178 if (!got_repo_check_packidx_bloom_filter(repo,
1180 continue; /* object will not be found in this index */
1182 for (i = 0; i < repo->pack_cache_size; i++) {
1183 if (repo->packidx_cache[i] == NULL)
1185 if (strcmp(repo->packidx_cache[i]->path_packidx,
1186 path_packidx) == 0) {
1192 continue; /* already searched */
1194 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1199 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1203 err = cache_packidx(repo, *packidx, path_packidx);
1207 *idx = got_packidx_get_object_idx(*packidx, id);
1209 err = NULL; /* found the object */
1214 err = got_error_no_obj(id);
1219 const struct got_error *
1220 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 DIR *packdir = NULL;
1225 struct dirent *dent;
1226 char *path_packidx = NULL;
1230 packdir_fd = openat(got_repo_get_fd(repo),
1231 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1232 if (packdir_fd == -1) {
1233 return got_error_from_errno_fmt("openat: %s/%s",
1234 got_repo_get_path_git_dir(repo),
1235 GOT_OBJECTS_PACK_DIR);
1238 packdir = fdopendir(packdir_fd);
1239 if (packdir == NULL) {
1240 err = got_error_from_errno("fdopendir");
1244 if (fstat(packdir_fd, &sb) == -1) {
1245 err = got_error_from_errno("fstat");
1248 repo->pack_path_mtime = sb.st_mtime;
1250 while ((dent = readdir(packdir)) != NULL) {
1251 if (!got_repo_is_packidx_filename(dent->d_name,
1252 strlen(dent->d_name)))
1255 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1256 dent->d_name) == -1) {
1257 err = got_error_from_errno("asprintf");
1258 path_packidx = NULL;
1262 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1269 if (packdir && closedir(packdir) != 0 && err == NULL)
1270 err = got_error_from_errno("closedir");
1274 const struct got_error *
1275 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1276 struct got_repository *repo)
1278 const struct got_error *err;
1283 /* Search pack index cache. */
1284 for (i = 0; i < repo->pack_cache_size; i++) {
1285 if (repo->packidx_cache[i] == NULL)
1287 if (strcmp(repo->packidx_cache[i]->path_packidx,
1288 path_packidx) == 0) {
1289 *packidx = repo->packidx_cache[i];
1293 /* No luck. Search the filesystem. */
1295 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1300 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1304 err = cache_packidx(repo, *packidx, path_packidx);
1307 got_packidx_close(*packidx);
1313 static const struct got_error *
1314 read_packfile_hdr(int fd, struct got_packidx *packidx)
1316 const struct got_error *err = NULL;
1317 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1318 struct got_packfile_hdr hdr;
1321 n = read(fd, &hdr, sizeof(hdr));
1323 return got_error_from_errno("read");
1324 if (n != sizeof(hdr))
1325 return got_error(GOT_ERR_BAD_PACKFILE);
1327 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1328 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1329 be32toh(hdr.nobjects) != totobj)
1330 err = got_error(GOT_ERR_BAD_PACKFILE);
1335 static const struct got_error *
1336 open_packfile(int *fd, struct got_repository *repo,
1337 const char *relpath, struct got_packidx *packidx)
1339 const struct got_error *err = NULL;
1341 *fd = openat(got_repo_get_fd(repo), relpath,
1342 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1344 return got_error_from_errno_fmt("openat: %s/%s",
1345 got_repo_get_path_git_dir(repo), relpath);
1348 err = read_packfile_hdr(*fd, packidx);
1358 const struct got_error *
1359 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1360 const char *path_packfile, struct got_packidx *packidx)
1362 const struct got_error *err = NULL;
1363 struct got_pack *pack = NULL;
1370 for (i = 0; i < repo->pack_cache_size; i++) {
1371 pack = &repo->packs[i];
1372 if (pack->path_packfile == NULL)
1374 if (strcmp(pack->path_packfile, path_packfile) == 0)
1375 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1378 if (i == repo->pack_cache_size) {
1379 struct got_pack tmp;
1382 } while (i > 0 && repo->pinned_pack >= 0 &&
1383 i == repo->pinned_pack);
1384 err = got_pack_close(&repo->packs[i]);
1387 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1388 return got_error_from_errno("ftruncate");
1389 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1390 return got_error_from_errno("ftruncate");
1391 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1392 memcpy(&repo->packs[i], &repo->packs[0],
1393 sizeof(repo->packs[i]));
1394 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1395 if (repo->pinned_pack == 0)
1396 repo->pinned_pack = i;
1397 else if (repo->pinned_pack == i)
1398 repo->pinned_pack = 0;
1402 pack = &repo->packs[i];
1404 pack->path_packfile = strdup(path_packfile);
1405 if (pack->path_packfile == NULL) {
1406 err = got_error_from_errno("strdup");
1410 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1414 if (fstat(pack->fd, &sb) != 0) {
1415 err = got_error_from_errno("fstat");
1418 pack->filesize = sb.st_size;
1420 pack->privsep_child = NULL;
1422 err = got_delta_cache_alloc(&pack->delta_cache);
1426 #ifndef GOT_PACK_NO_MMAP
1427 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1428 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1430 if (pack->map == MAP_FAILED) {
1431 if (errno != ENOMEM) {
1432 err = got_error_from_errno("mmap");
1435 pack->map = NULL; /* fall back to read(2) */
1442 got_pack_close(pack);
1449 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1451 struct got_pack *pack = NULL;
1454 for (i = 0; i < repo->pack_cache_size; i++) {
1455 pack = &repo->packs[i];
1456 if (pack->path_packfile == NULL)
1458 if (strcmp(pack->path_packfile, path_packfile) == 0)
1465 const struct got_error *
1466 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1467 struct got_pack *pack)
1470 int pinned_pack = -1, pinned_packidx = -1;
1472 for (i = 0; i < repo->pack_cache_size; i++) {
1473 if (repo->packidx_cache[i] &&
1474 strcmp(repo->packidx_cache[i]->path_packidx,
1475 packidx->path_packidx) == 0)
1477 if (repo->packs[i].path_packfile &&
1478 strcmp(repo->packs[i].path_packfile,
1479 pack->path_packfile) == 0)
1483 if (pinned_packidx == -1 || pinned_pack == -1)
1484 return got_error(GOT_ERR_PIN_PACK);
1486 repo->pinned_pack = pinned_pack;
1487 repo->pinned_packidx = pinned_packidx;
1488 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1493 got_repo_get_pinned_pack(struct got_repository *repo)
1495 if (repo->pinned_pack >= 0 &&
1496 repo->pinned_pack < repo->pack_cache_size)
1497 return &repo->packs[repo->pinned_pack];
1503 got_repo_unpin_pack(struct got_repository *repo)
1505 repo->pinned_packidx = -1;
1506 repo->pinned_pack = -1;
1507 repo->pinned_pid = 0;
1510 const struct got_error *
1511 got_repo_init(const char *repo_path, const char *head_name)
1513 const struct got_error *err = NULL;
1514 const char *dirnames[] = {
1516 GOT_OBJECTS_PACK_DIR,
1519 const char *description_str = "Unnamed repository; "
1520 "edit this file 'description' to name the repository.";
1521 const char *headref = "ref: refs/heads/";
1522 const char *gitconfig_str = "[core]\n"
1523 "\trepositoryformatversion = 0\n"
1524 "\tfilemode = true\n"
1526 char *headref_str, *path;
1529 if (!got_path_dir_is_empty(repo_path))
1530 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1532 for (i = 0; i < nitems(dirnames); i++) {
1533 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1534 return got_error_from_errno("asprintf");
1536 err = got_path_mkdir(path);
1542 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1543 return got_error_from_errno("asprintf");
1544 err = got_path_create_file(path, description_str);
1549 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1550 return got_error_from_errno("asprintf");
1551 if (asprintf(&headref_str, "%s%s", headref,
1552 head_name ? head_name : "main") == -1) {
1554 return got_error_from_errno("asprintf");
1556 err = got_path_create_file(path, headref_str);
1562 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1563 return got_error_from_errno("asprintf");
1564 err = got_path_create_file(path, gitconfig_str);
1572 static const struct got_error *
1573 match_packed_object(struct got_object_id **unique_id,
1574 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1576 const struct got_error *err = NULL;
1577 struct got_object_id_queue matched_ids;
1578 struct got_pathlist_entry *pe;
1580 STAILQ_INIT(&matched_ids);
1582 err = refresh_packidx_paths(repo);
1586 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1587 const char *path_packidx = pe->path;
1588 struct got_packidx *packidx;
1589 struct got_object_qid *qid;
1591 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1596 err = got_packidx_match_id_str_prefix(&matched_ids,
1597 packidx, id_str_prefix);
1599 got_packidx_close(packidx);
1602 err = got_packidx_close(packidx);
1606 STAILQ_FOREACH(qid, &matched_ids, entry) {
1607 if (obj_type != GOT_OBJ_TYPE_ANY) {
1609 err = got_object_get_type(&matched_type, repo,
1613 if (matched_type != obj_type)
1616 if (*unique_id == NULL) {
1617 *unique_id = got_object_id_dup(&qid->id);
1618 if (*unique_id == NULL) {
1619 err = got_error_from_errno("malloc");
1623 if (got_object_id_cmp(*unique_id,
1625 continue; /* packed multiple times */
1626 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1632 got_object_id_queue_free(&matched_ids);
1640 static const struct got_error *
1641 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1642 const char *object_dir, const char *id_str_prefix, int obj_type,
1643 struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 char *path, *id_str = NULL;
1648 struct dirent *dent;
1649 struct got_object_id id;
1651 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1652 err = got_error_from_errno("asprintf");
1656 dir = opendir(path);
1658 if (errno == ENOENT) {
1662 err = got_error_from_errno2("opendir", path);
1665 while ((dent = readdir(dir)) != NULL) {
1671 if (strcmp(dent->d_name, ".") == 0 ||
1672 strcmp(dent->d_name, "..") == 0)
1675 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1676 err = got_error_from_errno("asprintf");
1680 if (!got_parse_sha1_digest(id.sha1, id_str))
1684 * Directory entries do not necessarily appear in
1685 * sorted order, so we must iterate over all of them.
1687 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1691 if (*unique_id == NULL) {
1692 if (obj_type != GOT_OBJ_TYPE_ANY) {
1694 err = got_object_get_type(&matched_type, repo,
1698 if (matched_type != obj_type)
1701 *unique_id = got_object_id_dup(&id);
1702 if (*unique_id == NULL) {
1703 err = got_error_from_errno("got_object_id_dup");
1707 if (got_object_id_cmp(*unique_id, &id) == 0)
1708 continue; /* both packed and loose */
1709 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1714 if (dir && closedir(dir) != 0 && err == NULL)
1715 err = got_error_from_errno("closedir");
1725 const struct got_error *
1726 got_repo_match_object_id_prefix(struct got_object_id **id,
1727 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 char *path_objects = NULL, *object_dir = NULL;
1736 path_objects = got_repo_get_path_objects(repo);
1738 len = strlen(id_str_prefix);
1739 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1740 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1744 for (i = 0; i < len; i++) {
1745 if (isxdigit((unsigned char)id_str_prefix[i]))
1747 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1752 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1755 object_dir = strndup(id_str_prefix, 2);
1756 if (object_dir == NULL) {
1757 err = got_error_from_errno("strdup");
1760 err = match_loose_object(id, path_objects, object_dir,
1761 id_str_prefix, obj_type, repo);
1762 } else if (len == 1) {
1764 for (i = 0; i < 0xf; i++) {
1765 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1767 err = got_error_from_errno("asprintf");
1770 err = match_packed_object(id, repo, object_dir,
1774 err = match_loose_object(id, path_objects, object_dir,
1775 id_str_prefix, obj_type, repo);
1780 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1789 } else if (*id == NULL) {
1791 case GOT_OBJ_TYPE_BLOB:
1792 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1793 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1795 case GOT_OBJ_TYPE_TREE:
1796 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1797 GOT_OBJ_LABEL_TREE, id_str_prefix);
1799 case GOT_OBJ_TYPE_COMMIT:
1800 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1801 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1803 case GOT_OBJ_TYPE_TAG:
1804 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1805 GOT_OBJ_LABEL_TAG, id_str_prefix);
1808 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1816 const struct got_error *
1817 got_repo_match_object_id(struct got_object_id **id, char **label,
1818 const char *id_str, int obj_type, struct got_reflist_head *refs,
1819 struct got_repository *repo)
1821 const struct got_error *err;
1822 struct got_tag_object *tag;
1823 struct got_reference *ref = NULL;
1830 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1833 *id = got_object_id_dup(
1834 got_object_tag_get_object_id(tag));
1836 err = got_error_from_errno("got_object_id_dup");
1837 else if (label && asprintf(label, "refs/tags/%s",
1838 got_object_tag_get_name(tag)) == -1) {
1839 err = got_error_from_errno("asprintf");
1843 got_object_tag_close(tag);
1845 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1846 err->code != GOT_ERR_NO_OBJ)
1850 err = got_ref_open(&ref, repo, id_str, 0);
1852 err = got_ref_resolve(id, repo, ref);
1856 *label = strdup(got_ref_get_name(ref));
1857 if (*label == NULL) {
1858 err = got_error_from_errno("strdup");
1863 if (err->code != GOT_ERR_NOT_REF &&
1864 err->code != GOT_ERR_BAD_REF_NAME)
1866 err = got_repo_match_object_id_prefix(id, id_str,
1869 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1870 err = got_error_not_ref(id_str);
1874 err = got_object_id_str(label, *id);
1875 if (*label == NULL) {
1876 err = got_error_from_errno("strdup");
1887 const struct got_error *
1888 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1889 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1891 const struct got_error *err = NULL;
1892 struct got_reflist_entry *re;
1893 struct got_object_id *tag_id;
1894 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1898 TAILQ_FOREACH(re, refs, entry) {
1899 const char *refname;
1900 refname = got_ref_get_name(re->ref);
1901 if (got_ref_is_symbolic(re->ref))
1903 if (strncmp(refname, "refs/tags/", 10) != 0)
1905 if (!name_is_absolute)
1906 refname += strlen("refs/tags/");
1907 if (strcmp(refname, name) != 0)
1909 err = got_ref_resolve(&tag_id, repo, re->ref);
1912 err = got_object_open_as_tag(tag, repo, tag_id);
1916 if (obj_type == GOT_OBJ_TYPE_ANY ||
1917 got_object_tag_get_object_type(*tag) == obj_type)
1919 got_object_tag_close(*tag);
1923 if (err == NULL && *tag == NULL)
1924 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1925 GOT_OBJ_LABEL_TAG, name);
1929 static const struct got_error *
1930 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1931 const char *name, mode_t mode, struct got_object_id *blob_id)
1933 const struct got_error *err = NULL;
1937 *new_te = calloc(1, sizeof(**new_te));
1938 if (*new_te == NULL)
1939 return got_error_from_errno("calloc");
1941 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1942 sizeof((*new_te)->name)) {
1943 err = got_error(GOT_ERR_NO_SPACE);
1947 if (S_ISLNK(mode)) {
1948 (*new_te)->mode = S_IFLNK;
1950 (*new_te)->mode = S_IFREG;
1951 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1953 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1955 if (err && *new_te) {
1962 static const struct got_error *
1963 import_file(struct got_tree_entry **new_te, struct dirent *de,
1964 const char *path, struct got_repository *repo)
1966 const struct got_error *err;
1967 struct got_object_id *blob_id = NULL;
1971 if (asprintf(&filepath, "%s%s%s", path,
1972 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1973 return got_error_from_errno("asprintf");
1975 if (lstat(filepath, &sb) != 0) {
1976 err = got_error_from_errno2("lstat", path);
1980 err = got_object_blob_create(&blob_id, filepath, repo);
1984 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1993 static const struct got_error *
1994 insert_tree_entry(struct got_tree_entry *new_te,
1995 struct got_pathlist_head *paths)
1997 const struct got_error *err = NULL;
1998 struct got_pathlist_entry *new_pe;
2000 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2004 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2008 static const struct got_error *write_tree(struct got_object_id **,
2009 const char *, struct got_pathlist_head *, struct got_repository *,
2010 got_repo_import_cb progress_cb, void *progress_arg);
2012 static const struct got_error *
2013 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2014 const char *path, struct got_pathlist_head *ignores,
2015 struct got_repository *repo,
2016 got_repo_import_cb progress_cb, void *progress_arg)
2018 const struct got_error *err;
2019 struct got_object_id *id = NULL;
2022 if (asprintf(&subdirpath, "%s%s%s", path,
2023 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2024 return got_error_from_errno("asprintf");
2026 (*new_te) = calloc(1, sizeof(**new_te));
2027 if (*new_te == NULL)
2028 return got_error_from_errno("calloc");
2029 (*new_te)->mode = S_IFDIR;
2030 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2031 sizeof((*new_te)->name)) {
2032 err = got_error(GOT_ERR_NO_SPACE);
2035 err = write_tree(&id, subdirpath, ignores, repo,
2036 progress_cb, progress_arg);
2039 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2051 static const struct got_error *
2052 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2053 struct got_pathlist_head *ignores, struct got_repository *repo,
2054 got_repo_import_cb progress_cb, void *progress_arg)
2056 const struct got_error *err = NULL;
2060 struct got_tree_entry *new_te = NULL;
2061 struct got_pathlist_head paths;
2062 struct got_pathlist_entry *pe;
2064 *new_tree_id = NULL;
2068 dir = opendir(path_dir);
2070 err = got_error_from_errno2("opendir", path_dir);
2075 while ((de = readdir(dir)) != NULL) {
2079 if (strcmp(de->d_name, ".") == 0 ||
2080 strcmp(de->d_name, "..") == 0)
2083 TAILQ_FOREACH(pe, ignores, entry) {
2084 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2092 err = got_path_dirent_type(&type, path_dir, de);
2096 if (type == DT_DIR) {
2097 err = import_subdir(&new_te, de, path_dir,
2098 ignores, repo, progress_cb, progress_arg);
2100 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2105 } else if (type == DT_REG || type == DT_LNK) {
2106 err = import_file(&new_te, de, path_dir, repo);
2112 err = insert_tree_entry(new_te, &paths);
2118 if (TAILQ_EMPTY(&paths)) {
2119 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2120 "cannot create tree without any entries");
2124 TAILQ_FOREACH(pe, &paths, entry) {
2125 struct got_tree_entry *te = pe->data;
2127 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2129 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2130 err = got_error_from_errno("asprintf");
2133 err = (*progress_cb)(progress_arg, path);
2139 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2143 got_pathlist_free(&paths);
2147 const struct got_error *
2148 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2149 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2150 struct got_repository *repo, got_repo_import_cb progress_cb,
2153 const struct got_error *err;
2154 struct got_object_id *new_tree_id;
2156 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2157 progress_cb, progress_arg);
2161 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2162 author, time(NULL), author, time(NULL), logmsg, repo);
2167 const struct got_error *
2168 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 char *path_objects = NULL, *path = NULL;
2174 struct got_object_id id;
2180 path_objects = got_repo_get_path_objects(repo);
2181 if (path_objects == NULL)
2182 return got_error_from_errno("got_repo_get_path_objects");
2184 for (i = 0; i <= 0xff; i++) {
2185 struct dirent *dent;
2187 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2188 err = got_error_from_errno("asprintf");
2192 dir = opendir(path);
2194 if (errno == ENOENT) {
2198 err = got_error_from_errno2("opendir", path);
2202 while ((dent = readdir(dir)) != NULL) {
2207 if (strcmp(dent->d_name, ".") == 0 ||
2208 strcmp(dent->d_name, "..") == 0)
2211 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2212 err = got_error_from_errno("asprintf");
2216 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2222 err = got_object_open_loose_fd(&fd, &id, repo);
2226 if (fstat(fd, &sb) == -1) {
2227 err = got_error_from_errno("fstat");
2232 (*ondisk_size) += sb.st_size;
2234 if (close(fd) == -1) {
2235 err = got_error_from_errno("close");
2240 if (closedir(dir) != 0) {
2241 err = got_error_from_errno("closedir");
2250 if (dir && closedir(dir) != 0 && err == NULL)
2251 err = got_error_from_errno("closedir");
2262 const struct got_error *
2263 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2264 off_t *total_packsize, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 DIR *packdir = NULL;
2268 struct dirent *dent;
2269 struct got_packidx *packidx = NULL;
2271 char *path_packfile;
2277 *total_packsize = 0;
2279 packdir_fd = openat(got_repo_get_fd(repo),
2280 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2281 if (packdir_fd == -1) {
2282 return got_error_from_errno_fmt("openat: %s/%s",
2283 got_repo_get_path_git_dir(repo),
2284 GOT_OBJECTS_PACK_DIR);
2287 packdir = fdopendir(packdir_fd);
2288 if (packdir == NULL) {
2289 err = got_error_from_errno("fdopendir");
2293 while ((dent = readdir(packdir)) != NULL) {
2294 if (!got_repo_is_packidx_filename(dent->d_name,
2295 strlen(dent->d_name)))
2298 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2299 dent->d_name) == -1) {
2300 err = got_error_from_errno("asprintf");
2304 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2310 if (fstat(packidx->fd, &sb) == -1)
2312 *total_packsize += sb.st_size;
2314 err = got_packidx_get_packfile_path(&path_packfile,
2315 packidx->path_packidx);
2319 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2321 free(path_packfile);
2324 free(path_packfile);
2325 *total_packsize += sb.st_size;
2327 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2331 got_packidx_close(packidx);
2336 got_packidx_close(packidx);
2337 if (packdir && closedir(packdir) != 0 && err == NULL)
2338 err = got_error_from_errno("closedir");
2342 *total_packsize = 0;
2347 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2348 got_packidx_bloom_filter_cmp);