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 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
267 pack_fds_tmp[i] = got_opentempfd();
268 if (pack_fds_tmp[i] == -1) {
269 err = got_error_from_errno("got_opentempfd");
270 got_repo_pack_fds_close(pack_fds_tmp);
271 return err;
274 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
275 return err;
278 const struct got_error *
279 got_repo_pack_fds_close(int *pack_fds)
281 const struct got_error *err = NULL;
282 int i;
284 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
285 if (pack_fds[i] == -1)
286 continue;
287 if (close(pack_fds[i]) == -1) {
288 err = got_error_from_errno("close");
289 break;
292 free(pack_fds);
293 return err;
296 const struct got_error *
297 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
298 struct got_object *obj)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->objcache, id, obj);
303 if (err) {
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 err = NULL;
307 return err;
309 obj->refcnt++;
310 #endif
311 return NULL;
314 struct got_object *
315 got_repo_get_cached_object(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
321 const struct got_error *
322 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
323 struct got_tree_object *tree)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->treecache, id, tree);
328 if (err) {
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
331 err = NULL;
332 return err;
334 tree->refcnt++;
335 #endif
336 return NULL;
339 struct got_tree_object *
340 got_repo_get_cached_tree(struct got_repository *repo,
341 struct got_object_id *id)
343 return (struct got_tree_object *)got_object_cache_get(
344 &repo->treecache, id);
347 const struct got_error *
348 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
349 struct got_commit_object *commit)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->commitcache, id, commit);
354 if (err) {
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
357 err = NULL;
358 return err;
360 commit->refcnt++;
361 #endif
362 return NULL;
365 struct got_commit_object *
366 got_repo_get_cached_commit(struct got_repository *repo,
367 struct got_object_id *id)
369 return (struct got_commit_object *)got_object_cache_get(
370 &repo->commitcache, id);
373 const struct got_error *
374 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
375 struct got_tag_object *tag)
377 #ifndef GOT_NO_OBJ_CACHE
378 const struct got_error *err = NULL;
379 err = got_object_cache_add(&repo->tagcache, id, tag);
380 if (err) {
381 if (err->code == GOT_ERR_OBJ_EXISTS ||
382 err->code == GOT_ERR_OBJ_TOO_LARGE)
383 err = NULL;
384 return err;
386 tag->refcnt++;
387 #endif
388 return NULL;
391 struct got_tag_object *
392 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
394 return (struct got_tag_object *)got_object_cache_get(
395 &repo->tagcache, id);
398 const struct got_error *
399 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
400 struct got_raw_object *raw)
402 #ifndef GOT_NO_OBJ_CACHE
403 const struct got_error *err = NULL;
404 err = got_object_cache_add(&repo->rawcache, id, raw);
405 if (err) {
406 if (err->code == GOT_ERR_OBJ_EXISTS ||
407 err->code == GOT_ERR_OBJ_TOO_LARGE)
408 err = NULL;
409 return err;
411 raw->refcnt++;
412 #endif
413 return NULL;
417 struct got_raw_object *
418 got_repo_get_cached_raw_object(struct got_repository *repo,
419 struct got_object_id *id)
421 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
425 static const struct got_error *
426 open_repo(struct got_repository *repo, const char *path)
428 const struct got_error *err = NULL;
430 repo->gitdir_fd = -1;
432 /* bare git repository? */
433 repo->path_git_dir = strdup(path);
434 if (repo->path_git_dir == NULL)
435 return got_error_from_errno("strdup");
436 if (is_git_repo(repo)) {
437 repo->path = strdup(repo->path_git_dir);
438 if (repo->path == NULL) {
439 err = got_error_from_errno("strdup");
440 goto done;
442 repo->gitdir_fd = open(repo->path_git_dir,
443 O_DIRECTORY | O_CLOEXEC);
444 if (repo->gitdir_fd == -1) {
445 err = got_error_from_errno2("open",
446 repo->path_git_dir);
447 goto done;
449 return NULL;
452 /* git repository with working tree? */
453 free(repo->path_git_dir);
454 repo->path_git_dir = NULL;
455 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
456 err = got_error_from_errno("asprintf");
457 goto done;
459 if (is_git_repo(repo)) {
460 repo->path = strdup(path);
461 if (repo->path == NULL) {
462 err = got_error_from_errno("strdup");
463 goto done;
465 repo->gitdir_fd = open(repo->path_git_dir,
466 O_DIRECTORY | O_CLOEXEC);
467 if (repo->gitdir_fd == -1) {
468 err = got_error_from_errno2("open",
469 repo->path_git_dir);
470 goto done;
472 return NULL;
475 err = got_error(GOT_ERR_NOT_GIT_REPO);
476 done:
477 if (err) {
478 free(repo->path);
479 repo->path = NULL;
480 free(repo->path_git_dir);
481 repo->path_git_dir = NULL;
482 if (repo->gitdir_fd != -1)
483 close(repo->gitdir_fd);
484 repo->gitdir_fd = -1;
487 return err;
490 static const struct got_error *
491 parse_gitconfig_file(int *gitconfig_repository_format_version,
492 char **gitconfig_author_name, char **gitconfig_author_email,
493 struct got_remote_repo **remotes, int *nremotes,
494 char **gitconfig_owner, char ***extensions, int *nextensions,
495 const char *gitconfig_path)
497 const struct got_error *err = NULL, *child_err = NULL;
498 int fd = -1;
499 int imsg_fds[2] = { -1, -1 };
500 pid_t pid;
501 struct imsgbuf *ibuf;
503 *gitconfig_repository_format_version = 0;
504 if (extensions)
505 *extensions = NULL;
506 if (nextensions)
507 *nextensions = 0;
508 *gitconfig_author_name = NULL;
509 *gitconfig_author_email = NULL;
510 if (remotes)
511 *remotes = NULL;
512 if (nremotes)
513 *nremotes = 0;
514 if (gitconfig_owner)
515 *gitconfig_owner = NULL;
517 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
518 if (fd == -1) {
519 if (errno == ENOENT)
520 return NULL;
521 return got_error_from_errno2("open", gitconfig_path);
524 ibuf = calloc(1, sizeof(*ibuf));
525 if (ibuf == NULL) {
526 err = got_error_from_errno("calloc");
527 goto done;
530 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
531 err = got_error_from_errno("socketpair");
532 goto done;
535 pid = fork();
536 if (pid == -1) {
537 err = got_error_from_errno("fork");
538 goto done;
539 } else if (pid == 0) {
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
541 gitconfig_path);
542 /* not reached */
545 if (close(imsg_fds[1]) == -1) {
546 err = got_error_from_errno("close");
547 goto done;
549 imsg_fds[1] = -1;
550 imsg_init(ibuf, imsg_fds[0]);
552 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
553 if (err)
554 goto done;
555 fd = -1;
557 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
558 if (err)
559 goto done;
561 err = got_privsep_recv_gitconfig_int(
562 gitconfig_repository_format_version, ibuf);
563 if (err)
564 goto done;
566 if (extensions && nextensions) {
567 err = got_privsep_send_gitconfig_repository_extensions_req(
568 ibuf);
569 if (err)
570 goto done;
571 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
572 if (err)
573 goto done;
574 if (*nextensions > 0) {
575 int i;
576 *extensions = calloc(*nextensions, sizeof(char *));
577 if (*extensions == NULL) {
578 err = got_error_from_errno("calloc");
579 goto done;
581 for (i = 0; i < *nextensions; i++) {
582 char *ext;
583 err = got_privsep_recv_gitconfig_str(&ext,
584 ibuf);
585 if (err)
586 goto done;
587 (*extensions)[i] = ext;
592 err = got_privsep_send_gitconfig_author_name_req(ibuf);
593 if (err)
594 goto done;
596 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_send_gitconfig_author_email_req(ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
605 if (err)
606 goto done;
608 if (remotes && nremotes) {
609 err = got_privsep_send_gitconfig_remotes_req(ibuf);
610 if (err)
611 goto done;
613 err = got_privsep_recv_gitconfig_remotes(remotes,
614 nremotes, ibuf);
615 if (err)
616 goto done;
619 if (gitconfig_owner) {
620 err = got_privsep_send_gitconfig_owner_req(ibuf);
621 if (err)
622 goto done;
623 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
624 if (err)
625 goto done;
628 err = got_privsep_send_stop(imsg_fds[0]);
629 child_err = got_privsep_wait_for_child(pid);
630 if (child_err && err == NULL)
631 err = child_err;
632 done:
633 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
634 err = got_error_from_errno("close");
635 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
636 err = got_error_from_errno("close");
637 if (fd != -1 && close(fd) == -1 && err == NULL)
638 err = got_error_from_errno2("close", gitconfig_path);
639 free(ibuf);
640 return err;
643 static const struct got_error *
644 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
646 const struct got_error *err = NULL;
647 char *repo_gitconfig_path = NULL;
649 if (global_gitconfig_path) {
650 /* Read settings from ~/.gitconfig. */
651 int dummy_repo_version;
652 err = parse_gitconfig_file(&dummy_repo_version,
653 &repo->global_gitconfig_author_name,
654 &repo->global_gitconfig_author_email,
655 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
656 if (err)
657 return err;
660 /* Read repository's .git/config file. */
661 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
662 if (repo_gitconfig_path == NULL)
663 return got_error_from_errno("got_repo_get_path_gitconfig");
665 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
666 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
667 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
668 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
669 repo_gitconfig_path);
670 if (err)
671 goto done;
672 done:
673 free(repo_gitconfig_path);
674 return err;
677 static const struct got_error *
678 read_gotconfig(struct got_repository *repo)
680 const struct got_error *err = NULL;
681 char *gotconfig_path;
683 gotconfig_path = got_repo_get_path_gotconfig(repo);
684 if (gotconfig_path == NULL)
685 return got_error_from_errno("got_repo_get_path_gotconfig");
687 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
688 free(gotconfig_path);
689 return err;
692 /* Supported repository format extensions. */
693 static const char *const repo_extensions[] = {
694 "noop", /* Got supports repository format version 1. */
695 "preciousObjects", /* Supported by gotadmin cleanup. */
696 "worktreeConfig", /* Got does not care about Git work trees. */
697 };
699 const struct got_error *
700 got_repo_open(struct got_repository **repop, const char *path,
701 const char *global_gitconfig_path, int *pack_fds)
703 struct got_repository *repo = NULL;
704 const struct got_error *err = NULL;
705 char *repo_path = NULL;
706 size_t i, j = 0;
707 struct rlimit rl;
709 *repop = NULL;
711 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
712 return got_error_from_errno("getrlimit");
714 repo = calloc(1, sizeof(*repo));
715 if (repo == NULL)
716 return got_error_from_errno("calloc");
718 RB_INIT(&repo->packidx_bloom_filters);
719 TAILQ_INIT(&repo->packidx_paths);
721 for (i = 0; i < nitems(repo->privsep_children); i++) {
722 memset(&repo->privsep_children[i], 0,
723 sizeof(repo->privsep_children[0]));
724 repo->privsep_children[i].imsg_fd = -1;
727 err = got_object_cache_init(&repo->objcache,
728 GOT_OBJECT_CACHE_TYPE_OBJ);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->treecache,
732 GOT_OBJECT_CACHE_TYPE_TREE);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->commitcache,
736 GOT_OBJECT_CACHE_TYPE_COMMIT);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->tagcache,
740 GOT_OBJECT_CACHE_TYPE_TAG);
741 if (err)
742 goto done;
743 err = got_object_cache_init(&repo->rawcache,
744 GOT_OBJECT_CACHE_TYPE_RAW);
745 if (err)
746 goto done;
748 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
749 if (repo->pack_cache_size > rl.rlim_cur / 8)
750 repo->pack_cache_size = rl.rlim_cur / 8;
751 for (i = 0; i < nitems(repo->packs); i++) {
752 if (i < repo->pack_cache_size) {
753 repo->packs[i].basefd = pack_fds[j++];
754 repo->packs[i].accumfd = pack_fds[j++];
755 } else {
756 repo->packs[i].basefd = -1;
757 repo->packs[i].accumfd = -1;
760 repo->pinned_pack = -1;
761 repo->pinned_packidx = -1;
762 repo->pinned_pid = 0;
764 repo_path = realpath(path, NULL);
765 if (repo_path == NULL) {
766 err = got_error_from_errno2("realpath", path);
767 goto done;
770 for (;;) {
771 char *parent_path;
773 err = open_repo(repo, repo_path);
774 if (err == NULL)
775 break;
776 if (err->code != GOT_ERR_NOT_GIT_REPO)
777 goto done;
778 if (repo_path[0] == '/' && repo_path[1] == '\0') {
779 err = got_error(GOT_ERR_NOT_GIT_REPO);
780 goto done;
782 err = got_path_dirname(&parent_path, repo_path);
783 if (err)
784 goto done;
785 free(repo_path);
786 repo_path = parent_path;
789 err = read_gotconfig(repo);
790 if (err)
791 goto done;
793 err = read_gitconfig(repo, global_gitconfig_path);
794 if (err)
795 goto done;
796 if (repo->gitconfig_repository_format_version != 0) {
797 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
798 goto done;
800 for (i = 0; i < repo->nextensions; i++) {
801 char *ext = repo->extensions[i];
802 int j, supported = 0;
803 for (j = 0; j < nitems(repo_extensions); j++) {
804 if (strcmp(ext, repo_extensions[j]) == 0) {
805 supported = 1;
806 break;
809 if (!supported) {
810 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
811 goto done;
815 err = got_repo_list_packidx(&repo->packidx_paths, repo);
816 done:
817 if (err)
818 got_repo_close(repo);
819 else
820 *repop = repo;
821 free(repo_path);
822 return err;
825 const struct got_error *
826 got_repo_close(struct got_repository *repo)
828 const struct got_error *err = NULL, *child_err;
829 struct got_packidx_bloom_filter *bf;
830 struct got_pathlist_entry *pe;
831 size_t i;
833 for (i = 0; i < repo->pack_cache_size; i++) {
834 if (repo->packidx_cache[i] == NULL)
835 break;
836 got_packidx_close(repo->packidx_cache[i]);
839 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
840 &repo->packidx_bloom_filters))) {
841 RB_REMOVE(got_packidx_bloom_filter_tree,
842 &repo->packidx_bloom_filters, bf);
843 free(bf->bloom);
844 free(bf);
847 for (i = 0; i < repo->pack_cache_size; i++)
848 if (repo->packs[i].path_packfile)
849 if (repo->packs[i].path_packfile)
850 got_pack_close(&repo->packs[i]);
852 free(repo->path);
853 free(repo->path_git_dir);
855 got_object_cache_close(&repo->objcache);
856 got_object_cache_close(&repo->treecache);
857 got_object_cache_close(&repo->commitcache);
858 got_object_cache_close(&repo->tagcache);
859 got_object_cache_close(&repo->rawcache);
861 for (i = 0; i < nitems(repo->privsep_children); i++) {
862 if (repo->privsep_children[i].imsg_fd == -1)
863 continue;
864 imsg_clear(repo->privsep_children[i].ibuf);
865 free(repo->privsep_children[i].ibuf);
866 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
867 child_err = got_privsep_wait_for_child(
868 repo->privsep_children[i].pid);
869 if (child_err && err == NULL)
870 err = child_err;
871 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
872 err == NULL)
873 err = got_error_from_errno("close");
876 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
877 err == NULL)
878 err = got_error_from_errno("close");
880 if (repo->gotconfig)
881 got_gotconfig_free(repo->gotconfig);
882 free(repo->gitconfig_author_name);
883 free(repo->gitconfig_author_email);
884 for (i = 0; i < repo->ngitconfig_remotes; i++)
885 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
886 free(repo->gitconfig_remotes);
887 for (i = 0; i < repo->nextensions; i++)
888 free(repo->extensions[i]);
889 free(repo->extensions);
891 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
892 free((void *)pe->path);
893 got_pathlist_free(&repo->packidx_paths);
894 free(repo);
896 return err;
899 void
900 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
902 int i;
904 free(repo->name);
905 repo->name = NULL;
906 free(repo->fetch_url);
907 repo->fetch_url = NULL;
908 free(repo->send_url);
909 repo->send_url = NULL;
910 for (i = 0; i < repo->nfetch_branches; i++)
911 free(repo->fetch_branches[i]);
912 free(repo->fetch_branches);
913 repo->fetch_branches = NULL;
914 repo->nfetch_branches = 0;
915 for (i = 0; i < repo->nsend_branches; i++)
916 free(repo->send_branches[i]);
917 free(repo->send_branches);
918 repo->send_branches = NULL;
919 repo->nsend_branches = 0;
922 const struct got_error *
923 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
924 const char *input_path)
926 const struct got_error *err = NULL;
927 const char *repo_abspath = NULL;
928 size_t repolen, len;
929 char *canonpath, *path = NULL;
931 *in_repo_path = NULL;
933 canonpath = strdup(input_path);
934 if (canonpath == NULL) {
935 err = got_error_from_errno("strdup");
936 goto done;
938 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
939 if (err)
940 goto done;
942 repo_abspath = got_repo_get_path(repo);
944 if (canonpath[0] == '\0') {
945 path = strdup(canonpath);
946 if (path == NULL) {
947 err = got_error_from_errno("strdup");
948 goto done;
950 } else {
951 path = realpath(canonpath, NULL);
952 if (path == NULL) {
953 if (errno != ENOENT) {
954 err = got_error_from_errno2("realpath",
955 canonpath);
956 goto done;
958 /*
959 * Path is not on disk.
960 * Assume it is already relative to repository root.
961 */
962 path = strdup(canonpath);
963 if (path == NULL) {
964 err = got_error_from_errno("strdup");
965 goto done;
969 repolen = strlen(repo_abspath);
970 len = strlen(path);
973 if (strcmp(path, repo_abspath) == 0) {
974 free(path);
975 path = strdup("");
976 if (path == NULL) {
977 err = got_error_from_errno("strdup");
978 goto done;
980 } else if (len > repolen &&
981 got_path_is_child(path, repo_abspath, repolen)) {
982 /* Matched an on-disk path inside repository. */
983 if (got_repo_is_bare(repo)) {
984 /*
985 * Matched an on-disk path inside repository
986 * database. Treat input as repository-relative.
987 */
988 free(path);
989 path = canonpath;
990 canonpath = NULL;
991 } else {
992 char *child;
993 /* Strip common prefix with repository path. */
994 err = got_path_skip_common_ancestor(&child,
995 repo_abspath, path);
996 if (err)
997 goto done;
998 free(path);
999 path = child;
1001 } else {
1003 * Matched unrelated on-disk path.
1004 * Treat input as repository-relative.
1006 free(path);
1007 path = canonpath;
1008 canonpath = NULL;
1012 /* Make in-repository path absolute */
1013 if (path[0] != '/') {
1014 char *abspath;
1015 if (asprintf(&abspath, "/%s", path) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 goto done;
1019 free(path);
1020 path = abspath;
1023 done:
1024 free(canonpath);
1025 if (err)
1026 free(path);
1027 else
1028 *in_repo_path = path;
1029 return err;
1032 static const struct got_error *
1033 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1034 const char *path_packidx)
1036 const struct got_error *err = NULL;
1037 size_t i;
1039 for (i = 0; i < repo->pack_cache_size; i++) {
1040 if (repo->packidx_cache[i] == NULL)
1041 break;
1042 if (strcmp(repo->packidx_cache[i]->path_packidx,
1043 path_packidx) == 0) {
1044 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1047 if (i == repo->pack_cache_size) {
1048 do {
1049 i--;
1050 } while (i > 0 && repo->pinned_packidx >= 0 &&
1051 i == repo->pinned_packidx);
1052 err = got_packidx_close(repo->packidx_cache[i]);
1053 if (err)
1054 return err;
1057 repo->packidx_cache[i] = packidx;
1059 return NULL;
1062 int
1063 got_repo_is_packidx_filename(const char *name, size_t len)
1065 if (len != GOT_PACKIDX_NAMELEN)
1066 return 0;
1068 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1069 return 0;
1071 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1072 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1073 return 0;
1075 return 1;
1078 static struct got_packidx_bloom_filter *
1079 get_packidx_bloom_filter(struct got_repository *repo,
1080 const char *path, size_t path_len)
1082 struct got_packidx_bloom_filter key;
1084 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1085 return NULL; /* XXX */
1086 key.path_len = path_len;
1088 return RB_FIND(got_packidx_bloom_filter_tree,
1089 &repo->packidx_bloom_filters, &key);
1092 int
1093 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1094 const char *path_packidx, struct got_object_id *id)
1096 struct got_packidx_bloom_filter *bf;
1098 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1099 if (bf)
1100 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1102 /* No bloom filter means this pack index must be searched. */
1103 return 1;
1106 static const struct got_error *
1107 add_packidx_bloom_filter(struct got_repository *repo,
1108 struct got_packidx *packidx, const char *path_packidx)
1110 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1111 struct got_packidx_bloom_filter *bf;
1112 size_t len;
1115 * Don't use bloom filters for very large pack index files.
1116 * Large pack files will contain a relatively large fraction
1117 * of our objects so we will likely need to visit them anyway.
1118 * The more objects a pack file contains the higher the probability
1119 * of a false-positive match from the bloom filter. And reading
1120 * all object IDs from a large pack index file can be expensive.
1122 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1123 return NULL;
1125 /* Do we already have a filter for this pack index? */
1126 if (get_packidx_bloom_filter(repo, path_packidx,
1127 strlen(path_packidx)) != NULL)
1128 return NULL;
1130 bf = calloc(1, sizeof(*bf));
1131 if (bf == NULL)
1132 return got_error_from_errno("calloc");
1133 bf->bloom = calloc(1, sizeof(*bf->bloom));
1134 if (bf->bloom == NULL) {
1135 free(bf);
1136 return got_error_from_errno("calloc");
1139 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1140 if (len >= sizeof(bf->path)) {
1141 free(bf->bloom);
1142 free(bf);
1143 return got_error(GOT_ERR_NO_SPACE);
1145 bf->path_len = len;
1147 /* Minimum size supported by our bloom filter is 1000 entries. */
1148 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1149 for (i = 0; i < nobjects; i++) {
1150 struct got_packidx_object_id *id;
1151 id = &packidx->hdr.sorted_ids[i];
1152 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1155 RB_INSERT(got_packidx_bloom_filter_tree,
1156 &repo->packidx_bloom_filters, bf);
1157 return NULL;
1160 const struct got_error *
1161 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1162 struct got_repository *repo, struct got_object_id *id)
1164 const struct got_error *err;
1165 struct got_pathlist_entry *pe;
1166 size_t i;
1168 /* Search pack index cache. */
1169 for (i = 0; i < repo->pack_cache_size; i++) {
1170 if (repo->packidx_cache[i] == NULL)
1171 break;
1172 if (!got_repo_check_packidx_bloom_filter(repo,
1173 repo->packidx_cache[i]->path_packidx, id))
1174 continue; /* object will not be found in this index */
1175 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1176 if (*idx != -1) {
1177 *packidx = repo->packidx_cache[i];
1179 * Move this cache entry to the front. Repeatedly
1180 * searching a wrong pack index can be expensive.
1182 if (i > 0) {
1183 memmove(&repo->packidx_cache[1],
1184 &repo->packidx_cache[0],
1185 i * sizeof(repo->packidx_cache[0]));
1186 repo->packidx_cache[0] = *packidx;
1187 if (repo->pinned_packidx >= 0 &&
1188 repo->pinned_packidx < i)
1189 repo->pinned_packidx++;
1190 else if (repo->pinned_packidx == i)
1191 repo->pinned_packidx = 0;
1193 return NULL;
1196 /* No luck. Search the filesystem. */
1198 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1199 const char *path_packidx = pe->path;
1200 int is_cached = 0;
1202 if (!got_repo_check_packidx_bloom_filter(repo,
1203 pe->path, id))
1204 continue; /* object will not be found in this index */
1206 for (i = 0; i < repo->pack_cache_size; i++) {
1207 if (repo->packidx_cache[i] == NULL)
1208 break;
1209 if (strcmp(repo->packidx_cache[i]->path_packidx,
1210 path_packidx) == 0) {
1211 is_cached = 1;
1212 break;
1215 if (is_cached)
1216 continue; /* already searched */
1218 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1219 path_packidx, 0);
1220 if (err)
1221 goto done;
1223 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1224 if (err)
1225 goto done;
1227 err = cache_packidx(repo, *packidx, path_packidx);
1228 if (err)
1229 goto done;
1231 *idx = got_packidx_get_object_idx(*packidx, id);
1232 if (*idx != -1) {
1233 err = NULL; /* found the object */
1234 goto done;
1238 err = got_error_no_obj(id);
1239 done:
1240 return err;
1243 const struct got_error *
1244 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1245 struct got_repository *repo)
1247 const struct got_error *err = NULL;
1248 DIR *packdir = NULL;
1249 struct dirent *dent;
1250 char *path_packidx = NULL;
1251 int packdir_fd;
1253 packdir_fd = openat(got_repo_get_fd(repo),
1254 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1255 if (packdir_fd == -1) {
1256 return got_error_from_errno_fmt("openat: %s/%s",
1257 got_repo_get_path_git_dir(repo),
1258 GOT_OBJECTS_PACK_DIR);
1261 packdir = fdopendir(packdir_fd);
1262 if (packdir == NULL) {
1263 err = got_error_from_errno("fdopendir");
1264 goto done;
1267 while ((dent = readdir(packdir)) != NULL) {
1268 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1269 continue;
1271 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1272 dent->d_name) == -1) {
1273 err = got_error_from_errno("asprintf");
1274 path_packidx = NULL;
1275 break;
1278 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1279 if (err)
1280 break;
1282 done:
1283 if (err)
1284 free(path_packidx);
1285 if (packdir && closedir(packdir) != 0 && err == NULL)
1286 err = got_error_from_errno("closedir");
1287 return err;
1290 const struct got_error *
1291 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1292 struct got_repository *repo)
1294 const struct got_error *err;
1295 size_t i;
1297 *packidx = NULL;
1299 /* Search pack index cache. */
1300 for (i = 0; i < repo->pack_cache_size; i++) {
1301 if (repo->packidx_cache[i] == NULL)
1302 break;
1303 if (strcmp(repo->packidx_cache[i]->path_packidx,
1304 path_packidx) == 0) {
1305 *packidx = repo->packidx_cache[i];
1306 return NULL;
1309 /* No luck. Search the filesystem. */
1311 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1312 path_packidx, 0);
1313 if (err)
1314 return err;
1316 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1317 if (err)
1318 goto done;
1320 err = cache_packidx(repo, *packidx, path_packidx);
1321 done:
1322 if (err) {
1323 got_packidx_close(*packidx);
1324 *packidx = NULL;
1326 return err;
1329 static const struct got_error *
1330 read_packfile_hdr(int fd, struct got_packidx *packidx)
1332 const struct got_error *err = NULL;
1333 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1334 struct got_packfile_hdr hdr;
1335 ssize_t n;
1337 n = read(fd, &hdr, sizeof(hdr));
1338 if (n < 0)
1339 return got_error_from_errno("read");
1340 if (n != sizeof(hdr))
1341 return got_error(GOT_ERR_BAD_PACKFILE);
1343 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1344 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1345 be32toh(hdr.nobjects) != totobj)
1346 err = got_error(GOT_ERR_BAD_PACKFILE);
1348 return err;
1351 static const struct got_error *
1352 open_packfile(int *fd, struct got_repository *repo,
1353 const char *relpath, struct got_packidx *packidx)
1355 const struct got_error *err = NULL;
1357 *fd = openat(got_repo_get_fd(repo), relpath,
1358 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1359 if (*fd == -1)
1360 return got_error_from_errno_fmt("openat: %s/%s",
1361 got_repo_get_path_git_dir(repo), relpath);
1363 if (packidx) {
1364 err = read_packfile_hdr(*fd, packidx);
1365 if (err) {
1366 close(*fd);
1367 *fd = -1;
1371 return err;
1374 const struct got_error *
1375 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1376 const char *path_packfile, struct got_packidx *packidx)
1378 const struct got_error *err = NULL;
1379 struct got_pack *pack = NULL;
1380 struct stat sb;
1381 size_t i;
1383 if (packp)
1384 *packp = NULL;
1386 for (i = 0; i < repo->pack_cache_size; i++) {
1387 pack = &repo->packs[i];
1388 if (pack->path_packfile == NULL)
1389 break;
1390 if (strcmp(pack->path_packfile, path_packfile) == 0)
1391 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1394 if (i == repo->pack_cache_size) {
1395 struct got_pack tmp;
1396 do {
1397 i--;
1398 } while (i > 0 && repo->pinned_pack >= 0 &&
1399 i == repo->pinned_pack);
1400 err = got_pack_close(&repo->packs[i]);
1401 if (err)
1402 return err;
1403 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1404 return got_error_from_errno("ftruncate");
1405 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1406 return got_error_from_errno("ftruncate");
1407 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1408 memcpy(&repo->packs[i], &repo->packs[0],
1409 sizeof(repo->packs[i]));
1410 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1411 if (repo->pinned_pack == 0)
1412 repo->pinned_pack = i;
1413 else if (repo->pinned_pack == i)
1414 repo->pinned_pack = 0;
1415 i = 0;
1418 pack = &repo->packs[i];
1420 pack->path_packfile = strdup(path_packfile);
1421 if (pack->path_packfile == NULL) {
1422 err = got_error_from_errno("strdup");
1423 goto done;
1426 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1427 if (err)
1428 goto done;
1430 if (fstat(pack->fd, &sb) != 0) {
1431 err = got_error_from_errno("fstat");
1432 goto done;
1434 pack->filesize = sb.st_size;
1436 pack->privsep_child = NULL;
1438 #ifndef GOT_PACK_NO_MMAP
1439 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1440 pack->fd, 0);
1441 if (pack->map == MAP_FAILED) {
1442 if (errno != ENOMEM) {
1443 err = got_error_from_errno("mmap");
1444 goto done;
1446 pack->map = NULL; /* fall back to read(2) */
1448 #endif
1449 done:
1450 if (err) {
1451 if (pack) {
1452 free(pack->path_packfile);
1453 memset(pack, 0, sizeof(*pack));
1455 } else if (packp)
1456 *packp = pack;
1457 return err;
1460 struct got_pack *
1461 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1463 struct got_pack *pack = NULL;
1464 size_t i;
1466 for (i = 0; i < repo->pack_cache_size; i++) {
1467 pack = &repo->packs[i];
1468 if (pack->path_packfile == NULL)
1469 break;
1470 if (strcmp(pack->path_packfile, path_packfile) == 0)
1471 return pack;
1474 return NULL;
1477 const struct got_error *
1478 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1479 struct got_pack *pack)
1481 size_t i;
1482 int pinned_pack = -1, pinned_packidx = -1;
1484 for (i = 0; i < repo->pack_cache_size; i++) {
1485 if (repo->packidx_cache[i] &&
1486 strcmp(repo->packidx_cache[i]->path_packidx,
1487 packidx->path_packidx) == 0)
1488 pinned_packidx = i;
1489 if (repo->packs[i].path_packfile &&
1490 strcmp(repo->packs[i].path_packfile,
1491 pack->path_packfile) == 0)
1492 pinned_pack = i;
1495 if (pinned_packidx == -1 || pinned_pack == -1)
1496 return got_error(GOT_ERR_PIN_PACK);
1498 repo->pinned_pack = pinned_pack;
1499 repo->pinned_packidx = pinned_packidx;
1500 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1501 return NULL;
1504 struct got_pack *
1505 got_repo_get_pinned_pack(struct got_repository *repo)
1507 if (repo->pinned_pack >= 0 &&
1508 repo->pinned_pack < repo->pack_cache_size)
1509 return &repo->packs[repo->pinned_pack];
1511 return NULL;
1514 void
1515 got_repo_unpin_pack(struct got_repository *repo)
1517 repo->pinned_packidx = -1;
1518 repo->pinned_pack = -1;
1519 repo->pinned_pid = 0;
1522 const struct got_error *
1523 got_repo_init(const char *repo_path)
1525 const struct got_error *err = NULL;
1526 const char *dirnames[] = {
1527 GOT_OBJECTS_DIR,
1528 GOT_OBJECTS_PACK_DIR,
1529 GOT_REFS_DIR,
1531 const char *description_str = "Unnamed repository; "
1532 "edit this file 'description' to name the repository.";
1533 const char *headref_str = "ref: refs/heads/main";
1534 const char *gitconfig_str = "[core]\n"
1535 "\trepositoryformatversion = 0\n"
1536 "\tfilemode = true\n"
1537 "\tbare = true\n";
1538 char *path;
1539 size_t i;
1541 if (!got_path_dir_is_empty(repo_path))
1542 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1544 for (i = 0; i < nitems(dirnames); i++) {
1545 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1546 return got_error_from_errno("asprintf");
1548 err = got_path_mkdir(path);
1549 free(path);
1550 if (err)
1551 return err;
1554 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1555 return got_error_from_errno("asprintf");
1556 err = got_path_create_file(path, description_str);
1557 free(path);
1558 if (err)
1559 return err;
1561 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1562 return got_error_from_errno("asprintf");
1563 err = got_path_create_file(path, headref_str);
1564 free(path);
1565 if (err)
1566 return err;
1568 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1569 return got_error_from_errno("asprintf");
1570 err = got_path_create_file(path, gitconfig_str);
1571 free(path);
1572 if (err)
1573 return err;
1575 return NULL;
1578 static const struct got_error *
1579 match_packed_object(struct got_object_id **unique_id,
1580 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1582 const struct got_error *err = NULL;
1583 struct got_object_id_queue matched_ids;
1584 struct got_pathlist_entry *pe;
1586 STAILQ_INIT(&matched_ids);
1588 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1589 const char *path_packidx = pe->path;
1590 struct got_packidx *packidx;
1591 struct got_object_qid *qid;
1593 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1594 path_packidx, 0);
1595 if (err)
1596 break;
1598 err = got_packidx_match_id_str_prefix(&matched_ids,
1599 packidx, id_str_prefix);
1600 if (err) {
1601 got_packidx_close(packidx);
1602 break;
1604 err = got_packidx_close(packidx);
1605 if (err)
1606 break;
1608 STAILQ_FOREACH(qid, &matched_ids, entry) {
1609 if (obj_type != GOT_OBJ_TYPE_ANY) {
1610 int matched_type;
1611 err = got_object_get_type(&matched_type, repo,
1612 &qid->id);
1613 if (err)
1614 goto done;
1615 if (matched_type != obj_type)
1616 continue;
1618 if (*unique_id == NULL) {
1619 *unique_id = got_object_id_dup(&qid->id);
1620 if (*unique_id == NULL) {
1621 err = got_error_from_errno("malloc");
1622 goto done;
1624 } else {
1625 if (got_object_id_cmp(*unique_id,
1626 &qid->id) == 0)
1627 continue; /* packed multiple times */
1628 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1629 goto done;
1633 done:
1634 got_object_id_queue_free(&matched_ids);
1635 if (err) {
1636 free(*unique_id);
1637 *unique_id = NULL;
1639 return err;
1642 static const struct got_error *
1643 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1644 const char *object_dir, const char *id_str_prefix, int obj_type,
1645 struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 char *path;
1649 DIR *dir = NULL;
1650 struct dirent *dent;
1651 struct got_object_id id;
1653 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1654 err = got_error_from_errno("asprintf");
1655 goto done;
1658 dir = opendir(path);
1659 if (dir == NULL) {
1660 if (errno == ENOENT) {
1661 err = NULL;
1662 goto done;
1664 err = got_error_from_errno2("opendir", path);
1665 goto done;
1667 while ((dent = readdir(dir)) != NULL) {
1668 char *id_str;
1669 int cmp;
1671 if (strcmp(dent->d_name, ".") == 0 ||
1672 strcmp(dent->d_name, "..") == 0)
1673 continue;
1675 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 goto done;
1680 if (!got_parse_sha1_digest(id.sha1, id_str))
1681 continue;
1684 * Directory entries do not necessarily appear in
1685 * sorted order, so we must iterate over all of them.
1687 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1688 if (cmp != 0) {
1689 free(id_str);
1690 continue;
1693 if (*unique_id == NULL) {
1694 if (obj_type != GOT_OBJ_TYPE_ANY) {
1695 int matched_type;
1696 err = got_object_get_type(&matched_type, repo,
1697 &id);
1698 if (err)
1699 goto done;
1700 if (matched_type != obj_type)
1701 continue;
1703 *unique_id = got_object_id_dup(&id);
1704 if (*unique_id == NULL) {
1705 err = got_error_from_errno("got_object_id_dup");
1706 free(id_str);
1707 goto done;
1709 } else {
1710 if (got_object_id_cmp(*unique_id, &id) == 0)
1711 continue; /* both packed and loose */
1712 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1713 free(id_str);
1714 goto done;
1717 done:
1718 if (dir && closedir(dir) != 0 && err == NULL)
1719 err = got_error_from_errno("closedir");
1720 if (err) {
1721 free(*unique_id);
1722 *unique_id = NULL;
1724 free(path);
1725 return err;
1728 const struct got_error *
1729 got_repo_match_object_id_prefix(struct got_object_id **id,
1730 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 char *path_objects = got_repo_get_path_objects(repo);
1734 char *object_dir = NULL;
1735 size_t len;
1736 int i;
1738 *id = NULL;
1740 len = strlen(id_str_prefix);
1741 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1742 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1744 for (i = 0; i < len; i++) {
1745 if (isxdigit((unsigned char)id_str_prefix[i]))
1746 continue;
1747 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1750 if (len >= 2) {
1751 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1752 if (err)
1753 goto done;
1754 object_dir = strndup(id_str_prefix, 2);
1755 if (object_dir == NULL) {
1756 err = got_error_from_errno("strdup");
1757 goto done;
1759 err = match_loose_object(id, path_objects, object_dir,
1760 id_str_prefix, obj_type, repo);
1761 } else if (len == 1) {
1762 int i;
1763 for (i = 0; i < 0xf; i++) {
1764 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1765 == -1) {
1766 err = got_error_from_errno("asprintf");
1767 goto done;
1769 err = match_packed_object(id, repo, object_dir,
1770 obj_type);
1771 if (err)
1772 goto done;
1773 err = match_loose_object(id, path_objects, object_dir,
1774 id_str_prefix, obj_type, repo);
1775 if (err)
1776 goto done;
1778 } else {
1779 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1780 goto done;
1782 done:
1783 free(object_dir);
1784 if (err) {
1785 free(*id);
1786 *id = NULL;
1787 } else if (*id == NULL) {
1788 switch (obj_type) {
1789 case GOT_OBJ_TYPE_BLOB:
1790 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1791 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1792 break;
1793 case GOT_OBJ_TYPE_TREE:
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_TREE, id_str_prefix);
1796 break;
1797 case GOT_OBJ_TYPE_COMMIT:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1800 break;
1801 case GOT_OBJ_TYPE_TAG:
1802 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1803 GOT_OBJ_LABEL_TAG, id_str_prefix);
1804 break;
1805 default:
1806 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1807 break;
1811 return err;
1814 const struct got_error *
1815 got_repo_match_object_id(struct got_object_id **id, char **label,
1816 const char *id_str, int obj_type, struct got_reflist_head *refs,
1817 struct got_repository *repo)
1819 const struct got_error *err;
1820 struct got_tag_object *tag;
1821 struct got_reference *ref = NULL;
1823 *id = NULL;
1824 if (label)
1825 *label = NULL;
1827 if (refs) {
1828 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1829 refs, repo);
1830 if (err == NULL) {
1831 *id = got_object_id_dup(
1832 got_object_tag_get_object_id(tag));
1833 if (*id == NULL)
1834 err = got_error_from_errno("got_object_id_dup");
1835 else if (label && asprintf(label, "refs/tags/%s",
1836 got_object_tag_get_name(tag)) == -1) {
1837 err = got_error_from_errno("asprintf");
1838 free(*id);
1839 *id = NULL;
1841 got_object_tag_close(tag);
1842 return err;
1843 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1844 err->code != GOT_ERR_NO_OBJ)
1845 return err;
1848 err = got_ref_open(&ref, repo, id_str, 0);
1849 if (err == NULL) {
1850 err = got_ref_resolve(id, repo, ref);
1851 if (err)
1852 goto done;
1853 if (label) {
1854 *label = strdup(got_ref_get_name(ref));
1855 if (*label == NULL) {
1856 err = got_error_from_errno("strdup");
1857 goto done;
1860 } else {
1861 if (err->code != GOT_ERR_NOT_REF &&
1862 err->code != GOT_ERR_BAD_REF_NAME)
1863 goto done;
1864 err = got_repo_match_object_id_prefix(id, id_str,
1865 obj_type, repo);
1866 if (err) {
1867 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1868 err = got_error_not_ref(id_str);
1869 goto done;
1871 if (label) {
1872 err = got_object_id_str(label, *id);
1873 if (*label == NULL) {
1874 err = got_error_from_errno("strdup");
1875 goto done;
1879 done:
1880 if (ref)
1881 got_ref_close(ref);
1882 return err;
1885 const struct got_error *
1886 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1887 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1889 const struct got_error *err = NULL;
1890 struct got_reflist_entry *re;
1891 struct got_object_id *tag_id;
1892 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1894 *tag = NULL;
1896 TAILQ_FOREACH(re, refs, entry) {
1897 const char *refname;
1898 refname = got_ref_get_name(re->ref);
1899 if (got_ref_is_symbolic(re->ref))
1900 continue;
1901 if (strncmp(refname, "refs/tags/", 10) != 0)
1902 continue;
1903 if (!name_is_absolute)
1904 refname += strlen("refs/tags/");
1905 if (strcmp(refname, name) != 0)
1906 continue;
1907 err = got_ref_resolve(&tag_id, repo, re->ref);
1908 if (err)
1909 break;
1910 err = got_object_open_as_tag(tag, repo, tag_id);
1911 free(tag_id);
1912 if (err)
1913 break;
1914 if (obj_type == GOT_OBJ_TYPE_ANY ||
1915 got_object_tag_get_object_type(*tag) == obj_type)
1916 break;
1917 got_object_tag_close(*tag);
1918 *tag = NULL;
1921 if (err == NULL && *tag == NULL)
1922 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1923 GOT_OBJ_LABEL_TAG, name);
1924 return err;
1927 static const struct got_error *
1928 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1929 const char *name, mode_t mode, struct got_object_id *blob_id)
1931 const struct got_error *err = NULL;
1933 *new_te = NULL;
1935 *new_te = calloc(1, sizeof(**new_te));
1936 if (*new_te == NULL)
1937 return got_error_from_errno("calloc");
1939 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1940 sizeof((*new_te)->name)) {
1941 err = got_error(GOT_ERR_NO_SPACE);
1942 goto done;
1945 if (S_ISLNK(mode)) {
1946 (*new_te)->mode = S_IFLNK;
1947 } else {
1948 (*new_te)->mode = S_IFREG;
1949 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1951 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1952 done:
1953 if (err && *new_te) {
1954 free(*new_te);
1955 *new_te = NULL;
1957 return err;
1960 static const struct got_error *
1961 import_file(struct got_tree_entry **new_te, struct dirent *de,
1962 const char *path, struct got_repository *repo)
1964 const struct got_error *err;
1965 struct got_object_id *blob_id = NULL;
1966 char *filepath;
1967 struct stat sb;
1969 if (asprintf(&filepath, "%s%s%s", path,
1970 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1971 return got_error_from_errno("asprintf");
1973 if (lstat(filepath, &sb) != 0) {
1974 err = got_error_from_errno2("lstat", path);
1975 goto done;
1978 err = got_object_blob_create(&blob_id, filepath, repo);
1979 if (err)
1980 goto done;
1982 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1983 blob_id);
1984 done:
1985 free(filepath);
1986 if (err)
1987 free(blob_id);
1988 return err;
1991 static const struct got_error *
1992 insert_tree_entry(struct got_tree_entry *new_te,
1993 struct got_pathlist_head *paths)
1995 const struct got_error *err = NULL;
1996 struct got_pathlist_entry *new_pe;
1998 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1999 if (err)
2000 return err;
2001 if (new_pe == NULL)
2002 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2003 return NULL;
2006 static const struct got_error *write_tree(struct got_object_id **,
2007 const char *, struct got_pathlist_head *, struct got_repository *,
2008 got_repo_import_cb progress_cb, void *progress_arg);
2010 static const struct got_error *
2011 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2012 const char *path, struct got_pathlist_head *ignores,
2013 struct got_repository *repo,
2014 got_repo_import_cb progress_cb, void *progress_arg)
2016 const struct got_error *err;
2017 struct got_object_id *id = NULL;
2018 char *subdirpath;
2020 if (asprintf(&subdirpath, "%s%s%s", path,
2021 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2022 return got_error_from_errno("asprintf");
2024 (*new_te) = calloc(1, sizeof(**new_te));
2025 if (*new_te == NULL)
2026 return got_error_from_errno("calloc");
2027 (*new_te)->mode = S_IFDIR;
2028 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2029 sizeof((*new_te)->name)) {
2030 err = got_error(GOT_ERR_NO_SPACE);
2031 goto done;
2033 err = write_tree(&id, subdirpath, ignores, repo,
2034 progress_cb, progress_arg);
2035 if (err)
2036 goto done;
2037 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2039 done:
2040 free(id);
2041 free(subdirpath);
2042 if (err) {
2043 free(*new_te);
2044 *new_te = NULL;
2046 return err;
2049 static const struct got_error *
2050 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2051 struct got_pathlist_head *ignores, struct got_repository *repo,
2052 got_repo_import_cb progress_cb, void *progress_arg)
2054 const struct got_error *err = NULL;
2055 DIR *dir;
2056 struct dirent *de;
2057 int nentries;
2058 struct got_tree_entry *new_te = NULL;
2059 struct got_pathlist_head paths;
2060 struct got_pathlist_entry *pe;
2062 *new_tree_id = NULL;
2064 TAILQ_INIT(&paths);
2066 dir = opendir(path_dir);
2067 if (dir == NULL) {
2068 err = got_error_from_errno2("opendir", path_dir);
2069 goto done;
2072 nentries = 0;
2073 while ((de = readdir(dir)) != NULL) {
2074 int ignore = 0;
2075 int type;
2077 if (strcmp(de->d_name, ".") == 0 ||
2078 strcmp(de->d_name, "..") == 0)
2079 continue;
2081 TAILQ_FOREACH(pe, ignores, entry) {
2082 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2083 ignore = 1;
2084 break;
2087 if (ignore)
2088 continue;
2090 err = got_path_dirent_type(&type, path_dir, de);
2091 if (err)
2092 goto done;
2094 if (type == DT_DIR) {
2095 err = import_subdir(&new_te, de, path_dir,
2096 ignores, repo, progress_cb, progress_arg);
2097 if (err) {
2098 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2099 goto done;
2100 err = NULL;
2101 continue;
2103 } else if (type == DT_REG || type == DT_LNK) {
2104 err = import_file(&new_te, de, path_dir, repo);
2105 if (err)
2106 goto done;
2107 } else
2108 continue;
2110 err = insert_tree_entry(new_te, &paths);
2111 if (err)
2112 goto done;
2113 nentries++;
2116 if (TAILQ_EMPTY(&paths)) {
2117 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2118 "cannot create tree without any entries");
2119 goto done;
2122 TAILQ_FOREACH(pe, &paths, entry) {
2123 struct got_tree_entry *te = pe->data;
2124 char *path;
2125 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2126 continue;
2127 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2131 err = (*progress_cb)(progress_arg, path);
2132 free(path);
2133 if (err)
2134 goto done;
2137 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2138 done:
2139 if (dir)
2140 closedir(dir);
2141 got_pathlist_free(&paths);
2142 return err;
2145 const struct got_error *
2146 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2147 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2148 struct got_repository *repo, got_repo_import_cb progress_cb,
2149 void *progress_arg)
2151 const struct got_error *err;
2152 struct got_object_id *new_tree_id;
2154 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2155 progress_cb, progress_arg);
2156 if (err)
2157 return err;
2159 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2160 author, time(NULL), author, time(NULL), logmsg, repo);
2161 free(new_tree_id);
2162 return err;
2165 const struct got_error *
2166 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2167 struct got_repository *repo)
2169 const struct got_error *err = NULL;
2170 char *path_objects = NULL, *path = NULL;
2171 DIR *dir = NULL;
2172 struct got_object_id id;
2173 int i;
2175 *nobjects = 0;
2176 *ondisk_size = 0;
2178 path_objects = got_repo_get_path_objects(repo);
2179 if (path_objects == NULL)
2180 return got_error_from_errno("got_repo_get_path_objects");
2182 for (i = 0; i <= 0xff; i++) {
2183 struct dirent *dent;
2185 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2186 err = got_error_from_errno("asprintf");
2187 break;
2190 dir = opendir(path);
2191 if (dir == NULL) {
2192 if (errno == ENOENT) {
2193 err = NULL;
2194 continue;
2196 err = got_error_from_errno2("opendir", path);
2197 break;
2200 while ((dent = readdir(dir)) != NULL) {
2201 char *id_str;
2202 int fd;
2203 struct stat sb;
2205 if (strcmp(dent->d_name, ".") == 0 ||
2206 strcmp(dent->d_name, "..") == 0)
2207 continue;
2209 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2210 err = got_error_from_errno("asprintf");
2211 goto done;
2214 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2215 free(id_str);
2216 continue;
2218 free(id_str);
2220 err = got_object_open_loose_fd(&fd, &id, repo);
2221 if (err)
2222 goto done;
2224 if (fstat(fd, &sb) == -1) {
2225 err = got_error_from_errno("fstat");
2226 close(fd);
2227 goto done;
2229 (*nobjects)++;
2230 (*ondisk_size) += sb.st_size;
2232 if (close(fd) == -1) {
2233 err = got_error_from_errno("close");
2234 goto done;
2238 if (closedir(dir) != 0) {
2239 err = got_error_from_errno("closedir");
2240 goto done;
2242 dir = NULL;
2244 free(path);
2245 path = NULL;
2247 done:
2248 if (dir && closedir(dir) != 0 && err == NULL)
2249 err = got_error_from_errno("closedir");
2251 if (err) {
2252 *nobjects = 0;
2253 *ondisk_size = 0;
2255 free(path_objects);
2256 free(path);
2257 return err;
2260 const struct got_error *
2261 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2262 off_t *total_packsize, struct got_repository *repo)
2264 const struct got_error *err = NULL;
2265 DIR *packdir = NULL;
2266 struct dirent *dent;
2267 struct got_packidx *packidx = NULL;
2268 char *path_packidx;
2269 char *path_packfile;
2270 int packdir_fd;
2271 struct stat sb;
2273 *npackfiles = 0;
2274 *nobjects = 0;
2275 *total_packsize = 0;
2277 packdir_fd = openat(got_repo_get_fd(repo),
2278 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2279 if (packdir_fd == -1) {
2280 return got_error_from_errno_fmt("openat: %s/%s",
2281 got_repo_get_path_git_dir(repo),
2282 GOT_OBJECTS_PACK_DIR);
2285 packdir = fdopendir(packdir_fd);
2286 if (packdir == NULL) {
2287 err = got_error_from_errno("fdopendir");
2288 goto done;
2291 while ((dent = readdir(packdir)) != NULL) {
2292 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2293 continue;
2295 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2296 dent->d_name) == -1) {
2297 err = got_error_from_errno("asprintf");
2298 goto done;
2301 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2302 path_packidx, 0);
2303 free(path_packidx);
2304 if (err)
2305 goto done;
2307 if (fstat(packidx->fd, &sb) == -1)
2308 goto done;
2309 *total_packsize += sb.st_size;
2311 err = got_packidx_get_packfile_path(&path_packfile,
2312 packidx->path_packidx);
2313 if (err)
2314 goto done;
2316 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2317 0) == -1) {
2318 free(path_packfile);
2319 goto done;
2321 free(path_packfile);
2322 *total_packsize += sb.st_size;
2324 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2326 (*npackfiles)++;
2328 got_packidx_close(packidx);
2329 packidx = NULL;
2331 done:
2332 if (packidx)
2333 got_packidx_close(packidx);
2334 if (packdir && closedir(packdir) != 0 && err == NULL)
2335 err = got_error_from_errno("closedir");
2336 if (err) {
2337 *npackfiles = 0;
2338 *nobjects = 0;
2339 *total_packsize = 0;
2341 return err;
2344 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2345 got_packidx_bloom_filter_cmp);