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>
36 #include <uuid.h>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_worktree.h"
42 #include "got_object.h"
44 #include "got_lib_path.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 #define GOT_GIT_DIR ".git"
60 /* Mandatory files and directories inside the git directory. */
61 #define GOT_OBJECTS_DIR "objects"
62 #define GOT_REFS_DIR "refs"
63 #define GOT_HEAD_FILE "HEAD"
65 /* Other files and directories inside the git directory. */
66 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
67 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
68 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 #define GOT_PACKED_REFS_FILE "packed-refs"
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_is_bare(struct got_repository *repo)
85 {
86 return (strcmp(repo->path, repo->path_git_dir) == 0);
87 }
89 static char *
90 get_path_git_child(struct got_repository *repo, const char *basename)
91 {
92 char *path_child;
94 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
95 basename) == -1)
96 return NULL;
98 return path_child;
99 }
101 char *
102 got_repo_get_path_objects(struct got_repository *repo)
104 return get_path_git_child(repo, GOT_OBJECTS_DIR);
107 char *
108 got_repo_get_path_objects_pack(struct got_repository *repo)
110 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
113 char *
114 got_repo_get_path_refs(struct got_repository *repo)
116 return get_path_git_child(repo, GOT_REFS_DIR);
119 char *
120 got_repo_get_path_packed_refs(struct got_repository *repo)
122 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
125 static char *
126 get_path_head(struct got_repository *repo)
128 return get_path_git_child(repo, GOT_HEAD_FILE);
131 static int
132 is_git_repo(struct got_repository *repo)
134 const char *path_git = got_repo_get_path_git_dir(repo);
135 char *path_objects = got_repo_get_path_objects(repo);
136 char *path_refs = got_repo_get_path_refs(repo);
137 char *path_head = get_path_head(repo);
138 int ret = 0;
139 struct stat sb;
140 struct got_reference *head_ref;
142 if (lstat(path_git, &sb) == -1)
143 goto done;
144 if (!S_ISDIR(sb.st_mode))
145 goto done;
147 if (lstat(path_objects, &sb) == -1)
148 goto done;
149 if (!S_ISDIR(sb.st_mode))
150 goto done;
152 if (lstat(path_refs, &sb) == -1)
153 goto done;
154 if (!S_ISDIR(sb.st_mode))
155 goto done;
157 if (lstat(path_head, &sb) == -1)
158 goto done;
159 if (!S_ISREG(sb.st_mode))
160 goto done;
162 /* Check if the HEAD reference can be opened. */
163 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
164 goto done;
165 got_ref_close(head_ref);
167 ret = 1;
168 done:
169 free(path_objects);
170 free(path_refs);
171 free(path_head);
172 return ret;
176 const struct got_error *
177 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
178 struct got_object *obj)
180 #ifndef GOT_NO_OBJ_CACHE
181 const struct got_error *err = NULL;
182 err = got_object_cache_add(&repo->objcache, id, obj);
183 if (err)
184 return err;
185 obj->refcnt++;
186 #endif
187 return NULL;
190 struct got_object *
191 got_repo_get_cached_object(struct got_repository *repo,
192 struct got_object_id *id)
194 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
197 const struct got_error *
198 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
199 struct got_tree_object *tree)
201 #ifndef GOT_NO_OBJ_CACHE
202 const struct got_error *err = NULL;
203 err = got_object_cache_add(&repo->treecache, id, tree);
204 if (err)
205 return err;
206 tree->refcnt++;
207 #endif
208 return NULL;
211 struct got_tree_object *
212 got_repo_get_cached_tree(struct got_repository *repo,
213 struct got_object_id *id)
215 return (struct got_tree_object *)got_object_cache_get(
216 &repo->treecache, id);
219 const struct got_error *
220 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
221 struct got_commit_object *commit)
223 #ifndef GOT_NO_OBJ_CACHE
224 const struct got_error *err = NULL;
225 err = got_object_cache_add(&repo->commitcache, id, commit);
226 if (err)
227 return err;
228 commit->refcnt++;
229 #endif
230 return NULL;
233 struct got_commit_object *
234 got_repo_get_cached_commit(struct got_repository *repo,
235 struct got_object_id *id)
237 return (struct got_commit_object *)got_object_cache_get(
238 &repo->commitcache, id);
241 const struct got_error *
242 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
243 struct got_tag_object *tag)
245 #ifndef GOT_NO_OBJ_CACHE
246 const struct got_error *err = NULL;
247 err = got_object_cache_add(&repo->tagcache, id, tag);
248 if (err)
249 return err;
250 tag->refcnt++;
251 #endif
252 return NULL;
255 struct got_tag_object *
256 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
258 return (struct got_tag_object *)got_object_cache_get(
259 &repo->tagcache, id);
262 const struct got_error *
263 open_repo(struct got_repository *repo, const char *path)
265 const struct got_error *err = NULL;
267 /* bare git repository? */
268 repo->path_git_dir = strdup(path);
269 if (repo->path_git_dir == NULL)
270 return got_error_from_errno();
271 if (is_git_repo(repo)) {
272 repo->path = strdup(repo->path_git_dir);
273 if (repo->path == NULL) {
274 err = got_error_from_errno();
275 goto done;
277 return NULL;
280 /* git repository with working tree? */
281 free(repo->path_git_dir);
282 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
283 err = got_error_from_errno();
284 goto done;
286 if (is_git_repo(repo)) {
287 repo->path = strdup(path);
288 if (repo->path == NULL) {
289 err = got_error_from_errno();
290 goto done;
292 return NULL;
295 err = got_error(GOT_ERR_NOT_GIT_REPO);
296 done:
297 if (err) {
298 free(repo->path);
299 repo->path = NULL;
300 free(repo->path_git_dir);
301 repo->path_git_dir = NULL;
303 return err;
306 const struct got_error *
307 got_repo_open(struct got_repository **repop, const char *path)
309 struct got_repository *repo = NULL;
310 const struct got_error *err = NULL;
311 char *abspath, *normpath = NULL;
312 int i, tried_root = 0;
314 *repop = NULL;
316 if (got_path_is_absolute(path))
317 abspath = strdup(path);
318 else
319 abspath = got_path_get_absolute(path);
320 if (abspath == NULL)
321 return got_error(GOT_ERR_BAD_PATH);
323 repo = calloc(1, sizeof(*repo));
324 if (repo == NULL) {
325 err = got_error_from_errno();
326 goto done;
329 for (i = 0; i < nitems(repo->privsep_children); i++) {
330 memset(&repo->privsep_children[i], 0,
331 sizeof(repo->privsep_children[0]));
332 repo->privsep_children[i].imsg_fd = -1;
335 err = got_object_cache_init(&repo->objcache,
336 GOT_OBJECT_CACHE_TYPE_OBJ);
337 if (err)
338 goto done;
339 err = got_object_cache_init(&repo->treecache,
340 GOT_OBJECT_CACHE_TYPE_TREE);
341 if (err)
342 goto done;
343 err = got_object_cache_init(&repo->commitcache,
344 GOT_OBJECT_CACHE_TYPE_COMMIT);
345 if (err)
346 goto done;
347 err = got_object_cache_init(&repo->tagcache,
348 GOT_OBJECT_CACHE_TYPE_TAG);
349 if (err)
350 goto done;
352 normpath = got_path_normalize(abspath);
353 if (normpath == NULL) {
354 err = got_error(GOT_ERR_BAD_PATH);
355 goto done;
358 path = normpath;
359 do {
360 err = open_repo(repo, path);
361 if (err == NULL)
362 break;
363 if (err->code != GOT_ERR_NOT_GIT_REPO)
364 break;
365 if (path[0] == '/' && path[1] == '\0') {
366 if (tried_root) {
367 err = got_error(GOT_ERR_NOT_GIT_REPO);
368 break;
370 tried_root = 1;
372 path = dirname(path);
373 if (path == NULL)
374 err = got_error_from_errno();
375 } while (path);
376 done:
377 if (err)
378 got_repo_close(repo);
379 else
380 *repop = repo;
381 free(abspath);
382 free(normpath);
383 return err;
386 const struct got_error *
387 got_repo_close(struct got_repository *repo)
389 const struct got_error *err = NULL, *child_err;
390 int i;
392 for (i = 0; i < nitems(repo->packidx_cache); i++) {
393 if (repo->packidx_cache[i] == NULL)
394 break;
395 got_packidx_close(repo->packidx_cache[i]);
398 for (i = 0; i < nitems(repo->packs); i++) {
399 if (repo->packs[i].path_packfile == NULL)
400 break;
401 got_pack_close(&repo->packs[i]);
404 free(repo->path);
405 free(repo->path_git_dir);
407 got_object_cache_close(&repo->objcache);
408 got_object_cache_close(&repo->treecache);
409 got_object_cache_close(&repo->commitcache);
410 got_object_cache_close(&repo->tagcache);
412 for (i = 0; i < nitems(repo->privsep_children); i++) {
413 if (repo->privsep_children[i].imsg_fd == -1)
414 continue;
415 imsg_clear(repo->privsep_children[i].ibuf);
416 free(repo->privsep_children[i].ibuf);
417 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
418 child_err = got_privsep_wait_for_child(
419 repo->privsep_children[i].pid);
420 if (child_err && err == NULL)
421 err = child_err;
422 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
423 err == NULL)
424 err = got_error_from_errno();
426 free(repo);
428 return err;
431 const struct got_error *
432 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
433 const char *input_path, int check_disk)
435 const struct got_error *err = NULL;
436 const char *repo_abspath = NULL;
437 struct stat sb;
438 size_t repolen, cwdlen, len;
439 char *cwd, *canonpath, *path = NULL;
441 *in_repo_path = NULL;
443 cwd = getcwd(NULL, 0);
444 if (cwd == NULL)
445 return got_error_from_errno();
447 canonpath = strdup(input_path);
448 if (canonpath == NULL) {
449 err = got_error_from_errno();
450 goto done;
452 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
453 if (err)
454 goto done;
456 repo_abspath = got_repo_get_path(repo);
458 /* TODO: Call "get in-repository path of work-tree node" API. */
460 if (!check_disk)
461 path = strdup(canonpath);
462 else if (lstat(canonpath, &sb) != 0) {
463 if (errno != ENOENT) {
464 err = got_error_from_errno();
465 goto done;
467 /*
468 * Path is not on disk.
469 * Assume it is already relative to repository root.
470 */
471 path = strdup(canonpath);
472 } else {
473 int is_repo_child = 0, is_cwd_child = 0;
475 path = realpath(canonpath, NULL);
476 if (path == NULL) {
477 err = got_error_from_errno();
478 goto done;
481 repolen = strlen(repo_abspath);
482 cwdlen = strlen(cwd);
483 len = strlen(path);
485 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
486 is_repo_child = 1;
487 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
488 is_cwd_child = 1;
490 if (strcmp(path, repo_abspath) == 0) {
491 free(path);
492 path = strdup("");
493 if (path == NULL) {
494 err = got_error_from_errno();
495 goto done;
497 } else if (is_repo_child && is_cwd_child) {
498 char *child;
499 /* TODO: Is path inside a got worktree? */
500 /* Strip common prefix with repository path. */
501 err = got_path_skip_common_ancestor(&child,
502 repo_abspath, path);
503 if (err)
504 goto done;
505 free(path);
506 path = child;
507 } else if (is_repo_child) {
508 /* Matched an on-disk path inside repository. */
509 if (got_repo_is_bare(repo)) {
510 /*
511 * Matched an on-disk path inside repository
512 * database. Treat as repository-relative.
513 */
514 } else {
515 char *child;
516 /* Strip common prefix with repository path. */
517 err = got_path_skip_common_ancestor(&child,
518 repo_abspath, path);
519 if (err)
520 goto done;
521 free(path);
522 path = child;
524 } else if (is_cwd_child) {
525 char *child;
526 /* TODO: Is path inside a got worktree? */
527 /* Strip common prefix with cwd. */
528 err = got_path_skip_common_ancestor(&child, cwd,
529 path);
530 if (err)
531 goto done;
532 free(path);
533 path = child;
534 } else {
535 /*
536 * Matched unrelated on-disk path.
537 * Treat it as repository-relative.
538 */
542 /* Make in-repository path absolute */
543 if (path[0] != '/') {
544 char *abspath;
545 if (asprintf(&abspath, "/%s", path) == -1) {
546 err = got_error_from_errno();
547 goto done;
549 free(path);
550 path = abspath;
553 done:
554 free(cwd);
555 free(canonpath);
556 if (err)
557 free(path);
558 else
559 *in_repo_path = path;
560 return err;
563 const struct got_error *
564 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
566 const struct got_error *err = NULL;
567 int i;
569 for (i = 0; i < nitems(repo->packidx_cache); i++) {
570 if (repo->packidx_cache[i] == NULL)
571 break;
573 if (i == nitems(repo->packidx_cache)) {
574 err = got_packidx_close(repo->packidx_cache[i - 1]);
575 if (err)
576 return err;
579 /*
580 * Insert the new pack index at the front so it will
581 * be searched first in the future.
582 */
583 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
584 sizeof(repo->packidx_cache) -
585 sizeof(repo->packidx_cache[0]));
586 repo->packidx_cache[0] = packidx;
588 return NULL;
591 static int
592 is_packidx_filename(const char *name, size_t len)
594 if (len != GOT_PACKIDX_NAMELEN)
595 return 0;
597 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
598 return 0;
600 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
601 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
602 return 0;
604 return 1;
607 const struct got_error *
608 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
609 struct got_repository *repo, struct got_object_id *id)
611 const struct got_error *err;
612 char *path_packdir;
613 DIR *packdir;
614 struct dirent *dent;
615 char *path_packidx;
616 int i;
618 /* Search pack index cache. */
619 for (i = 0; i < nitems(repo->packidx_cache); i++) {
620 if (repo->packidx_cache[i] == NULL)
621 break;
622 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
623 if (*idx != -1) {
624 *packidx = repo->packidx_cache[i];
625 return NULL;
628 /* No luck. Search the filesystem. */
630 path_packdir = got_repo_get_path_objects_pack(repo);
631 if (path_packdir == NULL)
632 return got_error_from_errno();
634 packdir = opendir(path_packdir);
635 if (packdir == NULL) {
636 err = got_error_from_errno();
637 goto done;
640 while ((dent = readdir(packdir)) != NULL) {
641 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
642 continue;
644 if (asprintf(&path_packidx, "%s/%s", path_packdir,
645 dent->d_name) == -1) {
646 err = got_error_from_errno();
647 goto done;
650 err = got_packidx_open(packidx, path_packidx, 0);
651 free(path_packidx);
652 if (err)
653 goto done;
655 *idx = got_packidx_get_object_idx(*packidx, id);
656 if (*idx != -1) {
657 err = NULL; /* found the object */
658 err = got_repo_cache_packidx(repo, *packidx);
659 goto done;
662 err = got_packidx_close(*packidx);
663 *packidx = NULL;
664 if (err)
665 goto done;
668 err = got_error_no_obj(id);
669 done:
670 free(path_packdir);
671 if (packdir && closedir(packdir) != 0 && err == 0)
672 err = got_error_from_errno();
673 return err;
676 static const struct got_error *
677 read_packfile_hdr(int fd, struct got_packidx *packidx)
679 const struct got_error *err = NULL;
680 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
681 struct got_packfile_hdr hdr;
682 ssize_t n;
684 n = read(fd, &hdr, sizeof(hdr));
685 if (n < 0)
686 return got_error_from_errno();
687 if (n != sizeof(hdr))
688 return got_error(GOT_ERR_BAD_PACKFILE);
690 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
691 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
692 betoh32(hdr.nobjects) != totobj)
693 err = got_error(GOT_ERR_BAD_PACKFILE);
695 return err;
698 static const struct got_error *
699 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
701 const struct got_error *err = NULL;
703 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
704 if (*fd == -1)
705 return got_error_from_errno();
707 if (packidx) {
708 err = read_packfile_hdr(*fd, packidx);
709 if (err) {
710 close(*fd);
711 *fd = -1;
715 return err;
718 const struct got_error *
719 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
720 const char *path_packfile, struct got_packidx *packidx)
722 const struct got_error *err = NULL;
723 struct got_pack *pack = NULL;
724 int i;
726 if (packp)
727 *packp = NULL;
729 for (i = 0; i < nitems(repo->packs); i++) {
730 pack = &repo->packs[i];
731 if (pack->path_packfile == NULL)
732 break;
733 if (strcmp(pack->path_packfile, path_packfile) == 0)
734 return NULL;
737 if (i == nitems(repo->packs) - 1) {
738 err = got_pack_close(&repo->packs[i - 1]);
739 if (err)
740 return err;
741 memmove(&repo->packs[1], &repo->packs[0],
742 sizeof(repo->packs) - sizeof(repo->packs[0]));
743 i = 0;
746 pack = &repo->packs[i];
748 pack->path_packfile = strdup(path_packfile);
749 if (pack->path_packfile == NULL) {
750 err = got_error_from_errno();
751 goto done;
754 err = open_packfile(&pack->fd, path_packfile, packidx);
755 if (err)
756 goto done;
758 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
759 if (err)
760 goto done;
762 pack->privsep_child = NULL;
764 #ifndef GOT_PACK_NO_MMAP
765 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
766 pack->fd, 0);
767 if (pack->map == MAP_FAILED) {
768 if (errno != ENOMEM) {
769 err = got_error_from_errno();
770 goto done;
772 pack->map = NULL; /* fall back to read(2) */
774 #endif
775 done:
776 if (err) {
777 if (pack) {
778 free(pack->path_packfile);
779 memset(pack, 0, sizeof(*pack));
781 } else if (packp)
782 *packp = pack;
783 return err;
786 struct got_pack *
787 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
789 struct got_pack *pack = NULL;
790 int i;
792 for (i = 0; i < nitems(repo->packs); i++) {
793 pack = &repo->packs[i];
794 if (pack->path_packfile == NULL)
795 break;
796 if (strcmp(pack->path_packfile, path_packfile) == 0)
797 return pack;
800 return NULL;