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;
266 /* bare git repository? */
267 repo->path_git_dir = strdup(path);
268 if (repo->path_git_dir == NULL)
269 return got_error_from_errno();
270 if (is_git_repo(repo)) {
271 repo->path = strdup(repo->path_git_dir);
272 if (repo->path == NULL) {
273 err = got_error_from_errno();
274 goto done;
276 return NULL;
279 /* git repository with working tree? */
280 free(repo->path_git_dir);
281 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
282 err = got_error_from_errno();
283 goto done;
285 if (is_git_repo(repo)) {
286 repo->path = strdup(path);
287 if (repo->path == NULL) {
288 err = got_error_from_errno();
289 goto done;
291 return NULL;
294 err = got_error(GOT_ERR_NOT_GIT_REPO);
295 done:
296 if (err) {
297 free(repo->path);
298 repo->path = NULL;
299 free(repo->path_git_dir);
300 repo->path_git_dir = NULL;
302 return err;
305 const struct got_error *
306 got_repo_open(struct got_repository **repop, const char *path)
308 struct got_repository *repo = NULL;
309 const struct got_error *err = NULL;
310 char *abspath, *normpath = NULL;
311 int i, tried_root = 0;
313 *repop = NULL;
315 if (got_path_is_absolute(path))
316 abspath = strdup(path);
317 else
318 abspath = got_path_get_absolute(path);
319 if (abspath == NULL)
320 return got_error(GOT_ERR_BAD_PATH);
322 repo = calloc(1, sizeof(*repo));
323 if (repo == NULL) {
324 err = got_error_from_errno();
325 goto done;
328 for (i = 0; i < nitems(repo->privsep_children); i++) {
329 memset(&repo->privsep_children[i], 0,
330 sizeof(repo->privsep_children[0]));
331 repo->privsep_children[i].imsg_fd = -1;
334 err = got_object_cache_init(&repo->objcache,
335 GOT_OBJECT_CACHE_TYPE_OBJ);
336 if (err)
337 goto done;
338 err = got_object_cache_init(&repo->treecache,
339 GOT_OBJECT_CACHE_TYPE_TREE);
340 if (err)
341 goto done;
342 err = got_object_cache_init(&repo->commitcache,
343 GOT_OBJECT_CACHE_TYPE_COMMIT);
344 if (err)
345 goto done;
346 err = got_object_cache_init(&repo->tagcache,
347 GOT_OBJECT_CACHE_TYPE_TAG);
348 if (err)
349 goto done;
351 normpath = got_path_normalize(abspath);
352 if (normpath == NULL) {
353 err = got_error(GOT_ERR_BAD_PATH);
354 goto done;
357 path = normpath;
358 do {
359 err = open_repo(repo, path);
360 if (err == NULL)
361 break;
362 if (err->code != GOT_ERR_NOT_GIT_REPO)
363 break;
364 if (path[0] == '/' && path[1] == '\0') {
365 if (tried_root) {
366 err = got_error(GOT_ERR_NOT_GIT_REPO);
367 break;
369 tried_root = 1;
371 path = dirname(path);
372 if (path == NULL)
373 err = got_error_from_errno();
374 } while (path);
375 done:
376 if (err)
377 got_repo_close(repo);
378 else
379 *repop = repo;
380 free(abspath);
381 free(normpath);
382 return err;
385 const struct got_error *
386 got_repo_close(struct got_repository *repo)
388 const struct got_error *err = NULL, *child_err;
389 int i;
391 for (i = 0; i < nitems(repo->packidx_cache); i++) {
392 if (repo->packidx_cache[i] == NULL)
393 break;
394 got_packidx_close(repo->packidx_cache[i]);
397 for (i = 0; i < nitems(repo->packs); i++) {
398 if (repo->packs[i].path_packfile == NULL)
399 break;
400 got_pack_close(&repo->packs[i]);
403 free(repo->path);
404 free(repo->path_git_dir);
406 got_object_cache_close(&repo->objcache);
407 got_object_cache_close(&repo->treecache);
408 got_object_cache_close(&repo->commitcache);
409 got_object_cache_close(&repo->tagcache);
411 for (i = 0; i < nitems(repo->privsep_children); i++) {
412 if (repo->privsep_children[i].imsg_fd == -1)
413 continue;
414 imsg_clear(repo->privsep_children[i].ibuf);
415 free(repo->privsep_children[i].ibuf);
416 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
417 child_err = got_privsep_wait_for_child(
418 repo->privsep_children[i].pid);
419 if (child_err && err == NULL)
420 err = child_err;
421 close(repo->privsep_children[i].imsg_fd);
423 free(repo);
425 return err;
428 const struct got_error *
429 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
430 const char *input_path, int check_disk)
432 const struct got_error *err = NULL;
433 const char *repo_abspath = NULL;
434 struct stat sb;
435 size_t repolen, cwdlen, len;
436 char *cwd, *canonpath, *path = NULL;
438 *in_repo_path = NULL;
440 cwd = getcwd(NULL, 0);
441 if (cwd == NULL)
442 return got_error_from_errno();
444 canonpath = strdup(input_path);
445 if (canonpath == NULL) {
446 err = got_error_from_errno();
447 goto done;
449 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
450 if (err)
451 goto done;
453 repo_abspath = got_repo_get_path(repo);
455 /* TODO: Call "get in-repository path of work-tree node" API. */
457 if (!check_disk)
458 path = strdup(canonpath);
459 else if (lstat(canonpath, &sb) != 0) {
460 if (errno != ENOENT) {
461 err = got_error_from_errno();
462 goto done;
464 /*
465 * Path is not on disk.
466 * Assume it is already relative to repository root.
467 */
468 path = strdup(canonpath);
469 } else {
470 int is_repo_child = 0, is_cwd_child = 0;
472 path = realpath(canonpath, NULL);
473 if (path == NULL) {
474 err = got_error_from_errno();
475 goto done;
478 repolen = strlen(repo_abspath);
479 cwdlen = strlen(cwd);
480 len = strlen(path);
482 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
483 is_repo_child = 1;
484 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
485 is_cwd_child = 1;
487 if (strcmp(path, repo_abspath) == 0) {
488 free(path);
489 path = strdup("");
490 if (path == NULL) {
491 err = got_error_from_errno();
492 goto done;
494 } else if (is_repo_child && is_cwd_child) {
495 char *child;
496 /* TODO: Is path inside a got worktree? */
497 /* Strip common prefix with repository path. */
498 err = got_path_skip_common_ancestor(&child,
499 repo_abspath, path);
500 if (err)
501 goto done;
502 free(path);
503 path = child;
504 } else if (is_repo_child) {
505 /* Matched an on-disk path inside repository. */
506 if (got_repo_is_bare(repo)) {
507 /*
508 * Matched an on-disk path inside repository
509 * database. Treat as repository-relative.
510 */
511 } else {
512 char *child;
513 /* Strip common prefix with repository path. */
514 err = got_path_skip_common_ancestor(&child,
515 repo_abspath, path);
516 if (err)
517 goto done;
518 free(path);
519 path = child;
521 } else if (is_cwd_child) {
522 char *child;
523 /* TODO: Is path inside a got worktree? */
524 /* Strip common prefix with cwd. */
525 err = got_path_skip_common_ancestor(&child, cwd,
526 path);
527 if (err)
528 goto done;
529 free(path);
530 path = child;
531 } else {
532 /*
533 * Matched unrelated on-disk path.
534 * Treat it as repository-relative.
535 */
539 /* Make in-repository path absolute */
540 if (path[0] != '/') {
541 char *abspath;
542 if (asprintf(&abspath, "/%s", path) == -1) {
543 err = got_error_from_errno();
544 goto done;
546 free(path);
547 path = abspath;
550 done:
551 free(cwd);
552 free(canonpath);
553 if (err)
554 free(path);
555 else
556 *in_repo_path = path;
557 return err;
560 const struct got_error *
561 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
563 const struct got_error *err = NULL;
564 int i;
566 for (i = 0; i < nitems(repo->packidx_cache); i++) {
567 if (repo->packidx_cache[i] == NULL)
568 break;
570 if (i == nitems(repo->packidx_cache)) {
571 err = got_packidx_close(repo->packidx_cache[i - 1]);
572 if (err)
573 return err;
576 /*
577 * Insert the new pack index at the front so it will
578 * be searched first in the future.
579 */
580 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
581 sizeof(repo->packidx_cache) -
582 sizeof(repo->packidx_cache[0]));
583 repo->packidx_cache[0] = packidx;
585 return NULL;
588 static int
589 is_packidx_filename(const char *name, size_t len)
591 if (len != GOT_PACKIDX_NAMELEN)
592 return 0;
594 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
595 return 0;
597 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
598 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
599 return 0;
601 return 1;
604 const struct got_error *
605 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
606 struct got_repository *repo, struct got_object_id *id)
608 const struct got_error *err;
609 char *path_packdir;
610 DIR *packdir;
611 struct dirent *dent;
612 char *path_packidx;
613 int i;
615 /* Search pack index cache. */
616 for (i = 0; i < nitems(repo->packidx_cache); i++) {
617 if (repo->packidx_cache[i] == NULL)
618 break;
619 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
620 if (*idx != -1) {
621 *packidx = repo->packidx_cache[i];
622 return NULL;
625 /* No luck. Search the filesystem. */
627 path_packdir = got_repo_get_path_objects_pack(repo);
628 if (path_packdir == NULL)
629 return got_error_from_errno();
631 packdir = opendir(path_packdir);
632 if (packdir == NULL) {
633 err = got_error_from_errno();
634 goto done;
637 while ((dent = readdir(packdir)) != NULL) {
638 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
639 continue;
641 if (asprintf(&path_packidx, "%s/%s", path_packdir,
642 dent->d_name) == -1) {
643 err = got_error_from_errno();
644 goto done;
647 err = got_packidx_open(packidx, path_packidx, 0);
648 free(path_packidx);
649 if (err)
650 goto done;
652 *idx = got_packidx_get_object_idx(*packidx, id);
653 if (*idx != -1) {
654 err = NULL; /* found the object */
655 err = got_repo_cache_packidx(repo, *packidx);
656 goto done;
659 err = got_packidx_close(*packidx);
660 *packidx = NULL;
661 if (err)
662 goto done;
665 err = got_error_no_obj(id);
666 done:
667 free(path_packdir);
668 if (packdir && closedir(packdir) != 0 && err == 0)
669 err = got_error_from_errno();
670 return err;
673 static const struct got_error *
674 read_packfile_hdr(int fd, struct got_packidx *packidx)
676 const struct got_error *err = NULL;
677 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
678 struct got_packfile_hdr hdr;
679 ssize_t n;
681 n = read(fd, &hdr, sizeof(hdr));
682 if (n < 0)
683 return got_error_from_errno();
684 if (n != sizeof(hdr))
685 return got_error(GOT_ERR_BAD_PACKFILE);
687 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
688 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
689 betoh32(hdr.nobjects) != totobj)
690 err = got_error(GOT_ERR_BAD_PACKFILE);
692 return err;
695 static const struct got_error *
696 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
698 const struct got_error *err = NULL;
700 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
701 if (*fd == -1)
702 return got_error_from_errno();
704 if (packidx) {
705 err = read_packfile_hdr(*fd, packidx);
706 if (err) {
707 close(*fd);
708 *fd = -1;
712 return err;
715 const struct got_error *
716 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
717 const char *path_packfile, struct got_packidx *packidx)
719 const struct got_error *err = NULL;
720 struct got_pack *pack = NULL;
721 int i;
723 if (packp)
724 *packp = NULL;
726 for (i = 0; i < nitems(repo->packs); i++) {
727 pack = &repo->packs[i];
728 if (pack->path_packfile == NULL)
729 break;
730 if (strcmp(pack->path_packfile, path_packfile) == 0)
731 return NULL;
734 if (i == nitems(repo->packs) - 1) {
735 err = got_pack_close(&repo->packs[i - 1]);
736 if (err)
737 return err;
738 memmove(&repo->packs[1], &repo->packs[0],
739 sizeof(repo->packs) - sizeof(repo->packs[0]));
740 i = 0;
743 pack = &repo->packs[i];
745 pack->path_packfile = strdup(path_packfile);
746 if (pack->path_packfile == NULL) {
747 err = got_error_from_errno();
748 goto done;
751 err = open_packfile(&pack->fd, path_packfile, packidx);
752 if (err)
753 goto done;
755 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
756 if (err)
757 goto done;
759 pack->privsep_child = NULL;
761 #ifndef GOT_PACK_NO_MMAP
762 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
763 pack->fd, 0);
764 if (pack->map == MAP_FAILED)
765 pack->map = NULL; /* fall back to read(2) */
766 #endif
767 done:
768 if (err) {
769 if (pack) {
770 free(pack->path_packfile);
771 memset(pack, 0, sizeof(*pack));
773 } else if (packp)
774 *packp = pack;
775 return err;
778 struct got_pack *
779 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
781 struct got_pack *pack = NULL;
782 int i;
784 for (i = 0; i < nitems(repo->packs); i++) {
785 pack = &repo->packs[i];
786 if (pack->path_packfile == NULL)
787 break;
788 if (strcmp(pack->path_packfile, path_packfile) == 0)
789 return pack;
792 return NULL;