Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/resource.h>
26 #include <ctype.h>
27 #include <endian.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <limits.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sha1.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <imsg.h>
43 #include <uuid.h>
45 #include "bloom.h"
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #endif
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_get_fd(struct got_repository *repo)
90 {
91 return repo->gitdir_fd;
92 }
94 const char *
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
96 {
97 return repo->gitconfig_author_name;
98 }
100 const char *
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
106 const char *
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
112 const char *
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
118 const char *
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
124 void
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
132 int
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
138 static char *
139 get_path_git_child(struct got_repository *repo, const char *basename)
141 char *path_child;
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 basename) == -1)
145 return NULL;
147 return path_child;
150 char *
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
156 char *
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
162 char *
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
168 char *
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 static char *
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
180 char *
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
186 char *
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
198 void
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
206 static int
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
213 int ret = 0;
214 struct stat sb;
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_objects, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_refs, &sb) == -1)
228 goto done;
229 if (!S_ISDIR(sb.st_mode))
230 goto done;
232 if (lstat(path_head, &sb) == -1)
233 goto done;
234 if (!S_ISREG(sb.st_mode))
235 goto done;
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
239 goto done;
240 got_ref_close(head_ref);
242 ret = 1;
243 done:
244 free(path_objects);
245 free(path_refs);
246 free(path_head);
247 return ret;
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_error_from_errno("got_opentempfd");
265 got_repo_pack_fds_close(pack_fds_tmp);
266 return err;
269 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
270 return err;
273 const struct got_error *
274 got_repo_pack_fds_close(int *pack_fds)
276 const struct got_error *err = NULL;
277 int i;
279 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
280 if (pack_fds[i] == -1)
281 continue;
282 if (close(pack_fds[i]) == -1) {
283 err = got_error_from_errno("close");
284 break;
287 free(pack_fds);
288 pack_fds = NULL;
289 return err;
292 const struct got_error *
293 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
294 struct got_object *obj)
296 #ifndef GOT_NO_OBJ_CACHE
297 const struct got_error *err = NULL;
298 err = got_object_cache_add(&repo->objcache, id, obj);
299 if (err) {
300 if (err->code == GOT_ERR_OBJ_EXISTS ||
301 err->code == GOT_ERR_OBJ_TOO_LARGE)
302 err = NULL;
303 return err;
305 obj->refcnt++;
306 #endif
307 return NULL;
310 struct got_object *
311 got_repo_get_cached_object(struct got_repository *repo,
312 struct got_object_id *id)
314 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
317 const struct got_error *
318 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
319 struct got_tree_object *tree)
321 #ifndef GOT_NO_OBJ_CACHE
322 const struct got_error *err = NULL;
323 err = got_object_cache_add(&repo->treecache, id, tree);
324 if (err) {
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 err = NULL;
328 return err;
330 tree->refcnt++;
331 #endif
332 return NULL;
335 struct got_tree_object *
336 got_repo_get_cached_tree(struct got_repository *repo,
337 struct got_object_id *id)
339 return (struct got_tree_object *)got_object_cache_get(
340 &repo->treecache, id);
343 const struct got_error *
344 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
345 struct got_commit_object *commit)
347 #ifndef GOT_NO_OBJ_CACHE
348 const struct got_error *err = NULL;
349 err = got_object_cache_add(&repo->commitcache, id, commit);
350 if (err) {
351 if (err->code == GOT_ERR_OBJ_EXISTS ||
352 err->code == GOT_ERR_OBJ_TOO_LARGE)
353 err = NULL;
354 return err;
356 commit->refcnt++;
357 #endif
358 return NULL;
361 struct got_commit_object *
362 got_repo_get_cached_commit(struct got_repository *repo,
363 struct got_object_id *id)
365 return (struct got_commit_object *)got_object_cache_get(
366 &repo->commitcache, id);
369 const struct got_error *
370 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
371 struct got_tag_object *tag)
373 #ifndef GOT_NO_OBJ_CACHE
374 const struct got_error *err = NULL;
375 err = got_object_cache_add(&repo->tagcache, id, tag);
376 if (err) {
377 if (err->code == GOT_ERR_OBJ_EXISTS ||
378 err->code == GOT_ERR_OBJ_TOO_LARGE)
379 err = NULL;
380 return err;
382 tag->refcnt++;
383 #endif
384 return NULL;
387 struct got_tag_object *
388 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
390 return (struct got_tag_object *)got_object_cache_get(
391 &repo->tagcache, id);
394 const struct got_error *
395 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
396 struct got_raw_object *raw)
398 #ifndef GOT_NO_OBJ_CACHE
399 const struct got_error *err = NULL;
400 err = got_object_cache_add(&repo->rawcache, id, raw);
401 if (err) {
402 if (err->code == GOT_ERR_OBJ_EXISTS ||
403 err->code == GOT_ERR_OBJ_TOO_LARGE)
404 err = NULL;
405 return err;
407 raw->refcnt++;
408 #endif
409 return NULL;
413 struct got_raw_object *
414 got_repo_get_cached_raw_object(struct got_repository *repo,
415 struct got_object_id *id)
417 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
421 static const struct got_error *
422 open_repo(struct got_repository *repo, const char *path)
424 const struct got_error *err = NULL;
426 repo->gitdir_fd = -1;
428 /* bare git repository? */
429 repo->path_git_dir = strdup(path);
430 if (repo->path_git_dir == NULL)
431 return got_error_from_errno("strdup");
432 if (is_git_repo(repo)) {
433 repo->path = strdup(repo->path_git_dir);
434 if (repo->path == NULL) {
435 err = got_error_from_errno("strdup");
436 goto done;
438 repo->gitdir_fd = open(repo->path_git_dir,
439 O_DIRECTORY | O_CLOEXEC);
440 if (repo->gitdir_fd == -1) {
441 err = got_error_from_errno2("open",
442 repo->path_git_dir);
443 goto done;
445 return NULL;
448 /* git repository with working tree? */
449 free(repo->path_git_dir);
450 repo->path_git_dir = NULL;
451 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
452 err = got_error_from_errno("asprintf");
453 goto done;
455 if (is_git_repo(repo)) {
456 repo->path = strdup(path);
457 if (repo->path == NULL) {
458 err = got_error_from_errno("strdup");
459 goto done;
461 repo->gitdir_fd = open(repo->path_git_dir,
462 O_DIRECTORY | O_CLOEXEC);
463 if (repo->gitdir_fd == -1) {
464 err = got_error_from_errno2("open",
465 repo->path_git_dir);
466 goto done;
468 return NULL;
471 err = got_error(GOT_ERR_NOT_GIT_REPO);
472 done:
473 if (err) {
474 free(repo->path);
475 repo->path = NULL;
476 free(repo->path_git_dir);
477 repo->path_git_dir = NULL;
478 if (repo->gitdir_fd != -1)
479 close(repo->gitdir_fd);
480 repo->gitdir_fd = -1;
483 return err;
486 static const struct got_error *
487 parse_gitconfig_file(int *gitconfig_repository_format_version,
488 char **gitconfig_author_name, char **gitconfig_author_email,
489 struct got_remote_repo **remotes, int *nremotes,
490 char **gitconfig_owner, char ***extensions, int *nextensions,
491 const char *gitconfig_path)
493 const struct got_error *err = NULL, *child_err = NULL;
494 int fd = -1;
495 int imsg_fds[2] = { -1, -1 };
496 pid_t pid;
497 struct imsgbuf *ibuf;
499 *gitconfig_repository_format_version = 0;
500 if (extensions)
501 *extensions = NULL;
502 if (nextensions)
503 *nextensions = 0;
504 *gitconfig_author_name = NULL;
505 *gitconfig_author_email = NULL;
506 if (remotes)
507 *remotes = NULL;
508 if (nremotes)
509 *nremotes = 0;
510 if (gitconfig_owner)
511 *gitconfig_owner = NULL;
513 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
514 if (fd == -1) {
515 if (errno == ENOENT)
516 return NULL;
517 return got_error_from_errno2("open", gitconfig_path);
520 ibuf = calloc(1, sizeof(*ibuf));
521 if (ibuf == NULL) {
522 err = got_error_from_errno("calloc");
523 goto done;
526 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
527 err = got_error_from_errno("socketpair");
528 goto done;
531 pid = fork();
532 if (pid == -1) {
533 err = got_error_from_errno("fork");
534 goto done;
535 } else if (pid == 0) {
536 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
537 gitconfig_path);
538 /* not reached */
541 if (close(imsg_fds[1]) == -1) {
542 err = got_error_from_errno("close");
543 goto done;
545 imsg_fds[1] = -1;
546 imsg_init(ibuf, imsg_fds[0]);
548 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
549 if (err)
550 goto done;
551 fd = -1;
553 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
554 if (err)
555 goto done;
557 err = got_privsep_recv_gitconfig_int(
558 gitconfig_repository_format_version, ibuf);
559 if (err)
560 goto done;
562 if (extensions && nextensions) {
563 err = got_privsep_send_gitconfig_repository_extensions_req(
564 ibuf);
565 if (err)
566 goto done;
567 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
568 if (err)
569 goto done;
570 if (*nextensions > 0) {
571 int i;
572 *extensions = calloc(*nextensions, sizeof(char *));
573 if (*extensions == NULL) {
574 err = got_error_from_errno("calloc");
575 goto done;
577 for (i = 0; i < *nextensions; i++) {
578 char *ext;
579 err = got_privsep_recv_gitconfig_str(&ext,
580 ibuf);
581 if (err)
582 goto done;
583 (*extensions)[i] = ext;
588 err = got_privsep_send_gitconfig_author_name_req(ibuf);
589 if (err)
590 goto done;
592 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
593 if (err)
594 goto done;
596 err = got_privsep_send_gitconfig_author_email_req(ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
601 if (err)
602 goto done;
604 if (remotes && nremotes) {
605 err = got_privsep_send_gitconfig_remotes_req(ibuf);
606 if (err)
607 goto done;
609 err = got_privsep_recv_gitconfig_remotes(remotes,
610 nremotes, ibuf);
611 if (err)
612 goto done;
615 if (gitconfig_owner) {
616 err = got_privsep_send_gitconfig_owner_req(ibuf);
617 if (err)
618 goto done;
619 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
620 if (err)
621 goto done;
624 err = got_privsep_send_stop(imsg_fds[0]);
625 child_err = got_privsep_wait_for_child(pid);
626 if (child_err && err == NULL)
627 err = child_err;
628 done:
629 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
630 err = got_error_from_errno("close");
631 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
632 err = got_error_from_errno("close");
633 if (fd != -1 && close(fd) == -1 && err == NULL)
634 err = got_error_from_errno2("close", gitconfig_path);
635 free(ibuf);
636 return err;
639 static const struct got_error *
640 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
642 const struct got_error *err = NULL;
643 char *repo_gitconfig_path = NULL;
645 if (global_gitconfig_path) {
646 /* Read settings from ~/.gitconfig. */
647 int dummy_repo_version;
648 err = parse_gitconfig_file(&dummy_repo_version,
649 &repo->global_gitconfig_author_name,
650 &repo->global_gitconfig_author_email,
651 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
652 if (err)
653 return err;
656 /* Read repository's .git/config file. */
657 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
658 if (repo_gitconfig_path == NULL)
659 return got_error_from_errno("got_repo_get_path_gitconfig");
661 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
662 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
663 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
664 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
665 repo_gitconfig_path);
666 if (err)
667 goto done;
668 done:
669 free(repo_gitconfig_path);
670 return err;
673 static const struct got_error *
674 read_gotconfig(struct got_repository *repo)
676 const struct got_error *err = NULL;
677 char *gotconfig_path;
679 gotconfig_path = got_repo_get_path_gotconfig(repo);
680 if (gotconfig_path == NULL)
681 return got_error_from_errno("got_repo_get_path_gotconfig");
683 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
684 free(gotconfig_path);
685 return err;
688 /* Supported repository format extensions. */
689 static const char *const repo_extensions[] = {
690 "noop", /* Got supports repository format version 1. */
691 "preciousObjects", /* Supported by gotadmin cleanup. */
692 "worktreeConfig", /* Got does not care about Git work trees. */
693 };
695 const struct got_error *
696 got_repo_open(struct got_repository **repop, const char *path,
697 const char *global_gitconfig_path, int *pack_fds)
699 struct got_repository *repo = NULL;
700 const struct got_error *err = NULL;
701 char *repo_path = NULL;
702 size_t i, j = 0;
703 struct rlimit rl;
705 *repop = NULL;
707 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
708 return got_error_from_errno("getrlimit");
710 repo = calloc(1, sizeof(*repo));
711 if (repo == NULL)
712 return got_error_from_errno("calloc");
714 RB_INIT(&repo->packidx_bloom_filters);
715 TAILQ_INIT(&repo->packidx_paths);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 memset(&repo->privsep_children[i], 0,
719 sizeof(repo->privsep_children[0]));
720 repo->privsep_children[i].imsg_fd = -1;
723 err = got_object_cache_init(&repo->objcache,
724 GOT_OBJECT_CACHE_TYPE_OBJ);
725 if (err)
726 goto done;
727 err = got_object_cache_init(&repo->treecache,
728 GOT_OBJECT_CACHE_TYPE_TREE);
729 if (err)
730 goto done;
731 err = got_object_cache_init(&repo->commitcache,
732 GOT_OBJECT_CACHE_TYPE_COMMIT);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->tagcache,
736 GOT_OBJECT_CACHE_TYPE_TAG);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->rawcache,
740 GOT_OBJECT_CACHE_TYPE_RAW);
741 if (err)
742 goto done;
744 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
745 if (repo->pack_cache_size > rl.rlim_cur / 8)
746 repo->pack_cache_size = rl.rlim_cur / 8;
747 for (i = 0; i < nitems(repo->packs); i++) {
748 if (i < repo->pack_cache_size) {
749 repo->packs[i].basefd = pack_fds[j++];
750 repo->packs[i].accumfd = pack_fds[j++];
751 } else {
752 repo->packs[i].basefd = -1;
753 repo->packs[i].accumfd = -1;
757 repo_path = realpath(path, NULL);
758 if (repo_path == NULL) {
759 err = got_error_from_errno2("realpath", path);
760 goto done;
763 for (;;) {
764 char *parent_path;
766 err = open_repo(repo, repo_path);
767 if (err == NULL)
768 break;
769 if (err->code != GOT_ERR_NOT_GIT_REPO)
770 goto done;
771 if (repo_path[0] == '/' && repo_path[1] == '\0') {
772 err = got_error(GOT_ERR_NOT_GIT_REPO);
773 goto done;
775 err = got_path_dirname(&parent_path, repo_path);
776 if (err)
777 goto done;
778 free(repo_path);
779 repo_path = parent_path;
782 err = read_gotconfig(repo);
783 if (err)
784 goto done;
786 err = read_gitconfig(repo, global_gitconfig_path);
787 if (err)
788 goto done;
789 if (repo->gitconfig_repository_format_version != 0)
790 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
791 for (i = 0; i < repo->nextensions; i++) {
792 char *ext = repo->extensions[i];
793 int j, supported = 0;
794 for (j = 0; j < nitems(repo_extensions); j++) {
795 if (strcmp(ext, repo_extensions[j]) == 0) {
796 supported = 1;
797 break;
800 if (!supported) {
801 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
802 goto done;
806 err = got_repo_list_packidx(&repo->packidx_paths, repo);
807 done:
808 if (err)
809 got_repo_close(repo);
810 else
811 *repop = repo;
812 free(repo_path);
813 return err;
816 const struct got_error *
817 got_repo_close(struct got_repository *repo)
819 const struct got_error *err = NULL, *child_err;
820 struct got_packidx_bloom_filter *bf;
821 struct got_pathlist_entry *pe;
822 size_t i;
824 for (i = 0; i < repo->pack_cache_size; i++) {
825 if (repo->packidx_cache[i] == NULL)
826 break;
827 got_packidx_close(repo->packidx_cache[i]);
830 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
831 &repo->packidx_bloom_filters))) {
832 RB_REMOVE(got_packidx_bloom_filter_tree,
833 &repo->packidx_bloom_filters, bf);
834 free(bf->bloom);
835 free(bf);
838 for (i = 0; i < repo->pack_cache_size; i++)
839 if (repo->packs[i].path_packfile)
840 if (repo->packs[i].path_packfile)
841 got_pack_close(&repo->packs[i]);
843 free(repo->path);
844 free(repo->path_git_dir);
846 got_object_cache_close(&repo->objcache);
847 got_object_cache_close(&repo->treecache);
848 got_object_cache_close(&repo->commitcache);
849 got_object_cache_close(&repo->tagcache);
850 got_object_cache_close(&repo->rawcache);
852 for (i = 0; i < nitems(repo->privsep_children); i++) {
853 if (repo->privsep_children[i].imsg_fd == -1)
854 continue;
855 imsg_clear(repo->privsep_children[i].ibuf);
856 free(repo->privsep_children[i].ibuf);
857 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
858 child_err = got_privsep_wait_for_child(
859 repo->privsep_children[i].pid);
860 if (child_err && err == NULL)
861 err = child_err;
862 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
863 err == NULL)
864 err = got_error_from_errno("close");
867 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
868 err == NULL)
869 err = got_error_from_errno("close");
871 if (repo->gotconfig)
872 got_gotconfig_free(repo->gotconfig);
873 free(repo->gitconfig_author_name);
874 free(repo->gitconfig_author_email);
875 for (i = 0; i < repo->ngitconfig_remotes; i++)
876 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
877 free(repo->gitconfig_remotes);
878 for (i = 0; i < repo->nextensions; i++)
879 free(repo->extensions[i]);
880 free(repo->extensions);
882 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
883 free((void *)pe->path);
884 got_pathlist_free(&repo->packidx_paths);
885 free(repo);
887 return err;
890 void
891 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
893 int i;
895 free(repo->name);
896 repo->name = NULL;
897 free(repo->fetch_url);
898 repo->fetch_url = NULL;
899 free(repo->send_url);
900 repo->send_url = NULL;
901 for (i = 0; i < repo->nfetch_branches; i++)
902 free(repo->fetch_branches[i]);
903 free(repo->fetch_branches);
904 repo->fetch_branches = NULL;
905 repo->nfetch_branches = 0;
906 for (i = 0; i < repo->nsend_branches; i++)
907 free(repo->send_branches[i]);
908 free(repo->send_branches);
909 repo->send_branches = NULL;
910 repo->nsend_branches = 0;
913 const struct got_error *
914 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
915 const char *input_path)
917 const struct got_error *err = NULL;
918 const char *repo_abspath = NULL;
919 size_t repolen, len;
920 char *canonpath, *path = NULL;
922 *in_repo_path = NULL;
924 canonpath = strdup(input_path);
925 if (canonpath == NULL) {
926 err = got_error_from_errno("strdup");
927 goto done;
929 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
930 if (err)
931 goto done;
933 repo_abspath = got_repo_get_path(repo);
935 if (canonpath[0] == '\0') {
936 path = strdup(canonpath);
937 if (path == NULL) {
938 err = got_error_from_errno("strdup");
939 goto done;
941 } else {
942 path = realpath(canonpath, NULL);
943 if (path == NULL) {
944 if (errno != ENOENT) {
945 err = got_error_from_errno2("realpath",
946 canonpath);
947 goto done;
949 /*
950 * Path is not on disk.
951 * Assume it is already relative to repository root.
952 */
953 path = strdup(canonpath);
954 if (path == NULL) {
955 err = got_error_from_errno("strdup");
956 goto done;
960 repolen = strlen(repo_abspath);
961 len = strlen(path);
964 if (strcmp(path, repo_abspath) == 0) {
965 free(path);
966 path = strdup("");
967 if (path == NULL) {
968 err = got_error_from_errno("strdup");
969 goto done;
971 } else if (len > repolen &&
972 got_path_is_child(path, repo_abspath, repolen)) {
973 /* Matched an on-disk path inside repository. */
974 if (got_repo_is_bare(repo)) {
975 /*
976 * Matched an on-disk path inside repository
977 * database. Treat input as repository-relative.
978 */
979 free(path);
980 path = canonpath;
981 canonpath = NULL;
982 } else {
983 char *child;
984 /* Strip common prefix with repository path. */
985 err = got_path_skip_common_ancestor(&child,
986 repo_abspath, path);
987 if (err)
988 goto done;
989 free(path);
990 path = child;
992 } else {
993 /*
994 * Matched unrelated on-disk path.
995 * Treat input as repository-relative.
996 */
997 free(path);
998 path = canonpath;
999 canonpath = NULL;
1003 /* Make in-repository path absolute */
1004 if (path[0] != '/') {
1005 char *abspath;
1006 if (asprintf(&abspath, "/%s", path) == -1) {
1007 err = got_error_from_errno("asprintf");
1008 goto done;
1010 free(path);
1011 path = abspath;
1014 done:
1015 free(canonpath);
1016 if (err)
1017 free(path);
1018 else
1019 *in_repo_path = path;
1020 return err;
1023 static const struct got_error *
1024 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1025 const char *path_packidx)
1027 const struct got_error *err = NULL;
1028 size_t i;
1030 for (i = 0; i < repo->pack_cache_size; i++) {
1031 if (repo->packidx_cache[i] == NULL)
1032 break;
1033 if (strcmp(repo->packidx_cache[i]->path_packidx,
1034 path_packidx) == 0) {
1035 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1038 if (i == repo->pack_cache_size) {
1039 i = repo->pack_cache_size - 1;
1040 err = got_packidx_close(repo->packidx_cache[i]);
1041 if (err)
1042 return err;
1045 repo->packidx_cache[i] = packidx;
1047 return NULL;
1050 int
1051 got_repo_is_packidx_filename(const char *name, size_t len)
1053 if (len != GOT_PACKIDX_NAMELEN)
1054 return 0;
1056 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1057 return 0;
1059 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1060 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1061 return 0;
1063 return 1;
1066 static struct got_packidx_bloom_filter *
1067 get_packidx_bloom_filter(struct got_repository *repo,
1068 const char *path, size_t path_len)
1070 struct got_packidx_bloom_filter key;
1072 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1073 return NULL; /* XXX */
1074 key.path_len = path_len;
1076 return RB_FIND(got_packidx_bloom_filter_tree,
1077 &repo->packidx_bloom_filters, &key);
1080 int
1081 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path_packidx, struct got_object_id *id)
1084 struct got_packidx_bloom_filter *bf;
1086 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1087 if (bf)
1088 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1090 /* No bloom filter means this pack index must be searched. */
1091 return 1;
1094 static const struct got_error *
1095 add_packidx_bloom_filter(struct got_repository *repo,
1096 struct got_packidx *packidx, const char *path_packidx)
1098 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1099 struct got_packidx_bloom_filter *bf;
1100 size_t len;
1103 * Don't use bloom filters for very large pack index files.
1104 * Large pack files will contain a relatively large fraction
1105 * of our objects so we will likely need to visit them anyway.
1106 * The more objects a pack file contains the higher the probability
1107 * of a false-positive match from the bloom filter. And reading
1108 * all object IDs from a large pack index file can be expensive.
1110 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1111 return NULL;
1113 /* Do we already have a filter for this pack index? */
1114 if (get_packidx_bloom_filter(repo, path_packidx,
1115 strlen(path_packidx)) != NULL)
1116 return NULL;
1118 bf = calloc(1, sizeof(*bf));
1119 if (bf == NULL)
1120 return got_error_from_errno("calloc");
1121 bf->bloom = calloc(1, sizeof(*bf->bloom));
1122 if (bf->bloom == NULL) {
1123 free(bf);
1124 return got_error_from_errno("calloc");
1127 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1128 if (len >= sizeof(bf->path)) {
1129 free(bf->bloom);
1130 free(bf);
1131 return got_error(GOT_ERR_NO_SPACE);
1133 bf->path_len = len;
1135 /* Minimum size supported by our bloom filter is 1000 entries. */
1136 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1137 for (i = 0; i < nobjects; i++) {
1138 struct got_packidx_object_id *id;
1139 id = &packidx->hdr.sorted_ids[i];
1140 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1143 RB_INSERT(got_packidx_bloom_filter_tree,
1144 &repo->packidx_bloom_filters, bf);
1145 return NULL;
1148 const struct got_error *
1149 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1150 struct got_repository *repo, struct got_object_id *id)
1152 const struct got_error *err;
1153 struct got_pathlist_entry *pe;
1154 size_t i;
1156 /* Search pack index cache. */
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1159 break;
1160 if (!got_repo_check_packidx_bloom_filter(repo,
1161 repo->packidx_cache[i]->path_packidx, id))
1162 continue; /* object will not be found in this index */
1163 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1164 if (*idx != -1) {
1165 *packidx = repo->packidx_cache[i];
1167 * Move this cache entry to the front. Repeatedly
1168 * searching a wrong pack index can be expensive.
1170 if (i > 0) {
1171 memmove(&repo->packidx_cache[1],
1172 &repo->packidx_cache[0],
1173 i * sizeof(repo->packidx_cache[0]));
1174 repo->packidx_cache[0] = *packidx;
1176 return NULL;
1179 /* No luck. Search the filesystem. */
1181 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1182 const char *path_packidx = pe->path;
1183 int is_cached = 0;
1185 if (!got_repo_check_packidx_bloom_filter(repo,
1186 pe->path, id))
1187 continue; /* object will not be found in this index */
1189 for (i = 0; i < repo->pack_cache_size; i++) {
1190 if (repo->packidx_cache[i] == NULL)
1191 break;
1192 if (strcmp(repo->packidx_cache[i]->path_packidx,
1193 path_packidx) == 0) {
1194 is_cached = 1;
1195 break;
1198 if (is_cached)
1199 continue; /* already searched */
1201 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1202 path_packidx, 0);
1203 if (err)
1204 goto done;
1206 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1207 if (err)
1208 goto done;
1210 err = cache_packidx(repo, *packidx, path_packidx);
1211 if (err)
1212 goto done;
1214 *idx = got_packidx_get_object_idx(*packidx, id);
1215 if (*idx != -1) {
1216 err = NULL; /* found the object */
1217 goto done;
1221 err = got_error_no_obj(id);
1222 done:
1223 return err;
1226 const struct got_error *
1227 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1228 struct got_repository *repo)
1230 const struct got_error *err = NULL;
1231 DIR *packdir = NULL;
1232 struct dirent *dent;
1233 char *path_packidx = NULL;
1234 int packdir_fd;
1236 packdir_fd = openat(got_repo_get_fd(repo),
1237 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1238 if (packdir_fd == -1) {
1239 return got_error_from_errno_fmt("openat: %s/%s",
1240 got_repo_get_path_git_dir(repo),
1241 GOT_OBJECTS_PACK_DIR);
1244 packdir = fdopendir(packdir_fd);
1245 if (packdir == NULL) {
1246 err = got_error_from_errno("fdopendir");
1247 goto done;
1250 while ((dent = readdir(packdir)) != NULL) {
1251 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1252 continue;
1254 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1255 dent->d_name) == -1) {
1256 err = got_error_from_errno("asprintf");
1257 path_packidx = NULL;
1258 break;
1261 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1262 if (err)
1263 break;
1265 done:
1266 if (err)
1267 free(path_packidx);
1268 if (packdir && closedir(packdir) != 0 && err == NULL)
1269 err = got_error_from_errno("closedir");
1270 return err;
1273 const struct got_error *
1274 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1275 struct got_repository *repo)
1277 const struct got_error *err;
1278 size_t i;
1280 *packidx = NULL;
1282 /* Search pack index cache. */
1283 for (i = 0; i < repo->pack_cache_size; i++) {
1284 if (repo->packidx_cache[i] == NULL)
1285 break;
1286 if (strcmp(repo->packidx_cache[i]->path_packidx,
1287 path_packidx) == 0) {
1288 *packidx = repo->packidx_cache[i];
1289 return NULL;
1292 /* No luck. Search the filesystem. */
1294 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1295 path_packidx, 0);
1296 if (err)
1297 return err;
1299 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1300 if (err)
1301 goto done;
1303 err = cache_packidx(repo, *packidx, path_packidx);
1304 done:
1305 if (err) {
1306 got_packidx_close(*packidx);
1307 *packidx = NULL;
1309 return err;
1312 static const struct got_error *
1313 read_packfile_hdr(int fd, struct got_packidx *packidx)
1315 const struct got_error *err = NULL;
1316 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1317 struct got_packfile_hdr hdr;
1318 ssize_t n;
1320 n = read(fd, &hdr, sizeof(hdr));
1321 if (n < 0)
1322 return got_error_from_errno("read");
1323 if (n != sizeof(hdr))
1324 return got_error(GOT_ERR_BAD_PACKFILE);
1326 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1327 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1328 be32toh(hdr.nobjects) != totobj)
1329 err = got_error(GOT_ERR_BAD_PACKFILE);
1331 return err;
1334 static const struct got_error *
1335 open_packfile(int *fd, struct got_repository *repo,
1336 const char *relpath, struct got_packidx *packidx)
1338 const struct got_error *err = NULL;
1340 *fd = openat(got_repo_get_fd(repo), relpath,
1341 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1342 if (*fd == -1)
1343 return got_error_from_errno_fmt("openat: %s/%s",
1344 got_repo_get_path_git_dir(repo), relpath);
1346 if (packidx) {
1347 err = read_packfile_hdr(*fd, packidx);
1348 if (err) {
1349 close(*fd);
1350 *fd = -1;
1354 return err;
1357 const struct got_error *
1358 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1359 const char *path_packfile, struct got_packidx *packidx)
1361 const struct got_error *err = NULL;
1362 struct got_pack *pack = NULL;
1363 struct stat sb;
1364 size_t i;
1366 if (packp)
1367 *packp = NULL;
1369 for (i = 0; i < repo->pack_cache_size; i++) {
1370 pack = &repo->packs[i];
1371 if (pack->path_packfile == NULL)
1372 break;
1373 if (strcmp(pack->path_packfile, path_packfile) == 0)
1374 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1377 if (i == repo->pack_cache_size) {
1378 struct got_pack tmp;
1379 err = got_pack_close(&repo->packs[i - 1]);
1380 if (err)
1381 return err;
1382 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1383 return got_error_from_errno("ftruncate");
1384 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1385 return got_error_from_errno("ftruncate");
1386 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1387 memcpy(&repo->packs[i - 1], &repo->packs[0],
1388 sizeof(repo->packs[i - 1]));
1389 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1390 i = 0;
1393 pack = &repo->packs[i];
1395 pack->path_packfile = strdup(path_packfile);
1396 if (pack->path_packfile == NULL) {
1397 err = got_error_from_errno("strdup");
1398 goto done;
1401 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1402 if (err)
1403 goto done;
1405 if (fstat(pack->fd, &sb) != 0) {
1406 err = got_error_from_errno("fstat");
1407 goto done;
1409 pack->filesize = sb.st_size;
1411 pack->privsep_child = NULL;
1413 #ifndef GOT_PACK_NO_MMAP
1414 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1415 pack->fd, 0);
1416 if (pack->map == MAP_FAILED) {
1417 if (errno != ENOMEM) {
1418 err = got_error_from_errno("mmap");
1419 goto done;
1421 pack->map = NULL; /* fall back to read(2) */
1423 #endif
1424 done:
1425 if (err) {
1426 if (pack) {
1427 free(pack->path_packfile);
1428 memset(pack, 0, sizeof(*pack));
1430 } else if (packp)
1431 *packp = pack;
1432 return err;
1435 struct got_pack *
1436 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1438 struct got_pack *pack = NULL;
1439 size_t i;
1441 for (i = 0; i < repo->pack_cache_size; i++) {
1442 pack = &repo->packs[i];
1443 if (pack->path_packfile == NULL)
1444 break;
1445 if (strcmp(pack->path_packfile, path_packfile) == 0)
1446 return pack;
1449 return NULL;
1452 const struct got_error *
1453 got_repo_init(const char *repo_path)
1455 const struct got_error *err = NULL;
1456 const char *dirnames[] = {
1457 GOT_OBJECTS_DIR,
1458 GOT_OBJECTS_PACK_DIR,
1459 GOT_REFS_DIR,
1461 const char *description_str = "Unnamed repository; "
1462 "edit this file 'description' to name the repository.";
1463 const char *headref_str = "ref: refs/heads/main";
1464 const char *gitconfig_str = "[core]\n"
1465 "\trepositoryformatversion = 0\n"
1466 "\tfilemode = true\n"
1467 "\tbare = true\n";
1468 char *path;
1469 size_t i;
1471 if (!got_path_dir_is_empty(repo_path))
1472 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1474 for (i = 0; i < nitems(dirnames); i++) {
1475 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1476 return got_error_from_errno("asprintf");
1478 err = got_path_mkdir(path);
1479 free(path);
1480 if (err)
1481 return err;
1484 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1485 return got_error_from_errno("asprintf");
1486 err = got_path_create_file(path, description_str);
1487 free(path);
1488 if (err)
1489 return err;
1491 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1492 return got_error_from_errno("asprintf");
1493 err = got_path_create_file(path, headref_str);
1494 free(path);
1495 if (err)
1496 return err;
1498 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1499 return got_error_from_errno("asprintf");
1500 err = got_path_create_file(path, gitconfig_str);
1501 free(path);
1502 if (err)
1503 return err;
1505 return NULL;
1508 static const struct got_error *
1509 match_packed_object(struct got_object_id **unique_id,
1510 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1512 const struct got_error *err = NULL;
1513 struct got_object_id_queue matched_ids;
1514 struct got_pathlist_entry *pe;
1516 STAILQ_INIT(&matched_ids);
1518 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1519 const char *path_packidx = pe->path;
1520 struct got_packidx *packidx;
1521 struct got_object_qid *qid;
1523 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1524 path_packidx, 0);
1525 if (err)
1526 break;
1528 err = got_packidx_match_id_str_prefix(&matched_ids,
1529 packidx, id_str_prefix);
1530 if (err) {
1531 got_packidx_close(packidx);
1532 break;
1534 err = got_packidx_close(packidx);
1535 if (err)
1536 break;
1538 STAILQ_FOREACH(qid, &matched_ids, entry) {
1539 if (obj_type != GOT_OBJ_TYPE_ANY) {
1540 int matched_type;
1541 err = got_object_get_type(&matched_type, repo,
1542 &qid->id);
1543 if (err)
1544 goto done;
1545 if (matched_type != obj_type)
1546 continue;
1548 if (*unique_id == NULL) {
1549 *unique_id = got_object_id_dup(&qid->id);
1550 if (*unique_id == NULL) {
1551 err = got_error_from_errno("malloc");
1552 goto done;
1554 } else {
1555 if (got_object_id_cmp(*unique_id,
1556 &qid->id) == 0)
1557 continue; /* packed multiple times */
1558 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1559 goto done;
1563 done:
1564 got_object_id_queue_free(&matched_ids);
1565 if (err) {
1566 free(*unique_id);
1567 *unique_id = NULL;
1569 return err;
1572 static const struct got_error *
1573 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1574 const char *object_dir, const char *id_str_prefix, int obj_type,
1575 struct got_repository *repo)
1577 const struct got_error *err = NULL;
1578 char *path;
1579 DIR *dir = NULL;
1580 struct dirent *dent;
1581 struct got_object_id id;
1583 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1584 err = got_error_from_errno("asprintf");
1585 goto done;
1588 dir = opendir(path);
1589 if (dir == NULL) {
1590 if (errno == ENOENT) {
1591 err = NULL;
1592 goto done;
1594 err = got_error_from_errno2("opendir", path);
1595 goto done;
1597 while ((dent = readdir(dir)) != NULL) {
1598 char *id_str;
1599 int cmp;
1601 if (strcmp(dent->d_name, ".") == 0 ||
1602 strcmp(dent->d_name, "..") == 0)
1603 continue;
1605 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1606 err = got_error_from_errno("asprintf");
1607 goto done;
1610 if (!got_parse_sha1_digest(id.sha1, id_str))
1611 continue;
1614 * Directory entries do not necessarily appear in
1615 * sorted order, so we must iterate over all of them.
1617 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1618 if (cmp != 0) {
1619 free(id_str);
1620 continue;
1623 if (*unique_id == NULL) {
1624 if (obj_type != GOT_OBJ_TYPE_ANY) {
1625 int matched_type;
1626 err = got_object_get_type(&matched_type, repo,
1627 &id);
1628 if (err)
1629 goto done;
1630 if (matched_type != obj_type)
1631 continue;
1633 *unique_id = got_object_id_dup(&id);
1634 if (*unique_id == NULL) {
1635 err = got_error_from_errno("got_object_id_dup");
1636 free(id_str);
1637 goto done;
1639 } else {
1640 if (got_object_id_cmp(*unique_id, &id) == 0)
1641 continue; /* both packed and loose */
1642 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1643 free(id_str);
1644 goto done;
1647 done:
1648 if (dir && closedir(dir) != 0 && err == NULL)
1649 err = got_error_from_errno("closedir");
1650 if (err) {
1651 free(*unique_id);
1652 *unique_id = NULL;
1654 free(path);
1655 return err;
1658 const struct got_error *
1659 got_repo_match_object_id_prefix(struct got_object_id **id,
1660 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1662 const struct got_error *err = NULL;
1663 char *path_objects = got_repo_get_path_objects(repo);
1664 char *object_dir = NULL;
1665 size_t len;
1666 int i;
1668 *id = NULL;
1670 len = strlen(id_str_prefix);
1671 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1672 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1674 for (i = 0; i < len; i++) {
1675 if (isxdigit((unsigned char)id_str_prefix[i]))
1676 continue;
1677 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1680 if (len >= 2) {
1681 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1682 if (err)
1683 goto done;
1684 object_dir = strndup(id_str_prefix, 2);
1685 if (object_dir == NULL) {
1686 err = got_error_from_errno("strdup");
1687 goto done;
1689 err = match_loose_object(id, path_objects, object_dir,
1690 id_str_prefix, obj_type, repo);
1691 } else if (len == 1) {
1692 int i;
1693 for (i = 0; i < 0xf; i++) {
1694 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1695 == -1) {
1696 err = got_error_from_errno("asprintf");
1697 goto done;
1699 err = match_packed_object(id, repo, object_dir,
1700 obj_type);
1701 if (err)
1702 goto done;
1703 err = match_loose_object(id, path_objects, object_dir,
1704 id_str_prefix, obj_type, repo);
1705 if (err)
1706 goto done;
1708 } else {
1709 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1710 goto done;
1712 done:
1713 free(object_dir);
1714 if (err) {
1715 free(*id);
1716 *id = NULL;
1717 } else if (*id == NULL) {
1718 switch (obj_type) {
1719 case GOT_OBJ_TYPE_BLOB:
1720 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1721 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1722 break;
1723 case GOT_OBJ_TYPE_TREE:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_TREE, id_str_prefix);
1726 break;
1727 case GOT_OBJ_TYPE_COMMIT:
1728 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1729 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1730 break;
1731 case GOT_OBJ_TYPE_TAG:
1732 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1733 GOT_OBJ_LABEL_TAG, id_str_prefix);
1734 break;
1735 default:
1736 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1737 break;
1741 return err;
1744 const struct got_error *
1745 got_repo_match_object_id(struct got_object_id **id, char **label,
1746 const char *id_str, int obj_type, struct got_reflist_head *refs,
1747 struct got_repository *repo)
1749 const struct got_error *err;
1750 struct got_tag_object *tag;
1751 struct got_reference *ref = NULL;
1753 *id = NULL;
1754 if (label)
1755 *label = NULL;
1757 if (refs) {
1758 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1759 refs, repo);
1760 if (err == NULL) {
1761 *id = got_object_id_dup(
1762 got_object_tag_get_object_id(tag));
1763 if (*id == NULL)
1764 err = got_error_from_errno("got_object_id_dup");
1765 else if (label && asprintf(label, "refs/tags/%s",
1766 got_object_tag_get_name(tag)) == -1) {
1767 err = got_error_from_errno("asprintf");
1768 free(*id);
1769 *id = NULL;
1771 got_object_tag_close(tag);
1772 return err;
1773 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1774 err->code != GOT_ERR_NO_OBJ)
1775 return err;
1778 err = got_ref_open(&ref, repo, id_str, 0);
1779 if (err == NULL) {
1780 err = got_ref_resolve(id, repo, ref);
1781 if (err)
1782 goto done;
1783 if (label) {
1784 *label = strdup(got_ref_get_name(ref));
1785 if (*label == NULL) {
1786 err = got_error_from_errno("strdup");
1787 goto done;
1790 } else {
1791 if (err->code != GOT_ERR_NOT_REF &&
1792 err->code != GOT_ERR_BAD_REF_NAME)
1793 goto done;
1794 err = got_repo_match_object_id_prefix(id, id_str,
1795 obj_type, repo);
1796 if (err) {
1797 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1798 err = got_error_not_ref(id_str);
1799 goto done;
1801 if (label) {
1802 err = got_object_id_str(label, *id);
1803 if (*label == NULL) {
1804 err = got_error_from_errno("strdup");
1805 goto done;
1809 done:
1810 if (ref)
1811 got_ref_close(ref);
1812 return err;
1815 const struct got_error *
1816 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1817 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1819 const struct got_error *err = NULL;
1820 struct got_reflist_entry *re;
1821 struct got_object_id *tag_id;
1822 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1824 *tag = NULL;
1826 TAILQ_FOREACH(re, refs, entry) {
1827 const char *refname;
1828 refname = got_ref_get_name(re->ref);
1829 if (got_ref_is_symbolic(re->ref))
1830 continue;
1831 if (strncmp(refname, "refs/tags/", 10) != 0)
1832 continue;
1833 if (!name_is_absolute)
1834 refname += strlen("refs/tags/");
1835 if (strcmp(refname, name) != 0)
1836 continue;
1837 err = got_ref_resolve(&tag_id, repo, re->ref);
1838 if (err)
1839 break;
1840 err = got_object_open_as_tag(tag, repo, tag_id);
1841 free(tag_id);
1842 if (err)
1843 break;
1844 if (obj_type == GOT_OBJ_TYPE_ANY ||
1845 got_object_tag_get_object_type(*tag) == obj_type)
1846 break;
1847 got_object_tag_close(*tag);
1848 *tag = NULL;
1851 if (err == NULL && *tag == NULL)
1852 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1853 GOT_OBJ_LABEL_TAG, name);
1854 return err;
1857 static const struct got_error *
1858 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1859 const char *name, mode_t mode, struct got_object_id *blob_id)
1861 const struct got_error *err = NULL;
1863 *new_te = NULL;
1865 *new_te = calloc(1, sizeof(**new_te));
1866 if (*new_te == NULL)
1867 return got_error_from_errno("calloc");
1869 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1870 sizeof((*new_te)->name)) {
1871 err = got_error(GOT_ERR_NO_SPACE);
1872 goto done;
1875 if (S_ISLNK(mode)) {
1876 (*new_te)->mode = S_IFLNK;
1877 } else {
1878 (*new_te)->mode = S_IFREG;
1879 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1881 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1882 done:
1883 if (err && *new_te) {
1884 free(*new_te);
1885 *new_te = NULL;
1887 return err;
1890 static const struct got_error *
1891 import_file(struct got_tree_entry **new_te, struct dirent *de,
1892 const char *path, struct got_repository *repo)
1894 const struct got_error *err;
1895 struct got_object_id *blob_id = NULL;
1896 char *filepath;
1897 struct stat sb;
1899 if (asprintf(&filepath, "%s%s%s", path,
1900 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1901 return got_error_from_errno("asprintf");
1903 if (lstat(filepath, &sb) != 0) {
1904 err = got_error_from_errno2("lstat", path);
1905 goto done;
1908 err = got_object_blob_create(&blob_id, filepath, repo);
1909 if (err)
1910 goto done;
1912 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1913 blob_id);
1914 done:
1915 free(filepath);
1916 if (err)
1917 free(blob_id);
1918 return err;
1921 static const struct got_error *
1922 insert_tree_entry(struct got_tree_entry *new_te,
1923 struct got_pathlist_head *paths)
1925 const struct got_error *err = NULL;
1926 struct got_pathlist_entry *new_pe;
1928 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1929 if (err)
1930 return err;
1931 if (new_pe == NULL)
1932 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1933 return NULL;
1936 static const struct got_error *write_tree(struct got_object_id **,
1937 const char *, struct got_pathlist_head *, struct got_repository *,
1938 got_repo_import_cb progress_cb, void *progress_arg);
1940 static const struct got_error *
1941 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1942 const char *path, struct got_pathlist_head *ignores,
1943 struct got_repository *repo,
1944 got_repo_import_cb progress_cb, void *progress_arg)
1946 const struct got_error *err;
1947 struct got_object_id *id = NULL;
1948 char *subdirpath;
1950 if (asprintf(&subdirpath, "%s%s%s", path,
1951 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1952 return got_error_from_errno("asprintf");
1954 (*new_te) = calloc(1, sizeof(**new_te));
1955 if (*new_te == NULL)
1956 return got_error_from_errno("calloc");
1957 (*new_te)->mode = S_IFDIR;
1958 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1959 sizeof((*new_te)->name)) {
1960 err = got_error(GOT_ERR_NO_SPACE);
1961 goto done;
1963 err = write_tree(&id, subdirpath, ignores, repo,
1964 progress_cb, progress_arg);
1965 if (err)
1966 goto done;
1967 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1969 done:
1970 free(id);
1971 free(subdirpath);
1972 if (err) {
1973 free(*new_te);
1974 *new_te = NULL;
1976 return err;
1979 static const struct got_error *
1980 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1981 struct got_pathlist_head *ignores, struct got_repository *repo,
1982 got_repo_import_cb progress_cb, void *progress_arg)
1984 const struct got_error *err = NULL;
1985 DIR *dir;
1986 struct dirent *de;
1987 int nentries;
1988 struct got_tree_entry *new_te = NULL;
1989 struct got_pathlist_head paths;
1990 struct got_pathlist_entry *pe;
1992 *new_tree_id = NULL;
1994 TAILQ_INIT(&paths);
1996 dir = opendir(path_dir);
1997 if (dir == NULL) {
1998 err = got_error_from_errno2("opendir", path_dir);
1999 goto done;
2002 nentries = 0;
2003 while ((de = readdir(dir)) != NULL) {
2004 int ignore = 0;
2005 int type;
2007 if (strcmp(de->d_name, ".") == 0 ||
2008 strcmp(de->d_name, "..") == 0)
2009 continue;
2011 TAILQ_FOREACH(pe, ignores, entry) {
2012 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2013 ignore = 1;
2014 break;
2017 if (ignore)
2018 continue;
2020 err = got_path_dirent_type(&type, path_dir, de);
2021 if (err)
2022 goto done;
2024 if (type == DT_DIR) {
2025 err = import_subdir(&new_te, de, path_dir,
2026 ignores, repo, progress_cb, progress_arg);
2027 if (err) {
2028 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2029 goto done;
2030 err = NULL;
2031 continue;
2033 } else if (type == DT_REG || type == DT_LNK) {
2034 err = import_file(&new_te, de, path_dir, repo);
2035 if (err)
2036 goto done;
2037 } else
2038 continue;
2040 err = insert_tree_entry(new_te, &paths);
2041 if (err)
2042 goto done;
2043 nentries++;
2046 if (TAILQ_EMPTY(&paths)) {
2047 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2048 "cannot create tree without any entries");
2049 goto done;
2052 TAILQ_FOREACH(pe, &paths, entry) {
2053 struct got_tree_entry *te = pe->data;
2054 char *path;
2055 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2056 continue;
2057 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2058 err = got_error_from_errno("asprintf");
2059 goto done;
2061 err = (*progress_cb)(progress_arg, path);
2062 free(path);
2063 if (err)
2064 goto done;
2067 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2068 done:
2069 if (dir)
2070 closedir(dir);
2071 got_pathlist_free(&paths);
2072 return err;
2075 const struct got_error *
2076 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2077 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2078 struct got_repository *repo, got_repo_import_cb progress_cb,
2079 void *progress_arg)
2081 const struct got_error *err;
2082 struct got_object_id *new_tree_id;
2084 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2085 progress_cb, progress_arg);
2086 if (err)
2087 return err;
2089 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2090 author, time(NULL), author, time(NULL), logmsg, repo);
2091 free(new_tree_id);
2092 return err;
2095 const struct got_error *
2096 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2097 struct got_repository *repo)
2099 const struct got_error *err = NULL;
2100 char *path_objects = NULL, *path = NULL;
2101 DIR *dir = NULL;
2102 struct got_object_id id;
2103 int i;
2105 *nobjects = 0;
2106 *ondisk_size = 0;
2108 path_objects = got_repo_get_path_objects(repo);
2109 if (path_objects == NULL)
2110 return got_error_from_errno("got_repo_get_path_objects");
2112 for (i = 0; i <= 0xff; i++) {
2113 struct dirent *dent;
2115 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2116 err = got_error_from_errno("asprintf");
2117 break;
2120 dir = opendir(path);
2121 if (dir == NULL) {
2122 if (errno == ENOENT) {
2123 err = NULL;
2124 continue;
2126 err = got_error_from_errno2("opendir", path);
2127 break;
2130 while ((dent = readdir(dir)) != NULL) {
2131 char *id_str;
2132 int fd;
2133 struct stat sb;
2135 if (strcmp(dent->d_name, ".") == 0 ||
2136 strcmp(dent->d_name, "..") == 0)
2137 continue;
2139 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2140 err = got_error_from_errno("asprintf");
2141 goto done;
2144 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2145 free(id_str);
2146 continue;
2148 free(id_str);
2150 err = got_object_open_loose_fd(&fd, &id, repo);
2151 if (err)
2152 goto done;
2154 if (fstat(fd, &sb) == -1) {
2155 err = got_error_from_errno("fstat");
2156 close(fd);
2157 goto done;
2159 (*nobjects)++;
2160 (*ondisk_size) += sb.st_size;
2162 if (close(fd) == -1) {
2163 err = got_error_from_errno("close");
2164 goto done;
2168 if (closedir(dir) != 0) {
2169 err = got_error_from_errno("closedir");
2170 goto done;
2172 dir = NULL;
2174 free(path);
2175 path = NULL;
2177 done:
2178 if (dir && closedir(dir) != 0 && err == NULL)
2179 err = got_error_from_errno("closedir");
2181 if (err) {
2182 *nobjects = 0;
2183 *ondisk_size = 0;
2185 free(path_objects);
2186 free(path);
2187 return err;
2190 const struct got_error *
2191 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2192 off_t *total_packsize, struct got_repository *repo)
2194 const struct got_error *err = NULL;
2195 DIR *packdir = NULL;
2196 struct dirent *dent;
2197 struct got_packidx *packidx = NULL;
2198 char *path_packidx;
2199 char *path_packfile;
2200 int packdir_fd;
2201 struct stat sb;
2203 *npackfiles = 0;
2204 *nobjects = 0;
2205 *total_packsize = 0;
2207 packdir_fd = openat(got_repo_get_fd(repo),
2208 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2209 if (packdir_fd == -1) {
2210 return got_error_from_errno_fmt("openat: %s/%s",
2211 got_repo_get_path_git_dir(repo),
2212 GOT_OBJECTS_PACK_DIR);
2215 packdir = fdopendir(packdir_fd);
2216 if (packdir == NULL) {
2217 err = got_error_from_errno("fdopendir");
2218 goto done;
2221 while ((dent = readdir(packdir)) != NULL) {
2222 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2223 continue;
2225 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2226 dent->d_name) == -1) {
2227 err = got_error_from_errno("asprintf");
2228 goto done;
2231 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2232 path_packidx, 0);
2233 free(path_packidx);
2234 if (err)
2235 goto done;
2237 if (fstat(packidx->fd, &sb) == -1)
2238 goto done;
2239 *total_packsize += sb.st_size;
2241 err = got_packidx_get_packfile_path(&path_packfile,
2242 packidx->path_packidx);
2243 if (err)
2244 goto done;
2246 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2247 0) == -1) {
2248 free(path_packfile);
2249 goto done;
2251 free(path_packfile);
2252 *total_packsize += sb.st_size;
2254 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2256 (*npackfiles)++;
2258 got_packidx_close(packidx);
2259 packidx = NULL;
2261 done:
2262 if (packidx)
2263 got_packidx_close(packidx);
2264 if (packdir && closedir(packdir) != 0 && err == NULL)
2265 err = got_error_from_errno("closedir");
2266 if (err) {
2267 *npackfiles = 0;
2268 *nobjects = 0;
2269 *total_packsize = 0;
2271 return err;
2274 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2275 got_packidx_bloom_filter_cmp);