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"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_gotconfig.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 #endif
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
73 const char *
74 got_repo_get_path(struct got_repository *repo)
75 {
76 return repo->path;
77 }
79 const char *
80 got_repo_get_path_git_dir(struct got_repository *repo)
81 {
82 return repo->path_git_dir;
83 }
85 int
86 got_repo_get_fd(struct got_repository *repo)
87 {
88 return repo->gitdir_fd;
89 }
91 const char *
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_name;
95 }
97 const char *
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 {
100 return repo->gitconfig_author_email;
103 const char *
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
109 const char *
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
115 const char *
116 got_repo_get_gitconfig_owner(struct got_repository *repo)
118 return repo->gitconfig_owner;
121 void
122 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
123 struct got_repository *repo)
125 *extensions = repo->extensions;
126 *nextensions = repo->nextensions;
129 int
130 got_repo_is_bare(struct got_repository *repo)
132 return (strcmp(repo->path, repo->path_git_dir) == 0);
135 static char *
136 get_path_git_child(struct got_repository *repo, const char *basename)
138 char *path_child;
140 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
141 basename) == -1)
142 return NULL;
144 return path_child;
147 char *
148 got_repo_get_path_objects(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_DIR);
153 char *
154 got_repo_get_path_objects_pack(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
159 char *
160 got_repo_get_path_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_REFS_DIR);
165 char *
166 got_repo_get_path_packed_refs(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
171 static char *
172 get_path_head(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_HEAD_FILE);
177 char *
178 got_repo_get_path_gitconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GITCONFIG);
183 char *
184 got_repo_get_path_gotconfig(struct got_repository *repo)
186 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
189 const struct got_gotconfig *
190 got_repo_get_gotconfig(struct got_repository *repo)
192 return repo->gotconfig;
195 void
196 got_repo_get_gitconfig_remotes(int *nremotes,
197 const struct got_remote_repo **remotes, struct got_repository *repo)
199 *nremotes = repo->ngitconfig_remotes;
200 *remotes = repo->gitconfig_remotes;
203 static int
204 is_git_repo(struct got_repository *repo)
206 const char *path_git = got_repo_get_path_git_dir(repo);
207 char *path_objects = got_repo_get_path_objects(repo);
208 char *path_refs = got_repo_get_path_refs(repo);
209 char *path_head = get_path_head(repo);
210 int ret = 0;
211 struct stat sb;
212 struct got_reference *head_ref;
214 if (lstat(path_git, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_objects, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_refs, &sb) == -1)
225 goto done;
226 if (!S_ISDIR(sb.st_mode))
227 goto done;
229 if (lstat(path_head, &sb) == -1)
230 goto done;
231 if (!S_ISREG(sb.st_mode))
232 goto done;
234 /* Check if the HEAD reference can be opened. */
235 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
236 goto done;
237 got_ref_close(head_ref);
239 ret = 1;
240 done:
241 free(path_objects);
242 free(path_refs);
243 free(path_head);
244 return ret;
248 const struct got_error *
249 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
250 struct got_object *obj)
252 #ifndef GOT_NO_OBJ_CACHE
253 const struct got_error *err = NULL;
254 err = got_object_cache_add(&repo->objcache, id, obj);
255 if (err) {
256 if (err->code == GOT_ERR_OBJ_EXISTS ||
257 err->code == GOT_ERR_OBJ_TOO_LARGE)
258 err = NULL;
259 return err;
261 obj->refcnt++;
262 #endif
263 return NULL;
266 struct got_object *
267 got_repo_get_cached_object(struct got_repository *repo,
268 struct got_object_id *id)
270 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
273 const struct got_error *
274 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
275 struct got_tree_object *tree)
277 #ifndef GOT_NO_OBJ_CACHE
278 const struct got_error *err = NULL;
279 err = got_object_cache_add(&repo->treecache, id, tree);
280 if (err) {
281 if (err->code == GOT_ERR_OBJ_EXISTS ||
282 err->code == GOT_ERR_OBJ_TOO_LARGE)
283 err = NULL;
284 return err;
286 tree->refcnt++;
287 #endif
288 return NULL;
291 struct got_tree_object *
292 got_repo_get_cached_tree(struct got_repository *repo,
293 struct got_object_id *id)
295 return (struct got_tree_object *)got_object_cache_get(
296 &repo->treecache, id);
299 const struct got_error *
300 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
301 struct got_commit_object *commit)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->commitcache, id, commit);
306 if (err) {
307 if (err->code == GOT_ERR_OBJ_EXISTS ||
308 err->code == GOT_ERR_OBJ_TOO_LARGE)
309 err = NULL;
310 return err;
312 commit->refcnt++;
313 #endif
314 return NULL;
317 struct got_commit_object *
318 got_repo_get_cached_commit(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_commit_object *)got_object_cache_get(
322 &repo->commitcache, id);
325 const struct got_error *
326 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
327 struct got_tag_object *tag)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->tagcache, id, tag);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tag->refcnt++;
339 #endif
340 return NULL;
343 struct got_tag_object *
344 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
346 return (struct got_tag_object *)got_object_cache_get(
347 &repo->tagcache, id);
350 const struct got_error *
351 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
352 struct got_raw_object *raw)
354 #ifndef GOT_NO_OBJ_CACHE
355 const struct got_error *err = NULL;
356 err = got_object_cache_add(&repo->rawcache, id, raw);
357 if (err) {
358 if (err->code == GOT_ERR_OBJ_EXISTS ||
359 err->code == GOT_ERR_OBJ_TOO_LARGE)
360 err = NULL;
361 return err;
363 raw->refcnt++;
364 #endif
365 return NULL;
369 struct got_raw_object *
370 got_repo_get_cached_raw_object(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
377 static const struct got_error *
378 open_repo(struct got_repository *repo, const char *path)
380 const struct got_error *err = NULL;
382 repo->gitdir_fd = -1;
384 /* bare git repository? */
385 repo->path_git_dir = strdup(path);
386 if (repo->path_git_dir == NULL)
387 return got_error_from_errno("strdup");
388 if (is_git_repo(repo)) {
389 repo->path = strdup(repo->path_git_dir);
390 if (repo->path == NULL) {
391 err = got_error_from_errno("strdup");
392 goto done;
394 repo->gitdir_fd = open(repo->path_git_dir,
395 O_DIRECTORY | O_CLOEXEC);
396 if (repo->gitdir_fd == -1) {
397 err = got_error_from_errno2("open",
398 repo->path_git_dir);
399 goto done;
401 return NULL;
404 /* git repository with working tree? */
405 free(repo->path_git_dir);
406 repo->path_git_dir = NULL;
407 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
408 err = got_error_from_errno("asprintf");
409 goto done;
411 if (is_git_repo(repo)) {
412 repo->path = strdup(path);
413 if (repo->path == NULL) {
414 err = got_error_from_errno("strdup");
415 goto done;
417 repo->gitdir_fd = open(repo->path_git_dir,
418 O_DIRECTORY | O_CLOEXEC);
419 if (repo->gitdir_fd == -1) {
420 err = got_error_from_errno2("open",
421 repo->path_git_dir);
422 goto done;
424 return NULL;
427 err = got_error(GOT_ERR_NOT_GIT_REPO);
428 done:
429 if (err) {
430 free(repo->path);
431 repo->path = NULL;
432 free(repo->path_git_dir);
433 repo->path_git_dir = NULL;
434 if (repo->gitdir_fd != -1)
435 close(repo->gitdir_fd);
436 repo->gitdir_fd = -1;
439 return err;
442 static const struct got_error *
443 parse_gitconfig_file(int *gitconfig_repository_format_version,
444 char **gitconfig_author_name, char **gitconfig_author_email,
445 struct got_remote_repo **remotes, int *nremotes,
446 char **gitconfig_owner, char ***extensions, int *nextensions,
447 const char *gitconfig_path)
449 const struct got_error *err = NULL, *child_err = NULL;
450 int fd = -1;
451 int imsg_fds[2] = { -1, -1 };
452 pid_t pid;
453 struct imsgbuf *ibuf;
455 *gitconfig_repository_format_version = 0;
456 if (extensions)
457 *extensions = NULL;
458 if (nextensions)
459 *nextensions = 0;
460 *gitconfig_author_name = NULL;
461 *gitconfig_author_email = NULL;
462 if (remotes)
463 *remotes = NULL;
464 if (nremotes)
465 *nremotes = 0;
466 if (gitconfig_owner)
467 *gitconfig_owner = NULL;
469 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
470 if (fd == -1) {
471 if (errno == ENOENT)
472 return NULL;
473 return got_error_from_errno2("open", gitconfig_path);
476 ibuf = calloc(1, sizeof(*ibuf));
477 if (ibuf == NULL) {
478 err = got_error_from_errno("calloc");
479 goto done;
482 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
483 err = got_error_from_errno("socketpair");
484 goto done;
487 pid = fork();
488 if (pid == -1) {
489 err = got_error_from_errno("fork");
490 goto done;
491 } else if (pid == 0) {
492 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
493 gitconfig_path);
494 /* not reached */
497 if (close(imsg_fds[1]) == -1) {
498 err = got_error_from_errno("close");
499 goto done;
501 imsg_fds[1] = -1;
502 imsg_init(ibuf, imsg_fds[0]);
504 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
505 if (err)
506 goto done;
507 fd = -1;
509 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
510 if (err)
511 goto done;
513 err = got_privsep_recv_gitconfig_int(
514 gitconfig_repository_format_version, ibuf);
515 if (err)
516 goto done;
518 if (extensions && nextensions) {
519 err = got_privsep_send_gitconfig_repository_extensions_req(
520 ibuf);
521 if (err)
522 goto done;
523 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
524 if (err)
525 goto done;
526 if (*nextensions > 0) {
527 int i;
528 *extensions = calloc(*nextensions, sizeof(char *));
529 if (*extensions == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
533 for (i = 0; i < *nextensions; i++) {
534 char *ext;
535 err = got_privsep_recv_gitconfig_str(&ext,
536 ibuf);
537 if (err)
538 goto done;
539 (*extensions)[i] = ext;
544 err = got_privsep_send_gitconfig_author_name_req(ibuf);
545 if (err)
546 goto done;
548 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
549 if (err)
550 goto done;
552 err = got_privsep_send_gitconfig_author_email_req(ibuf);
553 if (err)
554 goto done;
556 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
557 if (err)
558 goto done;
560 if (remotes && nremotes) {
561 err = got_privsep_send_gitconfig_remotes_req(ibuf);
562 if (err)
563 goto done;
565 err = got_privsep_recv_gitconfig_remotes(remotes,
566 nremotes, ibuf);
567 if (err)
568 goto done;
571 if (gitconfig_owner) {
572 err = got_privsep_send_gitconfig_owner_req(ibuf);
573 if (err)
574 goto done;
575 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
576 if (err)
577 goto done;
580 err = got_privsep_send_stop(imsg_fds[0]);
581 child_err = got_privsep_wait_for_child(pid);
582 if (child_err && err == NULL)
583 err = child_err;
584 done:
585 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
586 err = got_error_from_errno("close");
587 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
588 err = got_error_from_errno("close");
589 if (fd != -1 && close(fd) == -1 && err == NULL)
590 err = got_error_from_errno2("close", gitconfig_path);
591 free(ibuf);
592 return err;
595 static const struct got_error *
596 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
598 const struct got_error *err = NULL;
599 char *repo_gitconfig_path = NULL;
601 if (global_gitconfig_path) {
602 /* Read settings from ~/.gitconfig. */
603 int dummy_repo_version;
604 err = parse_gitconfig_file(&dummy_repo_version,
605 &repo->global_gitconfig_author_name,
606 &repo->global_gitconfig_author_email,
607 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
608 if (err)
609 return err;
612 /* Read repository's .git/config file. */
613 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
614 if (repo_gitconfig_path == NULL)
615 return got_error_from_errno("got_repo_get_path_gitconfig");
617 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
618 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
619 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
620 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
621 repo_gitconfig_path);
622 if (err)
623 goto done;
624 done:
625 free(repo_gitconfig_path);
626 return err;
629 static const struct got_error *
630 read_gotconfig(struct got_repository *repo)
632 const struct got_error *err = NULL;
633 char *gotconfig_path;
635 gotconfig_path = got_repo_get_path_gotconfig(repo);
636 if (gotconfig_path == NULL)
637 return got_error_from_errno("got_repo_get_path_gotconfig");
639 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
640 free(gotconfig_path);
641 return err;
644 /* Supported repository format extensions. */
645 static const char *repo_extensions[] = {
646 "noop", /* Got supports repository format version 1. */
647 "preciousObjects", /* Supported by gotadmin cleanup. */
648 "worktreeConfig", /* Got does not care about Git work trees. */
649 };
651 const struct got_error *
652 got_repo_open(struct got_repository **repop, const char *path,
653 const char *global_gitconfig_path)
655 struct got_repository *repo = NULL;
656 const struct got_error *err = NULL;
657 char *repo_path = NULL;
658 size_t i;
659 struct rlimit rl;
661 *repop = NULL;
663 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
664 return got_error_from_errno("getrlimit");
666 repo = calloc(1, sizeof(*repo));
667 if (repo == NULL) {
668 err = got_error_from_errno("calloc");
669 goto done;
672 RB_INIT(&repo->packidx_bloom_filters);
673 TAILQ_INIT(&repo->packidx_paths);
675 for (i = 0; i < nitems(repo->privsep_children); i++) {
676 memset(&repo->privsep_children[i], 0,
677 sizeof(repo->privsep_children[0]));
678 repo->privsep_children[i].imsg_fd = -1;
681 err = got_object_cache_init(&repo->objcache,
682 GOT_OBJECT_CACHE_TYPE_OBJ);
683 if (err)
684 goto done;
685 err = got_object_cache_init(&repo->treecache,
686 GOT_OBJECT_CACHE_TYPE_TREE);
687 if (err)
688 goto done;
689 err = got_object_cache_init(&repo->commitcache,
690 GOT_OBJECT_CACHE_TYPE_COMMIT);
691 if (err)
692 goto done;
693 err = got_object_cache_init(&repo->tagcache,
694 GOT_OBJECT_CACHE_TYPE_TAG);
695 if (err)
696 goto done;
697 err = got_object_cache_init(&repo->rawcache,
698 GOT_OBJECT_CACHE_TYPE_RAW);
699 if (err)
700 goto done;
702 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
703 if (repo->pack_cache_size > rl.rlim_cur / 8)
704 repo->pack_cache_size = rl.rlim_cur / 8;
706 repo_path = realpath(path, NULL);
707 if (repo_path == NULL) {
708 err = got_error_from_errno2("realpath", path);
709 goto done;
712 for (;;) {
713 char *parent_path;
715 err = open_repo(repo, repo_path);
716 if (err == NULL)
717 break;
718 if (err->code != GOT_ERR_NOT_GIT_REPO)
719 goto done;
720 if (repo_path[0] == '/' && repo_path[1] == '\0') {
721 err = got_error(GOT_ERR_NOT_GIT_REPO);
722 goto done;
724 err = got_path_dirname(&parent_path, repo_path);
725 if (err)
726 goto done;
727 free(repo_path);
728 repo_path = parent_path;
731 err = read_gotconfig(repo);
732 if (err)
733 goto done;
735 err = read_gitconfig(repo, global_gitconfig_path);
736 if (err)
737 goto done;
738 if (repo->gitconfig_repository_format_version != 0)
739 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
740 for (i = 0; i < repo->nextensions; i++) {
741 char *ext = repo->extensions[i];
742 int j, supported = 0;
743 for (j = 0; j < nitems(repo_extensions); j++) {
744 if (strcmp(ext, repo_extensions[j]) == 0) {
745 supported = 1;
746 break;
749 if (!supported) {
750 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
751 goto done;
755 err = got_repo_list_packidx(&repo->packidx_paths, repo);
756 done:
757 if (err)
758 got_repo_close(repo);
759 else
760 *repop = repo;
761 free(repo_path);
762 return err;
765 const struct got_error *
766 got_repo_close(struct got_repository *repo)
768 const struct got_error *err = NULL, *child_err;
769 struct got_packidx_bloom_filter *bf;
770 struct got_pathlist_entry *pe;
771 size_t i;
773 for (i = 0; i < repo->pack_cache_size; i++) {
774 if (repo->packidx_cache[i] == NULL)
775 break;
776 got_packidx_close(repo->packidx_cache[i]);
779 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
780 &repo->packidx_bloom_filters))) {
781 RB_REMOVE(got_packidx_bloom_filter_tree,
782 &repo->packidx_bloom_filters, bf);
783 free(bf->bloom);
784 free(bf);
787 for (i = 0; i < repo->pack_cache_size; i++) {
788 if (repo->packs[i].path_packfile == NULL)
789 break;
790 got_pack_close(&repo->packs[i]);
793 free(repo->path);
794 free(repo->path_git_dir);
796 got_object_cache_close(&repo->objcache);
797 got_object_cache_close(&repo->treecache);
798 got_object_cache_close(&repo->commitcache);
799 got_object_cache_close(&repo->tagcache);
800 got_object_cache_close(&repo->rawcache);
802 for (i = 0; i < nitems(repo->privsep_children); i++) {
803 if (repo->privsep_children[i].imsg_fd == -1)
804 continue;
805 imsg_clear(repo->privsep_children[i].ibuf);
806 free(repo->privsep_children[i].ibuf);
807 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
808 child_err = got_privsep_wait_for_child(
809 repo->privsep_children[i].pid);
810 if (child_err && err == NULL)
811 err = child_err;
812 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
813 err == NULL)
814 err = got_error_from_errno("close");
817 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
818 err == NULL)
819 err = got_error_from_errno("close");
821 if (repo->gotconfig)
822 got_gotconfig_free(repo->gotconfig);
823 free(repo->gitconfig_author_name);
824 free(repo->gitconfig_author_email);
825 for (i = 0; i < repo->ngitconfig_remotes; i++)
826 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
827 free(repo->gitconfig_remotes);
828 for (i = 0; i < repo->nextensions; i++)
829 free(repo->extensions[i]);
830 free(repo->extensions);
832 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
833 free((void *)pe->path);
834 got_pathlist_free(&repo->packidx_paths);
835 free(repo);
837 return err;
840 void
841 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
843 int i;
845 free(repo->name);
846 repo->name = NULL;
847 free(repo->fetch_url);
848 repo->fetch_url = NULL;
849 free(repo->send_url);
850 repo->send_url = NULL;
851 for (i = 0; i < repo->nfetch_branches; i++)
852 free(repo->fetch_branches[i]);
853 free(repo->fetch_branches);
854 repo->fetch_branches = NULL;
855 repo->nfetch_branches = 0;
856 for (i = 0; i < repo->nsend_branches; i++)
857 free(repo->send_branches[i]);
858 free(repo->send_branches);
859 repo->send_branches = NULL;
860 repo->nsend_branches = 0;
863 const struct got_error *
864 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
865 const char *input_path)
867 const struct got_error *err = NULL;
868 const char *repo_abspath = NULL;
869 size_t repolen, len;
870 char *canonpath, *path = NULL;
872 *in_repo_path = NULL;
874 canonpath = strdup(input_path);
875 if (canonpath == NULL) {
876 err = got_error_from_errno("strdup");
877 goto done;
879 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
880 if (err)
881 goto done;
883 repo_abspath = got_repo_get_path(repo);
885 if (canonpath[0] == '\0') {
886 path = strdup(canonpath);
887 if (path == NULL) {
888 err = got_error_from_errno("strdup");
889 goto done;
891 } else {
892 path = realpath(canonpath, NULL);
893 if (path == NULL) {
894 if (errno != ENOENT) {
895 err = got_error_from_errno2("realpath",
896 canonpath);
897 goto done;
899 /*
900 * Path is not on disk.
901 * Assume it is already relative to repository root.
902 */
903 path = strdup(canonpath);
904 if (path == NULL) {
905 err = got_error_from_errno("strdup");
906 goto done;
910 repolen = strlen(repo_abspath);
911 len = strlen(path);
914 if (strcmp(path, repo_abspath) == 0) {
915 free(path);
916 path = strdup("");
917 if (path == NULL) {
918 err = got_error_from_errno("strdup");
919 goto done;
921 } else if (len > repolen &&
922 got_path_is_child(path, repo_abspath, repolen)) {
923 /* Matched an on-disk path inside repository. */
924 if (got_repo_is_bare(repo)) {
925 /*
926 * Matched an on-disk path inside repository
927 * database. Treat input as repository-relative.
928 */
929 free(path);
930 path = canonpath;
931 canonpath = NULL;
932 } else {
933 char *child;
934 /* Strip common prefix with repository path. */
935 err = got_path_skip_common_ancestor(&child,
936 repo_abspath, path);
937 if (err)
938 goto done;
939 free(path);
940 path = child;
942 } else {
943 /*
944 * Matched unrelated on-disk path.
945 * Treat input as repository-relative.
946 */
947 free(path);
948 path = canonpath;
949 canonpath = NULL;
953 /* Make in-repository path absolute */
954 if (path[0] != '/') {
955 char *abspath;
956 if (asprintf(&abspath, "/%s", path) == -1) {
957 err = got_error_from_errno("asprintf");
958 goto done;
960 free(path);
961 path = abspath;
964 done:
965 free(canonpath);
966 if (err)
967 free(path);
968 else
969 *in_repo_path = path;
970 return err;
973 static const struct got_error *
974 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
975 const char *path_packidx)
977 const struct got_error *err = NULL;
978 size_t i;
980 for (i = 0; i < repo->pack_cache_size; i++) {
981 if (repo->packidx_cache[i] == NULL)
982 break;
983 if (strcmp(repo->packidx_cache[i]->path_packidx,
984 path_packidx) == 0) {
985 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
988 if (i == repo->pack_cache_size) {
989 i = repo->pack_cache_size - 1;
990 err = got_packidx_close(repo->packidx_cache[i]);
991 if (err)
992 return err;
995 repo->packidx_cache[i] = packidx;
997 return NULL;
1000 int
1001 got_repo_is_packidx_filename(const char *name, size_t len)
1003 if (len != GOT_PACKIDX_NAMELEN)
1004 return 0;
1006 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1007 return 0;
1009 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1010 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1011 return 0;
1013 return 1;
1016 static struct got_packidx_bloom_filter *
1017 get_packidx_bloom_filter(struct got_repository *repo,
1018 const char *path, size_t path_len)
1020 struct got_packidx_bloom_filter key;
1022 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1023 return NULL; /* XXX */
1024 key.path_len = path_len;
1026 return RB_FIND(got_packidx_bloom_filter_tree,
1027 &repo->packidx_bloom_filters, &key);
1030 int
1031 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1032 const char *path_packidx, struct got_object_id *id)
1034 struct got_packidx_bloom_filter *bf;
1036 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1037 if (bf)
1038 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1040 /* No bloom filter means this pack index must be searched. */
1041 return 1;
1044 static const struct got_error *
1045 add_packidx_bloom_filter(struct got_repository *repo,
1046 struct got_packidx *packidx, const char *path_packidx)
1048 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1049 struct got_packidx_bloom_filter *bf;
1050 size_t len;
1053 * Don't use bloom filters for very large pack index files.
1054 * Large pack files will contain a relatively large fraction
1055 * of our objects so we will likely need to visit them anyway.
1056 * The more objects a pack file contains the higher the probability
1057 * of a false-positive match from the bloom filter. And reading
1058 * all object IDs from a large pack index file can be expensive.
1060 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1061 return NULL;
1063 /* Do we already have a filter for this pack index? */
1064 if (get_packidx_bloom_filter(repo, path_packidx,
1065 strlen(path_packidx)) != NULL)
1066 return NULL;
1068 bf = calloc(1, sizeof(*bf));
1069 if (bf == NULL)
1070 return got_error_from_errno("calloc");
1071 bf->bloom = calloc(1, sizeof(*bf->bloom));
1072 if (bf->bloom == NULL) {
1073 free(bf);
1074 return got_error_from_errno("calloc");
1077 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1078 if (len >= sizeof(bf->path)) {
1079 free(bf->bloom);
1080 free(bf);
1081 return got_error(GOT_ERR_NO_SPACE);
1083 bf->path_len = len;
1085 /* Minimum size supported by our bloom filter is 1000 entries. */
1086 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1087 for (i = 0; i < nobjects; i++) {
1088 struct got_packidx_object_id *id;
1089 id = &packidx->hdr.sorted_ids[i];
1090 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1093 RB_INSERT(got_packidx_bloom_filter_tree,
1094 &repo->packidx_bloom_filters, bf);
1095 return NULL;
1098 const struct got_error *
1099 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1100 struct got_repository *repo, struct got_object_id *id)
1102 const struct got_error *err;
1103 struct got_pathlist_entry *pe;
1104 size_t i;
1106 /* Search pack index cache. */
1107 for (i = 0; i < repo->pack_cache_size; i++) {
1108 if (repo->packidx_cache[i] == NULL)
1109 break;
1110 if (!got_repo_check_packidx_bloom_filter(repo,
1111 repo->packidx_cache[i]->path_packidx, id))
1112 continue; /* object will not be found in this index */
1113 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1114 if (*idx != -1) {
1115 *packidx = repo->packidx_cache[i];
1117 * Move this cache entry to the front. Repeatedly
1118 * searching a wrong pack index can be expensive.
1120 if (i > 0) {
1121 memmove(&repo->packidx_cache[1],
1122 &repo->packidx_cache[0],
1123 i * sizeof(repo->packidx_cache[0]));
1124 repo->packidx_cache[0] = *packidx;
1126 return NULL;
1129 /* No luck. Search the filesystem. */
1131 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1132 const char *path_packidx = pe->path;
1133 int is_cached = 0;
1135 if (!got_repo_check_packidx_bloom_filter(repo,
1136 pe->path, id))
1137 continue; /* object will not be found in this index */
1139 for (i = 0; i < repo->pack_cache_size; i++) {
1140 if (repo->packidx_cache[i] == NULL)
1141 break;
1142 if (strcmp(repo->packidx_cache[i]->path_packidx,
1143 path_packidx) == 0) {
1144 is_cached = 1;
1145 break;
1148 if (is_cached)
1149 continue; /* already searched */
1151 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1152 path_packidx, 0);
1153 if (err)
1154 goto done;
1156 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1157 if (err)
1158 goto done;
1160 err = cache_packidx(repo, *packidx, path_packidx);
1161 if (err)
1162 goto done;
1164 *idx = got_packidx_get_object_idx(*packidx, id);
1165 if (*idx != -1) {
1166 err = NULL; /* found the object */
1167 goto done;
1171 err = got_error_no_obj(id);
1172 done:
1173 return err;
1176 const struct got_error *
1177 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1178 struct got_repository *repo)
1180 const struct got_error *err = NULL;
1181 DIR *packdir = NULL;
1182 struct dirent *dent;
1183 char *path_packidx = NULL;
1184 int packdir_fd;
1186 packdir_fd = openat(got_repo_get_fd(repo),
1187 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1188 if (packdir_fd == -1) {
1189 return got_error_from_errno_fmt("openat: %s/%s",
1190 got_repo_get_path_git_dir(repo),
1191 GOT_OBJECTS_PACK_DIR);
1194 packdir = fdopendir(packdir_fd);
1195 if (packdir == NULL) {
1196 err = got_error_from_errno("fdopendir");
1197 goto done;
1200 while ((dent = readdir(packdir)) != NULL) {
1201 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1202 continue;
1204 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1205 dent->d_name) == -1) {
1206 err = got_error_from_errno("asprintf");
1207 path_packidx = NULL;
1208 break;
1211 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1212 if (err)
1213 break;
1215 done:
1216 if (err)
1217 free(path_packidx);
1218 if (packdir && closedir(packdir) != 0 && err == NULL)
1219 err = got_error_from_errno("closedir");
1220 return err;
1223 const struct got_error *
1224 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1225 struct got_repository *repo)
1227 const struct got_error *err;
1228 size_t i;
1230 *packidx = NULL;
1232 /* Search pack index cache. */
1233 for (i = 0; i < repo->pack_cache_size; i++) {
1234 if (repo->packidx_cache[i] == NULL)
1235 break;
1236 if (strcmp(repo->packidx_cache[i]->path_packidx,
1237 path_packidx) == 0) {
1238 *packidx = repo->packidx_cache[i];
1239 return NULL;
1242 /* No luck. Search the filesystem. */
1244 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1245 path_packidx, 0);
1246 if (err)
1247 return err;
1249 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1250 if (err)
1251 goto done;
1253 err = cache_packidx(repo, *packidx, path_packidx);
1254 done:
1255 if (err) {
1256 got_packidx_close(*packidx);
1257 *packidx = NULL;
1259 return err;
1262 static const struct got_error *
1263 read_packfile_hdr(int fd, struct got_packidx *packidx)
1265 const struct got_error *err = NULL;
1266 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1267 struct got_packfile_hdr hdr;
1268 ssize_t n;
1270 n = read(fd, &hdr, sizeof(hdr));
1271 if (n < 0)
1272 return got_error_from_errno("read");
1273 if (n != sizeof(hdr))
1274 return got_error(GOT_ERR_BAD_PACKFILE);
1276 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1277 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1278 be32toh(hdr.nobjects) != totobj)
1279 err = got_error(GOT_ERR_BAD_PACKFILE);
1281 return err;
1284 static const struct got_error *
1285 open_packfile(int *fd, struct got_repository *repo,
1286 const char *relpath, struct got_packidx *packidx)
1288 const struct got_error *err = NULL;
1290 *fd = openat(got_repo_get_fd(repo), relpath,
1291 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1292 if (*fd == -1)
1293 return got_error_from_errno_fmt("openat: %s/%s",
1294 got_repo_get_path_git_dir(repo), relpath);
1296 if (packidx) {
1297 err = read_packfile_hdr(*fd, packidx);
1298 if (err) {
1299 close(*fd);
1300 *fd = -1;
1304 return err;
1307 const struct got_error *
1308 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1309 const char *path_packfile, struct got_packidx *packidx)
1311 const struct got_error *err = NULL;
1312 struct got_pack *pack = NULL;
1313 struct stat sb;
1314 size_t i;
1316 if (packp)
1317 *packp = NULL;
1319 for (i = 0; i < repo->pack_cache_size; i++) {
1320 pack = &repo->packs[i];
1321 if (pack->path_packfile == NULL)
1322 break;
1323 if (strcmp(pack->path_packfile, path_packfile) == 0)
1324 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1327 if (i == repo->pack_cache_size) {
1328 err = got_pack_close(&repo->packs[i - 1]);
1329 if (err)
1330 return err;
1331 memmove(&repo->packs[1], &repo->packs[0],
1332 sizeof(repo->packs) - sizeof(repo->packs[0]));
1333 i = 0;
1336 pack = &repo->packs[i];
1338 pack->path_packfile = strdup(path_packfile);
1339 if (pack->path_packfile == NULL) {
1340 err = got_error_from_errno("strdup");
1341 goto done;
1344 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1345 if (err)
1346 goto done;
1348 if (fstat(pack->fd, &sb) != 0) {
1349 err = got_error_from_errno("fstat");
1350 goto done;
1352 pack->filesize = sb.st_size;
1354 pack->privsep_child = NULL;
1356 #ifndef GOT_PACK_NO_MMAP
1357 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1358 pack->fd, 0);
1359 if (pack->map == MAP_FAILED) {
1360 if (errno != ENOMEM) {
1361 err = got_error_from_errno("mmap");
1362 goto done;
1364 pack->map = NULL; /* fall back to read(2) */
1366 #endif
1367 done:
1368 if (err) {
1369 if (pack) {
1370 free(pack->path_packfile);
1371 memset(pack, 0, sizeof(*pack));
1373 } else if (packp)
1374 *packp = pack;
1375 return err;
1378 struct got_pack *
1379 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1381 struct got_pack *pack = NULL;
1382 size_t i;
1384 for (i = 0; i < repo->pack_cache_size; i++) {
1385 pack = &repo->packs[i];
1386 if (pack->path_packfile == NULL)
1387 break;
1388 if (strcmp(pack->path_packfile, path_packfile) == 0)
1389 return pack;
1392 return NULL;
1395 const struct got_error *
1396 got_repo_init(const char *repo_path)
1398 const struct got_error *err = NULL;
1399 const char *dirnames[] = {
1400 GOT_OBJECTS_DIR,
1401 GOT_OBJECTS_PACK_DIR,
1402 GOT_REFS_DIR,
1404 const char *description_str = "Unnamed repository; "
1405 "edit this file 'description' to name the repository.";
1406 const char *headref_str = "ref: refs/heads/main";
1407 const char *gitconfig_str = "[core]\n"
1408 "\trepositoryformatversion = 0\n"
1409 "\tfilemode = true\n"
1410 "\tbare = true\n";
1411 char *path;
1412 size_t i;
1414 if (!got_path_dir_is_empty(repo_path))
1415 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1417 for (i = 0; i < nitems(dirnames); i++) {
1418 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1419 return got_error_from_errno("asprintf");
1421 err = got_path_mkdir(path);
1422 free(path);
1423 if (err)
1424 return err;
1427 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1428 return got_error_from_errno("asprintf");
1429 err = got_path_create_file(path, description_str);
1430 free(path);
1431 if (err)
1432 return err;
1434 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1435 return got_error_from_errno("asprintf");
1436 err = got_path_create_file(path, headref_str);
1437 free(path);
1438 if (err)
1439 return err;
1441 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1442 return got_error_from_errno("asprintf");
1443 err = got_path_create_file(path, gitconfig_str);
1444 free(path);
1445 if (err)
1446 return err;
1448 return NULL;
1451 static const struct got_error *
1452 match_packed_object(struct got_object_id **unique_id,
1453 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1455 const struct got_error *err = NULL;
1456 struct got_object_id_queue matched_ids;
1457 struct got_pathlist_entry *pe;
1459 STAILQ_INIT(&matched_ids);
1461 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1462 const char *path_packidx = pe->path;
1463 struct got_packidx *packidx;
1464 struct got_object_qid *qid;
1466 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1467 path_packidx, 0);
1468 if (err)
1469 break;
1471 err = got_packidx_match_id_str_prefix(&matched_ids,
1472 packidx, id_str_prefix);
1473 if (err) {
1474 got_packidx_close(packidx);
1475 break;
1477 err = got_packidx_close(packidx);
1478 if (err)
1479 break;
1481 STAILQ_FOREACH(qid, &matched_ids, entry) {
1482 if (obj_type != GOT_OBJ_TYPE_ANY) {
1483 int matched_type;
1484 err = got_object_get_type(&matched_type, repo,
1485 qid->id);
1486 if (err)
1487 goto done;
1488 if (matched_type != obj_type)
1489 continue;
1491 if (*unique_id == NULL) {
1492 *unique_id = got_object_id_dup(qid->id);
1493 if (*unique_id == NULL) {
1494 err = got_error_from_errno("malloc");
1495 goto done;
1497 } else {
1498 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1499 continue; /* packed multiple times */
1500 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1501 goto done;
1505 done:
1506 got_object_id_queue_free(&matched_ids);
1507 if (err) {
1508 free(*unique_id);
1509 *unique_id = NULL;
1511 return err;
1514 static const struct got_error *
1515 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1516 const char *object_dir, const char *id_str_prefix, int obj_type,
1517 struct got_repository *repo)
1519 const struct got_error *err = NULL;
1520 char *path;
1521 DIR *dir = NULL;
1522 struct dirent *dent;
1523 struct got_object_id id;
1525 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1526 err = got_error_from_errno("asprintf");
1527 goto done;
1530 dir = opendir(path);
1531 if (dir == NULL) {
1532 if (errno == ENOENT) {
1533 err = NULL;
1534 goto done;
1536 err = got_error_from_errno2("opendir", path);
1537 goto done;
1539 while ((dent = readdir(dir)) != NULL) {
1540 char *id_str;
1541 int cmp;
1543 if (strcmp(dent->d_name, ".") == 0 ||
1544 strcmp(dent->d_name, "..") == 0)
1545 continue;
1547 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1548 err = got_error_from_errno("asprintf");
1549 goto done;
1552 if (!got_parse_sha1_digest(id.sha1, id_str))
1553 continue;
1556 * Directory entries do not necessarily appear in
1557 * sorted order, so we must iterate over all of them.
1559 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1560 if (cmp != 0) {
1561 free(id_str);
1562 continue;
1565 if (*unique_id == NULL) {
1566 if (obj_type != GOT_OBJ_TYPE_ANY) {
1567 int matched_type;
1568 err = got_object_get_type(&matched_type, repo,
1569 &id);
1570 if (err)
1571 goto done;
1572 if (matched_type != obj_type)
1573 continue;
1575 *unique_id = got_object_id_dup(&id);
1576 if (*unique_id == NULL) {
1577 err = got_error_from_errno("got_object_id_dup");
1578 free(id_str);
1579 goto done;
1581 } else {
1582 if (got_object_id_cmp(*unique_id, &id) == 0)
1583 continue; /* both packed and loose */
1584 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1585 free(id_str);
1586 goto done;
1589 done:
1590 if (dir && closedir(dir) != 0 && err == NULL)
1591 err = got_error_from_errno("closedir");
1592 if (err) {
1593 free(*unique_id);
1594 *unique_id = NULL;
1596 free(path);
1597 return err;
1600 const struct got_error *
1601 got_repo_match_object_id_prefix(struct got_object_id **id,
1602 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1604 const struct got_error *err = NULL;
1605 char *path_objects = got_repo_get_path_objects(repo);
1606 char *object_dir = NULL;
1607 size_t len;
1608 int i;
1610 *id = NULL;
1612 len = strlen(id_str_prefix);
1613 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1614 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1616 for (i = 0; i < len; i++) {
1617 if (isxdigit((unsigned char)id_str_prefix[i]))
1618 continue;
1619 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1622 if (len >= 2) {
1623 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1624 if (err)
1625 goto done;
1626 object_dir = strndup(id_str_prefix, 2);
1627 if (object_dir == NULL) {
1628 err = got_error_from_errno("strdup");
1629 goto done;
1631 err = match_loose_object(id, path_objects, object_dir,
1632 id_str_prefix, obj_type, repo);
1633 } else if (len == 1) {
1634 int i;
1635 for (i = 0; i < 0xf; i++) {
1636 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1637 == -1) {
1638 err = got_error_from_errno("asprintf");
1639 goto done;
1641 err = match_packed_object(id, repo, object_dir,
1642 obj_type);
1643 if (err)
1644 goto done;
1645 err = match_loose_object(id, path_objects, object_dir,
1646 id_str_prefix, obj_type, repo);
1647 if (err)
1648 goto done;
1650 } else {
1651 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1652 goto done;
1654 done:
1655 free(object_dir);
1656 if (err) {
1657 free(*id);
1658 *id = NULL;
1659 } else if (*id == NULL) {
1660 switch (obj_type) {
1661 case GOT_OBJ_TYPE_BLOB:
1662 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1663 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1664 break;
1665 case GOT_OBJ_TYPE_TREE:
1666 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1667 GOT_OBJ_LABEL_TREE, id_str_prefix);
1668 break;
1669 case GOT_OBJ_TYPE_COMMIT:
1670 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1671 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1672 break;
1673 case GOT_OBJ_TYPE_TAG:
1674 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1675 GOT_OBJ_LABEL_TAG, id_str_prefix);
1676 break;
1677 default:
1678 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1679 break;
1683 return err;
1686 const struct got_error *
1687 got_repo_match_object_id(struct got_object_id **id, char **label,
1688 const char *id_str, int obj_type, struct got_reflist_head *refs,
1689 struct got_repository *repo)
1691 const struct got_error *err;
1692 struct got_tag_object *tag;
1693 struct got_reference *ref = NULL;
1695 *id = NULL;
1696 if (label)
1697 *label = NULL;
1699 if (refs) {
1700 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1701 refs, repo);
1702 if (err == NULL) {
1703 *id = got_object_id_dup(
1704 got_object_tag_get_object_id(tag));
1705 if (*id == NULL)
1706 err = got_error_from_errno("got_object_id_dup");
1707 else if (label && asprintf(label, "refs/tags/%s",
1708 got_object_tag_get_name(tag)) == -1) {
1709 err = got_error_from_errno("asprintf");
1710 free(*id);
1711 *id = NULL;
1713 got_object_tag_close(tag);
1714 return err;
1715 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1716 err->code != GOT_ERR_NO_OBJ)
1717 return err;
1720 err = got_ref_open(&ref, repo, id_str, 0);
1721 if (err == NULL) {
1722 err = got_ref_resolve(id, repo, ref);
1723 if (err)
1724 goto done;
1725 if (label) {
1726 *label = strdup(got_ref_get_name(ref));
1727 if (*label == NULL) {
1728 err = got_error_from_errno("strdup");
1729 goto done;
1732 } else {
1733 if (err->code != GOT_ERR_NOT_REF &&
1734 err->code != GOT_ERR_BAD_REF_NAME)
1735 goto done;
1736 err = got_repo_match_object_id_prefix(id, id_str,
1737 obj_type, repo);
1738 if (err) {
1739 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1740 err = got_error_not_ref(id_str);
1741 goto done;
1743 if (label) {
1744 err = got_object_id_str(label, *id);
1745 if (*label == NULL) {
1746 err = got_error_from_errno("strdup");
1747 goto done;
1751 done:
1752 if (ref)
1753 got_ref_close(ref);
1754 return err;
1757 const struct got_error *
1758 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1759 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1761 const struct got_error *err = NULL;
1762 struct got_reflist_entry *re;
1763 struct got_object_id *tag_id;
1764 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1766 *tag = NULL;
1768 TAILQ_FOREACH(re, refs, entry) {
1769 const char *refname;
1770 refname = got_ref_get_name(re->ref);
1771 if (got_ref_is_symbolic(re->ref))
1772 continue;
1773 if (strncmp(refname, "refs/tags/", 10) != 0)
1774 continue;
1775 if (!name_is_absolute)
1776 refname += strlen("refs/tags/");
1777 if (strcmp(refname, name) != 0)
1778 continue;
1779 err = got_ref_resolve(&tag_id, repo, re->ref);
1780 if (err)
1781 break;
1782 err = got_object_open_as_tag(tag, repo, tag_id);
1783 free(tag_id);
1784 if (err)
1785 break;
1786 if (obj_type == GOT_OBJ_TYPE_ANY ||
1787 got_object_tag_get_object_type(*tag) == obj_type)
1788 break;
1789 got_object_tag_close(*tag);
1790 *tag = NULL;
1793 if (err == NULL && *tag == NULL)
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_TAG, name);
1796 return err;
1799 static const struct got_error *
1800 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1801 const char *name, mode_t mode, struct got_object_id *blob_id)
1803 const struct got_error *err = NULL;
1805 *new_te = NULL;
1807 *new_te = calloc(1, sizeof(**new_te));
1808 if (*new_te == NULL)
1809 return got_error_from_errno("calloc");
1811 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1812 sizeof((*new_te)->name)) {
1813 err = got_error(GOT_ERR_NO_SPACE);
1814 goto done;
1817 if (S_ISLNK(mode)) {
1818 (*new_te)->mode = S_IFLNK;
1819 } else {
1820 (*new_te)->mode = S_IFREG;
1821 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1823 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1824 done:
1825 if (err && *new_te) {
1826 free(*new_te);
1827 *new_te = NULL;
1829 return err;
1832 static const struct got_error *
1833 import_file(struct got_tree_entry **new_te, struct dirent *de,
1834 const char *path, struct got_repository *repo)
1836 const struct got_error *err;
1837 struct got_object_id *blob_id = NULL;
1838 char *filepath;
1839 struct stat sb;
1841 if (asprintf(&filepath, "%s%s%s", path,
1842 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1843 return got_error_from_errno("asprintf");
1845 if (lstat(filepath, &sb) != 0) {
1846 err = got_error_from_errno2("lstat", path);
1847 goto done;
1850 err = got_object_blob_create(&blob_id, filepath, repo);
1851 if (err)
1852 goto done;
1854 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1855 blob_id);
1856 done:
1857 free(filepath);
1858 if (err)
1859 free(blob_id);
1860 return err;
1863 static const struct got_error *
1864 insert_tree_entry(struct got_tree_entry *new_te,
1865 struct got_pathlist_head *paths)
1867 const struct got_error *err = NULL;
1868 struct got_pathlist_entry *new_pe;
1870 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1871 if (err)
1872 return err;
1873 if (new_pe == NULL)
1874 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1875 return NULL;
1878 static const struct got_error *write_tree(struct got_object_id **,
1879 const char *, struct got_pathlist_head *, struct got_repository *,
1880 got_repo_import_cb progress_cb, void *progress_arg);
1882 static const struct got_error *
1883 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1884 const char *path, struct got_pathlist_head *ignores,
1885 struct got_repository *repo,
1886 got_repo_import_cb progress_cb, void *progress_arg)
1888 const struct got_error *err;
1889 struct got_object_id *id = NULL;
1890 char *subdirpath;
1892 if (asprintf(&subdirpath, "%s%s%s", path,
1893 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1894 return got_error_from_errno("asprintf");
1896 (*new_te) = calloc(1, sizeof(**new_te));
1897 if (*new_te == NULL)
1898 return got_error_from_errno("calloc");
1899 (*new_te)->mode = S_IFDIR;
1900 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1901 sizeof((*new_te)->name)) {
1902 err = got_error(GOT_ERR_NO_SPACE);
1903 goto done;
1905 err = write_tree(&id, subdirpath, ignores, repo,
1906 progress_cb, progress_arg);
1907 if (err)
1908 goto done;
1909 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1911 done:
1912 free(id);
1913 free(subdirpath);
1914 if (err) {
1915 free(*new_te);
1916 *new_te = NULL;
1918 return err;
1921 static const struct got_error *
1922 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1923 struct got_pathlist_head *ignores, struct got_repository *repo,
1924 got_repo_import_cb progress_cb, void *progress_arg)
1926 const struct got_error *err = NULL;
1927 DIR *dir;
1928 struct dirent *de;
1929 int nentries;
1930 struct got_tree_entry *new_te = NULL;
1931 struct got_pathlist_head paths;
1932 struct got_pathlist_entry *pe;
1934 *new_tree_id = NULL;
1936 TAILQ_INIT(&paths);
1938 dir = opendir(path_dir);
1939 if (dir == NULL) {
1940 err = got_error_from_errno2("opendir", path_dir);
1941 goto done;
1944 nentries = 0;
1945 while ((de = readdir(dir)) != NULL) {
1946 int ignore = 0;
1947 int type;
1949 if (strcmp(de->d_name, ".") == 0 ||
1950 strcmp(de->d_name, "..") == 0)
1951 continue;
1953 TAILQ_FOREACH(pe, ignores, entry) {
1954 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1955 ignore = 1;
1956 break;
1959 if (ignore)
1960 continue;
1962 err = got_path_dirent_type(&type, path_dir, de);
1963 if (err)
1964 goto done;
1966 if (type == DT_DIR) {
1967 err = import_subdir(&new_te, de, path_dir,
1968 ignores, repo, progress_cb, progress_arg);
1969 if (err) {
1970 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1971 goto done;
1972 err = NULL;
1973 continue;
1975 } else if (type == DT_REG || type == DT_LNK) {
1976 err = import_file(&new_te, de, path_dir, repo);
1977 if (err)
1978 goto done;
1979 } else
1980 continue;
1982 err = insert_tree_entry(new_te, &paths);
1983 if (err)
1984 goto done;
1985 nentries++;
1988 if (TAILQ_EMPTY(&paths)) {
1989 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1990 "cannot create tree without any entries");
1991 goto done;
1994 TAILQ_FOREACH(pe, &paths, entry) {
1995 struct got_tree_entry *te = pe->data;
1996 char *path;
1997 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1998 continue;
1999 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2000 err = got_error_from_errno("asprintf");
2001 goto done;
2003 err = (*progress_cb)(progress_arg, path);
2004 free(path);
2005 if (err)
2006 goto done;
2009 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2010 done:
2011 if (dir)
2012 closedir(dir);
2013 got_pathlist_free(&paths);
2014 return err;
2017 const struct got_error *
2018 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2019 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2020 struct got_repository *repo, got_repo_import_cb progress_cb,
2021 void *progress_arg)
2023 const struct got_error *err;
2024 struct got_object_id *new_tree_id;
2026 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2027 progress_cb, progress_arg);
2028 if (err)
2029 return err;
2031 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2032 author, time(NULL), author, time(NULL), logmsg, repo);
2033 free(new_tree_id);
2034 return err;
2037 const struct got_error *
2038 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2039 struct got_repository *repo)
2041 const struct got_error *err = NULL;
2042 char *path_objects = NULL, *path = NULL;
2043 DIR *dir = NULL;
2044 struct got_object_id id;
2045 int i;
2047 *nobjects = 0;
2048 *ondisk_size = 0;
2050 path_objects = got_repo_get_path_objects(repo);
2051 if (path_objects == NULL)
2052 return got_error_from_errno("got_repo_get_path_objects");
2054 for (i = 0; i <= 0xff; i++) {
2055 struct dirent *dent;
2057 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2058 err = got_error_from_errno("asprintf");
2059 break;
2062 dir = opendir(path);
2063 if (dir == NULL) {
2064 if (errno == ENOENT) {
2065 err = NULL;
2066 continue;
2068 err = got_error_from_errno2("opendir", path);
2069 break;
2072 while ((dent = readdir(dir)) != NULL) {
2073 char *id_str;
2074 int fd;
2075 struct stat sb;
2077 if (strcmp(dent->d_name, ".") == 0 ||
2078 strcmp(dent->d_name, "..") == 0)
2079 continue;
2081 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2082 err = got_error_from_errno("asprintf");
2083 goto done;
2086 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2087 free(id_str);
2088 continue;
2090 free(id_str);
2092 err = got_object_open_loose_fd(&fd, &id, repo);
2093 if (err)
2094 goto done;
2096 if (fstat(fd, &sb) == -1) {
2097 err = got_error_from_errno("fstat");
2098 close(fd);
2099 goto done;
2101 (*nobjects)++;
2102 (*ondisk_size) += sb.st_size;
2104 if (close(fd) == -1) {
2105 err = got_error_from_errno("close");
2106 goto done;
2110 if (closedir(dir) != 0) {
2111 err = got_error_from_errno("closedir");
2112 goto done;
2114 dir = NULL;
2116 free(path);
2117 path = NULL;
2119 done:
2120 if (dir && closedir(dir) != 0 && err == NULL)
2121 err = got_error_from_errno("closedir");
2123 if (err) {
2124 *nobjects = 0;
2125 *ondisk_size = 0;
2127 free(path_objects);
2128 free(path);
2129 return err;
2132 const struct got_error *
2133 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2134 off_t *total_packsize, struct got_repository *repo)
2136 const struct got_error *err = NULL;
2137 DIR *packdir = NULL;
2138 struct dirent *dent;
2139 struct got_packidx *packidx = NULL;
2140 char *path_packidx;
2141 char *path_packfile;
2142 int packdir_fd;
2143 struct stat sb;
2145 *npackfiles = 0;
2146 *nobjects = 0;
2147 *total_packsize = 0;
2149 packdir_fd = openat(got_repo_get_fd(repo),
2150 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2151 if (packdir_fd == -1) {
2152 return got_error_from_errno_fmt("openat: %s/%s",
2153 got_repo_get_path_git_dir(repo),
2154 GOT_OBJECTS_PACK_DIR);
2157 packdir = fdopendir(packdir_fd);
2158 if (packdir == NULL) {
2159 err = got_error_from_errno("fdopendir");
2160 goto done;
2163 while ((dent = readdir(packdir)) != NULL) {
2164 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2165 continue;
2167 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2168 dent->d_name) == -1) {
2169 err = got_error_from_errno("asprintf");
2170 goto done;
2173 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2174 path_packidx, 0);
2175 free(path_packidx);
2176 if (err)
2177 goto done;
2179 if (fstat(packidx->fd, &sb) == -1)
2180 goto done;
2181 *total_packsize += sb.st_size;
2183 err = got_packidx_get_packfile_path(&path_packfile,
2184 packidx->path_packidx);
2185 if (err)
2186 goto done;
2188 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2189 0) == -1) {
2190 free(path_packfile);
2191 goto done;
2193 free(path_packfile);
2194 *total_packsize += sb.st_size;
2196 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2198 (*npackfiles)++;
2200 got_packidx_close(packidx);
2201 packidx = NULL;
2203 done:
2204 if (packidx)
2205 got_packidx_close(packidx);
2206 if (packdir && closedir(packdir) != 0 && err == NULL)
2207 err = got_error_from_errno("closedir");
2208 if (err) {
2209 *npackfiles = 0;
2210 *nobjects = 0;
2211 *total_packsize = 0;
2213 return err;
2216 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2217 got_packidx_bloom_filter_cmp);