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 for (i = 0; i < repo->nextensions; i++) {
789 char *ext = repo->extensions[i];
790 int j, supported = 0;
791 for (j = 0; j < nitems(repo_extensions); j++) {
792 if (strcmp(ext, repo_extensions[j]) == 0) {
793 supported = 1;
794 break;
797 if (!supported) {
798 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
799 goto done;
803 err = got_repo_list_packidx(&repo->packidx_paths, repo);
804 done:
805 if (err)
806 got_repo_close(repo);
807 else
808 *repop = repo;
809 free(repo_path);
810 return err;
813 const struct got_error *
814 got_repo_close(struct got_repository *repo)
816 const struct got_error *err = NULL, *child_err;
817 struct got_packidx_bloom_filter *bf;
818 struct got_pathlist_entry *pe;
819 size_t i;
821 for (i = 0; i < repo->pack_cache_size; i++) {
822 if (repo->packidx_cache[i] == NULL)
823 break;
824 got_packidx_close(repo->packidx_cache[i]);
827 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
828 &repo->packidx_bloom_filters))) {
829 RB_REMOVE(got_packidx_bloom_filter_tree,
830 &repo->packidx_bloom_filters, bf);
831 free(bf->bloom);
832 free(bf);
835 for (i = 0; i < repo->pack_cache_size; i++)
836 if (repo->packs[i].path_packfile)
837 if (repo->packs[i].path_packfile)
838 got_pack_close(&repo->packs[i]);
840 free(repo->path);
841 free(repo->path_git_dir);
843 got_object_cache_close(&repo->objcache);
844 got_object_cache_close(&repo->treecache);
845 got_object_cache_close(&repo->commitcache);
846 got_object_cache_close(&repo->tagcache);
847 got_object_cache_close(&repo->rawcache);
849 for (i = 0; i < nitems(repo->privsep_children); i++) {
850 if (repo->privsep_children[i].imsg_fd == -1)
851 continue;
852 imsg_clear(repo->privsep_children[i].ibuf);
853 free(repo->privsep_children[i].ibuf);
854 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
855 child_err = got_privsep_wait_for_child(
856 repo->privsep_children[i].pid);
857 if (child_err && err == NULL)
858 err = child_err;
859 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
860 err == NULL)
861 err = got_error_from_errno("close");
864 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
865 err == NULL)
866 err = got_error_from_errno("close");
868 if (repo->gotconfig)
869 got_gotconfig_free(repo->gotconfig);
870 free(repo->gitconfig_author_name);
871 free(repo->gitconfig_author_email);
872 for (i = 0; i < repo->ngitconfig_remotes; i++)
873 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
874 free(repo->gitconfig_remotes);
875 for (i = 0; i < repo->nextensions; i++)
876 free(repo->extensions[i]);
877 free(repo->extensions);
879 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
880 free((void *)pe->path);
881 got_pathlist_free(&repo->packidx_paths);
882 free(repo);
884 return err;
887 void
888 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
890 int i;
892 free(repo->name);
893 repo->name = NULL;
894 free(repo->fetch_url);
895 repo->fetch_url = NULL;
896 free(repo->send_url);
897 repo->send_url = NULL;
898 for (i = 0; i < repo->nfetch_branches; i++)
899 free(repo->fetch_branches[i]);
900 free(repo->fetch_branches);
901 repo->fetch_branches = NULL;
902 repo->nfetch_branches = 0;
903 for (i = 0; i < repo->nsend_branches; i++)
904 free(repo->send_branches[i]);
905 free(repo->send_branches);
906 repo->send_branches = NULL;
907 repo->nsend_branches = 0;
910 const struct got_error *
911 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
912 const char *input_path)
914 const struct got_error *err = NULL;
915 const char *repo_abspath = NULL;
916 size_t repolen, len;
917 char *canonpath, *path = NULL;
919 *in_repo_path = NULL;
921 canonpath = strdup(input_path);
922 if (canonpath == NULL) {
923 err = got_error_from_errno("strdup");
924 goto done;
926 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
927 if (err)
928 goto done;
930 repo_abspath = got_repo_get_path(repo);
932 if (canonpath[0] == '\0') {
933 path = strdup(canonpath);
934 if (path == NULL) {
935 err = got_error_from_errno("strdup");
936 goto done;
938 } else {
939 path = realpath(canonpath, NULL);
940 if (path == NULL) {
941 if (errno != ENOENT) {
942 err = got_error_from_errno2("realpath",
943 canonpath);
944 goto done;
946 /*
947 * Path is not on disk.
948 * Assume it is already relative to repository root.
949 */
950 path = strdup(canonpath);
951 if (path == NULL) {
952 err = got_error_from_errno("strdup");
953 goto done;
957 repolen = strlen(repo_abspath);
958 len = strlen(path);
961 if (strcmp(path, repo_abspath) == 0) {
962 free(path);
963 path = strdup("");
964 if (path == NULL) {
965 err = got_error_from_errno("strdup");
966 goto done;
968 } else if (len > repolen &&
969 got_path_is_child(path, repo_abspath, repolen)) {
970 /* Matched an on-disk path inside repository. */
971 if (got_repo_is_bare(repo)) {
972 /*
973 * Matched an on-disk path inside repository
974 * database. Treat input as repository-relative.
975 */
976 free(path);
977 path = canonpath;
978 canonpath = NULL;
979 } else {
980 char *child;
981 /* Strip common prefix with repository path. */
982 err = got_path_skip_common_ancestor(&child,
983 repo_abspath, path);
984 if (err)
985 goto done;
986 free(path);
987 path = child;
989 } else {
990 /*
991 * Matched unrelated on-disk path.
992 * Treat input as repository-relative.
993 */
994 free(path);
995 path = canonpath;
996 canonpath = NULL;
1000 /* Make in-repository path absolute */
1001 if (path[0] != '/') {
1002 char *abspath;
1003 if (asprintf(&abspath, "/%s", path) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1007 free(path);
1008 path = abspath;
1011 done:
1012 free(canonpath);
1013 if (err)
1014 free(path);
1015 else
1016 *in_repo_path = path;
1017 return err;
1020 static const struct got_error *
1021 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1022 const char *path_packidx)
1024 const struct got_error *err = NULL;
1025 size_t i;
1027 for (i = 0; i < repo->pack_cache_size; i++) {
1028 if (repo->packidx_cache[i] == NULL)
1029 break;
1030 if (strcmp(repo->packidx_cache[i]->path_packidx,
1031 path_packidx) == 0) {
1032 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1035 if (i == repo->pack_cache_size) {
1036 do {
1037 i--;
1038 } while (i > 0 && repo->pinned_packidx >= 0 &&
1039 i == repo->pinned_packidx);
1040 err = got_packidx_close(repo->packidx_cache[i]);
1041 if (err)
1042 return err;
1045 repo->packidx_cache[i] = packidx;
1047 return NULL;
1050 int
1051 got_repo_is_packidx_filename(const char *name, size_t len)
1053 if (len != GOT_PACKIDX_NAMELEN)
1054 return 0;
1056 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1057 return 0;
1059 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1060 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1061 return 0;
1063 return 1;
1066 static struct got_packidx_bloom_filter *
1067 get_packidx_bloom_filter(struct got_repository *repo,
1068 const char *path, size_t path_len)
1070 struct got_packidx_bloom_filter key;
1072 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1073 return NULL; /* XXX */
1074 key.path_len = path_len;
1076 return RB_FIND(got_packidx_bloom_filter_tree,
1077 &repo->packidx_bloom_filters, &key);
1080 int
1081 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path_packidx, struct got_object_id *id)
1084 struct got_packidx_bloom_filter *bf;
1086 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1087 if (bf)
1088 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1090 /* No bloom filter means this pack index must be searched. */
1091 return 1;
1094 static const struct got_error *
1095 add_packidx_bloom_filter(struct got_repository *repo,
1096 struct got_packidx *packidx, const char *path_packidx)
1098 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1099 struct got_packidx_bloom_filter *bf;
1100 size_t len;
1103 * Don't use bloom filters for very large pack index files.
1104 * Large pack files will contain a relatively large fraction
1105 * of our objects so we will likely need to visit them anyway.
1106 * The more objects a pack file contains the higher the probability
1107 * of a false-positive match from the bloom filter. And reading
1108 * all object IDs from a large pack index file can be expensive.
1110 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1111 return NULL;
1113 /* Do we already have a filter for this pack index? */
1114 if (get_packidx_bloom_filter(repo, path_packidx,
1115 strlen(path_packidx)) != NULL)
1116 return NULL;
1118 bf = calloc(1, sizeof(*bf));
1119 if (bf == NULL)
1120 return got_error_from_errno("calloc");
1121 bf->bloom = calloc(1, sizeof(*bf->bloom));
1122 if (bf->bloom == NULL) {
1123 free(bf);
1124 return got_error_from_errno("calloc");
1127 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1128 if (len >= sizeof(bf->path)) {
1129 free(bf->bloom);
1130 free(bf);
1131 return got_error(GOT_ERR_NO_SPACE);
1133 bf->path_len = len;
1135 /* Minimum size supported by our bloom filter is 1000 entries. */
1136 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1137 for (i = 0; i < nobjects; i++) {
1138 struct got_packidx_object_id *id;
1139 id = &packidx->hdr.sorted_ids[i];
1140 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1143 RB_INSERT(got_packidx_bloom_filter_tree,
1144 &repo->packidx_bloom_filters, bf);
1145 return NULL;
1148 const struct got_error *
1149 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1150 struct got_repository *repo, struct got_object_id *id)
1152 const struct got_error *err;
1153 struct got_pathlist_entry *pe;
1154 size_t i;
1156 /* Search pack index cache. */
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1159 break;
1160 if (!got_repo_check_packidx_bloom_filter(repo,
1161 repo->packidx_cache[i]->path_packidx, id))
1162 continue; /* object will not be found in this index */
1163 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1164 if (*idx != -1) {
1165 *packidx = repo->packidx_cache[i];
1167 * Move this cache entry to the front. Repeatedly
1168 * searching a wrong pack index can be expensive.
1170 if (i > 0) {
1171 memmove(&repo->packidx_cache[1],
1172 &repo->packidx_cache[0],
1173 i * sizeof(repo->packidx_cache[0]));
1174 repo->packidx_cache[0] = *packidx;
1175 if (repo->pinned_packidx >= 0 &&
1176 repo->pinned_packidx < i)
1177 repo->pinned_packidx++;
1178 else if (repo->pinned_packidx == i)
1179 repo->pinned_packidx = 0;
1181 return NULL;
1184 /* No luck. Search the filesystem. */
1186 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1187 const char *path_packidx = pe->path;
1188 int is_cached = 0;
1190 if (!got_repo_check_packidx_bloom_filter(repo,
1191 pe->path, id))
1192 continue; /* object will not be found in this index */
1194 for (i = 0; i < repo->pack_cache_size; i++) {
1195 if (repo->packidx_cache[i] == NULL)
1196 break;
1197 if (strcmp(repo->packidx_cache[i]->path_packidx,
1198 path_packidx) == 0) {
1199 is_cached = 1;
1200 break;
1203 if (is_cached)
1204 continue; /* already searched */
1206 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1207 path_packidx, 0);
1208 if (err)
1209 goto done;
1211 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1212 if (err)
1213 goto done;
1215 err = cache_packidx(repo, *packidx, path_packidx);
1216 if (err)
1217 goto done;
1219 *idx = got_packidx_get_object_idx(*packidx, id);
1220 if (*idx != -1) {
1221 err = NULL; /* found the object */
1222 goto done;
1226 err = got_error_no_obj(id);
1227 done:
1228 return err;
1231 const struct got_error *
1232 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1233 struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 DIR *packdir = NULL;
1237 struct dirent *dent;
1238 char *path_packidx = NULL;
1239 int packdir_fd;
1241 packdir_fd = openat(got_repo_get_fd(repo),
1242 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1243 if (packdir_fd == -1) {
1244 return got_error_from_errno_fmt("openat: %s/%s",
1245 got_repo_get_path_git_dir(repo),
1246 GOT_OBJECTS_PACK_DIR);
1249 packdir = fdopendir(packdir_fd);
1250 if (packdir == NULL) {
1251 err = got_error_from_errno("fdopendir");
1252 goto done;
1255 while ((dent = readdir(packdir)) != NULL) {
1256 if (!got_repo_is_packidx_filename(dent->d_name,
1257 strlen(dent->d_name)))
1258 continue;
1260 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1261 dent->d_name) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 path_packidx = NULL;
1264 break;
1267 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1268 if (err)
1269 break;
1271 done:
1272 if (err)
1273 free(path_packidx);
1274 if (packdir && closedir(packdir) != 0 && err == NULL)
1275 err = got_error_from_errno("closedir");
1276 return err;
1279 const struct got_error *
1280 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1281 struct got_repository *repo)
1283 const struct got_error *err;
1284 size_t i;
1286 *packidx = NULL;
1288 /* Search pack index cache. */
1289 for (i = 0; i < repo->pack_cache_size; i++) {
1290 if (repo->packidx_cache[i] == NULL)
1291 break;
1292 if (strcmp(repo->packidx_cache[i]->path_packidx,
1293 path_packidx) == 0) {
1294 *packidx = repo->packidx_cache[i];
1295 return NULL;
1298 /* No luck. Search the filesystem. */
1300 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1301 path_packidx, 0);
1302 if (err)
1303 return err;
1305 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1306 if (err)
1307 goto done;
1309 err = cache_packidx(repo, *packidx, path_packidx);
1310 done:
1311 if (err) {
1312 got_packidx_close(*packidx);
1313 *packidx = NULL;
1315 return err;
1318 static const struct got_error *
1319 read_packfile_hdr(int fd, struct got_packidx *packidx)
1321 const struct got_error *err = NULL;
1322 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1323 struct got_packfile_hdr hdr;
1324 ssize_t n;
1326 n = read(fd, &hdr, sizeof(hdr));
1327 if (n < 0)
1328 return got_error_from_errno("read");
1329 if (n != sizeof(hdr))
1330 return got_error(GOT_ERR_BAD_PACKFILE);
1332 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1333 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1334 be32toh(hdr.nobjects) != totobj)
1335 err = got_error(GOT_ERR_BAD_PACKFILE);
1337 return err;
1340 static const struct got_error *
1341 open_packfile(int *fd, struct got_repository *repo,
1342 const char *relpath, struct got_packidx *packidx)
1344 const struct got_error *err = NULL;
1346 *fd = openat(got_repo_get_fd(repo), relpath,
1347 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1348 if (*fd == -1)
1349 return got_error_from_errno_fmt("openat: %s/%s",
1350 got_repo_get_path_git_dir(repo), relpath);
1352 if (packidx) {
1353 err = read_packfile_hdr(*fd, packidx);
1354 if (err) {
1355 close(*fd);
1356 *fd = -1;
1360 return err;
1363 const struct got_error *
1364 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1365 const char *path_packfile, struct got_packidx *packidx)
1367 const struct got_error *err = NULL;
1368 struct got_pack *pack = NULL;
1369 struct stat sb;
1370 size_t i;
1372 if (packp)
1373 *packp = NULL;
1375 for (i = 0; i < repo->pack_cache_size; i++) {
1376 pack = &repo->packs[i];
1377 if (pack->path_packfile == NULL)
1378 break;
1379 if (strcmp(pack->path_packfile, path_packfile) == 0)
1380 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1383 if (i == repo->pack_cache_size) {
1384 struct got_pack tmp;
1385 do {
1386 i--;
1387 } while (i > 0 && repo->pinned_pack >= 0 &&
1388 i == repo->pinned_pack);
1389 err = got_pack_close(&repo->packs[i]);
1390 if (err)
1391 return err;
1392 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1393 return got_error_from_errno("ftruncate");
1394 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1395 return got_error_from_errno("ftruncate");
1396 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1397 memcpy(&repo->packs[i], &repo->packs[0],
1398 sizeof(repo->packs[i]));
1399 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1400 if (repo->pinned_pack == 0)
1401 repo->pinned_pack = i;
1402 else if (repo->pinned_pack == i)
1403 repo->pinned_pack = 0;
1404 i = 0;
1407 pack = &repo->packs[i];
1409 pack->path_packfile = strdup(path_packfile);
1410 if (pack->path_packfile == NULL) {
1411 err = got_error_from_errno("strdup");
1412 goto done;
1415 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1416 if (err)
1417 goto done;
1419 if (fstat(pack->fd, &sb) != 0) {
1420 err = got_error_from_errno("fstat");
1421 goto done;
1423 pack->filesize = sb.st_size;
1425 pack->privsep_child = NULL;
1427 #ifndef GOT_PACK_NO_MMAP
1428 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1429 pack->fd, 0);
1430 if (pack->map == MAP_FAILED) {
1431 if (errno != ENOMEM) {
1432 err = got_error_from_errno("mmap");
1433 goto done;
1435 pack->map = NULL; /* fall back to read(2) */
1437 #endif
1438 done:
1439 if (err) {
1440 if (pack) {
1441 free(pack->path_packfile);
1442 memset(pack, 0, sizeof(*pack));
1444 } else if (packp)
1445 *packp = pack;
1446 return err;
1449 struct got_pack *
1450 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1452 struct got_pack *pack = NULL;
1453 size_t i;
1455 for (i = 0; i < repo->pack_cache_size; i++) {
1456 pack = &repo->packs[i];
1457 if (pack->path_packfile == NULL)
1458 break;
1459 if (strcmp(pack->path_packfile, path_packfile) == 0)
1460 return pack;
1463 return NULL;
1466 const struct got_error *
1467 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1468 struct got_pack *pack)
1470 size_t i;
1471 int pinned_pack = -1, pinned_packidx = -1;
1473 for (i = 0; i < repo->pack_cache_size; i++) {
1474 if (repo->packidx_cache[i] &&
1475 strcmp(repo->packidx_cache[i]->path_packidx,
1476 packidx->path_packidx) == 0)
1477 pinned_packidx = i;
1478 if (repo->packs[i].path_packfile &&
1479 strcmp(repo->packs[i].path_packfile,
1480 pack->path_packfile) == 0)
1481 pinned_pack = i;
1484 if (pinned_packidx == -1 || pinned_pack == -1)
1485 return got_error(GOT_ERR_PIN_PACK);
1487 repo->pinned_pack = pinned_pack;
1488 repo->pinned_packidx = pinned_packidx;
1489 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1490 return NULL;
1493 struct got_pack *
1494 got_repo_get_pinned_pack(struct got_repository *repo)
1496 if (repo->pinned_pack >= 0 &&
1497 repo->pinned_pack < repo->pack_cache_size)
1498 return &repo->packs[repo->pinned_pack];
1500 return NULL;
1503 void
1504 got_repo_unpin_pack(struct got_repository *repo)
1506 repo->pinned_packidx = -1;
1507 repo->pinned_pack = -1;
1508 repo->pinned_pid = 0;
1511 const struct got_error *
1512 got_repo_init(const char *repo_path)
1514 const struct got_error *err = NULL;
1515 const char *dirnames[] = {
1516 GOT_OBJECTS_DIR,
1517 GOT_OBJECTS_PACK_DIR,
1518 GOT_REFS_DIR,
1520 const char *description_str = "Unnamed repository; "
1521 "edit this file 'description' to name the repository.";
1522 const char *headref_str = "ref: refs/heads/main";
1523 const char *gitconfig_str = "[core]\n"
1524 "\trepositoryformatversion = 0\n"
1525 "\tfilemode = true\n"
1526 "\tbare = true\n";
1527 char *path;
1528 size_t i;
1530 if (!got_path_dir_is_empty(repo_path))
1531 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1533 for (i = 0; i < nitems(dirnames); i++) {
1534 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1535 return got_error_from_errno("asprintf");
1537 err = got_path_mkdir(path);
1538 free(path);
1539 if (err)
1540 return err;
1543 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1544 return got_error_from_errno("asprintf");
1545 err = got_path_create_file(path, description_str);
1546 free(path);
1547 if (err)
1548 return err;
1550 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1551 return got_error_from_errno("asprintf");
1552 err = got_path_create_file(path, headref_str);
1553 free(path);
1554 if (err)
1555 return err;
1557 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1558 return got_error_from_errno("asprintf");
1559 err = got_path_create_file(path, gitconfig_str);
1560 free(path);
1561 if (err)
1562 return err;
1564 return NULL;
1567 static const struct got_error *
1568 match_packed_object(struct got_object_id **unique_id,
1569 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1571 const struct got_error *err = NULL;
1572 struct got_object_id_queue matched_ids;
1573 struct got_pathlist_entry *pe;
1575 STAILQ_INIT(&matched_ids);
1577 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1578 const char *path_packidx = pe->path;
1579 struct got_packidx *packidx;
1580 struct got_object_qid *qid;
1582 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1583 path_packidx, 0);
1584 if (err)
1585 break;
1587 err = got_packidx_match_id_str_prefix(&matched_ids,
1588 packidx, id_str_prefix);
1589 if (err) {
1590 got_packidx_close(packidx);
1591 break;
1593 err = got_packidx_close(packidx);
1594 if (err)
1595 break;
1597 STAILQ_FOREACH(qid, &matched_ids, entry) {
1598 if (obj_type != GOT_OBJ_TYPE_ANY) {
1599 int matched_type;
1600 err = got_object_get_type(&matched_type, repo,
1601 &qid->id);
1602 if (err)
1603 goto done;
1604 if (matched_type != obj_type)
1605 continue;
1607 if (*unique_id == NULL) {
1608 *unique_id = got_object_id_dup(&qid->id);
1609 if (*unique_id == NULL) {
1610 err = got_error_from_errno("malloc");
1611 goto done;
1613 } else {
1614 if (got_object_id_cmp(*unique_id,
1615 &qid->id) == 0)
1616 continue; /* packed multiple times */
1617 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1618 goto done;
1622 done:
1623 got_object_id_queue_free(&matched_ids);
1624 if (err) {
1625 free(*unique_id);
1626 *unique_id = NULL;
1628 return err;
1631 static const struct got_error *
1632 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1633 const char *object_dir, const char *id_str_prefix, int obj_type,
1634 struct got_repository *repo)
1636 const struct got_error *err = NULL;
1637 char *path;
1638 DIR *dir = NULL;
1639 struct dirent *dent;
1640 struct got_object_id id;
1642 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1643 err = got_error_from_errno("asprintf");
1644 goto done;
1647 dir = opendir(path);
1648 if (dir == NULL) {
1649 if (errno == ENOENT) {
1650 err = NULL;
1651 goto done;
1653 err = got_error_from_errno2("opendir", path);
1654 goto done;
1656 while ((dent = readdir(dir)) != NULL) {
1657 char *id_str;
1658 int cmp;
1660 if (strcmp(dent->d_name, ".") == 0 ||
1661 strcmp(dent->d_name, "..") == 0)
1662 continue;
1664 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1665 err = got_error_from_errno("asprintf");
1666 goto done;
1669 if (!got_parse_sha1_digest(id.sha1, id_str))
1670 continue;
1673 * Directory entries do not necessarily appear in
1674 * sorted order, so we must iterate over all of them.
1676 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1677 if (cmp != 0) {
1678 free(id_str);
1679 continue;
1682 if (*unique_id == NULL) {
1683 if (obj_type != GOT_OBJ_TYPE_ANY) {
1684 int matched_type;
1685 err = got_object_get_type(&matched_type, repo,
1686 &id);
1687 if (err)
1688 goto done;
1689 if (matched_type != obj_type)
1690 continue;
1692 *unique_id = got_object_id_dup(&id);
1693 if (*unique_id == NULL) {
1694 err = got_error_from_errno("got_object_id_dup");
1695 free(id_str);
1696 goto done;
1698 } else {
1699 if (got_object_id_cmp(*unique_id, &id) == 0)
1700 continue; /* both packed and loose */
1701 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1702 free(id_str);
1703 goto done;
1706 done:
1707 if (dir && closedir(dir) != 0 && err == NULL)
1708 err = got_error_from_errno("closedir");
1709 if (err) {
1710 free(*unique_id);
1711 *unique_id = NULL;
1713 free(path);
1714 return err;
1717 const struct got_error *
1718 got_repo_match_object_id_prefix(struct got_object_id **id,
1719 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 char *path_objects = got_repo_get_path_objects(repo);
1723 char *object_dir = NULL;
1724 size_t len;
1725 int i;
1727 *id = NULL;
1729 len = strlen(id_str_prefix);
1730 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1731 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1733 for (i = 0; i < len; i++) {
1734 if (isxdigit((unsigned char)id_str_prefix[i]))
1735 continue;
1736 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1739 if (len >= 2) {
1740 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1741 if (err)
1742 goto done;
1743 object_dir = strndup(id_str_prefix, 2);
1744 if (object_dir == NULL) {
1745 err = got_error_from_errno("strdup");
1746 goto done;
1748 err = match_loose_object(id, path_objects, object_dir,
1749 id_str_prefix, obj_type, repo);
1750 } else if (len == 1) {
1751 int i;
1752 for (i = 0; i < 0xf; i++) {
1753 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1754 == -1) {
1755 err = got_error_from_errno("asprintf");
1756 goto done;
1758 err = match_packed_object(id, repo, object_dir,
1759 obj_type);
1760 if (err)
1761 goto done;
1762 err = match_loose_object(id, path_objects, object_dir,
1763 id_str_prefix, obj_type, repo);
1764 if (err)
1765 goto done;
1767 } else {
1768 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1769 goto done;
1771 done:
1772 free(object_dir);
1773 if (err) {
1774 free(*id);
1775 *id = NULL;
1776 } else if (*id == NULL) {
1777 switch (obj_type) {
1778 case GOT_OBJ_TYPE_BLOB:
1779 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1780 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1781 break;
1782 case GOT_OBJ_TYPE_TREE:
1783 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1784 GOT_OBJ_LABEL_TREE, id_str_prefix);
1785 break;
1786 case GOT_OBJ_TYPE_COMMIT:
1787 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1788 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1789 break;
1790 case GOT_OBJ_TYPE_TAG:
1791 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1792 GOT_OBJ_LABEL_TAG, id_str_prefix);
1793 break;
1794 default:
1795 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1796 break;
1800 return err;
1803 const struct got_error *
1804 got_repo_match_object_id(struct got_object_id **id, char **label,
1805 const char *id_str, int obj_type, struct got_reflist_head *refs,
1806 struct got_repository *repo)
1808 const struct got_error *err;
1809 struct got_tag_object *tag;
1810 struct got_reference *ref = NULL;
1812 *id = NULL;
1813 if (label)
1814 *label = NULL;
1816 if (refs) {
1817 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1818 refs, repo);
1819 if (err == NULL) {
1820 *id = got_object_id_dup(
1821 got_object_tag_get_object_id(tag));
1822 if (*id == NULL)
1823 err = got_error_from_errno("got_object_id_dup");
1824 else if (label && asprintf(label, "refs/tags/%s",
1825 got_object_tag_get_name(tag)) == -1) {
1826 err = got_error_from_errno("asprintf");
1827 free(*id);
1828 *id = NULL;
1830 got_object_tag_close(tag);
1831 return err;
1832 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1833 err->code != GOT_ERR_NO_OBJ)
1834 return err;
1837 err = got_ref_open(&ref, repo, id_str, 0);
1838 if (err == NULL) {
1839 err = got_ref_resolve(id, repo, ref);
1840 if (err)
1841 goto done;
1842 if (label) {
1843 *label = strdup(got_ref_get_name(ref));
1844 if (*label == NULL) {
1845 err = got_error_from_errno("strdup");
1846 goto done;
1849 } else {
1850 if (err->code != GOT_ERR_NOT_REF &&
1851 err->code != GOT_ERR_BAD_REF_NAME)
1852 goto done;
1853 err = got_repo_match_object_id_prefix(id, id_str,
1854 obj_type, repo);
1855 if (err) {
1856 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1857 err = got_error_not_ref(id_str);
1858 goto done;
1860 if (label) {
1861 err = got_object_id_str(label, *id);
1862 if (*label == NULL) {
1863 err = got_error_from_errno("strdup");
1864 goto done;
1868 done:
1869 if (ref)
1870 got_ref_close(ref);
1871 return err;
1874 const struct got_error *
1875 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1876 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1878 const struct got_error *err = NULL;
1879 struct got_reflist_entry *re;
1880 struct got_object_id *tag_id;
1881 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1883 *tag = NULL;
1885 TAILQ_FOREACH(re, refs, entry) {
1886 const char *refname;
1887 refname = got_ref_get_name(re->ref);
1888 if (got_ref_is_symbolic(re->ref))
1889 continue;
1890 if (strncmp(refname, "refs/tags/", 10) != 0)
1891 continue;
1892 if (!name_is_absolute)
1893 refname += strlen("refs/tags/");
1894 if (strcmp(refname, name) != 0)
1895 continue;
1896 err = got_ref_resolve(&tag_id, repo, re->ref);
1897 if (err)
1898 break;
1899 err = got_object_open_as_tag(tag, repo, tag_id);
1900 free(tag_id);
1901 if (err)
1902 break;
1903 if (obj_type == GOT_OBJ_TYPE_ANY ||
1904 got_object_tag_get_object_type(*tag) == obj_type)
1905 break;
1906 got_object_tag_close(*tag);
1907 *tag = NULL;
1910 if (err == NULL && *tag == NULL)
1911 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1912 GOT_OBJ_LABEL_TAG, name);
1913 return err;
1916 static const struct got_error *
1917 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1918 const char *name, mode_t mode, struct got_object_id *blob_id)
1920 const struct got_error *err = NULL;
1922 *new_te = NULL;
1924 *new_te = calloc(1, sizeof(**new_te));
1925 if (*new_te == NULL)
1926 return got_error_from_errno("calloc");
1928 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1929 sizeof((*new_te)->name)) {
1930 err = got_error(GOT_ERR_NO_SPACE);
1931 goto done;
1934 if (S_ISLNK(mode)) {
1935 (*new_te)->mode = S_IFLNK;
1936 } else {
1937 (*new_te)->mode = S_IFREG;
1938 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1940 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1941 done:
1942 if (err && *new_te) {
1943 free(*new_te);
1944 *new_te = NULL;
1946 return err;
1949 static const struct got_error *
1950 import_file(struct got_tree_entry **new_te, struct dirent *de,
1951 const char *path, struct got_repository *repo)
1953 const struct got_error *err;
1954 struct got_object_id *blob_id = NULL;
1955 char *filepath;
1956 struct stat sb;
1958 if (asprintf(&filepath, "%s%s%s", path,
1959 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1960 return got_error_from_errno("asprintf");
1962 if (lstat(filepath, &sb) != 0) {
1963 err = got_error_from_errno2("lstat", path);
1964 goto done;
1967 err = got_object_blob_create(&blob_id, filepath, repo);
1968 if (err)
1969 goto done;
1971 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1972 blob_id);
1973 done:
1974 free(filepath);
1975 if (err)
1976 free(blob_id);
1977 return err;
1980 static const struct got_error *
1981 insert_tree_entry(struct got_tree_entry *new_te,
1982 struct got_pathlist_head *paths)
1984 const struct got_error *err = NULL;
1985 struct got_pathlist_entry *new_pe;
1987 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1988 if (err)
1989 return err;
1990 if (new_pe == NULL)
1991 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1992 return NULL;
1995 static const struct got_error *write_tree(struct got_object_id **,
1996 const char *, struct got_pathlist_head *, struct got_repository *,
1997 got_repo_import_cb progress_cb, void *progress_arg);
1999 static const struct got_error *
2000 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2001 const char *path, struct got_pathlist_head *ignores,
2002 struct got_repository *repo,
2003 got_repo_import_cb progress_cb, void *progress_arg)
2005 const struct got_error *err;
2006 struct got_object_id *id = NULL;
2007 char *subdirpath;
2009 if (asprintf(&subdirpath, "%s%s%s", path,
2010 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2011 return got_error_from_errno("asprintf");
2013 (*new_te) = calloc(1, sizeof(**new_te));
2014 if (*new_te == NULL)
2015 return got_error_from_errno("calloc");
2016 (*new_te)->mode = S_IFDIR;
2017 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2018 sizeof((*new_te)->name)) {
2019 err = got_error(GOT_ERR_NO_SPACE);
2020 goto done;
2022 err = write_tree(&id, subdirpath, ignores, repo,
2023 progress_cb, progress_arg);
2024 if (err)
2025 goto done;
2026 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2028 done:
2029 free(id);
2030 free(subdirpath);
2031 if (err) {
2032 free(*new_te);
2033 *new_te = NULL;
2035 return err;
2038 static const struct got_error *
2039 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2040 struct got_pathlist_head *ignores, struct got_repository *repo,
2041 got_repo_import_cb progress_cb, void *progress_arg)
2043 const struct got_error *err = NULL;
2044 DIR *dir;
2045 struct dirent *de;
2046 int nentries;
2047 struct got_tree_entry *new_te = NULL;
2048 struct got_pathlist_head paths;
2049 struct got_pathlist_entry *pe;
2051 *new_tree_id = NULL;
2053 TAILQ_INIT(&paths);
2055 dir = opendir(path_dir);
2056 if (dir == NULL) {
2057 err = got_error_from_errno2("opendir", path_dir);
2058 goto done;
2061 nentries = 0;
2062 while ((de = readdir(dir)) != NULL) {
2063 int ignore = 0;
2064 int type;
2066 if (strcmp(de->d_name, ".") == 0 ||
2067 strcmp(de->d_name, "..") == 0)
2068 continue;
2070 TAILQ_FOREACH(pe, ignores, entry) {
2071 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2072 ignore = 1;
2073 break;
2076 if (ignore)
2077 continue;
2079 err = got_path_dirent_type(&type, path_dir, de);
2080 if (err)
2081 goto done;
2083 if (type == DT_DIR) {
2084 err = import_subdir(&new_te, de, path_dir,
2085 ignores, repo, progress_cb, progress_arg);
2086 if (err) {
2087 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2088 goto done;
2089 err = NULL;
2090 continue;
2092 } else if (type == DT_REG || type == DT_LNK) {
2093 err = import_file(&new_te, de, path_dir, repo);
2094 if (err)
2095 goto done;
2096 } else
2097 continue;
2099 err = insert_tree_entry(new_te, &paths);
2100 if (err)
2101 goto done;
2102 nentries++;
2105 if (TAILQ_EMPTY(&paths)) {
2106 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2107 "cannot create tree without any entries");
2108 goto done;
2111 TAILQ_FOREACH(pe, &paths, entry) {
2112 struct got_tree_entry *te = pe->data;
2113 char *path;
2114 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2115 continue;
2116 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2117 err = got_error_from_errno("asprintf");
2118 goto done;
2120 err = (*progress_cb)(progress_arg, path);
2121 free(path);
2122 if (err)
2123 goto done;
2126 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2127 done:
2128 if (dir)
2129 closedir(dir);
2130 got_pathlist_free(&paths);
2131 return err;
2134 const struct got_error *
2135 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2136 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2137 struct got_repository *repo, got_repo_import_cb progress_cb,
2138 void *progress_arg)
2140 const struct got_error *err;
2141 struct got_object_id *new_tree_id;
2143 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2144 progress_cb, progress_arg);
2145 if (err)
2146 return err;
2148 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2149 author, time(NULL), author, time(NULL), logmsg, repo);
2150 free(new_tree_id);
2151 return err;
2154 const struct got_error *
2155 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2156 struct got_repository *repo)
2158 const struct got_error *err = NULL;
2159 char *path_objects = NULL, *path = NULL;
2160 DIR *dir = NULL;
2161 struct got_object_id id;
2162 int i;
2164 *nobjects = 0;
2165 *ondisk_size = 0;
2167 path_objects = got_repo_get_path_objects(repo);
2168 if (path_objects == NULL)
2169 return got_error_from_errno("got_repo_get_path_objects");
2171 for (i = 0; i <= 0xff; i++) {
2172 struct dirent *dent;
2174 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2175 err = got_error_from_errno("asprintf");
2176 break;
2179 dir = opendir(path);
2180 if (dir == NULL) {
2181 if (errno == ENOENT) {
2182 err = NULL;
2183 continue;
2185 err = got_error_from_errno2("opendir", path);
2186 break;
2189 while ((dent = readdir(dir)) != NULL) {
2190 char *id_str;
2191 int fd;
2192 struct stat sb;
2194 if (strcmp(dent->d_name, ".") == 0 ||
2195 strcmp(dent->d_name, "..") == 0)
2196 continue;
2198 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2199 err = got_error_from_errno("asprintf");
2200 goto done;
2203 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2204 free(id_str);
2205 continue;
2207 free(id_str);
2209 err = got_object_open_loose_fd(&fd, &id, repo);
2210 if (err)
2211 goto done;
2213 if (fstat(fd, &sb) == -1) {
2214 err = got_error_from_errno("fstat");
2215 close(fd);
2216 goto done;
2218 (*nobjects)++;
2219 (*ondisk_size) += sb.st_size;
2221 if (close(fd) == -1) {
2222 err = got_error_from_errno("close");
2223 goto done;
2227 if (closedir(dir) != 0) {
2228 err = got_error_from_errno("closedir");
2229 goto done;
2231 dir = NULL;
2233 free(path);
2234 path = NULL;
2236 done:
2237 if (dir && closedir(dir) != 0 && err == NULL)
2238 err = got_error_from_errno("closedir");
2240 if (err) {
2241 *nobjects = 0;
2242 *ondisk_size = 0;
2244 free(path_objects);
2245 free(path);
2246 return err;
2249 const struct got_error *
2250 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2251 off_t *total_packsize, struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 DIR *packdir = NULL;
2255 struct dirent *dent;
2256 struct got_packidx *packidx = NULL;
2257 char *path_packidx;
2258 char *path_packfile;
2259 int packdir_fd;
2260 struct stat sb;
2262 *npackfiles = 0;
2263 *nobjects = 0;
2264 *total_packsize = 0;
2266 packdir_fd = openat(got_repo_get_fd(repo),
2267 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2268 if (packdir_fd == -1) {
2269 return got_error_from_errno_fmt("openat: %s/%s",
2270 got_repo_get_path_git_dir(repo),
2271 GOT_OBJECTS_PACK_DIR);
2274 packdir = fdopendir(packdir_fd);
2275 if (packdir == NULL) {
2276 err = got_error_from_errno("fdopendir");
2277 goto done;
2280 while ((dent = readdir(packdir)) != NULL) {
2281 if (!got_repo_is_packidx_filename(dent->d_name,
2282 strlen(dent->d_name)))
2283 continue;
2285 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2286 dent->d_name) == -1) {
2287 err = got_error_from_errno("asprintf");
2288 goto done;
2291 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2292 path_packidx, 0);
2293 free(path_packidx);
2294 if (err)
2295 goto done;
2297 if (fstat(packidx->fd, &sb) == -1)
2298 goto done;
2299 *total_packsize += sb.st_size;
2301 err = got_packidx_get_packfile_path(&path_packfile,
2302 packidx->path_packidx);
2303 if (err)
2304 goto done;
2306 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2307 0) == -1) {
2308 free(path_packfile);
2309 goto done;
2311 free(path_packfile);
2312 *total_packsize += sb.st_size;
2314 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2316 (*npackfiles)++;
2318 got_packidx_close(packidx);
2319 packidx = NULL;
2321 done:
2322 if (packidx)
2323 got_packidx_close(packidx);
2324 if (packdir && closedir(packdir) != 0 && err == NULL)
2325 err = got_error_from_errno("closedir");
2326 if (err) {
2327 *npackfiles = 0;
2328 *nobjects = 0;
2329 *total_packsize = 0;
2331 return err;
2334 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2335 got_packidx_bloom_filter_cmp);