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>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
51 #include "got_lib_delta.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]))
68 got_repo_get_path(struct got_repository *repo)
74 got_repo_get_path_git_dir(struct got_repository *repo)
76 return repo->path_git_dir;
80 got_repo_get_fd(struct got_repository *repo)
82 return repo->gitdir_fd;
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 return repo->gitconfig_author_name;
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 return repo->gitconfig_author_email;
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
100 return repo->global_gitconfig_author_name;
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
116 got_repo_is_bare(struct got_repository *repo)
118 return (strcmp(repo->path, repo->path_git_dir) == 0);
122 get_path_git_child(struct got_repository *repo, const char *basename)
126 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
134 got_repo_get_path_objects(struct got_repository *repo)
136 return get_path_git_child(repo, GOT_OBJECTS_DIR);
140 got_repo_get_path_objects_pack(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
146 got_repo_get_path_refs(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_REFS_DIR);
152 got_repo_get_path_packed_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
158 get_path_head(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_HEAD_FILE);
164 got_repo_get_path_gitconfig(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_GITCONFIG);
170 got_repo_get_path_gotconfig(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
175 const struct got_gotconfig *
176 got_repo_get_gotconfig(struct got_repository *repo)
178 return repo->gotconfig;
182 got_repo_get_gitconfig_remotes(int *nremotes,
183 const struct got_remote_repo **remotes, struct got_repository *repo)
185 *nremotes = repo->ngitconfig_remotes;
186 *remotes = repo->gitconfig_remotes;
190 is_git_repo(struct got_repository *repo)
192 const char *path_git = got_repo_get_path_git_dir(repo);
193 char *path_objects = got_repo_get_path_objects(repo);
194 char *path_refs = got_repo_get_path_refs(repo);
195 char *path_head = get_path_head(repo);
198 struct got_reference *head_ref;
200 if (lstat(path_git, &sb) == -1)
202 if (!S_ISDIR(sb.st_mode))
205 if (lstat(path_objects, &sb) == -1)
207 if (!S_ISDIR(sb.st_mode))
210 if (lstat(path_refs, &sb) == -1)
212 if (!S_ISDIR(sb.st_mode))
215 if (lstat(path_head, &sb) == -1)
217 if (!S_ISREG(sb.st_mode))
220 /* Check if the HEAD reference can be opened. */
221 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
223 got_ref_close(head_ref);
234 const struct got_error *
235 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
236 struct got_object *obj)
238 #ifndef GOT_NO_OBJ_CACHE
239 const struct got_error *err = NULL;
240 err = got_object_cache_add(&repo->objcache, id, obj);
242 if (err->code == GOT_ERR_OBJ_EXISTS ||
243 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 got_repo_get_cached_object(struct got_repository *repo,
254 struct got_object_id *id)
256 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
259 const struct got_error *
260 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
261 struct got_tree_object *tree)
263 #ifndef GOT_NO_OBJ_CACHE
264 const struct got_error *err = NULL;
265 err = got_object_cache_add(&repo->treecache, id, tree);
267 if (err->code == GOT_ERR_OBJ_EXISTS ||
268 err->code == GOT_ERR_OBJ_TOO_LARGE)
277 struct got_tree_object *
278 got_repo_get_cached_tree(struct got_repository *repo,
279 struct got_object_id *id)
281 return (struct got_tree_object *)got_object_cache_get(
282 &repo->treecache, id);
285 const struct got_error *
286 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
287 struct got_commit_object *commit)
289 #ifndef GOT_NO_OBJ_CACHE
290 const struct got_error *err = NULL;
291 err = got_object_cache_add(&repo->commitcache, id, commit);
293 if (err->code == GOT_ERR_OBJ_EXISTS ||
294 err->code == GOT_ERR_OBJ_TOO_LARGE)
303 struct got_commit_object *
304 got_repo_get_cached_commit(struct got_repository *repo,
305 struct got_object_id *id)
307 return (struct got_commit_object *)got_object_cache_get(
308 &repo->commitcache, id);
311 const struct got_error *
312 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
313 struct got_tag_object *tag)
315 #ifndef GOT_NO_OBJ_CACHE
316 const struct got_error *err = NULL;
317 err = got_object_cache_add(&repo->tagcache, id, tag);
319 if (err->code == GOT_ERR_OBJ_EXISTS ||
320 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 struct got_tag_object *
330 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
332 return (struct got_tag_object *)got_object_cache_get(
333 &repo->tagcache, id);
336 const struct got_error *
337 open_repo(struct got_repository *repo, const char *path)
339 const struct got_error *err = NULL;
341 repo->gitdir_fd = -1;
343 /* bare git repository? */
344 repo->path_git_dir = strdup(path);
345 if (repo->path_git_dir == NULL)
346 return got_error_from_errno("strdup");
347 if (is_git_repo(repo)) {
348 repo->path = strdup(repo->path_git_dir);
349 if (repo->path == NULL) {
350 err = got_error_from_errno("strdup");
353 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
354 if (repo->gitdir_fd == -1) {
355 err = got_error_from_errno2("open",
362 /* git repository with working tree? */
363 free(repo->path_git_dir);
364 repo->path_git_dir = NULL;
365 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
366 err = got_error_from_errno("asprintf");
369 if (is_git_repo(repo)) {
370 repo->path = strdup(path);
371 if (repo->path == NULL) {
372 err = got_error_from_errno("strdup");
375 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
376 if (repo->gitdir_fd == -1) {
377 err = got_error_from_errno2("open",
384 err = got_error(GOT_ERR_NOT_GIT_REPO);
389 free(repo->path_git_dir);
390 repo->path_git_dir = NULL;
391 if (repo->gitdir_fd != -1)
392 close(repo->gitdir_fd);
393 repo->gitdir_fd = -1;
399 static const struct got_error *
400 parse_gitconfig_file(int *gitconfig_repository_format_version,
401 char **gitconfig_author_name, char **gitconfig_author_email,
402 struct got_remote_repo **remotes, int *nremotes,
403 char **gitconfig_owner, char ***extensions, int *nextensions,
404 const char *gitconfig_path)
406 const struct got_error *err = NULL, *child_err = NULL;
408 int imsg_fds[2] = { -1, -1 };
410 struct imsgbuf *ibuf;
412 *gitconfig_repository_format_version = 0;
417 *gitconfig_author_name = NULL;
418 *gitconfig_author_email = NULL;
424 *gitconfig_owner = NULL;
426 fd = open(gitconfig_path, O_RDONLY);
430 return got_error_from_errno2("open", gitconfig_path);
433 ibuf = calloc(1, sizeof(*ibuf));
435 err = got_error_from_errno("calloc");
439 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
440 err = got_error_from_errno("socketpair");
446 err = got_error_from_errno("fork");
448 } else if (pid == 0) {
449 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
454 if (close(imsg_fds[1]) == -1) {
455 err = got_error_from_errno("close");
459 imsg_init(ibuf, imsg_fds[0]);
461 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
466 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
470 err = got_privsep_recv_gitconfig_int(
471 gitconfig_repository_format_version, ibuf);
475 if (extensions && nextensions) {
476 err = got_privsep_send_gitconfig_repository_extensions_req(
480 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
483 if (*nextensions > 0) {
485 *extensions = calloc(*nextensions, sizeof(char *));
486 if (*extensions == NULL) {
487 err = got_error_from_errno("calloc");
490 for (i = 0; i < *nextensions; i++) {
492 err = got_privsep_recv_gitconfig_str(&ext,
496 (*extensions)[i] = ext;
501 err = got_privsep_send_gitconfig_author_name_req(ibuf);
505 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
509 err = got_privsep_send_gitconfig_author_email_req(ibuf);
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
517 if (remotes && nremotes) {
518 err = got_privsep_send_gitconfig_remotes_req(ibuf);
522 err = got_privsep_recv_gitconfig_remotes(remotes,
528 if (gitconfig_owner) {
529 err = got_privsep_send_gitconfig_owner_req(ibuf);
532 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
538 err = got_privsep_send_stop(imsg_fds[0]);
539 child_err = got_privsep_wait_for_child(pid);
540 if (child_err && err == NULL)
543 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
544 err = got_error_from_errno("close");
545 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
546 err = got_error_from_errno("close");
547 if (fd != -1 && close(fd) == -1 && err == NULL)
548 err = got_error_from_errno2("close", gitconfig_path);
553 static const struct got_error *
554 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
556 const struct got_error *err = NULL;
557 char *repo_gitconfig_path = NULL;
559 if (global_gitconfig_path) {
560 /* Read settings from ~/.gitconfig. */
561 int dummy_repo_version;
562 err = parse_gitconfig_file(&dummy_repo_version,
563 &repo->global_gitconfig_author_name,
564 &repo->global_gitconfig_author_email,
565 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
570 /* Read repository's .git/config file. */
571 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
572 if (repo_gitconfig_path == NULL)
573 return got_error_from_errno("got_repo_get_path_gitconfig");
575 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
576 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
577 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
578 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
579 repo_gitconfig_path);
583 free(repo_gitconfig_path);
587 static const struct got_error *
588 read_gotconfig(struct got_repository *repo)
590 const struct got_error *err = NULL;
591 char *gotconfig_path;
593 gotconfig_path = got_repo_get_path_gotconfig(repo);
594 if (gotconfig_path == NULL)
595 return got_error_from_errno("got_repo_get_path_gotconfig");
597 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
598 free(gotconfig_path);
602 /* Supported repository format extensions. */
603 static const char *repo_extensions[] = {
604 "noop", /* Got supports repository format version 1. */
605 "preciousObjects", /* Got has no garbage collection yet. */
606 "worktreeConfig", /* Got does not care about Git work trees. */
609 const struct got_error *
610 got_repo_open(struct got_repository **repop, const char *path,
611 const char *global_gitconfig_path)
613 struct got_repository *repo = NULL;
614 const struct got_error *err = NULL;
615 char *repo_path = NULL;
621 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
622 return got_error_from_errno("getrlimit");
624 repo = calloc(1, sizeof(*repo));
626 err = got_error_from_errno("calloc");
630 for (i = 0; i < nitems(repo->privsep_children); i++) {
631 memset(&repo->privsep_children[i], 0,
632 sizeof(repo->privsep_children[0]));
633 repo->privsep_children[i].imsg_fd = -1;
636 err = got_object_cache_init(&repo->objcache,
637 GOT_OBJECT_CACHE_TYPE_OBJ);
640 err = got_object_cache_init(&repo->treecache,
641 GOT_OBJECT_CACHE_TYPE_TREE);
644 err = got_object_cache_init(&repo->commitcache,
645 GOT_OBJECT_CACHE_TYPE_COMMIT);
648 err = got_object_cache_init(&repo->tagcache,
649 GOT_OBJECT_CACHE_TYPE_TAG);
653 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
654 if (repo->pack_cache_size > rl.rlim_cur / 8)
655 repo->pack_cache_size = rl.rlim_cur / 8;
657 repo_path = realpath(path, NULL);
658 if (repo_path == NULL) {
659 err = got_error_from_errno2("realpath", path);
666 err = open_repo(repo, repo_path);
669 if (err->code != GOT_ERR_NOT_GIT_REPO)
671 if (repo_path[0] == '/' && repo_path[1] == '\0') {
672 err = got_error(GOT_ERR_NOT_GIT_REPO);
675 err = got_path_dirname(&parent_path, repo_path);
679 repo_path = parent_path;
682 err = read_gotconfig(repo);
686 err = read_gitconfig(repo, global_gitconfig_path);
689 if (repo->gitconfig_repository_format_version != 0)
690 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
691 for (i = 0; i < repo->nextensions; i++) {
692 char *ext = repo->extensions[i];
693 int j, supported = 0;
694 for (j = 0; j < nitems(repo_extensions); j++) {
695 if (strcmp(ext, repo_extensions[j]) == 0) {
701 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
707 got_repo_close(repo);
714 const struct got_error *
715 got_repo_close(struct got_repository *repo)
717 const struct got_error *err = NULL, *child_err;
720 for (i = 0; i < repo->pack_cache_size; i++) {
721 if (repo->packidx_cache[i] == NULL)
723 got_packidx_close(repo->packidx_cache[i]);
726 for (i = 0; i < repo->pack_cache_size; i++) {
727 if (repo->packs[i].path_packfile == NULL)
729 got_pack_close(&repo->packs[i]);
733 free(repo->path_git_dir);
735 got_object_cache_close(&repo->objcache);
736 got_object_cache_close(&repo->treecache);
737 got_object_cache_close(&repo->commitcache);
738 got_object_cache_close(&repo->tagcache);
740 for (i = 0; i < nitems(repo->privsep_children); i++) {
741 if (repo->privsep_children[i].imsg_fd == -1)
743 imsg_clear(repo->privsep_children[i].ibuf);
744 free(repo->privsep_children[i].ibuf);
745 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
746 child_err = got_privsep_wait_for_child(
747 repo->privsep_children[i].pid);
748 if (child_err && err == NULL)
750 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
752 err = got_error_from_errno("close");
756 got_gotconfig_free(repo->gotconfig);
757 free(repo->gitconfig_author_name);
758 free(repo->gitconfig_author_email);
759 for (i = 0; i < repo->ngitconfig_remotes; i++)
760 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
761 free(repo->gitconfig_remotes);
762 for (i = 0; i < repo->nextensions; i++)
763 free(repo->extensions[i]);
764 free(repo->extensions);
771 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
779 for (i = 0; i < repo->nbranches; i++)
780 free(repo->branches[i]);
781 free(repo->branches);
782 repo->branches = NULL;
786 const struct got_error *
787 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
788 const char *input_path)
790 const struct got_error *err = NULL;
791 const char *repo_abspath = NULL;
793 char *canonpath, *path = NULL;
795 *in_repo_path = NULL;
797 canonpath = strdup(input_path);
798 if (canonpath == NULL) {
799 err = got_error_from_errno("strdup");
802 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
806 repo_abspath = got_repo_get_path(repo);
808 if (canonpath[0] == '\0') {
809 path = strdup(canonpath);
811 err = got_error_from_errno("strdup");
815 path = realpath(canonpath, NULL);
817 if (errno != ENOENT) {
818 err = got_error_from_errno2("realpath",
823 * Path is not on disk.
824 * Assume it is already relative to repository root.
826 path = strdup(canonpath);
828 err = got_error_from_errno("strdup");
833 repolen = strlen(repo_abspath);
837 if (strcmp(path, repo_abspath) == 0) {
841 err = got_error_from_errno("strdup");
844 } else if (len > repolen &&
845 got_path_is_child(path, repo_abspath, repolen)) {
846 /* Matched an on-disk path inside repository. */
847 if (got_repo_is_bare(repo)) {
849 * Matched an on-disk path inside repository
850 * database. Treat input as repository-relative.
857 /* Strip common prefix with repository path. */
858 err = got_path_skip_common_ancestor(&child,
867 * Matched unrelated on-disk path.
868 * Treat input as repository-relative.
876 /* Make in-repository path absolute */
877 if (path[0] != '/') {
879 if (asprintf(&abspath, "/%s", path) == -1) {
880 err = got_error_from_errno("asprintf");
892 *in_repo_path = path;
896 static const struct got_error *
897 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
898 const char *path_packidx)
900 const struct got_error *err = NULL;
903 for (i = 0; i < repo->pack_cache_size; i++) {
904 if (repo->packidx_cache[i] == NULL)
906 if (strcmp(repo->packidx_cache[i]->path_packidx,
907 path_packidx) == 0) {
908 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
911 if (i == repo->pack_cache_size) {
912 err = got_packidx_close(repo->packidx_cache[i - 1]);
918 * Insert the new pack index at the front so it will
919 * be searched first in the future.
921 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
922 sizeof(repo->packidx_cache) -
923 sizeof(repo->packidx_cache[0]));
924 repo->packidx_cache[0] = packidx;
930 is_packidx_filename(const char *name, size_t len)
932 if (len != GOT_PACKIDX_NAMELEN)
935 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
938 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
939 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
945 const struct got_error *
946 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
947 struct got_repository *repo, struct got_object_id *id)
949 const struct got_error *err;
956 /* Search pack index cache. */
957 for (i = 0; i < repo->pack_cache_size; i++) {
958 if (repo->packidx_cache[i] == NULL)
960 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
962 *packidx = repo->packidx_cache[i];
964 * Move this cache entry to the front. Repeatedly
965 * searching a wrong pack index can be expensive.
968 struct got_packidx *p;
969 p = repo->packidx_cache[0];
970 repo->packidx_cache[0] = *packidx;
971 repo->packidx_cache[i] = p;
976 /* No luck. Search the filesystem. */
978 packdir_fd = openat(got_repo_get_fd(repo),
979 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
980 if (packdir_fd == -1) {
982 err = got_error_no_obj(id);
984 err = got_error_from_errno_fmt("openat: %s/%s",
985 got_repo_get_path_git_dir(repo),
986 GOT_OBJECTS_PACK_DIR);
990 packdir = fdopendir(packdir_fd);
991 if (packdir == NULL) {
992 err = got_error_from_errno("fdopendir");
996 while ((dent = readdir(packdir)) != NULL) {
999 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1002 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1003 dent->d_name) == -1) {
1004 err = got_error_from_errno("asprintf");
1008 for (i = 0; i < repo->pack_cache_size; i++) {
1009 if (repo->packidx_cache[i] == NULL)
1011 if (strcmp(repo->packidx_cache[i]->path_packidx,
1012 path_packidx) == 0) {
1019 continue; /* already searched */
1022 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1029 err = cache_packidx(repo, *packidx, path_packidx);
1034 *idx = got_packidx_get_object_idx(*packidx, id);
1036 err = NULL; /* found the object */
1041 err = got_error_no_obj(id);
1043 if (packdir && closedir(packdir) != 0 && err == NULL)
1044 err = got_error_from_errno("closedir");
1048 static const struct got_error *
1049 read_packfile_hdr(int fd, struct got_packidx *packidx)
1051 const struct got_error *err = NULL;
1052 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1053 struct got_packfile_hdr hdr;
1056 n = read(fd, &hdr, sizeof(hdr));
1058 return got_error_from_errno("read");
1059 if (n != sizeof(hdr))
1060 return got_error(GOT_ERR_BAD_PACKFILE);
1062 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1063 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1064 be32toh(hdr.nobjects) != totobj)
1065 err = got_error(GOT_ERR_BAD_PACKFILE);
1070 static const struct got_error *
1071 open_packfile(int *fd, struct got_repository *repo,
1072 const char *relpath, struct got_packidx *packidx)
1074 const struct got_error *err = NULL;
1076 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1078 return got_error_from_errno_fmt("openat: %s/%s",
1079 got_repo_get_path_git_dir(repo), relpath);
1082 err = read_packfile_hdr(*fd, packidx);
1092 const struct got_error *
1093 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1094 const char *path_packfile, struct got_packidx *packidx)
1096 const struct got_error *err = NULL;
1097 struct got_pack *pack = NULL;
1104 for (i = 0; i < repo->pack_cache_size; i++) {
1105 pack = &repo->packs[i];
1106 if (pack->path_packfile == NULL)
1108 if (strcmp(pack->path_packfile, path_packfile) == 0)
1109 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1112 if (i == repo->pack_cache_size) {
1113 err = got_pack_close(&repo->packs[i - 1]);
1116 memmove(&repo->packs[1], &repo->packs[0],
1117 sizeof(repo->packs) - sizeof(repo->packs[0]));
1121 pack = &repo->packs[i];
1123 pack->path_packfile = strdup(path_packfile);
1124 if (pack->path_packfile == NULL) {
1125 err = got_error_from_errno("strdup");
1129 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1133 if (fstat(pack->fd, &sb) != 0) {
1134 err = got_error_from_errno("fstat");
1137 pack->filesize = sb.st_size;
1139 pack->privsep_child = NULL;
1141 #ifndef GOT_PACK_NO_MMAP
1142 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1144 if (pack->map == MAP_FAILED) {
1145 if (errno != ENOMEM) {
1146 err = got_error_from_errno("mmap");
1149 pack->map = NULL; /* fall back to read(2) */
1155 free(pack->path_packfile);
1156 memset(pack, 0, sizeof(*pack));
1164 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1166 struct got_pack *pack = NULL;
1169 for (i = 0; i < repo->pack_cache_size; i++) {
1170 pack = &repo->packs[i];
1171 if (pack->path_packfile == NULL)
1173 if (strcmp(pack->path_packfile, path_packfile) == 0)
1180 const struct got_error *
1181 got_repo_init(const char *repo_path)
1183 const struct got_error *err = NULL;
1184 const char *dirnames[] = {
1186 GOT_OBJECTS_PACK_DIR,
1189 const char *description_str = "Unnamed repository; "
1190 "edit this file 'description' to name the repository.";
1191 const char *headref_str = "ref: refs/heads/main";
1192 const char *gitconfig_str = "[core]\n"
1193 "\trepositoryformatversion = 0\n"
1194 "\tfilemode = true\n"
1199 if (!got_path_dir_is_empty(repo_path))
1200 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1202 for (i = 0; i < nitems(dirnames); i++) {
1203 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1204 return got_error_from_errno("asprintf");
1206 err = got_path_mkdir(path);
1212 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1213 return got_error_from_errno("asprintf");
1214 err = got_path_create_file(path, description_str);
1219 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1220 return got_error_from_errno("asprintf");
1221 err = got_path_create_file(path, headref_str);
1226 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1227 return got_error_from_errno("asprintf");
1228 err = got_path_create_file(path, gitconfig_str);
1236 static const struct got_error *
1237 match_packed_object(struct got_object_id **unique_id,
1238 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1240 const struct got_error *err = NULL;
1241 DIR *packdir = NULL;
1242 struct dirent *dent;
1244 struct got_object_id_queue matched_ids;
1247 SIMPLEQ_INIT(&matched_ids);
1249 packdir_fd = openat(got_repo_get_fd(repo),
1250 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1251 if (packdir_fd == -1) {
1252 if (errno != ENOENT)
1253 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1257 packdir = fdopendir(packdir_fd);
1258 if (packdir == NULL) {
1259 err = got_error_from_errno("fdopendir");
1263 while ((dent = readdir(packdir)) != NULL) {
1264 struct got_packidx *packidx;
1265 struct got_object_qid *qid;
1267 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1270 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1271 dent->d_name) == -1) {
1272 err = got_error_from_errno("strdup");
1276 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1282 err = got_packidx_match_id_str_prefix(&matched_ids,
1283 packidx, id_str_prefix);
1285 got_packidx_close(packidx);
1288 err = got_packidx_close(packidx);
1292 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1293 if (obj_type != GOT_OBJ_TYPE_ANY) {
1295 err = got_object_get_type(&matched_type, repo,
1299 if (matched_type != obj_type)
1302 if (*unique_id == NULL) {
1303 *unique_id = got_object_id_dup(qid->id);
1304 if (*unique_id == NULL) {
1305 err = got_error_from_errno("malloc");
1309 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1310 continue; /* packed multiple times */
1311 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1317 got_object_id_queue_free(&matched_ids);
1318 if (packdir && closedir(packdir) != 0 && err == NULL)
1319 err = got_error_from_errno("closedir");
1327 static const struct got_error *
1328 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1329 const char *object_dir, const char *id_str_prefix, int obj_type,
1330 struct got_repository *repo)
1332 const struct got_error *err = NULL;
1335 struct dirent *dent;
1336 struct got_object_id id;
1338 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1339 err = got_error_from_errno("asprintf");
1343 dir = opendir(path);
1345 if (errno == ENOENT) {
1349 err = got_error_from_errno2("opendir", path);
1352 while ((dent = readdir(dir)) != NULL) {
1356 if (strcmp(dent->d_name, ".") == 0 ||
1357 strcmp(dent->d_name, "..") == 0)
1360 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1361 err = got_error_from_errno("asprintf");
1365 if (!got_parse_sha1_digest(id.sha1, id_str))
1369 * Directory entries do not necessarily appear in
1370 * sorted order, so we must iterate over all of them.
1372 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1378 if (*unique_id == NULL) {
1379 if (obj_type != GOT_OBJ_TYPE_ANY) {
1381 err = got_object_get_type(&matched_type, repo,
1385 if (matched_type != obj_type)
1388 *unique_id = got_object_id_dup(&id);
1389 if (*unique_id == NULL) {
1390 err = got_error_from_errno("got_object_id_dup");
1395 if (got_object_id_cmp(*unique_id, &id) == 0)
1396 continue; /* both packed and loose */
1397 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1403 if (dir && closedir(dir) != 0 && err == NULL)
1404 err = got_error_from_errno("closedir");
1413 const struct got_error *
1414 got_repo_match_object_id_prefix(struct got_object_id **id,
1415 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 char *path_objects = got_repo_get_path_objects(repo);
1419 char *object_dir = NULL;
1425 for (i = 0; i < strlen(id_str_prefix); i++) {
1426 if (isxdigit((unsigned char)id_str_prefix[i]))
1428 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1431 len = strlen(id_str_prefix);
1433 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1436 object_dir = strndup(id_str_prefix, 2);
1437 if (object_dir == NULL) {
1438 err = got_error_from_errno("strdup");
1441 err = match_loose_object(id, path_objects, object_dir,
1442 id_str_prefix, obj_type, repo);
1443 } else if (len == 1) {
1445 for (i = 0; i < 0xf; i++) {
1446 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1448 err = got_error_from_errno("asprintf");
1451 err = match_packed_object(id, repo, object_dir,
1455 err = match_loose_object(id, path_objects, object_dir,
1456 id_str_prefix, obj_type, repo);
1461 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1469 } else if (*id == NULL)
1470 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1475 const struct got_error *
1476 got_repo_match_object_id(struct got_object_id **id, char **label,
1477 const char *id_str, int obj_type, struct got_reflist_head *refs,
1478 struct got_repository *repo)
1480 const struct got_error *err;
1481 struct got_tag_object *tag;
1482 struct got_reference *ref = NULL;
1489 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1492 *id = got_object_id_dup(
1493 got_object_tag_get_object_id(tag));
1495 err = got_error_from_errno("got_object_id_dup");
1496 else if (label && asprintf(label, "refs/tags/%s",
1497 got_object_tag_get_name(tag)) == -1) {
1498 err = got_error_from_errno("asprintf");
1502 got_object_tag_close(tag);
1504 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1505 err->code != GOT_ERR_NO_OBJ)
1509 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1511 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1513 err = got_ref_open(&ref, repo, id_str, 0);
1517 *label = strdup(got_ref_get_name(ref));
1518 if (*label == NULL) {
1519 err = got_error_from_errno("strdup");
1523 err = got_ref_resolve(id, repo, ref);
1525 err = got_object_id_str(label, *id);
1526 if (*label == NULL) {
1527 err = got_error_from_errno("strdup");
1537 const struct got_error *
1538 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1539 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1541 const struct got_error *err = NULL;
1542 struct got_reflist_entry *re;
1543 struct got_object_id *tag_id;
1544 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1548 TAILQ_FOREACH(re, refs, entry) {
1549 const char *refname;
1550 refname = got_ref_get_name(re->ref);
1551 if (got_ref_is_symbolic(re->ref))
1553 if (strncmp(refname, "refs/tags/", 10) != 0)
1555 if (!name_is_absolute)
1556 refname += strlen("refs/tags/");
1557 if (strcmp(refname, name) != 0)
1559 err = got_ref_resolve(&tag_id, repo, re->ref);
1562 err = got_object_open_as_tag(tag, repo, tag_id);
1566 if (obj_type == GOT_OBJ_TYPE_ANY ||
1567 got_object_tag_get_object_type(*tag) == obj_type)
1569 got_object_tag_close(*tag);
1573 if (err == NULL && *tag == NULL)
1574 err = got_error_path(name, GOT_ERR_NO_OBJ);
1578 static const struct got_error *
1579 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1580 const char *name, mode_t mode, struct got_object_id *blob_id)
1582 const struct got_error *err = NULL;
1586 *new_te = calloc(1, sizeof(**new_te));
1587 if (*new_te == NULL)
1588 return got_error_from_errno("calloc");
1590 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1591 sizeof((*new_te)->name)) {
1592 err = got_error(GOT_ERR_NO_SPACE);
1596 if (S_ISLNK(mode)) {
1597 (*new_te)->mode = S_IFLNK;
1599 (*new_te)->mode = S_IFREG;
1600 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1602 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1604 if (err && *new_te) {
1611 static const struct got_error *
1612 import_file(struct got_tree_entry **new_te, struct dirent *de,
1613 const char *path, struct got_repository *repo)
1615 const struct got_error *err;
1616 struct got_object_id *blob_id = NULL;
1620 if (asprintf(&filepath, "%s%s%s", path,
1621 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1622 return got_error_from_errno("asprintf");
1624 if (lstat(filepath, &sb) != 0) {
1625 err = got_error_from_errno2("lstat", path);
1629 err = got_object_blob_create(&blob_id, filepath, repo);
1633 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1642 static const struct got_error *
1643 insert_tree_entry(struct got_tree_entry *new_te,
1644 struct got_pathlist_head *paths)
1646 const struct got_error *err = NULL;
1647 struct got_pathlist_entry *new_pe;
1649 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1653 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1657 static const struct got_error *write_tree(struct got_object_id **,
1658 const char *, struct got_pathlist_head *, struct got_repository *,
1659 got_repo_import_cb progress_cb, void *progress_arg);
1661 static const struct got_error *
1662 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1663 const char *path, struct got_pathlist_head *ignores,
1664 struct got_repository *repo,
1665 got_repo_import_cb progress_cb, void *progress_arg)
1667 const struct got_error *err;
1668 struct got_object_id *id = NULL;
1671 if (asprintf(&subdirpath, "%s%s%s", path,
1672 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1673 return got_error_from_errno("asprintf");
1675 (*new_te) = calloc(1, sizeof(**new_te));
1676 if (*new_te == NULL)
1677 return got_error_from_errno("calloc");
1678 (*new_te)->mode = S_IFDIR;
1679 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1680 sizeof((*new_te)->name)) {
1681 err = got_error(GOT_ERR_NO_SPACE);
1684 err = write_tree(&id, subdirpath, ignores, repo,
1685 progress_cb, progress_arg);
1688 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1700 static const struct got_error *
1701 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1702 struct got_pathlist_head *ignores, struct got_repository *repo,
1703 got_repo_import_cb progress_cb, void *progress_arg)
1705 const struct got_error *err = NULL;
1709 struct got_tree_entry *new_te = NULL;
1710 struct got_pathlist_head paths;
1711 struct got_pathlist_entry *pe;
1713 *new_tree_id = NULL;
1717 dir = opendir(path_dir);
1719 err = got_error_from_errno2("opendir", path_dir);
1724 while ((de = readdir(dir)) != NULL) {
1728 if (strcmp(de->d_name, ".") == 0 ||
1729 strcmp(de->d_name, "..") == 0)
1732 TAILQ_FOREACH(pe, ignores, entry) {
1733 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1741 err = got_path_dirent_type(&type, path_dir, de);
1745 if (type == DT_DIR) {
1746 err = import_subdir(&new_te, de, path_dir,
1747 ignores, repo, progress_cb, progress_arg);
1749 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1754 } else if (type == DT_REG || type == DT_LNK) {
1755 err = import_file(&new_te, de, path_dir, repo);
1761 err = insert_tree_entry(new_te, &paths);
1767 if (TAILQ_EMPTY(&paths)) {
1768 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1769 "cannot create tree without any entries");
1773 TAILQ_FOREACH(pe, &paths, entry) {
1774 struct got_tree_entry *te = pe->data;
1776 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1778 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1779 err = got_error_from_errno("asprintf");
1782 err = (*progress_cb)(progress_arg, path);
1788 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1792 got_pathlist_free(&paths);
1796 const struct got_error *
1797 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1798 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1799 struct got_repository *repo, got_repo_import_cb progress_cb,
1802 const struct got_error *err;
1803 struct got_object_id *new_tree_id;
1805 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1806 progress_cb, progress_arg);
1810 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1811 author, time(NULL), author, time(NULL), logmsg, repo);
1816 const struct got_error *
1817 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1818 struct got_repository *repo)
1820 const struct got_error *err = NULL;
1821 char *path_objects = NULL, *path = NULL;
1823 struct got_object_id id;
1829 path_objects = got_repo_get_path_objects(repo);
1830 if (path_objects == NULL)
1831 return got_error_from_errno("got_repo_get_path_objects");
1833 for (i = 0; i <= 0xff; i++) {
1834 struct dirent *dent;
1836 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1837 err = got_error_from_errno("asprintf");
1841 dir = opendir(path);
1843 if (errno == ENOENT) {
1847 err = got_error_from_errno2("opendir", path);
1851 while ((dent = readdir(dir)) != NULL) {
1856 if (strcmp(dent->d_name, ".") == 0 ||
1857 strcmp(dent->d_name, "..") == 0)
1860 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1861 err = got_error_from_errno("asprintf");
1865 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1871 err = got_object_open_loose_fd(&fd, &id, repo);
1875 if (fstat(fd, &sb) == -1) {
1876 err = got_error_from_errno("fstat");
1881 (*ondisk_size) += sb.st_size;
1883 if (close(fd) == -1) {
1884 err = got_error_from_errno("close");
1889 if (closedir(dir) != 0) {
1890 err = got_error_from_errno("closedir");
1899 if (dir && closedir(dir) != 0 && err == NULL)
1900 err = got_error_from_errno("closedir");
1911 const struct got_error *
1912 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1913 off_t *total_packsize, struct got_repository *repo)
1915 const struct got_error *err;
1916 DIR *packdir = NULL;
1917 struct dirent *dent;
1918 struct got_packidx *packidx = NULL;
1920 char *path_packfile;
1926 *total_packsize = 0;
1928 packdir_fd = openat(got_repo_get_fd(repo),
1929 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1930 if (packdir_fd == -1) {
1931 return got_error_from_errno_fmt("openat: %s/%s",
1932 got_repo_get_path_git_dir(repo),
1933 GOT_OBJECTS_PACK_DIR);
1936 packdir = fdopendir(packdir_fd);
1937 if (packdir == NULL) {
1938 err = got_error_from_errno("fdopendir");
1942 while ((dent = readdir(packdir)) != NULL) {
1943 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1946 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1947 dent->d_name) == -1) {
1948 err = got_error_from_errno("asprintf");
1952 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1958 if (fstat(packidx->fd, &sb) == -1)
1960 *total_packsize += sb.st_size;
1962 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1966 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1968 free(path_packfile);
1971 free(path_packfile);
1972 *total_packsize += sb.st_size;
1974 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1978 got_packidx_close(packidx);
1983 got_packidx_close(packidx);
1984 if (packdir && closedir(packdir) != 0 && err == NULL)
1985 err = got_error_from_errno("closedir");
1989 *total_packsize = 0;