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);
674 for (i = 0; i < nitems(repo->privsep_children); i++) {
675 memset(&repo->privsep_children[i], 0,
676 sizeof(repo->privsep_children[0]));
677 repo->privsep_children[i].imsg_fd = -1;
680 err = got_object_cache_init(&repo->objcache,
681 GOT_OBJECT_CACHE_TYPE_OBJ);
682 if (err)
683 goto done;
684 err = got_object_cache_init(&repo->treecache,
685 GOT_OBJECT_CACHE_TYPE_TREE);
686 if (err)
687 goto done;
688 err = got_object_cache_init(&repo->commitcache,
689 GOT_OBJECT_CACHE_TYPE_COMMIT);
690 if (err)
691 goto done;
692 err = got_object_cache_init(&repo->tagcache,
693 GOT_OBJECT_CACHE_TYPE_TAG);
694 if (err)
695 goto done;
696 err = got_object_cache_init(&repo->rawcache,
697 GOT_OBJECT_CACHE_TYPE_RAW);
698 if (err)
699 goto done;
701 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
702 if (repo->pack_cache_size > rl.rlim_cur / 8)
703 repo->pack_cache_size = rl.rlim_cur / 8;
705 repo_path = realpath(path, NULL);
706 if (repo_path == NULL) {
707 err = got_error_from_errno2("realpath", path);
708 goto done;
711 for (;;) {
712 char *parent_path;
714 err = open_repo(repo, repo_path);
715 if (err == NULL)
716 break;
717 if (err->code != GOT_ERR_NOT_GIT_REPO)
718 goto done;
719 if (repo_path[0] == '/' && repo_path[1] == '\0') {
720 err = got_error(GOT_ERR_NOT_GIT_REPO);
721 goto done;
723 err = got_path_dirname(&parent_path, repo_path);
724 if (err)
725 goto done;
726 free(repo_path);
727 repo_path = parent_path;
730 err = read_gotconfig(repo);
731 if (err)
732 goto done;
734 err = read_gitconfig(repo, global_gitconfig_path);
735 if (err)
736 goto done;
737 if (repo->gitconfig_repository_format_version != 0)
738 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
739 for (i = 0; i < repo->nextensions; i++) {
740 char *ext = repo->extensions[i];
741 int j, supported = 0;
742 for (j = 0; j < nitems(repo_extensions); j++) {
743 if (strcmp(ext, repo_extensions[j]) == 0) {
744 supported = 1;
745 break;
748 if (!supported) {
749 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
750 goto done;
753 done:
754 if (err)
755 got_repo_close(repo);
756 else
757 *repop = repo;
758 free(repo_path);
759 return err;
762 const struct got_error *
763 got_repo_close(struct got_repository *repo)
765 const struct got_error *err = NULL, *child_err;
766 struct got_packidx_bloom_filter *bf;
767 size_t i;
769 for (i = 0; i < repo->pack_cache_size; i++) {
770 if (repo->packidx_cache[i] == NULL)
771 break;
772 got_packidx_close(repo->packidx_cache[i]);
775 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
776 &repo->packidx_bloom_filters))) {
777 RB_REMOVE(got_packidx_bloom_filter_tree,
778 &repo->packidx_bloom_filters, bf);
779 free(bf->bloom);
780 free(bf);
783 for (i = 0; i < repo->pack_cache_size; i++) {
784 if (repo->packs[i].path_packfile == NULL)
785 break;
786 got_pack_close(&repo->packs[i]);
789 free(repo->path);
790 free(repo->path_git_dir);
792 got_object_cache_close(&repo->objcache);
793 got_object_cache_close(&repo->treecache);
794 got_object_cache_close(&repo->commitcache);
795 got_object_cache_close(&repo->tagcache);
796 got_object_cache_close(&repo->rawcache);
798 for (i = 0; i < nitems(repo->privsep_children); i++) {
799 if (repo->privsep_children[i].imsg_fd == -1)
800 continue;
801 imsg_clear(repo->privsep_children[i].ibuf);
802 free(repo->privsep_children[i].ibuf);
803 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
804 child_err = got_privsep_wait_for_child(
805 repo->privsep_children[i].pid);
806 if (child_err && err == NULL)
807 err = child_err;
808 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
809 err == NULL)
810 err = got_error_from_errno("close");
813 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
814 err == NULL)
815 err = got_error_from_errno("close");
817 if (repo->gotconfig)
818 got_gotconfig_free(repo->gotconfig);
819 free(repo->gitconfig_author_name);
820 free(repo->gitconfig_author_email);
821 for (i = 0; i < repo->ngitconfig_remotes; i++)
822 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
823 free(repo->gitconfig_remotes);
824 for (i = 0; i < repo->nextensions; i++)
825 free(repo->extensions[i]);
826 free(repo->extensions);
827 free(repo);
829 return err;
832 void
833 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
835 int i;
837 free(repo->name);
838 repo->name = NULL;
839 free(repo->fetch_url);
840 repo->fetch_url = NULL;
841 free(repo->send_url);
842 repo->send_url = NULL;
843 for (i = 0; i < repo->nfetch_branches; i++)
844 free(repo->fetch_branches[i]);
845 free(repo->fetch_branches);
846 repo->fetch_branches = NULL;
847 repo->nfetch_branches = 0;
848 for (i = 0; i < repo->nsend_branches; i++)
849 free(repo->send_branches[i]);
850 free(repo->send_branches);
851 repo->send_branches = NULL;
852 repo->nsend_branches = 0;
855 const struct got_error *
856 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
857 const char *input_path)
859 const struct got_error *err = NULL;
860 const char *repo_abspath = NULL;
861 size_t repolen, len;
862 char *canonpath, *path = NULL;
864 *in_repo_path = NULL;
866 canonpath = strdup(input_path);
867 if (canonpath == NULL) {
868 err = got_error_from_errno("strdup");
869 goto done;
871 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
872 if (err)
873 goto done;
875 repo_abspath = got_repo_get_path(repo);
877 if (canonpath[0] == '\0') {
878 path = strdup(canonpath);
879 if (path == NULL) {
880 err = got_error_from_errno("strdup");
881 goto done;
883 } else {
884 path = realpath(canonpath, NULL);
885 if (path == NULL) {
886 if (errno != ENOENT) {
887 err = got_error_from_errno2("realpath",
888 canonpath);
889 goto done;
891 /*
892 * Path is not on disk.
893 * Assume it is already relative to repository root.
894 */
895 path = strdup(canonpath);
896 if (path == NULL) {
897 err = got_error_from_errno("strdup");
898 goto done;
902 repolen = strlen(repo_abspath);
903 len = strlen(path);
906 if (strcmp(path, repo_abspath) == 0) {
907 free(path);
908 path = strdup("");
909 if (path == NULL) {
910 err = got_error_from_errno("strdup");
911 goto done;
913 } else if (len > repolen &&
914 got_path_is_child(path, repo_abspath, repolen)) {
915 /* Matched an on-disk path inside repository. */
916 if (got_repo_is_bare(repo)) {
917 /*
918 * Matched an on-disk path inside repository
919 * database. Treat input as repository-relative.
920 */
921 free(path);
922 path = canonpath;
923 canonpath = NULL;
924 } else {
925 char *child;
926 /* Strip common prefix with repository path. */
927 err = got_path_skip_common_ancestor(&child,
928 repo_abspath, path);
929 if (err)
930 goto done;
931 free(path);
932 path = child;
934 } else {
935 /*
936 * Matched unrelated on-disk path.
937 * Treat input as repository-relative.
938 */
939 free(path);
940 path = canonpath;
941 canonpath = NULL;
945 /* Make in-repository path absolute */
946 if (path[0] != '/') {
947 char *abspath;
948 if (asprintf(&abspath, "/%s", path) == -1) {
949 err = got_error_from_errno("asprintf");
950 goto done;
952 free(path);
953 path = abspath;
956 done:
957 free(canonpath);
958 if (err)
959 free(path);
960 else
961 *in_repo_path = path;
962 return err;
965 static const struct got_error *
966 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
967 const char *path_packidx)
969 const struct got_error *err = NULL;
970 size_t i;
972 for (i = 0; i < repo->pack_cache_size; i++) {
973 if (repo->packidx_cache[i] == NULL)
974 break;
975 if (strcmp(repo->packidx_cache[i]->path_packidx,
976 path_packidx) == 0) {
977 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
980 if (i == repo->pack_cache_size) {
981 i = repo->pack_cache_size - 1;
982 err = got_packidx_close(repo->packidx_cache[i]);
983 if (err)
984 return err;
987 repo->packidx_cache[i] = packidx;
989 return NULL;
992 int
993 got_repo_is_packidx_filename(const char *name, size_t len)
995 if (len != GOT_PACKIDX_NAMELEN)
996 return 0;
998 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
999 return 0;
1001 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1002 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1003 return 0;
1005 return 1;
1008 static struct got_packidx_bloom_filter *
1009 get_packidx_bloom_filter(struct got_repository *repo,
1010 const char *path, size_t path_len)
1012 struct got_packidx_bloom_filter key;
1014 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1015 return NULL; /* XXX */
1016 key.path_len = path_len;
1018 return RB_FIND(got_packidx_bloom_filter_tree,
1019 &repo->packidx_bloom_filters, &key);
1022 int
1023 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1024 const char *path_packidx, struct got_object_id *id)
1026 struct got_packidx_bloom_filter *bf;
1028 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1029 if (bf)
1030 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1032 /* No bloom filter means this pack index must be searched. */
1033 return 1;
1036 static const struct got_error *
1037 add_packidx_bloom_filter(struct got_repository *repo,
1038 struct got_packidx *packidx, const char *path_packidx)
1040 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1041 struct got_packidx_bloom_filter *bf;
1042 size_t len;
1045 * Don't use bloom filters for very large pack index files.
1046 * Large pack files will contain a relatively large fraction
1047 * of our objects so we will likely need to visit them anyway.
1048 * The more objects a pack file contains the higher the probability
1049 * of a false-positive match from the bloom filter. And reading
1050 * all object IDs from a large pack index file can be expensive.
1052 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1053 return NULL;
1055 /* Do we already have a filter for this pack index? */
1056 if (get_packidx_bloom_filter(repo, path_packidx,
1057 strlen(path_packidx)) != NULL)
1058 return NULL;
1060 bf = calloc(1, sizeof(*bf));
1061 if (bf == NULL)
1062 return got_error_from_errno("calloc");
1063 bf->bloom = calloc(1, sizeof(*bf->bloom));
1064 if (bf->bloom == NULL) {
1065 free(bf);
1066 return got_error_from_errno("calloc");
1069 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1070 if (len >= sizeof(bf->path)) {
1071 free(bf->bloom);
1072 free(bf);
1073 return got_error(GOT_ERR_NO_SPACE);
1075 bf->path_len = len;
1077 /* Minimum size supported by our bloom filter is 1000 entries. */
1078 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1079 for (i = 0; i < nobjects; i++) {
1080 struct got_packidx_object_id *id;
1081 id = &packidx->hdr.sorted_ids[i];
1082 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1085 RB_INSERT(got_packidx_bloom_filter_tree,
1086 &repo->packidx_bloom_filters, bf);
1087 return NULL;
1090 const struct got_error *
1091 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1092 struct got_repository *repo, struct got_object_id *id)
1094 const struct got_error *err;
1095 DIR *packdir = NULL;
1096 struct dirent *dent;
1097 char *path_packidx;
1098 size_t i;
1099 int packdir_fd;
1101 /* Search pack index cache. */
1102 for (i = 0; i < repo->pack_cache_size; i++) {
1103 if (repo->packidx_cache[i] == NULL)
1104 break;
1105 if (!got_repo_check_packidx_bloom_filter(repo,
1106 repo->packidx_cache[i]->path_packidx, id))
1107 continue; /* object will not be found in this index */
1108 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1109 if (*idx != -1) {
1110 *packidx = repo->packidx_cache[i];
1112 * Move this cache entry to the front. Repeatedly
1113 * searching a wrong pack index can be expensive.
1115 if (i > 0) {
1116 memmove(&repo->packidx_cache[1],
1117 &repo->packidx_cache[0],
1118 i * sizeof(repo->packidx_cache[0]));
1119 repo->packidx_cache[0] = *packidx;
1121 return NULL;
1124 /* No luck. Search the filesystem. */
1126 packdir_fd = openat(got_repo_get_fd(repo),
1127 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1128 if (packdir_fd == -1) {
1129 if (errno == ENOENT)
1130 err = got_error_no_obj(id);
1131 else
1132 err = got_error_from_errno_fmt("openat: %s/%s",
1133 got_repo_get_path_git_dir(repo),
1134 GOT_OBJECTS_PACK_DIR);
1135 goto done;
1138 packdir = fdopendir(packdir_fd);
1139 if (packdir == NULL) {
1140 err = got_error_from_errno("fdopendir");
1141 goto done;
1144 while ((dent = readdir(packdir)) != NULL) {
1145 int is_cached = 0;
1147 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1148 continue;
1150 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1151 dent->d_name) == -1) {
1152 err = got_error_from_errno("asprintf");
1153 goto done;
1156 if (!got_repo_check_packidx_bloom_filter(repo,
1157 path_packidx, id)) {
1158 free(path_packidx);
1159 continue; /* object will not be found in this index */
1162 for (i = 0; i < repo->pack_cache_size; i++) {
1163 if (repo->packidx_cache[i] == NULL)
1164 break;
1165 if (strcmp(repo->packidx_cache[i]->path_packidx,
1166 path_packidx) == 0) {
1167 is_cached = 1;
1168 break;
1171 if (is_cached) {
1172 free(path_packidx);
1173 continue; /* already searched */
1176 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1177 path_packidx, 0);
1178 if (err) {
1179 free(path_packidx);
1180 goto done;
1183 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1184 if (err) {
1185 free(path_packidx);
1186 goto done;
1189 err = cache_packidx(repo, *packidx, path_packidx);
1190 free(path_packidx);
1191 if (err)
1192 goto done;
1194 *idx = got_packidx_get_object_idx(*packidx, id);
1195 if (*idx != -1) {
1196 err = NULL; /* found the object */
1197 goto done;
1201 err = got_error_no_obj(id);
1202 done:
1203 if (packdir && closedir(packdir) != 0 && err == NULL)
1204 err = got_error_from_errno("closedir");
1205 return err;
1208 const struct got_error *
1209 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1213 DIR *packdir = NULL;
1214 struct dirent *dent;
1215 char *path_packidx = NULL;
1216 int packdir_fd;
1218 packdir_fd = openat(got_repo_get_fd(repo),
1219 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1220 if (packdir_fd == -1) {
1221 return got_error_from_errno_fmt("openat: %s/%s",
1222 got_repo_get_path_git_dir(repo),
1223 GOT_OBJECTS_PACK_DIR);
1226 packdir = fdopendir(packdir_fd);
1227 if (packdir == NULL) {
1228 err = got_error_from_errno("fdopendir");
1229 goto done;
1232 while ((dent = readdir(packdir)) != NULL) {
1233 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1234 continue;
1236 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1237 dent->d_name) == -1) {
1238 err = got_error_from_errno("asprintf");
1239 path_packidx = NULL;
1240 break;
1243 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1244 if (err)
1245 break;
1247 done:
1248 if (err)
1249 free(path_packidx);
1250 if (packdir && closedir(packdir) != 0 && err == NULL)
1251 err = got_error_from_errno("closedir");
1252 return err;
1255 const struct got_error *
1256 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1257 struct got_repository *repo)
1259 const struct got_error *err;
1260 size_t i;
1262 *packidx = NULL;
1264 /* Search pack index cache. */
1265 for (i = 0; i < repo->pack_cache_size; i++) {
1266 if (repo->packidx_cache[i] == NULL)
1267 break;
1268 if (strcmp(repo->packidx_cache[i]->path_packidx,
1269 path_packidx) == 0) {
1270 *packidx = repo->packidx_cache[i];
1271 return NULL;
1274 /* No luck. Search the filesystem. */
1276 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1277 path_packidx, 0);
1278 if (err)
1279 return err;
1281 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1282 if (err)
1283 goto done;
1285 err = cache_packidx(repo, *packidx, path_packidx);
1286 done:
1287 if (err) {
1288 got_packidx_close(*packidx);
1289 *packidx = NULL;
1291 return err;
1294 static const struct got_error *
1295 read_packfile_hdr(int fd, struct got_packidx *packidx)
1297 const struct got_error *err = NULL;
1298 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1299 struct got_packfile_hdr hdr;
1300 ssize_t n;
1302 n = read(fd, &hdr, sizeof(hdr));
1303 if (n < 0)
1304 return got_error_from_errno("read");
1305 if (n != sizeof(hdr))
1306 return got_error(GOT_ERR_BAD_PACKFILE);
1308 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1309 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1310 be32toh(hdr.nobjects) != totobj)
1311 err = got_error(GOT_ERR_BAD_PACKFILE);
1313 return err;
1316 static const struct got_error *
1317 open_packfile(int *fd, struct got_repository *repo,
1318 const char *relpath, struct got_packidx *packidx)
1320 const struct got_error *err = NULL;
1322 *fd = openat(got_repo_get_fd(repo), relpath,
1323 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1324 if (*fd == -1)
1325 return got_error_from_errno_fmt("openat: %s/%s",
1326 got_repo_get_path_git_dir(repo), relpath);
1328 if (packidx) {
1329 err = read_packfile_hdr(*fd, packidx);
1330 if (err) {
1331 close(*fd);
1332 *fd = -1;
1336 return err;
1339 const struct got_error *
1340 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1341 const char *path_packfile, struct got_packidx *packidx)
1343 const struct got_error *err = NULL;
1344 struct got_pack *pack = NULL;
1345 struct stat sb;
1346 size_t i;
1348 if (packp)
1349 *packp = NULL;
1351 for (i = 0; i < repo->pack_cache_size; i++) {
1352 pack = &repo->packs[i];
1353 if (pack->path_packfile == NULL)
1354 break;
1355 if (strcmp(pack->path_packfile, path_packfile) == 0)
1356 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1359 if (i == repo->pack_cache_size) {
1360 err = got_pack_close(&repo->packs[i - 1]);
1361 if (err)
1362 return err;
1363 memmove(&repo->packs[1], &repo->packs[0],
1364 sizeof(repo->packs) - sizeof(repo->packs[0]));
1365 i = 0;
1368 pack = &repo->packs[i];
1370 pack->path_packfile = strdup(path_packfile);
1371 if (pack->path_packfile == NULL) {
1372 err = got_error_from_errno("strdup");
1373 goto done;
1376 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1377 if (err)
1378 goto done;
1380 if (fstat(pack->fd, &sb) != 0) {
1381 err = got_error_from_errno("fstat");
1382 goto done;
1384 pack->filesize = sb.st_size;
1386 pack->privsep_child = NULL;
1388 #ifndef GOT_PACK_NO_MMAP
1389 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1390 pack->fd, 0);
1391 if (pack->map == MAP_FAILED) {
1392 if (errno != ENOMEM) {
1393 err = got_error_from_errno("mmap");
1394 goto done;
1396 pack->map = NULL; /* fall back to read(2) */
1398 #endif
1399 done:
1400 if (err) {
1401 if (pack) {
1402 free(pack->path_packfile);
1403 memset(pack, 0, sizeof(*pack));
1405 } else if (packp)
1406 *packp = pack;
1407 return err;
1410 struct got_pack *
1411 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1413 struct got_pack *pack = NULL;
1414 size_t i;
1416 for (i = 0; i < repo->pack_cache_size; i++) {
1417 pack = &repo->packs[i];
1418 if (pack->path_packfile == NULL)
1419 break;
1420 if (strcmp(pack->path_packfile, path_packfile) == 0)
1421 return pack;
1424 return NULL;
1427 const struct got_error *
1428 got_repo_init(const char *repo_path)
1430 const struct got_error *err = NULL;
1431 const char *dirnames[] = {
1432 GOT_OBJECTS_DIR,
1433 GOT_OBJECTS_PACK_DIR,
1434 GOT_REFS_DIR,
1436 const char *description_str = "Unnamed repository; "
1437 "edit this file 'description' to name the repository.";
1438 const char *headref_str = "ref: refs/heads/main";
1439 const char *gitconfig_str = "[core]\n"
1440 "\trepositoryformatversion = 0\n"
1441 "\tfilemode = true\n"
1442 "\tbare = true\n";
1443 char *path;
1444 size_t i;
1446 if (!got_path_dir_is_empty(repo_path))
1447 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1449 for (i = 0; i < nitems(dirnames); i++) {
1450 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1451 return got_error_from_errno("asprintf");
1453 err = got_path_mkdir(path);
1454 free(path);
1455 if (err)
1456 return err;
1459 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1460 return got_error_from_errno("asprintf");
1461 err = got_path_create_file(path, description_str);
1462 free(path);
1463 if (err)
1464 return err;
1466 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1467 return got_error_from_errno("asprintf");
1468 err = got_path_create_file(path, headref_str);
1469 free(path);
1470 if (err)
1471 return err;
1473 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1474 return got_error_from_errno("asprintf");
1475 err = got_path_create_file(path, gitconfig_str);
1476 free(path);
1477 if (err)
1478 return err;
1480 return NULL;
1483 static const struct got_error *
1484 match_packed_object(struct got_object_id **unique_id,
1485 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1487 const struct got_error *err = NULL;
1488 DIR *packdir = NULL;
1489 struct dirent *dent;
1490 char *path_packidx;
1491 struct got_object_id_queue matched_ids;
1492 int packdir_fd;
1494 STAILQ_INIT(&matched_ids);
1496 packdir_fd = openat(got_repo_get_fd(repo),
1497 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1498 if (packdir_fd == -1) {
1499 if (errno != ENOENT) {
1500 err = got_error_from_errno2("openat",
1501 GOT_OBJECTS_PACK_DIR);
1503 goto done;
1506 packdir = fdopendir(packdir_fd);
1507 if (packdir == NULL) {
1508 err = got_error_from_errno("fdopendir");
1509 goto done;
1512 while ((dent = readdir(packdir)) != NULL) {
1513 struct got_packidx *packidx;
1514 struct got_object_qid *qid;
1516 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1517 continue;
1519 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1520 dent->d_name) == -1) {
1521 err = got_error_from_errno("strdup");
1522 break;
1525 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1526 path_packidx, 0);
1527 free(path_packidx);
1528 if (err)
1529 break;
1531 err = got_packidx_match_id_str_prefix(&matched_ids,
1532 packidx, id_str_prefix);
1533 if (err) {
1534 got_packidx_close(packidx);
1535 break;
1537 err = got_packidx_close(packidx);
1538 if (err)
1539 break;
1541 STAILQ_FOREACH(qid, &matched_ids, entry) {
1542 if (obj_type != GOT_OBJ_TYPE_ANY) {
1543 int matched_type;
1544 err = got_object_get_type(&matched_type, repo,
1545 qid->id);
1546 if (err)
1547 goto done;
1548 if (matched_type != obj_type)
1549 continue;
1551 if (*unique_id == NULL) {
1552 *unique_id = got_object_id_dup(qid->id);
1553 if (*unique_id == NULL) {
1554 err = got_error_from_errno("malloc");
1555 goto done;
1557 } else {
1558 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1559 continue; /* packed multiple times */
1560 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1561 goto done;
1565 done:
1566 got_object_id_queue_free(&matched_ids);
1567 if (packdir && closedir(packdir) != 0 && err == NULL)
1568 err = got_error_from_errno("closedir");
1569 if (err) {
1570 free(*unique_id);
1571 *unique_id = NULL;
1573 return err;
1576 static const struct got_error *
1577 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1578 const char *object_dir, const char *id_str_prefix, int obj_type,
1579 struct got_repository *repo)
1581 const struct got_error *err = NULL;
1582 char *path;
1583 DIR *dir = NULL;
1584 struct dirent *dent;
1585 struct got_object_id id;
1587 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1588 err = got_error_from_errno("asprintf");
1589 goto done;
1592 dir = opendir(path);
1593 if (dir == NULL) {
1594 if (errno == ENOENT) {
1595 err = NULL;
1596 goto done;
1598 err = got_error_from_errno2("opendir", path);
1599 goto done;
1601 while ((dent = readdir(dir)) != NULL) {
1602 char *id_str;
1603 int cmp;
1605 if (strcmp(dent->d_name, ".") == 0 ||
1606 strcmp(dent->d_name, "..") == 0)
1607 continue;
1609 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1610 err = got_error_from_errno("asprintf");
1611 goto done;
1614 if (!got_parse_sha1_digest(id.sha1, id_str))
1615 continue;
1618 * Directory entries do not necessarily appear in
1619 * sorted order, so we must iterate over all of them.
1621 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1622 if (cmp != 0) {
1623 free(id_str);
1624 continue;
1627 if (*unique_id == NULL) {
1628 if (obj_type != GOT_OBJ_TYPE_ANY) {
1629 int matched_type;
1630 err = got_object_get_type(&matched_type, repo,
1631 &id);
1632 if (err)
1633 goto done;
1634 if (matched_type != obj_type)
1635 continue;
1637 *unique_id = got_object_id_dup(&id);
1638 if (*unique_id == NULL) {
1639 err = got_error_from_errno("got_object_id_dup");
1640 free(id_str);
1641 goto done;
1643 } else {
1644 if (got_object_id_cmp(*unique_id, &id) == 0)
1645 continue; /* both packed and loose */
1646 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1647 free(id_str);
1648 goto done;
1651 done:
1652 if (dir && closedir(dir) != 0 && err == NULL)
1653 err = got_error_from_errno("closedir");
1654 if (err) {
1655 free(*unique_id);
1656 *unique_id = NULL;
1658 free(path);
1659 return err;
1662 const struct got_error *
1663 got_repo_match_object_id_prefix(struct got_object_id **id,
1664 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1666 const struct got_error *err = NULL;
1667 char *path_objects = got_repo_get_path_objects(repo);
1668 char *object_dir = NULL;
1669 size_t len;
1670 int i;
1672 *id = NULL;
1674 for (i = 0; i < strlen(id_str_prefix); i++) {
1675 if (isxdigit((unsigned char)id_str_prefix[i]))
1676 continue;
1677 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1680 len = strlen(id_str_prefix);
1681 if (len >= 2) {
1682 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1683 if (err)
1684 goto done;
1685 object_dir = strndup(id_str_prefix, 2);
1686 if (object_dir == NULL) {
1687 err = got_error_from_errno("strdup");
1688 goto done;
1690 err = match_loose_object(id, path_objects, object_dir,
1691 id_str_prefix, obj_type, repo);
1692 } else if (len == 1) {
1693 int i;
1694 for (i = 0; i < 0xf; i++) {
1695 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1696 == -1) {
1697 err = got_error_from_errno("asprintf");
1698 goto done;
1700 err = match_packed_object(id, repo, object_dir,
1701 obj_type);
1702 if (err)
1703 goto done;
1704 err = match_loose_object(id, path_objects, object_dir,
1705 id_str_prefix, obj_type, repo);
1706 if (err)
1707 goto done;
1709 } else {
1710 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1711 goto done;
1713 done:
1714 free(object_dir);
1715 if (err) {
1716 free(*id);
1717 *id = NULL;
1718 } else if (*id == NULL) {
1719 switch (obj_type) {
1720 case GOT_OBJ_TYPE_BLOB:
1721 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1722 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1723 break;
1724 case GOT_OBJ_TYPE_TREE:
1725 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1726 GOT_OBJ_LABEL_TREE, id_str_prefix);
1727 break;
1728 case GOT_OBJ_TYPE_COMMIT:
1729 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1730 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1731 break;
1732 case GOT_OBJ_TYPE_TAG:
1733 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1734 GOT_OBJ_LABEL_TAG, id_str_prefix);
1735 break;
1736 default:
1737 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1738 break;
1742 return err;
1745 const struct got_error *
1746 got_repo_match_object_id(struct got_object_id **id, char **label,
1747 const char *id_str, int obj_type, struct got_reflist_head *refs,
1748 struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_tag_object *tag;
1752 struct got_reference *ref = NULL;
1754 *id = NULL;
1755 if (label)
1756 *label = NULL;
1758 if (refs) {
1759 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1760 refs, repo);
1761 if (err == NULL) {
1762 *id = got_object_id_dup(
1763 got_object_tag_get_object_id(tag));
1764 if (*id == NULL)
1765 err = got_error_from_errno("got_object_id_dup");
1766 else if (label && asprintf(label, "refs/tags/%s",
1767 got_object_tag_get_name(tag)) == -1) {
1768 err = got_error_from_errno("asprintf");
1769 free(*id);
1770 *id = NULL;
1772 got_object_tag_close(tag);
1773 return err;
1774 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1775 err->code != GOT_ERR_NO_OBJ)
1776 return err;
1779 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1780 if (err) {
1781 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1782 return err;
1783 err = got_ref_open(&ref, repo, id_str, 0);
1784 if (err != NULL)
1785 goto done;
1786 if (label) {
1787 *label = strdup(got_ref_get_name(ref));
1788 if (*label == NULL) {
1789 err = got_error_from_errno("strdup");
1790 goto done;
1793 err = got_ref_resolve(id, repo, ref);
1794 } else if (label) {
1795 err = got_object_id_str(label, *id);
1796 if (*label == NULL) {
1797 err = got_error_from_errno("strdup");
1798 goto done;
1801 done:
1802 if (ref)
1803 got_ref_close(ref);
1804 return err;
1807 const struct got_error *
1808 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1809 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_reflist_entry *re;
1813 struct got_object_id *tag_id;
1814 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1816 *tag = NULL;
1818 TAILQ_FOREACH(re, refs, entry) {
1819 const char *refname;
1820 refname = got_ref_get_name(re->ref);
1821 if (got_ref_is_symbolic(re->ref))
1822 continue;
1823 if (strncmp(refname, "refs/tags/", 10) != 0)
1824 continue;
1825 if (!name_is_absolute)
1826 refname += strlen("refs/tags/");
1827 if (strcmp(refname, name) != 0)
1828 continue;
1829 err = got_ref_resolve(&tag_id, repo, re->ref);
1830 if (err)
1831 break;
1832 err = got_object_open_as_tag(tag, repo, tag_id);
1833 free(tag_id);
1834 if (err)
1835 break;
1836 if (obj_type == GOT_OBJ_TYPE_ANY ||
1837 got_object_tag_get_object_type(*tag) == obj_type)
1838 break;
1839 got_object_tag_close(*tag);
1840 *tag = NULL;
1843 if (err == NULL && *tag == NULL)
1844 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1845 GOT_OBJ_LABEL_TAG, name);
1846 return err;
1849 static const struct got_error *
1850 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1851 const char *name, mode_t mode, struct got_object_id *blob_id)
1853 const struct got_error *err = NULL;
1855 *new_te = NULL;
1857 *new_te = calloc(1, sizeof(**new_te));
1858 if (*new_te == NULL)
1859 return got_error_from_errno("calloc");
1861 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1862 sizeof((*new_te)->name)) {
1863 err = got_error(GOT_ERR_NO_SPACE);
1864 goto done;
1867 if (S_ISLNK(mode)) {
1868 (*new_te)->mode = S_IFLNK;
1869 } else {
1870 (*new_te)->mode = S_IFREG;
1871 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1873 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1874 done:
1875 if (err && *new_te) {
1876 free(*new_te);
1877 *new_te = NULL;
1879 return err;
1882 static const struct got_error *
1883 import_file(struct got_tree_entry **new_te, struct dirent *de,
1884 const char *path, struct got_repository *repo)
1886 const struct got_error *err;
1887 struct got_object_id *blob_id = NULL;
1888 char *filepath;
1889 struct stat sb;
1891 if (asprintf(&filepath, "%s%s%s", path,
1892 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1893 return got_error_from_errno("asprintf");
1895 if (lstat(filepath, &sb) != 0) {
1896 err = got_error_from_errno2("lstat", path);
1897 goto done;
1900 err = got_object_blob_create(&blob_id, filepath, repo);
1901 if (err)
1902 goto done;
1904 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1905 blob_id);
1906 done:
1907 free(filepath);
1908 if (err)
1909 free(blob_id);
1910 return err;
1913 static const struct got_error *
1914 insert_tree_entry(struct got_tree_entry *new_te,
1915 struct got_pathlist_head *paths)
1917 const struct got_error *err = NULL;
1918 struct got_pathlist_entry *new_pe;
1920 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1921 if (err)
1922 return err;
1923 if (new_pe == NULL)
1924 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1925 return NULL;
1928 static const struct got_error *write_tree(struct got_object_id **,
1929 const char *, struct got_pathlist_head *, struct got_repository *,
1930 got_repo_import_cb progress_cb, void *progress_arg);
1932 static const struct got_error *
1933 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1934 const char *path, struct got_pathlist_head *ignores,
1935 struct got_repository *repo,
1936 got_repo_import_cb progress_cb, void *progress_arg)
1938 const struct got_error *err;
1939 struct got_object_id *id = NULL;
1940 char *subdirpath;
1942 if (asprintf(&subdirpath, "%s%s%s", path,
1943 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1944 return got_error_from_errno("asprintf");
1946 (*new_te) = calloc(1, sizeof(**new_te));
1947 if (*new_te == NULL)
1948 return got_error_from_errno("calloc");
1949 (*new_te)->mode = S_IFDIR;
1950 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1951 sizeof((*new_te)->name)) {
1952 err = got_error(GOT_ERR_NO_SPACE);
1953 goto done;
1955 err = write_tree(&id, subdirpath, ignores, repo,
1956 progress_cb, progress_arg);
1957 if (err)
1958 goto done;
1959 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1961 done:
1962 free(id);
1963 free(subdirpath);
1964 if (err) {
1965 free(*new_te);
1966 *new_te = NULL;
1968 return err;
1971 static const struct got_error *
1972 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1973 struct got_pathlist_head *ignores, struct got_repository *repo,
1974 got_repo_import_cb progress_cb, void *progress_arg)
1976 const struct got_error *err = NULL;
1977 DIR *dir;
1978 struct dirent *de;
1979 int nentries;
1980 struct got_tree_entry *new_te = NULL;
1981 struct got_pathlist_head paths;
1982 struct got_pathlist_entry *pe;
1984 *new_tree_id = NULL;
1986 TAILQ_INIT(&paths);
1988 dir = opendir(path_dir);
1989 if (dir == NULL) {
1990 err = got_error_from_errno2("opendir", path_dir);
1991 goto done;
1994 nentries = 0;
1995 while ((de = readdir(dir)) != NULL) {
1996 int ignore = 0;
1997 int type;
1999 if (strcmp(de->d_name, ".") == 0 ||
2000 strcmp(de->d_name, "..") == 0)
2001 continue;
2003 TAILQ_FOREACH(pe, ignores, entry) {
2004 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2005 ignore = 1;
2006 break;
2009 if (ignore)
2010 continue;
2012 err = got_path_dirent_type(&type, path_dir, de);
2013 if (err)
2014 goto done;
2016 if (type == DT_DIR) {
2017 err = import_subdir(&new_te, de, path_dir,
2018 ignores, repo, progress_cb, progress_arg);
2019 if (err) {
2020 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2021 goto done;
2022 err = NULL;
2023 continue;
2025 } else if (type == DT_REG || type == DT_LNK) {
2026 err = import_file(&new_te, de, path_dir, repo);
2027 if (err)
2028 goto done;
2029 } else
2030 continue;
2032 err = insert_tree_entry(new_te, &paths);
2033 if (err)
2034 goto done;
2035 nentries++;
2038 if (TAILQ_EMPTY(&paths)) {
2039 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2040 "cannot create tree without any entries");
2041 goto done;
2044 TAILQ_FOREACH(pe, &paths, entry) {
2045 struct got_tree_entry *te = pe->data;
2046 char *path;
2047 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2048 continue;
2049 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2053 err = (*progress_cb)(progress_arg, path);
2054 free(path);
2055 if (err)
2056 goto done;
2059 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2060 done:
2061 if (dir)
2062 closedir(dir);
2063 got_pathlist_free(&paths);
2064 return err;
2067 const struct got_error *
2068 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2069 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2070 struct got_repository *repo, got_repo_import_cb progress_cb,
2071 void *progress_arg)
2073 const struct got_error *err;
2074 struct got_object_id *new_tree_id;
2076 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2077 progress_cb, progress_arg);
2078 if (err)
2079 return err;
2081 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2082 author, time(NULL), author, time(NULL), logmsg, repo);
2083 free(new_tree_id);
2084 return err;
2087 const struct got_error *
2088 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2089 struct got_repository *repo)
2091 const struct got_error *err = NULL;
2092 char *path_objects = NULL, *path = NULL;
2093 DIR *dir = NULL;
2094 struct got_object_id id;
2095 int i;
2097 *nobjects = 0;
2098 *ondisk_size = 0;
2100 path_objects = got_repo_get_path_objects(repo);
2101 if (path_objects == NULL)
2102 return got_error_from_errno("got_repo_get_path_objects");
2104 for (i = 0; i <= 0xff; i++) {
2105 struct dirent *dent;
2107 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2108 err = got_error_from_errno("asprintf");
2109 break;
2112 dir = opendir(path);
2113 if (dir == NULL) {
2114 if (errno == ENOENT) {
2115 err = NULL;
2116 continue;
2118 err = got_error_from_errno2("opendir", path);
2119 break;
2122 while ((dent = readdir(dir)) != NULL) {
2123 char *id_str;
2124 int fd;
2125 struct stat sb;
2127 if (strcmp(dent->d_name, ".") == 0 ||
2128 strcmp(dent->d_name, "..") == 0)
2129 continue;
2131 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2132 err = got_error_from_errno("asprintf");
2133 goto done;
2136 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2137 free(id_str);
2138 continue;
2140 free(id_str);
2142 err = got_object_open_loose_fd(&fd, &id, repo);
2143 if (err)
2144 goto done;
2146 if (fstat(fd, &sb) == -1) {
2147 err = got_error_from_errno("fstat");
2148 close(fd);
2149 goto done;
2151 (*nobjects)++;
2152 (*ondisk_size) += sb.st_size;
2154 if (close(fd) == -1) {
2155 err = got_error_from_errno("close");
2156 goto done;
2160 if (closedir(dir) != 0) {
2161 err = got_error_from_errno("closedir");
2162 goto done;
2164 dir = NULL;
2166 free(path);
2167 path = NULL;
2169 done:
2170 if (dir && closedir(dir) != 0 && err == NULL)
2171 err = got_error_from_errno("closedir");
2173 if (err) {
2174 *nobjects = 0;
2175 *ondisk_size = 0;
2177 free(path_objects);
2178 free(path);
2179 return err;
2182 const struct got_error *
2183 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2184 off_t *total_packsize, struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 DIR *packdir = NULL;
2188 struct dirent *dent;
2189 struct got_packidx *packidx = NULL;
2190 char *path_packidx;
2191 char *path_packfile;
2192 int packdir_fd;
2193 struct stat sb;
2195 *npackfiles = 0;
2196 *nobjects = 0;
2197 *total_packsize = 0;
2199 packdir_fd = openat(got_repo_get_fd(repo),
2200 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2201 if (packdir_fd == -1) {
2202 return got_error_from_errno_fmt("openat: %s/%s",
2203 got_repo_get_path_git_dir(repo),
2204 GOT_OBJECTS_PACK_DIR);
2207 packdir = fdopendir(packdir_fd);
2208 if (packdir == NULL) {
2209 err = got_error_from_errno("fdopendir");
2210 goto done;
2213 while ((dent = readdir(packdir)) != NULL) {
2214 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2215 continue;
2217 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2218 dent->d_name) == -1) {
2219 err = got_error_from_errno("asprintf");
2220 goto done;
2223 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2224 path_packidx, 0);
2225 free(path_packidx);
2226 if (err)
2227 goto done;
2229 if (fstat(packidx->fd, &sb) == -1)
2230 goto done;
2231 *total_packsize += sb.st_size;
2233 err = got_packidx_get_packfile_path(&path_packfile,
2234 packidx->path_packidx);
2235 if (err)
2236 goto done;
2238 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2239 0) == -1) {
2240 free(path_packfile);
2241 goto done;
2243 free(path_packfile);
2244 *total_packsize += sb.st_size;
2246 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2248 (*npackfiles)++;
2250 got_packidx_close(packidx);
2251 packidx = NULL;
2253 done:
2254 if (packidx)
2255 got_packidx_close(packidx);
2256 if (packdir && closedir(packdir) != 0 && err == NULL)
2257 err = got_error_from_errno("closedir");
2258 if (err) {
2259 *npackfiles = 0;
2260 *nobjects = 0;
2261 *total_packsize = 0;
2263 return err;
2266 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2267 got_packidx_bloom_filter_cmp);