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 *const 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 return got_error_from_errno("calloc");
670 RB_INIT(&repo->packidx_bloom_filters);
671 TAILQ_INIT(&repo->packidx_paths);
673 for (i = 0; i < nitems(repo->privsep_children); i++) {
674 memset(&repo->privsep_children[i], 0,
675 sizeof(repo->privsep_children[0]));
676 repo->privsep_children[i].imsg_fd = -1;
679 err = got_object_cache_init(&repo->objcache,
680 GOT_OBJECT_CACHE_TYPE_OBJ);
681 if (err)
682 goto done;
683 err = got_object_cache_init(&repo->treecache,
684 GOT_OBJECT_CACHE_TYPE_TREE);
685 if (err)
686 goto done;
687 err = got_object_cache_init(&repo->commitcache,
688 GOT_OBJECT_CACHE_TYPE_COMMIT);
689 if (err)
690 goto done;
691 err = got_object_cache_init(&repo->tagcache,
692 GOT_OBJECT_CACHE_TYPE_TAG);
693 if (err)
694 goto done;
695 err = got_object_cache_init(&repo->rawcache,
696 GOT_OBJECT_CACHE_TYPE_RAW);
697 if (err)
698 goto done;
700 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
701 if (repo->pack_cache_size > rl.rlim_cur / 8)
702 repo->pack_cache_size = rl.rlim_cur / 8;
704 repo_path = realpath(path, NULL);
705 if (repo_path == NULL) {
706 err = got_error_from_errno2("realpath", path);
707 goto done;
710 for (;;) {
711 char *parent_path;
713 err = open_repo(repo, repo_path);
714 if (err == NULL)
715 break;
716 if (err->code != GOT_ERR_NOT_GIT_REPO)
717 goto done;
718 if (repo_path[0] == '/' && repo_path[1] == '\0') {
719 err = got_error(GOT_ERR_NOT_GIT_REPO);
720 goto done;
722 err = got_path_dirname(&parent_path, repo_path);
723 if (err)
724 goto done;
725 free(repo_path);
726 repo_path = parent_path;
729 err = read_gotconfig(repo);
730 if (err)
731 goto done;
733 err = read_gitconfig(repo, global_gitconfig_path);
734 if (err)
735 goto done;
736 if (repo->gitconfig_repository_format_version != 0)
737 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
738 for (i = 0; i < repo->nextensions; i++) {
739 char *ext = repo->extensions[i];
740 int j, supported = 0;
741 for (j = 0; j < nitems(repo_extensions); j++) {
742 if (strcmp(ext, repo_extensions[j]) == 0) {
743 supported = 1;
744 break;
747 if (!supported) {
748 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
749 goto done;
753 err = got_repo_list_packidx(&repo->packidx_paths, repo);
754 done:
755 if (err)
756 got_repo_close(repo);
757 else
758 *repop = repo;
759 free(repo_path);
760 return err;
763 const struct got_error *
764 got_repo_close(struct got_repository *repo)
766 const struct got_error *err = NULL, *child_err;
767 struct got_packidx_bloom_filter *bf;
768 struct got_pathlist_entry *pe;
769 size_t i;
771 for (i = 0; i < repo->pack_cache_size; i++) {
772 if (repo->packidx_cache[i] == NULL)
773 break;
774 got_packidx_close(repo->packidx_cache[i]);
777 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
778 &repo->packidx_bloom_filters))) {
779 RB_REMOVE(got_packidx_bloom_filter_tree,
780 &repo->packidx_bloom_filters, bf);
781 free(bf->bloom);
782 free(bf);
785 for (i = 0; i < repo->pack_cache_size; i++) {
786 if (repo->packs[i].path_packfile == NULL)
787 break;
788 got_pack_close(&repo->packs[i]);
791 free(repo->path);
792 free(repo->path_git_dir);
794 got_object_cache_close(&repo->objcache);
795 got_object_cache_close(&repo->treecache);
796 got_object_cache_close(&repo->commitcache);
797 got_object_cache_close(&repo->tagcache);
798 got_object_cache_close(&repo->rawcache);
800 for (i = 0; i < nitems(repo->privsep_children); i++) {
801 if (repo->privsep_children[i].imsg_fd == -1)
802 continue;
803 imsg_clear(repo->privsep_children[i].ibuf);
804 free(repo->privsep_children[i].ibuf);
805 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
806 child_err = got_privsep_wait_for_child(
807 repo->privsep_children[i].pid);
808 if (child_err && err == NULL)
809 err = child_err;
810 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
811 err == NULL)
812 err = got_error_from_errno("close");
815 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
816 err == NULL)
817 err = got_error_from_errno("close");
819 if (repo->gotconfig)
820 got_gotconfig_free(repo->gotconfig);
821 free(repo->gitconfig_author_name);
822 free(repo->gitconfig_author_email);
823 for (i = 0; i < repo->ngitconfig_remotes; i++)
824 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
825 free(repo->gitconfig_remotes);
826 for (i = 0; i < repo->nextensions; i++)
827 free(repo->extensions[i]);
828 free(repo->extensions);
830 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
831 free((void *)pe->path);
832 got_pathlist_free(&repo->packidx_paths);
833 free(repo);
835 return err;
838 void
839 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
841 int i;
843 free(repo->name);
844 repo->name = NULL;
845 free(repo->fetch_url);
846 repo->fetch_url = NULL;
847 free(repo->send_url);
848 repo->send_url = NULL;
849 for (i = 0; i < repo->nfetch_branches; i++)
850 free(repo->fetch_branches[i]);
851 free(repo->fetch_branches);
852 repo->fetch_branches = NULL;
853 repo->nfetch_branches = 0;
854 for (i = 0; i < repo->nsend_branches; i++)
855 free(repo->send_branches[i]);
856 free(repo->send_branches);
857 repo->send_branches = NULL;
858 repo->nsend_branches = 0;
861 const struct got_error *
862 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
863 const char *input_path)
865 const struct got_error *err = NULL;
866 const char *repo_abspath = NULL;
867 size_t repolen, len;
868 char *canonpath, *path = NULL;
870 *in_repo_path = NULL;
872 canonpath = strdup(input_path);
873 if (canonpath == NULL) {
874 err = got_error_from_errno("strdup");
875 goto done;
877 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
878 if (err)
879 goto done;
881 repo_abspath = got_repo_get_path(repo);
883 if (canonpath[0] == '\0') {
884 path = strdup(canonpath);
885 if (path == NULL) {
886 err = got_error_from_errno("strdup");
887 goto done;
889 } else {
890 path = realpath(canonpath, NULL);
891 if (path == NULL) {
892 if (errno != ENOENT) {
893 err = got_error_from_errno2("realpath",
894 canonpath);
895 goto done;
897 /*
898 * Path is not on disk.
899 * Assume it is already relative to repository root.
900 */
901 path = strdup(canonpath);
902 if (path == NULL) {
903 err = got_error_from_errno("strdup");
904 goto done;
908 repolen = strlen(repo_abspath);
909 len = strlen(path);
912 if (strcmp(path, repo_abspath) == 0) {
913 free(path);
914 path = strdup("");
915 if (path == NULL) {
916 err = got_error_from_errno("strdup");
917 goto done;
919 } else if (len > repolen &&
920 got_path_is_child(path, repo_abspath, repolen)) {
921 /* Matched an on-disk path inside repository. */
922 if (got_repo_is_bare(repo)) {
923 /*
924 * Matched an on-disk path inside repository
925 * database. Treat input as repository-relative.
926 */
927 free(path);
928 path = canonpath;
929 canonpath = NULL;
930 } else {
931 char *child;
932 /* Strip common prefix with repository path. */
933 err = got_path_skip_common_ancestor(&child,
934 repo_abspath, path);
935 if (err)
936 goto done;
937 free(path);
938 path = child;
940 } else {
941 /*
942 * Matched unrelated on-disk path.
943 * Treat input as repository-relative.
944 */
945 free(path);
946 path = canonpath;
947 canonpath = NULL;
951 /* Make in-repository path absolute */
952 if (path[0] != '/') {
953 char *abspath;
954 if (asprintf(&abspath, "/%s", path) == -1) {
955 err = got_error_from_errno("asprintf");
956 goto done;
958 free(path);
959 path = abspath;
962 done:
963 free(canonpath);
964 if (err)
965 free(path);
966 else
967 *in_repo_path = path;
968 return err;
971 static const struct got_error *
972 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
973 const char *path_packidx)
975 const struct got_error *err = NULL;
976 size_t i;
978 for (i = 0; i < repo->pack_cache_size; i++) {
979 if (repo->packidx_cache[i] == NULL)
980 break;
981 if (strcmp(repo->packidx_cache[i]->path_packidx,
982 path_packidx) == 0) {
983 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
986 if (i == repo->pack_cache_size) {
987 i = repo->pack_cache_size - 1;
988 err = got_packidx_close(repo->packidx_cache[i]);
989 if (err)
990 return err;
993 repo->packidx_cache[i] = packidx;
995 return NULL;
998 int
999 got_repo_is_packidx_filename(const char *name, size_t len)
1001 if (len != GOT_PACKIDX_NAMELEN)
1002 return 0;
1004 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1005 return 0;
1007 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1008 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1009 return 0;
1011 return 1;
1014 static struct got_packidx_bloom_filter *
1015 get_packidx_bloom_filter(struct got_repository *repo,
1016 const char *path, size_t path_len)
1018 struct got_packidx_bloom_filter key;
1020 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1021 return NULL; /* XXX */
1022 key.path_len = path_len;
1024 return RB_FIND(got_packidx_bloom_filter_tree,
1025 &repo->packidx_bloom_filters, &key);
1028 int
1029 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1030 const char *path_packidx, struct got_object_id *id)
1032 struct got_packidx_bloom_filter *bf;
1034 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1035 if (bf)
1036 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1038 /* No bloom filter means this pack index must be searched. */
1039 return 1;
1042 static const struct got_error *
1043 add_packidx_bloom_filter(struct got_repository *repo,
1044 struct got_packidx *packidx, const char *path_packidx)
1046 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1047 struct got_packidx_bloom_filter *bf;
1048 size_t len;
1051 * Don't use bloom filters for very large pack index files.
1052 * Large pack files will contain a relatively large fraction
1053 * of our objects so we will likely need to visit them anyway.
1054 * The more objects a pack file contains the higher the probability
1055 * of a false-positive match from the bloom filter. And reading
1056 * all object IDs from a large pack index file can be expensive.
1058 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1059 return NULL;
1061 /* Do we already have a filter for this pack index? */
1062 if (get_packidx_bloom_filter(repo, path_packidx,
1063 strlen(path_packidx)) != NULL)
1064 return NULL;
1066 bf = calloc(1, sizeof(*bf));
1067 if (bf == NULL)
1068 return got_error_from_errno("calloc");
1069 bf->bloom = calloc(1, sizeof(*bf->bloom));
1070 if (bf->bloom == NULL) {
1071 free(bf);
1072 return got_error_from_errno("calloc");
1075 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1076 if (len >= sizeof(bf->path)) {
1077 free(bf->bloom);
1078 free(bf);
1079 return got_error(GOT_ERR_NO_SPACE);
1081 bf->path_len = len;
1083 /* Minimum size supported by our bloom filter is 1000 entries. */
1084 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1085 for (i = 0; i < nobjects; i++) {
1086 struct got_packidx_object_id *id;
1087 id = &packidx->hdr.sorted_ids[i];
1088 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1091 RB_INSERT(got_packidx_bloom_filter_tree,
1092 &repo->packidx_bloom_filters, bf);
1093 return NULL;
1096 const struct got_error *
1097 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1098 struct got_repository *repo, struct got_object_id *id)
1100 const struct got_error *err;
1101 struct got_pathlist_entry *pe;
1102 size_t i;
1104 /* Search pack index cache. */
1105 for (i = 0; i < repo->pack_cache_size; i++) {
1106 if (repo->packidx_cache[i] == NULL)
1107 break;
1108 if (!got_repo_check_packidx_bloom_filter(repo,
1109 repo->packidx_cache[i]->path_packidx, id))
1110 continue; /* object will not be found in this index */
1111 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1112 if (*idx != -1) {
1113 *packidx = repo->packidx_cache[i];
1115 * Move this cache entry to the front. Repeatedly
1116 * searching a wrong pack index can be expensive.
1118 if (i > 0) {
1119 memmove(&repo->packidx_cache[1],
1120 &repo->packidx_cache[0],
1121 i * sizeof(repo->packidx_cache[0]));
1122 repo->packidx_cache[0] = *packidx;
1124 return NULL;
1127 /* No luck. Search the filesystem. */
1129 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1130 const char *path_packidx = pe->path;
1131 int is_cached = 0;
1133 if (!got_repo_check_packidx_bloom_filter(repo,
1134 pe->path, id))
1135 continue; /* object will not be found in this index */
1137 for (i = 0; i < repo->pack_cache_size; i++) {
1138 if (repo->packidx_cache[i] == NULL)
1139 break;
1140 if (strcmp(repo->packidx_cache[i]->path_packidx,
1141 path_packidx) == 0) {
1142 is_cached = 1;
1143 break;
1146 if (is_cached)
1147 continue; /* already searched */
1149 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1150 path_packidx, 0);
1151 if (err)
1152 goto done;
1154 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1155 if (err)
1156 goto done;
1158 err = cache_packidx(repo, *packidx, path_packidx);
1159 if (err)
1160 goto done;
1162 *idx = got_packidx_get_object_idx(*packidx, id);
1163 if (*idx != -1) {
1164 err = NULL; /* found the object */
1165 goto done;
1169 err = got_error_no_obj(id);
1170 done:
1171 return err;
1174 const struct got_error *
1175 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1176 struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 DIR *packdir = NULL;
1180 struct dirent *dent;
1181 char *path_packidx = NULL;
1182 int packdir_fd;
1184 packdir_fd = openat(got_repo_get_fd(repo),
1185 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1186 if (packdir_fd == -1) {
1187 return got_error_from_errno_fmt("openat: %s/%s",
1188 got_repo_get_path_git_dir(repo),
1189 GOT_OBJECTS_PACK_DIR);
1192 packdir = fdopendir(packdir_fd);
1193 if (packdir == NULL) {
1194 err = got_error_from_errno("fdopendir");
1195 goto done;
1198 while ((dent = readdir(packdir)) != NULL) {
1199 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1200 continue;
1202 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1203 dent->d_name) == -1) {
1204 err = got_error_from_errno("asprintf");
1205 path_packidx = NULL;
1206 break;
1209 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1210 if (err)
1211 break;
1213 done:
1214 if (err)
1215 free(path_packidx);
1216 if (packdir && closedir(packdir) != 0 && err == NULL)
1217 err = got_error_from_errno("closedir");
1218 return err;
1221 const struct got_error *
1222 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1223 struct got_repository *repo)
1225 const struct got_error *err;
1226 size_t i;
1228 *packidx = NULL;
1230 /* Search pack index cache. */
1231 for (i = 0; i < repo->pack_cache_size; i++) {
1232 if (repo->packidx_cache[i] == NULL)
1233 break;
1234 if (strcmp(repo->packidx_cache[i]->path_packidx,
1235 path_packidx) == 0) {
1236 *packidx = repo->packidx_cache[i];
1237 return NULL;
1240 /* No luck. Search the filesystem. */
1242 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1243 path_packidx, 0);
1244 if (err)
1245 return err;
1247 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1248 if (err)
1249 goto done;
1251 err = cache_packidx(repo, *packidx, path_packidx);
1252 done:
1253 if (err) {
1254 got_packidx_close(*packidx);
1255 *packidx = NULL;
1257 return err;
1260 static const struct got_error *
1261 read_packfile_hdr(int fd, struct got_packidx *packidx)
1263 const struct got_error *err = NULL;
1264 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1265 struct got_packfile_hdr hdr;
1266 ssize_t n;
1268 n = read(fd, &hdr, sizeof(hdr));
1269 if (n < 0)
1270 return got_error_from_errno("read");
1271 if (n != sizeof(hdr))
1272 return got_error(GOT_ERR_BAD_PACKFILE);
1274 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1275 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1276 be32toh(hdr.nobjects) != totobj)
1277 err = got_error(GOT_ERR_BAD_PACKFILE);
1279 return err;
1282 static const struct got_error *
1283 open_packfile(int *fd, struct got_repository *repo,
1284 const char *relpath, struct got_packidx *packidx)
1286 const struct got_error *err = NULL;
1288 *fd = openat(got_repo_get_fd(repo), relpath,
1289 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1290 if (*fd == -1)
1291 return got_error_from_errno_fmt("openat: %s/%s",
1292 got_repo_get_path_git_dir(repo), relpath);
1294 if (packidx) {
1295 err = read_packfile_hdr(*fd, packidx);
1296 if (err) {
1297 close(*fd);
1298 *fd = -1;
1302 return err;
1305 const struct got_error *
1306 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1307 const char *path_packfile, struct got_packidx *packidx)
1309 const struct got_error *err = NULL;
1310 struct got_pack *pack = NULL;
1311 struct stat sb;
1312 size_t i;
1314 if (packp)
1315 *packp = NULL;
1317 for (i = 0; i < repo->pack_cache_size; i++) {
1318 pack = &repo->packs[i];
1319 if (pack->path_packfile == NULL)
1320 break;
1321 if (strcmp(pack->path_packfile, path_packfile) == 0)
1322 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1325 if (i == repo->pack_cache_size) {
1326 err = got_pack_close(&repo->packs[i - 1]);
1327 if (err)
1328 return err;
1329 memmove(&repo->packs[1], &repo->packs[0],
1330 sizeof(repo->packs) - sizeof(repo->packs[0]));
1331 i = 0;
1334 pack = &repo->packs[i];
1336 pack->path_packfile = strdup(path_packfile);
1337 if (pack->path_packfile == NULL) {
1338 err = got_error_from_errno("strdup");
1339 goto done;
1342 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1343 if (err)
1344 goto done;
1346 if (fstat(pack->fd, &sb) != 0) {
1347 err = got_error_from_errno("fstat");
1348 goto done;
1350 pack->filesize = sb.st_size;
1352 pack->privsep_child = NULL;
1354 #ifndef GOT_PACK_NO_MMAP
1355 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1356 pack->fd, 0);
1357 if (pack->map == MAP_FAILED) {
1358 if (errno != ENOMEM) {
1359 err = got_error_from_errno("mmap");
1360 goto done;
1362 pack->map = NULL; /* fall back to read(2) */
1364 #endif
1365 done:
1366 if (err) {
1367 if (pack) {
1368 free(pack->path_packfile);
1369 memset(pack, 0, sizeof(*pack));
1371 } else if (packp)
1372 *packp = pack;
1373 return err;
1376 struct got_pack *
1377 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1379 struct got_pack *pack = NULL;
1380 size_t i;
1382 for (i = 0; i < repo->pack_cache_size; i++) {
1383 pack = &repo->packs[i];
1384 if (pack->path_packfile == NULL)
1385 break;
1386 if (strcmp(pack->path_packfile, path_packfile) == 0)
1387 return pack;
1390 return NULL;
1393 const struct got_error *
1394 got_repo_init(const char *repo_path)
1396 const struct got_error *err = NULL;
1397 const char *dirnames[] = {
1398 GOT_OBJECTS_DIR,
1399 GOT_OBJECTS_PACK_DIR,
1400 GOT_REFS_DIR,
1402 const char *description_str = "Unnamed repository; "
1403 "edit this file 'description' to name the repository.";
1404 const char *headref_str = "ref: refs/heads/main";
1405 const char *gitconfig_str = "[core]\n"
1406 "\trepositoryformatversion = 0\n"
1407 "\tfilemode = true\n"
1408 "\tbare = true\n";
1409 char *path;
1410 size_t i;
1412 if (!got_path_dir_is_empty(repo_path))
1413 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1415 for (i = 0; i < nitems(dirnames); i++) {
1416 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1417 return got_error_from_errno("asprintf");
1419 err = got_path_mkdir(path);
1420 free(path);
1421 if (err)
1422 return err;
1425 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1426 return got_error_from_errno("asprintf");
1427 err = got_path_create_file(path, description_str);
1428 free(path);
1429 if (err)
1430 return err;
1432 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1433 return got_error_from_errno("asprintf");
1434 err = got_path_create_file(path, headref_str);
1435 free(path);
1436 if (err)
1437 return err;
1439 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1440 return got_error_from_errno("asprintf");
1441 err = got_path_create_file(path, gitconfig_str);
1442 free(path);
1443 if (err)
1444 return err;
1446 return NULL;
1449 static const struct got_error *
1450 match_packed_object(struct got_object_id **unique_id,
1451 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1453 const struct got_error *err = NULL;
1454 struct got_object_id_queue matched_ids;
1455 struct got_pathlist_entry *pe;
1457 STAILQ_INIT(&matched_ids);
1459 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1460 const char *path_packidx = pe->path;
1461 struct got_packidx *packidx;
1462 struct got_object_qid *qid;
1464 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1465 path_packidx, 0);
1466 if (err)
1467 break;
1469 err = got_packidx_match_id_str_prefix(&matched_ids,
1470 packidx, id_str_prefix);
1471 if (err) {
1472 got_packidx_close(packidx);
1473 break;
1475 err = got_packidx_close(packidx);
1476 if (err)
1477 break;
1479 STAILQ_FOREACH(qid, &matched_ids, entry) {
1480 if (obj_type != GOT_OBJ_TYPE_ANY) {
1481 int matched_type;
1482 err = got_object_get_type(&matched_type, repo,
1483 qid->id);
1484 if (err)
1485 goto done;
1486 if (matched_type != obj_type)
1487 continue;
1489 if (*unique_id == NULL) {
1490 *unique_id = got_object_id_dup(qid->id);
1491 if (*unique_id == NULL) {
1492 err = got_error_from_errno("malloc");
1493 goto done;
1495 } else {
1496 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1497 continue; /* packed multiple times */
1498 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1499 goto done;
1503 done:
1504 got_object_id_queue_free(&matched_ids);
1505 if (err) {
1506 free(*unique_id);
1507 *unique_id = NULL;
1509 return err;
1512 static const struct got_error *
1513 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1514 const char *object_dir, const char *id_str_prefix, int obj_type,
1515 struct got_repository *repo)
1517 const struct got_error *err = NULL;
1518 char *path;
1519 DIR *dir = NULL;
1520 struct dirent *dent;
1521 struct got_object_id id;
1523 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1524 err = got_error_from_errno("asprintf");
1525 goto done;
1528 dir = opendir(path);
1529 if (dir == NULL) {
1530 if (errno == ENOENT) {
1531 err = NULL;
1532 goto done;
1534 err = got_error_from_errno2("opendir", path);
1535 goto done;
1537 while ((dent = readdir(dir)) != NULL) {
1538 char *id_str;
1539 int cmp;
1541 if (strcmp(dent->d_name, ".") == 0 ||
1542 strcmp(dent->d_name, "..") == 0)
1543 continue;
1545 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1546 err = got_error_from_errno("asprintf");
1547 goto done;
1550 if (!got_parse_sha1_digest(id.sha1, id_str))
1551 continue;
1554 * Directory entries do not necessarily appear in
1555 * sorted order, so we must iterate over all of them.
1557 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1558 if (cmp != 0) {
1559 free(id_str);
1560 continue;
1563 if (*unique_id == NULL) {
1564 if (obj_type != GOT_OBJ_TYPE_ANY) {
1565 int matched_type;
1566 err = got_object_get_type(&matched_type, repo,
1567 &id);
1568 if (err)
1569 goto done;
1570 if (matched_type != obj_type)
1571 continue;
1573 *unique_id = got_object_id_dup(&id);
1574 if (*unique_id == NULL) {
1575 err = got_error_from_errno("got_object_id_dup");
1576 free(id_str);
1577 goto done;
1579 } else {
1580 if (got_object_id_cmp(*unique_id, &id) == 0)
1581 continue; /* both packed and loose */
1582 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1583 free(id_str);
1584 goto done;
1587 done:
1588 if (dir && closedir(dir) != 0 && err == NULL)
1589 err = got_error_from_errno("closedir");
1590 if (err) {
1591 free(*unique_id);
1592 *unique_id = NULL;
1594 free(path);
1595 return err;
1598 const struct got_error *
1599 got_repo_match_object_id_prefix(struct got_object_id **id,
1600 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1602 const struct got_error *err = NULL;
1603 char *path_objects = got_repo_get_path_objects(repo);
1604 char *object_dir = NULL;
1605 size_t len;
1606 int i;
1608 *id = NULL;
1610 len = strlen(id_str_prefix);
1611 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1612 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1614 for (i = 0; i < len; i++) {
1615 if (isxdigit((unsigned char)id_str_prefix[i]))
1616 continue;
1617 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1620 if (len >= 2) {
1621 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1622 if (err)
1623 goto done;
1624 object_dir = strndup(id_str_prefix, 2);
1625 if (object_dir == NULL) {
1626 err = got_error_from_errno("strdup");
1627 goto done;
1629 err = match_loose_object(id, path_objects, object_dir,
1630 id_str_prefix, obj_type, repo);
1631 } else if (len == 1) {
1632 int i;
1633 for (i = 0; i < 0xf; i++) {
1634 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1635 == -1) {
1636 err = got_error_from_errno("asprintf");
1637 goto done;
1639 err = match_packed_object(id, repo, object_dir,
1640 obj_type);
1641 if (err)
1642 goto done;
1643 err = match_loose_object(id, path_objects, object_dir,
1644 id_str_prefix, obj_type, repo);
1645 if (err)
1646 goto done;
1648 } else {
1649 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1650 goto done;
1652 done:
1653 free(object_dir);
1654 if (err) {
1655 free(*id);
1656 *id = NULL;
1657 } else if (*id == NULL) {
1658 switch (obj_type) {
1659 case GOT_OBJ_TYPE_BLOB:
1660 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1661 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1662 break;
1663 case GOT_OBJ_TYPE_TREE:
1664 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1665 GOT_OBJ_LABEL_TREE, id_str_prefix);
1666 break;
1667 case GOT_OBJ_TYPE_COMMIT:
1668 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1669 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1670 break;
1671 case GOT_OBJ_TYPE_TAG:
1672 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1673 GOT_OBJ_LABEL_TAG, id_str_prefix);
1674 break;
1675 default:
1676 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1677 break;
1681 return err;
1684 const struct got_error *
1685 got_repo_match_object_id(struct got_object_id **id, char **label,
1686 const char *id_str, int obj_type, struct got_reflist_head *refs,
1687 struct got_repository *repo)
1689 const struct got_error *err;
1690 struct got_tag_object *tag;
1691 struct got_reference *ref = NULL;
1693 *id = NULL;
1694 if (label)
1695 *label = NULL;
1697 if (refs) {
1698 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1699 refs, repo);
1700 if (err == NULL) {
1701 *id = got_object_id_dup(
1702 got_object_tag_get_object_id(tag));
1703 if (*id == NULL)
1704 err = got_error_from_errno("got_object_id_dup");
1705 else if (label && asprintf(label, "refs/tags/%s",
1706 got_object_tag_get_name(tag)) == -1) {
1707 err = got_error_from_errno("asprintf");
1708 free(*id);
1709 *id = NULL;
1711 got_object_tag_close(tag);
1712 return err;
1713 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1714 err->code != GOT_ERR_NO_OBJ)
1715 return err;
1718 err = got_ref_open(&ref, repo, id_str, 0);
1719 if (err == NULL) {
1720 err = got_ref_resolve(id, repo, ref);
1721 if (err)
1722 goto done;
1723 if (label) {
1724 *label = strdup(got_ref_get_name(ref));
1725 if (*label == NULL) {
1726 err = got_error_from_errno("strdup");
1727 goto done;
1730 } else {
1731 if (err->code != GOT_ERR_NOT_REF &&
1732 err->code != GOT_ERR_BAD_REF_NAME)
1733 goto done;
1734 err = got_repo_match_object_id_prefix(id, id_str,
1735 obj_type, repo);
1736 if (err) {
1737 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1738 err = got_error_not_ref(id_str);
1739 goto done;
1741 if (label) {
1742 err = got_object_id_str(label, *id);
1743 if (*label == NULL) {
1744 err = got_error_from_errno("strdup");
1745 goto done;
1749 done:
1750 if (ref)
1751 got_ref_close(ref);
1752 return err;
1755 const struct got_error *
1756 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1757 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1759 const struct got_error *err = NULL;
1760 struct got_reflist_entry *re;
1761 struct got_object_id *tag_id;
1762 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1764 *tag = NULL;
1766 TAILQ_FOREACH(re, refs, entry) {
1767 const char *refname;
1768 refname = got_ref_get_name(re->ref);
1769 if (got_ref_is_symbolic(re->ref))
1770 continue;
1771 if (strncmp(refname, "refs/tags/", 10) != 0)
1772 continue;
1773 if (!name_is_absolute)
1774 refname += strlen("refs/tags/");
1775 if (strcmp(refname, name) != 0)
1776 continue;
1777 err = got_ref_resolve(&tag_id, repo, re->ref);
1778 if (err)
1779 break;
1780 err = got_object_open_as_tag(tag, repo, tag_id);
1781 free(tag_id);
1782 if (err)
1783 break;
1784 if (obj_type == GOT_OBJ_TYPE_ANY ||
1785 got_object_tag_get_object_type(*tag) == obj_type)
1786 break;
1787 got_object_tag_close(*tag);
1788 *tag = NULL;
1791 if (err == NULL && *tag == NULL)
1792 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1793 GOT_OBJ_LABEL_TAG, name);
1794 return err;
1797 static const struct got_error *
1798 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1799 const char *name, mode_t mode, struct got_object_id *blob_id)
1801 const struct got_error *err = NULL;
1803 *new_te = NULL;
1805 *new_te = calloc(1, sizeof(**new_te));
1806 if (*new_te == NULL)
1807 return got_error_from_errno("calloc");
1809 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1810 sizeof((*new_te)->name)) {
1811 err = got_error(GOT_ERR_NO_SPACE);
1812 goto done;
1815 if (S_ISLNK(mode)) {
1816 (*new_te)->mode = S_IFLNK;
1817 } else {
1818 (*new_te)->mode = S_IFREG;
1819 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1821 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1822 done:
1823 if (err && *new_te) {
1824 free(*new_te);
1825 *new_te = NULL;
1827 return err;
1830 static const struct got_error *
1831 import_file(struct got_tree_entry **new_te, struct dirent *de,
1832 const char *path, struct got_repository *repo)
1834 const struct got_error *err;
1835 struct got_object_id *blob_id = NULL;
1836 char *filepath;
1837 struct stat sb;
1839 if (asprintf(&filepath, "%s%s%s", path,
1840 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1841 return got_error_from_errno("asprintf");
1843 if (lstat(filepath, &sb) != 0) {
1844 err = got_error_from_errno2("lstat", path);
1845 goto done;
1848 err = got_object_blob_create(&blob_id, filepath, repo);
1849 if (err)
1850 goto done;
1852 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1853 blob_id);
1854 done:
1855 free(filepath);
1856 if (err)
1857 free(blob_id);
1858 return err;
1861 static const struct got_error *
1862 insert_tree_entry(struct got_tree_entry *new_te,
1863 struct got_pathlist_head *paths)
1865 const struct got_error *err = NULL;
1866 struct got_pathlist_entry *new_pe;
1868 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1869 if (err)
1870 return err;
1871 if (new_pe == NULL)
1872 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1873 return NULL;
1876 static const struct got_error *write_tree(struct got_object_id **,
1877 const char *, struct got_pathlist_head *, struct got_repository *,
1878 got_repo_import_cb progress_cb, void *progress_arg);
1880 static const struct got_error *
1881 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1882 const char *path, struct got_pathlist_head *ignores,
1883 struct got_repository *repo,
1884 got_repo_import_cb progress_cb, void *progress_arg)
1886 const struct got_error *err;
1887 struct got_object_id *id = NULL;
1888 char *subdirpath;
1890 if (asprintf(&subdirpath, "%s%s%s", path,
1891 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1892 return got_error_from_errno("asprintf");
1894 (*new_te) = calloc(1, sizeof(**new_te));
1895 if (*new_te == NULL)
1896 return got_error_from_errno("calloc");
1897 (*new_te)->mode = S_IFDIR;
1898 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1899 sizeof((*new_te)->name)) {
1900 err = got_error(GOT_ERR_NO_SPACE);
1901 goto done;
1903 err = write_tree(&id, subdirpath, ignores, repo,
1904 progress_cb, progress_arg);
1905 if (err)
1906 goto done;
1907 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1909 done:
1910 free(id);
1911 free(subdirpath);
1912 if (err) {
1913 free(*new_te);
1914 *new_te = NULL;
1916 return err;
1919 static const struct got_error *
1920 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1921 struct got_pathlist_head *ignores, struct got_repository *repo,
1922 got_repo_import_cb progress_cb, void *progress_arg)
1924 const struct got_error *err = NULL;
1925 DIR *dir;
1926 struct dirent *de;
1927 int nentries;
1928 struct got_tree_entry *new_te = NULL;
1929 struct got_pathlist_head paths;
1930 struct got_pathlist_entry *pe;
1932 *new_tree_id = NULL;
1934 TAILQ_INIT(&paths);
1936 dir = opendir(path_dir);
1937 if (dir == NULL) {
1938 err = got_error_from_errno2("opendir", path_dir);
1939 goto done;
1942 nentries = 0;
1943 while ((de = readdir(dir)) != NULL) {
1944 int ignore = 0;
1945 int type;
1947 if (strcmp(de->d_name, ".") == 0 ||
1948 strcmp(de->d_name, "..") == 0)
1949 continue;
1951 TAILQ_FOREACH(pe, ignores, entry) {
1952 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1953 ignore = 1;
1954 break;
1957 if (ignore)
1958 continue;
1960 err = got_path_dirent_type(&type, path_dir, de);
1961 if (err)
1962 goto done;
1964 if (type == DT_DIR) {
1965 err = import_subdir(&new_te, de, path_dir,
1966 ignores, repo, progress_cb, progress_arg);
1967 if (err) {
1968 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1969 goto done;
1970 err = NULL;
1971 continue;
1973 } else if (type == DT_REG || type == DT_LNK) {
1974 err = import_file(&new_te, de, path_dir, repo);
1975 if (err)
1976 goto done;
1977 } else
1978 continue;
1980 err = insert_tree_entry(new_te, &paths);
1981 if (err)
1982 goto done;
1983 nentries++;
1986 if (TAILQ_EMPTY(&paths)) {
1987 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1988 "cannot create tree without any entries");
1989 goto done;
1992 TAILQ_FOREACH(pe, &paths, entry) {
1993 struct got_tree_entry *te = pe->data;
1994 char *path;
1995 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1996 continue;
1997 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1998 err = got_error_from_errno("asprintf");
1999 goto done;
2001 err = (*progress_cb)(progress_arg, path);
2002 free(path);
2003 if (err)
2004 goto done;
2007 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2008 done:
2009 if (dir)
2010 closedir(dir);
2011 got_pathlist_free(&paths);
2012 return err;
2015 const struct got_error *
2016 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2017 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2018 struct got_repository *repo, got_repo_import_cb progress_cb,
2019 void *progress_arg)
2021 const struct got_error *err;
2022 struct got_object_id *new_tree_id;
2024 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2025 progress_cb, progress_arg);
2026 if (err)
2027 return err;
2029 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2030 author, time(NULL), author, time(NULL), logmsg, repo);
2031 free(new_tree_id);
2032 return err;
2035 const struct got_error *
2036 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2037 struct got_repository *repo)
2039 const struct got_error *err = NULL;
2040 char *path_objects = NULL, *path = NULL;
2041 DIR *dir = NULL;
2042 struct got_object_id id;
2043 int i;
2045 *nobjects = 0;
2046 *ondisk_size = 0;
2048 path_objects = got_repo_get_path_objects(repo);
2049 if (path_objects == NULL)
2050 return got_error_from_errno("got_repo_get_path_objects");
2052 for (i = 0; i <= 0xff; i++) {
2053 struct dirent *dent;
2055 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2056 err = got_error_from_errno("asprintf");
2057 break;
2060 dir = opendir(path);
2061 if (dir == NULL) {
2062 if (errno == ENOENT) {
2063 err = NULL;
2064 continue;
2066 err = got_error_from_errno2("opendir", path);
2067 break;
2070 while ((dent = readdir(dir)) != NULL) {
2071 char *id_str;
2072 int fd;
2073 struct stat sb;
2075 if (strcmp(dent->d_name, ".") == 0 ||
2076 strcmp(dent->d_name, "..") == 0)
2077 continue;
2079 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2080 err = got_error_from_errno("asprintf");
2081 goto done;
2084 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2085 free(id_str);
2086 continue;
2088 free(id_str);
2090 err = got_object_open_loose_fd(&fd, &id, repo);
2091 if (err)
2092 goto done;
2094 if (fstat(fd, &sb) == -1) {
2095 err = got_error_from_errno("fstat");
2096 close(fd);
2097 goto done;
2099 (*nobjects)++;
2100 (*ondisk_size) += sb.st_size;
2102 if (close(fd) == -1) {
2103 err = got_error_from_errno("close");
2104 goto done;
2108 if (closedir(dir) != 0) {
2109 err = got_error_from_errno("closedir");
2110 goto done;
2112 dir = NULL;
2114 free(path);
2115 path = NULL;
2117 done:
2118 if (dir && closedir(dir) != 0 && err == NULL)
2119 err = got_error_from_errno("closedir");
2121 if (err) {
2122 *nobjects = 0;
2123 *ondisk_size = 0;
2125 free(path_objects);
2126 free(path);
2127 return err;
2130 const struct got_error *
2131 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2132 off_t *total_packsize, struct got_repository *repo)
2134 const struct got_error *err = NULL;
2135 DIR *packdir = NULL;
2136 struct dirent *dent;
2137 struct got_packidx *packidx = NULL;
2138 char *path_packidx;
2139 char *path_packfile;
2140 int packdir_fd;
2141 struct stat sb;
2143 *npackfiles = 0;
2144 *nobjects = 0;
2145 *total_packsize = 0;
2147 packdir_fd = openat(got_repo_get_fd(repo),
2148 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2149 if (packdir_fd == -1) {
2150 return got_error_from_errno_fmt("openat: %s/%s",
2151 got_repo_get_path_git_dir(repo),
2152 GOT_OBJECTS_PACK_DIR);
2155 packdir = fdopendir(packdir_fd);
2156 if (packdir == NULL) {
2157 err = got_error_from_errno("fdopendir");
2158 goto done;
2161 while ((dent = readdir(packdir)) != NULL) {
2162 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2163 continue;
2165 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2166 dent->d_name) == -1) {
2167 err = got_error_from_errno("asprintf");
2168 goto done;
2171 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2172 path_packidx, 0);
2173 free(path_packidx);
2174 if (err)
2175 goto done;
2177 if (fstat(packidx->fd, &sb) == -1)
2178 goto done;
2179 *total_packsize += sb.st_size;
2181 err = got_packidx_get_packfile_path(&path_packfile,
2182 packidx->path_packidx);
2183 if (err)
2184 goto done;
2186 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2187 0) == -1) {
2188 free(path_packfile);
2189 goto done;
2191 free(path_packfile);
2192 *total_packsize += sb.st_size;
2194 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2196 (*npackfiles)++;
2198 got_packidx_close(packidx);
2199 packidx = NULL;
2201 done:
2202 if (packidx)
2203 got_packidx_close(packidx);
2204 if (packdir && closedir(packdir) != 0 && err == NULL)
2205 err = got_error_from_errno("closedir");
2206 if (err) {
2207 *npackfiles = 0;
2208 *nobjects = 0;
2209 *total_packsize = 0;
2211 return err;
2214 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2215 got_packidx_bloom_filter_cmp);