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;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
257 free(pack_fds_tmp);
258 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_error_from_errno("got_opentempfd");
265 got_repo_pack_fds_close(pack_fds_tmp);
266 return err;
269 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
270 return err;
273 const struct got_error *
274 got_repo_pack_fds_close(int *pack_fds)
276 const struct got_error *err = NULL;
277 int i;
279 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
280 if (pack_fds[i] == -1)
281 continue;
282 if (close(pack_fds[i]) == -1) {
283 err = got_error_from_errno("close");
284 break;
287 free(pack_fds);
288 return err;
291 const struct got_error *
292 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
293 struct got_object *obj)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->objcache, id, obj);
298 if (err) {
299 if (err->code == GOT_ERR_OBJ_EXISTS ||
300 err->code == GOT_ERR_OBJ_TOO_LARGE)
301 err = NULL;
302 return err;
304 obj->refcnt++;
305 #endif
306 return NULL;
309 struct got_object *
310 got_repo_get_cached_object(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
316 const struct got_error *
317 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
318 struct got_tree_object *tree)
320 #ifndef GOT_NO_OBJ_CACHE
321 const struct got_error *err = NULL;
322 err = got_object_cache_add(&repo->treecache, id, tree);
323 if (err) {
324 if (err->code == GOT_ERR_OBJ_EXISTS ||
325 err->code == GOT_ERR_OBJ_TOO_LARGE)
326 err = NULL;
327 return err;
329 tree->refcnt++;
330 #endif
331 return NULL;
334 struct got_tree_object *
335 got_repo_get_cached_tree(struct got_repository *repo,
336 struct got_object_id *id)
338 return (struct got_tree_object *)got_object_cache_get(
339 &repo->treecache, id);
342 const struct got_error *
343 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
344 struct got_commit_object *commit)
346 #ifndef GOT_NO_OBJ_CACHE
347 const struct got_error *err = NULL;
348 err = got_object_cache_add(&repo->commitcache, id, commit);
349 if (err) {
350 if (err->code == GOT_ERR_OBJ_EXISTS ||
351 err->code == GOT_ERR_OBJ_TOO_LARGE)
352 err = NULL;
353 return err;
355 commit->refcnt++;
356 #endif
357 return NULL;
360 struct got_commit_object *
361 got_repo_get_cached_commit(struct got_repository *repo,
362 struct got_object_id *id)
364 return (struct got_commit_object *)got_object_cache_get(
365 &repo->commitcache, id);
368 const struct got_error *
369 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
370 struct got_tag_object *tag)
372 #ifndef GOT_NO_OBJ_CACHE
373 const struct got_error *err = NULL;
374 err = got_object_cache_add(&repo->tagcache, id, tag);
375 if (err) {
376 if (err->code == GOT_ERR_OBJ_EXISTS ||
377 err->code == GOT_ERR_OBJ_TOO_LARGE)
378 err = NULL;
379 return err;
381 tag->refcnt++;
382 #endif
383 return NULL;
386 struct got_tag_object *
387 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
389 return (struct got_tag_object *)got_object_cache_get(
390 &repo->tagcache, id);
393 const struct got_error *
394 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
395 struct got_raw_object *raw)
397 #ifndef GOT_NO_OBJ_CACHE
398 const struct got_error *err = NULL;
399 err = got_object_cache_add(&repo->rawcache, id, raw);
400 if (err) {
401 if (err->code == GOT_ERR_OBJ_EXISTS ||
402 err->code == GOT_ERR_OBJ_TOO_LARGE)
403 err = NULL;
404 return err;
406 raw->refcnt++;
407 #endif
408 return NULL;
412 struct got_raw_object *
413 got_repo_get_cached_raw_object(struct got_repository *repo,
414 struct got_object_id *id)
416 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
420 static const struct got_error *
421 open_repo(struct got_repository *repo, const char *path)
423 const struct got_error *err = NULL;
425 repo->gitdir_fd = -1;
427 /* bare git repository? */
428 repo->path_git_dir = strdup(path);
429 if (repo->path_git_dir == NULL)
430 return got_error_from_errno("strdup");
431 if (is_git_repo(repo)) {
432 repo->path = strdup(repo->path_git_dir);
433 if (repo->path == NULL) {
434 err = got_error_from_errno("strdup");
435 goto done;
437 repo->gitdir_fd = open(repo->path_git_dir,
438 O_DIRECTORY | O_CLOEXEC);
439 if (repo->gitdir_fd == -1) {
440 err = got_error_from_errno2("open",
441 repo->path_git_dir);
442 goto done;
444 return NULL;
447 /* git repository with working tree? */
448 free(repo->path_git_dir);
449 repo->path_git_dir = NULL;
450 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
451 err = got_error_from_errno("asprintf");
452 goto done;
454 if (is_git_repo(repo)) {
455 repo->path = strdup(path);
456 if (repo->path == NULL) {
457 err = got_error_from_errno("strdup");
458 goto done;
460 repo->gitdir_fd = open(repo->path_git_dir,
461 O_DIRECTORY | O_CLOEXEC);
462 if (repo->gitdir_fd == -1) {
463 err = got_error_from_errno2("open",
464 repo->path_git_dir);
465 goto done;
467 return NULL;
470 err = got_error(GOT_ERR_NOT_GIT_REPO);
471 done:
472 if (err) {
473 free(repo->path);
474 repo->path = NULL;
475 free(repo->path_git_dir);
476 repo->path_git_dir = NULL;
477 if (repo->gitdir_fd != -1)
478 close(repo->gitdir_fd);
479 repo->gitdir_fd = -1;
482 return err;
485 static const struct got_error *
486 parse_gitconfig_file(int *gitconfig_repository_format_version,
487 char **gitconfig_author_name, char **gitconfig_author_email,
488 struct got_remote_repo **remotes, int *nremotes,
489 char **gitconfig_owner, char ***extensions, int *nextensions,
490 const char *gitconfig_path)
492 const struct got_error *err = NULL, *child_err = NULL;
493 int fd = -1;
494 int imsg_fds[2] = { -1, -1 };
495 pid_t pid;
496 struct imsgbuf *ibuf;
498 *gitconfig_repository_format_version = 0;
499 if (extensions)
500 *extensions = NULL;
501 if (nextensions)
502 *nextensions = 0;
503 *gitconfig_author_name = NULL;
504 *gitconfig_author_email = NULL;
505 if (remotes)
506 *remotes = NULL;
507 if (nremotes)
508 *nremotes = 0;
509 if (gitconfig_owner)
510 *gitconfig_owner = NULL;
512 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
513 if (fd == -1) {
514 if (errno == ENOENT)
515 return NULL;
516 return got_error_from_errno2("open", gitconfig_path);
519 ibuf = calloc(1, sizeof(*ibuf));
520 if (ibuf == NULL) {
521 err = got_error_from_errno("calloc");
522 goto done;
525 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
526 err = got_error_from_errno("socketpair");
527 goto done;
530 pid = fork();
531 if (pid == -1) {
532 err = got_error_from_errno("fork");
533 goto done;
534 } else if (pid == 0) {
535 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
536 gitconfig_path);
537 /* not reached */
540 if (close(imsg_fds[1]) == -1) {
541 err = got_error_from_errno("close");
542 goto done;
544 imsg_fds[1] = -1;
545 imsg_init(ibuf, imsg_fds[0]);
547 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
548 if (err)
549 goto done;
550 fd = -1;
552 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
553 if (err)
554 goto done;
556 err = got_privsep_recv_gitconfig_int(
557 gitconfig_repository_format_version, ibuf);
558 if (err)
559 goto done;
561 if (extensions && nextensions) {
562 err = got_privsep_send_gitconfig_repository_extensions_req(
563 ibuf);
564 if (err)
565 goto done;
566 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
567 if (err)
568 goto done;
569 if (*nextensions > 0) {
570 int i;
571 *extensions = calloc(*nextensions, sizeof(char *));
572 if (*extensions == NULL) {
573 err = got_error_from_errno("calloc");
574 goto done;
576 for (i = 0; i < *nextensions; i++) {
577 char *ext;
578 err = got_privsep_recv_gitconfig_str(&ext,
579 ibuf);
580 if (err)
581 goto done;
582 (*extensions)[i] = ext;
587 err = got_privsep_send_gitconfig_author_name_req(ibuf);
588 if (err)
589 goto done;
591 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
592 if (err)
593 goto done;
595 err = got_privsep_send_gitconfig_author_email_req(ibuf);
596 if (err)
597 goto done;
599 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
600 if (err)
601 goto done;
603 if (remotes && nremotes) {
604 err = got_privsep_send_gitconfig_remotes_req(ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_recv_gitconfig_remotes(remotes,
609 nremotes, ibuf);
610 if (err)
611 goto done;
614 if (gitconfig_owner) {
615 err = got_privsep_send_gitconfig_owner_req(ibuf);
616 if (err)
617 goto done;
618 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
619 if (err)
620 goto done;
623 err = got_privsep_send_stop(imsg_fds[0]);
624 child_err = got_privsep_wait_for_child(pid);
625 if (child_err && err == NULL)
626 err = child_err;
627 done:
628 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
631 err = got_error_from_errno("close");
632 if (fd != -1 && close(fd) == -1 && err == NULL)
633 err = got_error_from_errno2("close", gitconfig_path);
634 free(ibuf);
635 return err;
638 static const struct got_error *
639 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
641 const struct got_error *err = NULL;
642 char *repo_gitconfig_path = NULL;
644 if (global_gitconfig_path) {
645 /* Read settings from ~/.gitconfig. */
646 int dummy_repo_version;
647 err = parse_gitconfig_file(&dummy_repo_version,
648 &repo->global_gitconfig_author_name,
649 &repo->global_gitconfig_author_email,
650 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
651 if (err)
652 return err;
655 /* Read repository's .git/config file. */
656 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
657 if (repo_gitconfig_path == NULL)
658 return got_error_from_errno("got_repo_get_path_gitconfig");
660 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
661 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
662 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
663 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
664 repo_gitconfig_path);
665 if (err)
666 goto done;
667 done:
668 free(repo_gitconfig_path);
669 return err;
672 static const struct got_error *
673 read_gotconfig(struct got_repository *repo)
675 const struct got_error *err = NULL;
676 char *gotconfig_path;
678 gotconfig_path = got_repo_get_path_gotconfig(repo);
679 if (gotconfig_path == NULL)
680 return got_error_from_errno("got_repo_get_path_gotconfig");
682 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
683 free(gotconfig_path);
684 return err;
687 /* Supported repository format extensions. */
688 static const char *const repo_extensions[] = {
689 "noop", /* Got supports repository format version 1. */
690 "preciousObjects", /* Supported by gotadmin cleanup. */
691 "worktreeConfig", /* Got does not care about Git work trees. */
692 };
694 const struct got_error *
695 got_repo_open(struct got_repository **repop, const char *path,
696 const char *global_gitconfig_path, int *pack_fds)
698 struct got_repository *repo = NULL;
699 const struct got_error *err = NULL;
700 char *repo_path = NULL;
701 size_t i, j = 0;
702 struct rlimit rl;
704 *repop = NULL;
706 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
707 return got_error_from_errno("getrlimit");
709 repo = calloc(1, sizeof(*repo));
710 if (repo == NULL)
711 return got_error_from_errno("calloc");
713 RB_INIT(&repo->packidx_bloom_filters);
714 TAILQ_INIT(&repo->packidx_paths);
716 for (i = 0; i < nitems(repo->privsep_children); i++) {
717 memset(&repo->privsep_children[i], 0,
718 sizeof(repo->privsep_children[0]));
719 repo->privsep_children[i].imsg_fd = -1;
722 err = got_object_cache_init(&repo->objcache,
723 GOT_OBJECT_CACHE_TYPE_OBJ);
724 if (err)
725 goto done;
726 err = got_object_cache_init(&repo->treecache,
727 GOT_OBJECT_CACHE_TYPE_TREE);
728 if (err)
729 goto done;
730 err = got_object_cache_init(&repo->commitcache,
731 GOT_OBJECT_CACHE_TYPE_COMMIT);
732 if (err)
733 goto done;
734 err = got_object_cache_init(&repo->tagcache,
735 GOT_OBJECT_CACHE_TYPE_TAG);
736 if (err)
737 goto done;
738 err = got_object_cache_init(&repo->rawcache,
739 GOT_OBJECT_CACHE_TYPE_RAW);
740 if (err)
741 goto done;
743 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
744 if (repo->pack_cache_size > rl.rlim_cur / 8)
745 repo->pack_cache_size = rl.rlim_cur / 8;
746 for (i = 0; i < nitems(repo->packs); i++) {
747 if (i < repo->pack_cache_size) {
748 repo->packs[i].basefd = pack_fds[j++];
749 repo->packs[i].accumfd = pack_fds[j++];
750 } else {
751 repo->packs[i].basefd = -1;
752 repo->packs[i].accumfd = -1;
755 repo->pinned_pack = -1;
756 repo->pinned_packidx = -1;
757 repo->pinned_pid = 0;
759 repo_path = realpath(path, NULL);
760 if (repo_path == NULL) {
761 err = got_error_from_errno2("realpath", path);
762 goto done;
765 for (;;) {
766 char *parent_path;
768 err = open_repo(repo, repo_path);
769 if (err == NULL)
770 break;
771 if (err->code != GOT_ERR_NOT_GIT_REPO)
772 goto done;
773 if (repo_path[0] == '/' && repo_path[1] == '\0') {
774 err = got_error(GOT_ERR_NOT_GIT_REPO);
775 goto done;
777 err = got_path_dirname(&parent_path, repo_path);
778 if (err)
779 goto done;
780 free(repo_path);
781 repo_path = parent_path;
784 err = read_gotconfig(repo);
785 if (err)
786 goto done;
788 err = read_gitconfig(repo, global_gitconfig_path);
789 if (err)
790 goto done;
791 if (repo->gitconfig_repository_format_version != 0) {
792 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
793 goto done;
795 for (i = 0; i < repo->nextensions; i++) {
796 char *ext = repo->extensions[i];
797 int j, supported = 0;
798 for (j = 0; j < nitems(repo_extensions); j++) {
799 if (strcmp(ext, repo_extensions[j]) == 0) {
800 supported = 1;
801 break;
804 if (!supported) {
805 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
806 goto done;
810 err = got_repo_list_packidx(&repo->packidx_paths, repo);
811 done:
812 if (err)
813 got_repo_close(repo);
814 else
815 *repop = repo;
816 free(repo_path);
817 return err;
820 const struct got_error *
821 got_repo_close(struct got_repository *repo)
823 const struct got_error *err = NULL, *child_err;
824 struct got_packidx_bloom_filter *bf;
825 struct got_pathlist_entry *pe;
826 size_t i;
828 for (i = 0; i < repo->pack_cache_size; i++) {
829 if (repo->packidx_cache[i] == NULL)
830 break;
831 got_packidx_close(repo->packidx_cache[i]);
834 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters))) {
836 RB_REMOVE(got_packidx_bloom_filter_tree,
837 &repo->packidx_bloom_filters, bf);
838 free(bf->bloom);
839 free(bf);
842 for (i = 0; i < repo->pack_cache_size; i++)
843 if (repo->packs[i].path_packfile)
844 if (repo->packs[i].path_packfile)
845 got_pack_close(&repo->packs[i]);
847 free(repo->path);
848 free(repo->path_git_dir);
850 got_object_cache_close(&repo->objcache);
851 got_object_cache_close(&repo->treecache);
852 got_object_cache_close(&repo->commitcache);
853 got_object_cache_close(&repo->tagcache);
854 got_object_cache_close(&repo->rawcache);
856 for (i = 0; i < nitems(repo->privsep_children); i++) {
857 if (repo->privsep_children[i].imsg_fd == -1)
858 continue;
859 imsg_clear(repo->privsep_children[i].ibuf);
860 free(repo->privsep_children[i].ibuf);
861 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
862 child_err = got_privsep_wait_for_child(
863 repo->privsep_children[i].pid);
864 if (child_err && err == NULL)
865 err = child_err;
866 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
867 err == NULL)
868 err = got_error_from_errno("close");
871 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
872 err == NULL)
873 err = got_error_from_errno("close");
875 if (repo->gotconfig)
876 got_gotconfig_free(repo->gotconfig);
877 free(repo->gitconfig_author_name);
878 free(repo->gitconfig_author_email);
879 for (i = 0; i < repo->ngitconfig_remotes; i++)
880 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
881 free(repo->gitconfig_remotes);
882 for (i = 0; i < repo->nextensions; i++)
883 free(repo->extensions[i]);
884 free(repo->extensions);
886 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
887 free((void *)pe->path);
888 got_pathlist_free(&repo->packidx_paths);
889 free(repo);
891 return err;
894 void
895 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
897 int i;
899 free(repo->name);
900 repo->name = NULL;
901 free(repo->fetch_url);
902 repo->fetch_url = NULL;
903 free(repo->send_url);
904 repo->send_url = NULL;
905 for (i = 0; i < repo->nfetch_branches; i++)
906 free(repo->fetch_branches[i]);
907 free(repo->fetch_branches);
908 repo->fetch_branches = NULL;
909 repo->nfetch_branches = 0;
910 for (i = 0; i < repo->nsend_branches; i++)
911 free(repo->send_branches[i]);
912 free(repo->send_branches);
913 repo->send_branches = NULL;
914 repo->nsend_branches = 0;
917 const struct got_error *
918 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
919 const char *input_path)
921 const struct got_error *err = NULL;
922 const char *repo_abspath = NULL;
923 size_t repolen, len;
924 char *canonpath, *path = NULL;
926 *in_repo_path = NULL;
928 canonpath = strdup(input_path);
929 if (canonpath == NULL) {
930 err = got_error_from_errno("strdup");
931 goto done;
933 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
934 if (err)
935 goto done;
937 repo_abspath = got_repo_get_path(repo);
939 if (canonpath[0] == '\0') {
940 path = strdup(canonpath);
941 if (path == NULL) {
942 err = got_error_from_errno("strdup");
943 goto done;
945 } else {
946 path = realpath(canonpath, NULL);
947 if (path == NULL) {
948 if (errno != ENOENT) {
949 err = got_error_from_errno2("realpath",
950 canonpath);
951 goto done;
953 /*
954 * Path is not on disk.
955 * Assume it is already relative to repository root.
956 */
957 path = strdup(canonpath);
958 if (path == NULL) {
959 err = got_error_from_errno("strdup");
960 goto done;
964 repolen = strlen(repo_abspath);
965 len = strlen(path);
968 if (strcmp(path, repo_abspath) == 0) {
969 free(path);
970 path = strdup("");
971 if (path == NULL) {
972 err = got_error_from_errno("strdup");
973 goto done;
975 } else if (len > repolen &&
976 got_path_is_child(path, repo_abspath, repolen)) {
977 /* Matched an on-disk path inside repository. */
978 if (got_repo_is_bare(repo)) {
979 /*
980 * Matched an on-disk path inside repository
981 * database. Treat input as repository-relative.
982 */
983 free(path);
984 path = canonpath;
985 canonpath = NULL;
986 } else {
987 char *child;
988 /* Strip common prefix with repository path. */
989 err = got_path_skip_common_ancestor(&child,
990 repo_abspath, path);
991 if (err)
992 goto done;
993 free(path);
994 path = child;
996 } else {
997 /*
998 * Matched unrelated on-disk path.
999 * Treat input as repository-relative.
1001 free(path);
1002 path = canonpath;
1003 canonpath = NULL;
1007 /* Make in-repository path absolute */
1008 if (path[0] != '/') {
1009 char *abspath;
1010 if (asprintf(&abspath, "/%s", path) == -1) {
1011 err = got_error_from_errno("asprintf");
1012 goto done;
1014 free(path);
1015 path = abspath;
1018 done:
1019 free(canonpath);
1020 if (err)
1021 free(path);
1022 else
1023 *in_repo_path = path;
1024 return err;
1027 static const struct got_error *
1028 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1029 const char *path_packidx)
1031 const struct got_error *err = NULL;
1032 size_t i;
1034 for (i = 0; i < repo->pack_cache_size; i++) {
1035 if (repo->packidx_cache[i] == NULL)
1036 break;
1037 if (strcmp(repo->packidx_cache[i]->path_packidx,
1038 path_packidx) == 0) {
1039 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1042 if (i == repo->pack_cache_size) {
1043 do {
1044 i--;
1045 } while (i > 0 && repo->pinned_packidx >= 0 &&
1046 i == repo->pinned_packidx);
1047 err = got_packidx_close(repo->packidx_cache[i]);
1048 if (err)
1049 return err;
1052 repo->packidx_cache[i] = packidx;
1054 return NULL;
1057 int
1058 got_repo_is_packidx_filename(const char *name, size_t len)
1060 if (len != GOT_PACKIDX_NAMELEN)
1061 return 0;
1063 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1064 return 0;
1066 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1067 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1068 return 0;
1070 return 1;
1073 static struct got_packidx_bloom_filter *
1074 get_packidx_bloom_filter(struct got_repository *repo,
1075 const char *path, size_t path_len)
1077 struct got_packidx_bloom_filter key;
1079 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1080 return NULL; /* XXX */
1081 key.path_len = path_len;
1083 return RB_FIND(got_packidx_bloom_filter_tree,
1084 &repo->packidx_bloom_filters, &key);
1087 int
1088 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1089 const char *path_packidx, struct got_object_id *id)
1091 struct got_packidx_bloom_filter *bf;
1093 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1094 if (bf)
1095 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1097 /* No bloom filter means this pack index must be searched. */
1098 return 1;
1101 static const struct got_error *
1102 add_packidx_bloom_filter(struct got_repository *repo,
1103 struct got_packidx *packidx, const char *path_packidx)
1105 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1106 struct got_packidx_bloom_filter *bf;
1107 size_t len;
1110 * Don't use bloom filters for very large pack index files.
1111 * Large pack files will contain a relatively large fraction
1112 * of our objects so we will likely need to visit them anyway.
1113 * The more objects a pack file contains the higher the probability
1114 * of a false-positive match from the bloom filter. And reading
1115 * all object IDs from a large pack index file can be expensive.
1117 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1118 return NULL;
1120 /* Do we already have a filter for this pack index? */
1121 if (get_packidx_bloom_filter(repo, path_packidx,
1122 strlen(path_packidx)) != NULL)
1123 return NULL;
1125 bf = calloc(1, sizeof(*bf));
1126 if (bf == NULL)
1127 return got_error_from_errno("calloc");
1128 bf->bloom = calloc(1, sizeof(*bf->bloom));
1129 if (bf->bloom == NULL) {
1130 free(bf);
1131 return got_error_from_errno("calloc");
1134 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1135 if (len >= sizeof(bf->path)) {
1136 free(bf->bloom);
1137 free(bf);
1138 return got_error(GOT_ERR_NO_SPACE);
1140 bf->path_len = len;
1142 /* Minimum size supported by our bloom filter is 1000 entries. */
1143 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1144 for (i = 0; i < nobjects; i++) {
1145 struct got_packidx_object_id *id;
1146 id = &packidx->hdr.sorted_ids[i];
1147 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1150 RB_INSERT(got_packidx_bloom_filter_tree,
1151 &repo->packidx_bloom_filters, bf);
1152 return NULL;
1155 const struct got_error *
1156 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1157 struct got_repository *repo, struct got_object_id *id)
1159 const struct got_error *err;
1160 struct got_pathlist_entry *pe;
1161 size_t i;
1163 /* Search pack index cache. */
1164 for (i = 0; i < repo->pack_cache_size; i++) {
1165 if (repo->packidx_cache[i] == NULL)
1166 break;
1167 if (!got_repo_check_packidx_bloom_filter(repo,
1168 repo->packidx_cache[i]->path_packidx, id))
1169 continue; /* object will not be found in this index */
1170 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1171 if (*idx != -1) {
1172 *packidx = repo->packidx_cache[i];
1174 * Move this cache entry to the front. Repeatedly
1175 * searching a wrong pack index can be expensive.
1177 if (i > 0) {
1178 memmove(&repo->packidx_cache[1],
1179 &repo->packidx_cache[0],
1180 i * sizeof(repo->packidx_cache[0]));
1181 repo->packidx_cache[0] = *packidx;
1182 if (repo->pinned_packidx >= 0 &&
1183 repo->pinned_packidx < i)
1184 repo->pinned_packidx++;
1185 else if (repo->pinned_packidx == i)
1186 repo->pinned_packidx = 0;
1188 return NULL;
1191 /* No luck. Search the filesystem. */
1193 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1194 const char *path_packidx = pe->path;
1195 int is_cached = 0;
1197 if (!got_repo_check_packidx_bloom_filter(repo,
1198 pe->path, id))
1199 continue; /* object will not be found in this index */
1201 for (i = 0; i < repo->pack_cache_size; i++) {
1202 if (repo->packidx_cache[i] == NULL)
1203 break;
1204 if (strcmp(repo->packidx_cache[i]->path_packidx,
1205 path_packidx) == 0) {
1206 is_cached = 1;
1207 break;
1210 if (is_cached)
1211 continue; /* already searched */
1213 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1214 path_packidx, 0);
1215 if (err)
1216 goto done;
1218 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1219 if (err)
1220 goto done;
1222 err = cache_packidx(repo, *packidx, path_packidx);
1223 if (err)
1224 goto done;
1226 *idx = got_packidx_get_object_idx(*packidx, id);
1227 if (*idx != -1) {
1228 err = NULL; /* found the object */
1229 goto done;
1233 err = got_error_no_obj(id);
1234 done:
1235 return err;
1238 const struct got_error *
1239 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1240 struct got_repository *repo)
1242 const struct got_error *err = NULL;
1243 DIR *packdir = NULL;
1244 struct dirent *dent;
1245 char *path_packidx = NULL;
1246 int packdir_fd;
1248 packdir_fd = openat(got_repo_get_fd(repo),
1249 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1250 if (packdir_fd == -1) {
1251 return got_error_from_errno_fmt("openat: %s/%s",
1252 got_repo_get_path_git_dir(repo),
1253 GOT_OBJECTS_PACK_DIR);
1256 packdir = fdopendir(packdir_fd);
1257 if (packdir == NULL) {
1258 err = got_error_from_errno("fdopendir");
1259 goto done;
1262 while ((dent = readdir(packdir)) != NULL) {
1263 if (!got_repo_is_packidx_filename(dent->d_name,
1264 strlen(dent->d_name)))
1265 continue;
1267 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1268 dent->d_name) == -1) {
1269 err = got_error_from_errno("asprintf");
1270 path_packidx = NULL;
1271 break;
1274 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1275 if (err)
1276 break;
1278 done:
1279 if (err)
1280 free(path_packidx);
1281 if (packdir && closedir(packdir) != 0 && err == NULL)
1282 err = got_error_from_errno("closedir");
1283 return err;
1286 const struct got_error *
1287 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1288 struct got_repository *repo)
1290 const struct got_error *err;
1291 size_t i;
1293 *packidx = NULL;
1295 /* Search pack index cache. */
1296 for (i = 0; i < repo->pack_cache_size; i++) {
1297 if (repo->packidx_cache[i] == NULL)
1298 break;
1299 if (strcmp(repo->packidx_cache[i]->path_packidx,
1300 path_packidx) == 0) {
1301 *packidx = repo->packidx_cache[i];
1302 return NULL;
1305 /* No luck. Search the filesystem. */
1307 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1308 path_packidx, 0);
1309 if (err)
1310 return err;
1312 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1313 if (err)
1314 goto done;
1316 err = cache_packidx(repo, *packidx, path_packidx);
1317 done:
1318 if (err) {
1319 got_packidx_close(*packidx);
1320 *packidx = NULL;
1322 return err;
1325 static const struct got_error *
1326 read_packfile_hdr(int fd, struct got_packidx *packidx)
1328 const struct got_error *err = NULL;
1329 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1330 struct got_packfile_hdr hdr;
1331 ssize_t n;
1333 n = read(fd, &hdr, sizeof(hdr));
1334 if (n < 0)
1335 return got_error_from_errno("read");
1336 if (n != sizeof(hdr))
1337 return got_error(GOT_ERR_BAD_PACKFILE);
1339 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1340 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1341 be32toh(hdr.nobjects) != totobj)
1342 err = got_error(GOT_ERR_BAD_PACKFILE);
1344 return err;
1347 static const struct got_error *
1348 open_packfile(int *fd, struct got_repository *repo,
1349 const char *relpath, struct got_packidx *packidx)
1351 const struct got_error *err = NULL;
1353 *fd = openat(got_repo_get_fd(repo), relpath,
1354 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1355 if (*fd == -1)
1356 return got_error_from_errno_fmt("openat: %s/%s",
1357 got_repo_get_path_git_dir(repo), relpath);
1359 if (packidx) {
1360 err = read_packfile_hdr(*fd, packidx);
1361 if (err) {
1362 close(*fd);
1363 *fd = -1;
1367 return err;
1370 const struct got_error *
1371 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1372 const char *path_packfile, struct got_packidx *packidx)
1374 const struct got_error *err = NULL;
1375 struct got_pack *pack = NULL;
1376 struct stat sb;
1377 size_t i;
1379 if (packp)
1380 *packp = NULL;
1382 for (i = 0; i < repo->pack_cache_size; i++) {
1383 pack = &repo->packs[i];
1384 if (pack->path_packfile == NULL)
1385 break;
1386 if (strcmp(pack->path_packfile, path_packfile) == 0)
1387 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1390 if (i == repo->pack_cache_size) {
1391 struct got_pack tmp;
1392 do {
1393 i--;
1394 } while (i > 0 && repo->pinned_pack >= 0 &&
1395 i == repo->pinned_pack);
1396 err = got_pack_close(&repo->packs[i]);
1397 if (err)
1398 return err;
1399 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1400 return got_error_from_errno("ftruncate");
1401 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1402 return got_error_from_errno("ftruncate");
1403 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1404 memcpy(&repo->packs[i], &repo->packs[0],
1405 sizeof(repo->packs[i]));
1406 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1407 if (repo->pinned_pack == 0)
1408 repo->pinned_pack = i;
1409 else if (repo->pinned_pack == i)
1410 repo->pinned_pack = 0;
1411 i = 0;
1414 pack = &repo->packs[i];
1416 pack->path_packfile = strdup(path_packfile);
1417 if (pack->path_packfile == NULL) {
1418 err = got_error_from_errno("strdup");
1419 goto done;
1422 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1423 if (err)
1424 goto done;
1426 if (fstat(pack->fd, &sb) != 0) {
1427 err = got_error_from_errno("fstat");
1428 goto done;
1430 pack->filesize = sb.st_size;
1432 pack->privsep_child = NULL;
1434 #ifndef GOT_PACK_NO_MMAP
1435 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1436 pack->fd, 0);
1437 if (pack->map == MAP_FAILED) {
1438 if (errno != ENOMEM) {
1439 err = got_error_from_errno("mmap");
1440 goto done;
1442 pack->map = NULL; /* fall back to read(2) */
1444 #endif
1445 done:
1446 if (err) {
1447 if (pack) {
1448 free(pack->path_packfile);
1449 memset(pack, 0, sizeof(*pack));
1451 } else if (packp)
1452 *packp = pack;
1453 return err;
1456 struct got_pack *
1457 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1459 struct got_pack *pack = NULL;
1460 size_t i;
1462 for (i = 0; i < repo->pack_cache_size; i++) {
1463 pack = &repo->packs[i];
1464 if (pack->path_packfile == NULL)
1465 break;
1466 if (strcmp(pack->path_packfile, path_packfile) == 0)
1467 return pack;
1470 return NULL;
1473 const struct got_error *
1474 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1475 struct got_pack *pack)
1477 size_t i;
1478 int pinned_pack = -1, pinned_packidx = -1;
1480 for (i = 0; i < repo->pack_cache_size; i++) {
1481 if (repo->packidx_cache[i] &&
1482 strcmp(repo->packidx_cache[i]->path_packidx,
1483 packidx->path_packidx) == 0)
1484 pinned_packidx = i;
1485 if (repo->packs[i].path_packfile &&
1486 strcmp(repo->packs[i].path_packfile,
1487 pack->path_packfile) == 0)
1488 pinned_pack = i;
1491 if (pinned_packidx == -1 || pinned_pack == -1)
1492 return got_error(GOT_ERR_PIN_PACK);
1494 repo->pinned_pack = pinned_pack;
1495 repo->pinned_packidx = pinned_packidx;
1496 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1497 return NULL;
1500 struct got_pack *
1501 got_repo_get_pinned_pack(struct got_repository *repo)
1503 if (repo->pinned_pack >= 0 &&
1504 repo->pinned_pack < repo->pack_cache_size)
1505 return &repo->packs[repo->pinned_pack];
1507 return NULL;
1510 void
1511 got_repo_unpin_pack(struct got_repository *repo)
1513 repo->pinned_packidx = -1;
1514 repo->pinned_pack = -1;
1515 repo->pinned_pid = 0;
1518 const struct got_error *
1519 got_repo_init(const char *repo_path)
1521 const struct got_error *err = NULL;
1522 const char *dirnames[] = {
1523 GOT_OBJECTS_DIR,
1524 GOT_OBJECTS_PACK_DIR,
1525 GOT_REFS_DIR,
1527 const char *description_str = "Unnamed repository; "
1528 "edit this file 'description' to name the repository.";
1529 const char *headref_str = "ref: refs/heads/main";
1530 const char *gitconfig_str = "[core]\n"
1531 "\trepositoryformatversion = 0\n"
1532 "\tfilemode = true\n"
1533 "\tbare = true\n";
1534 char *path;
1535 size_t i;
1537 if (!got_path_dir_is_empty(repo_path))
1538 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1540 for (i = 0; i < nitems(dirnames); i++) {
1541 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1542 return got_error_from_errno("asprintf");
1544 err = got_path_mkdir(path);
1545 free(path);
1546 if (err)
1547 return err;
1550 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1551 return got_error_from_errno("asprintf");
1552 err = got_path_create_file(path, description_str);
1553 free(path);
1554 if (err)
1555 return err;
1557 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1558 return got_error_from_errno("asprintf");
1559 err = got_path_create_file(path, headref_str);
1560 free(path);
1561 if (err)
1562 return err;
1564 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1565 return got_error_from_errno("asprintf");
1566 err = got_path_create_file(path, gitconfig_str);
1567 free(path);
1568 if (err)
1569 return err;
1571 return NULL;
1574 static const struct got_error *
1575 match_packed_object(struct got_object_id **unique_id,
1576 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1578 const struct got_error *err = NULL;
1579 struct got_object_id_queue matched_ids;
1580 struct got_pathlist_entry *pe;
1582 STAILQ_INIT(&matched_ids);
1584 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1585 const char *path_packidx = pe->path;
1586 struct got_packidx *packidx;
1587 struct got_object_qid *qid;
1589 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1590 path_packidx, 0);
1591 if (err)
1592 break;
1594 err = got_packidx_match_id_str_prefix(&matched_ids,
1595 packidx, id_str_prefix);
1596 if (err) {
1597 got_packidx_close(packidx);
1598 break;
1600 err = got_packidx_close(packidx);
1601 if (err)
1602 break;
1604 STAILQ_FOREACH(qid, &matched_ids, entry) {
1605 if (obj_type != GOT_OBJ_TYPE_ANY) {
1606 int matched_type;
1607 err = got_object_get_type(&matched_type, repo,
1608 &qid->id);
1609 if (err)
1610 goto done;
1611 if (matched_type != obj_type)
1612 continue;
1614 if (*unique_id == NULL) {
1615 *unique_id = got_object_id_dup(&qid->id);
1616 if (*unique_id == NULL) {
1617 err = got_error_from_errno("malloc");
1618 goto done;
1620 } else {
1621 if (got_object_id_cmp(*unique_id,
1622 &qid->id) == 0)
1623 continue; /* packed multiple times */
1624 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1625 goto done;
1629 done:
1630 got_object_id_queue_free(&matched_ids);
1631 if (err) {
1632 free(*unique_id);
1633 *unique_id = NULL;
1635 return err;
1638 static const struct got_error *
1639 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1640 const char *object_dir, const char *id_str_prefix, int obj_type,
1641 struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 char *path;
1645 DIR *dir = NULL;
1646 struct dirent *dent;
1647 struct got_object_id id;
1649 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1650 err = got_error_from_errno("asprintf");
1651 goto done;
1654 dir = opendir(path);
1655 if (dir == NULL) {
1656 if (errno == ENOENT) {
1657 err = NULL;
1658 goto done;
1660 err = got_error_from_errno2("opendir", path);
1661 goto done;
1663 while ((dent = readdir(dir)) != NULL) {
1664 char *id_str;
1665 int cmp;
1667 if (strcmp(dent->d_name, ".") == 0 ||
1668 strcmp(dent->d_name, "..") == 0)
1669 continue;
1671 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1672 err = got_error_from_errno("asprintf");
1673 goto done;
1676 if (!got_parse_sha1_digest(id.sha1, id_str))
1677 continue;
1680 * Directory entries do not necessarily appear in
1681 * sorted order, so we must iterate over all of them.
1683 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1684 if (cmp != 0) {
1685 free(id_str);
1686 continue;
1689 if (*unique_id == NULL) {
1690 if (obj_type != GOT_OBJ_TYPE_ANY) {
1691 int matched_type;
1692 err = got_object_get_type(&matched_type, repo,
1693 &id);
1694 if (err)
1695 goto done;
1696 if (matched_type != obj_type)
1697 continue;
1699 *unique_id = got_object_id_dup(&id);
1700 if (*unique_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1702 free(id_str);
1703 goto done;
1705 } else {
1706 if (got_object_id_cmp(*unique_id, &id) == 0)
1707 continue; /* both packed and loose */
1708 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1709 free(id_str);
1710 goto done;
1713 done:
1714 if (dir && closedir(dir) != 0 && err == NULL)
1715 err = got_error_from_errno("closedir");
1716 if (err) {
1717 free(*unique_id);
1718 *unique_id = NULL;
1720 free(path);
1721 return err;
1724 const struct got_error *
1725 got_repo_match_object_id_prefix(struct got_object_id **id,
1726 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1728 const struct got_error *err = NULL;
1729 char *path_objects = got_repo_get_path_objects(repo);
1730 char *object_dir = NULL;
1731 size_t len;
1732 int i;
1734 *id = NULL;
1736 len = strlen(id_str_prefix);
1737 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1738 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1740 for (i = 0; i < len; i++) {
1741 if (isxdigit((unsigned char)id_str_prefix[i]))
1742 continue;
1743 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1746 if (len >= 2) {
1747 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1748 if (err)
1749 goto done;
1750 object_dir = strndup(id_str_prefix, 2);
1751 if (object_dir == NULL) {
1752 err = got_error_from_errno("strdup");
1753 goto done;
1755 err = match_loose_object(id, path_objects, object_dir,
1756 id_str_prefix, obj_type, repo);
1757 } else if (len == 1) {
1758 int i;
1759 for (i = 0; i < 0xf; i++) {
1760 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1761 == -1) {
1762 err = got_error_from_errno("asprintf");
1763 goto done;
1765 err = match_packed_object(id, repo, object_dir,
1766 obj_type);
1767 if (err)
1768 goto done;
1769 err = match_loose_object(id, path_objects, object_dir,
1770 id_str_prefix, obj_type, repo);
1771 if (err)
1772 goto done;
1774 } else {
1775 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 goto done;
1778 done:
1779 free(object_dir);
1780 if (err) {
1781 free(*id);
1782 *id = NULL;
1783 } else if (*id == NULL) {
1784 switch (obj_type) {
1785 case GOT_OBJ_TYPE_BLOB:
1786 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1787 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1788 break;
1789 case GOT_OBJ_TYPE_TREE:
1790 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1791 GOT_OBJ_LABEL_TREE, id_str_prefix);
1792 break;
1793 case GOT_OBJ_TYPE_COMMIT:
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1796 break;
1797 case GOT_OBJ_TYPE_TAG:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_TAG, id_str_prefix);
1800 break;
1801 default:
1802 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1803 break;
1807 return err;
1810 const struct got_error *
1811 got_repo_match_object_id(struct got_object_id **id, char **label,
1812 const char *id_str, int obj_type, struct got_reflist_head *refs,
1813 struct got_repository *repo)
1815 const struct got_error *err;
1816 struct got_tag_object *tag;
1817 struct got_reference *ref = NULL;
1819 *id = NULL;
1820 if (label)
1821 *label = NULL;
1823 if (refs) {
1824 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1825 refs, repo);
1826 if (err == NULL) {
1827 *id = got_object_id_dup(
1828 got_object_tag_get_object_id(tag));
1829 if (*id == NULL)
1830 err = got_error_from_errno("got_object_id_dup");
1831 else if (label && asprintf(label, "refs/tags/%s",
1832 got_object_tag_get_name(tag)) == -1) {
1833 err = got_error_from_errno("asprintf");
1834 free(*id);
1835 *id = NULL;
1837 got_object_tag_close(tag);
1838 return err;
1839 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1840 err->code != GOT_ERR_NO_OBJ)
1841 return err;
1844 err = got_ref_open(&ref, repo, id_str, 0);
1845 if (err == NULL) {
1846 err = got_ref_resolve(id, repo, ref);
1847 if (err)
1848 goto done;
1849 if (label) {
1850 *label = strdup(got_ref_get_name(ref));
1851 if (*label == NULL) {
1852 err = got_error_from_errno("strdup");
1853 goto done;
1856 } else {
1857 if (err->code != GOT_ERR_NOT_REF &&
1858 err->code != GOT_ERR_BAD_REF_NAME)
1859 goto done;
1860 err = got_repo_match_object_id_prefix(id, id_str,
1861 obj_type, repo);
1862 if (err) {
1863 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1864 err = got_error_not_ref(id_str);
1865 goto done;
1867 if (label) {
1868 err = got_object_id_str(label, *id);
1869 if (*label == NULL) {
1870 err = got_error_from_errno("strdup");
1871 goto done;
1875 done:
1876 if (ref)
1877 got_ref_close(ref);
1878 return err;
1881 const struct got_error *
1882 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1883 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1885 const struct got_error *err = NULL;
1886 struct got_reflist_entry *re;
1887 struct got_object_id *tag_id;
1888 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1890 *tag = NULL;
1892 TAILQ_FOREACH(re, refs, entry) {
1893 const char *refname;
1894 refname = got_ref_get_name(re->ref);
1895 if (got_ref_is_symbolic(re->ref))
1896 continue;
1897 if (strncmp(refname, "refs/tags/", 10) != 0)
1898 continue;
1899 if (!name_is_absolute)
1900 refname += strlen("refs/tags/");
1901 if (strcmp(refname, name) != 0)
1902 continue;
1903 err = got_ref_resolve(&tag_id, repo, re->ref);
1904 if (err)
1905 break;
1906 err = got_object_open_as_tag(tag, repo, tag_id);
1907 free(tag_id);
1908 if (err)
1909 break;
1910 if (obj_type == GOT_OBJ_TYPE_ANY ||
1911 got_object_tag_get_object_type(*tag) == obj_type)
1912 break;
1913 got_object_tag_close(*tag);
1914 *tag = NULL;
1917 if (err == NULL && *tag == NULL)
1918 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1919 GOT_OBJ_LABEL_TAG, name);
1920 return err;
1923 static const struct got_error *
1924 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1925 const char *name, mode_t mode, struct got_object_id *blob_id)
1927 const struct got_error *err = NULL;
1929 *new_te = NULL;
1931 *new_te = calloc(1, sizeof(**new_te));
1932 if (*new_te == NULL)
1933 return got_error_from_errno("calloc");
1935 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1936 sizeof((*new_te)->name)) {
1937 err = got_error(GOT_ERR_NO_SPACE);
1938 goto done;
1941 if (S_ISLNK(mode)) {
1942 (*new_te)->mode = S_IFLNK;
1943 } else {
1944 (*new_te)->mode = S_IFREG;
1945 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1947 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1948 done:
1949 if (err && *new_te) {
1950 free(*new_te);
1951 *new_te = NULL;
1953 return err;
1956 static const struct got_error *
1957 import_file(struct got_tree_entry **new_te, struct dirent *de,
1958 const char *path, struct got_repository *repo)
1960 const struct got_error *err;
1961 struct got_object_id *blob_id = NULL;
1962 char *filepath;
1963 struct stat sb;
1965 if (asprintf(&filepath, "%s%s%s", path,
1966 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1967 return got_error_from_errno("asprintf");
1969 if (lstat(filepath, &sb) != 0) {
1970 err = got_error_from_errno2("lstat", path);
1971 goto done;
1974 err = got_object_blob_create(&blob_id, filepath, repo);
1975 if (err)
1976 goto done;
1978 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1979 blob_id);
1980 done:
1981 free(filepath);
1982 if (err)
1983 free(blob_id);
1984 return err;
1987 static const struct got_error *
1988 insert_tree_entry(struct got_tree_entry *new_te,
1989 struct got_pathlist_head *paths)
1991 const struct got_error *err = NULL;
1992 struct got_pathlist_entry *new_pe;
1994 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1995 if (err)
1996 return err;
1997 if (new_pe == NULL)
1998 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1999 return NULL;
2002 static const struct got_error *write_tree(struct got_object_id **,
2003 const char *, struct got_pathlist_head *, struct got_repository *,
2004 got_repo_import_cb progress_cb, void *progress_arg);
2006 static const struct got_error *
2007 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2008 const char *path, struct got_pathlist_head *ignores,
2009 struct got_repository *repo,
2010 got_repo_import_cb progress_cb, void *progress_arg)
2012 const struct got_error *err;
2013 struct got_object_id *id = NULL;
2014 char *subdirpath;
2016 if (asprintf(&subdirpath, "%s%s%s", path,
2017 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2018 return got_error_from_errno("asprintf");
2020 (*new_te) = calloc(1, sizeof(**new_te));
2021 if (*new_te == NULL)
2022 return got_error_from_errno("calloc");
2023 (*new_te)->mode = S_IFDIR;
2024 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2025 sizeof((*new_te)->name)) {
2026 err = got_error(GOT_ERR_NO_SPACE);
2027 goto done;
2029 err = write_tree(&id, subdirpath, ignores, repo,
2030 progress_cb, progress_arg);
2031 if (err)
2032 goto done;
2033 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2035 done:
2036 free(id);
2037 free(subdirpath);
2038 if (err) {
2039 free(*new_te);
2040 *new_te = NULL;
2042 return err;
2045 static const struct got_error *
2046 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2047 struct got_pathlist_head *ignores, struct got_repository *repo,
2048 got_repo_import_cb progress_cb, void *progress_arg)
2050 const struct got_error *err = NULL;
2051 DIR *dir;
2052 struct dirent *de;
2053 int nentries;
2054 struct got_tree_entry *new_te = NULL;
2055 struct got_pathlist_head paths;
2056 struct got_pathlist_entry *pe;
2058 *new_tree_id = NULL;
2060 TAILQ_INIT(&paths);
2062 dir = opendir(path_dir);
2063 if (dir == NULL) {
2064 err = got_error_from_errno2("opendir", path_dir);
2065 goto done;
2068 nentries = 0;
2069 while ((de = readdir(dir)) != NULL) {
2070 int ignore = 0;
2071 int type;
2073 if (strcmp(de->d_name, ".") == 0 ||
2074 strcmp(de->d_name, "..") == 0)
2075 continue;
2077 TAILQ_FOREACH(pe, ignores, entry) {
2078 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2079 ignore = 1;
2080 break;
2083 if (ignore)
2084 continue;
2086 err = got_path_dirent_type(&type, path_dir, de);
2087 if (err)
2088 goto done;
2090 if (type == DT_DIR) {
2091 err = import_subdir(&new_te, de, path_dir,
2092 ignores, repo, progress_cb, progress_arg);
2093 if (err) {
2094 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2095 goto done;
2096 err = NULL;
2097 continue;
2099 } else if (type == DT_REG || type == DT_LNK) {
2100 err = import_file(&new_te, de, path_dir, repo);
2101 if (err)
2102 goto done;
2103 } else
2104 continue;
2106 err = insert_tree_entry(new_te, &paths);
2107 if (err)
2108 goto done;
2109 nentries++;
2112 if (TAILQ_EMPTY(&paths)) {
2113 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2114 "cannot create tree without any entries");
2115 goto done;
2118 TAILQ_FOREACH(pe, &paths, entry) {
2119 struct got_tree_entry *te = pe->data;
2120 char *path;
2121 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2122 continue;
2123 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2124 err = got_error_from_errno("asprintf");
2125 goto done;
2127 err = (*progress_cb)(progress_arg, path);
2128 free(path);
2129 if (err)
2130 goto done;
2133 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2134 done:
2135 if (dir)
2136 closedir(dir);
2137 got_pathlist_free(&paths);
2138 return err;
2141 const struct got_error *
2142 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2143 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2144 struct got_repository *repo, got_repo_import_cb progress_cb,
2145 void *progress_arg)
2147 const struct got_error *err;
2148 struct got_object_id *new_tree_id;
2150 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2151 progress_cb, progress_arg);
2152 if (err)
2153 return err;
2155 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2156 author, time(NULL), author, time(NULL), logmsg, repo);
2157 free(new_tree_id);
2158 return err;
2161 const struct got_error *
2162 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2163 struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 char *path_objects = NULL, *path = NULL;
2167 DIR *dir = NULL;
2168 struct got_object_id id;
2169 int i;
2171 *nobjects = 0;
2172 *ondisk_size = 0;
2174 path_objects = got_repo_get_path_objects(repo);
2175 if (path_objects == NULL)
2176 return got_error_from_errno("got_repo_get_path_objects");
2178 for (i = 0; i <= 0xff; i++) {
2179 struct dirent *dent;
2181 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2182 err = got_error_from_errno("asprintf");
2183 break;
2186 dir = opendir(path);
2187 if (dir == NULL) {
2188 if (errno == ENOENT) {
2189 err = NULL;
2190 continue;
2192 err = got_error_from_errno2("opendir", path);
2193 break;
2196 while ((dent = readdir(dir)) != NULL) {
2197 char *id_str;
2198 int fd;
2199 struct stat sb;
2201 if (strcmp(dent->d_name, ".") == 0 ||
2202 strcmp(dent->d_name, "..") == 0)
2203 continue;
2205 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2206 err = got_error_from_errno("asprintf");
2207 goto done;
2210 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2211 free(id_str);
2212 continue;
2214 free(id_str);
2216 err = got_object_open_loose_fd(&fd, &id, repo);
2217 if (err)
2218 goto done;
2220 if (fstat(fd, &sb) == -1) {
2221 err = got_error_from_errno("fstat");
2222 close(fd);
2223 goto done;
2225 (*nobjects)++;
2226 (*ondisk_size) += sb.st_size;
2228 if (close(fd) == -1) {
2229 err = got_error_from_errno("close");
2230 goto done;
2234 if (closedir(dir) != 0) {
2235 err = got_error_from_errno("closedir");
2236 goto done;
2238 dir = NULL;
2240 free(path);
2241 path = NULL;
2243 done:
2244 if (dir && closedir(dir) != 0 && err == NULL)
2245 err = got_error_from_errno("closedir");
2247 if (err) {
2248 *nobjects = 0;
2249 *ondisk_size = 0;
2251 free(path_objects);
2252 free(path);
2253 return err;
2256 const struct got_error *
2257 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2258 off_t *total_packsize, struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 DIR *packdir = NULL;
2262 struct dirent *dent;
2263 struct got_packidx *packidx = NULL;
2264 char *path_packidx;
2265 char *path_packfile;
2266 int packdir_fd;
2267 struct stat sb;
2269 *npackfiles = 0;
2270 *nobjects = 0;
2271 *total_packsize = 0;
2273 packdir_fd = openat(got_repo_get_fd(repo),
2274 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2275 if (packdir_fd == -1) {
2276 return got_error_from_errno_fmt("openat: %s/%s",
2277 got_repo_get_path_git_dir(repo),
2278 GOT_OBJECTS_PACK_DIR);
2281 packdir = fdopendir(packdir_fd);
2282 if (packdir == NULL) {
2283 err = got_error_from_errno("fdopendir");
2284 goto done;
2287 while ((dent = readdir(packdir)) != NULL) {
2288 if (!got_repo_is_packidx_filename(dent->d_name,
2289 strlen(dent->d_name)))
2290 continue;
2292 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2293 dent->d_name) == -1) {
2294 err = got_error_from_errno("asprintf");
2295 goto done;
2298 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2299 path_packidx, 0);
2300 free(path_packidx);
2301 if (err)
2302 goto done;
2304 if (fstat(packidx->fd, &sb) == -1)
2305 goto done;
2306 *total_packsize += sb.st_size;
2308 err = got_packidx_get_packfile_path(&path_packfile,
2309 packidx->path_packidx);
2310 if (err)
2311 goto done;
2313 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2314 0) == -1) {
2315 free(path_packfile);
2316 goto done;
2318 free(path_packfile);
2319 *total_packsize += sb.st_size;
2321 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2323 (*npackfiles)++;
2325 got_packidx_close(packidx);
2326 packidx = NULL;
2328 done:
2329 if (packidx)
2330 got_packidx_close(packidx);
2331 if (packdir && closedir(packdir) != 0 && err == NULL)
2332 err = got_error_from_errno("closedir");
2333 if (err) {
2334 *npackfiles = 0;
2335 *nobjects = 0;
2336 *total_packsize = 0;
2338 return err;
2341 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2342 got_packidx_bloom_filter_cmp);