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 err = got_pack_close(&repo->packs[i - 1]);
1345 if (err)
1346 return err;
1347 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1348 return got_error_from_errno("ftruncate");
1349 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1350 return got_error_from_errno("ftruncate");
1351 memmove(&repo->packs[1], &repo->packs[0],
1352 sizeof(repo->packs) - sizeof(repo->packs[0]));
1353 i = 0;
1356 pack = &repo->packs[i];
1358 pack->path_packfile = strdup(path_packfile);
1359 if (pack->path_packfile == NULL) {
1360 err = got_error_from_errno("strdup");
1361 goto done;
1364 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1365 if (err)
1366 goto done;
1368 if (fstat(pack->fd, &sb) != 0) {
1369 err = got_error_from_errno("fstat");
1370 goto done;
1372 pack->filesize = sb.st_size;
1374 pack->privsep_child = NULL;
1376 #ifndef GOT_PACK_NO_MMAP
1377 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1378 pack->fd, 0);
1379 if (pack->map == MAP_FAILED) {
1380 if (errno != ENOMEM) {
1381 err = got_error_from_errno("mmap");
1382 goto done;
1384 pack->map = NULL; /* fall back to read(2) */
1386 #endif
1387 done:
1388 if (err) {
1389 if (pack) {
1390 free(pack->path_packfile);
1391 memset(pack, 0, sizeof(*pack));
1393 } else if (packp)
1394 *packp = pack;
1395 return err;
1398 struct got_pack *
1399 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1401 struct got_pack *pack = NULL;
1402 size_t i;
1404 for (i = 0; i < repo->pack_cache_size; i++) {
1405 pack = &repo->packs[i];
1406 if (pack->path_packfile == NULL)
1407 break;
1408 if (strcmp(pack->path_packfile, path_packfile) == 0)
1409 return pack;
1412 return NULL;
1415 const struct got_error *
1416 got_repo_init(const char *repo_path)
1418 const struct got_error *err = NULL;
1419 const char *dirnames[] = {
1420 GOT_OBJECTS_DIR,
1421 GOT_OBJECTS_PACK_DIR,
1422 GOT_REFS_DIR,
1424 const char *description_str = "Unnamed repository; "
1425 "edit this file 'description' to name the repository.";
1426 const char *headref_str = "ref: refs/heads/main";
1427 const char *gitconfig_str = "[core]\n"
1428 "\trepositoryformatversion = 0\n"
1429 "\tfilemode = true\n"
1430 "\tbare = true\n";
1431 char *path;
1432 size_t i;
1434 if (!got_path_dir_is_empty(repo_path))
1435 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1437 for (i = 0; i < nitems(dirnames); i++) {
1438 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1439 return got_error_from_errno("asprintf");
1441 err = got_path_mkdir(path);
1442 free(path);
1443 if (err)
1444 return err;
1447 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1448 return got_error_from_errno("asprintf");
1449 err = got_path_create_file(path, description_str);
1450 free(path);
1451 if (err)
1452 return err;
1454 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1455 return got_error_from_errno("asprintf");
1456 err = got_path_create_file(path, headref_str);
1457 free(path);
1458 if (err)
1459 return err;
1461 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1462 return got_error_from_errno("asprintf");
1463 err = got_path_create_file(path, gitconfig_str);
1464 free(path);
1465 if (err)
1466 return err;
1468 return NULL;
1471 static const struct got_error *
1472 match_packed_object(struct got_object_id **unique_id,
1473 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1475 const struct got_error *err = NULL;
1476 struct got_object_id_queue matched_ids;
1477 struct got_pathlist_entry *pe;
1479 STAILQ_INIT(&matched_ids);
1481 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1482 const char *path_packidx = pe->path;
1483 struct got_packidx *packidx;
1484 struct got_object_qid *qid;
1486 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1487 path_packidx, 0);
1488 if (err)
1489 break;
1491 err = got_packidx_match_id_str_prefix(&matched_ids,
1492 packidx, id_str_prefix);
1493 if (err) {
1494 got_packidx_close(packidx);
1495 break;
1497 err = got_packidx_close(packidx);
1498 if (err)
1499 break;
1501 STAILQ_FOREACH(qid, &matched_ids, entry) {
1502 if (obj_type != GOT_OBJ_TYPE_ANY) {
1503 int matched_type;
1504 err = got_object_get_type(&matched_type, repo,
1505 &qid->id);
1506 if (err)
1507 goto done;
1508 if (matched_type != obj_type)
1509 continue;
1511 if (*unique_id == NULL) {
1512 *unique_id = got_object_id_dup(&qid->id);
1513 if (*unique_id == NULL) {
1514 err = got_error_from_errno("malloc");
1515 goto done;
1517 } else {
1518 if (got_object_id_cmp(*unique_id,
1519 &qid->id) == 0)
1520 continue; /* packed multiple times */
1521 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1522 goto done;
1526 done:
1527 got_object_id_queue_free(&matched_ids);
1528 if (err) {
1529 free(*unique_id);
1530 *unique_id = NULL;
1532 return err;
1535 static const struct got_error *
1536 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1537 const char *object_dir, const char *id_str_prefix, int obj_type,
1538 struct got_repository *repo)
1540 const struct got_error *err = NULL;
1541 char *path;
1542 DIR *dir = NULL;
1543 struct dirent *dent;
1544 struct got_object_id id;
1546 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1547 err = got_error_from_errno("asprintf");
1548 goto done;
1551 dir = opendir(path);
1552 if (dir == NULL) {
1553 if (errno == ENOENT) {
1554 err = NULL;
1555 goto done;
1557 err = got_error_from_errno2("opendir", path);
1558 goto done;
1560 while ((dent = readdir(dir)) != NULL) {
1561 char *id_str;
1562 int cmp;
1564 if (strcmp(dent->d_name, ".") == 0 ||
1565 strcmp(dent->d_name, "..") == 0)
1566 continue;
1568 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1569 err = got_error_from_errno("asprintf");
1570 goto done;
1573 if (!got_parse_sha1_digest(id.sha1, id_str))
1574 continue;
1577 * Directory entries do not necessarily appear in
1578 * sorted order, so we must iterate over all of them.
1580 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1581 if (cmp != 0) {
1582 free(id_str);
1583 continue;
1586 if (*unique_id == NULL) {
1587 if (obj_type != GOT_OBJ_TYPE_ANY) {
1588 int matched_type;
1589 err = got_object_get_type(&matched_type, repo,
1590 &id);
1591 if (err)
1592 goto done;
1593 if (matched_type != obj_type)
1594 continue;
1596 *unique_id = got_object_id_dup(&id);
1597 if (*unique_id == NULL) {
1598 err = got_error_from_errno("got_object_id_dup");
1599 free(id_str);
1600 goto done;
1602 } else {
1603 if (got_object_id_cmp(*unique_id, &id) == 0)
1604 continue; /* both packed and loose */
1605 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1606 free(id_str);
1607 goto done;
1610 done:
1611 if (dir && closedir(dir) != 0 && err == NULL)
1612 err = got_error_from_errno("closedir");
1613 if (err) {
1614 free(*unique_id);
1615 *unique_id = NULL;
1617 free(path);
1618 return err;
1621 const struct got_error *
1622 got_repo_match_object_id_prefix(struct got_object_id **id,
1623 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 char *path_objects = got_repo_get_path_objects(repo);
1627 char *object_dir = NULL;
1628 size_t len;
1629 int i;
1631 *id = NULL;
1633 len = strlen(id_str_prefix);
1634 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1635 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1637 for (i = 0; i < len; i++) {
1638 if (isxdigit((unsigned char)id_str_prefix[i]))
1639 continue;
1640 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1643 if (len >= 2) {
1644 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1645 if (err)
1646 goto done;
1647 object_dir = strndup(id_str_prefix, 2);
1648 if (object_dir == NULL) {
1649 err = got_error_from_errno("strdup");
1650 goto done;
1652 err = match_loose_object(id, path_objects, object_dir,
1653 id_str_prefix, obj_type, repo);
1654 } else if (len == 1) {
1655 int i;
1656 for (i = 0; i < 0xf; i++) {
1657 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1658 == -1) {
1659 err = got_error_from_errno("asprintf");
1660 goto done;
1662 err = match_packed_object(id, repo, object_dir,
1663 obj_type);
1664 if (err)
1665 goto done;
1666 err = match_loose_object(id, path_objects, object_dir,
1667 id_str_prefix, obj_type, repo);
1668 if (err)
1669 goto done;
1671 } else {
1672 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1673 goto done;
1675 done:
1676 free(object_dir);
1677 if (err) {
1678 free(*id);
1679 *id = NULL;
1680 } else if (*id == NULL) {
1681 switch (obj_type) {
1682 case GOT_OBJ_TYPE_BLOB:
1683 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1684 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1685 break;
1686 case GOT_OBJ_TYPE_TREE:
1687 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1688 GOT_OBJ_LABEL_TREE, id_str_prefix);
1689 break;
1690 case GOT_OBJ_TYPE_COMMIT:
1691 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1692 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1693 break;
1694 case GOT_OBJ_TYPE_TAG:
1695 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1696 GOT_OBJ_LABEL_TAG, id_str_prefix);
1697 break;
1698 default:
1699 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1700 break;
1704 return err;
1707 const struct got_error *
1708 got_repo_match_object_id(struct got_object_id **id, char **label,
1709 const char *id_str, int obj_type, struct got_reflist_head *refs,
1710 struct got_repository *repo)
1712 const struct got_error *err;
1713 struct got_tag_object *tag;
1714 struct got_reference *ref = NULL;
1716 *id = NULL;
1717 if (label)
1718 *label = NULL;
1720 if (refs) {
1721 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1722 refs, repo);
1723 if (err == NULL) {
1724 *id = got_object_id_dup(
1725 got_object_tag_get_object_id(tag));
1726 if (*id == NULL)
1727 err = got_error_from_errno("got_object_id_dup");
1728 else if (label && asprintf(label, "refs/tags/%s",
1729 got_object_tag_get_name(tag)) == -1) {
1730 err = got_error_from_errno("asprintf");
1731 free(*id);
1732 *id = NULL;
1734 got_object_tag_close(tag);
1735 return err;
1736 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1737 err->code != GOT_ERR_NO_OBJ)
1738 return err;
1741 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1742 if (err) {
1743 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1744 return err;
1745 err = got_ref_open(&ref, repo, id_str, 0);
1746 if (err != NULL)
1747 goto done;
1748 if (label) {
1749 *label = strdup(got_ref_get_name(ref));
1750 if (*label == NULL) {
1751 err = got_error_from_errno("strdup");
1752 goto done;
1755 err = got_ref_resolve(id, repo, ref);
1756 } else if (label) {
1757 err = got_object_id_str(label, *id);
1758 if (*label == NULL) {
1759 err = got_error_from_errno("strdup");
1760 goto done;
1763 done:
1764 if (ref)
1765 got_ref_close(ref);
1766 return err;
1769 const struct got_error *
1770 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1771 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1773 const struct got_error *err = NULL;
1774 struct got_reflist_entry *re;
1775 struct got_object_id *tag_id;
1776 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1778 *tag = NULL;
1780 TAILQ_FOREACH(re, refs, entry) {
1781 const char *refname;
1782 refname = got_ref_get_name(re->ref);
1783 if (got_ref_is_symbolic(re->ref))
1784 continue;
1785 if (strncmp(refname, "refs/tags/", 10) != 0)
1786 continue;
1787 if (!name_is_absolute)
1788 refname += strlen("refs/tags/");
1789 if (strcmp(refname, name) != 0)
1790 continue;
1791 err = got_ref_resolve(&tag_id, repo, re->ref);
1792 if (err)
1793 break;
1794 err = got_object_open_as_tag(tag, repo, tag_id);
1795 free(tag_id);
1796 if (err)
1797 break;
1798 if (obj_type == GOT_OBJ_TYPE_ANY ||
1799 got_object_tag_get_object_type(*tag) == obj_type)
1800 break;
1801 got_object_tag_close(*tag);
1802 *tag = NULL;
1805 if (err == NULL && *tag == NULL)
1806 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1807 GOT_OBJ_LABEL_TAG, name);
1808 return err;
1811 static const struct got_error *
1812 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1813 const char *name, mode_t mode, struct got_object_id *blob_id)
1815 const struct got_error *err = NULL;
1817 *new_te = NULL;
1819 *new_te = calloc(1, sizeof(**new_te));
1820 if (*new_te == NULL)
1821 return got_error_from_errno("calloc");
1823 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1824 sizeof((*new_te)->name)) {
1825 err = got_error(GOT_ERR_NO_SPACE);
1826 goto done;
1829 if (S_ISLNK(mode)) {
1830 (*new_te)->mode = S_IFLNK;
1831 } else {
1832 (*new_te)->mode = S_IFREG;
1833 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1835 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1836 done:
1837 if (err && *new_te) {
1838 free(*new_te);
1839 *new_te = NULL;
1841 return err;
1844 static const struct got_error *
1845 import_file(struct got_tree_entry **new_te, struct dirent *de,
1846 const char *path, struct got_repository *repo)
1848 const struct got_error *err;
1849 struct got_object_id *blob_id = NULL;
1850 char *filepath;
1851 struct stat sb;
1853 if (asprintf(&filepath, "%s%s%s", path,
1854 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1855 return got_error_from_errno("asprintf");
1857 if (lstat(filepath, &sb) != 0) {
1858 err = got_error_from_errno2("lstat", path);
1859 goto done;
1862 err = got_object_blob_create(&blob_id, filepath, repo);
1863 if (err)
1864 goto done;
1866 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1867 blob_id);
1868 done:
1869 free(filepath);
1870 if (err)
1871 free(blob_id);
1872 return err;
1875 static const struct got_error *
1876 insert_tree_entry(struct got_tree_entry *new_te,
1877 struct got_pathlist_head *paths)
1879 const struct got_error *err = NULL;
1880 struct got_pathlist_entry *new_pe;
1882 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1883 if (err)
1884 return err;
1885 if (new_pe == NULL)
1886 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1887 return NULL;
1890 static const struct got_error *write_tree(struct got_object_id **,
1891 const char *, struct got_pathlist_head *, struct got_repository *,
1892 got_repo_import_cb progress_cb, void *progress_arg);
1894 static const struct got_error *
1895 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1896 const char *path, struct got_pathlist_head *ignores,
1897 struct got_repository *repo,
1898 got_repo_import_cb progress_cb, void *progress_arg)
1900 const struct got_error *err;
1901 struct got_object_id *id = NULL;
1902 char *subdirpath;
1904 if (asprintf(&subdirpath, "%s%s%s", path,
1905 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1906 return got_error_from_errno("asprintf");
1908 (*new_te) = calloc(1, sizeof(**new_te));
1909 if (*new_te == NULL)
1910 return got_error_from_errno("calloc");
1911 (*new_te)->mode = S_IFDIR;
1912 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1913 sizeof((*new_te)->name)) {
1914 err = got_error(GOT_ERR_NO_SPACE);
1915 goto done;
1917 err = write_tree(&id, subdirpath, ignores, repo,
1918 progress_cb, progress_arg);
1919 if (err)
1920 goto done;
1921 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1923 done:
1924 free(id);
1925 free(subdirpath);
1926 if (err) {
1927 free(*new_te);
1928 *new_te = NULL;
1930 return err;
1933 static const struct got_error *
1934 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1935 struct got_pathlist_head *ignores, struct got_repository *repo,
1936 got_repo_import_cb progress_cb, void *progress_arg)
1938 const struct got_error *err = NULL;
1939 DIR *dir;
1940 struct dirent *de;
1941 int nentries;
1942 struct got_tree_entry *new_te = NULL;
1943 struct got_pathlist_head paths;
1944 struct got_pathlist_entry *pe;
1946 *new_tree_id = NULL;
1948 TAILQ_INIT(&paths);
1950 dir = opendir(path_dir);
1951 if (dir == NULL) {
1952 err = got_error_from_errno2("opendir", path_dir);
1953 goto done;
1956 nentries = 0;
1957 while ((de = readdir(dir)) != NULL) {
1958 int ignore = 0;
1959 int type;
1961 if (strcmp(de->d_name, ".") == 0 ||
1962 strcmp(de->d_name, "..") == 0)
1963 continue;
1965 TAILQ_FOREACH(pe, ignores, entry) {
1966 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1967 ignore = 1;
1968 break;
1971 if (ignore)
1972 continue;
1974 err = got_path_dirent_type(&type, path_dir, de);
1975 if (err)
1976 goto done;
1978 if (type == DT_DIR) {
1979 err = import_subdir(&new_te, de, path_dir,
1980 ignores, repo, progress_cb, progress_arg);
1981 if (err) {
1982 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1983 goto done;
1984 err = NULL;
1985 continue;
1987 } else if (type == DT_REG || type == DT_LNK) {
1988 err = import_file(&new_te, de, path_dir, repo);
1989 if (err)
1990 goto done;
1991 } else
1992 continue;
1994 err = insert_tree_entry(new_te, &paths);
1995 if (err)
1996 goto done;
1997 nentries++;
2000 if (TAILQ_EMPTY(&paths)) {
2001 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2002 "cannot create tree without any entries");
2003 goto done;
2006 TAILQ_FOREACH(pe, &paths, entry) {
2007 struct got_tree_entry *te = pe->data;
2008 char *path;
2009 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2010 continue;
2011 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2012 err = got_error_from_errno("asprintf");
2013 goto done;
2015 err = (*progress_cb)(progress_arg, path);
2016 free(path);
2017 if (err)
2018 goto done;
2021 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2022 done:
2023 if (dir)
2024 closedir(dir);
2025 got_pathlist_free(&paths);
2026 return err;
2029 const struct got_error *
2030 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2031 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2032 struct got_repository *repo, got_repo_import_cb progress_cb,
2033 void *progress_arg)
2035 const struct got_error *err;
2036 struct got_object_id *new_tree_id;
2038 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2039 progress_cb, progress_arg);
2040 if (err)
2041 return err;
2043 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2044 author, time(NULL), author, time(NULL), logmsg, repo);
2045 free(new_tree_id);
2046 return err;
2049 const struct got_error *
2050 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2051 struct got_repository *repo)
2053 const struct got_error *err = NULL;
2054 char *path_objects = NULL, *path = NULL;
2055 DIR *dir = NULL;
2056 struct got_object_id id;
2057 int i;
2059 *nobjects = 0;
2060 *ondisk_size = 0;
2062 path_objects = got_repo_get_path_objects(repo);
2063 if (path_objects == NULL)
2064 return got_error_from_errno("got_repo_get_path_objects");
2066 for (i = 0; i <= 0xff; i++) {
2067 struct dirent *dent;
2069 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2070 err = got_error_from_errno("asprintf");
2071 break;
2074 dir = opendir(path);
2075 if (dir == NULL) {
2076 if (errno == ENOENT) {
2077 err = NULL;
2078 continue;
2080 err = got_error_from_errno2("opendir", path);
2081 break;
2084 while ((dent = readdir(dir)) != NULL) {
2085 char *id_str;
2086 int fd;
2087 struct stat sb;
2089 if (strcmp(dent->d_name, ".") == 0 ||
2090 strcmp(dent->d_name, "..") == 0)
2091 continue;
2093 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2094 err = got_error_from_errno("asprintf");
2095 goto done;
2098 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2099 free(id_str);
2100 continue;
2102 free(id_str);
2104 err = got_object_open_loose_fd(&fd, &id, repo);
2105 if (err)
2106 goto done;
2108 if (fstat(fd, &sb) == -1) {
2109 err = got_error_from_errno("fstat");
2110 close(fd);
2111 goto done;
2113 (*nobjects)++;
2114 (*ondisk_size) += sb.st_size;
2116 if (close(fd) == -1) {
2117 err = got_error_from_errno("close");
2118 goto done;
2122 if (closedir(dir) != 0) {
2123 err = got_error_from_errno("closedir");
2124 goto done;
2126 dir = NULL;
2128 free(path);
2129 path = NULL;
2131 done:
2132 if (dir && closedir(dir) != 0 && err == NULL)
2133 err = got_error_from_errno("closedir");
2135 if (err) {
2136 *nobjects = 0;
2137 *ondisk_size = 0;
2139 free(path_objects);
2140 free(path);
2141 return err;
2144 const struct got_error *
2145 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2146 off_t *total_packsize, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 DIR *packdir = NULL;
2150 struct dirent *dent;
2151 struct got_packidx *packidx = NULL;
2152 char *path_packidx;
2153 char *path_packfile;
2154 int packdir_fd;
2155 struct stat sb;
2157 *npackfiles = 0;
2158 *nobjects = 0;
2159 *total_packsize = 0;
2161 packdir_fd = openat(got_repo_get_fd(repo),
2162 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2163 if (packdir_fd == -1) {
2164 return got_error_from_errno_fmt("openat: %s/%s",
2165 got_repo_get_path_git_dir(repo),
2166 GOT_OBJECTS_PACK_DIR);
2169 packdir = fdopendir(packdir_fd);
2170 if (packdir == NULL) {
2171 err = got_error_from_errno("fdopendir");
2172 goto done;
2175 while ((dent = readdir(packdir)) != NULL) {
2176 if (!got_repo_is_packidx_filename(dent->d_name,
2177 strlen(dent->d_name)))
2178 continue;
2180 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2181 dent->d_name) == -1) {
2182 err = got_error_from_errno("asprintf");
2183 goto done;
2186 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2187 path_packidx, 0);
2188 free(path_packidx);
2189 if (err)
2190 goto done;
2192 if (fstat(packidx->fd, &sb) == -1)
2193 goto done;
2194 *total_packsize += sb.st_size;
2196 err = got_packidx_get_packfile_path(&path_packfile,
2197 packidx->path_packidx);
2198 if (err)
2199 goto done;
2201 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2202 0) == -1) {
2203 free(path_packfile);
2204 goto done;
2206 free(path_packfile);
2207 *total_packsize += sb.st_size;
2209 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2211 (*npackfiles)++;
2213 got_packidx_close(packidx);
2214 packidx = NULL;
2216 done:
2217 if (packidx)
2218 got_packidx_close(packidx);
2219 if (packdir && closedir(packdir) != 0 && err == NULL)
2220 err = got_error_from_errno("closedir");
2221 if (err) {
2222 *npackfiles = 0;
2223 *nobjects = 0;
2224 *total_packsize = 0;
2226 return err;
2229 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2230 got_packidx_bloom_filter_cmp);