2 * Copyright (c) 2018, 2019 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>
22 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_worktree.h"
43 #include "got_object.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_repository.h"
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 #define GOT_GIT_DIR ".git"
60 /* Mandatory files and directories inside the git directory. */
61 #define GOT_OBJECTS_DIR "objects"
62 #define GOT_REFS_DIR "refs"
63 #define GOT_HEAD_FILE "HEAD"
65 /* Other files and directories inside the git directory. */
66 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
67 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
68 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 #define GOT_PACKED_REFS_FILE "packed-refs"
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_is_bare(struct got_repository *repo)
86 return (strcmp(repo->path, repo->path_git_dir) == 0);
90 get_path_git_child(struct got_repository *repo, const char *basename)
94 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
102 got_repo_get_path_objects(struct got_repository *repo)
104 return get_path_git_child(repo, GOT_OBJECTS_DIR);
108 got_repo_get_path_objects_pack(struct got_repository *repo)
110 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
114 got_repo_get_path_refs(struct got_repository *repo)
116 return get_path_git_child(repo, GOT_REFS_DIR);
120 got_repo_get_path_packed_refs(struct got_repository *repo)
122 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
126 get_path_head(struct got_repository *repo)
128 return get_path_git_child(repo, GOT_HEAD_FILE);
132 is_git_repo(struct got_repository *repo)
134 const char *path_git = got_repo_get_path_git_dir(repo);
135 char *path_objects = got_repo_get_path_objects(repo);
136 char *path_refs = got_repo_get_path_refs(repo);
137 char *path_head = get_path_head(repo);
140 struct got_reference *head_ref;
142 if (lstat(path_git, &sb) == -1)
144 if (!S_ISDIR(sb.st_mode))
147 if (lstat(path_objects, &sb) == -1)
149 if (!S_ISDIR(sb.st_mode))
152 if (lstat(path_refs, &sb) == -1)
154 if (!S_ISDIR(sb.st_mode))
157 if (lstat(path_head, &sb) == -1)
159 if (!S_ISREG(sb.st_mode))
162 /* Check if the HEAD reference can be opened. */
163 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
165 got_ref_close(head_ref);
176 const struct got_error *
177 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
178 struct got_object *obj)
180 #ifndef GOT_NO_OBJ_CACHE
181 const struct got_error *err = NULL;
182 err = got_object_cache_add(&repo->objcache, id, obj);
184 if (err->code == GOT_ERR_OBJ_EXISTS ||
185 err->code == GOT_ERR_OBJ_TOO_LARGE)
195 got_repo_get_cached_object(struct got_repository *repo,
196 struct got_object_id *id)
198 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
201 const struct got_error *
202 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
203 struct got_tree_object *tree)
205 #ifndef GOT_NO_OBJ_CACHE
206 const struct got_error *err = NULL;
207 err = got_object_cache_add(&repo->treecache, id, tree);
209 if (err->code == GOT_ERR_OBJ_EXISTS ||
210 err->code == GOT_ERR_OBJ_TOO_LARGE)
219 struct got_tree_object *
220 got_repo_get_cached_tree(struct got_repository *repo,
221 struct got_object_id *id)
223 return (struct got_tree_object *)got_object_cache_get(
224 &repo->treecache, id);
227 const struct got_error *
228 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
229 struct got_commit_object *commit)
231 #ifndef GOT_NO_OBJ_CACHE
232 const struct got_error *err = NULL;
233 err = got_object_cache_add(&repo->commitcache, id, commit);
235 if (err->code == GOT_ERR_OBJ_EXISTS ||
236 err->code == GOT_ERR_OBJ_TOO_LARGE)
245 struct got_commit_object *
246 got_repo_get_cached_commit(struct got_repository *repo,
247 struct got_object_id *id)
249 return (struct got_commit_object *)got_object_cache_get(
250 &repo->commitcache, id);
253 const struct got_error *
254 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
255 struct got_tag_object *tag)
257 #ifndef GOT_NO_OBJ_CACHE
258 const struct got_error *err = NULL;
259 err = got_object_cache_add(&repo->tagcache, id, tag);
261 if (err->code == GOT_ERR_OBJ_EXISTS ||
262 err->code == GOT_ERR_OBJ_TOO_LARGE)
271 struct got_tag_object *
272 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
274 return (struct got_tag_object *)got_object_cache_get(
275 &repo->tagcache, id);
278 const struct got_error *
279 open_repo(struct got_repository *repo, const char *path)
281 const struct got_error *err = NULL;
283 /* bare git repository? */
284 repo->path_git_dir = strdup(path);
285 if (repo->path_git_dir == NULL)
286 return got_error_from_errno("strdup");
287 if (is_git_repo(repo)) {
288 repo->path = strdup(repo->path_git_dir);
289 if (repo->path == NULL) {
290 err = got_error_from_errno("strdup");
296 /* git repository with working tree? */
297 free(repo->path_git_dir);
298 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
299 err = got_error_from_errno("asprintf");
302 if (is_git_repo(repo)) {
303 repo->path = strdup(path);
304 if (repo->path == NULL) {
305 err = got_error_from_errno("strdup");
311 err = got_error(GOT_ERR_NOT_GIT_REPO);
316 free(repo->path_git_dir);
317 repo->path_git_dir = NULL;
322 const struct got_error *
323 got_repo_open(struct got_repository **repop, const char *path)
325 struct got_repository *repo = NULL;
326 const struct got_error *err = NULL;
327 char *abspath, *normpath = NULL;
328 int i, tried_root = 0;
332 if (got_path_is_absolute(path))
333 abspath = strdup(path);
335 abspath = got_path_get_absolute(path);
337 return got_error(GOT_ERR_BAD_PATH);
339 repo = calloc(1, sizeof(*repo));
341 err = got_error_from_errno("calloc");
345 for (i = 0; i < nitems(repo->privsep_children); i++) {
346 memset(&repo->privsep_children[i], 0,
347 sizeof(repo->privsep_children[0]));
348 repo->privsep_children[i].imsg_fd = -1;
351 err = got_object_cache_init(&repo->objcache,
352 GOT_OBJECT_CACHE_TYPE_OBJ);
355 err = got_object_cache_init(&repo->treecache,
356 GOT_OBJECT_CACHE_TYPE_TREE);
359 err = got_object_cache_init(&repo->commitcache,
360 GOT_OBJECT_CACHE_TYPE_COMMIT);
363 err = got_object_cache_init(&repo->tagcache,
364 GOT_OBJECT_CACHE_TYPE_TAG);
368 normpath = got_path_normalize(abspath);
369 if (normpath == NULL) {
370 err = got_error(GOT_ERR_BAD_PATH);
376 err = open_repo(repo, path);
379 if (err->code != GOT_ERR_NOT_GIT_REPO)
381 if (path[0] == '/' && path[1] == '\0') {
383 err = got_error(GOT_ERR_NOT_GIT_REPO);
388 path = dirname(path);
390 err = got_error_from_errno2("dirname", path);
394 got_repo_close(repo);
402 const struct got_error *
403 got_repo_close(struct got_repository *repo)
405 const struct got_error *err = NULL, *child_err;
408 for (i = 0; i < nitems(repo->packidx_cache); i++) {
409 if (repo->packidx_cache[i] == NULL)
411 got_packidx_close(repo->packidx_cache[i]);
414 for (i = 0; i < nitems(repo->packs); i++) {
415 if (repo->packs[i].path_packfile == NULL)
417 got_pack_close(&repo->packs[i]);
421 free(repo->path_git_dir);
423 got_object_cache_close(&repo->objcache);
424 got_object_cache_close(&repo->treecache);
425 got_object_cache_close(&repo->commitcache);
426 got_object_cache_close(&repo->tagcache);
428 for (i = 0; i < nitems(repo->privsep_children); i++) {
429 if (repo->privsep_children[i].imsg_fd == -1)
431 imsg_clear(repo->privsep_children[i].ibuf);
432 free(repo->privsep_children[i].ibuf);
433 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
434 child_err = got_privsep_wait_for_child(
435 repo->privsep_children[i].pid);
436 if (child_err && err == NULL)
438 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
440 err = got_error_from_errno("close");
447 const struct got_error *
448 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
449 const char *input_path, int check_disk)
451 const struct got_error *err = NULL;
452 const char *repo_abspath = NULL;
453 size_t repolen, cwdlen, len;
454 char *cwd, *canonpath, *path = NULL;
456 *in_repo_path = NULL;
458 cwd = getcwd(NULL, 0);
460 return got_error_from_errno("getcwd");
462 canonpath = strdup(input_path);
463 if (canonpath == NULL) {
464 err = got_error_from_errno("strdup");
467 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
471 repo_abspath = got_repo_get_path(repo);
474 path = strdup(canonpath);
476 err = got_error_from_errno("strdup");
480 int is_repo_child = 0, is_cwd_child = 0;
482 path = realpath(canonpath, NULL);
484 if (errno != ENOENT) {
485 err = got_error_from_errno2("realpath",
490 * Path is not on disk.
491 * Assume it is already relative to repository root.
493 path = strdup(canonpath);
495 err = got_error_from_errno("strdup");
500 repolen = strlen(repo_abspath);
501 cwdlen = strlen(cwd);
504 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
506 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
509 if (strcmp(path, repo_abspath) == 0) {
513 err = got_error_from_errno("strdup");
516 } else if (is_repo_child && is_cwd_child) {
518 /* Strip common prefix with repository path. */
519 err = got_path_skip_common_ancestor(&child,
525 } else if (is_repo_child) {
526 /* Matched an on-disk path inside repository. */
527 if (got_repo_is_bare(repo)) {
529 * Matched an on-disk path inside repository
530 * database. Treat as repository-relative.
534 /* Strip common prefix with repository path. */
535 err = got_path_skip_common_ancestor(&child,
542 } else if (is_cwd_child) {
544 /* Strip common prefix with cwd. */
545 err = got_path_skip_common_ancestor(&child, cwd,
553 * Matched unrelated on-disk path.
554 * Treat it as repository-relative.
559 /* Make in-repository path absolute */
560 if (path[0] != '/') {
562 if (asprintf(&abspath, "/%s", path) == -1) {
563 err = got_error_from_errno("asprintf");
576 *in_repo_path = path;
580 const struct got_error *
581 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
583 const struct got_error *err = NULL;
586 for (i = 0; i < nitems(repo->packidx_cache); i++) {
587 if (repo->packidx_cache[i] == NULL)
590 if (i == nitems(repo->packidx_cache)) {
591 err = got_packidx_close(repo->packidx_cache[i - 1]);
597 * Insert the new pack index at the front so it will
598 * be searched first in the future.
600 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
601 sizeof(repo->packidx_cache) -
602 sizeof(repo->packidx_cache[0]));
603 repo->packidx_cache[0] = packidx;
609 is_packidx_filename(const char *name, size_t len)
611 if (len != GOT_PACKIDX_NAMELEN)
614 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
617 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
618 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
624 const struct got_error *
625 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
626 struct got_repository *repo, struct got_object_id *id)
628 const struct got_error *err;
635 /* Search pack index cache. */
636 for (i = 0; i < nitems(repo->packidx_cache); i++) {
637 if (repo->packidx_cache[i] == NULL)
639 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
641 *packidx = repo->packidx_cache[i];
645 /* No luck. Search the filesystem. */
647 path_packdir = got_repo_get_path_objects_pack(repo);
648 if (path_packdir == NULL)
649 return got_error_from_errno("got_repo_get_path_objects_pack");
651 packdir = opendir(path_packdir);
652 if (packdir == NULL) {
653 err = got_error_from_errno2("opendir", path_packdir);
657 while ((dent = readdir(packdir)) != NULL) {
658 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
661 if (asprintf(&path_packidx, "%s/%s", path_packdir,
662 dent->d_name) == -1) {
663 err = got_error_from_errno("asprintf");
667 err = got_packidx_open(packidx, path_packidx, 0);
672 *idx = got_packidx_get_object_idx(*packidx, id);
674 err = NULL; /* found the object */
675 err = got_repo_cache_packidx(repo, *packidx);
679 err = got_packidx_close(*packidx);
685 err = got_error_no_obj(id);
688 if (packdir && closedir(packdir) != 0 && err == 0)
689 err = got_error_from_errno("closedir");
693 static const struct got_error *
694 read_packfile_hdr(int fd, struct got_packidx *packidx)
696 const struct got_error *err = NULL;
697 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
698 struct got_packfile_hdr hdr;
701 n = read(fd, &hdr, sizeof(hdr));
703 return got_error_from_errno("read");
704 if (n != sizeof(hdr))
705 return got_error(GOT_ERR_BAD_PACKFILE);
707 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
708 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
709 betoh32(hdr.nobjects) != totobj)
710 err = got_error(GOT_ERR_BAD_PACKFILE);
715 static const struct got_error *
716 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
718 const struct got_error *err = NULL;
720 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
722 return got_error_from_errno2("open", path_packfile);
725 err = read_packfile_hdr(*fd, packidx);
735 const struct got_error *
736 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
737 const char *path_packfile, struct got_packidx *packidx)
739 const struct got_error *err = NULL;
740 struct got_pack *pack = NULL;
747 for (i = 0; i < nitems(repo->packs); i++) {
748 pack = &repo->packs[i];
749 if (pack->path_packfile == NULL)
751 if (strcmp(pack->path_packfile, path_packfile) == 0)
755 if (i == nitems(repo->packs) - 1) {
756 err = got_pack_close(&repo->packs[i - 1]);
759 memmove(&repo->packs[1], &repo->packs[0],
760 sizeof(repo->packs) - sizeof(repo->packs[0]));
764 pack = &repo->packs[i];
766 pack->path_packfile = strdup(path_packfile);
767 if (pack->path_packfile == NULL) {
768 err = got_error_from_errno("strdup");
772 err = open_packfile(&pack->fd, path_packfile, packidx);
776 if (fstat(pack->fd, &sb) != 0) {
777 err = got_error_from_errno("fstat");
780 pack->filesize = sb.st_size;
782 pack->privsep_child = NULL;
784 #ifndef GOT_PACK_NO_MMAP
785 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
787 if (pack->map == MAP_FAILED) {
788 if (errno != ENOMEM) {
789 err = got_error_from_errno("mmap");
792 pack->map = NULL; /* fall back to read(2) */
798 free(pack->path_packfile);
799 memset(pack, 0, sizeof(*pack));
807 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
809 struct got_pack *pack = NULL;
812 for (i = 0; i < nitems(repo->packs); i++) {
813 pack = &repo->packs[i];
814 if (pack->path_packfile == NULL)
816 if (strcmp(pack->path_packfile, path_packfile) == 0)