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/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <uuid.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"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 int
81 got_repo_get_fd(struct got_repository *repo)
82 {
83 return repo->gitdir_fd;
84 }
86 const char *
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_name;
90 }
92 const char *
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_email;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
104 const char *
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
110 const char *
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
116 void
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
124 int
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 static char *
131 get_path_git_child(struct got_repository *repo, const char *basename)
133 char *path_child;
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
136 basename) == -1)
137 return NULL;
139 return path_child;
142 char *
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 char *
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 char *
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
160 char *
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 static char *
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
172 char *
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
178 char *
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
190 void
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
198 static int
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
205 int ret = 0;
206 struct stat sb;
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
210 goto done;
211 if (!S_ISDIR(sb.st_mode))
212 goto done;
214 if (lstat(path_objects, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_refs, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_head, &sb) == -1)
225 goto done;
226 if (!S_ISREG(sb.st_mode))
227 goto done;
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 goto done;
232 got_ref_close(head_ref);
234 ret = 1;
235 done:
236 free(path_objects);
237 free(path_refs);
238 free(path_head);
239 return ret;
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err) {
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 err = NULL;
254 return err;
256 obj->refcnt++;
257 #endif
258 return NULL;
261 struct got_object *
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err) {
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
278 err = NULL;
279 return err;
281 tree->refcnt++;
282 #endif
283 return NULL;
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 commit->refcnt++;
308 #endif
309 return NULL;
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err) {
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
330 err = NULL;
331 return err;
333 tag->refcnt++;
334 #endif
335 return NULL;
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
352 if (err) {
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
355 err = NULL;
356 return err;
358 raw->refcnt++;
359 #endif
360 return NULL;
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
389 repo->gitdir_fd = open(repo->path_git_dir,
390 O_DIRECTORY | O_CLOEXEC);
391 if (repo->gitdir_fd == -1) {
392 err = got_error_from_errno2("open",
393 repo->path_git_dir);
394 goto done;
396 return NULL;
399 /* git repository with working tree? */
400 free(repo->path_git_dir);
401 repo->path_git_dir = NULL;
402 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
403 err = got_error_from_errno("asprintf");
404 goto done;
406 if (is_git_repo(repo)) {
407 repo->path = strdup(path);
408 if (repo->path == NULL) {
409 err = got_error_from_errno("strdup");
410 goto done;
412 repo->gitdir_fd = open(repo->path_git_dir,
413 O_DIRECTORY | O_CLOEXEC);
414 if (repo->gitdir_fd == -1) {
415 err = got_error_from_errno2("open",
416 repo->path_git_dir);
417 goto done;
419 return NULL;
422 err = got_error(GOT_ERR_NOT_GIT_REPO);
423 done:
424 if (err) {
425 free(repo->path);
426 repo->path = NULL;
427 free(repo->path_git_dir);
428 repo->path_git_dir = NULL;
429 if (repo->gitdir_fd != -1)
430 close(repo->gitdir_fd);
431 repo->gitdir_fd = -1;
434 return err;
437 static const struct got_error *
438 parse_gitconfig_file(int *gitconfig_repository_format_version,
439 char **gitconfig_author_name, char **gitconfig_author_email,
440 struct got_remote_repo **remotes, int *nremotes,
441 char **gitconfig_owner, char ***extensions, int *nextensions,
442 const char *gitconfig_path)
444 const struct got_error *err = NULL, *child_err = NULL;
445 int fd = -1;
446 int imsg_fds[2] = { -1, -1 };
447 pid_t pid;
448 struct imsgbuf *ibuf;
450 *gitconfig_repository_format_version = 0;
451 if (extensions)
452 *extensions = NULL;
453 if (nextensions)
454 *nextensions = 0;
455 *gitconfig_author_name = NULL;
456 *gitconfig_author_email = NULL;
457 if (remotes)
458 *remotes = NULL;
459 if (nremotes)
460 *nremotes = 0;
461 if (gitconfig_owner)
462 *gitconfig_owner = NULL;
464 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
465 if (fd == -1) {
466 if (errno == ENOENT)
467 return NULL;
468 return got_error_from_errno2("open", gitconfig_path);
471 ibuf = calloc(1, sizeof(*ibuf));
472 if (ibuf == NULL) {
473 err = got_error_from_errno("calloc");
474 goto done;
477 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
478 err = got_error_from_errno("socketpair");
479 goto done;
482 pid = fork();
483 if (pid == -1) {
484 err = got_error_from_errno("fork");
485 goto done;
486 } else if (pid == 0) {
487 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
488 gitconfig_path);
489 /* not reached */
492 if (close(imsg_fds[1]) == -1) {
493 err = got_error_from_errno("close");
494 goto done;
496 imsg_fds[1] = -1;
497 imsg_init(ibuf, imsg_fds[0]);
499 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
500 if (err)
501 goto done;
502 fd = -1;
504 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
505 if (err)
506 goto done;
508 err = got_privsep_recv_gitconfig_int(
509 gitconfig_repository_format_version, ibuf);
510 if (err)
511 goto done;
513 if (extensions && nextensions) {
514 err = got_privsep_send_gitconfig_repository_extensions_req(
515 ibuf);
516 if (err)
517 goto done;
518 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
519 if (err)
520 goto done;
521 if (*nextensions > 0) {
522 int i;
523 *extensions = calloc(*nextensions, sizeof(char *));
524 if (*extensions == NULL) {
525 err = got_error_from_errno("calloc");
526 goto done;
528 for (i = 0; i < *nextensions; i++) {
529 char *ext;
530 err = got_privsep_recv_gitconfig_str(&ext,
531 ibuf);
532 if (err)
533 goto done;
534 (*extensions)[i] = ext;
539 err = got_privsep_send_gitconfig_author_name_req(ibuf);
540 if (err)
541 goto done;
543 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
544 if (err)
545 goto done;
547 err = got_privsep_send_gitconfig_author_email_req(ibuf);
548 if (err)
549 goto done;
551 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
552 if (err)
553 goto done;
555 if (remotes && nremotes) {
556 err = got_privsep_send_gitconfig_remotes_req(ibuf);
557 if (err)
558 goto done;
560 err = got_privsep_recv_gitconfig_remotes(remotes,
561 nremotes, ibuf);
562 if (err)
563 goto done;
566 if (gitconfig_owner) {
567 err = got_privsep_send_gitconfig_owner_req(ibuf);
568 if (err)
569 goto done;
570 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
571 if (err)
572 goto done;
575 imsg_clear(ibuf);
576 err = got_privsep_send_stop(imsg_fds[0]);
577 child_err = got_privsep_wait_for_child(pid);
578 if (child_err && err == NULL)
579 err = child_err;
580 done:
581 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
584 err = got_error_from_errno("close");
585 if (fd != -1 && close(fd) == -1 && err == NULL)
586 err = got_error_from_errno2("close", gitconfig_path);
587 free(ibuf);
588 return err;
591 static const struct got_error *
592 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
594 const struct got_error *err = NULL;
595 char *repo_gitconfig_path = NULL;
597 if (global_gitconfig_path) {
598 /* Read settings from ~/.gitconfig. */
599 int dummy_repo_version;
600 err = parse_gitconfig_file(&dummy_repo_version,
601 &repo->global_gitconfig_author_name,
602 &repo->global_gitconfig_author_email,
603 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
604 if (err)
605 return err;
608 /* Read repository's .git/config file. */
609 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
610 if (repo_gitconfig_path == NULL)
611 return got_error_from_errno("got_repo_get_path_gitconfig");
613 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
614 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
615 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
616 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
617 repo_gitconfig_path);
618 if (err)
619 goto done;
620 done:
621 free(repo_gitconfig_path);
622 return err;
625 static const struct got_error *
626 read_gotconfig(struct got_repository *repo)
628 const struct got_error *err = NULL;
629 char *gotconfig_path;
631 gotconfig_path = got_repo_get_path_gotconfig(repo);
632 if (gotconfig_path == NULL)
633 return got_error_from_errno("got_repo_get_path_gotconfig");
635 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
636 free(gotconfig_path);
637 return err;
640 /* Supported repository format extensions. */
641 static const char *repo_extensions[] = {
642 "noop", /* Got supports repository format version 1. */
643 "preciousObjects", /* Supported by gotadmin cleanup. */
644 "worktreeConfig", /* Got does not care about Git work trees. */
645 };
647 const struct got_error *
648 got_repo_open(struct got_repository **repop, const char *path,
649 const char *global_gitconfig_path)
651 struct got_repository *repo = NULL;
652 const struct got_error *err = NULL;
653 char *repo_path = NULL;
654 size_t i;
655 struct rlimit rl;
657 *repop = NULL;
659 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
660 return got_error_from_errno("getrlimit");
662 repo = calloc(1, sizeof(*repo));
663 if (repo == NULL) {
664 err = got_error_from_errno("calloc");
665 goto done;
668 RB_INIT(&repo->packidx_bloom_filters);
670 for (i = 0; i < nitems(repo->privsep_children); i++) {
671 memset(&repo->privsep_children[i], 0,
672 sizeof(repo->privsep_children[0]));
673 repo->privsep_children[i].imsg_fd = -1;
676 err = got_object_cache_init(&repo->objcache,
677 GOT_OBJECT_CACHE_TYPE_OBJ);
678 if (err)
679 goto done;
680 err = got_object_cache_init(&repo->treecache,
681 GOT_OBJECT_CACHE_TYPE_TREE);
682 if (err)
683 goto done;
684 err = got_object_cache_init(&repo->commitcache,
685 GOT_OBJECT_CACHE_TYPE_COMMIT);
686 if (err)
687 goto done;
688 err = got_object_cache_init(&repo->tagcache,
689 GOT_OBJECT_CACHE_TYPE_TAG);
690 if (err)
691 goto done;
692 err = got_object_cache_init(&repo->rawcache,
693 GOT_OBJECT_CACHE_TYPE_RAW);
694 if (err)
695 goto done;
697 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
698 if (repo->pack_cache_size > rl.rlim_cur / 8)
699 repo->pack_cache_size = rl.rlim_cur / 8;
701 repo_path = realpath(path, NULL);
702 if (repo_path == NULL) {
703 err = got_error_from_errno2("realpath", path);
704 goto done;
707 for (;;) {
708 char *parent_path;
710 err = open_repo(repo, repo_path);
711 if (err == NULL)
712 break;
713 if (err->code != GOT_ERR_NOT_GIT_REPO)
714 goto done;
715 if (repo_path[0] == '/' && repo_path[1] == '\0') {
716 err = got_error(GOT_ERR_NOT_GIT_REPO);
717 goto done;
719 err = got_path_dirname(&parent_path, repo_path);
720 if (err)
721 goto done;
722 free(repo_path);
723 repo_path = parent_path;
726 err = read_gotconfig(repo);
727 if (err)
728 goto done;
730 err = read_gitconfig(repo, global_gitconfig_path);
731 if (err)
732 goto done;
733 if (repo->gitconfig_repository_format_version != 0)
734 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
735 for (i = 0; i < repo->nextensions; i++) {
736 char *ext = repo->extensions[i];
737 int j, supported = 0;
738 for (j = 0; j < nitems(repo_extensions); j++) {
739 if (strcmp(ext, repo_extensions[j]) == 0) {
740 supported = 1;
741 break;
744 if (!supported) {
745 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
746 goto done;
749 done:
750 if (err)
751 got_repo_close(repo);
752 else
753 *repop = repo;
754 free(repo_path);
755 return err;
758 const struct got_error *
759 got_repo_close(struct got_repository *repo)
761 const struct got_error *err = NULL, *child_err;
762 struct got_packidx_bloom_filter *bf;
763 size_t i;
765 for (i = 0; i < repo->pack_cache_size; i++) {
766 if (repo->packidx_cache[i] == NULL)
767 break;
768 got_packidx_close(repo->packidx_cache[i]);
771 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters))) {
773 RB_REMOVE(got_packidx_bloom_filter_tree,
774 &repo->packidx_bloom_filters, bf);
775 free(bf->bloom);
776 free(bf);
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packs[i].path_packfile == NULL)
781 break;
782 got_pack_close(&repo->packs[i]);
785 free(repo->path);
786 free(repo->path_git_dir);
788 got_object_cache_close(&repo->objcache);
789 got_object_cache_close(&repo->treecache);
790 got_object_cache_close(&repo->commitcache);
791 got_object_cache_close(&repo->tagcache);
792 got_object_cache_close(&repo->rawcache);
794 for (i = 0; i < nitems(repo->privsep_children); i++) {
795 if (repo->privsep_children[i].imsg_fd == -1)
796 continue;
797 imsg_clear(repo->privsep_children[i].ibuf);
798 free(repo->privsep_children[i].ibuf);
799 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
800 child_err = got_privsep_wait_for_child(
801 repo->privsep_children[i].pid);
802 if (child_err && err == NULL)
803 err = child_err;
804 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
805 err == NULL)
806 err = got_error_from_errno("close");
809 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
810 err == NULL)
811 err = got_error_from_errno("close");
813 if (repo->gotconfig)
814 got_gotconfig_free(repo->gotconfig);
815 free(repo->gitconfig_author_name);
816 free(repo->gitconfig_author_email);
817 for (i = 0; i < repo->ngitconfig_remotes; i++)
818 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
819 free(repo->gitconfig_remotes);
820 for (i = 0; i < repo->nextensions; i++)
821 free(repo->extensions[i]);
822 free(repo->extensions);
823 free(repo);
825 return err;
828 void
829 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
831 int i;
833 free(repo->name);
834 repo->name = NULL;
835 free(repo->fetch_url);
836 repo->fetch_url = NULL;
837 free(repo->send_url);
838 repo->send_url = NULL;
839 for (i = 0; i < repo->nfetch_branches; i++)
840 free(repo->fetch_branches[i]);
841 free(repo->fetch_branches);
842 repo->fetch_branches = NULL;
843 repo->nfetch_branches = 0;
844 for (i = 0; i < repo->nsend_branches; i++)
845 free(repo->send_branches[i]);
846 free(repo->send_branches);
847 repo->send_branches = NULL;
848 repo->nsend_branches = 0;
851 const struct got_error *
852 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
853 const char *input_path)
855 const struct got_error *err = NULL;
856 const char *repo_abspath = NULL;
857 size_t repolen, len;
858 char *canonpath, *path = NULL;
860 *in_repo_path = NULL;
862 canonpath = strdup(input_path);
863 if (canonpath == NULL) {
864 err = got_error_from_errno("strdup");
865 goto done;
867 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
868 if (err)
869 goto done;
871 repo_abspath = got_repo_get_path(repo);
873 if (canonpath[0] == '\0') {
874 path = strdup(canonpath);
875 if (path == NULL) {
876 err = got_error_from_errno("strdup");
877 goto done;
879 } else {
880 path = realpath(canonpath, NULL);
881 if (path == NULL) {
882 if (errno != ENOENT) {
883 err = got_error_from_errno2("realpath",
884 canonpath);
885 goto done;
887 /*
888 * Path is not on disk.
889 * Assume it is already relative to repository root.
890 */
891 path = strdup(canonpath);
892 if (path == NULL) {
893 err = got_error_from_errno("strdup");
894 goto done;
898 repolen = strlen(repo_abspath);
899 len = strlen(path);
902 if (strcmp(path, repo_abspath) == 0) {
903 free(path);
904 path = strdup("");
905 if (path == NULL) {
906 err = got_error_from_errno("strdup");
907 goto done;
909 } else if (len > repolen &&
910 got_path_is_child(path, repo_abspath, repolen)) {
911 /* Matched an on-disk path inside repository. */
912 if (got_repo_is_bare(repo)) {
913 /*
914 * Matched an on-disk path inside repository
915 * database. Treat input as repository-relative.
916 */
917 free(path);
918 path = canonpath;
919 canonpath = NULL;
920 } else {
921 char *child;
922 /* Strip common prefix with repository path. */
923 err = got_path_skip_common_ancestor(&child,
924 repo_abspath, path);
925 if (err)
926 goto done;
927 free(path);
928 path = child;
930 } else {
931 /*
932 * Matched unrelated on-disk path.
933 * Treat input as repository-relative.
934 */
935 free(path);
936 path = canonpath;
937 canonpath = NULL;
941 /* Make in-repository path absolute */
942 if (path[0] != '/') {
943 char *abspath;
944 if (asprintf(&abspath, "/%s", path) == -1) {
945 err = got_error_from_errno("asprintf");
946 goto done;
948 free(path);
949 path = abspath;
952 done:
953 free(canonpath);
954 if (err)
955 free(path);
956 else
957 *in_repo_path = path;
958 return err;
961 static const struct got_error *
962 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
963 const char *path_packidx)
965 const struct got_error *err = NULL;
966 size_t i;
968 for (i = 0; i < repo->pack_cache_size; i++) {
969 if (repo->packidx_cache[i] == NULL)
970 break;
971 if (strcmp(repo->packidx_cache[i]->path_packidx,
972 path_packidx) == 0) {
973 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
976 if (i == repo->pack_cache_size) {
977 i = repo->pack_cache_size - 1;
978 err = got_packidx_close(repo->packidx_cache[i]);
979 if (err)
980 return err;
983 repo->packidx_cache[i] = packidx;
985 return NULL;
988 int
989 got_repo_is_packidx_filename(const char *name, size_t len)
991 if (len != GOT_PACKIDX_NAMELEN)
992 return 0;
994 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
995 return 0;
997 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
998 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
999 return 0;
1001 return 1;
1004 static struct got_packidx_bloom_filter *
1005 get_packidx_bloom_filter(struct got_repository *repo,
1006 const char *path, size_t path_len)
1008 struct got_packidx_bloom_filter key;
1010 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1011 return NULL; /* XXX */
1012 key.path_len = path_len;
1014 return RB_FIND(got_packidx_bloom_filter_tree,
1015 &repo->packidx_bloom_filters, &key);
1018 static int
1019 check_packidx_bloom_filter(struct got_repository *repo,
1020 const char *path_packidx, struct got_object_id *id)
1022 struct got_packidx_bloom_filter *bf;
1024 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1025 if (bf)
1026 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1028 /* No bloom filter means this pack index must be searched. */
1029 return 1;
1032 static const struct got_error *
1033 add_packidx_bloom_filter(struct got_repository *repo,
1034 struct got_packidx *packidx, const char *path_packidx)
1036 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1037 struct got_packidx_bloom_filter *bf;
1038 size_t len;
1041 * Don't use bloom filters for very large pack index files.
1042 * Large pack files will contain a relatively large fraction
1043 * of our objects so we will likely need to visit them anyway.
1044 * The more objects a pack file contains the higher the probability
1045 * of a false-positive match from the bloom filter. And reading
1046 * all object IDs from a large pack index file can be expensive.
1048 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1049 return NULL;
1051 /* Do we already have a filter for this pack index? */
1052 if (get_packidx_bloom_filter(repo, path_packidx,
1053 strlen(path_packidx)) != NULL)
1054 return NULL;
1056 bf = calloc(1, sizeof(*bf));
1057 if (bf == NULL)
1058 return got_error_from_errno("calloc");
1059 bf->bloom = calloc(1, sizeof(*bf->bloom));
1060 if (bf->bloom == NULL) {
1061 free(bf);
1062 return got_error_from_errno("calloc");
1065 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1066 if (len >= sizeof(bf->path)) {
1067 free(bf->bloom);
1068 free(bf);
1069 return got_error(GOT_ERR_NO_SPACE);
1071 bf->path_len = len;
1073 /* Minimum size supported by our bloom filter is 1000 entries. */
1074 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1075 for (i = 0; i < nobjects; i++) {
1076 struct got_packidx_object_id *id;
1077 id = &packidx->hdr.sorted_ids[i];
1078 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1081 RB_INSERT(got_packidx_bloom_filter_tree,
1082 &repo->packidx_bloom_filters, bf);
1083 return NULL;
1086 const struct got_error *
1087 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1088 struct got_repository *repo, struct got_object_id *id)
1090 const struct got_error *err;
1091 DIR *packdir = NULL;
1092 struct dirent *dent;
1093 char *path_packidx;
1094 size_t i;
1095 int packdir_fd;
1097 /* Search pack index cache. */
1098 for (i = 0; i < repo->pack_cache_size; i++) {
1099 if (repo->packidx_cache[i] == NULL)
1100 break;
1101 if (!check_packidx_bloom_filter(repo,
1102 repo->packidx_cache[i]->path_packidx, id))
1103 continue; /* object will not be found in this index */
1104 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1105 if (*idx != -1) {
1106 *packidx = repo->packidx_cache[i];
1108 * Move this cache entry to the front. Repeatedly
1109 * searching a wrong pack index can be expensive.
1111 if (i > 0) {
1112 memmove(&repo->packidx_cache[1],
1113 &repo->packidx_cache[0],
1114 i * sizeof(repo->packidx_cache[0]));
1115 repo->packidx_cache[0] = *packidx;
1117 return NULL;
1120 /* No luck. Search the filesystem. */
1122 packdir_fd = openat(got_repo_get_fd(repo),
1123 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1124 if (packdir_fd == -1) {
1125 if (errno == ENOENT)
1126 err = got_error_no_obj(id);
1127 else
1128 err = got_error_from_errno_fmt("openat: %s/%s",
1129 got_repo_get_path_git_dir(repo),
1130 GOT_OBJECTS_PACK_DIR);
1131 goto done;
1134 packdir = fdopendir(packdir_fd);
1135 if (packdir == NULL) {
1136 err = got_error_from_errno("fdopendir");
1137 goto done;
1140 while ((dent = readdir(packdir)) != NULL) {
1141 int is_cached = 0;
1143 if (!got_repo_is_packidx_filename(dent->d_name,
1144 strlen(dent->d_name)))
1145 continue;
1147 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1148 dent->d_name) == -1) {
1149 err = got_error_from_errno("asprintf");
1150 goto done;
1153 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1154 free(path_packidx);
1155 continue; /* object will not be found in this index */
1158 for (i = 0; i < repo->pack_cache_size; i++) {
1159 if (repo->packidx_cache[i] == NULL)
1160 break;
1161 if (strcmp(repo->packidx_cache[i]->path_packidx,
1162 path_packidx) == 0) {
1163 is_cached = 1;
1164 break;
1167 if (is_cached) {
1168 free(path_packidx);
1169 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1173 path_packidx, 0);
1174 if (err) {
1175 free(path_packidx);
1176 goto done;
1179 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1180 if (err) {
1181 free(path_packidx);
1182 goto done;
1185 err = cache_packidx(repo, *packidx, path_packidx);
1186 free(path_packidx);
1187 if (err)
1188 goto done;
1190 *idx = got_packidx_get_object_idx(*packidx, id);
1191 if (*idx != -1) {
1192 err = NULL; /* found the object */
1193 goto done;
1197 err = got_error_no_obj(id);
1198 done:
1199 if (packdir && closedir(packdir) != 0 && err == NULL)
1200 err = got_error_from_errno("closedir");
1201 return err;
1204 static const struct got_error *
1205 read_packfile_hdr(int fd, struct got_packidx *packidx)
1207 const struct got_error *err = NULL;
1208 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1209 struct got_packfile_hdr hdr;
1210 ssize_t n;
1212 n = read(fd, &hdr, sizeof(hdr));
1213 if (n < 0)
1214 return got_error_from_errno("read");
1215 if (n != sizeof(hdr))
1216 return got_error(GOT_ERR_BAD_PACKFILE);
1218 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1219 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1220 be32toh(hdr.nobjects) != totobj)
1221 err = got_error(GOT_ERR_BAD_PACKFILE);
1223 return err;
1226 static const struct got_error *
1227 open_packfile(int *fd, struct got_repository *repo,
1228 const char *relpath, struct got_packidx *packidx)
1230 const struct got_error *err = NULL;
1232 *fd = openat(got_repo_get_fd(repo), relpath,
1233 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1234 if (*fd == -1)
1235 return got_error_from_errno_fmt("openat: %s/%s",
1236 got_repo_get_path_git_dir(repo), relpath);
1238 if (packidx) {
1239 err = read_packfile_hdr(*fd, packidx);
1240 if (err) {
1241 close(*fd);
1242 *fd = -1;
1246 return err;
1249 const struct got_error *
1250 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1251 const char *path_packfile, struct got_packidx *packidx)
1253 const struct got_error *err = NULL;
1254 struct got_pack *pack = NULL;
1255 struct stat sb;
1256 size_t i;
1258 if (packp)
1259 *packp = NULL;
1261 for (i = 0; i < repo->pack_cache_size; i++) {
1262 pack = &repo->packs[i];
1263 if (pack->path_packfile == NULL)
1264 break;
1265 if (strcmp(pack->path_packfile, path_packfile) == 0)
1266 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1269 if (i == repo->pack_cache_size) {
1270 err = got_pack_close(&repo->packs[i - 1]);
1271 if (err)
1272 return err;
1273 memmove(&repo->packs[1], &repo->packs[0],
1274 sizeof(repo->packs) - sizeof(repo->packs[0]));
1275 i = 0;
1278 pack = &repo->packs[i];
1280 pack->path_packfile = strdup(path_packfile);
1281 if (pack->path_packfile == NULL) {
1282 err = got_error_from_errno("strdup");
1283 goto done;
1286 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1287 if (err)
1288 goto done;
1290 if (fstat(pack->fd, &sb) != 0) {
1291 err = got_error_from_errno("fstat");
1292 goto done;
1294 pack->filesize = sb.st_size;
1296 pack->privsep_child = NULL;
1298 #ifndef GOT_PACK_NO_MMAP
1299 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1300 pack->fd, 0);
1301 if (pack->map == MAP_FAILED) {
1302 if (errno != ENOMEM) {
1303 err = got_error_from_errno("mmap");
1304 goto done;
1306 pack->map = NULL; /* fall back to read(2) */
1308 #endif
1309 done:
1310 if (err) {
1311 if (pack) {
1312 free(pack->path_packfile);
1313 memset(pack, 0, sizeof(*pack));
1315 } else if (packp)
1316 *packp = pack;
1317 return err;
1320 struct got_pack *
1321 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1323 struct got_pack *pack = NULL;
1324 size_t i;
1326 for (i = 0; i < repo->pack_cache_size; i++) {
1327 pack = &repo->packs[i];
1328 if (pack->path_packfile == NULL)
1329 break;
1330 if (strcmp(pack->path_packfile, path_packfile) == 0)
1331 return pack;
1334 return NULL;
1337 const struct got_error *
1338 got_repo_init(const char *repo_path)
1340 const struct got_error *err = NULL;
1341 const char *dirnames[] = {
1342 GOT_OBJECTS_DIR,
1343 GOT_OBJECTS_PACK_DIR,
1344 GOT_REFS_DIR,
1346 const char *description_str = "Unnamed repository; "
1347 "edit this file 'description' to name the repository.";
1348 const char *headref_str = "ref: refs/heads/main";
1349 const char *gitconfig_str = "[core]\n"
1350 "\trepositoryformatversion = 0\n"
1351 "\tfilemode = true\n"
1352 "\tbare = true\n";
1353 char *path;
1354 size_t i;
1356 if (!got_path_dir_is_empty(repo_path))
1357 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1359 for (i = 0; i < nitems(dirnames); i++) {
1360 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1361 return got_error_from_errno("asprintf");
1363 err = got_path_mkdir(path);
1364 free(path);
1365 if (err)
1366 return err;
1369 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1370 return got_error_from_errno("asprintf");
1371 err = got_path_create_file(path, description_str);
1372 free(path);
1373 if (err)
1374 return err;
1376 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1377 return got_error_from_errno("asprintf");
1378 err = got_path_create_file(path, headref_str);
1379 free(path);
1380 if (err)
1381 return err;
1383 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1384 return got_error_from_errno("asprintf");
1385 err = got_path_create_file(path, gitconfig_str);
1386 free(path);
1387 if (err)
1388 return err;
1390 return NULL;
1393 static const struct got_error *
1394 match_packed_object(struct got_object_id **unique_id,
1395 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1397 const struct got_error *err = NULL;
1398 DIR *packdir = NULL;
1399 struct dirent *dent;
1400 char *path_packidx;
1401 struct got_object_id_queue matched_ids;
1402 int packdir_fd;
1404 STAILQ_INIT(&matched_ids);
1406 packdir_fd = openat(got_repo_get_fd(repo),
1407 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1408 if (packdir_fd == -1) {
1409 if (errno != ENOENT) {
1410 err = got_error_from_errno2("openat",
1411 GOT_OBJECTS_PACK_DIR);
1413 goto done;
1416 packdir = fdopendir(packdir_fd);
1417 if (packdir == NULL) {
1418 err = got_error_from_errno("fdopendir");
1419 goto done;
1422 while ((dent = readdir(packdir)) != NULL) {
1423 struct got_packidx *packidx;
1424 struct got_object_qid *qid;
1426 if (!got_repo_is_packidx_filename(dent->d_name,
1427 strlen(dent->d_name)))
1428 continue;
1430 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1431 dent->d_name) == -1) {
1432 err = got_error_from_errno("strdup");
1433 break;
1436 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1437 path_packidx, 0);
1438 free(path_packidx);
1439 if (err)
1440 break;
1442 err = got_packidx_match_id_str_prefix(&matched_ids,
1443 packidx, id_str_prefix);
1444 if (err) {
1445 got_packidx_close(packidx);
1446 break;
1448 err = got_packidx_close(packidx);
1449 if (err)
1450 break;
1452 STAILQ_FOREACH(qid, &matched_ids, entry) {
1453 if (obj_type != GOT_OBJ_TYPE_ANY) {
1454 int matched_type;
1455 err = got_object_get_type(&matched_type, repo,
1456 qid->id);
1457 if (err)
1458 goto done;
1459 if (matched_type != obj_type)
1460 continue;
1462 if (*unique_id == NULL) {
1463 *unique_id = got_object_id_dup(qid->id);
1464 if (*unique_id == NULL) {
1465 err = got_error_from_errno("malloc");
1466 goto done;
1468 } else {
1469 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1470 continue; /* packed multiple times */
1471 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1472 goto done;
1476 done:
1477 got_object_id_queue_free(&matched_ids);
1478 if (packdir && closedir(packdir) != 0 && err == NULL)
1479 err = got_error_from_errno("closedir");
1480 if (err) {
1481 free(*unique_id);
1482 *unique_id = NULL;
1484 return err;
1487 static const struct got_error *
1488 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1489 const char *object_dir, const char *id_str_prefix, int obj_type,
1490 struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 char *path;
1494 DIR *dir = NULL;
1495 struct dirent *dent;
1496 struct got_object_id id;
1498 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1499 err = got_error_from_errno("asprintf");
1500 goto done;
1503 dir = opendir(path);
1504 if (dir == NULL) {
1505 if (errno == ENOENT) {
1506 err = NULL;
1507 goto done;
1509 err = got_error_from_errno2("opendir", path);
1510 goto done;
1512 while ((dent = readdir(dir)) != NULL) {
1513 char *id_str;
1514 int cmp;
1516 if (strcmp(dent->d_name, ".") == 0 ||
1517 strcmp(dent->d_name, "..") == 0)
1518 continue;
1520 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1521 err = got_error_from_errno("asprintf");
1522 goto done;
1525 if (!got_parse_sha1_digest(id.sha1, id_str))
1526 continue;
1529 * Directory entries do not necessarily appear in
1530 * sorted order, so we must iterate over all of them.
1532 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1533 if (cmp != 0) {
1534 free(id_str);
1535 continue;
1538 if (*unique_id == NULL) {
1539 if (obj_type != GOT_OBJ_TYPE_ANY) {
1540 int matched_type;
1541 err = got_object_get_type(&matched_type, repo,
1542 &id);
1543 if (err)
1544 goto done;
1545 if (matched_type != obj_type)
1546 continue;
1548 *unique_id = got_object_id_dup(&id);
1549 if (*unique_id == NULL) {
1550 err = got_error_from_errno("got_object_id_dup");
1551 free(id_str);
1552 goto done;
1554 } else {
1555 if (got_object_id_cmp(*unique_id, &id) == 0)
1556 continue; /* both packed and loose */
1557 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1558 free(id_str);
1559 goto done;
1562 done:
1563 if (dir && closedir(dir) != 0 && err == NULL)
1564 err = got_error_from_errno("closedir");
1565 if (err) {
1566 free(*unique_id);
1567 *unique_id = NULL;
1569 free(path);
1570 return err;
1573 const struct got_error *
1574 got_repo_match_object_id_prefix(struct got_object_id **id,
1575 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1577 const struct got_error *err = NULL;
1578 char *path_objects = got_repo_get_path_objects(repo);
1579 char *object_dir = NULL;
1580 size_t len;
1581 int i;
1583 *id = NULL;
1585 for (i = 0; i < strlen(id_str_prefix); i++) {
1586 if (isxdigit((unsigned char)id_str_prefix[i]))
1587 continue;
1588 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1591 len = strlen(id_str_prefix);
1592 if (len >= 2) {
1593 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1594 if (err)
1595 goto done;
1596 object_dir = strndup(id_str_prefix, 2);
1597 if (object_dir == NULL) {
1598 err = got_error_from_errno("strdup");
1599 goto done;
1601 err = match_loose_object(id, path_objects, object_dir,
1602 id_str_prefix, obj_type, repo);
1603 } else if (len == 1) {
1604 int i;
1605 for (i = 0; i < 0xf; i++) {
1606 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1607 == -1) {
1608 err = got_error_from_errno("asprintf");
1609 goto done;
1611 err = match_packed_object(id, repo, object_dir,
1612 obj_type);
1613 if (err)
1614 goto done;
1615 err = match_loose_object(id, path_objects, object_dir,
1616 id_str_prefix, obj_type, repo);
1617 if (err)
1618 goto done;
1620 } else {
1621 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1622 goto done;
1624 done:
1625 free(object_dir);
1626 if (err) {
1627 free(*id);
1628 *id = NULL;
1629 } else if (*id == NULL) {
1630 switch (obj_type) {
1631 case GOT_OBJ_TYPE_BLOB:
1632 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1633 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1634 break;
1635 case GOT_OBJ_TYPE_TREE:
1636 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1637 GOT_OBJ_LABEL_TREE, id_str_prefix);
1638 break;
1639 case GOT_OBJ_TYPE_COMMIT:
1640 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1641 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1642 break;
1643 case GOT_OBJ_TYPE_TAG:
1644 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1645 GOT_OBJ_LABEL_TAG, id_str_prefix);
1646 break;
1647 default:
1648 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1649 break;
1653 return err;
1656 const struct got_error *
1657 got_repo_match_object_id(struct got_object_id **id, char **label,
1658 const char *id_str, int obj_type, struct got_reflist_head *refs,
1659 struct got_repository *repo)
1661 const struct got_error *err;
1662 struct got_tag_object *tag;
1663 struct got_reference *ref = NULL;
1665 *id = NULL;
1666 if (label)
1667 *label = NULL;
1669 if (refs) {
1670 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1671 refs, repo);
1672 if (err == NULL) {
1673 *id = got_object_id_dup(
1674 got_object_tag_get_object_id(tag));
1675 if (*id == NULL)
1676 err = got_error_from_errno("got_object_id_dup");
1677 else if (label && asprintf(label, "refs/tags/%s",
1678 got_object_tag_get_name(tag)) == -1) {
1679 err = got_error_from_errno("asprintf");
1680 free(*id);
1681 *id = NULL;
1683 got_object_tag_close(tag);
1684 return err;
1685 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1686 err->code != GOT_ERR_NO_OBJ)
1687 return err;
1690 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1691 if (err) {
1692 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1693 return err;
1694 err = got_ref_open(&ref, repo, id_str, 0);
1695 if (err != NULL)
1696 goto done;
1697 if (label) {
1698 *label = strdup(got_ref_get_name(ref));
1699 if (*label == NULL) {
1700 err = got_error_from_errno("strdup");
1701 goto done;
1704 err = got_ref_resolve(id, repo, ref);
1705 } else if (label) {
1706 err = got_object_id_str(label, *id);
1707 if (*label == NULL) {
1708 err = got_error_from_errno("strdup");
1709 goto done;
1712 done:
1713 if (ref)
1714 got_ref_close(ref);
1715 return err;
1718 const struct got_error *
1719 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1720 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1722 const struct got_error *err = NULL;
1723 struct got_reflist_entry *re;
1724 struct got_object_id *tag_id;
1725 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1727 *tag = NULL;
1729 TAILQ_FOREACH(re, refs, entry) {
1730 const char *refname;
1731 refname = got_ref_get_name(re->ref);
1732 if (got_ref_is_symbolic(re->ref))
1733 continue;
1734 if (strncmp(refname, "refs/tags/", 10) != 0)
1735 continue;
1736 if (!name_is_absolute)
1737 refname += strlen("refs/tags/");
1738 if (strcmp(refname, name) != 0)
1739 continue;
1740 err = got_ref_resolve(&tag_id, repo, re->ref);
1741 if (err)
1742 break;
1743 err = got_object_open_as_tag(tag, repo, tag_id);
1744 free(tag_id);
1745 if (err)
1746 break;
1747 if (obj_type == GOT_OBJ_TYPE_ANY ||
1748 got_object_tag_get_object_type(*tag) == obj_type)
1749 break;
1750 got_object_tag_close(*tag);
1751 *tag = NULL;
1754 if (err == NULL && *tag == NULL)
1755 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1756 GOT_OBJ_LABEL_TAG, name);
1757 return err;
1760 static const struct got_error *
1761 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1762 const char *name, mode_t mode, struct got_object_id *blob_id)
1764 const struct got_error *err = NULL;
1766 *new_te = NULL;
1768 *new_te = calloc(1, sizeof(**new_te));
1769 if (*new_te == NULL)
1770 return got_error_from_errno("calloc");
1772 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1773 sizeof((*new_te)->name)) {
1774 err = got_error(GOT_ERR_NO_SPACE);
1775 goto done;
1778 if (S_ISLNK(mode)) {
1779 (*new_te)->mode = S_IFLNK;
1780 } else {
1781 (*new_te)->mode = S_IFREG;
1782 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1784 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1785 done:
1786 if (err && *new_te) {
1787 free(*new_te);
1788 *new_te = NULL;
1790 return err;
1793 static const struct got_error *
1794 import_file(struct got_tree_entry **new_te, struct dirent *de,
1795 const char *path, struct got_repository *repo)
1797 const struct got_error *err;
1798 struct got_object_id *blob_id = NULL;
1799 char *filepath;
1800 struct stat sb;
1802 if (asprintf(&filepath, "%s%s%s", path,
1803 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1804 return got_error_from_errno("asprintf");
1806 if (lstat(filepath, &sb) != 0) {
1807 err = got_error_from_errno2("lstat", path);
1808 goto done;
1811 err = got_object_blob_create(&blob_id, filepath, repo);
1812 if (err)
1813 goto done;
1815 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1816 blob_id);
1817 done:
1818 free(filepath);
1819 if (err)
1820 free(blob_id);
1821 return err;
1824 static const struct got_error *
1825 insert_tree_entry(struct got_tree_entry *new_te,
1826 struct got_pathlist_head *paths)
1828 const struct got_error *err = NULL;
1829 struct got_pathlist_entry *new_pe;
1831 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1832 if (err)
1833 return err;
1834 if (new_pe == NULL)
1835 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1836 return NULL;
1839 static const struct got_error *write_tree(struct got_object_id **,
1840 const char *, struct got_pathlist_head *, struct got_repository *,
1841 got_repo_import_cb progress_cb, void *progress_arg);
1843 static const struct got_error *
1844 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1845 const char *path, struct got_pathlist_head *ignores,
1846 struct got_repository *repo,
1847 got_repo_import_cb progress_cb, void *progress_arg)
1849 const struct got_error *err;
1850 struct got_object_id *id = NULL;
1851 char *subdirpath;
1853 if (asprintf(&subdirpath, "%s%s%s", path,
1854 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1855 return got_error_from_errno("asprintf");
1857 (*new_te) = calloc(1, sizeof(**new_te));
1858 if (*new_te == NULL)
1859 return got_error_from_errno("calloc");
1860 (*new_te)->mode = S_IFDIR;
1861 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1862 sizeof((*new_te)->name)) {
1863 err = got_error(GOT_ERR_NO_SPACE);
1864 goto done;
1866 err = write_tree(&id, subdirpath, ignores, repo,
1867 progress_cb, progress_arg);
1868 if (err)
1869 goto done;
1870 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1872 done:
1873 free(id);
1874 free(subdirpath);
1875 if (err) {
1876 free(*new_te);
1877 *new_te = NULL;
1879 return err;
1882 static const struct got_error *
1883 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1884 struct got_pathlist_head *ignores, struct got_repository *repo,
1885 got_repo_import_cb progress_cb, void *progress_arg)
1887 const struct got_error *err = NULL;
1888 DIR *dir;
1889 struct dirent *de;
1890 int nentries;
1891 struct got_tree_entry *new_te = NULL;
1892 struct got_pathlist_head paths;
1893 struct got_pathlist_entry *pe;
1895 *new_tree_id = NULL;
1897 TAILQ_INIT(&paths);
1899 dir = opendir(path_dir);
1900 if (dir == NULL) {
1901 err = got_error_from_errno2("opendir", path_dir);
1902 goto done;
1905 nentries = 0;
1906 while ((de = readdir(dir)) != NULL) {
1907 int ignore = 0;
1908 int type;
1910 if (strcmp(de->d_name, ".") == 0 ||
1911 strcmp(de->d_name, "..") == 0)
1912 continue;
1914 TAILQ_FOREACH(pe, ignores, entry) {
1915 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1916 ignore = 1;
1917 break;
1920 if (ignore)
1921 continue;
1923 err = got_path_dirent_type(&type, path_dir, de);
1924 if (err)
1925 goto done;
1927 if (type == DT_DIR) {
1928 err = import_subdir(&new_te, de, path_dir,
1929 ignores, repo, progress_cb, progress_arg);
1930 if (err) {
1931 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1932 goto done;
1933 err = NULL;
1934 continue;
1936 } else if (type == DT_REG || type == DT_LNK) {
1937 err = import_file(&new_te, de, path_dir, repo);
1938 if (err)
1939 goto done;
1940 } else
1941 continue;
1943 err = insert_tree_entry(new_te, &paths);
1944 if (err)
1945 goto done;
1946 nentries++;
1949 if (TAILQ_EMPTY(&paths)) {
1950 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1951 "cannot create tree without any entries");
1952 goto done;
1955 TAILQ_FOREACH(pe, &paths, entry) {
1956 struct got_tree_entry *te = pe->data;
1957 char *path;
1958 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1959 continue;
1960 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1961 err = got_error_from_errno("asprintf");
1962 goto done;
1964 err = (*progress_cb)(progress_arg, path);
1965 free(path);
1966 if (err)
1967 goto done;
1970 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1971 done:
1972 if (dir)
1973 closedir(dir);
1974 got_pathlist_free(&paths);
1975 return err;
1978 const struct got_error *
1979 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1980 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1981 struct got_repository *repo, got_repo_import_cb progress_cb,
1982 void *progress_arg)
1984 const struct got_error *err;
1985 struct got_object_id *new_tree_id;
1987 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1988 progress_cb, progress_arg);
1989 if (err)
1990 return err;
1992 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1993 author, time(NULL), author, time(NULL), logmsg, repo);
1994 free(new_tree_id);
1995 return err;
1998 const struct got_error *
1999 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2000 struct got_repository *repo)
2002 const struct got_error *err = NULL;
2003 char *path_objects = NULL, *path = NULL;
2004 DIR *dir = NULL;
2005 struct got_object_id id;
2006 int i;
2008 *nobjects = 0;
2009 *ondisk_size = 0;
2011 path_objects = got_repo_get_path_objects(repo);
2012 if (path_objects == NULL)
2013 return got_error_from_errno("got_repo_get_path_objects");
2015 for (i = 0; i <= 0xff; i++) {
2016 struct dirent *dent;
2018 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2019 err = got_error_from_errno("asprintf");
2020 break;
2023 dir = opendir(path);
2024 if (dir == NULL) {
2025 if (errno == ENOENT) {
2026 err = NULL;
2027 continue;
2029 err = got_error_from_errno2("opendir", path);
2030 break;
2033 while ((dent = readdir(dir)) != NULL) {
2034 char *id_str;
2035 int fd;
2036 struct stat sb;
2038 if (strcmp(dent->d_name, ".") == 0 ||
2039 strcmp(dent->d_name, "..") == 0)
2040 continue;
2042 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2043 err = got_error_from_errno("asprintf");
2044 goto done;
2047 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2048 free(id_str);
2049 continue;
2051 free(id_str);
2053 err = got_object_open_loose_fd(&fd, &id, repo);
2054 if (err)
2055 goto done;
2057 if (fstat(fd, &sb) == -1) {
2058 err = got_error_from_errno("fstat");
2059 close(fd);
2060 goto done;
2062 (*nobjects)++;
2063 (*ondisk_size) += sb.st_size;
2065 if (close(fd) == -1) {
2066 err = got_error_from_errno("close");
2067 goto done;
2071 if (closedir(dir) != 0) {
2072 err = got_error_from_errno("closedir");
2073 goto done;
2075 dir = NULL;
2077 free(path);
2078 path = NULL;
2080 done:
2081 if (dir && closedir(dir) != 0 && err == NULL)
2082 err = got_error_from_errno("closedir");
2084 if (err) {
2085 *nobjects = 0;
2086 *ondisk_size = 0;
2088 free(path_objects);
2089 free(path);
2090 return err;
2093 const struct got_error *
2094 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2095 off_t *total_packsize, struct got_repository *repo)
2097 const struct got_error *err = NULL;
2098 DIR *packdir = NULL;
2099 struct dirent *dent;
2100 struct got_packidx *packidx = NULL;
2101 char *path_packidx;
2102 char *path_packfile;
2103 int packdir_fd;
2104 struct stat sb;
2106 *npackfiles = 0;
2107 *nobjects = 0;
2108 *total_packsize = 0;
2110 packdir_fd = openat(got_repo_get_fd(repo),
2111 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2112 if (packdir_fd == -1) {
2113 return got_error_from_errno_fmt("openat: %s/%s",
2114 got_repo_get_path_git_dir(repo),
2115 GOT_OBJECTS_PACK_DIR);
2118 packdir = fdopendir(packdir_fd);
2119 if (packdir == NULL) {
2120 err = got_error_from_errno("fdopendir");
2121 goto done;
2124 while ((dent = readdir(packdir)) != NULL) {
2125 if (!got_repo_is_packidx_filename(dent->d_name,
2126 strlen(dent->d_name)))
2127 continue;
2129 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2130 dent->d_name) == -1) {
2131 err = got_error_from_errno("asprintf");
2132 goto done;
2135 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2136 path_packidx, 0);
2137 free(path_packidx);
2138 if (err)
2139 goto done;
2141 if (fstat(packidx->fd, &sb) == -1)
2142 goto done;
2143 *total_packsize += sb.st_size;
2145 err = got_packidx_get_packfile_path(&path_packfile,
2146 packidx->path_packidx);
2147 if (err)
2148 goto done;
2150 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2151 0) == -1) {
2152 free(path_packfile);
2153 goto done;
2155 free(path_packfile);
2156 *total_packsize += sb.st_size;
2158 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2160 (*npackfiles)++;
2162 got_packidx_close(packidx);
2163 packidx = NULL;
2165 done:
2166 if (packidx)
2167 got_packidx_close(packidx);
2168 if (packdir && closedir(packdir) != 0 && err == NULL)
2169 err = got_error_from_errno("closedir");
2170 if (err) {
2171 *npackfiles = 0;
2172 *nobjects = 0;
2173 *total_packsize = 0;
2175 return err;
2178 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2179 got_packidx_bloom_filter_cmp);