Blob


1 /*
2 * Copyright (c) 2018, 2019 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"
68 #define GOT_PACKED_REFS_FILE "packed-refs"
70 const char *
71 got_repo_get_path(struct got_repository *repo)
72 {
73 return repo->path;
74 }
76 const char *
77 got_repo_get_path_git_dir(struct got_repository *repo)
78 {
79 return repo->path_git_dir;
80 }
82 int
83 got_repo_is_bare(struct got_repository *repo)
84 {
85 return (strcmp(repo->path, repo->path_git_dir) == 0);
86 }
88 static char *
89 get_path_git_child(struct got_repository *repo, const char *basename)
90 {
91 char *path_child;
93 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
94 basename) == -1)
95 return NULL;
97 return path_child;
98 }
100 char *
101 got_repo_get_path_objects(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_OBJECTS_DIR);
106 char *
107 got_repo_get_path_objects_pack(struct got_repository *repo)
109 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
112 char *
113 got_repo_get_path_refs(struct got_repository *repo)
115 return get_path_git_child(repo, GOT_REFS_DIR);
118 char *
119 got_repo_get_path_packed_refs(struct got_repository *repo)
121 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
124 static char *
125 get_path_head(struct got_repository *repo)
127 return get_path_git_child(repo, GOT_HEAD_FILE);
130 static int
131 is_git_repo(struct got_repository *repo)
133 const char *path_git = got_repo_get_path_git_dir(repo);
134 char *path_objects = got_repo_get_path_objects(repo);
135 char *path_refs = got_repo_get_path_refs(repo);
136 char *path_head = get_path_head(repo);
137 int ret = 0;
138 struct stat sb;
139 struct got_reference *head_ref;
141 if (lstat(path_git, &sb) == -1)
142 goto done;
143 if (!S_ISDIR(sb.st_mode))
144 goto done;
146 if (lstat(path_objects, &sb) == -1)
147 goto done;
148 if (!S_ISDIR(sb.st_mode))
149 goto done;
151 if (lstat(path_refs, &sb) == -1)
152 goto done;
153 if (!S_ISDIR(sb.st_mode))
154 goto done;
156 if (lstat(path_head, &sb) == -1)
157 goto done;
158 if (!S_ISREG(sb.st_mode))
159 goto done;
161 /* Check if the HEAD reference can be opened. */
162 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
163 goto done;
164 got_ref_close(head_ref);
166 ret = 1;
167 done:
168 free(path_objects);
169 free(path_refs);
170 free(path_head);
171 return ret;
175 const struct got_error *
176 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
177 struct got_object *obj)
179 #ifndef GOT_NO_OBJ_CACHE
180 const struct got_error *err = NULL;
181 err = got_object_cache_add(&repo->objcache, id, obj);
182 if (err)
183 return err;
184 obj->refcnt++;
185 #endif
186 return NULL;
189 struct got_object *
190 got_repo_get_cached_object(struct got_repository *repo,
191 struct got_object_id *id)
193 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
196 const struct got_error *
197 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
198 struct got_tree_object *tree)
200 #ifndef GOT_NO_OBJ_CACHE
201 const struct got_error *err = NULL;
202 err = got_object_cache_add(&repo->treecache, id, tree);
203 if (err)
204 return err;
205 tree->refcnt++;
206 #endif
207 return NULL;
210 struct got_tree_object *
211 got_repo_get_cached_tree(struct got_repository *repo,
212 struct got_object_id *id)
214 return (struct got_tree_object *)got_object_cache_get(
215 &repo->treecache, id);
218 const struct got_error *
219 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
220 struct got_commit_object *commit)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->commitcache, id, commit);
225 if (err)
226 return err;
227 commit->refcnt++;
228 #endif
229 return NULL;
232 struct got_commit_object *
233 got_repo_get_cached_commit(struct got_repository *repo,
234 struct got_object_id *id)
236 return (struct got_commit_object *)got_object_cache_get(
237 &repo->commitcache, id);
240 const struct got_error *
241 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
242 struct got_tag_object *tag)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->tagcache, id, tag);
247 if (err)
248 return err;
249 tag->refcnt++;
250 #endif
251 return NULL;
254 struct got_tag_object *
255 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
257 return (struct got_tag_object *)got_object_cache_get(
258 &repo->tagcache, id);
261 const struct got_error *
262 open_repo(struct got_repository *repo, const char *path)
264 const struct got_error *err = NULL;
265 struct got_worktree *worktree = NULL;
267 /* bare git repository? */
268 repo->path_git_dir = strdup(path);
269 if (repo->path_git_dir == NULL) {
270 err = got_error_from_errno();
271 goto done;
273 if (is_git_repo(repo)) {
274 repo->path = strdup(repo->path_git_dir);
275 if (repo->path == NULL) {
276 err = got_error_from_errno();
277 goto done;
279 return NULL;
282 /* git repository with working tree? */
283 free(repo->path_git_dir);
284 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
285 err = got_error_from_errno();
286 goto done;
288 if (is_git_repo(repo)) {
289 repo->path = strdup(path);
290 if (repo->path == NULL) {
291 err = got_error_from_errno();
292 goto done;
294 return NULL;
297 /* got work tree checked out from bare git repository? */
298 free(repo->path_git_dir);
299 repo->path_git_dir = NULL;
300 err = got_worktree_open(&worktree, path);
301 if (err) {
302 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
303 err = got_error(GOT_ERR_NOT_GIT_REPO);
304 goto done;
306 repo->path_git_dir = strdup(worktree->repo_path);
307 if (repo->path_git_dir == NULL) {
308 err = got_error_from_errno();
309 goto done;
312 /* got work tree checked out from git repository with working tree? */
313 if (!is_git_repo(repo)) {
314 free(repo->path_git_dir);
315 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
316 GOT_GIT_DIR) == -1) {
317 err = got_error_from_errno();
318 repo->path_git_dir = NULL;
319 goto done;
321 if (!is_git_repo(repo)) {
322 err = got_error(GOT_ERR_NOT_GIT_REPO);
323 goto done;
325 repo->path = strdup(worktree->repo_path);
326 if (repo->path == NULL) {
327 err = got_error_from_errno();
328 goto done;
330 } else {
331 repo->path = strdup(repo->path_git_dir);
332 if (repo->path == NULL) {
333 err = got_error_from_errno();
334 goto done;
337 done:
338 if (worktree)
339 got_worktree_close(worktree);
340 return err;
343 const struct got_error *
344 got_repo_open(struct got_repository **repop, const char *path)
346 struct got_repository *repo = NULL;
347 const struct got_error *err = NULL;
348 char *abspath, *normpath = NULL;
349 int i, tried_root = 0;
351 *repop = NULL;
353 if (got_path_is_absolute(path))
354 abspath = strdup(path);
355 else
356 abspath = got_path_get_absolute(path);
357 if (abspath == NULL)
358 return got_error(GOT_ERR_BAD_PATH);
360 repo = calloc(1, sizeof(*repo));
361 if (repo == NULL) {
362 err = got_error_from_errno();
363 goto done;
366 for (i = 0; i < nitems(repo->privsep_children); i++) {
367 memset(&repo->privsep_children[i], 0,
368 sizeof(repo->privsep_children[0]));
369 repo->privsep_children[i].imsg_fd = -1;
372 err = got_object_cache_init(&repo->objcache,
373 GOT_OBJECT_CACHE_TYPE_OBJ);
374 if (err)
375 goto done;
376 err = got_object_cache_init(&repo->treecache,
377 GOT_OBJECT_CACHE_TYPE_TREE);
378 if (err)
379 goto done;
380 err = got_object_cache_init(&repo->commitcache,
381 GOT_OBJECT_CACHE_TYPE_COMMIT);
382 if (err)
383 goto done;
384 err = got_object_cache_init(&repo->tagcache,
385 GOT_OBJECT_CACHE_TYPE_TAG);
386 if (err)
387 goto done;
389 normpath = got_path_normalize(abspath);
390 if (normpath == NULL) {
391 err = got_error(GOT_ERR_BAD_PATH);
392 goto done;
395 path = normpath;
396 do {
397 err = open_repo(repo, path);
398 if (err == NULL)
399 break;
400 if (err->code != GOT_ERR_NOT_GIT_REPO)
401 break;
402 if (path[0] == '/' && path[1] == '\0') {
403 if (tried_root) {
404 err = got_error(GOT_ERR_NOT_GIT_REPO);
405 break;
407 tried_root = 1;
409 path = dirname(path);
410 if (path == NULL)
411 err = got_error_from_errno();
412 } while (path);
413 done:
414 if (err)
415 got_repo_close(repo);
416 else
417 *repop = repo;
418 free(abspath);
419 free(normpath);
420 return err;
423 const struct got_error *
424 got_repo_close(struct got_repository *repo)
426 const struct got_error *err = NULL, *child_err;
427 int i;
429 for (i = 0; i < nitems(repo->packidx_cache); i++) {
430 if (repo->packidx_cache[i] == NULL)
431 break;
432 got_packidx_close(repo->packidx_cache[i]);
435 for (i = 0; i < nitems(repo->packs); i++) {
436 if (repo->packs[i].path_packfile == NULL)
437 break;
438 got_pack_close(&repo->packs[i]);
441 free(repo->path);
442 free(repo->path_git_dir);
444 got_object_cache_close(&repo->objcache);
445 got_object_cache_close(&repo->treecache);
446 got_object_cache_close(&repo->commitcache);
447 got_object_cache_close(&repo->tagcache);
449 for (i = 0; i < nitems(repo->privsep_children); i++) {
450 if (repo->privsep_children[i].imsg_fd == -1)
451 continue;
452 imsg_clear(repo->privsep_children[i].ibuf);
453 free(repo->privsep_children[i].ibuf);
454 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
455 child_err = got_privsep_wait_for_child(
456 repo->privsep_children[i].pid);
457 if (child_err && err == NULL)
458 err = child_err;
459 close(repo->privsep_children[i].imsg_fd);
461 free(repo);
463 return err;
466 const struct got_error *
467 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
468 const char *input_path, int check_disk)
470 const struct got_error *err = NULL;
471 const char *repo_abspath = NULL;
472 struct stat sb;
473 size_t repolen, cwdlen, len;
474 char *cwd, *canonpath, *path = NULL;
476 *in_repo_path = NULL;
478 cwd = getcwd(NULL, 0);
479 if (cwd == NULL)
480 return got_error_from_errno();
482 canonpath = strdup(input_path);
483 if (canonpath == NULL) {
484 err = got_error_from_errno();
485 goto done;
487 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
488 if (err)
489 goto done;
491 repo_abspath = got_repo_get_path(repo);
493 /* TODO: Call "get in-repository path of work-tree node" API. */
495 if (!check_disk)
496 path = strdup(canonpath);
497 else if (lstat(canonpath, &sb) != 0) {
498 if (errno != ENOENT) {
499 err = got_error_from_errno();
500 goto done;
502 /*
503 * Path is not on disk.
504 * Assume it is already relative to repository root.
505 */
506 path = strdup(canonpath);
507 } else {
508 int is_repo_child = 0, is_cwd_child = 0;
510 path = realpath(canonpath, NULL);
511 if (path == NULL) {
512 err = got_error_from_errno();
513 goto done;
516 repolen = strlen(repo_abspath);
517 cwdlen = strlen(cwd);
518 len = strlen(path);
520 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
521 is_repo_child = 1;
522 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
523 is_cwd_child = 1;
525 if (strcmp(path, repo_abspath) == 0) {
526 free(path);
527 path = strdup("");
528 if (path == NULL) {
529 err = got_error_from_errno();
530 goto done;
532 } else if (is_repo_child && is_cwd_child) {
533 char *child;
534 /* TODO: Is path inside a got worktree? */
535 /* Strip common prefix with repository path. */
536 err = got_path_skip_common_ancestor(&child,
537 repo_abspath, path);
538 if (err)
539 goto done;
540 free(path);
541 path = child;
542 } else if (is_repo_child) {
543 /* Matched an on-disk path inside repository. */
544 if (got_repo_is_bare(repo)) {
545 /*
546 * Matched an on-disk path inside repository
547 * database. Treat as repository-relative.
548 */
549 } else {
550 char *child;
551 /* Strip common prefix with repository path. */
552 err = got_path_skip_common_ancestor(&child,
553 repo_abspath, path);
554 if (err)
555 goto done;
556 free(path);
557 path = child;
559 } else if (is_cwd_child) {
560 char *child;
561 /* TODO: Is path inside a got worktree? */
562 /* Strip common prefix with cwd. */
563 err = got_path_skip_common_ancestor(&child, cwd,
564 path);
565 if (err)
566 goto done;
567 free(path);
568 path = child;
569 } else {
570 /*
571 * Matched unrelated on-disk path.
572 * Treat it as repository-relative.
573 */
577 /* Make in-repository path absolute */
578 if (path[0] != '/') {
579 char *abspath;
580 if (asprintf(&abspath, "/%s", path) == -1) {
581 err = got_error_from_errno();
582 goto done;
584 free(path);
585 path = abspath;
588 done:
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;
608 if (i == nitems(repo->packidx_cache)) {
609 err = got_packidx_close(repo->packidx_cache[i - 1]);
610 if (err)
611 return err;
614 /*
615 * Insert the new pack index at the front so it will
616 * be searched first in the future.
617 */
618 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
619 sizeof(repo->packidx_cache) -
620 sizeof(repo->packidx_cache[0]));
621 repo->packidx_cache[0] = packidx;
623 return NULL;
626 static int
627 is_packidx_filename(const char *name, size_t len)
629 if (len != GOT_PACKIDX_NAMELEN)
630 return 0;
632 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
633 return 0;
635 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
636 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
637 return 0;
639 return 1;
642 const struct got_error *
643 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
644 struct got_repository *repo, struct got_object_id *id)
646 const struct got_error *err;
647 char *path_packdir;
648 DIR *packdir;
649 struct dirent *dent;
650 char *path_packidx;
651 int i;
653 /* Search pack index cache. */
654 for (i = 0; i < nitems(repo->packidx_cache); i++) {
655 if (repo->packidx_cache[i] == NULL)
656 break;
657 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
658 if (*idx != -1) {
659 *packidx = repo->packidx_cache[i];
660 return NULL;
663 /* No luck. Search the filesystem. */
665 path_packdir = got_repo_get_path_objects_pack(repo);
666 if (path_packdir == NULL)
667 return got_error_from_errno();
669 packdir = opendir(path_packdir);
670 if (packdir == NULL) {
671 err = got_error_from_errno();
672 goto done;
675 while ((dent = readdir(packdir)) != NULL) {
676 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
677 continue;
679 if (asprintf(&path_packidx, "%s/%s", path_packdir,
680 dent->d_name) == -1) {
681 err = got_error_from_errno();
682 goto done;
685 err = got_packidx_open(packidx, path_packidx, 0);
686 free(path_packidx);
687 if (err)
688 goto done;
690 *idx = got_packidx_get_object_idx(*packidx, id);
691 if (*idx != -1) {
692 err = NULL; /* found the object */
693 err = got_repo_cache_packidx(repo, *packidx);
694 goto done;
697 err = got_packidx_close(*packidx);
698 *packidx = NULL;
699 if (err)
700 goto done;
703 err = got_error_no_obj(id);
704 done:
705 free(path_packdir);
706 if (packdir && closedir(packdir) != 0 && err == 0)
707 err = got_error_from_errno();
708 return err;
711 static const struct got_error *
712 read_packfile_hdr(int fd, struct got_packidx *packidx)
714 const struct got_error *err = NULL;
715 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
716 struct got_packfile_hdr hdr;
717 ssize_t n;
719 n = read(fd, &hdr, sizeof(hdr));
720 if (n < 0)
721 return got_error_from_errno();
722 if (n != sizeof(hdr))
723 return got_error(GOT_ERR_BAD_PACKFILE);
725 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
726 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
727 betoh32(hdr.nobjects) != totobj)
728 err = got_error(GOT_ERR_BAD_PACKFILE);
730 return err;
733 static const struct got_error *
734 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
736 const struct got_error *err = NULL;
738 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
739 if (*fd == -1)
740 return got_error_from_errno();
742 if (packidx) {
743 err = read_packfile_hdr(*fd, packidx);
744 if (err) {
745 close(*fd);
746 *fd = -1;
750 return err;
753 const struct got_error *
754 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
755 const char *path_packfile, struct got_packidx *packidx)
757 const struct got_error *err = NULL;
758 struct got_pack *pack = NULL;
759 int i;
761 if (packp)
762 *packp = NULL;
764 for (i = 0; i < nitems(repo->packs); i++) {
765 pack = &repo->packs[i];
766 if (pack->path_packfile == NULL)
767 break;
768 if (strcmp(pack->path_packfile, path_packfile) == 0)
769 return NULL;
772 if (i == nitems(repo->packs) - 1) {
773 err = got_pack_close(&repo->packs[i - 1]);
774 if (err)
775 return err;
776 memmove(&repo->packs[1], &repo->packs[0],
777 sizeof(repo->packs) - sizeof(repo->packs[0]));
778 i = 0;
781 pack = &repo->packs[i];
783 pack->path_packfile = strdup(path_packfile);
784 if (pack->path_packfile == NULL) {
785 err = got_error_from_errno();
786 goto done;
789 err = open_packfile(&pack->fd, path_packfile, packidx);
790 if (err)
791 goto done;
793 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
794 if (err)
795 goto done;
797 pack->privsep_child = NULL;
799 #ifndef GOT_PACK_NO_MMAP
800 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
801 pack->fd, 0);
802 if (pack->map == MAP_FAILED)
803 pack->map = NULL; /* fall back to read(2) */
804 #endif
805 done:
806 if (err) {
807 if (pack) {
808 free(pack->path_packfile);
809 memset(pack, 0, sizeof(*pack));
811 } else if (packp)
812 *packp = pack;
813 return err;
816 struct got_pack *
817 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
819 struct got_pack *pack = NULL;
820 int i;
822 for (i = 0; i < nitems(repo->packs); i++) {
823 pack = &repo->packs[i];
824 if (pack->path_packfile == NULL)
825 break;
826 if (strcmp(pack->path_packfile, path_packfile) == 0)
827 return pack;
830 return NULL;