Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
74 const char *
75 got_repo_get_path(struct got_repository *repo)
76 {
77 return repo->path;
78 }
80 const char *
81 got_repo_get_path_git_dir(struct got_repository *repo)
82 {
83 return repo->path_git_dir;
84 }
86 int
87 got_repo_get_fd(struct got_repository *repo)
88 {
89 return repo->gitdir_fd;
90 }
92 const char *
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_name;
96 }
98 const char *
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
104 const char *
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
110 const char *
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
116 const char *
117 got_repo_get_gitconfig_owner(struct got_repository *repo)
119 return repo->gitconfig_owner;
122 void
123 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
124 struct got_repository *repo)
126 *extensions = repo->extensions;
127 *nextensions = repo->nextensions;
130 int
131 got_repo_is_bare(struct got_repository *repo)
133 return (strcmp(repo->path, repo->path_git_dir) == 0);
136 static char *
137 get_path_git_child(struct got_repository *repo, const char *basename)
139 char *path_child;
141 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
142 basename) == -1)
143 return NULL;
145 return path_child;
148 char *
149 got_repo_get_path_objects(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_DIR);
154 char *
155 got_repo_get_path_objects_pack(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
160 char *
161 got_repo_get_path_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_REFS_DIR);
166 char *
167 got_repo_get_path_packed_refs(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
172 static char *
173 get_path_head(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_HEAD_FILE);
178 char *
179 got_repo_get_path_gitconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GITCONFIG);
184 char *
185 got_repo_get_path_gotconfig(struct got_repository *repo)
187 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
190 const struct got_gotconfig *
191 got_repo_get_gotconfig(struct got_repository *repo)
193 return repo->gotconfig;
196 void
197 got_repo_get_gitconfig_remotes(int *nremotes,
198 const struct got_remote_repo **remotes, struct got_repository *repo)
200 *nremotes = repo->ngitconfig_remotes;
201 *remotes = repo->gitconfig_remotes;
204 static int
205 is_git_repo(struct got_repository *repo)
207 const char *path_git = got_repo_get_path_git_dir(repo);
208 char *path_objects = got_repo_get_path_objects(repo);
209 char *path_refs = got_repo_get_path_refs(repo);
210 char *path_head = get_path_head(repo);
211 int ret = 0;
212 struct stat sb;
213 struct got_reference *head_ref;
215 if (lstat(path_git, &sb) == -1)
216 goto done;
217 if (!S_ISDIR(sb.st_mode))
218 goto done;
220 if (lstat(path_objects, &sb) == -1)
221 goto done;
222 if (!S_ISDIR(sb.st_mode))
223 goto done;
225 if (lstat(path_refs, &sb) == -1)
226 goto done;
227 if (!S_ISDIR(sb.st_mode))
228 goto done;
230 if (lstat(path_head, &sb) == -1)
231 goto done;
232 if (!S_ISREG(sb.st_mode))
233 goto done;
235 /* Check if the HEAD reference can be opened. */
236 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
237 goto done;
238 got_ref_close(head_ref);
240 ret = 1;
241 done:
242 free(path_objects);
243 free(path_refs);
244 free(path_head);
245 return ret;
249 const struct got_error *
250 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
251 struct got_object *obj)
253 #ifndef GOT_NO_OBJ_CACHE
254 const struct got_error *err = NULL;
255 err = got_object_cache_add(&repo->objcache, id, obj);
256 if (err) {
257 if (err->code == GOT_ERR_OBJ_EXISTS ||
258 err->code == GOT_ERR_OBJ_TOO_LARGE)
259 err = NULL;
260 return err;
262 obj->refcnt++;
263 #endif
264 return NULL;
267 struct got_object *
268 got_repo_get_cached_object(struct got_repository *repo,
269 struct got_object_id *id)
271 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
274 const struct got_error *
275 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
276 struct got_tree_object *tree)
278 #ifndef GOT_NO_OBJ_CACHE
279 const struct got_error *err = NULL;
280 err = got_object_cache_add(&repo->treecache, id, tree);
281 if (err) {
282 if (err->code == GOT_ERR_OBJ_EXISTS ||
283 err->code == GOT_ERR_OBJ_TOO_LARGE)
284 err = NULL;
285 return err;
287 tree->refcnt++;
288 #endif
289 return NULL;
292 struct got_tree_object *
293 got_repo_get_cached_tree(struct got_repository *repo,
294 struct got_object_id *id)
296 return (struct got_tree_object *)got_object_cache_get(
297 &repo->treecache, id);
300 const struct got_error *
301 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
302 struct got_commit_object *commit)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->commitcache, id, commit);
307 if (err) {
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 err = NULL;
311 return err;
313 commit->refcnt++;
314 #endif
315 return NULL;
318 struct got_commit_object *
319 got_repo_get_cached_commit(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_commit_object *)got_object_cache_get(
323 &repo->commitcache, id);
326 const struct got_error *
327 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
328 struct got_tag_object *tag)
330 #ifndef GOT_NO_OBJ_CACHE
331 const struct got_error *err = NULL;
332 err = got_object_cache_add(&repo->tagcache, id, tag);
333 if (err) {
334 if (err->code == GOT_ERR_OBJ_EXISTS ||
335 err->code == GOT_ERR_OBJ_TOO_LARGE)
336 err = NULL;
337 return err;
339 tag->refcnt++;
340 #endif
341 return NULL;
344 struct got_tag_object *
345 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
347 return (struct got_tag_object *)got_object_cache_get(
348 &repo->tagcache, id);
351 const struct got_error *
352 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
353 struct got_raw_object *raw)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->rawcache, id, raw);
358 if (err) {
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
361 err = NULL;
362 return err;
364 raw->refcnt++;
365 #endif
366 return NULL;
370 struct got_raw_object *
371 got_repo_get_cached_raw_object(struct got_repository *repo,
372 struct got_object_id *id)
374 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
378 static const struct got_error *
379 open_repo(struct got_repository *repo, const char *path)
381 const struct got_error *err = NULL;
383 repo->gitdir_fd = -1;
385 /* bare git repository? */
386 repo->path_git_dir = strdup(path);
387 if (repo->path_git_dir == NULL)
388 return got_error_from_errno("strdup");
389 if (is_git_repo(repo)) {
390 repo->path = strdup(repo->path_git_dir);
391 if (repo->path == NULL) {
392 err = got_error_from_errno("strdup");
393 goto done;
395 repo->gitdir_fd = open(repo->path_git_dir,
396 O_DIRECTORY | O_CLOEXEC);
397 if (repo->gitdir_fd == -1) {
398 err = got_error_from_errno2("open",
399 repo->path_git_dir);
400 goto done;
402 return NULL;
405 /* git repository with working tree? */
406 free(repo->path_git_dir);
407 repo->path_git_dir = NULL;
408 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
409 err = got_error_from_errno("asprintf");
410 goto done;
412 if (is_git_repo(repo)) {
413 repo->path = strdup(path);
414 if (repo->path == NULL) {
415 err = got_error_from_errno("strdup");
416 goto done;
418 repo->gitdir_fd = open(repo->path_git_dir,
419 O_DIRECTORY | O_CLOEXEC);
420 if (repo->gitdir_fd == -1) {
421 err = got_error_from_errno2("open",
422 repo->path_git_dir);
423 goto done;
425 return NULL;
428 err = got_error(GOT_ERR_NOT_GIT_REPO);
429 done:
430 if (err) {
431 free(repo->path);
432 repo->path = NULL;
433 free(repo->path_git_dir);
434 repo->path_git_dir = NULL;
435 if (repo->gitdir_fd != -1)
436 close(repo->gitdir_fd);
437 repo->gitdir_fd = -1;
440 return err;
443 static const struct got_error *
444 parse_gitconfig_file(int *gitconfig_repository_format_version,
445 char **gitconfig_author_name, char **gitconfig_author_email,
446 struct got_remote_repo **remotes, int *nremotes,
447 char **gitconfig_owner, char ***extensions, int *nextensions,
448 const char *gitconfig_path)
450 const struct got_error *err = NULL, *child_err = NULL;
451 int fd = -1;
452 int imsg_fds[2] = { -1, -1 };
453 pid_t pid;
454 struct imsgbuf *ibuf;
456 *gitconfig_repository_format_version = 0;
457 if (extensions)
458 *extensions = NULL;
459 if (nextensions)
460 *nextensions = 0;
461 *gitconfig_author_name = NULL;
462 *gitconfig_author_email = NULL;
463 if (remotes)
464 *remotes = NULL;
465 if (nremotes)
466 *nremotes = 0;
467 if (gitconfig_owner)
468 *gitconfig_owner = NULL;
470 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
471 if (fd == -1) {
472 if (errno == ENOENT)
473 return NULL;
474 return got_error_from_errno2("open", gitconfig_path);
477 ibuf = calloc(1, sizeof(*ibuf));
478 if (ibuf == NULL) {
479 err = got_error_from_errno("calloc");
480 goto done;
483 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
484 err = got_error_from_errno("socketpair");
485 goto done;
488 pid = fork();
489 if (pid == -1) {
490 err = got_error_from_errno("fork");
491 goto done;
492 } else if (pid == 0) {
493 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
494 gitconfig_path);
495 /* not reached */
498 if (close(imsg_fds[1]) == -1) {
499 err = got_error_from_errno("close");
500 goto done;
502 imsg_fds[1] = -1;
503 imsg_init(ibuf, imsg_fds[0]);
505 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
506 if (err)
507 goto done;
508 fd = -1;
510 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
511 if (err)
512 goto done;
514 err = got_privsep_recv_gitconfig_int(
515 gitconfig_repository_format_version, ibuf);
516 if (err)
517 goto done;
519 if (extensions && nextensions) {
520 err = got_privsep_send_gitconfig_repository_extensions_req(
521 ibuf);
522 if (err)
523 goto done;
524 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
525 if (err)
526 goto done;
527 if (*nextensions > 0) {
528 int i;
529 *extensions = calloc(*nextensions, sizeof(char *));
530 if (*extensions == NULL) {
531 err = got_error_from_errno("calloc");
532 goto done;
534 for (i = 0; i < *nextensions; i++) {
535 char *ext;
536 err = got_privsep_recv_gitconfig_str(&ext,
537 ibuf);
538 if (err)
539 goto done;
540 (*extensions)[i] = ext;
545 err = got_privsep_send_gitconfig_author_name_req(ibuf);
546 if (err)
547 goto done;
549 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
550 if (err)
551 goto done;
553 err = got_privsep_send_gitconfig_author_email_req(ibuf);
554 if (err)
555 goto done;
557 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
558 if (err)
559 goto done;
561 if (remotes && nremotes) {
562 err = got_privsep_send_gitconfig_remotes_req(ibuf);
563 if (err)
564 goto done;
566 err = got_privsep_recv_gitconfig_remotes(remotes,
567 nremotes, ibuf);
568 if (err)
569 goto done;
572 if (gitconfig_owner) {
573 err = got_privsep_send_gitconfig_owner_req(ibuf);
574 if (err)
575 goto done;
576 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
577 if (err)
578 goto done;
581 err = got_privsep_send_stop(imsg_fds[0]);
582 child_err = got_privsep_wait_for_child(pid);
583 if (child_err && err == NULL)
584 err = child_err;
585 done:
586 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
587 err = got_error_from_errno("close");
588 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
589 err = got_error_from_errno("close");
590 if (fd != -1 && close(fd) == -1 && err == NULL)
591 err = got_error_from_errno2("close", gitconfig_path);
592 free(ibuf);
593 return err;
596 static const struct got_error *
597 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
599 const struct got_error *err = NULL;
600 char *repo_gitconfig_path = NULL;
602 if (global_gitconfig_path) {
603 /* Read settings from ~/.gitconfig. */
604 int dummy_repo_version;
605 err = parse_gitconfig_file(&dummy_repo_version,
606 &repo->global_gitconfig_author_name,
607 &repo->global_gitconfig_author_email,
608 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
609 if (err)
610 return err;
613 /* Read repository's .git/config file. */
614 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
615 if (repo_gitconfig_path == NULL)
616 return got_error_from_errno("got_repo_get_path_gitconfig");
618 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
619 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
620 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
621 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
622 repo_gitconfig_path);
623 if (err)
624 goto done;
625 done:
626 free(repo_gitconfig_path);
627 return err;
630 static const struct got_error *
631 read_gotconfig(struct got_repository *repo)
633 const struct got_error *err = NULL;
634 char *gotconfig_path;
636 gotconfig_path = got_repo_get_path_gotconfig(repo);
637 if (gotconfig_path == NULL)
638 return got_error_from_errno("got_repo_get_path_gotconfig");
640 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
641 free(gotconfig_path);
642 return err;
645 /* Supported repository format extensions. */
646 static const char *const repo_extensions[] = {
647 "noop", /* Got supports repository format version 1. */
648 "preciousObjects", /* Supported by gotadmin cleanup. */
649 "worktreeConfig", /* Got does not care about Git work trees. */
650 };
652 const struct got_error *
653 got_repo_open(struct got_repository **repop, const char *path,
654 const char *global_gitconfig_path)
656 struct got_repository *repo = NULL;
657 const struct got_error *err = NULL;
658 char *repo_path = NULL;
659 size_t i;
660 struct rlimit rl;
662 *repop = NULL;
664 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
665 return got_error_from_errno("getrlimit");
667 repo = calloc(1, sizeof(*repo));
668 if (repo == NULL)
669 return got_error_from_errno("calloc");
671 RB_INIT(&repo->packidx_bloom_filters);
672 TAILQ_INIT(&repo->packidx_paths);
674 for (i = 0; i < nitems(repo->privsep_children); i++) {
675 memset(&repo->privsep_children[i], 0,
676 sizeof(repo->privsep_children[0]));
677 repo->privsep_children[i].imsg_fd = -1;
680 err = got_object_cache_init(&repo->objcache,
681 GOT_OBJECT_CACHE_TYPE_OBJ);
682 if (err)
683 goto done;
684 err = got_object_cache_init(&repo->treecache,
685 GOT_OBJECT_CACHE_TYPE_TREE);
686 if (err)
687 goto done;
688 err = got_object_cache_init(&repo->commitcache,
689 GOT_OBJECT_CACHE_TYPE_COMMIT);
690 if (err)
691 goto done;
692 err = got_object_cache_init(&repo->tagcache,
693 GOT_OBJECT_CACHE_TYPE_TAG);
694 if (err)
695 goto done;
696 err = got_object_cache_init(&repo->rawcache,
697 GOT_OBJECT_CACHE_TYPE_RAW);
698 if (err)
699 goto done;
701 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
702 if (repo->pack_cache_size > rl.rlim_cur / 8)
703 repo->pack_cache_size = rl.rlim_cur / 8;
704 for (i = 0; i < nitems(repo->packs); i++) {
705 if (i < repo->pack_cache_size) {
706 repo->packs[i].basefd = got_opentempfd();
707 if (repo->packs[i].basefd == -1)
708 return got_error_from_errno("got_opentempfd");
709 repo->packs[i].accumfd = got_opentempfd();
710 if (repo->packs[i].accumfd == -1)
711 return got_error_from_errno("got_opentempfd");
712 } else {
713 repo->packs[i].basefd = -1;
714 repo->packs[i].accumfd = -1;
718 repo_path = realpath(path, NULL);
719 if (repo_path == NULL) {
720 err = got_error_from_errno2("realpath", path);
721 goto done;
724 for (;;) {
725 char *parent_path;
727 err = open_repo(repo, repo_path);
728 if (err == NULL)
729 break;
730 if (err->code != GOT_ERR_NOT_GIT_REPO)
731 goto done;
732 if (repo_path[0] == '/' && repo_path[1] == '\0') {
733 err = got_error(GOT_ERR_NOT_GIT_REPO);
734 goto done;
736 err = got_path_dirname(&parent_path, repo_path);
737 if (err)
738 goto done;
739 free(repo_path);
740 repo_path = parent_path;
743 err = read_gotconfig(repo);
744 if (err)
745 goto done;
747 err = read_gitconfig(repo, global_gitconfig_path);
748 if (err)
749 goto done;
750 if (repo->gitconfig_repository_format_version != 0)
751 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
752 for (i = 0; i < repo->nextensions; i++) {
753 char *ext = repo->extensions[i];
754 int j, supported = 0;
755 for (j = 0; j < nitems(repo_extensions); j++) {
756 if (strcmp(ext, repo_extensions[j]) == 0) {
757 supported = 1;
758 break;
761 if (!supported) {
762 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
763 goto done;
767 err = got_repo_list_packidx(&repo->packidx_paths, repo);
768 done:
769 if (err)
770 got_repo_close(repo);
771 else
772 *repop = repo;
773 free(repo_path);
774 return err;
777 const struct got_error *
778 got_repo_close(struct got_repository *repo)
780 const struct got_error *err = NULL, *child_err;
781 struct got_packidx_bloom_filter *bf;
782 struct got_pathlist_entry *pe;
783 size_t i;
785 for (i = 0; i < repo->pack_cache_size; i++) {
786 if (repo->packidx_cache[i] == NULL)
787 break;
788 got_packidx_close(repo->packidx_cache[i]);
791 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
792 &repo->packidx_bloom_filters))) {
793 RB_REMOVE(got_packidx_bloom_filter_tree,
794 &repo->packidx_bloom_filters, bf);
795 free(bf->bloom);
796 free(bf);
799 for (i = 0; i < repo->pack_cache_size; i++) {
800 if (repo->packs[i].path_packfile == NULL)
801 break;
802 got_pack_close(&repo->packs[i]);
803 if (repo->packs[i].basefd != -1) {
804 if (close(repo->packs[i].basefd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 repo->packs[i].basefd = -1;
808 if (repo->packs[i].accumfd != -1) {
809 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
810 err = got_error_from_errno("close");
811 repo->packs[i].accumfd = -1;
815 free(repo->path);
816 free(repo->path_git_dir);
818 got_object_cache_close(&repo->objcache);
819 got_object_cache_close(&repo->treecache);
820 got_object_cache_close(&repo->commitcache);
821 got_object_cache_close(&repo->tagcache);
822 got_object_cache_close(&repo->rawcache);
824 for (i = 0; i < nitems(repo->privsep_children); i++) {
825 if (repo->privsep_children[i].imsg_fd == -1)
826 continue;
827 imsg_clear(repo->privsep_children[i].ibuf);
828 free(repo->privsep_children[i].ibuf);
829 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
830 child_err = got_privsep_wait_for_child(
831 repo->privsep_children[i].pid);
832 if (child_err && err == NULL)
833 err = child_err;
834 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
835 err == NULL)
836 err = got_error_from_errno("close");
839 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
840 err == NULL)
841 err = got_error_from_errno("close");
843 if (repo->gotconfig)
844 got_gotconfig_free(repo->gotconfig);
845 free(repo->gitconfig_author_name);
846 free(repo->gitconfig_author_email);
847 for (i = 0; i < repo->ngitconfig_remotes; i++)
848 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
849 free(repo->gitconfig_remotes);
850 for (i = 0; i < repo->nextensions; i++)
851 free(repo->extensions[i]);
852 free(repo->extensions);
854 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
855 free((void *)pe->path);
856 got_pathlist_free(&repo->packidx_paths);
857 free(repo);
859 return err;
862 void
863 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
865 int i;
867 free(repo->name);
868 repo->name = NULL;
869 free(repo->fetch_url);
870 repo->fetch_url = NULL;
871 free(repo->send_url);
872 repo->send_url = NULL;
873 for (i = 0; i < repo->nfetch_branches; i++)
874 free(repo->fetch_branches[i]);
875 free(repo->fetch_branches);
876 repo->fetch_branches = NULL;
877 repo->nfetch_branches = 0;
878 for (i = 0; i < repo->nsend_branches; i++)
879 free(repo->send_branches[i]);
880 free(repo->send_branches);
881 repo->send_branches = NULL;
882 repo->nsend_branches = 0;
885 const struct got_error *
886 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
887 const char *input_path)
889 const struct got_error *err = NULL;
890 const char *repo_abspath = NULL;
891 size_t repolen, len;
892 char *canonpath, *path = NULL;
894 *in_repo_path = NULL;
896 canonpath = strdup(input_path);
897 if (canonpath == NULL) {
898 err = got_error_from_errno("strdup");
899 goto done;
901 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
902 if (err)
903 goto done;
905 repo_abspath = got_repo_get_path(repo);
907 if (canonpath[0] == '\0') {
908 path = strdup(canonpath);
909 if (path == NULL) {
910 err = got_error_from_errno("strdup");
911 goto done;
913 } else {
914 path = realpath(canonpath, NULL);
915 if (path == NULL) {
916 if (errno != ENOENT) {
917 err = got_error_from_errno2("realpath",
918 canonpath);
919 goto done;
921 /*
922 * Path is not on disk.
923 * Assume it is already relative to repository root.
924 */
925 path = strdup(canonpath);
926 if (path == NULL) {
927 err = got_error_from_errno("strdup");
928 goto done;
932 repolen = strlen(repo_abspath);
933 len = strlen(path);
936 if (strcmp(path, repo_abspath) == 0) {
937 free(path);
938 path = strdup("");
939 if (path == NULL) {
940 err = got_error_from_errno("strdup");
941 goto done;
943 } else if (len > repolen &&
944 got_path_is_child(path, repo_abspath, repolen)) {
945 /* Matched an on-disk path inside repository. */
946 if (got_repo_is_bare(repo)) {
947 /*
948 * Matched an on-disk path inside repository
949 * database. Treat input as repository-relative.
950 */
951 free(path);
952 path = canonpath;
953 canonpath = NULL;
954 } else {
955 char *child;
956 /* Strip common prefix with repository path. */
957 err = got_path_skip_common_ancestor(&child,
958 repo_abspath, path);
959 if (err)
960 goto done;
961 free(path);
962 path = child;
964 } else {
965 /*
966 * Matched unrelated on-disk path.
967 * Treat input as repository-relative.
968 */
969 free(path);
970 path = canonpath;
971 canonpath = NULL;
975 /* Make in-repository path absolute */
976 if (path[0] != '/') {
977 char *abspath;
978 if (asprintf(&abspath, "/%s", path) == -1) {
979 err = got_error_from_errno("asprintf");
980 goto done;
982 free(path);
983 path = abspath;
986 done:
987 free(canonpath);
988 if (err)
989 free(path);
990 else
991 *in_repo_path = path;
992 return err;
995 static const struct got_error *
996 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
997 const char *path_packidx)
999 const struct got_error *err = NULL;
1000 size_t i;
1002 for (i = 0; i < repo->pack_cache_size; i++) {
1003 if (repo->packidx_cache[i] == NULL)
1004 break;
1005 if (strcmp(repo->packidx_cache[i]->path_packidx,
1006 path_packidx) == 0) {
1007 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1010 if (i == repo->pack_cache_size) {
1011 i = repo->pack_cache_size - 1;
1012 err = got_packidx_close(repo->packidx_cache[i]);
1013 if (err)
1014 return err;
1017 repo->packidx_cache[i] = packidx;
1019 return NULL;
1022 int
1023 got_repo_is_packidx_filename(const char *name, size_t len)
1025 if (len != GOT_PACKIDX_NAMELEN)
1026 return 0;
1028 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1029 return 0;
1031 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1032 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1033 return 0;
1035 return 1;
1038 static struct got_packidx_bloom_filter *
1039 get_packidx_bloom_filter(struct got_repository *repo,
1040 const char *path, size_t path_len)
1042 struct got_packidx_bloom_filter key;
1044 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1045 return NULL; /* XXX */
1046 key.path_len = path_len;
1048 return RB_FIND(got_packidx_bloom_filter_tree,
1049 &repo->packidx_bloom_filters, &key);
1052 int
1053 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1054 const char *path_packidx, struct got_object_id *id)
1056 struct got_packidx_bloom_filter *bf;
1058 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1059 if (bf)
1060 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1062 /* No bloom filter means this pack index must be searched. */
1063 return 1;
1066 static const struct got_error *
1067 add_packidx_bloom_filter(struct got_repository *repo,
1068 struct got_packidx *packidx, const char *path_packidx)
1070 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1071 struct got_packidx_bloom_filter *bf;
1072 size_t len;
1075 * Don't use bloom filters for very large pack index files.
1076 * Large pack files will contain a relatively large fraction
1077 * of our objects so we will likely need to visit them anyway.
1078 * The more objects a pack file contains the higher the probability
1079 * of a false-positive match from the bloom filter. And reading
1080 * all object IDs from a large pack index file can be expensive.
1082 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1083 return NULL;
1085 /* Do we already have a filter for this pack index? */
1086 if (get_packidx_bloom_filter(repo, path_packidx,
1087 strlen(path_packidx)) != NULL)
1088 return NULL;
1090 bf = calloc(1, sizeof(*bf));
1091 if (bf == NULL)
1092 return got_error_from_errno("calloc");
1093 bf->bloom = calloc(1, sizeof(*bf->bloom));
1094 if (bf->bloom == NULL) {
1095 free(bf);
1096 return got_error_from_errno("calloc");
1099 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1100 if (len >= sizeof(bf->path)) {
1101 free(bf->bloom);
1102 free(bf);
1103 return got_error(GOT_ERR_NO_SPACE);
1105 bf->path_len = len;
1107 /* Minimum size supported by our bloom filter is 1000 entries. */
1108 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1109 for (i = 0; i < nobjects; i++) {
1110 struct got_packidx_object_id *id;
1111 id = &packidx->hdr.sorted_ids[i];
1112 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1115 RB_INSERT(got_packidx_bloom_filter_tree,
1116 &repo->packidx_bloom_filters, bf);
1117 return NULL;
1120 const struct got_error *
1121 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1122 struct got_repository *repo, struct got_object_id *id)
1124 const struct got_error *err;
1125 struct got_pathlist_entry *pe;
1126 size_t i;
1128 /* Search pack index cache. */
1129 for (i = 0; i < repo->pack_cache_size; i++) {
1130 if (repo->packidx_cache[i] == NULL)
1131 break;
1132 if (!got_repo_check_packidx_bloom_filter(repo,
1133 repo->packidx_cache[i]->path_packidx, id))
1134 continue; /* object will not be found in this index */
1135 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1136 if (*idx != -1) {
1137 *packidx = repo->packidx_cache[i];
1139 * Move this cache entry to the front. Repeatedly
1140 * searching a wrong pack index can be expensive.
1142 if (i > 0) {
1143 memmove(&repo->packidx_cache[1],
1144 &repo->packidx_cache[0],
1145 i * sizeof(repo->packidx_cache[0]));
1146 repo->packidx_cache[0] = *packidx;
1148 return NULL;
1151 /* No luck. Search the filesystem. */
1153 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1154 const char *path_packidx = pe->path;
1155 int is_cached = 0;
1157 if (!got_repo_check_packidx_bloom_filter(repo,
1158 pe->path, id))
1159 continue; /* object will not be found in this index */
1161 for (i = 0; i < repo->pack_cache_size; i++) {
1162 if (repo->packidx_cache[i] == NULL)
1163 break;
1164 if (strcmp(repo->packidx_cache[i]->path_packidx,
1165 path_packidx) == 0) {
1166 is_cached = 1;
1167 break;
1170 if (is_cached)
1171 continue; /* already searched */
1173 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1174 path_packidx, 0);
1175 if (err)
1176 goto done;
1178 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1179 if (err)
1180 goto done;
1182 err = cache_packidx(repo, *packidx, path_packidx);
1183 if (err)
1184 goto done;
1186 *idx = got_packidx_get_object_idx(*packidx, id);
1187 if (*idx != -1) {
1188 err = NULL; /* found the object */
1189 goto done;
1193 err = got_error_no_obj(id);
1194 done:
1195 return err;
1198 const struct got_error *
1199 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1200 struct got_repository *repo)
1202 const struct got_error *err = NULL;
1203 DIR *packdir = NULL;
1204 struct dirent *dent;
1205 char *path_packidx = NULL;
1206 int packdir_fd;
1208 packdir_fd = openat(got_repo_get_fd(repo),
1209 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1210 if (packdir_fd == -1) {
1211 return got_error_from_errno_fmt("openat: %s/%s",
1212 got_repo_get_path_git_dir(repo),
1213 GOT_OBJECTS_PACK_DIR);
1216 packdir = fdopendir(packdir_fd);
1217 if (packdir == NULL) {
1218 err = got_error_from_errno("fdopendir");
1219 goto done;
1222 while ((dent = readdir(packdir)) != NULL) {
1223 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1224 continue;
1226 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1227 dent->d_name) == -1) {
1228 err = got_error_from_errno("asprintf");
1229 path_packidx = NULL;
1230 break;
1233 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1234 if (err)
1235 break;
1237 done:
1238 if (err)
1239 free(path_packidx);
1240 if (packdir && closedir(packdir) != 0 && err == NULL)
1241 err = got_error_from_errno("closedir");
1242 return err;
1245 const struct got_error *
1246 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1247 struct got_repository *repo)
1249 const struct got_error *err;
1250 size_t i;
1252 *packidx = NULL;
1254 /* Search pack index cache. */
1255 for (i = 0; i < repo->pack_cache_size; i++) {
1256 if (repo->packidx_cache[i] == NULL)
1257 break;
1258 if (strcmp(repo->packidx_cache[i]->path_packidx,
1259 path_packidx) == 0) {
1260 *packidx = repo->packidx_cache[i];
1261 return NULL;
1264 /* No luck. Search the filesystem. */
1266 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1267 path_packidx, 0);
1268 if (err)
1269 return err;
1271 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1272 if (err)
1273 goto done;
1275 err = cache_packidx(repo, *packidx, path_packidx);
1276 done:
1277 if (err) {
1278 got_packidx_close(*packidx);
1279 *packidx = NULL;
1281 return err;
1284 static const struct got_error *
1285 read_packfile_hdr(int fd, struct got_packidx *packidx)
1287 const struct got_error *err = NULL;
1288 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1289 struct got_packfile_hdr hdr;
1290 ssize_t n;
1292 n = read(fd, &hdr, sizeof(hdr));
1293 if (n < 0)
1294 return got_error_from_errno("read");
1295 if (n != sizeof(hdr))
1296 return got_error(GOT_ERR_BAD_PACKFILE);
1298 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1299 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1300 be32toh(hdr.nobjects) != totobj)
1301 err = got_error(GOT_ERR_BAD_PACKFILE);
1303 return err;
1306 static const struct got_error *
1307 open_packfile(int *fd, struct got_repository *repo,
1308 const char *relpath, struct got_packidx *packidx)
1310 const struct got_error *err = NULL;
1312 *fd = openat(got_repo_get_fd(repo), relpath,
1313 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1314 if (*fd == -1)
1315 return got_error_from_errno_fmt("openat: %s/%s",
1316 got_repo_get_path_git_dir(repo), relpath);
1318 if (packidx) {
1319 err = read_packfile_hdr(*fd, packidx);
1320 if (err) {
1321 close(*fd);
1322 *fd = -1;
1326 return err;
1329 const struct got_error *
1330 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1331 const char *path_packfile, struct got_packidx *packidx)
1333 const struct got_error *err = NULL;
1334 struct got_pack *pack = NULL;
1335 struct stat sb;
1336 size_t i;
1338 if (packp)
1339 *packp = NULL;
1341 for (i = 0; i < repo->pack_cache_size; i++) {
1342 pack = &repo->packs[i];
1343 if (pack->path_packfile == NULL)
1344 break;
1345 if (strcmp(pack->path_packfile, path_packfile) == 0)
1346 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1349 if (i == repo->pack_cache_size) {
1350 err = got_pack_close(&repo->packs[i - 1]);
1351 if (err)
1352 return err;
1353 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1354 return got_error_from_errno("ftruncate");
1355 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1356 return got_error_from_errno("ftruncate");
1357 memmove(&repo->packs[1], &repo->packs[0],
1358 sizeof(repo->packs) - sizeof(repo->packs[0]));
1359 i = 0;
1362 pack = &repo->packs[i];
1364 pack->path_packfile = strdup(path_packfile);
1365 if (pack->path_packfile == NULL) {
1366 err = got_error_from_errno("strdup");
1367 goto done;
1370 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1371 if (err)
1372 goto done;
1374 if (fstat(pack->fd, &sb) != 0) {
1375 err = got_error_from_errno("fstat");
1376 goto done;
1378 pack->filesize = sb.st_size;
1380 pack->privsep_child = NULL;
1382 #ifndef GOT_PACK_NO_MMAP
1383 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1384 pack->fd, 0);
1385 if (pack->map == MAP_FAILED) {
1386 if (errno != ENOMEM) {
1387 err = got_error_from_errno("mmap");
1388 goto done;
1390 pack->map = NULL; /* fall back to read(2) */
1392 #endif
1393 done:
1394 if (err) {
1395 if (pack) {
1396 free(pack->path_packfile);
1397 memset(pack, 0, sizeof(*pack));
1399 } else if (packp)
1400 *packp = pack;
1401 return err;
1404 struct got_pack *
1405 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1407 struct got_pack *pack = NULL;
1408 size_t i;
1410 for (i = 0; i < repo->pack_cache_size; i++) {
1411 pack = &repo->packs[i];
1412 if (pack->path_packfile == NULL)
1413 break;
1414 if (strcmp(pack->path_packfile, path_packfile) == 0)
1415 return pack;
1418 return NULL;
1421 const struct got_error *
1422 got_repo_init(const char *repo_path)
1424 const struct got_error *err = NULL;
1425 const char *dirnames[] = {
1426 GOT_OBJECTS_DIR,
1427 GOT_OBJECTS_PACK_DIR,
1428 GOT_REFS_DIR,
1430 const char *description_str = "Unnamed repository; "
1431 "edit this file 'description' to name the repository.";
1432 const char *headref_str = "ref: refs/heads/main";
1433 const char *gitconfig_str = "[core]\n"
1434 "\trepositoryformatversion = 0\n"
1435 "\tfilemode = true\n"
1436 "\tbare = true\n";
1437 char *path;
1438 size_t i;
1440 if (!got_path_dir_is_empty(repo_path))
1441 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1443 for (i = 0; i < nitems(dirnames); i++) {
1444 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1445 return got_error_from_errno("asprintf");
1447 err = got_path_mkdir(path);
1448 free(path);
1449 if (err)
1450 return err;
1453 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1454 return got_error_from_errno("asprintf");
1455 err = got_path_create_file(path, description_str);
1456 free(path);
1457 if (err)
1458 return err;
1460 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1461 return got_error_from_errno("asprintf");
1462 err = got_path_create_file(path, headref_str);
1463 free(path);
1464 if (err)
1465 return err;
1467 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1468 return got_error_from_errno("asprintf");
1469 err = got_path_create_file(path, gitconfig_str);
1470 free(path);
1471 if (err)
1472 return err;
1474 return NULL;
1477 static const struct got_error *
1478 match_packed_object(struct got_object_id **unique_id,
1479 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1481 const struct got_error *err = NULL;
1482 struct got_object_id_queue matched_ids;
1483 struct got_pathlist_entry *pe;
1485 STAILQ_INIT(&matched_ids);
1487 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1488 const char *path_packidx = pe->path;
1489 struct got_packidx *packidx;
1490 struct got_object_qid *qid;
1492 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1493 path_packidx, 0);
1494 if (err)
1495 break;
1497 err = got_packidx_match_id_str_prefix(&matched_ids,
1498 packidx, id_str_prefix);
1499 if (err) {
1500 got_packidx_close(packidx);
1501 break;
1503 err = got_packidx_close(packidx);
1504 if (err)
1505 break;
1507 STAILQ_FOREACH(qid, &matched_ids, entry) {
1508 if (obj_type != GOT_OBJ_TYPE_ANY) {
1509 int matched_type;
1510 err = got_object_get_type(&matched_type, repo,
1511 &qid->id);
1512 if (err)
1513 goto done;
1514 if (matched_type != obj_type)
1515 continue;
1517 if (*unique_id == NULL) {
1518 *unique_id = got_object_id_dup(&qid->id);
1519 if (*unique_id == NULL) {
1520 err = got_error_from_errno("malloc");
1521 goto done;
1523 } else {
1524 if (got_object_id_cmp(*unique_id,
1525 &qid->id) == 0)
1526 continue; /* packed multiple times */
1527 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1528 goto done;
1532 done:
1533 got_object_id_queue_free(&matched_ids);
1534 if (err) {
1535 free(*unique_id);
1536 *unique_id = NULL;
1538 return err;
1541 static const struct got_error *
1542 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1543 const char *object_dir, const char *id_str_prefix, int obj_type,
1544 struct got_repository *repo)
1546 const struct got_error *err = NULL;
1547 char *path;
1548 DIR *dir = NULL;
1549 struct dirent *dent;
1550 struct got_object_id id;
1552 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1553 err = got_error_from_errno("asprintf");
1554 goto done;
1557 dir = opendir(path);
1558 if (dir == NULL) {
1559 if (errno == ENOENT) {
1560 err = NULL;
1561 goto done;
1563 err = got_error_from_errno2("opendir", path);
1564 goto done;
1566 while ((dent = readdir(dir)) != NULL) {
1567 char *id_str;
1568 int cmp;
1570 if (strcmp(dent->d_name, ".") == 0 ||
1571 strcmp(dent->d_name, "..") == 0)
1572 continue;
1574 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1575 err = got_error_from_errno("asprintf");
1576 goto done;
1579 if (!got_parse_sha1_digest(id.sha1, id_str))
1580 continue;
1583 * Directory entries do not necessarily appear in
1584 * sorted order, so we must iterate over all of them.
1586 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1587 if (cmp != 0) {
1588 free(id_str);
1589 continue;
1592 if (*unique_id == NULL) {
1593 if (obj_type != GOT_OBJ_TYPE_ANY) {
1594 int matched_type;
1595 err = got_object_get_type(&matched_type, repo,
1596 &id);
1597 if (err)
1598 goto done;
1599 if (matched_type != obj_type)
1600 continue;
1602 *unique_id = got_object_id_dup(&id);
1603 if (*unique_id == NULL) {
1604 err = got_error_from_errno("got_object_id_dup");
1605 free(id_str);
1606 goto done;
1608 } else {
1609 if (got_object_id_cmp(*unique_id, &id) == 0)
1610 continue; /* both packed and loose */
1611 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1612 free(id_str);
1613 goto done;
1616 done:
1617 if (dir && closedir(dir) != 0 && err == NULL)
1618 err = got_error_from_errno("closedir");
1619 if (err) {
1620 free(*unique_id);
1621 *unique_id = NULL;
1623 free(path);
1624 return err;
1627 const struct got_error *
1628 got_repo_match_object_id_prefix(struct got_object_id **id,
1629 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1631 const struct got_error *err = NULL;
1632 char *path_objects = got_repo_get_path_objects(repo);
1633 char *object_dir = NULL;
1634 size_t len;
1635 int i;
1637 *id = NULL;
1639 len = strlen(id_str_prefix);
1640 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1641 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1643 for (i = 0; i < len; i++) {
1644 if (isxdigit((unsigned char)id_str_prefix[i]))
1645 continue;
1646 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1649 if (len >= 2) {
1650 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1651 if (err)
1652 goto done;
1653 object_dir = strndup(id_str_prefix, 2);
1654 if (object_dir == NULL) {
1655 err = got_error_from_errno("strdup");
1656 goto done;
1658 err = match_loose_object(id, path_objects, object_dir,
1659 id_str_prefix, obj_type, repo);
1660 } else if (len == 1) {
1661 int i;
1662 for (i = 0; i < 0xf; i++) {
1663 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1664 == -1) {
1665 err = got_error_from_errno("asprintf");
1666 goto done;
1668 err = match_packed_object(id, repo, object_dir,
1669 obj_type);
1670 if (err)
1671 goto done;
1672 err = match_loose_object(id, path_objects, object_dir,
1673 id_str_prefix, obj_type, repo);
1674 if (err)
1675 goto done;
1677 } else {
1678 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1679 goto done;
1681 done:
1682 free(object_dir);
1683 if (err) {
1684 free(*id);
1685 *id = NULL;
1686 } else if (*id == NULL) {
1687 switch (obj_type) {
1688 case GOT_OBJ_TYPE_BLOB:
1689 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1690 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1691 break;
1692 case GOT_OBJ_TYPE_TREE:
1693 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1694 GOT_OBJ_LABEL_TREE, id_str_prefix);
1695 break;
1696 case GOT_OBJ_TYPE_COMMIT:
1697 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1698 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1699 break;
1700 case GOT_OBJ_TYPE_TAG:
1701 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1702 GOT_OBJ_LABEL_TAG, id_str_prefix);
1703 break;
1704 default:
1705 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1706 break;
1710 return err;
1713 const struct got_error *
1714 got_repo_match_object_id(struct got_object_id **id, char **label,
1715 const char *id_str, int obj_type, struct got_reflist_head *refs,
1716 struct got_repository *repo)
1718 const struct got_error *err;
1719 struct got_tag_object *tag;
1720 struct got_reference *ref = NULL;
1722 *id = NULL;
1723 if (label)
1724 *label = NULL;
1726 if (refs) {
1727 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1728 refs, repo);
1729 if (err == NULL) {
1730 *id = got_object_id_dup(
1731 got_object_tag_get_object_id(tag));
1732 if (*id == NULL)
1733 err = got_error_from_errno("got_object_id_dup");
1734 else if (label && asprintf(label, "refs/tags/%s",
1735 got_object_tag_get_name(tag)) == -1) {
1736 err = got_error_from_errno("asprintf");
1737 free(*id);
1738 *id = NULL;
1740 got_object_tag_close(tag);
1741 return err;
1742 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1743 err->code != GOT_ERR_NO_OBJ)
1744 return err;
1747 err = got_ref_open(&ref, repo, id_str, 0);
1748 if (err == NULL) {
1749 err = got_ref_resolve(id, repo, ref);
1750 if (err)
1751 goto done;
1752 if (label) {
1753 *label = strdup(got_ref_get_name(ref));
1754 if (*label == NULL) {
1755 err = got_error_from_errno("strdup");
1756 goto done;
1759 } else {
1760 if (err->code != GOT_ERR_NOT_REF &&
1761 err->code != GOT_ERR_BAD_REF_NAME)
1762 goto done;
1763 err = got_repo_match_object_id_prefix(id, id_str,
1764 obj_type, repo);
1765 if (err) {
1766 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1767 err = got_error_not_ref(id_str);
1768 goto done;
1770 if (label) {
1771 err = got_object_id_str(label, *id);
1772 if (*label == NULL) {
1773 err = got_error_from_errno("strdup");
1774 goto done;
1778 done:
1779 if (ref)
1780 got_ref_close(ref);
1781 return err;
1784 const struct got_error *
1785 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1786 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1788 const struct got_error *err = NULL;
1789 struct got_reflist_entry *re;
1790 struct got_object_id *tag_id;
1791 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1793 *tag = NULL;
1795 TAILQ_FOREACH(re, refs, entry) {
1796 const char *refname;
1797 refname = got_ref_get_name(re->ref);
1798 if (got_ref_is_symbolic(re->ref))
1799 continue;
1800 if (strncmp(refname, "refs/tags/", 10) != 0)
1801 continue;
1802 if (!name_is_absolute)
1803 refname += strlen("refs/tags/");
1804 if (strcmp(refname, name) != 0)
1805 continue;
1806 err = got_ref_resolve(&tag_id, repo, re->ref);
1807 if (err)
1808 break;
1809 err = got_object_open_as_tag(tag, repo, tag_id);
1810 free(tag_id);
1811 if (err)
1812 break;
1813 if (obj_type == GOT_OBJ_TYPE_ANY ||
1814 got_object_tag_get_object_type(*tag) == obj_type)
1815 break;
1816 got_object_tag_close(*tag);
1817 *tag = NULL;
1820 if (err == NULL && *tag == NULL)
1821 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1822 GOT_OBJ_LABEL_TAG, name);
1823 return err;
1826 static const struct got_error *
1827 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1828 const char *name, mode_t mode, struct got_object_id *blob_id)
1830 const struct got_error *err = NULL;
1832 *new_te = NULL;
1834 *new_te = calloc(1, sizeof(**new_te));
1835 if (*new_te == NULL)
1836 return got_error_from_errno("calloc");
1838 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1839 sizeof((*new_te)->name)) {
1840 err = got_error(GOT_ERR_NO_SPACE);
1841 goto done;
1844 if (S_ISLNK(mode)) {
1845 (*new_te)->mode = S_IFLNK;
1846 } else {
1847 (*new_te)->mode = S_IFREG;
1848 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1850 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1851 done:
1852 if (err && *new_te) {
1853 free(*new_te);
1854 *new_te = NULL;
1856 return err;
1859 static const struct got_error *
1860 import_file(struct got_tree_entry **new_te, struct dirent *de,
1861 const char *path, struct got_repository *repo)
1863 const struct got_error *err;
1864 struct got_object_id *blob_id = NULL;
1865 char *filepath;
1866 struct stat sb;
1868 if (asprintf(&filepath, "%s%s%s", path,
1869 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1870 return got_error_from_errno("asprintf");
1872 if (lstat(filepath, &sb) != 0) {
1873 err = got_error_from_errno2("lstat", path);
1874 goto done;
1877 err = got_object_blob_create(&blob_id, filepath, repo);
1878 if (err)
1879 goto done;
1881 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1882 blob_id);
1883 done:
1884 free(filepath);
1885 if (err)
1886 free(blob_id);
1887 return err;
1890 static const struct got_error *
1891 insert_tree_entry(struct got_tree_entry *new_te,
1892 struct got_pathlist_head *paths)
1894 const struct got_error *err = NULL;
1895 struct got_pathlist_entry *new_pe;
1897 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1898 if (err)
1899 return err;
1900 if (new_pe == NULL)
1901 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1902 return NULL;
1905 static const struct got_error *write_tree(struct got_object_id **,
1906 const char *, struct got_pathlist_head *, struct got_repository *,
1907 got_repo_import_cb progress_cb, void *progress_arg);
1909 static const struct got_error *
1910 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1911 const char *path, struct got_pathlist_head *ignores,
1912 struct got_repository *repo,
1913 got_repo_import_cb progress_cb, void *progress_arg)
1915 const struct got_error *err;
1916 struct got_object_id *id = NULL;
1917 char *subdirpath;
1919 if (asprintf(&subdirpath, "%s%s%s", path,
1920 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1921 return got_error_from_errno("asprintf");
1923 (*new_te) = calloc(1, sizeof(**new_te));
1924 if (*new_te == NULL)
1925 return got_error_from_errno("calloc");
1926 (*new_te)->mode = S_IFDIR;
1927 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1928 sizeof((*new_te)->name)) {
1929 err = got_error(GOT_ERR_NO_SPACE);
1930 goto done;
1932 err = write_tree(&id, subdirpath, ignores, repo,
1933 progress_cb, progress_arg);
1934 if (err)
1935 goto done;
1936 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1938 done:
1939 free(id);
1940 free(subdirpath);
1941 if (err) {
1942 free(*new_te);
1943 *new_te = NULL;
1945 return err;
1948 static const struct got_error *
1949 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1950 struct got_pathlist_head *ignores, struct got_repository *repo,
1951 got_repo_import_cb progress_cb, void *progress_arg)
1953 const struct got_error *err = NULL;
1954 DIR *dir;
1955 struct dirent *de;
1956 int nentries;
1957 struct got_tree_entry *new_te = NULL;
1958 struct got_pathlist_head paths;
1959 struct got_pathlist_entry *pe;
1961 *new_tree_id = NULL;
1963 TAILQ_INIT(&paths);
1965 dir = opendir(path_dir);
1966 if (dir == NULL) {
1967 err = got_error_from_errno2("opendir", path_dir);
1968 goto done;
1971 nentries = 0;
1972 while ((de = readdir(dir)) != NULL) {
1973 int ignore = 0;
1974 int type;
1976 if (strcmp(de->d_name, ".") == 0 ||
1977 strcmp(de->d_name, "..") == 0)
1978 continue;
1980 TAILQ_FOREACH(pe, ignores, entry) {
1981 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1982 ignore = 1;
1983 break;
1986 if (ignore)
1987 continue;
1989 err = got_path_dirent_type(&type, path_dir, de);
1990 if (err)
1991 goto done;
1993 if (type == DT_DIR) {
1994 err = import_subdir(&new_te, de, path_dir,
1995 ignores, repo, progress_cb, progress_arg);
1996 if (err) {
1997 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1998 goto done;
1999 err = NULL;
2000 continue;
2002 } else if (type == DT_REG || type == DT_LNK) {
2003 err = import_file(&new_te, de, path_dir, repo);
2004 if (err)
2005 goto done;
2006 } else
2007 continue;
2009 err = insert_tree_entry(new_te, &paths);
2010 if (err)
2011 goto done;
2012 nentries++;
2015 if (TAILQ_EMPTY(&paths)) {
2016 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2017 "cannot create tree without any entries");
2018 goto done;
2021 TAILQ_FOREACH(pe, &paths, entry) {
2022 struct got_tree_entry *te = pe->data;
2023 char *path;
2024 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2025 continue;
2026 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2027 err = got_error_from_errno("asprintf");
2028 goto done;
2030 err = (*progress_cb)(progress_arg, path);
2031 free(path);
2032 if (err)
2033 goto done;
2036 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2037 done:
2038 if (dir)
2039 closedir(dir);
2040 got_pathlist_free(&paths);
2041 return err;
2044 const struct got_error *
2045 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2046 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2047 struct got_repository *repo, got_repo_import_cb progress_cb,
2048 void *progress_arg)
2050 const struct got_error *err;
2051 struct got_object_id *new_tree_id;
2053 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2054 progress_cb, progress_arg);
2055 if (err)
2056 return err;
2058 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2059 author, time(NULL), author, time(NULL), logmsg, repo);
2060 free(new_tree_id);
2061 return err;
2064 const struct got_error *
2065 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2066 struct got_repository *repo)
2068 const struct got_error *err = NULL;
2069 char *path_objects = NULL, *path = NULL;
2070 DIR *dir = NULL;
2071 struct got_object_id id;
2072 int i;
2074 *nobjects = 0;
2075 *ondisk_size = 0;
2077 path_objects = got_repo_get_path_objects(repo);
2078 if (path_objects == NULL)
2079 return got_error_from_errno("got_repo_get_path_objects");
2081 for (i = 0; i <= 0xff; i++) {
2082 struct dirent *dent;
2084 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2085 err = got_error_from_errno("asprintf");
2086 break;
2089 dir = opendir(path);
2090 if (dir == NULL) {
2091 if (errno == ENOENT) {
2092 err = NULL;
2093 continue;
2095 err = got_error_from_errno2("opendir", path);
2096 break;
2099 while ((dent = readdir(dir)) != NULL) {
2100 char *id_str;
2101 int fd;
2102 struct stat sb;
2104 if (strcmp(dent->d_name, ".") == 0 ||
2105 strcmp(dent->d_name, "..") == 0)
2106 continue;
2108 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2109 err = got_error_from_errno("asprintf");
2110 goto done;
2113 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2114 free(id_str);
2115 continue;
2117 free(id_str);
2119 err = got_object_open_loose_fd(&fd, &id, repo);
2120 if (err)
2121 goto done;
2123 if (fstat(fd, &sb) == -1) {
2124 err = got_error_from_errno("fstat");
2125 close(fd);
2126 goto done;
2128 (*nobjects)++;
2129 (*ondisk_size) += sb.st_size;
2131 if (close(fd) == -1) {
2132 err = got_error_from_errno("close");
2133 goto done;
2137 if (closedir(dir) != 0) {
2138 err = got_error_from_errno("closedir");
2139 goto done;
2141 dir = NULL;
2143 free(path);
2144 path = NULL;
2146 done:
2147 if (dir && closedir(dir) != 0 && err == NULL)
2148 err = got_error_from_errno("closedir");
2150 if (err) {
2151 *nobjects = 0;
2152 *ondisk_size = 0;
2154 free(path_objects);
2155 free(path);
2156 return err;
2159 const struct got_error *
2160 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2161 off_t *total_packsize, struct got_repository *repo)
2163 const struct got_error *err = NULL;
2164 DIR *packdir = NULL;
2165 struct dirent *dent;
2166 struct got_packidx *packidx = NULL;
2167 char *path_packidx;
2168 char *path_packfile;
2169 int packdir_fd;
2170 struct stat sb;
2172 *npackfiles = 0;
2173 *nobjects = 0;
2174 *total_packsize = 0;
2176 packdir_fd = openat(got_repo_get_fd(repo),
2177 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2178 if (packdir_fd == -1) {
2179 return got_error_from_errno_fmt("openat: %s/%s",
2180 got_repo_get_path_git_dir(repo),
2181 GOT_OBJECTS_PACK_DIR);
2184 packdir = fdopendir(packdir_fd);
2185 if (packdir == NULL) {
2186 err = got_error_from_errno("fdopendir");
2187 goto done;
2190 while ((dent = readdir(packdir)) != NULL) {
2191 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2192 continue;
2194 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2195 dent->d_name) == -1) {
2196 err = got_error_from_errno("asprintf");
2197 goto done;
2200 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2201 path_packidx, 0);
2202 free(path_packidx);
2203 if (err)
2204 goto done;
2206 if (fstat(packidx->fd, &sb) == -1)
2207 goto done;
2208 *total_packsize += sb.st_size;
2210 err = got_packidx_get_packfile_path(&path_packfile,
2211 packidx->path_packidx);
2212 if (err)
2213 goto done;
2215 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2216 0) == -1) {
2217 free(path_packfile);
2218 goto done;
2220 free(path_packfile);
2221 *total_packsize += sb.st_size;
2223 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2225 (*npackfiles)++;
2227 got_packidx_close(packidx);
2228 packidx = NULL;
2230 done:
2231 if (packidx)
2232 got_packidx_close(packidx);
2233 if (packdir && closedir(packdir) != 0 && err == NULL)
2234 err = got_error_from_errno("closedir");
2235 if (err) {
2236 *npackfiles = 0;
2237 *nobjects = 0;
2238 *total_packsize = 0;
2240 return err;
2243 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2244 got_packidx_bloom_filter_cmp);