Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, *pack_fds_tmp;
257 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 if (pack_fds_tmp == NULL)
259 return got_error_from_errno("calloc");
260 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 if (*pack_fds == NULL) {
262 free(pack_fds_tmp);
263 return got_error_from_errno("calloc");
266 /*
267 * got_repo_pack_fds_close will try to close all of the
268 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 * a value from got_opentempfd(), which would result in a close(0) if
270 * we do not initialize to -1 here.
271 */
272 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
273 pack_fds_tmp[i] = -1;
275 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
276 pack_fds_tmp[i] = got_opentempfd();
277 if (pack_fds_tmp[i] == -1) {
278 err = got_error_from_errno("got_opentempfd");
279 got_repo_pack_fds_close(pack_fds_tmp);
280 return err;
283 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
284 return err;
287 const struct got_error *
288 got_repo_pack_fds_close(int *pack_fds)
290 const struct got_error *err = NULL;
291 int i;
293 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
294 if (pack_fds[i] == -1)
295 continue;
296 if (close(pack_fds[i]) == -1) {
297 err = got_error_from_errno("close");
298 break;
301 free(pack_fds);
302 return err;
305 const struct got_error *
306 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
307 struct got_object *obj)
309 #ifndef GOT_NO_OBJ_CACHE
310 const struct got_error *err = NULL;
311 err = got_object_cache_add(&repo->objcache, id, obj);
312 if (err) {
313 if (err->code == GOT_ERR_OBJ_EXISTS ||
314 err->code == GOT_ERR_OBJ_TOO_LARGE)
315 err = NULL;
316 return err;
318 obj->refcnt++;
319 #endif
320 return NULL;
323 struct got_object *
324 got_repo_get_cached_object(struct got_repository *repo,
325 struct got_object_id *id)
327 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
330 const struct got_error *
331 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
332 struct got_tree_object *tree)
334 #ifndef GOT_NO_OBJ_CACHE
335 const struct got_error *err = NULL;
336 err = got_object_cache_add(&repo->treecache, id, tree);
337 if (err) {
338 if (err->code == GOT_ERR_OBJ_EXISTS ||
339 err->code == GOT_ERR_OBJ_TOO_LARGE)
340 err = NULL;
341 return err;
343 tree->refcnt++;
344 #endif
345 return NULL;
348 struct got_tree_object *
349 got_repo_get_cached_tree(struct got_repository *repo,
350 struct got_object_id *id)
352 return (struct got_tree_object *)got_object_cache_get(
353 &repo->treecache, id);
356 const struct got_error *
357 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
358 struct got_commit_object *commit)
360 #ifndef GOT_NO_OBJ_CACHE
361 const struct got_error *err = NULL;
362 err = got_object_cache_add(&repo->commitcache, id, commit);
363 if (err) {
364 if (err->code == GOT_ERR_OBJ_EXISTS ||
365 err->code == GOT_ERR_OBJ_TOO_LARGE)
366 err = NULL;
367 return err;
369 commit->refcnt++;
370 #endif
371 return NULL;
374 struct got_commit_object *
375 got_repo_get_cached_commit(struct got_repository *repo,
376 struct got_object_id *id)
378 return (struct got_commit_object *)got_object_cache_get(
379 &repo->commitcache, id);
382 const struct got_error *
383 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
384 struct got_tag_object *tag)
386 #ifndef GOT_NO_OBJ_CACHE
387 const struct got_error *err = NULL;
388 err = got_object_cache_add(&repo->tagcache, id, tag);
389 if (err) {
390 if (err->code == GOT_ERR_OBJ_EXISTS ||
391 err->code == GOT_ERR_OBJ_TOO_LARGE)
392 err = NULL;
393 return err;
395 tag->refcnt++;
396 #endif
397 return NULL;
400 struct got_tag_object *
401 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
403 return (struct got_tag_object *)got_object_cache_get(
404 &repo->tagcache, id);
407 const struct got_error *
408 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
409 struct got_raw_object *raw)
411 #ifndef GOT_NO_OBJ_CACHE
412 const struct got_error *err = NULL;
413 err = got_object_cache_add(&repo->rawcache, id, raw);
414 if (err) {
415 if (err->code == GOT_ERR_OBJ_EXISTS ||
416 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 err = NULL;
418 return err;
420 raw->refcnt++;
421 #endif
422 return NULL;
426 struct got_raw_object *
427 got_repo_get_cached_raw_object(struct got_repository *repo,
428 struct got_object_id *id)
430 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
434 static const struct got_error *
435 open_repo(struct got_repository *repo, const char *path)
437 const struct got_error *err = NULL;
439 repo->gitdir_fd = -1;
441 /* bare git repository? */
442 repo->path_git_dir = strdup(path);
443 if (repo->path_git_dir == NULL)
444 return got_error_from_errno("strdup");
445 if (is_git_repo(repo)) {
446 repo->path = strdup(repo->path_git_dir);
447 if (repo->path == NULL) {
448 err = got_error_from_errno("strdup");
449 goto done;
451 repo->gitdir_fd = open(repo->path_git_dir,
452 O_DIRECTORY | O_CLOEXEC);
453 if (repo->gitdir_fd == -1) {
454 err = got_error_from_errno2("open",
455 repo->path_git_dir);
456 goto done;
458 return NULL;
461 /* git repository with working tree? */
462 free(repo->path_git_dir);
463 repo->path_git_dir = NULL;
464 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
465 err = got_error_from_errno("asprintf");
466 goto done;
468 if (is_git_repo(repo)) {
469 repo->path = strdup(path);
470 if (repo->path == NULL) {
471 err = got_error_from_errno("strdup");
472 goto done;
474 repo->gitdir_fd = open(repo->path_git_dir,
475 O_DIRECTORY | O_CLOEXEC);
476 if (repo->gitdir_fd == -1) {
477 err = got_error_from_errno2("open",
478 repo->path_git_dir);
479 goto done;
481 return NULL;
484 err = got_error(GOT_ERR_NOT_GIT_REPO);
485 done:
486 if (err) {
487 free(repo->path);
488 repo->path = NULL;
489 free(repo->path_git_dir);
490 repo->path_git_dir = NULL;
491 if (repo->gitdir_fd != -1)
492 close(repo->gitdir_fd);
493 repo->gitdir_fd = -1;
496 return err;
499 static const struct got_error *
500 parse_gitconfig_file(int *gitconfig_repository_format_version,
501 char **gitconfig_author_name, char **gitconfig_author_email,
502 struct got_remote_repo **remotes, int *nremotes,
503 char **gitconfig_owner, char ***extensions, int *nextensions,
504 const char *gitconfig_path)
506 const struct got_error *err = NULL, *child_err = NULL;
507 int fd = -1;
508 int imsg_fds[2] = { -1, -1 };
509 pid_t pid;
510 struct imsgbuf *ibuf;
512 *gitconfig_repository_format_version = 0;
513 if (extensions)
514 *extensions = NULL;
515 if (nextensions)
516 *nextensions = 0;
517 *gitconfig_author_name = NULL;
518 *gitconfig_author_email = NULL;
519 if (remotes)
520 *remotes = NULL;
521 if (nremotes)
522 *nremotes = 0;
523 if (gitconfig_owner)
524 *gitconfig_owner = NULL;
526 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
527 if (fd == -1) {
528 if (errno == ENOENT)
529 return NULL;
530 return got_error_from_errno2("open", gitconfig_path);
533 ibuf = calloc(1, sizeof(*ibuf));
534 if (ibuf == NULL) {
535 err = got_error_from_errno("calloc");
536 goto done;
539 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
540 err = got_error_from_errno("socketpair");
541 goto done;
544 pid = fork();
545 if (pid == -1) {
546 err = got_error_from_errno("fork");
547 goto done;
548 } else if (pid == 0) {
549 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
550 gitconfig_path);
551 /* not reached */
554 if (close(imsg_fds[1]) == -1) {
555 err = got_error_from_errno("close");
556 goto done;
558 imsg_fds[1] = -1;
559 imsg_init(ibuf, imsg_fds[0]);
561 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
562 if (err)
563 goto done;
564 fd = -1;
566 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
567 if (err)
568 goto done;
570 err = got_privsep_recv_gitconfig_int(
571 gitconfig_repository_format_version, ibuf);
572 if (err)
573 goto done;
575 if (extensions && nextensions) {
576 err = got_privsep_send_gitconfig_repository_extensions_req(
577 ibuf);
578 if (err)
579 goto done;
580 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
581 if (err)
582 goto done;
583 if (*nextensions > 0) {
584 int i;
585 *extensions = calloc(*nextensions, sizeof(char *));
586 if (*extensions == NULL) {
587 err = got_error_from_errno("calloc");
588 goto done;
590 for (i = 0; i < *nextensions; i++) {
591 char *ext;
592 err = got_privsep_recv_gitconfig_str(&ext,
593 ibuf);
594 if (err)
595 goto done;
596 (*extensions)[i] = ext;
601 err = got_privsep_send_gitconfig_author_name_req(ibuf);
602 if (err)
603 goto done;
605 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
606 if (err)
607 goto done;
609 err = got_privsep_send_gitconfig_author_email_req(ibuf);
610 if (err)
611 goto done;
613 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
614 if (err)
615 goto done;
617 if (remotes && nremotes) {
618 err = got_privsep_send_gitconfig_remotes_req(ibuf);
619 if (err)
620 goto done;
622 err = got_privsep_recv_gitconfig_remotes(remotes,
623 nremotes, ibuf);
624 if (err)
625 goto done;
628 if (gitconfig_owner) {
629 err = got_privsep_send_gitconfig_owner_req(ibuf);
630 if (err)
631 goto done;
632 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
633 if (err)
634 goto done;
637 err = got_privsep_send_stop(imsg_fds[0]);
638 child_err = got_privsep_wait_for_child(pid);
639 if (child_err && err == NULL)
640 err = child_err;
641 done:
642 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
643 err = got_error_from_errno("close");
644 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
645 err = got_error_from_errno("close");
646 if (fd != -1 && close(fd) == -1 && err == NULL)
647 err = got_error_from_errno2("close", gitconfig_path);
648 free(ibuf);
649 return err;
652 static const struct got_error *
653 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
655 const struct got_error *err = NULL;
656 char *repo_gitconfig_path = NULL;
658 if (global_gitconfig_path) {
659 /* Read settings from ~/.gitconfig. */
660 int dummy_repo_version;
661 err = parse_gitconfig_file(&dummy_repo_version,
662 &repo->global_gitconfig_author_name,
663 &repo->global_gitconfig_author_email,
664 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
665 if (err)
666 return err;
669 /* Read repository's .git/config file. */
670 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
671 if (repo_gitconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gitconfig");
674 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
675 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
676 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
677 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
678 repo_gitconfig_path);
679 if (err)
680 goto done;
681 done:
682 free(repo_gitconfig_path);
683 return err;
686 static const struct got_error *
687 read_gotconfig(struct got_repository *repo)
689 const struct got_error *err = NULL;
690 char *gotconfig_path;
692 gotconfig_path = got_repo_get_path_gotconfig(repo);
693 if (gotconfig_path == NULL)
694 return got_error_from_errno("got_repo_get_path_gotconfig");
696 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
697 free(gotconfig_path);
698 return err;
701 /* Supported repository format extensions. */
702 static const char *const repo_extensions[] = {
703 "noop", /* Got supports repository format version 1. */
704 "preciousObjects", /* Supported by gotadmin cleanup. */
705 "worktreeConfig", /* Got does not care about Git work trees. */
706 };
708 const struct got_error *
709 got_repo_open(struct got_repository **repop, const char *path,
710 const char *global_gitconfig_path, int *pack_fds)
712 struct got_repository *repo = NULL;
713 const struct got_error *err = NULL;
714 char *repo_path = NULL;
715 size_t i, j = 0;
716 struct rlimit rl;
718 *repop = NULL;
720 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
721 return got_error_from_errno("getrlimit");
723 repo = calloc(1, sizeof(*repo));
724 if (repo == NULL)
725 return got_error_from_errno("calloc");
727 RB_INIT(&repo->packidx_bloom_filters);
728 TAILQ_INIT(&repo->packidx_paths);
730 for (i = 0; i < nitems(repo->privsep_children); i++) {
731 memset(&repo->privsep_children[i], 0,
732 sizeof(repo->privsep_children[0]));
733 repo->privsep_children[i].imsg_fd = -1;
736 err = got_object_cache_init(&repo->objcache,
737 GOT_OBJECT_CACHE_TYPE_OBJ);
738 if (err)
739 goto done;
740 err = got_object_cache_init(&repo->treecache,
741 GOT_OBJECT_CACHE_TYPE_TREE);
742 if (err)
743 goto done;
744 err = got_object_cache_init(&repo->commitcache,
745 GOT_OBJECT_CACHE_TYPE_COMMIT);
746 if (err)
747 goto done;
748 err = got_object_cache_init(&repo->tagcache,
749 GOT_OBJECT_CACHE_TYPE_TAG);
750 if (err)
751 goto done;
752 err = got_object_cache_init(&repo->rawcache,
753 GOT_OBJECT_CACHE_TYPE_RAW);
754 if (err)
755 goto done;
757 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
758 if (repo->pack_cache_size > rl.rlim_cur / 8)
759 repo->pack_cache_size = rl.rlim_cur / 8;
760 for (i = 0; i < nitems(repo->packs); i++) {
761 if (i < repo->pack_cache_size) {
762 repo->packs[i].basefd = pack_fds[j++];
763 repo->packs[i].accumfd = pack_fds[j++];
764 } else {
765 repo->packs[i].basefd = -1;
766 repo->packs[i].accumfd = -1;
769 repo->pinned_pack = -1;
770 repo->pinned_packidx = -1;
771 repo->pinned_pid = 0;
773 repo_path = realpath(path, NULL);
774 if (repo_path == NULL) {
775 err = got_error_from_errno2("realpath", path);
776 goto done;
779 for (;;) {
780 char *parent_path;
782 err = open_repo(repo, repo_path);
783 if (err == NULL)
784 break;
785 if (err->code != GOT_ERR_NOT_GIT_REPO)
786 goto done;
787 if (repo_path[0] == '/' && repo_path[1] == '\0') {
788 err = got_error(GOT_ERR_NOT_GIT_REPO);
789 goto done;
791 err = got_path_dirname(&parent_path, repo_path);
792 if (err)
793 goto done;
794 free(repo_path);
795 repo_path = parent_path;
798 err = read_gotconfig(repo);
799 if (err)
800 goto done;
802 err = read_gitconfig(repo, global_gitconfig_path);
803 if (err)
804 goto done;
805 if (repo->gitconfig_repository_format_version != 0) {
806 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
807 goto done;
809 for (i = 0; i < repo->nextensions; i++) {
810 char *ext = repo->extensions[i];
811 int j, supported = 0;
812 for (j = 0; j < nitems(repo_extensions); j++) {
813 if (strcmp(ext, repo_extensions[j]) == 0) {
814 supported = 1;
815 break;
818 if (!supported) {
819 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
820 goto done;
824 err = got_repo_list_packidx(&repo->packidx_paths, repo);
825 done:
826 if (err)
827 got_repo_close(repo);
828 else
829 *repop = repo;
830 free(repo_path);
831 return err;
834 const struct got_error *
835 got_repo_close(struct got_repository *repo)
837 const struct got_error *err = NULL, *child_err;
838 struct got_packidx_bloom_filter *bf;
839 struct got_pathlist_entry *pe;
840 size_t i;
842 for (i = 0; i < repo->pack_cache_size; i++) {
843 if (repo->packidx_cache[i] == NULL)
844 break;
845 got_packidx_close(repo->packidx_cache[i]);
848 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
849 &repo->packidx_bloom_filters))) {
850 RB_REMOVE(got_packidx_bloom_filter_tree,
851 &repo->packidx_bloom_filters, bf);
852 free(bf->bloom);
853 free(bf);
856 for (i = 0; i < repo->pack_cache_size; i++)
857 if (repo->packs[i].path_packfile)
858 if (repo->packs[i].path_packfile)
859 got_pack_close(&repo->packs[i]);
861 free(repo->path);
862 free(repo->path_git_dir);
864 got_object_cache_close(&repo->objcache);
865 got_object_cache_close(&repo->treecache);
866 got_object_cache_close(&repo->commitcache);
867 got_object_cache_close(&repo->tagcache);
868 got_object_cache_close(&repo->rawcache);
870 for (i = 0; i < nitems(repo->privsep_children); i++) {
871 if (repo->privsep_children[i].imsg_fd == -1)
872 continue;
873 imsg_clear(repo->privsep_children[i].ibuf);
874 free(repo->privsep_children[i].ibuf);
875 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
876 child_err = got_privsep_wait_for_child(
877 repo->privsep_children[i].pid);
878 if (child_err && err == NULL)
879 err = child_err;
880 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
881 err == NULL)
882 err = got_error_from_errno("close");
885 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
886 err == NULL)
887 err = got_error_from_errno("close");
889 if (repo->gotconfig)
890 got_gotconfig_free(repo->gotconfig);
891 free(repo->gitconfig_author_name);
892 free(repo->gitconfig_author_email);
893 for (i = 0; i < repo->ngitconfig_remotes; i++)
894 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
895 free(repo->gitconfig_remotes);
896 for (i = 0; i < repo->nextensions; i++)
897 free(repo->extensions[i]);
898 free(repo->extensions);
900 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
901 free((void *)pe->path);
902 got_pathlist_free(&repo->packidx_paths);
903 free(repo);
905 return err;
908 void
909 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
911 int i;
913 free(repo->name);
914 repo->name = NULL;
915 free(repo->fetch_url);
916 repo->fetch_url = NULL;
917 free(repo->send_url);
918 repo->send_url = NULL;
919 for (i = 0; i < repo->nfetch_branches; i++)
920 free(repo->fetch_branches[i]);
921 free(repo->fetch_branches);
922 repo->fetch_branches = NULL;
923 repo->nfetch_branches = 0;
924 for (i = 0; i < repo->nsend_branches; i++)
925 free(repo->send_branches[i]);
926 free(repo->send_branches);
927 repo->send_branches = NULL;
928 repo->nsend_branches = 0;
931 const struct got_error *
932 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
933 const char *input_path)
935 const struct got_error *err = NULL;
936 const char *repo_abspath = NULL;
937 size_t repolen, len;
938 char *canonpath, *path = NULL;
940 *in_repo_path = NULL;
942 canonpath = strdup(input_path);
943 if (canonpath == NULL) {
944 err = got_error_from_errno("strdup");
945 goto done;
947 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
948 if (err)
949 goto done;
951 repo_abspath = got_repo_get_path(repo);
953 if (canonpath[0] == '\0') {
954 path = strdup(canonpath);
955 if (path == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
959 } else {
960 path = realpath(canonpath, NULL);
961 if (path == NULL) {
962 if (errno != ENOENT) {
963 err = got_error_from_errno2("realpath",
964 canonpath);
965 goto done;
967 /*
968 * Path is not on disk.
969 * Assume it is already relative to repository root.
970 */
971 path = strdup(canonpath);
972 if (path == NULL) {
973 err = got_error_from_errno("strdup");
974 goto done;
978 repolen = strlen(repo_abspath);
979 len = strlen(path);
982 if (strcmp(path, repo_abspath) == 0) {
983 free(path);
984 path = strdup("");
985 if (path == NULL) {
986 err = got_error_from_errno("strdup");
987 goto done;
989 } else if (len > repolen &&
990 got_path_is_child(path, repo_abspath, repolen)) {
991 /* Matched an on-disk path inside repository. */
992 if (got_repo_is_bare(repo)) {
993 /*
994 * Matched an on-disk path inside repository
995 * database. Treat input as repository-relative.
996 */
997 free(path);
998 path = canonpath;
999 canonpath = NULL;
1000 } else {
1001 char *child;
1002 /* Strip common prefix with repository path. */
1003 err = got_path_skip_common_ancestor(&child,
1004 repo_abspath, path);
1005 if (err)
1006 goto done;
1007 free(path);
1008 path = child;
1010 } else {
1012 * Matched unrelated on-disk path.
1013 * Treat input as repository-relative.
1015 free(path);
1016 path = canonpath;
1017 canonpath = NULL;
1021 /* Make in-repository path absolute */
1022 if (path[0] != '/') {
1023 char *abspath;
1024 if (asprintf(&abspath, "/%s", path) == -1) {
1025 err = got_error_from_errno("asprintf");
1026 goto done;
1028 free(path);
1029 path = abspath;
1032 done:
1033 free(canonpath);
1034 if (err)
1035 free(path);
1036 else
1037 *in_repo_path = path;
1038 return err;
1041 static const struct got_error *
1042 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1043 const char *path_packidx)
1045 const struct got_error *err = NULL;
1046 size_t i;
1048 for (i = 0; i < repo->pack_cache_size; i++) {
1049 if (repo->packidx_cache[i] == NULL)
1050 break;
1051 if (strcmp(repo->packidx_cache[i]->path_packidx,
1052 path_packidx) == 0) {
1053 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1056 if (i == repo->pack_cache_size) {
1057 do {
1058 i--;
1059 } while (i > 0 && repo->pinned_packidx >= 0 &&
1060 i == repo->pinned_packidx);
1061 err = got_packidx_close(repo->packidx_cache[i]);
1062 if (err)
1063 return err;
1066 repo->packidx_cache[i] = packidx;
1068 return NULL;
1071 int
1072 got_repo_is_packidx_filename(const char *name, size_t len)
1074 if (len != GOT_PACKIDX_NAMELEN)
1075 return 0;
1077 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1078 return 0;
1080 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1081 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1082 return 0;
1084 return 1;
1087 static struct got_packidx_bloom_filter *
1088 get_packidx_bloom_filter(struct got_repository *repo,
1089 const char *path, size_t path_len)
1091 struct got_packidx_bloom_filter key;
1093 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1094 return NULL; /* XXX */
1095 key.path_len = path_len;
1097 return RB_FIND(got_packidx_bloom_filter_tree,
1098 &repo->packidx_bloom_filters, &key);
1101 int
1102 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1103 const char *path_packidx, struct got_object_id *id)
1105 struct got_packidx_bloom_filter *bf;
1107 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1108 if (bf)
1109 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1111 /* No bloom filter means this pack index must be searched. */
1112 return 1;
1115 static const struct got_error *
1116 add_packidx_bloom_filter(struct got_repository *repo,
1117 struct got_packidx *packidx, const char *path_packidx)
1119 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1120 struct got_packidx_bloom_filter *bf;
1121 size_t len;
1124 * Don't use bloom filters for very large pack index files.
1125 * Large pack files will contain a relatively large fraction
1126 * of our objects so we will likely need to visit them anyway.
1127 * The more objects a pack file contains the higher the probability
1128 * of a false-positive match from the bloom filter. And reading
1129 * all object IDs from a large pack index file can be expensive.
1131 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1132 return NULL;
1134 /* Do we already have a filter for this pack index? */
1135 if (get_packidx_bloom_filter(repo, path_packidx,
1136 strlen(path_packidx)) != NULL)
1137 return NULL;
1139 bf = calloc(1, sizeof(*bf));
1140 if (bf == NULL)
1141 return got_error_from_errno("calloc");
1142 bf->bloom = calloc(1, sizeof(*bf->bloom));
1143 if (bf->bloom == NULL) {
1144 free(bf);
1145 return got_error_from_errno("calloc");
1148 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1149 if (len >= sizeof(bf->path)) {
1150 free(bf->bloom);
1151 free(bf);
1152 return got_error(GOT_ERR_NO_SPACE);
1154 bf->path_len = len;
1156 /* Minimum size supported by our bloom filter is 1000 entries. */
1157 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1158 for (i = 0; i < nobjects; i++) {
1159 struct got_packidx_object_id *id;
1160 id = &packidx->hdr.sorted_ids[i];
1161 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1164 RB_INSERT(got_packidx_bloom_filter_tree,
1165 &repo->packidx_bloom_filters, bf);
1166 return NULL;
1169 const struct got_error *
1170 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1171 struct got_repository *repo, struct got_object_id *id)
1173 const struct got_error *err;
1174 struct got_pathlist_entry *pe;
1175 size_t i;
1177 /* Search pack index cache. */
1178 for (i = 0; i < repo->pack_cache_size; i++) {
1179 if (repo->packidx_cache[i] == NULL)
1180 break;
1181 if (!got_repo_check_packidx_bloom_filter(repo,
1182 repo->packidx_cache[i]->path_packidx, id))
1183 continue; /* object will not be found in this index */
1184 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1185 if (*idx != -1) {
1186 *packidx = repo->packidx_cache[i];
1188 * Move this cache entry to the front. Repeatedly
1189 * searching a wrong pack index can be expensive.
1191 if (i > 0) {
1192 memmove(&repo->packidx_cache[1],
1193 &repo->packidx_cache[0],
1194 i * sizeof(repo->packidx_cache[0]));
1195 repo->packidx_cache[0] = *packidx;
1196 if (repo->pinned_packidx >= 0 &&
1197 repo->pinned_packidx < i)
1198 repo->pinned_packidx++;
1199 else if (repo->pinned_packidx == i)
1200 repo->pinned_packidx = 0;
1202 return NULL;
1205 /* No luck. Search the filesystem. */
1207 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1208 const char *path_packidx = pe->path;
1209 int is_cached = 0;
1211 if (!got_repo_check_packidx_bloom_filter(repo,
1212 pe->path, id))
1213 continue; /* object will not be found in this index */
1215 for (i = 0; i < repo->pack_cache_size; i++) {
1216 if (repo->packidx_cache[i] == NULL)
1217 break;
1218 if (strcmp(repo->packidx_cache[i]->path_packidx,
1219 path_packidx) == 0) {
1220 is_cached = 1;
1221 break;
1224 if (is_cached)
1225 continue; /* already searched */
1227 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1228 path_packidx, 0);
1229 if (err)
1230 goto done;
1232 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1233 if (err)
1234 goto done;
1236 err = cache_packidx(repo, *packidx, path_packidx);
1237 if (err)
1238 goto done;
1240 *idx = got_packidx_get_object_idx(*packidx, id);
1241 if (*idx != -1) {
1242 err = NULL; /* found the object */
1243 goto done;
1247 err = got_error_no_obj(id);
1248 done:
1249 return err;
1252 const struct got_error *
1253 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1254 struct got_repository *repo)
1256 const struct got_error *err = NULL;
1257 DIR *packdir = NULL;
1258 struct dirent *dent;
1259 char *path_packidx = NULL;
1260 int packdir_fd;
1262 packdir_fd = openat(got_repo_get_fd(repo),
1263 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1264 if (packdir_fd == -1) {
1265 return got_error_from_errno_fmt("openat: %s/%s",
1266 got_repo_get_path_git_dir(repo),
1267 GOT_OBJECTS_PACK_DIR);
1270 packdir = fdopendir(packdir_fd);
1271 if (packdir == NULL) {
1272 err = got_error_from_errno("fdopendir");
1273 goto done;
1276 while ((dent = readdir(packdir)) != NULL) {
1277 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1278 continue;
1280 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1281 dent->d_name) == -1) {
1282 err = got_error_from_errno("asprintf");
1283 path_packidx = NULL;
1284 break;
1287 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1288 if (err)
1289 break;
1291 done:
1292 if (err)
1293 free(path_packidx);
1294 if (packdir && closedir(packdir) != 0 && err == NULL)
1295 err = got_error_from_errno("closedir");
1296 return err;
1299 const struct got_error *
1300 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1301 struct got_repository *repo)
1303 const struct got_error *err;
1304 size_t i;
1306 *packidx = NULL;
1308 /* Search pack index cache. */
1309 for (i = 0; i < repo->pack_cache_size; i++) {
1310 if (repo->packidx_cache[i] == NULL)
1311 break;
1312 if (strcmp(repo->packidx_cache[i]->path_packidx,
1313 path_packidx) == 0) {
1314 *packidx = repo->packidx_cache[i];
1315 return NULL;
1318 /* No luck. Search the filesystem. */
1320 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1321 path_packidx, 0);
1322 if (err)
1323 return err;
1325 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1326 if (err)
1327 goto done;
1329 err = cache_packidx(repo, *packidx, path_packidx);
1330 done:
1331 if (err) {
1332 got_packidx_close(*packidx);
1333 *packidx = NULL;
1335 return err;
1338 static const struct got_error *
1339 read_packfile_hdr(int fd, struct got_packidx *packidx)
1341 const struct got_error *err = NULL;
1342 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1343 struct got_packfile_hdr hdr;
1344 ssize_t n;
1346 n = read(fd, &hdr, sizeof(hdr));
1347 if (n < 0)
1348 return got_error_from_errno("read");
1349 if (n != sizeof(hdr))
1350 return got_error(GOT_ERR_BAD_PACKFILE);
1352 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1353 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1354 be32toh(hdr.nobjects) != totobj)
1355 err = got_error(GOT_ERR_BAD_PACKFILE);
1357 return err;
1360 static const struct got_error *
1361 open_packfile(int *fd, struct got_repository *repo,
1362 const char *relpath, struct got_packidx *packidx)
1364 const struct got_error *err = NULL;
1366 *fd = openat(got_repo_get_fd(repo), relpath,
1367 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1368 if (*fd == -1)
1369 return got_error_from_errno_fmt("openat: %s/%s",
1370 got_repo_get_path_git_dir(repo), relpath);
1372 if (packidx) {
1373 err = read_packfile_hdr(*fd, packidx);
1374 if (err) {
1375 close(*fd);
1376 *fd = -1;
1380 return err;
1383 const struct got_error *
1384 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1385 const char *path_packfile, struct got_packidx *packidx)
1387 const struct got_error *err = NULL;
1388 struct got_pack *pack = NULL;
1389 struct stat sb;
1390 size_t i;
1392 if (packp)
1393 *packp = NULL;
1395 for (i = 0; i < repo->pack_cache_size; i++) {
1396 pack = &repo->packs[i];
1397 if (pack->path_packfile == NULL)
1398 break;
1399 if (strcmp(pack->path_packfile, path_packfile) == 0)
1400 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1403 if (i == repo->pack_cache_size) {
1404 struct got_pack tmp;
1405 do {
1406 i--;
1407 } while (i > 0 && repo->pinned_pack >= 0 &&
1408 i == repo->pinned_pack);
1409 err = got_pack_close(&repo->packs[i]);
1410 if (err)
1411 return err;
1412 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1413 return got_error_from_errno("ftruncate");
1414 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1415 return got_error_from_errno("ftruncate");
1416 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1417 memcpy(&repo->packs[i], &repo->packs[0],
1418 sizeof(repo->packs[i]));
1419 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1420 if (repo->pinned_pack == 0)
1421 repo->pinned_pack = i;
1422 else if (repo->pinned_pack == i)
1423 repo->pinned_pack = 0;
1424 i = 0;
1427 pack = &repo->packs[i];
1429 pack->path_packfile = strdup(path_packfile);
1430 if (pack->path_packfile == NULL) {
1431 err = got_error_from_errno("strdup");
1432 goto done;
1435 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1436 if (err)
1437 goto done;
1439 if (fstat(pack->fd, &sb) != 0) {
1440 err = got_error_from_errno("fstat");
1441 goto done;
1443 pack->filesize = sb.st_size;
1445 pack->privsep_child = NULL;
1447 #ifndef GOT_PACK_NO_MMAP
1448 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1449 pack->fd, 0);
1450 if (pack->map == MAP_FAILED) {
1451 if (errno != ENOMEM) {
1452 err = got_error_from_errno("mmap");
1453 goto done;
1455 pack->map = NULL; /* fall back to read(2) */
1457 #endif
1458 done:
1459 if (err) {
1460 if (pack) {
1461 free(pack->path_packfile);
1462 memset(pack, 0, sizeof(*pack));
1464 } else if (packp)
1465 *packp = pack;
1466 return err;
1469 struct got_pack *
1470 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1472 struct got_pack *pack = NULL;
1473 size_t i;
1475 for (i = 0; i < repo->pack_cache_size; i++) {
1476 pack = &repo->packs[i];
1477 if (pack->path_packfile == NULL)
1478 break;
1479 if (strcmp(pack->path_packfile, path_packfile) == 0)
1480 return pack;
1483 return NULL;
1486 const struct got_error *
1487 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1488 struct got_pack *pack)
1490 size_t i;
1491 int pinned_pack = -1, pinned_packidx = -1;
1493 for (i = 0; i < repo->pack_cache_size; i++) {
1494 if (repo->packidx_cache[i] &&
1495 strcmp(repo->packidx_cache[i]->path_packidx,
1496 packidx->path_packidx) == 0)
1497 pinned_packidx = i;
1498 if (repo->packs[i].path_packfile &&
1499 strcmp(repo->packs[i].path_packfile,
1500 pack->path_packfile) == 0)
1501 pinned_pack = i;
1504 if (pinned_packidx == -1 || pinned_pack == -1)
1505 return got_error(GOT_ERR_PIN_PACK);
1507 repo->pinned_pack = pinned_pack;
1508 repo->pinned_packidx = pinned_packidx;
1509 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1510 return NULL;
1513 struct got_pack *
1514 got_repo_get_pinned_pack(struct got_repository *repo)
1516 if (repo->pinned_pack >= 0 &&
1517 repo->pinned_pack < repo->pack_cache_size)
1518 return &repo->packs[repo->pinned_pack];
1520 return NULL;
1523 void
1524 got_repo_unpin_pack(struct got_repository *repo)
1526 repo->pinned_packidx = -1;
1527 repo->pinned_pack = -1;
1528 repo->pinned_pid = 0;
1531 const struct got_error *
1532 got_repo_init(const char *repo_path)
1534 const struct got_error *err = NULL;
1535 const char *dirnames[] = {
1536 GOT_OBJECTS_DIR,
1537 GOT_OBJECTS_PACK_DIR,
1538 GOT_REFS_DIR,
1540 const char *description_str = "Unnamed repository; "
1541 "edit this file 'description' to name the repository.";
1542 const char *headref_str = "ref: refs/heads/main";
1543 const char *gitconfig_str = "[core]\n"
1544 "\trepositoryformatversion = 0\n"
1545 "\tfilemode = true\n"
1546 "\tbare = true\n";
1547 char *path;
1548 size_t i;
1550 if (!got_path_dir_is_empty(repo_path))
1551 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1553 for (i = 0; i < nitems(dirnames); i++) {
1554 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1555 return got_error_from_errno("asprintf");
1557 err = got_path_mkdir(path);
1558 free(path);
1559 if (err)
1560 return err;
1563 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1564 return got_error_from_errno("asprintf");
1565 err = got_path_create_file(path, description_str);
1566 free(path);
1567 if (err)
1568 return err;
1570 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1571 return got_error_from_errno("asprintf");
1572 err = got_path_create_file(path, headref_str);
1573 free(path);
1574 if (err)
1575 return err;
1577 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1578 return got_error_from_errno("asprintf");
1579 err = got_path_create_file(path, gitconfig_str);
1580 free(path);
1581 if (err)
1582 return err;
1584 return NULL;
1587 static const struct got_error *
1588 match_packed_object(struct got_object_id **unique_id,
1589 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1591 const struct got_error *err = NULL;
1592 struct got_object_id_queue matched_ids;
1593 struct got_pathlist_entry *pe;
1595 STAILQ_INIT(&matched_ids);
1597 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1598 const char *path_packidx = pe->path;
1599 struct got_packidx *packidx;
1600 struct got_object_qid *qid;
1602 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1603 path_packidx, 0);
1604 if (err)
1605 break;
1607 err = got_packidx_match_id_str_prefix(&matched_ids,
1608 packidx, id_str_prefix);
1609 if (err) {
1610 got_packidx_close(packidx);
1611 break;
1613 err = got_packidx_close(packidx);
1614 if (err)
1615 break;
1617 STAILQ_FOREACH(qid, &matched_ids, entry) {
1618 if (obj_type != GOT_OBJ_TYPE_ANY) {
1619 int matched_type;
1620 err = got_object_get_type(&matched_type, repo,
1621 &qid->id);
1622 if (err)
1623 goto done;
1624 if (matched_type != obj_type)
1625 continue;
1627 if (*unique_id == NULL) {
1628 *unique_id = got_object_id_dup(&qid->id);
1629 if (*unique_id == NULL) {
1630 err = got_error_from_errno("malloc");
1631 goto done;
1633 } else {
1634 if (got_object_id_cmp(*unique_id,
1635 &qid->id) == 0)
1636 continue; /* packed multiple times */
1637 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1638 goto done;
1642 done:
1643 got_object_id_queue_free(&matched_ids);
1644 if (err) {
1645 free(*unique_id);
1646 *unique_id = NULL;
1648 return err;
1651 static const struct got_error *
1652 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1653 const char *object_dir, const char *id_str_prefix, int obj_type,
1654 struct got_repository *repo)
1656 const struct got_error *err = NULL;
1657 char *path;
1658 DIR *dir = NULL;
1659 struct dirent *dent;
1660 struct got_object_id id;
1662 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1663 err = got_error_from_errno("asprintf");
1664 goto done;
1667 dir = opendir(path);
1668 if (dir == NULL) {
1669 if (errno == ENOENT) {
1670 err = NULL;
1671 goto done;
1673 err = got_error_from_errno2("opendir", path);
1674 goto done;
1676 while ((dent = readdir(dir)) != NULL) {
1677 char *id_str;
1678 int cmp;
1680 if (strcmp(dent->d_name, ".") == 0 ||
1681 strcmp(dent->d_name, "..") == 0)
1682 continue;
1684 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1685 err = got_error_from_errno("asprintf");
1686 goto done;
1689 if (!got_parse_sha1_digest(id.sha1, id_str))
1690 continue;
1693 * Directory entries do not necessarily appear in
1694 * sorted order, so we must iterate over all of them.
1696 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1697 if (cmp != 0) {
1698 free(id_str);
1699 continue;
1702 if (*unique_id == NULL) {
1703 if (obj_type != GOT_OBJ_TYPE_ANY) {
1704 int matched_type;
1705 err = got_object_get_type(&matched_type, repo,
1706 &id);
1707 if (err)
1708 goto done;
1709 if (matched_type != obj_type)
1710 continue;
1712 *unique_id = got_object_id_dup(&id);
1713 if (*unique_id == NULL) {
1714 err = got_error_from_errno("got_object_id_dup");
1715 free(id_str);
1716 goto done;
1718 } else {
1719 if (got_object_id_cmp(*unique_id, &id) == 0)
1720 continue; /* both packed and loose */
1721 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1722 free(id_str);
1723 goto done;
1726 done:
1727 if (dir && closedir(dir) != 0 && err == NULL)
1728 err = got_error_from_errno("closedir");
1729 if (err) {
1730 free(*unique_id);
1731 *unique_id = NULL;
1733 free(path);
1734 return err;
1737 const struct got_error *
1738 got_repo_match_object_id_prefix(struct got_object_id **id,
1739 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1741 const struct got_error *err = NULL;
1742 char *path_objects = got_repo_get_path_objects(repo);
1743 char *object_dir = NULL;
1744 size_t len;
1745 int i;
1747 *id = NULL;
1749 len = strlen(id_str_prefix);
1750 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1751 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1753 for (i = 0; i < len; i++) {
1754 if (isxdigit((unsigned char)id_str_prefix[i]))
1755 continue;
1756 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1759 if (len >= 2) {
1760 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1761 if (err)
1762 goto done;
1763 object_dir = strndup(id_str_prefix, 2);
1764 if (object_dir == NULL) {
1765 err = got_error_from_errno("strdup");
1766 goto done;
1768 err = match_loose_object(id, path_objects, object_dir,
1769 id_str_prefix, obj_type, repo);
1770 } else if (len == 1) {
1771 int i;
1772 for (i = 0; i < 0xf; i++) {
1773 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1774 == -1) {
1775 err = got_error_from_errno("asprintf");
1776 goto done;
1778 err = match_packed_object(id, repo, object_dir,
1779 obj_type);
1780 if (err)
1781 goto done;
1782 err = match_loose_object(id, path_objects, object_dir,
1783 id_str_prefix, obj_type, repo);
1784 if (err)
1785 goto done;
1787 } else {
1788 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1789 goto done;
1791 done:
1792 free(object_dir);
1793 if (err) {
1794 free(*id);
1795 *id = NULL;
1796 } else if (*id == NULL) {
1797 switch (obj_type) {
1798 case GOT_OBJ_TYPE_BLOB:
1799 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1800 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1801 break;
1802 case GOT_OBJ_TYPE_TREE:
1803 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1804 GOT_OBJ_LABEL_TREE, id_str_prefix);
1805 break;
1806 case GOT_OBJ_TYPE_COMMIT:
1807 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1808 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1809 break;
1810 case GOT_OBJ_TYPE_TAG:
1811 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1812 GOT_OBJ_LABEL_TAG, id_str_prefix);
1813 break;
1814 default:
1815 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1816 break;
1820 return err;
1823 const struct got_error *
1824 got_repo_match_object_id(struct got_object_id **id, char **label,
1825 const char *id_str, int obj_type, struct got_reflist_head *refs,
1826 struct got_repository *repo)
1828 const struct got_error *err;
1829 struct got_tag_object *tag;
1830 struct got_reference *ref = NULL;
1832 *id = NULL;
1833 if (label)
1834 *label = NULL;
1836 if (refs) {
1837 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1838 refs, repo);
1839 if (err == NULL) {
1840 *id = got_object_id_dup(
1841 got_object_tag_get_object_id(tag));
1842 if (*id == NULL)
1843 err = got_error_from_errno("got_object_id_dup");
1844 else if (label && asprintf(label, "refs/tags/%s",
1845 got_object_tag_get_name(tag)) == -1) {
1846 err = got_error_from_errno("asprintf");
1847 free(*id);
1848 *id = NULL;
1850 got_object_tag_close(tag);
1851 return err;
1852 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1853 err->code != GOT_ERR_NO_OBJ)
1854 return err;
1857 err = got_ref_open(&ref, repo, id_str, 0);
1858 if (err == NULL) {
1859 err = got_ref_resolve(id, repo, ref);
1860 if (err)
1861 goto done;
1862 if (label) {
1863 *label = strdup(got_ref_get_name(ref));
1864 if (*label == NULL) {
1865 err = got_error_from_errno("strdup");
1866 goto done;
1869 } else {
1870 if (err->code != GOT_ERR_NOT_REF &&
1871 err->code != GOT_ERR_BAD_REF_NAME)
1872 goto done;
1873 err = got_repo_match_object_id_prefix(id, id_str,
1874 obj_type, repo);
1875 if (err) {
1876 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1877 err = got_error_not_ref(id_str);
1878 goto done;
1880 if (label) {
1881 err = got_object_id_str(label, *id);
1882 if (*label == NULL) {
1883 err = got_error_from_errno("strdup");
1884 goto done;
1888 done:
1889 if (ref)
1890 got_ref_close(ref);
1891 return err;
1894 const struct got_error *
1895 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1896 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1898 const struct got_error *err = NULL;
1899 struct got_reflist_entry *re;
1900 struct got_object_id *tag_id;
1901 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1903 *tag = NULL;
1905 TAILQ_FOREACH(re, refs, entry) {
1906 const char *refname;
1907 refname = got_ref_get_name(re->ref);
1908 if (got_ref_is_symbolic(re->ref))
1909 continue;
1910 if (strncmp(refname, "refs/tags/", 10) != 0)
1911 continue;
1912 if (!name_is_absolute)
1913 refname += strlen("refs/tags/");
1914 if (strcmp(refname, name) != 0)
1915 continue;
1916 err = got_ref_resolve(&tag_id, repo, re->ref);
1917 if (err)
1918 break;
1919 err = got_object_open_as_tag(tag, repo, tag_id);
1920 free(tag_id);
1921 if (err)
1922 break;
1923 if (obj_type == GOT_OBJ_TYPE_ANY ||
1924 got_object_tag_get_object_type(*tag) == obj_type)
1925 break;
1926 got_object_tag_close(*tag);
1927 *tag = NULL;
1930 if (err == NULL && *tag == NULL)
1931 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1932 GOT_OBJ_LABEL_TAG, name);
1933 return err;
1936 static const struct got_error *
1937 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1938 const char *name, mode_t mode, struct got_object_id *blob_id)
1940 const struct got_error *err = NULL;
1942 *new_te = NULL;
1944 *new_te = calloc(1, sizeof(**new_te));
1945 if (*new_te == NULL)
1946 return got_error_from_errno("calloc");
1948 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1949 sizeof((*new_te)->name)) {
1950 err = got_error(GOT_ERR_NO_SPACE);
1951 goto done;
1954 if (S_ISLNK(mode)) {
1955 (*new_te)->mode = S_IFLNK;
1956 } else {
1957 (*new_te)->mode = S_IFREG;
1958 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1960 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1961 done:
1962 if (err && *new_te) {
1963 free(*new_te);
1964 *new_te = NULL;
1966 return err;
1969 static const struct got_error *
1970 import_file(struct got_tree_entry **new_te, struct dirent *de,
1971 const char *path, struct got_repository *repo)
1973 const struct got_error *err;
1974 struct got_object_id *blob_id = NULL;
1975 char *filepath;
1976 struct stat sb;
1978 if (asprintf(&filepath, "%s%s%s", path,
1979 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1980 return got_error_from_errno("asprintf");
1982 if (lstat(filepath, &sb) != 0) {
1983 err = got_error_from_errno2("lstat", path);
1984 goto done;
1987 err = got_object_blob_create(&blob_id, filepath, repo);
1988 if (err)
1989 goto done;
1991 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1992 blob_id);
1993 done:
1994 free(filepath);
1995 if (err)
1996 free(blob_id);
1997 return err;
2000 static const struct got_error *
2001 insert_tree_entry(struct got_tree_entry *new_te,
2002 struct got_pathlist_head *paths)
2004 const struct got_error *err = NULL;
2005 struct got_pathlist_entry *new_pe;
2007 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2008 if (err)
2009 return err;
2010 if (new_pe == NULL)
2011 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2012 return NULL;
2015 static const struct got_error *write_tree(struct got_object_id **,
2016 const char *, struct got_pathlist_head *, struct got_repository *,
2017 got_repo_import_cb progress_cb, void *progress_arg);
2019 static const struct got_error *
2020 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2021 const char *path, struct got_pathlist_head *ignores,
2022 struct got_repository *repo,
2023 got_repo_import_cb progress_cb, void *progress_arg)
2025 const struct got_error *err;
2026 struct got_object_id *id = NULL;
2027 char *subdirpath;
2029 if (asprintf(&subdirpath, "%s%s%s", path,
2030 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2031 return got_error_from_errno("asprintf");
2033 (*new_te) = calloc(1, sizeof(**new_te));
2034 if (*new_te == NULL)
2035 return got_error_from_errno("calloc");
2036 (*new_te)->mode = S_IFDIR;
2037 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2038 sizeof((*new_te)->name)) {
2039 err = got_error(GOT_ERR_NO_SPACE);
2040 goto done;
2042 err = write_tree(&id, subdirpath, ignores, repo,
2043 progress_cb, progress_arg);
2044 if (err)
2045 goto done;
2046 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2048 done:
2049 free(id);
2050 free(subdirpath);
2051 if (err) {
2052 free(*new_te);
2053 *new_te = NULL;
2055 return err;
2058 static const struct got_error *
2059 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2060 struct got_pathlist_head *ignores, struct got_repository *repo,
2061 got_repo_import_cb progress_cb, void *progress_arg)
2063 const struct got_error *err = NULL;
2064 DIR *dir;
2065 struct dirent *de;
2066 int nentries;
2067 struct got_tree_entry *new_te = NULL;
2068 struct got_pathlist_head paths;
2069 struct got_pathlist_entry *pe;
2071 *new_tree_id = NULL;
2073 TAILQ_INIT(&paths);
2075 dir = opendir(path_dir);
2076 if (dir == NULL) {
2077 err = got_error_from_errno2("opendir", path_dir);
2078 goto done;
2081 nentries = 0;
2082 while ((de = readdir(dir)) != NULL) {
2083 int ignore = 0;
2084 int type;
2086 if (strcmp(de->d_name, ".") == 0 ||
2087 strcmp(de->d_name, "..") == 0)
2088 continue;
2090 TAILQ_FOREACH(pe, ignores, entry) {
2091 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2092 ignore = 1;
2093 break;
2096 if (ignore)
2097 continue;
2099 err = got_path_dirent_type(&type, path_dir, de);
2100 if (err)
2101 goto done;
2103 if (type == DT_DIR) {
2104 err = import_subdir(&new_te, de, path_dir,
2105 ignores, repo, progress_cb, progress_arg);
2106 if (err) {
2107 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2108 goto done;
2109 err = NULL;
2110 continue;
2112 } else if (type == DT_REG || type == DT_LNK) {
2113 err = import_file(&new_te, de, path_dir, repo);
2114 if (err)
2115 goto done;
2116 } else
2117 continue;
2119 err = insert_tree_entry(new_te, &paths);
2120 if (err)
2121 goto done;
2122 nentries++;
2125 if (TAILQ_EMPTY(&paths)) {
2126 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2127 "cannot create tree without any entries");
2128 goto done;
2131 TAILQ_FOREACH(pe, &paths, entry) {
2132 struct got_tree_entry *te = pe->data;
2133 char *path;
2134 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2135 continue;
2136 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2137 err = got_error_from_errno("asprintf");
2138 goto done;
2140 err = (*progress_cb)(progress_arg, path);
2141 free(path);
2142 if (err)
2143 goto done;
2146 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2147 done:
2148 if (dir)
2149 closedir(dir);
2150 got_pathlist_free(&paths);
2151 return err;
2154 const struct got_error *
2155 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2156 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2157 struct got_repository *repo, got_repo_import_cb progress_cb,
2158 void *progress_arg)
2160 const struct got_error *err;
2161 struct got_object_id *new_tree_id;
2163 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2164 progress_cb, progress_arg);
2165 if (err)
2166 return err;
2168 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2169 author, time(NULL), author, time(NULL), logmsg, repo);
2170 free(new_tree_id);
2171 return err;
2174 const struct got_error *
2175 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2176 struct got_repository *repo)
2178 const struct got_error *err = NULL;
2179 char *path_objects = NULL, *path = NULL;
2180 DIR *dir = NULL;
2181 struct got_object_id id;
2182 int i;
2184 *nobjects = 0;
2185 *ondisk_size = 0;
2187 path_objects = got_repo_get_path_objects(repo);
2188 if (path_objects == NULL)
2189 return got_error_from_errno("got_repo_get_path_objects");
2191 for (i = 0; i <= 0xff; i++) {
2192 struct dirent *dent;
2194 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2195 err = got_error_from_errno("asprintf");
2196 break;
2199 dir = opendir(path);
2200 if (dir == NULL) {
2201 if (errno == ENOENT) {
2202 err = NULL;
2203 continue;
2205 err = got_error_from_errno2("opendir", path);
2206 break;
2209 while ((dent = readdir(dir)) != NULL) {
2210 char *id_str;
2211 int fd;
2212 struct stat sb;
2214 if (strcmp(dent->d_name, ".") == 0 ||
2215 strcmp(dent->d_name, "..") == 0)
2216 continue;
2218 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2219 err = got_error_from_errno("asprintf");
2220 goto done;
2223 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2224 free(id_str);
2225 continue;
2227 free(id_str);
2229 err = got_object_open_loose_fd(&fd, &id, repo);
2230 if (err)
2231 goto done;
2233 if (fstat(fd, &sb) == -1) {
2234 err = got_error_from_errno("fstat");
2235 close(fd);
2236 goto done;
2238 (*nobjects)++;
2239 (*ondisk_size) += sb.st_size;
2241 if (close(fd) == -1) {
2242 err = got_error_from_errno("close");
2243 goto done;
2247 if (closedir(dir) != 0) {
2248 err = got_error_from_errno("closedir");
2249 goto done;
2251 dir = NULL;
2253 free(path);
2254 path = NULL;
2256 done:
2257 if (dir && closedir(dir) != 0 && err == NULL)
2258 err = got_error_from_errno("closedir");
2260 if (err) {
2261 *nobjects = 0;
2262 *ondisk_size = 0;
2264 free(path_objects);
2265 free(path);
2266 return err;
2269 const struct got_error *
2270 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2271 off_t *total_packsize, struct got_repository *repo)
2273 const struct got_error *err = NULL;
2274 DIR *packdir = NULL;
2275 struct dirent *dent;
2276 struct got_packidx *packidx = NULL;
2277 char *path_packidx;
2278 char *path_packfile;
2279 int packdir_fd;
2280 struct stat sb;
2282 *npackfiles = 0;
2283 *nobjects = 0;
2284 *total_packsize = 0;
2286 packdir_fd = openat(got_repo_get_fd(repo),
2287 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2288 if (packdir_fd == -1) {
2289 return got_error_from_errno_fmt("openat: %s/%s",
2290 got_repo_get_path_git_dir(repo),
2291 GOT_OBJECTS_PACK_DIR);
2294 packdir = fdopendir(packdir_fd);
2295 if (packdir == NULL) {
2296 err = got_error_from_errno("fdopendir");
2297 goto done;
2300 while ((dent = readdir(packdir)) != NULL) {
2301 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2302 continue;
2304 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2305 dent->d_name) == -1) {
2306 err = got_error_from_errno("asprintf");
2307 goto done;
2310 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2311 path_packidx, 0);
2312 free(path_packidx);
2313 if (err)
2314 goto done;
2316 if (fstat(packidx->fd, &sb) == -1)
2317 goto done;
2318 *total_packsize += sb.st_size;
2320 err = got_packidx_get_packfile_path(&path_packfile,
2321 packidx->path_packidx);
2322 if (err)
2323 goto done;
2325 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2326 0) == -1) {
2327 free(path_packfile);
2328 goto done;
2330 free(path_packfile);
2331 *total_packsize += sb.st_size;
2333 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2335 (*npackfiles)++;
2337 got_packidx_close(packidx);
2338 packidx = NULL;
2340 done:
2341 if (packidx)
2342 got_packidx_close(packidx);
2343 if (packdir && closedir(packdir) != 0 && err == NULL)
2344 err = got_error_from_errno("closedir");
2345 if (err) {
2346 *npackfiles = 0;
2347 *nobjects = 0;
2348 *total_packsize = 0;
2350 return err;
2353 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2354 got_packidx_bloom_filter_cmp);