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>
39 #include "bloom.h"
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_object.h"
47 #include "got_opentemp.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 *const 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 return got_error_from_errno("calloc");
665 RB_INIT(&repo->packidx_bloom_filters);
666 TAILQ_INIT(&repo->packidx_paths);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
676 if (err)
677 goto done;
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
680 if (err)
681 goto done;
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
684 if (err)
685 goto done;
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
688 if (err)
689 goto done;
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
692 if (err)
693 goto done;
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
698 for (i = 0; i < nitems(repo->packs); i++) {
699 if (i < repo->pack_cache_size) {
700 repo->packs[i].basefd = got_opentempfd();
701 if (repo->packs[i].basefd == -1)
702 return got_error_from_errno("got_opentempfd");
703 repo->packs[i].accumfd = got_opentempfd();
704 if (repo->packs[i].accumfd == -1)
705 return got_error_from_errno("got_opentempfd");
706 } else {
707 repo->packs[i].basefd = -1;
708 repo->packs[i].accumfd = -1;
712 repo_path = realpath(path, NULL);
713 if (repo_path == NULL) {
714 err = got_error_from_errno2("realpath", path);
715 goto done;
718 for (;;) {
719 char *parent_path;
721 err = open_repo(repo, repo_path);
722 if (err == NULL)
723 break;
724 if (err->code != GOT_ERR_NOT_GIT_REPO)
725 goto done;
726 if (repo_path[0] == '/' && repo_path[1] == '\0') {
727 err = got_error(GOT_ERR_NOT_GIT_REPO);
728 goto done;
730 err = got_path_dirname(&parent_path, repo_path);
731 if (err)
732 goto done;
733 free(repo_path);
734 repo_path = parent_path;
737 err = read_gotconfig(repo);
738 if (err)
739 goto done;
741 err = read_gitconfig(repo, global_gitconfig_path);
742 if (err)
743 goto done;
744 if (repo->gitconfig_repository_format_version != 0)
745 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
746 for (i = 0; i < repo->nextensions; i++) {
747 char *ext = repo->extensions[i];
748 int j, supported = 0;
749 for (j = 0; j < nitems(repo_extensions); j++) {
750 if (strcmp(ext, repo_extensions[j]) == 0) {
751 supported = 1;
752 break;
755 if (!supported) {
756 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
757 goto done;
761 err = got_repo_list_packidx(&repo->packidx_paths, repo);
762 done:
763 if (err)
764 got_repo_close(repo);
765 else
766 *repop = repo;
767 free(repo_path);
768 return err;
771 const struct got_error *
772 got_repo_close(struct got_repository *repo)
774 const struct got_error *err = NULL, *child_err;
775 struct got_packidx_bloom_filter *bf;
776 struct got_pathlist_entry *pe;
777 size_t i;
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packidx_cache[i] == NULL)
781 break;
782 got_packidx_close(repo->packidx_cache[i]);
785 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
786 &repo->packidx_bloom_filters))) {
787 RB_REMOVE(got_packidx_bloom_filter_tree,
788 &repo->packidx_bloom_filters, bf);
789 free(bf->bloom);
790 free(bf);
793 for (i = 0; i < repo->pack_cache_size; i++) {
794 if (repo->packs[i].path_packfile)
795 got_pack_close(&repo->packs[i]);
796 if (repo->packs[i].basefd != -1) {
797 if (close(repo->packs[i].basefd) == -1 && err == NULL)
798 err = got_error_from_errno("close");
799 repo->packs[i].basefd = -1;
801 if (repo->packs[i].accumfd != -1) {
802 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
803 err = got_error_from_errno("close");
804 repo->packs[i].accumfd = -1;
808 free(repo->path);
809 free(repo->path_git_dir);
811 got_object_cache_close(&repo->objcache);
812 got_object_cache_close(&repo->treecache);
813 got_object_cache_close(&repo->commitcache);
814 got_object_cache_close(&repo->tagcache);
815 got_object_cache_close(&repo->rawcache);
817 for (i = 0; i < nitems(repo->privsep_children); i++) {
818 if (repo->privsep_children[i].imsg_fd == -1)
819 continue;
820 imsg_clear(repo->privsep_children[i].ibuf);
821 free(repo->privsep_children[i].ibuf);
822 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
823 child_err = got_privsep_wait_for_child(
824 repo->privsep_children[i].pid);
825 if (child_err && err == NULL)
826 err = child_err;
827 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
828 err == NULL)
829 err = got_error_from_errno("close");
832 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
833 err == NULL)
834 err = got_error_from_errno("close");
836 if (repo->gotconfig)
837 got_gotconfig_free(repo->gotconfig);
838 free(repo->gitconfig_author_name);
839 free(repo->gitconfig_author_email);
840 for (i = 0; i < repo->ngitconfig_remotes; i++)
841 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
842 free(repo->gitconfig_remotes);
843 for (i = 0; i < repo->nextensions; i++)
844 free(repo->extensions[i]);
845 free(repo->extensions);
847 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
848 free((void *)pe->path);
849 got_pathlist_free(&repo->packidx_paths);
850 free(repo);
852 return err;
855 void
856 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
858 int i;
860 free(repo->name);
861 repo->name = NULL;
862 free(repo->fetch_url);
863 repo->fetch_url = NULL;
864 free(repo->send_url);
865 repo->send_url = NULL;
866 for (i = 0; i < repo->nfetch_branches; i++)
867 free(repo->fetch_branches[i]);
868 free(repo->fetch_branches);
869 repo->fetch_branches = NULL;
870 repo->nfetch_branches = 0;
871 for (i = 0; i < repo->nsend_branches; i++)
872 free(repo->send_branches[i]);
873 free(repo->send_branches);
874 repo->send_branches = NULL;
875 repo->nsend_branches = 0;
878 const struct got_error *
879 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
880 const char *input_path)
882 const struct got_error *err = NULL;
883 const char *repo_abspath = NULL;
884 size_t repolen, len;
885 char *canonpath, *path = NULL;
887 *in_repo_path = NULL;
889 canonpath = strdup(input_path);
890 if (canonpath == NULL) {
891 err = got_error_from_errno("strdup");
892 goto done;
894 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
895 if (err)
896 goto done;
898 repo_abspath = got_repo_get_path(repo);
900 if (canonpath[0] == '\0') {
901 path = strdup(canonpath);
902 if (path == NULL) {
903 err = got_error_from_errno("strdup");
904 goto done;
906 } else {
907 path = realpath(canonpath, NULL);
908 if (path == NULL) {
909 if (errno != ENOENT) {
910 err = got_error_from_errno2("realpath",
911 canonpath);
912 goto done;
914 /*
915 * Path is not on disk.
916 * Assume it is already relative to repository root.
917 */
918 path = strdup(canonpath);
919 if (path == NULL) {
920 err = got_error_from_errno("strdup");
921 goto done;
925 repolen = strlen(repo_abspath);
926 len = strlen(path);
929 if (strcmp(path, repo_abspath) == 0) {
930 free(path);
931 path = strdup("");
932 if (path == NULL) {
933 err = got_error_from_errno("strdup");
934 goto done;
936 } else if (len > repolen &&
937 got_path_is_child(path, repo_abspath, repolen)) {
938 /* Matched an on-disk path inside repository. */
939 if (got_repo_is_bare(repo)) {
940 /*
941 * Matched an on-disk path inside repository
942 * database. Treat input as repository-relative.
943 */
944 free(path);
945 path = canonpath;
946 canonpath = NULL;
947 } else {
948 char *child;
949 /* Strip common prefix with repository path. */
950 err = got_path_skip_common_ancestor(&child,
951 repo_abspath, path);
952 if (err)
953 goto done;
954 free(path);
955 path = child;
957 } else {
958 /*
959 * Matched unrelated on-disk path.
960 * Treat input as repository-relative.
961 */
962 free(path);
963 path = canonpath;
964 canonpath = NULL;
968 /* Make in-repository path absolute */
969 if (path[0] != '/') {
970 char *abspath;
971 if (asprintf(&abspath, "/%s", path) == -1) {
972 err = got_error_from_errno("asprintf");
973 goto done;
975 free(path);
976 path = abspath;
979 done:
980 free(canonpath);
981 if (err)
982 free(path);
983 else
984 *in_repo_path = path;
985 return err;
988 static const struct got_error *
989 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
990 const char *path_packidx)
992 const struct got_error *err = NULL;
993 size_t i;
995 for (i = 0; i < repo->pack_cache_size; i++) {
996 if (repo->packidx_cache[i] == NULL)
997 break;
998 if (strcmp(repo->packidx_cache[i]->path_packidx,
999 path_packidx) == 0) {
1000 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1003 if (i == repo->pack_cache_size) {
1004 i = repo->pack_cache_size - 1;
1005 err = got_packidx_close(repo->packidx_cache[i]);
1006 if (err)
1007 return err;
1010 repo->packidx_cache[i] = packidx;
1012 return NULL;
1015 int
1016 got_repo_is_packidx_filename(const char *name, size_t len)
1018 if (len != GOT_PACKIDX_NAMELEN)
1019 return 0;
1021 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1022 return 0;
1024 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1025 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1026 return 0;
1028 return 1;
1031 static struct got_packidx_bloom_filter *
1032 get_packidx_bloom_filter(struct got_repository *repo,
1033 const char *path, size_t path_len)
1035 struct got_packidx_bloom_filter key;
1037 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1038 return NULL; /* XXX */
1039 key.path_len = path_len;
1041 return RB_FIND(got_packidx_bloom_filter_tree,
1042 &repo->packidx_bloom_filters, &key);
1045 int
1046 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1047 const char *path_packidx, struct got_object_id *id)
1049 struct got_packidx_bloom_filter *bf;
1051 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1052 if (bf)
1053 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1055 /* No bloom filter means this pack index must be searched. */
1056 return 1;
1059 static const struct got_error *
1060 add_packidx_bloom_filter(struct got_repository *repo,
1061 struct got_packidx *packidx, const char *path_packidx)
1063 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1064 struct got_packidx_bloom_filter *bf;
1065 size_t len;
1068 * Don't use bloom filters for very large pack index files.
1069 * Large pack files will contain a relatively large fraction
1070 * of our objects so we will likely need to visit them anyway.
1071 * The more objects a pack file contains the higher the probability
1072 * of a false-positive match from the bloom filter. And reading
1073 * all object IDs from a large pack index file can be expensive.
1075 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1076 return NULL;
1078 /* Do we already have a filter for this pack index? */
1079 if (get_packidx_bloom_filter(repo, path_packidx,
1080 strlen(path_packidx)) != NULL)
1081 return NULL;
1083 bf = calloc(1, sizeof(*bf));
1084 if (bf == NULL)
1085 return got_error_from_errno("calloc");
1086 bf->bloom = calloc(1, sizeof(*bf->bloom));
1087 if (bf->bloom == NULL) {
1088 free(bf);
1089 return got_error_from_errno("calloc");
1092 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1093 if (len >= sizeof(bf->path)) {
1094 free(bf->bloom);
1095 free(bf);
1096 return got_error(GOT_ERR_NO_SPACE);
1098 bf->path_len = len;
1100 /* Minimum size supported by our bloom filter is 1000 entries. */
1101 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1102 for (i = 0; i < nobjects; i++) {
1103 struct got_packidx_object_id *id;
1104 id = &packidx->hdr.sorted_ids[i];
1105 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1108 RB_INSERT(got_packidx_bloom_filter_tree,
1109 &repo->packidx_bloom_filters, bf);
1110 return NULL;
1113 const struct got_error *
1114 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1115 struct got_repository *repo, struct got_object_id *id)
1117 const struct got_error *err;
1118 struct got_pathlist_entry *pe;
1119 size_t i;
1121 /* Search pack index cache. */
1122 for (i = 0; i < repo->pack_cache_size; i++) {
1123 if (repo->packidx_cache[i] == NULL)
1124 break;
1125 if (!got_repo_check_packidx_bloom_filter(repo,
1126 repo->packidx_cache[i]->path_packidx, id))
1127 continue; /* object will not be found in this index */
1128 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1129 if (*idx != -1) {
1130 *packidx = repo->packidx_cache[i];
1132 * Move this cache entry to the front. Repeatedly
1133 * searching a wrong pack index can be expensive.
1135 if (i > 0) {
1136 memmove(&repo->packidx_cache[1],
1137 &repo->packidx_cache[0],
1138 i * sizeof(repo->packidx_cache[0]));
1139 repo->packidx_cache[0] = *packidx;
1141 return NULL;
1144 /* No luck. Search the filesystem. */
1146 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1147 const char *path_packidx = pe->path;
1148 int is_cached = 0;
1150 if (!got_repo_check_packidx_bloom_filter(repo,
1151 pe->path, id))
1152 continue; /* object will not be found in this index */
1154 for (i = 0; i < repo->pack_cache_size; i++) {
1155 if (repo->packidx_cache[i] == NULL)
1156 break;
1157 if (strcmp(repo->packidx_cache[i]->path_packidx,
1158 path_packidx) == 0) {
1159 is_cached = 1;
1160 break;
1163 if (is_cached)
1164 continue; /* already searched */
1166 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1167 path_packidx, 0);
1168 if (err)
1169 goto done;
1171 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1172 if (err)
1173 goto done;
1175 err = cache_packidx(repo, *packidx, path_packidx);
1176 if (err)
1177 goto done;
1179 *idx = got_packidx_get_object_idx(*packidx, id);
1180 if (*idx != -1) {
1181 err = NULL; /* found the object */
1182 goto done;
1186 err = got_error_no_obj(id);
1187 done:
1188 return err;
1191 const struct got_error *
1192 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1193 struct got_repository *repo)
1195 const struct got_error *err = NULL;
1196 DIR *packdir = NULL;
1197 struct dirent *dent;
1198 char *path_packidx = NULL;
1199 int packdir_fd;
1201 packdir_fd = openat(got_repo_get_fd(repo),
1202 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1203 if (packdir_fd == -1) {
1204 return got_error_from_errno_fmt("openat: %s/%s",
1205 got_repo_get_path_git_dir(repo),
1206 GOT_OBJECTS_PACK_DIR);
1209 packdir = fdopendir(packdir_fd);
1210 if (packdir == NULL) {
1211 err = got_error_from_errno("fdopendir");
1212 goto done;
1215 while ((dent = readdir(packdir)) != NULL) {
1216 if (!got_repo_is_packidx_filename(dent->d_name,
1217 strlen(dent->d_name)))
1218 continue;
1220 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1221 dent->d_name) == -1) {
1222 err = got_error_from_errno("asprintf");
1223 path_packidx = NULL;
1224 break;
1227 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1228 if (err)
1229 break;
1231 done:
1232 if (err)
1233 free(path_packidx);
1234 if (packdir && closedir(packdir) != 0 && err == NULL)
1235 err = got_error_from_errno("closedir");
1236 return err;
1239 const struct got_error *
1240 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1241 struct got_repository *repo)
1243 const struct got_error *err;
1244 size_t i;
1246 *packidx = NULL;
1248 /* Search pack index cache. */
1249 for (i = 0; i < repo->pack_cache_size; i++) {
1250 if (repo->packidx_cache[i] == NULL)
1251 break;
1252 if (strcmp(repo->packidx_cache[i]->path_packidx,
1253 path_packidx) == 0) {
1254 *packidx = repo->packidx_cache[i];
1255 return NULL;
1258 /* No luck. Search the filesystem. */
1260 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1261 path_packidx, 0);
1262 if (err)
1263 return err;
1265 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1266 if (err)
1267 goto done;
1269 err = cache_packidx(repo, *packidx, path_packidx);
1270 done:
1271 if (err) {
1272 got_packidx_close(*packidx);
1273 *packidx = NULL;
1275 return err;
1278 static const struct got_error *
1279 read_packfile_hdr(int fd, struct got_packidx *packidx)
1281 const struct got_error *err = NULL;
1282 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1283 struct got_packfile_hdr hdr;
1284 ssize_t n;
1286 n = read(fd, &hdr, sizeof(hdr));
1287 if (n < 0)
1288 return got_error_from_errno("read");
1289 if (n != sizeof(hdr))
1290 return got_error(GOT_ERR_BAD_PACKFILE);
1292 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1293 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1294 be32toh(hdr.nobjects) != totobj)
1295 err = got_error(GOT_ERR_BAD_PACKFILE);
1297 return err;
1300 static const struct got_error *
1301 open_packfile(int *fd, struct got_repository *repo,
1302 const char *relpath, struct got_packidx *packidx)
1304 const struct got_error *err = NULL;
1306 *fd = openat(got_repo_get_fd(repo), relpath,
1307 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1308 if (*fd == -1)
1309 return got_error_from_errno_fmt("openat: %s/%s",
1310 got_repo_get_path_git_dir(repo), relpath);
1312 if (packidx) {
1313 err = read_packfile_hdr(*fd, packidx);
1314 if (err) {
1315 close(*fd);
1316 *fd = -1;
1320 return err;
1323 const struct got_error *
1324 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1325 const char *path_packfile, struct got_packidx *packidx)
1327 const struct got_error *err = NULL;
1328 struct got_pack *pack = NULL;
1329 struct stat sb;
1330 size_t i;
1332 if (packp)
1333 *packp = NULL;
1335 for (i = 0; i < repo->pack_cache_size; i++) {
1336 pack = &repo->packs[i];
1337 if (pack->path_packfile == NULL)
1338 break;
1339 if (strcmp(pack->path_packfile, path_packfile) == 0)
1340 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1343 if (i == repo->pack_cache_size) {
1344 struct got_pack tmp;
1345 err = got_pack_close(&repo->packs[i - 1]);
1346 if (err)
1347 return err;
1348 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1349 return got_error_from_errno("ftruncate");
1350 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1351 return got_error_from_errno("ftruncate");
1352 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1353 memcpy(&repo->packs[i - 1], &repo->packs[0],
1354 sizeof(repo->packs[i - 1]));
1355 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1356 i = 0;
1359 pack = &repo->packs[i];
1361 pack->path_packfile = strdup(path_packfile);
1362 if (pack->path_packfile == NULL) {
1363 err = got_error_from_errno("strdup");
1364 goto done;
1367 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1368 if (err)
1369 goto done;
1371 if (fstat(pack->fd, &sb) != 0) {
1372 err = got_error_from_errno("fstat");
1373 goto done;
1375 pack->filesize = sb.st_size;
1377 pack->privsep_child = NULL;
1379 #ifndef GOT_PACK_NO_MMAP
1380 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1381 pack->fd, 0);
1382 if (pack->map == MAP_FAILED) {
1383 if (errno != ENOMEM) {
1384 err = got_error_from_errno("mmap");
1385 goto done;
1387 pack->map = NULL; /* fall back to read(2) */
1389 #endif
1390 done:
1391 if (err) {
1392 if (pack) {
1393 free(pack->path_packfile);
1394 memset(pack, 0, sizeof(*pack));
1396 } else if (packp)
1397 *packp = pack;
1398 return err;
1401 struct got_pack *
1402 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1404 struct got_pack *pack = NULL;
1405 size_t i;
1407 for (i = 0; i < repo->pack_cache_size; i++) {
1408 pack = &repo->packs[i];
1409 if (pack->path_packfile == NULL)
1410 break;
1411 if (strcmp(pack->path_packfile, path_packfile) == 0)
1412 return pack;
1415 return NULL;
1418 const struct got_error *
1419 got_repo_init(const char *repo_path)
1421 const struct got_error *err = NULL;
1422 const char *dirnames[] = {
1423 GOT_OBJECTS_DIR,
1424 GOT_OBJECTS_PACK_DIR,
1425 GOT_REFS_DIR,
1427 const char *description_str = "Unnamed repository; "
1428 "edit this file 'description' to name the repository.";
1429 const char *headref_str = "ref: refs/heads/main";
1430 const char *gitconfig_str = "[core]\n"
1431 "\trepositoryformatversion = 0\n"
1432 "\tfilemode = true\n"
1433 "\tbare = true\n";
1434 char *path;
1435 size_t i;
1437 if (!got_path_dir_is_empty(repo_path))
1438 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1440 for (i = 0; i < nitems(dirnames); i++) {
1441 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1442 return got_error_from_errno("asprintf");
1444 err = got_path_mkdir(path);
1445 free(path);
1446 if (err)
1447 return err;
1450 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1451 return got_error_from_errno("asprintf");
1452 err = got_path_create_file(path, description_str);
1453 free(path);
1454 if (err)
1455 return err;
1457 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1458 return got_error_from_errno("asprintf");
1459 err = got_path_create_file(path, headref_str);
1460 free(path);
1461 if (err)
1462 return err;
1464 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1465 return got_error_from_errno("asprintf");
1466 err = got_path_create_file(path, gitconfig_str);
1467 free(path);
1468 if (err)
1469 return err;
1471 return NULL;
1474 static const struct got_error *
1475 match_packed_object(struct got_object_id **unique_id,
1476 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1478 const struct got_error *err = NULL;
1479 struct got_object_id_queue matched_ids;
1480 struct got_pathlist_entry *pe;
1482 STAILQ_INIT(&matched_ids);
1484 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1485 const char *path_packidx = pe->path;
1486 struct got_packidx *packidx;
1487 struct got_object_qid *qid;
1489 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1490 path_packidx, 0);
1491 if (err)
1492 break;
1494 err = got_packidx_match_id_str_prefix(&matched_ids,
1495 packidx, id_str_prefix);
1496 if (err) {
1497 got_packidx_close(packidx);
1498 break;
1500 err = got_packidx_close(packidx);
1501 if (err)
1502 break;
1504 STAILQ_FOREACH(qid, &matched_ids, entry) {
1505 if (obj_type != GOT_OBJ_TYPE_ANY) {
1506 int matched_type;
1507 err = got_object_get_type(&matched_type, repo,
1508 &qid->id);
1509 if (err)
1510 goto done;
1511 if (matched_type != obj_type)
1512 continue;
1514 if (*unique_id == NULL) {
1515 *unique_id = got_object_id_dup(&qid->id);
1516 if (*unique_id == NULL) {
1517 err = got_error_from_errno("malloc");
1518 goto done;
1520 } else {
1521 if (got_object_id_cmp(*unique_id,
1522 &qid->id) == 0)
1523 continue; /* packed multiple times */
1524 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1525 goto done;
1529 done:
1530 got_object_id_queue_free(&matched_ids);
1531 if (err) {
1532 free(*unique_id);
1533 *unique_id = NULL;
1535 return err;
1538 static const struct got_error *
1539 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1540 const char *object_dir, const char *id_str_prefix, int obj_type,
1541 struct got_repository *repo)
1543 const struct got_error *err = NULL;
1544 char *path;
1545 DIR *dir = NULL;
1546 struct dirent *dent;
1547 struct got_object_id id;
1549 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1550 err = got_error_from_errno("asprintf");
1551 goto done;
1554 dir = opendir(path);
1555 if (dir == NULL) {
1556 if (errno == ENOENT) {
1557 err = NULL;
1558 goto done;
1560 err = got_error_from_errno2("opendir", path);
1561 goto done;
1563 while ((dent = readdir(dir)) != NULL) {
1564 char *id_str;
1565 int cmp;
1567 if (strcmp(dent->d_name, ".") == 0 ||
1568 strcmp(dent->d_name, "..") == 0)
1569 continue;
1571 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1572 err = got_error_from_errno("asprintf");
1573 goto done;
1576 if (!got_parse_sha1_digest(id.sha1, id_str))
1577 continue;
1580 * Directory entries do not necessarily appear in
1581 * sorted order, so we must iterate over all of them.
1583 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1584 if (cmp != 0) {
1585 free(id_str);
1586 continue;
1589 if (*unique_id == NULL) {
1590 if (obj_type != GOT_OBJ_TYPE_ANY) {
1591 int matched_type;
1592 err = got_object_get_type(&matched_type, repo,
1593 &id);
1594 if (err)
1595 goto done;
1596 if (matched_type != obj_type)
1597 continue;
1599 *unique_id = got_object_id_dup(&id);
1600 if (*unique_id == NULL) {
1601 err = got_error_from_errno("got_object_id_dup");
1602 free(id_str);
1603 goto done;
1605 } else {
1606 if (got_object_id_cmp(*unique_id, &id) == 0)
1607 continue; /* both packed and loose */
1608 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1609 free(id_str);
1610 goto done;
1613 done:
1614 if (dir && closedir(dir) != 0 && err == NULL)
1615 err = got_error_from_errno("closedir");
1616 if (err) {
1617 free(*unique_id);
1618 *unique_id = NULL;
1620 free(path);
1621 return err;
1624 const struct got_error *
1625 got_repo_match_object_id_prefix(struct got_object_id **id,
1626 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 char *path_objects = got_repo_get_path_objects(repo);
1630 char *object_dir = NULL;
1631 size_t len;
1632 int i;
1634 *id = NULL;
1636 len = strlen(id_str_prefix);
1637 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1638 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1640 for (i = 0; i < len; i++) {
1641 if (isxdigit((unsigned char)id_str_prefix[i]))
1642 continue;
1643 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1646 if (len >= 2) {
1647 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1648 if (err)
1649 goto done;
1650 object_dir = strndup(id_str_prefix, 2);
1651 if (object_dir == NULL) {
1652 err = got_error_from_errno("strdup");
1653 goto done;
1655 err = match_loose_object(id, path_objects, object_dir,
1656 id_str_prefix, obj_type, repo);
1657 } else if (len == 1) {
1658 int i;
1659 for (i = 0; i < 0xf; i++) {
1660 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1661 == -1) {
1662 err = got_error_from_errno("asprintf");
1663 goto done;
1665 err = match_packed_object(id, repo, object_dir,
1666 obj_type);
1667 if (err)
1668 goto done;
1669 err = match_loose_object(id, path_objects, object_dir,
1670 id_str_prefix, obj_type, repo);
1671 if (err)
1672 goto done;
1674 } else {
1675 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1676 goto done;
1678 done:
1679 free(object_dir);
1680 if (err) {
1681 free(*id);
1682 *id = NULL;
1683 } else if (*id == NULL) {
1684 switch (obj_type) {
1685 case GOT_OBJ_TYPE_BLOB:
1686 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1687 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1688 break;
1689 case GOT_OBJ_TYPE_TREE:
1690 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1691 GOT_OBJ_LABEL_TREE, id_str_prefix);
1692 break;
1693 case GOT_OBJ_TYPE_COMMIT:
1694 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1695 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1696 break;
1697 case GOT_OBJ_TYPE_TAG:
1698 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1699 GOT_OBJ_LABEL_TAG, id_str_prefix);
1700 break;
1701 default:
1702 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1703 break;
1707 return err;
1710 const struct got_error *
1711 got_repo_match_object_id(struct got_object_id **id, char **label,
1712 const char *id_str, int obj_type, struct got_reflist_head *refs,
1713 struct got_repository *repo)
1715 const struct got_error *err;
1716 struct got_tag_object *tag;
1717 struct got_reference *ref = NULL;
1719 *id = NULL;
1720 if (label)
1721 *label = NULL;
1723 if (refs) {
1724 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1725 refs, repo);
1726 if (err == NULL) {
1727 *id = got_object_id_dup(
1728 got_object_tag_get_object_id(tag));
1729 if (*id == NULL)
1730 err = got_error_from_errno("got_object_id_dup");
1731 else if (label && asprintf(label, "refs/tags/%s",
1732 got_object_tag_get_name(tag)) == -1) {
1733 err = got_error_from_errno("asprintf");
1734 free(*id);
1735 *id = NULL;
1737 got_object_tag_close(tag);
1738 return err;
1739 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1740 err->code != GOT_ERR_NO_OBJ)
1741 return err;
1744 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1745 if (err) {
1746 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1747 return err;
1748 err = got_ref_open(&ref, repo, id_str, 0);
1749 if (err != NULL)
1750 goto done;
1751 if (label) {
1752 *label = strdup(got_ref_get_name(ref));
1753 if (*label == NULL) {
1754 err = got_error_from_errno("strdup");
1755 goto done;
1758 err = got_ref_resolve(id, repo, ref);
1759 } else if (label) {
1760 err = got_object_id_str(label, *id);
1761 if (*label == NULL) {
1762 err = got_error_from_errno("strdup");
1763 goto done;
1766 done:
1767 if (ref)
1768 got_ref_close(ref);
1769 return err;
1772 const struct got_error *
1773 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1774 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1776 const struct got_error *err = NULL;
1777 struct got_reflist_entry *re;
1778 struct got_object_id *tag_id;
1779 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1781 *tag = NULL;
1783 TAILQ_FOREACH(re, refs, entry) {
1784 const char *refname;
1785 refname = got_ref_get_name(re->ref);
1786 if (got_ref_is_symbolic(re->ref))
1787 continue;
1788 if (strncmp(refname, "refs/tags/", 10) != 0)
1789 continue;
1790 if (!name_is_absolute)
1791 refname += strlen("refs/tags/");
1792 if (strcmp(refname, name) != 0)
1793 continue;
1794 err = got_ref_resolve(&tag_id, repo, re->ref);
1795 if (err)
1796 break;
1797 err = got_object_open_as_tag(tag, repo, tag_id);
1798 free(tag_id);
1799 if (err)
1800 break;
1801 if (obj_type == GOT_OBJ_TYPE_ANY ||
1802 got_object_tag_get_object_type(*tag) == obj_type)
1803 break;
1804 got_object_tag_close(*tag);
1805 *tag = NULL;
1808 if (err == NULL && *tag == NULL)
1809 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1810 GOT_OBJ_LABEL_TAG, name);
1811 return err;
1814 static const struct got_error *
1815 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1816 const char *name, mode_t mode, struct got_object_id *blob_id)
1818 const struct got_error *err = NULL;
1820 *new_te = NULL;
1822 *new_te = calloc(1, sizeof(**new_te));
1823 if (*new_te == NULL)
1824 return got_error_from_errno("calloc");
1826 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1827 sizeof((*new_te)->name)) {
1828 err = got_error(GOT_ERR_NO_SPACE);
1829 goto done;
1832 if (S_ISLNK(mode)) {
1833 (*new_te)->mode = S_IFLNK;
1834 } else {
1835 (*new_te)->mode = S_IFREG;
1836 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1838 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1839 done:
1840 if (err && *new_te) {
1841 free(*new_te);
1842 *new_te = NULL;
1844 return err;
1847 static const struct got_error *
1848 import_file(struct got_tree_entry **new_te, struct dirent *de,
1849 const char *path, struct got_repository *repo)
1851 const struct got_error *err;
1852 struct got_object_id *blob_id = NULL;
1853 char *filepath;
1854 struct stat sb;
1856 if (asprintf(&filepath, "%s%s%s", path,
1857 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1858 return got_error_from_errno("asprintf");
1860 if (lstat(filepath, &sb) != 0) {
1861 err = got_error_from_errno2("lstat", path);
1862 goto done;
1865 err = got_object_blob_create(&blob_id, filepath, repo);
1866 if (err)
1867 goto done;
1869 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1870 blob_id);
1871 done:
1872 free(filepath);
1873 if (err)
1874 free(blob_id);
1875 return err;
1878 static const struct got_error *
1879 insert_tree_entry(struct got_tree_entry *new_te,
1880 struct got_pathlist_head *paths)
1882 const struct got_error *err = NULL;
1883 struct got_pathlist_entry *new_pe;
1885 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1886 if (err)
1887 return err;
1888 if (new_pe == NULL)
1889 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1890 return NULL;
1893 static const struct got_error *write_tree(struct got_object_id **,
1894 const char *, struct got_pathlist_head *, struct got_repository *,
1895 got_repo_import_cb progress_cb, void *progress_arg);
1897 static const struct got_error *
1898 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1899 const char *path, struct got_pathlist_head *ignores,
1900 struct got_repository *repo,
1901 got_repo_import_cb progress_cb, void *progress_arg)
1903 const struct got_error *err;
1904 struct got_object_id *id = NULL;
1905 char *subdirpath;
1907 if (asprintf(&subdirpath, "%s%s%s", path,
1908 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1909 return got_error_from_errno("asprintf");
1911 (*new_te) = calloc(1, sizeof(**new_te));
1912 if (*new_te == NULL)
1913 return got_error_from_errno("calloc");
1914 (*new_te)->mode = S_IFDIR;
1915 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1916 sizeof((*new_te)->name)) {
1917 err = got_error(GOT_ERR_NO_SPACE);
1918 goto done;
1920 err = write_tree(&id, subdirpath, ignores, repo,
1921 progress_cb, progress_arg);
1922 if (err)
1923 goto done;
1924 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1926 done:
1927 free(id);
1928 free(subdirpath);
1929 if (err) {
1930 free(*new_te);
1931 *new_te = NULL;
1933 return err;
1936 static const struct got_error *
1937 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1938 struct got_pathlist_head *ignores, struct got_repository *repo,
1939 got_repo_import_cb progress_cb, void *progress_arg)
1941 const struct got_error *err = NULL;
1942 DIR *dir;
1943 struct dirent *de;
1944 int nentries;
1945 struct got_tree_entry *new_te = NULL;
1946 struct got_pathlist_head paths;
1947 struct got_pathlist_entry *pe;
1949 *new_tree_id = NULL;
1951 TAILQ_INIT(&paths);
1953 dir = opendir(path_dir);
1954 if (dir == NULL) {
1955 err = got_error_from_errno2("opendir", path_dir);
1956 goto done;
1959 nentries = 0;
1960 while ((de = readdir(dir)) != NULL) {
1961 int ignore = 0;
1962 int type;
1964 if (strcmp(de->d_name, ".") == 0 ||
1965 strcmp(de->d_name, "..") == 0)
1966 continue;
1968 TAILQ_FOREACH(pe, ignores, entry) {
1969 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1970 ignore = 1;
1971 break;
1974 if (ignore)
1975 continue;
1977 err = got_path_dirent_type(&type, path_dir, de);
1978 if (err)
1979 goto done;
1981 if (type == DT_DIR) {
1982 err = import_subdir(&new_te, de, path_dir,
1983 ignores, repo, progress_cb, progress_arg);
1984 if (err) {
1985 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1986 goto done;
1987 err = NULL;
1988 continue;
1990 } else if (type == DT_REG || type == DT_LNK) {
1991 err = import_file(&new_te, de, path_dir, repo);
1992 if (err)
1993 goto done;
1994 } else
1995 continue;
1997 err = insert_tree_entry(new_te, &paths);
1998 if (err)
1999 goto done;
2000 nentries++;
2003 if (TAILQ_EMPTY(&paths)) {
2004 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2005 "cannot create tree without any entries");
2006 goto done;
2009 TAILQ_FOREACH(pe, &paths, entry) {
2010 struct got_tree_entry *te = pe->data;
2011 char *path;
2012 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2013 continue;
2014 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2015 err = got_error_from_errno("asprintf");
2016 goto done;
2018 err = (*progress_cb)(progress_arg, path);
2019 free(path);
2020 if (err)
2021 goto done;
2024 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2025 done:
2026 if (dir)
2027 closedir(dir);
2028 got_pathlist_free(&paths);
2029 return err;
2032 const struct got_error *
2033 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2034 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2035 struct got_repository *repo, got_repo_import_cb progress_cb,
2036 void *progress_arg)
2038 const struct got_error *err;
2039 struct got_object_id *new_tree_id;
2041 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2042 progress_cb, progress_arg);
2043 if (err)
2044 return err;
2046 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2047 author, time(NULL), author, time(NULL), logmsg, repo);
2048 free(new_tree_id);
2049 return err;
2052 const struct got_error *
2053 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2054 struct got_repository *repo)
2056 const struct got_error *err = NULL;
2057 char *path_objects = NULL, *path = NULL;
2058 DIR *dir = NULL;
2059 struct got_object_id id;
2060 int i;
2062 *nobjects = 0;
2063 *ondisk_size = 0;
2065 path_objects = got_repo_get_path_objects(repo);
2066 if (path_objects == NULL)
2067 return got_error_from_errno("got_repo_get_path_objects");
2069 for (i = 0; i <= 0xff; i++) {
2070 struct dirent *dent;
2072 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2073 err = got_error_from_errno("asprintf");
2074 break;
2077 dir = opendir(path);
2078 if (dir == NULL) {
2079 if (errno == ENOENT) {
2080 err = NULL;
2081 continue;
2083 err = got_error_from_errno2("opendir", path);
2084 break;
2087 while ((dent = readdir(dir)) != NULL) {
2088 char *id_str;
2089 int fd;
2090 struct stat sb;
2092 if (strcmp(dent->d_name, ".") == 0 ||
2093 strcmp(dent->d_name, "..") == 0)
2094 continue;
2096 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2097 err = got_error_from_errno("asprintf");
2098 goto done;
2101 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2102 free(id_str);
2103 continue;
2105 free(id_str);
2107 err = got_object_open_loose_fd(&fd, &id, repo);
2108 if (err)
2109 goto done;
2111 if (fstat(fd, &sb) == -1) {
2112 err = got_error_from_errno("fstat");
2113 close(fd);
2114 goto done;
2116 (*nobjects)++;
2117 (*ondisk_size) += sb.st_size;
2119 if (close(fd) == -1) {
2120 err = got_error_from_errno("close");
2121 goto done;
2125 if (closedir(dir) != 0) {
2126 err = got_error_from_errno("closedir");
2127 goto done;
2129 dir = NULL;
2131 free(path);
2132 path = NULL;
2134 done:
2135 if (dir && closedir(dir) != 0 && err == NULL)
2136 err = got_error_from_errno("closedir");
2138 if (err) {
2139 *nobjects = 0;
2140 *ondisk_size = 0;
2142 free(path_objects);
2143 free(path);
2144 return err;
2147 const struct got_error *
2148 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2149 off_t *total_packsize, struct got_repository *repo)
2151 const struct got_error *err = NULL;
2152 DIR *packdir = NULL;
2153 struct dirent *dent;
2154 struct got_packidx *packidx = NULL;
2155 char *path_packidx;
2156 char *path_packfile;
2157 int packdir_fd;
2158 struct stat sb;
2160 *npackfiles = 0;
2161 *nobjects = 0;
2162 *total_packsize = 0;
2164 packdir_fd = openat(got_repo_get_fd(repo),
2165 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2166 if (packdir_fd == -1) {
2167 return got_error_from_errno_fmt("openat: %s/%s",
2168 got_repo_get_path_git_dir(repo),
2169 GOT_OBJECTS_PACK_DIR);
2172 packdir = fdopendir(packdir_fd);
2173 if (packdir == NULL) {
2174 err = got_error_from_errno("fdopendir");
2175 goto done;
2178 while ((dent = readdir(packdir)) != NULL) {
2179 if (!got_repo_is_packidx_filename(dent->d_name,
2180 strlen(dent->d_name)))
2181 continue;
2183 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2184 dent->d_name) == -1) {
2185 err = got_error_from_errno("asprintf");
2186 goto done;
2189 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2190 path_packidx, 0);
2191 free(path_packidx);
2192 if (err)
2193 goto done;
2195 if (fstat(packidx->fd, &sb) == -1)
2196 goto done;
2197 *total_packsize += sb.st_size;
2199 err = got_packidx_get_packfile_path(&path_packfile,
2200 packidx->path_packidx);
2201 if (err)
2202 goto done;
2204 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2205 0) == -1) {
2206 free(path_packfile);
2207 goto done;
2209 free(path_packfile);
2210 *total_packsize += sb.st_size;
2212 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2214 (*npackfiles)++;
2216 got_packidx_close(packidx);
2217 packidx = NULL;
2219 done:
2220 if (packidx)
2221 got_packidx_close(packidx);
2222 if (packdir && closedir(packdir) != 0 && err == NULL)
2223 err = got_error_from_errno("closedir");
2224 if (err) {
2225 *npackfiles = 0;
2226 *nobjects = 0;
2227 *total_packsize = 0;
2229 return err;
2232 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2233 got_packidx_bloom_filter_cmp);