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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/resource.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
40 #include "bloom.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_get_fd(struct got_repository *repo)
85 {
86 return repo->gitdir_fd;
87 }
89 const char *
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 {
92 return repo->gitconfig_author_name;
93 }
95 const char *
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 {
98 return repo->gitconfig_author_email;
99 }
101 const char *
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
107 const char *
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
113 const char *
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
119 void
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
127 int
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 static char *
134 get_path_git_child(struct got_repository *repo, const char *basename)
136 char *path_child;
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
139 basename) == -1)
140 return NULL;
142 return path_child;
145 char *
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 char *
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 char *
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
163 char *
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 static char *
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
175 char *
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
181 char *
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
193 void
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
201 static int
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
208 int ret = 0;
209 struct stat sb;
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
213 goto done;
214 if (!S_ISDIR(sb.st_mode))
215 goto done;
217 if (lstat(path_objects, &sb) == -1)
218 goto done;
219 if (!S_ISDIR(sb.st_mode))
220 goto done;
222 if (lstat(path_refs, &sb) == -1)
223 goto done;
224 if (!S_ISDIR(sb.st_mode))
225 goto done;
227 if (lstat(path_head, &sb) == -1)
228 goto done;
229 if (!S_ISREG(sb.st_mode))
230 goto done;
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 goto done;
235 got_ref_close(head_ref);
237 ret = 1;
238 done:
239 free(path_objects);
240 free(path_refs);
241 free(path_head);
242 return ret;
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
256 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
257 pack_fds_tmp[i] = got_opentempfd();
258 if (pack_fds_tmp[i] == -1) {
259 err = got_error_from_errno("got_opentempfd");
260 got_repo_pack_fds_close(pack_fds_tmp);
261 return err;
264 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
265 return err;
268 const struct got_error *
269 got_repo_pack_fds_close(int *pack_fds)
271 const struct got_error *err = NULL;
272 int i;
274 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
275 if (pack_fds[i] == -1)
276 continue;
277 if (close(pack_fds[i]) == -1) {
278 err = got_error_from_errno("close");
279 break;
282 free(pack_fds);
283 return err;
286 const struct got_error *
287 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
288 struct got_object *obj)
290 #ifndef GOT_NO_OBJ_CACHE
291 const struct got_error *err = NULL;
292 err = got_object_cache_add(&repo->objcache, id, obj);
293 if (err) {
294 if (err->code == GOT_ERR_OBJ_EXISTS ||
295 err->code == GOT_ERR_OBJ_TOO_LARGE)
296 err = NULL;
297 return err;
299 obj->refcnt++;
300 #endif
301 return NULL;
304 struct got_object *
305 got_repo_get_cached_object(struct got_repository *repo,
306 struct got_object_id *id)
308 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
311 const struct got_error *
312 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
313 struct got_tree_object *tree)
315 #ifndef GOT_NO_OBJ_CACHE
316 const struct got_error *err = NULL;
317 err = got_object_cache_add(&repo->treecache, id, tree);
318 if (err) {
319 if (err->code == GOT_ERR_OBJ_EXISTS ||
320 err->code == GOT_ERR_OBJ_TOO_LARGE)
321 err = NULL;
322 return err;
324 tree->refcnt++;
325 #endif
326 return NULL;
329 struct got_tree_object *
330 got_repo_get_cached_tree(struct got_repository *repo,
331 struct got_object_id *id)
333 return (struct got_tree_object *)got_object_cache_get(
334 &repo->treecache, id);
337 const struct got_error *
338 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
339 struct got_commit_object *commit)
341 #ifndef GOT_NO_OBJ_CACHE
342 const struct got_error *err = NULL;
343 err = got_object_cache_add(&repo->commitcache, id, commit);
344 if (err) {
345 if (err->code == GOT_ERR_OBJ_EXISTS ||
346 err->code == GOT_ERR_OBJ_TOO_LARGE)
347 err = NULL;
348 return err;
350 commit->refcnt++;
351 #endif
352 return NULL;
355 struct got_commit_object *
356 got_repo_get_cached_commit(struct got_repository *repo,
357 struct got_object_id *id)
359 return (struct got_commit_object *)got_object_cache_get(
360 &repo->commitcache, id);
363 const struct got_error *
364 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
365 struct got_tag_object *tag)
367 #ifndef GOT_NO_OBJ_CACHE
368 const struct got_error *err = NULL;
369 err = got_object_cache_add(&repo->tagcache, id, tag);
370 if (err) {
371 if (err->code == GOT_ERR_OBJ_EXISTS ||
372 err->code == GOT_ERR_OBJ_TOO_LARGE)
373 err = NULL;
374 return err;
376 tag->refcnt++;
377 #endif
378 return NULL;
381 struct got_tag_object *
382 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
384 return (struct got_tag_object *)got_object_cache_get(
385 &repo->tagcache, id);
388 const struct got_error *
389 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
390 struct got_raw_object *raw)
392 #ifndef GOT_NO_OBJ_CACHE
393 const struct got_error *err = NULL;
394 err = got_object_cache_add(&repo->rawcache, id, raw);
395 if (err) {
396 if (err->code == GOT_ERR_OBJ_EXISTS ||
397 err->code == GOT_ERR_OBJ_TOO_LARGE)
398 err = NULL;
399 return err;
401 raw->refcnt++;
402 #endif
403 return NULL;
407 struct got_raw_object *
408 got_repo_get_cached_raw_object(struct got_repository *repo,
409 struct got_object_id *id)
411 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
415 static const struct got_error *
416 open_repo(struct got_repository *repo, const char *path)
418 const struct got_error *err = NULL;
420 repo->gitdir_fd = -1;
422 /* bare git repository? */
423 repo->path_git_dir = strdup(path);
424 if (repo->path_git_dir == NULL)
425 return got_error_from_errno("strdup");
426 if (is_git_repo(repo)) {
427 repo->path = strdup(repo->path_git_dir);
428 if (repo->path == NULL) {
429 err = got_error_from_errno("strdup");
430 goto done;
432 repo->gitdir_fd = open(repo->path_git_dir,
433 O_DIRECTORY | O_CLOEXEC);
434 if (repo->gitdir_fd == -1) {
435 err = got_error_from_errno2("open",
436 repo->path_git_dir);
437 goto done;
439 return NULL;
442 /* git repository with working tree? */
443 free(repo->path_git_dir);
444 repo->path_git_dir = NULL;
445 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
446 err = got_error_from_errno("asprintf");
447 goto done;
449 if (is_git_repo(repo)) {
450 repo->path = strdup(path);
451 if (repo->path == NULL) {
452 err = got_error_from_errno("strdup");
453 goto done;
455 repo->gitdir_fd = open(repo->path_git_dir,
456 O_DIRECTORY | O_CLOEXEC);
457 if (repo->gitdir_fd == -1) {
458 err = got_error_from_errno2("open",
459 repo->path_git_dir);
460 goto done;
462 return NULL;
465 err = got_error(GOT_ERR_NOT_GIT_REPO);
466 done:
467 if (err) {
468 free(repo->path);
469 repo->path = NULL;
470 free(repo->path_git_dir);
471 repo->path_git_dir = NULL;
472 if (repo->gitdir_fd != -1)
473 close(repo->gitdir_fd);
474 repo->gitdir_fd = -1;
477 return err;
480 static const struct got_error *
481 parse_gitconfig_file(int *gitconfig_repository_format_version,
482 char **gitconfig_author_name, char **gitconfig_author_email,
483 struct got_remote_repo **remotes, int *nremotes,
484 char **gitconfig_owner, char ***extensions, int *nextensions,
485 const char *gitconfig_path)
487 const struct got_error *err = NULL, *child_err = NULL;
488 int fd = -1;
489 int imsg_fds[2] = { -1, -1 };
490 pid_t pid;
491 struct imsgbuf *ibuf;
493 *gitconfig_repository_format_version = 0;
494 if (extensions)
495 *extensions = NULL;
496 if (nextensions)
497 *nextensions = 0;
498 *gitconfig_author_name = NULL;
499 *gitconfig_author_email = NULL;
500 if (remotes)
501 *remotes = NULL;
502 if (nremotes)
503 *nremotes = 0;
504 if (gitconfig_owner)
505 *gitconfig_owner = NULL;
507 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
508 if (fd == -1) {
509 if (errno == ENOENT)
510 return NULL;
511 return got_error_from_errno2("open", gitconfig_path);
514 ibuf = calloc(1, sizeof(*ibuf));
515 if (ibuf == NULL) {
516 err = got_error_from_errno("calloc");
517 goto done;
520 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
521 err = got_error_from_errno("socketpair");
522 goto done;
525 pid = fork();
526 if (pid == -1) {
527 err = got_error_from_errno("fork");
528 goto done;
529 } else if (pid == 0) {
530 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
531 gitconfig_path);
532 /* not reached */
535 if (close(imsg_fds[1]) == -1) {
536 err = got_error_from_errno("close");
537 goto done;
539 imsg_fds[1] = -1;
540 imsg_init(ibuf, imsg_fds[0]);
542 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
543 if (err)
544 goto done;
545 fd = -1;
547 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
548 if (err)
549 goto done;
551 err = got_privsep_recv_gitconfig_int(
552 gitconfig_repository_format_version, ibuf);
553 if (err)
554 goto done;
556 if (extensions && nextensions) {
557 err = got_privsep_send_gitconfig_repository_extensions_req(
558 ibuf);
559 if (err)
560 goto done;
561 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
562 if (err)
563 goto done;
564 if (*nextensions > 0) {
565 int i;
566 *extensions = calloc(*nextensions, sizeof(char *));
567 if (*extensions == NULL) {
568 err = got_error_from_errno("calloc");
569 goto done;
571 for (i = 0; i < *nextensions; i++) {
572 char *ext;
573 err = got_privsep_recv_gitconfig_str(&ext,
574 ibuf);
575 if (err)
576 goto done;
577 (*extensions)[i] = ext;
582 err = got_privsep_send_gitconfig_author_name_req(ibuf);
583 if (err)
584 goto done;
586 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
587 if (err)
588 goto done;
590 err = got_privsep_send_gitconfig_author_email_req(ibuf);
591 if (err)
592 goto done;
594 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
595 if (err)
596 goto done;
598 if (remotes && nremotes) {
599 err = got_privsep_send_gitconfig_remotes_req(ibuf);
600 if (err)
601 goto done;
603 err = got_privsep_recv_gitconfig_remotes(remotes,
604 nremotes, ibuf);
605 if (err)
606 goto done;
609 if (gitconfig_owner) {
610 err = got_privsep_send_gitconfig_owner_req(ibuf);
611 if (err)
612 goto done;
613 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
614 if (err)
615 goto done;
618 err = got_privsep_send_stop(imsg_fds[0]);
619 child_err = got_privsep_wait_for_child(pid);
620 if (child_err && err == NULL)
621 err = child_err;
622 done:
623 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
624 err = got_error_from_errno("close");
625 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
626 err = got_error_from_errno("close");
627 if (fd != -1 && close(fd) == -1 && err == NULL)
628 err = got_error_from_errno2("close", gitconfig_path);
629 free(ibuf);
630 return err;
633 static const struct got_error *
634 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
636 const struct got_error *err = NULL;
637 char *repo_gitconfig_path = NULL;
639 if (global_gitconfig_path) {
640 /* Read settings from ~/.gitconfig. */
641 int dummy_repo_version;
642 err = parse_gitconfig_file(&dummy_repo_version,
643 &repo->global_gitconfig_author_name,
644 &repo->global_gitconfig_author_email,
645 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
646 if (err)
647 return err;
650 /* Read repository's .git/config file. */
651 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
652 if (repo_gitconfig_path == NULL)
653 return got_error_from_errno("got_repo_get_path_gitconfig");
655 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
656 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
657 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
658 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
659 repo_gitconfig_path);
660 if (err)
661 goto done;
662 done:
663 free(repo_gitconfig_path);
664 return err;
667 static const struct got_error *
668 read_gotconfig(struct got_repository *repo)
670 const struct got_error *err = NULL;
671 char *gotconfig_path;
673 gotconfig_path = got_repo_get_path_gotconfig(repo);
674 if (gotconfig_path == NULL)
675 return got_error_from_errno("got_repo_get_path_gotconfig");
677 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
678 free(gotconfig_path);
679 return err;
682 /* Supported repository format extensions. */
683 static const char *const repo_extensions[] = {
684 "noop", /* Got supports repository format version 1. */
685 "preciousObjects", /* Supported by gotadmin cleanup. */
686 "worktreeConfig", /* Got does not care about Git work trees. */
687 };
689 const struct got_error *
690 got_repo_open(struct got_repository **repop, const char *path,
691 const char *global_gitconfig_path, int *pack_fds)
693 struct got_repository *repo = NULL;
694 const struct got_error *err = NULL;
695 char *repo_path = NULL;
696 size_t i, j = 0;
697 struct rlimit rl;
699 *repop = NULL;
701 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
702 return got_error_from_errno("getrlimit");
704 repo = calloc(1, sizeof(*repo));
705 if (repo == NULL)
706 return got_error_from_errno("calloc");
708 RB_INIT(&repo->packidx_bloom_filters);
709 TAILQ_INIT(&repo->packidx_paths);
711 for (i = 0; i < nitems(repo->privsep_children); i++) {
712 memset(&repo->privsep_children[i], 0,
713 sizeof(repo->privsep_children[0]));
714 repo->privsep_children[i].imsg_fd = -1;
717 err = got_object_cache_init(&repo->objcache,
718 GOT_OBJECT_CACHE_TYPE_OBJ);
719 if (err)
720 goto done;
721 err = got_object_cache_init(&repo->treecache,
722 GOT_OBJECT_CACHE_TYPE_TREE);
723 if (err)
724 goto done;
725 err = got_object_cache_init(&repo->commitcache,
726 GOT_OBJECT_CACHE_TYPE_COMMIT);
727 if (err)
728 goto done;
729 err = got_object_cache_init(&repo->tagcache,
730 GOT_OBJECT_CACHE_TYPE_TAG);
731 if (err)
732 goto done;
733 err = got_object_cache_init(&repo->rawcache,
734 GOT_OBJECT_CACHE_TYPE_RAW);
735 if (err)
736 goto done;
738 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
739 if (repo->pack_cache_size > rl.rlim_cur / 8)
740 repo->pack_cache_size = rl.rlim_cur / 8;
741 for (i = 0; i < nitems(repo->packs); i++) {
742 if (i < repo->pack_cache_size) {
743 repo->packs[i].basefd = pack_fds[j++];
744 repo->packs[i].accumfd = pack_fds[j++];
745 } else {
746 repo->packs[i].basefd = -1;
747 repo->packs[i].accumfd = -1;
750 repo->pinned_pack = -1;
751 repo->pinned_packidx = -1;
752 repo->pinned_pid = 0;
754 repo_path = realpath(path, NULL);
755 if (repo_path == NULL) {
756 err = got_error_from_errno2("realpath", path);
757 goto done;
760 for (;;) {
761 char *parent_path;
763 err = open_repo(repo, repo_path);
764 if (err == NULL)
765 break;
766 if (err->code != GOT_ERR_NOT_GIT_REPO)
767 goto done;
768 if (repo_path[0] == '/' && repo_path[1] == '\0') {
769 err = got_error(GOT_ERR_NOT_GIT_REPO);
770 goto done;
772 err = got_path_dirname(&parent_path, repo_path);
773 if (err)
774 goto done;
775 free(repo_path);
776 repo_path = parent_path;
779 err = read_gotconfig(repo);
780 if (err)
781 goto done;
783 err = read_gitconfig(repo, global_gitconfig_path);
784 if (err)
785 goto done;
786 if (repo->gitconfig_repository_format_version != 0) {
787 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
788 goto done;
790 for (i = 0; i < repo->nextensions; i++) {
791 char *ext = repo->extensions[i];
792 int j, supported = 0;
793 for (j = 0; j < nitems(repo_extensions); j++) {
794 if (strcmp(ext, repo_extensions[j]) == 0) {
795 supported = 1;
796 break;
799 if (!supported) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 goto done;
805 err = got_repo_list_packidx(&repo->packidx_paths, repo);
806 done:
807 if (err)
808 got_repo_close(repo);
809 else
810 *repop = repo;
811 free(repo_path);
812 return err;
815 const struct got_error *
816 got_repo_close(struct got_repository *repo)
818 const struct got_error *err = NULL, *child_err;
819 struct got_packidx_bloom_filter *bf;
820 struct got_pathlist_entry *pe;
821 size_t i;
823 for (i = 0; i < repo->pack_cache_size; i++) {
824 if (repo->packidx_cache[i] == NULL)
825 break;
826 got_packidx_close(repo->packidx_cache[i]);
829 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
830 &repo->packidx_bloom_filters))) {
831 RB_REMOVE(got_packidx_bloom_filter_tree,
832 &repo->packidx_bloom_filters, bf);
833 free(bf->bloom);
834 free(bf);
837 for (i = 0; i < repo->pack_cache_size; i++)
838 if (repo->packs[i].path_packfile)
839 if (repo->packs[i].path_packfile)
840 got_pack_close(&repo->packs[i]);
842 free(repo->path);
843 free(repo->path_git_dir);
845 got_object_cache_close(&repo->objcache);
846 got_object_cache_close(&repo->treecache);
847 got_object_cache_close(&repo->commitcache);
848 got_object_cache_close(&repo->tagcache);
849 got_object_cache_close(&repo->rawcache);
851 for (i = 0; i < nitems(repo->privsep_children); i++) {
852 if (repo->privsep_children[i].imsg_fd == -1)
853 continue;
854 imsg_clear(repo->privsep_children[i].ibuf);
855 free(repo->privsep_children[i].ibuf);
856 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
857 child_err = got_privsep_wait_for_child(
858 repo->privsep_children[i].pid);
859 if (child_err && err == NULL)
860 err = child_err;
861 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
862 err == NULL)
863 err = got_error_from_errno("close");
866 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
867 err == NULL)
868 err = got_error_from_errno("close");
870 if (repo->gotconfig)
871 got_gotconfig_free(repo->gotconfig);
872 free(repo->gitconfig_author_name);
873 free(repo->gitconfig_author_email);
874 for (i = 0; i < repo->ngitconfig_remotes; i++)
875 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
876 free(repo->gitconfig_remotes);
877 for (i = 0; i < repo->nextensions; i++)
878 free(repo->extensions[i]);
879 free(repo->extensions);
881 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
882 free((void *)pe->path);
883 got_pathlist_free(&repo->packidx_paths);
884 free(repo);
886 return err;
889 void
890 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
892 int i;
894 free(repo->name);
895 repo->name = NULL;
896 free(repo->fetch_url);
897 repo->fetch_url = NULL;
898 free(repo->send_url);
899 repo->send_url = NULL;
900 for (i = 0; i < repo->nfetch_branches; i++)
901 free(repo->fetch_branches[i]);
902 free(repo->fetch_branches);
903 repo->fetch_branches = NULL;
904 repo->nfetch_branches = 0;
905 for (i = 0; i < repo->nsend_branches; i++)
906 free(repo->send_branches[i]);
907 free(repo->send_branches);
908 repo->send_branches = NULL;
909 repo->nsend_branches = 0;
912 const struct got_error *
913 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
914 const char *input_path)
916 const struct got_error *err = NULL;
917 const char *repo_abspath = NULL;
918 size_t repolen, len;
919 char *canonpath, *path = NULL;
921 *in_repo_path = NULL;
923 canonpath = strdup(input_path);
924 if (canonpath == NULL) {
925 err = got_error_from_errno("strdup");
926 goto done;
928 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
929 if (err)
930 goto done;
932 repo_abspath = got_repo_get_path(repo);
934 if (canonpath[0] == '\0') {
935 path = strdup(canonpath);
936 if (path == NULL) {
937 err = got_error_from_errno("strdup");
938 goto done;
940 } else {
941 path = realpath(canonpath, NULL);
942 if (path == NULL) {
943 if (errno != ENOENT) {
944 err = got_error_from_errno2("realpath",
945 canonpath);
946 goto done;
948 /*
949 * Path is not on disk.
950 * Assume it is already relative to repository root.
951 */
952 path = strdup(canonpath);
953 if (path == NULL) {
954 err = got_error_from_errno("strdup");
955 goto done;
959 repolen = strlen(repo_abspath);
960 len = strlen(path);
963 if (strcmp(path, repo_abspath) == 0) {
964 free(path);
965 path = strdup("");
966 if (path == NULL) {
967 err = got_error_from_errno("strdup");
968 goto done;
970 } else if (len > repolen &&
971 got_path_is_child(path, repo_abspath, repolen)) {
972 /* Matched an on-disk path inside repository. */
973 if (got_repo_is_bare(repo)) {
974 /*
975 * Matched an on-disk path inside repository
976 * database. Treat input as repository-relative.
977 */
978 free(path);
979 path = canonpath;
980 canonpath = NULL;
981 } else {
982 char *child;
983 /* Strip common prefix with repository path. */
984 err = got_path_skip_common_ancestor(&child,
985 repo_abspath, path);
986 if (err)
987 goto done;
988 free(path);
989 path = child;
991 } else {
992 /*
993 * Matched unrelated on-disk path.
994 * Treat input as repository-relative.
995 */
996 free(path);
997 path = canonpath;
998 canonpath = NULL;
1002 /* Make in-repository path absolute */
1003 if (path[0] != '/') {
1004 char *abspath;
1005 if (asprintf(&abspath, "/%s", path) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1009 free(path);
1010 path = abspath;
1013 done:
1014 free(canonpath);
1015 if (err)
1016 free(path);
1017 else
1018 *in_repo_path = path;
1019 return err;
1022 static const struct got_error *
1023 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1024 const char *path_packidx)
1026 const struct got_error *err = NULL;
1027 size_t i;
1029 for (i = 0; i < repo->pack_cache_size; i++) {
1030 if (repo->packidx_cache[i] == NULL)
1031 break;
1032 if (strcmp(repo->packidx_cache[i]->path_packidx,
1033 path_packidx) == 0) {
1034 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1037 if (i == repo->pack_cache_size) {
1038 do {
1039 i--;
1040 } while (i > 0 && repo->pinned_packidx >= 0 &&
1041 i == repo->pinned_packidx);
1042 err = got_packidx_close(repo->packidx_cache[i]);
1043 if (err)
1044 return err;
1047 repo->packidx_cache[i] = packidx;
1049 return NULL;
1052 int
1053 got_repo_is_packidx_filename(const char *name, size_t len)
1055 if (len != GOT_PACKIDX_NAMELEN)
1056 return 0;
1058 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1059 return 0;
1061 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1062 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1063 return 0;
1065 return 1;
1068 static struct got_packidx_bloom_filter *
1069 get_packidx_bloom_filter(struct got_repository *repo,
1070 const char *path, size_t path_len)
1072 struct got_packidx_bloom_filter key;
1074 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1075 return NULL; /* XXX */
1076 key.path_len = path_len;
1078 return RB_FIND(got_packidx_bloom_filter_tree,
1079 &repo->packidx_bloom_filters, &key);
1082 int
1083 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1084 const char *path_packidx, struct got_object_id *id)
1086 struct got_packidx_bloom_filter *bf;
1088 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1089 if (bf)
1090 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1092 /* No bloom filter means this pack index must be searched. */
1093 return 1;
1096 static const struct got_error *
1097 add_packidx_bloom_filter(struct got_repository *repo,
1098 struct got_packidx *packidx, const char *path_packidx)
1100 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1101 struct got_packidx_bloom_filter *bf;
1102 size_t len;
1105 * Don't use bloom filters for very large pack index files.
1106 * Large pack files will contain a relatively large fraction
1107 * of our objects so we will likely need to visit them anyway.
1108 * The more objects a pack file contains the higher the probability
1109 * of a false-positive match from the bloom filter. And reading
1110 * all object IDs from a large pack index file can be expensive.
1112 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1113 return NULL;
1115 /* Do we already have a filter for this pack index? */
1116 if (get_packidx_bloom_filter(repo, path_packidx,
1117 strlen(path_packidx)) != NULL)
1118 return NULL;
1120 bf = calloc(1, sizeof(*bf));
1121 if (bf == NULL)
1122 return got_error_from_errno("calloc");
1123 bf->bloom = calloc(1, sizeof(*bf->bloom));
1124 if (bf->bloom == NULL) {
1125 free(bf);
1126 return got_error_from_errno("calloc");
1129 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1130 if (len >= sizeof(bf->path)) {
1131 free(bf->bloom);
1132 free(bf);
1133 return got_error(GOT_ERR_NO_SPACE);
1135 bf->path_len = len;
1137 /* Minimum size supported by our bloom filter is 1000 entries. */
1138 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1139 for (i = 0; i < nobjects; i++) {
1140 struct got_packidx_object_id *id;
1141 id = &packidx->hdr.sorted_ids[i];
1142 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1145 RB_INSERT(got_packidx_bloom_filter_tree,
1146 &repo->packidx_bloom_filters, bf);
1147 return NULL;
1150 const struct got_error *
1151 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1152 struct got_repository *repo, struct got_object_id *id)
1154 const struct got_error *err;
1155 struct got_pathlist_entry *pe;
1156 size_t i;
1158 /* Search pack index cache. */
1159 for (i = 0; i < repo->pack_cache_size; i++) {
1160 if (repo->packidx_cache[i] == NULL)
1161 break;
1162 if (!got_repo_check_packidx_bloom_filter(repo,
1163 repo->packidx_cache[i]->path_packidx, id))
1164 continue; /* object will not be found in this index */
1165 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1166 if (*idx != -1) {
1167 *packidx = repo->packidx_cache[i];
1169 * Move this cache entry to the front. Repeatedly
1170 * searching a wrong pack index can be expensive.
1172 if (i > 0) {
1173 memmove(&repo->packidx_cache[1],
1174 &repo->packidx_cache[0],
1175 i * sizeof(repo->packidx_cache[0]));
1176 repo->packidx_cache[0] = *packidx;
1177 if (repo->pinned_packidx >= 0 &&
1178 repo->pinned_packidx < i)
1179 repo->pinned_packidx++;
1180 else if (repo->pinned_packidx == i)
1181 repo->pinned_packidx = 0;
1183 return NULL;
1186 /* No luck. Search the filesystem. */
1188 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1189 const char *path_packidx = pe->path;
1190 int is_cached = 0;
1192 if (!got_repo_check_packidx_bloom_filter(repo,
1193 pe->path, id))
1194 continue; /* object will not be found in this index */
1196 for (i = 0; i < repo->pack_cache_size; i++) {
1197 if (repo->packidx_cache[i] == NULL)
1198 break;
1199 if (strcmp(repo->packidx_cache[i]->path_packidx,
1200 path_packidx) == 0) {
1201 is_cached = 1;
1202 break;
1205 if (is_cached)
1206 continue; /* already searched */
1208 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1209 path_packidx, 0);
1210 if (err)
1211 goto done;
1213 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1214 if (err)
1215 goto done;
1217 err = cache_packidx(repo, *packidx, path_packidx);
1218 if (err)
1219 goto done;
1221 *idx = got_packidx_get_object_idx(*packidx, id);
1222 if (*idx != -1) {
1223 err = NULL; /* found the object */
1224 goto done;
1228 err = got_error_no_obj(id);
1229 done:
1230 return err;
1233 const struct got_error *
1234 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1235 struct got_repository *repo)
1237 const struct got_error *err = NULL;
1238 DIR *packdir = NULL;
1239 struct dirent *dent;
1240 char *path_packidx = NULL;
1241 int packdir_fd;
1243 packdir_fd = openat(got_repo_get_fd(repo),
1244 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1245 if (packdir_fd == -1) {
1246 return got_error_from_errno_fmt("openat: %s/%s",
1247 got_repo_get_path_git_dir(repo),
1248 GOT_OBJECTS_PACK_DIR);
1251 packdir = fdopendir(packdir_fd);
1252 if (packdir == NULL) {
1253 err = got_error_from_errno("fdopendir");
1254 goto done;
1257 while ((dent = readdir(packdir)) != NULL) {
1258 if (!got_repo_is_packidx_filename(dent->d_name,
1259 strlen(dent->d_name)))
1260 continue;
1262 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1263 dent->d_name) == -1) {
1264 err = got_error_from_errno("asprintf");
1265 path_packidx = NULL;
1266 break;
1269 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1270 if (err)
1271 break;
1273 done:
1274 if (err)
1275 free(path_packidx);
1276 if (packdir && closedir(packdir) != 0 && err == NULL)
1277 err = got_error_from_errno("closedir");
1278 return err;
1281 const struct got_error *
1282 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1283 struct got_repository *repo)
1285 const struct got_error *err;
1286 size_t i;
1288 *packidx = NULL;
1290 /* Search pack index cache. */
1291 for (i = 0; i < repo->pack_cache_size; i++) {
1292 if (repo->packidx_cache[i] == NULL)
1293 break;
1294 if (strcmp(repo->packidx_cache[i]->path_packidx,
1295 path_packidx) == 0) {
1296 *packidx = repo->packidx_cache[i];
1297 return NULL;
1300 /* No luck. Search the filesystem. */
1302 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1303 path_packidx, 0);
1304 if (err)
1305 return err;
1307 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1308 if (err)
1309 goto done;
1311 err = cache_packidx(repo, *packidx, path_packidx);
1312 done:
1313 if (err) {
1314 got_packidx_close(*packidx);
1315 *packidx = NULL;
1317 return err;
1320 static const struct got_error *
1321 read_packfile_hdr(int fd, struct got_packidx *packidx)
1323 const struct got_error *err = NULL;
1324 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1325 struct got_packfile_hdr hdr;
1326 ssize_t n;
1328 n = read(fd, &hdr, sizeof(hdr));
1329 if (n < 0)
1330 return got_error_from_errno("read");
1331 if (n != sizeof(hdr))
1332 return got_error(GOT_ERR_BAD_PACKFILE);
1334 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1335 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1336 be32toh(hdr.nobjects) != totobj)
1337 err = got_error(GOT_ERR_BAD_PACKFILE);
1339 return err;
1342 static const struct got_error *
1343 open_packfile(int *fd, struct got_repository *repo,
1344 const char *relpath, struct got_packidx *packidx)
1346 const struct got_error *err = NULL;
1348 *fd = openat(got_repo_get_fd(repo), relpath,
1349 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1350 if (*fd == -1)
1351 return got_error_from_errno_fmt("openat: %s/%s",
1352 got_repo_get_path_git_dir(repo), relpath);
1354 if (packidx) {
1355 err = read_packfile_hdr(*fd, packidx);
1356 if (err) {
1357 close(*fd);
1358 *fd = -1;
1362 return err;
1365 const struct got_error *
1366 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1367 const char *path_packfile, struct got_packidx *packidx)
1369 const struct got_error *err = NULL;
1370 struct got_pack *pack = NULL;
1371 struct stat sb;
1372 size_t i;
1374 if (packp)
1375 *packp = NULL;
1377 for (i = 0; i < repo->pack_cache_size; i++) {
1378 pack = &repo->packs[i];
1379 if (pack->path_packfile == NULL)
1380 break;
1381 if (strcmp(pack->path_packfile, path_packfile) == 0)
1382 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1385 if (i == repo->pack_cache_size) {
1386 struct got_pack tmp;
1387 do {
1388 i--;
1389 } while (i > 0 && repo->pinned_pack >= 0 &&
1390 i == repo->pinned_pack);
1391 err = got_pack_close(&repo->packs[i]);
1392 if (err)
1393 return err;
1394 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1395 return got_error_from_errno("ftruncate");
1396 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1397 return got_error_from_errno("ftruncate");
1398 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1399 memcpy(&repo->packs[i], &repo->packs[0],
1400 sizeof(repo->packs[i]));
1401 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1402 if (repo->pinned_pack == 0)
1403 repo->pinned_pack = i;
1404 else if (repo->pinned_pack == i)
1405 repo->pinned_pack = 0;
1406 i = 0;
1409 pack = &repo->packs[i];
1411 pack->path_packfile = strdup(path_packfile);
1412 if (pack->path_packfile == NULL) {
1413 err = got_error_from_errno("strdup");
1414 goto done;
1417 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1418 if (err)
1419 goto done;
1421 if (fstat(pack->fd, &sb) != 0) {
1422 err = got_error_from_errno("fstat");
1423 goto done;
1425 pack->filesize = sb.st_size;
1427 pack->privsep_child = NULL;
1429 #ifndef GOT_PACK_NO_MMAP
1430 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1431 pack->fd, 0);
1432 if (pack->map == MAP_FAILED) {
1433 if (errno != ENOMEM) {
1434 err = got_error_from_errno("mmap");
1435 goto done;
1437 pack->map = NULL; /* fall back to read(2) */
1439 #endif
1440 done:
1441 if (err) {
1442 if (pack) {
1443 free(pack->path_packfile);
1444 memset(pack, 0, sizeof(*pack));
1446 } else if (packp)
1447 *packp = pack;
1448 return err;
1451 struct got_pack *
1452 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1454 struct got_pack *pack = NULL;
1455 size_t i;
1457 for (i = 0; i < repo->pack_cache_size; i++) {
1458 pack = &repo->packs[i];
1459 if (pack->path_packfile == NULL)
1460 break;
1461 if (strcmp(pack->path_packfile, path_packfile) == 0)
1462 return pack;
1465 return NULL;
1468 const struct got_error *
1469 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1470 struct got_pack *pack)
1472 size_t i;
1473 int pinned_pack = -1, pinned_packidx = -1;
1475 for (i = 0; i < repo->pack_cache_size; i++) {
1476 if (repo->packidx_cache[i] &&
1477 strcmp(repo->packidx_cache[i]->path_packidx,
1478 packidx->path_packidx) == 0)
1479 pinned_packidx = i;
1480 if (repo->packs[i].path_packfile &&
1481 strcmp(repo->packs[i].path_packfile,
1482 pack->path_packfile) == 0)
1483 pinned_pack = i;
1486 if (pinned_packidx == -1 || pinned_pack == -1)
1487 return got_error(GOT_ERR_PIN_PACK);
1489 repo->pinned_pack = pinned_pack;
1490 repo->pinned_packidx = pinned_packidx;
1491 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1492 return NULL;
1495 struct got_pack *
1496 got_repo_get_pinned_pack(struct got_repository *repo)
1498 if (repo->pinned_pack >= 0 &&
1499 repo->pinned_pack < repo->pack_cache_size)
1500 return &repo->packs[repo->pinned_pack];
1502 return NULL;
1505 void
1506 got_repo_unpin_pack(struct got_repository *repo)
1508 repo->pinned_packidx = -1;
1509 repo->pinned_pack = -1;
1510 repo->pinned_pid = 0;
1513 const struct got_error *
1514 got_repo_init(const char *repo_path)
1516 const struct got_error *err = NULL;
1517 const char *dirnames[] = {
1518 GOT_OBJECTS_DIR,
1519 GOT_OBJECTS_PACK_DIR,
1520 GOT_REFS_DIR,
1522 const char *description_str = "Unnamed repository; "
1523 "edit this file 'description' to name the repository.";
1524 const char *headref_str = "ref: refs/heads/main";
1525 const char *gitconfig_str = "[core]\n"
1526 "\trepositoryformatversion = 0\n"
1527 "\tfilemode = true\n"
1528 "\tbare = true\n";
1529 char *path;
1530 size_t i;
1532 if (!got_path_dir_is_empty(repo_path))
1533 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1535 for (i = 0; i < nitems(dirnames); i++) {
1536 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1537 return got_error_from_errno("asprintf");
1539 err = got_path_mkdir(path);
1540 free(path);
1541 if (err)
1542 return err;
1545 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1546 return got_error_from_errno("asprintf");
1547 err = got_path_create_file(path, description_str);
1548 free(path);
1549 if (err)
1550 return err;
1552 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1553 return got_error_from_errno("asprintf");
1554 err = got_path_create_file(path, headref_str);
1555 free(path);
1556 if (err)
1557 return err;
1559 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1560 return got_error_from_errno("asprintf");
1561 err = got_path_create_file(path, gitconfig_str);
1562 free(path);
1563 if (err)
1564 return err;
1566 return NULL;
1569 static const struct got_error *
1570 match_packed_object(struct got_object_id **unique_id,
1571 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1573 const struct got_error *err = NULL;
1574 struct got_object_id_queue matched_ids;
1575 struct got_pathlist_entry *pe;
1577 STAILQ_INIT(&matched_ids);
1579 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1580 const char *path_packidx = pe->path;
1581 struct got_packidx *packidx;
1582 struct got_object_qid *qid;
1584 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1585 path_packidx, 0);
1586 if (err)
1587 break;
1589 err = got_packidx_match_id_str_prefix(&matched_ids,
1590 packidx, id_str_prefix);
1591 if (err) {
1592 got_packidx_close(packidx);
1593 break;
1595 err = got_packidx_close(packidx);
1596 if (err)
1597 break;
1599 STAILQ_FOREACH(qid, &matched_ids, entry) {
1600 if (obj_type != GOT_OBJ_TYPE_ANY) {
1601 int matched_type;
1602 err = got_object_get_type(&matched_type, repo,
1603 &qid->id);
1604 if (err)
1605 goto done;
1606 if (matched_type != obj_type)
1607 continue;
1609 if (*unique_id == NULL) {
1610 *unique_id = got_object_id_dup(&qid->id);
1611 if (*unique_id == NULL) {
1612 err = got_error_from_errno("malloc");
1613 goto done;
1615 } else {
1616 if (got_object_id_cmp(*unique_id,
1617 &qid->id) == 0)
1618 continue; /* packed multiple times */
1619 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1620 goto done;
1624 done:
1625 got_object_id_queue_free(&matched_ids);
1626 if (err) {
1627 free(*unique_id);
1628 *unique_id = NULL;
1630 return err;
1633 static const struct got_error *
1634 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1635 const char *object_dir, const char *id_str_prefix, int obj_type,
1636 struct got_repository *repo)
1638 const struct got_error *err = NULL;
1639 char *path;
1640 DIR *dir = NULL;
1641 struct dirent *dent;
1642 struct got_object_id id;
1644 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 goto done;
1649 dir = opendir(path);
1650 if (dir == NULL) {
1651 if (errno == ENOENT) {
1652 err = NULL;
1653 goto done;
1655 err = got_error_from_errno2("opendir", path);
1656 goto done;
1658 while ((dent = readdir(dir)) != NULL) {
1659 char *id_str;
1660 int cmp;
1662 if (strcmp(dent->d_name, ".") == 0 ||
1663 strcmp(dent->d_name, "..") == 0)
1664 continue;
1666 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1667 err = got_error_from_errno("asprintf");
1668 goto done;
1671 if (!got_parse_sha1_digest(id.sha1, id_str))
1672 continue;
1675 * Directory entries do not necessarily appear in
1676 * sorted order, so we must iterate over all of them.
1678 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1679 if (cmp != 0) {
1680 free(id_str);
1681 continue;
1684 if (*unique_id == NULL) {
1685 if (obj_type != GOT_OBJ_TYPE_ANY) {
1686 int matched_type;
1687 err = got_object_get_type(&matched_type, repo,
1688 &id);
1689 if (err)
1690 goto done;
1691 if (matched_type != obj_type)
1692 continue;
1694 *unique_id = got_object_id_dup(&id);
1695 if (*unique_id == NULL) {
1696 err = got_error_from_errno("got_object_id_dup");
1697 free(id_str);
1698 goto done;
1700 } else {
1701 if (got_object_id_cmp(*unique_id, &id) == 0)
1702 continue; /* both packed and loose */
1703 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1704 free(id_str);
1705 goto done;
1708 done:
1709 if (dir && closedir(dir) != 0 && err == NULL)
1710 err = got_error_from_errno("closedir");
1711 if (err) {
1712 free(*unique_id);
1713 *unique_id = NULL;
1715 free(path);
1716 return err;
1719 const struct got_error *
1720 got_repo_match_object_id_prefix(struct got_object_id **id,
1721 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 char *path_objects = got_repo_get_path_objects(repo);
1725 char *object_dir = NULL;
1726 size_t len;
1727 int i;
1729 *id = NULL;
1731 len = strlen(id_str_prefix);
1732 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1733 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1735 for (i = 0; i < len; i++) {
1736 if (isxdigit((unsigned char)id_str_prefix[i]))
1737 continue;
1738 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1741 if (len >= 2) {
1742 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1743 if (err)
1744 goto done;
1745 object_dir = strndup(id_str_prefix, 2);
1746 if (object_dir == NULL) {
1747 err = got_error_from_errno("strdup");
1748 goto done;
1750 err = match_loose_object(id, path_objects, object_dir,
1751 id_str_prefix, obj_type, repo);
1752 } else if (len == 1) {
1753 int i;
1754 for (i = 0; i < 0xf; i++) {
1755 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1756 == -1) {
1757 err = got_error_from_errno("asprintf");
1758 goto done;
1760 err = match_packed_object(id, repo, object_dir,
1761 obj_type);
1762 if (err)
1763 goto done;
1764 err = match_loose_object(id, path_objects, object_dir,
1765 id_str_prefix, obj_type, repo);
1766 if (err)
1767 goto done;
1769 } else {
1770 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1771 goto done;
1773 done:
1774 free(object_dir);
1775 if (err) {
1776 free(*id);
1777 *id = NULL;
1778 } else if (*id == NULL) {
1779 switch (obj_type) {
1780 case GOT_OBJ_TYPE_BLOB:
1781 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1782 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1783 break;
1784 case GOT_OBJ_TYPE_TREE:
1785 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1786 GOT_OBJ_LABEL_TREE, id_str_prefix);
1787 break;
1788 case GOT_OBJ_TYPE_COMMIT:
1789 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1790 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1791 break;
1792 case GOT_OBJ_TYPE_TAG:
1793 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1794 GOT_OBJ_LABEL_TAG, id_str_prefix);
1795 break;
1796 default:
1797 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1798 break;
1802 return err;
1805 const struct got_error *
1806 got_repo_match_object_id(struct got_object_id **id, char **label,
1807 const char *id_str, int obj_type, struct got_reflist_head *refs,
1808 struct got_repository *repo)
1810 const struct got_error *err;
1811 struct got_tag_object *tag;
1812 struct got_reference *ref = NULL;
1814 *id = NULL;
1815 if (label)
1816 *label = NULL;
1818 if (refs) {
1819 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1820 refs, repo);
1821 if (err == NULL) {
1822 *id = got_object_id_dup(
1823 got_object_tag_get_object_id(tag));
1824 if (*id == NULL)
1825 err = got_error_from_errno("got_object_id_dup");
1826 else if (label && asprintf(label, "refs/tags/%s",
1827 got_object_tag_get_name(tag)) == -1) {
1828 err = got_error_from_errno("asprintf");
1829 free(*id);
1830 *id = NULL;
1832 got_object_tag_close(tag);
1833 return err;
1834 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1835 err->code != GOT_ERR_NO_OBJ)
1836 return err;
1839 err = got_ref_open(&ref, repo, id_str, 0);
1840 if (err == NULL) {
1841 err = got_ref_resolve(id, repo, ref);
1842 if (err)
1843 goto done;
1844 if (label) {
1845 *label = strdup(got_ref_get_name(ref));
1846 if (*label == NULL) {
1847 err = got_error_from_errno("strdup");
1848 goto done;
1851 } else {
1852 if (err->code != GOT_ERR_NOT_REF &&
1853 err->code != GOT_ERR_BAD_REF_NAME)
1854 goto done;
1855 err = got_repo_match_object_id_prefix(id, id_str,
1856 obj_type, repo);
1857 if (err) {
1858 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1859 err = got_error_not_ref(id_str);
1860 goto done;
1862 if (label) {
1863 err = got_object_id_str(label, *id);
1864 if (*label == NULL) {
1865 err = got_error_from_errno("strdup");
1866 goto done;
1870 done:
1871 if (ref)
1872 got_ref_close(ref);
1873 return err;
1876 const struct got_error *
1877 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1878 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1880 const struct got_error *err = NULL;
1881 struct got_reflist_entry *re;
1882 struct got_object_id *tag_id;
1883 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1885 *tag = NULL;
1887 TAILQ_FOREACH(re, refs, entry) {
1888 const char *refname;
1889 refname = got_ref_get_name(re->ref);
1890 if (got_ref_is_symbolic(re->ref))
1891 continue;
1892 if (strncmp(refname, "refs/tags/", 10) != 0)
1893 continue;
1894 if (!name_is_absolute)
1895 refname += strlen("refs/tags/");
1896 if (strcmp(refname, name) != 0)
1897 continue;
1898 err = got_ref_resolve(&tag_id, repo, re->ref);
1899 if (err)
1900 break;
1901 err = got_object_open_as_tag(tag, repo, tag_id);
1902 free(tag_id);
1903 if (err)
1904 break;
1905 if (obj_type == GOT_OBJ_TYPE_ANY ||
1906 got_object_tag_get_object_type(*tag) == obj_type)
1907 break;
1908 got_object_tag_close(*tag);
1909 *tag = NULL;
1912 if (err == NULL && *tag == NULL)
1913 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1914 GOT_OBJ_LABEL_TAG, name);
1915 return err;
1918 static const struct got_error *
1919 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1920 const char *name, mode_t mode, struct got_object_id *blob_id)
1922 const struct got_error *err = NULL;
1924 *new_te = NULL;
1926 *new_te = calloc(1, sizeof(**new_te));
1927 if (*new_te == NULL)
1928 return got_error_from_errno("calloc");
1930 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1931 sizeof((*new_te)->name)) {
1932 err = got_error(GOT_ERR_NO_SPACE);
1933 goto done;
1936 if (S_ISLNK(mode)) {
1937 (*new_te)->mode = S_IFLNK;
1938 } else {
1939 (*new_te)->mode = S_IFREG;
1940 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1942 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1943 done:
1944 if (err && *new_te) {
1945 free(*new_te);
1946 *new_te = NULL;
1948 return err;
1951 static const struct got_error *
1952 import_file(struct got_tree_entry **new_te, struct dirent *de,
1953 const char *path, struct got_repository *repo)
1955 const struct got_error *err;
1956 struct got_object_id *blob_id = NULL;
1957 char *filepath;
1958 struct stat sb;
1960 if (asprintf(&filepath, "%s%s%s", path,
1961 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1962 return got_error_from_errno("asprintf");
1964 if (lstat(filepath, &sb) != 0) {
1965 err = got_error_from_errno2("lstat", path);
1966 goto done;
1969 err = got_object_blob_create(&blob_id, filepath, repo);
1970 if (err)
1971 goto done;
1973 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1974 blob_id);
1975 done:
1976 free(filepath);
1977 if (err)
1978 free(blob_id);
1979 return err;
1982 static const struct got_error *
1983 insert_tree_entry(struct got_tree_entry *new_te,
1984 struct got_pathlist_head *paths)
1986 const struct got_error *err = NULL;
1987 struct got_pathlist_entry *new_pe;
1989 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1990 if (err)
1991 return err;
1992 if (new_pe == NULL)
1993 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1994 return NULL;
1997 static const struct got_error *write_tree(struct got_object_id **,
1998 const char *, struct got_pathlist_head *, struct got_repository *,
1999 got_repo_import_cb progress_cb, void *progress_arg);
2001 static const struct got_error *
2002 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2003 const char *path, struct got_pathlist_head *ignores,
2004 struct got_repository *repo,
2005 got_repo_import_cb progress_cb, void *progress_arg)
2007 const struct got_error *err;
2008 struct got_object_id *id = NULL;
2009 char *subdirpath;
2011 if (asprintf(&subdirpath, "%s%s%s", path,
2012 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2013 return got_error_from_errno("asprintf");
2015 (*new_te) = calloc(1, sizeof(**new_te));
2016 if (*new_te == NULL)
2017 return got_error_from_errno("calloc");
2018 (*new_te)->mode = S_IFDIR;
2019 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2020 sizeof((*new_te)->name)) {
2021 err = got_error(GOT_ERR_NO_SPACE);
2022 goto done;
2024 err = write_tree(&id, subdirpath, ignores, repo,
2025 progress_cb, progress_arg);
2026 if (err)
2027 goto done;
2028 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2030 done:
2031 free(id);
2032 free(subdirpath);
2033 if (err) {
2034 free(*new_te);
2035 *new_te = NULL;
2037 return err;
2040 static const struct got_error *
2041 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2042 struct got_pathlist_head *ignores, struct got_repository *repo,
2043 got_repo_import_cb progress_cb, void *progress_arg)
2045 const struct got_error *err = NULL;
2046 DIR *dir;
2047 struct dirent *de;
2048 int nentries;
2049 struct got_tree_entry *new_te = NULL;
2050 struct got_pathlist_head paths;
2051 struct got_pathlist_entry *pe;
2053 *new_tree_id = NULL;
2055 TAILQ_INIT(&paths);
2057 dir = opendir(path_dir);
2058 if (dir == NULL) {
2059 err = got_error_from_errno2("opendir", path_dir);
2060 goto done;
2063 nentries = 0;
2064 while ((de = readdir(dir)) != NULL) {
2065 int ignore = 0;
2066 int type;
2068 if (strcmp(de->d_name, ".") == 0 ||
2069 strcmp(de->d_name, "..") == 0)
2070 continue;
2072 TAILQ_FOREACH(pe, ignores, entry) {
2073 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2074 ignore = 1;
2075 break;
2078 if (ignore)
2079 continue;
2081 err = got_path_dirent_type(&type, path_dir, de);
2082 if (err)
2083 goto done;
2085 if (type == DT_DIR) {
2086 err = import_subdir(&new_te, de, path_dir,
2087 ignores, repo, progress_cb, progress_arg);
2088 if (err) {
2089 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2090 goto done;
2091 err = NULL;
2092 continue;
2094 } else if (type == DT_REG || type == DT_LNK) {
2095 err = import_file(&new_te, de, path_dir, repo);
2096 if (err)
2097 goto done;
2098 } else
2099 continue;
2101 err = insert_tree_entry(new_te, &paths);
2102 if (err)
2103 goto done;
2104 nentries++;
2107 if (TAILQ_EMPTY(&paths)) {
2108 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2109 "cannot create tree without any entries");
2110 goto done;
2113 TAILQ_FOREACH(pe, &paths, entry) {
2114 struct got_tree_entry *te = pe->data;
2115 char *path;
2116 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2117 continue;
2118 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2119 err = got_error_from_errno("asprintf");
2120 goto done;
2122 err = (*progress_cb)(progress_arg, path);
2123 free(path);
2124 if (err)
2125 goto done;
2128 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2129 done:
2130 if (dir)
2131 closedir(dir);
2132 got_pathlist_free(&paths);
2133 return err;
2136 const struct got_error *
2137 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2138 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2139 struct got_repository *repo, got_repo_import_cb progress_cb,
2140 void *progress_arg)
2142 const struct got_error *err;
2143 struct got_object_id *new_tree_id;
2145 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2146 progress_cb, progress_arg);
2147 if (err)
2148 return err;
2150 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2151 author, time(NULL), author, time(NULL), logmsg, repo);
2152 free(new_tree_id);
2153 return err;
2156 const struct got_error *
2157 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2158 struct got_repository *repo)
2160 const struct got_error *err = NULL;
2161 char *path_objects = NULL, *path = NULL;
2162 DIR *dir = NULL;
2163 struct got_object_id id;
2164 int i;
2166 *nobjects = 0;
2167 *ondisk_size = 0;
2169 path_objects = got_repo_get_path_objects(repo);
2170 if (path_objects == NULL)
2171 return got_error_from_errno("got_repo_get_path_objects");
2173 for (i = 0; i <= 0xff; i++) {
2174 struct dirent *dent;
2176 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2177 err = got_error_from_errno("asprintf");
2178 break;
2181 dir = opendir(path);
2182 if (dir == NULL) {
2183 if (errno == ENOENT) {
2184 err = NULL;
2185 continue;
2187 err = got_error_from_errno2("opendir", path);
2188 break;
2191 while ((dent = readdir(dir)) != NULL) {
2192 char *id_str;
2193 int fd;
2194 struct stat sb;
2196 if (strcmp(dent->d_name, ".") == 0 ||
2197 strcmp(dent->d_name, "..") == 0)
2198 continue;
2200 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2201 err = got_error_from_errno("asprintf");
2202 goto done;
2205 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2206 free(id_str);
2207 continue;
2209 free(id_str);
2211 err = got_object_open_loose_fd(&fd, &id, repo);
2212 if (err)
2213 goto done;
2215 if (fstat(fd, &sb) == -1) {
2216 err = got_error_from_errno("fstat");
2217 close(fd);
2218 goto done;
2220 (*nobjects)++;
2221 (*ondisk_size) += sb.st_size;
2223 if (close(fd) == -1) {
2224 err = got_error_from_errno("close");
2225 goto done;
2229 if (closedir(dir) != 0) {
2230 err = got_error_from_errno("closedir");
2231 goto done;
2233 dir = NULL;
2235 free(path);
2236 path = NULL;
2238 done:
2239 if (dir && closedir(dir) != 0 && err == NULL)
2240 err = got_error_from_errno("closedir");
2242 if (err) {
2243 *nobjects = 0;
2244 *ondisk_size = 0;
2246 free(path_objects);
2247 free(path);
2248 return err;
2251 const struct got_error *
2252 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2253 off_t *total_packsize, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 DIR *packdir = NULL;
2257 struct dirent *dent;
2258 struct got_packidx *packidx = NULL;
2259 char *path_packidx;
2260 char *path_packfile;
2261 int packdir_fd;
2262 struct stat sb;
2264 *npackfiles = 0;
2265 *nobjects = 0;
2266 *total_packsize = 0;
2268 packdir_fd = openat(got_repo_get_fd(repo),
2269 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2270 if (packdir_fd == -1) {
2271 return got_error_from_errno_fmt("openat: %s/%s",
2272 got_repo_get_path_git_dir(repo),
2273 GOT_OBJECTS_PACK_DIR);
2276 packdir = fdopendir(packdir_fd);
2277 if (packdir == NULL) {
2278 err = got_error_from_errno("fdopendir");
2279 goto done;
2282 while ((dent = readdir(packdir)) != NULL) {
2283 if (!got_repo_is_packidx_filename(dent->d_name,
2284 strlen(dent->d_name)))
2285 continue;
2287 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2288 dent->d_name) == -1) {
2289 err = got_error_from_errno("asprintf");
2290 goto done;
2293 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2294 path_packidx, 0);
2295 free(path_packidx);
2296 if (err)
2297 goto done;
2299 if (fstat(packidx->fd, &sb) == -1)
2300 goto done;
2301 *total_packsize += sb.st_size;
2303 err = got_packidx_get_packfile_path(&path_packfile,
2304 packidx->path_packidx);
2305 if (err)
2306 goto done;
2308 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2309 0) == -1) {
2310 free(path_packfile);
2311 goto done;
2313 free(path_packfile);
2314 *total_packsize += sb.st_size;
2316 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2318 (*npackfiles)++;
2320 got_packidx_close(packidx);
2321 packidx = NULL;
2323 done:
2324 if (packidx)
2325 got_packidx_close(packidx);
2326 if (packdir && closedir(packdir) != 0 && err == NULL)
2327 err = got_error_from_errno("closedir");
2328 if (err) {
2329 *npackfiles = 0;
2330 *nobjects = 0;
2331 *total_packsize = 0;
2333 return err;
2336 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2337 got_packidx_bloom_filter_cmp);