Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <uuid.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 int
81 got_repo_get_fd(struct got_repository *repo)
82 {
83 return repo->gitdir_fd;
84 }
86 const char *
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_name;
90 }
92 const char *
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_email;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
104 const char *
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
110 const char *
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
116 void
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
124 int
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 static char *
131 get_path_git_child(struct got_repository *repo, const char *basename)
133 char *path_child;
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
136 basename) == -1)
137 return NULL;
139 return path_child;
142 char *
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 char *
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 char *
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
160 char *
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 static char *
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
172 char *
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
178 char *
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
190 void
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
198 static int
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
205 int ret = 0;
206 struct stat sb;
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
210 goto done;
211 if (!S_ISDIR(sb.st_mode))
212 goto done;
214 if (lstat(path_objects, &sb) == -1)
215 goto done;
216 if (!S_ISDIR(sb.st_mode))
217 goto done;
219 if (lstat(path_refs, &sb) == -1)
220 goto done;
221 if (!S_ISDIR(sb.st_mode))
222 goto done;
224 if (lstat(path_head, &sb) == -1)
225 goto done;
226 if (!S_ISREG(sb.st_mode))
227 goto done;
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 goto done;
232 got_ref_close(head_ref);
234 ret = 1;
235 done:
236 free(path_objects);
237 free(path_refs);
238 free(path_head);
239 return ret;
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err) {
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 err = NULL;
254 return err;
256 obj->refcnt++;
257 #endif
258 return NULL;
261 struct got_object *
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err) {
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
278 err = NULL;
279 return err;
281 tree->refcnt++;
282 #endif
283 return NULL;
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 commit->refcnt++;
308 #endif
309 return NULL;
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err) {
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
330 err = NULL;
331 return err;
333 tag->refcnt++;
334 #endif
335 return NULL;
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
352 if (err) {
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
355 err = NULL;
356 return err;
358 raw->refcnt++;
359 #endif
360 return NULL;
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
389 repo->gitdir_fd = open(repo->path_git_dir,
390 O_DIRECTORY | O_CLOEXEC);
391 if (repo->gitdir_fd == -1) {
392 err = got_error_from_errno2("open",
393 repo->path_git_dir);
394 goto done;
396 return NULL;
399 /* git repository with working tree? */
400 free(repo->path_git_dir);
401 repo->path_git_dir = NULL;
402 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
403 err = got_error_from_errno("asprintf");
404 goto done;
406 if (is_git_repo(repo)) {
407 repo->path = strdup(path);
408 if (repo->path == NULL) {
409 err = got_error_from_errno("strdup");
410 goto done;
412 repo->gitdir_fd = open(repo->path_git_dir,
413 O_DIRECTORY | O_CLOEXEC);
414 if (repo->gitdir_fd == -1) {
415 err = got_error_from_errno2("open",
416 repo->path_git_dir);
417 goto done;
419 return NULL;
422 err = got_error(GOT_ERR_NOT_GIT_REPO);
423 done:
424 if (err) {
425 free(repo->path);
426 repo->path = NULL;
427 free(repo->path_git_dir);
428 repo->path_git_dir = NULL;
429 if (repo->gitdir_fd != -1)
430 close(repo->gitdir_fd);
431 repo->gitdir_fd = -1;
434 return err;
437 static const struct got_error *
438 parse_gitconfig_file(int *gitconfig_repository_format_version,
439 char **gitconfig_author_name, char **gitconfig_author_email,
440 struct got_remote_repo **remotes, int *nremotes,
441 char **gitconfig_owner, char ***extensions, int *nextensions,
442 const char *gitconfig_path)
444 const struct got_error *err = NULL, *child_err = NULL;
445 int fd = -1;
446 int imsg_fds[2] = { -1, -1 };
447 pid_t pid;
448 struct imsgbuf *ibuf;
450 *gitconfig_repository_format_version = 0;
451 if (extensions)
452 *extensions = NULL;
453 if (nextensions)
454 *nextensions = 0;
455 *gitconfig_author_name = NULL;
456 *gitconfig_author_email = NULL;
457 if (remotes)
458 *remotes = NULL;
459 if (nremotes)
460 *nremotes = 0;
461 if (gitconfig_owner)
462 *gitconfig_owner = NULL;
464 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
465 if (fd == -1) {
466 if (errno == ENOENT)
467 return NULL;
468 return got_error_from_errno2("open", gitconfig_path);
471 ibuf = calloc(1, sizeof(*ibuf));
472 if (ibuf == NULL) {
473 err = got_error_from_errno("calloc");
474 goto done;
477 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
478 err = got_error_from_errno("socketpair");
479 goto done;
482 pid = fork();
483 if (pid == -1) {
484 err = got_error_from_errno("fork");
485 goto done;
486 } else if (pid == 0) {
487 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
488 gitconfig_path);
489 /* not reached */
492 if (close(imsg_fds[1]) == -1) {
493 err = got_error_from_errno("close");
494 goto done;
496 imsg_fds[1] = -1;
497 imsg_init(ibuf, imsg_fds[0]);
499 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
500 if (err)
501 goto done;
502 fd = -1;
504 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
505 if (err)
506 goto done;
508 err = got_privsep_recv_gitconfig_int(
509 gitconfig_repository_format_version, ibuf);
510 if (err)
511 goto done;
513 if (extensions && nextensions) {
514 err = got_privsep_send_gitconfig_repository_extensions_req(
515 ibuf);
516 if (err)
517 goto done;
518 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
519 if (err)
520 goto done;
521 if (*nextensions > 0) {
522 int i;
523 *extensions = calloc(*nextensions, sizeof(char *));
524 if (*extensions == NULL) {
525 err = got_error_from_errno("calloc");
526 goto done;
528 for (i = 0; i < *nextensions; i++) {
529 char *ext;
530 err = got_privsep_recv_gitconfig_str(&ext,
531 ibuf);
532 if (err)
533 goto done;
534 (*extensions)[i] = ext;
539 err = got_privsep_send_gitconfig_author_name_req(ibuf);
540 if (err)
541 goto done;
543 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
544 if (err)
545 goto done;
547 err = got_privsep_send_gitconfig_author_email_req(ibuf);
548 if (err)
549 goto done;
551 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
552 if (err)
553 goto done;
555 if (remotes && nremotes) {
556 err = got_privsep_send_gitconfig_remotes_req(ibuf);
557 if (err)
558 goto done;
560 err = got_privsep_recv_gitconfig_remotes(remotes,
561 nremotes, ibuf);
562 if (err)
563 goto done;
566 if (gitconfig_owner) {
567 err = got_privsep_send_gitconfig_owner_req(ibuf);
568 if (err)
569 goto done;
570 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
571 if (err)
572 goto done;
575 err = got_privsep_send_stop(imsg_fds[0]);
576 child_err = got_privsep_wait_for_child(pid);
577 if (child_err && err == NULL)
578 err = child_err;
579 done:
580 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
581 err = got_error_from_errno("close");
582 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
583 err = got_error_from_errno("close");
584 if (fd != -1 && close(fd) == -1 && err == NULL)
585 err = got_error_from_errno2("close", gitconfig_path);
586 free(ibuf);
587 return err;
590 static const struct got_error *
591 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
593 const struct got_error *err = NULL;
594 char *repo_gitconfig_path = NULL;
596 if (global_gitconfig_path) {
597 /* Read settings from ~/.gitconfig. */
598 int dummy_repo_version;
599 err = parse_gitconfig_file(&dummy_repo_version,
600 &repo->global_gitconfig_author_name,
601 &repo->global_gitconfig_author_email,
602 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
603 if (err)
604 return err;
607 /* Read repository's .git/config file. */
608 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
609 if (repo_gitconfig_path == NULL)
610 return got_error_from_errno("got_repo_get_path_gitconfig");
612 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
613 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
614 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
615 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
616 repo_gitconfig_path);
617 if (err)
618 goto done;
619 done:
620 free(repo_gitconfig_path);
621 return err;
624 static const struct got_error *
625 read_gotconfig(struct got_repository *repo)
627 const struct got_error *err = NULL;
628 char *gotconfig_path;
630 gotconfig_path = got_repo_get_path_gotconfig(repo);
631 if (gotconfig_path == NULL)
632 return got_error_from_errno("got_repo_get_path_gotconfig");
634 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
635 free(gotconfig_path);
636 return err;
639 /* Supported repository format extensions. */
640 static const char *repo_extensions[] = {
641 "noop", /* Got supports repository format version 1. */
642 "preciousObjects", /* Supported by gotadmin cleanup. */
643 "worktreeConfig", /* Got does not care about Git work trees. */
644 };
646 const struct got_error *
647 got_repo_open(struct got_repository **repop, const char *path,
648 const char *global_gitconfig_path)
650 struct got_repository *repo = NULL;
651 const struct got_error *err = NULL;
652 char *repo_path = NULL;
653 size_t i;
654 struct rlimit rl;
656 *repop = NULL;
658 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
659 return got_error_from_errno("getrlimit");
661 repo = calloc(1, sizeof(*repo));
662 if (repo == NULL) {
663 err = got_error_from_errno("calloc");
664 goto done;
667 RB_INIT(&repo->packidx_bloom_filters);
669 for (i = 0; i < nitems(repo->privsep_children); i++) {
670 memset(&repo->privsep_children[i], 0,
671 sizeof(repo->privsep_children[0]));
672 repo->privsep_children[i].imsg_fd = -1;
675 err = got_object_cache_init(&repo->objcache,
676 GOT_OBJECT_CACHE_TYPE_OBJ);
677 if (err)
678 goto done;
679 err = got_object_cache_init(&repo->treecache,
680 GOT_OBJECT_CACHE_TYPE_TREE);
681 if (err)
682 goto done;
683 err = got_object_cache_init(&repo->commitcache,
684 GOT_OBJECT_CACHE_TYPE_COMMIT);
685 if (err)
686 goto done;
687 err = got_object_cache_init(&repo->tagcache,
688 GOT_OBJECT_CACHE_TYPE_TAG);
689 if (err)
690 goto done;
691 err = got_object_cache_init(&repo->rawcache,
692 GOT_OBJECT_CACHE_TYPE_RAW);
693 if (err)
694 goto done;
696 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
697 if (repo->pack_cache_size > rl.rlim_cur / 8)
698 repo->pack_cache_size = rl.rlim_cur / 8;
700 repo_path = realpath(path, NULL);
701 if (repo_path == NULL) {
702 err = got_error_from_errno2("realpath", path);
703 goto done;
706 for (;;) {
707 char *parent_path;
709 err = open_repo(repo, repo_path);
710 if (err == NULL)
711 break;
712 if (err->code != GOT_ERR_NOT_GIT_REPO)
713 goto done;
714 if (repo_path[0] == '/' && repo_path[1] == '\0') {
715 err = got_error(GOT_ERR_NOT_GIT_REPO);
716 goto done;
718 err = got_path_dirname(&parent_path, repo_path);
719 if (err)
720 goto done;
721 free(repo_path);
722 repo_path = parent_path;
725 err = read_gotconfig(repo);
726 if (err)
727 goto done;
729 err = read_gitconfig(repo, global_gitconfig_path);
730 if (err)
731 goto done;
732 if (repo->gitconfig_repository_format_version != 0)
733 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
734 for (i = 0; i < repo->nextensions; i++) {
735 char *ext = repo->extensions[i];
736 int j, supported = 0;
737 for (j = 0; j < nitems(repo_extensions); j++) {
738 if (strcmp(ext, repo_extensions[j]) == 0) {
739 supported = 1;
740 break;
743 if (!supported) {
744 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
745 goto done;
748 done:
749 if (err)
750 got_repo_close(repo);
751 else
752 *repop = repo;
753 free(repo_path);
754 return err;
757 const struct got_error *
758 got_repo_close(struct got_repository *repo)
760 const struct got_error *err = NULL, *child_err;
761 struct got_packidx_bloom_filter *bf;
762 size_t i;
764 for (i = 0; i < repo->pack_cache_size; i++) {
765 if (repo->packidx_cache[i] == NULL)
766 break;
767 got_packidx_close(repo->packidx_cache[i]);
770 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
771 &repo->packidx_bloom_filters))) {
772 RB_REMOVE(got_packidx_bloom_filter_tree,
773 &repo->packidx_bloom_filters, bf);
774 free(bf->bloom);
775 free(bf);
778 for (i = 0; i < repo->pack_cache_size; i++) {
779 if (repo->packs[i].path_packfile == NULL)
780 break;
781 got_pack_close(&repo->packs[i]);
784 free(repo->path);
785 free(repo->path_git_dir);
787 got_object_cache_close(&repo->objcache);
788 got_object_cache_close(&repo->treecache);
789 got_object_cache_close(&repo->commitcache);
790 got_object_cache_close(&repo->tagcache);
791 got_object_cache_close(&repo->rawcache);
793 for (i = 0; i < nitems(repo->privsep_children); i++) {
794 if (repo->privsep_children[i].imsg_fd == -1)
795 continue;
796 imsg_clear(repo->privsep_children[i].ibuf);
797 free(repo->privsep_children[i].ibuf);
798 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
799 child_err = got_privsep_wait_for_child(
800 repo->privsep_children[i].pid);
801 if (child_err && err == NULL)
802 err = child_err;
803 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
804 err == NULL)
805 err = got_error_from_errno("close");
808 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
809 err == NULL)
810 err = got_error_from_errno("close");
812 if (repo->gotconfig)
813 got_gotconfig_free(repo->gotconfig);
814 free(repo->gitconfig_author_name);
815 free(repo->gitconfig_author_email);
816 for (i = 0; i < repo->ngitconfig_remotes; i++)
817 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
818 free(repo->gitconfig_remotes);
819 for (i = 0; i < repo->nextensions; i++)
820 free(repo->extensions[i]);
821 free(repo->extensions);
822 free(repo);
824 return err;
827 void
828 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
830 int i;
832 free(repo->name);
833 repo->name = NULL;
834 free(repo->fetch_url);
835 repo->fetch_url = NULL;
836 free(repo->send_url);
837 repo->send_url = NULL;
838 for (i = 0; i < repo->nfetch_branches; i++)
839 free(repo->fetch_branches[i]);
840 free(repo->fetch_branches);
841 repo->fetch_branches = NULL;
842 repo->nfetch_branches = 0;
843 for (i = 0; i < repo->nsend_branches; i++)
844 free(repo->send_branches[i]);
845 free(repo->send_branches);
846 repo->send_branches = NULL;
847 repo->nsend_branches = 0;
850 const struct got_error *
851 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
852 const char *input_path)
854 const struct got_error *err = NULL;
855 const char *repo_abspath = NULL;
856 size_t repolen, len;
857 char *canonpath, *path = NULL;
859 *in_repo_path = NULL;
861 canonpath = strdup(input_path);
862 if (canonpath == NULL) {
863 err = got_error_from_errno("strdup");
864 goto done;
866 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
867 if (err)
868 goto done;
870 repo_abspath = got_repo_get_path(repo);
872 if (canonpath[0] == '\0') {
873 path = strdup(canonpath);
874 if (path == NULL) {
875 err = got_error_from_errno("strdup");
876 goto done;
878 } else {
879 path = realpath(canonpath, NULL);
880 if (path == NULL) {
881 if (errno != ENOENT) {
882 err = got_error_from_errno2("realpath",
883 canonpath);
884 goto done;
886 /*
887 * Path is not on disk.
888 * Assume it is already relative to repository root.
889 */
890 path = strdup(canonpath);
891 if (path == NULL) {
892 err = got_error_from_errno("strdup");
893 goto done;
897 repolen = strlen(repo_abspath);
898 len = strlen(path);
901 if (strcmp(path, repo_abspath) == 0) {
902 free(path);
903 path = strdup("");
904 if (path == NULL) {
905 err = got_error_from_errno("strdup");
906 goto done;
908 } else if (len > repolen &&
909 got_path_is_child(path, repo_abspath, repolen)) {
910 /* Matched an on-disk path inside repository. */
911 if (got_repo_is_bare(repo)) {
912 /*
913 * Matched an on-disk path inside repository
914 * database. Treat input as repository-relative.
915 */
916 free(path);
917 path = canonpath;
918 canonpath = NULL;
919 } else {
920 char *child;
921 /* Strip common prefix with repository path. */
922 err = got_path_skip_common_ancestor(&child,
923 repo_abspath, path);
924 if (err)
925 goto done;
926 free(path);
927 path = child;
929 } else {
930 /*
931 * Matched unrelated on-disk path.
932 * Treat input as repository-relative.
933 */
934 free(path);
935 path = canonpath;
936 canonpath = NULL;
940 /* Make in-repository path absolute */
941 if (path[0] != '/') {
942 char *abspath;
943 if (asprintf(&abspath, "/%s", path) == -1) {
944 err = got_error_from_errno("asprintf");
945 goto done;
947 free(path);
948 path = abspath;
951 done:
952 free(canonpath);
953 if (err)
954 free(path);
955 else
956 *in_repo_path = path;
957 return err;
960 static const struct got_error *
961 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
962 const char *path_packidx)
964 const struct got_error *err = NULL;
965 size_t i;
967 for (i = 0; i < repo->pack_cache_size; i++) {
968 if (repo->packidx_cache[i] == NULL)
969 break;
970 if (strcmp(repo->packidx_cache[i]->path_packidx,
971 path_packidx) == 0) {
972 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
975 if (i == repo->pack_cache_size) {
976 i = repo->pack_cache_size - 1;
977 err = got_packidx_close(repo->packidx_cache[i]);
978 if (err)
979 return err;
982 repo->packidx_cache[i] = packidx;
984 return NULL;
987 int
988 got_repo_is_packidx_filename(const char *name, size_t len)
990 if (len != GOT_PACKIDX_NAMELEN)
991 return 0;
993 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
994 return 0;
996 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
997 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
998 return 0;
1000 return 1;
1003 static struct got_packidx_bloom_filter *
1004 get_packidx_bloom_filter(struct got_repository *repo,
1005 const char *path, size_t path_len)
1007 struct got_packidx_bloom_filter key;
1009 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1010 return NULL; /* XXX */
1011 key.path_len = path_len;
1013 return RB_FIND(got_packidx_bloom_filter_tree,
1014 &repo->packidx_bloom_filters, &key);
1017 int
1018 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1019 const char *path_packidx, struct got_object_id *id)
1021 struct got_packidx_bloom_filter *bf;
1023 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1024 if (bf)
1025 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1027 /* No bloom filter means this pack index must be searched. */
1028 return 1;
1031 static const struct got_error *
1032 add_packidx_bloom_filter(struct got_repository *repo,
1033 struct got_packidx *packidx, const char *path_packidx)
1035 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1036 struct got_packidx_bloom_filter *bf;
1037 size_t len;
1040 * Don't use bloom filters for very large pack index files.
1041 * Large pack files will contain a relatively large fraction
1042 * of our objects so we will likely need to visit them anyway.
1043 * The more objects a pack file contains the higher the probability
1044 * of a false-positive match from the bloom filter. And reading
1045 * all object IDs from a large pack index file can be expensive.
1047 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1048 return NULL;
1050 /* Do we already have a filter for this pack index? */
1051 if (get_packidx_bloom_filter(repo, path_packidx,
1052 strlen(path_packidx)) != NULL)
1053 return NULL;
1055 bf = calloc(1, sizeof(*bf));
1056 if (bf == NULL)
1057 return got_error_from_errno("calloc");
1058 bf->bloom = calloc(1, sizeof(*bf->bloom));
1059 if (bf->bloom == NULL) {
1060 free(bf);
1061 return got_error_from_errno("calloc");
1064 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1065 if (len >= sizeof(bf->path)) {
1066 free(bf->bloom);
1067 free(bf);
1068 return got_error(GOT_ERR_NO_SPACE);
1070 bf->path_len = len;
1072 /* Minimum size supported by our bloom filter is 1000 entries. */
1073 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1074 for (i = 0; i < nobjects; i++) {
1075 struct got_packidx_object_id *id;
1076 id = &packidx->hdr.sorted_ids[i];
1077 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1080 RB_INSERT(got_packidx_bloom_filter_tree,
1081 &repo->packidx_bloom_filters, bf);
1082 return NULL;
1085 const struct got_error *
1086 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1087 struct got_repository *repo, struct got_object_id *id)
1089 const struct got_error *err;
1090 DIR *packdir = NULL;
1091 struct dirent *dent;
1092 char *path_packidx;
1093 size_t i;
1094 int packdir_fd;
1096 /* Search pack index cache. */
1097 for (i = 0; i < repo->pack_cache_size; i++) {
1098 if (repo->packidx_cache[i] == NULL)
1099 break;
1100 if (!got_repo_check_packidx_bloom_filter(repo,
1101 repo->packidx_cache[i]->path_packidx, id))
1102 continue; /* object will not be found in this index */
1103 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1104 if (*idx != -1) {
1105 *packidx = repo->packidx_cache[i];
1107 * Move this cache entry to the front. Repeatedly
1108 * searching a wrong pack index can be expensive.
1110 if (i > 0) {
1111 memmove(&repo->packidx_cache[1],
1112 &repo->packidx_cache[0],
1113 i * sizeof(repo->packidx_cache[0]));
1114 repo->packidx_cache[0] = *packidx;
1116 return NULL;
1119 /* No luck. Search the filesystem. */
1121 packdir_fd = openat(got_repo_get_fd(repo),
1122 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1123 if (packdir_fd == -1) {
1124 if (errno == ENOENT)
1125 err = got_error_no_obj(id);
1126 else
1127 err = got_error_from_errno_fmt("openat: %s/%s",
1128 got_repo_get_path_git_dir(repo),
1129 GOT_OBJECTS_PACK_DIR);
1130 goto done;
1133 packdir = fdopendir(packdir_fd);
1134 if (packdir == NULL) {
1135 err = got_error_from_errno("fdopendir");
1136 goto done;
1139 while ((dent = readdir(packdir)) != NULL) {
1140 int is_cached = 0;
1142 if (!got_repo_is_packidx_filename(dent->d_name,
1143 strlen(dent->d_name)))
1144 continue;
1146 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1147 dent->d_name) == -1) {
1148 err = got_error_from_errno("asprintf");
1149 goto done;
1152 if (!got_repo_check_packidx_bloom_filter(repo,
1153 path_packidx, id)) {
1154 free(path_packidx);
1155 continue; /* object will not be found in this index */
1158 for (i = 0; i < repo->pack_cache_size; i++) {
1159 if (repo->packidx_cache[i] == NULL)
1160 break;
1161 if (strcmp(repo->packidx_cache[i]->path_packidx,
1162 path_packidx) == 0) {
1163 is_cached = 1;
1164 break;
1167 if (is_cached) {
1168 free(path_packidx);
1169 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1173 path_packidx, 0);
1174 if (err) {
1175 free(path_packidx);
1176 goto done;
1179 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1180 if (err) {
1181 free(path_packidx);
1182 goto done;
1185 err = cache_packidx(repo, *packidx, path_packidx);
1186 free(path_packidx);
1187 if (err)
1188 goto done;
1190 *idx = got_packidx_get_object_idx(*packidx, id);
1191 if (*idx != -1) {
1192 err = NULL; /* found the object */
1193 goto done;
1197 err = got_error_no_obj(id);
1198 done:
1199 if (packdir && closedir(packdir) != 0 && err == NULL)
1200 err = got_error_from_errno("closedir");
1201 return err;
1204 const struct got_error *
1205 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 DIR *packdir = NULL;
1210 struct dirent *dent;
1211 char *path_packidx = NULL;
1212 int packdir_fd;
1214 packdir_fd = openat(got_repo_get_fd(repo),
1215 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1216 if (packdir_fd == -1) {
1217 return got_error_from_errno_fmt("openat: %s/%s",
1218 got_repo_get_path_git_dir(repo),
1219 GOT_OBJECTS_PACK_DIR);
1222 packdir = fdopendir(packdir_fd);
1223 if (packdir == NULL) {
1224 err = got_error_from_errno("fdopendir");
1225 goto done;
1228 while ((dent = readdir(packdir)) != NULL) {
1229 if (!got_repo_is_packidx_filename(dent->d_name,
1230 strlen(dent->d_name)))
1231 continue;
1233 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1234 dent->d_name) == -1) {
1235 err = got_error_from_errno("asprintf");
1236 path_packidx = NULL;
1237 break;
1240 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1241 if (err)
1242 break;
1244 done:
1245 if (err)
1246 free(path_packidx);
1247 if (packdir && closedir(packdir) != 0 && err == NULL)
1248 err = got_error_from_errno("closedir");
1249 return err;
1252 const struct got_error *
1253 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1254 struct got_repository *repo)
1256 const struct got_error *err;
1257 size_t i;
1259 *packidx = NULL;
1261 /* Search pack index cache. */
1262 for (i = 0; i < repo->pack_cache_size; i++) {
1263 if (repo->packidx_cache[i] == NULL)
1264 break;
1265 if (strcmp(repo->packidx_cache[i]->path_packidx,
1266 path_packidx) == 0) {
1267 *packidx = repo->packidx_cache[i];
1268 return NULL;
1271 /* No luck. Search the filesystem. */
1273 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1274 path_packidx, 0);
1275 if (err)
1276 return err;
1278 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1279 if (err)
1280 goto done;
1282 err = cache_packidx(repo, *packidx, path_packidx);
1283 done:
1284 if (err) {
1285 got_packidx_close(*packidx);
1286 *packidx = NULL;
1288 return err;
1291 static const struct got_error *
1292 read_packfile_hdr(int fd, struct got_packidx *packidx)
1294 const struct got_error *err = NULL;
1295 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1296 struct got_packfile_hdr hdr;
1297 ssize_t n;
1299 n = read(fd, &hdr, sizeof(hdr));
1300 if (n < 0)
1301 return got_error_from_errno("read");
1302 if (n != sizeof(hdr))
1303 return got_error(GOT_ERR_BAD_PACKFILE);
1305 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1306 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1307 be32toh(hdr.nobjects) != totobj)
1308 err = got_error(GOT_ERR_BAD_PACKFILE);
1310 return err;
1313 static const struct got_error *
1314 open_packfile(int *fd, struct got_repository *repo,
1315 const char *relpath, struct got_packidx *packidx)
1317 const struct got_error *err = NULL;
1319 *fd = openat(got_repo_get_fd(repo), relpath,
1320 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1321 if (*fd == -1)
1322 return got_error_from_errno_fmt("openat: %s/%s",
1323 got_repo_get_path_git_dir(repo), relpath);
1325 if (packidx) {
1326 err = read_packfile_hdr(*fd, packidx);
1327 if (err) {
1328 close(*fd);
1329 *fd = -1;
1333 return err;
1336 const struct got_error *
1337 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1338 const char *path_packfile, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1341 struct got_pack *pack = NULL;
1342 struct stat sb;
1343 size_t i;
1345 if (packp)
1346 *packp = NULL;
1348 for (i = 0; i < repo->pack_cache_size; i++) {
1349 pack = &repo->packs[i];
1350 if (pack->path_packfile == NULL)
1351 break;
1352 if (strcmp(pack->path_packfile, path_packfile) == 0)
1353 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1356 if (i == repo->pack_cache_size) {
1357 err = got_pack_close(&repo->packs[i - 1]);
1358 if (err)
1359 return err;
1360 memmove(&repo->packs[1], &repo->packs[0],
1361 sizeof(repo->packs) - sizeof(repo->packs[0]));
1362 i = 0;
1365 pack = &repo->packs[i];
1367 pack->path_packfile = strdup(path_packfile);
1368 if (pack->path_packfile == NULL) {
1369 err = got_error_from_errno("strdup");
1370 goto done;
1373 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1374 if (err)
1375 goto done;
1377 if (fstat(pack->fd, &sb) != 0) {
1378 err = got_error_from_errno("fstat");
1379 goto done;
1381 pack->filesize = sb.st_size;
1383 pack->privsep_child = NULL;
1385 #ifndef GOT_PACK_NO_MMAP
1386 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1387 pack->fd, 0);
1388 if (pack->map == MAP_FAILED) {
1389 if (errno != ENOMEM) {
1390 err = got_error_from_errno("mmap");
1391 goto done;
1393 pack->map = NULL; /* fall back to read(2) */
1395 #endif
1396 done:
1397 if (err) {
1398 if (pack) {
1399 free(pack->path_packfile);
1400 memset(pack, 0, sizeof(*pack));
1402 } else if (packp)
1403 *packp = pack;
1404 return err;
1407 struct got_pack *
1408 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1410 struct got_pack *pack = NULL;
1411 size_t i;
1413 for (i = 0; i < repo->pack_cache_size; i++) {
1414 pack = &repo->packs[i];
1415 if (pack->path_packfile == NULL)
1416 break;
1417 if (strcmp(pack->path_packfile, path_packfile) == 0)
1418 return pack;
1421 return NULL;
1424 const struct got_error *
1425 got_repo_init(const char *repo_path)
1427 const struct got_error *err = NULL;
1428 const char *dirnames[] = {
1429 GOT_OBJECTS_DIR,
1430 GOT_OBJECTS_PACK_DIR,
1431 GOT_REFS_DIR,
1433 const char *description_str = "Unnamed repository; "
1434 "edit this file 'description' to name the repository.";
1435 const char *headref_str = "ref: refs/heads/main";
1436 const char *gitconfig_str = "[core]\n"
1437 "\trepositoryformatversion = 0\n"
1438 "\tfilemode = true\n"
1439 "\tbare = true\n";
1440 char *path;
1441 size_t i;
1443 if (!got_path_dir_is_empty(repo_path))
1444 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1446 for (i = 0; i < nitems(dirnames); i++) {
1447 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1448 return got_error_from_errno("asprintf");
1450 err = got_path_mkdir(path);
1451 free(path);
1452 if (err)
1453 return err;
1456 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1457 return got_error_from_errno("asprintf");
1458 err = got_path_create_file(path, description_str);
1459 free(path);
1460 if (err)
1461 return err;
1463 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1464 return got_error_from_errno("asprintf");
1465 err = got_path_create_file(path, headref_str);
1466 free(path);
1467 if (err)
1468 return err;
1470 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1471 return got_error_from_errno("asprintf");
1472 err = got_path_create_file(path, gitconfig_str);
1473 free(path);
1474 if (err)
1475 return err;
1477 return NULL;
1480 static const struct got_error *
1481 match_packed_object(struct got_object_id **unique_id,
1482 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1484 const struct got_error *err = NULL;
1485 DIR *packdir = NULL;
1486 struct dirent *dent;
1487 char *path_packidx;
1488 struct got_object_id_queue matched_ids;
1489 int packdir_fd;
1491 STAILQ_INIT(&matched_ids);
1493 packdir_fd = openat(got_repo_get_fd(repo),
1494 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1495 if (packdir_fd == -1) {
1496 if (errno != ENOENT) {
1497 err = got_error_from_errno2("openat",
1498 GOT_OBJECTS_PACK_DIR);
1500 goto done;
1503 packdir = fdopendir(packdir_fd);
1504 if (packdir == NULL) {
1505 err = got_error_from_errno("fdopendir");
1506 goto done;
1509 while ((dent = readdir(packdir)) != NULL) {
1510 struct got_packidx *packidx;
1511 struct got_object_qid *qid;
1513 if (!got_repo_is_packidx_filename(dent->d_name,
1514 strlen(dent->d_name)))
1515 continue;
1517 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1518 dent->d_name) == -1) {
1519 err = got_error_from_errno("strdup");
1520 break;
1523 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1524 path_packidx, 0);
1525 free(path_packidx);
1526 if (err)
1527 break;
1529 err = got_packidx_match_id_str_prefix(&matched_ids,
1530 packidx, id_str_prefix);
1531 if (err) {
1532 got_packidx_close(packidx);
1533 break;
1535 err = got_packidx_close(packidx);
1536 if (err)
1537 break;
1539 STAILQ_FOREACH(qid, &matched_ids, entry) {
1540 if (obj_type != GOT_OBJ_TYPE_ANY) {
1541 int matched_type;
1542 err = got_object_get_type(&matched_type, repo,
1543 qid->id);
1544 if (err)
1545 goto done;
1546 if (matched_type != obj_type)
1547 continue;
1549 if (*unique_id == NULL) {
1550 *unique_id = got_object_id_dup(qid->id);
1551 if (*unique_id == NULL) {
1552 err = got_error_from_errno("malloc");
1553 goto done;
1555 } else {
1556 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1557 continue; /* packed multiple times */
1558 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1559 goto done;
1563 done:
1564 got_object_id_queue_free(&matched_ids);
1565 if (packdir && closedir(packdir) != 0 && err == NULL)
1566 err = got_error_from_errno("closedir");
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 for (i = 0; i < strlen(id_str_prefix); i++) {
1673 if (isxdigit((unsigned char)id_str_prefix[i]))
1674 continue;
1675 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1678 len = strlen(id_str_prefix);
1679 if (len >= 2) {
1680 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1681 if (err)
1682 goto done;
1683 object_dir = strndup(id_str_prefix, 2);
1684 if (object_dir == NULL) {
1685 err = got_error_from_errno("strdup");
1686 goto done;
1688 err = match_loose_object(id, path_objects, object_dir,
1689 id_str_prefix, obj_type, repo);
1690 } else if (len == 1) {
1691 int i;
1692 for (i = 0; i < 0xf; i++) {
1693 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1694 == -1) {
1695 err = got_error_from_errno("asprintf");
1696 goto done;
1698 err = match_packed_object(id, repo, object_dir,
1699 obj_type);
1700 if (err)
1701 goto done;
1702 err = match_loose_object(id, path_objects, object_dir,
1703 id_str_prefix, obj_type, repo);
1704 if (err)
1705 goto done;
1707 } else {
1708 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1709 goto done;
1711 done:
1712 free(object_dir);
1713 if (err) {
1714 free(*id);
1715 *id = NULL;
1716 } else if (*id == NULL) {
1717 switch (obj_type) {
1718 case GOT_OBJ_TYPE_BLOB:
1719 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1720 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1721 break;
1722 case GOT_OBJ_TYPE_TREE:
1723 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1724 GOT_OBJ_LABEL_TREE, id_str_prefix);
1725 break;
1726 case GOT_OBJ_TYPE_COMMIT:
1727 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1728 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1729 break;
1730 case GOT_OBJ_TYPE_TAG:
1731 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1732 GOT_OBJ_LABEL_TAG, id_str_prefix);
1733 break;
1734 default:
1735 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1736 break;
1740 return err;
1743 const struct got_error *
1744 got_repo_match_object_id(struct got_object_id **id, char **label,
1745 const char *id_str, int obj_type, struct got_reflist_head *refs,
1746 struct got_repository *repo)
1748 const struct got_error *err;
1749 struct got_tag_object *tag;
1750 struct got_reference *ref = NULL;
1752 *id = NULL;
1753 if (label)
1754 *label = NULL;
1756 if (refs) {
1757 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1758 refs, repo);
1759 if (err == NULL) {
1760 *id = got_object_id_dup(
1761 got_object_tag_get_object_id(tag));
1762 if (*id == NULL)
1763 err = got_error_from_errno("got_object_id_dup");
1764 else if (label && asprintf(label, "refs/tags/%s",
1765 got_object_tag_get_name(tag)) == -1) {
1766 err = got_error_from_errno("asprintf");
1767 free(*id);
1768 *id = NULL;
1770 got_object_tag_close(tag);
1771 return err;
1772 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1773 err->code != GOT_ERR_NO_OBJ)
1774 return err;
1777 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1778 if (err) {
1779 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1780 return err;
1781 err = got_ref_open(&ref, repo, id_str, 0);
1782 if (err != NULL)
1783 goto done;
1784 if (label) {
1785 *label = strdup(got_ref_get_name(ref));
1786 if (*label == NULL) {
1787 err = got_error_from_errno("strdup");
1788 goto done;
1791 err = got_ref_resolve(id, repo, ref);
1792 } else if (label) {
1793 err = got_object_id_str(label, *id);
1794 if (*label == NULL) {
1795 err = got_error_from_errno("strdup");
1796 goto done;
1799 done:
1800 if (ref)
1801 got_ref_close(ref);
1802 return err;
1805 const struct got_error *
1806 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1807 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1809 const struct got_error *err = NULL;
1810 struct got_reflist_entry *re;
1811 struct got_object_id *tag_id;
1812 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1814 *tag = NULL;
1816 TAILQ_FOREACH(re, refs, entry) {
1817 const char *refname;
1818 refname = got_ref_get_name(re->ref);
1819 if (got_ref_is_symbolic(re->ref))
1820 continue;
1821 if (strncmp(refname, "refs/tags/", 10) != 0)
1822 continue;
1823 if (!name_is_absolute)
1824 refname += strlen("refs/tags/");
1825 if (strcmp(refname, name) != 0)
1826 continue;
1827 err = got_ref_resolve(&tag_id, repo, re->ref);
1828 if (err)
1829 break;
1830 err = got_object_open_as_tag(tag, repo, tag_id);
1831 free(tag_id);
1832 if (err)
1833 break;
1834 if (obj_type == GOT_OBJ_TYPE_ANY ||
1835 got_object_tag_get_object_type(*tag) == obj_type)
1836 break;
1837 got_object_tag_close(*tag);
1838 *tag = NULL;
1841 if (err == NULL && *tag == NULL)
1842 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1843 GOT_OBJ_LABEL_TAG, name);
1844 return err;
1847 static const struct got_error *
1848 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1849 const char *name, mode_t mode, struct got_object_id *blob_id)
1851 const struct got_error *err = NULL;
1853 *new_te = NULL;
1855 *new_te = calloc(1, sizeof(**new_te));
1856 if (*new_te == NULL)
1857 return got_error_from_errno("calloc");
1859 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1860 sizeof((*new_te)->name)) {
1861 err = got_error(GOT_ERR_NO_SPACE);
1862 goto done;
1865 if (S_ISLNK(mode)) {
1866 (*new_te)->mode = S_IFLNK;
1867 } else {
1868 (*new_te)->mode = S_IFREG;
1869 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1871 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1872 done:
1873 if (err && *new_te) {
1874 free(*new_te);
1875 *new_te = NULL;
1877 return err;
1880 static const struct got_error *
1881 import_file(struct got_tree_entry **new_te, struct dirent *de,
1882 const char *path, struct got_repository *repo)
1884 const struct got_error *err;
1885 struct got_object_id *blob_id = NULL;
1886 char *filepath;
1887 struct stat sb;
1889 if (asprintf(&filepath, "%s%s%s", path,
1890 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1891 return got_error_from_errno("asprintf");
1893 if (lstat(filepath, &sb) != 0) {
1894 err = got_error_from_errno2("lstat", path);
1895 goto done;
1898 err = got_object_blob_create(&blob_id, filepath, repo);
1899 if (err)
1900 goto done;
1902 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1903 blob_id);
1904 done:
1905 free(filepath);
1906 if (err)
1907 free(blob_id);
1908 return err;
1911 static const struct got_error *
1912 insert_tree_entry(struct got_tree_entry *new_te,
1913 struct got_pathlist_head *paths)
1915 const struct got_error *err = NULL;
1916 struct got_pathlist_entry *new_pe;
1918 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1919 if (err)
1920 return err;
1921 if (new_pe == NULL)
1922 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1923 return NULL;
1926 static const struct got_error *write_tree(struct got_object_id **,
1927 const char *, struct got_pathlist_head *, struct got_repository *,
1928 got_repo_import_cb progress_cb, void *progress_arg);
1930 static const struct got_error *
1931 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1932 const char *path, struct got_pathlist_head *ignores,
1933 struct got_repository *repo,
1934 got_repo_import_cb progress_cb, void *progress_arg)
1936 const struct got_error *err;
1937 struct got_object_id *id = NULL;
1938 char *subdirpath;
1940 if (asprintf(&subdirpath, "%s%s%s", path,
1941 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1942 return got_error_from_errno("asprintf");
1944 (*new_te) = calloc(1, sizeof(**new_te));
1945 if (*new_te == NULL)
1946 return got_error_from_errno("calloc");
1947 (*new_te)->mode = S_IFDIR;
1948 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1949 sizeof((*new_te)->name)) {
1950 err = got_error(GOT_ERR_NO_SPACE);
1951 goto done;
1953 err = write_tree(&id, subdirpath, ignores, repo,
1954 progress_cb, progress_arg);
1955 if (err)
1956 goto done;
1957 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1959 done:
1960 free(id);
1961 free(subdirpath);
1962 if (err) {
1963 free(*new_te);
1964 *new_te = NULL;
1966 return err;
1969 static const struct got_error *
1970 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1971 struct got_pathlist_head *ignores, struct got_repository *repo,
1972 got_repo_import_cb progress_cb, void *progress_arg)
1974 const struct got_error *err = NULL;
1975 DIR *dir;
1976 struct dirent *de;
1977 int nentries;
1978 struct got_tree_entry *new_te = NULL;
1979 struct got_pathlist_head paths;
1980 struct got_pathlist_entry *pe;
1982 *new_tree_id = NULL;
1984 TAILQ_INIT(&paths);
1986 dir = opendir(path_dir);
1987 if (dir == NULL) {
1988 err = got_error_from_errno2("opendir", path_dir);
1989 goto done;
1992 nentries = 0;
1993 while ((de = readdir(dir)) != NULL) {
1994 int ignore = 0;
1995 int type;
1997 if (strcmp(de->d_name, ".") == 0 ||
1998 strcmp(de->d_name, "..") == 0)
1999 continue;
2001 TAILQ_FOREACH(pe, ignores, entry) {
2002 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2003 ignore = 1;
2004 break;
2007 if (ignore)
2008 continue;
2010 err = got_path_dirent_type(&type, path_dir, de);
2011 if (err)
2012 goto done;
2014 if (type == DT_DIR) {
2015 err = import_subdir(&new_te, de, path_dir,
2016 ignores, repo, progress_cb, progress_arg);
2017 if (err) {
2018 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2019 goto done;
2020 err = NULL;
2021 continue;
2023 } else if (type == DT_REG || type == DT_LNK) {
2024 err = import_file(&new_te, de, path_dir, repo);
2025 if (err)
2026 goto done;
2027 } else
2028 continue;
2030 err = insert_tree_entry(new_te, &paths);
2031 if (err)
2032 goto done;
2033 nentries++;
2036 if (TAILQ_EMPTY(&paths)) {
2037 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2038 "cannot create tree without any entries");
2039 goto done;
2042 TAILQ_FOREACH(pe, &paths, entry) {
2043 struct got_tree_entry *te = pe->data;
2044 char *path;
2045 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2046 continue;
2047 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2048 err = got_error_from_errno("asprintf");
2049 goto done;
2051 err = (*progress_cb)(progress_arg, path);
2052 free(path);
2053 if (err)
2054 goto done;
2057 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2058 done:
2059 if (dir)
2060 closedir(dir);
2061 got_pathlist_free(&paths);
2062 return err;
2065 const struct got_error *
2066 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2067 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2068 struct got_repository *repo, got_repo_import_cb progress_cb,
2069 void *progress_arg)
2071 const struct got_error *err;
2072 struct got_object_id *new_tree_id;
2074 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2075 progress_cb, progress_arg);
2076 if (err)
2077 return err;
2079 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2080 author, time(NULL), author, time(NULL), logmsg, repo);
2081 free(new_tree_id);
2082 return err;
2085 const struct got_error *
2086 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2087 struct got_repository *repo)
2089 const struct got_error *err = NULL;
2090 char *path_objects = NULL, *path = NULL;
2091 DIR *dir = NULL;
2092 struct got_object_id id;
2093 int i;
2095 *nobjects = 0;
2096 *ondisk_size = 0;
2098 path_objects = got_repo_get_path_objects(repo);
2099 if (path_objects == NULL)
2100 return got_error_from_errno("got_repo_get_path_objects");
2102 for (i = 0; i <= 0xff; i++) {
2103 struct dirent *dent;
2105 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2106 err = got_error_from_errno("asprintf");
2107 break;
2110 dir = opendir(path);
2111 if (dir == NULL) {
2112 if (errno == ENOENT) {
2113 err = NULL;
2114 continue;
2116 err = got_error_from_errno2("opendir", path);
2117 break;
2120 while ((dent = readdir(dir)) != NULL) {
2121 char *id_str;
2122 int fd;
2123 struct stat sb;
2125 if (strcmp(dent->d_name, ".") == 0 ||
2126 strcmp(dent->d_name, "..") == 0)
2127 continue;
2129 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2130 err = got_error_from_errno("asprintf");
2131 goto done;
2134 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2135 free(id_str);
2136 continue;
2138 free(id_str);
2140 err = got_object_open_loose_fd(&fd, &id, repo);
2141 if (err)
2142 goto done;
2144 if (fstat(fd, &sb) == -1) {
2145 err = got_error_from_errno("fstat");
2146 close(fd);
2147 goto done;
2149 (*nobjects)++;
2150 (*ondisk_size) += sb.st_size;
2152 if (close(fd) == -1) {
2153 err = got_error_from_errno("close");
2154 goto done;
2158 if (closedir(dir) != 0) {
2159 err = got_error_from_errno("closedir");
2160 goto done;
2162 dir = NULL;
2164 free(path);
2165 path = NULL;
2167 done:
2168 if (dir && closedir(dir) != 0 && err == NULL)
2169 err = got_error_from_errno("closedir");
2171 if (err) {
2172 *nobjects = 0;
2173 *ondisk_size = 0;
2175 free(path_objects);
2176 free(path);
2177 return err;
2180 const struct got_error *
2181 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2182 off_t *total_packsize, struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 DIR *packdir = NULL;
2186 struct dirent *dent;
2187 struct got_packidx *packidx = NULL;
2188 char *path_packidx;
2189 char *path_packfile;
2190 int packdir_fd;
2191 struct stat sb;
2193 *npackfiles = 0;
2194 *nobjects = 0;
2195 *total_packsize = 0;
2197 packdir_fd = openat(got_repo_get_fd(repo),
2198 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2199 if (packdir_fd == -1) {
2200 return got_error_from_errno_fmt("openat: %s/%s",
2201 got_repo_get_path_git_dir(repo),
2202 GOT_OBJECTS_PACK_DIR);
2205 packdir = fdopendir(packdir_fd);
2206 if (packdir == NULL) {
2207 err = got_error_from_errno("fdopendir");
2208 goto done;
2211 while ((dent = readdir(packdir)) != NULL) {
2212 if (!got_repo_is_packidx_filename(dent->d_name,
2213 strlen(dent->d_name)))
2214 continue;
2216 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2217 dent->d_name) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 goto done;
2222 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2223 path_packidx, 0);
2224 free(path_packidx);
2225 if (err)
2226 goto done;
2228 if (fstat(packidx->fd, &sb) == -1)
2229 goto done;
2230 *total_packsize += sb.st_size;
2232 err = got_packidx_get_packfile_path(&path_packfile,
2233 packidx->path_packidx);
2234 if (err)
2235 goto done;
2237 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2238 0) == -1) {
2239 free(path_packfile);
2240 goto done;
2242 free(path_packfile);
2243 *total_packsize += sb.st_size;
2245 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2247 (*npackfiles)++;
2249 got_packidx_close(packidx);
2250 packidx = NULL;
2252 done:
2253 if (packidx)
2254 got_packidx_close(packidx);
2255 if (packdir && closedir(packdir) != 0 && err == NULL)
2256 err = got_error_from_errno("closedir");
2257 if (err) {
2258 *npackfiles = 0;
2259 *nobjects = 0;
2260 *total_packsize = 0;
2262 return err;
2265 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2266 got_packidx_bloom_filter_cmp);