Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/syslimits.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sha1.h>
30 #include <string.h>
31 #include <zlib.h>
32 #include <errno.h>
33 #include <libgen.h>
34 #include <stdint.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_worktree.h"
41 #include "got_object.h"
43 #include "got_lib_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_repository.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 #define GOT_GIT_DIR ".git"
59 /* Mandatory files and directories inside the git directory. */
60 #define GOT_OBJECTS_DIR "objects"
61 #define GOT_REFS_DIR "refs"
62 #define GOT_HEAD_FILE "HEAD"
64 /* Other files and directories inside the git directory. */
65 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
66 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
67 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 char *
70 got_repo_get_path(struct got_repository *repo)
71 {
72 return strdup(repo->path);
73 }
75 char *
76 got_repo_get_path_git_dir(struct got_repository *repo)
77 {
78 return strdup(repo->path_git_dir);
79 }
81 int
82 got_repo_is_bare(struct got_repository *repo)
83 {
84 return (strcmp(repo->path, repo->path_git_dir) == 0);
85 }
87 static char *
88 get_path_git_child(struct got_repository *repo, const char *basename)
89 {
90 char *path_child;
92 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
93 basename) == -1)
94 return NULL;
96 return path_child;
97 }
99 char *
100 got_repo_get_path_objects(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_OBJECTS_DIR);
105 char *
106 got_repo_get_path_objects_pack(struct got_repository *repo)
108 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
111 char *
112 got_repo_get_path_refs(struct got_repository *repo)
114 return get_path_git_child(repo, GOT_REFS_DIR);
117 static char *
118 get_path_head(struct got_repository *repo)
120 return get_path_git_child(repo, GOT_HEAD_FILE);
123 static int
124 is_git_repo(struct got_repository *repo)
126 char *path_git = got_repo_get_path_git_dir(repo);
127 char *path_objects = got_repo_get_path_objects(repo);
128 char *path_refs = got_repo_get_path_refs(repo);
129 char *path_head = get_path_head(repo);
130 int ret = 0;
131 struct stat sb;
132 struct got_reference *head_ref;
134 if (lstat(path_git, &sb) == -1)
135 goto done;
136 if (!S_ISDIR(sb.st_mode))
137 goto done;
139 if (lstat(path_objects, &sb) == -1)
140 goto done;
141 if (!S_ISDIR(sb.st_mode))
142 goto done;
144 if (lstat(path_refs, &sb) == -1)
145 goto done;
146 if (!S_ISDIR(sb.st_mode))
147 goto done;
149 if (lstat(path_head, &sb) == -1)
150 goto done;
151 if (!S_ISREG(sb.st_mode))
152 goto done;
154 /* Check if the HEAD reference can be opened. */
155 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
156 goto done;
157 got_ref_close(head_ref);
159 ret = 1;
160 done:
161 free(path_git);
162 free(path_objects);
163 free(path_refs);
164 free(path_head);
165 return ret;
169 const struct got_error *
170 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
171 struct got_object *obj)
173 #ifndef GOT_NO_OBJ_CACHE
174 const struct got_error *err = NULL;
175 err = got_object_cache_add(&repo->objcache, id, obj);
176 if (err)
177 return err;
178 obj->refcnt++;
179 #endif
180 return NULL;
183 struct got_object *
184 got_repo_get_cached_object(struct got_repository *repo,
185 struct got_object_id *id)
187 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
190 const struct got_error *
191 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
192 struct got_tree_object *tree)
194 #ifndef GOT_NO_OBJ_CACHE
195 const struct got_error *err = NULL;
196 err = got_object_cache_add(&repo->treecache, id, tree);
197 if (err)
198 return err;
199 tree->refcnt++;
200 #endif
201 return NULL;
204 struct got_tree_object *
205 got_repo_get_cached_tree(struct got_repository *repo,
206 struct got_object_id *id)
208 return (struct got_tree_object *)got_object_cache_get(
209 &repo->treecache, id);
212 const struct got_error *
213 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
214 struct got_commit_object *commit)
216 #ifndef GOT_NO_OBJ_CACHE
217 const struct got_error *err = NULL;
218 err = got_object_cache_add(&repo->commitcache, id, commit);
219 if (err)
220 return err;
221 commit->refcnt++;
222 #endif
223 return NULL;
226 struct got_commit_object *
227 got_repo_get_cached_commit(struct got_repository *repo,
228 struct got_object_id *id)
230 return (struct got_commit_object *)got_object_cache_get(
231 &repo->commitcache, id);
234 const struct got_error *
235 open_repo(struct got_repository *repo, const char *path)
237 const struct got_error *err = NULL;
238 struct got_worktree *worktree = NULL;
240 /* bare git repository? */
241 repo->path_git_dir = strdup(path);
242 if (repo->path_git_dir == NULL) {
243 err = got_error_from_errno();
244 goto done;
246 if (is_git_repo(repo)) {
247 repo->path = strdup(repo->path_git_dir);
248 if (repo->path == NULL) {
249 err = got_error_from_errno();
250 goto done;
252 return NULL;
255 /* git repository with working tree? */
256 free(repo->path_git_dir);
257 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
258 err = got_error_from_errno();
259 goto done;
261 if (is_git_repo(repo)) {
262 repo->path = strdup(path);
263 if (repo->path == NULL) {
264 err = got_error_from_errno();
265 goto done;
267 return NULL;
270 /* got work tree checked out from bare git repository? */
271 free(repo->path_git_dir);
272 repo->path_git_dir = NULL;
273 err = got_worktree_open(&worktree, path);
274 if (err) {
275 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
276 err = got_error(GOT_ERR_NOT_GIT_REPO);
277 goto done;
279 repo->path_git_dir = strdup(worktree->repo_path);
280 if (repo->path_git_dir == NULL) {
281 err = got_error_from_errno();
282 goto done;
285 /* got work tree checked out from git repository with working tree? */
286 if (!is_git_repo(repo)) {
287 free(repo->path_git_dir);
288 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
289 GOT_GIT_DIR) == -1) {
290 err = got_error_from_errno();
291 repo->path_git_dir = NULL;
292 goto done;
294 if (!is_git_repo(repo)) {
295 err = got_error(GOT_ERR_NOT_GIT_REPO);
296 goto done;
298 repo->path = strdup(worktree->repo_path);
299 if (repo->path == NULL) {
300 err = got_error_from_errno();
301 goto done;
303 } else {
304 repo->path = strdup(repo->path_git_dir);
305 if (repo->path == NULL) {
306 err = got_error_from_errno();
307 goto done;
310 done:
311 if (worktree)
312 got_worktree_close(worktree);
313 return err;
316 const struct got_error *
317 got_repo_open(struct got_repository **repop, const char *path)
319 struct got_repository *repo = NULL;
320 const struct got_error *err = NULL;
321 char *abspath, *normpath = NULL;
322 int i, tried_root = 0;
324 *repop = NULL;
326 if (got_path_is_absolute(path))
327 abspath = strdup(path);
328 else
329 abspath = got_path_get_absolute(path);
330 if (abspath == NULL)
331 return got_error(GOT_ERR_BAD_PATH);
333 repo = calloc(1, sizeof(*repo));
334 if (repo == NULL) {
335 err = got_error_from_errno();
336 goto done;
339 for (i = 0; i < nitems(repo->privsep_children); i++) {
340 memset(&repo->privsep_children[i], 0,
341 sizeof(repo->privsep_children[0]));
342 repo->privsep_children[i].imsg_fd = -1;
345 err = got_object_cache_init(&repo->objcache,
346 GOT_OBJECT_CACHE_TYPE_OBJ);
347 if (err)
348 goto done;
349 err = got_object_cache_init(&repo->treecache,
350 GOT_OBJECT_CACHE_TYPE_TREE);
351 if (err)
352 goto done;
353 err = got_object_cache_init(&repo->commitcache,
354 GOT_OBJECT_CACHE_TYPE_COMMIT);
355 if (err)
356 goto done;
358 normpath = got_path_normalize(abspath);
359 if (normpath == NULL) {
360 err = got_error(GOT_ERR_BAD_PATH);
361 goto done;
364 path = normpath;
365 do {
366 err = open_repo(repo, path);
367 if (err == NULL)
368 break;
369 if (err->code != GOT_ERR_NOT_GIT_REPO)
370 break;
371 if (path[0] == '/' && path[1] == '\0') {
372 if (tried_root) {
373 err = got_error(GOT_ERR_NOT_GIT_REPO);
374 break;
376 tried_root = 1;
378 path = dirname(path);
379 if (path == NULL)
380 err = got_error_from_errno();
381 } while (path);
382 done:
383 if (err)
384 got_repo_close(repo);
385 else
386 *repop = repo;
387 free(abspath);
388 free(normpath);
389 return err;
392 const struct got_error *
393 got_repo_close(struct got_repository *repo)
395 const struct got_error *err = NULL, *child_err;
396 int i;
398 for (i = 0; i < nitems(repo->packidx_cache); i++) {
399 if (repo->packidx_cache[i] == NULL)
400 break;
401 got_packidx_close(repo->packidx_cache[i]);
404 for (i = 0; i < nitems(repo->packs); i++) {
405 if (repo->packs[i].path_packfile == NULL)
406 break;
407 got_pack_close(&repo->packs[i]);
410 free(repo->path);
411 free(repo->path_git_dir);
413 got_object_cache_close(&repo->objcache);
414 got_object_cache_close(&repo->treecache);
415 got_object_cache_close(&repo->commitcache);
417 for (i = 0; i < nitems(repo->privsep_children); i++) {
418 if (repo->privsep_children[i].imsg_fd == -1)
419 continue;
420 imsg_clear(repo->privsep_children[i].ibuf);
421 free(repo->privsep_children[i].ibuf);
422 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
423 child_err = got_privsep_wait_for_child(
424 repo->privsep_children[i].pid);
425 if (child_err && err == NULL)
426 err = child_err;
427 close(repo->privsep_children[i].imsg_fd);
429 free(repo);
431 return err;
434 const struct got_error *
435 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
436 const char *input_path, int check_disk)
438 const struct got_error *err = NULL;
439 char *repo_abspath = NULL, *cwd = NULL;
440 struct stat sb;
441 size_t repolen, cwdlen, len;
442 char *canonpath, *path = NULL;
444 *in_repo_path = NULL;
446 cwd = getcwd(NULL, 0);
447 if (cwd == NULL)
448 return got_error_from_errno();
450 canonpath = strdup(input_path);
451 if (canonpath == NULL) {
452 err = got_error_from_errno();
453 goto done;
455 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
456 if (err)
457 goto done;
459 repo_abspath = got_repo_get_path(repo);
460 if (repo_abspath == NULL) {
461 err = got_error_from_errno();
462 goto done;
465 /* TODO: Call "get in-repository path of work-tree node" API. */
467 if (!check_disk)
468 path = strdup(canonpath);
469 else if (lstat(canonpath, &sb) != 0) {
470 if (errno != ENOENT) {
471 err = got_error_from_errno();
472 goto done;
474 /*
475 * Path is not on disk.
476 * Assume it is already relative to repository root.
477 */
478 path = strdup(canonpath);
479 } else {
480 int is_repo_child = 0, is_cwd_child = 0;
482 path = realpath(canonpath, NULL);
483 if (path == NULL) {
484 err = got_error_from_errno();
485 goto done;
488 repolen = strlen(repo_abspath);
489 cwdlen = strlen(cwd);
490 len = strlen(path);
492 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
493 is_repo_child = 1;
494 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
495 is_cwd_child = 1;
497 if (strcmp(path, repo_abspath) == 0) {
498 free(path);
499 path = strdup("");
500 if (path == NULL) {
501 err = got_error_from_errno();
502 goto done;
504 } else if (is_repo_child && is_cwd_child) {
505 char *child;
506 /* TODO: Is path inside a got worktree? */
507 /* Strip common prefix with repository path. */
508 err = got_path_skip_common_ancestor(&child,
509 repo_abspath, path);
510 if (err)
511 goto done;
512 free(path);
513 path = child;
514 } else if (is_repo_child) {
515 /* Matched an on-disk path inside repository. */
516 if (got_repo_is_bare(repo)) {
517 /*
518 * Matched an on-disk path inside repository
519 * database. Treat as repository-relative.
520 */
521 } else {
522 char *child;
523 /* Strip common prefix with repository path. */
524 err = got_path_skip_common_ancestor(&child,
525 repo_abspath, path);
526 if (err)
527 goto done;
528 free(path);
529 path = child;
531 } else if (is_cwd_child) {
532 char *child;
533 /* TODO: Is path inside a got worktree? */
534 /* Strip common prefix with cwd. */
535 err = got_path_skip_common_ancestor(&child, cwd,
536 path);
537 if (err)
538 goto done;
539 free(path);
540 path = child;
541 } else {
542 /*
543 * Matched unrelated on-disk path.
544 * Treat it as repository-relative.
545 */
549 /* Make in-repository path absolute */
550 if (path[0] != '/') {
551 char *abspath;
552 if (asprintf(&abspath, "/%s", path) == -1) {
553 err = got_error_from_errno();
554 goto done;
556 free(path);
557 path = abspath;
560 done:
561 free(repo_abspath);
562 free(cwd);
563 free(canonpath);
564 if (err)
565 free(path);
566 else
567 *in_repo_path = path;
568 return err;
571 const struct got_error *
572 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
574 const struct got_error *err = NULL;
575 int i;
577 for (i = 0; i < nitems(repo->packidx_cache); i++) {
578 if (repo->packidx_cache[i] == NULL)
579 break;
581 if (i == nitems(repo->packidx_cache)) {
582 err = got_packidx_close(repo->packidx_cache[i - 1]);
583 if (err)
584 return err;
587 /*
588 * Insert the new pack index at the front so it will
589 * be searched first in the future.
590 */
591 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
592 sizeof(repo->packidx_cache) -
593 sizeof(repo->packidx_cache[0]));
594 repo->packidx_cache[0] = packidx;
596 return NULL;
599 static int
600 is_packidx_filename(const char *name, size_t len)
602 if (len != GOT_PACKIDX_NAMELEN)
603 return 0;
605 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
606 return 0;
608 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
609 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
610 return 0;
612 return 1;
615 const struct got_error *
616 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
617 struct got_repository *repo, struct got_object_id *id)
619 const struct got_error *err;
620 char *path_packdir;
621 DIR *packdir;
622 struct dirent *dent;
623 char *path_packidx;
624 int i;
626 /* Search pack index cache. */
627 for (i = 0; i < nitems(repo->packidx_cache); i++) {
628 if (repo->packidx_cache[i] == NULL)
629 break;
630 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
631 if (*idx != -1) {
632 *packidx = repo->packidx_cache[i];
633 return NULL;
636 /* No luck. Search the filesystem. */
638 path_packdir = got_repo_get_path_objects_pack(repo);
639 if (path_packdir == NULL)
640 return got_error_from_errno();
642 packdir = opendir(path_packdir);
643 if (packdir == NULL) {
644 err = got_error_from_errno();
645 goto done;
648 while ((dent = readdir(packdir)) != NULL) {
649 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
650 continue;
652 if (asprintf(&path_packidx, "%s/%s", path_packdir,
653 dent->d_name) == -1) {
654 err = got_error_from_errno();
655 goto done;
658 err = got_packidx_open(packidx, path_packidx, 0);
659 free(path_packidx);
660 if (err)
661 goto done;
663 *idx = got_packidx_get_object_idx(*packidx, id);
664 if (*idx != -1) {
665 err = NULL; /* found the object */
666 err = got_repo_cache_packidx(repo, *packidx);
667 goto done;
670 err = got_packidx_close(*packidx);
671 *packidx = NULL;
672 if (err)
673 goto done;
676 err = got_error(GOT_ERR_NO_OBJ);
677 done:
678 free(path_packdir);
679 if (packdir && closedir(packdir) != 0 && err == 0)
680 err = got_error_from_errno();
681 return err;
684 static const struct got_error *
685 read_packfile_hdr(int fd, struct got_packidx *packidx)
687 const struct got_error *err = NULL;
688 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
689 struct got_packfile_hdr hdr;
690 ssize_t n;
692 n = read(fd, &hdr, sizeof(hdr));
693 if (n < 0)
694 return got_error_from_errno();
695 if (n != sizeof(hdr))
696 return got_error(GOT_ERR_BAD_PACKFILE);
698 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
699 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
700 betoh32(hdr.nobjects) != totobj)
701 err = got_error(GOT_ERR_BAD_PACKFILE);
703 return err;
706 static const struct got_error *
707 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
709 const struct got_error *err = NULL;
711 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
712 if (*fd == -1)
713 return got_error_from_errno();
715 if (packidx) {
716 err = read_packfile_hdr(*fd, packidx);
717 if (err) {
718 close(*fd);
719 *fd = -1;
723 return err;
726 const struct got_error *
727 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
728 const char *path_packfile, struct got_packidx *packidx)
730 const struct got_error *err = NULL;
731 struct got_pack *pack = NULL;
732 int i;
734 if (packp)
735 *packp = NULL;
737 for (i = 0; i < nitems(repo->packs); i++) {
738 pack = &repo->packs[i];
739 if (pack->path_packfile == NULL)
740 break;
741 if (strcmp(pack->path_packfile, path_packfile) == 0)
742 return NULL;
745 if (i == nitems(repo->packs) - 1) {
746 err = got_pack_close(&repo->packs[i - 1]);
747 if (err)
748 return err;
749 memmove(&repo->packs[1], &repo->packs[0],
750 sizeof(repo->packs) - sizeof(repo->packs[0]));
751 i = 0;
754 pack = &repo->packs[i];
756 pack->path_packfile = strdup(path_packfile);
757 if (pack->path_packfile == NULL) {
758 err = got_error_from_errno();
759 goto done;
762 err = open_packfile(&pack->fd, path_packfile, packidx);
763 if (err)
764 goto done;
766 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
767 if (err)
768 goto done;
770 pack->privsep_child = NULL;
772 #ifndef GOT_PACK_NO_MMAP
773 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
774 pack->fd, 0);
775 if (pack->map == MAP_FAILED)
776 pack->map = NULL; /* fall back to read(2) */
777 #endif
778 done:
779 if (err) {
780 if (pack) {
781 free(pack->path_packfile);
782 memset(pack, 0, sizeof(*pack));
784 } else if (packp)
785 *packp = pack;
786 return err;
789 struct got_pack *
790 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
792 struct got_pack *pack = NULL;
793 int i;
795 for (i = 0; i < nitems(repo->packs); i++) {
796 pack = &repo->packs[i];
797 if (pack->path_packfile == NULL)
798 break;
799 if (strcmp(pack->path_packfile, path_packfile) == 0)
800 return pack;
803 return NULL;