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 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_repo_pack_fds_close(pack_fds_tmp);
265 if (err)
266 return err;
267 else
268 return got_error_from_errno("got_opentempfd");
271 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
272 return err;
275 const struct got_error *
276 got_repo_pack_fds_close(int *pack_fds)
278 const struct got_error *err = NULL;
279 int i;
281 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
282 if (pack_fds[i] == -1)
283 continue;
284 if (close(pack_fds[i]) == -1) {
285 err = got_error_from_errno("close");
286 break;
289 free(pack_fds);
290 pack_fds = NULL;
291 return err;
294 const struct got_error *
295 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
296 struct got_object *obj)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->objcache, id, obj);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 obj->refcnt++;
308 #endif
309 return NULL;
312 struct got_object *
313 got_repo_get_cached_object(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
319 const struct got_error *
320 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
321 struct got_tree_object *tree)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->treecache, id, tree);
326 if (err) {
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 err = NULL;
330 return err;
332 tree->refcnt++;
333 #endif
334 return NULL;
337 struct got_tree_object *
338 got_repo_get_cached_tree(struct got_repository *repo,
339 struct got_object_id *id)
341 return (struct got_tree_object *)got_object_cache_get(
342 &repo->treecache, id);
345 const struct got_error *
346 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
347 struct got_commit_object *commit)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->commitcache, id, commit);
352 if (err) {
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
355 err = NULL;
356 return err;
358 commit->refcnt++;
359 #endif
360 return NULL;
363 struct got_commit_object *
364 got_repo_get_cached_commit(struct got_repository *repo,
365 struct got_object_id *id)
367 return (struct got_commit_object *)got_object_cache_get(
368 &repo->commitcache, id);
371 const struct got_error *
372 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
373 struct got_tag_object *tag)
375 #ifndef GOT_NO_OBJ_CACHE
376 const struct got_error *err = NULL;
377 err = got_object_cache_add(&repo->tagcache, id, tag);
378 if (err) {
379 if (err->code == GOT_ERR_OBJ_EXISTS ||
380 err->code == GOT_ERR_OBJ_TOO_LARGE)
381 err = NULL;
382 return err;
384 tag->refcnt++;
385 #endif
386 return NULL;
389 struct got_tag_object *
390 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
392 return (struct got_tag_object *)got_object_cache_get(
393 &repo->tagcache, id);
396 const struct got_error *
397 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
398 struct got_raw_object *raw)
400 #ifndef GOT_NO_OBJ_CACHE
401 const struct got_error *err = NULL;
402 err = got_object_cache_add(&repo->rawcache, id, raw);
403 if (err) {
404 if (err->code == GOT_ERR_OBJ_EXISTS ||
405 err->code == GOT_ERR_OBJ_TOO_LARGE)
406 err = NULL;
407 return err;
409 raw->refcnt++;
410 #endif
411 return NULL;
415 struct got_raw_object *
416 got_repo_get_cached_raw_object(struct got_repository *repo,
417 struct got_object_id *id)
419 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
423 static const struct got_error *
424 open_repo(struct got_repository *repo, const char *path)
426 const struct got_error *err = NULL;
428 repo->gitdir_fd = -1;
430 /* bare git repository? */
431 repo->path_git_dir = strdup(path);
432 if (repo->path_git_dir == NULL)
433 return got_error_from_errno("strdup");
434 if (is_git_repo(repo)) {
435 repo->path = strdup(repo->path_git_dir);
436 if (repo->path == NULL) {
437 err = got_error_from_errno("strdup");
438 goto done;
440 repo->gitdir_fd = open(repo->path_git_dir,
441 O_DIRECTORY | O_CLOEXEC);
442 if (repo->gitdir_fd == -1) {
443 err = got_error_from_errno2("open",
444 repo->path_git_dir);
445 goto done;
447 return NULL;
450 /* git repository with working tree? */
451 free(repo->path_git_dir);
452 repo->path_git_dir = NULL;
453 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
454 err = got_error_from_errno("asprintf");
455 goto done;
457 if (is_git_repo(repo)) {
458 repo->path = strdup(path);
459 if (repo->path == NULL) {
460 err = got_error_from_errno("strdup");
461 goto done;
463 repo->gitdir_fd = open(repo->path_git_dir,
464 O_DIRECTORY | O_CLOEXEC);
465 if (repo->gitdir_fd == -1) {
466 err = got_error_from_errno2("open",
467 repo->path_git_dir);
468 goto done;
470 return NULL;
473 err = got_error(GOT_ERR_NOT_GIT_REPO);
474 done:
475 if (err) {
476 free(repo->path);
477 repo->path = NULL;
478 free(repo->path_git_dir);
479 repo->path_git_dir = NULL;
480 if (repo->gitdir_fd != -1)
481 close(repo->gitdir_fd);
482 repo->gitdir_fd = -1;
485 return err;
488 static const struct got_error *
489 parse_gitconfig_file(int *gitconfig_repository_format_version,
490 char **gitconfig_author_name, char **gitconfig_author_email,
491 struct got_remote_repo **remotes, int *nremotes,
492 char **gitconfig_owner, char ***extensions, int *nextensions,
493 const char *gitconfig_path)
495 const struct got_error *err = NULL, *child_err = NULL;
496 int fd = -1;
497 int imsg_fds[2] = { -1, -1 };
498 pid_t pid;
499 struct imsgbuf *ibuf;
501 *gitconfig_repository_format_version = 0;
502 if (extensions)
503 *extensions = NULL;
504 if (nextensions)
505 *nextensions = 0;
506 *gitconfig_author_name = NULL;
507 *gitconfig_author_email = NULL;
508 if (remotes)
509 *remotes = NULL;
510 if (nremotes)
511 *nremotes = 0;
512 if (gitconfig_owner)
513 *gitconfig_owner = NULL;
515 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
516 if (fd == -1) {
517 if (errno == ENOENT)
518 return NULL;
519 return got_error_from_errno2("open", gitconfig_path);
522 ibuf = calloc(1, sizeof(*ibuf));
523 if (ibuf == NULL) {
524 err = got_error_from_errno("calloc");
525 goto done;
528 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 err = got_error_from_errno("socketpair");
530 goto done;
533 pid = fork();
534 if (pid == -1) {
535 err = got_error_from_errno("fork");
536 goto done;
537 } else if (pid == 0) {
538 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
539 gitconfig_path);
540 /* not reached */
543 if (close(imsg_fds[1]) == -1) {
544 err = got_error_from_errno("close");
545 goto done;
547 imsg_fds[1] = -1;
548 imsg_init(ibuf, imsg_fds[0]);
550 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
551 if (err)
552 goto done;
553 fd = -1;
555 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
556 if (err)
557 goto done;
559 err = got_privsep_recv_gitconfig_int(
560 gitconfig_repository_format_version, ibuf);
561 if (err)
562 goto done;
564 if (extensions && nextensions) {
565 err = got_privsep_send_gitconfig_repository_extensions_req(
566 ibuf);
567 if (err)
568 goto done;
569 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
570 if (err)
571 goto done;
572 if (*nextensions > 0) {
573 int i;
574 *extensions = calloc(*nextensions, sizeof(char *));
575 if (*extensions == NULL) {
576 err = got_error_from_errno("calloc");
577 goto done;
579 for (i = 0; i < *nextensions; i++) {
580 char *ext;
581 err = got_privsep_recv_gitconfig_str(&ext,
582 ibuf);
583 if (err)
584 goto done;
585 (*extensions)[i] = ext;
590 err = got_privsep_send_gitconfig_author_name_req(ibuf);
591 if (err)
592 goto done;
594 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
595 if (err)
596 goto done;
598 err = got_privsep_send_gitconfig_author_email_req(ibuf);
599 if (err)
600 goto done;
602 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
603 if (err)
604 goto done;
606 if (remotes && nremotes) {
607 err = got_privsep_send_gitconfig_remotes_req(ibuf);
608 if (err)
609 goto done;
611 err = got_privsep_recv_gitconfig_remotes(remotes,
612 nremotes, ibuf);
613 if (err)
614 goto done;
617 if (gitconfig_owner) {
618 err = got_privsep_send_gitconfig_owner_req(ibuf);
619 if (err)
620 goto done;
621 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
622 if (err)
623 goto done;
626 err = got_privsep_send_stop(imsg_fds[0]);
627 child_err = got_privsep_wait_for_child(pid);
628 if (child_err && err == NULL)
629 err = child_err;
630 done:
631 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
632 err = got_error_from_errno("close");
633 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
634 err = got_error_from_errno("close");
635 if (fd != -1 && close(fd) == -1 && err == NULL)
636 err = got_error_from_errno2("close", gitconfig_path);
637 free(ibuf);
638 return err;
641 static const struct got_error *
642 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
644 const struct got_error *err = NULL;
645 char *repo_gitconfig_path = NULL;
647 if (global_gitconfig_path) {
648 /* Read settings from ~/.gitconfig. */
649 int dummy_repo_version;
650 err = parse_gitconfig_file(&dummy_repo_version,
651 &repo->global_gitconfig_author_name,
652 &repo->global_gitconfig_author_email,
653 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
654 if (err)
655 return err;
658 /* Read repository's .git/config file. */
659 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
660 if (repo_gitconfig_path == NULL)
661 return got_error_from_errno("got_repo_get_path_gitconfig");
663 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
664 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
665 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
666 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
667 repo_gitconfig_path);
668 if (err)
669 goto done;
670 done:
671 free(repo_gitconfig_path);
672 return err;
675 static const struct got_error *
676 read_gotconfig(struct got_repository *repo)
678 const struct got_error *err = NULL;
679 char *gotconfig_path;
681 gotconfig_path = got_repo_get_path_gotconfig(repo);
682 if (gotconfig_path == NULL)
683 return got_error_from_errno("got_repo_get_path_gotconfig");
685 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
686 free(gotconfig_path);
687 return err;
690 /* Supported repository format extensions. */
691 static const char *const repo_extensions[] = {
692 "noop", /* Got supports repository format version 1. */
693 "preciousObjects", /* Supported by gotadmin cleanup. */
694 "worktreeConfig", /* Got does not care about Git work trees. */
695 };
697 const struct got_error *
698 got_repo_open(struct got_repository **repop, const char *path,
699 const char *global_gitconfig_path, int *pack_fds)
701 struct got_repository *repo = NULL;
702 const struct got_error *err = NULL;
703 char *repo_path = NULL;
704 size_t i, j = 0;
705 struct rlimit rl;
707 *repop = NULL;
709 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
710 return got_error_from_errno("getrlimit");
712 repo = calloc(1, sizeof(*repo));
713 if (repo == NULL)
714 return got_error_from_errno("calloc");
716 RB_INIT(&repo->packidx_bloom_filters);
717 TAILQ_INIT(&repo->packidx_paths);
719 for (i = 0; i < nitems(repo->privsep_children); i++) {
720 memset(&repo->privsep_children[i], 0,
721 sizeof(repo->privsep_children[0]));
722 repo->privsep_children[i].imsg_fd = -1;
725 err = got_object_cache_init(&repo->objcache,
726 GOT_OBJECT_CACHE_TYPE_OBJ);
727 if (err)
728 goto done;
729 err = got_object_cache_init(&repo->treecache,
730 GOT_OBJECT_CACHE_TYPE_TREE);
731 if (err)
732 goto done;
733 err = got_object_cache_init(&repo->commitcache,
734 GOT_OBJECT_CACHE_TYPE_COMMIT);
735 if (err)
736 goto done;
737 err = got_object_cache_init(&repo->tagcache,
738 GOT_OBJECT_CACHE_TYPE_TAG);
739 if (err)
740 goto done;
741 err = got_object_cache_init(&repo->rawcache,
742 GOT_OBJECT_CACHE_TYPE_RAW);
743 if (err)
744 goto done;
746 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
747 if (repo->pack_cache_size > rl.rlim_cur / 8)
748 repo->pack_cache_size = rl.rlim_cur / 8;
749 for (i = 0; i < nitems(repo->packs); i++) {
750 if (i < repo->pack_cache_size) {
751 repo->packs[i].basefd = pack_fds[j++];
752 repo->packs[i].accumfd = pack_fds[j++];
753 } else {
754 repo->packs[i].basefd = -1;
755 repo->packs[i].accumfd = -1;
759 repo_path = realpath(path, NULL);
760 if (repo_path == NULL) {
761 err = got_error_from_errno2("realpath", path);
762 goto done;
765 for (;;) {
766 char *parent_path;
768 err = open_repo(repo, repo_path);
769 if (err == NULL)
770 break;
771 if (err->code != GOT_ERR_NOT_GIT_REPO)
772 goto done;
773 if (repo_path[0] == '/' && repo_path[1] == '\0') {
774 err = got_error(GOT_ERR_NOT_GIT_REPO);
775 goto done;
777 err = got_path_dirname(&parent_path, repo_path);
778 if (err)
779 goto done;
780 free(repo_path);
781 repo_path = parent_path;
784 err = read_gotconfig(repo);
785 if (err)
786 goto done;
788 err = read_gitconfig(repo, global_gitconfig_path);
789 if (err)
790 goto done;
791 if (repo->gitconfig_repository_format_version != 0)
792 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
793 for (i = 0; i < repo->nextensions; i++) {
794 char *ext = repo->extensions[i];
795 int j, supported = 0;
796 for (j = 0; j < nitems(repo_extensions); j++) {
797 if (strcmp(ext, repo_extensions[j]) == 0) {
798 supported = 1;
799 break;
802 if (!supported) {
803 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
804 goto done;
808 err = got_repo_list_packidx(&repo->packidx_paths, repo);
809 done:
810 if (err)
811 got_repo_close(repo);
812 else
813 *repop = repo;
814 free(repo_path);
815 return err;
818 const struct got_error *
819 got_repo_close(struct got_repository *repo)
821 const struct got_error *err = NULL, *child_err;
822 struct got_packidx_bloom_filter *bf;
823 struct got_pathlist_entry *pe;
824 size_t i;
826 for (i = 0; i < repo->pack_cache_size; i++) {
827 if (repo->packidx_cache[i] == NULL)
828 break;
829 got_packidx_close(repo->packidx_cache[i]);
832 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
833 &repo->packidx_bloom_filters))) {
834 RB_REMOVE(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters, bf);
836 free(bf->bloom);
837 free(bf);
840 for (i = 0; i < repo->pack_cache_size; i++)
841 if (repo->packs[i].path_packfile)
842 if (repo->packs[i].path_packfile)
843 got_pack_close(&repo->packs[i]);
845 free(repo->path);
846 free(repo->path_git_dir);
848 got_object_cache_close(&repo->objcache);
849 got_object_cache_close(&repo->treecache);
850 got_object_cache_close(&repo->commitcache);
851 got_object_cache_close(&repo->tagcache);
852 got_object_cache_close(&repo->rawcache);
854 for (i = 0; i < nitems(repo->privsep_children); i++) {
855 if (repo->privsep_children[i].imsg_fd == -1)
856 continue;
857 imsg_clear(repo->privsep_children[i].ibuf);
858 free(repo->privsep_children[i].ibuf);
859 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
860 child_err = got_privsep_wait_for_child(
861 repo->privsep_children[i].pid);
862 if (child_err && err == NULL)
863 err = child_err;
864 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
865 err == NULL)
866 err = got_error_from_errno("close");
869 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
870 err == NULL)
871 err = got_error_from_errno("close");
873 if (repo->gotconfig)
874 got_gotconfig_free(repo->gotconfig);
875 free(repo->gitconfig_author_name);
876 free(repo->gitconfig_author_email);
877 for (i = 0; i < repo->ngitconfig_remotes; i++)
878 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
879 free(repo->gitconfig_remotes);
880 for (i = 0; i < repo->nextensions; i++)
881 free(repo->extensions[i]);
882 free(repo->extensions);
884 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
885 free((void *)pe->path);
886 got_pathlist_free(&repo->packidx_paths);
887 free(repo);
889 return err;
892 void
893 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
895 int i;
897 free(repo->name);
898 repo->name = NULL;
899 free(repo->fetch_url);
900 repo->fetch_url = NULL;
901 free(repo->send_url);
902 repo->send_url = NULL;
903 for (i = 0; i < repo->nfetch_branches; i++)
904 free(repo->fetch_branches[i]);
905 free(repo->fetch_branches);
906 repo->fetch_branches = NULL;
907 repo->nfetch_branches = 0;
908 for (i = 0; i < repo->nsend_branches; i++)
909 free(repo->send_branches[i]);
910 free(repo->send_branches);
911 repo->send_branches = NULL;
912 repo->nsend_branches = 0;
915 const struct got_error *
916 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
917 const char *input_path)
919 const struct got_error *err = NULL;
920 const char *repo_abspath = NULL;
921 size_t repolen, len;
922 char *canonpath, *path = NULL;
924 *in_repo_path = NULL;
926 canonpath = strdup(input_path);
927 if (canonpath == NULL) {
928 err = got_error_from_errno("strdup");
929 goto done;
931 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
932 if (err)
933 goto done;
935 repo_abspath = got_repo_get_path(repo);
937 if (canonpath[0] == '\0') {
938 path = strdup(canonpath);
939 if (path == NULL) {
940 err = got_error_from_errno("strdup");
941 goto done;
943 } else {
944 path = realpath(canonpath, NULL);
945 if (path == NULL) {
946 if (errno != ENOENT) {
947 err = got_error_from_errno2("realpath",
948 canonpath);
949 goto done;
951 /*
952 * Path is not on disk.
953 * Assume it is already relative to repository root.
954 */
955 path = strdup(canonpath);
956 if (path == NULL) {
957 err = got_error_from_errno("strdup");
958 goto done;
962 repolen = strlen(repo_abspath);
963 len = strlen(path);
966 if (strcmp(path, repo_abspath) == 0) {
967 free(path);
968 path = strdup("");
969 if (path == NULL) {
970 err = got_error_from_errno("strdup");
971 goto done;
973 } else if (len > repolen &&
974 got_path_is_child(path, repo_abspath, repolen)) {
975 /* Matched an on-disk path inside repository. */
976 if (got_repo_is_bare(repo)) {
977 /*
978 * Matched an on-disk path inside repository
979 * database. Treat input as repository-relative.
980 */
981 free(path);
982 path = canonpath;
983 canonpath = NULL;
984 } else {
985 char *child;
986 /* Strip common prefix with repository path. */
987 err = got_path_skip_common_ancestor(&child,
988 repo_abspath, path);
989 if (err)
990 goto done;
991 free(path);
992 path = child;
994 } else {
995 /*
996 * Matched unrelated on-disk path.
997 * Treat input as repository-relative.
998 */
999 free(path);
1000 path = canonpath;
1001 canonpath = NULL;
1005 /* Make in-repository path absolute */
1006 if (path[0] != '/') {
1007 char *abspath;
1008 if (asprintf(&abspath, "/%s", path) == -1) {
1009 err = got_error_from_errno("asprintf");
1010 goto done;
1012 free(path);
1013 path = abspath;
1016 done:
1017 free(canonpath);
1018 if (err)
1019 free(path);
1020 else
1021 *in_repo_path = path;
1022 return err;
1025 static const struct got_error *
1026 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1027 const char *path_packidx)
1029 const struct got_error *err = NULL;
1030 size_t i;
1032 for (i = 0; i < repo->pack_cache_size; i++) {
1033 if (repo->packidx_cache[i] == NULL)
1034 break;
1035 if (strcmp(repo->packidx_cache[i]->path_packidx,
1036 path_packidx) == 0) {
1037 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1040 if (i == repo->pack_cache_size) {
1041 i = repo->pack_cache_size - 1;
1042 err = got_packidx_close(repo->packidx_cache[i]);
1043 if (err)
1044 return err;
1047 repo->packidx_cache[i] = packidx;
1049 return NULL;
1052 int
1053 got_repo_is_packidx_filename(const char *name, size_t len)
1055 if (len != GOT_PACKIDX_NAMELEN)
1056 return 0;
1058 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1059 return 0;
1061 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1062 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1063 return 0;
1065 return 1;
1068 static struct got_packidx_bloom_filter *
1069 get_packidx_bloom_filter(struct got_repository *repo,
1070 const char *path, size_t path_len)
1072 struct got_packidx_bloom_filter key;
1074 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1075 return NULL; /* XXX */
1076 key.path_len = path_len;
1078 return RB_FIND(got_packidx_bloom_filter_tree,
1079 &repo->packidx_bloom_filters, &key);
1082 int
1083 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1084 const char *path_packidx, struct got_object_id *id)
1086 struct got_packidx_bloom_filter *bf;
1088 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1089 if (bf)
1090 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1092 /* No bloom filter means this pack index must be searched. */
1093 return 1;
1096 static const struct got_error *
1097 add_packidx_bloom_filter(struct got_repository *repo,
1098 struct got_packidx *packidx, const char *path_packidx)
1100 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1101 struct got_packidx_bloom_filter *bf;
1102 size_t len;
1105 * Don't use bloom filters for very large pack index files.
1106 * Large pack files will contain a relatively large fraction
1107 * of our objects so we will likely need to visit them anyway.
1108 * The more objects a pack file contains the higher the probability
1109 * of a false-positive match from the bloom filter. And reading
1110 * all object IDs from a large pack index file can be expensive.
1112 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1113 return NULL;
1115 /* Do we already have a filter for this pack index? */
1116 if (get_packidx_bloom_filter(repo, path_packidx,
1117 strlen(path_packidx)) != NULL)
1118 return NULL;
1120 bf = calloc(1, sizeof(*bf));
1121 if (bf == NULL)
1122 return got_error_from_errno("calloc");
1123 bf->bloom = calloc(1, sizeof(*bf->bloom));
1124 if (bf->bloom == NULL) {
1125 free(bf);
1126 return got_error_from_errno("calloc");
1129 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1130 if (len >= sizeof(bf->path)) {
1131 free(bf->bloom);
1132 free(bf);
1133 return got_error(GOT_ERR_NO_SPACE);
1135 bf->path_len = len;
1137 /* Minimum size supported by our bloom filter is 1000 entries. */
1138 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1139 for (i = 0; i < nobjects; i++) {
1140 struct got_packidx_object_id *id;
1141 id = &packidx->hdr.sorted_ids[i];
1142 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1145 RB_INSERT(got_packidx_bloom_filter_tree,
1146 &repo->packidx_bloom_filters, bf);
1147 return NULL;
1150 const struct got_error *
1151 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1152 struct got_repository *repo, struct got_object_id *id)
1154 const struct got_error *err;
1155 struct got_pathlist_entry *pe;
1156 size_t i;
1158 /* Search pack index cache. */
1159 for (i = 0; i < repo->pack_cache_size; i++) {
1160 if (repo->packidx_cache[i] == NULL)
1161 break;
1162 if (!got_repo_check_packidx_bloom_filter(repo,
1163 repo->packidx_cache[i]->path_packidx, id))
1164 continue; /* object will not be found in this index */
1165 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1166 if (*idx != -1) {
1167 *packidx = repo->packidx_cache[i];
1169 * Move this cache entry to the front. Repeatedly
1170 * searching a wrong pack index can be expensive.
1172 if (i > 0) {
1173 memmove(&repo->packidx_cache[1],
1174 &repo->packidx_cache[0],
1175 i * sizeof(repo->packidx_cache[0]));
1176 repo->packidx_cache[0] = *packidx;
1178 return NULL;
1181 /* No luck. Search the filesystem. */
1183 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1184 const char *path_packidx = pe->path;
1185 int is_cached = 0;
1187 if (!got_repo_check_packidx_bloom_filter(repo,
1188 pe->path, id))
1189 continue; /* object will not be found in this index */
1191 for (i = 0; i < repo->pack_cache_size; i++) {
1192 if (repo->packidx_cache[i] == NULL)
1193 break;
1194 if (strcmp(repo->packidx_cache[i]->path_packidx,
1195 path_packidx) == 0) {
1196 is_cached = 1;
1197 break;
1200 if (is_cached)
1201 continue; /* already searched */
1203 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1204 path_packidx, 0);
1205 if (err)
1206 goto done;
1208 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1209 if (err)
1210 goto done;
1212 err = cache_packidx(repo, *packidx, path_packidx);
1213 if (err)
1214 goto done;
1216 *idx = got_packidx_get_object_idx(*packidx, id);
1217 if (*idx != -1) {
1218 err = NULL; /* found the object */
1219 goto done;
1223 err = got_error_no_obj(id);
1224 done:
1225 return err;
1228 const struct got_error *
1229 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1230 struct got_repository *repo)
1232 const struct got_error *err = NULL;
1233 DIR *packdir = NULL;
1234 struct dirent *dent;
1235 char *path_packidx = NULL;
1236 int packdir_fd;
1238 packdir_fd = openat(got_repo_get_fd(repo),
1239 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1240 if (packdir_fd == -1) {
1241 return got_error_from_errno_fmt("openat: %s/%s",
1242 got_repo_get_path_git_dir(repo),
1243 GOT_OBJECTS_PACK_DIR);
1246 packdir = fdopendir(packdir_fd);
1247 if (packdir == NULL) {
1248 err = got_error_from_errno("fdopendir");
1249 goto done;
1252 while ((dent = readdir(packdir)) != NULL) {
1253 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1254 continue;
1256 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1257 dent->d_name) == -1) {
1258 err = got_error_from_errno("asprintf");
1259 path_packidx = NULL;
1260 break;
1263 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1264 if (err)
1265 break;
1267 done:
1268 if (err)
1269 free(path_packidx);
1270 if (packdir && closedir(packdir) != 0 && err == NULL)
1271 err = got_error_from_errno("closedir");
1272 return err;
1275 const struct got_error *
1276 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1277 struct got_repository *repo)
1279 const struct got_error *err;
1280 size_t i;
1282 *packidx = NULL;
1284 /* Search pack index cache. */
1285 for (i = 0; i < repo->pack_cache_size; i++) {
1286 if (repo->packidx_cache[i] == NULL)
1287 break;
1288 if (strcmp(repo->packidx_cache[i]->path_packidx,
1289 path_packidx) == 0) {
1290 *packidx = repo->packidx_cache[i];
1291 return NULL;
1294 /* No luck. Search the filesystem. */
1296 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1297 path_packidx, 0);
1298 if (err)
1299 return err;
1301 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1302 if (err)
1303 goto done;
1305 err = cache_packidx(repo, *packidx, path_packidx);
1306 done:
1307 if (err) {
1308 got_packidx_close(*packidx);
1309 *packidx = NULL;
1311 return err;
1314 static const struct got_error *
1315 read_packfile_hdr(int fd, struct got_packidx *packidx)
1317 const struct got_error *err = NULL;
1318 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1319 struct got_packfile_hdr hdr;
1320 ssize_t n;
1322 n = read(fd, &hdr, sizeof(hdr));
1323 if (n < 0)
1324 return got_error_from_errno("read");
1325 if (n != sizeof(hdr))
1326 return got_error(GOT_ERR_BAD_PACKFILE);
1328 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1329 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1330 be32toh(hdr.nobjects) != totobj)
1331 err = got_error(GOT_ERR_BAD_PACKFILE);
1333 return err;
1336 static const struct got_error *
1337 open_packfile(int *fd, struct got_repository *repo,
1338 const char *relpath, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1342 *fd = openat(got_repo_get_fd(repo), relpath,
1343 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1344 if (*fd == -1)
1345 return got_error_from_errno_fmt("openat: %s/%s",
1346 got_repo_get_path_git_dir(repo), relpath);
1348 if (packidx) {
1349 err = read_packfile_hdr(*fd, packidx);
1350 if (err) {
1351 close(*fd);
1352 *fd = -1;
1356 return err;
1359 const struct got_error *
1360 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1361 const char *path_packfile, struct got_packidx *packidx)
1363 const struct got_error *err = NULL;
1364 struct got_pack *pack = NULL;
1365 struct stat sb;
1366 size_t i;
1368 if (packp)
1369 *packp = NULL;
1371 for (i = 0; i < repo->pack_cache_size; i++) {
1372 pack = &repo->packs[i];
1373 if (pack->path_packfile == NULL)
1374 break;
1375 if (strcmp(pack->path_packfile, path_packfile) == 0)
1376 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1379 if (i == repo->pack_cache_size) {
1380 struct got_pack tmp;
1381 err = got_pack_close(&repo->packs[i - 1]);
1382 if (err)
1383 return err;
1384 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1385 return got_error_from_errno("ftruncate");
1386 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1387 return got_error_from_errno("ftruncate");
1388 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1389 memcpy(&repo->packs[i - 1], &repo->packs[0],
1390 sizeof(repo->packs[i - 1]));
1391 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1392 i = 0;
1395 pack = &repo->packs[i];
1397 pack->path_packfile = strdup(path_packfile);
1398 if (pack->path_packfile == NULL) {
1399 err = got_error_from_errno("strdup");
1400 goto done;
1403 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1404 if (err)
1405 goto done;
1407 if (fstat(pack->fd, &sb) != 0) {
1408 err = got_error_from_errno("fstat");
1409 goto done;
1411 pack->filesize = sb.st_size;
1413 pack->privsep_child = NULL;
1415 #ifndef GOT_PACK_NO_MMAP
1416 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1417 pack->fd, 0);
1418 if (pack->map == MAP_FAILED) {
1419 if (errno != ENOMEM) {
1420 err = got_error_from_errno("mmap");
1421 goto done;
1423 pack->map = NULL; /* fall back to read(2) */
1425 #endif
1426 done:
1427 if (err) {
1428 if (pack) {
1429 free(pack->path_packfile);
1430 memset(pack, 0, sizeof(*pack));
1432 } else if (packp)
1433 *packp = pack;
1434 return err;
1437 struct got_pack *
1438 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1440 struct got_pack *pack = NULL;
1441 size_t i;
1443 for (i = 0; i < repo->pack_cache_size; i++) {
1444 pack = &repo->packs[i];
1445 if (pack->path_packfile == NULL)
1446 break;
1447 if (strcmp(pack->path_packfile, path_packfile) == 0)
1448 return pack;
1451 return NULL;
1454 const struct got_error *
1455 got_repo_init(const char *repo_path)
1457 const struct got_error *err = NULL;
1458 const char *dirnames[] = {
1459 GOT_OBJECTS_DIR,
1460 GOT_OBJECTS_PACK_DIR,
1461 GOT_REFS_DIR,
1463 const char *description_str = "Unnamed repository; "
1464 "edit this file 'description' to name the repository.";
1465 const char *headref_str = "ref: refs/heads/main";
1466 const char *gitconfig_str = "[core]\n"
1467 "\trepositoryformatversion = 0\n"
1468 "\tfilemode = true\n"
1469 "\tbare = true\n";
1470 char *path;
1471 size_t i;
1473 if (!got_path_dir_is_empty(repo_path))
1474 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1476 for (i = 0; i < nitems(dirnames); i++) {
1477 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1478 return got_error_from_errno("asprintf");
1480 err = got_path_mkdir(path);
1481 free(path);
1482 if (err)
1483 return err;
1486 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1487 return got_error_from_errno("asprintf");
1488 err = got_path_create_file(path, description_str);
1489 free(path);
1490 if (err)
1491 return err;
1493 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1494 return got_error_from_errno("asprintf");
1495 err = got_path_create_file(path, headref_str);
1496 free(path);
1497 if (err)
1498 return err;
1500 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1501 return got_error_from_errno("asprintf");
1502 err = got_path_create_file(path, gitconfig_str);
1503 free(path);
1504 if (err)
1505 return err;
1507 return NULL;
1510 static const struct got_error *
1511 match_packed_object(struct got_object_id **unique_id,
1512 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1514 const struct got_error *err = NULL;
1515 struct got_object_id_queue matched_ids;
1516 struct got_pathlist_entry *pe;
1518 STAILQ_INIT(&matched_ids);
1520 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1521 const char *path_packidx = pe->path;
1522 struct got_packidx *packidx;
1523 struct got_object_qid *qid;
1525 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1526 path_packidx, 0);
1527 if (err)
1528 break;
1530 err = got_packidx_match_id_str_prefix(&matched_ids,
1531 packidx, id_str_prefix);
1532 if (err) {
1533 got_packidx_close(packidx);
1534 break;
1536 err = got_packidx_close(packidx);
1537 if (err)
1538 break;
1540 STAILQ_FOREACH(qid, &matched_ids, entry) {
1541 if (obj_type != GOT_OBJ_TYPE_ANY) {
1542 int matched_type;
1543 err = got_object_get_type(&matched_type, repo,
1544 &qid->id);
1545 if (err)
1546 goto done;
1547 if (matched_type != obj_type)
1548 continue;
1550 if (*unique_id == NULL) {
1551 *unique_id = got_object_id_dup(&qid->id);
1552 if (*unique_id == NULL) {
1553 err = got_error_from_errno("malloc");
1554 goto done;
1556 } else {
1557 if (got_object_id_cmp(*unique_id,
1558 &qid->id) == 0)
1559 continue; /* packed multiple times */
1560 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1561 goto done;
1565 done:
1566 got_object_id_queue_free(&matched_ids);
1567 if (err) {
1568 free(*unique_id);
1569 *unique_id = NULL;
1571 return err;
1574 static const struct got_error *
1575 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1576 const char *object_dir, const char *id_str_prefix, int obj_type,
1577 struct got_repository *repo)
1579 const struct got_error *err = NULL;
1580 char *path;
1581 DIR *dir = NULL;
1582 struct dirent *dent;
1583 struct got_object_id id;
1585 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1586 err = got_error_from_errno("asprintf");
1587 goto done;
1590 dir = opendir(path);
1591 if (dir == NULL) {
1592 if (errno == ENOENT) {
1593 err = NULL;
1594 goto done;
1596 err = got_error_from_errno2("opendir", path);
1597 goto done;
1599 while ((dent = readdir(dir)) != NULL) {
1600 char *id_str;
1601 int cmp;
1603 if (strcmp(dent->d_name, ".") == 0 ||
1604 strcmp(dent->d_name, "..") == 0)
1605 continue;
1607 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1608 err = got_error_from_errno("asprintf");
1609 goto done;
1612 if (!got_parse_sha1_digest(id.sha1, id_str))
1613 continue;
1616 * Directory entries do not necessarily appear in
1617 * sorted order, so we must iterate over all of them.
1619 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1620 if (cmp != 0) {
1621 free(id_str);
1622 continue;
1625 if (*unique_id == NULL) {
1626 if (obj_type != GOT_OBJ_TYPE_ANY) {
1627 int matched_type;
1628 err = got_object_get_type(&matched_type, repo,
1629 &id);
1630 if (err)
1631 goto done;
1632 if (matched_type != obj_type)
1633 continue;
1635 *unique_id = got_object_id_dup(&id);
1636 if (*unique_id == NULL) {
1637 err = got_error_from_errno("got_object_id_dup");
1638 free(id_str);
1639 goto done;
1641 } else {
1642 if (got_object_id_cmp(*unique_id, &id) == 0)
1643 continue; /* both packed and loose */
1644 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1645 free(id_str);
1646 goto done;
1649 done:
1650 if (dir && closedir(dir) != 0 && err == NULL)
1651 err = got_error_from_errno("closedir");
1652 if (err) {
1653 free(*unique_id);
1654 *unique_id = NULL;
1656 free(path);
1657 return err;
1660 const struct got_error *
1661 got_repo_match_object_id_prefix(struct got_object_id **id,
1662 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 char *path_objects = got_repo_get_path_objects(repo);
1666 char *object_dir = NULL;
1667 size_t len;
1668 int i;
1670 *id = NULL;
1672 len = strlen(id_str_prefix);
1673 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1674 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1676 for (i = 0; i < len; i++) {
1677 if (isxdigit((unsigned char)id_str_prefix[i]))
1678 continue;
1679 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1682 if (len >= 2) {
1683 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1684 if (err)
1685 goto done;
1686 object_dir = strndup(id_str_prefix, 2);
1687 if (object_dir == NULL) {
1688 err = got_error_from_errno("strdup");
1689 goto done;
1691 err = match_loose_object(id, path_objects, object_dir,
1692 id_str_prefix, obj_type, repo);
1693 } else if (len == 1) {
1694 int i;
1695 for (i = 0; i < 0xf; i++) {
1696 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1697 == -1) {
1698 err = got_error_from_errno("asprintf");
1699 goto done;
1701 err = match_packed_object(id, repo, object_dir,
1702 obj_type);
1703 if (err)
1704 goto done;
1705 err = match_loose_object(id, path_objects, object_dir,
1706 id_str_prefix, obj_type, repo);
1707 if (err)
1708 goto done;
1710 } else {
1711 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1712 goto done;
1714 done:
1715 free(object_dir);
1716 if (err) {
1717 free(*id);
1718 *id = NULL;
1719 } else if (*id == NULL) {
1720 switch (obj_type) {
1721 case GOT_OBJ_TYPE_BLOB:
1722 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1723 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1724 break;
1725 case GOT_OBJ_TYPE_TREE:
1726 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1727 GOT_OBJ_LABEL_TREE, id_str_prefix);
1728 break;
1729 case GOT_OBJ_TYPE_COMMIT:
1730 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1731 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1732 break;
1733 case GOT_OBJ_TYPE_TAG:
1734 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1735 GOT_OBJ_LABEL_TAG, id_str_prefix);
1736 break;
1737 default:
1738 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1739 break;
1743 return err;
1746 const struct got_error *
1747 got_repo_match_object_id(struct got_object_id **id, char **label,
1748 const char *id_str, int obj_type, struct got_reflist_head *refs,
1749 struct got_repository *repo)
1751 const struct got_error *err;
1752 struct got_tag_object *tag;
1753 struct got_reference *ref = NULL;
1755 *id = NULL;
1756 if (label)
1757 *label = NULL;
1759 if (refs) {
1760 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1761 refs, repo);
1762 if (err == NULL) {
1763 *id = got_object_id_dup(
1764 got_object_tag_get_object_id(tag));
1765 if (*id == NULL)
1766 err = got_error_from_errno("got_object_id_dup");
1767 else if (label && asprintf(label, "refs/tags/%s",
1768 got_object_tag_get_name(tag)) == -1) {
1769 err = got_error_from_errno("asprintf");
1770 free(*id);
1771 *id = NULL;
1773 got_object_tag_close(tag);
1774 return err;
1775 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1776 err->code != GOT_ERR_NO_OBJ)
1777 return err;
1780 err = got_ref_open(&ref, repo, id_str, 0);
1781 if (err == NULL) {
1782 err = got_ref_resolve(id, repo, ref);
1783 if (err)
1784 goto done;
1785 if (label) {
1786 *label = strdup(got_ref_get_name(ref));
1787 if (*label == NULL) {
1788 err = got_error_from_errno("strdup");
1789 goto done;
1792 } else {
1793 if (err->code != GOT_ERR_NOT_REF &&
1794 err->code != GOT_ERR_BAD_REF_NAME)
1795 goto done;
1796 err = got_repo_match_object_id_prefix(id, id_str,
1797 obj_type, repo);
1798 if (err) {
1799 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1800 err = got_error_not_ref(id_str);
1801 goto done;
1803 if (label) {
1804 err = got_object_id_str(label, *id);
1805 if (*label == NULL) {
1806 err = got_error_from_errno("strdup");
1807 goto done;
1811 done:
1812 if (ref)
1813 got_ref_close(ref);
1814 return err;
1817 const struct got_error *
1818 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1819 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1821 const struct got_error *err = NULL;
1822 struct got_reflist_entry *re;
1823 struct got_object_id *tag_id;
1824 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1826 *tag = NULL;
1828 TAILQ_FOREACH(re, refs, entry) {
1829 const char *refname;
1830 refname = got_ref_get_name(re->ref);
1831 if (got_ref_is_symbolic(re->ref))
1832 continue;
1833 if (strncmp(refname, "refs/tags/", 10) != 0)
1834 continue;
1835 if (!name_is_absolute)
1836 refname += strlen("refs/tags/");
1837 if (strcmp(refname, name) != 0)
1838 continue;
1839 err = got_ref_resolve(&tag_id, repo, re->ref);
1840 if (err)
1841 break;
1842 err = got_object_open_as_tag(tag, repo, tag_id);
1843 free(tag_id);
1844 if (err)
1845 break;
1846 if (obj_type == GOT_OBJ_TYPE_ANY ||
1847 got_object_tag_get_object_type(*tag) == obj_type)
1848 break;
1849 got_object_tag_close(*tag);
1850 *tag = NULL;
1853 if (err == NULL && *tag == NULL)
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_TAG, name);
1856 return err;
1859 static const struct got_error *
1860 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1861 const char *name, mode_t mode, struct got_object_id *blob_id)
1863 const struct got_error *err = NULL;
1865 *new_te = NULL;
1867 *new_te = calloc(1, sizeof(**new_te));
1868 if (*new_te == NULL)
1869 return got_error_from_errno("calloc");
1871 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1872 sizeof((*new_te)->name)) {
1873 err = got_error(GOT_ERR_NO_SPACE);
1874 goto done;
1877 if (S_ISLNK(mode)) {
1878 (*new_te)->mode = S_IFLNK;
1879 } else {
1880 (*new_te)->mode = S_IFREG;
1881 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1883 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1884 done:
1885 if (err && *new_te) {
1886 free(*new_te);
1887 *new_te = NULL;
1889 return err;
1892 static const struct got_error *
1893 import_file(struct got_tree_entry **new_te, struct dirent *de,
1894 const char *path, struct got_repository *repo)
1896 const struct got_error *err;
1897 struct got_object_id *blob_id = NULL;
1898 char *filepath;
1899 struct stat sb;
1901 if (asprintf(&filepath, "%s%s%s", path,
1902 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (lstat(filepath, &sb) != 0) {
1906 err = got_error_from_errno2("lstat", path);
1907 goto done;
1910 err = got_object_blob_create(&blob_id, filepath, repo);
1911 if (err)
1912 goto done;
1914 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1915 blob_id);
1916 done:
1917 free(filepath);
1918 if (err)
1919 free(blob_id);
1920 return err;
1923 static const struct got_error *
1924 insert_tree_entry(struct got_tree_entry *new_te,
1925 struct got_pathlist_head *paths)
1927 const struct got_error *err = NULL;
1928 struct got_pathlist_entry *new_pe;
1930 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1931 if (err)
1932 return err;
1933 if (new_pe == NULL)
1934 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1935 return NULL;
1938 static const struct got_error *write_tree(struct got_object_id **,
1939 const char *, struct got_pathlist_head *, struct got_repository *,
1940 got_repo_import_cb progress_cb, void *progress_arg);
1942 static const struct got_error *
1943 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1944 const char *path, struct got_pathlist_head *ignores,
1945 struct got_repository *repo,
1946 got_repo_import_cb progress_cb, void *progress_arg)
1948 const struct got_error *err;
1949 struct got_object_id *id = NULL;
1950 char *subdirpath;
1952 if (asprintf(&subdirpath, "%s%s%s", path,
1953 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1954 return got_error_from_errno("asprintf");
1956 (*new_te) = calloc(1, sizeof(**new_te));
1957 if (*new_te == NULL)
1958 return got_error_from_errno("calloc");
1959 (*new_te)->mode = S_IFDIR;
1960 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1961 sizeof((*new_te)->name)) {
1962 err = got_error(GOT_ERR_NO_SPACE);
1963 goto done;
1965 err = write_tree(&id, subdirpath, ignores, repo,
1966 progress_cb, progress_arg);
1967 if (err)
1968 goto done;
1969 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1971 done:
1972 free(id);
1973 free(subdirpath);
1974 if (err) {
1975 free(*new_te);
1976 *new_te = NULL;
1978 return err;
1981 static const struct got_error *
1982 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1983 struct got_pathlist_head *ignores, struct got_repository *repo,
1984 got_repo_import_cb progress_cb, void *progress_arg)
1986 const struct got_error *err = NULL;
1987 DIR *dir;
1988 struct dirent *de;
1989 int nentries;
1990 struct got_tree_entry *new_te = NULL;
1991 struct got_pathlist_head paths;
1992 struct got_pathlist_entry *pe;
1994 *new_tree_id = NULL;
1996 TAILQ_INIT(&paths);
1998 dir = opendir(path_dir);
1999 if (dir == NULL) {
2000 err = got_error_from_errno2("opendir", path_dir);
2001 goto done;
2004 nentries = 0;
2005 while ((de = readdir(dir)) != NULL) {
2006 int ignore = 0;
2007 int type;
2009 if (strcmp(de->d_name, ".") == 0 ||
2010 strcmp(de->d_name, "..") == 0)
2011 continue;
2013 TAILQ_FOREACH(pe, ignores, entry) {
2014 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2015 ignore = 1;
2016 break;
2019 if (ignore)
2020 continue;
2022 err = got_path_dirent_type(&type, path_dir, de);
2023 if (err)
2024 goto done;
2026 if (type == DT_DIR) {
2027 err = import_subdir(&new_te, de, path_dir,
2028 ignores, repo, progress_cb, progress_arg);
2029 if (err) {
2030 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2031 goto done;
2032 err = NULL;
2033 continue;
2035 } else if (type == DT_REG || type == DT_LNK) {
2036 err = import_file(&new_te, de, path_dir, repo);
2037 if (err)
2038 goto done;
2039 } else
2040 continue;
2042 err = insert_tree_entry(new_te, &paths);
2043 if (err)
2044 goto done;
2045 nentries++;
2048 if (TAILQ_EMPTY(&paths)) {
2049 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2050 "cannot create tree without any entries");
2051 goto done;
2054 TAILQ_FOREACH(pe, &paths, entry) {
2055 struct got_tree_entry *te = pe->data;
2056 char *path;
2057 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2058 continue;
2059 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2060 err = got_error_from_errno("asprintf");
2061 goto done;
2063 err = (*progress_cb)(progress_arg, path);
2064 free(path);
2065 if (err)
2066 goto done;
2069 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2070 done:
2071 if (dir)
2072 closedir(dir);
2073 got_pathlist_free(&paths);
2074 return err;
2077 const struct got_error *
2078 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2079 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2080 struct got_repository *repo, got_repo_import_cb progress_cb,
2081 void *progress_arg)
2083 const struct got_error *err;
2084 struct got_object_id *new_tree_id;
2086 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2087 progress_cb, progress_arg);
2088 if (err)
2089 return err;
2091 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2092 author, time(NULL), author, time(NULL), logmsg, repo);
2093 free(new_tree_id);
2094 return err;
2097 const struct got_error *
2098 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2099 struct got_repository *repo)
2101 const struct got_error *err = NULL;
2102 char *path_objects = NULL, *path = NULL;
2103 DIR *dir = NULL;
2104 struct got_object_id id;
2105 int i;
2107 *nobjects = 0;
2108 *ondisk_size = 0;
2110 path_objects = got_repo_get_path_objects(repo);
2111 if (path_objects == NULL)
2112 return got_error_from_errno("got_repo_get_path_objects");
2114 for (i = 0; i <= 0xff; i++) {
2115 struct dirent *dent;
2117 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2118 err = got_error_from_errno("asprintf");
2119 break;
2122 dir = opendir(path);
2123 if (dir == NULL) {
2124 if (errno == ENOENT) {
2125 err = NULL;
2126 continue;
2128 err = got_error_from_errno2("opendir", path);
2129 break;
2132 while ((dent = readdir(dir)) != NULL) {
2133 char *id_str;
2134 int fd;
2135 struct stat sb;
2137 if (strcmp(dent->d_name, ".") == 0 ||
2138 strcmp(dent->d_name, "..") == 0)
2139 continue;
2141 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2142 err = got_error_from_errno("asprintf");
2143 goto done;
2146 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2147 free(id_str);
2148 continue;
2150 free(id_str);
2152 err = got_object_open_loose_fd(&fd, &id, repo);
2153 if (err)
2154 goto done;
2156 if (fstat(fd, &sb) == -1) {
2157 err = got_error_from_errno("fstat");
2158 close(fd);
2159 goto done;
2161 (*nobjects)++;
2162 (*ondisk_size) += sb.st_size;
2164 if (close(fd) == -1) {
2165 err = got_error_from_errno("close");
2166 goto done;
2170 if (closedir(dir) != 0) {
2171 err = got_error_from_errno("closedir");
2172 goto done;
2174 dir = NULL;
2176 free(path);
2177 path = NULL;
2179 done:
2180 if (dir && closedir(dir) != 0 && err == NULL)
2181 err = got_error_from_errno("closedir");
2183 if (err) {
2184 *nobjects = 0;
2185 *ondisk_size = 0;
2187 free(path_objects);
2188 free(path);
2189 return err;
2192 const struct got_error *
2193 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2194 off_t *total_packsize, struct got_repository *repo)
2196 const struct got_error *err = NULL;
2197 DIR *packdir = NULL;
2198 struct dirent *dent;
2199 struct got_packidx *packidx = NULL;
2200 char *path_packidx;
2201 char *path_packfile;
2202 int packdir_fd;
2203 struct stat sb;
2205 *npackfiles = 0;
2206 *nobjects = 0;
2207 *total_packsize = 0;
2209 packdir_fd = openat(got_repo_get_fd(repo),
2210 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2211 if (packdir_fd == -1) {
2212 return got_error_from_errno_fmt("openat: %s/%s",
2213 got_repo_get_path_git_dir(repo),
2214 GOT_OBJECTS_PACK_DIR);
2217 packdir = fdopendir(packdir_fd);
2218 if (packdir == NULL) {
2219 err = got_error_from_errno("fdopendir");
2220 goto done;
2223 while ((dent = readdir(packdir)) != NULL) {
2224 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2225 continue;
2227 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2228 dent->d_name) == -1) {
2229 err = got_error_from_errno("asprintf");
2230 goto done;
2233 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2234 path_packidx, 0);
2235 free(path_packidx);
2236 if (err)
2237 goto done;
2239 if (fstat(packidx->fd, &sb) == -1)
2240 goto done;
2241 *total_packsize += sb.st_size;
2243 err = got_packidx_get_packfile_path(&path_packfile,
2244 packidx->path_packidx);
2245 if (err)
2246 goto done;
2248 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2249 0) == -1) {
2250 free(path_packfile);
2251 goto done;
2253 free(path_packfile);
2254 *total_packsize += sb.st_size;
2256 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2258 (*npackfiles)++;
2260 got_packidx_close(packidx);
2261 packidx = NULL;
2263 done:
2264 if (packidx)
2265 got_packidx_close(packidx);
2266 if (packdir && closedir(packdir) != 0 && err == NULL)
2267 err = got_error_from_errno("closedir");
2268 if (err) {
2269 *npackfiles = 0;
2270 *nobjects = 0;
2271 *total_packsize = 0;
2273 return err;
2276 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2277 got_packidx_bloom_filter_cmp);