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 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
422 err == NULL)
423 err = got_error_from_errno();
425 free(repo);
427 return err;
430 const struct got_error *
431 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
432 const char *input_path, int check_disk)
434 const struct got_error *err = NULL;
435 const char *repo_abspath = NULL;
436 struct stat sb;
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_from_errno();
446 canonpath = strdup(input_path);
447 if (canonpath == NULL) {
448 err = got_error_from_errno();
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 /* TODO: Call "get in-repository path of work-tree node" API. */
459 if (!check_disk)
460 path = strdup(canonpath);
461 else if (lstat(canonpath, &sb) != 0) {
462 if (errno != ENOENT) {
463 err = got_error_from_errno();
464 goto done;
466 /*
467 * Path is not on disk.
468 * Assume it is already relative to repository root.
469 */
470 path = strdup(canonpath);
471 } else {
472 int is_repo_child = 0, is_cwd_child = 0;
474 path = realpath(canonpath, NULL);
475 if (path == NULL) {
476 err = got_error_from_errno();
477 goto done;
480 repolen = strlen(repo_abspath);
481 cwdlen = strlen(cwd);
482 len = strlen(path);
484 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
485 is_repo_child = 1;
486 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
487 is_cwd_child = 1;
489 if (strcmp(path, repo_abspath) == 0) {
490 free(path);
491 path = strdup("");
492 if (path == NULL) {
493 err = got_error_from_errno();
494 goto done;
496 } else if (is_repo_child && is_cwd_child) {
497 char *child;
498 /* TODO: Is path inside a got worktree? */
499 /* Strip common prefix with repository path. */
500 err = got_path_skip_common_ancestor(&child,
501 repo_abspath, path);
502 if (err)
503 goto done;
504 free(path);
505 path = child;
506 } else if (is_repo_child) {
507 /* Matched an on-disk path inside repository. */
508 if (got_repo_is_bare(repo)) {
509 /*
510 * Matched an on-disk path inside repository
511 * database. Treat as repository-relative.
512 */
513 } else {
514 char *child;
515 /* Strip common prefix with repository path. */
516 err = got_path_skip_common_ancestor(&child,
517 repo_abspath, path);
518 if (err)
519 goto done;
520 free(path);
521 path = child;
523 } else if (is_cwd_child) {
524 char *child;
525 /* TODO: Is path inside a got worktree? */
526 /* Strip common prefix with cwd. */
527 err = got_path_skip_common_ancestor(&child, cwd,
528 path);
529 if (err)
530 goto done;
531 free(path);
532 path = child;
533 } else {
534 /*
535 * Matched unrelated on-disk path.
536 * Treat it as repository-relative.
537 */
541 /* Make in-repository path absolute */
542 if (path[0] != '/') {
543 char *abspath;
544 if (asprintf(&abspath, "/%s", path) == -1) {
545 err = got_error_from_errno();
546 goto done;
548 free(path);
549 path = abspath;
552 done:
553 free(cwd);
554 free(canonpath);
555 if (err)
556 free(path);
557 else
558 *in_repo_path = path;
559 return err;
562 const struct got_error *
563 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
565 const struct got_error *err = NULL;
566 int i;
568 for (i = 0; i < nitems(repo->packidx_cache); i++) {
569 if (repo->packidx_cache[i] == NULL)
570 break;
572 if (i == nitems(repo->packidx_cache)) {
573 err = got_packidx_close(repo->packidx_cache[i - 1]);
574 if (err)
575 return err;
578 /*
579 * Insert the new pack index at the front so it will
580 * be searched first in the future.
581 */
582 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
583 sizeof(repo->packidx_cache) -
584 sizeof(repo->packidx_cache[0]));
585 repo->packidx_cache[0] = packidx;
587 return NULL;
590 static int
591 is_packidx_filename(const char *name, size_t len)
593 if (len != GOT_PACKIDX_NAMELEN)
594 return 0;
596 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
597 return 0;
599 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
600 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
601 return 0;
603 return 1;
606 const struct got_error *
607 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
608 struct got_repository *repo, struct got_object_id *id)
610 const struct got_error *err;
611 char *path_packdir;
612 DIR *packdir;
613 struct dirent *dent;
614 char *path_packidx;
615 int i;
617 /* Search pack index cache. */
618 for (i = 0; i < nitems(repo->packidx_cache); i++) {
619 if (repo->packidx_cache[i] == NULL)
620 break;
621 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
622 if (*idx != -1) {
623 *packidx = repo->packidx_cache[i];
624 return NULL;
627 /* No luck. Search the filesystem. */
629 path_packdir = got_repo_get_path_objects_pack(repo);
630 if (path_packdir == NULL)
631 return got_error_from_errno();
633 packdir = opendir(path_packdir);
634 if (packdir == NULL) {
635 err = got_error_from_errno();
636 goto done;
639 while ((dent = readdir(packdir)) != NULL) {
640 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
641 continue;
643 if (asprintf(&path_packidx, "%s/%s", path_packdir,
644 dent->d_name) == -1) {
645 err = got_error_from_errno();
646 goto done;
649 err = got_packidx_open(packidx, path_packidx, 0);
650 free(path_packidx);
651 if (err)
652 goto done;
654 *idx = got_packidx_get_object_idx(*packidx, id);
655 if (*idx != -1) {
656 err = NULL; /* found the object */
657 err = got_repo_cache_packidx(repo, *packidx);
658 goto done;
661 err = got_packidx_close(*packidx);
662 *packidx = NULL;
663 if (err)
664 goto done;
667 err = got_error_no_obj(id);
668 done:
669 free(path_packdir);
670 if (packdir && closedir(packdir) != 0 && err == 0)
671 err = got_error_from_errno();
672 return err;
675 static const struct got_error *
676 read_packfile_hdr(int fd, struct got_packidx *packidx)
678 const struct got_error *err = NULL;
679 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
680 struct got_packfile_hdr hdr;
681 ssize_t n;
683 n = read(fd, &hdr, sizeof(hdr));
684 if (n < 0)
685 return got_error_from_errno();
686 if (n != sizeof(hdr))
687 return got_error(GOT_ERR_BAD_PACKFILE);
689 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
690 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
691 betoh32(hdr.nobjects) != totobj)
692 err = got_error(GOT_ERR_BAD_PACKFILE);
694 return err;
697 static const struct got_error *
698 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
700 const struct got_error *err = NULL;
702 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
703 if (*fd == -1)
704 return got_error_from_errno();
706 if (packidx) {
707 err = read_packfile_hdr(*fd, packidx);
708 if (err) {
709 close(*fd);
710 *fd = -1;
714 return err;
717 const struct got_error *
718 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
719 const char *path_packfile, struct got_packidx *packidx)
721 const struct got_error *err = NULL;
722 struct got_pack *pack = NULL;
723 int i;
725 if (packp)
726 *packp = NULL;
728 for (i = 0; i < nitems(repo->packs); i++) {
729 pack = &repo->packs[i];
730 if (pack->path_packfile == NULL)
731 break;
732 if (strcmp(pack->path_packfile, path_packfile) == 0)
733 return NULL;
736 if (i == nitems(repo->packs) - 1) {
737 err = got_pack_close(&repo->packs[i - 1]);
738 if (err)
739 return err;
740 memmove(&repo->packs[1], &repo->packs[0],
741 sizeof(repo->packs) - sizeof(repo->packs[0]));
742 i = 0;
745 pack = &repo->packs[i];
747 pack->path_packfile = strdup(path_packfile);
748 if (pack->path_packfile == NULL) {
749 err = got_error_from_errno();
750 goto done;
753 err = open_packfile(&pack->fd, path_packfile, packidx);
754 if (err)
755 goto done;
757 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
758 if (err)
759 goto done;
761 pack->privsep_child = NULL;
763 #ifndef GOT_PACK_NO_MMAP
764 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
765 pack->fd, 0);
766 if (pack->map == MAP_FAILED) {
767 if (errno != ENOMEM) {
768 err = got_error_from_errno();
769 goto done;
771 pack->map = NULL; /* fall back to read(2) */
773 #endif
774 done:
775 if (err) {
776 if (pack) {
777 free(pack->path_packfile);
778 memset(pack, 0, sizeof(*pack));
780 } else if (packp)
781 *packp = pack;
782 return err;
785 struct got_pack *
786 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
788 struct got_pack *pack = NULL;
789 int i;
791 for (i = 0; i < nitems(repo->packs); i++) {
792 pack = &repo->packs[i];
793 if (pack->path_packfile == NULL)
794 break;
795 if (strcmp(pack->path_packfile, path_packfile) == 0)
796 return pack;
799 return NULL;