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)
801 got_pack_close(&repo->packs[i]);
802 if (repo->packs[i].basefd != -1) {
803 if (close(repo->packs[i].basefd) == -1 && err == NULL)
804 err = got_error_from_errno("close");
805 repo->packs[i].basefd = -1;
807 if (repo->packs[i].accumfd != -1) {
808 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 repo->packs[i].accumfd = -1;
814 free(repo->path);
815 free(repo->path_git_dir);
817 got_object_cache_close(&repo->objcache);
818 got_object_cache_close(&repo->treecache);
819 got_object_cache_close(&repo->commitcache);
820 got_object_cache_close(&repo->tagcache);
821 got_object_cache_close(&repo->rawcache);
823 for (i = 0; i < nitems(repo->privsep_children); i++) {
824 if (repo->privsep_children[i].imsg_fd == -1)
825 continue;
826 imsg_clear(repo->privsep_children[i].ibuf);
827 free(repo->privsep_children[i].ibuf);
828 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
829 child_err = got_privsep_wait_for_child(
830 repo->privsep_children[i].pid);
831 if (child_err && err == NULL)
832 err = child_err;
833 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
834 err == NULL)
835 err = got_error_from_errno("close");
838 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
839 err == NULL)
840 err = got_error_from_errno("close");
842 if (repo->gotconfig)
843 got_gotconfig_free(repo->gotconfig);
844 free(repo->gitconfig_author_name);
845 free(repo->gitconfig_author_email);
846 for (i = 0; i < repo->ngitconfig_remotes; i++)
847 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
848 free(repo->gitconfig_remotes);
849 for (i = 0; i < repo->nextensions; i++)
850 free(repo->extensions[i]);
851 free(repo->extensions);
853 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
854 free((void *)pe->path);
855 got_pathlist_free(&repo->packidx_paths);
856 free(repo);
858 return err;
861 void
862 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
864 int i;
866 free(repo->name);
867 repo->name = NULL;
868 free(repo->fetch_url);
869 repo->fetch_url = NULL;
870 free(repo->send_url);
871 repo->send_url = NULL;
872 for (i = 0; i < repo->nfetch_branches; i++)
873 free(repo->fetch_branches[i]);
874 free(repo->fetch_branches);
875 repo->fetch_branches = NULL;
876 repo->nfetch_branches = 0;
877 for (i = 0; i < repo->nsend_branches; i++)
878 free(repo->send_branches[i]);
879 free(repo->send_branches);
880 repo->send_branches = NULL;
881 repo->nsend_branches = 0;
884 const struct got_error *
885 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
886 const char *input_path)
888 const struct got_error *err = NULL;
889 const char *repo_abspath = NULL;
890 size_t repolen, len;
891 char *canonpath, *path = NULL;
893 *in_repo_path = NULL;
895 canonpath = strdup(input_path);
896 if (canonpath == NULL) {
897 err = got_error_from_errno("strdup");
898 goto done;
900 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
901 if (err)
902 goto done;
904 repo_abspath = got_repo_get_path(repo);
906 if (canonpath[0] == '\0') {
907 path = strdup(canonpath);
908 if (path == NULL) {
909 err = got_error_from_errno("strdup");
910 goto done;
912 } else {
913 path = realpath(canonpath, NULL);
914 if (path == NULL) {
915 if (errno != ENOENT) {
916 err = got_error_from_errno2("realpath",
917 canonpath);
918 goto done;
920 /*
921 * Path is not on disk.
922 * Assume it is already relative to repository root.
923 */
924 path = strdup(canonpath);
925 if (path == NULL) {
926 err = got_error_from_errno("strdup");
927 goto done;
931 repolen = strlen(repo_abspath);
932 len = strlen(path);
935 if (strcmp(path, repo_abspath) == 0) {
936 free(path);
937 path = strdup("");
938 if (path == NULL) {
939 err = got_error_from_errno("strdup");
940 goto done;
942 } else if (len > repolen &&
943 got_path_is_child(path, repo_abspath, repolen)) {
944 /* Matched an on-disk path inside repository. */
945 if (got_repo_is_bare(repo)) {
946 /*
947 * Matched an on-disk path inside repository
948 * database. Treat input as repository-relative.
949 */
950 free(path);
951 path = canonpath;
952 canonpath = NULL;
953 } else {
954 char *child;
955 /* Strip common prefix with repository path. */
956 err = got_path_skip_common_ancestor(&child,
957 repo_abspath, path);
958 if (err)
959 goto done;
960 free(path);
961 path = child;
963 } else {
964 /*
965 * Matched unrelated on-disk path.
966 * Treat input as repository-relative.
967 */
968 free(path);
969 path = canonpath;
970 canonpath = NULL;
974 /* Make in-repository path absolute */
975 if (path[0] != '/') {
976 char *abspath;
977 if (asprintf(&abspath, "/%s", path) == -1) {
978 err = got_error_from_errno("asprintf");
979 goto done;
981 free(path);
982 path = abspath;
985 done:
986 free(canonpath);
987 if (err)
988 free(path);
989 else
990 *in_repo_path = path;
991 return err;
994 static const struct got_error *
995 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
996 const char *path_packidx)
998 const struct got_error *err = NULL;
999 size_t i;
1001 for (i = 0; i < repo->pack_cache_size; i++) {
1002 if (repo->packidx_cache[i] == NULL)
1003 break;
1004 if (strcmp(repo->packidx_cache[i]->path_packidx,
1005 path_packidx) == 0) {
1006 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1009 if (i == repo->pack_cache_size) {
1010 i = repo->pack_cache_size - 1;
1011 err = got_packidx_close(repo->packidx_cache[i]);
1012 if (err)
1013 return err;
1016 repo->packidx_cache[i] = packidx;
1018 return NULL;
1021 int
1022 got_repo_is_packidx_filename(const char *name, size_t len)
1024 if (len != GOT_PACKIDX_NAMELEN)
1025 return 0;
1027 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1028 return 0;
1030 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1031 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1032 return 0;
1034 return 1;
1037 static struct got_packidx_bloom_filter *
1038 get_packidx_bloom_filter(struct got_repository *repo,
1039 const char *path, size_t path_len)
1041 struct got_packidx_bloom_filter key;
1043 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1044 return NULL; /* XXX */
1045 key.path_len = path_len;
1047 return RB_FIND(got_packidx_bloom_filter_tree,
1048 &repo->packidx_bloom_filters, &key);
1051 int
1052 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1053 const char *path_packidx, struct got_object_id *id)
1055 struct got_packidx_bloom_filter *bf;
1057 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1058 if (bf)
1059 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1061 /* No bloom filter means this pack index must be searched. */
1062 return 1;
1065 static const struct got_error *
1066 add_packidx_bloom_filter(struct got_repository *repo,
1067 struct got_packidx *packidx, const char *path_packidx)
1069 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1070 struct got_packidx_bloom_filter *bf;
1071 size_t len;
1074 * Don't use bloom filters for very large pack index files.
1075 * Large pack files will contain a relatively large fraction
1076 * of our objects so we will likely need to visit them anyway.
1077 * The more objects a pack file contains the higher the probability
1078 * of a false-positive match from the bloom filter. And reading
1079 * all object IDs from a large pack index file can be expensive.
1081 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1082 return NULL;
1084 /* Do we already have a filter for this pack index? */
1085 if (get_packidx_bloom_filter(repo, path_packidx,
1086 strlen(path_packidx)) != NULL)
1087 return NULL;
1089 bf = calloc(1, sizeof(*bf));
1090 if (bf == NULL)
1091 return got_error_from_errno("calloc");
1092 bf->bloom = calloc(1, sizeof(*bf->bloom));
1093 if (bf->bloom == NULL) {
1094 free(bf);
1095 return got_error_from_errno("calloc");
1098 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1099 if (len >= sizeof(bf->path)) {
1100 free(bf->bloom);
1101 free(bf);
1102 return got_error(GOT_ERR_NO_SPACE);
1104 bf->path_len = len;
1106 /* Minimum size supported by our bloom filter is 1000 entries. */
1107 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1108 for (i = 0; i < nobjects; i++) {
1109 struct got_packidx_object_id *id;
1110 id = &packidx->hdr.sorted_ids[i];
1111 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1114 RB_INSERT(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, bf);
1116 return NULL;
1119 const struct got_error *
1120 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1121 struct got_repository *repo, struct got_object_id *id)
1123 const struct got_error *err;
1124 struct got_pathlist_entry *pe;
1125 size_t i;
1127 /* Search pack index cache. */
1128 for (i = 0; i < repo->pack_cache_size; i++) {
1129 if (repo->packidx_cache[i] == NULL)
1130 break;
1131 if (!got_repo_check_packidx_bloom_filter(repo,
1132 repo->packidx_cache[i]->path_packidx, id))
1133 continue; /* object will not be found in this index */
1134 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1135 if (*idx != -1) {
1136 *packidx = repo->packidx_cache[i];
1138 * Move this cache entry to the front. Repeatedly
1139 * searching a wrong pack index can be expensive.
1141 if (i > 0) {
1142 memmove(&repo->packidx_cache[1],
1143 &repo->packidx_cache[0],
1144 i * sizeof(repo->packidx_cache[0]));
1145 repo->packidx_cache[0] = *packidx;
1147 return NULL;
1150 /* No luck. Search the filesystem. */
1152 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1153 const char *path_packidx = pe->path;
1154 int is_cached = 0;
1156 if (!got_repo_check_packidx_bloom_filter(repo,
1157 pe->path, id))
1158 continue; /* object will not be found in this index */
1160 for (i = 0; i < repo->pack_cache_size; i++) {
1161 if (repo->packidx_cache[i] == NULL)
1162 break;
1163 if (strcmp(repo->packidx_cache[i]->path_packidx,
1164 path_packidx) == 0) {
1165 is_cached = 1;
1166 break;
1169 if (is_cached)
1170 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1173 path_packidx, 0);
1174 if (err)
1175 goto done;
1177 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1178 if (err)
1179 goto done;
1181 err = cache_packidx(repo, *packidx, path_packidx);
1182 if (err)
1183 goto done;
1185 *idx = got_packidx_get_object_idx(*packidx, id);
1186 if (*idx != -1) {
1187 err = NULL; /* found the object */
1188 goto done;
1192 err = got_error_no_obj(id);
1193 done:
1194 return err;
1197 const struct got_error *
1198 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1202 DIR *packdir = NULL;
1203 struct dirent *dent;
1204 char *path_packidx = NULL;
1205 int packdir_fd;
1207 packdir_fd = openat(got_repo_get_fd(repo),
1208 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1209 if (packdir_fd == -1) {
1210 return got_error_from_errno_fmt("openat: %s/%s",
1211 got_repo_get_path_git_dir(repo),
1212 GOT_OBJECTS_PACK_DIR);
1215 packdir = fdopendir(packdir_fd);
1216 if (packdir == NULL) {
1217 err = got_error_from_errno("fdopendir");
1218 goto done;
1221 while ((dent = readdir(packdir)) != NULL) {
1222 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1223 continue;
1225 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1226 dent->d_name) == -1) {
1227 err = got_error_from_errno("asprintf");
1228 path_packidx = NULL;
1229 break;
1232 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1233 if (err)
1234 break;
1236 done:
1237 if (err)
1238 free(path_packidx);
1239 if (packdir && closedir(packdir) != 0 && err == NULL)
1240 err = got_error_from_errno("closedir");
1241 return err;
1244 const struct got_error *
1245 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1246 struct got_repository *repo)
1248 const struct got_error *err;
1249 size_t i;
1251 *packidx = NULL;
1253 /* Search pack index cache. */
1254 for (i = 0; i < repo->pack_cache_size; i++) {
1255 if (repo->packidx_cache[i] == NULL)
1256 break;
1257 if (strcmp(repo->packidx_cache[i]->path_packidx,
1258 path_packidx) == 0) {
1259 *packidx = repo->packidx_cache[i];
1260 return NULL;
1263 /* No luck. Search the filesystem. */
1265 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1266 path_packidx, 0);
1267 if (err)
1268 return err;
1270 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1271 if (err)
1272 goto done;
1274 err = cache_packidx(repo, *packidx, path_packidx);
1275 done:
1276 if (err) {
1277 got_packidx_close(*packidx);
1278 *packidx = NULL;
1280 return err;
1283 static const struct got_error *
1284 read_packfile_hdr(int fd, struct got_packidx *packidx)
1286 const struct got_error *err = NULL;
1287 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1288 struct got_packfile_hdr hdr;
1289 ssize_t n;
1291 n = read(fd, &hdr, sizeof(hdr));
1292 if (n < 0)
1293 return got_error_from_errno("read");
1294 if (n != sizeof(hdr))
1295 return got_error(GOT_ERR_BAD_PACKFILE);
1297 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1298 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1299 be32toh(hdr.nobjects) != totobj)
1300 err = got_error(GOT_ERR_BAD_PACKFILE);
1302 return err;
1305 static const struct got_error *
1306 open_packfile(int *fd, struct got_repository *repo,
1307 const char *relpath, struct got_packidx *packidx)
1309 const struct got_error *err = NULL;
1311 *fd = openat(got_repo_get_fd(repo), relpath,
1312 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1313 if (*fd == -1)
1314 return got_error_from_errno_fmt("openat: %s/%s",
1315 got_repo_get_path_git_dir(repo), relpath);
1317 if (packidx) {
1318 err = read_packfile_hdr(*fd, packidx);
1319 if (err) {
1320 close(*fd);
1321 *fd = -1;
1325 return err;
1328 const struct got_error *
1329 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1330 const char *path_packfile, struct got_packidx *packidx)
1332 const struct got_error *err = NULL;
1333 struct got_pack *pack = NULL;
1334 struct stat sb;
1335 size_t i;
1337 if (packp)
1338 *packp = NULL;
1340 for (i = 0; i < repo->pack_cache_size; i++) {
1341 pack = &repo->packs[i];
1342 if (pack->path_packfile == NULL)
1343 break;
1344 if (strcmp(pack->path_packfile, path_packfile) == 0)
1345 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1348 if (i == repo->pack_cache_size) {
1349 struct got_pack tmp;
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 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1358 memcpy(&repo->packs[i - 1], &repo->packs[0],
1359 sizeof(repo->packs[i - 1]));
1360 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1361 i = 0;
1364 pack = &repo->packs[i];
1366 pack->path_packfile = strdup(path_packfile);
1367 if (pack->path_packfile == NULL) {
1368 err = got_error_from_errno("strdup");
1369 goto done;
1372 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1373 if (err)
1374 goto done;
1376 if (fstat(pack->fd, &sb) != 0) {
1377 err = got_error_from_errno("fstat");
1378 goto done;
1380 pack->filesize = sb.st_size;
1382 pack->privsep_child = NULL;
1384 #ifndef GOT_PACK_NO_MMAP
1385 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1386 pack->fd, 0);
1387 if (pack->map == MAP_FAILED) {
1388 if (errno != ENOMEM) {
1389 err = got_error_from_errno("mmap");
1390 goto done;
1392 pack->map = NULL; /* fall back to read(2) */
1394 #endif
1395 done:
1396 if (err) {
1397 if (pack) {
1398 free(pack->path_packfile);
1399 memset(pack, 0, sizeof(*pack));
1401 } else if (packp)
1402 *packp = pack;
1403 return err;
1406 struct got_pack *
1407 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1409 struct got_pack *pack = NULL;
1410 size_t i;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1415 break;
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1417 return pack;
1420 return NULL;
1423 const struct got_error *
1424 got_repo_init(const char *repo_path)
1426 const struct got_error *err = NULL;
1427 const char *dirnames[] = {
1428 GOT_OBJECTS_DIR,
1429 GOT_OBJECTS_PACK_DIR,
1430 GOT_REFS_DIR,
1432 const char *description_str = "Unnamed repository; "
1433 "edit this file 'description' to name the repository.";
1434 const char *headref_str = "ref: refs/heads/main";
1435 const char *gitconfig_str = "[core]\n"
1436 "\trepositoryformatversion = 0\n"
1437 "\tfilemode = true\n"
1438 "\tbare = true\n";
1439 char *path;
1440 size_t i;
1442 if (!got_path_dir_is_empty(repo_path))
1443 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1445 for (i = 0; i < nitems(dirnames); i++) {
1446 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1447 return got_error_from_errno("asprintf");
1449 err = got_path_mkdir(path);
1450 free(path);
1451 if (err)
1452 return err;
1455 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1456 return got_error_from_errno("asprintf");
1457 err = got_path_create_file(path, description_str);
1458 free(path);
1459 if (err)
1460 return err;
1462 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1463 return got_error_from_errno("asprintf");
1464 err = got_path_create_file(path, headref_str);
1465 free(path);
1466 if (err)
1467 return err;
1469 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1470 return got_error_from_errno("asprintf");
1471 err = got_path_create_file(path, gitconfig_str);
1472 free(path);
1473 if (err)
1474 return err;
1476 return NULL;
1479 static const struct got_error *
1480 match_packed_object(struct got_object_id **unique_id,
1481 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1483 const struct got_error *err = NULL;
1484 struct got_object_id_queue matched_ids;
1485 struct got_pathlist_entry *pe;
1487 STAILQ_INIT(&matched_ids);
1489 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1490 const char *path_packidx = pe->path;
1491 struct got_packidx *packidx;
1492 struct got_object_qid *qid;
1494 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1495 path_packidx, 0);
1496 if (err)
1497 break;
1499 err = got_packidx_match_id_str_prefix(&matched_ids,
1500 packidx, id_str_prefix);
1501 if (err) {
1502 got_packidx_close(packidx);
1503 break;
1505 err = got_packidx_close(packidx);
1506 if (err)
1507 break;
1509 STAILQ_FOREACH(qid, &matched_ids, entry) {
1510 if (obj_type != GOT_OBJ_TYPE_ANY) {
1511 int matched_type;
1512 err = got_object_get_type(&matched_type, repo,
1513 &qid->id);
1514 if (err)
1515 goto done;
1516 if (matched_type != obj_type)
1517 continue;
1519 if (*unique_id == NULL) {
1520 *unique_id = got_object_id_dup(&qid->id);
1521 if (*unique_id == NULL) {
1522 err = got_error_from_errno("malloc");
1523 goto done;
1525 } else {
1526 if (got_object_id_cmp(*unique_id,
1527 &qid->id) == 0)
1528 continue; /* packed multiple times */
1529 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1530 goto done;
1534 done:
1535 got_object_id_queue_free(&matched_ids);
1536 if (err) {
1537 free(*unique_id);
1538 *unique_id = NULL;
1540 return err;
1543 static const struct got_error *
1544 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1545 const char *object_dir, const char *id_str_prefix, int obj_type,
1546 struct got_repository *repo)
1548 const struct got_error *err = NULL;
1549 char *path;
1550 DIR *dir = NULL;
1551 struct dirent *dent;
1552 struct got_object_id id;
1554 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1555 err = got_error_from_errno("asprintf");
1556 goto done;
1559 dir = opendir(path);
1560 if (dir == NULL) {
1561 if (errno == ENOENT) {
1562 err = NULL;
1563 goto done;
1565 err = got_error_from_errno2("opendir", path);
1566 goto done;
1568 while ((dent = readdir(dir)) != NULL) {
1569 char *id_str;
1570 int cmp;
1572 if (strcmp(dent->d_name, ".") == 0 ||
1573 strcmp(dent->d_name, "..") == 0)
1574 continue;
1576 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1577 err = got_error_from_errno("asprintf");
1578 goto done;
1581 if (!got_parse_sha1_digest(id.sha1, id_str))
1582 continue;
1585 * Directory entries do not necessarily appear in
1586 * sorted order, so we must iterate over all of them.
1588 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1589 if (cmp != 0) {
1590 free(id_str);
1591 continue;
1594 if (*unique_id == NULL) {
1595 if (obj_type != GOT_OBJ_TYPE_ANY) {
1596 int matched_type;
1597 err = got_object_get_type(&matched_type, repo,
1598 &id);
1599 if (err)
1600 goto done;
1601 if (matched_type != obj_type)
1602 continue;
1604 *unique_id = got_object_id_dup(&id);
1605 if (*unique_id == NULL) {
1606 err = got_error_from_errno("got_object_id_dup");
1607 free(id_str);
1608 goto done;
1610 } else {
1611 if (got_object_id_cmp(*unique_id, &id) == 0)
1612 continue; /* both packed and loose */
1613 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1614 free(id_str);
1615 goto done;
1618 done:
1619 if (dir && closedir(dir) != 0 && err == NULL)
1620 err = got_error_from_errno("closedir");
1621 if (err) {
1622 free(*unique_id);
1623 *unique_id = NULL;
1625 free(path);
1626 return err;
1629 const struct got_error *
1630 got_repo_match_object_id_prefix(struct got_object_id **id,
1631 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 char *path_objects = got_repo_get_path_objects(repo);
1635 char *object_dir = NULL;
1636 size_t len;
1637 int i;
1639 *id = NULL;
1641 len = strlen(id_str_prefix);
1642 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1643 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1645 for (i = 0; i < len; i++) {
1646 if (isxdigit((unsigned char)id_str_prefix[i]))
1647 continue;
1648 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1651 if (len >= 2) {
1652 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1653 if (err)
1654 goto done;
1655 object_dir = strndup(id_str_prefix, 2);
1656 if (object_dir == NULL) {
1657 err = got_error_from_errno("strdup");
1658 goto done;
1660 err = match_loose_object(id, path_objects, object_dir,
1661 id_str_prefix, obj_type, repo);
1662 } else if (len == 1) {
1663 int i;
1664 for (i = 0; i < 0xf; i++) {
1665 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1666 == -1) {
1667 err = got_error_from_errno("asprintf");
1668 goto done;
1670 err = match_packed_object(id, repo, object_dir,
1671 obj_type);
1672 if (err)
1673 goto done;
1674 err = match_loose_object(id, path_objects, object_dir,
1675 id_str_prefix, obj_type, repo);
1676 if (err)
1677 goto done;
1679 } else {
1680 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1681 goto done;
1683 done:
1684 free(object_dir);
1685 if (err) {
1686 free(*id);
1687 *id = NULL;
1688 } else if (*id == NULL) {
1689 switch (obj_type) {
1690 case GOT_OBJ_TYPE_BLOB:
1691 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1692 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1693 break;
1694 case GOT_OBJ_TYPE_TREE:
1695 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1696 GOT_OBJ_LABEL_TREE, id_str_prefix);
1697 break;
1698 case GOT_OBJ_TYPE_COMMIT:
1699 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1700 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1701 break;
1702 case GOT_OBJ_TYPE_TAG:
1703 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1704 GOT_OBJ_LABEL_TAG, id_str_prefix);
1705 break;
1706 default:
1707 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1708 break;
1712 return err;
1715 const struct got_error *
1716 got_repo_match_object_id(struct got_object_id **id, char **label,
1717 const char *id_str, int obj_type, struct got_reflist_head *refs,
1718 struct got_repository *repo)
1720 const struct got_error *err;
1721 struct got_tag_object *tag;
1722 struct got_reference *ref = NULL;
1724 *id = NULL;
1725 if (label)
1726 *label = NULL;
1728 if (refs) {
1729 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1730 refs, repo);
1731 if (err == NULL) {
1732 *id = got_object_id_dup(
1733 got_object_tag_get_object_id(tag));
1734 if (*id == NULL)
1735 err = got_error_from_errno("got_object_id_dup");
1736 else if (label && asprintf(label, "refs/tags/%s",
1737 got_object_tag_get_name(tag)) == -1) {
1738 err = got_error_from_errno("asprintf");
1739 free(*id);
1740 *id = NULL;
1742 got_object_tag_close(tag);
1743 return err;
1744 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1745 err->code != GOT_ERR_NO_OBJ)
1746 return err;
1749 err = got_ref_open(&ref, repo, id_str, 0);
1750 if (err == NULL) {
1751 err = got_ref_resolve(id, repo, ref);
1752 if (err)
1753 goto done;
1754 if (label) {
1755 *label = strdup(got_ref_get_name(ref));
1756 if (*label == NULL) {
1757 err = got_error_from_errno("strdup");
1758 goto done;
1761 } else {
1762 if (err->code != GOT_ERR_NOT_REF &&
1763 err->code != GOT_ERR_BAD_REF_NAME)
1764 goto done;
1765 err = got_repo_match_object_id_prefix(id, id_str,
1766 obj_type, repo);
1767 if (err) {
1768 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1769 err = got_error_not_ref(id_str);
1770 goto done;
1772 if (label) {
1773 err = got_object_id_str(label, *id);
1774 if (*label == NULL) {
1775 err = got_error_from_errno("strdup");
1776 goto done;
1780 done:
1781 if (ref)
1782 got_ref_close(ref);
1783 return err;
1786 const struct got_error *
1787 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1788 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1790 const struct got_error *err = NULL;
1791 struct got_reflist_entry *re;
1792 struct got_object_id *tag_id;
1793 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1795 *tag = NULL;
1797 TAILQ_FOREACH(re, refs, entry) {
1798 const char *refname;
1799 refname = got_ref_get_name(re->ref);
1800 if (got_ref_is_symbolic(re->ref))
1801 continue;
1802 if (strncmp(refname, "refs/tags/", 10) != 0)
1803 continue;
1804 if (!name_is_absolute)
1805 refname += strlen("refs/tags/");
1806 if (strcmp(refname, name) != 0)
1807 continue;
1808 err = got_ref_resolve(&tag_id, repo, re->ref);
1809 if (err)
1810 break;
1811 err = got_object_open_as_tag(tag, repo, tag_id);
1812 free(tag_id);
1813 if (err)
1814 break;
1815 if (obj_type == GOT_OBJ_TYPE_ANY ||
1816 got_object_tag_get_object_type(*tag) == obj_type)
1817 break;
1818 got_object_tag_close(*tag);
1819 *tag = NULL;
1822 if (err == NULL && *tag == NULL)
1823 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1824 GOT_OBJ_LABEL_TAG, name);
1825 return err;
1828 static const struct got_error *
1829 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1830 const char *name, mode_t mode, struct got_object_id *blob_id)
1832 const struct got_error *err = NULL;
1834 *new_te = NULL;
1836 *new_te = calloc(1, sizeof(**new_te));
1837 if (*new_te == NULL)
1838 return got_error_from_errno("calloc");
1840 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1841 sizeof((*new_te)->name)) {
1842 err = got_error(GOT_ERR_NO_SPACE);
1843 goto done;
1846 if (S_ISLNK(mode)) {
1847 (*new_te)->mode = S_IFLNK;
1848 } else {
1849 (*new_te)->mode = S_IFREG;
1850 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1852 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1853 done:
1854 if (err && *new_te) {
1855 free(*new_te);
1856 *new_te = NULL;
1858 return err;
1861 static const struct got_error *
1862 import_file(struct got_tree_entry **new_te, struct dirent *de,
1863 const char *path, struct got_repository *repo)
1865 const struct got_error *err;
1866 struct got_object_id *blob_id = NULL;
1867 char *filepath;
1868 struct stat sb;
1870 if (asprintf(&filepath, "%s%s%s", path,
1871 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1872 return got_error_from_errno("asprintf");
1874 if (lstat(filepath, &sb) != 0) {
1875 err = got_error_from_errno2("lstat", path);
1876 goto done;
1879 err = got_object_blob_create(&blob_id, filepath, repo);
1880 if (err)
1881 goto done;
1883 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1884 blob_id);
1885 done:
1886 free(filepath);
1887 if (err)
1888 free(blob_id);
1889 return err;
1892 static const struct got_error *
1893 insert_tree_entry(struct got_tree_entry *new_te,
1894 struct got_pathlist_head *paths)
1896 const struct got_error *err = NULL;
1897 struct got_pathlist_entry *new_pe;
1899 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1900 if (err)
1901 return err;
1902 if (new_pe == NULL)
1903 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1904 return NULL;
1907 static const struct got_error *write_tree(struct got_object_id **,
1908 const char *, struct got_pathlist_head *, struct got_repository *,
1909 got_repo_import_cb progress_cb, void *progress_arg);
1911 static const struct got_error *
1912 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1913 const char *path, struct got_pathlist_head *ignores,
1914 struct got_repository *repo,
1915 got_repo_import_cb progress_cb, void *progress_arg)
1917 const struct got_error *err;
1918 struct got_object_id *id = NULL;
1919 char *subdirpath;
1921 if (asprintf(&subdirpath, "%s%s%s", path,
1922 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1923 return got_error_from_errno("asprintf");
1925 (*new_te) = calloc(1, sizeof(**new_te));
1926 if (*new_te == NULL)
1927 return got_error_from_errno("calloc");
1928 (*new_te)->mode = S_IFDIR;
1929 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1930 sizeof((*new_te)->name)) {
1931 err = got_error(GOT_ERR_NO_SPACE);
1932 goto done;
1934 err = write_tree(&id, subdirpath, ignores, repo,
1935 progress_cb, progress_arg);
1936 if (err)
1937 goto done;
1938 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1940 done:
1941 free(id);
1942 free(subdirpath);
1943 if (err) {
1944 free(*new_te);
1945 *new_te = NULL;
1947 return err;
1950 static const struct got_error *
1951 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1952 struct got_pathlist_head *ignores, struct got_repository *repo,
1953 got_repo_import_cb progress_cb, void *progress_arg)
1955 const struct got_error *err = NULL;
1956 DIR *dir;
1957 struct dirent *de;
1958 int nentries;
1959 struct got_tree_entry *new_te = NULL;
1960 struct got_pathlist_head paths;
1961 struct got_pathlist_entry *pe;
1963 *new_tree_id = NULL;
1965 TAILQ_INIT(&paths);
1967 dir = opendir(path_dir);
1968 if (dir == NULL) {
1969 err = got_error_from_errno2("opendir", path_dir);
1970 goto done;
1973 nentries = 0;
1974 while ((de = readdir(dir)) != NULL) {
1975 int ignore = 0;
1976 int type;
1978 if (strcmp(de->d_name, ".") == 0 ||
1979 strcmp(de->d_name, "..") == 0)
1980 continue;
1982 TAILQ_FOREACH(pe, ignores, entry) {
1983 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1984 ignore = 1;
1985 break;
1988 if (ignore)
1989 continue;
1991 err = got_path_dirent_type(&type, path_dir, de);
1992 if (err)
1993 goto done;
1995 if (type == DT_DIR) {
1996 err = import_subdir(&new_te, de, path_dir,
1997 ignores, repo, progress_cb, progress_arg);
1998 if (err) {
1999 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2000 goto done;
2001 err = NULL;
2002 continue;
2004 } else if (type == DT_REG || type == DT_LNK) {
2005 err = import_file(&new_te, de, path_dir, repo);
2006 if (err)
2007 goto done;
2008 } else
2009 continue;
2011 err = insert_tree_entry(new_te, &paths);
2012 if (err)
2013 goto done;
2014 nentries++;
2017 if (TAILQ_EMPTY(&paths)) {
2018 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2019 "cannot create tree without any entries");
2020 goto done;
2023 TAILQ_FOREACH(pe, &paths, entry) {
2024 struct got_tree_entry *te = pe->data;
2025 char *path;
2026 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2027 continue;
2028 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2029 err = got_error_from_errno("asprintf");
2030 goto done;
2032 err = (*progress_cb)(progress_arg, path);
2033 free(path);
2034 if (err)
2035 goto done;
2038 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2039 done:
2040 if (dir)
2041 closedir(dir);
2042 got_pathlist_free(&paths);
2043 return err;
2046 const struct got_error *
2047 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2048 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2049 struct got_repository *repo, got_repo_import_cb progress_cb,
2050 void *progress_arg)
2052 const struct got_error *err;
2053 struct got_object_id *new_tree_id;
2055 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2056 progress_cb, progress_arg);
2057 if (err)
2058 return err;
2060 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2061 author, time(NULL), author, time(NULL), logmsg, repo);
2062 free(new_tree_id);
2063 return err;
2066 const struct got_error *
2067 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 char *path_objects = NULL, *path = NULL;
2072 DIR *dir = NULL;
2073 struct got_object_id id;
2074 int i;
2076 *nobjects = 0;
2077 *ondisk_size = 0;
2079 path_objects = got_repo_get_path_objects(repo);
2080 if (path_objects == NULL)
2081 return got_error_from_errno("got_repo_get_path_objects");
2083 for (i = 0; i <= 0xff; i++) {
2084 struct dirent *dent;
2086 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2087 err = got_error_from_errno("asprintf");
2088 break;
2091 dir = opendir(path);
2092 if (dir == NULL) {
2093 if (errno == ENOENT) {
2094 err = NULL;
2095 continue;
2097 err = got_error_from_errno2("opendir", path);
2098 break;
2101 while ((dent = readdir(dir)) != NULL) {
2102 char *id_str;
2103 int fd;
2104 struct stat sb;
2106 if (strcmp(dent->d_name, ".") == 0 ||
2107 strcmp(dent->d_name, "..") == 0)
2108 continue;
2110 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2111 err = got_error_from_errno("asprintf");
2112 goto done;
2115 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2116 free(id_str);
2117 continue;
2119 free(id_str);
2121 err = got_object_open_loose_fd(&fd, &id, repo);
2122 if (err)
2123 goto done;
2125 if (fstat(fd, &sb) == -1) {
2126 err = got_error_from_errno("fstat");
2127 close(fd);
2128 goto done;
2130 (*nobjects)++;
2131 (*ondisk_size) += sb.st_size;
2133 if (close(fd) == -1) {
2134 err = got_error_from_errno("close");
2135 goto done;
2139 if (closedir(dir) != 0) {
2140 err = got_error_from_errno("closedir");
2141 goto done;
2143 dir = NULL;
2145 free(path);
2146 path = NULL;
2148 done:
2149 if (dir && closedir(dir) != 0 && err == NULL)
2150 err = got_error_from_errno("closedir");
2152 if (err) {
2153 *nobjects = 0;
2154 *ondisk_size = 0;
2156 free(path_objects);
2157 free(path);
2158 return err;
2161 const struct got_error *
2162 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2163 off_t *total_packsize, struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 DIR *packdir = NULL;
2167 struct dirent *dent;
2168 struct got_packidx *packidx = NULL;
2169 char *path_packidx;
2170 char *path_packfile;
2171 int packdir_fd;
2172 struct stat sb;
2174 *npackfiles = 0;
2175 *nobjects = 0;
2176 *total_packsize = 0;
2178 packdir_fd = openat(got_repo_get_fd(repo),
2179 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2180 if (packdir_fd == -1) {
2181 return got_error_from_errno_fmt("openat: %s/%s",
2182 got_repo_get_path_git_dir(repo),
2183 GOT_OBJECTS_PACK_DIR);
2186 packdir = fdopendir(packdir_fd);
2187 if (packdir == NULL) {
2188 err = got_error_from_errno("fdopendir");
2189 goto done;
2192 while ((dent = readdir(packdir)) != NULL) {
2193 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2194 continue;
2196 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2197 dent->d_name) == -1) {
2198 err = got_error_from_errno("asprintf");
2199 goto done;
2202 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2203 path_packidx, 0);
2204 free(path_packidx);
2205 if (err)
2206 goto done;
2208 if (fstat(packidx->fd, &sb) == -1)
2209 goto done;
2210 *total_packsize += sb.st_size;
2212 err = got_packidx_get_packfile_path(&path_packfile,
2213 packidx->path_packidx);
2214 if (err)
2215 goto done;
2217 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2218 0) == -1) {
2219 free(path_packfile);
2220 goto done;
2222 free(path_packfile);
2223 *total_packsize += sb.st_size;
2225 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2227 (*npackfiles)++;
2229 got_packidx_close(packidx);
2230 packidx = NULL;
2232 done:
2233 if (packidx)
2234 got_packidx_close(packidx);
2235 if (packdir && closedir(packdir) != 0 && err == NULL)
2236 err = got_error_from_errno("closedir");
2237 if (err) {
2238 *npackfiles = 0;
2239 *nobjects = 0;
2240 *total_packsize = 0;
2242 return err;
2245 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2246 got_packidx_bloom_filter_cmp);