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 const char *
70 got_repo_get_path(struct got_repository *repo)
71 {
72 return repo->path;
73 }
75 const char *
76 got_repo_get_path_git_dir(struct got_repository *repo)
77 {
78 return 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 const 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_objects);
162 free(path_refs);
163 free(path_head);
164 return ret;
168 const struct got_error *
169 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
170 struct got_object *obj)
172 #ifndef GOT_NO_OBJ_CACHE
173 const struct got_error *err = NULL;
174 err = got_object_cache_add(&repo->objcache, id, obj);
175 if (err)
176 return err;
177 obj->refcnt++;
178 #endif
179 return NULL;
182 struct got_object *
183 got_repo_get_cached_object(struct got_repository *repo,
184 struct got_object_id *id)
186 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
189 const struct got_error *
190 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
191 struct got_tree_object *tree)
193 #ifndef GOT_NO_OBJ_CACHE
194 const struct got_error *err = NULL;
195 err = got_object_cache_add(&repo->treecache, id, tree);
196 if (err)
197 return err;
198 tree->refcnt++;
199 #endif
200 return NULL;
203 struct got_tree_object *
204 got_repo_get_cached_tree(struct got_repository *repo,
205 struct got_object_id *id)
207 return (struct got_tree_object *)got_object_cache_get(
208 &repo->treecache, id);
211 const struct got_error *
212 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
213 struct got_commit_object *commit)
215 #ifndef GOT_NO_OBJ_CACHE
216 const struct got_error *err = NULL;
217 err = got_object_cache_add(&repo->commitcache, id, commit);
218 if (err)
219 return err;
220 commit->refcnt++;
221 #endif
222 return NULL;
225 struct got_commit_object *
226 got_repo_get_cached_commit(struct got_repository *repo,
227 struct got_object_id *id)
229 return (struct got_commit_object *)got_object_cache_get(
230 &repo->commitcache, id);
233 const struct got_error *
234 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
235 struct got_tag_object *tag)
237 #ifndef GOT_NO_OBJ_CACHE
238 const struct got_error *err = NULL;
239 err = got_object_cache_add(&repo->tagcache, id, tag);
240 if (err)
241 return err;
242 tag->refcnt++;
243 #endif
244 return NULL;
247 struct got_tag_object *
248 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
250 return (struct got_tag_object *)got_object_cache_get(
251 &repo->tagcache, id);
254 const struct got_error *
255 open_repo(struct got_repository *repo, const char *path)
257 const struct got_error *err = NULL;
258 struct got_worktree *worktree = NULL;
260 /* bare git repository? */
261 repo->path_git_dir = strdup(path);
262 if (repo->path_git_dir == NULL) {
263 err = got_error_from_errno();
264 goto done;
266 if (is_git_repo(repo)) {
267 repo->path = strdup(repo->path_git_dir);
268 if (repo->path == NULL) {
269 err = got_error_from_errno();
270 goto done;
272 return NULL;
275 /* git repository with working tree? */
276 free(repo->path_git_dir);
277 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
278 err = got_error_from_errno();
279 goto done;
281 if (is_git_repo(repo)) {
282 repo->path = strdup(path);
283 if (repo->path == NULL) {
284 err = got_error_from_errno();
285 goto done;
287 return NULL;
290 /* got work tree checked out from bare git repository? */
291 free(repo->path_git_dir);
292 repo->path_git_dir = NULL;
293 err = got_worktree_open(&worktree, path);
294 if (err) {
295 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
296 err = got_error(GOT_ERR_NOT_GIT_REPO);
297 goto done;
299 repo->path_git_dir = strdup(worktree->repo_path);
300 if (repo->path_git_dir == NULL) {
301 err = got_error_from_errno();
302 goto done;
305 /* got work tree checked out from git repository with working tree? */
306 if (!is_git_repo(repo)) {
307 free(repo->path_git_dir);
308 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
309 GOT_GIT_DIR) == -1) {
310 err = got_error_from_errno();
311 repo->path_git_dir = NULL;
312 goto done;
314 if (!is_git_repo(repo)) {
315 err = got_error(GOT_ERR_NOT_GIT_REPO);
316 goto done;
318 repo->path = strdup(worktree->repo_path);
319 if (repo->path == NULL) {
320 err = got_error_from_errno();
321 goto done;
323 } else {
324 repo->path = strdup(repo->path_git_dir);
325 if (repo->path == NULL) {
326 err = got_error_from_errno();
327 goto done;
330 done:
331 if (worktree)
332 got_worktree_close(worktree);
333 return err;
336 const struct got_error *
337 got_repo_open(struct got_repository **repop, const char *path)
339 struct got_repository *repo = NULL;
340 const struct got_error *err = NULL;
341 char *abspath, *normpath = NULL;
342 int i, tried_root = 0;
344 *repop = NULL;
346 if (got_path_is_absolute(path))
347 abspath = strdup(path);
348 else
349 abspath = got_path_get_absolute(path);
350 if (abspath == NULL)
351 return got_error(GOT_ERR_BAD_PATH);
353 repo = calloc(1, sizeof(*repo));
354 if (repo == NULL) {
355 err = got_error_from_errno();
356 goto done;
359 for (i = 0; i < nitems(repo->privsep_children); i++) {
360 memset(&repo->privsep_children[i], 0,
361 sizeof(repo->privsep_children[0]));
362 repo->privsep_children[i].imsg_fd = -1;
365 err = got_object_cache_init(&repo->objcache,
366 GOT_OBJECT_CACHE_TYPE_OBJ);
367 if (err)
368 goto done;
369 err = got_object_cache_init(&repo->treecache,
370 GOT_OBJECT_CACHE_TYPE_TREE);
371 if (err)
372 goto done;
373 err = got_object_cache_init(&repo->commitcache,
374 GOT_OBJECT_CACHE_TYPE_COMMIT);
375 if (err)
376 goto done;
377 err = got_object_cache_init(&repo->tagcache,
378 GOT_OBJECT_CACHE_TYPE_TAG);
379 if (err)
380 goto done;
382 normpath = got_path_normalize(abspath);
383 if (normpath == NULL) {
384 err = got_error(GOT_ERR_BAD_PATH);
385 goto done;
388 path = normpath;
389 do {
390 err = open_repo(repo, path);
391 if (err == NULL)
392 break;
393 if (err->code != GOT_ERR_NOT_GIT_REPO)
394 break;
395 if (path[0] == '/' && path[1] == '\0') {
396 if (tried_root) {
397 err = got_error(GOT_ERR_NOT_GIT_REPO);
398 break;
400 tried_root = 1;
402 path = dirname(path);
403 if (path == NULL)
404 err = got_error_from_errno();
405 } while (path);
406 done:
407 if (err)
408 got_repo_close(repo);
409 else
410 *repop = repo;
411 free(abspath);
412 free(normpath);
413 return err;
416 const struct got_error *
417 got_repo_close(struct got_repository *repo)
419 const struct got_error *err = NULL, *child_err;
420 int i;
422 for (i = 0; i < nitems(repo->packidx_cache); i++) {
423 if (repo->packidx_cache[i] == NULL)
424 break;
425 got_packidx_close(repo->packidx_cache[i]);
428 for (i = 0; i < nitems(repo->packs); i++) {
429 if (repo->packs[i].path_packfile == NULL)
430 break;
431 got_pack_close(&repo->packs[i]);
434 free(repo->path);
435 free(repo->path_git_dir);
437 got_object_cache_close(&repo->objcache);
438 got_object_cache_close(&repo->treecache);
439 got_object_cache_close(&repo->commitcache);
440 got_object_cache_close(&repo->tagcache);
442 for (i = 0; i < nitems(repo->privsep_children); i++) {
443 if (repo->privsep_children[i].imsg_fd == -1)
444 continue;
445 imsg_clear(repo->privsep_children[i].ibuf);
446 free(repo->privsep_children[i].ibuf);
447 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
448 child_err = got_privsep_wait_for_child(
449 repo->privsep_children[i].pid);
450 if (child_err && err == NULL)
451 err = child_err;
452 close(repo->privsep_children[i].imsg_fd);
454 free(repo);
456 return err;
459 const struct got_error *
460 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
461 const char *input_path, int check_disk)
463 const struct got_error *err = NULL;
464 const char *repo_abspath = NULL;
465 struct stat sb;
466 size_t repolen, cwdlen, len;
467 char *cwd, *canonpath, *path = NULL;
469 *in_repo_path = NULL;
471 cwd = getcwd(NULL, 0);
472 if (cwd == NULL)
473 return got_error_from_errno();
475 canonpath = strdup(input_path);
476 if (canonpath == NULL) {
477 err = got_error_from_errno();
478 goto done;
480 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
481 if (err)
482 goto done;
484 repo_abspath = got_repo_get_path(repo);
486 /* TODO: Call "get in-repository path of work-tree node" API. */
488 if (!check_disk)
489 path = strdup(canonpath);
490 else if (lstat(canonpath, &sb) != 0) {
491 if (errno != ENOENT) {
492 err = got_error_from_errno();
493 goto done;
495 /*
496 * Path is not on disk.
497 * Assume it is already relative to repository root.
498 */
499 path = strdup(canonpath);
500 } else {
501 int is_repo_child = 0, is_cwd_child = 0;
503 path = realpath(canonpath, NULL);
504 if (path == NULL) {
505 err = got_error_from_errno();
506 goto done;
509 repolen = strlen(repo_abspath);
510 cwdlen = strlen(cwd);
511 len = strlen(path);
513 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
514 is_repo_child = 1;
515 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
516 is_cwd_child = 1;
518 if (strcmp(path, repo_abspath) == 0) {
519 free(path);
520 path = strdup("");
521 if (path == NULL) {
522 err = got_error_from_errno();
523 goto done;
525 } else if (is_repo_child && is_cwd_child) {
526 char *child;
527 /* TODO: Is path inside a got worktree? */
528 /* Strip common prefix with repository path. */
529 err = got_path_skip_common_ancestor(&child,
530 repo_abspath, path);
531 if (err)
532 goto done;
533 free(path);
534 path = child;
535 } else if (is_repo_child) {
536 /* Matched an on-disk path inside repository. */
537 if (got_repo_is_bare(repo)) {
538 /*
539 * Matched an on-disk path inside repository
540 * database. Treat as repository-relative.
541 */
542 } else {
543 char *child;
544 /* Strip common prefix with repository path. */
545 err = got_path_skip_common_ancestor(&child,
546 repo_abspath, path);
547 if (err)
548 goto done;
549 free(path);
550 path = child;
552 } else if (is_cwd_child) {
553 char *child;
554 /* TODO: Is path inside a got worktree? */
555 /* Strip common prefix with cwd. */
556 err = got_path_skip_common_ancestor(&child, cwd,
557 path);
558 if (err)
559 goto done;
560 free(path);
561 path = child;
562 } else {
563 /*
564 * Matched unrelated on-disk path.
565 * Treat it as repository-relative.
566 */
570 /* Make in-repository path absolute */
571 if (path[0] != '/') {
572 char *abspath;
573 if (asprintf(&abspath, "/%s", path) == -1) {
574 err = got_error_from_errno();
575 goto done;
577 free(path);
578 path = abspath;
581 done:
582 free(cwd);
583 free(canonpath);
584 if (err)
585 free(path);
586 else
587 *in_repo_path = path;
588 return err;
591 const struct got_error *
592 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
594 const struct got_error *err = NULL;
595 int i;
597 for (i = 0; i < nitems(repo->packidx_cache); i++) {
598 if (repo->packidx_cache[i] == NULL)
599 break;
601 if (i == nitems(repo->packidx_cache)) {
602 err = got_packidx_close(repo->packidx_cache[i - 1]);
603 if (err)
604 return err;
607 /*
608 * Insert the new pack index at the front so it will
609 * be searched first in the future.
610 */
611 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
612 sizeof(repo->packidx_cache) -
613 sizeof(repo->packidx_cache[0]));
614 repo->packidx_cache[0] = packidx;
616 return NULL;
619 static int
620 is_packidx_filename(const char *name, size_t len)
622 if (len != GOT_PACKIDX_NAMELEN)
623 return 0;
625 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
626 return 0;
628 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
629 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
630 return 0;
632 return 1;
635 const struct got_error *
636 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
637 struct got_repository *repo, struct got_object_id *id)
639 const struct got_error *err;
640 char *path_packdir;
641 DIR *packdir;
642 struct dirent *dent;
643 char *path_packidx;
644 int i;
646 /* Search pack index cache. */
647 for (i = 0; i < nitems(repo->packidx_cache); i++) {
648 if (repo->packidx_cache[i] == NULL)
649 break;
650 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
651 if (*idx != -1) {
652 *packidx = repo->packidx_cache[i];
653 return NULL;
656 /* No luck. Search the filesystem. */
658 path_packdir = got_repo_get_path_objects_pack(repo);
659 if (path_packdir == NULL)
660 return got_error_from_errno();
662 packdir = opendir(path_packdir);
663 if (packdir == NULL) {
664 err = got_error_from_errno();
665 goto done;
668 while ((dent = readdir(packdir)) != NULL) {
669 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
670 continue;
672 if (asprintf(&path_packidx, "%s/%s", path_packdir,
673 dent->d_name) == -1) {
674 err = got_error_from_errno();
675 goto done;
678 err = got_packidx_open(packidx, path_packidx, 0);
679 free(path_packidx);
680 if (err)
681 goto done;
683 *idx = got_packidx_get_object_idx(*packidx, id);
684 if (*idx != -1) {
685 err = NULL; /* found the object */
686 err = got_repo_cache_packidx(repo, *packidx);
687 goto done;
690 err = got_packidx_close(*packidx);
691 *packidx = NULL;
692 if (err)
693 goto done;
696 err = got_error_no_obj(id);
697 done:
698 free(path_packdir);
699 if (packdir && closedir(packdir) != 0 && err == 0)
700 err = got_error_from_errno();
701 return err;
704 static const struct got_error *
705 read_packfile_hdr(int fd, struct got_packidx *packidx)
707 const struct got_error *err = NULL;
708 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
709 struct got_packfile_hdr hdr;
710 ssize_t n;
712 n = read(fd, &hdr, sizeof(hdr));
713 if (n < 0)
714 return got_error_from_errno();
715 if (n != sizeof(hdr))
716 return got_error(GOT_ERR_BAD_PACKFILE);
718 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
719 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
720 betoh32(hdr.nobjects) != totobj)
721 err = got_error(GOT_ERR_BAD_PACKFILE);
723 return err;
726 static const struct got_error *
727 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
731 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
732 if (*fd == -1)
733 return got_error_from_errno();
735 if (packidx) {
736 err = read_packfile_hdr(*fd, packidx);
737 if (err) {
738 close(*fd);
739 *fd = -1;
743 return err;
746 const struct got_error *
747 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
748 const char *path_packfile, struct got_packidx *packidx)
750 const struct got_error *err = NULL;
751 struct got_pack *pack = NULL;
752 int i;
754 if (packp)
755 *packp = NULL;
757 for (i = 0; i < nitems(repo->packs); i++) {
758 pack = &repo->packs[i];
759 if (pack->path_packfile == NULL)
760 break;
761 if (strcmp(pack->path_packfile, path_packfile) == 0)
762 return NULL;
765 if (i == nitems(repo->packs) - 1) {
766 err = got_pack_close(&repo->packs[i - 1]);
767 if (err)
768 return err;
769 memmove(&repo->packs[1], &repo->packs[0],
770 sizeof(repo->packs) - sizeof(repo->packs[0]));
771 i = 0;
774 pack = &repo->packs[i];
776 pack->path_packfile = strdup(path_packfile);
777 if (pack->path_packfile == NULL) {
778 err = got_error_from_errno();
779 goto done;
782 err = open_packfile(&pack->fd, path_packfile, packidx);
783 if (err)
784 goto done;
786 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
787 if (err)
788 goto done;
790 pack->privsep_child = NULL;
792 #ifndef GOT_PACK_NO_MMAP
793 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
794 pack->fd, 0);
795 if (pack->map == MAP_FAILED)
796 pack->map = NULL; /* fall back to read(2) */
797 #endif
798 done:
799 if (err) {
800 if (pack) {
801 free(pack->path_packfile);
802 memset(pack, 0, sizeof(*pack));
804 } else if (packp)
805 *packp = pack;
806 return err;
809 struct got_pack *
810 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
812 struct got_pack *pack = NULL;
813 int i;
815 for (i = 0; i < nitems(repo->packs); i++) {
816 pack = &repo->packs[i];
817 if (pack->path_packfile == NULL)
818 break;
819 if (strcmp(pack->path_packfile, path_packfile) == 0)
820 return pack;
823 return NULL;