Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/resource.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
39 #include "bloom.h"
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_object.h"
47 #include "got_opentemp.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
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_pack_fds_open(int **pack_fds)
248 const struct got_error *err = NULL;
249 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
251 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
252 if (*pack_fds == NULL)
253 return got_error_from_errno("calloc");
255 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
256 pack_fds_tmp[i] = got_opentempfd();
257 if (pack_fds_tmp[i] == -1) {
258 err = got_error_from_errno("got_opentempfd");
259 got_repo_pack_fds_close(pack_fds_tmp);
260 return err;
263 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
264 return err;
267 const struct got_error *
268 got_repo_pack_fds_close(int *pack_fds)
270 const struct got_error *err = NULL;
271 int i;
273 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
274 if (pack_fds[i] == -1)
275 continue;
276 if (close(pack_fds[i]) == -1) {
277 err = got_error_from_errno("close");
278 break;
281 free(pack_fds);
282 return err;
285 const struct got_error *
286 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
287 struct got_object *obj)
289 #ifndef GOT_NO_OBJ_CACHE
290 const struct got_error *err = NULL;
291 err = got_object_cache_add(&repo->objcache, id, obj);
292 if (err) {
293 if (err->code == GOT_ERR_OBJ_EXISTS ||
294 err->code == GOT_ERR_OBJ_TOO_LARGE)
295 err = NULL;
296 return err;
298 obj->refcnt++;
299 #endif
300 return NULL;
303 struct got_object *
304 got_repo_get_cached_object(struct got_repository *repo,
305 struct got_object_id *id)
307 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
310 const struct got_error *
311 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
312 struct got_tree_object *tree)
314 #ifndef GOT_NO_OBJ_CACHE
315 const struct got_error *err = NULL;
316 err = got_object_cache_add(&repo->treecache, id, tree);
317 if (err) {
318 if (err->code == GOT_ERR_OBJ_EXISTS ||
319 err->code == GOT_ERR_OBJ_TOO_LARGE)
320 err = NULL;
321 return err;
323 tree->refcnt++;
324 #endif
325 return NULL;
328 struct got_tree_object *
329 got_repo_get_cached_tree(struct got_repository *repo,
330 struct got_object_id *id)
332 return (struct got_tree_object *)got_object_cache_get(
333 &repo->treecache, id);
336 const struct got_error *
337 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
338 struct got_commit_object *commit)
340 #ifndef GOT_NO_OBJ_CACHE
341 const struct got_error *err = NULL;
342 err = got_object_cache_add(&repo->commitcache, id, commit);
343 if (err) {
344 if (err->code == GOT_ERR_OBJ_EXISTS ||
345 err->code == GOT_ERR_OBJ_TOO_LARGE)
346 err = NULL;
347 return err;
349 commit->refcnt++;
350 #endif
351 return NULL;
354 struct got_commit_object *
355 got_repo_get_cached_commit(struct got_repository *repo,
356 struct got_object_id *id)
358 return (struct got_commit_object *)got_object_cache_get(
359 &repo->commitcache, id);
362 const struct got_error *
363 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
364 struct got_tag_object *tag)
366 #ifndef GOT_NO_OBJ_CACHE
367 const struct got_error *err = NULL;
368 err = got_object_cache_add(&repo->tagcache, id, tag);
369 if (err) {
370 if (err->code == GOT_ERR_OBJ_EXISTS ||
371 err->code == GOT_ERR_OBJ_TOO_LARGE)
372 err = NULL;
373 return err;
375 tag->refcnt++;
376 #endif
377 return NULL;
380 struct got_tag_object *
381 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
383 return (struct got_tag_object *)got_object_cache_get(
384 &repo->tagcache, id);
387 const struct got_error *
388 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
389 struct got_raw_object *raw)
391 #ifndef GOT_NO_OBJ_CACHE
392 const struct got_error *err = NULL;
393 err = got_object_cache_add(&repo->rawcache, id, raw);
394 if (err) {
395 if (err->code == GOT_ERR_OBJ_EXISTS ||
396 err->code == GOT_ERR_OBJ_TOO_LARGE)
397 err = NULL;
398 return err;
400 raw->refcnt++;
401 #endif
402 return NULL;
406 struct got_raw_object *
407 got_repo_get_cached_raw_object(struct got_repository *repo,
408 struct got_object_id *id)
410 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
414 static const struct got_error *
415 open_repo(struct got_repository *repo, const char *path)
417 const struct got_error *err = NULL;
419 repo->gitdir_fd = -1;
421 /* bare git repository? */
422 repo->path_git_dir = strdup(path);
423 if (repo->path_git_dir == NULL)
424 return got_error_from_errno("strdup");
425 if (is_git_repo(repo)) {
426 repo->path = strdup(repo->path_git_dir);
427 if (repo->path == NULL) {
428 err = got_error_from_errno("strdup");
429 goto done;
431 repo->gitdir_fd = open(repo->path_git_dir,
432 O_DIRECTORY | O_CLOEXEC);
433 if (repo->gitdir_fd == -1) {
434 err = got_error_from_errno2("open",
435 repo->path_git_dir);
436 goto done;
438 return NULL;
441 /* git repository with working tree? */
442 free(repo->path_git_dir);
443 repo->path_git_dir = NULL;
444 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
445 err = got_error_from_errno("asprintf");
446 goto done;
448 if (is_git_repo(repo)) {
449 repo->path = strdup(path);
450 if (repo->path == NULL) {
451 err = got_error_from_errno("strdup");
452 goto done;
454 repo->gitdir_fd = open(repo->path_git_dir,
455 O_DIRECTORY | O_CLOEXEC);
456 if (repo->gitdir_fd == -1) {
457 err = got_error_from_errno2("open",
458 repo->path_git_dir);
459 goto done;
461 return NULL;
464 err = got_error(GOT_ERR_NOT_GIT_REPO);
465 done:
466 if (err) {
467 free(repo->path);
468 repo->path = NULL;
469 free(repo->path_git_dir);
470 repo->path_git_dir = NULL;
471 if (repo->gitdir_fd != -1)
472 close(repo->gitdir_fd);
473 repo->gitdir_fd = -1;
476 return err;
479 static const struct got_error *
480 parse_gitconfig_file(int *gitconfig_repository_format_version,
481 char **gitconfig_author_name, char **gitconfig_author_email,
482 struct got_remote_repo **remotes, int *nremotes,
483 char **gitconfig_owner, char ***extensions, int *nextensions,
484 const char *gitconfig_path)
486 const struct got_error *err = NULL, *child_err = NULL;
487 int fd = -1;
488 int imsg_fds[2] = { -1, -1 };
489 pid_t pid;
490 struct imsgbuf *ibuf;
492 *gitconfig_repository_format_version = 0;
493 if (extensions)
494 *extensions = NULL;
495 if (nextensions)
496 *nextensions = 0;
497 *gitconfig_author_name = NULL;
498 *gitconfig_author_email = NULL;
499 if (remotes)
500 *remotes = NULL;
501 if (nremotes)
502 *nremotes = 0;
503 if (gitconfig_owner)
504 *gitconfig_owner = NULL;
506 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
507 if (fd == -1) {
508 if (errno == ENOENT)
509 return NULL;
510 return got_error_from_errno2("open", gitconfig_path);
513 ibuf = calloc(1, sizeof(*ibuf));
514 if (ibuf == NULL) {
515 err = got_error_from_errno("calloc");
516 goto done;
519 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
520 err = got_error_from_errno("socketpair");
521 goto done;
524 pid = fork();
525 if (pid == -1) {
526 err = got_error_from_errno("fork");
527 goto done;
528 } else if (pid == 0) {
529 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
530 gitconfig_path);
531 /* not reached */
534 if (close(imsg_fds[1]) == -1) {
535 err = got_error_from_errno("close");
536 goto done;
538 imsg_fds[1] = -1;
539 imsg_init(ibuf, imsg_fds[0]);
541 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
542 if (err)
543 goto done;
544 fd = -1;
546 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
547 if (err)
548 goto done;
550 err = got_privsep_recv_gitconfig_int(
551 gitconfig_repository_format_version, ibuf);
552 if (err)
553 goto done;
555 if (extensions && nextensions) {
556 err = got_privsep_send_gitconfig_repository_extensions_req(
557 ibuf);
558 if (err)
559 goto done;
560 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
561 if (err)
562 goto done;
563 if (*nextensions > 0) {
564 int i;
565 *extensions = calloc(*nextensions, sizeof(char *));
566 if (*extensions == NULL) {
567 err = got_error_from_errno("calloc");
568 goto done;
570 for (i = 0; i < *nextensions; i++) {
571 char *ext;
572 err = got_privsep_recv_gitconfig_str(&ext,
573 ibuf);
574 if (err)
575 goto done;
576 (*extensions)[i] = ext;
581 err = got_privsep_send_gitconfig_author_name_req(ibuf);
582 if (err)
583 goto done;
585 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
586 if (err)
587 goto done;
589 err = got_privsep_send_gitconfig_author_email_req(ibuf);
590 if (err)
591 goto done;
593 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
594 if (err)
595 goto done;
597 if (remotes && nremotes) {
598 err = got_privsep_send_gitconfig_remotes_req(ibuf);
599 if (err)
600 goto done;
602 err = got_privsep_recv_gitconfig_remotes(remotes,
603 nremotes, ibuf);
604 if (err)
605 goto done;
608 if (gitconfig_owner) {
609 err = got_privsep_send_gitconfig_owner_req(ibuf);
610 if (err)
611 goto done;
612 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
613 if (err)
614 goto done;
617 err = got_privsep_send_stop(imsg_fds[0]);
618 child_err = got_privsep_wait_for_child(pid);
619 if (child_err && err == NULL)
620 err = child_err;
621 done:
622 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
623 err = got_error_from_errno("close");
624 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
625 err = got_error_from_errno("close");
626 if (fd != -1 && close(fd) == -1 && err == NULL)
627 err = got_error_from_errno2("close", gitconfig_path);
628 free(ibuf);
629 return err;
632 static const struct got_error *
633 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
635 const struct got_error *err = NULL;
636 char *repo_gitconfig_path = NULL;
638 if (global_gitconfig_path) {
639 /* Read settings from ~/.gitconfig. */
640 int dummy_repo_version;
641 err = parse_gitconfig_file(&dummy_repo_version,
642 &repo->global_gitconfig_author_name,
643 &repo->global_gitconfig_author_email,
644 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
645 if (err)
646 return err;
649 /* Read repository's .git/config file. */
650 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
651 if (repo_gitconfig_path == NULL)
652 return got_error_from_errno("got_repo_get_path_gitconfig");
654 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
655 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
656 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
657 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
658 repo_gitconfig_path);
659 if (err)
660 goto done;
661 done:
662 free(repo_gitconfig_path);
663 return err;
666 static const struct got_error *
667 read_gotconfig(struct got_repository *repo)
669 const struct got_error *err = NULL;
670 char *gotconfig_path;
672 gotconfig_path = got_repo_get_path_gotconfig(repo);
673 if (gotconfig_path == NULL)
674 return got_error_from_errno("got_repo_get_path_gotconfig");
676 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
677 free(gotconfig_path);
678 return err;
681 /* Supported repository format extensions. */
682 static const char *const repo_extensions[] = {
683 "noop", /* Got supports repository format version 1. */
684 "preciousObjects", /* Supported by gotadmin cleanup. */
685 "worktreeConfig", /* Got does not care about Git work trees. */
686 };
688 const struct got_error *
689 got_repo_open(struct got_repository **repop, const char *path,
690 const char *global_gitconfig_path, int *pack_fds)
692 struct got_repository *repo = NULL;
693 const struct got_error *err = NULL;
694 char *repo_path = NULL;
695 size_t i, j = 0;
696 struct rlimit rl;
698 *repop = NULL;
700 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
701 return got_error_from_errno("getrlimit");
703 repo = calloc(1, sizeof(*repo));
704 if (repo == NULL)
705 return got_error_from_errno("calloc");
707 RB_INIT(&repo->packidx_bloom_filters);
708 TAILQ_INIT(&repo->packidx_paths);
710 for (i = 0; i < nitems(repo->privsep_children); i++) {
711 memset(&repo->privsep_children[i], 0,
712 sizeof(repo->privsep_children[0]));
713 repo->privsep_children[i].imsg_fd = -1;
716 err = got_object_cache_init(&repo->objcache,
717 GOT_OBJECT_CACHE_TYPE_OBJ);
718 if (err)
719 goto done;
720 err = got_object_cache_init(&repo->treecache,
721 GOT_OBJECT_CACHE_TYPE_TREE);
722 if (err)
723 goto done;
724 err = got_object_cache_init(&repo->commitcache,
725 GOT_OBJECT_CACHE_TYPE_COMMIT);
726 if (err)
727 goto done;
728 err = got_object_cache_init(&repo->tagcache,
729 GOT_OBJECT_CACHE_TYPE_TAG);
730 if (err)
731 goto done;
732 err = got_object_cache_init(&repo->rawcache,
733 GOT_OBJECT_CACHE_TYPE_RAW);
734 if (err)
735 goto done;
737 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
738 if (repo->pack_cache_size > rl.rlim_cur / 8)
739 repo->pack_cache_size = rl.rlim_cur / 8;
740 for (i = 0; i < nitems(repo->packs); i++) {
741 if (i < repo->pack_cache_size) {
742 repo->packs[i].basefd = pack_fds[j++];
743 repo->packs[i].accumfd = pack_fds[j++];
744 } else {
745 repo->packs[i].basefd = -1;
746 repo->packs[i].accumfd = -1;
750 repo_path = realpath(path, NULL);
751 if (repo_path == NULL) {
752 err = got_error_from_errno2("realpath", path);
753 goto done;
756 for (;;) {
757 char *parent_path;
759 err = open_repo(repo, repo_path);
760 if (err == NULL)
761 break;
762 if (err->code != GOT_ERR_NOT_GIT_REPO)
763 goto done;
764 if (repo_path[0] == '/' && repo_path[1] == '\0') {
765 err = got_error(GOT_ERR_NOT_GIT_REPO);
766 goto done;
768 err = got_path_dirname(&parent_path, repo_path);
769 if (err)
770 goto done;
771 free(repo_path);
772 repo_path = parent_path;
775 err = read_gotconfig(repo);
776 if (err)
777 goto done;
779 err = read_gitconfig(repo, global_gitconfig_path);
780 if (err)
781 goto done;
782 if (repo->gitconfig_repository_format_version != 0)
783 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
784 for (i = 0; i < repo->nextensions; i++) {
785 char *ext = repo->extensions[i];
786 int j, supported = 0;
787 for (j = 0; j < nitems(repo_extensions); j++) {
788 if (strcmp(ext, repo_extensions[j]) == 0) {
789 supported = 1;
790 break;
793 if (!supported) {
794 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
795 goto done;
799 err = got_repo_list_packidx(&repo->packidx_paths, repo);
800 done:
801 if (err)
802 got_repo_close(repo);
803 else
804 *repop = repo;
805 free(repo_path);
806 return err;
809 const struct got_error *
810 got_repo_close(struct got_repository *repo)
812 const struct got_error *err = NULL, *child_err;
813 struct got_packidx_bloom_filter *bf;
814 struct got_pathlist_entry *pe;
815 size_t i;
817 for (i = 0; i < repo->pack_cache_size; i++) {
818 if (repo->packidx_cache[i] == NULL)
819 break;
820 got_packidx_close(repo->packidx_cache[i]);
823 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
824 &repo->packidx_bloom_filters))) {
825 RB_REMOVE(got_packidx_bloom_filter_tree,
826 &repo->packidx_bloom_filters, bf);
827 free(bf->bloom);
828 free(bf);
831 for (i = 0; i < repo->pack_cache_size; i++)
832 if (repo->packs[i].path_packfile)
833 if (repo->packs[i].path_packfile)
834 got_pack_close(&repo->packs[i]);
836 free(repo->path);
837 free(repo->path_git_dir);
839 got_object_cache_close(&repo->objcache);
840 got_object_cache_close(&repo->treecache);
841 got_object_cache_close(&repo->commitcache);
842 got_object_cache_close(&repo->tagcache);
843 got_object_cache_close(&repo->rawcache);
845 for (i = 0; i < nitems(repo->privsep_children); i++) {
846 if (repo->privsep_children[i].imsg_fd == -1)
847 continue;
848 imsg_clear(repo->privsep_children[i].ibuf);
849 free(repo->privsep_children[i].ibuf);
850 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
851 child_err = got_privsep_wait_for_child(
852 repo->privsep_children[i].pid);
853 if (child_err && err == NULL)
854 err = child_err;
855 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
856 err == NULL)
857 err = got_error_from_errno("close");
860 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
861 err == NULL)
862 err = got_error_from_errno("close");
864 if (repo->gotconfig)
865 got_gotconfig_free(repo->gotconfig);
866 free(repo->gitconfig_author_name);
867 free(repo->gitconfig_author_email);
868 for (i = 0; i < repo->ngitconfig_remotes; i++)
869 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
870 free(repo->gitconfig_remotes);
871 for (i = 0; i < repo->nextensions; i++)
872 free(repo->extensions[i]);
873 free(repo->extensions);
875 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
876 free((void *)pe->path);
877 got_pathlist_free(&repo->packidx_paths);
878 free(repo);
880 return err;
883 void
884 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
886 int i;
888 free(repo->name);
889 repo->name = NULL;
890 free(repo->fetch_url);
891 repo->fetch_url = NULL;
892 free(repo->send_url);
893 repo->send_url = NULL;
894 for (i = 0; i < repo->nfetch_branches; i++)
895 free(repo->fetch_branches[i]);
896 free(repo->fetch_branches);
897 repo->fetch_branches = NULL;
898 repo->nfetch_branches = 0;
899 for (i = 0; i < repo->nsend_branches; i++)
900 free(repo->send_branches[i]);
901 free(repo->send_branches);
902 repo->send_branches = NULL;
903 repo->nsend_branches = 0;
906 const struct got_error *
907 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
908 const char *input_path)
910 const struct got_error *err = NULL;
911 const char *repo_abspath = NULL;
912 size_t repolen, len;
913 char *canonpath, *path = NULL;
915 *in_repo_path = NULL;
917 canonpath = strdup(input_path);
918 if (canonpath == NULL) {
919 err = got_error_from_errno("strdup");
920 goto done;
922 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
923 if (err)
924 goto done;
926 repo_abspath = got_repo_get_path(repo);
928 if (canonpath[0] == '\0') {
929 path = strdup(canonpath);
930 if (path == NULL) {
931 err = got_error_from_errno("strdup");
932 goto done;
934 } else {
935 path = realpath(canonpath, NULL);
936 if (path == NULL) {
937 if (errno != ENOENT) {
938 err = got_error_from_errno2("realpath",
939 canonpath);
940 goto done;
942 /*
943 * Path is not on disk.
944 * Assume it is already relative to repository root.
945 */
946 path = strdup(canonpath);
947 if (path == NULL) {
948 err = got_error_from_errno("strdup");
949 goto done;
953 repolen = strlen(repo_abspath);
954 len = strlen(path);
957 if (strcmp(path, repo_abspath) == 0) {
958 free(path);
959 path = strdup("");
960 if (path == NULL) {
961 err = got_error_from_errno("strdup");
962 goto done;
964 } else if (len > repolen &&
965 got_path_is_child(path, repo_abspath, repolen)) {
966 /* Matched an on-disk path inside repository. */
967 if (got_repo_is_bare(repo)) {
968 /*
969 * Matched an on-disk path inside repository
970 * database. Treat input as repository-relative.
971 */
972 free(path);
973 path = canonpath;
974 canonpath = NULL;
975 } else {
976 char *child;
977 /* Strip common prefix with repository path. */
978 err = got_path_skip_common_ancestor(&child,
979 repo_abspath, path);
980 if (err)
981 goto done;
982 free(path);
983 path = child;
985 } else {
986 /*
987 * Matched unrelated on-disk path.
988 * Treat input as repository-relative.
989 */
990 free(path);
991 path = canonpath;
992 canonpath = NULL;
996 /* Make in-repository path absolute */
997 if (path[0] != '/') {
998 char *abspath;
999 if (asprintf(&abspath, "/%s", path) == -1) {
1000 err = got_error_from_errno("asprintf");
1001 goto done;
1003 free(path);
1004 path = abspath;
1007 done:
1008 free(canonpath);
1009 if (err)
1010 free(path);
1011 else
1012 *in_repo_path = path;
1013 return err;
1016 static const struct got_error *
1017 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1018 const char *path_packidx)
1020 const struct got_error *err = NULL;
1021 size_t i;
1023 for (i = 0; i < repo->pack_cache_size; i++) {
1024 if (repo->packidx_cache[i] == NULL)
1025 break;
1026 if (strcmp(repo->packidx_cache[i]->path_packidx,
1027 path_packidx) == 0) {
1028 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1031 if (i == repo->pack_cache_size) {
1032 i = repo->pack_cache_size - 1;
1033 err = got_packidx_close(repo->packidx_cache[i]);
1034 if (err)
1035 return err;
1038 repo->packidx_cache[i] = packidx;
1040 return NULL;
1043 int
1044 got_repo_is_packidx_filename(const char *name, size_t len)
1046 if (len != GOT_PACKIDX_NAMELEN)
1047 return 0;
1049 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1050 return 0;
1052 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1053 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1054 return 0;
1056 return 1;
1059 static struct got_packidx_bloom_filter *
1060 get_packidx_bloom_filter(struct got_repository *repo,
1061 const char *path, size_t path_len)
1063 struct got_packidx_bloom_filter key;
1065 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1066 return NULL; /* XXX */
1067 key.path_len = path_len;
1069 return RB_FIND(got_packidx_bloom_filter_tree,
1070 &repo->packidx_bloom_filters, &key);
1073 int
1074 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1075 const char *path_packidx, struct got_object_id *id)
1077 struct got_packidx_bloom_filter *bf;
1079 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1080 if (bf)
1081 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1083 /* No bloom filter means this pack index must be searched. */
1084 return 1;
1087 static const struct got_error *
1088 add_packidx_bloom_filter(struct got_repository *repo,
1089 struct got_packidx *packidx, const char *path_packidx)
1091 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1092 struct got_packidx_bloom_filter *bf;
1093 size_t len;
1096 * Don't use bloom filters for very large pack index files.
1097 * Large pack files will contain a relatively large fraction
1098 * of our objects so we will likely need to visit them anyway.
1099 * The more objects a pack file contains the higher the probability
1100 * of a false-positive match from the bloom filter. And reading
1101 * all object IDs from a large pack index file can be expensive.
1103 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1104 return NULL;
1106 /* Do we already have a filter for this pack index? */
1107 if (get_packidx_bloom_filter(repo, path_packidx,
1108 strlen(path_packidx)) != NULL)
1109 return NULL;
1111 bf = calloc(1, sizeof(*bf));
1112 if (bf == NULL)
1113 return got_error_from_errno("calloc");
1114 bf->bloom = calloc(1, sizeof(*bf->bloom));
1115 if (bf->bloom == NULL) {
1116 free(bf);
1117 return got_error_from_errno("calloc");
1120 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1121 if (len >= sizeof(bf->path)) {
1122 free(bf->bloom);
1123 free(bf);
1124 return got_error(GOT_ERR_NO_SPACE);
1126 bf->path_len = len;
1128 /* Minimum size supported by our bloom filter is 1000 entries. */
1129 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1130 for (i = 0; i < nobjects; i++) {
1131 struct got_packidx_object_id *id;
1132 id = &packidx->hdr.sorted_ids[i];
1133 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1136 RB_INSERT(got_packidx_bloom_filter_tree,
1137 &repo->packidx_bloom_filters, bf);
1138 return NULL;
1141 const struct got_error *
1142 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1143 struct got_repository *repo, struct got_object_id *id)
1145 const struct got_error *err;
1146 struct got_pathlist_entry *pe;
1147 size_t i;
1149 /* Search pack index cache. */
1150 for (i = 0; i < repo->pack_cache_size; i++) {
1151 if (repo->packidx_cache[i] == NULL)
1152 break;
1153 if (!got_repo_check_packidx_bloom_filter(repo,
1154 repo->packidx_cache[i]->path_packidx, id))
1155 continue; /* object will not be found in this index */
1156 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1157 if (*idx != -1) {
1158 *packidx = repo->packidx_cache[i];
1160 * Move this cache entry to the front. Repeatedly
1161 * searching a wrong pack index can be expensive.
1163 if (i > 0) {
1164 memmove(&repo->packidx_cache[1],
1165 &repo->packidx_cache[0],
1166 i * sizeof(repo->packidx_cache[0]));
1167 repo->packidx_cache[0] = *packidx;
1169 return NULL;
1172 /* No luck. Search the filesystem. */
1174 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1175 const char *path_packidx = pe->path;
1176 int is_cached = 0;
1178 if (!got_repo_check_packidx_bloom_filter(repo,
1179 pe->path, id))
1180 continue; /* object will not be found in this index */
1182 for (i = 0; i < repo->pack_cache_size; i++) {
1183 if (repo->packidx_cache[i] == NULL)
1184 break;
1185 if (strcmp(repo->packidx_cache[i]->path_packidx,
1186 path_packidx) == 0) {
1187 is_cached = 1;
1188 break;
1191 if (is_cached)
1192 continue; /* already searched */
1194 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1195 path_packidx, 0);
1196 if (err)
1197 goto done;
1199 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1200 if (err)
1201 goto done;
1203 err = cache_packidx(repo, *packidx, path_packidx);
1204 if (err)
1205 goto done;
1207 *idx = got_packidx_get_object_idx(*packidx, id);
1208 if (*idx != -1) {
1209 err = NULL; /* found the object */
1210 goto done;
1214 err = got_error_no_obj(id);
1215 done:
1216 return err;
1219 const struct got_error *
1220 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 DIR *packdir = NULL;
1225 struct dirent *dent;
1226 char *path_packidx = NULL;
1227 int packdir_fd;
1229 packdir_fd = openat(got_repo_get_fd(repo),
1230 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1231 if (packdir_fd == -1) {
1232 return got_error_from_errno_fmt("openat: %s/%s",
1233 got_repo_get_path_git_dir(repo),
1234 GOT_OBJECTS_PACK_DIR);
1237 packdir = fdopendir(packdir_fd);
1238 if (packdir == NULL) {
1239 err = got_error_from_errno("fdopendir");
1240 goto done;
1243 while ((dent = readdir(packdir)) != NULL) {
1244 if (!got_repo_is_packidx_filename(dent->d_name,
1245 strlen(dent->d_name)))
1246 continue;
1248 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1249 dent->d_name) == -1) {
1250 err = got_error_from_errno("asprintf");
1251 path_packidx = NULL;
1252 break;
1255 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1256 if (err)
1257 break;
1259 done:
1260 if (err)
1261 free(path_packidx);
1262 if (packdir && closedir(packdir) != 0 && err == NULL)
1263 err = got_error_from_errno("closedir");
1264 return err;
1267 const struct got_error *
1268 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1269 struct got_repository *repo)
1271 const struct got_error *err;
1272 size_t i;
1274 *packidx = NULL;
1276 /* Search pack index cache. */
1277 for (i = 0; i < repo->pack_cache_size; i++) {
1278 if (repo->packidx_cache[i] == NULL)
1279 break;
1280 if (strcmp(repo->packidx_cache[i]->path_packidx,
1281 path_packidx) == 0) {
1282 *packidx = repo->packidx_cache[i];
1283 return NULL;
1286 /* No luck. Search the filesystem. */
1288 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1289 path_packidx, 0);
1290 if (err)
1291 return err;
1293 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1294 if (err)
1295 goto done;
1297 err = cache_packidx(repo, *packidx, path_packidx);
1298 done:
1299 if (err) {
1300 got_packidx_close(*packidx);
1301 *packidx = NULL;
1303 return err;
1306 static const struct got_error *
1307 read_packfile_hdr(int fd, struct got_packidx *packidx)
1309 const struct got_error *err = NULL;
1310 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1311 struct got_packfile_hdr hdr;
1312 ssize_t n;
1314 n = read(fd, &hdr, sizeof(hdr));
1315 if (n < 0)
1316 return got_error_from_errno("read");
1317 if (n != sizeof(hdr))
1318 return got_error(GOT_ERR_BAD_PACKFILE);
1320 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1321 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1322 be32toh(hdr.nobjects) != totobj)
1323 err = got_error(GOT_ERR_BAD_PACKFILE);
1325 return err;
1328 static const struct got_error *
1329 open_packfile(int *fd, struct got_repository *repo,
1330 const char *relpath, struct got_packidx *packidx)
1332 const struct got_error *err = NULL;
1334 *fd = openat(got_repo_get_fd(repo), relpath,
1335 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1336 if (*fd == -1)
1337 return got_error_from_errno_fmt("openat: %s/%s",
1338 got_repo_get_path_git_dir(repo), relpath);
1340 if (packidx) {
1341 err = read_packfile_hdr(*fd, packidx);
1342 if (err) {
1343 close(*fd);
1344 *fd = -1;
1348 return err;
1351 const struct got_error *
1352 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1353 const char *path_packfile, struct got_packidx *packidx)
1355 const struct got_error *err = NULL;
1356 struct got_pack *pack = NULL;
1357 struct stat sb;
1358 size_t i;
1360 if (packp)
1361 *packp = NULL;
1363 for (i = 0; i < repo->pack_cache_size; i++) {
1364 pack = &repo->packs[i];
1365 if (pack->path_packfile == NULL)
1366 break;
1367 if (strcmp(pack->path_packfile, path_packfile) == 0)
1368 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1371 if (i == repo->pack_cache_size) {
1372 struct got_pack tmp;
1373 err = got_pack_close(&repo->packs[i - 1]);
1374 if (err)
1375 return err;
1376 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1377 return got_error_from_errno("ftruncate");
1378 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1379 return got_error_from_errno("ftruncate");
1380 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1381 memcpy(&repo->packs[i - 1], &repo->packs[0],
1382 sizeof(repo->packs[i - 1]));
1383 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1384 i = 0;
1387 pack = &repo->packs[i];
1389 pack->path_packfile = strdup(path_packfile);
1390 if (pack->path_packfile == NULL) {
1391 err = got_error_from_errno("strdup");
1392 goto done;
1395 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1396 if (err)
1397 goto done;
1399 if (fstat(pack->fd, &sb) != 0) {
1400 err = got_error_from_errno("fstat");
1401 goto done;
1403 pack->filesize = sb.st_size;
1405 pack->privsep_child = NULL;
1407 #ifndef GOT_PACK_NO_MMAP
1408 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1409 pack->fd, 0);
1410 if (pack->map == MAP_FAILED) {
1411 if (errno != ENOMEM) {
1412 err = got_error_from_errno("mmap");
1413 goto done;
1415 pack->map = NULL; /* fall back to read(2) */
1417 #endif
1418 done:
1419 if (err) {
1420 if (pack) {
1421 free(pack->path_packfile);
1422 memset(pack, 0, sizeof(*pack));
1424 } else if (packp)
1425 *packp = pack;
1426 return err;
1429 struct got_pack *
1430 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1432 struct got_pack *pack = NULL;
1433 size_t i;
1435 for (i = 0; i < repo->pack_cache_size; i++) {
1436 pack = &repo->packs[i];
1437 if (pack->path_packfile == NULL)
1438 break;
1439 if (strcmp(pack->path_packfile, path_packfile) == 0)
1440 return pack;
1443 return NULL;
1446 const struct got_error *
1447 got_repo_init(const char *repo_path)
1449 const struct got_error *err = NULL;
1450 const char *dirnames[] = {
1451 GOT_OBJECTS_DIR,
1452 GOT_OBJECTS_PACK_DIR,
1453 GOT_REFS_DIR,
1455 const char *description_str = "Unnamed repository; "
1456 "edit this file 'description' to name the repository.";
1457 const char *headref_str = "ref: refs/heads/main";
1458 const char *gitconfig_str = "[core]\n"
1459 "\trepositoryformatversion = 0\n"
1460 "\tfilemode = true\n"
1461 "\tbare = true\n";
1462 char *path;
1463 size_t i;
1465 if (!got_path_dir_is_empty(repo_path))
1466 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1468 for (i = 0; i < nitems(dirnames); i++) {
1469 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1470 return got_error_from_errno("asprintf");
1472 err = got_path_mkdir(path);
1473 free(path);
1474 if (err)
1475 return err;
1478 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1479 return got_error_from_errno("asprintf");
1480 err = got_path_create_file(path, description_str);
1481 free(path);
1482 if (err)
1483 return err;
1485 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1486 return got_error_from_errno("asprintf");
1487 err = got_path_create_file(path, headref_str);
1488 free(path);
1489 if (err)
1490 return err;
1492 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1493 return got_error_from_errno("asprintf");
1494 err = got_path_create_file(path, gitconfig_str);
1495 free(path);
1496 if (err)
1497 return err;
1499 return NULL;
1502 static const struct got_error *
1503 match_packed_object(struct got_object_id **unique_id,
1504 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1506 const struct got_error *err = NULL;
1507 struct got_object_id_queue matched_ids;
1508 struct got_pathlist_entry *pe;
1510 STAILQ_INIT(&matched_ids);
1512 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1513 const char *path_packidx = pe->path;
1514 struct got_packidx *packidx;
1515 struct got_object_qid *qid;
1517 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1518 path_packidx, 0);
1519 if (err)
1520 break;
1522 err = got_packidx_match_id_str_prefix(&matched_ids,
1523 packidx, id_str_prefix);
1524 if (err) {
1525 got_packidx_close(packidx);
1526 break;
1528 err = got_packidx_close(packidx);
1529 if (err)
1530 break;
1532 STAILQ_FOREACH(qid, &matched_ids, entry) {
1533 if (obj_type != GOT_OBJ_TYPE_ANY) {
1534 int matched_type;
1535 err = got_object_get_type(&matched_type, repo,
1536 &qid->id);
1537 if (err)
1538 goto done;
1539 if (matched_type != obj_type)
1540 continue;
1542 if (*unique_id == NULL) {
1543 *unique_id = got_object_id_dup(&qid->id);
1544 if (*unique_id == NULL) {
1545 err = got_error_from_errno("malloc");
1546 goto done;
1548 } else {
1549 if (got_object_id_cmp(*unique_id,
1550 &qid->id) == 0)
1551 continue; /* packed multiple times */
1552 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1553 goto done;
1557 done:
1558 got_object_id_queue_free(&matched_ids);
1559 if (err) {
1560 free(*unique_id);
1561 *unique_id = NULL;
1563 return err;
1566 static const struct got_error *
1567 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1568 const char *object_dir, const char *id_str_prefix, int obj_type,
1569 struct got_repository *repo)
1571 const struct got_error *err = NULL;
1572 char *path;
1573 DIR *dir = NULL;
1574 struct dirent *dent;
1575 struct got_object_id id;
1577 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1578 err = got_error_from_errno("asprintf");
1579 goto done;
1582 dir = opendir(path);
1583 if (dir == NULL) {
1584 if (errno == ENOENT) {
1585 err = NULL;
1586 goto done;
1588 err = got_error_from_errno2("opendir", path);
1589 goto done;
1591 while ((dent = readdir(dir)) != NULL) {
1592 char *id_str;
1593 int cmp;
1595 if (strcmp(dent->d_name, ".") == 0 ||
1596 strcmp(dent->d_name, "..") == 0)
1597 continue;
1599 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1600 err = got_error_from_errno("asprintf");
1601 goto done;
1604 if (!got_parse_sha1_digest(id.sha1, id_str))
1605 continue;
1608 * Directory entries do not necessarily appear in
1609 * sorted order, so we must iterate over all of them.
1611 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1612 if (cmp != 0) {
1613 free(id_str);
1614 continue;
1617 if (*unique_id == NULL) {
1618 if (obj_type != GOT_OBJ_TYPE_ANY) {
1619 int matched_type;
1620 err = got_object_get_type(&matched_type, repo,
1621 &id);
1622 if (err)
1623 goto done;
1624 if (matched_type != obj_type)
1625 continue;
1627 *unique_id = got_object_id_dup(&id);
1628 if (*unique_id == NULL) {
1629 err = got_error_from_errno("got_object_id_dup");
1630 free(id_str);
1631 goto done;
1633 } else {
1634 if (got_object_id_cmp(*unique_id, &id) == 0)
1635 continue; /* both packed and loose */
1636 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1637 free(id_str);
1638 goto done;
1641 done:
1642 if (dir && closedir(dir) != 0 && err == NULL)
1643 err = got_error_from_errno("closedir");
1644 if (err) {
1645 free(*unique_id);
1646 *unique_id = NULL;
1648 free(path);
1649 return err;
1652 const struct got_error *
1653 got_repo_match_object_id_prefix(struct got_object_id **id,
1654 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1656 const struct got_error *err = NULL;
1657 char *path_objects = got_repo_get_path_objects(repo);
1658 char *object_dir = NULL;
1659 size_t len;
1660 int i;
1662 *id = NULL;
1664 len = strlen(id_str_prefix);
1665 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1666 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1668 for (i = 0; i < len; i++) {
1669 if (isxdigit((unsigned char)id_str_prefix[i]))
1670 continue;
1671 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1674 if (len >= 2) {
1675 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1676 if (err)
1677 goto done;
1678 object_dir = strndup(id_str_prefix, 2);
1679 if (object_dir == NULL) {
1680 err = got_error_from_errno("strdup");
1681 goto done;
1683 err = match_loose_object(id, path_objects, object_dir,
1684 id_str_prefix, obj_type, repo);
1685 } else if (len == 1) {
1686 int i;
1687 for (i = 0; i < 0xf; i++) {
1688 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1689 == -1) {
1690 err = got_error_from_errno("asprintf");
1691 goto done;
1693 err = match_packed_object(id, repo, object_dir,
1694 obj_type);
1695 if (err)
1696 goto done;
1697 err = match_loose_object(id, path_objects, object_dir,
1698 id_str_prefix, obj_type, repo);
1699 if (err)
1700 goto done;
1702 } else {
1703 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1704 goto done;
1706 done:
1707 free(object_dir);
1708 if (err) {
1709 free(*id);
1710 *id = NULL;
1711 } else if (*id == NULL) {
1712 switch (obj_type) {
1713 case GOT_OBJ_TYPE_BLOB:
1714 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1715 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1716 break;
1717 case GOT_OBJ_TYPE_TREE:
1718 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1719 GOT_OBJ_LABEL_TREE, id_str_prefix);
1720 break;
1721 case GOT_OBJ_TYPE_COMMIT:
1722 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1723 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1724 break;
1725 case GOT_OBJ_TYPE_TAG:
1726 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1727 GOT_OBJ_LABEL_TAG, id_str_prefix);
1728 break;
1729 default:
1730 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1731 break;
1735 return err;
1738 const struct got_error *
1739 got_repo_match_object_id(struct got_object_id **id, char **label,
1740 const char *id_str, int obj_type, struct got_reflist_head *refs,
1741 struct got_repository *repo)
1743 const struct got_error *err;
1744 struct got_tag_object *tag;
1745 struct got_reference *ref = NULL;
1747 *id = NULL;
1748 if (label)
1749 *label = NULL;
1751 if (refs) {
1752 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1753 refs, repo);
1754 if (err == NULL) {
1755 *id = got_object_id_dup(
1756 got_object_tag_get_object_id(tag));
1757 if (*id == NULL)
1758 err = got_error_from_errno("got_object_id_dup");
1759 else if (label && asprintf(label, "refs/tags/%s",
1760 got_object_tag_get_name(tag)) == -1) {
1761 err = got_error_from_errno("asprintf");
1762 free(*id);
1763 *id = NULL;
1765 got_object_tag_close(tag);
1766 return err;
1767 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1768 err->code != GOT_ERR_NO_OBJ)
1769 return err;
1772 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1773 if (err) {
1774 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1775 return err;
1776 err = got_ref_open(&ref, repo, id_str, 0);
1777 if (err != NULL)
1778 goto done;
1779 if (label) {
1780 *label = strdup(got_ref_get_name(ref));
1781 if (*label == NULL) {
1782 err = got_error_from_errno("strdup");
1783 goto done;
1786 err = got_ref_resolve(id, repo, ref);
1787 } else if (label) {
1788 err = got_object_id_str(label, *id);
1789 if (*label == NULL) {
1790 err = got_error_from_errno("strdup");
1791 goto done;
1794 done:
1795 if (ref)
1796 got_ref_close(ref);
1797 return err;
1800 const struct got_error *
1801 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1802 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1804 const struct got_error *err = NULL;
1805 struct got_reflist_entry *re;
1806 struct got_object_id *tag_id;
1807 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1809 *tag = NULL;
1811 TAILQ_FOREACH(re, refs, entry) {
1812 const char *refname;
1813 refname = got_ref_get_name(re->ref);
1814 if (got_ref_is_symbolic(re->ref))
1815 continue;
1816 if (strncmp(refname, "refs/tags/", 10) != 0)
1817 continue;
1818 if (!name_is_absolute)
1819 refname += strlen("refs/tags/");
1820 if (strcmp(refname, name) != 0)
1821 continue;
1822 err = got_ref_resolve(&tag_id, repo, re->ref);
1823 if (err)
1824 break;
1825 err = got_object_open_as_tag(tag, repo, tag_id);
1826 free(tag_id);
1827 if (err)
1828 break;
1829 if (obj_type == GOT_OBJ_TYPE_ANY ||
1830 got_object_tag_get_object_type(*tag) == obj_type)
1831 break;
1832 got_object_tag_close(*tag);
1833 *tag = NULL;
1836 if (err == NULL && *tag == NULL)
1837 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1838 GOT_OBJ_LABEL_TAG, name);
1839 return err;
1842 static const struct got_error *
1843 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1844 const char *name, mode_t mode, struct got_object_id *blob_id)
1846 const struct got_error *err = NULL;
1848 *new_te = NULL;
1850 *new_te = calloc(1, sizeof(**new_te));
1851 if (*new_te == NULL)
1852 return got_error_from_errno("calloc");
1854 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1855 sizeof((*new_te)->name)) {
1856 err = got_error(GOT_ERR_NO_SPACE);
1857 goto done;
1860 if (S_ISLNK(mode)) {
1861 (*new_te)->mode = S_IFLNK;
1862 } else {
1863 (*new_te)->mode = S_IFREG;
1864 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1866 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1867 done:
1868 if (err && *new_te) {
1869 free(*new_te);
1870 *new_te = NULL;
1872 return err;
1875 static const struct got_error *
1876 import_file(struct got_tree_entry **new_te, struct dirent *de,
1877 const char *path, struct got_repository *repo)
1879 const struct got_error *err;
1880 struct got_object_id *blob_id = NULL;
1881 char *filepath;
1882 struct stat sb;
1884 if (asprintf(&filepath, "%s%s%s", path,
1885 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1886 return got_error_from_errno("asprintf");
1888 if (lstat(filepath, &sb) != 0) {
1889 err = got_error_from_errno2("lstat", path);
1890 goto done;
1893 err = got_object_blob_create(&blob_id, filepath, repo);
1894 if (err)
1895 goto done;
1897 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1898 blob_id);
1899 done:
1900 free(filepath);
1901 if (err)
1902 free(blob_id);
1903 return err;
1906 static const struct got_error *
1907 insert_tree_entry(struct got_tree_entry *new_te,
1908 struct got_pathlist_head *paths)
1910 const struct got_error *err = NULL;
1911 struct got_pathlist_entry *new_pe;
1913 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1914 if (err)
1915 return err;
1916 if (new_pe == NULL)
1917 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1918 return NULL;
1921 static const struct got_error *write_tree(struct got_object_id **,
1922 const char *, struct got_pathlist_head *, struct got_repository *,
1923 got_repo_import_cb progress_cb, void *progress_arg);
1925 static const struct got_error *
1926 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1927 const char *path, struct got_pathlist_head *ignores,
1928 struct got_repository *repo,
1929 got_repo_import_cb progress_cb, void *progress_arg)
1931 const struct got_error *err;
1932 struct got_object_id *id = NULL;
1933 char *subdirpath;
1935 if (asprintf(&subdirpath, "%s%s%s", path,
1936 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1937 return got_error_from_errno("asprintf");
1939 (*new_te) = calloc(1, sizeof(**new_te));
1940 if (*new_te == NULL)
1941 return got_error_from_errno("calloc");
1942 (*new_te)->mode = S_IFDIR;
1943 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1944 sizeof((*new_te)->name)) {
1945 err = got_error(GOT_ERR_NO_SPACE);
1946 goto done;
1948 err = write_tree(&id, subdirpath, ignores, repo,
1949 progress_cb, progress_arg);
1950 if (err)
1951 goto done;
1952 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1954 done:
1955 free(id);
1956 free(subdirpath);
1957 if (err) {
1958 free(*new_te);
1959 *new_te = NULL;
1961 return err;
1964 static const struct got_error *
1965 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1966 struct got_pathlist_head *ignores, struct got_repository *repo,
1967 got_repo_import_cb progress_cb, void *progress_arg)
1969 const struct got_error *err = NULL;
1970 DIR *dir;
1971 struct dirent *de;
1972 int nentries;
1973 struct got_tree_entry *new_te = NULL;
1974 struct got_pathlist_head paths;
1975 struct got_pathlist_entry *pe;
1977 *new_tree_id = NULL;
1979 TAILQ_INIT(&paths);
1981 dir = opendir(path_dir);
1982 if (dir == NULL) {
1983 err = got_error_from_errno2("opendir", path_dir);
1984 goto done;
1987 nentries = 0;
1988 while ((de = readdir(dir)) != NULL) {
1989 int ignore = 0;
1990 int type;
1992 if (strcmp(de->d_name, ".") == 0 ||
1993 strcmp(de->d_name, "..") == 0)
1994 continue;
1996 TAILQ_FOREACH(pe, ignores, entry) {
1997 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1998 ignore = 1;
1999 break;
2002 if (ignore)
2003 continue;
2005 err = got_path_dirent_type(&type, path_dir, de);
2006 if (err)
2007 goto done;
2009 if (type == DT_DIR) {
2010 err = import_subdir(&new_te, de, path_dir,
2011 ignores, repo, progress_cb, progress_arg);
2012 if (err) {
2013 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2014 goto done;
2015 err = NULL;
2016 continue;
2018 } else if (type == DT_REG || type == DT_LNK) {
2019 err = import_file(&new_te, de, path_dir, repo);
2020 if (err)
2021 goto done;
2022 } else
2023 continue;
2025 err = insert_tree_entry(new_te, &paths);
2026 if (err)
2027 goto done;
2028 nentries++;
2031 if (TAILQ_EMPTY(&paths)) {
2032 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2033 "cannot create tree without any entries");
2034 goto done;
2037 TAILQ_FOREACH(pe, &paths, entry) {
2038 struct got_tree_entry *te = pe->data;
2039 char *path;
2040 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2041 continue;
2042 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2043 err = got_error_from_errno("asprintf");
2044 goto done;
2046 err = (*progress_cb)(progress_arg, path);
2047 free(path);
2048 if (err)
2049 goto done;
2052 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2053 done:
2054 if (dir)
2055 closedir(dir);
2056 got_pathlist_free(&paths);
2057 return err;
2060 const struct got_error *
2061 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2062 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2063 struct got_repository *repo, got_repo_import_cb progress_cb,
2064 void *progress_arg)
2066 const struct got_error *err;
2067 struct got_object_id *new_tree_id;
2069 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2070 progress_cb, progress_arg);
2071 if (err)
2072 return err;
2074 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2075 author, time(NULL), author, time(NULL), logmsg, repo);
2076 free(new_tree_id);
2077 return err;
2080 const struct got_error *
2081 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2082 struct got_repository *repo)
2084 const struct got_error *err = NULL;
2085 char *path_objects = NULL, *path = NULL;
2086 DIR *dir = NULL;
2087 struct got_object_id id;
2088 int i;
2090 *nobjects = 0;
2091 *ondisk_size = 0;
2093 path_objects = got_repo_get_path_objects(repo);
2094 if (path_objects == NULL)
2095 return got_error_from_errno("got_repo_get_path_objects");
2097 for (i = 0; i <= 0xff; i++) {
2098 struct dirent *dent;
2100 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2101 err = got_error_from_errno("asprintf");
2102 break;
2105 dir = opendir(path);
2106 if (dir == NULL) {
2107 if (errno == ENOENT) {
2108 err = NULL;
2109 continue;
2111 err = got_error_from_errno2("opendir", path);
2112 break;
2115 while ((dent = readdir(dir)) != NULL) {
2116 char *id_str;
2117 int fd;
2118 struct stat sb;
2120 if (strcmp(dent->d_name, ".") == 0 ||
2121 strcmp(dent->d_name, "..") == 0)
2122 continue;
2124 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2125 err = got_error_from_errno("asprintf");
2126 goto done;
2129 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2130 free(id_str);
2131 continue;
2133 free(id_str);
2135 err = got_object_open_loose_fd(&fd, &id, repo);
2136 if (err)
2137 goto done;
2139 if (fstat(fd, &sb) == -1) {
2140 err = got_error_from_errno("fstat");
2141 close(fd);
2142 goto done;
2144 (*nobjects)++;
2145 (*ondisk_size) += sb.st_size;
2147 if (close(fd) == -1) {
2148 err = got_error_from_errno("close");
2149 goto done;
2153 if (closedir(dir) != 0) {
2154 err = got_error_from_errno("closedir");
2155 goto done;
2157 dir = NULL;
2159 free(path);
2160 path = NULL;
2162 done:
2163 if (dir && closedir(dir) != 0 && err == NULL)
2164 err = got_error_from_errno("closedir");
2166 if (err) {
2167 *nobjects = 0;
2168 *ondisk_size = 0;
2170 free(path_objects);
2171 free(path);
2172 return err;
2175 const struct got_error *
2176 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2177 off_t *total_packsize, struct got_repository *repo)
2179 const struct got_error *err = NULL;
2180 DIR *packdir = NULL;
2181 struct dirent *dent;
2182 struct got_packidx *packidx = NULL;
2183 char *path_packidx;
2184 char *path_packfile;
2185 int packdir_fd;
2186 struct stat sb;
2188 *npackfiles = 0;
2189 *nobjects = 0;
2190 *total_packsize = 0;
2192 packdir_fd = openat(got_repo_get_fd(repo),
2193 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2194 if (packdir_fd == -1) {
2195 return got_error_from_errno_fmt("openat: %s/%s",
2196 got_repo_get_path_git_dir(repo),
2197 GOT_OBJECTS_PACK_DIR);
2200 packdir = fdopendir(packdir_fd);
2201 if (packdir == NULL) {
2202 err = got_error_from_errno("fdopendir");
2203 goto done;
2206 while ((dent = readdir(packdir)) != NULL) {
2207 if (!got_repo_is_packidx_filename(dent->d_name,
2208 strlen(dent->d_name)))
2209 continue;
2211 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2212 dent->d_name) == -1) {
2213 err = got_error_from_errno("asprintf");
2214 goto done;
2217 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2218 path_packidx, 0);
2219 free(path_packidx);
2220 if (err)
2221 goto done;
2223 if (fstat(packidx->fd, &sb) == -1)
2224 goto done;
2225 *total_packsize += sb.st_size;
2227 err = got_packidx_get_packfile_path(&path_packfile,
2228 packidx->path_packidx);
2229 if (err)
2230 goto done;
2232 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2233 0) == -1) {
2234 free(path_packfile);
2235 goto done;
2237 free(path_packfile);
2238 *total_packsize += sb.st_size;
2240 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2242 (*npackfiles)++;
2244 got_packidx_close(packidx);
2245 packidx = NULL;
2247 done:
2248 if (packidx)
2249 got_packidx_close(packidx);
2250 if (packdir && closedir(packdir) != 0 && err == NULL)
2251 err = got_error_from_errno("closedir");
2252 if (err) {
2253 *npackfiles = 0;
2254 *nobjects = 0;
2255 *total_packsize = 0;
2257 return err;
2260 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2261 got_packidx_bloom_filter_cmp);