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"
43 #include "got_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_prefix_errno("strdup");
271 if (is_git_repo(repo)) {
272 repo->path = strdup(repo->path_git_dir);
273 if (repo->path == NULL) {
274 err = got_error_prefix_errno("strdup");
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_prefix_errno("asprintf");
284 goto done;
286 if (is_git_repo(repo)) {
287 repo->path = strdup(path);
288 if (repo->path == NULL) {
289 err = got_error_prefix_errno("strdup");
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_prefix_errno("calloc");
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_prefix_errno2("dirname", path);
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_prefix_errno("close");
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 size_t repolen, cwdlen, len;
438 char *cwd, *canonpath, *path = NULL;
440 *in_repo_path = NULL;
442 cwd = getcwd(NULL, 0);
443 if (cwd == NULL)
444 return got_error_prefix_errno("getcwd");
446 canonpath = strdup(input_path);
447 if (canonpath == NULL) {
448 err = got_error_prefix_errno("strdup");
449 goto done;
451 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
452 if (err)
453 goto done;
455 repo_abspath = got_repo_get_path(repo);
457 if (!check_disk) {
458 path = strdup(canonpath);
459 if (path == NULL) {
460 err = got_error_prefix_errno("strdup");
461 goto done;
463 } else {
464 int is_repo_child = 0, is_cwd_child = 0;
466 path = realpath(canonpath, NULL);
467 if (path == NULL) {
468 if (errno != ENOENT) {
469 err = got_error_prefix_errno2("realpath",
470 canonpath);
471 goto done;
473 /*
474 * Path is not on disk.
475 * Assume it is already relative to repository root.
476 */
477 path = strdup(canonpath);
478 if (path == NULL) {
479 err = got_error_prefix_errno("strdup");
480 goto done;
484 repolen = strlen(repo_abspath);
485 cwdlen = strlen(cwd);
486 len = strlen(path);
488 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
489 is_repo_child = 1;
490 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
491 is_cwd_child = 1;
493 if (strcmp(path, repo_abspath) == 0) {
494 free(path);
495 path = strdup("");
496 if (path == NULL) {
497 err = got_error_prefix_errno("strdup");
498 goto done;
500 } else if (is_repo_child && is_cwd_child) {
501 char *child;
502 /* Strip common prefix with repository path. */
503 err = got_path_skip_common_ancestor(&child,
504 repo_abspath, path);
505 if (err)
506 goto done;
507 free(path);
508 path = child;
509 } else if (is_repo_child) {
510 /* Matched an on-disk path inside repository. */
511 if (got_repo_is_bare(repo)) {
512 /*
513 * Matched an on-disk path inside repository
514 * database. Treat as repository-relative.
515 */
516 } else {
517 char *child;
518 /* Strip common prefix with repository path. */
519 err = got_path_skip_common_ancestor(&child,
520 repo_abspath, path);
521 if (err)
522 goto done;
523 free(path);
524 path = child;
526 } else if (is_cwd_child) {
527 char *child;
528 /* Strip common prefix with cwd. */
529 err = got_path_skip_common_ancestor(&child, cwd,
530 path);
531 if (err)
532 goto done;
533 free(path);
534 path = child;
535 } else {
536 /*
537 * Matched unrelated on-disk path.
538 * Treat it as repository-relative.
539 */
543 /* Make in-repository path absolute */
544 if (path[0] != '/') {
545 char *abspath;
546 if (asprintf(&abspath, "/%s", path) == -1) {
547 err = got_error_prefix_errno("asprintf");
548 goto done;
550 free(path);
551 path = abspath;
554 done:
555 free(cwd);
556 free(canonpath);
557 if (err)
558 free(path);
559 else
560 *in_repo_path = path;
561 return err;
564 const struct got_error *
565 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
567 const struct got_error *err = NULL;
568 int i;
570 for (i = 0; i < nitems(repo->packidx_cache); i++) {
571 if (repo->packidx_cache[i] == NULL)
572 break;
574 if (i == nitems(repo->packidx_cache)) {
575 err = got_packidx_close(repo->packidx_cache[i - 1]);
576 if (err)
577 return err;
580 /*
581 * Insert the new pack index at the front so it will
582 * be searched first in the future.
583 */
584 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
585 sizeof(repo->packidx_cache) -
586 sizeof(repo->packidx_cache[0]));
587 repo->packidx_cache[0] = packidx;
589 return NULL;
592 static int
593 is_packidx_filename(const char *name, size_t len)
595 if (len != GOT_PACKIDX_NAMELEN)
596 return 0;
598 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
599 return 0;
601 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
602 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
603 return 0;
605 return 1;
608 const struct got_error *
609 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
610 struct got_repository *repo, struct got_object_id *id)
612 const struct got_error *err;
613 char *path_packdir;
614 DIR *packdir;
615 struct dirent *dent;
616 char *path_packidx;
617 int i;
619 /* Search pack index cache. */
620 for (i = 0; i < nitems(repo->packidx_cache); i++) {
621 if (repo->packidx_cache[i] == NULL)
622 break;
623 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
624 if (*idx != -1) {
625 *packidx = repo->packidx_cache[i];
626 return NULL;
629 /* No luck. Search the filesystem. */
631 path_packdir = got_repo_get_path_objects_pack(repo);
632 if (path_packdir == NULL)
633 return got_error_prefix_errno("got_repo_get_path_objects_pack");
635 packdir = opendir(path_packdir);
636 if (packdir == NULL) {
637 err = got_error_prefix_errno2("opendir", path_packdir);
638 goto done;
641 while ((dent = readdir(packdir)) != NULL) {
642 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
643 continue;
645 if (asprintf(&path_packidx, "%s/%s", path_packdir,
646 dent->d_name) == -1) {
647 err = got_error_prefix_errno("asprintf");
648 goto done;
651 err = got_packidx_open(packidx, path_packidx, 0);
652 free(path_packidx);
653 if (err)
654 goto done;
656 *idx = got_packidx_get_object_idx(*packidx, id);
657 if (*idx != -1) {
658 err = NULL; /* found the object */
659 err = got_repo_cache_packidx(repo, *packidx);
660 goto done;
663 err = got_packidx_close(*packidx);
664 *packidx = NULL;
665 if (err)
666 goto done;
669 err = got_error_no_obj(id);
670 done:
671 free(path_packdir);
672 if (packdir && closedir(packdir) != 0 && err == 0)
673 err = got_error_prefix_errno("closedir");
674 return err;
677 static const struct got_error *
678 read_packfile_hdr(int fd, struct got_packidx *packidx)
680 const struct got_error *err = NULL;
681 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
682 struct got_packfile_hdr hdr;
683 ssize_t n;
685 n = read(fd, &hdr, sizeof(hdr));
686 if (n < 0)
687 return got_error_prefix_errno("read");
688 if (n != sizeof(hdr))
689 return got_error(GOT_ERR_BAD_PACKFILE);
691 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
692 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
693 betoh32(hdr.nobjects) != totobj)
694 err = got_error(GOT_ERR_BAD_PACKFILE);
696 return err;
699 static const struct got_error *
700 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
702 const struct got_error *err = NULL;
704 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
705 if (*fd == -1)
706 return got_error_prefix_errno2("open", path_packfile);
708 if (packidx) {
709 err = read_packfile_hdr(*fd, packidx);
710 if (err) {
711 close(*fd);
712 *fd = -1;
716 return err;
719 const struct got_error *
720 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
721 const char *path_packfile, struct got_packidx *packidx)
723 const struct got_error *err = NULL;
724 struct got_pack *pack = NULL;
725 int i;
727 if (packp)
728 *packp = NULL;
730 for (i = 0; i < nitems(repo->packs); i++) {
731 pack = &repo->packs[i];
732 if (pack->path_packfile == NULL)
733 break;
734 if (strcmp(pack->path_packfile, path_packfile) == 0)
735 return NULL;
738 if (i == nitems(repo->packs) - 1) {
739 err = got_pack_close(&repo->packs[i - 1]);
740 if (err)
741 return err;
742 memmove(&repo->packs[1], &repo->packs[0],
743 sizeof(repo->packs) - sizeof(repo->packs[0]));
744 i = 0;
747 pack = &repo->packs[i];
749 pack->path_packfile = strdup(path_packfile);
750 if (pack->path_packfile == NULL) {
751 err = got_error_prefix_errno("strdup");
752 goto done;
755 err = open_packfile(&pack->fd, path_packfile, packidx);
756 if (err)
757 goto done;
759 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
760 if (err)
761 goto done;
763 pack->privsep_child = NULL;
765 #ifndef GOT_PACK_NO_MMAP
766 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
767 pack->fd, 0);
768 if (pack->map == MAP_FAILED) {
769 if (errno != ENOMEM) {
770 err = got_error_prefix_errno("mmap");
771 goto done;
773 pack->map = NULL; /* fall back to read(2) */
775 #endif
776 done:
777 if (err) {
778 if (pack) {
779 free(pack->path_packfile);
780 memset(pack, 0, sizeof(*pack));
782 } else if (packp)
783 *packp = pack;
784 return err;
787 struct got_pack *
788 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
790 struct got_pack *pack = NULL;
791 int i;
793 for (i = 0; i < nitems(repo->packs); i++) {
794 pack = &repo->packs[i];
795 if (pack->path_packfile == NULL)
796 break;
797 if (strcmp(pack->path_packfile, path_packfile) == 0)
798 return pack;
801 return NULL;