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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, *pack_fds_tmp;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
257 free(pack_fds_tmp);
258 return got_error_from_errno("calloc");
261 /*
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), resulting in a close(0).
265 */
266 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
267 pack_fds_tmp[i] = -1;
269 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
270 pack_fds_tmp[i] = got_opentempfd();
271 if (pack_fds_tmp[i] == -1) {
272 err = got_error_from_errno("got_opentempfd");
273 got_repo_pack_fds_close(pack_fds_tmp);
274 return err;
277 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
278 return err;
281 const struct got_error *
282 got_repo_pack_fds_close(int *pack_fds)
284 const struct got_error *err = NULL;
285 int i;
287 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
288 if (pack_fds[i] == -1)
289 continue;
290 if (close(pack_fds[i]) == -1) {
291 err = got_error_from_errno("close");
292 break;
295 free(pack_fds);
296 return err;
299 const struct got_error *
300 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
301 struct got_object *obj)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->objcache, id, obj);
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 obj->refcnt++;
313 #endif
314 return NULL;
317 struct got_object *
318 got_repo_get_cached_object(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
324 const struct got_error *
325 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
326 struct got_tree_object *tree)
328 #ifndef GOT_NO_OBJ_CACHE
329 const struct got_error *err = NULL;
330 err = got_object_cache_add(&repo->treecache, id, tree);
331 if (err) {
332 if (err->code == GOT_ERR_OBJ_EXISTS ||
333 err->code == GOT_ERR_OBJ_TOO_LARGE)
334 err = NULL;
335 return err;
337 tree->refcnt++;
338 #endif
339 return NULL;
342 struct got_tree_object *
343 got_repo_get_cached_tree(struct got_repository *repo,
344 struct got_object_id *id)
346 return (struct got_tree_object *)got_object_cache_get(
347 &repo->treecache, id);
350 const struct got_error *
351 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
352 struct got_commit_object *commit)
354 #ifndef GOT_NO_OBJ_CACHE
355 const struct got_error *err = NULL;
356 err = got_object_cache_add(&repo->commitcache, id, commit);
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 commit->refcnt++;
364 #endif
365 return NULL;
368 struct got_commit_object *
369 got_repo_get_cached_commit(struct got_repository *repo,
370 struct got_object_id *id)
372 return (struct got_commit_object *)got_object_cache_get(
373 &repo->commitcache, id);
376 const struct got_error *
377 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
378 struct got_tag_object *tag)
380 #ifndef GOT_NO_OBJ_CACHE
381 const struct got_error *err = NULL;
382 err = got_object_cache_add(&repo->tagcache, id, tag);
383 if (err) {
384 if (err->code == GOT_ERR_OBJ_EXISTS ||
385 err->code == GOT_ERR_OBJ_TOO_LARGE)
386 err = NULL;
387 return err;
389 tag->refcnt++;
390 #endif
391 return NULL;
394 struct got_tag_object *
395 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
397 return (struct got_tag_object *)got_object_cache_get(
398 &repo->tagcache, id);
401 const struct got_error *
402 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
403 struct got_raw_object *raw)
405 #ifndef GOT_NO_OBJ_CACHE
406 const struct got_error *err = NULL;
407 err = got_object_cache_add(&repo->rawcache, id, raw);
408 if (err) {
409 if (err->code == GOT_ERR_OBJ_EXISTS ||
410 err->code == GOT_ERR_OBJ_TOO_LARGE)
411 err = NULL;
412 return err;
414 raw->refcnt++;
415 #endif
416 return NULL;
420 struct got_raw_object *
421 got_repo_get_cached_raw_object(struct got_repository *repo,
422 struct got_object_id *id)
424 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
428 static const struct got_error *
429 open_repo(struct got_repository *repo, const char *path)
431 const struct got_error *err = NULL;
433 repo->gitdir_fd = -1;
435 /* bare git repository? */
436 repo->path_git_dir = strdup(path);
437 if (repo->path_git_dir == NULL)
438 return got_error_from_errno("strdup");
439 if (is_git_repo(repo)) {
440 repo->path = strdup(repo->path_git_dir);
441 if (repo->path == NULL) {
442 err = got_error_from_errno("strdup");
443 goto done;
445 repo->gitdir_fd = open(repo->path_git_dir,
446 O_DIRECTORY | O_CLOEXEC);
447 if (repo->gitdir_fd == -1) {
448 err = got_error_from_errno2("open",
449 repo->path_git_dir);
450 goto done;
452 return NULL;
455 /* git repository with working tree? */
456 free(repo->path_git_dir);
457 repo->path_git_dir = NULL;
458 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
459 err = got_error_from_errno("asprintf");
460 goto done;
462 if (is_git_repo(repo)) {
463 repo->path = strdup(path);
464 if (repo->path == NULL) {
465 err = got_error_from_errno("strdup");
466 goto done;
468 repo->gitdir_fd = open(repo->path_git_dir,
469 O_DIRECTORY | O_CLOEXEC);
470 if (repo->gitdir_fd == -1) {
471 err = got_error_from_errno2("open",
472 repo->path_git_dir);
473 goto done;
475 return NULL;
478 err = got_error(GOT_ERR_NOT_GIT_REPO);
479 done:
480 if (err) {
481 free(repo->path);
482 repo->path = NULL;
483 free(repo->path_git_dir);
484 repo->path_git_dir = NULL;
485 if (repo->gitdir_fd != -1)
486 close(repo->gitdir_fd);
487 repo->gitdir_fd = -1;
490 return err;
493 static const struct got_error *
494 parse_gitconfig_file(int *gitconfig_repository_format_version,
495 char **gitconfig_author_name, char **gitconfig_author_email,
496 struct got_remote_repo **remotes, int *nremotes,
497 char **gitconfig_owner, char ***extensions, int *nextensions,
498 const char *gitconfig_path)
500 const struct got_error *err = NULL, *child_err = NULL;
501 int fd = -1;
502 int imsg_fds[2] = { -1, -1 };
503 pid_t pid;
504 struct imsgbuf *ibuf;
506 *gitconfig_repository_format_version = 0;
507 if (extensions)
508 *extensions = NULL;
509 if (nextensions)
510 *nextensions = 0;
511 *gitconfig_author_name = NULL;
512 *gitconfig_author_email = NULL;
513 if (remotes)
514 *remotes = NULL;
515 if (nremotes)
516 *nremotes = 0;
517 if (gitconfig_owner)
518 *gitconfig_owner = NULL;
520 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
521 if (fd == -1) {
522 if (errno == ENOENT)
523 return NULL;
524 return got_error_from_errno2("open", gitconfig_path);
527 ibuf = calloc(1, sizeof(*ibuf));
528 if (ibuf == NULL) {
529 err = got_error_from_errno("calloc");
530 goto done;
533 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
534 err = got_error_from_errno("socketpair");
535 goto done;
538 pid = fork();
539 if (pid == -1) {
540 err = got_error_from_errno("fork");
541 goto done;
542 } else if (pid == 0) {
543 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
544 gitconfig_path);
545 /* not reached */
548 if (close(imsg_fds[1]) == -1) {
549 err = got_error_from_errno("close");
550 goto done;
552 imsg_fds[1] = -1;
553 imsg_init(ibuf, imsg_fds[0]);
555 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
556 if (err)
557 goto done;
558 fd = -1;
560 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
561 if (err)
562 goto done;
564 err = got_privsep_recv_gitconfig_int(
565 gitconfig_repository_format_version, ibuf);
566 if (err)
567 goto done;
569 if (extensions && nextensions) {
570 err = got_privsep_send_gitconfig_repository_extensions_req(
571 ibuf);
572 if (err)
573 goto done;
574 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
575 if (err)
576 goto done;
577 if (*nextensions > 0) {
578 int i;
579 *extensions = calloc(*nextensions, sizeof(char *));
580 if (*extensions == NULL) {
581 err = got_error_from_errno("calloc");
582 goto done;
584 for (i = 0; i < *nextensions; i++) {
585 char *ext;
586 err = got_privsep_recv_gitconfig_str(&ext,
587 ibuf);
588 if (err)
589 goto done;
590 (*extensions)[i] = ext;
595 err = got_privsep_send_gitconfig_author_name_req(ibuf);
596 if (err)
597 goto done;
599 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
600 if (err)
601 goto done;
603 err = got_privsep_send_gitconfig_author_email_req(ibuf);
604 if (err)
605 goto done;
607 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
608 if (err)
609 goto done;
611 if (remotes && nremotes) {
612 err = got_privsep_send_gitconfig_remotes_req(ibuf);
613 if (err)
614 goto done;
616 err = got_privsep_recv_gitconfig_remotes(remotes,
617 nremotes, ibuf);
618 if (err)
619 goto done;
622 if (gitconfig_owner) {
623 err = got_privsep_send_gitconfig_owner_req(ibuf);
624 if (err)
625 goto done;
626 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
627 if (err)
628 goto done;
631 err = got_privsep_send_stop(imsg_fds[0]);
632 child_err = got_privsep_wait_for_child(pid);
633 if (child_err && err == NULL)
634 err = child_err;
635 done:
636 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
637 err = got_error_from_errno("close");
638 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
639 err = got_error_from_errno("close");
640 if (fd != -1 && close(fd) == -1 && err == NULL)
641 err = got_error_from_errno2("close", gitconfig_path);
642 free(ibuf);
643 return err;
646 static const struct got_error *
647 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
649 const struct got_error *err = NULL;
650 char *repo_gitconfig_path = NULL;
652 if (global_gitconfig_path) {
653 /* Read settings from ~/.gitconfig. */
654 int dummy_repo_version;
655 err = parse_gitconfig_file(&dummy_repo_version,
656 &repo->global_gitconfig_author_name,
657 &repo->global_gitconfig_author_email,
658 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
659 if (err)
660 return err;
663 /* Read repository's .git/config file. */
664 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
665 if (repo_gitconfig_path == NULL)
666 return got_error_from_errno("got_repo_get_path_gitconfig");
668 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
669 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
670 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
671 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
672 repo_gitconfig_path);
673 if (err)
674 goto done;
675 done:
676 free(repo_gitconfig_path);
677 return err;
680 static const struct got_error *
681 read_gotconfig(struct got_repository *repo)
683 const struct got_error *err = NULL;
684 char *gotconfig_path;
686 gotconfig_path = got_repo_get_path_gotconfig(repo);
687 if (gotconfig_path == NULL)
688 return got_error_from_errno("got_repo_get_path_gotconfig");
690 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
691 free(gotconfig_path);
692 return err;
695 /* Supported repository format extensions. */
696 static const char *const repo_extensions[] = {
697 "noop", /* Got supports repository format version 1. */
698 "preciousObjects", /* Supported by gotadmin cleanup. */
699 "worktreeConfig", /* Got does not care about Git work trees. */
700 };
702 const struct got_error *
703 got_repo_open(struct got_repository **repop, const char *path,
704 const char *global_gitconfig_path, int *pack_fds)
706 struct got_repository *repo = NULL;
707 const struct got_error *err = NULL;
708 char *repo_path = NULL;
709 size_t i, j = 0;
710 struct rlimit rl;
712 *repop = NULL;
714 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
715 return got_error_from_errno("getrlimit");
717 repo = calloc(1, sizeof(*repo));
718 if (repo == NULL)
719 return got_error_from_errno("calloc");
721 RB_INIT(&repo->packidx_bloom_filters);
722 TAILQ_INIT(&repo->packidx_paths);
724 for (i = 0; i < nitems(repo->privsep_children); i++) {
725 memset(&repo->privsep_children[i], 0,
726 sizeof(repo->privsep_children[0]));
727 repo->privsep_children[i].imsg_fd = -1;
730 err = got_object_cache_init(&repo->objcache,
731 GOT_OBJECT_CACHE_TYPE_OBJ);
732 if (err)
733 goto done;
734 err = got_object_cache_init(&repo->treecache,
735 GOT_OBJECT_CACHE_TYPE_TREE);
736 if (err)
737 goto done;
738 err = got_object_cache_init(&repo->commitcache,
739 GOT_OBJECT_CACHE_TYPE_COMMIT);
740 if (err)
741 goto done;
742 err = got_object_cache_init(&repo->tagcache,
743 GOT_OBJECT_CACHE_TYPE_TAG);
744 if (err)
745 goto done;
746 err = got_object_cache_init(&repo->rawcache,
747 GOT_OBJECT_CACHE_TYPE_RAW);
748 if (err)
749 goto done;
751 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
752 if (repo->pack_cache_size > rl.rlim_cur / 8)
753 repo->pack_cache_size = rl.rlim_cur / 8;
754 for (i = 0; i < nitems(repo->packs); i++) {
755 if (i < repo->pack_cache_size) {
756 repo->packs[i].basefd = pack_fds[j++];
757 repo->packs[i].accumfd = pack_fds[j++];
758 } else {
759 repo->packs[i].basefd = -1;
760 repo->packs[i].accumfd = -1;
763 repo->pinned_pack = -1;
764 repo->pinned_packidx = -1;
765 repo->pinned_pid = 0;
767 repo_path = realpath(path, NULL);
768 if (repo_path == NULL) {
769 err = got_error_from_errno2("realpath", path);
770 goto done;
773 for (;;) {
774 char *parent_path;
776 err = open_repo(repo, repo_path);
777 if (err == NULL)
778 break;
779 if (err->code != GOT_ERR_NOT_GIT_REPO)
780 goto done;
781 if (repo_path[0] == '/' && repo_path[1] == '\0') {
782 err = got_error(GOT_ERR_NOT_GIT_REPO);
783 goto done;
785 err = got_path_dirname(&parent_path, repo_path);
786 if (err)
787 goto done;
788 free(repo_path);
789 repo_path = parent_path;
792 err = read_gotconfig(repo);
793 if (err)
794 goto done;
796 err = read_gitconfig(repo, global_gitconfig_path);
797 if (err)
798 goto done;
799 if (repo->gitconfig_repository_format_version != 0) {
800 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
801 goto done;
803 for (i = 0; i < repo->nextensions; i++) {
804 char *ext = repo->extensions[i];
805 int j, supported = 0;
806 for (j = 0; j < nitems(repo_extensions); j++) {
807 if (strcmp(ext, repo_extensions[j]) == 0) {
808 supported = 1;
809 break;
812 if (!supported) {
813 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
814 goto done;
818 err = got_repo_list_packidx(&repo->packidx_paths, repo);
819 done:
820 if (err)
821 got_repo_close(repo);
822 else
823 *repop = repo;
824 free(repo_path);
825 return err;
828 const struct got_error *
829 got_repo_close(struct got_repository *repo)
831 const struct got_error *err = NULL, *child_err;
832 struct got_packidx_bloom_filter *bf;
833 struct got_pathlist_entry *pe;
834 size_t i;
836 for (i = 0; i < repo->pack_cache_size; i++) {
837 if (repo->packidx_cache[i] == NULL)
838 break;
839 got_packidx_close(repo->packidx_cache[i]);
842 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters))) {
844 RB_REMOVE(got_packidx_bloom_filter_tree,
845 &repo->packidx_bloom_filters, bf);
846 free(bf->bloom);
847 free(bf);
850 for (i = 0; i < repo->pack_cache_size; i++)
851 if (repo->packs[i].path_packfile)
852 if (repo->packs[i].path_packfile)
853 got_pack_close(&repo->packs[i]);
855 free(repo->path);
856 free(repo->path_git_dir);
858 got_object_cache_close(&repo->objcache);
859 got_object_cache_close(&repo->treecache);
860 got_object_cache_close(&repo->commitcache);
861 got_object_cache_close(&repo->tagcache);
862 got_object_cache_close(&repo->rawcache);
864 for (i = 0; i < nitems(repo->privsep_children); i++) {
865 if (repo->privsep_children[i].imsg_fd == -1)
866 continue;
867 imsg_clear(repo->privsep_children[i].ibuf);
868 free(repo->privsep_children[i].ibuf);
869 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
870 child_err = got_privsep_wait_for_child(
871 repo->privsep_children[i].pid);
872 if (child_err && err == NULL)
873 err = child_err;
874 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
875 err == NULL)
876 err = got_error_from_errno("close");
879 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
880 err == NULL)
881 err = got_error_from_errno("close");
883 if (repo->gotconfig)
884 got_gotconfig_free(repo->gotconfig);
885 free(repo->gitconfig_author_name);
886 free(repo->gitconfig_author_email);
887 for (i = 0; i < repo->ngitconfig_remotes; i++)
888 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
889 free(repo->gitconfig_remotes);
890 for (i = 0; i < repo->nextensions; i++)
891 free(repo->extensions[i]);
892 free(repo->extensions);
894 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
895 free((void *)pe->path);
896 got_pathlist_free(&repo->packidx_paths);
897 free(repo);
899 return err;
902 void
903 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
905 int i;
907 free(repo->name);
908 repo->name = NULL;
909 free(repo->fetch_url);
910 repo->fetch_url = NULL;
911 free(repo->send_url);
912 repo->send_url = NULL;
913 for (i = 0; i < repo->nfetch_branches; i++)
914 free(repo->fetch_branches[i]);
915 free(repo->fetch_branches);
916 repo->fetch_branches = NULL;
917 repo->nfetch_branches = 0;
918 for (i = 0; i < repo->nsend_branches; i++)
919 free(repo->send_branches[i]);
920 free(repo->send_branches);
921 repo->send_branches = NULL;
922 repo->nsend_branches = 0;
925 const struct got_error *
926 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
927 const char *input_path)
929 const struct got_error *err = NULL;
930 const char *repo_abspath = NULL;
931 size_t repolen, len;
932 char *canonpath, *path = NULL;
934 *in_repo_path = NULL;
936 canonpath = strdup(input_path);
937 if (canonpath == NULL) {
938 err = got_error_from_errno("strdup");
939 goto done;
941 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
942 if (err)
943 goto done;
945 repo_abspath = got_repo_get_path(repo);
947 if (canonpath[0] == '\0') {
948 path = strdup(canonpath);
949 if (path == NULL) {
950 err = got_error_from_errno("strdup");
951 goto done;
953 } else {
954 path = realpath(canonpath, NULL);
955 if (path == NULL) {
956 if (errno != ENOENT) {
957 err = got_error_from_errno2("realpath",
958 canonpath);
959 goto done;
961 /*
962 * Path is not on disk.
963 * Assume it is already relative to repository root.
964 */
965 path = strdup(canonpath);
966 if (path == NULL) {
967 err = got_error_from_errno("strdup");
968 goto done;
972 repolen = strlen(repo_abspath);
973 len = strlen(path);
976 if (strcmp(path, repo_abspath) == 0) {
977 free(path);
978 path = strdup("");
979 if (path == NULL) {
980 err = got_error_from_errno("strdup");
981 goto done;
983 } else if (len > repolen &&
984 got_path_is_child(path, repo_abspath, repolen)) {
985 /* Matched an on-disk path inside repository. */
986 if (got_repo_is_bare(repo)) {
987 /*
988 * Matched an on-disk path inside repository
989 * database. Treat input as repository-relative.
990 */
991 free(path);
992 path = canonpath;
993 canonpath = NULL;
994 } else {
995 char *child;
996 /* Strip common prefix with repository path. */
997 err = got_path_skip_common_ancestor(&child,
998 repo_abspath, path);
999 if (err)
1000 goto done;
1001 free(path);
1002 path = child;
1004 } else {
1006 * Matched unrelated on-disk path.
1007 * Treat input as repository-relative.
1009 free(path);
1010 path = canonpath;
1011 canonpath = NULL;
1015 /* Make in-repository path absolute */
1016 if (path[0] != '/') {
1017 char *abspath;
1018 if (asprintf(&abspath, "/%s", path) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 goto done;
1022 free(path);
1023 path = abspath;
1026 done:
1027 free(canonpath);
1028 if (err)
1029 free(path);
1030 else
1031 *in_repo_path = path;
1032 return err;
1035 static const struct got_error *
1036 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1037 const char *path_packidx)
1039 const struct got_error *err = NULL;
1040 size_t i;
1042 for (i = 0; i < repo->pack_cache_size; i++) {
1043 if (repo->packidx_cache[i] == NULL)
1044 break;
1045 if (strcmp(repo->packidx_cache[i]->path_packidx,
1046 path_packidx) == 0) {
1047 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1050 if (i == repo->pack_cache_size) {
1051 do {
1052 i--;
1053 } while (i > 0 && repo->pinned_packidx >= 0 &&
1054 i == repo->pinned_packidx);
1055 err = got_packidx_close(repo->packidx_cache[i]);
1056 if (err)
1057 return err;
1060 repo->packidx_cache[i] = packidx;
1062 return NULL;
1065 int
1066 got_repo_is_packidx_filename(const char *name, size_t len)
1068 if (len != GOT_PACKIDX_NAMELEN)
1069 return 0;
1071 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1072 return 0;
1074 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1075 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1076 return 0;
1078 return 1;
1081 static struct got_packidx_bloom_filter *
1082 get_packidx_bloom_filter(struct got_repository *repo,
1083 const char *path, size_t path_len)
1085 struct got_packidx_bloom_filter key;
1087 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1088 return NULL; /* XXX */
1089 key.path_len = path_len;
1091 return RB_FIND(got_packidx_bloom_filter_tree,
1092 &repo->packidx_bloom_filters, &key);
1095 int
1096 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1097 const char *path_packidx, struct got_object_id *id)
1099 struct got_packidx_bloom_filter *bf;
1101 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1102 if (bf)
1103 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1105 /* No bloom filter means this pack index must be searched. */
1106 return 1;
1109 static const struct got_error *
1110 add_packidx_bloom_filter(struct got_repository *repo,
1111 struct got_packidx *packidx, const char *path_packidx)
1113 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1114 struct got_packidx_bloom_filter *bf;
1115 size_t len;
1118 * Don't use bloom filters for very large pack index files.
1119 * Large pack files will contain a relatively large fraction
1120 * of our objects so we will likely need to visit them anyway.
1121 * The more objects a pack file contains the higher the probability
1122 * of a false-positive match from the bloom filter. And reading
1123 * all object IDs from a large pack index file can be expensive.
1125 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1126 return NULL;
1128 /* Do we already have a filter for this pack index? */
1129 if (get_packidx_bloom_filter(repo, path_packidx,
1130 strlen(path_packidx)) != NULL)
1131 return NULL;
1133 bf = calloc(1, sizeof(*bf));
1134 if (bf == NULL)
1135 return got_error_from_errno("calloc");
1136 bf->bloom = calloc(1, sizeof(*bf->bloom));
1137 if (bf->bloom == NULL) {
1138 free(bf);
1139 return got_error_from_errno("calloc");
1142 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1143 if (len >= sizeof(bf->path)) {
1144 free(bf->bloom);
1145 free(bf);
1146 return got_error(GOT_ERR_NO_SPACE);
1148 bf->path_len = len;
1150 /* Minimum size supported by our bloom filter is 1000 entries. */
1151 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1152 for (i = 0; i < nobjects; i++) {
1153 struct got_packidx_object_id *id;
1154 id = &packidx->hdr.sorted_ids[i];
1155 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1158 RB_INSERT(got_packidx_bloom_filter_tree,
1159 &repo->packidx_bloom_filters, bf);
1160 return NULL;
1163 const struct got_error *
1164 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1165 struct got_repository *repo, struct got_object_id *id)
1167 const struct got_error *err;
1168 struct got_pathlist_entry *pe;
1169 size_t i;
1171 /* Search pack index cache. */
1172 for (i = 0; i < repo->pack_cache_size; i++) {
1173 if (repo->packidx_cache[i] == NULL)
1174 break;
1175 if (!got_repo_check_packidx_bloom_filter(repo,
1176 repo->packidx_cache[i]->path_packidx, id))
1177 continue; /* object will not be found in this index */
1178 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1179 if (*idx != -1) {
1180 *packidx = repo->packidx_cache[i];
1182 * Move this cache entry to the front. Repeatedly
1183 * searching a wrong pack index can be expensive.
1185 if (i > 0) {
1186 memmove(&repo->packidx_cache[1],
1187 &repo->packidx_cache[0],
1188 i * sizeof(repo->packidx_cache[0]));
1189 repo->packidx_cache[0] = *packidx;
1190 if (repo->pinned_packidx >= 0 &&
1191 repo->pinned_packidx < i)
1192 repo->pinned_packidx++;
1193 else if (repo->pinned_packidx == i)
1194 repo->pinned_packidx = 0;
1196 return NULL;
1199 /* No luck. Search the filesystem. */
1201 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1202 const char *path_packidx = pe->path;
1203 int is_cached = 0;
1205 if (!got_repo_check_packidx_bloom_filter(repo,
1206 pe->path, id))
1207 continue; /* object will not be found in this index */
1209 for (i = 0; i < repo->pack_cache_size; i++) {
1210 if (repo->packidx_cache[i] == NULL)
1211 break;
1212 if (strcmp(repo->packidx_cache[i]->path_packidx,
1213 path_packidx) == 0) {
1214 is_cached = 1;
1215 break;
1218 if (is_cached)
1219 continue; /* already searched */
1221 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1222 path_packidx, 0);
1223 if (err)
1224 goto done;
1226 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1227 if (err)
1228 goto done;
1230 err = cache_packidx(repo, *packidx, path_packidx);
1231 if (err)
1232 goto done;
1234 *idx = got_packidx_get_object_idx(*packidx, id);
1235 if (*idx != -1) {
1236 err = NULL; /* found the object */
1237 goto done;
1241 err = got_error_no_obj(id);
1242 done:
1243 return err;
1246 const struct got_error *
1247 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1248 struct got_repository *repo)
1250 const struct got_error *err = NULL;
1251 DIR *packdir = NULL;
1252 struct dirent *dent;
1253 char *path_packidx = NULL;
1254 int packdir_fd;
1256 packdir_fd = openat(got_repo_get_fd(repo),
1257 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1258 if (packdir_fd == -1) {
1259 return got_error_from_errno_fmt("openat: %s/%s",
1260 got_repo_get_path_git_dir(repo),
1261 GOT_OBJECTS_PACK_DIR);
1264 packdir = fdopendir(packdir_fd);
1265 if (packdir == NULL) {
1266 err = got_error_from_errno("fdopendir");
1267 goto done;
1270 while ((dent = readdir(packdir)) != NULL) {
1271 if (!got_repo_is_packidx_filename(dent->d_name,
1272 strlen(dent->d_name)))
1273 continue;
1275 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1276 dent->d_name) == -1) {
1277 err = got_error_from_errno("asprintf");
1278 path_packidx = NULL;
1279 break;
1282 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1283 if (err)
1284 break;
1286 done:
1287 if (err)
1288 free(path_packidx);
1289 if (packdir && closedir(packdir) != 0 && err == NULL)
1290 err = got_error_from_errno("closedir");
1291 return err;
1294 const struct got_error *
1295 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1296 struct got_repository *repo)
1298 const struct got_error *err;
1299 size_t i;
1301 *packidx = NULL;
1303 /* Search pack index cache. */
1304 for (i = 0; i < repo->pack_cache_size; i++) {
1305 if (repo->packidx_cache[i] == NULL)
1306 break;
1307 if (strcmp(repo->packidx_cache[i]->path_packidx,
1308 path_packidx) == 0) {
1309 *packidx = repo->packidx_cache[i];
1310 return NULL;
1313 /* No luck. Search the filesystem. */
1315 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1316 path_packidx, 0);
1317 if (err)
1318 return err;
1320 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1321 if (err)
1322 goto done;
1324 err = cache_packidx(repo, *packidx, path_packidx);
1325 done:
1326 if (err) {
1327 got_packidx_close(*packidx);
1328 *packidx = NULL;
1330 return err;
1333 static const struct got_error *
1334 read_packfile_hdr(int fd, struct got_packidx *packidx)
1336 const struct got_error *err = NULL;
1337 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1338 struct got_packfile_hdr hdr;
1339 ssize_t n;
1341 n = read(fd, &hdr, sizeof(hdr));
1342 if (n < 0)
1343 return got_error_from_errno("read");
1344 if (n != sizeof(hdr))
1345 return got_error(GOT_ERR_BAD_PACKFILE);
1347 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1348 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1349 be32toh(hdr.nobjects) != totobj)
1350 err = got_error(GOT_ERR_BAD_PACKFILE);
1352 return err;
1355 static const struct got_error *
1356 open_packfile(int *fd, struct got_repository *repo,
1357 const char *relpath, struct got_packidx *packidx)
1359 const struct got_error *err = NULL;
1361 *fd = openat(got_repo_get_fd(repo), relpath,
1362 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1363 if (*fd == -1)
1364 return got_error_from_errno_fmt("openat: %s/%s",
1365 got_repo_get_path_git_dir(repo), relpath);
1367 if (packidx) {
1368 err = read_packfile_hdr(*fd, packidx);
1369 if (err) {
1370 close(*fd);
1371 *fd = -1;
1375 return err;
1378 const struct got_error *
1379 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1380 const char *path_packfile, struct got_packidx *packidx)
1382 const struct got_error *err = NULL;
1383 struct got_pack *pack = NULL;
1384 struct stat sb;
1385 size_t i;
1387 if (packp)
1388 *packp = NULL;
1390 for (i = 0; i < repo->pack_cache_size; i++) {
1391 pack = &repo->packs[i];
1392 if (pack->path_packfile == NULL)
1393 break;
1394 if (strcmp(pack->path_packfile, path_packfile) == 0)
1395 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1398 if (i == repo->pack_cache_size) {
1399 struct got_pack tmp;
1400 do {
1401 i--;
1402 } while (i > 0 && repo->pinned_pack >= 0 &&
1403 i == repo->pinned_pack);
1404 err = got_pack_close(&repo->packs[i]);
1405 if (err)
1406 return err;
1407 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1408 return got_error_from_errno("ftruncate");
1409 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1410 return got_error_from_errno("ftruncate");
1411 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1412 memcpy(&repo->packs[i], &repo->packs[0],
1413 sizeof(repo->packs[i]));
1414 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1415 if (repo->pinned_pack == 0)
1416 repo->pinned_pack = i;
1417 else if (repo->pinned_pack == i)
1418 repo->pinned_pack = 0;
1419 i = 0;
1422 pack = &repo->packs[i];
1424 pack->path_packfile = strdup(path_packfile);
1425 if (pack->path_packfile == NULL) {
1426 err = got_error_from_errno("strdup");
1427 goto done;
1430 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1431 if (err)
1432 goto done;
1434 if (fstat(pack->fd, &sb) != 0) {
1435 err = got_error_from_errno("fstat");
1436 goto done;
1438 pack->filesize = sb.st_size;
1440 pack->privsep_child = NULL;
1442 #ifndef GOT_PACK_NO_MMAP
1443 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1444 pack->fd, 0);
1445 if (pack->map == MAP_FAILED) {
1446 if (errno != ENOMEM) {
1447 err = got_error_from_errno("mmap");
1448 goto done;
1450 pack->map = NULL; /* fall back to read(2) */
1452 #endif
1453 done:
1454 if (err) {
1455 if (pack) {
1456 free(pack->path_packfile);
1457 memset(pack, 0, sizeof(*pack));
1459 } else if (packp)
1460 *packp = pack;
1461 return err;
1464 struct got_pack *
1465 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1467 struct got_pack *pack = NULL;
1468 size_t i;
1470 for (i = 0; i < repo->pack_cache_size; i++) {
1471 pack = &repo->packs[i];
1472 if (pack->path_packfile == NULL)
1473 break;
1474 if (strcmp(pack->path_packfile, path_packfile) == 0)
1475 return pack;
1478 return NULL;
1481 const struct got_error *
1482 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1483 struct got_pack *pack)
1485 size_t i;
1486 int pinned_pack = -1, pinned_packidx = -1;
1488 for (i = 0; i < repo->pack_cache_size; i++) {
1489 if (repo->packidx_cache[i] &&
1490 strcmp(repo->packidx_cache[i]->path_packidx,
1491 packidx->path_packidx) == 0)
1492 pinned_packidx = i;
1493 if (repo->packs[i].path_packfile &&
1494 strcmp(repo->packs[i].path_packfile,
1495 pack->path_packfile) == 0)
1496 pinned_pack = i;
1499 if (pinned_packidx == -1 || pinned_pack == -1)
1500 return got_error(GOT_ERR_PIN_PACK);
1502 repo->pinned_pack = pinned_pack;
1503 repo->pinned_packidx = pinned_packidx;
1504 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1505 return NULL;
1508 struct got_pack *
1509 got_repo_get_pinned_pack(struct got_repository *repo)
1511 if (repo->pinned_pack >= 0 &&
1512 repo->pinned_pack < repo->pack_cache_size)
1513 return &repo->packs[repo->pinned_pack];
1515 return NULL;
1518 void
1519 got_repo_unpin_pack(struct got_repository *repo)
1521 repo->pinned_packidx = -1;
1522 repo->pinned_pack = -1;
1523 repo->pinned_pid = 0;
1526 const struct got_error *
1527 got_repo_init(const char *repo_path)
1529 const struct got_error *err = NULL;
1530 const char *dirnames[] = {
1531 GOT_OBJECTS_DIR,
1532 GOT_OBJECTS_PACK_DIR,
1533 GOT_REFS_DIR,
1535 const char *description_str = "Unnamed repository; "
1536 "edit this file 'description' to name the repository.";
1537 const char *headref_str = "ref: refs/heads/main";
1538 const char *gitconfig_str = "[core]\n"
1539 "\trepositoryformatversion = 0\n"
1540 "\tfilemode = true\n"
1541 "\tbare = true\n";
1542 char *path;
1543 size_t i;
1545 if (!got_path_dir_is_empty(repo_path))
1546 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1548 for (i = 0; i < nitems(dirnames); i++) {
1549 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1550 return got_error_from_errno("asprintf");
1552 err = got_path_mkdir(path);
1553 free(path);
1554 if (err)
1555 return err;
1558 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1559 return got_error_from_errno("asprintf");
1560 err = got_path_create_file(path, description_str);
1561 free(path);
1562 if (err)
1563 return err;
1565 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1566 return got_error_from_errno("asprintf");
1567 err = got_path_create_file(path, headref_str);
1568 free(path);
1569 if (err)
1570 return err;
1572 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1573 return got_error_from_errno("asprintf");
1574 err = got_path_create_file(path, gitconfig_str);
1575 free(path);
1576 if (err)
1577 return err;
1579 return NULL;
1582 static const struct got_error *
1583 match_packed_object(struct got_object_id **unique_id,
1584 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1586 const struct got_error *err = NULL;
1587 struct got_object_id_queue matched_ids;
1588 struct got_pathlist_entry *pe;
1590 STAILQ_INIT(&matched_ids);
1592 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1593 const char *path_packidx = pe->path;
1594 struct got_packidx *packidx;
1595 struct got_object_qid *qid;
1597 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1598 path_packidx, 0);
1599 if (err)
1600 break;
1602 err = got_packidx_match_id_str_prefix(&matched_ids,
1603 packidx, id_str_prefix);
1604 if (err) {
1605 got_packidx_close(packidx);
1606 break;
1608 err = got_packidx_close(packidx);
1609 if (err)
1610 break;
1612 STAILQ_FOREACH(qid, &matched_ids, entry) {
1613 if (obj_type != GOT_OBJ_TYPE_ANY) {
1614 int matched_type;
1615 err = got_object_get_type(&matched_type, repo,
1616 &qid->id);
1617 if (err)
1618 goto done;
1619 if (matched_type != obj_type)
1620 continue;
1622 if (*unique_id == NULL) {
1623 *unique_id = got_object_id_dup(&qid->id);
1624 if (*unique_id == NULL) {
1625 err = got_error_from_errno("malloc");
1626 goto done;
1628 } else {
1629 if (got_object_id_cmp(*unique_id,
1630 &qid->id) == 0)
1631 continue; /* packed multiple times */
1632 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1633 goto done;
1637 done:
1638 got_object_id_queue_free(&matched_ids);
1639 if (err) {
1640 free(*unique_id);
1641 *unique_id = NULL;
1643 return err;
1646 static const struct got_error *
1647 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1648 const char *object_dir, const char *id_str_prefix, int obj_type,
1649 struct got_repository *repo)
1651 const struct got_error *err = NULL;
1652 char *path;
1653 DIR *dir = NULL;
1654 struct dirent *dent;
1655 struct got_object_id id;
1657 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1658 err = got_error_from_errno("asprintf");
1659 goto done;
1662 dir = opendir(path);
1663 if (dir == NULL) {
1664 if (errno == ENOENT) {
1665 err = NULL;
1666 goto done;
1668 err = got_error_from_errno2("opendir", path);
1669 goto done;
1671 while ((dent = readdir(dir)) != NULL) {
1672 char *id_str;
1673 int cmp;
1675 if (strcmp(dent->d_name, ".") == 0 ||
1676 strcmp(dent->d_name, "..") == 0)
1677 continue;
1679 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1680 err = got_error_from_errno("asprintf");
1681 goto done;
1684 if (!got_parse_sha1_digest(id.sha1, id_str))
1685 continue;
1688 * Directory entries do not necessarily appear in
1689 * sorted order, so we must iterate over all of them.
1691 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1692 if (cmp != 0) {
1693 free(id_str);
1694 continue;
1697 if (*unique_id == NULL) {
1698 if (obj_type != GOT_OBJ_TYPE_ANY) {
1699 int matched_type;
1700 err = got_object_get_type(&matched_type, repo,
1701 &id);
1702 if (err)
1703 goto done;
1704 if (matched_type != obj_type)
1705 continue;
1707 *unique_id = got_object_id_dup(&id);
1708 if (*unique_id == NULL) {
1709 err = got_error_from_errno("got_object_id_dup");
1710 free(id_str);
1711 goto done;
1713 } else {
1714 if (got_object_id_cmp(*unique_id, &id) == 0)
1715 continue; /* both packed and loose */
1716 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1717 free(id_str);
1718 goto done;
1721 done:
1722 if (dir && closedir(dir) != 0 && err == NULL)
1723 err = got_error_from_errno("closedir");
1724 if (err) {
1725 free(*unique_id);
1726 *unique_id = NULL;
1728 free(path);
1729 return err;
1732 const struct got_error *
1733 got_repo_match_object_id_prefix(struct got_object_id **id,
1734 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1736 const struct got_error *err = NULL;
1737 char *path_objects = got_repo_get_path_objects(repo);
1738 char *object_dir = NULL;
1739 size_t len;
1740 int i;
1742 *id = NULL;
1744 len = strlen(id_str_prefix);
1745 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1746 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1748 for (i = 0; i < len; i++) {
1749 if (isxdigit((unsigned char)id_str_prefix[i]))
1750 continue;
1751 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1754 if (len >= 2) {
1755 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1756 if (err)
1757 goto done;
1758 object_dir = strndup(id_str_prefix, 2);
1759 if (object_dir == NULL) {
1760 err = got_error_from_errno("strdup");
1761 goto done;
1763 err = match_loose_object(id, path_objects, object_dir,
1764 id_str_prefix, obj_type, repo);
1765 } else if (len == 1) {
1766 int i;
1767 for (i = 0; i < 0xf; i++) {
1768 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1769 == -1) {
1770 err = got_error_from_errno("asprintf");
1771 goto done;
1773 err = match_packed_object(id, repo, object_dir,
1774 obj_type);
1775 if (err)
1776 goto done;
1777 err = match_loose_object(id, path_objects, object_dir,
1778 id_str_prefix, obj_type, repo);
1779 if (err)
1780 goto done;
1782 } else {
1783 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1784 goto done;
1786 done:
1787 free(object_dir);
1788 if (err) {
1789 free(*id);
1790 *id = NULL;
1791 } else if (*id == NULL) {
1792 switch (obj_type) {
1793 case GOT_OBJ_TYPE_BLOB:
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1796 break;
1797 case GOT_OBJ_TYPE_TREE:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_TREE, id_str_prefix);
1800 break;
1801 case GOT_OBJ_TYPE_COMMIT:
1802 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1803 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1804 break;
1805 case GOT_OBJ_TYPE_TAG:
1806 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1807 GOT_OBJ_LABEL_TAG, id_str_prefix);
1808 break;
1809 default:
1810 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1811 break;
1815 return err;
1818 const struct got_error *
1819 got_repo_match_object_id(struct got_object_id **id, char **label,
1820 const char *id_str, int obj_type, struct got_reflist_head *refs,
1821 struct got_repository *repo)
1823 const struct got_error *err;
1824 struct got_tag_object *tag;
1825 struct got_reference *ref = NULL;
1827 *id = NULL;
1828 if (label)
1829 *label = NULL;
1831 if (refs) {
1832 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1833 refs, repo);
1834 if (err == NULL) {
1835 *id = got_object_id_dup(
1836 got_object_tag_get_object_id(tag));
1837 if (*id == NULL)
1838 err = got_error_from_errno("got_object_id_dup");
1839 else if (label && asprintf(label, "refs/tags/%s",
1840 got_object_tag_get_name(tag)) == -1) {
1841 err = got_error_from_errno("asprintf");
1842 free(*id);
1843 *id = NULL;
1845 got_object_tag_close(tag);
1846 return err;
1847 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1848 err->code != GOT_ERR_NO_OBJ)
1849 return err;
1852 err = got_ref_open(&ref, repo, id_str, 0);
1853 if (err == NULL) {
1854 err = got_ref_resolve(id, repo, ref);
1855 if (err)
1856 goto done;
1857 if (label) {
1858 *label = strdup(got_ref_get_name(ref));
1859 if (*label == NULL) {
1860 err = got_error_from_errno("strdup");
1861 goto done;
1864 } else {
1865 if (err->code != GOT_ERR_NOT_REF &&
1866 err->code != GOT_ERR_BAD_REF_NAME)
1867 goto done;
1868 err = got_repo_match_object_id_prefix(id, id_str,
1869 obj_type, repo);
1870 if (err) {
1871 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1872 err = got_error_not_ref(id_str);
1873 goto done;
1875 if (label) {
1876 err = got_object_id_str(label, *id);
1877 if (*label == NULL) {
1878 err = got_error_from_errno("strdup");
1879 goto done;
1883 done:
1884 if (ref)
1885 got_ref_close(ref);
1886 return err;
1889 const struct got_error *
1890 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1891 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1893 const struct got_error *err = NULL;
1894 struct got_reflist_entry *re;
1895 struct got_object_id *tag_id;
1896 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1898 *tag = NULL;
1900 TAILQ_FOREACH(re, refs, entry) {
1901 const char *refname;
1902 refname = got_ref_get_name(re->ref);
1903 if (got_ref_is_symbolic(re->ref))
1904 continue;
1905 if (strncmp(refname, "refs/tags/", 10) != 0)
1906 continue;
1907 if (!name_is_absolute)
1908 refname += strlen("refs/tags/");
1909 if (strcmp(refname, name) != 0)
1910 continue;
1911 err = got_ref_resolve(&tag_id, repo, re->ref);
1912 if (err)
1913 break;
1914 err = got_object_open_as_tag(tag, repo, tag_id);
1915 free(tag_id);
1916 if (err)
1917 break;
1918 if (obj_type == GOT_OBJ_TYPE_ANY ||
1919 got_object_tag_get_object_type(*tag) == obj_type)
1920 break;
1921 got_object_tag_close(*tag);
1922 *tag = NULL;
1925 if (err == NULL && *tag == NULL)
1926 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1927 GOT_OBJ_LABEL_TAG, name);
1928 return err;
1931 static const struct got_error *
1932 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1933 const char *name, mode_t mode, struct got_object_id *blob_id)
1935 const struct got_error *err = NULL;
1937 *new_te = NULL;
1939 *new_te = calloc(1, sizeof(**new_te));
1940 if (*new_te == NULL)
1941 return got_error_from_errno("calloc");
1943 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1944 sizeof((*new_te)->name)) {
1945 err = got_error(GOT_ERR_NO_SPACE);
1946 goto done;
1949 if (S_ISLNK(mode)) {
1950 (*new_te)->mode = S_IFLNK;
1951 } else {
1952 (*new_te)->mode = S_IFREG;
1953 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1955 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1956 done:
1957 if (err && *new_te) {
1958 free(*new_te);
1959 *new_te = NULL;
1961 return err;
1964 static const struct got_error *
1965 import_file(struct got_tree_entry **new_te, struct dirent *de,
1966 const char *path, struct got_repository *repo)
1968 const struct got_error *err;
1969 struct got_object_id *blob_id = NULL;
1970 char *filepath;
1971 struct stat sb;
1973 if (asprintf(&filepath, "%s%s%s", path,
1974 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1975 return got_error_from_errno("asprintf");
1977 if (lstat(filepath, &sb) != 0) {
1978 err = got_error_from_errno2("lstat", path);
1979 goto done;
1982 err = got_object_blob_create(&blob_id, filepath, repo);
1983 if (err)
1984 goto done;
1986 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1987 blob_id);
1988 done:
1989 free(filepath);
1990 if (err)
1991 free(blob_id);
1992 return err;
1995 static const struct got_error *
1996 insert_tree_entry(struct got_tree_entry *new_te,
1997 struct got_pathlist_head *paths)
1999 const struct got_error *err = NULL;
2000 struct got_pathlist_entry *new_pe;
2002 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2003 if (err)
2004 return err;
2005 if (new_pe == NULL)
2006 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2007 return NULL;
2010 static const struct got_error *write_tree(struct got_object_id **,
2011 const char *, struct got_pathlist_head *, struct got_repository *,
2012 got_repo_import_cb progress_cb, void *progress_arg);
2014 static const struct got_error *
2015 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2016 const char *path, struct got_pathlist_head *ignores,
2017 struct got_repository *repo,
2018 got_repo_import_cb progress_cb, void *progress_arg)
2020 const struct got_error *err;
2021 struct got_object_id *id = NULL;
2022 char *subdirpath;
2024 if (asprintf(&subdirpath, "%s%s%s", path,
2025 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2026 return got_error_from_errno("asprintf");
2028 (*new_te) = calloc(1, sizeof(**new_te));
2029 if (*new_te == NULL)
2030 return got_error_from_errno("calloc");
2031 (*new_te)->mode = S_IFDIR;
2032 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2033 sizeof((*new_te)->name)) {
2034 err = got_error(GOT_ERR_NO_SPACE);
2035 goto done;
2037 err = write_tree(&id, subdirpath, ignores, repo,
2038 progress_cb, progress_arg);
2039 if (err)
2040 goto done;
2041 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2043 done:
2044 free(id);
2045 free(subdirpath);
2046 if (err) {
2047 free(*new_te);
2048 *new_te = NULL;
2050 return err;
2053 static const struct got_error *
2054 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2055 struct got_pathlist_head *ignores, struct got_repository *repo,
2056 got_repo_import_cb progress_cb, void *progress_arg)
2058 const struct got_error *err = NULL;
2059 DIR *dir;
2060 struct dirent *de;
2061 int nentries;
2062 struct got_tree_entry *new_te = NULL;
2063 struct got_pathlist_head paths;
2064 struct got_pathlist_entry *pe;
2066 *new_tree_id = NULL;
2068 TAILQ_INIT(&paths);
2070 dir = opendir(path_dir);
2071 if (dir == NULL) {
2072 err = got_error_from_errno2("opendir", path_dir);
2073 goto done;
2076 nentries = 0;
2077 while ((de = readdir(dir)) != NULL) {
2078 int ignore = 0;
2079 int type;
2081 if (strcmp(de->d_name, ".") == 0 ||
2082 strcmp(de->d_name, "..") == 0)
2083 continue;
2085 TAILQ_FOREACH(pe, ignores, entry) {
2086 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2087 ignore = 1;
2088 break;
2091 if (ignore)
2092 continue;
2094 err = got_path_dirent_type(&type, path_dir, de);
2095 if (err)
2096 goto done;
2098 if (type == DT_DIR) {
2099 err = import_subdir(&new_te, de, path_dir,
2100 ignores, repo, progress_cb, progress_arg);
2101 if (err) {
2102 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2103 goto done;
2104 err = NULL;
2105 continue;
2107 } else if (type == DT_REG || type == DT_LNK) {
2108 err = import_file(&new_te, de, path_dir, repo);
2109 if (err)
2110 goto done;
2111 } else
2112 continue;
2114 err = insert_tree_entry(new_te, &paths);
2115 if (err)
2116 goto done;
2117 nentries++;
2120 if (TAILQ_EMPTY(&paths)) {
2121 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2122 "cannot create tree without any entries");
2123 goto done;
2126 TAILQ_FOREACH(pe, &paths, entry) {
2127 struct got_tree_entry *te = pe->data;
2128 char *path;
2129 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2130 continue;
2131 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2132 err = got_error_from_errno("asprintf");
2133 goto done;
2135 err = (*progress_cb)(progress_arg, path);
2136 free(path);
2137 if (err)
2138 goto done;
2141 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2142 done:
2143 if (dir)
2144 closedir(dir);
2145 got_pathlist_free(&paths);
2146 return err;
2149 const struct got_error *
2150 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2151 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2152 struct got_repository *repo, got_repo_import_cb progress_cb,
2153 void *progress_arg)
2155 const struct got_error *err;
2156 struct got_object_id *new_tree_id;
2158 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2159 progress_cb, progress_arg);
2160 if (err)
2161 return err;
2163 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2164 author, time(NULL), author, time(NULL), logmsg, repo);
2165 free(new_tree_id);
2166 return err;
2169 const struct got_error *
2170 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2171 struct got_repository *repo)
2173 const struct got_error *err = NULL;
2174 char *path_objects = NULL, *path = NULL;
2175 DIR *dir = NULL;
2176 struct got_object_id id;
2177 int i;
2179 *nobjects = 0;
2180 *ondisk_size = 0;
2182 path_objects = got_repo_get_path_objects(repo);
2183 if (path_objects == NULL)
2184 return got_error_from_errno("got_repo_get_path_objects");
2186 for (i = 0; i <= 0xff; i++) {
2187 struct dirent *dent;
2189 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2190 err = got_error_from_errno("asprintf");
2191 break;
2194 dir = opendir(path);
2195 if (dir == NULL) {
2196 if (errno == ENOENT) {
2197 err = NULL;
2198 continue;
2200 err = got_error_from_errno2("opendir", path);
2201 break;
2204 while ((dent = readdir(dir)) != NULL) {
2205 char *id_str;
2206 int fd;
2207 struct stat sb;
2209 if (strcmp(dent->d_name, ".") == 0 ||
2210 strcmp(dent->d_name, "..") == 0)
2211 continue;
2213 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2214 err = got_error_from_errno("asprintf");
2215 goto done;
2218 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2219 free(id_str);
2220 continue;
2222 free(id_str);
2224 err = got_object_open_loose_fd(&fd, &id, repo);
2225 if (err)
2226 goto done;
2228 if (fstat(fd, &sb) == -1) {
2229 err = got_error_from_errno("fstat");
2230 close(fd);
2231 goto done;
2233 (*nobjects)++;
2234 (*ondisk_size) += sb.st_size;
2236 if (close(fd) == -1) {
2237 err = got_error_from_errno("close");
2238 goto done;
2242 if (closedir(dir) != 0) {
2243 err = got_error_from_errno("closedir");
2244 goto done;
2246 dir = NULL;
2248 free(path);
2249 path = NULL;
2251 done:
2252 if (dir && closedir(dir) != 0 && err == NULL)
2253 err = got_error_from_errno("closedir");
2255 if (err) {
2256 *nobjects = 0;
2257 *ondisk_size = 0;
2259 free(path_objects);
2260 free(path);
2261 return err;
2264 const struct got_error *
2265 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2266 off_t *total_packsize, struct got_repository *repo)
2268 const struct got_error *err = NULL;
2269 DIR *packdir = NULL;
2270 struct dirent *dent;
2271 struct got_packidx *packidx = NULL;
2272 char *path_packidx;
2273 char *path_packfile;
2274 int packdir_fd;
2275 struct stat sb;
2277 *npackfiles = 0;
2278 *nobjects = 0;
2279 *total_packsize = 0;
2281 packdir_fd = openat(got_repo_get_fd(repo),
2282 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2283 if (packdir_fd == -1) {
2284 return got_error_from_errno_fmt("openat: %s/%s",
2285 got_repo_get_path_git_dir(repo),
2286 GOT_OBJECTS_PACK_DIR);
2289 packdir = fdopendir(packdir_fd);
2290 if (packdir == NULL) {
2291 err = got_error_from_errno("fdopendir");
2292 goto done;
2295 while ((dent = readdir(packdir)) != NULL) {
2296 if (!got_repo_is_packidx_filename(dent->d_name,
2297 strlen(dent->d_name)))
2298 continue;
2300 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2301 dent->d_name) == -1) {
2302 err = got_error_from_errno("asprintf");
2303 goto done;
2306 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2307 path_packidx, 0);
2308 free(path_packidx);
2309 if (err)
2310 goto done;
2312 if (fstat(packidx->fd, &sb) == -1)
2313 goto done;
2314 *total_packsize += sb.st_size;
2316 err = got_packidx_get_packfile_path(&path_packfile,
2317 packidx->path_packidx);
2318 if (err)
2319 goto done;
2321 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2322 0) == -1) {
2323 free(path_packfile);
2324 goto done;
2326 free(path_packfile);
2327 *total_packsize += sb.st_size;
2329 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2331 (*npackfiles)++;
2333 got_packidx_close(packidx);
2334 packidx = NULL;
2336 done:
2337 if (packidx)
2338 got_packidx_close(packidx);
2339 if (packdir && closedir(packdir) != 0 && err == NULL)
2340 err = got_error_from_errno("closedir");
2341 if (err) {
2342 *npackfiles = 0;
2343 *nobjects = 0;
2344 *total_packsize = 0;
2346 return err;
2349 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2350 got_packidx_bloom_filter_cmp);