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 got_repo_cache_mini_commit(struct got_repository *repo,
236 struct got_object_id *id, struct got_commit_object_mini *commit)
238 #ifndef GOT_NO_OBJ_CACHE
239 const struct got_error *err = NULL;
240 err = got_object_cache_add(&repo->minicommitcache, id, commit);
241 if (err)
242 return err;
243 commit->refcnt++;
244 #endif
245 return NULL;
248 struct got_commit_object_mini *
249 got_repo_get_cached_mini_commit(struct got_repository *repo,
250 struct got_object_id *id)
252 return (struct got_commit_object_mini *)got_object_cache_get(
253 &repo->minicommitcache, id);
256 const struct got_error *
257 open_repo(struct got_repository *repo, const char *path)
259 const struct got_error *err = NULL;
260 struct got_worktree *worktree = NULL;
262 /* bare git repository? */
263 repo->path_git_dir = strdup(path);
264 if (repo->path_git_dir == NULL) {
265 err = got_error_from_errno();
266 goto done;
268 if (is_git_repo(repo)) {
269 repo->path = strdup(repo->path_git_dir);
270 if (repo->path == NULL) {
271 err = got_error_from_errno();
272 goto done;
274 return NULL;
277 /* git repository with working tree? */
278 free(repo->path_git_dir);
279 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
280 err = got_error_from_errno();
281 goto done;
283 if (is_git_repo(repo)) {
284 repo->path = strdup(path);
285 if (repo->path == NULL) {
286 err = got_error_from_errno();
287 goto done;
289 return NULL;
292 /* got work tree checked out from bare git repository? */
293 free(repo->path_git_dir);
294 repo->path_git_dir = NULL;
295 err = got_worktree_open(&worktree, path);
296 if (err) {
297 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
298 err = got_error(GOT_ERR_NOT_GIT_REPO);
299 goto done;
301 repo->path_git_dir = strdup(worktree->repo_path);
302 if (repo->path_git_dir == NULL) {
303 err = got_error_from_errno();
304 goto done;
307 /* got work tree checked out from git repository with working tree? */
308 if (!is_git_repo(repo)) {
309 free(repo->path_git_dir);
310 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
311 GOT_GIT_DIR) == -1) {
312 err = got_error_from_errno();
313 repo->path_git_dir = NULL;
314 goto done;
316 if (!is_git_repo(repo)) {
317 err = got_error(GOT_ERR_NOT_GIT_REPO);
318 goto done;
320 repo->path = strdup(worktree->repo_path);
321 if (repo->path == NULL) {
322 err = got_error_from_errno();
323 goto done;
325 } else {
326 repo->path = strdup(repo->path_git_dir);
327 if (repo->path == NULL) {
328 err = got_error_from_errno();
329 goto done;
332 done:
333 if (worktree)
334 got_worktree_close(worktree);
335 return err;
338 const struct got_error *
339 got_repo_open(struct got_repository **repop, const char *path)
341 struct got_repository *repo = NULL;
342 const struct got_error *err = NULL;
343 char *abspath, *normpath = NULL;
344 int i, tried_root = 0;
346 *repop = NULL;
348 if (got_path_is_absolute(path))
349 abspath = strdup(path);
350 else
351 abspath = got_path_get_absolute(path);
352 if (abspath == NULL)
353 return got_error(GOT_ERR_BAD_PATH);
355 repo = calloc(1, sizeof(*repo));
356 if (repo == NULL) {
357 err = got_error_from_errno();
358 goto done;
361 for (i = 0; i < nitems(repo->privsep_children); i++) {
362 memset(&repo->privsep_children[i], 0,
363 sizeof(repo->privsep_children[0]));
364 repo->privsep_children[i].imsg_fd = -1;
367 err = got_object_cache_init(&repo->objcache,
368 GOT_OBJECT_CACHE_TYPE_OBJ);
369 if (err)
370 goto done;
371 err = got_object_cache_init(&repo->treecache,
372 GOT_OBJECT_CACHE_TYPE_TREE);
373 if (err)
374 goto done;
375 err = got_object_cache_init(&repo->commitcache,
376 GOT_OBJECT_CACHE_TYPE_COMMIT);
377 if (err)
378 goto done;
379 err = got_object_cache_init(&repo->minicommitcache,
380 GOT_OBJECT_CACHE_TYPE_MINI_COMMIT);
381 if (err)
382 goto done;
384 normpath = got_path_normalize(abspath);
385 if (normpath == NULL) {
386 err = got_error(GOT_ERR_BAD_PATH);
387 goto done;
390 path = normpath;
391 do {
392 err = open_repo(repo, path);
393 if (err == NULL)
394 break;
395 if (err->code != GOT_ERR_NOT_GIT_REPO)
396 break;
397 if (path[0] == '/' && path[1] == '\0') {
398 if (tried_root) {
399 err = got_error(GOT_ERR_NOT_GIT_REPO);
400 break;
402 tried_root = 1;
404 path = dirname(path);
405 if (path == NULL)
406 err = got_error_from_errno();
407 } while (path);
408 done:
409 if (err)
410 got_repo_close(repo);
411 else
412 *repop = repo;
413 free(abspath);
414 free(normpath);
415 return err;
418 const struct got_error *
419 got_repo_close(struct got_repository *repo)
421 const struct got_error *err = NULL, *child_err;
422 int i;
424 for (i = 0; i < nitems(repo->packidx_cache); i++) {
425 if (repo->packidx_cache[i] == NULL)
426 break;
427 got_packidx_close(repo->packidx_cache[i]);
430 for (i = 0; i < nitems(repo->packs); i++) {
431 if (repo->packs[i].path_packfile == NULL)
432 break;
433 got_pack_close(&repo->packs[i]);
436 free(repo->path);
437 free(repo->path_git_dir);
439 got_object_cache_close(&repo->objcache);
440 got_object_cache_close(&repo->treecache);
441 got_object_cache_close(&repo->commitcache);
442 got_object_cache_close(&repo->minicommitcache);
444 for (i = 0; i < nitems(repo->privsep_children); i++) {
445 if (repo->privsep_children[i].imsg_fd == -1)
446 continue;
447 imsg_clear(repo->privsep_children[i].ibuf);
448 free(repo->privsep_children[i].ibuf);
449 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
450 child_err = got_privsep_wait_for_child(
451 repo->privsep_children[i].pid);
452 if (child_err && err == NULL)
453 err = child_err;
454 close(repo->privsep_children[i].imsg_fd);
456 free(repo);
458 return err;
461 const struct got_error *
462 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
463 const char *input_path, int check_disk)
465 const struct got_error *err = NULL;
466 char *repo_abspath = NULL, *cwd = NULL;
467 struct stat sb;
468 size_t repolen, cwdlen, len;
469 char *canonpath, *path = NULL;
471 *in_repo_path = NULL;
473 cwd = getcwd(NULL, 0);
474 if (cwd == NULL)
475 return got_error_from_errno();
477 canonpath = strdup(input_path);
478 if (canonpath == NULL) {
479 err = got_error_from_errno();
480 goto done;
482 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
483 if (err)
484 goto done;
486 repo_abspath = got_repo_get_path(repo);
487 if (repo_abspath == NULL) {
488 err = got_error_from_errno();
489 goto done;
492 /* TODO: Call "get in-repository path of work-tree node" API. */
494 if (!check_disk)
495 path = strdup(canonpath);
496 else if (lstat(canonpath, &sb) != 0) {
497 if (errno != ENOENT) {
498 err = got_error_from_errno();
499 goto done;
501 /*
502 * Path is not on disk.
503 * Assume it is already relative to repository root.
504 */
505 path = strdup(canonpath);
506 } else {
507 int is_repo_child = 0, is_cwd_child = 0;
509 path = realpath(canonpath, NULL);
510 if (path == NULL) {
511 err = got_error_from_errno();
512 goto done;
515 repolen = strlen(repo_abspath);
516 cwdlen = strlen(cwd);
517 len = strlen(path);
519 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
520 is_repo_child = 1;
521 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
522 is_cwd_child = 1;
524 if (strcmp(path, repo_abspath) == 0) {
525 free(path);
526 path = strdup("");
527 if (path == NULL) {
528 err = got_error_from_errno();
529 goto done;
531 } else if (is_repo_child && is_cwd_child) {
532 char *child;
533 /* TODO: Is path inside a got worktree? */
534 /* Strip common prefix with repository path. */
535 err = got_path_skip_common_ancestor(&child,
536 repo_abspath, path);
537 if (err)
538 goto done;
539 free(path);
540 path = child;
541 } else if (is_repo_child) {
542 /* Matched an on-disk path inside repository. */
543 if (got_repo_is_bare(repo)) {
544 /*
545 * Matched an on-disk path inside repository
546 * database. Treat as repository-relative.
547 */
548 } else {
549 char *child;
550 /* Strip common prefix with repository path. */
551 err = got_path_skip_common_ancestor(&child,
552 repo_abspath, path);
553 if (err)
554 goto done;
555 free(path);
556 path = child;
558 } else if (is_cwd_child) {
559 char *child;
560 /* TODO: Is path inside a got worktree? */
561 /* Strip common prefix with cwd. */
562 err = got_path_skip_common_ancestor(&child, cwd,
563 path);
564 if (err)
565 goto done;
566 free(path);
567 path = child;
568 } else {
569 /*
570 * Matched unrelated on-disk path.
571 * Treat it as repository-relative.
572 */
576 /* Make in-repository path absolute */
577 if (path[0] != '/') {
578 char *abspath;
579 if (asprintf(&abspath, "/%s", path) == -1) {
580 err = got_error_from_errno();
581 goto done;
583 free(path);
584 path = abspath;
587 done:
588 free(repo_abspath);
589 free(cwd);
590 free(canonpath);
591 if (err)
592 free(path);
593 else
594 *in_repo_path = path;
595 return err;
598 const struct got_error *
599 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
601 const struct got_error *err = NULL;
602 int i;
604 for (i = 0; i < nitems(repo->packidx_cache); i++) {
605 if (repo->packidx_cache[i] == NULL)
606 break;
609 if (i == nitems(repo->packidx_cache)) {
610 err = got_packidx_close(repo->packidx_cache[i - 1]);
611 if (err)
612 return err;
613 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
614 sizeof(repo->packidx_cache) -
615 sizeof(repo->packidx_cache[0]));
616 i = 0;
619 repo->packidx_cache[i] = packidx;
620 return NULL;
623 static int
624 is_packidx_filename(const char *name, size_t len)
626 if (len != GOT_PACKIDX_NAMELEN)
627 return 0;
629 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
630 return 0;
632 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
633 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
634 return 0;
636 return 1;
639 const struct got_error *
640 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
641 struct got_repository *repo, struct got_object_id *id)
643 const struct got_error *err;
644 char *path_packdir;
645 DIR *packdir;
646 struct dirent *dent;
647 char *path_packidx;
648 int i;
650 /* Search pack index cache. */
651 for (i = 0; i < nitems(repo->packidx_cache); i++) {
652 if (repo->packidx_cache[i] == NULL)
653 break;
654 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
655 if (*idx != -1) {
656 *packidx = repo->packidx_cache[i];
657 return NULL;
660 /* No luck. Search the filesystem. */
662 path_packdir = got_repo_get_path_objects_pack(repo);
663 if (path_packdir == NULL)
664 return got_error_from_errno();
666 packdir = opendir(path_packdir);
667 if (packdir == NULL) {
668 err = got_error_from_errno();
669 goto done;
672 while ((dent = readdir(packdir)) != NULL) {
673 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
674 continue;
676 if (asprintf(&path_packidx, "%s/%s", path_packdir,
677 dent->d_name) == -1) {
678 err = got_error_from_errno();
679 goto done;
682 err = got_packidx_open(packidx, path_packidx, 0);
683 free(path_packidx);
684 if (err)
685 goto done;
687 *idx = got_packidx_get_object_idx(*packidx, id);
688 if (*idx != -1) {
689 err = NULL; /* found the object */
690 err = got_repo_cache_packidx(repo, *packidx);
691 goto done;
694 err = got_packidx_close(*packidx);
695 *packidx = NULL;
696 if (err)
697 goto done;
700 err = got_error(GOT_ERR_NO_OBJ);
701 done:
702 free(path_packdir);
703 if (packdir && closedir(packdir) != 0 && err == 0)
704 err = got_error_from_errno();
705 return err;
708 static const struct got_error *
709 read_packfile_hdr(int fd, struct got_packidx *packidx)
711 const struct got_error *err = NULL;
712 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
713 struct got_packfile_hdr hdr;
714 ssize_t n;
716 n = read(fd, &hdr, sizeof(hdr));
717 if (n < 0)
718 return got_error_from_errno();
719 if (n != sizeof(hdr))
720 return got_error(GOT_ERR_BAD_PACKFILE);
722 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
723 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
724 betoh32(hdr.nobjects) != totobj)
725 err = got_error(GOT_ERR_BAD_PACKFILE);
727 return err;
730 static const struct got_error *
731 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
733 const struct got_error *err = NULL;
735 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
736 if (*fd == -1)
737 return got_error_from_errno();
739 if (packidx) {
740 err = read_packfile_hdr(*fd, packidx);
741 if (err) {
742 close(*fd);
743 *fd = -1;
747 return err;
750 const struct got_error *
751 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
752 const char *path_packfile, struct got_packidx *packidx)
754 const struct got_error *err = NULL;
755 struct got_pack *pack = NULL;
756 int i;
758 if (packp)
759 *packp = NULL;
761 for (i = 0; i < nitems(repo->packs); i++) {
762 pack = &repo->packs[i];
763 if (pack->path_packfile == NULL)
764 break;
765 if (strcmp(pack->path_packfile, path_packfile) == 0)
766 return NULL;
769 if (i == nitems(repo->packs) - 1) {
770 err = got_pack_close(&repo->packs[i - 1]);
771 if (err)
772 return err;
773 memmove(&repo->packs[1], &repo->packs[0],
774 sizeof(repo->packs) - sizeof(repo->packs[0]));
775 i = 0;
778 pack = &repo->packs[i];
780 pack->path_packfile = strdup(path_packfile);
781 if (pack->path_packfile == NULL) {
782 err = got_error_from_errno();
783 goto done;
786 err = open_packfile(&pack->fd, path_packfile, packidx);
787 if (err)
788 goto done;
790 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
791 if (err)
792 goto done;
794 pack->privsep_child = NULL;
796 #ifndef GOT_PACK_NO_MMAP
797 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
798 pack->fd, 0);
799 if (pack->map == MAP_FAILED)
800 pack->map = NULL; /* fall back to read(2) */
801 #endif
802 done:
803 if (err) {
804 if (pack) {
805 free(pack->path_packfile);
806 memset(pack, 0, sizeof(*pack));
808 } else if (packp)
809 *packp = pack;
810 return err;
813 struct got_pack *
814 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
816 struct got_pack *pack = NULL;
817 int i;
819 for (i = 0; i < nitems(repo->packs); i++) {
820 pack = &repo->packs[i];
821 if (pack->path_packfile == NULL)
822 break;
823 if (strcmp(pack->path_packfile, path_packfile) == 0)
824 return pack;
827 return NULL;