Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_gotconfig.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 #endif
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
73 const char *
74 got_repo_get_path(struct got_repository *repo)
75 {
76 return repo->path;
77 }
79 const char *
80 got_repo_get_path_git_dir(struct got_repository *repo)
81 {
82 return repo->path_git_dir;
83 }
85 int
86 got_repo_get_fd(struct got_repository *repo)
87 {
88 return repo->gitdir_fd;
89 }
91 const char *
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_name;
95 }
97 const char *
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 {
100 return repo->gitconfig_author_email;
103 const char *
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
109 const char *
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
115 const char *
116 got_repo_get_gitconfig_owner(struct got_repository *repo)
118 return repo->gitconfig_owner;
121 void
122 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
123 struct got_repository *repo)
125 *extensions = repo->extensions;
126 *nextensions = repo->nextensions;
129 int
130 got_repo_is_bare(struct got_repository *repo)
132 return (strcmp(repo->path, repo->path_git_dir) == 0);
135 static char *
136 get_path_git_child(struct got_repository *repo, const char *basename)
138 char *path_child;
140 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
141 basename) == -1)
142 return NULL;
144 return path_child;
147 char *
148 got_repo_get_path_objects(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_DIR);
153 char *
154 got_repo_get_path_objects_pack(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
159 char *
160 got_repo_get_path_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_REFS_DIR);
165 char *
166 got_repo_get_path_packed_refs(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
171 static char *
172 get_path_head(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_HEAD_FILE);
177 char *
178 got_repo_get_path_gitconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GITCONFIG);
183 char *
184 got_repo_get_path_gotconfig(struct got_repository *repo)
186 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
189 const struct got_gotconfig *
190 got_repo_get_gotconfig(struct got_repository *repo)
192 return repo->gotconfig;
195 void
196 got_repo_get_gitconfig_remotes(int *nremotes,
197 const struct got_remote_repo **remotes, struct got_repository *repo)
199 *nremotes = repo->ngitconfig_remotes;
200 *remotes = repo->gitconfig_remotes;
203 static int
204 is_git_repo(struct got_repository *repo)
206 const char *path_git = got_repo_get_path_git_dir(repo);
207 char *path_objects = got_repo_get_path_objects(repo);
208 char *path_refs = got_repo_get_path_refs(repo);
209 char *path_head = get_path_head(repo);
210 int ret = 0;
211 struct stat sb;
212 struct got_reference *head_ref;
214 if (lstat(path_git, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_objects, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_refs, &sb) == -1)
225 goto done;
226 if (!S_ISDIR(sb.st_mode))
227 goto done;
229 if (lstat(path_head, &sb) == -1)
230 goto done;
231 if (!S_ISREG(sb.st_mode))
232 goto done;
234 /* Check if the HEAD reference can be opened. */
235 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
236 goto done;
237 got_ref_close(head_ref);
239 ret = 1;
240 done:
241 free(path_objects);
242 free(path_refs);
243 free(path_head);
244 return ret;
248 const struct got_error *
249 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
250 struct got_object *obj)
252 #ifndef GOT_NO_OBJ_CACHE
253 const struct got_error *err = NULL;
254 err = got_object_cache_add(&repo->objcache, id, obj);
255 if (err) {
256 if (err->code == GOT_ERR_OBJ_EXISTS ||
257 err->code == GOT_ERR_OBJ_TOO_LARGE)
258 err = NULL;
259 return err;
261 obj->refcnt++;
262 #endif
263 return NULL;
266 struct got_object *
267 got_repo_get_cached_object(struct got_repository *repo,
268 struct got_object_id *id)
270 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
273 const struct got_error *
274 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
275 struct got_tree_object *tree)
277 #ifndef GOT_NO_OBJ_CACHE
278 const struct got_error *err = NULL;
279 err = got_object_cache_add(&repo->treecache, id, tree);
280 if (err) {
281 if (err->code == GOT_ERR_OBJ_EXISTS ||
282 err->code == GOT_ERR_OBJ_TOO_LARGE)
283 err = NULL;
284 return err;
286 tree->refcnt++;
287 #endif
288 return NULL;
291 struct got_tree_object *
292 got_repo_get_cached_tree(struct got_repository *repo,
293 struct got_object_id *id)
295 return (struct got_tree_object *)got_object_cache_get(
296 &repo->treecache, id);
299 const struct got_error *
300 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
301 struct got_commit_object *commit)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->commitcache, id, commit);
306 if (err) {
307 if (err->code == GOT_ERR_OBJ_EXISTS ||
308 err->code == GOT_ERR_OBJ_TOO_LARGE)
309 err = NULL;
310 return err;
312 commit->refcnt++;
313 #endif
314 return NULL;
317 struct got_commit_object *
318 got_repo_get_cached_commit(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_commit_object *)got_object_cache_get(
322 &repo->commitcache, id);
325 const struct got_error *
326 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
327 struct got_tag_object *tag)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->tagcache, id, tag);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tag->refcnt++;
339 #endif
340 return NULL;
343 struct got_tag_object *
344 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
346 return (struct got_tag_object *)got_object_cache_get(
347 &repo->tagcache, id);
350 static const struct got_error *
351 open_repo(struct got_repository *repo, const char *path)
353 const struct got_error *err = NULL;
355 repo->gitdir_fd = -1;
357 /* bare git repository? */
358 repo->path_git_dir = strdup(path);
359 if (repo->path_git_dir == NULL)
360 return got_error_from_errno("strdup");
361 if (is_git_repo(repo)) {
362 repo->path = strdup(repo->path_git_dir);
363 if (repo->path == NULL) {
364 err = got_error_from_errno("strdup");
365 goto done;
367 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
368 if (repo->gitdir_fd == -1) {
369 err = got_error_from_errno2("open",
370 repo->path_git_dir);
371 goto done;
373 return NULL;
376 /* git repository with working tree? */
377 free(repo->path_git_dir);
378 repo->path_git_dir = NULL;
379 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
380 err = got_error_from_errno("asprintf");
381 goto done;
383 if (is_git_repo(repo)) {
384 repo->path = strdup(path);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
389 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
390 if (repo->gitdir_fd == -1) {
391 err = got_error_from_errno2("open",
392 repo->path_git_dir);
393 goto done;
395 return NULL;
398 err = got_error(GOT_ERR_NOT_GIT_REPO);
399 done:
400 if (err) {
401 free(repo->path);
402 repo->path = NULL;
403 free(repo->path_git_dir);
404 repo->path_git_dir = NULL;
405 if (repo->gitdir_fd != -1)
406 close(repo->gitdir_fd);
407 repo->gitdir_fd = -1;
410 return err;
413 static const struct got_error *
414 parse_gitconfig_file(int *gitconfig_repository_format_version,
415 char **gitconfig_author_name, char **gitconfig_author_email,
416 struct got_remote_repo **remotes, int *nremotes,
417 char **gitconfig_owner, char ***extensions, int *nextensions,
418 const char *gitconfig_path)
420 const struct got_error *err = NULL, *child_err = NULL;
421 int fd = -1;
422 int imsg_fds[2] = { -1, -1 };
423 pid_t pid;
424 struct imsgbuf *ibuf;
426 *gitconfig_repository_format_version = 0;
427 if (extensions)
428 *extensions = NULL;
429 if (nextensions)
430 *nextensions = 0;
431 *gitconfig_author_name = NULL;
432 *gitconfig_author_email = NULL;
433 if (remotes)
434 *remotes = NULL;
435 if (nremotes)
436 *nremotes = 0;
437 if (gitconfig_owner)
438 *gitconfig_owner = NULL;
440 fd = open(gitconfig_path, O_RDONLY);
441 if (fd == -1) {
442 if (errno == ENOENT)
443 return NULL;
444 return got_error_from_errno2("open", gitconfig_path);
447 ibuf = calloc(1, sizeof(*ibuf));
448 if (ibuf == NULL) {
449 err = got_error_from_errno("calloc");
450 goto done;
453 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
454 err = got_error_from_errno("socketpair");
455 goto done;
458 pid = fork();
459 if (pid == -1) {
460 err = got_error_from_errno("fork");
461 goto done;
462 } else if (pid == 0) {
463 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
464 gitconfig_path);
465 /* not reached */
468 if (close(imsg_fds[1]) == -1) {
469 err = got_error_from_errno("close");
470 goto done;
472 imsg_fds[1] = -1;
473 imsg_init(ibuf, imsg_fds[0]);
475 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
476 if (err)
477 goto done;
478 fd = -1;
480 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
481 if (err)
482 goto done;
484 err = got_privsep_recv_gitconfig_int(
485 gitconfig_repository_format_version, ibuf);
486 if (err)
487 goto done;
489 if (extensions && nextensions) {
490 err = got_privsep_send_gitconfig_repository_extensions_req(
491 ibuf);
492 if (err)
493 goto done;
494 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
495 if (err)
496 goto done;
497 if (*nextensions > 0) {
498 int i;
499 *extensions = calloc(*nextensions, sizeof(char *));
500 if (*extensions == NULL) {
501 err = got_error_from_errno("calloc");
502 goto done;
504 for (i = 0; i < *nextensions; i++) {
505 char *ext;
506 err = got_privsep_recv_gitconfig_str(&ext,
507 ibuf);
508 if (err)
509 goto done;
510 (*extensions)[i] = ext;
515 err = got_privsep_send_gitconfig_author_name_req(ibuf);
516 if (err)
517 goto done;
519 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
520 if (err)
521 goto done;
523 err = got_privsep_send_gitconfig_author_email_req(ibuf);
524 if (err)
525 goto done;
527 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
528 if (err)
529 goto done;
531 if (remotes && nremotes) {
532 err = got_privsep_send_gitconfig_remotes_req(ibuf);
533 if (err)
534 goto done;
536 err = got_privsep_recv_gitconfig_remotes(remotes,
537 nremotes, ibuf);
538 if (err)
539 goto done;
542 if (gitconfig_owner) {
543 err = got_privsep_send_gitconfig_owner_req(ibuf);
544 if (err)
545 goto done;
546 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
547 if (err)
548 goto done;
551 imsg_clear(ibuf);
552 err = got_privsep_send_stop(imsg_fds[0]);
553 child_err = got_privsep_wait_for_child(pid);
554 if (child_err && err == NULL)
555 err = child_err;
556 done:
557 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
558 err = got_error_from_errno("close");
559 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
560 err = got_error_from_errno("close");
561 if (fd != -1 && close(fd) == -1 && err == NULL)
562 err = got_error_from_errno2("close", gitconfig_path);
563 free(ibuf);
564 return err;
567 static const struct got_error *
568 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
570 const struct got_error *err = NULL;
571 char *repo_gitconfig_path = NULL;
573 if (global_gitconfig_path) {
574 /* Read settings from ~/.gitconfig. */
575 int dummy_repo_version;
576 err = parse_gitconfig_file(&dummy_repo_version,
577 &repo->global_gitconfig_author_name,
578 &repo->global_gitconfig_author_email,
579 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
580 if (err)
581 return err;
584 /* Read repository's .git/config file. */
585 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
586 if (repo_gitconfig_path == NULL)
587 return got_error_from_errno("got_repo_get_path_gitconfig");
589 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
590 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
591 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
592 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
593 repo_gitconfig_path);
594 if (err)
595 goto done;
596 done:
597 free(repo_gitconfig_path);
598 return err;
601 static const struct got_error *
602 read_gotconfig(struct got_repository *repo)
604 const struct got_error *err = NULL;
605 char *gotconfig_path;
607 gotconfig_path = got_repo_get_path_gotconfig(repo);
608 if (gotconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gotconfig");
611 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
612 free(gotconfig_path);
613 return err;
616 /* Supported repository format extensions. */
617 static const char *repo_extensions[] = {
618 "noop", /* Got supports repository format version 1. */
619 "preciousObjects", /* Supported by gotadmin cleanup. */
620 "worktreeConfig", /* Got does not care about Git work trees. */
621 };
623 const struct got_error *
624 got_repo_open(struct got_repository **repop, const char *path,
625 const char *global_gitconfig_path)
627 struct got_repository *repo = NULL;
628 const struct got_error *err = NULL;
629 char *repo_path = NULL;
630 size_t i;
631 struct rlimit rl;
633 *repop = NULL;
635 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
636 return got_error_from_errno("getrlimit");
638 repo = calloc(1, sizeof(*repo));
639 if (repo == NULL) {
640 err = got_error_from_errno("calloc");
641 goto done;
644 RB_INIT(&repo->packidx_bloom_filters);
646 for (i = 0; i < nitems(repo->privsep_children); i++) {
647 memset(&repo->privsep_children[i], 0,
648 sizeof(repo->privsep_children[0]));
649 repo->privsep_children[i].imsg_fd = -1;
652 err = got_object_cache_init(&repo->objcache,
653 GOT_OBJECT_CACHE_TYPE_OBJ);
654 if (err)
655 goto done;
656 err = got_object_cache_init(&repo->treecache,
657 GOT_OBJECT_CACHE_TYPE_TREE);
658 if (err)
659 goto done;
660 err = got_object_cache_init(&repo->commitcache,
661 GOT_OBJECT_CACHE_TYPE_COMMIT);
662 if (err)
663 goto done;
664 err = got_object_cache_init(&repo->tagcache,
665 GOT_OBJECT_CACHE_TYPE_TAG);
666 if (err)
667 goto done;
669 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
670 if (repo->pack_cache_size > rl.rlim_cur / 8)
671 repo->pack_cache_size = rl.rlim_cur / 8;
673 repo_path = realpath(path, NULL);
674 if (repo_path == NULL) {
675 err = got_error_from_errno2("realpath", path);
676 goto done;
679 for (;;) {
680 char *parent_path;
682 err = open_repo(repo, repo_path);
683 if (err == NULL)
684 break;
685 if (err->code != GOT_ERR_NOT_GIT_REPO)
686 goto done;
687 if (repo_path[0] == '/' && repo_path[1] == '\0') {
688 err = got_error(GOT_ERR_NOT_GIT_REPO);
689 goto done;
691 err = got_path_dirname(&parent_path, repo_path);
692 if (err)
693 goto done;
694 free(repo_path);
695 repo_path = parent_path;
698 err = read_gotconfig(repo);
699 if (err)
700 goto done;
702 err = read_gitconfig(repo, global_gitconfig_path);
703 if (err)
704 goto done;
705 if (repo->gitconfig_repository_format_version != 0)
706 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
707 for (i = 0; i < repo->nextensions; i++) {
708 char *ext = repo->extensions[i];
709 int j, supported = 0;
710 for (j = 0; j < nitems(repo_extensions); j++) {
711 if (strcmp(ext, repo_extensions[j]) == 0) {
712 supported = 1;
713 break;
716 if (!supported) {
717 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
718 goto done;
721 done:
722 if (err)
723 got_repo_close(repo);
724 else
725 *repop = repo;
726 free(repo_path);
727 return err;
730 const struct got_error *
731 got_repo_close(struct got_repository *repo)
733 const struct got_error *err = NULL, *child_err;
734 struct got_packidx_bloom_filter *bf;
735 size_t i;
737 for (i = 0; i < repo->pack_cache_size; i++) {
738 if (repo->packidx_cache[i] == NULL)
739 break;
740 got_packidx_close(repo->packidx_cache[i]);
743 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
744 &repo->packidx_bloom_filters))) {
745 RB_REMOVE(got_packidx_bloom_filter_tree,
746 &repo->packidx_bloom_filters, bf);
747 free(bf->bloom);
748 free(bf);
751 for (i = 0; i < repo->pack_cache_size; i++) {
752 if (repo->packs[i].path_packfile == NULL)
753 break;
754 got_pack_close(&repo->packs[i]);
757 free(repo->path);
758 free(repo->path_git_dir);
760 got_object_cache_close(&repo->objcache);
761 got_object_cache_close(&repo->treecache);
762 got_object_cache_close(&repo->commitcache);
763 got_object_cache_close(&repo->tagcache);
765 for (i = 0; i < nitems(repo->privsep_children); i++) {
766 if (repo->privsep_children[i].imsg_fd == -1)
767 continue;
768 imsg_clear(repo->privsep_children[i].ibuf);
769 free(repo->privsep_children[i].ibuf);
770 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
771 child_err = got_privsep_wait_for_child(
772 repo->privsep_children[i].pid);
773 if (child_err && err == NULL)
774 err = child_err;
775 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
776 err == NULL)
777 err = got_error_from_errno("close");
780 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
781 err == NULL)
782 err = got_error_from_errno("close");
784 if (repo->gotconfig)
785 got_gotconfig_free(repo->gotconfig);
786 free(repo->gitconfig_author_name);
787 free(repo->gitconfig_author_email);
788 for (i = 0; i < repo->ngitconfig_remotes; i++)
789 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
790 free(repo->gitconfig_remotes);
791 for (i = 0; i < repo->nextensions; i++)
792 free(repo->extensions[i]);
793 free(repo->extensions);
794 free(repo);
796 return err;
799 void
800 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
802 int i;
804 free(repo->name);
805 repo->name = NULL;
806 free(repo->fetch_url);
807 repo->fetch_url = NULL;
808 free(repo->send_url);
809 repo->send_url = NULL;
810 for (i = 0; i < repo->nfetch_branches; i++)
811 free(repo->fetch_branches[i]);
812 free(repo->fetch_branches);
813 repo->fetch_branches = NULL;
814 repo->nfetch_branches = 0;
815 for (i = 0; i < repo->nsend_branches; i++)
816 free(repo->send_branches[i]);
817 free(repo->send_branches);
818 repo->send_branches = NULL;
819 repo->nsend_branches = 0;
822 const struct got_error *
823 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
824 const char *input_path)
826 const struct got_error *err = NULL;
827 const char *repo_abspath = NULL;
828 size_t repolen, len;
829 char *canonpath, *path = NULL;
831 *in_repo_path = NULL;
833 canonpath = strdup(input_path);
834 if (canonpath == NULL) {
835 err = got_error_from_errno("strdup");
836 goto done;
838 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
839 if (err)
840 goto done;
842 repo_abspath = got_repo_get_path(repo);
844 if (canonpath[0] == '\0') {
845 path = strdup(canonpath);
846 if (path == NULL) {
847 err = got_error_from_errno("strdup");
848 goto done;
850 } else {
851 path = realpath(canonpath, NULL);
852 if (path == NULL) {
853 if (errno != ENOENT) {
854 err = got_error_from_errno2("realpath",
855 canonpath);
856 goto done;
858 /*
859 * Path is not on disk.
860 * Assume it is already relative to repository root.
861 */
862 path = strdup(canonpath);
863 if (path == NULL) {
864 err = got_error_from_errno("strdup");
865 goto done;
869 repolen = strlen(repo_abspath);
870 len = strlen(path);
873 if (strcmp(path, repo_abspath) == 0) {
874 free(path);
875 path = strdup("");
876 if (path == NULL) {
877 err = got_error_from_errno("strdup");
878 goto done;
880 } else if (len > repolen &&
881 got_path_is_child(path, repo_abspath, repolen)) {
882 /* Matched an on-disk path inside repository. */
883 if (got_repo_is_bare(repo)) {
884 /*
885 * Matched an on-disk path inside repository
886 * database. Treat input as repository-relative.
887 */
888 free(path);
889 path = canonpath;
890 canonpath = NULL;
891 } else {
892 char *child;
893 /* Strip common prefix with repository path. */
894 err = got_path_skip_common_ancestor(&child,
895 repo_abspath, path);
896 if (err)
897 goto done;
898 free(path);
899 path = child;
901 } else {
902 /*
903 * Matched unrelated on-disk path.
904 * Treat input as repository-relative.
905 */
906 free(path);
907 path = canonpath;
908 canonpath = NULL;
912 /* Make in-repository path absolute */
913 if (path[0] != '/') {
914 char *abspath;
915 if (asprintf(&abspath, "/%s", path) == -1) {
916 err = got_error_from_errno("asprintf");
917 goto done;
919 free(path);
920 path = abspath;
923 done:
924 free(canonpath);
925 if (err)
926 free(path);
927 else
928 *in_repo_path = path;
929 return err;
932 static const struct got_error *
933 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
934 const char *path_packidx)
936 const struct got_error *err = NULL;
937 size_t i;
939 for (i = 0; i < repo->pack_cache_size; i++) {
940 if (repo->packidx_cache[i] == NULL)
941 break;
942 if (strcmp(repo->packidx_cache[i]->path_packidx,
943 path_packidx) == 0) {
944 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
947 if (i == repo->pack_cache_size) {
948 i = repo->pack_cache_size - 1;
949 err = got_packidx_close(repo->packidx_cache[i]);
950 if (err)
951 return err;
954 repo->packidx_cache[i] = packidx;
956 return NULL;
959 int
960 got_repo_is_packidx_filename(const char *name, size_t len)
962 if (len != GOT_PACKIDX_NAMELEN)
963 return 0;
965 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
966 return 0;
968 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
969 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
970 return 0;
972 return 1;
975 static struct got_packidx_bloom_filter *
976 get_packidx_bloom_filter(struct got_repository *repo,
977 const char *path, size_t path_len)
979 struct got_packidx_bloom_filter key;
981 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
982 return NULL; /* XXX */
983 key.path_len = path_len;
985 return RB_FIND(got_packidx_bloom_filter_tree,
986 &repo->packidx_bloom_filters, &key);
989 static int
990 check_packidx_bloom_filter(struct got_repository *repo,
991 const char *path_packidx, struct got_object_id *id)
993 struct got_packidx_bloom_filter *bf;
995 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
996 if (bf)
997 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
999 /* No bloom filter means this pack index must be searched. */
1000 return 1;
1003 static const struct got_error *
1004 add_packidx_bloom_filter(struct got_repository *repo,
1005 struct got_packidx *packidx, const char *path_packidx)
1007 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1008 struct got_packidx_bloom_filter *bf;
1009 size_t len;
1012 * Don't use bloom filters for very large pack index files.
1013 * Large pack files will contain a relatively large fraction
1014 * of our objects so we will likely need to visit them anyway.
1015 * The more objects a pack file contains the higher the probability
1016 * of a false-positive match from the bloom filter. And reading
1017 * all object IDs from a large pack index file can be expensive.
1019 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1020 return NULL;
1022 /* Do we already have a filter for this pack index? */
1023 if (get_packidx_bloom_filter(repo, path_packidx,
1024 strlen(path_packidx)) != NULL)
1025 return NULL;
1027 bf = calloc(1, sizeof(*bf));
1028 if (bf == NULL)
1029 return got_error_from_errno("calloc");
1030 bf->bloom = calloc(1, sizeof(*bf->bloom));
1031 if (bf->bloom == NULL) {
1032 free(bf);
1033 return got_error_from_errno("calloc");
1036 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1037 if (len >= sizeof(bf->path)) {
1038 free(bf->bloom);
1039 free(bf);
1040 return got_error(GOT_ERR_NO_SPACE);
1042 bf->path_len = len;
1044 /* Minimum size supported by our bloom filter is 1000 entries. */
1045 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1046 for (i = 0; i < nobjects; i++) {
1047 struct got_packidx_object_id *id;
1048 id = &packidx->hdr.sorted_ids[i];
1049 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1052 RB_INSERT(got_packidx_bloom_filter_tree,
1053 &repo->packidx_bloom_filters, bf);
1054 return NULL;
1057 const struct got_error *
1058 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1059 struct got_repository *repo, struct got_object_id *id)
1061 const struct got_error *err;
1062 DIR *packdir = NULL;
1063 struct dirent *dent;
1064 char *path_packidx;
1065 size_t i;
1066 int packdir_fd;
1068 /* Search pack index cache. */
1069 for (i = 0; i < repo->pack_cache_size; i++) {
1070 if (repo->packidx_cache[i] == NULL)
1071 break;
1072 if (!check_packidx_bloom_filter(repo,
1073 repo->packidx_cache[i]->path_packidx, id))
1074 continue; /* object will not be found in this index */
1075 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1076 if (*idx != -1) {
1077 *packidx = repo->packidx_cache[i];
1079 * Move this cache entry to the front. Repeatedly
1080 * searching a wrong pack index can be expensive.
1082 if (i > 0) {
1083 memmove(&repo->packidx_cache[1],
1084 &repo->packidx_cache[0],
1085 i * sizeof(repo->packidx_cache[0]));
1086 repo->packidx_cache[0] = *packidx;
1088 return NULL;
1091 /* No luck. Search the filesystem. */
1093 packdir_fd = openat(got_repo_get_fd(repo),
1094 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1095 if (packdir_fd == -1) {
1096 if (errno == ENOENT)
1097 err = got_error_no_obj(id);
1098 else
1099 err = got_error_from_errno_fmt("openat: %s/%s",
1100 got_repo_get_path_git_dir(repo),
1101 GOT_OBJECTS_PACK_DIR);
1102 goto done;
1105 packdir = fdopendir(packdir_fd);
1106 if (packdir == NULL) {
1107 err = got_error_from_errno("fdopendir");
1108 goto done;
1111 while ((dent = readdir(packdir)) != NULL) {
1112 int is_cached = 0;
1114 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1115 continue;
1117 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1118 dent->d_name) == -1) {
1119 err = got_error_from_errno("asprintf");
1120 goto done;
1123 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1124 free(path_packidx);
1125 continue; /* object will not be found in this index */
1128 for (i = 0; i < repo->pack_cache_size; i++) {
1129 if (repo->packidx_cache[i] == NULL)
1130 break;
1131 if (strcmp(repo->packidx_cache[i]->path_packidx,
1132 path_packidx) == 0) {
1133 is_cached = 1;
1134 break;
1137 if (is_cached) {
1138 free(path_packidx);
1139 continue; /* already searched */
1142 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1143 path_packidx, 0);
1144 if (err) {
1145 free(path_packidx);
1146 goto done;
1149 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1150 if (err) {
1151 free(path_packidx);
1152 goto done;
1155 err = cache_packidx(repo, *packidx, path_packidx);
1156 free(path_packidx);
1157 if (err)
1158 goto done;
1160 *idx = got_packidx_get_object_idx(*packidx, id);
1161 if (*idx != -1) {
1162 err = NULL; /* found the object */
1163 goto done;
1167 err = got_error_no_obj(id);
1168 done:
1169 if (packdir && closedir(packdir) != 0 && err == NULL)
1170 err = got_error_from_errno("closedir");
1171 return err;
1174 static const struct got_error *
1175 read_packfile_hdr(int fd, struct got_packidx *packidx)
1177 const struct got_error *err = NULL;
1178 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1179 struct got_packfile_hdr hdr;
1180 ssize_t n;
1182 n = read(fd, &hdr, sizeof(hdr));
1183 if (n < 0)
1184 return got_error_from_errno("read");
1185 if (n != sizeof(hdr))
1186 return got_error(GOT_ERR_BAD_PACKFILE);
1188 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1189 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1190 be32toh(hdr.nobjects) != totobj)
1191 err = got_error(GOT_ERR_BAD_PACKFILE);
1193 return err;
1196 static const struct got_error *
1197 open_packfile(int *fd, struct got_repository *repo,
1198 const char *relpath, struct got_packidx *packidx)
1200 const struct got_error *err = NULL;
1202 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1203 if (*fd == -1)
1204 return got_error_from_errno_fmt("openat: %s/%s",
1205 got_repo_get_path_git_dir(repo), relpath);
1207 if (packidx) {
1208 err = read_packfile_hdr(*fd, packidx);
1209 if (err) {
1210 close(*fd);
1211 *fd = -1;
1215 return err;
1218 const struct got_error *
1219 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1220 const char *path_packfile, struct got_packidx *packidx)
1222 const struct got_error *err = NULL;
1223 struct got_pack *pack = NULL;
1224 struct stat sb;
1225 size_t i;
1227 if (packp)
1228 *packp = NULL;
1230 for (i = 0; i < repo->pack_cache_size; i++) {
1231 pack = &repo->packs[i];
1232 if (pack->path_packfile == NULL)
1233 break;
1234 if (strcmp(pack->path_packfile, path_packfile) == 0)
1235 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1238 if (i == repo->pack_cache_size) {
1239 err = got_pack_close(&repo->packs[i - 1]);
1240 if (err)
1241 return err;
1242 memmove(&repo->packs[1], &repo->packs[0],
1243 sizeof(repo->packs) - sizeof(repo->packs[0]));
1244 i = 0;
1247 pack = &repo->packs[i];
1249 pack->path_packfile = strdup(path_packfile);
1250 if (pack->path_packfile == NULL) {
1251 err = got_error_from_errno("strdup");
1252 goto done;
1255 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1256 if (err)
1257 goto done;
1259 if (fstat(pack->fd, &sb) != 0) {
1260 err = got_error_from_errno("fstat");
1261 goto done;
1263 pack->filesize = sb.st_size;
1265 pack->privsep_child = NULL;
1267 #ifndef GOT_PACK_NO_MMAP
1268 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1269 pack->fd, 0);
1270 if (pack->map == MAP_FAILED) {
1271 if (errno != ENOMEM) {
1272 err = got_error_from_errno("mmap");
1273 goto done;
1275 pack->map = NULL; /* fall back to read(2) */
1277 #endif
1278 done:
1279 if (err) {
1280 if (pack) {
1281 free(pack->path_packfile);
1282 memset(pack, 0, sizeof(*pack));
1284 } else if (packp)
1285 *packp = pack;
1286 return err;
1289 struct got_pack *
1290 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1292 struct got_pack *pack = NULL;
1293 size_t i;
1295 for (i = 0; i < repo->pack_cache_size; i++) {
1296 pack = &repo->packs[i];
1297 if (pack->path_packfile == NULL)
1298 break;
1299 if (strcmp(pack->path_packfile, path_packfile) == 0)
1300 return pack;
1303 return NULL;
1306 const struct got_error *
1307 got_repo_init(const char *repo_path)
1309 const struct got_error *err = NULL;
1310 const char *dirnames[] = {
1311 GOT_OBJECTS_DIR,
1312 GOT_OBJECTS_PACK_DIR,
1313 GOT_REFS_DIR,
1315 const char *description_str = "Unnamed repository; "
1316 "edit this file 'description' to name the repository.";
1317 const char *headref_str = "ref: refs/heads/main";
1318 const char *gitconfig_str = "[core]\n"
1319 "\trepositoryformatversion = 0\n"
1320 "\tfilemode = true\n"
1321 "\tbare = true\n";
1322 char *path;
1323 size_t i;
1325 if (!got_path_dir_is_empty(repo_path))
1326 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1328 for (i = 0; i < nitems(dirnames); i++) {
1329 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1330 return got_error_from_errno("asprintf");
1332 err = got_path_mkdir(path);
1333 free(path);
1334 if (err)
1335 return err;
1338 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1339 return got_error_from_errno("asprintf");
1340 err = got_path_create_file(path, description_str);
1341 free(path);
1342 if (err)
1343 return err;
1345 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1346 return got_error_from_errno("asprintf");
1347 err = got_path_create_file(path, headref_str);
1348 free(path);
1349 if (err)
1350 return err;
1352 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1353 return got_error_from_errno("asprintf");
1354 err = got_path_create_file(path, gitconfig_str);
1355 free(path);
1356 if (err)
1357 return err;
1359 return NULL;
1362 static const struct got_error *
1363 match_packed_object(struct got_object_id **unique_id,
1364 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1366 const struct got_error *err = NULL;
1367 DIR *packdir = NULL;
1368 struct dirent *dent;
1369 char *path_packidx;
1370 struct got_object_id_queue matched_ids;
1371 int packdir_fd;
1373 STAILQ_INIT(&matched_ids);
1375 packdir_fd = openat(got_repo_get_fd(repo),
1376 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1377 if (packdir_fd == -1) {
1378 if (errno != ENOENT)
1379 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1380 goto done;
1383 packdir = fdopendir(packdir_fd);
1384 if (packdir == NULL) {
1385 err = got_error_from_errno("fdopendir");
1386 goto done;
1389 while ((dent = readdir(packdir)) != NULL) {
1390 struct got_packidx *packidx;
1391 struct got_object_qid *qid;
1393 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1394 continue;
1396 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1397 dent->d_name) == -1) {
1398 err = got_error_from_errno("strdup");
1399 break;
1402 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1403 path_packidx, 0);
1404 free(path_packidx);
1405 if (err)
1406 break;
1408 err = got_packidx_match_id_str_prefix(&matched_ids,
1409 packidx, id_str_prefix);
1410 if (err) {
1411 got_packidx_close(packidx);
1412 break;
1414 err = got_packidx_close(packidx);
1415 if (err)
1416 break;
1418 STAILQ_FOREACH(qid, &matched_ids, entry) {
1419 if (obj_type != GOT_OBJ_TYPE_ANY) {
1420 int matched_type;
1421 err = got_object_get_type(&matched_type, repo,
1422 qid->id);
1423 if (err)
1424 goto done;
1425 if (matched_type != obj_type)
1426 continue;
1428 if (*unique_id == NULL) {
1429 *unique_id = got_object_id_dup(qid->id);
1430 if (*unique_id == NULL) {
1431 err = got_error_from_errno("malloc");
1432 goto done;
1434 } else {
1435 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1436 continue; /* packed multiple times */
1437 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1438 goto done;
1442 done:
1443 got_object_id_queue_free(&matched_ids);
1444 if (packdir && closedir(packdir) != 0 && err == NULL)
1445 err = got_error_from_errno("closedir");
1446 if (err) {
1447 free(*unique_id);
1448 *unique_id = NULL;
1450 return err;
1453 static const struct got_error *
1454 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1455 const char *object_dir, const char *id_str_prefix, int obj_type,
1456 struct got_repository *repo)
1458 const struct got_error *err = NULL;
1459 char *path;
1460 DIR *dir = NULL;
1461 struct dirent *dent;
1462 struct got_object_id id;
1464 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1465 err = got_error_from_errno("asprintf");
1466 goto done;
1469 dir = opendir(path);
1470 if (dir == NULL) {
1471 if (errno == ENOENT) {
1472 err = NULL;
1473 goto done;
1475 err = got_error_from_errno2("opendir", path);
1476 goto done;
1478 while ((dent = readdir(dir)) != NULL) {
1479 char *id_str;
1480 int cmp;
1482 if (strcmp(dent->d_name, ".") == 0 ||
1483 strcmp(dent->d_name, "..") == 0)
1484 continue;
1486 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1487 err = got_error_from_errno("asprintf");
1488 goto done;
1491 if (!got_parse_sha1_digest(id.sha1, id_str))
1492 continue;
1495 * Directory entries do not necessarily appear in
1496 * sorted order, so we must iterate over all of them.
1498 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1499 if (cmp != 0) {
1500 free(id_str);
1501 continue;
1504 if (*unique_id == NULL) {
1505 if (obj_type != GOT_OBJ_TYPE_ANY) {
1506 int matched_type;
1507 err = got_object_get_type(&matched_type, repo,
1508 &id);
1509 if (err)
1510 goto done;
1511 if (matched_type != obj_type)
1512 continue;
1514 *unique_id = got_object_id_dup(&id);
1515 if (*unique_id == NULL) {
1516 err = got_error_from_errno("got_object_id_dup");
1517 free(id_str);
1518 goto done;
1520 } else {
1521 if (got_object_id_cmp(*unique_id, &id) == 0)
1522 continue; /* both packed and loose */
1523 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1524 free(id_str);
1525 goto done;
1528 done:
1529 if (dir && closedir(dir) != 0 && err == NULL)
1530 err = got_error_from_errno("closedir");
1531 if (err) {
1532 free(*unique_id);
1533 *unique_id = NULL;
1535 free(path);
1536 return err;
1539 const struct got_error *
1540 got_repo_match_object_id_prefix(struct got_object_id **id,
1541 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1543 const struct got_error *err = NULL;
1544 char *path_objects = got_repo_get_path_objects(repo);
1545 char *object_dir = NULL;
1546 size_t len;
1547 int i;
1549 *id = NULL;
1551 for (i = 0; i < strlen(id_str_prefix); i++) {
1552 if (isxdigit((unsigned char)id_str_prefix[i]))
1553 continue;
1554 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1557 len = strlen(id_str_prefix);
1558 if (len >= 2) {
1559 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1560 if (err)
1561 goto done;
1562 object_dir = strndup(id_str_prefix, 2);
1563 if (object_dir == NULL) {
1564 err = got_error_from_errno("strdup");
1565 goto done;
1567 err = match_loose_object(id, path_objects, object_dir,
1568 id_str_prefix, obj_type, repo);
1569 } else if (len == 1) {
1570 int i;
1571 for (i = 0; i < 0xf; i++) {
1572 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1573 == -1) {
1574 err = got_error_from_errno("asprintf");
1575 goto done;
1577 err = match_packed_object(id, repo, object_dir,
1578 obj_type);
1579 if (err)
1580 goto done;
1581 err = match_loose_object(id, path_objects, object_dir,
1582 id_str_prefix, obj_type, repo);
1583 if (err)
1584 goto done;
1586 } else {
1587 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1588 goto done;
1590 done:
1591 free(object_dir);
1592 if (err) {
1593 free(*id);
1594 *id = NULL;
1595 } else if (*id == NULL) {
1596 switch (obj_type) {
1597 case GOT_OBJ_TYPE_BLOB:
1598 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1599 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1600 break;
1601 case GOT_OBJ_TYPE_TREE:
1602 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1603 GOT_OBJ_LABEL_TREE, id_str_prefix);
1604 break;
1605 case GOT_OBJ_TYPE_COMMIT:
1606 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1607 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1608 break;
1609 case GOT_OBJ_TYPE_TAG:
1610 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1611 GOT_OBJ_LABEL_TAG, id_str_prefix);
1612 break;
1613 default:
1614 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1615 break;
1619 return err;
1622 const struct got_error *
1623 got_repo_match_object_id(struct got_object_id **id, char **label,
1624 const char *id_str, int obj_type, struct got_reflist_head *refs,
1625 struct got_repository *repo)
1627 const struct got_error *err;
1628 struct got_tag_object *tag;
1629 struct got_reference *ref = NULL;
1631 *id = NULL;
1632 if (label)
1633 *label = NULL;
1635 if (refs) {
1636 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1637 refs, repo);
1638 if (err == NULL) {
1639 *id = got_object_id_dup(
1640 got_object_tag_get_object_id(tag));
1641 if (*id == NULL)
1642 err = got_error_from_errno("got_object_id_dup");
1643 else if (label && asprintf(label, "refs/tags/%s",
1644 got_object_tag_get_name(tag)) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 free(*id);
1647 *id = NULL;
1649 got_object_tag_close(tag);
1650 return err;
1651 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1652 err->code != GOT_ERR_NO_OBJ)
1653 return err;
1656 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1657 if (err) {
1658 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1659 return err;
1660 err = got_ref_open(&ref, repo, id_str, 0);
1661 if (err != NULL)
1662 goto done;
1663 if (label) {
1664 *label = strdup(got_ref_get_name(ref));
1665 if (*label == NULL) {
1666 err = got_error_from_errno("strdup");
1667 goto done;
1670 err = got_ref_resolve(id, repo, ref);
1671 } else if (label) {
1672 err = got_object_id_str(label, *id);
1673 if (*label == NULL) {
1674 err = got_error_from_errno("strdup");
1675 goto done;
1678 done:
1679 if (ref)
1680 got_ref_close(ref);
1681 return err;
1684 const struct got_error *
1685 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1686 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_reflist_entry *re;
1690 struct got_object_id *tag_id;
1691 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1693 *tag = NULL;
1695 TAILQ_FOREACH(re, refs, entry) {
1696 const char *refname;
1697 refname = got_ref_get_name(re->ref);
1698 if (got_ref_is_symbolic(re->ref))
1699 continue;
1700 if (strncmp(refname, "refs/tags/", 10) != 0)
1701 continue;
1702 if (!name_is_absolute)
1703 refname += strlen("refs/tags/");
1704 if (strcmp(refname, name) != 0)
1705 continue;
1706 err = got_ref_resolve(&tag_id, repo, re->ref);
1707 if (err)
1708 break;
1709 err = got_object_open_as_tag(tag, repo, tag_id);
1710 free(tag_id);
1711 if (err)
1712 break;
1713 if (obj_type == GOT_OBJ_TYPE_ANY ||
1714 got_object_tag_get_object_type(*tag) == obj_type)
1715 break;
1716 got_object_tag_close(*tag);
1717 *tag = NULL;
1720 if (err == NULL && *tag == NULL)
1721 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1722 GOT_OBJ_LABEL_TAG, name);
1723 return err;
1726 static const struct got_error *
1727 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1728 const char *name, mode_t mode, struct got_object_id *blob_id)
1730 const struct got_error *err = NULL;
1732 *new_te = NULL;
1734 *new_te = calloc(1, sizeof(**new_te));
1735 if (*new_te == NULL)
1736 return got_error_from_errno("calloc");
1738 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1739 sizeof((*new_te)->name)) {
1740 err = got_error(GOT_ERR_NO_SPACE);
1741 goto done;
1744 if (S_ISLNK(mode)) {
1745 (*new_te)->mode = S_IFLNK;
1746 } else {
1747 (*new_te)->mode = S_IFREG;
1748 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1750 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1751 done:
1752 if (err && *new_te) {
1753 free(*new_te);
1754 *new_te = NULL;
1756 return err;
1759 static const struct got_error *
1760 import_file(struct got_tree_entry **new_te, struct dirent *de,
1761 const char *path, struct got_repository *repo)
1763 const struct got_error *err;
1764 struct got_object_id *blob_id = NULL;
1765 char *filepath;
1766 struct stat sb;
1768 if (asprintf(&filepath, "%s%s%s", path,
1769 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1770 return got_error_from_errno("asprintf");
1772 if (lstat(filepath, &sb) != 0) {
1773 err = got_error_from_errno2("lstat", path);
1774 goto done;
1777 err = got_object_blob_create(&blob_id, filepath, repo);
1778 if (err)
1779 goto done;
1781 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1782 blob_id);
1783 done:
1784 free(filepath);
1785 if (err)
1786 free(blob_id);
1787 return err;
1790 static const struct got_error *
1791 insert_tree_entry(struct got_tree_entry *new_te,
1792 struct got_pathlist_head *paths)
1794 const struct got_error *err = NULL;
1795 struct got_pathlist_entry *new_pe;
1797 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1798 if (err)
1799 return err;
1800 if (new_pe == NULL)
1801 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1802 return NULL;
1805 static const struct got_error *write_tree(struct got_object_id **,
1806 const char *, struct got_pathlist_head *, struct got_repository *,
1807 got_repo_import_cb progress_cb, void *progress_arg);
1809 static const struct got_error *
1810 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1811 const char *path, struct got_pathlist_head *ignores,
1812 struct got_repository *repo,
1813 got_repo_import_cb progress_cb, void *progress_arg)
1815 const struct got_error *err;
1816 struct got_object_id *id = NULL;
1817 char *subdirpath;
1819 if (asprintf(&subdirpath, "%s%s%s", path,
1820 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1821 return got_error_from_errno("asprintf");
1823 (*new_te) = calloc(1, sizeof(**new_te));
1824 if (*new_te == NULL)
1825 return got_error_from_errno("calloc");
1826 (*new_te)->mode = S_IFDIR;
1827 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1828 sizeof((*new_te)->name)) {
1829 err = got_error(GOT_ERR_NO_SPACE);
1830 goto done;
1832 err = write_tree(&id, subdirpath, ignores, repo,
1833 progress_cb, progress_arg);
1834 if (err)
1835 goto done;
1836 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1838 done:
1839 free(id);
1840 free(subdirpath);
1841 if (err) {
1842 free(*new_te);
1843 *new_te = NULL;
1845 return err;
1848 static const struct got_error *
1849 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1850 struct got_pathlist_head *ignores, struct got_repository *repo,
1851 got_repo_import_cb progress_cb, void *progress_arg)
1853 const struct got_error *err = NULL;
1854 DIR *dir;
1855 struct dirent *de;
1856 int nentries;
1857 struct got_tree_entry *new_te = NULL;
1858 struct got_pathlist_head paths;
1859 struct got_pathlist_entry *pe;
1861 *new_tree_id = NULL;
1863 TAILQ_INIT(&paths);
1865 dir = opendir(path_dir);
1866 if (dir == NULL) {
1867 err = got_error_from_errno2("opendir", path_dir);
1868 goto done;
1871 nentries = 0;
1872 while ((de = readdir(dir)) != NULL) {
1873 int ignore = 0;
1874 int type;
1876 if (strcmp(de->d_name, ".") == 0 ||
1877 strcmp(de->d_name, "..") == 0)
1878 continue;
1880 TAILQ_FOREACH(pe, ignores, entry) {
1881 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1882 ignore = 1;
1883 break;
1886 if (ignore)
1887 continue;
1889 err = got_path_dirent_type(&type, path_dir, de);
1890 if (err)
1891 goto done;
1893 if (type == DT_DIR) {
1894 err = import_subdir(&new_te, de, path_dir,
1895 ignores, repo, progress_cb, progress_arg);
1896 if (err) {
1897 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1898 goto done;
1899 err = NULL;
1900 continue;
1902 } else if (type == DT_REG || type == DT_LNK) {
1903 err = import_file(&new_te, de, path_dir, repo);
1904 if (err)
1905 goto done;
1906 } else
1907 continue;
1909 err = insert_tree_entry(new_te, &paths);
1910 if (err)
1911 goto done;
1912 nentries++;
1915 if (TAILQ_EMPTY(&paths)) {
1916 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1917 "cannot create tree without any entries");
1918 goto done;
1921 TAILQ_FOREACH(pe, &paths, entry) {
1922 struct got_tree_entry *te = pe->data;
1923 char *path;
1924 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1925 continue;
1926 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1927 err = got_error_from_errno("asprintf");
1928 goto done;
1930 err = (*progress_cb)(progress_arg, path);
1931 free(path);
1932 if (err)
1933 goto done;
1936 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1937 done:
1938 if (dir)
1939 closedir(dir);
1940 got_pathlist_free(&paths);
1941 return err;
1944 const struct got_error *
1945 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1946 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1947 struct got_repository *repo, got_repo_import_cb progress_cb,
1948 void *progress_arg)
1950 const struct got_error *err;
1951 struct got_object_id *new_tree_id;
1953 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1954 progress_cb, progress_arg);
1955 if (err)
1956 return err;
1958 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1959 author, time(NULL), author, time(NULL), logmsg, repo);
1960 free(new_tree_id);
1961 return err;
1964 const struct got_error *
1965 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1966 struct got_repository *repo)
1968 const struct got_error *err = NULL;
1969 char *path_objects = NULL, *path = NULL;
1970 DIR *dir = NULL;
1971 struct got_object_id id;
1972 int i;
1974 *nobjects = 0;
1975 *ondisk_size = 0;
1977 path_objects = got_repo_get_path_objects(repo);
1978 if (path_objects == NULL)
1979 return got_error_from_errno("got_repo_get_path_objects");
1981 for (i = 0; i <= 0xff; i++) {
1982 struct dirent *dent;
1984 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1985 err = got_error_from_errno("asprintf");
1986 break;
1989 dir = opendir(path);
1990 if (dir == NULL) {
1991 if (errno == ENOENT) {
1992 err = NULL;
1993 continue;
1995 err = got_error_from_errno2("opendir", path);
1996 break;
1999 while ((dent = readdir(dir)) != NULL) {
2000 char *id_str;
2001 int fd;
2002 struct stat sb;
2004 if (strcmp(dent->d_name, ".") == 0 ||
2005 strcmp(dent->d_name, "..") == 0)
2006 continue;
2008 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2009 err = got_error_from_errno("asprintf");
2010 goto done;
2013 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2014 free(id_str);
2015 continue;
2017 free(id_str);
2019 err = got_object_open_loose_fd(&fd, &id, repo);
2020 if (err)
2021 goto done;
2023 if (fstat(fd, &sb) == -1) {
2024 err = got_error_from_errno("fstat");
2025 close(fd);
2026 goto done;
2028 (*nobjects)++;
2029 (*ondisk_size) += sb.st_size;
2031 if (close(fd) == -1) {
2032 err = got_error_from_errno("close");
2033 goto done;
2037 if (closedir(dir) != 0) {
2038 err = got_error_from_errno("closedir");
2039 goto done;
2041 dir = NULL;
2043 free(path);
2044 path = NULL;
2046 done:
2047 if (dir && closedir(dir) != 0 && err == NULL)
2048 err = got_error_from_errno("closedir");
2050 if (err) {
2051 *nobjects = 0;
2052 *ondisk_size = 0;
2054 free(path_objects);
2055 free(path);
2056 return err;
2059 const struct got_error *
2060 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2061 off_t *total_packsize, struct got_repository *repo)
2063 const struct got_error *err = NULL;
2064 DIR *packdir = NULL;
2065 struct dirent *dent;
2066 struct got_packidx *packidx = NULL;
2067 char *path_packidx;
2068 char *path_packfile;
2069 int packdir_fd;
2070 struct stat sb;
2072 *npackfiles = 0;
2073 *nobjects = 0;
2074 *total_packsize = 0;
2076 packdir_fd = openat(got_repo_get_fd(repo),
2077 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2078 if (packdir_fd == -1) {
2079 return got_error_from_errno_fmt("openat: %s/%s",
2080 got_repo_get_path_git_dir(repo),
2081 GOT_OBJECTS_PACK_DIR);
2084 packdir = fdopendir(packdir_fd);
2085 if (packdir == NULL) {
2086 err = got_error_from_errno("fdopendir");
2087 goto done;
2090 while ((dent = readdir(packdir)) != NULL) {
2091 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
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);