Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <fnmatch.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <stdint.h>
40 #include <uuid.h>
42 #include "bloom.h"
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #endif
67 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
68 got_packidx_bloom_filter_cmp);
70 const char *
71 got_repo_get_path(struct got_repository *repo)
72 {
73 return repo->path;
74 }
76 const char *
77 got_repo_get_path_git_dir(struct got_repository *repo)
78 {
79 return repo->path_git_dir;
80 }
82 int
83 got_repo_get_fd(struct got_repository *repo)
84 {
85 return repo->gitdir_fd;
86 }
88 const char *
89 got_repo_get_gitconfig_author_name(struct got_repository *repo)
90 {
91 return repo->gitconfig_author_name;
92 }
94 const char *
95 got_repo_get_gitconfig_author_email(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_email;
98 }
100 const char *
101 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
103 return repo->global_gitconfig_author_name;
106 const char *
107 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
109 return repo->global_gitconfig_author_email;
112 const char *
113 got_repo_get_gitconfig_owner(struct got_repository *repo)
115 return repo->gitconfig_owner;
118 void
119 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
120 struct got_repository *repo)
122 *extensions = repo->extensions;
123 *nextensions = repo->nextensions;
126 int
127 got_repo_is_bare(struct got_repository *repo)
129 return (strcmp(repo->path, repo->path_git_dir) == 0);
132 static char *
133 get_path_git_child(struct got_repository *repo, const char *basename)
135 char *path_child;
137 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
138 basename) == -1)
139 return NULL;
141 return path_child;
144 char *
145 got_repo_get_path_objects(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_OBJECTS_DIR);
150 char *
151 got_repo_get_path_objects_pack(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
156 char *
157 got_repo_get_path_refs(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_REFS_DIR);
162 char *
163 got_repo_get_path_packed_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
168 static char *
169 get_path_head(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_HEAD_FILE);
174 char *
175 got_repo_get_path_gitconfig(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_GITCONFIG);
180 char *
181 got_repo_get_path_gotconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
186 const struct got_gotconfig *
187 got_repo_get_gotconfig(struct got_repository *repo)
189 return repo->gotconfig;
192 void
193 got_repo_get_gitconfig_remotes(int *nremotes,
194 const struct got_remote_repo **remotes, struct got_repository *repo)
196 *nremotes = repo->ngitconfig_remotes;
197 *remotes = repo->gitconfig_remotes;
200 static int
201 is_git_repo(struct got_repository *repo)
203 const char *path_git = got_repo_get_path_git_dir(repo);
204 char *path_objects = got_repo_get_path_objects(repo);
205 char *path_refs = got_repo_get_path_refs(repo);
206 char *path_head = get_path_head(repo);
207 int ret = 0;
208 struct stat sb;
209 struct got_reference *head_ref;
211 if (lstat(path_git, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_objects, &sb) == -1)
217 goto done;
218 if (!S_ISDIR(sb.st_mode))
219 goto done;
221 if (lstat(path_refs, &sb) == -1)
222 goto done;
223 if (!S_ISDIR(sb.st_mode))
224 goto done;
226 if (lstat(path_head, &sb) == -1)
227 goto done;
228 if (!S_ISREG(sb.st_mode))
229 goto done;
231 /* Check if the HEAD reference can be opened. */
232 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
233 goto done;
234 got_ref_close(head_ref);
236 ret = 1;
237 done:
238 free(path_objects);
239 free(path_refs);
240 free(path_head);
241 return ret;
245 const struct got_error *
246 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
247 struct got_object *obj)
249 #ifndef GOT_NO_OBJ_CACHE
250 const struct got_error *err = NULL;
251 err = got_object_cache_add(&repo->objcache, id, obj);
252 if (err) {
253 if (err->code == GOT_ERR_OBJ_EXISTS ||
254 err->code == GOT_ERR_OBJ_TOO_LARGE)
255 err = NULL;
256 return err;
258 obj->refcnt++;
259 #endif
260 return NULL;
263 struct got_object *
264 got_repo_get_cached_object(struct got_repository *repo,
265 struct got_object_id *id)
267 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
270 const struct got_error *
271 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
272 struct got_tree_object *tree)
274 #ifndef GOT_NO_OBJ_CACHE
275 const struct got_error *err = NULL;
276 err = got_object_cache_add(&repo->treecache, id, tree);
277 if (err) {
278 if (err->code == GOT_ERR_OBJ_EXISTS ||
279 err->code == GOT_ERR_OBJ_TOO_LARGE)
280 err = NULL;
281 return err;
283 tree->refcnt++;
284 #endif
285 return NULL;
288 struct got_tree_object *
289 got_repo_get_cached_tree(struct got_repository *repo,
290 struct got_object_id *id)
292 return (struct got_tree_object *)got_object_cache_get(
293 &repo->treecache, id);
296 const struct got_error *
297 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
298 struct got_commit_object *commit)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->commitcache, id, commit);
303 if (err) {
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 err = NULL;
307 return err;
309 commit->refcnt++;
310 #endif
311 return NULL;
314 struct got_commit_object *
315 got_repo_get_cached_commit(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_commit_object *)got_object_cache_get(
319 &repo->commitcache, id);
322 const struct got_error *
323 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
324 struct got_tag_object *tag)
326 #ifndef GOT_NO_OBJ_CACHE
327 const struct got_error *err = NULL;
328 err = got_object_cache_add(&repo->tagcache, id, tag);
329 if (err) {
330 if (err->code == GOT_ERR_OBJ_EXISTS ||
331 err->code == GOT_ERR_OBJ_TOO_LARGE)
332 err = NULL;
333 return err;
335 tag->refcnt++;
336 #endif
337 return NULL;
340 struct got_tag_object *
341 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
343 return (struct got_tag_object *)got_object_cache_get(
344 &repo->tagcache, id);
347 const struct got_error *
348 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
349 struct got_raw_object *raw)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->rawcache, id, raw);
354 if (err) {
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
357 err = NULL;
358 return err;
360 raw->refcnt++;
361 #endif
362 return NULL;
366 struct got_raw_object *
367 got_repo_get_cached_raw_object(struct got_repository *repo,
368 struct got_object_id *id)
370 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
374 static const struct got_error *
375 open_repo(struct got_repository *repo, const char *path)
377 const struct got_error *err = NULL;
379 repo->gitdir_fd = -1;
381 /* bare git repository? */
382 repo->path_git_dir = strdup(path);
383 if (repo->path_git_dir == NULL)
384 return got_error_from_errno("strdup");
385 if (is_git_repo(repo)) {
386 repo->path = strdup(repo->path_git_dir);
387 if (repo->path == NULL) {
388 err = got_error_from_errno("strdup");
389 goto done;
391 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
392 if (repo->gitdir_fd == -1) {
393 err = got_error_from_errno2("open",
394 repo->path_git_dir);
395 goto done;
397 return NULL;
400 /* git repository with working tree? */
401 free(repo->path_git_dir);
402 repo->path_git_dir = NULL;
403 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
404 err = got_error_from_errno("asprintf");
405 goto done;
407 if (is_git_repo(repo)) {
408 repo->path = strdup(path);
409 if (repo->path == NULL) {
410 err = got_error_from_errno("strdup");
411 goto done;
413 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
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);
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 imsg_clear(ibuf);
576 err = got_privsep_send_stop(imsg_fds[0]);
577 child_err = got_privsep_wait_for_child(pid);
578 if (child_err && err == NULL)
579 err = child_err;
580 done:
581 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
584 err = got_error_from_errno("close");
585 if (fd != -1 && close(fd) == -1 && err == NULL)
586 err = got_error_from_errno2("close", gitconfig_path);
587 free(ibuf);
588 return err;
591 static const struct got_error *
592 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
594 const struct got_error *err = NULL;
595 char *repo_gitconfig_path = NULL;
597 if (global_gitconfig_path) {
598 /* Read settings from ~/.gitconfig. */
599 int dummy_repo_version;
600 err = parse_gitconfig_file(&dummy_repo_version,
601 &repo->global_gitconfig_author_name,
602 &repo->global_gitconfig_author_email,
603 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
604 if (err)
605 return err;
608 /* Read repository's .git/config file. */
609 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
610 if (repo_gitconfig_path == NULL)
611 return got_error_from_errno("got_repo_get_path_gitconfig");
613 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
614 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
615 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
616 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
617 repo_gitconfig_path);
618 if (err)
619 goto done;
620 done:
621 free(repo_gitconfig_path);
622 return err;
625 static const struct got_error *
626 read_gotconfig(struct got_repository *repo)
628 const struct got_error *err = NULL;
629 char *gotconfig_path;
631 gotconfig_path = got_repo_get_path_gotconfig(repo);
632 if (gotconfig_path == NULL)
633 return got_error_from_errno("got_repo_get_path_gotconfig");
635 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
636 free(gotconfig_path);
637 return err;
640 /* Supported repository format extensions. */
641 static const char *repo_extensions[] = {
642 "noop", /* Got supports repository format version 1. */
643 "preciousObjects", /* Supported by gotadmin cleanup. */
644 "worktreeConfig", /* Got does not care about Git work trees. */
645 };
647 const struct got_error *
648 got_repo_open(struct got_repository **repop, const char *path,
649 const char *global_gitconfig_path)
651 struct got_repository *repo = NULL;
652 const struct got_error *err = NULL;
653 char *repo_path = NULL;
654 size_t i;
655 struct rlimit rl;
657 *repop = NULL;
659 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
660 return got_error_from_errno("getrlimit");
662 repo = calloc(1, sizeof(*repo));
663 if (repo == NULL) {
664 err = got_error_from_errno("calloc");
665 goto done;
668 RB_INIT(&repo->packidx_bloom_filters);
670 for (i = 0; i < nitems(repo->privsep_children); i++) {
671 memset(&repo->privsep_children[i], 0,
672 sizeof(repo->privsep_children[0]));
673 repo->privsep_children[i].imsg_fd = -1;
676 err = got_object_cache_init(&repo->objcache,
677 GOT_OBJECT_CACHE_TYPE_OBJ);
678 if (err)
679 goto done;
680 err = got_object_cache_init(&repo->treecache,
681 GOT_OBJECT_CACHE_TYPE_TREE);
682 if (err)
683 goto done;
684 err = got_object_cache_init(&repo->commitcache,
685 GOT_OBJECT_CACHE_TYPE_COMMIT);
686 if (err)
687 goto done;
688 err = got_object_cache_init(&repo->tagcache,
689 GOT_OBJECT_CACHE_TYPE_TAG);
690 if (err)
691 goto done;
692 err = got_object_cache_init(&repo->rawcache,
693 GOT_OBJECT_CACHE_TYPE_RAW);
694 if (err)
695 goto done;
697 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
698 if (repo->pack_cache_size > rl.rlim_cur / 8)
699 repo->pack_cache_size = rl.rlim_cur / 8;
701 repo_path = realpath(path, NULL);
702 if (repo_path == NULL) {
703 err = got_error_from_errno2("realpath", path);
704 goto done;
707 for (;;) {
708 char *parent_path;
710 err = open_repo(repo, repo_path);
711 if (err == NULL)
712 break;
713 if (err->code != GOT_ERR_NOT_GIT_REPO)
714 goto done;
715 if (repo_path[0] == '/' && repo_path[1] == '\0') {
716 err = got_error(GOT_ERR_NOT_GIT_REPO);
717 goto done;
719 err = got_path_dirname(&parent_path, repo_path);
720 if (err)
721 goto done;
722 free(repo_path);
723 repo_path = parent_path;
726 err = read_gotconfig(repo);
727 if (err)
728 goto done;
730 err = read_gitconfig(repo, global_gitconfig_path);
731 if (err)
732 goto done;
733 if (repo->gitconfig_repository_format_version != 0)
734 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
735 for (i = 0; i < repo->nextensions; i++) {
736 char *ext = repo->extensions[i];
737 int j, supported = 0;
738 for (j = 0; j < nitems(repo_extensions); j++) {
739 if (strcmp(ext, repo_extensions[j]) == 0) {
740 supported = 1;
741 break;
744 if (!supported) {
745 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
746 goto done;
749 done:
750 if (err)
751 got_repo_close(repo);
752 else
753 *repop = repo;
754 free(repo_path);
755 return err;
758 const struct got_error *
759 got_repo_close(struct got_repository *repo)
761 const struct got_error *err = NULL, *child_err;
762 struct got_packidx_bloom_filter *bf;
763 size_t i;
765 for (i = 0; i < repo->pack_cache_size; i++) {
766 if (repo->packidx_cache[i] == NULL)
767 break;
768 got_packidx_close(repo->packidx_cache[i]);
771 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters))) {
773 RB_REMOVE(got_packidx_bloom_filter_tree,
774 &repo->packidx_bloom_filters, bf);
775 free(bf->bloom);
776 free(bf);
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packs[i].path_packfile == NULL)
781 break;
782 got_pack_close(&repo->packs[i]);
785 free(repo->path);
786 free(repo->path_git_dir);
788 got_object_cache_close(&repo->objcache);
789 got_object_cache_close(&repo->treecache);
790 got_object_cache_close(&repo->commitcache);
791 got_object_cache_close(&repo->tagcache);
792 got_object_cache_close(&repo->rawcache);
794 for (i = 0; i < nitems(repo->privsep_children); i++) {
795 if (repo->privsep_children[i].imsg_fd == -1)
796 continue;
797 imsg_clear(repo->privsep_children[i].ibuf);
798 free(repo->privsep_children[i].ibuf);
799 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
800 child_err = got_privsep_wait_for_child(
801 repo->privsep_children[i].pid);
802 if (child_err && err == NULL)
803 err = child_err;
804 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
805 err == NULL)
806 err = got_error_from_errno("close");
809 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
810 err == NULL)
811 err = got_error_from_errno("close");
813 if (repo->gotconfig)
814 got_gotconfig_free(repo->gotconfig);
815 free(repo->gitconfig_author_name);
816 free(repo->gitconfig_author_email);
817 for (i = 0; i < repo->ngitconfig_remotes; i++)
818 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
819 free(repo->gitconfig_remotes);
820 for (i = 0; i < repo->nextensions; i++)
821 free(repo->extensions[i]);
822 free(repo->extensions);
823 free(repo);
825 return err;
828 void
829 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
831 int i;
833 free(repo->name);
834 repo->name = NULL;
835 free(repo->fetch_url);
836 repo->fetch_url = NULL;
837 free(repo->send_url);
838 repo->send_url = NULL;
839 for (i = 0; i < repo->nfetch_branches; i++)
840 free(repo->fetch_branches[i]);
841 free(repo->fetch_branches);
842 repo->fetch_branches = NULL;
843 repo->nfetch_branches = 0;
844 for (i = 0; i < repo->nsend_branches; i++)
845 free(repo->send_branches[i]);
846 free(repo->send_branches);
847 repo->send_branches = NULL;
848 repo->nsend_branches = 0;
851 const struct got_error *
852 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
853 const char *input_path)
855 const struct got_error *err = NULL;
856 const char *repo_abspath = NULL;
857 size_t repolen, len;
858 char *canonpath, *path = NULL;
860 *in_repo_path = NULL;
862 canonpath = strdup(input_path);
863 if (canonpath == NULL) {
864 err = got_error_from_errno("strdup");
865 goto done;
867 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
868 if (err)
869 goto done;
871 repo_abspath = got_repo_get_path(repo);
873 if (canonpath[0] == '\0') {
874 path = strdup(canonpath);
875 if (path == NULL) {
876 err = got_error_from_errno("strdup");
877 goto done;
879 } else {
880 path = realpath(canonpath, NULL);
881 if (path == NULL) {
882 if (errno != ENOENT) {
883 err = got_error_from_errno2("realpath",
884 canonpath);
885 goto done;
887 /*
888 * Path is not on disk.
889 * Assume it is already relative to repository root.
890 */
891 path = strdup(canonpath);
892 if (path == NULL) {
893 err = got_error_from_errno("strdup");
894 goto done;
898 repolen = strlen(repo_abspath);
899 len = strlen(path);
902 if (strcmp(path, repo_abspath) == 0) {
903 free(path);
904 path = strdup("");
905 if (path == NULL) {
906 err = got_error_from_errno("strdup");
907 goto done;
909 } else if (len > repolen &&
910 got_path_is_child(path, repo_abspath, repolen)) {
911 /* Matched an on-disk path inside repository. */
912 if (got_repo_is_bare(repo)) {
913 /*
914 * Matched an on-disk path inside repository
915 * database. Treat input as repository-relative.
916 */
917 free(path);
918 path = canonpath;
919 canonpath = NULL;
920 } else {
921 char *child;
922 /* Strip common prefix with repository path. */
923 err = got_path_skip_common_ancestor(&child,
924 repo_abspath, path);
925 if (err)
926 goto done;
927 free(path);
928 path = child;
930 } else {
931 /*
932 * Matched unrelated on-disk path.
933 * Treat input as repository-relative.
934 */
935 free(path);
936 path = canonpath;
937 canonpath = NULL;
941 /* Make in-repository path absolute */
942 if (path[0] != '/') {
943 char *abspath;
944 if (asprintf(&abspath, "/%s", path) == -1) {
945 err = got_error_from_errno("asprintf");
946 goto done;
948 free(path);
949 path = abspath;
952 done:
953 free(canonpath);
954 if (err)
955 free(path);
956 else
957 *in_repo_path = path;
958 return err;
961 static const struct got_error *
962 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
963 const char *path_packidx)
965 const struct got_error *err = NULL;
966 size_t i;
968 for (i = 0; i < repo->pack_cache_size; i++) {
969 if (repo->packidx_cache[i] == NULL)
970 break;
971 if (strcmp(repo->packidx_cache[i]->path_packidx,
972 path_packidx) == 0) {
973 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
976 if (i == repo->pack_cache_size) {
977 i = repo->pack_cache_size - 1;
978 err = got_packidx_close(repo->packidx_cache[i]);
979 if (err)
980 return err;
983 repo->packidx_cache[i] = packidx;
985 return NULL;
988 int
989 got_repo_is_packidx_filename(const char *name, size_t len)
991 if (len != GOT_PACKIDX_NAMELEN)
992 return 0;
994 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
995 return 0;
997 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
998 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
999 return 0;
1001 return 1;
1004 static struct got_packidx_bloom_filter *
1005 get_packidx_bloom_filter(struct got_repository *repo,
1006 const char *path, size_t path_len)
1008 struct got_packidx_bloom_filter key;
1010 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1011 return NULL; /* XXX */
1012 key.path_len = path_len;
1014 return RB_FIND(got_packidx_bloom_filter_tree,
1015 &repo->packidx_bloom_filters, &key);
1018 static int
1019 check_packidx_bloom_filter(struct got_repository *repo,
1020 const char *path_packidx, struct got_object_id *id)
1022 struct got_packidx_bloom_filter *bf;
1024 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1025 if (bf)
1026 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1028 /* No bloom filter means this pack index must be searched. */
1029 return 1;
1032 static const struct got_error *
1033 add_packidx_bloom_filter(struct got_repository *repo,
1034 struct got_packidx *packidx, const char *path_packidx)
1036 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1037 struct got_packidx_bloom_filter *bf;
1038 size_t len;
1041 * Don't use bloom filters for very large pack index files.
1042 * Large pack files will contain a relatively large fraction
1043 * of our objects so we will likely need to visit them anyway.
1044 * The more objects a pack file contains the higher the probability
1045 * of a false-positive match from the bloom filter. And reading
1046 * all object IDs from a large pack index file can be expensive.
1048 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1049 return NULL;
1051 /* Do we already have a filter for this pack index? */
1052 if (get_packidx_bloom_filter(repo, path_packidx,
1053 strlen(path_packidx)) != NULL)
1054 return NULL;
1056 bf = calloc(1, sizeof(*bf));
1057 if (bf == NULL)
1058 return got_error_from_errno("calloc");
1059 bf->bloom = calloc(1, sizeof(*bf->bloom));
1060 if (bf->bloom == NULL) {
1061 free(bf);
1062 return got_error_from_errno("calloc");
1065 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1066 if (len >= sizeof(bf->path)) {
1067 free(bf->bloom);
1068 free(bf);
1069 return got_error(GOT_ERR_NO_SPACE);
1071 bf->path_len = len;
1073 /* Minimum size supported by our bloom filter is 1000 entries. */
1074 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1075 for (i = 0; i < nobjects; i++) {
1076 struct got_packidx_object_id *id;
1077 id = &packidx->hdr.sorted_ids[i];
1078 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1081 RB_INSERT(got_packidx_bloom_filter_tree,
1082 &repo->packidx_bloom_filters, bf);
1083 return NULL;
1086 const struct got_error *
1087 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1088 struct got_repository *repo, struct got_object_id *id)
1090 const struct got_error *err;
1091 DIR *packdir = NULL;
1092 struct dirent *dent;
1093 char *path_packidx;
1094 size_t i;
1095 int packdir_fd;
1097 /* Search pack index cache. */
1098 for (i = 0; i < repo->pack_cache_size; i++) {
1099 if (repo->packidx_cache[i] == NULL)
1100 break;
1101 if (!check_packidx_bloom_filter(repo,
1102 repo->packidx_cache[i]->path_packidx, id))
1103 continue; /* object will not be found in this index */
1104 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1105 if (*idx != -1) {
1106 *packidx = repo->packidx_cache[i];
1108 * Move this cache entry to the front. Repeatedly
1109 * searching a wrong pack index can be expensive.
1111 if (i > 0) {
1112 memmove(&repo->packidx_cache[1],
1113 &repo->packidx_cache[0],
1114 i * sizeof(repo->packidx_cache[0]));
1115 repo->packidx_cache[0] = *packidx;
1117 return NULL;
1120 /* No luck. Search the filesystem. */
1122 packdir_fd = openat(got_repo_get_fd(repo),
1123 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1124 if (packdir_fd == -1) {
1125 if (errno == ENOENT)
1126 err = got_error_no_obj(id);
1127 else
1128 err = got_error_from_errno_fmt("openat: %s/%s",
1129 got_repo_get_path_git_dir(repo),
1130 GOT_OBJECTS_PACK_DIR);
1131 goto done;
1134 packdir = fdopendir(packdir_fd);
1135 if (packdir == NULL) {
1136 err = got_error_from_errno("fdopendir");
1137 goto done;
1140 while ((dent = readdir(packdir)) != NULL) {
1141 int is_cached = 0;
1143 if (!got_repo_is_packidx_filename(dent->d_name,
1144 strlen(dent->d_name)))
1145 continue;
1147 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1148 dent->d_name) == -1) {
1149 err = got_error_from_errno("asprintf");
1150 goto done;
1153 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1154 free(path_packidx);
1155 continue; /* object will not be found in this index */
1158 for (i = 0; i < repo->pack_cache_size; i++) {
1159 if (repo->packidx_cache[i] == NULL)
1160 break;
1161 if (strcmp(repo->packidx_cache[i]->path_packidx,
1162 path_packidx) == 0) {
1163 is_cached = 1;
1164 break;
1167 if (is_cached) {
1168 free(path_packidx);
1169 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1173 path_packidx, 0);
1174 if (err) {
1175 free(path_packidx);
1176 goto done;
1179 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1180 if (err) {
1181 free(path_packidx);
1182 goto done;
1185 err = cache_packidx(repo, *packidx, path_packidx);
1186 free(path_packidx);
1187 if (err)
1188 goto done;
1190 *idx = got_packidx_get_object_idx(*packidx, id);
1191 if (*idx != -1) {
1192 err = NULL; /* found the object */
1193 goto done;
1197 err = got_error_no_obj(id);
1198 done:
1199 if (packdir && closedir(packdir) != 0 && err == NULL)
1200 err = got_error_from_errno("closedir");
1201 return err;
1204 static const struct got_error *
1205 read_packfile_hdr(int fd, struct got_packidx *packidx)
1207 const struct got_error *err = NULL;
1208 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1209 struct got_packfile_hdr hdr;
1210 ssize_t n;
1212 n = read(fd, &hdr, sizeof(hdr));
1213 if (n < 0)
1214 return got_error_from_errno("read");
1215 if (n != sizeof(hdr))
1216 return got_error(GOT_ERR_BAD_PACKFILE);
1218 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1219 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1220 be32toh(hdr.nobjects) != totobj)
1221 err = got_error(GOT_ERR_BAD_PACKFILE);
1223 return err;
1226 static const struct got_error *
1227 open_packfile(int *fd, struct got_repository *repo,
1228 const char *relpath, struct got_packidx *packidx)
1230 const struct got_error *err = NULL;
1232 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1233 if (*fd == -1)
1234 return got_error_from_errno_fmt("openat: %s/%s",
1235 got_repo_get_path_git_dir(repo), relpath);
1237 if (packidx) {
1238 err = read_packfile_hdr(*fd, packidx);
1239 if (err) {
1240 close(*fd);
1241 *fd = -1;
1245 return err;
1248 const struct got_error *
1249 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1250 const char *path_packfile, struct got_packidx *packidx)
1252 const struct got_error *err = NULL;
1253 struct got_pack *pack = NULL;
1254 struct stat sb;
1255 size_t i;
1257 if (packp)
1258 *packp = NULL;
1260 for (i = 0; i < repo->pack_cache_size; i++) {
1261 pack = &repo->packs[i];
1262 if (pack->path_packfile == NULL)
1263 break;
1264 if (strcmp(pack->path_packfile, path_packfile) == 0)
1265 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1268 if (i == repo->pack_cache_size) {
1269 err = got_pack_close(&repo->packs[i - 1]);
1270 if (err)
1271 return err;
1272 memmove(&repo->packs[1], &repo->packs[0],
1273 sizeof(repo->packs) - sizeof(repo->packs[0]));
1274 i = 0;
1277 pack = &repo->packs[i];
1279 pack->path_packfile = strdup(path_packfile);
1280 if (pack->path_packfile == NULL) {
1281 err = got_error_from_errno("strdup");
1282 goto done;
1285 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1286 if (err)
1287 goto done;
1289 if (fstat(pack->fd, &sb) != 0) {
1290 err = got_error_from_errno("fstat");
1291 goto done;
1293 pack->filesize = sb.st_size;
1295 pack->privsep_child = NULL;
1297 #ifndef GOT_PACK_NO_MMAP
1298 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1299 pack->fd, 0);
1300 if (pack->map == MAP_FAILED) {
1301 if (errno != ENOMEM) {
1302 err = got_error_from_errno("mmap");
1303 goto done;
1305 pack->map = NULL; /* fall back to read(2) */
1307 #endif
1308 done:
1309 if (err) {
1310 if (pack) {
1311 free(pack->path_packfile);
1312 memset(pack, 0, sizeof(*pack));
1314 } else if (packp)
1315 *packp = pack;
1316 return err;
1319 struct got_pack *
1320 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1322 struct got_pack *pack = NULL;
1323 size_t i;
1325 for (i = 0; i < repo->pack_cache_size; i++) {
1326 pack = &repo->packs[i];
1327 if (pack->path_packfile == NULL)
1328 break;
1329 if (strcmp(pack->path_packfile, path_packfile) == 0)
1330 return pack;
1333 return NULL;
1336 const struct got_error *
1337 got_repo_init(const char *repo_path)
1339 const struct got_error *err = NULL;
1340 const char *dirnames[] = {
1341 GOT_OBJECTS_DIR,
1342 GOT_OBJECTS_PACK_DIR,
1343 GOT_REFS_DIR,
1345 const char *description_str = "Unnamed repository; "
1346 "edit this file 'description' to name the repository.";
1347 const char *headref_str = "ref: refs/heads/main";
1348 const char *gitconfig_str = "[core]\n"
1349 "\trepositoryformatversion = 0\n"
1350 "\tfilemode = true\n"
1351 "\tbare = true\n";
1352 char *path;
1353 size_t i;
1355 if (!got_path_dir_is_empty(repo_path))
1356 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1358 for (i = 0; i < nitems(dirnames); i++) {
1359 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1360 return got_error_from_errno("asprintf");
1362 err = got_path_mkdir(path);
1363 free(path);
1364 if (err)
1365 return err;
1368 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1369 return got_error_from_errno("asprintf");
1370 err = got_path_create_file(path, description_str);
1371 free(path);
1372 if (err)
1373 return err;
1375 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1376 return got_error_from_errno("asprintf");
1377 err = got_path_create_file(path, headref_str);
1378 free(path);
1379 if (err)
1380 return err;
1382 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1383 return got_error_from_errno("asprintf");
1384 err = got_path_create_file(path, gitconfig_str);
1385 free(path);
1386 if (err)
1387 return err;
1389 return NULL;
1392 static const struct got_error *
1393 match_packed_object(struct got_object_id **unique_id,
1394 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1396 const struct got_error *err = NULL;
1397 DIR *packdir = NULL;
1398 struct dirent *dent;
1399 char *path_packidx;
1400 struct got_object_id_queue matched_ids;
1401 int packdir_fd;
1403 STAILQ_INIT(&matched_ids);
1405 packdir_fd = openat(got_repo_get_fd(repo),
1406 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1407 if (packdir_fd == -1) {
1408 if (errno != ENOENT)
1409 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1410 goto done;
1413 packdir = fdopendir(packdir_fd);
1414 if (packdir == NULL) {
1415 err = got_error_from_errno("fdopendir");
1416 goto done;
1419 while ((dent = readdir(packdir)) != NULL) {
1420 struct got_packidx *packidx;
1421 struct got_object_qid *qid;
1423 if (!got_repo_is_packidx_filename(dent->d_name,
1424 strlen(dent->d_name)))
1425 continue;
1427 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1428 dent->d_name) == -1) {
1429 err = got_error_from_errno("strdup");
1430 break;
1433 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1434 path_packidx, 0);
1435 free(path_packidx);
1436 if (err)
1437 break;
1439 err = got_packidx_match_id_str_prefix(&matched_ids,
1440 packidx, id_str_prefix);
1441 if (err) {
1442 got_packidx_close(packidx);
1443 break;
1445 err = got_packidx_close(packidx);
1446 if (err)
1447 break;
1449 STAILQ_FOREACH(qid, &matched_ids, entry) {
1450 if (obj_type != GOT_OBJ_TYPE_ANY) {
1451 int matched_type;
1452 err = got_object_get_type(&matched_type, repo,
1453 qid->id);
1454 if (err)
1455 goto done;
1456 if (matched_type != obj_type)
1457 continue;
1459 if (*unique_id == NULL) {
1460 *unique_id = got_object_id_dup(qid->id);
1461 if (*unique_id == NULL) {
1462 err = got_error_from_errno("malloc");
1463 goto done;
1465 } else {
1466 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1467 continue; /* packed multiple times */
1468 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1469 goto done;
1473 done:
1474 got_object_id_queue_free(&matched_ids);
1475 if (packdir && closedir(packdir) != 0 && err == NULL)
1476 err = got_error_from_errno("closedir");
1477 if (err) {
1478 free(*unique_id);
1479 *unique_id = NULL;
1481 return err;
1484 static const struct got_error *
1485 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1486 const char *object_dir, const char *id_str_prefix, int obj_type,
1487 struct got_repository *repo)
1489 const struct got_error *err = NULL;
1490 char *path;
1491 DIR *dir = NULL;
1492 struct dirent *dent;
1493 struct got_object_id id;
1495 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1496 err = got_error_from_errno("asprintf");
1497 goto done;
1500 dir = opendir(path);
1501 if (dir == NULL) {
1502 if (errno == ENOENT) {
1503 err = NULL;
1504 goto done;
1506 err = got_error_from_errno2("opendir", path);
1507 goto done;
1509 while ((dent = readdir(dir)) != NULL) {
1510 char *id_str;
1511 int cmp;
1513 if (strcmp(dent->d_name, ".") == 0 ||
1514 strcmp(dent->d_name, "..") == 0)
1515 continue;
1517 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1518 err = got_error_from_errno("asprintf");
1519 goto done;
1522 if (!got_parse_sha1_digest(id.sha1, id_str))
1523 continue;
1526 * Directory entries do not necessarily appear in
1527 * sorted order, so we must iterate over all of them.
1529 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1530 if (cmp != 0) {
1531 free(id_str);
1532 continue;
1535 if (*unique_id == NULL) {
1536 if (obj_type != GOT_OBJ_TYPE_ANY) {
1537 int matched_type;
1538 err = got_object_get_type(&matched_type, repo,
1539 &id);
1540 if (err)
1541 goto done;
1542 if (matched_type != obj_type)
1543 continue;
1545 *unique_id = got_object_id_dup(&id);
1546 if (*unique_id == NULL) {
1547 err = got_error_from_errno("got_object_id_dup");
1548 free(id_str);
1549 goto done;
1551 } else {
1552 if (got_object_id_cmp(*unique_id, &id) == 0)
1553 continue; /* both packed and loose */
1554 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1555 free(id_str);
1556 goto done;
1559 done:
1560 if (dir && closedir(dir) != 0 && err == NULL)
1561 err = got_error_from_errno("closedir");
1562 if (err) {
1563 free(*unique_id);
1564 *unique_id = NULL;
1566 free(path);
1567 return err;
1570 const struct got_error *
1571 got_repo_match_object_id_prefix(struct got_object_id **id,
1572 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 char *path_objects = got_repo_get_path_objects(repo);
1576 char *object_dir = NULL;
1577 size_t len;
1578 int i;
1580 *id = NULL;
1582 for (i = 0; i < strlen(id_str_prefix); i++) {
1583 if (isxdigit((unsigned char)id_str_prefix[i]))
1584 continue;
1585 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1588 len = strlen(id_str_prefix);
1589 if (len >= 2) {
1590 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1591 if (err)
1592 goto done;
1593 object_dir = strndup(id_str_prefix, 2);
1594 if (object_dir == NULL) {
1595 err = got_error_from_errno("strdup");
1596 goto done;
1598 err = match_loose_object(id, path_objects, object_dir,
1599 id_str_prefix, obj_type, repo);
1600 } else if (len == 1) {
1601 int i;
1602 for (i = 0; i < 0xf; i++) {
1603 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1604 == -1) {
1605 err = got_error_from_errno("asprintf");
1606 goto done;
1608 err = match_packed_object(id, repo, object_dir,
1609 obj_type);
1610 if (err)
1611 goto done;
1612 err = match_loose_object(id, path_objects, object_dir,
1613 id_str_prefix, obj_type, repo);
1614 if (err)
1615 goto done;
1617 } else {
1618 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1619 goto done;
1621 done:
1622 free(object_dir);
1623 if (err) {
1624 free(*id);
1625 *id = NULL;
1626 } else if (*id == NULL) {
1627 switch (obj_type) {
1628 case GOT_OBJ_TYPE_BLOB:
1629 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1630 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1631 break;
1632 case GOT_OBJ_TYPE_TREE:
1633 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1634 GOT_OBJ_LABEL_TREE, id_str_prefix);
1635 break;
1636 case GOT_OBJ_TYPE_COMMIT:
1637 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1638 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1639 break;
1640 case GOT_OBJ_TYPE_TAG:
1641 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1642 GOT_OBJ_LABEL_TAG, id_str_prefix);
1643 break;
1644 default:
1645 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1646 break;
1650 return err;
1653 const struct got_error *
1654 got_repo_match_object_id(struct got_object_id **id, char **label,
1655 const char *id_str, int obj_type, struct got_reflist_head *refs,
1656 struct got_repository *repo)
1658 const struct got_error *err;
1659 struct got_tag_object *tag;
1660 struct got_reference *ref = NULL;
1662 *id = NULL;
1663 if (label)
1664 *label = NULL;
1666 if (refs) {
1667 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1668 refs, repo);
1669 if (err == NULL) {
1670 *id = got_object_id_dup(
1671 got_object_tag_get_object_id(tag));
1672 if (*id == NULL)
1673 err = got_error_from_errno("got_object_id_dup");
1674 else if (label && asprintf(label, "refs/tags/%s",
1675 got_object_tag_get_name(tag)) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 free(*id);
1678 *id = NULL;
1680 got_object_tag_close(tag);
1681 return err;
1682 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1683 err->code != GOT_ERR_NO_OBJ)
1684 return err;
1687 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1688 if (err) {
1689 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1690 return err;
1691 err = got_ref_open(&ref, repo, id_str, 0);
1692 if (err != NULL)
1693 goto done;
1694 if (label) {
1695 *label = strdup(got_ref_get_name(ref));
1696 if (*label == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1701 err = got_ref_resolve(id, repo, ref);
1702 } else if (label) {
1703 err = got_object_id_str(label, *id);
1704 if (*label == NULL) {
1705 err = got_error_from_errno("strdup");
1706 goto done;
1709 done:
1710 if (ref)
1711 got_ref_close(ref);
1712 return err;
1715 const struct got_error *
1716 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1717 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1719 const struct got_error *err = NULL;
1720 struct got_reflist_entry *re;
1721 struct got_object_id *tag_id;
1722 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1724 *tag = NULL;
1726 TAILQ_FOREACH(re, refs, entry) {
1727 const char *refname;
1728 refname = got_ref_get_name(re->ref);
1729 if (got_ref_is_symbolic(re->ref))
1730 continue;
1731 if (strncmp(refname, "refs/tags/", 10) != 0)
1732 continue;
1733 if (!name_is_absolute)
1734 refname += strlen("refs/tags/");
1735 if (strcmp(refname, name) != 0)
1736 continue;
1737 err = got_ref_resolve(&tag_id, repo, re->ref);
1738 if (err)
1739 break;
1740 err = got_object_open_as_tag(tag, repo, tag_id);
1741 free(tag_id);
1742 if (err)
1743 break;
1744 if (obj_type == GOT_OBJ_TYPE_ANY ||
1745 got_object_tag_get_object_type(*tag) == obj_type)
1746 break;
1747 got_object_tag_close(*tag);
1748 *tag = NULL;
1751 if (err == NULL && *tag == NULL)
1752 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1753 GOT_OBJ_LABEL_TAG, name);
1754 return err;
1757 static const struct got_error *
1758 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1759 const char *name, mode_t mode, struct got_object_id *blob_id)
1761 const struct got_error *err = NULL;
1763 *new_te = NULL;
1765 *new_te = calloc(1, sizeof(**new_te));
1766 if (*new_te == NULL)
1767 return got_error_from_errno("calloc");
1769 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1770 sizeof((*new_te)->name)) {
1771 err = got_error(GOT_ERR_NO_SPACE);
1772 goto done;
1775 if (S_ISLNK(mode)) {
1776 (*new_te)->mode = S_IFLNK;
1777 } else {
1778 (*new_te)->mode = S_IFREG;
1779 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1781 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1782 done:
1783 if (err && *new_te) {
1784 free(*new_te);
1785 *new_te = NULL;
1787 return err;
1790 static const struct got_error *
1791 import_file(struct got_tree_entry **new_te, struct dirent *de,
1792 const char *path, struct got_repository *repo)
1794 const struct got_error *err;
1795 struct got_object_id *blob_id = NULL;
1796 char *filepath;
1797 struct stat sb;
1799 if (asprintf(&filepath, "%s%s%s", path,
1800 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1801 return got_error_from_errno("asprintf");
1803 if (lstat(filepath, &sb) != 0) {
1804 err = got_error_from_errno2("lstat", path);
1805 goto done;
1808 err = got_object_blob_create(&blob_id, filepath, repo);
1809 if (err)
1810 goto done;
1812 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1813 blob_id);
1814 done:
1815 free(filepath);
1816 if (err)
1817 free(blob_id);
1818 return err;
1821 static const struct got_error *
1822 insert_tree_entry(struct got_tree_entry *new_te,
1823 struct got_pathlist_head *paths)
1825 const struct got_error *err = NULL;
1826 struct got_pathlist_entry *new_pe;
1828 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1829 if (err)
1830 return err;
1831 if (new_pe == NULL)
1832 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1833 return NULL;
1836 static const struct got_error *write_tree(struct got_object_id **,
1837 const char *, struct got_pathlist_head *, struct got_repository *,
1838 got_repo_import_cb progress_cb, void *progress_arg);
1840 static const struct got_error *
1841 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1842 const char *path, struct got_pathlist_head *ignores,
1843 struct got_repository *repo,
1844 got_repo_import_cb progress_cb, void *progress_arg)
1846 const struct got_error *err;
1847 struct got_object_id *id = NULL;
1848 char *subdirpath;
1850 if (asprintf(&subdirpath, "%s%s%s", path,
1851 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1852 return got_error_from_errno("asprintf");
1854 (*new_te) = calloc(1, sizeof(**new_te));
1855 if (*new_te == NULL)
1856 return got_error_from_errno("calloc");
1857 (*new_te)->mode = S_IFDIR;
1858 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1859 sizeof((*new_te)->name)) {
1860 err = got_error(GOT_ERR_NO_SPACE);
1861 goto done;
1863 err = write_tree(&id, subdirpath, ignores, repo,
1864 progress_cb, progress_arg);
1865 if (err)
1866 goto done;
1867 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1869 done:
1870 free(id);
1871 free(subdirpath);
1872 if (err) {
1873 free(*new_te);
1874 *new_te = NULL;
1876 return err;
1879 static const struct got_error *
1880 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1881 struct got_pathlist_head *ignores, struct got_repository *repo,
1882 got_repo_import_cb progress_cb, void *progress_arg)
1884 const struct got_error *err = NULL;
1885 DIR *dir;
1886 struct dirent *de;
1887 int nentries;
1888 struct got_tree_entry *new_te = NULL;
1889 struct got_pathlist_head paths;
1890 struct got_pathlist_entry *pe;
1892 *new_tree_id = NULL;
1894 TAILQ_INIT(&paths);
1896 dir = opendir(path_dir);
1897 if (dir == NULL) {
1898 err = got_error_from_errno2("opendir", path_dir);
1899 goto done;
1902 nentries = 0;
1903 while ((de = readdir(dir)) != NULL) {
1904 int ignore = 0;
1905 int type;
1907 if (strcmp(de->d_name, ".") == 0 ||
1908 strcmp(de->d_name, "..") == 0)
1909 continue;
1911 TAILQ_FOREACH(pe, ignores, entry) {
1912 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1913 ignore = 1;
1914 break;
1917 if (ignore)
1918 continue;
1920 err = got_path_dirent_type(&type, path_dir, de);
1921 if (err)
1922 goto done;
1924 if (type == DT_DIR) {
1925 err = import_subdir(&new_te, de, path_dir,
1926 ignores, repo, progress_cb, progress_arg);
1927 if (err) {
1928 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1929 goto done;
1930 err = NULL;
1931 continue;
1933 } else if (type == DT_REG || type == DT_LNK) {
1934 err = import_file(&new_te, de, path_dir, repo);
1935 if (err)
1936 goto done;
1937 } else
1938 continue;
1940 err = insert_tree_entry(new_te, &paths);
1941 if (err)
1942 goto done;
1943 nentries++;
1946 if (TAILQ_EMPTY(&paths)) {
1947 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1948 "cannot create tree without any entries");
1949 goto done;
1952 TAILQ_FOREACH(pe, &paths, entry) {
1953 struct got_tree_entry *te = pe->data;
1954 char *path;
1955 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1956 continue;
1957 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1958 err = got_error_from_errno("asprintf");
1959 goto done;
1961 err = (*progress_cb)(progress_arg, path);
1962 free(path);
1963 if (err)
1964 goto done;
1967 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1968 done:
1969 if (dir)
1970 closedir(dir);
1971 got_pathlist_free(&paths);
1972 return err;
1975 const struct got_error *
1976 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1977 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1978 struct got_repository *repo, got_repo_import_cb progress_cb,
1979 void *progress_arg)
1981 const struct got_error *err;
1982 struct got_object_id *new_tree_id;
1984 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1985 progress_cb, progress_arg);
1986 if (err)
1987 return err;
1989 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1990 author, time(NULL), author, time(NULL), logmsg, repo);
1991 free(new_tree_id);
1992 return err;
1995 const struct got_error *
1996 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1997 struct got_repository *repo)
1999 const struct got_error *err = NULL;
2000 char *path_objects = NULL, *path = NULL;
2001 DIR *dir = NULL;
2002 struct got_object_id id;
2003 int i;
2005 *nobjects = 0;
2006 *ondisk_size = 0;
2008 path_objects = got_repo_get_path_objects(repo);
2009 if (path_objects == NULL)
2010 return got_error_from_errno("got_repo_get_path_objects");
2012 for (i = 0; i <= 0xff; i++) {
2013 struct dirent *dent;
2015 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2016 err = got_error_from_errno("asprintf");
2017 break;
2020 dir = opendir(path);
2021 if (dir == NULL) {
2022 if (errno == ENOENT) {
2023 err = NULL;
2024 continue;
2026 err = got_error_from_errno2("opendir", path);
2027 break;
2030 while ((dent = readdir(dir)) != NULL) {
2031 char *id_str;
2032 int fd;
2033 struct stat sb;
2035 if (strcmp(dent->d_name, ".") == 0 ||
2036 strcmp(dent->d_name, "..") == 0)
2037 continue;
2039 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2040 err = got_error_from_errno("asprintf");
2041 goto done;
2044 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2045 free(id_str);
2046 continue;
2048 free(id_str);
2050 err = got_object_open_loose_fd(&fd, &id, repo);
2051 if (err)
2052 goto done;
2054 if (fstat(fd, &sb) == -1) {
2055 err = got_error_from_errno("fstat");
2056 close(fd);
2057 goto done;
2059 (*nobjects)++;
2060 (*ondisk_size) += sb.st_size;
2062 if (close(fd) == -1) {
2063 err = got_error_from_errno("close");
2064 goto done;
2068 if (closedir(dir) != 0) {
2069 err = got_error_from_errno("closedir");
2070 goto done;
2072 dir = NULL;
2074 free(path);
2075 path = NULL;
2077 done:
2078 if (dir && closedir(dir) != 0 && err == NULL)
2079 err = got_error_from_errno("closedir");
2081 if (err) {
2082 *nobjects = 0;
2083 *ondisk_size = 0;
2085 free(path_objects);
2086 free(path);
2087 return err;
2090 const struct got_error *
2091 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2092 off_t *total_packsize, struct got_repository *repo)
2094 const struct got_error *err = NULL;
2095 DIR *packdir = NULL;
2096 struct dirent *dent;
2097 struct got_packidx *packidx = NULL;
2098 char *path_packidx;
2099 char *path_packfile;
2100 int packdir_fd;
2101 struct stat sb;
2103 *npackfiles = 0;
2104 *nobjects = 0;
2105 *total_packsize = 0;
2107 packdir_fd = openat(got_repo_get_fd(repo),
2108 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2109 if (packdir_fd == -1) {
2110 return got_error_from_errno_fmt("openat: %s/%s",
2111 got_repo_get_path_git_dir(repo),
2112 GOT_OBJECTS_PACK_DIR);
2115 packdir = fdopendir(packdir_fd);
2116 if (packdir == NULL) {
2117 err = got_error_from_errno("fdopendir");
2118 goto done;
2121 while ((dent = readdir(packdir)) != NULL) {
2122 if (!got_repo_is_packidx_filename(dent->d_name,
2123 strlen(dent->d_name)))
2124 continue;
2126 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2127 dent->d_name) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2132 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2133 path_packidx, 0);
2134 free(path_packidx);
2135 if (err)
2136 goto done;
2138 if (fstat(packidx->fd, &sb) == -1)
2139 goto done;
2140 *total_packsize += sb.st_size;
2142 err = got_packidx_get_packfile_path(&path_packfile,
2143 packidx->path_packidx);
2144 if (err)
2145 goto done;
2147 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2148 0) == -1) {
2149 free(path_packfile);
2150 goto done;
2152 free(path_packfile);
2153 *total_packsize += sb.st_size;
2155 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2157 (*npackfiles)++;
2159 got_packidx_close(packidx);
2160 packidx = NULL;
2162 done:
2163 if (packidx)
2164 got_packidx_close(packidx);
2165 if (packdir && closedir(packdir) != 0 && err == NULL)
2166 err = got_error_from_errno("closedir");
2167 if (err) {
2168 *npackfiles = 0;
2169 *nobjects = 0;
2170 *total_packsize = 0;
2172 return err;
2175 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2176 got_packidx_bloom_filter_cmp);