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"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_gotconfig.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 #endif
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
73 const char *
74 got_repo_get_path(struct got_repository *repo)
75 {
76 return repo->path;
77 }
79 const char *
80 got_repo_get_path_git_dir(struct got_repository *repo)
81 {
82 return repo->path_git_dir;
83 }
85 int
86 got_repo_get_fd(struct got_repository *repo)
87 {
88 return repo->gitdir_fd;
89 }
91 const char *
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_name;
95 }
97 const char *
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 {
100 return repo->gitconfig_author_email;
103 const char *
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
109 const char *
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
115 const char *
116 got_repo_get_gitconfig_owner(struct got_repository *repo)
118 return repo->gitconfig_owner;
121 void
122 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
123 struct got_repository *repo)
125 *extensions = repo->extensions;
126 *nextensions = repo->nextensions;
129 int
130 got_repo_is_bare(struct got_repository *repo)
132 return (strcmp(repo->path, repo->path_git_dir) == 0);
135 static char *
136 get_path_git_child(struct got_repository *repo, const char *basename)
138 char *path_child;
140 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
141 basename) == -1)
142 return NULL;
144 return path_child;
147 char *
148 got_repo_get_path_objects(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_DIR);
153 char *
154 got_repo_get_path_objects_pack(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
159 char *
160 got_repo_get_path_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_REFS_DIR);
165 char *
166 got_repo_get_path_packed_refs(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
171 static char *
172 get_path_head(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_HEAD_FILE);
177 char *
178 got_repo_get_path_gitconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GITCONFIG);
183 char *
184 got_repo_get_path_gotconfig(struct got_repository *repo)
186 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
189 const struct got_gotconfig *
190 got_repo_get_gotconfig(struct got_repository *repo)
192 return repo->gotconfig;
195 void
196 got_repo_get_gitconfig_remotes(int *nremotes,
197 const struct got_remote_repo **remotes, struct got_repository *repo)
199 *nremotes = repo->ngitconfig_remotes;
200 *remotes = repo->gitconfig_remotes;
203 static int
204 is_git_repo(struct got_repository *repo)
206 const char *path_git = got_repo_get_path_git_dir(repo);
207 char *path_objects = got_repo_get_path_objects(repo);
208 char *path_refs = got_repo_get_path_refs(repo);
209 char *path_head = get_path_head(repo);
210 int ret = 0;
211 struct stat sb;
212 struct got_reference *head_ref;
214 if (lstat(path_git, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_objects, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_refs, &sb) == -1)
225 goto done;
226 if (!S_ISDIR(sb.st_mode))
227 goto done;
229 if (lstat(path_head, &sb) == -1)
230 goto done;
231 if (!S_ISREG(sb.st_mode))
232 goto done;
234 /* Check if the HEAD reference can be opened. */
235 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
236 goto done;
237 got_ref_close(head_ref);
239 ret = 1;
240 done:
241 free(path_objects);
242 free(path_refs);
243 free(path_head);
244 return ret;
248 const struct got_error *
249 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
250 struct got_object *obj)
252 #ifndef GOT_NO_OBJ_CACHE
253 const struct got_error *err = NULL;
254 err = got_object_cache_add(&repo->objcache, id, obj);
255 if (err) {
256 if (err->code == GOT_ERR_OBJ_EXISTS ||
257 err->code == GOT_ERR_OBJ_TOO_LARGE)
258 err = NULL;
259 return err;
261 obj->refcnt++;
262 #endif
263 return NULL;
266 struct got_object *
267 got_repo_get_cached_object(struct got_repository *repo,
268 struct got_object_id *id)
270 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
273 const struct got_error *
274 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
275 struct got_tree_object *tree)
277 #ifndef GOT_NO_OBJ_CACHE
278 const struct got_error *err = NULL;
279 err = got_object_cache_add(&repo->treecache, id, tree);
280 if (err) {
281 if (err->code == GOT_ERR_OBJ_EXISTS ||
282 err->code == GOT_ERR_OBJ_TOO_LARGE)
283 err = NULL;
284 return err;
286 tree->refcnt++;
287 #endif
288 return NULL;
291 struct got_tree_object *
292 got_repo_get_cached_tree(struct got_repository *repo,
293 struct got_object_id *id)
295 return (struct got_tree_object *)got_object_cache_get(
296 &repo->treecache, id);
299 const struct got_error *
300 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
301 struct got_commit_object *commit)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->commitcache, id, commit);
306 if (err) {
307 if (err->code == GOT_ERR_OBJ_EXISTS ||
308 err->code == GOT_ERR_OBJ_TOO_LARGE)
309 err = NULL;
310 return err;
312 commit->refcnt++;
313 #endif
314 return NULL;
317 struct got_commit_object *
318 got_repo_get_cached_commit(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_commit_object *)got_object_cache_get(
322 &repo->commitcache, id);
325 const struct got_error *
326 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
327 struct got_tag_object *tag)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->tagcache, id, tag);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tag->refcnt++;
339 #endif
340 return NULL;
343 struct got_tag_object *
344 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
346 return (struct got_tag_object *)got_object_cache_get(
347 &repo->tagcache, id);
350 const struct got_error *
351 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
352 struct got_raw_object *raw)
354 #ifndef GOT_NO_OBJ_CACHE
355 const struct got_error *err = NULL;
356 err = got_object_cache_add(&repo->rawcache, id, raw);
357 if (err) {
358 if (err->code == GOT_ERR_OBJ_EXISTS ||
359 err->code == GOT_ERR_OBJ_TOO_LARGE)
360 err = NULL;
361 return err;
363 raw->refcnt++;
364 #endif
365 return NULL;
369 struct got_raw_object *
370 got_repo_get_cached_raw_object(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
377 static const struct got_error *
378 open_repo(struct got_repository *repo, const char *path)
380 const struct got_error *err = NULL;
382 repo->gitdir_fd = -1;
384 /* bare git repository? */
385 repo->path_git_dir = strdup(path);
386 if (repo->path_git_dir == NULL)
387 return got_error_from_errno("strdup");
388 if (is_git_repo(repo)) {
389 repo->path = strdup(repo->path_git_dir);
390 if (repo->path == NULL) {
391 err = got_error_from_errno("strdup");
392 goto done;
394 repo->gitdir_fd = open(repo->path_git_dir,
395 O_DIRECTORY | O_CLOEXEC);
396 if (repo->gitdir_fd == -1) {
397 err = got_error_from_errno2("open",
398 repo->path_git_dir);
399 goto done;
401 return NULL;
404 /* git repository with working tree? */
405 free(repo->path_git_dir);
406 repo->path_git_dir = NULL;
407 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
408 err = got_error_from_errno("asprintf");
409 goto done;
411 if (is_git_repo(repo)) {
412 repo->path = strdup(path);
413 if (repo->path == NULL) {
414 err = got_error_from_errno("strdup");
415 goto done;
417 repo->gitdir_fd = open(repo->path_git_dir,
418 O_DIRECTORY | O_CLOEXEC);
419 if (repo->gitdir_fd == -1) {
420 err = got_error_from_errno2("open",
421 repo->path_git_dir);
422 goto done;
424 return NULL;
427 err = got_error(GOT_ERR_NOT_GIT_REPO);
428 done:
429 if (err) {
430 free(repo->path);
431 repo->path = NULL;
432 free(repo->path_git_dir);
433 repo->path_git_dir = NULL;
434 if (repo->gitdir_fd != -1)
435 close(repo->gitdir_fd);
436 repo->gitdir_fd = -1;
439 return err;
442 static const struct got_error *
443 parse_gitconfig_file(int *gitconfig_repository_format_version,
444 char **gitconfig_author_name, char **gitconfig_author_email,
445 struct got_remote_repo **remotes, int *nremotes,
446 char **gitconfig_owner, char ***extensions, int *nextensions,
447 const char *gitconfig_path)
449 const struct got_error *err = NULL, *child_err = NULL;
450 int fd = -1;
451 int imsg_fds[2] = { -1, -1 };
452 pid_t pid;
453 struct imsgbuf *ibuf;
455 *gitconfig_repository_format_version = 0;
456 if (extensions)
457 *extensions = NULL;
458 if (nextensions)
459 *nextensions = 0;
460 *gitconfig_author_name = NULL;
461 *gitconfig_author_email = NULL;
462 if (remotes)
463 *remotes = NULL;
464 if (nremotes)
465 *nremotes = 0;
466 if (gitconfig_owner)
467 *gitconfig_owner = NULL;
469 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
470 if (fd == -1) {
471 if (errno == ENOENT)
472 return NULL;
473 return got_error_from_errno2("open", gitconfig_path);
476 ibuf = calloc(1, sizeof(*ibuf));
477 if (ibuf == NULL) {
478 err = got_error_from_errno("calloc");
479 goto done;
482 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
483 err = got_error_from_errno("socketpair");
484 goto done;
487 pid = fork();
488 if (pid == -1) {
489 err = got_error_from_errno("fork");
490 goto done;
491 } else if (pid == 0) {
492 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
493 gitconfig_path);
494 /* not reached */
497 if (close(imsg_fds[1]) == -1) {
498 err = got_error_from_errno("close");
499 goto done;
501 imsg_fds[1] = -1;
502 imsg_init(ibuf, imsg_fds[0]);
504 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
505 if (err)
506 goto done;
507 fd = -1;
509 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
510 if (err)
511 goto done;
513 err = got_privsep_recv_gitconfig_int(
514 gitconfig_repository_format_version, ibuf);
515 if (err)
516 goto done;
518 if (extensions && nextensions) {
519 err = got_privsep_send_gitconfig_repository_extensions_req(
520 ibuf);
521 if (err)
522 goto done;
523 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
524 if (err)
525 goto done;
526 if (*nextensions > 0) {
527 int i;
528 *extensions = calloc(*nextensions, sizeof(char *));
529 if (*extensions == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
533 for (i = 0; i < *nextensions; i++) {
534 char *ext;
535 err = got_privsep_recv_gitconfig_str(&ext,
536 ibuf);
537 if (err)
538 goto done;
539 (*extensions)[i] = ext;
544 err = got_privsep_send_gitconfig_author_name_req(ibuf);
545 if (err)
546 goto done;
548 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
549 if (err)
550 goto done;
552 err = got_privsep_send_gitconfig_author_email_req(ibuf);
553 if (err)
554 goto done;
556 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
557 if (err)
558 goto done;
560 if (remotes && nremotes) {
561 err = got_privsep_send_gitconfig_remotes_req(ibuf);
562 if (err)
563 goto done;
565 err = got_privsep_recv_gitconfig_remotes(remotes,
566 nremotes, ibuf);
567 if (err)
568 goto done;
571 if (gitconfig_owner) {
572 err = got_privsep_send_gitconfig_owner_req(ibuf);
573 if (err)
574 goto done;
575 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
576 if (err)
577 goto done;
580 imsg_clear(ibuf);
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 *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 err = got_error_from_errno("calloc");
670 goto done;
673 RB_INIT(&repo->packidx_bloom_filters);
675 for (i = 0; i < nitems(repo->privsep_children); i++) {
676 memset(&repo->privsep_children[i], 0,
677 sizeof(repo->privsep_children[0]));
678 repo->privsep_children[i].imsg_fd = -1;
681 err = got_object_cache_init(&repo->objcache,
682 GOT_OBJECT_CACHE_TYPE_OBJ);
683 if (err)
684 goto done;
685 err = got_object_cache_init(&repo->treecache,
686 GOT_OBJECT_CACHE_TYPE_TREE);
687 if (err)
688 goto done;
689 err = got_object_cache_init(&repo->commitcache,
690 GOT_OBJECT_CACHE_TYPE_COMMIT);
691 if (err)
692 goto done;
693 err = got_object_cache_init(&repo->tagcache,
694 GOT_OBJECT_CACHE_TYPE_TAG);
695 if (err)
696 goto done;
697 err = got_object_cache_init(&repo->rawcache,
698 GOT_OBJECT_CACHE_TYPE_RAW);
699 if (err)
700 goto done;
702 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
703 if (repo->pack_cache_size > rl.rlim_cur / 8)
704 repo->pack_cache_size = rl.rlim_cur / 8;
706 repo_path = realpath(path, NULL);
707 if (repo_path == NULL) {
708 err = got_error_from_errno2("realpath", path);
709 goto done;
712 for (;;) {
713 char *parent_path;
715 err = open_repo(repo, repo_path);
716 if (err == NULL)
717 break;
718 if (err->code != GOT_ERR_NOT_GIT_REPO)
719 goto done;
720 if (repo_path[0] == '/' && repo_path[1] == '\0') {
721 err = got_error(GOT_ERR_NOT_GIT_REPO);
722 goto done;
724 err = got_path_dirname(&parent_path, repo_path);
725 if (err)
726 goto done;
727 free(repo_path);
728 repo_path = parent_path;
731 err = read_gotconfig(repo);
732 if (err)
733 goto done;
735 err = read_gitconfig(repo, global_gitconfig_path);
736 if (err)
737 goto done;
738 if (repo->gitconfig_repository_format_version != 0)
739 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
740 for (i = 0; i < repo->nextensions; i++) {
741 char *ext = repo->extensions[i];
742 int j, supported = 0;
743 for (j = 0; j < nitems(repo_extensions); j++) {
744 if (strcmp(ext, repo_extensions[j]) == 0) {
745 supported = 1;
746 break;
749 if (!supported) {
750 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
751 goto done;
754 done:
755 if (err)
756 got_repo_close(repo);
757 else
758 *repop = repo;
759 free(repo_path);
760 return err;
763 const struct got_error *
764 got_repo_close(struct got_repository *repo)
766 const struct got_error *err = NULL, *child_err;
767 struct got_packidx_bloom_filter *bf;
768 size_t i;
770 for (i = 0; i < repo->pack_cache_size; i++) {
771 if (repo->packidx_cache[i] == NULL)
772 break;
773 got_packidx_close(repo->packidx_cache[i]);
776 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
777 &repo->packidx_bloom_filters))) {
778 RB_REMOVE(got_packidx_bloom_filter_tree,
779 &repo->packidx_bloom_filters, bf);
780 free(bf->bloom);
781 free(bf);
784 for (i = 0; i < repo->pack_cache_size; i++) {
785 if (repo->packs[i].path_packfile == NULL)
786 break;
787 got_pack_close(&repo->packs[i]);
790 free(repo->path);
791 free(repo->path_git_dir);
793 got_object_cache_close(&repo->objcache);
794 got_object_cache_close(&repo->treecache);
795 got_object_cache_close(&repo->commitcache);
796 got_object_cache_close(&repo->tagcache);
797 got_object_cache_close(&repo->rawcache);
799 for (i = 0; i < nitems(repo->privsep_children); i++) {
800 if (repo->privsep_children[i].imsg_fd == -1)
801 continue;
802 imsg_clear(repo->privsep_children[i].ibuf);
803 free(repo->privsep_children[i].ibuf);
804 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
805 child_err = got_privsep_wait_for_child(
806 repo->privsep_children[i].pid);
807 if (child_err && err == NULL)
808 err = child_err;
809 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
810 err == NULL)
811 err = got_error_from_errno("close");
814 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
815 err == NULL)
816 err = got_error_from_errno("close");
818 if (repo->gotconfig)
819 got_gotconfig_free(repo->gotconfig);
820 free(repo->gitconfig_author_name);
821 free(repo->gitconfig_author_email);
822 for (i = 0; i < repo->ngitconfig_remotes; i++)
823 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
824 free(repo->gitconfig_remotes);
825 for (i = 0; i < repo->nextensions; i++)
826 free(repo->extensions[i]);
827 free(repo->extensions);
828 free(repo);
830 return err;
833 void
834 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
836 int i;
838 free(repo->name);
839 repo->name = NULL;
840 free(repo->fetch_url);
841 repo->fetch_url = NULL;
842 free(repo->send_url);
843 repo->send_url = NULL;
844 for (i = 0; i < repo->nfetch_branches; i++)
845 free(repo->fetch_branches[i]);
846 free(repo->fetch_branches);
847 repo->fetch_branches = NULL;
848 repo->nfetch_branches = 0;
849 for (i = 0; i < repo->nsend_branches; i++)
850 free(repo->send_branches[i]);
851 free(repo->send_branches);
852 repo->send_branches = NULL;
853 repo->nsend_branches = 0;
856 const struct got_error *
857 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
858 const char *input_path)
860 const struct got_error *err = NULL;
861 const char *repo_abspath = NULL;
862 size_t repolen, len;
863 char *canonpath, *path = NULL;
865 *in_repo_path = NULL;
867 canonpath = strdup(input_path);
868 if (canonpath == NULL) {
869 err = got_error_from_errno("strdup");
870 goto done;
872 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
873 if (err)
874 goto done;
876 repo_abspath = got_repo_get_path(repo);
878 if (canonpath[0] == '\0') {
879 path = strdup(canonpath);
880 if (path == NULL) {
881 err = got_error_from_errno("strdup");
882 goto done;
884 } else {
885 path = realpath(canonpath, NULL);
886 if (path == NULL) {
887 if (errno != ENOENT) {
888 err = got_error_from_errno2("realpath",
889 canonpath);
890 goto done;
892 /*
893 * Path is not on disk.
894 * Assume it is already relative to repository root.
895 */
896 path = strdup(canonpath);
897 if (path == NULL) {
898 err = got_error_from_errno("strdup");
899 goto done;
903 repolen = strlen(repo_abspath);
904 len = strlen(path);
907 if (strcmp(path, repo_abspath) == 0) {
908 free(path);
909 path = strdup("");
910 if (path == NULL) {
911 err = got_error_from_errno("strdup");
912 goto done;
914 } else if (len > repolen &&
915 got_path_is_child(path, repo_abspath, repolen)) {
916 /* Matched an on-disk path inside repository. */
917 if (got_repo_is_bare(repo)) {
918 /*
919 * Matched an on-disk path inside repository
920 * database. Treat input as repository-relative.
921 */
922 free(path);
923 path = canonpath;
924 canonpath = NULL;
925 } else {
926 char *child;
927 /* Strip common prefix with repository path. */
928 err = got_path_skip_common_ancestor(&child,
929 repo_abspath, path);
930 if (err)
931 goto done;
932 free(path);
933 path = child;
935 } else {
936 /*
937 * Matched unrelated on-disk path.
938 * Treat input as repository-relative.
939 */
940 free(path);
941 path = canonpath;
942 canonpath = NULL;
946 /* Make in-repository path absolute */
947 if (path[0] != '/') {
948 char *abspath;
949 if (asprintf(&abspath, "/%s", path) == -1) {
950 err = got_error_from_errno("asprintf");
951 goto done;
953 free(path);
954 path = abspath;
957 done:
958 free(canonpath);
959 if (err)
960 free(path);
961 else
962 *in_repo_path = path;
963 return err;
966 static const struct got_error *
967 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
968 const char *path_packidx)
970 const struct got_error *err = NULL;
971 size_t i;
973 for (i = 0; i < repo->pack_cache_size; i++) {
974 if (repo->packidx_cache[i] == NULL)
975 break;
976 if (strcmp(repo->packidx_cache[i]->path_packidx,
977 path_packidx) == 0) {
978 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
981 if (i == repo->pack_cache_size) {
982 i = repo->pack_cache_size - 1;
983 err = got_packidx_close(repo->packidx_cache[i]);
984 if (err)
985 return err;
988 repo->packidx_cache[i] = packidx;
990 return NULL;
993 int
994 got_repo_is_packidx_filename(const char *name, size_t len)
996 if (len != GOT_PACKIDX_NAMELEN)
997 return 0;
999 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1000 return 0;
1002 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1003 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1004 return 0;
1006 return 1;
1009 static struct got_packidx_bloom_filter *
1010 get_packidx_bloom_filter(struct got_repository *repo,
1011 const char *path, size_t path_len)
1013 struct got_packidx_bloom_filter key;
1015 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1016 return NULL; /* XXX */
1017 key.path_len = path_len;
1019 return RB_FIND(got_packidx_bloom_filter_tree,
1020 &repo->packidx_bloom_filters, &key);
1023 static int
1024 check_packidx_bloom_filter(struct got_repository *repo,
1025 const char *path_packidx, struct got_object_id *id)
1027 struct got_packidx_bloom_filter *bf;
1029 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1030 if (bf)
1031 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1033 /* No bloom filter means this pack index must be searched. */
1034 return 1;
1037 static const struct got_error *
1038 add_packidx_bloom_filter(struct got_repository *repo,
1039 struct got_packidx *packidx, const char *path_packidx)
1041 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1042 struct got_packidx_bloom_filter *bf;
1043 size_t len;
1046 * Don't use bloom filters for very large pack index files.
1047 * Large pack files will contain a relatively large fraction
1048 * of our objects so we will likely need to visit them anyway.
1049 * The more objects a pack file contains the higher the probability
1050 * of a false-positive match from the bloom filter. And reading
1051 * all object IDs from a large pack index file can be expensive.
1053 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1054 return NULL;
1056 /* Do we already have a filter for this pack index? */
1057 if (get_packidx_bloom_filter(repo, path_packidx,
1058 strlen(path_packidx)) != NULL)
1059 return NULL;
1061 bf = calloc(1, sizeof(*bf));
1062 if (bf == NULL)
1063 return got_error_from_errno("calloc");
1064 bf->bloom = calloc(1, sizeof(*bf->bloom));
1065 if (bf->bloom == NULL) {
1066 free(bf);
1067 return got_error_from_errno("calloc");
1070 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1071 if (len >= sizeof(bf->path)) {
1072 free(bf->bloom);
1073 free(bf);
1074 return got_error(GOT_ERR_NO_SPACE);
1076 bf->path_len = len;
1078 /* Minimum size supported by our bloom filter is 1000 entries. */
1079 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1080 for (i = 0; i < nobjects; i++) {
1081 struct got_packidx_object_id *id;
1082 id = &packidx->hdr.sorted_ids[i];
1083 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1086 RB_INSERT(got_packidx_bloom_filter_tree,
1087 &repo->packidx_bloom_filters, bf);
1088 return NULL;
1091 const struct got_error *
1092 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1093 struct got_repository *repo, struct got_object_id *id)
1095 const struct got_error *err;
1096 DIR *packdir = NULL;
1097 struct dirent *dent;
1098 char *path_packidx;
1099 size_t i;
1100 int packdir_fd;
1102 /* Search pack index cache. */
1103 for (i = 0; i < repo->pack_cache_size; i++) {
1104 if (repo->packidx_cache[i] == NULL)
1105 break;
1106 if (!check_packidx_bloom_filter(repo,
1107 repo->packidx_cache[i]->path_packidx, id))
1108 continue; /* object will not be found in this index */
1109 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1110 if (*idx != -1) {
1111 *packidx = repo->packidx_cache[i];
1113 * Move this cache entry to the front. Repeatedly
1114 * searching a wrong pack index can be expensive.
1116 if (i > 0) {
1117 memmove(&repo->packidx_cache[1],
1118 &repo->packidx_cache[0],
1119 i * sizeof(repo->packidx_cache[0]));
1120 repo->packidx_cache[0] = *packidx;
1122 return NULL;
1125 /* No luck. Search the filesystem. */
1127 packdir_fd = openat(got_repo_get_fd(repo),
1128 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1129 if (packdir_fd == -1) {
1130 if (errno == ENOENT)
1131 err = got_error_no_obj(id);
1132 else
1133 err = got_error_from_errno_fmt("openat: %s/%s",
1134 got_repo_get_path_git_dir(repo),
1135 GOT_OBJECTS_PACK_DIR);
1136 goto done;
1139 packdir = fdopendir(packdir_fd);
1140 if (packdir == NULL) {
1141 err = got_error_from_errno("fdopendir");
1142 goto done;
1145 while ((dent = readdir(packdir)) != NULL) {
1146 int is_cached = 0;
1148 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1149 continue;
1151 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1152 dent->d_name) == -1) {
1153 err = got_error_from_errno("asprintf");
1154 goto done;
1157 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1158 free(path_packidx);
1159 continue; /* object will not be found in this index */
1162 for (i = 0; i < repo->pack_cache_size; i++) {
1163 if (repo->packidx_cache[i] == NULL)
1164 break;
1165 if (strcmp(repo->packidx_cache[i]->path_packidx,
1166 path_packidx) == 0) {
1167 is_cached = 1;
1168 break;
1171 if (is_cached) {
1172 free(path_packidx);
1173 continue; /* already searched */
1176 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1177 path_packidx, 0);
1178 if (err) {
1179 free(path_packidx);
1180 goto done;
1183 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1184 if (err) {
1185 free(path_packidx);
1186 goto done;
1189 err = cache_packidx(repo, *packidx, path_packidx);
1190 free(path_packidx);
1191 if (err)
1192 goto done;
1194 *idx = got_packidx_get_object_idx(*packidx, id);
1195 if (*idx != -1) {
1196 err = NULL; /* found the object */
1197 goto done;
1201 err = got_error_no_obj(id);
1202 done:
1203 if (packdir && closedir(packdir) != 0 && err == NULL)
1204 err = got_error_from_errno("closedir");
1205 return err;
1208 static const struct got_error *
1209 read_packfile_hdr(int fd, struct got_packidx *packidx)
1211 const struct got_error *err = NULL;
1212 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1213 struct got_packfile_hdr hdr;
1214 ssize_t n;
1216 n = read(fd, &hdr, sizeof(hdr));
1217 if (n < 0)
1218 return got_error_from_errno("read");
1219 if (n != sizeof(hdr))
1220 return got_error(GOT_ERR_BAD_PACKFILE);
1222 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1223 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1224 be32toh(hdr.nobjects) != totobj)
1225 err = got_error(GOT_ERR_BAD_PACKFILE);
1227 return err;
1230 static const struct got_error *
1231 open_packfile(int *fd, struct got_repository *repo,
1232 const char *relpath, struct got_packidx *packidx)
1234 const struct got_error *err = NULL;
1236 *fd = openat(got_repo_get_fd(repo), relpath,
1237 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1238 if (*fd == -1)
1239 return got_error_from_errno_fmt("openat: %s/%s",
1240 got_repo_get_path_git_dir(repo), relpath);
1242 if (packidx) {
1243 err = read_packfile_hdr(*fd, packidx);
1244 if (err) {
1245 close(*fd);
1246 *fd = -1;
1250 return err;
1253 const struct got_error *
1254 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1255 const char *path_packfile, struct got_packidx *packidx)
1257 const struct got_error *err = NULL;
1258 struct got_pack *pack = NULL;
1259 struct stat sb;
1260 size_t i;
1262 if (packp)
1263 *packp = NULL;
1265 for (i = 0; i < repo->pack_cache_size; i++) {
1266 pack = &repo->packs[i];
1267 if (pack->path_packfile == NULL)
1268 break;
1269 if (strcmp(pack->path_packfile, path_packfile) == 0)
1270 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1273 if (i == repo->pack_cache_size) {
1274 err = got_pack_close(&repo->packs[i - 1]);
1275 if (err)
1276 return err;
1277 memmove(&repo->packs[1], &repo->packs[0],
1278 sizeof(repo->packs) - sizeof(repo->packs[0]));
1279 i = 0;
1282 pack = &repo->packs[i];
1284 pack->path_packfile = strdup(path_packfile);
1285 if (pack->path_packfile == NULL) {
1286 err = got_error_from_errno("strdup");
1287 goto done;
1290 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1291 if (err)
1292 goto done;
1294 if (fstat(pack->fd, &sb) != 0) {
1295 err = got_error_from_errno("fstat");
1296 goto done;
1298 pack->filesize = sb.st_size;
1300 pack->privsep_child = NULL;
1302 #ifndef GOT_PACK_NO_MMAP
1303 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1304 pack->fd, 0);
1305 if (pack->map == MAP_FAILED) {
1306 if (errno != ENOMEM) {
1307 err = got_error_from_errno("mmap");
1308 goto done;
1310 pack->map = NULL; /* fall back to read(2) */
1312 #endif
1313 done:
1314 if (err) {
1315 if (pack) {
1316 free(pack->path_packfile);
1317 memset(pack, 0, sizeof(*pack));
1319 } else if (packp)
1320 *packp = pack;
1321 return err;
1324 struct got_pack *
1325 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1327 struct got_pack *pack = NULL;
1328 size_t i;
1330 for (i = 0; i < repo->pack_cache_size; i++) {
1331 pack = &repo->packs[i];
1332 if (pack->path_packfile == NULL)
1333 break;
1334 if (strcmp(pack->path_packfile, path_packfile) == 0)
1335 return pack;
1338 return NULL;
1341 const struct got_error *
1342 got_repo_init(const char *repo_path)
1344 const struct got_error *err = NULL;
1345 const char *dirnames[] = {
1346 GOT_OBJECTS_DIR,
1347 GOT_OBJECTS_PACK_DIR,
1348 GOT_REFS_DIR,
1350 const char *description_str = "Unnamed repository; "
1351 "edit this file 'description' to name the repository.";
1352 const char *headref_str = "ref: refs/heads/main";
1353 const char *gitconfig_str = "[core]\n"
1354 "\trepositoryformatversion = 0\n"
1355 "\tfilemode = true\n"
1356 "\tbare = true\n";
1357 char *path;
1358 size_t i;
1360 if (!got_path_dir_is_empty(repo_path))
1361 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1363 for (i = 0; i < nitems(dirnames); i++) {
1364 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1365 return got_error_from_errno("asprintf");
1367 err = got_path_mkdir(path);
1368 free(path);
1369 if (err)
1370 return err;
1373 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1374 return got_error_from_errno("asprintf");
1375 err = got_path_create_file(path, description_str);
1376 free(path);
1377 if (err)
1378 return err;
1380 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1381 return got_error_from_errno("asprintf");
1382 err = got_path_create_file(path, headref_str);
1383 free(path);
1384 if (err)
1385 return err;
1387 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1388 return got_error_from_errno("asprintf");
1389 err = got_path_create_file(path, gitconfig_str);
1390 free(path);
1391 if (err)
1392 return err;
1394 return NULL;
1397 static const struct got_error *
1398 match_packed_object(struct got_object_id **unique_id,
1399 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1401 const struct got_error *err = NULL;
1402 DIR *packdir = NULL;
1403 struct dirent *dent;
1404 char *path_packidx;
1405 struct got_object_id_queue matched_ids;
1406 int packdir_fd;
1408 STAILQ_INIT(&matched_ids);
1410 packdir_fd = openat(got_repo_get_fd(repo),
1411 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1412 if (packdir_fd == -1) {
1413 if (errno != ENOENT) {
1414 err = got_error_from_errno2("openat",
1415 GOT_OBJECTS_PACK_DIR);
1417 goto done;
1420 packdir = fdopendir(packdir_fd);
1421 if (packdir == NULL) {
1422 err = got_error_from_errno("fdopendir");
1423 goto done;
1426 while ((dent = readdir(packdir)) != NULL) {
1427 struct got_packidx *packidx;
1428 struct got_object_qid *qid;
1430 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1431 continue;
1433 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1434 dent->d_name) == -1) {
1435 err = got_error_from_errno("strdup");
1436 break;
1439 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1440 path_packidx, 0);
1441 free(path_packidx);
1442 if (err)
1443 break;
1445 err = got_packidx_match_id_str_prefix(&matched_ids,
1446 packidx, id_str_prefix);
1447 if (err) {
1448 got_packidx_close(packidx);
1449 break;
1451 err = got_packidx_close(packidx);
1452 if (err)
1453 break;
1455 STAILQ_FOREACH(qid, &matched_ids, entry) {
1456 if (obj_type != GOT_OBJ_TYPE_ANY) {
1457 int matched_type;
1458 err = got_object_get_type(&matched_type, repo,
1459 qid->id);
1460 if (err)
1461 goto done;
1462 if (matched_type != obj_type)
1463 continue;
1465 if (*unique_id == NULL) {
1466 *unique_id = got_object_id_dup(qid->id);
1467 if (*unique_id == NULL) {
1468 err = got_error_from_errno("malloc");
1469 goto done;
1471 } else {
1472 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1473 continue; /* packed multiple times */
1474 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1475 goto done;
1479 done:
1480 got_object_id_queue_free(&matched_ids);
1481 if (packdir && closedir(packdir) != 0 && err == NULL)
1482 err = got_error_from_errno("closedir");
1483 if (err) {
1484 free(*unique_id);
1485 *unique_id = NULL;
1487 return err;
1490 static const struct got_error *
1491 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1492 const char *object_dir, const char *id_str_prefix, int obj_type,
1493 struct got_repository *repo)
1495 const struct got_error *err = NULL;
1496 char *path;
1497 DIR *dir = NULL;
1498 struct dirent *dent;
1499 struct got_object_id id;
1501 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1502 err = got_error_from_errno("asprintf");
1503 goto done;
1506 dir = opendir(path);
1507 if (dir == NULL) {
1508 if (errno == ENOENT) {
1509 err = NULL;
1510 goto done;
1512 err = got_error_from_errno2("opendir", path);
1513 goto done;
1515 while ((dent = readdir(dir)) != NULL) {
1516 char *id_str;
1517 int cmp;
1519 if (strcmp(dent->d_name, ".") == 0 ||
1520 strcmp(dent->d_name, "..") == 0)
1521 continue;
1523 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1524 err = got_error_from_errno("asprintf");
1525 goto done;
1528 if (!got_parse_sha1_digest(id.sha1, id_str))
1529 continue;
1532 * Directory entries do not necessarily appear in
1533 * sorted order, so we must iterate over all of them.
1535 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1536 if (cmp != 0) {
1537 free(id_str);
1538 continue;
1541 if (*unique_id == NULL) {
1542 if (obj_type != GOT_OBJ_TYPE_ANY) {
1543 int matched_type;
1544 err = got_object_get_type(&matched_type, repo,
1545 &id);
1546 if (err)
1547 goto done;
1548 if (matched_type != obj_type)
1549 continue;
1551 *unique_id = got_object_id_dup(&id);
1552 if (*unique_id == NULL) {
1553 err = got_error_from_errno("got_object_id_dup");
1554 free(id_str);
1555 goto done;
1557 } else {
1558 if (got_object_id_cmp(*unique_id, &id) == 0)
1559 continue; /* both packed and loose */
1560 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1561 free(id_str);
1562 goto done;
1565 done:
1566 if (dir && closedir(dir) != 0 && err == NULL)
1567 err = got_error_from_errno("closedir");
1568 if (err) {
1569 free(*unique_id);
1570 *unique_id = NULL;
1572 free(path);
1573 return err;
1576 const struct got_error *
1577 got_repo_match_object_id_prefix(struct got_object_id **id,
1578 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1580 const struct got_error *err = NULL;
1581 char *path_objects = got_repo_get_path_objects(repo);
1582 char *object_dir = NULL;
1583 size_t len;
1584 int i;
1586 *id = NULL;
1588 for (i = 0; i < strlen(id_str_prefix); i++) {
1589 if (isxdigit((unsigned char)id_str_prefix[i]))
1590 continue;
1591 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1594 len = strlen(id_str_prefix);
1595 if (len >= 2) {
1596 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1597 if (err)
1598 goto done;
1599 object_dir = strndup(id_str_prefix, 2);
1600 if (object_dir == NULL) {
1601 err = got_error_from_errno("strdup");
1602 goto done;
1604 err = match_loose_object(id, path_objects, object_dir,
1605 id_str_prefix, obj_type, repo);
1606 } else if (len == 1) {
1607 int i;
1608 for (i = 0; i < 0xf; i++) {
1609 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1610 == -1) {
1611 err = got_error_from_errno("asprintf");
1612 goto done;
1614 err = match_packed_object(id, repo, object_dir,
1615 obj_type);
1616 if (err)
1617 goto done;
1618 err = match_loose_object(id, path_objects, object_dir,
1619 id_str_prefix, obj_type, repo);
1620 if (err)
1621 goto done;
1623 } else {
1624 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1625 goto done;
1627 done:
1628 free(object_dir);
1629 if (err) {
1630 free(*id);
1631 *id = NULL;
1632 } else if (*id == NULL) {
1633 switch (obj_type) {
1634 case GOT_OBJ_TYPE_BLOB:
1635 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1636 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1637 break;
1638 case GOT_OBJ_TYPE_TREE:
1639 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1640 GOT_OBJ_LABEL_TREE, id_str_prefix);
1641 break;
1642 case GOT_OBJ_TYPE_COMMIT:
1643 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1644 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1645 break;
1646 case GOT_OBJ_TYPE_TAG:
1647 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1648 GOT_OBJ_LABEL_TAG, id_str_prefix);
1649 break;
1650 default:
1651 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1652 break;
1656 return err;
1659 const struct got_error *
1660 got_repo_match_object_id(struct got_object_id **id, char **label,
1661 const char *id_str, int obj_type, struct got_reflist_head *refs,
1662 struct got_repository *repo)
1664 const struct got_error *err;
1665 struct got_tag_object *tag;
1666 struct got_reference *ref = NULL;
1668 *id = NULL;
1669 if (label)
1670 *label = NULL;
1672 if (refs) {
1673 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1674 refs, repo);
1675 if (err == NULL) {
1676 *id = got_object_id_dup(
1677 got_object_tag_get_object_id(tag));
1678 if (*id == NULL)
1679 err = got_error_from_errno("got_object_id_dup");
1680 else if (label && asprintf(label, "refs/tags/%s",
1681 got_object_tag_get_name(tag)) == -1) {
1682 err = got_error_from_errno("asprintf");
1683 free(*id);
1684 *id = NULL;
1686 got_object_tag_close(tag);
1687 return err;
1688 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1689 err->code != GOT_ERR_NO_OBJ)
1690 return err;
1693 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1694 if (err) {
1695 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1696 return err;
1697 err = got_ref_open(&ref, repo, id_str, 0);
1698 if (err != NULL)
1699 goto done;
1700 if (label) {
1701 *label = strdup(got_ref_get_name(ref));
1702 if (*label == NULL) {
1703 err = got_error_from_errno("strdup");
1704 goto done;
1707 err = got_ref_resolve(id, repo, ref);
1708 } else if (label) {
1709 err = got_object_id_str(label, *id);
1710 if (*label == NULL) {
1711 err = got_error_from_errno("strdup");
1712 goto done;
1715 done:
1716 if (ref)
1717 got_ref_close(ref);
1718 return err;
1721 const struct got_error *
1722 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1723 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1725 const struct got_error *err = NULL;
1726 struct got_reflist_entry *re;
1727 struct got_object_id *tag_id;
1728 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1730 *tag = NULL;
1732 TAILQ_FOREACH(re, refs, entry) {
1733 const char *refname;
1734 refname = got_ref_get_name(re->ref);
1735 if (got_ref_is_symbolic(re->ref))
1736 continue;
1737 if (strncmp(refname, "refs/tags/", 10) != 0)
1738 continue;
1739 if (!name_is_absolute)
1740 refname += strlen("refs/tags/");
1741 if (strcmp(refname, name) != 0)
1742 continue;
1743 err = got_ref_resolve(&tag_id, repo, re->ref);
1744 if (err)
1745 break;
1746 err = got_object_open_as_tag(tag, repo, tag_id);
1747 free(tag_id);
1748 if (err)
1749 break;
1750 if (obj_type == GOT_OBJ_TYPE_ANY ||
1751 got_object_tag_get_object_type(*tag) == obj_type)
1752 break;
1753 got_object_tag_close(*tag);
1754 *tag = NULL;
1757 if (err == NULL && *tag == NULL)
1758 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1759 GOT_OBJ_LABEL_TAG, name);
1760 return err;
1763 static const struct got_error *
1764 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1765 const char *name, mode_t mode, struct got_object_id *blob_id)
1767 const struct got_error *err = NULL;
1769 *new_te = NULL;
1771 *new_te = calloc(1, sizeof(**new_te));
1772 if (*new_te == NULL)
1773 return got_error_from_errno("calloc");
1775 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1776 sizeof((*new_te)->name)) {
1777 err = got_error(GOT_ERR_NO_SPACE);
1778 goto done;
1781 if (S_ISLNK(mode)) {
1782 (*new_te)->mode = S_IFLNK;
1783 } else {
1784 (*new_te)->mode = S_IFREG;
1785 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1787 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1788 done:
1789 if (err && *new_te) {
1790 free(*new_te);
1791 *new_te = NULL;
1793 return err;
1796 static const struct got_error *
1797 import_file(struct got_tree_entry **new_te, struct dirent *de,
1798 const char *path, struct got_repository *repo)
1800 const struct got_error *err;
1801 struct got_object_id *blob_id = NULL;
1802 char *filepath;
1803 struct stat sb;
1805 if (asprintf(&filepath, "%s%s%s", path,
1806 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1807 return got_error_from_errno("asprintf");
1809 if (lstat(filepath, &sb) != 0) {
1810 err = got_error_from_errno2("lstat", path);
1811 goto done;
1814 err = got_object_blob_create(&blob_id, filepath, repo);
1815 if (err)
1816 goto done;
1818 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1819 blob_id);
1820 done:
1821 free(filepath);
1822 if (err)
1823 free(blob_id);
1824 return err;
1827 static const struct got_error *
1828 insert_tree_entry(struct got_tree_entry *new_te,
1829 struct got_pathlist_head *paths)
1831 const struct got_error *err = NULL;
1832 struct got_pathlist_entry *new_pe;
1834 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1835 if (err)
1836 return err;
1837 if (new_pe == NULL)
1838 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1839 return NULL;
1842 static const struct got_error *write_tree(struct got_object_id **,
1843 const char *, struct got_pathlist_head *, struct got_repository *,
1844 got_repo_import_cb progress_cb, void *progress_arg);
1846 static const struct got_error *
1847 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1848 const char *path, struct got_pathlist_head *ignores,
1849 struct got_repository *repo,
1850 got_repo_import_cb progress_cb, void *progress_arg)
1852 const struct got_error *err;
1853 struct got_object_id *id = NULL;
1854 char *subdirpath;
1856 if (asprintf(&subdirpath, "%s%s%s", path,
1857 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1858 return got_error_from_errno("asprintf");
1860 (*new_te) = calloc(1, sizeof(**new_te));
1861 if (*new_te == NULL)
1862 return got_error_from_errno("calloc");
1863 (*new_te)->mode = S_IFDIR;
1864 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1865 sizeof((*new_te)->name)) {
1866 err = got_error(GOT_ERR_NO_SPACE);
1867 goto done;
1869 err = write_tree(&id, subdirpath, ignores, repo,
1870 progress_cb, progress_arg);
1871 if (err)
1872 goto done;
1873 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1875 done:
1876 free(id);
1877 free(subdirpath);
1878 if (err) {
1879 free(*new_te);
1880 *new_te = NULL;
1882 return err;
1885 static const struct got_error *
1886 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1887 struct got_pathlist_head *ignores, struct got_repository *repo,
1888 got_repo_import_cb progress_cb, void *progress_arg)
1890 const struct got_error *err = NULL;
1891 DIR *dir;
1892 struct dirent *de;
1893 int nentries;
1894 struct got_tree_entry *new_te = NULL;
1895 struct got_pathlist_head paths;
1896 struct got_pathlist_entry *pe;
1898 *new_tree_id = NULL;
1900 TAILQ_INIT(&paths);
1902 dir = opendir(path_dir);
1903 if (dir == NULL) {
1904 err = got_error_from_errno2("opendir", path_dir);
1905 goto done;
1908 nentries = 0;
1909 while ((de = readdir(dir)) != NULL) {
1910 int ignore = 0;
1911 int type;
1913 if (strcmp(de->d_name, ".") == 0 ||
1914 strcmp(de->d_name, "..") == 0)
1915 continue;
1917 TAILQ_FOREACH(pe, ignores, entry) {
1918 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1919 ignore = 1;
1920 break;
1923 if (ignore)
1924 continue;
1926 err = got_path_dirent_type(&type, path_dir, de);
1927 if (err)
1928 goto done;
1930 if (type == DT_DIR) {
1931 err = import_subdir(&new_te, de, path_dir,
1932 ignores, repo, progress_cb, progress_arg);
1933 if (err) {
1934 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1935 goto done;
1936 err = NULL;
1937 continue;
1939 } else if (type == DT_REG || type == DT_LNK) {
1940 err = import_file(&new_te, de, path_dir, repo);
1941 if (err)
1942 goto done;
1943 } else
1944 continue;
1946 err = insert_tree_entry(new_te, &paths);
1947 if (err)
1948 goto done;
1949 nentries++;
1952 if (TAILQ_EMPTY(&paths)) {
1953 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1954 "cannot create tree without any entries");
1955 goto done;
1958 TAILQ_FOREACH(pe, &paths, entry) {
1959 struct got_tree_entry *te = pe->data;
1960 char *path;
1961 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1962 continue;
1963 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1964 err = got_error_from_errno("asprintf");
1965 goto done;
1967 err = (*progress_cb)(progress_arg, path);
1968 free(path);
1969 if (err)
1970 goto done;
1973 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1974 done:
1975 if (dir)
1976 closedir(dir);
1977 got_pathlist_free(&paths);
1978 return err;
1981 const struct got_error *
1982 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1983 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1984 struct got_repository *repo, got_repo_import_cb progress_cb,
1985 void *progress_arg)
1987 const struct got_error *err;
1988 struct got_object_id *new_tree_id;
1990 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1991 progress_cb, progress_arg);
1992 if (err)
1993 return err;
1995 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1996 author, time(NULL), author, time(NULL), logmsg, repo);
1997 free(new_tree_id);
1998 return err;
2001 const struct got_error *
2002 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2003 struct got_repository *repo)
2005 const struct got_error *err = NULL;
2006 char *path_objects = NULL, *path = NULL;
2007 DIR *dir = NULL;
2008 struct got_object_id id;
2009 int i;
2011 *nobjects = 0;
2012 *ondisk_size = 0;
2014 path_objects = got_repo_get_path_objects(repo);
2015 if (path_objects == NULL)
2016 return got_error_from_errno("got_repo_get_path_objects");
2018 for (i = 0; i <= 0xff; i++) {
2019 struct dirent *dent;
2021 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2022 err = got_error_from_errno("asprintf");
2023 break;
2026 dir = opendir(path);
2027 if (dir == NULL) {
2028 if (errno == ENOENT) {
2029 err = NULL;
2030 continue;
2032 err = got_error_from_errno2("opendir", path);
2033 break;
2036 while ((dent = readdir(dir)) != NULL) {
2037 char *id_str;
2038 int fd;
2039 struct stat sb;
2041 if (strcmp(dent->d_name, ".") == 0 ||
2042 strcmp(dent->d_name, "..") == 0)
2043 continue;
2045 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2046 err = got_error_from_errno("asprintf");
2047 goto done;
2050 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2051 free(id_str);
2052 continue;
2054 free(id_str);
2056 err = got_object_open_loose_fd(&fd, &id, repo);
2057 if (err)
2058 goto done;
2060 if (fstat(fd, &sb) == -1) {
2061 err = got_error_from_errno("fstat");
2062 close(fd);
2063 goto done;
2065 (*nobjects)++;
2066 (*ondisk_size) += sb.st_size;
2068 if (close(fd) == -1) {
2069 err = got_error_from_errno("close");
2070 goto done;
2074 if (closedir(dir) != 0) {
2075 err = got_error_from_errno("closedir");
2076 goto done;
2078 dir = NULL;
2080 free(path);
2081 path = NULL;
2083 done:
2084 if (dir && closedir(dir) != 0 && err == NULL)
2085 err = got_error_from_errno("closedir");
2087 if (err) {
2088 *nobjects = 0;
2089 *ondisk_size = 0;
2091 free(path_objects);
2092 free(path);
2093 return err;
2096 const struct got_error *
2097 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2098 off_t *total_packsize, struct got_repository *repo)
2100 const struct got_error *err = NULL;
2101 DIR *packdir = NULL;
2102 struct dirent *dent;
2103 struct got_packidx *packidx = NULL;
2104 char *path_packidx;
2105 char *path_packfile;
2106 int packdir_fd;
2107 struct stat sb;
2109 *npackfiles = 0;
2110 *nobjects = 0;
2111 *total_packsize = 0;
2113 packdir_fd = openat(got_repo_get_fd(repo),
2114 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2115 if (packdir_fd == -1) {
2116 return got_error_from_errno_fmt("openat: %s/%s",
2117 got_repo_get_path_git_dir(repo),
2118 GOT_OBJECTS_PACK_DIR);
2121 packdir = fdopendir(packdir_fd);
2122 if (packdir == NULL) {
2123 err = got_error_from_errno("fdopendir");
2124 goto done;
2127 while ((dent = readdir(packdir)) != NULL) {
2128 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2129 continue;
2131 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2132 dent->d_name) == -1) {
2133 err = got_error_from_errno("asprintf");
2134 goto done;
2137 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2138 path_packidx, 0);
2139 free(path_packidx);
2140 if (err)
2141 goto done;
2143 if (fstat(packidx->fd, &sb) == -1)
2144 goto done;
2145 *total_packsize += sb.st_size;
2147 err = got_packidx_get_packfile_path(&path_packfile,
2148 packidx->path_packidx);
2149 if (err)
2150 goto done;
2152 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2153 0) == -1) {
2154 free(path_packfile);
2155 goto done;
2157 free(path_packfile);
2158 *total_packsize += sb.st_size;
2160 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2162 (*npackfiles)++;
2164 got_packidx_close(packidx);
2165 packidx = NULL;
2167 done:
2168 if (packidx)
2169 got_packidx_close(packidx);
2170 if (packdir && closedir(packdir) != 0 && err == NULL)
2171 err = got_error_from_errno("closedir");
2172 if (err) {
2173 *npackfiles = 0;
2174 *nobjects = 0;
2175 *total_packsize = 0;
2177 return err;
2180 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2181 got_packidx_bloom_filter_cmp);