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 err = 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)
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;
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 (lstat(canonpath, &sb) != 0) {
468 if (errno != ENOENT) {
469 err = got_error_from_errno();
470 goto done;
472 /*
473 * Path is not on disk.
474 * Assume it is already relative to repository root.
475 */
476 path = strdup(canonpath);
477 } else {
478 int is_repo_child = 0, is_cwd_child = 0;
480 path = realpath(canonpath, NULL);
481 if (path == NULL) {
482 err = got_error_from_errno();
483 goto done;
486 repolen = strlen(repo_abspath);
487 cwdlen = strlen(cwd);
488 len = strlen(path);
490 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
491 is_repo_child = 1;
492 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
493 is_cwd_child = 1;
495 if (strcmp(path, repo_abspath) == 0) {
496 free(path);
497 path = strdup("");
498 if (path == NULL) {
499 err = got_error_from_errno();
500 goto done;
502 } else if (is_repo_child && is_cwd_child) {
503 char *child;
504 /* TODO: Is path inside a got worktree? */
505 /* Strip common prefix with repository path. */
506 err = got_path_skip_common_ancestor(&child,
507 repo_abspath, path);
508 if (err)
509 goto done;
510 free(path);
511 path = child;
512 } else if (is_repo_child) {
513 /* Matched an on-disk path inside repository. */
514 if (got_repo_is_bare(repo)) {
515 /*
516 * Matched an on-disk path inside repository
517 * database. Treat as repository-relative.
518 */
519 } else {
520 char *child;
521 /* Strip common prefix with repository path. */
522 err = got_path_skip_common_ancestor(&child,
523 repo_abspath, path);
524 if (err)
525 goto done;
526 free(path);
527 path = child;
529 } else if (is_cwd_child) {
530 char *child;
531 /* TODO: Is path inside a got worktree? */
532 /* Strip common prefix with cwd. */
533 err = got_path_skip_common_ancestor(&child, cwd,
534 path);
535 if (err)
536 goto done;
537 free(path);
538 path = child;
539 } else {
540 /*
541 * Matched unrelated on-disk path.
542 * Treat it as repository-relative.
543 */
547 /* Make in-repository path absolute */
548 if (path[0] != '/') {
549 char *abspath;
550 if (asprintf(&abspath, "/%s", path) == -1) {
551 err = got_error_from_errno();
552 goto done;
554 free(path);
555 path = abspath;
558 done:
559 free(repo_abspath);
560 free(cwd);
561 free(canonpath);
562 if (err)
563 free(path);
564 else
565 *in_repo_path = path;
566 return err;
569 const struct got_error *
570 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
572 const struct got_error *err = NULL;
573 int i;
575 for (i = 0; i < nitems(repo->packidx_cache); i++) {
576 if (repo->packidx_cache[i] == NULL)
577 break;
580 if (i == nitems(repo->packidx_cache)) {
581 err = got_packidx_close(repo->packidx_cache[i - 1]);
582 if (err)
583 return err;
584 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
585 sizeof(repo->packidx_cache) -
586 sizeof(repo->packidx_cache[0]));
587 i = 0;
590 repo->packidx_cache[i] = packidx;
591 return NULL;
594 static int
595 is_packidx_filename(const char *name, size_t len)
597 if (len != GOT_PACKIDX_NAMELEN)
598 return 0;
600 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
601 return 0;
603 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
604 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
605 return 0;
607 return 1;
610 const struct got_error *
611 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
612 struct got_repository *repo, struct got_object_id *id)
614 const struct got_error *err;
615 char *path_packdir;
616 DIR *packdir;
617 struct dirent *dent;
618 char *path_packidx;
619 int i;
621 /* Search pack index cache. */
622 for (i = 0; i < nitems(repo->packidx_cache); i++) {
623 if (repo->packidx_cache[i] == NULL)
624 break;
625 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
626 if (*idx != -1) {
627 *packidx = repo->packidx_cache[i];
628 return NULL;
631 /* No luck. Search the filesystem. */
633 path_packdir = got_repo_get_path_objects_pack(repo);
634 if (path_packdir == NULL)
635 return got_error_from_errno();
637 packdir = opendir(path_packdir);
638 if (packdir == NULL) {
639 err = got_error_from_errno();
640 goto done;
643 while ((dent = readdir(packdir)) != NULL) {
644 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
645 continue;
647 if (asprintf(&path_packidx, "%s/%s", path_packdir,
648 dent->d_name) == -1) {
649 err = got_error_from_errno();
650 goto done;
653 err = got_packidx_open(packidx, path_packidx, 0);
654 free(path_packidx);
655 if (err)
656 goto done;
658 *idx = got_packidx_get_object_idx(*packidx, id);
659 if (*idx != -1) {
660 err = NULL; /* found the object */
661 err = got_repo_cache_packidx(repo, *packidx);
662 goto done;
665 err = got_packidx_close(*packidx);
666 *packidx = NULL;
667 if (err)
668 goto done;
671 err = got_error(GOT_ERR_NO_OBJ);
672 done:
673 free(path_packdir);
674 if (packdir && closedir(packdir) != 0 && err == 0)
675 err = got_error_from_errno();
676 return err;
679 static const struct got_error *
680 read_packfile_hdr(int fd, struct got_packidx *packidx)
682 const struct got_error *err = NULL;
683 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
684 struct got_packfile_hdr hdr;
685 ssize_t n;
687 n = read(fd, &hdr, sizeof(hdr));
688 if (n < 0)
689 return got_error_from_errno();
690 if (n != sizeof(hdr))
691 return got_error(GOT_ERR_BAD_PACKFILE);
693 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
694 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
695 betoh32(hdr.nobjects) != totobj)
696 err = got_error(GOT_ERR_BAD_PACKFILE);
698 return err;
701 static const struct got_error *
702 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
704 const struct got_error *err = NULL;
706 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
707 if (*fd == -1)
708 return got_error_from_errno();
710 if (packidx) {
711 err = read_packfile_hdr(*fd, packidx);
712 if (err) {
713 close(*fd);
714 *fd = -1;
718 return err;
721 const struct got_error *
722 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
723 const char *path_packfile, struct got_packidx *packidx)
725 const struct got_error *err = NULL;
726 struct got_pack *pack = NULL;
727 int i;
729 if (packp)
730 *packp = NULL;
732 for (i = 0; i < nitems(repo->packs); i++) {
733 pack = &repo->packs[i];
734 if (pack->path_packfile == NULL)
735 break;
736 if (strcmp(pack->path_packfile, path_packfile) == 0)
737 return NULL;
740 if (i == nitems(repo->packs) - 1) {
741 err = got_pack_close(&repo->packs[i - 1]);
742 if (err)
743 return err;
744 memmove(&repo->packs[1], &repo->packs[0],
745 sizeof(repo->packs) - sizeof(repo->packs[0]));
746 i = 0;
749 pack = &repo->packs[i];
751 pack->path_packfile = strdup(path_packfile);
752 if (pack->path_packfile == NULL) {
753 err = got_error_from_errno();
754 goto done;
757 err = open_packfile(&pack->fd, path_packfile, packidx);
758 if (err)
759 goto done;
761 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
762 if (err)
763 goto done;
765 pack->privsep_child = NULL;
767 #ifndef GOT_PACK_NO_MMAP
768 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
769 pack->fd, 0);
770 if (pack->map == MAP_FAILED)
771 pack->map = NULL; /* fall back to read(2) */
772 #endif
773 done:
774 if (err) {
775 if (pack) {
776 free(pack->path_packfile);
777 memset(pack, 0, sizeof(*pack));
779 } else if (packp)
780 *packp = pack;
781 return err;
784 struct got_pack *
785 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
787 struct got_pack *pack = NULL;
788 int i;
790 for (i = 0; i < nitems(repo->packs); i++) {
791 pack = &repo->packs[i];
792 if (pack->path_packfile == NULL)
793 break;
794 if (strcmp(pack->path_packfile, path_packfile) == 0)
795 return pack;
798 return NULL;