Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <fnmatch.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <stdint.h>
40 #include <uuid.h>
42 #include "bloom.h"
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
68 got_packidx_bloom_filter_cmp);
70 const char *
71 got_repo_get_path(struct got_repository *repo)
72 {
73 return repo->path;
74 }
76 const char *
77 got_repo_get_path_git_dir(struct got_repository *repo)
78 {
79 return repo->path_git_dir;
80 }
82 int
83 got_repo_get_fd(struct got_repository *repo)
84 {
85 return repo->gitdir_fd;
86 }
88 const char *
89 got_repo_get_gitconfig_author_name(struct got_repository *repo)
90 {
91 return repo->gitconfig_author_name;
92 }
94 const char *
95 got_repo_get_gitconfig_author_email(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_email;
98 }
100 const char *
101 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
103 return repo->global_gitconfig_author_name;
106 const char *
107 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
109 return repo->global_gitconfig_author_email;
112 const char *
113 got_repo_get_gitconfig_owner(struct got_repository *repo)
115 return repo->gitconfig_owner;
118 void
119 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
120 struct got_repository *repo)
122 *extensions = repo->extensions;
123 *nextensions = repo->nextensions;
126 int
127 got_repo_is_bare(struct got_repository *repo)
129 return (strcmp(repo->path, repo->path_git_dir) == 0);
132 static char *
133 get_path_git_child(struct got_repository *repo, const char *basename)
135 char *path_child;
137 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
138 basename) == -1)
139 return NULL;
141 return path_child;
144 char *
145 got_repo_get_path_objects(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_OBJECTS_DIR);
150 char *
151 got_repo_get_path_objects_pack(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
156 char *
157 got_repo_get_path_refs(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_REFS_DIR);
162 char *
163 got_repo_get_path_packed_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
168 static char *
169 get_path_head(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_HEAD_FILE);
174 char *
175 got_repo_get_path_gitconfig(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_GITCONFIG);
180 char *
181 got_repo_get_path_gotconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
186 const struct got_gotconfig *
187 got_repo_get_gotconfig(struct got_repository *repo)
189 return repo->gotconfig;
192 void
193 got_repo_get_gitconfig_remotes(int *nremotes,
194 const struct got_remote_repo **remotes, struct got_repository *repo)
196 *nremotes = repo->ngitconfig_remotes;
197 *remotes = repo->gitconfig_remotes;
200 static int
201 is_git_repo(struct got_repository *repo)
203 const char *path_git = got_repo_get_path_git_dir(repo);
204 char *path_objects = got_repo_get_path_objects(repo);
205 char *path_refs = got_repo_get_path_refs(repo);
206 char *path_head = get_path_head(repo);
207 int ret = 0;
208 struct stat sb;
209 struct got_reference *head_ref;
211 if (lstat(path_git, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_objects, &sb) == -1)
217 goto done;
218 if (!S_ISDIR(sb.st_mode))
219 goto done;
221 if (lstat(path_refs, &sb) == -1)
222 goto done;
223 if (!S_ISDIR(sb.st_mode))
224 goto done;
226 if (lstat(path_head, &sb) == -1)
227 goto done;
228 if (!S_ISREG(sb.st_mode))
229 goto done;
231 /* Check if the HEAD reference can be opened. */
232 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
233 goto done;
234 got_ref_close(head_ref);
236 ret = 1;
237 done:
238 free(path_objects);
239 free(path_refs);
240 free(path_head);
241 return ret;
245 const struct got_error *
246 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
247 struct got_object *obj)
249 #ifndef GOT_NO_OBJ_CACHE
250 const struct got_error *err = NULL;
251 err = got_object_cache_add(&repo->objcache, id, obj);
252 if (err) {
253 if (err->code == GOT_ERR_OBJ_EXISTS ||
254 err->code == GOT_ERR_OBJ_TOO_LARGE)
255 err = NULL;
256 return err;
258 obj->refcnt++;
259 #endif
260 return NULL;
263 struct got_object *
264 got_repo_get_cached_object(struct got_repository *repo,
265 struct got_object_id *id)
267 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
270 const struct got_error *
271 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
272 struct got_tree_object *tree)
274 #ifndef GOT_NO_OBJ_CACHE
275 const struct got_error *err = NULL;
276 err = got_object_cache_add(&repo->treecache, id, tree);
277 if (err) {
278 if (err->code == GOT_ERR_OBJ_EXISTS ||
279 err->code == GOT_ERR_OBJ_TOO_LARGE)
280 err = NULL;
281 return err;
283 tree->refcnt++;
284 #endif
285 return NULL;
288 struct got_tree_object *
289 got_repo_get_cached_tree(struct got_repository *repo,
290 struct got_object_id *id)
292 return (struct got_tree_object *)got_object_cache_get(
293 &repo->treecache, id);
296 const struct got_error *
297 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
298 struct got_commit_object *commit)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->commitcache, id, commit);
303 if (err) {
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 err = NULL;
307 return err;
309 commit->refcnt++;
310 #endif
311 return NULL;
314 struct got_commit_object *
315 got_repo_get_cached_commit(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_commit_object *)got_object_cache_get(
319 &repo->commitcache, id);
322 const struct got_error *
323 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
324 struct got_tag_object *tag)
326 #ifndef GOT_NO_OBJ_CACHE
327 const struct got_error *err = NULL;
328 err = got_object_cache_add(&repo->tagcache, id, tag);
329 if (err) {
330 if (err->code == GOT_ERR_OBJ_EXISTS ||
331 err->code == GOT_ERR_OBJ_TOO_LARGE)
332 err = NULL;
333 return err;
335 tag->refcnt++;
336 #endif
337 return NULL;
340 struct got_tag_object *
341 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
343 return (struct got_tag_object *)got_object_cache_get(
344 &repo->tagcache, id);
347 static const struct got_error *
348 open_repo(struct got_repository *repo, const char *path)
350 const struct got_error *err = NULL;
352 repo->gitdir_fd = -1;
354 /* bare git repository? */
355 repo->path_git_dir = strdup(path);
356 if (repo->path_git_dir == NULL)
357 return got_error_from_errno("strdup");
358 if (is_git_repo(repo)) {
359 repo->path = strdup(repo->path_git_dir);
360 if (repo->path == NULL) {
361 err = got_error_from_errno("strdup");
362 goto done;
364 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
365 if (repo->gitdir_fd == -1) {
366 err = got_error_from_errno2("open",
367 repo->path_git_dir);
368 goto done;
370 return NULL;
373 /* git repository with working tree? */
374 free(repo->path_git_dir);
375 repo->path_git_dir = NULL;
376 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
377 err = got_error_from_errno("asprintf");
378 goto done;
380 if (is_git_repo(repo)) {
381 repo->path = strdup(path);
382 if (repo->path == NULL) {
383 err = got_error_from_errno("strdup");
384 goto done;
386 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
387 if (repo->gitdir_fd == -1) {
388 err = got_error_from_errno2("open",
389 repo->path_git_dir);
390 goto done;
392 return NULL;
395 err = got_error(GOT_ERR_NOT_GIT_REPO);
396 done:
397 if (err) {
398 free(repo->path);
399 repo->path = NULL;
400 free(repo->path_git_dir);
401 repo->path_git_dir = NULL;
402 if (repo->gitdir_fd != -1)
403 close(repo->gitdir_fd);
404 repo->gitdir_fd = -1;
407 return err;
410 static const struct got_error *
411 parse_gitconfig_file(int *gitconfig_repository_format_version,
412 char **gitconfig_author_name, char **gitconfig_author_email,
413 struct got_remote_repo **remotes, int *nremotes,
414 char **gitconfig_owner, char ***extensions, int *nextensions,
415 const char *gitconfig_path)
417 const struct got_error *err = NULL, *child_err = NULL;
418 int fd = -1;
419 int imsg_fds[2] = { -1, -1 };
420 pid_t pid;
421 struct imsgbuf *ibuf;
423 *gitconfig_repository_format_version = 0;
424 if (extensions)
425 *extensions = NULL;
426 if (nextensions)
427 *nextensions = 0;
428 *gitconfig_author_name = NULL;
429 *gitconfig_author_email = NULL;
430 if (remotes)
431 *remotes = NULL;
432 if (nremotes)
433 *nremotes = 0;
434 if (gitconfig_owner)
435 *gitconfig_owner = NULL;
437 fd = open(gitconfig_path, O_RDONLY);
438 if (fd == -1) {
439 if (errno == ENOENT)
440 return NULL;
441 return got_error_from_errno2("open", gitconfig_path);
444 ibuf = calloc(1, sizeof(*ibuf));
445 if (ibuf == NULL) {
446 err = got_error_from_errno("calloc");
447 goto done;
450 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
451 err = got_error_from_errno("socketpair");
452 goto done;
455 pid = fork();
456 if (pid == -1) {
457 err = got_error_from_errno("fork");
458 goto done;
459 } else if (pid == 0) {
460 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
461 gitconfig_path);
462 /* not reached */
465 if (close(imsg_fds[1]) == -1) {
466 err = got_error_from_errno("close");
467 goto done;
469 imsg_fds[1] = -1;
470 imsg_init(ibuf, imsg_fds[0]);
472 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
473 if (err)
474 goto done;
475 fd = -1;
477 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
478 if (err)
479 goto done;
481 err = got_privsep_recv_gitconfig_int(
482 gitconfig_repository_format_version, ibuf);
483 if (err)
484 goto done;
486 if (extensions && nextensions) {
487 err = got_privsep_send_gitconfig_repository_extensions_req(
488 ibuf);
489 if (err)
490 goto done;
491 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
492 if (err)
493 goto done;
494 if (*nextensions > 0) {
495 int i;
496 *extensions = calloc(*nextensions, sizeof(char *));
497 if (*extensions == NULL) {
498 err = got_error_from_errno("calloc");
499 goto done;
501 for (i = 0; i < *nextensions; i++) {
502 char *ext;
503 err = got_privsep_recv_gitconfig_str(&ext,
504 ibuf);
505 if (err)
506 goto done;
507 (*extensions)[i] = ext;
512 err = got_privsep_send_gitconfig_author_name_req(ibuf);
513 if (err)
514 goto done;
516 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
517 if (err)
518 goto done;
520 err = got_privsep_send_gitconfig_author_email_req(ibuf);
521 if (err)
522 goto done;
524 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
525 if (err)
526 goto done;
528 if (remotes && nremotes) {
529 err = got_privsep_send_gitconfig_remotes_req(ibuf);
530 if (err)
531 goto done;
533 err = got_privsep_recv_gitconfig_remotes(remotes,
534 nremotes, ibuf);
535 if (err)
536 goto done;
539 if (gitconfig_owner) {
540 err = got_privsep_send_gitconfig_owner_req(ibuf);
541 if (err)
542 goto done;
543 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
544 if (err)
545 goto done;
548 imsg_clear(ibuf);
549 err = got_privsep_send_stop(imsg_fds[0]);
550 child_err = got_privsep_wait_for_child(pid);
551 if (child_err && err == NULL)
552 err = child_err;
553 done:
554 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
555 err = got_error_from_errno("close");
556 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
557 err = got_error_from_errno("close");
558 if (fd != -1 && close(fd) == -1 && err == NULL)
559 err = got_error_from_errno2("close", gitconfig_path);
560 free(ibuf);
561 return err;
564 static const struct got_error *
565 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
567 const struct got_error *err = NULL;
568 char *repo_gitconfig_path = NULL;
570 if (global_gitconfig_path) {
571 /* Read settings from ~/.gitconfig. */
572 int dummy_repo_version;
573 err = parse_gitconfig_file(&dummy_repo_version,
574 &repo->global_gitconfig_author_name,
575 &repo->global_gitconfig_author_email,
576 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
577 if (err)
578 return err;
581 /* Read repository's .git/config file. */
582 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
583 if (repo_gitconfig_path == NULL)
584 return got_error_from_errno("got_repo_get_path_gitconfig");
586 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
587 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
588 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
589 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
590 repo_gitconfig_path);
591 if (err)
592 goto done;
593 done:
594 free(repo_gitconfig_path);
595 return err;
598 static const struct got_error *
599 read_gotconfig(struct got_repository *repo)
601 const struct got_error *err = NULL;
602 char *gotconfig_path;
604 gotconfig_path = got_repo_get_path_gotconfig(repo);
605 if (gotconfig_path == NULL)
606 return got_error_from_errno("got_repo_get_path_gotconfig");
608 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
609 free(gotconfig_path);
610 return err;
613 /* Supported repository format extensions. */
614 static const char *repo_extensions[] = {
615 "noop", /* Got supports repository format version 1. */
616 "preciousObjects", /* Supported by gotadmin cleanup. */
617 "worktreeConfig", /* Got does not care about Git work trees. */
618 };
620 const struct got_error *
621 got_repo_open(struct got_repository **repop, const char *path,
622 const char *global_gitconfig_path)
624 struct got_repository *repo = NULL;
625 const struct got_error *err = NULL;
626 char *repo_path = NULL;
627 size_t i;
628 struct rlimit rl;
630 *repop = NULL;
632 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
633 return got_error_from_errno("getrlimit");
635 repo = calloc(1, sizeof(*repo));
636 if (repo == NULL) {
637 err = got_error_from_errno("calloc");
638 goto done;
641 RB_INIT(&repo->packidx_bloom_filters);
643 for (i = 0; i < nitems(repo->privsep_children); i++) {
644 memset(&repo->privsep_children[i], 0,
645 sizeof(repo->privsep_children[0]));
646 repo->privsep_children[i].imsg_fd = -1;
649 err = got_object_cache_init(&repo->objcache,
650 GOT_OBJECT_CACHE_TYPE_OBJ);
651 if (err)
652 goto done;
653 err = got_object_cache_init(&repo->treecache,
654 GOT_OBJECT_CACHE_TYPE_TREE);
655 if (err)
656 goto done;
657 err = got_object_cache_init(&repo->commitcache,
658 GOT_OBJECT_CACHE_TYPE_COMMIT);
659 if (err)
660 goto done;
661 err = got_object_cache_init(&repo->tagcache,
662 GOT_OBJECT_CACHE_TYPE_TAG);
663 if (err)
664 goto done;
666 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
667 if (repo->pack_cache_size > rl.rlim_cur / 8)
668 repo->pack_cache_size = rl.rlim_cur / 8;
670 repo_path = realpath(path, NULL);
671 if (repo_path == NULL) {
672 err = got_error_from_errno2("realpath", path);
673 goto done;
676 for (;;) {
677 char *parent_path;
679 err = open_repo(repo, repo_path);
680 if (err == NULL)
681 break;
682 if (err->code != GOT_ERR_NOT_GIT_REPO)
683 goto done;
684 if (repo_path[0] == '/' && repo_path[1] == '\0') {
685 err = got_error(GOT_ERR_NOT_GIT_REPO);
686 goto done;
688 err = got_path_dirname(&parent_path, repo_path);
689 if (err)
690 goto done;
691 free(repo_path);
692 repo_path = parent_path;
695 err = read_gotconfig(repo);
696 if (err)
697 goto done;
699 err = read_gitconfig(repo, global_gitconfig_path);
700 if (err)
701 goto done;
702 if (repo->gitconfig_repository_format_version != 0)
703 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
704 for (i = 0; i < repo->nextensions; i++) {
705 char *ext = repo->extensions[i];
706 int j, supported = 0;
707 for (j = 0; j < nitems(repo_extensions); j++) {
708 if (strcmp(ext, repo_extensions[j]) == 0) {
709 supported = 1;
710 break;
713 if (!supported) {
714 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
715 goto done;
718 done:
719 if (err)
720 got_repo_close(repo);
721 else
722 *repop = repo;
723 free(repo_path);
724 return err;
727 const struct got_error *
728 got_repo_close(struct got_repository *repo)
730 const struct got_error *err = NULL, *child_err;
731 struct got_packidx_bloom_filter *bf;
732 size_t i;
734 for (i = 0; i < repo->pack_cache_size; i++) {
735 if (repo->packidx_cache[i] == NULL)
736 break;
737 got_packidx_close(repo->packidx_cache[i]);
740 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
741 &repo->packidx_bloom_filters))) {
742 RB_REMOVE(got_packidx_bloom_filter_tree,
743 &repo->packidx_bloom_filters, bf);
744 free(bf->bloom);
745 free(bf);
748 for (i = 0; i < repo->pack_cache_size; i++) {
749 if (repo->packs[i].path_packfile == NULL)
750 break;
751 got_pack_close(&repo->packs[i]);
754 free(repo->path);
755 free(repo->path_git_dir);
757 got_object_cache_close(&repo->objcache);
758 got_object_cache_close(&repo->treecache);
759 got_object_cache_close(&repo->commitcache);
760 got_object_cache_close(&repo->tagcache);
762 for (i = 0; i < nitems(repo->privsep_children); i++) {
763 if (repo->privsep_children[i].imsg_fd == -1)
764 continue;
765 imsg_clear(repo->privsep_children[i].ibuf);
766 free(repo->privsep_children[i].ibuf);
767 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
768 child_err = got_privsep_wait_for_child(
769 repo->privsep_children[i].pid);
770 if (child_err && err == NULL)
771 err = child_err;
772 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
773 err == NULL)
774 err = got_error_from_errno("close");
777 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
778 err == NULL)
779 err = got_error_from_errno("close");
781 if (repo->gotconfig)
782 got_gotconfig_free(repo->gotconfig);
783 free(repo->gitconfig_author_name);
784 free(repo->gitconfig_author_email);
785 for (i = 0; i < repo->ngitconfig_remotes; i++)
786 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
787 free(repo->gitconfig_remotes);
788 for (i = 0; i < repo->nextensions; i++)
789 free(repo->extensions[i]);
790 free(repo->extensions);
791 free(repo);
793 return err;
796 void
797 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
799 int i;
801 free(repo->name);
802 repo->name = NULL;
803 free(repo->fetch_url);
804 repo->fetch_url = NULL;
805 free(repo->send_url);
806 repo->send_url = NULL;
807 for (i = 0; i < repo->nfetch_branches; i++)
808 free(repo->fetch_branches[i]);
809 free(repo->fetch_branches);
810 repo->fetch_branches = NULL;
811 repo->nfetch_branches = 0;
812 for (i = 0; i < repo->nsend_branches; i++)
813 free(repo->send_branches[i]);
814 free(repo->send_branches);
815 repo->send_branches = NULL;
816 repo->nsend_branches = 0;
819 const struct got_error *
820 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
821 const char *input_path)
823 const struct got_error *err = NULL;
824 const char *repo_abspath = NULL;
825 size_t repolen, len;
826 char *canonpath, *path = NULL;
828 *in_repo_path = NULL;
830 canonpath = strdup(input_path);
831 if (canonpath == NULL) {
832 err = got_error_from_errno("strdup");
833 goto done;
835 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
836 if (err)
837 goto done;
839 repo_abspath = got_repo_get_path(repo);
841 if (canonpath[0] == '\0') {
842 path = strdup(canonpath);
843 if (path == NULL) {
844 err = got_error_from_errno("strdup");
845 goto done;
847 } else {
848 path = realpath(canonpath, NULL);
849 if (path == NULL) {
850 if (errno != ENOENT) {
851 err = got_error_from_errno2("realpath",
852 canonpath);
853 goto done;
855 /*
856 * Path is not on disk.
857 * Assume it is already relative to repository root.
858 */
859 path = strdup(canonpath);
860 if (path == NULL) {
861 err = got_error_from_errno("strdup");
862 goto done;
866 repolen = strlen(repo_abspath);
867 len = strlen(path);
870 if (strcmp(path, repo_abspath) == 0) {
871 free(path);
872 path = strdup("");
873 if (path == NULL) {
874 err = got_error_from_errno("strdup");
875 goto done;
877 } else if (len > repolen &&
878 got_path_is_child(path, repo_abspath, repolen)) {
879 /* Matched an on-disk path inside repository. */
880 if (got_repo_is_bare(repo)) {
881 /*
882 * Matched an on-disk path inside repository
883 * database. Treat input as repository-relative.
884 */
885 free(path);
886 path = canonpath;
887 canonpath = NULL;
888 } else {
889 char *child;
890 /* Strip common prefix with repository path. */
891 err = got_path_skip_common_ancestor(&child,
892 repo_abspath, path);
893 if (err)
894 goto done;
895 free(path);
896 path = child;
898 } else {
899 /*
900 * Matched unrelated on-disk path.
901 * Treat input as repository-relative.
902 */
903 free(path);
904 path = canonpath;
905 canonpath = NULL;
909 /* Make in-repository path absolute */
910 if (path[0] != '/') {
911 char *abspath;
912 if (asprintf(&abspath, "/%s", path) == -1) {
913 err = got_error_from_errno("asprintf");
914 goto done;
916 free(path);
917 path = abspath;
920 done:
921 free(canonpath);
922 if (err)
923 free(path);
924 else
925 *in_repo_path = path;
926 return err;
929 static const struct got_error *
930 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
931 const char *path_packidx)
933 const struct got_error *err = NULL;
934 size_t i;
936 for (i = 0; i < repo->pack_cache_size; i++) {
937 if (repo->packidx_cache[i] == NULL)
938 break;
939 if (strcmp(repo->packidx_cache[i]->path_packidx,
940 path_packidx) == 0) {
941 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
944 if (i == repo->pack_cache_size) {
945 i = repo->pack_cache_size - 1;
946 err = got_packidx_close(repo->packidx_cache[i]);
947 if (err)
948 return err;
951 repo->packidx_cache[i] = packidx;
953 return NULL;
956 int
957 got_repo_is_packidx_filename(const char *name, size_t len)
959 if (len != GOT_PACKIDX_NAMELEN)
960 return 0;
962 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
963 return 0;
965 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
966 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
967 return 0;
969 return 1;
972 static struct got_packidx_bloom_filter *
973 get_packidx_bloom_filter(struct got_repository *repo,
974 const char *path, size_t path_len)
976 struct got_packidx_bloom_filter key;
978 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
979 return NULL; /* XXX */
980 key.path_len = path_len;
982 return RB_FIND(got_packidx_bloom_filter_tree,
983 &repo->packidx_bloom_filters, &key);
986 static int
987 check_packidx_bloom_filter(struct got_repository *repo,
988 const char *path_packidx, struct got_object_id *id)
990 struct got_packidx_bloom_filter *bf;
992 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
993 if (bf)
994 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
996 /* No bloom filter means this pack index must be searched. */
997 return 1;
1000 static const struct got_error *
1001 add_packidx_bloom_filter(struct got_repository *repo,
1002 struct got_packidx *packidx, const char *path_packidx)
1004 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1005 struct got_packidx_bloom_filter *bf;
1006 size_t len;
1009 * Don't use bloom filters for very large pack index files.
1010 * Large pack files will contain a relatively large fraction
1011 * of our objects so we will likely need to visit them anyway.
1012 * The more objects a pack file contains the higher the probability
1013 * of a false-positive match from the bloom filter. And reading
1014 * all object IDs from a large pack index file can be expensive.
1016 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1017 return NULL;
1019 /* Do we already have a filter for this pack index? */
1020 if (get_packidx_bloom_filter(repo, path_packidx,
1021 strlen(path_packidx)) != NULL)
1022 return NULL;
1024 bf = calloc(1, sizeof(*bf));
1025 if (bf == NULL)
1026 return got_error_from_errno("calloc");
1027 bf->bloom = calloc(1, sizeof(*bf->bloom));
1028 if (bf->bloom == NULL) {
1029 free(bf);
1030 return got_error_from_errno("calloc");
1033 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1034 if (len >= sizeof(bf->path)) {
1035 free(bf->bloom);
1036 free(bf);
1037 return got_error(GOT_ERR_NO_SPACE);
1039 bf->path_len = len;
1041 /* Minimum size supported by our bloom filter is 1000 entries. */
1042 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1043 for (i = 0; i < nobjects; i++) {
1044 struct got_packidx_object_id *id;
1045 id = &packidx->hdr.sorted_ids[i];
1046 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1049 RB_INSERT(got_packidx_bloom_filter_tree,
1050 &repo->packidx_bloom_filters, bf);
1051 return NULL;
1054 const struct got_error *
1055 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1056 struct got_repository *repo, struct got_object_id *id)
1058 const struct got_error *err;
1059 DIR *packdir = NULL;
1060 struct dirent *dent;
1061 char *path_packidx;
1062 size_t i;
1063 int packdir_fd;
1065 /* Search pack index cache. */
1066 for (i = 0; i < repo->pack_cache_size; i++) {
1067 if (repo->packidx_cache[i] == NULL)
1068 break;
1069 if (!check_packidx_bloom_filter(repo,
1070 repo->packidx_cache[i]->path_packidx, id))
1071 continue; /* object will not be found in this index */
1072 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1073 if (*idx != -1) {
1074 *packidx = repo->packidx_cache[i];
1076 * Move this cache entry to the front. Repeatedly
1077 * searching a wrong pack index can be expensive.
1079 if (i > 0) {
1080 memmove(&repo->packidx_cache[1],
1081 &repo->packidx_cache[0],
1082 i * sizeof(repo->packidx_cache[0]));
1083 repo->packidx_cache[0] = *packidx;
1085 return NULL;
1088 /* No luck. Search the filesystem. */
1090 packdir_fd = openat(got_repo_get_fd(repo),
1091 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1092 if (packdir_fd == -1) {
1093 if (errno == ENOENT)
1094 err = got_error_no_obj(id);
1095 else
1096 err = got_error_from_errno_fmt("openat: %s/%s",
1097 got_repo_get_path_git_dir(repo),
1098 GOT_OBJECTS_PACK_DIR);
1099 goto done;
1102 packdir = fdopendir(packdir_fd);
1103 if (packdir == NULL) {
1104 err = got_error_from_errno("fdopendir");
1105 goto done;
1108 while ((dent = readdir(packdir)) != NULL) {
1109 int is_cached = 0;
1111 if (!got_repo_is_packidx_filename(dent->d_name,
1112 strlen(dent->d_name)))
1113 continue;
1115 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1116 dent->d_name) == -1) {
1117 err = got_error_from_errno("asprintf");
1118 goto done;
1121 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1122 free(path_packidx);
1123 continue; /* object will not be found in this index */
1126 for (i = 0; i < repo->pack_cache_size; i++) {
1127 if (repo->packidx_cache[i] == NULL)
1128 break;
1129 if (strcmp(repo->packidx_cache[i]->path_packidx,
1130 path_packidx) == 0) {
1131 is_cached = 1;
1132 break;
1135 if (is_cached) {
1136 free(path_packidx);
1137 continue; /* already searched */
1140 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1141 path_packidx, 0);
1142 if (err) {
1143 free(path_packidx);
1144 goto done;
1147 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1148 if (err) {
1149 free(path_packidx);
1150 goto done;
1153 err = cache_packidx(repo, *packidx, path_packidx);
1154 free(path_packidx);
1155 if (err)
1156 goto done;
1158 *idx = got_packidx_get_object_idx(*packidx, id);
1159 if (*idx != -1) {
1160 err = NULL; /* found the object */
1161 goto done;
1165 err = got_error_no_obj(id);
1166 done:
1167 if (packdir && closedir(packdir) != 0 && err == NULL)
1168 err = got_error_from_errno("closedir");
1169 return err;
1172 static const struct got_error *
1173 read_packfile_hdr(int fd, struct got_packidx *packidx)
1175 const struct got_error *err = NULL;
1176 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1177 struct got_packfile_hdr hdr;
1178 ssize_t n;
1180 n = read(fd, &hdr, sizeof(hdr));
1181 if (n < 0)
1182 return got_error_from_errno("read");
1183 if (n != sizeof(hdr))
1184 return got_error(GOT_ERR_BAD_PACKFILE);
1186 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1187 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1188 be32toh(hdr.nobjects) != totobj)
1189 err = got_error(GOT_ERR_BAD_PACKFILE);
1191 return err;
1194 static const struct got_error *
1195 open_packfile(int *fd, struct got_repository *repo,
1196 const char *relpath, struct got_packidx *packidx)
1198 const struct got_error *err = NULL;
1200 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1201 if (*fd == -1)
1202 return got_error_from_errno_fmt("openat: %s/%s",
1203 got_repo_get_path_git_dir(repo), relpath);
1205 if (packidx) {
1206 err = read_packfile_hdr(*fd, packidx);
1207 if (err) {
1208 close(*fd);
1209 *fd = -1;
1213 return err;
1216 const struct got_error *
1217 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1218 const char *path_packfile, struct got_packidx *packidx)
1220 const struct got_error *err = NULL;
1221 struct got_pack *pack = NULL;
1222 struct stat sb;
1223 size_t i;
1225 if (packp)
1226 *packp = NULL;
1228 for (i = 0; i < repo->pack_cache_size; i++) {
1229 pack = &repo->packs[i];
1230 if (pack->path_packfile == NULL)
1231 break;
1232 if (strcmp(pack->path_packfile, path_packfile) == 0)
1233 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1236 if (i == repo->pack_cache_size) {
1237 err = got_pack_close(&repo->packs[i - 1]);
1238 if (err)
1239 return err;
1240 memmove(&repo->packs[1], &repo->packs[0],
1241 sizeof(repo->packs) - sizeof(repo->packs[0]));
1242 i = 0;
1245 pack = &repo->packs[i];
1247 pack->path_packfile = strdup(path_packfile);
1248 if (pack->path_packfile == NULL) {
1249 err = got_error_from_errno("strdup");
1250 goto done;
1253 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1254 if (err)
1255 goto done;
1257 if (fstat(pack->fd, &sb) != 0) {
1258 err = got_error_from_errno("fstat");
1259 goto done;
1261 pack->filesize = sb.st_size;
1263 pack->privsep_child = NULL;
1265 #ifndef GOT_PACK_NO_MMAP
1266 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1267 pack->fd, 0);
1268 if (pack->map == MAP_FAILED) {
1269 if (errno != ENOMEM) {
1270 err = got_error_from_errno("mmap");
1271 goto done;
1273 pack->map = NULL; /* fall back to read(2) */
1275 #endif
1276 done:
1277 if (err) {
1278 if (pack) {
1279 free(pack->path_packfile);
1280 memset(pack, 0, sizeof(*pack));
1282 } else if (packp)
1283 *packp = pack;
1284 return err;
1287 struct got_pack *
1288 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1290 struct got_pack *pack = NULL;
1291 size_t i;
1293 for (i = 0; i < repo->pack_cache_size; i++) {
1294 pack = &repo->packs[i];
1295 if (pack->path_packfile == NULL)
1296 break;
1297 if (strcmp(pack->path_packfile, path_packfile) == 0)
1298 return pack;
1301 return NULL;
1304 const struct got_error *
1305 got_repo_init(const char *repo_path)
1307 const struct got_error *err = NULL;
1308 const char *dirnames[] = {
1309 GOT_OBJECTS_DIR,
1310 GOT_OBJECTS_PACK_DIR,
1311 GOT_REFS_DIR,
1313 const char *description_str = "Unnamed repository; "
1314 "edit this file 'description' to name the repository.";
1315 const char *headref_str = "ref: refs/heads/main";
1316 const char *gitconfig_str = "[core]\n"
1317 "\trepositoryformatversion = 0\n"
1318 "\tfilemode = true\n"
1319 "\tbare = true\n";
1320 char *path;
1321 size_t i;
1323 if (!got_path_dir_is_empty(repo_path))
1324 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1326 for (i = 0; i < nitems(dirnames); i++) {
1327 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1328 return got_error_from_errno("asprintf");
1330 err = got_path_mkdir(path);
1331 free(path);
1332 if (err)
1333 return err;
1336 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1337 return got_error_from_errno("asprintf");
1338 err = got_path_create_file(path, description_str);
1339 free(path);
1340 if (err)
1341 return err;
1343 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1344 return got_error_from_errno("asprintf");
1345 err = got_path_create_file(path, headref_str);
1346 free(path);
1347 if (err)
1348 return err;
1350 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1351 return got_error_from_errno("asprintf");
1352 err = got_path_create_file(path, gitconfig_str);
1353 free(path);
1354 if (err)
1355 return err;
1357 return NULL;
1360 static const struct got_error *
1361 match_packed_object(struct got_object_id **unique_id,
1362 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1364 const struct got_error *err = NULL;
1365 DIR *packdir = NULL;
1366 struct dirent *dent;
1367 char *path_packidx;
1368 struct got_object_id_queue matched_ids;
1369 int packdir_fd;
1371 STAILQ_INIT(&matched_ids);
1373 packdir_fd = openat(got_repo_get_fd(repo),
1374 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1375 if (packdir_fd == -1) {
1376 if (errno != ENOENT)
1377 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1378 goto done;
1381 packdir = fdopendir(packdir_fd);
1382 if (packdir == NULL) {
1383 err = got_error_from_errno("fdopendir");
1384 goto done;
1387 while ((dent = readdir(packdir)) != NULL) {
1388 struct got_packidx *packidx;
1389 struct got_object_qid *qid;
1391 if (!got_repo_is_packidx_filename(dent->d_name,
1392 strlen(dent->d_name)))
1393 continue;
1395 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1396 dent->d_name) == -1) {
1397 err = got_error_from_errno("strdup");
1398 break;
1401 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1402 path_packidx, 0);
1403 free(path_packidx);
1404 if (err)
1405 break;
1407 err = got_packidx_match_id_str_prefix(&matched_ids,
1408 packidx, id_str_prefix);
1409 if (err) {
1410 got_packidx_close(packidx);
1411 break;
1413 err = got_packidx_close(packidx);
1414 if (err)
1415 break;
1417 STAILQ_FOREACH(qid, &matched_ids, entry) {
1418 if (obj_type != GOT_OBJ_TYPE_ANY) {
1419 int matched_type;
1420 err = got_object_get_type(&matched_type, repo,
1421 qid->id);
1422 if (err)
1423 goto done;
1424 if (matched_type != obj_type)
1425 continue;
1427 if (*unique_id == NULL) {
1428 *unique_id = got_object_id_dup(qid->id);
1429 if (*unique_id == NULL) {
1430 err = got_error_from_errno("malloc");
1431 goto done;
1433 } else {
1434 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1435 continue; /* packed multiple times */
1436 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1437 goto done;
1441 done:
1442 got_object_id_queue_free(&matched_ids);
1443 if (packdir && closedir(packdir) != 0 && err == NULL)
1444 err = got_error_from_errno("closedir");
1445 if (err) {
1446 free(*unique_id);
1447 *unique_id = NULL;
1449 return err;
1452 static const struct got_error *
1453 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1454 const char *object_dir, const char *id_str_prefix, int obj_type,
1455 struct got_repository *repo)
1457 const struct got_error *err = NULL;
1458 char *path;
1459 DIR *dir = NULL;
1460 struct dirent *dent;
1461 struct got_object_id id;
1463 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1464 err = got_error_from_errno("asprintf");
1465 goto done;
1468 dir = opendir(path);
1469 if (dir == NULL) {
1470 if (errno == ENOENT) {
1471 err = NULL;
1472 goto done;
1474 err = got_error_from_errno2("opendir", path);
1475 goto done;
1477 while ((dent = readdir(dir)) != NULL) {
1478 char *id_str;
1479 int cmp;
1481 if (strcmp(dent->d_name, ".") == 0 ||
1482 strcmp(dent->d_name, "..") == 0)
1483 continue;
1485 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1486 err = got_error_from_errno("asprintf");
1487 goto done;
1490 if (!got_parse_sha1_digest(id.sha1, id_str))
1491 continue;
1494 * Directory entries do not necessarily appear in
1495 * sorted order, so we must iterate over all of them.
1497 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1498 if (cmp != 0) {
1499 free(id_str);
1500 continue;
1503 if (*unique_id == NULL) {
1504 if (obj_type != GOT_OBJ_TYPE_ANY) {
1505 int matched_type;
1506 err = got_object_get_type(&matched_type, repo,
1507 &id);
1508 if (err)
1509 goto done;
1510 if (matched_type != obj_type)
1511 continue;
1513 *unique_id = got_object_id_dup(&id);
1514 if (*unique_id == NULL) {
1515 err = got_error_from_errno("got_object_id_dup");
1516 free(id_str);
1517 goto done;
1519 } else {
1520 if (got_object_id_cmp(*unique_id, &id) == 0)
1521 continue; /* both packed and loose */
1522 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1523 free(id_str);
1524 goto done;
1527 done:
1528 if (dir && closedir(dir) != 0 && err == NULL)
1529 err = got_error_from_errno("closedir");
1530 if (err) {
1531 free(*unique_id);
1532 *unique_id = NULL;
1534 free(path);
1535 return err;
1538 const struct got_error *
1539 got_repo_match_object_id_prefix(struct got_object_id **id,
1540 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1542 const struct got_error *err = NULL;
1543 char *path_objects = got_repo_get_path_objects(repo);
1544 char *object_dir = NULL;
1545 size_t len;
1546 int i;
1548 *id = NULL;
1550 for (i = 0; i < strlen(id_str_prefix); i++) {
1551 if (isxdigit((unsigned char)id_str_prefix[i]))
1552 continue;
1553 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1556 len = strlen(id_str_prefix);
1557 if (len >= 2) {
1558 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1559 if (err)
1560 goto done;
1561 object_dir = strndup(id_str_prefix, 2);
1562 if (object_dir == NULL) {
1563 err = got_error_from_errno("strdup");
1564 goto done;
1566 err = match_loose_object(id, path_objects, object_dir,
1567 id_str_prefix, obj_type, repo);
1568 } else if (len == 1) {
1569 int i;
1570 for (i = 0; i < 0xf; i++) {
1571 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1572 == -1) {
1573 err = got_error_from_errno("asprintf");
1574 goto done;
1576 err = match_packed_object(id, repo, object_dir,
1577 obj_type);
1578 if (err)
1579 goto done;
1580 err = match_loose_object(id, path_objects, object_dir,
1581 id_str_prefix, obj_type, repo);
1582 if (err)
1583 goto done;
1585 } else {
1586 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1587 goto done;
1589 done:
1590 free(object_dir);
1591 if (err) {
1592 free(*id);
1593 *id = NULL;
1594 } else if (*id == NULL) {
1595 switch (obj_type) {
1596 case GOT_OBJ_TYPE_BLOB:
1597 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1598 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1599 break;
1600 case GOT_OBJ_TYPE_TREE:
1601 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1602 GOT_OBJ_LABEL_TREE, id_str_prefix);
1603 break;
1604 case GOT_OBJ_TYPE_COMMIT:
1605 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1606 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1607 break;
1608 case GOT_OBJ_TYPE_TAG:
1609 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1610 GOT_OBJ_LABEL_TAG, id_str_prefix);
1611 break;
1612 default:
1613 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1614 break;
1618 return err;
1621 const struct got_error *
1622 got_repo_match_object_id(struct got_object_id **id, char **label,
1623 const char *id_str, int obj_type, struct got_reflist_head *refs,
1624 struct got_repository *repo)
1626 const struct got_error *err;
1627 struct got_tag_object *tag;
1628 struct got_reference *ref = NULL;
1630 *id = NULL;
1631 if (label)
1632 *label = NULL;
1634 if (refs) {
1635 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1636 refs, repo);
1637 if (err == NULL) {
1638 *id = got_object_id_dup(
1639 got_object_tag_get_object_id(tag));
1640 if (*id == NULL)
1641 err = got_error_from_errno("got_object_id_dup");
1642 else if (label && asprintf(label, "refs/tags/%s",
1643 got_object_tag_get_name(tag)) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 free(*id);
1646 *id = NULL;
1648 got_object_tag_close(tag);
1649 return err;
1650 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1651 err->code != GOT_ERR_NO_OBJ)
1652 return err;
1655 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1656 if (err) {
1657 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1658 return err;
1659 err = got_ref_open(&ref, repo, id_str, 0);
1660 if (err != NULL)
1661 goto done;
1662 if (label) {
1663 *label = strdup(got_ref_get_name(ref));
1664 if (*label == NULL) {
1665 err = got_error_from_errno("strdup");
1666 goto done;
1669 err = got_ref_resolve(id, repo, ref);
1670 } else if (label) {
1671 err = got_object_id_str(label, *id);
1672 if (*label == NULL) {
1673 err = got_error_from_errno("strdup");
1674 goto done;
1677 done:
1678 if (ref)
1679 got_ref_close(ref);
1680 return err;
1683 const struct got_error *
1684 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1685 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1687 const struct got_error *err = NULL;
1688 struct got_reflist_entry *re;
1689 struct got_object_id *tag_id;
1690 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1692 *tag = NULL;
1694 TAILQ_FOREACH(re, refs, entry) {
1695 const char *refname;
1696 refname = got_ref_get_name(re->ref);
1697 if (got_ref_is_symbolic(re->ref))
1698 continue;
1699 if (strncmp(refname, "refs/tags/", 10) != 0)
1700 continue;
1701 if (!name_is_absolute)
1702 refname += strlen("refs/tags/");
1703 if (strcmp(refname, name) != 0)
1704 continue;
1705 err = got_ref_resolve(&tag_id, repo, re->ref);
1706 if (err)
1707 break;
1708 err = got_object_open_as_tag(tag, repo, tag_id);
1709 free(tag_id);
1710 if (err)
1711 break;
1712 if (obj_type == GOT_OBJ_TYPE_ANY ||
1713 got_object_tag_get_object_type(*tag) == obj_type)
1714 break;
1715 got_object_tag_close(*tag);
1716 *tag = NULL;
1719 if (err == NULL && *tag == NULL)
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_TAG, name);
1722 return err;
1725 static const struct got_error *
1726 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1727 const char *name, mode_t mode, struct got_object_id *blob_id)
1729 const struct got_error *err = NULL;
1731 *new_te = NULL;
1733 *new_te = calloc(1, sizeof(**new_te));
1734 if (*new_te == NULL)
1735 return got_error_from_errno("calloc");
1737 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1738 sizeof((*new_te)->name)) {
1739 err = got_error(GOT_ERR_NO_SPACE);
1740 goto done;
1743 if (S_ISLNK(mode)) {
1744 (*new_te)->mode = S_IFLNK;
1745 } else {
1746 (*new_te)->mode = S_IFREG;
1747 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1749 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1750 done:
1751 if (err && *new_te) {
1752 free(*new_te);
1753 *new_te = NULL;
1755 return err;
1758 static const struct got_error *
1759 import_file(struct got_tree_entry **new_te, struct dirent *de,
1760 const char *path, struct got_repository *repo)
1762 const struct got_error *err;
1763 struct got_object_id *blob_id = NULL;
1764 char *filepath;
1765 struct stat sb;
1767 if (asprintf(&filepath, "%s%s%s", path,
1768 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1769 return got_error_from_errno("asprintf");
1771 if (lstat(filepath, &sb) != 0) {
1772 err = got_error_from_errno2("lstat", path);
1773 goto done;
1776 err = got_object_blob_create(&blob_id, filepath, repo);
1777 if (err)
1778 goto done;
1780 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1781 blob_id);
1782 done:
1783 free(filepath);
1784 if (err)
1785 free(blob_id);
1786 return err;
1789 static const struct got_error *
1790 insert_tree_entry(struct got_tree_entry *new_te,
1791 struct got_pathlist_head *paths)
1793 const struct got_error *err = NULL;
1794 struct got_pathlist_entry *new_pe;
1796 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1797 if (err)
1798 return err;
1799 if (new_pe == NULL)
1800 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1801 return NULL;
1804 static const struct got_error *write_tree(struct got_object_id **,
1805 const char *, struct got_pathlist_head *, struct got_repository *,
1806 got_repo_import_cb progress_cb, void *progress_arg);
1808 static const struct got_error *
1809 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1810 const char *path, struct got_pathlist_head *ignores,
1811 struct got_repository *repo,
1812 got_repo_import_cb progress_cb, void *progress_arg)
1814 const struct got_error *err;
1815 struct got_object_id *id = NULL;
1816 char *subdirpath;
1818 if (asprintf(&subdirpath, "%s%s%s", path,
1819 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1820 return got_error_from_errno("asprintf");
1822 (*new_te) = calloc(1, sizeof(**new_te));
1823 if (*new_te == NULL)
1824 return got_error_from_errno("calloc");
1825 (*new_te)->mode = S_IFDIR;
1826 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1827 sizeof((*new_te)->name)) {
1828 err = got_error(GOT_ERR_NO_SPACE);
1829 goto done;
1831 err = write_tree(&id, subdirpath, ignores, repo,
1832 progress_cb, progress_arg);
1833 if (err)
1834 goto done;
1835 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1837 done:
1838 free(id);
1839 free(subdirpath);
1840 if (err) {
1841 free(*new_te);
1842 *new_te = NULL;
1844 return err;
1847 static const struct got_error *
1848 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1849 struct got_pathlist_head *ignores, struct got_repository *repo,
1850 got_repo_import_cb progress_cb, void *progress_arg)
1852 const struct got_error *err = NULL;
1853 DIR *dir;
1854 struct dirent *de;
1855 int nentries;
1856 struct got_tree_entry *new_te = NULL;
1857 struct got_pathlist_head paths;
1858 struct got_pathlist_entry *pe;
1860 *new_tree_id = NULL;
1862 TAILQ_INIT(&paths);
1864 dir = opendir(path_dir);
1865 if (dir == NULL) {
1866 err = got_error_from_errno2("opendir", path_dir);
1867 goto done;
1870 nentries = 0;
1871 while ((de = readdir(dir)) != NULL) {
1872 int ignore = 0;
1873 int type;
1875 if (strcmp(de->d_name, ".") == 0 ||
1876 strcmp(de->d_name, "..") == 0)
1877 continue;
1879 TAILQ_FOREACH(pe, ignores, entry) {
1880 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1881 ignore = 1;
1882 break;
1885 if (ignore)
1886 continue;
1888 err = got_path_dirent_type(&type, path_dir, de);
1889 if (err)
1890 goto done;
1892 if (type == DT_DIR) {
1893 err = import_subdir(&new_te, de, path_dir,
1894 ignores, repo, progress_cb, progress_arg);
1895 if (err) {
1896 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1897 goto done;
1898 err = NULL;
1899 continue;
1901 } else if (type == DT_REG || type == DT_LNK) {
1902 err = import_file(&new_te, de, path_dir, repo);
1903 if (err)
1904 goto done;
1905 } else
1906 continue;
1908 err = insert_tree_entry(new_te, &paths);
1909 if (err)
1910 goto done;
1911 nentries++;
1914 if (TAILQ_EMPTY(&paths)) {
1915 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1916 "cannot create tree without any entries");
1917 goto done;
1920 TAILQ_FOREACH(pe, &paths, entry) {
1921 struct got_tree_entry *te = pe->data;
1922 char *path;
1923 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1924 continue;
1925 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1926 err = got_error_from_errno("asprintf");
1927 goto done;
1929 err = (*progress_cb)(progress_arg, path);
1930 free(path);
1931 if (err)
1932 goto done;
1935 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1936 done:
1937 if (dir)
1938 closedir(dir);
1939 got_pathlist_free(&paths);
1940 return err;
1943 const struct got_error *
1944 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1945 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1946 struct got_repository *repo, got_repo_import_cb progress_cb,
1947 void *progress_arg)
1949 const struct got_error *err;
1950 struct got_object_id *new_tree_id;
1952 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1953 progress_cb, progress_arg);
1954 if (err)
1955 return err;
1957 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1958 author, time(NULL), author, time(NULL), logmsg, repo);
1959 free(new_tree_id);
1960 return err;
1963 const struct got_error *
1964 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1965 struct got_repository *repo)
1967 const struct got_error *err = NULL;
1968 char *path_objects = NULL, *path = NULL;
1969 DIR *dir = NULL;
1970 struct got_object_id id;
1971 int i;
1973 *nobjects = 0;
1974 *ondisk_size = 0;
1976 path_objects = got_repo_get_path_objects(repo);
1977 if (path_objects == NULL)
1978 return got_error_from_errno("got_repo_get_path_objects");
1980 for (i = 0; i <= 0xff; i++) {
1981 struct dirent *dent;
1983 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1984 err = got_error_from_errno("asprintf");
1985 break;
1988 dir = opendir(path);
1989 if (dir == NULL) {
1990 if (errno == ENOENT) {
1991 err = NULL;
1992 continue;
1994 err = got_error_from_errno2("opendir", path);
1995 break;
1998 while ((dent = readdir(dir)) != NULL) {
1999 char *id_str;
2000 int fd;
2001 struct stat sb;
2003 if (strcmp(dent->d_name, ".") == 0 ||
2004 strcmp(dent->d_name, "..") == 0)
2005 continue;
2007 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2008 err = got_error_from_errno("asprintf");
2009 goto done;
2012 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2013 free(id_str);
2014 continue;
2016 free(id_str);
2018 err = got_object_open_loose_fd(&fd, &id, repo);
2019 if (err)
2020 goto done;
2022 if (fstat(fd, &sb) == -1) {
2023 err = got_error_from_errno("fstat");
2024 close(fd);
2025 goto done;
2027 (*nobjects)++;
2028 (*ondisk_size) += sb.st_size;
2030 if (close(fd) == -1) {
2031 err = got_error_from_errno("close");
2032 goto done;
2036 if (closedir(dir) != 0) {
2037 err = got_error_from_errno("closedir");
2038 goto done;
2040 dir = NULL;
2042 free(path);
2043 path = NULL;
2045 done:
2046 if (dir && closedir(dir) != 0 && err == NULL)
2047 err = got_error_from_errno("closedir");
2049 if (err) {
2050 *nobjects = 0;
2051 *ondisk_size = 0;
2053 free(path_objects);
2054 free(path);
2055 return err;
2058 const struct got_error *
2059 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2060 off_t *total_packsize, struct got_repository *repo)
2062 const struct got_error *err = NULL;
2063 DIR *packdir = NULL;
2064 struct dirent *dent;
2065 struct got_packidx *packidx = NULL;
2066 char *path_packidx;
2067 char *path_packfile;
2068 int packdir_fd;
2069 struct stat sb;
2071 *npackfiles = 0;
2072 *nobjects = 0;
2073 *total_packsize = 0;
2075 packdir_fd = openat(got_repo_get_fd(repo),
2076 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2077 if (packdir_fd == -1) {
2078 return got_error_from_errno_fmt("openat: %s/%s",
2079 got_repo_get_path_git_dir(repo),
2080 GOT_OBJECTS_PACK_DIR);
2083 packdir = fdopendir(packdir_fd);
2084 if (packdir == NULL) {
2085 err = got_error_from_errno("fdopendir");
2086 goto done;
2089 while ((dent = readdir(packdir)) != NULL) {
2090 if (!got_repo_is_packidx_filename(dent->d_name,
2091 strlen(dent->d_name)))
2092 continue;
2094 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2095 dent->d_name) == -1) {
2096 err = got_error_from_errno("asprintf");
2097 goto done;
2100 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2101 path_packidx, 0);
2102 free(path_packidx);
2103 if (err)
2104 goto done;
2106 if (fstat(packidx->fd, &sb) == -1)
2107 goto done;
2108 *total_packsize += sb.st_size;
2110 err = got_packidx_get_packfile_path(&path_packfile,
2111 packidx->path_packidx);
2112 if (err)
2113 goto done;
2115 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2116 0) == -1) {
2117 free(path_packfile);
2118 goto done;
2120 free(path_packfile);
2121 *total_packsize += sb.st_size;
2123 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2125 (*npackfiles)++;
2127 got_packidx_close(packidx);
2128 packidx = NULL;
2130 done:
2131 if (packidx)
2132 got_packidx_close(packidx);
2133 if (packdir && closedir(packdir) != 0 && err == NULL)
2134 err = got_error_from_errno("closedir");
2135 if (err) {
2136 *npackfiles = 0;
2137 *nobjects = 0;
2138 *total_packsize = 0;
2140 return err;
2143 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2144 got_packidx_bloom_filter_cmp);