Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <uuid.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 int
81 got_repo_get_fd(struct got_repository *repo)
82 {
83 return repo->gitdir_fd;
84 }
86 const char *
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_name;
90 }
92 const char *
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_email;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
104 const char *
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
110 const char *
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
116 void
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
124 int
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 static char *
131 get_path_git_child(struct got_repository *repo, const char *basename)
133 char *path_child;
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
136 basename) == -1)
137 return NULL;
139 return path_child;
142 char *
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 char *
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 char *
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
160 char *
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 static char *
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
172 char *
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
178 char *
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
190 void
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
198 static int
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
205 int ret = 0;
206 struct stat sb;
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
210 goto done;
211 if (!S_ISDIR(sb.st_mode))
212 goto done;
214 if (lstat(path_objects, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_refs, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_head, &sb) == -1)
225 goto done;
226 if (!S_ISREG(sb.st_mode))
227 goto done;
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 goto done;
232 got_ref_close(head_ref);
234 ret = 1;
235 done:
236 free(path_objects);
237 free(path_refs);
238 free(path_head);
239 return ret;
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err) {
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 err = NULL;
254 return err;
256 obj->refcnt++;
257 #endif
258 return NULL;
261 struct got_object *
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err) {
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
278 err = NULL;
279 return err;
281 tree->refcnt++;
282 #endif
283 return NULL;
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 commit->refcnt++;
308 #endif
309 return NULL;
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err) {
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
330 err = NULL;
331 return err;
333 tag->refcnt++;
334 #endif
335 return NULL;
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
352 if (err) {
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
355 err = NULL;
356 return err;
358 raw->refcnt++;
359 #endif
360 return NULL;
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
389 repo->gitdir_fd = open(repo->path_git_dir, 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 /* git repository with working tree? */
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
402 err = got_error_from_errno("asprintf");
403 goto done;
405 if (is_git_repo(repo)) {
406 repo->path = strdup(path);
407 if (repo->path == NULL) {
408 err = got_error_from_errno("strdup");
409 goto done;
411 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
412 if (repo->gitdir_fd == -1) {
413 err = got_error_from_errno2("open",
414 repo->path_git_dir);
415 goto done;
417 return NULL;
420 err = got_error(GOT_ERR_NOT_GIT_REPO);
421 done:
422 if (err) {
423 free(repo->path);
424 repo->path = NULL;
425 free(repo->path_git_dir);
426 repo->path_git_dir = NULL;
427 if (repo->gitdir_fd != -1)
428 close(repo->gitdir_fd);
429 repo->gitdir_fd = -1;
432 return err;
435 static const struct got_error *
436 parse_gitconfig_file(int *gitconfig_repository_format_version,
437 char **gitconfig_author_name, char **gitconfig_author_email,
438 struct got_remote_repo **remotes, int *nremotes,
439 char **gitconfig_owner, char ***extensions, int *nextensions,
440 const char *gitconfig_path)
442 const struct got_error *err = NULL, *child_err = NULL;
443 int fd = -1;
444 int imsg_fds[2] = { -1, -1 };
445 pid_t pid;
446 struct imsgbuf *ibuf;
448 *gitconfig_repository_format_version = 0;
449 if (extensions)
450 *extensions = NULL;
451 if (nextensions)
452 *nextensions = 0;
453 *gitconfig_author_name = NULL;
454 *gitconfig_author_email = NULL;
455 if (remotes)
456 *remotes = NULL;
457 if (nremotes)
458 *nremotes = 0;
459 if (gitconfig_owner)
460 *gitconfig_owner = NULL;
462 fd = open(gitconfig_path, O_RDONLY);
463 if (fd == -1) {
464 if (errno == ENOENT)
465 return NULL;
466 return got_error_from_errno2("open", gitconfig_path);
469 ibuf = calloc(1, sizeof(*ibuf));
470 if (ibuf == NULL) {
471 err = got_error_from_errno("calloc");
472 goto done;
475 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
476 err = got_error_from_errno("socketpair");
477 goto done;
480 pid = fork();
481 if (pid == -1) {
482 err = got_error_from_errno("fork");
483 goto done;
484 } else if (pid == 0) {
485 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
486 gitconfig_path);
487 /* not reached */
490 if (close(imsg_fds[1]) == -1) {
491 err = got_error_from_errno("close");
492 goto done;
494 imsg_fds[1] = -1;
495 imsg_init(ibuf, imsg_fds[0]);
497 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
498 if (err)
499 goto done;
500 fd = -1;
502 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
503 if (err)
504 goto done;
506 err = got_privsep_recv_gitconfig_int(
507 gitconfig_repository_format_version, ibuf);
508 if (err)
509 goto done;
511 if (extensions && nextensions) {
512 err = got_privsep_send_gitconfig_repository_extensions_req(
513 ibuf);
514 if (err)
515 goto done;
516 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
517 if (err)
518 goto done;
519 if (*nextensions > 0) {
520 int i;
521 *extensions = calloc(*nextensions, sizeof(char *));
522 if (*extensions == NULL) {
523 err = got_error_from_errno("calloc");
524 goto done;
526 for (i = 0; i < *nextensions; i++) {
527 char *ext;
528 err = got_privsep_recv_gitconfig_str(&ext,
529 ibuf);
530 if (err)
531 goto done;
532 (*extensions)[i] = ext;
537 err = got_privsep_send_gitconfig_author_name_req(ibuf);
538 if (err)
539 goto done;
541 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
542 if (err)
543 goto done;
545 err = got_privsep_send_gitconfig_author_email_req(ibuf);
546 if (err)
547 goto done;
549 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
550 if (err)
551 goto done;
553 if (remotes && nremotes) {
554 err = got_privsep_send_gitconfig_remotes_req(ibuf);
555 if (err)
556 goto done;
558 err = got_privsep_recv_gitconfig_remotes(remotes,
559 nremotes, ibuf);
560 if (err)
561 goto done;
564 if (gitconfig_owner) {
565 err = got_privsep_send_gitconfig_owner_req(ibuf);
566 if (err)
567 goto done;
568 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
569 if (err)
570 goto done;
573 imsg_clear(ibuf);
574 err = got_privsep_send_stop(imsg_fds[0]);
575 child_err = got_privsep_wait_for_child(pid);
576 if (child_err && err == NULL)
577 err = child_err;
578 done:
579 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
580 err = got_error_from_errno("close");
581 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (fd != -1 && close(fd) == -1 && err == NULL)
584 err = got_error_from_errno2("close", gitconfig_path);
585 free(ibuf);
586 return err;
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = parse_gitconfig_file(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
602 if (err)
603 return err;
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
612 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
613 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
614 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
615 repo_gitconfig_path);
616 if (err)
617 goto done;
618 done:
619 free(repo_gitconfig_path);
620 return err;
623 static const struct got_error *
624 read_gotconfig(struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *gotconfig_path;
629 gotconfig_path = got_repo_get_path_gotconfig(repo);
630 if (gotconfig_path == NULL)
631 return got_error_from_errno("got_repo_get_path_gotconfig");
633 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
634 free(gotconfig_path);
635 return err;
638 /* Supported repository format extensions. */
639 static const char *repo_extensions[] = {
640 "noop", /* Got supports repository format version 1. */
641 "preciousObjects", /* Supported by gotadmin cleanup. */
642 "worktreeConfig", /* Got does not care about Git work trees. */
643 };
645 const struct got_error *
646 got_repo_open(struct got_repository **repop, const char *path,
647 const char *global_gitconfig_path)
649 struct got_repository *repo = NULL;
650 const struct got_error *err = NULL;
651 char *repo_path = NULL;
652 size_t i;
653 struct rlimit rl;
655 *repop = NULL;
657 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
658 return got_error_from_errno("getrlimit");
660 repo = calloc(1, sizeof(*repo));
661 if (repo == NULL) {
662 err = got_error_from_errno("calloc");
663 goto done;
666 RB_INIT(&repo->packidx_bloom_filters);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
676 if (err)
677 goto done;
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
680 if (err)
681 goto done;
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
684 if (err)
685 goto done;
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
688 if (err)
689 goto done;
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
692 if (err)
693 goto done;
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
699 repo_path = realpath(path, NULL);
700 if (repo_path == NULL) {
701 err = got_error_from_errno2("realpath", path);
702 goto done;
705 for (;;) {
706 char *parent_path;
708 err = open_repo(repo, repo_path);
709 if (err == NULL)
710 break;
711 if (err->code != GOT_ERR_NOT_GIT_REPO)
712 goto done;
713 if (repo_path[0] == '/' && repo_path[1] == '\0') {
714 err = got_error(GOT_ERR_NOT_GIT_REPO);
715 goto done;
717 err = got_path_dirname(&parent_path, repo_path);
718 if (err)
719 goto done;
720 free(repo_path);
721 repo_path = parent_path;
724 err = read_gotconfig(repo);
725 if (err)
726 goto done;
728 err = read_gitconfig(repo, global_gitconfig_path);
729 if (err)
730 goto done;
731 if (repo->gitconfig_repository_format_version != 0)
732 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
733 for (i = 0; i < repo->nextensions; i++) {
734 char *ext = repo->extensions[i];
735 int j, supported = 0;
736 for (j = 0; j < nitems(repo_extensions); j++) {
737 if (strcmp(ext, repo_extensions[j]) == 0) {
738 supported = 1;
739 break;
742 if (!supported) {
743 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
744 goto done;
747 done:
748 if (err)
749 got_repo_close(repo);
750 else
751 *repop = repo;
752 free(repo_path);
753 return err;
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
761 size_t i;
763 for (i = 0; i < repo->pack_cache_size; i++) {
764 if (repo->packidx_cache[i] == NULL)
765 break;
766 got_packidx_close(repo->packidx_cache[i]);
769 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
770 &repo->packidx_bloom_filters))) {
771 RB_REMOVE(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters, bf);
773 free(bf->bloom);
774 free(bf);
777 for (i = 0; i < repo->pack_cache_size; i++) {
778 if (repo->packs[i].path_packfile == NULL)
779 break;
780 got_pack_close(&repo->packs[i]);
783 free(repo->path);
784 free(repo->path_git_dir);
786 got_object_cache_close(&repo->objcache);
787 got_object_cache_close(&repo->treecache);
788 got_object_cache_close(&repo->commitcache);
789 got_object_cache_close(&repo->tagcache);
790 got_object_cache_close(&repo->rawcache);
792 for (i = 0; i < nitems(repo->privsep_children); i++) {
793 if (repo->privsep_children[i].imsg_fd == -1)
794 continue;
795 imsg_clear(repo->privsep_children[i].ibuf);
796 free(repo->privsep_children[i].ibuf);
797 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
798 child_err = got_privsep_wait_for_child(
799 repo->privsep_children[i].pid);
800 if (child_err && err == NULL)
801 err = child_err;
802 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
803 err == NULL)
804 err = got_error_from_errno("close");
807 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
808 err == NULL)
809 err = got_error_from_errno("close");
811 if (repo->gotconfig)
812 got_gotconfig_free(repo->gotconfig);
813 free(repo->gitconfig_author_name);
814 free(repo->gitconfig_author_email);
815 for (i = 0; i < repo->ngitconfig_remotes; i++)
816 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
817 free(repo->gitconfig_remotes);
818 for (i = 0; i < repo->nextensions; i++)
819 free(repo->extensions[i]);
820 free(repo->extensions);
821 free(repo);
823 return err;
826 void
827 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
829 int i;
831 free(repo->name);
832 repo->name = NULL;
833 free(repo->fetch_url);
834 repo->fetch_url = NULL;
835 free(repo->send_url);
836 repo->send_url = NULL;
837 for (i = 0; i < repo->nfetch_branches; i++)
838 free(repo->fetch_branches[i]);
839 free(repo->fetch_branches);
840 repo->fetch_branches = NULL;
841 repo->nfetch_branches = 0;
842 for (i = 0; i < repo->nsend_branches; i++)
843 free(repo->send_branches[i]);
844 free(repo->send_branches);
845 repo->send_branches = NULL;
846 repo->nsend_branches = 0;
849 const struct got_error *
850 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
851 const char *input_path)
853 const struct got_error *err = NULL;
854 const char *repo_abspath = NULL;
855 size_t repolen, len;
856 char *canonpath, *path = NULL;
858 *in_repo_path = NULL;
860 canonpath = strdup(input_path);
861 if (canonpath == NULL) {
862 err = got_error_from_errno("strdup");
863 goto done;
865 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
866 if (err)
867 goto done;
869 repo_abspath = got_repo_get_path(repo);
871 if (canonpath[0] == '\0') {
872 path = strdup(canonpath);
873 if (path == NULL) {
874 err = got_error_from_errno("strdup");
875 goto done;
877 } else {
878 path = realpath(canonpath, NULL);
879 if (path == NULL) {
880 if (errno != ENOENT) {
881 err = got_error_from_errno2("realpath",
882 canonpath);
883 goto done;
885 /*
886 * Path is not on disk.
887 * Assume it is already relative to repository root.
888 */
889 path = strdup(canonpath);
890 if (path == NULL) {
891 err = got_error_from_errno("strdup");
892 goto done;
896 repolen = strlen(repo_abspath);
897 len = strlen(path);
900 if (strcmp(path, repo_abspath) == 0) {
901 free(path);
902 path = strdup("");
903 if (path == NULL) {
904 err = got_error_from_errno("strdup");
905 goto done;
907 } else if (len > repolen &&
908 got_path_is_child(path, repo_abspath, repolen)) {
909 /* Matched an on-disk path inside repository. */
910 if (got_repo_is_bare(repo)) {
911 /*
912 * Matched an on-disk path inside repository
913 * database. Treat input as repository-relative.
914 */
915 free(path);
916 path = canonpath;
917 canonpath = NULL;
918 } else {
919 char *child;
920 /* Strip common prefix with repository path. */
921 err = got_path_skip_common_ancestor(&child,
922 repo_abspath, path);
923 if (err)
924 goto done;
925 free(path);
926 path = child;
928 } else {
929 /*
930 * Matched unrelated on-disk path.
931 * Treat input as repository-relative.
932 */
933 free(path);
934 path = canonpath;
935 canonpath = NULL;
939 /* Make in-repository path absolute */
940 if (path[0] != '/') {
941 char *abspath;
942 if (asprintf(&abspath, "/%s", path) == -1) {
943 err = got_error_from_errno("asprintf");
944 goto done;
946 free(path);
947 path = abspath;
950 done:
951 free(canonpath);
952 if (err)
953 free(path);
954 else
955 *in_repo_path = path;
956 return err;
959 static const struct got_error *
960 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
961 const char *path_packidx)
963 const struct got_error *err = NULL;
964 size_t i;
966 for (i = 0; i < repo->pack_cache_size; i++) {
967 if (repo->packidx_cache[i] == NULL)
968 break;
969 if (strcmp(repo->packidx_cache[i]->path_packidx,
970 path_packidx) == 0) {
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == repo->pack_cache_size) {
975 i = repo->pack_cache_size - 1;
976 err = got_packidx_close(repo->packidx_cache[i]);
977 if (err)
978 return err;
981 repo->packidx_cache[i] = packidx;
983 return NULL;
986 int
987 got_repo_is_packidx_filename(const char *name, size_t len)
989 if (len != GOT_PACKIDX_NAMELEN)
990 return 0;
992 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
993 return 0;
995 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
996 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
997 return 0;
999 return 1;
1002 static struct got_packidx_bloom_filter *
1003 get_packidx_bloom_filter(struct got_repository *repo,
1004 const char *path, size_t path_len)
1006 struct got_packidx_bloom_filter key;
1008 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1009 return NULL; /* XXX */
1010 key.path_len = path_len;
1012 return RB_FIND(got_packidx_bloom_filter_tree,
1013 &repo->packidx_bloom_filters, &key);
1016 static int
1017 check_packidx_bloom_filter(struct got_repository *repo,
1018 const char *path_packidx, struct got_object_id *id)
1020 struct got_packidx_bloom_filter *bf;
1022 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1023 if (bf)
1024 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1026 /* No bloom filter means this pack index must be searched. */
1027 return 1;
1030 static const struct got_error *
1031 add_packidx_bloom_filter(struct got_repository *repo,
1032 struct got_packidx *packidx, const char *path_packidx)
1034 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1035 struct got_packidx_bloom_filter *bf;
1036 size_t len;
1039 * Don't use bloom filters for very large pack index files.
1040 * Large pack files will contain a relatively large fraction
1041 * of our objects so we will likely need to visit them anyway.
1042 * The more objects a pack file contains the higher the probability
1043 * of a false-positive match from the bloom filter. And reading
1044 * all object IDs from a large pack index file can be expensive.
1046 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1047 return NULL;
1049 /* Do we already have a filter for this pack index? */
1050 if (get_packidx_bloom_filter(repo, path_packidx,
1051 strlen(path_packidx)) != NULL)
1052 return NULL;
1054 bf = calloc(1, sizeof(*bf));
1055 if (bf == NULL)
1056 return got_error_from_errno("calloc");
1057 bf->bloom = calloc(1, sizeof(*bf->bloom));
1058 if (bf->bloom == NULL) {
1059 free(bf);
1060 return got_error_from_errno("calloc");
1063 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1064 if (len >= sizeof(bf->path)) {
1065 free(bf->bloom);
1066 free(bf);
1067 return got_error(GOT_ERR_NO_SPACE);
1069 bf->path_len = len;
1071 /* Minimum size supported by our bloom filter is 1000 entries. */
1072 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1073 for (i = 0; i < nobjects; i++) {
1074 struct got_packidx_object_id *id;
1075 id = &packidx->hdr.sorted_ids[i];
1076 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1079 RB_INSERT(got_packidx_bloom_filter_tree,
1080 &repo->packidx_bloom_filters, bf);
1081 return NULL;
1084 const struct got_error *
1085 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1086 struct got_repository *repo, struct got_object_id *id)
1088 const struct got_error *err;
1089 DIR *packdir = NULL;
1090 struct dirent *dent;
1091 char *path_packidx;
1092 size_t i;
1093 int packdir_fd;
1095 /* Search pack index cache. */
1096 for (i = 0; i < repo->pack_cache_size; i++) {
1097 if (repo->packidx_cache[i] == NULL)
1098 break;
1099 if (!check_packidx_bloom_filter(repo,
1100 repo->packidx_cache[i]->path_packidx, id))
1101 continue; /* object will not be found in this index */
1102 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1103 if (*idx != -1) {
1104 *packidx = repo->packidx_cache[i];
1106 * Move this cache entry to the front. Repeatedly
1107 * searching a wrong pack index can be expensive.
1109 if (i > 0) {
1110 memmove(&repo->packidx_cache[1],
1111 &repo->packidx_cache[0],
1112 i * sizeof(repo->packidx_cache[0]));
1113 repo->packidx_cache[0] = *packidx;
1115 return NULL;
1118 /* No luck. Search the filesystem. */
1120 packdir_fd = openat(got_repo_get_fd(repo),
1121 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1122 if (packdir_fd == -1) {
1123 if (errno == ENOENT)
1124 err = got_error_no_obj(id);
1125 else
1126 err = got_error_from_errno_fmt("openat: %s/%s",
1127 got_repo_get_path_git_dir(repo),
1128 GOT_OBJECTS_PACK_DIR);
1129 goto done;
1132 packdir = fdopendir(packdir_fd);
1133 if (packdir == NULL) {
1134 err = got_error_from_errno("fdopendir");
1135 goto done;
1138 while ((dent = readdir(packdir)) != NULL) {
1139 int is_cached = 0;
1141 if (!got_repo_is_packidx_filename(dent->d_name,
1142 strlen(dent->d_name)))
1143 continue;
1145 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1146 dent->d_name) == -1) {
1147 err = got_error_from_errno("asprintf");
1148 goto done;
1151 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1152 free(path_packidx);
1153 continue; /* object will not be found in this index */
1156 for (i = 0; i < repo->pack_cache_size; i++) {
1157 if (repo->packidx_cache[i] == NULL)
1158 break;
1159 if (strcmp(repo->packidx_cache[i]->path_packidx,
1160 path_packidx) == 0) {
1161 is_cached = 1;
1162 break;
1165 if (is_cached) {
1166 free(path_packidx);
1167 continue; /* already searched */
1170 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1171 path_packidx, 0);
1172 if (err) {
1173 free(path_packidx);
1174 goto done;
1177 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1178 if (err) {
1179 free(path_packidx);
1180 goto done;
1183 err = cache_packidx(repo, *packidx, path_packidx);
1184 free(path_packidx);
1185 if (err)
1186 goto done;
1188 *idx = got_packidx_get_object_idx(*packidx, id);
1189 if (*idx != -1) {
1190 err = NULL; /* found the object */
1191 goto done;
1195 err = got_error_no_obj(id);
1196 done:
1197 if (packdir && closedir(packdir) != 0 && err == NULL)
1198 err = got_error_from_errno("closedir");
1199 return err;
1202 static const struct got_error *
1203 read_packfile_hdr(int fd, struct got_packidx *packidx)
1205 const struct got_error *err = NULL;
1206 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1207 struct got_packfile_hdr hdr;
1208 ssize_t n;
1210 n = read(fd, &hdr, sizeof(hdr));
1211 if (n < 0)
1212 return got_error_from_errno("read");
1213 if (n != sizeof(hdr))
1214 return got_error(GOT_ERR_BAD_PACKFILE);
1216 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1217 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1218 be32toh(hdr.nobjects) != totobj)
1219 err = got_error(GOT_ERR_BAD_PACKFILE);
1221 return err;
1224 static const struct got_error *
1225 open_packfile(int *fd, struct got_repository *repo,
1226 const char *relpath, struct got_packidx *packidx)
1228 const struct got_error *err = NULL;
1230 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1231 if (*fd == -1)
1232 return got_error_from_errno_fmt("openat: %s/%s",
1233 got_repo_get_path_git_dir(repo), relpath);
1235 if (packidx) {
1236 err = read_packfile_hdr(*fd, packidx);
1237 if (err) {
1238 close(*fd);
1239 *fd = -1;
1243 return err;
1246 const struct got_error *
1247 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1248 const char *path_packfile, struct got_packidx *packidx)
1250 const struct got_error *err = NULL;
1251 struct got_pack *pack = NULL;
1252 struct stat sb;
1253 size_t i;
1255 if (packp)
1256 *packp = NULL;
1258 for (i = 0; i < repo->pack_cache_size; i++) {
1259 pack = &repo->packs[i];
1260 if (pack->path_packfile == NULL)
1261 break;
1262 if (strcmp(pack->path_packfile, path_packfile) == 0)
1263 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1266 if (i == repo->pack_cache_size) {
1267 err = got_pack_close(&repo->packs[i - 1]);
1268 if (err)
1269 return err;
1270 memmove(&repo->packs[1], &repo->packs[0],
1271 sizeof(repo->packs) - sizeof(repo->packs[0]));
1272 i = 0;
1275 pack = &repo->packs[i];
1277 pack->path_packfile = strdup(path_packfile);
1278 if (pack->path_packfile == NULL) {
1279 err = got_error_from_errno("strdup");
1280 goto done;
1283 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1284 if (err)
1285 goto done;
1287 if (fstat(pack->fd, &sb) != 0) {
1288 err = got_error_from_errno("fstat");
1289 goto done;
1291 pack->filesize = sb.st_size;
1293 pack->privsep_child = NULL;
1295 #ifndef GOT_PACK_NO_MMAP
1296 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1297 pack->fd, 0);
1298 if (pack->map == MAP_FAILED) {
1299 if (errno != ENOMEM) {
1300 err = got_error_from_errno("mmap");
1301 goto done;
1303 pack->map = NULL; /* fall back to read(2) */
1305 #endif
1306 done:
1307 if (err) {
1308 if (pack) {
1309 free(pack->path_packfile);
1310 memset(pack, 0, sizeof(*pack));
1312 } else if (packp)
1313 *packp = pack;
1314 return err;
1317 struct got_pack *
1318 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1320 struct got_pack *pack = NULL;
1321 size_t i;
1323 for (i = 0; i < repo->pack_cache_size; i++) {
1324 pack = &repo->packs[i];
1325 if (pack->path_packfile == NULL)
1326 break;
1327 if (strcmp(pack->path_packfile, path_packfile) == 0)
1328 return pack;
1331 return NULL;
1334 const struct got_error *
1335 got_repo_init(const char *repo_path)
1337 const struct got_error *err = NULL;
1338 const char *dirnames[] = {
1339 GOT_OBJECTS_DIR,
1340 GOT_OBJECTS_PACK_DIR,
1341 GOT_REFS_DIR,
1343 const char *description_str = "Unnamed repository; "
1344 "edit this file 'description' to name the repository.";
1345 const char *headref_str = "ref: refs/heads/main";
1346 const char *gitconfig_str = "[core]\n"
1347 "\trepositoryformatversion = 0\n"
1348 "\tfilemode = true\n"
1349 "\tbare = true\n";
1350 char *path;
1351 size_t i;
1353 if (!got_path_dir_is_empty(repo_path))
1354 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1356 for (i = 0; i < nitems(dirnames); i++) {
1357 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1358 return got_error_from_errno("asprintf");
1360 err = got_path_mkdir(path);
1361 free(path);
1362 if (err)
1363 return err;
1366 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1367 return got_error_from_errno("asprintf");
1368 err = got_path_create_file(path, description_str);
1369 free(path);
1370 if (err)
1371 return err;
1373 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1374 return got_error_from_errno("asprintf");
1375 err = got_path_create_file(path, headref_str);
1376 free(path);
1377 if (err)
1378 return err;
1380 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1381 return got_error_from_errno("asprintf");
1382 err = got_path_create_file(path, gitconfig_str);
1383 free(path);
1384 if (err)
1385 return err;
1387 return NULL;
1390 static const struct got_error *
1391 match_packed_object(struct got_object_id **unique_id,
1392 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1394 const struct got_error *err = NULL;
1395 DIR *packdir = NULL;
1396 struct dirent *dent;
1397 char *path_packidx;
1398 struct got_object_id_queue matched_ids;
1399 int packdir_fd;
1401 STAILQ_INIT(&matched_ids);
1403 packdir_fd = openat(got_repo_get_fd(repo),
1404 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1405 if (packdir_fd == -1) {
1406 if (errno != ENOENT)
1407 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1408 goto done;
1411 packdir = fdopendir(packdir_fd);
1412 if (packdir == NULL) {
1413 err = got_error_from_errno("fdopendir");
1414 goto done;
1417 while ((dent = readdir(packdir)) != NULL) {
1418 struct got_packidx *packidx;
1419 struct got_object_qid *qid;
1421 if (!got_repo_is_packidx_filename(dent->d_name,
1422 strlen(dent->d_name)))
1423 continue;
1425 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1426 dent->d_name) == -1) {
1427 err = got_error_from_errno("strdup");
1428 break;
1431 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1432 path_packidx, 0);
1433 free(path_packidx);
1434 if (err)
1435 break;
1437 err = got_packidx_match_id_str_prefix(&matched_ids,
1438 packidx, id_str_prefix);
1439 if (err) {
1440 got_packidx_close(packidx);
1441 break;
1443 err = got_packidx_close(packidx);
1444 if (err)
1445 break;
1447 STAILQ_FOREACH(qid, &matched_ids, entry) {
1448 if (obj_type != GOT_OBJ_TYPE_ANY) {
1449 int matched_type;
1450 err = got_object_get_type(&matched_type, repo,
1451 qid->id);
1452 if (err)
1453 goto done;
1454 if (matched_type != obj_type)
1455 continue;
1457 if (*unique_id == NULL) {
1458 *unique_id = got_object_id_dup(qid->id);
1459 if (*unique_id == NULL) {
1460 err = got_error_from_errno("malloc");
1461 goto done;
1463 } else {
1464 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1465 continue; /* packed multiple times */
1466 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1467 goto done;
1471 done:
1472 got_object_id_queue_free(&matched_ids);
1473 if (packdir && closedir(packdir) != 0 && err == NULL)
1474 err = got_error_from_errno("closedir");
1475 if (err) {
1476 free(*unique_id);
1477 *unique_id = NULL;
1479 return err;
1482 static const struct got_error *
1483 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1484 const char *object_dir, const char *id_str_prefix, int obj_type,
1485 struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 char *path;
1489 DIR *dir = NULL;
1490 struct dirent *dent;
1491 struct got_object_id id;
1493 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1494 err = got_error_from_errno("asprintf");
1495 goto done;
1498 dir = opendir(path);
1499 if (dir == NULL) {
1500 if (errno == ENOENT) {
1501 err = NULL;
1502 goto done;
1504 err = got_error_from_errno2("opendir", path);
1505 goto done;
1507 while ((dent = readdir(dir)) != NULL) {
1508 char *id_str;
1509 int cmp;
1511 if (strcmp(dent->d_name, ".") == 0 ||
1512 strcmp(dent->d_name, "..") == 0)
1513 continue;
1515 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1516 err = got_error_from_errno("asprintf");
1517 goto done;
1520 if (!got_parse_sha1_digest(id.sha1, id_str))
1521 continue;
1524 * Directory entries do not necessarily appear in
1525 * sorted order, so we must iterate over all of them.
1527 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1528 if (cmp != 0) {
1529 free(id_str);
1530 continue;
1533 if (*unique_id == NULL) {
1534 if (obj_type != GOT_OBJ_TYPE_ANY) {
1535 int matched_type;
1536 err = got_object_get_type(&matched_type, repo,
1537 &id);
1538 if (err)
1539 goto done;
1540 if (matched_type != obj_type)
1541 continue;
1543 *unique_id = got_object_id_dup(&id);
1544 if (*unique_id == NULL) {
1545 err = got_error_from_errno("got_object_id_dup");
1546 free(id_str);
1547 goto done;
1549 } else {
1550 if (got_object_id_cmp(*unique_id, &id) == 0)
1551 continue; /* both packed and loose */
1552 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1553 free(id_str);
1554 goto done;
1557 done:
1558 if (dir && closedir(dir) != 0 && err == NULL)
1559 err = got_error_from_errno("closedir");
1560 if (err) {
1561 free(*unique_id);
1562 *unique_id = NULL;
1564 free(path);
1565 return err;
1568 const struct got_error *
1569 got_repo_match_object_id_prefix(struct got_object_id **id,
1570 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1572 const struct got_error *err = NULL;
1573 char *path_objects = got_repo_get_path_objects(repo);
1574 char *object_dir = NULL;
1575 size_t len;
1576 int i;
1578 *id = NULL;
1580 for (i = 0; i < strlen(id_str_prefix); i++) {
1581 if (isxdigit((unsigned char)id_str_prefix[i]))
1582 continue;
1583 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1586 len = strlen(id_str_prefix);
1587 if (len >= 2) {
1588 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1589 if (err)
1590 goto done;
1591 object_dir = strndup(id_str_prefix, 2);
1592 if (object_dir == NULL) {
1593 err = got_error_from_errno("strdup");
1594 goto done;
1596 err = match_loose_object(id, path_objects, object_dir,
1597 id_str_prefix, obj_type, repo);
1598 } else if (len == 1) {
1599 int i;
1600 for (i = 0; i < 0xf; i++) {
1601 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1602 == -1) {
1603 err = got_error_from_errno("asprintf");
1604 goto done;
1606 err = match_packed_object(id, repo, object_dir,
1607 obj_type);
1608 if (err)
1609 goto done;
1610 err = match_loose_object(id, path_objects, object_dir,
1611 id_str_prefix, obj_type, repo);
1612 if (err)
1613 goto done;
1615 } else {
1616 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1617 goto done;
1619 done:
1620 free(object_dir);
1621 if (err) {
1622 free(*id);
1623 *id = NULL;
1624 } else if (*id == NULL) {
1625 switch (obj_type) {
1626 case GOT_OBJ_TYPE_BLOB:
1627 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1628 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1629 break;
1630 case GOT_OBJ_TYPE_TREE:
1631 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1632 GOT_OBJ_LABEL_TREE, id_str_prefix);
1633 break;
1634 case GOT_OBJ_TYPE_COMMIT:
1635 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1636 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1637 break;
1638 case GOT_OBJ_TYPE_TAG:
1639 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1640 GOT_OBJ_LABEL_TAG, id_str_prefix);
1641 break;
1642 default:
1643 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1644 break;
1648 return err;
1651 const struct got_error *
1652 got_repo_match_object_id(struct got_object_id **id, char **label,
1653 const char *id_str, int obj_type, struct got_reflist_head *refs,
1654 struct got_repository *repo)
1656 const struct got_error *err;
1657 struct got_tag_object *tag;
1658 struct got_reference *ref = NULL;
1660 *id = NULL;
1661 if (label)
1662 *label = NULL;
1664 if (refs) {
1665 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1666 refs, repo);
1667 if (err == NULL) {
1668 *id = got_object_id_dup(
1669 got_object_tag_get_object_id(tag));
1670 if (*id == NULL)
1671 err = got_error_from_errno("got_object_id_dup");
1672 else if (label && asprintf(label, "refs/tags/%s",
1673 got_object_tag_get_name(tag)) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 free(*id);
1676 *id = NULL;
1678 got_object_tag_close(tag);
1679 return err;
1680 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1681 err->code != GOT_ERR_NO_OBJ)
1682 return err;
1685 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1686 if (err) {
1687 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1688 return err;
1689 err = got_ref_open(&ref, repo, id_str, 0);
1690 if (err != NULL)
1691 goto done;
1692 if (label) {
1693 *label = strdup(got_ref_get_name(ref));
1694 if (*label == NULL) {
1695 err = got_error_from_errno("strdup");
1696 goto done;
1699 err = got_ref_resolve(id, repo, ref);
1700 } else if (label) {
1701 err = got_object_id_str(label, *id);
1702 if (*label == NULL) {
1703 err = got_error_from_errno("strdup");
1704 goto done;
1707 done:
1708 if (ref)
1709 got_ref_close(ref);
1710 return err;
1713 const struct got_error *
1714 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1715 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_reflist_entry *re;
1719 struct got_object_id *tag_id;
1720 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1722 *tag = NULL;
1724 TAILQ_FOREACH(re, refs, entry) {
1725 const char *refname;
1726 refname = got_ref_get_name(re->ref);
1727 if (got_ref_is_symbolic(re->ref))
1728 continue;
1729 if (strncmp(refname, "refs/tags/", 10) != 0)
1730 continue;
1731 if (!name_is_absolute)
1732 refname += strlen("refs/tags/");
1733 if (strcmp(refname, name) != 0)
1734 continue;
1735 err = got_ref_resolve(&tag_id, repo, re->ref);
1736 if (err)
1737 break;
1738 err = got_object_open_as_tag(tag, repo, tag_id);
1739 free(tag_id);
1740 if (err)
1741 break;
1742 if (obj_type == GOT_OBJ_TYPE_ANY ||
1743 got_object_tag_get_object_type(*tag) == obj_type)
1744 break;
1745 got_object_tag_close(*tag);
1746 *tag = NULL;
1749 if (err == NULL && *tag == NULL)
1750 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1751 GOT_OBJ_LABEL_TAG, name);
1752 return err;
1755 static const struct got_error *
1756 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1757 const char *name, mode_t mode, struct got_object_id *blob_id)
1759 const struct got_error *err = NULL;
1761 *new_te = NULL;
1763 *new_te = calloc(1, sizeof(**new_te));
1764 if (*new_te == NULL)
1765 return got_error_from_errno("calloc");
1767 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1768 sizeof((*new_te)->name)) {
1769 err = got_error(GOT_ERR_NO_SPACE);
1770 goto done;
1773 if (S_ISLNK(mode)) {
1774 (*new_te)->mode = S_IFLNK;
1775 } else {
1776 (*new_te)->mode = S_IFREG;
1777 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1779 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1780 done:
1781 if (err && *new_te) {
1782 free(*new_te);
1783 *new_te = NULL;
1785 return err;
1788 static const struct got_error *
1789 import_file(struct got_tree_entry **new_te, struct dirent *de,
1790 const char *path, struct got_repository *repo)
1792 const struct got_error *err;
1793 struct got_object_id *blob_id = NULL;
1794 char *filepath;
1795 struct stat sb;
1797 if (asprintf(&filepath, "%s%s%s", path,
1798 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1799 return got_error_from_errno("asprintf");
1801 if (lstat(filepath, &sb) != 0) {
1802 err = got_error_from_errno2("lstat", path);
1803 goto done;
1806 err = got_object_blob_create(&blob_id, filepath, repo);
1807 if (err)
1808 goto done;
1810 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1811 blob_id);
1812 done:
1813 free(filepath);
1814 if (err)
1815 free(blob_id);
1816 return err;
1819 static const struct got_error *
1820 insert_tree_entry(struct got_tree_entry *new_te,
1821 struct got_pathlist_head *paths)
1823 const struct got_error *err = NULL;
1824 struct got_pathlist_entry *new_pe;
1826 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1827 if (err)
1828 return err;
1829 if (new_pe == NULL)
1830 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1831 return NULL;
1834 static const struct got_error *write_tree(struct got_object_id **,
1835 const char *, struct got_pathlist_head *, struct got_repository *,
1836 got_repo_import_cb progress_cb, void *progress_arg);
1838 static const struct got_error *
1839 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1840 const char *path, struct got_pathlist_head *ignores,
1841 struct got_repository *repo,
1842 got_repo_import_cb progress_cb, void *progress_arg)
1844 const struct got_error *err;
1845 struct got_object_id *id = NULL;
1846 char *subdirpath;
1848 if (asprintf(&subdirpath, "%s%s%s", path,
1849 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1850 return got_error_from_errno("asprintf");
1852 (*new_te) = calloc(1, sizeof(**new_te));
1853 if (*new_te == NULL)
1854 return got_error_from_errno("calloc");
1855 (*new_te)->mode = S_IFDIR;
1856 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1857 sizeof((*new_te)->name)) {
1858 err = got_error(GOT_ERR_NO_SPACE);
1859 goto done;
1861 err = write_tree(&id, subdirpath, ignores, repo,
1862 progress_cb, progress_arg);
1863 if (err)
1864 goto done;
1865 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1867 done:
1868 free(id);
1869 free(subdirpath);
1870 if (err) {
1871 free(*new_te);
1872 *new_te = NULL;
1874 return err;
1877 static const struct got_error *
1878 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1879 struct got_pathlist_head *ignores, struct got_repository *repo,
1880 got_repo_import_cb progress_cb, void *progress_arg)
1882 const struct got_error *err = NULL;
1883 DIR *dir;
1884 struct dirent *de;
1885 int nentries;
1886 struct got_tree_entry *new_te = NULL;
1887 struct got_pathlist_head paths;
1888 struct got_pathlist_entry *pe;
1890 *new_tree_id = NULL;
1892 TAILQ_INIT(&paths);
1894 dir = opendir(path_dir);
1895 if (dir == NULL) {
1896 err = got_error_from_errno2("opendir", path_dir);
1897 goto done;
1900 nentries = 0;
1901 while ((de = readdir(dir)) != NULL) {
1902 int ignore = 0;
1903 int type;
1905 if (strcmp(de->d_name, ".") == 0 ||
1906 strcmp(de->d_name, "..") == 0)
1907 continue;
1909 TAILQ_FOREACH(pe, ignores, entry) {
1910 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1911 ignore = 1;
1912 break;
1915 if (ignore)
1916 continue;
1918 err = got_path_dirent_type(&type, path_dir, de);
1919 if (err)
1920 goto done;
1922 if (type == DT_DIR) {
1923 err = import_subdir(&new_te, de, path_dir,
1924 ignores, repo, progress_cb, progress_arg);
1925 if (err) {
1926 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1927 goto done;
1928 err = NULL;
1929 continue;
1931 } else if (type == DT_REG || type == DT_LNK) {
1932 err = import_file(&new_te, de, path_dir, repo);
1933 if (err)
1934 goto done;
1935 } else
1936 continue;
1938 err = insert_tree_entry(new_te, &paths);
1939 if (err)
1940 goto done;
1941 nentries++;
1944 if (TAILQ_EMPTY(&paths)) {
1945 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1946 "cannot create tree without any entries");
1947 goto done;
1950 TAILQ_FOREACH(pe, &paths, entry) {
1951 struct got_tree_entry *te = pe->data;
1952 char *path;
1953 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1954 continue;
1955 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1956 err = got_error_from_errno("asprintf");
1957 goto done;
1959 err = (*progress_cb)(progress_arg, path);
1960 free(path);
1961 if (err)
1962 goto done;
1965 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1966 done:
1967 if (dir)
1968 closedir(dir);
1969 got_pathlist_free(&paths);
1970 return err;
1973 const struct got_error *
1974 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1975 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1976 struct got_repository *repo, got_repo_import_cb progress_cb,
1977 void *progress_arg)
1979 const struct got_error *err;
1980 struct got_object_id *new_tree_id;
1982 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1983 progress_cb, progress_arg);
1984 if (err)
1985 return err;
1987 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1988 author, time(NULL), author, time(NULL), logmsg, repo);
1989 free(new_tree_id);
1990 return err;
1993 const struct got_error *
1994 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1995 struct got_repository *repo)
1997 const struct got_error *err = NULL;
1998 char *path_objects = NULL, *path = NULL;
1999 DIR *dir = NULL;
2000 struct got_object_id id;
2001 int i;
2003 *nobjects = 0;
2004 *ondisk_size = 0;
2006 path_objects = got_repo_get_path_objects(repo);
2007 if (path_objects == NULL)
2008 return got_error_from_errno("got_repo_get_path_objects");
2010 for (i = 0; i <= 0xff; i++) {
2011 struct dirent *dent;
2013 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2014 err = got_error_from_errno("asprintf");
2015 break;
2018 dir = opendir(path);
2019 if (dir == NULL) {
2020 if (errno == ENOENT) {
2021 err = NULL;
2022 continue;
2024 err = got_error_from_errno2("opendir", path);
2025 break;
2028 while ((dent = readdir(dir)) != NULL) {
2029 char *id_str;
2030 int fd;
2031 struct stat sb;
2033 if (strcmp(dent->d_name, ".") == 0 ||
2034 strcmp(dent->d_name, "..") == 0)
2035 continue;
2037 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2038 err = got_error_from_errno("asprintf");
2039 goto done;
2042 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2043 free(id_str);
2044 continue;
2046 free(id_str);
2048 err = got_object_open_loose_fd(&fd, &id, repo);
2049 if (err)
2050 goto done;
2052 if (fstat(fd, &sb) == -1) {
2053 err = got_error_from_errno("fstat");
2054 close(fd);
2055 goto done;
2057 (*nobjects)++;
2058 (*ondisk_size) += sb.st_size;
2060 if (close(fd) == -1) {
2061 err = got_error_from_errno("close");
2062 goto done;
2066 if (closedir(dir) != 0) {
2067 err = got_error_from_errno("closedir");
2068 goto done;
2070 dir = NULL;
2072 free(path);
2073 path = NULL;
2075 done:
2076 if (dir && closedir(dir) != 0 && err == NULL)
2077 err = got_error_from_errno("closedir");
2079 if (err) {
2080 *nobjects = 0;
2081 *ondisk_size = 0;
2083 free(path_objects);
2084 free(path);
2085 return err;
2088 const struct got_error *
2089 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2090 off_t *total_packsize, struct got_repository *repo)
2092 const struct got_error *err = NULL;
2093 DIR *packdir = NULL;
2094 struct dirent *dent;
2095 struct got_packidx *packidx = NULL;
2096 char *path_packidx;
2097 char *path_packfile;
2098 int packdir_fd;
2099 struct stat sb;
2101 *npackfiles = 0;
2102 *nobjects = 0;
2103 *total_packsize = 0;
2105 packdir_fd = openat(got_repo_get_fd(repo),
2106 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2107 if (packdir_fd == -1) {
2108 return got_error_from_errno_fmt("openat: %s/%s",
2109 got_repo_get_path_git_dir(repo),
2110 GOT_OBJECTS_PACK_DIR);
2113 packdir = fdopendir(packdir_fd);
2114 if (packdir == NULL) {
2115 err = got_error_from_errno("fdopendir");
2116 goto done;
2119 while ((dent = readdir(packdir)) != NULL) {
2120 if (!got_repo_is_packidx_filename(dent->d_name,
2121 strlen(dent->d_name)))
2122 continue;
2124 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2125 dent->d_name) == -1) {
2126 err = got_error_from_errno("asprintf");
2127 goto done;
2130 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2131 path_packidx, 0);
2132 free(path_packidx);
2133 if (err)
2134 goto done;
2136 if (fstat(packidx->fd, &sb) == -1)
2137 goto done;
2138 *total_packsize += sb.st_size;
2140 err = got_packidx_get_packfile_path(&path_packfile,
2141 packidx->path_packidx);
2142 if (err)
2143 goto done;
2145 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2146 0) == -1) {
2147 free(path_packfile);
2148 goto done;
2150 free(path_packfile);
2151 *total_packsize += sb.st_size;
2153 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2155 (*npackfiles)++;
2157 got_packidx_close(packidx);
2158 packidx = NULL;
2160 done:
2161 if (packidx)
2162 got_packidx_close(packidx);
2163 if (packdir && closedir(packdir) != 0 && err == NULL)
2164 err = got_error_from_errno("closedir");
2165 if (err) {
2166 *npackfiles = 0;
2167 *nobjects = 0;
2168 *total_packsize = 0;
2170 return err;
2173 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2174 got_packidx_bloom_filter_cmp);