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_tag(struct got_repository *repo, struct got_object_id *id,
236 struct got_tag_object *tag)
238 #ifndef GOT_NO_OBJ_CACHE
239 const struct got_error *err = NULL;
240 err = got_object_cache_add(&repo->tagcache, id, tag);
241 if (err)
242 return err;
243 tag->refcnt++;
244 #endif
245 return NULL;
248 struct got_tag_object *
249 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
251 return (struct got_tag_object *)got_object_cache_get(
252 &repo->tagcache, id);
255 const struct got_error *
256 open_repo(struct got_repository *repo, const char *path)
258 const struct got_error *err = NULL;
259 struct got_worktree *worktree = NULL;
261 /* bare git repository? */
262 repo->path_git_dir = strdup(path);
263 if (repo->path_git_dir == NULL) {
264 err = got_error_from_errno();
265 goto done;
267 if (is_git_repo(repo)) {
268 repo->path = strdup(repo->path_git_dir);
269 if (repo->path == NULL) {
270 err = got_error_from_errno();
271 goto done;
273 return NULL;
276 /* git repository with working tree? */
277 free(repo->path_git_dir);
278 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
279 err = got_error_from_errno();
280 goto done;
282 if (is_git_repo(repo)) {
283 repo->path = strdup(path);
284 if (repo->path == NULL) {
285 err = got_error_from_errno();
286 goto done;
288 return NULL;
291 /* got work tree checked out from bare git repository? */
292 free(repo->path_git_dir);
293 repo->path_git_dir = NULL;
294 err = got_worktree_open(&worktree, path);
295 if (err) {
296 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
297 err = got_error(GOT_ERR_NOT_GIT_REPO);
298 goto done;
300 repo->path_git_dir = strdup(worktree->repo_path);
301 if (repo->path_git_dir == NULL) {
302 err = got_error_from_errno();
303 goto done;
306 /* got work tree checked out from git repository with working tree? */
307 if (!is_git_repo(repo)) {
308 free(repo->path_git_dir);
309 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
310 GOT_GIT_DIR) == -1) {
311 err = got_error_from_errno();
312 repo->path_git_dir = NULL;
313 goto done;
315 if (!is_git_repo(repo)) {
316 err = got_error(GOT_ERR_NOT_GIT_REPO);
317 goto done;
319 repo->path = strdup(worktree->repo_path);
320 if (repo->path == NULL) {
321 err = got_error_from_errno();
322 goto done;
324 } else {
325 repo->path = strdup(repo->path_git_dir);
326 if (repo->path == NULL) {
327 err = got_error_from_errno();
328 goto done;
331 done:
332 if (worktree)
333 got_worktree_close(worktree);
334 return err;
337 const struct got_error *
338 got_repo_open(struct got_repository **repop, const char *path)
340 struct got_repository *repo = NULL;
341 const struct got_error *err = NULL;
342 char *abspath, *normpath = NULL;
343 int i, tried_root = 0;
345 *repop = NULL;
347 if (got_path_is_absolute(path))
348 abspath = strdup(path);
349 else
350 abspath = got_path_get_absolute(path);
351 if (abspath == NULL)
352 return got_error(GOT_ERR_BAD_PATH);
354 repo = calloc(1, sizeof(*repo));
355 if (repo == NULL) {
356 err = got_error_from_errno();
357 goto done;
360 for (i = 0; i < nitems(repo->privsep_children); i++) {
361 memset(&repo->privsep_children[i], 0,
362 sizeof(repo->privsep_children[0]));
363 repo->privsep_children[i].imsg_fd = -1;
366 err = got_object_cache_init(&repo->objcache,
367 GOT_OBJECT_CACHE_TYPE_OBJ);
368 if (err)
369 goto done;
370 err = got_object_cache_init(&repo->treecache,
371 GOT_OBJECT_CACHE_TYPE_TREE);
372 if (err)
373 goto done;
374 err = got_object_cache_init(&repo->commitcache,
375 GOT_OBJECT_CACHE_TYPE_COMMIT);
376 if (err)
377 goto done;
378 err = got_object_cache_init(&repo->tagcache,
379 GOT_OBJECT_CACHE_TYPE_TAG);
380 if (err)
381 goto done;
383 normpath = got_path_normalize(abspath);
384 if (normpath == NULL) {
385 err = got_error(GOT_ERR_BAD_PATH);
386 goto done;
389 path = normpath;
390 do {
391 err = open_repo(repo, path);
392 if (err == NULL)
393 break;
394 if (err->code != GOT_ERR_NOT_GIT_REPO)
395 break;
396 if (path[0] == '/' && path[1] == '\0') {
397 if (tried_root) {
398 err = got_error(GOT_ERR_NOT_GIT_REPO);
399 break;
401 tried_root = 1;
403 path = dirname(path);
404 if (path == NULL)
405 err = got_error_from_errno();
406 } while (path);
407 done:
408 if (err)
409 got_repo_close(repo);
410 else
411 *repop = repo;
412 free(abspath);
413 free(normpath);
414 return err;
417 const struct got_error *
418 got_repo_close(struct got_repository *repo)
420 const struct got_error *err = NULL, *child_err;
421 int i;
423 for (i = 0; i < nitems(repo->packidx_cache); i++) {
424 if (repo->packidx_cache[i] == NULL)
425 break;
426 got_packidx_close(repo->packidx_cache[i]);
429 for (i = 0; i < nitems(repo->packs); i++) {
430 if (repo->packs[i].path_packfile == NULL)
431 break;
432 got_pack_close(&repo->packs[i]);
435 free(repo->path);
436 free(repo->path_git_dir);
438 got_object_cache_close(&repo->objcache);
439 got_object_cache_close(&repo->treecache);
440 got_object_cache_close(&repo->commitcache);
441 got_object_cache_close(&repo->tagcache);
443 for (i = 0; i < nitems(repo->privsep_children); i++) {
444 if (repo->privsep_children[i].imsg_fd == -1)
445 continue;
446 imsg_clear(repo->privsep_children[i].ibuf);
447 free(repo->privsep_children[i].ibuf);
448 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
449 child_err = got_privsep_wait_for_child(
450 repo->privsep_children[i].pid);
451 if (child_err && err == NULL)
452 err = child_err;
453 close(repo->privsep_children[i].imsg_fd);
455 free(repo);
457 return err;
460 const struct got_error *
461 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
462 const char *input_path, int check_disk)
464 const struct got_error *err = NULL;
465 char *repo_abspath = NULL, *cwd = NULL;
466 struct stat sb;
467 size_t repolen, cwdlen, len;
468 char *canonpath, *path = NULL;
470 *in_repo_path = NULL;
472 cwd = getcwd(NULL, 0);
473 if (cwd == NULL)
474 return got_error_from_errno();
476 canonpath = strdup(input_path);
477 if (canonpath == NULL) {
478 err = got_error_from_errno();
479 goto done;
481 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
482 if (err)
483 goto done;
485 repo_abspath = got_repo_get_path(repo);
486 if (repo_abspath == NULL) {
487 err = got_error_from_errno();
488 goto done;
491 /* TODO: Call "get in-repository path of work-tree node" API. */
493 if (!check_disk)
494 path = strdup(canonpath);
495 else if (lstat(canonpath, &sb) != 0) {
496 if (errno != ENOENT) {
497 err = got_error_from_errno();
498 goto done;
500 /*
501 * Path is not on disk.
502 * Assume it is already relative to repository root.
503 */
504 path = strdup(canonpath);
505 } else {
506 int is_repo_child = 0, is_cwd_child = 0;
508 path = realpath(canonpath, NULL);
509 if (path == NULL) {
510 err = got_error_from_errno();
511 goto done;
514 repolen = strlen(repo_abspath);
515 cwdlen = strlen(cwd);
516 len = strlen(path);
518 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
519 is_repo_child = 1;
520 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
521 is_cwd_child = 1;
523 if (strcmp(path, repo_abspath) == 0) {
524 free(path);
525 path = strdup("");
526 if (path == NULL) {
527 err = got_error_from_errno();
528 goto done;
530 } else if (is_repo_child && is_cwd_child) {
531 char *child;
532 /* TODO: Is path inside a got worktree? */
533 /* Strip common prefix with repository path. */
534 err = got_path_skip_common_ancestor(&child,
535 repo_abspath, path);
536 if (err)
537 goto done;
538 free(path);
539 path = child;
540 } else if (is_repo_child) {
541 /* Matched an on-disk path inside repository. */
542 if (got_repo_is_bare(repo)) {
543 /*
544 * Matched an on-disk path inside repository
545 * database. Treat as repository-relative.
546 */
547 } else {
548 char *child;
549 /* Strip common prefix with repository path. */
550 err = got_path_skip_common_ancestor(&child,
551 repo_abspath, path);
552 if (err)
553 goto done;
554 free(path);
555 path = child;
557 } else if (is_cwd_child) {
558 char *child;
559 /* TODO: Is path inside a got worktree? */
560 /* Strip common prefix with cwd. */
561 err = got_path_skip_common_ancestor(&child, cwd,
562 path);
563 if (err)
564 goto done;
565 free(path);
566 path = child;
567 } else {
568 /*
569 * Matched unrelated on-disk path.
570 * Treat it as repository-relative.
571 */
575 /* Make in-repository path absolute */
576 if (path[0] != '/') {
577 char *abspath;
578 if (asprintf(&abspath, "/%s", path) == -1) {
579 err = got_error_from_errno();
580 goto done;
582 free(path);
583 path = abspath;
586 done:
587 free(repo_abspath);
588 free(cwd);
589 free(canonpath);
590 if (err)
591 free(path);
592 else
593 *in_repo_path = path;
594 return err;
597 const struct got_error *
598 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
600 const struct got_error *err = NULL;
601 int i;
603 for (i = 0; i < nitems(repo->packidx_cache); i++) {
604 if (repo->packidx_cache[i] == NULL)
605 break;
607 if (i == nitems(repo->packidx_cache)) {
608 err = got_packidx_close(repo->packidx_cache[i - 1]);
609 if (err)
610 return err;
613 /*
614 * Insert the new pack index at the front so it will
615 * be searched first in the future.
616 */
617 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
618 sizeof(repo->packidx_cache) -
619 sizeof(repo->packidx_cache[0]));
620 repo->packidx_cache[0] = packidx;
622 return NULL;
625 static int
626 is_packidx_filename(const char *name, size_t len)
628 if (len != GOT_PACKIDX_NAMELEN)
629 return 0;
631 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
632 return 0;
634 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
635 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
636 return 0;
638 return 1;
641 const struct got_error *
642 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
643 struct got_repository *repo, struct got_object_id *id)
645 const struct got_error *err;
646 char *path_packdir;
647 DIR *packdir;
648 struct dirent *dent;
649 char *path_packidx;
650 int i;
652 /* Search pack index cache. */
653 for (i = 0; i < nitems(repo->packidx_cache); i++) {
654 if (repo->packidx_cache[i] == NULL)
655 break;
656 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
657 if (*idx != -1) {
658 *packidx = repo->packidx_cache[i];
659 return NULL;
662 /* No luck. Search the filesystem. */
664 path_packdir = got_repo_get_path_objects_pack(repo);
665 if (path_packdir == NULL)
666 return got_error_from_errno();
668 packdir = opendir(path_packdir);
669 if (packdir == NULL) {
670 err = got_error_from_errno();
671 goto done;
674 while ((dent = readdir(packdir)) != NULL) {
675 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
676 continue;
678 if (asprintf(&path_packidx, "%s/%s", path_packdir,
679 dent->d_name) == -1) {
680 err = got_error_from_errno();
681 goto done;
684 err = got_packidx_open(packidx, path_packidx, 0);
685 free(path_packidx);
686 if (err)
687 goto done;
689 *idx = got_packidx_get_object_idx(*packidx, id);
690 if (*idx != -1) {
691 err = NULL; /* found the object */
692 err = got_repo_cache_packidx(repo, *packidx);
693 goto done;
696 err = got_packidx_close(*packidx);
697 *packidx = NULL;
698 if (err)
699 goto done;
702 err = got_error_no_obj(id);
703 done:
704 free(path_packdir);
705 if (packdir && closedir(packdir) != 0 && err == 0)
706 err = got_error_from_errno();
707 return err;
710 static const struct got_error *
711 read_packfile_hdr(int fd, struct got_packidx *packidx)
713 const struct got_error *err = NULL;
714 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
715 struct got_packfile_hdr hdr;
716 ssize_t n;
718 n = read(fd, &hdr, sizeof(hdr));
719 if (n < 0)
720 return got_error_from_errno();
721 if (n != sizeof(hdr))
722 return got_error(GOT_ERR_BAD_PACKFILE);
724 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
725 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
726 betoh32(hdr.nobjects) != totobj)
727 err = got_error(GOT_ERR_BAD_PACKFILE);
729 return err;
732 static const struct got_error *
733 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
735 const struct got_error *err = NULL;
737 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
738 if (*fd == -1)
739 return got_error_from_errno();
741 if (packidx) {
742 err = read_packfile_hdr(*fd, packidx);
743 if (err) {
744 close(*fd);
745 *fd = -1;
749 return err;
752 const struct got_error *
753 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
754 const char *path_packfile, struct got_packidx *packidx)
756 const struct got_error *err = NULL;
757 struct got_pack *pack = NULL;
758 int i;
760 if (packp)
761 *packp = NULL;
763 for (i = 0; i < nitems(repo->packs); i++) {
764 pack = &repo->packs[i];
765 if (pack->path_packfile == NULL)
766 break;
767 if (strcmp(pack->path_packfile, path_packfile) == 0)
768 return NULL;
771 if (i == nitems(repo->packs) - 1) {
772 err = got_pack_close(&repo->packs[i - 1]);
773 if (err)
774 return err;
775 memmove(&repo->packs[1], &repo->packs[0],
776 sizeof(repo->packs) - sizeof(repo->packs[0]));
777 i = 0;
780 pack = &repo->packs[i];
782 pack->path_packfile = strdup(path_packfile);
783 if (pack->path_packfile == NULL) {
784 err = got_error_from_errno();
785 goto done;
788 err = open_packfile(&pack->fd, path_packfile, packidx);
789 if (err)
790 goto done;
792 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
793 if (err)
794 goto done;
796 pack->privsep_child = NULL;
798 #ifndef GOT_PACK_NO_MMAP
799 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
800 pack->fd, 0);
801 if (pack->map == MAP_FAILED)
802 pack->map = NULL; /* fall back to read(2) */
803 #endif
804 done:
805 if (err) {
806 if (pack) {
807 free(pack->path_packfile);
808 memset(pack, 0, sizeof(*pack));
810 } else if (packp)
811 *packp = pack;
812 return err;
815 struct got_pack *
816 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
818 struct got_pack *pack = NULL;
819 int i;
821 for (i = 0; i < nitems(repo->packs); i++) {
822 pack = &repo->packs[i];
823 if (pack->path_packfile == NULL)
824 break;
825 if (strcmp(pack->path_packfile, path_packfile) == 0)
826 return pack;
829 return NULL;