Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <uuid.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 const char *
66 got_repo_get_path(struct got_repository *repo)
67 {
68 return repo->path;
69 }
71 const char *
72 got_repo_get_path_git_dir(struct got_repository *repo)
73 {
74 return repo->path_git_dir;
75 }
77 int
78 got_repo_get_fd(struct got_repository *repo)
79 {
80 return repo->gitdir_fd;
81 }
83 const char *
84 got_repo_get_gitconfig_author_name(struct got_repository *repo)
85 {
86 return repo->gitconfig_author_name;
87 }
89 const char *
90 got_repo_get_gitconfig_author_email(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_email;
93 }
95 const char *
96 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
97 {
98 return repo->global_gitconfig_author_name;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
104 return repo->global_gitconfig_author_email;
107 const char *
108 got_repo_get_gitconfig_owner(struct got_repository *repo)
110 return repo->gitconfig_owner;
113 void
114 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
115 struct got_repository *repo)
117 *extensions = repo->extensions;
118 *nextensions = repo->nextensions;
121 int
122 got_repo_is_bare(struct got_repository *repo)
124 return (strcmp(repo->path, repo->path_git_dir) == 0);
127 static char *
128 get_path_git_child(struct got_repository *repo, const char *basename)
130 char *path_child;
132 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
133 basename) == -1)
134 return NULL;
136 return path_child;
139 char *
140 got_repo_get_path_objects(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_DIR);
145 char *
146 got_repo_get_path_objects_pack(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
151 char *
152 got_repo_get_path_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_REFS_DIR);
157 char *
158 got_repo_get_path_packed_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
163 static char *
164 get_path_head(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_HEAD_FILE);
169 char *
170 got_repo_get_path_gitconfig(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_GITCONFIG);
175 char *
176 got_repo_get_path_gotconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
181 const struct got_gotconfig *
182 got_repo_get_gotconfig(struct got_repository *repo)
184 return repo->gotconfig;
187 void
188 got_repo_get_gitconfig_remotes(int *nremotes,
189 const struct got_remote_repo **remotes, struct got_repository *repo)
191 *nremotes = repo->ngitconfig_remotes;
192 *remotes = repo->gitconfig_remotes;
195 static int
196 is_git_repo(struct got_repository *repo)
198 const char *path_git = got_repo_get_path_git_dir(repo);
199 char *path_objects = got_repo_get_path_objects(repo);
200 char *path_refs = got_repo_get_path_refs(repo);
201 char *path_head = get_path_head(repo);
202 int ret = 0;
203 struct stat sb;
204 struct got_reference *head_ref;
206 if (lstat(path_git, &sb) == -1)
207 goto done;
208 if (!S_ISDIR(sb.st_mode))
209 goto done;
211 if (lstat(path_objects, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_refs, &sb) == -1)
217 goto done;
218 if (!S_ISDIR(sb.st_mode))
219 goto done;
221 if (lstat(path_head, &sb) == -1)
222 goto done;
223 if (!S_ISREG(sb.st_mode))
224 goto done;
226 /* Check if the HEAD reference can be opened. */
227 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
228 goto done;
229 got_ref_close(head_ref);
231 ret = 1;
232 done:
233 free(path_objects);
234 free(path_refs);
235 free(path_head);
236 return ret;
240 const struct got_error *
241 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
242 struct got_object *obj)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->objcache, id, obj);
247 if (err) {
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
250 err = NULL;
251 return err;
253 obj->refcnt++;
254 #endif
255 return NULL;
258 struct got_object *
259 got_repo_get_cached_object(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
265 const struct got_error *
266 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
267 struct got_tree_object *tree)
269 #ifndef GOT_NO_OBJ_CACHE
270 const struct got_error *err = NULL;
271 err = got_object_cache_add(&repo->treecache, id, tree);
272 if (err) {
273 if (err->code == GOT_ERR_OBJ_EXISTS ||
274 err->code == GOT_ERR_OBJ_TOO_LARGE)
275 err = NULL;
276 return err;
278 tree->refcnt++;
279 #endif
280 return NULL;
283 struct got_tree_object *
284 got_repo_get_cached_tree(struct got_repository *repo,
285 struct got_object_id *id)
287 return (struct got_tree_object *)got_object_cache_get(
288 &repo->treecache, id);
291 const struct got_error *
292 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
293 struct got_commit_object *commit)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->commitcache, id, commit);
298 if (err) {
299 if (err->code == GOT_ERR_OBJ_EXISTS ||
300 err->code == GOT_ERR_OBJ_TOO_LARGE)
301 err = NULL;
302 return err;
304 commit->refcnt++;
305 #endif
306 return NULL;
309 struct got_commit_object *
310 got_repo_get_cached_commit(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_commit_object *)got_object_cache_get(
314 &repo->commitcache, id);
317 const struct got_error *
318 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
319 struct got_tag_object *tag)
321 #ifndef GOT_NO_OBJ_CACHE
322 const struct got_error *err = NULL;
323 err = got_object_cache_add(&repo->tagcache, id, tag);
324 if (err) {
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 err = NULL;
328 return err;
330 tag->refcnt++;
331 #endif
332 return NULL;
335 struct got_tag_object *
336 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
338 return (struct got_tag_object *)got_object_cache_get(
339 &repo->tagcache, id);
342 static const struct got_error *
343 open_repo(struct got_repository *repo, const char *path)
345 const struct got_error *err = NULL;
347 repo->gitdir_fd = -1;
349 /* bare git repository? */
350 repo->path_git_dir = strdup(path);
351 if (repo->path_git_dir == NULL)
352 return got_error_from_errno("strdup");
353 if (is_git_repo(repo)) {
354 repo->path = strdup(repo->path_git_dir);
355 if (repo->path == NULL) {
356 err = got_error_from_errno("strdup");
357 goto done;
359 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
360 if (repo->gitdir_fd == -1) {
361 err = got_error_from_errno2("open",
362 repo->path_git_dir);
363 goto done;
365 return NULL;
368 /* git repository with working tree? */
369 free(repo->path_git_dir);
370 repo->path_git_dir = NULL;
371 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
372 err = got_error_from_errno("asprintf");
373 goto done;
375 if (is_git_repo(repo)) {
376 repo->path = strdup(path);
377 if (repo->path == NULL) {
378 err = got_error_from_errno("strdup");
379 goto done;
381 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
382 if (repo->gitdir_fd == -1) {
383 err = got_error_from_errno2("open",
384 repo->path_git_dir);
385 goto done;
387 return NULL;
390 err = got_error(GOT_ERR_NOT_GIT_REPO);
391 done:
392 if (err) {
393 free(repo->path);
394 repo->path = NULL;
395 free(repo->path_git_dir);
396 repo->path_git_dir = NULL;
397 if (repo->gitdir_fd != -1)
398 close(repo->gitdir_fd);
399 repo->gitdir_fd = -1;
402 return err;
405 static const struct got_error *
406 parse_gitconfig_file(int *gitconfig_repository_format_version,
407 char **gitconfig_author_name, char **gitconfig_author_email,
408 struct got_remote_repo **remotes, int *nremotes,
409 char **gitconfig_owner, char ***extensions, int *nextensions,
410 const char *gitconfig_path)
412 const struct got_error *err = NULL, *child_err = NULL;
413 int fd = -1;
414 int imsg_fds[2] = { -1, -1 };
415 pid_t pid;
416 struct imsgbuf *ibuf;
418 *gitconfig_repository_format_version = 0;
419 if (extensions)
420 *extensions = NULL;
421 if (nextensions)
422 *nextensions = 0;
423 *gitconfig_author_name = NULL;
424 *gitconfig_author_email = NULL;
425 if (remotes)
426 *remotes = NULL;
427 if (nremotes)
428 *nremotes = 0;
429 if (gitconfig_owner)
430 *gitconfig_owner = NULL;
432 fd = open(gitconfig_path, O_RDONLY);
433 if (fd == -1) {
434 if (errno == ENOENT)
435 return NULL;
436 return got_error_from_errno2("open", gitconfig_path);
439 ibuf = calloc(1, sizeof(*ibuf));
440 if (ibuf == NULL) {
441 err = got_error_from_errno("calloc");
442 goto done;
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
446 err = got_error_from_errno("socketpair");
447 goto done;
450 pid = fork();
451 if (pid == -1) {
452 err = got_error_from_errno("fork");
453 goto done;
454 } else if (pid == 0) {
455 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
456 gitconfig_path);
457 /* not reached */
460 if (close(imsg_fds[1]) == -1) {
461 err = got_error_from_errno("close");
462 goto done;
464 imsg_fds[1] = -1;
465 imsg_init(ibuf, imsg_fds[0]);
467 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
468 if (err)
469 goto done;
470 fd = -1;
472 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
473 if (err)
474 goto done;
476 err = got_privsep_recv_gitconfig_int(
477 gitconfig_repository_format_version, ibuf);
478 if (err)
479 goto done;
481 if (extensions && nextensions) {
482 err = got_privsep_send_gitconfig_repository_extensions_req(
483 ibuf);
484 if (err)
485 goto done;
486 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
487 if (err)
488 goto done;
489 if (*nextensions > 0) {
490 int i;
491 *extensions = calloc(*nextensions, sizeof(char *));
492 if (*extensions == NULL) {
493 err = got_error_from_errno("calloc");
494 goto done;
496 for (i = 0; i < *nextensions; i++) {
497 char *ext;
498 err = got_privsep_recv_gitconfig_str(&ext,
499 ibuf);
500 if (err)
501 goto done;
502 (*extensions)[i] = ext;
507 err = got_privsep_send_gitconfig_author_name_req(ibuf);
508 if (err)
509 goto done;
511 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
512 if (err)
513 goto done;
515 err = got_privsep_send_gitconfig_author_email_req(ibuf);
516 if (err)
517 goto done;
519 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
520 if (err)
521 goto done;
523 if (remotes && nremotes) {
524 err = got_privsep_send_gitconfig_remotes_req(ibuf);
525 if (err)
526 goto done;
528 err = got_privsep_recv_gitconfig_remotes(remotes,
529 nremotes, ibuf);
530 if (err)
531 goto done;
534 if (gitconfig_owner) {
535 err = got_privsep_send_gitconfig_owner_req(ibuf);
536 if (err)
537 goto done;
538 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
539 if (err)
540 goto done;
543 imsg_clear(ibuf);
544 err = got_privsep_send_stop(imsg_fds[0]);
545 child_err = got_privsep_wait_for_child(pid);
546 if (child_err && err == NULL)
547 err = child_err;
548 done:
549 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
550 err = got_error_from_errno("close");
551 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (fd != -1 && close(fd) == -1 && err == NULL)
554 err = got_error_from_errno2("close", gitconfig_path);
555 free(ibuf);
556 return err;
559 static const struct got_error *
560 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
562 const struct got_error *err = NULL;
563 char *repo_gitconfig_path = NULL;
565 if (global_gitconfig_path) {
566 /* Read settings from ~/.gitconfig. */
567 int dummy_repo_version;
568 err = parse_gitconfig_file(&dummy_repo_version,
569 &repo->global_gitconfig_author_name,
570 &repo->global_gitconfig_author_email,
571 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
572 if (err)
573 return err;
576 /* Read repository's .git/config file. */
577 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
578 if (repo_gitconfig_path == NULL)
579 return got_error_from_errno("got_repo_get_path_gitconfig");
581 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
582 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
583 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
584 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
585 repo_gitconfig_path);
586 if (err)
587 goto done;
588 done:
589 free(repo_gitconfig_path);
590 return err;
593 static const struct got_error *
594 read_gotconfig(struct got_repository *repo)
596 const struct got_error *err = NULL;
597 char *gotconfig_path;
599 gotconfig_path = got_repo_get_path_gotconfig(repo);
600 if (gotconfig_path == NULL)
601 return got_error_from_errno("got_repo_get_path_gotconfig");
603 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
604 free(gotconfig_path);
605 return err;
608 /* Supported repository format extensions. */
609 static const char *repo_extensions[] = {
610 "noop", /* Got supports repository format version 1. */
611 "preciousObjects", /* Supported by gotadmin cleanup. */
612 "worktreeConfig", /* Got does not care about Git work trees. */
613 };
615 const struct got_error *
616 got_repo_open(struct got_repository **repop, const char *path,
617 const char *global_gitconfig_path)
619 struct got_repository *repo = NULL;
620 const struct got_error *err = NULL;
621 char *repo_path = NULL;
622 size_t i;
623 struct rlimit rl;
625 *repop = NULL;
627 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
628 return got_error_from_errno("getrlimit");
630 repo = calloc(1, sizeof(*repo));
631 if (repo == NULL) {
632 err = got_error_from_errno("calloc");
633 goto done;
636 STAILQ_INIT(&repo->packidx_bloom_filters);
638 for (i = 0; i < nitems(repo->privsep_children); i++) {
639 memset(&repo->privsep_children[i], 0,
640 sizeof(repo->privsep_children[0]));
641 repo->privsep_children[i].imsg_fd = -1;
644 err = got_object_cache_init(&repo->objcache,
645 GOT_OBJECT_CACHE_TYPE_OBJ);
646 if (err)
647 goto done;
648 err = got_object_cache_init(&repo->treecache,
649 GOT_OBJECT_CACHE_TYPE_TREE);
650 if (err)
651 goto done;
652 err = got_object_cache_init(&repo->commitcache,
653 GOT_OBJECT_CACHE_TYPE_COMMIT);
654 if (err)
655 goto done;
656 err = got_object_cache_init(&repo->tagcache,
657 GOT_OBJECT_CACHE_TYPE_TAG);
658 if (err)
659 goto done;
661 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
662 if (repo->pack_cache_size > rl.rlim_cur / 8)
663 repo->pack_cache_size = rl.rlim_cur / 8;
665 repo_path = realpath(path, NULL);
666 if (repo_path == NULL) {
667 err = got_error_from_errno2("realpath", path);
668 goto done;
671 for (;;) {
672 char *parent_path;
674 err = open_repo(repo, repo_path);
675 if (err == NULL)
676 break;
677 if (err->code != GOT_ERR_NOT_GIT_REPO)
678 goto done;
679 if (repo_path[0] == '/' && repo_path[1] == '\0') {
680 err = got_error(GOT_ERR_NOT_GIT_REPO);
681 goto done;
683 err = got_path_dirname(&parent_path, repo_path);
684 if (err)
685 goto done;
686 free(repo_path);
687 repo_path = parent_path;
690 err = read_gotconfig(repo);
691 if (err)
692 goto done;
694 err = read_gitconfig(repo, global_gitconfig_path);
695 if (err)
696 goto done;
697 if (repo->gitconfig_repository_format_version != 0)
698 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
699 for (i = 0; i < repo->nextensions; i++) {
700 char *ext = repo->extensions[i];
701 int j, supported = 0;
702 for (j = 0; j < nitems(repo_extensions); j++) {
703 if (strcmp(ext, repo_extensions[j]) == 0) {
704 supported = 1;
705 break;
708 if (!supported) {
709 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
710 goto done;
713 done:
714 if (err)
715 got_repo_close(repo);
716 else
717 *repop = repo;
718 free(repo_path);
719 return err;
722 const struct got_error *
723 got_repo_close(struct got_repository *repo)
725 const struct got_error *err = NULL, *child_err;
726 size_t i;
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packidx_cache[i] == NULL)
730 break;
731 got_packidx_close(repo->packidx_cache[i]);
734 while (!STAILQ_EMPTY(&repo->packidx_bloom_filters)) {
735 struct got_packidx_bloom_filter *bf;
736 bf = STAILQ_FIRST(&repo->packidx_bloom_filters);
737 STAILQ_REMOVE_HEAD(&repo->packidx_bloom_filters, entry);
738 free(bf->bloom);
739 free(bf);
742 for (i = 0; i < repo->pack_cache_size; i++) {
743 if (repo->packs[i].path_packfile == NULL)
744 break;
745 got_pack_close(&repo->packs[i]);
748 free(repo->path);
749 free(repo->path_git_dir);
751 got_object_cache_close(&repo->objcache);
752 got_object_cache_close(&repo->treecache);
753 got_object_cache_close(&repo->commitcache);
754 got_object_cache_close(&repo->tagcache);
756 for (i = 0; i < nitems(repo->privsep_children); i++) {
757 if (repo->privsep_children[i].imsg_fd == -1)
758 continue;
759 imsg_clear(repo->privsep_children[i].ibuf);
760 free(repo->privsep_children[i].ibuf);
761 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
762 child_err = got_privsep_wait_for_child(
763 repo->privsep_children[i].pid);
764 if (child_err && err == NULL)
765 err = child_err;
766 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
767 err == NULL)
768 err = got_error_from_errno("close");
771 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
772 err == NULL)
773 err = got_error_from_errno("close");
775 if (repo->gotconfig)
776 got_gotconfig_free(repo->gotconfig);
777 free(repo->gitconfig_author_name);
778 free(repo->gitconfig_author_email);
779 for (i = 0; i < repo->ngitconfig_remotes; i++)
780 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
781 free(repo->gitconfig_remotes);
782 for (i = 0; i < repo->nextensions; i++)
783 free(repo->extensions[i]);
784 free(repo->extensions);
785 free(repo);
787 return err;
790 void
791 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
793 int i;
795 free(repo->name);
796 repo->name = NULL;
797 free(repo->fetch_url);
798 repo->fetch_url = NULL;
799 free(repo->send_url);
800 repo->send_url = NULL;
801 for (i = 0; i < repo->nfetch_branches; i++)
802 free(repo->fetch_branches[i]);
803 free(repo->fetch_branches);
804 repo->fetch_branches = NULL;
805 repo->nfetch_branches = 0;
806 for (i = 0; i < repo->nsend_branches; i++)
807 free(repo->send_branches[i]);
808 free(repo->send_branches);
809 repo->send_branches = NULL;
810 repo->nsend_branches = 0;
813 const struct got_error *
814 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
815 const char *input_path)
817 const struct got_error *err = NULL;
818 const char *repo_abspath = NULL;
819 size_t repolen, len;
820 char *canonpath, *path = NULL;
822 *in_repo_path = NULL;
824 canonpath = strdup(input_path);
825 if (canonpath == NULL) {
826 err = got_error_from_errno("strdup");
827 goto done;
829 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
830 if (err)
831 goto done;
833 repo_abspath = got_repo_get_path(repo);
835 if (canonpath[0] == '\0') {
836 path = strdup(canonpath);
837 if (path == NULL) {
838 err = got_error_from_errno("strdup");
839 goto done;
841 } else {
842 path = realpath(canonpath, NULL);
843 if (path == NULL) {
844 if (errno != ENOENT) {
845 err = got_error_from_errno2("realpath",
846 canonpath);
847 goto done;
849 /*
850 * Path is not on disk.
851 * Assume it is already relative to repository root.
852 */
853 path = strdup(canonpath);
854 if (path == NULL) {
855 err = got_error_from_errno("strdup");
856 goto done;
860 repolen = strlen(repo_abspath);
861 len = strlen(path);
864 if (strcmp(path, repo_abspath) == 0) {
865 free(path);
866 path = strdup("");
867 if (path == NULL) {
868 err = got_error_from_errno("strdup");
869 goto done;
871 } else if (len > repolen &&
872 got_path_is_child(path, repo_abspath, repolen)) {
873 /* Matched an on-disk path inside repository. */
874 if (got_repo_is_bare(repo)) {
875 /*
876 * Matched an on-disk path inside repository
877 * database. Treat input as repository-relative.
878 */
879 free(path);
880 path = canonpath;
881 canonpath = NULL;
882 } else {
883 char *child;
884 /* Strip common prefix with repository path. */
885 err = got_path_skip_common_ancestor(&child,
886 repo_abspath, path);
887 if (err)
888 goto done;
889 free(path);
890 path = child;
892 } else {
893 /*
894 * Matched unrelated on-disk path.
895 * Treat input as repository-relative.
896 */
897 free(path);
898 path = canonpath;
899 canonpath = NULL;
903 /* Make in-repository path absolute */
904 if (path[0] != '/') {
905 char *abspath;
906 if (asprintf(&abspath, "/%s", path) == -1) {
907 err = got_error_from_errno("asprintf");
908 goto done;
910 free(path);
911 path = abspath;
914 done:
915 free(canonpath);
916 if (err)
917 free(path);
918 else
919 *in_repo_path = path;
920 return err;
923 static const struct got_error *
924 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
925 const char *path_packidx)
927 const struct got_error *err = NULL;
928 size_t i;
930 for (i = 0; i < repo->pack_cache_size; i++) {
931 if (repo->packidx_cache[i] == NULL)
932 break;
933 if (strcmp(repo->packidx_cache[i]->path_packidx,
934 path_packidx) == 0) {
935 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
938 if (i == repo->pack_cache_size) {
939 i = repo->pack_cache_size - 1;
940 err = got_packidx_close(repo->packidx_cache[i]);
941 if (err)
942 return err;
945 repo->packidx_cache[i] = packidx;
947 return NULL;
950 int
951 got_repo_is_packidx_filename(const char *name, size_t len)
953 if (len != GOT_PACKIDX_NAMELEN)
954 return 0;
956 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
957 return 0;
959 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
960 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
961 return 0;
963 return 1;
966 static int
967 check_packidx_bloom_filter(struct got_repository *repo,
968 const char *path_packidx, struct got_object_id *id)
970 struct got_packidx_bloom_filter *bf;
972 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
973 if (got_path_cmp(bf->path_packidx, path_packidx,
974 bf->path_packidx_len, strlen(path_packidx)) == 0) {
975 return bloom_check(bf->bloom, id->sha1,
976 sizeof(id->sha1));
980 /* No bloom filter means this pack index must be searched. */
981 return 1;
984 static const struct got_error *
985 add_packidx_bloom_filter(struct got_repository *repo,
986 struct got_packidx *packidx, const char *path_packidx)
988 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
989 struct got_packidx_bloom_filter *bf;
990 size_t len;
992 /*
993 * Don't use bloom filters for very large pack index files.
994 * Large pack files will contain a relatively large fraction
995 * of our objects so we will likely need to visit them anyway.
996 * The more objects a pack file contains the higher the probability
997 * of a false-positive match from the bloom filter. And reading
998 * all object IDs from a large pack index file can be expensive.
999 */
1000 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1001 return NULL;
1003 /* Do we already have a filter for this pack index? */
1004 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
1005 if (got_path_cmp(bf->path_packidx, path_packidx,
1006 bf->path_packidx_len, strlen(path_packidx)) == 0)
1007 return NULL;
1010 bf = calloc(1, sizeof(*bf));
1011 if (bf == NULL)
1012 return got_error_from_errno("calloc");
1013 bf->bloom = calloc(1, sizeof(*bf->bloom));
1014 if (bf->bloom == NULL) {
1015 free(bf);
1016 return got_error_from_errno("calloc");
1020 len = strlcpy(bf->path_packidx, path_packidx, sizeof(bf->path_packidx));
1021 if (len >= sizeof(bf->path_packidx)) {
1022 free(bf->bloom);
1023 free(bf);
1024 return got_error(GOT_ERR_NO_SPACE);
1026 bf->path_packidx_len = len;
1028 /* Minimum size supported by our bloom filter is 1000 entries. */
1029 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1030 for (i = 0; i < nobjects; i++) {
1031 struct got_packidx_object_id *id;
1032 id = &packidx->hdr.sorted_ids[i];
1033 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1036 STAILQ_INSERT_TAIL(&repo->packidx_bloom_filters, bf, entry);
1037 return NULL;
1040 const struct got_error *
1041 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1042 struct got_repository *repo, struct got_object_id *id)
1044 const struct got_error *err;
1045 DIR *packdir = NULL;
1046 struct dirent *dent;
1047 char *path_packidx;
1048 size_t i;
1049 int packdir_fd;
1051 /* Search pack index cache. */
1052 for (i = 0; i < repo->pack_cache_size; i++) {
1053 if (repo->packidx_cache[i] == NULL)
1054 break;
1055 if (!check_packidx_bloom_filter(repo,
1056 repo->packidx_cache[i]->path_packidx, id))
1057 continue; /* object will not be found in this index */
1058 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1059 if (*idx != -1) {
1060 *packidx = repo->packidx_cache[i];
1062 * Move this cache entry to the front. Repeatedly
1063 * searching a wrong pack index can be expensive.
1065 if (i > 0) {
1066 memmove(&repo->packidx_cache[1],
1067 &repo->packidx_cache[0],
1068 i * sizeof(repo->packidx_cache[0]));
1069 repo->packidx_cache[0] = *packidx;
1071 return NULL;
1074 /* No luck. Search the filesystem. */
1076 packdir_fd = openat(got_repo_get_fd(repo),
1077 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1078 if (packdir_fd == -1) {
1079 if (errno == ENOENT)
1080 err = got_error_no_obj(id);
1081 else
1082 err = got_error_from_errno_fmt("openat: %s/%s",
1083 got_repo_get_path_git_dir(repo),
1084 GOT_OBJECTS_PACK_DIR);
1085 goto done;
1088 packdir = fdopendir(packdir_fd);
1089 if (packdir == NULL) {
1090 err = got_error_from_errno("fdopendir");
1091 goto done;
1094 while ((dent = readdir(packdir)) != NULL) {
1095 int is_cached = 0;
1097 if (!got_repo_is_packidx_filename(dent->d_name,
1098 strlen(dent->d_name)))
1099 continue;
1101 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1102 dent->d_name) == -1) {
1103 err = got_error_from_errno("asprintf");
1104 goto done;
1107 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1108 free(path_packidx);
1109 continue; /* object will not be found in this index */
1112 for (i = 0; i < repo->pack_cache_size; i++) {
1113 if (repo->packidx_cache[i] == NULL)
1114 break;
1115 if (strcmp(repo->packidx_cache[i]->path_packidx,
1116 path_packidx) == 0) {
1117 is_cached = 1;
1118 break;
1121 if (is_cached) {
1122 free(path_packidx);
1123 continue; /* already searched */
1126 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1127 path_packidx, 0);
1128 if (err) {
1129 free(path_packidx);
1130 goto done;
1133 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1134 if (err) {
1135 free(path_packidx);
1136 goto done;
1139 err = cache_packidx(repo, *packidx, path_packidx);
1140 free(path_packidx);
1141 if (err)
1142 goto done;
1144 *idx = got_packidx_get_object_idx(*packidx, id);
1145 if (*idx != -1) {
1146 err = NULL; /* found the object */
1147 goto done;
1151 err = got_error_no_obj(id);
1152 done:
1153 if (packdir && closedir(packdir) != 0 && err == NULL)
1154 err = got_error_from_errno("closedir");
1155 return err;
1158 static const struct got_error *
1159 read_packfile_hdr(int fd, struct got_packidx *packidx)
1161 const struct got_error *err = NULL;
1162 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1163 struct got_packfile_hdr hdr;
1164 ssize_t n;
1166 n = read(fd, &hdr, sizeof(hdr));
1167 if (n < 0)
1168 return got_error_from_errno("read");
1169 if (n != sizeof(hdr))
1170 return got_error(GOT_ERR_BAD_PACKFILE);
1172 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1173 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1174 be32toh(hdr.nobjects) != totobj)
1175 err = got_error(GOT_ERR_BAD_PACKFILE);
1177 return err;
1180 static const struct got_error *
1181 open_packfile(int *fd, struct got_repository *repo,
1182 const char *relpath, struct got_packidx *packidx)
1184 const struct got_error *err = NULL;
1186 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1187 if (*fd == -1)
1188 return got_error_from_errno_fmt("openat: %s/%s",
1189 got_repo_get_path_git_dir(repo), relpath);
1191 if (packidx) {
1192 err = read_packfile_hdr(*fd, packidx);
1193 if (err) {
1194 close(*fd);
1195 *fd = -1;
1199 return err;
1202 const struct got_error *
1203 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1204 const char *path_packfile, struct got_packidx *packidx)
1206 const struct got_error *err = NULL;
1207 struct got_pack *pack = NULL;
1208 struct stat sb;
1209 size_t i;
1211 if (packp)
1212 *packp = NULL;
1214 for (i = 0; i < repo->pack_cache_size; i++) {
1215 pack = &repo->packs[i];
1216 if (pack->path_packfile == NULL)
1217 break;
1218 if (strcmp(pack->path_packfile, path_packfile) == 0)
1219 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1222 if (i == repo->pack_cache_size) {
1223 err = got_pack_close(&repo->packs[i - 1]);
1224 if (err)
1225 return err;
1226 memmove(&repo->packs[1], &repo->packs[0],
1227 sizeof(repo->packs) - sizeof(repo->packs[0]));
1228 i = 0;
1231 pack = &repo->packs[i];
1233 pack->path_packfile = strdup(path_packfile);
1234 if (pack->path_packfile == NULL) {
1235 err = got_error_from_errno("strdup");
1236 goto done;
1239 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1240 if (err)
1241 goto done;
1243 if (fstat(pack->fd, &sb) != 0) {
1244 err = got_error_from_errno("fstat");
1245 goto done;
1247 pack->filesize = sb.st_size;
1249 pack->privsep_child = NULL;
1251 #ifndef GOT_PACK_NO_MMAP
1252 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1253 pack->fd, 0);
1254 if (pack->map == MAP_FAILED) {
1255 if (errno != ENOMEM) {
1256 err = got_error_from_errno("mmap");
1257 goto done;
1259 pack->map = NULL; /* fall back to read(2) */
1261 #endif
1262 done:
1263 if (err) {
1264 if (pack) {
1265 free(pack->path_packfile);
1266 memset(pack, 0, sizeof(*pack));
1268 } else if (packp)
1269 *packp = pack;
1270 return err;
1273 struct got_pack *
1274 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1276 struct got_pack *pack = NULL;
1277 size_t i;
1279 for (i = 0; i < repo->pack_cache_size; i++) {
1280 pack = &repo->packs[i];
1281 if (pack->path_packfile == NULL)
1282 break;
1283 if (strcmp(pack->path_packfile, path_packfile) == 0)
1284 return pack;
1287 return NULL;
1290 const struct got_error *
1291 got_repo_init(const char *repo_path)
1293 const struct got_error *err = NULL;
1294 const char *dirnames[] = {
1295 GOT_OBJECTS_DIR,
1296 GOT_OBJECTS_PACK_DIR,
1297 GOT_REFS_DIR,
1299 const char *description_str = "Unnamed repository; "
1300 "edit this file 'description' to name the repository.";
1301 const char *headref_str = "ref: refs/heads/main";
1302 const char *gitconfig_str = "[core]\n"
1303 "\trepositoryformatversion = 0\n"
1304 "\tfilemode = true\n"
1305 "\tbare = true\n";
1306 char *path;
1307 size_t i;
1309 if (!got_path_dir_is_empty(repo_path))
1310 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1312 for (i = 0; i < nitems(dirnames); i++) {
1313 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1314 return got_error_from_errno("asprintf");
1316 err = got_path_mkdir(path);
1317 free(path);
1318 if (err)
1319 return err;
1322 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1323 return got_error_from_errno("asprintf");
1324 err = got_path_create_file(path, description_str);
1325 free(path);
1326 if (err)
1327 return err;
1329 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1330 return got_error_from_errno("asprintf");
1331 err = got_path_create_file(path, headref_str);
1332 free(path);
1333 if (err)
1334 return err;
1336 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1337 return got_error_from_errno("asprintf");
1338 err = got_path_create_file(path, gitconfig_str);
1339 free(path);
1340 if (err)
1341 return err;
1343 return NULL;
1346 static const struct got_error *
1347 match_packed_object(struct got_object_id **unique_id,
1348 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1350 const struct got_error *err = NULL;
1351 DIR *packdir = NULL;
1352 struct dirent *dent;
1353 char *path_packidx;
1354 struct got_object_id_queue matched_ids;
1355 int packdir_fd;
1357 STAILQ_INIT(&matched_ids);
1359 packdir_fd = openat(got_repo_get_fd(repo),
1360 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1361 if (packdir_fd == -1) {
1362 if (errno != ENOENT)
1363 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1364 goto done;
1367 packdir = fdopendir(packdir_fd);
1368 if (packdir == NULL) {
1369 err = got_error_from_errno("fdopendir");
1370 goto done;
1373 while ((dent = readdir(packdir)) != NULL) {
1374 struct got_packidx *packidx;
1375 struct got_object_qid *qid;
1377 if (!got_repo_is_packidx_filename(dent->d_name,
1378 strlen(dent->d_name)))
1379 continue;
1381 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1382 dent->d_name) == -1) {
1383 err = got_error_from_errno("strdup");
1384 break;
1387 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1388 path_packidx, 0);
1389 free(path_packidx);
1390 if (err)
1391 break;
1393 err = got_packidx_match_id_str_prefix(&matched_ids,
1394 packidx, id_str_prefix);
1395 if (err) {
1396 got_packidx_close(packidx);
1397 break;
1399 err = got_packidx_close(packidx);
1400 if (err)
1401 break;
1403 STAILQ_FOREACH(qid, &matched_ids, entry) {
1404 if (obj_type != GOT_OBJ_TYPE_ANY) {
1405 int matched_type;
1406 err = got_object_get_type(&matched_type, repo,
1407 qid->id);
1408 if (err)
1409 goto done;
1410 if (matched_type != obj_type)
1411 continue;
1413 if (*unique_id == NULL) {
1414 *unique_id = got_object_id_dup(qid->id);
1415 if (*unique_id == NULL) {
1416 err = got_error_from_errno("malloc");
1417 goto done;
1419 } else {
1420 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1421 continue; /* packed multiple times */
1422 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1423 goto done;
1427 done:
1428 got_object_id_queue_free(&matched_ids);
1429 if (packdir && closedir(packdir) != 0 && err == NULL)
1430 err = got_error_from_errno("closedir");
1431 if (err) {
1432 free(*unique_id);
1433 *unique_id = NULL;
1435 return err;
1438 static const struct got_error *
1439 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1440 const char *object_dir, const char *id_str_prefix, int obj_type,
1441 struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 char *path;
1445 DIR *dir = NULL;
1446 struct dirent *dent;
1447 struct got_object_id id;
1449 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1450 err = got_error_from_errno("asprintf");
1451 goto done;
1454 dir = opendir(path);
1455 if (dir == NULL) {
1456 if (errno == ENOENT) {
1457 err = NULL;
1458 goto done;
1460 err = got_error_from_errno2("opendir", path);
1461 goto done;
1463 while ((dent = readdir(dir)) != NULL) {
1464 char *id_str;
1465 int cmp;
1467 if (strcmp(dent->d_name, ".") == 0 ||
1468 strcmp(dent->d_name, "..") == 0)
1469 continue;
1471 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1472 err = got_error_from_errno("asprintf");
1473 goto done;
1476 if (!got_parse_sha1_digest(id.sha1, id_str))
1477 continue;
1480 * Directory entries do not necessarily appear in
1481 * sorted order, so we must iterate over all of them.
1483 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1484 if (cmp != 0) {
1485 free(id_str);
1486 continue;
1489 if (*unique_id == NULL) {
1490 if (obj_type != GOT_OBJ_TYPE_ANY) {
1491 int matched_type;
1492 err = got_object_get_type(&matched_type, repo,
1493 &id);
1494 if (err)
1495 goto done;
1496 if (matched_type != obj_type)
1497 continue;
1499 *unique_id = got_object_id_dup(&id);
1500 if (*unique_id == NULL) {
1501 err = got_error_from_errno("got_object_id_dup");
1502 free(id_str);
1503 goto done;
1505 } else {
1506 if (got_object_id_cmp(*unique_id, &id) == 0)
1507 continue; /* both packed and loose */
1508 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1509 free(id_str);
1510 goto done;
1513 done:
1514 if (dir && closedir(dir) != 0 && err == NULL)
1515 err = got_error_from_errno("closedir");
1516 if (err) {
1517 free(*unique_id);
1518 *unique_id = NULL;
1520 free(path);
1521 return err;
1524 const struct got_error *
1525 got_repo_match_object_id_prefix(struct got_object_id **id,
1526 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1528 const struct got_error *err = NULL;
1529 char *path_objects = got_repo_get_path_objects(repo);
1530 char *object_dir = NULL;
1531 size_t len;
1532 int i;
1534 *id = NULL;
1536 for (i = 0; i < strlen(id_str_prefix); i++) {
1537 if (isxdigit((unsigned char)id_str_prefix[i]))
1538 continue;
1539 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1542 len = strlen(id_str_prefix);
1543 if (len >= 2) {
1544 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1545 if (err)
1546 goto done;
1547 object_dir = strndup(id_str_prefix, 2);
1548 if (object_dir == NULL) {
1549 err = got_error_from_errno("strdup");
1550 goto done;
1552 err = match_loose_object(id, path_objects, object_dir,
1553 id_str_prefix, obj_type, repo);
1554 } else if (len == 1) {
1555 int i;
1556 for (i = 0; i < 0xf; i++) {
1557 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1558 == -1) {
1559 err = got_error_from_errno("asprintf");
1560 goto done;
1562 err = match_packed_object(id, repo, object_dir,
1563 obj_type);
1564 if (err)
1565 goto done;
1566 err = match_loose_object(id, path_objects, object_dir,
1567 id_str_prefix, obj_type, repo);
1568 if (err)
1569 goto done;
1571 } else {
1572 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1573 goto done;
1575 done:
1576 free(object_dir);
1577 if (err) {
1578 free(*id);
1579 *id = NULL;
1580 } else if (*id == NULL) {
1581 switch (obj_type) {
1582 case GOT_OBJ_TYPE_BLOB:
1583 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1584 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1585 break;
1586 case GOT_OBJ_TYPE_TREE:
1587 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1588 GOT_OBJ_LABEL_TREE, id_str_prefix);
1589 break;
1590 case GOT_OBJ_TYPE_COMMIT:
1591 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1592 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1593 break;
1594 case GOT_OBJ_TYPE_TAG:
1595 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1596 GOT_OBJ_LABEL_TAG, id_str_prefix);
1597 break;
1598 default:
1599 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1600 break;
1604 return err;
1607 const struct got_error *
1608 got_repo_match_object_id(struct got_object_id **id, char **label,
1609 const char *id_str, int obj_type, struct got_reflist_head *refs,
1610 struct got_repository *repo)
1612 const struct got_error *err;
1613 struct got_tag_object *tag;
1614 struct got_reference *ref = NULL;
1616 *id = NULL;
1617 if (label)
1618 *label = NULL;
1620 if (refs) {
1621 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1622 refs, repo);
1623 if (err == NULL) {
1624 *id = got_object_id_dup(
1625 got_object_tag_get_object_id(tag));
1626 if (*id == NULL)
1627 err = got_error_from_errno("got_object_id_dup");
1628 else if (label && asprintf(label, "refs/tags/%s",
1629 got_object_tag_get_name(tag)) == -1) {
1630 err = got_error_from_errno("asprintf");
1631 free(*id);
1632 *id = NULL;
1634 got_object_tag_close(tag);
1635 return err;
1636 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1637 err->code != GOT_ERR_NO_OBJ)
1638 return err;
1641 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1642 if (err) {
1643 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1644 return err;
1645 err = got_ref_open(&ref, repo, id_str, 0);
1646 if (err != NULL)
1647 goto done;
1648 if (label) {
1649 *label = strdup(got_ref_get_name(ref));
1650 if (*label == NULL) {
1651 err = got_error_from_errno("strdup");
1652 goto done;
1655 err = got_ref_resolve(id, repo, ref);
1656 } else if (label) {
1657 err = got_object_id_str(label, *id);
1658 if (*label == NULL) {
1659 err = got_error_from_errno("strdup");
1660 goto done;
1663 done:
1664 if (ref)
1665 got_ref_close(ref);
1666 return err;
1669 const struct got_error *
1670 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1671 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 struct got_reflist_entry *re;
1675 struct got_object_id *tag_id;
1676 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1678 *tag = NULL;
1680 TAILQ_FOREACH(re, refs, entry) {
1681 const char *refname;
1682 refname = got_ref_get_name(re->ref);
1683 if (got_ref_is_symbolic(re->ref))
1684 continue;
1685 if (strncmp(refname, "refs/tags/", 10) != 0)
1686 continue;
1687 if (!name_is_absolute)
1688 refname += strlen("refs/tags/");
1689 if (strcmp(refname, name) != 0)
1690 continue;
1691 err = got_ref_resolve(&tag_id, repo, re->ref);
1692 if (err)
1693 break;
1694 err = got_object_open_as_tag(tag, repo, tag_id);
1695 free(tag_id);
1696 if (err)
1697 break;
1698 if (obj_type == GOT_OBJ_TYPE_ANY ||
1699 got_object_tag_get_object_type(*tag) == obj_type)
1700 break;
1701 got_object_tag_close(*tag);
1702 *tag = NULL;
1705 if (err == NULL && *tag == NULL)
1706 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1707 GOT_OBJ_LABEL_TAG, name);
1708 return err;
1711 static const struct got_error *
1712 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1713 const char *name, mode_t mode, struct got_object_id *blob_id)
1715 const struct got_error *err = NULL;
1717 *new_te = NULL;
1719 *new_te = calloc(1, sizeof(**new_te));
1720 if (*new_te == NULL)
1721 return got_error_from_errno("calloc");
1723 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1724 sizeof((*new_te)->name)) {
1725 err = got_error(GOT_ERR_NO_SPACE);
1726 goto done;
1729 if (S_ISLNK(mode)) {
1730 (*new_te)->mode = S_IFLNK;
1731 } else {
1732 (*new_te)->mode = S_IFREG;
1733 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1735 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1736 done:
1737 if (err && *new_te) {
1738 free(*new_te);
1739 *new_te = NULL;
1741 return err;
1744 static const struct got_error *
1745 import_file(struct got_tree_entry **new_te, struct dirent *de,
1746 const char *path, struct got_repository *repo)
1748 const struct got_error *err;
1749 struct got_object_id *blob_id = NULL;
1750 char *filepath;
1751 struct stat sb;
1753 if (asprintf(&filepath, "%s%s%s", path,
1754 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1755 return got_error_from_errno("asprintf");
1757 if (lstat(filepath, &sb) != 0) {
1758 err = got_error_from_errno2("lstat", path);
1759 goto done;
1762 err = got_object_blob_create(&blob_id, filepath, repo);
1763 if (err)
1764 goto done;
1766 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1767 blob_id);
1768 done:
1769 free(filepath);
1770 if (err)
1771 free(blob_id);
1772 return err;
1775 static const struct got_error *
1776 insert_tree_entry(struct got_tree_entry *new_te,
1777 struct got_pathlist_head *paths)
1779 const struct got_error *err = NULL;
1780 struct got_pathlist_entry *new_pe;
1782 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1783 if (err)
1784 return err;
1785 if (new_pe == NULL)
1786 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1787 return NULL;
1790 static const struct got_error *write_tree(struct got_object_id **,
1791 const char *, struct got_pathlist_head *, struct got_repository *,
1792 got_repo_import_cb progress_cb, void *progress_arg);
1794 static const struct got_error *
1795 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1796 const char *path, struct got_pathlist_head *ignores,
1797 struct got_repository *repo,
1798 got_repo_import_cb progress_cb, void *progress_arg)
1800 const struct got_error *err;
1801 struct got_object_id *id = NULL;
1802 char *subdirpath;
1804 if (asprintf(&subdirpath, "%s%s%s", path,
1805 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1806 return got_error_from_errno("asprintf");
1808 (*new_te) = calloc(1, sizeof(**new_te));
1809 if (*new_te == NULL)
1810 return got_error_from_errno("calloc");
1811 (*new_te)->mode = S_IFDIR;
1812 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1813 sizeof((*new_te)->name)) {
1814 err = got_error(GOT_ERR_NO_SPACE);
1815 goto done;
1817 err = write_tree(&id, subdirpath, ignores, repo,
1818 progress_cb, progress_arg);
1819 if (err)
1820 goto done;
1821 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1823 done:
1824 free(id);
1825 free(subdirpath);
1826 if (err) {
1827 free(*new_te);
1828 *new_te = NULL;
1830 return err;
1833 static const struct got_error *
1834 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1835 struct got_pathlist_head *ignores, struct got_repository *repo,
1836 got_repo_import_cb progress_cb, void *progress_arg)
1838 const struct got_error *err = NULL;
1839 DIR *dir;
1840 struct dirent *de;
1841 int nentries;
1842 struct got_tree_entry *new_te = NULL;
1843 struct got_pathlist_head paths;
1844 struct got_pathlist_entry *pe;
1846 *new_tree_id = NULL;
1848 TAILQ_INIT(&paths);
1850 dir = opendir(path_dir);
1851 if (dir == NULL) {
1852 err = got_error_from_errno2("opendir", path_dir);
1853 goto done;
1856 nentries = 0;
1857 while ((de = readdir(dir)) != NULL) {
1858 int ignore = 0;
1859 int type;
1861 if (strcmp(de->d_name, ".") == 0 ||
1862 strcmp(de->d_name, "..") == 0)
1863 continue;
1865 TAILQ_FOREACH(pe, ignores, entry) {
1866 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1867 ignore = 1;
1868 break;
1871 if (ignore)
1872 continue;
1874 err = got_path_dirent_type(&type, path_dir, de);
1875 if (err)
1876 goto done;
1878 if (type == DT_DIR) {
1879 err = import_subdir(&new_te, de, path_dir,
1880 ignores, repo, progress_cb, progress_arg);
1881 if (err) {
1882 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1883 goto done;
1884 err = NULL;
1885 continue;
1887 } else if (type == DT_REG || type == DT_LNK) {
1888 err = import_file(&new_te, de, path_dir, repo);
1889 if (err)
1890 goto done;
1891 } else
1892 continue;
1894 err = insert_tree_entry(new_te, &paths);
1895 if (err)
1896 goto done;
1897 nentries++;
1900 if (TAILQ_EMPTY(&paths)) {
1901 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1902 "cannot create tree without any entries");
1903 goto done;
1906 TAILQ_FOREACH(pe, &paths, entry) {
1907 struct got_tree_entry *te = pe->data;
1908 char *path;
1909 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1910 continue;
1911 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1912 err = got_error_from_errno("asprintf");
1913 goto done;
1915 err = (*progress_cb)(progress_arg, path);
1916 free(path);
1917 if (err)
1918 goto done;
1921 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1922 done:
1923 if (dir)
1924 closedir(dir);
1925 got_pathlist_free(&paths);
1926 return err;
1929 const struct got_error *
1930 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1931 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1932 struct got_repository *repo, got_repo_import_cb progress_cb,
1933 void *progress_arg)
1935 const struct got_error *err;
1936 struct got_object_id *new_tree_id;
1938 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1939 progress_cb, progress_arg);
1940 if (err)
1941 return err;
1943 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1944 author, time(NULL), author, time(NULL), logmsg, repo);
1945 free(new_tree_id);
1946 return err;
1949 const struct got_error *
1950 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1951 struct got_repository *repo)
1953 const struct got_error *err = NULL;
1954 char *path_objects = NULL, *path = NULL;
1955 DIR *dir = NULL;
1956 struct got_object_id id;
1957 int i;
1959 *nobjects = 0;
1960 *ondisk_size = 0;
1962 path_objects = got_repo_get_path_objects(repo);
1963 if (path_objects == NULL)
1964 return got_error_from_errno("got_repo_get_path_objects");
1966 for (i = 0; i <= 0xff; i++) {
1967 struct dirent *dent;
1969 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1970 err = got_error_from_errno("asprintf");
1971 break;
1974 dir = opendir(path);
1975 if (dir == NULL) {
1976 if (errno == ENOENT) {
1977 err = NULL;
1978 continue;
1980 err = got_error_from_errno2("opendir", path);
1981 break;
1984 while ((dent = readdir(dir)) != NULL) {
1985 char *id_str;
1986 int fd;
1987 struct stat sb;
1989 if (strcmp(dent->d_name, ".") == 0 ||
1990 strcmp(dent->d_name, "..") == 0)
1991 continue;
1993 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1994 err = got_error_from_errno("asprintf");
1995 goto done;
1998 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1999 free(id_str);
2000 continue;
2002 free(id_str);
2004 err = got_object_open_loose_fd(&fd, &id, repo);
2005 if (err)
2006 goto done;
2008 if (fstat(fd, &sb) == -1) {
2009 err = got_error_from_errno("fstat");
2010 close(fd);
2011 goto done;
2013 (*nobjects)++;
2014 (*ondisk_size) += sb.st_size;
2016 if (close(fd) == -1) {
2017 err = got_error_from_errno("close");
2018 goto done;
2022 if (closedir(dir) != 0) {
2023 err = got_error_from_errno("closedir");
2024 goto done;
2026 dir = NULL;
2028 free(path);
2029 path = NULL;
2031 done:
2032 if (dir && closedir(dir) != 0 && err == NULL)
2033 err = got_error_from_errno("closedir");
2035 if (err) {
2036 *nobjects = 0;
2037 *ondisk_size = 0;
2039 free(path_objects);
2040 free(path);
2041 return err;
2044 const struct got_error *
2045 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2046 off_t *total_packsize, struct got_repository *repo)
2048 const struct got_error *err = NULL;
2049 DIR *packdir = NULL;
2050 struct dirent *dent;
2051 struct got_packidx *packidx = NULL;
2052 char *path_packidx;
2053 char *path_packfile;
2054 int packdir_fd;
2055 struct stat sb;
2057 *npackfiles = 0;
2058 *nobjects = 0;
2059 *total_packsize = 0;
2061 packdir_fd = openat(got_repo_get_fd(repo),
2062 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2063 if (packdir_fd == -1) {
2064 return got_error_from_errno_fmt("openat: %s/%s",
2065 got_repo_get_path_git_dir(repo),
2066 GOT_OBJECTS_PACK_DIR);
2069 packdir = fdopendir(packdir_fd);
2070 if (packdir == NULL) {
2071 err = got_error_from_errno("fdopendir");
2072 goto done;
2075 while ((dent = readdir(packdir)) != NULL) {
2076 if (!got_repo_is_packidx_filename(dent->d_name,
2077 strlen(dent->d_name)))
2078 continue;
2080 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2081 dent->d_name) == -1) {
2082 err = got_error_from_errno("asprintf");
2083 goto done;
2086 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2087 path_packidx, 0);
2088 free(path_packidx);
2089 if (err)
2090 goto done;
2092 if (fstat(packidx->fd, &sb) == -1)
2093 goto done;
2094 *total_packsize += sb.st_size;
2096 err = got_packidx_get_packfile_path(&path_packfile,
2097 packidx->path_packidx);
2098 if (err)
2099 goto done;
2101 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2102 0) == -1) {
2103 free(path_packfile);
2104 goto done;
2106 free(path_packfile);
2107 *total_packsize += sb.st_size;
2109 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2111 (*npackfiles)++;
2113 got_packidx_close(packidx);
2114 packidx = NULL;
2116 done:
2117 if (packidx)
2118 got_packidx_close(packidx);
2119 if (packdir && closedir(packdir) != 0 && err == NULL)
2120 err = got_error_from_errno("closedir");
2121 if (err) {
2122 *npackfiles = 0;
2123 *nobjects = 0;
2124 *total_packsize = 0;
2126 return err;