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 /*
262 * got_repo_pack_fds_close will try to close all of the
263 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
264 * a value from got_opentempfd(), which would result in a close(0) if
265 * we do not initialize to -1 here.
266 */
267 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
268 pack_fds_tmp[i] = -1;
270 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
271 pack_fds_tmp[i] = got_opentempfd();
272 if (pack_fds_tmp[i] == -1) {
273 err = got_error_from_errno("got_opentempfd");
274 got_repo_pack_fds_close(pack_fds_tmp);
275 return err;
278 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
279 return err;
282 const struct got_error *
283 got_repo_pack_fds_close(int *pack_fds)
285 const struct got_error *err = NULL;
286 int i;
288 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
289 if (pack_fds[i] == -1)
290 continue;
291 if (close(pack_fds[i]) == -1) {
292 err = got_error_from_errno("close");
293 break;
296 free(pack_fds);
297 return err;
300 const struct got_error *
301 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
302 struct got_object *obj)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->objcache, id, obj);
307 if (err) {
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 err = NULL;
311 return err;
313 obj->refcnt++;
314 #endif
315 return NULL;
318 struct got_object *
319 got_repo_get_cached_object(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
325 const struct got_error *
326 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
327 struct got_tree_object *tree)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->treecache, id, tree);
332 if (err) {
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 err = NULL;
336 return err;
338 tree->refcnt++;
339 #endif
340 return NULL;
343 struct got_tree_object *
344 got_repo_get_cached_tree(struct got_repository *repo,
345 struct got_object_id *id)
347 return (struct got_tree_object *)got_object_cache_get(
348 &repo->treecache, id);
351 const struct got_error *
352 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
353 struct got_commit_object *commit)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->commitcache, id, commit);
358 if (err) {
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
361 err = NULL;
362 return err;
364 commit->refcnt++;
365 #endif
366 return NULL;
369 struct got_commit_object *
370 got_repo_get_cached_commit(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_commit_object *)got_object_cache_get(
374 &repo->commitcache, id);
377 const struct got_error *
378 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
379 struct got_tag_object *tag)
381 #ifndef GOT_NO_OBJ_CACHE
382 const struct got_error *err = NULL;
383 err = got_object_cache_add(&repo->tagcache, id, tag);
384 if (err) {
385 if (err->code == GOT_ERR_OBJ_EXISTS ||
386 err->code == GOT_ERR_OBJ_TOO_LARGE)
387 err = NULL;
388 return err;
390 tag->refcnt++;
391 #endif
392 return NULL;
395 struct got_tag_object *
396 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
398 return (struct got_tag_object *)got_object_cache_get(
399 &repo->tagcache, id);
402 const struct got_error *
403 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
404 struct got_raw_object *raw)
406 #ifndef GOT_NO_OBJ_CACHE
407 const struct got_error *err = NULL;
408 err = got_object_cache_add(&repo->rawcache, id, raw);
409 if (err) {
410 if (err->code == GOT_ERR_OBJ_EXISTS ||
411 err->code == GOT_ERR_OBJ_TOO_LARGE)
412 err = NULL;
413 return err;
415 raw->refcnt++;
416 #endif
417 return NULL;
421 struct got_raw_object *
422 got_repo_get_cached_raw_object(struct got_repository *repo,
423 struct got_object_id *id)
425 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
429 static const struct got_error *
430 open_repo(struct got_repository *repo, const char *path)
432 const struct got_error *err = NULL;
434 repo->gitdir_fd = -1;
436 /* bare git repository? */
437 repo->path_git_dir = strdup(path);
438 if (repo->path_git_dir == NULL)
439 return got_error_from_errno("strdup");
440 if (is_git_repo(repo)) {
441 repo->path = strdup(repo->path_git_dir);
442 if (repo->path == NULL) {
443 err = got_error_from_errno("strdup");
444 goto done;
446 repo->gitdir_fd = open(repo->path_git_dir,
447 O_DIRECTORY | O_CLOEXEC);
448 if (repo->gitdir_fd == -1) {
449 err = got_error_from_errno2("open",
450 repo->path_git_dir);
451 goto done;
453 return NULL;
456 /* git repository with working tree? */
457 free(repo->path_git_dir);
458 repo->path_git_dir = NULL;
459 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
463 if (is_git_repo(repo)) {
464 repo->path = strdup(path);
465 if (repo->path == NULL) {
466 err = got_error_from_errno("strdup");
467 goto done;
469 repo->gitdir_fd = open(repo->path_git_dir,
470 O_DIRECTORY | O_CLOEXEC);
471 if (repo->gitdir_fd == -1) {
472 err = got_error_from_errno2("open",
473 repo->path_git_dir);
474 goto done;
476 return NULL;
479 err = got_error(GOT_ERR_NOT_GIT_REPO);
480 done:
481 if (err) {
482 free(repo->path);
483 repo->path = NULL;
484 free(repo->path_git_dir);
485 repo->path_git_dir = NULL;
486 if (repo->gitdir_fd != -1)
487 close(repo->gitdir_fd);
488 repo->gitdir_fd = -1;
491 return err;
494 static const struct got_error *
495 parse_gitconfig_file(int *gitconfig_repository_format_version,
496 char **gitconfig_author_name, char **gitconfig_author_email,
497 struct got_remote_repo **remotes, int *nremotes,
498 char **gitconfig_owner, char ***extensions, int *nextensions,
499 const char *gitconfig_path)
501 const struct got_error *err = NULL, *child_err = NULL;
502 int fd = -1;
503 int imsg_fds[2] = { -1, -1 };
504 pid_t pid;
505 struct imsgbuf *ibuf;
507 *gitconfig_repository_format_version = 0;
508 if (extensions)
509 *extensions = NULL;
510 if (nextensions)
511 *nextensions = 0;
512 *gitconfig_author_name = NULL;
513 *gitconfig_author_email = NULL;
514 if (remotes)
515 *remotes = NULL;
516 if (nremotes)
517 *nremotes = 0;
518 if (gitconfig_owner)
519 *gitconfig_owner = NULL;
521 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
522 if (fd == -1) {
523 if (errno == ENOENT)
524 return NULL;
525 return got_error_from_errno2("open", gitconfig_path);
528 ibuf = calloc(1, sizeof(*ibuf));
529 if (ibuf == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
534 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
535 err = got_error_from_errno("socketpair");
536 goto done;
539 pid = fork();
540 if (pid == -1) {
541 err = got_error_from_errno("fork");
542 goto done;
543 } else if (pid == 0) {
544 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
545 gitconfig_path);
546 /* not reached */
549 if (close(imsg_fds[1]) == -1) {
550 err = got_error_from_errno("close");
551 goto done;
553 imsg_fds[1] = -1;
554 imsg_init(ibuf, imsg_fds[0]);
556 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
557 if (err)
558 goto done;
559 fd = -1;
561 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
562 if (err)
563 goto done;
565 err = got_privsep_recv_gitconfig_int(
566 gitconfig_repository_format_version, ibuf);
567 if (err)
568 goto done;
570 if (extensions && nextensions) {
571 err = got_privsep_send_gitconfig_repository_extensions_req(
572 ibuf);
573 if (err)
574 goto done;
575 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
576 if (err)
577 goto done;
578 if (*nextensions > 0) {
579 int i;
580 *extensions = calloc(*nextensions, sizeof(char *));
581 if (*extensions == NULL) {
582 err = got_error_from_errno("calloc");
583 goto done;
585 for (i = 0; i < *nextensions; i++) {
586 char *ext;
587 err = got_privsep_recv_gitconfig_str(&ext,
588 ibuf);
589 if (err)
590 goto done;
591 (*extensions)[i] = ext;
596 err = got_privsep_send_gitconfig_author_name_req(ibuf);
597 if (err)
598 goto done;
600 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_send_gitconfig_author_email_req(ibuf);
605 if (err)
606 goto done;
608 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
609 if (err)
610 goto done;
612 if (remotes && nremotes) {
613 err = got_privsep_send_gitconfig_remotes_req(ibuf);
614 if (err)
615 goto done;
617 err = got_privsep_recv_gitconfig_remotes(remotes,
618 nremotes, ibuf);
619 if (err)
620 goto done;
623 if (gitconfig_owner) {
624 err = got_privsep_send_gitconfig_owner_req(ibuf);
625 if (err)
626 goto done;
627 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
628 if (err)
629 goto done;
632 err = got_privsep_send_stop(imsg_fds[0]);
633 child_err = got_privsep_wait_for_child(pid);
634 if (child_err && err == NULL)
635 err = child_err;
636 done:
637 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
638 err = got_error_from_errno("close");
639 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
640 err = got_error_from_errno("close");
641 if (fd != -1 && close(fd) == -1 && err == NULL)
642 err = got_error_from_errno2("close", gitconfig_path);
643 free(ibuf);
644 return err;
647 static const struct got_error *
648 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
650 const struct got_error *err = NULL;
651 char *repo_gitconfig_path = NULL;
653 if (global_gitconfig_path) {
654 /* Read settings from ~/.gitconfig. */
655 int dummy_repo_version;
656 err = parse_gitconfig_file(&dummy_repo_version,
657 &repo->global_gitconfig_author_name,
658 &repo->global_gitconfig_author_email,
659 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
660 if (err)
661 return err;
664 /* Read repository's .git/config file. */
665 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
666 if (repo_gitconfig_path == NULL)
667 return got_error_from_errno("got_repo_get_path_gitconfig");
669 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
670 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
671 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
672 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
673 repo_gitconfig_path);
674 if (err)
675 goto done;
676 done:
677 free(repo_gitconfig_path);
678 return err;
681 static const struct got_error *
682 read_gotconfig(struct got_repository *repo)
684 const struct got_error *err = NULL;
685 char *gotconfig_path;
687 gotconfig_path = got_repo_get_path_gotconfig(repo);
688 if (gotconfig_path == NULL)
689 return got_error_from_errno("got_repo_get_path_gotconfig");
691 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
692 free(gotconfig_path);
693 return err;
696 /* Supported repository format extensions. */
697 static const char *const repo_extensions[] = {
698 "noop", /* Got supports repository format version 1. */
699 "preciousObjects", /* Supported by gotadmin cleanup. */
700 "worktreeConfig", /* Got does not care about Git work trees. */
701 };
703 const struct got_error *
704 got_repo_open(struct got_repository **repop, const char *path,
705 const char *global_gitconfig_path, int *pack_fds)
707 struct got_repository *repo = NULL;
708 const struct got_error *err = NULL;
709 char *repo_path = NULL;
710 size_t i, j = 0;
711 struct rlimit rl;
713 *repop = NULL;
715 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
716 return got_error_from_errno("getrlimit");
718 repo = calloc(1, sizeof(*repo));
719 if (repo == NULL)
720 return got_error_from_errno("calloc");
722 RB_INIT(&repo->packidx_bloom_filters);
723 TAILQ_INIT(&repo->packidx_paths);
725 for (i = 0; i < nitems(repo->privsep_children); i++) {
726 memset(&repo->privsep_children[i], 0,
727 sizeof(repo->privsep_children[0]));
728 repo->privsep_children[i].imsg_fd = -1;
731 err = got_object_cache_init(&repo->objcache,
732 GOT_OBJECT_CACHE_TYPE_OBJ);
733 if (err)
734 goto done;
735 err = got_object_cache_init(&repo->treecache,
736 GOT_OBJECT_CACHE_TYPE_TREE);
737 if (err)
738 goto done;
739 err = got_object_cache_init(&repo->commitcache,
740 GOT_OBJECT_CACHE_TYPE_COMMIT);
741 if (err)
742 goto done;
743 err = got_object_cache_init(&repo->tagcache,
744 GOT_OBJECT_CACHE_TYPE_TAG);
745 if (err)
746 goto done;
747 err = got_object_cache_init(&repo->rawcache,
748 GOT_OBJECT_CACHE_TYPE_RAW);
749 if (err)
750 goto done;
752 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
753 if (repo->pack_cache_size > rl.rlim_cur / 8)
754 repo->pack_cache_size = rl.rlim_cur / 8;
755 for (i = 0; i < nitems(repo->packs); i++) {
756 if (i < repo->pack_cache_size) {
757 repo->packs[i].basefd = pack_fds[j++];
758 repo->packs[i].accumfd = pack_fds[j++];
759 } else {
760 repo->packs[i].basefd = -1;
761 repo->packs[i].accumfd = -1;
764 repo->pinned_pack = -1;
765 repo->pinned_packidx = -1;
766 repo->pinned_pid = 0;
768 repo_path = realpath(path, NULL);
769 if (repo_path == NULL) {
770 err = got_error_from_errno2("realpath", path);
771 goto done;
774 for (;;) {
775 char *parent_path;
777 err = open_repo(repo, repo_path);
778 if (err == NULL)
779 break;
780 if (err->code != GOT_ERR_NOT_GIT_REPO)
781 goto done;
782 if (repo_path[0] == '/' && repo_path[1] == '\0') {
783 err = got_error(GOT_ERR_NOT_GIT_REPO);
784 goto done;
786 err = got_path_dirname(&parent_path, repo_path);
787 if (err)
788 goto done;
789 free(repo_path);
790 repo_path = parent_path;
793 err = read_gotconfig(repo);
794 if (err)
795 goto done;
797 err = read_gitconfig(repo, global_gitconfig_path);
798 if (err)
799 goto done;
800 if (repo->gitconfig_repository_format_version != 0) {
801 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
802 goto done;
804 for (i = 0; i < repo->nextensions; i++) {
805 char *ext = repo->extensions[i];
806 int j, supported = 0;
807 for (j = 0; j < nitems(repo_extensions); j++) {
808 if (strcmp(ext, repo_extensions[j]) == 0) {
809 supported = 1;
810 break;
813 if (!supported) {
814 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
815 goto done;
819 err = got_repo_list_packidx(&repo->packidx_paths, repo);
820 done:
821 if (err)
822 got_repo_close(repo);
823 else
824 *repop = repo;
825 free(repo_path);
826 return err;
829 const struct got_error *
830 got_repo_close(struct got_repository *repo)
832 const struct got_error *err = NULL, *child_err;
833 struct got_packidx_bloom_filter *bf;
834 struct got_pathlist_entry *pe;
835 size_t i;
837 for (i = 0; i < repo->pack_cache_size; i++) {
838 if (repo->packidx_cache[i] == NULL)
839 break;
840 got_packidx_close(repo->packidx_cache[i]);
843 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
844 &repo->packidx_bloom_filters))) {
845 RB_REMOVE(got_packidx_bloom_filter_tree,
846 &repo->packidx_bloom_filters, bf);
847 free(bf->bloom);
848 free(bf);
851 for (i = 0; i < repo->pack_cache_size; i++)
852 if (repo->packs[i].path_packfile)
853 if (repo->packs[i].path_packfile)
854 got_pack_close(&repo->packs[i]);
856 free(repo->path);
857 free(repo->path_git_dir);
859 got_object_cache_close(&repo->objcache);
860 got_object_cache_close(&repo->treecache);
861 got_object_cache_close(&repo->commitcache);
862 got_object_cache_close(&repo->tagcache);
863 got_object_cache_close(&repo->rawcache);
865 for (i = 0; i < nitems(repo->privsep_children); i++) {
866 if (repo->privsep_children[i].imsg_fd == -1)
867 continue;
868 imsg_clear(repo->privsep_children[i].ibuf);
869 free(repo->privsep_children[i].ibuf);
870 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
871 child_err = got_privsep_wait_for_child(
872 repo->privsep_children[i].pid);
873 if (child_err && err == NULL)
874 err = child_err;
875 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
876 err == NULL)
877 err = got_error_from_errno("close");
880 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
881 err == NULL)
882 err = got_error_from_errno("close");
884 if (repo->gotconfig)
885 got_gotconfig_free(repo->gotconfig);
886 free(repo->gitconfig_author_name);
887 free(repo->gitconfig_author_email);
888 for (i = 0; i < repo->ngitconfig_remotes; i++)
889 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
890 free(repo->gitconfig_remotes);
891 for (i = 0; i < repo->nextensions; i++)
892 free(repo->extensions[i]);
893 free(repo->extensions);
895 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
896 free((void *)pe->path);
897 got_pathlist_free(&repo->packidx_paths);
898 free(repo);
900 return err;
903 void
904 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
906 int i;
908 free(repo->name);
909 repo->name = NULL;
910 free(repo->fetch_url);
911 repo->fetch_url = NULL;
912 free(repo->send_url);
913 repo->send_url = NULL;
914 for (i = 0; i < repo->nfetch_branches; i++)
915 free(repo->fetch_branches[i]);
916 free(repo->fetch_branches);
917 repo->fetch_branches = NULL;
918 repo->nfetch_branches = 0;
919 for (i = 0; i < repo->nsend_branches; i++)
920 free(repo->send_branches[i]);
921 free(repo->send_branches);
922 repo->send_branches = NULL;
923 repo->nsend_branches = 0;
926 const struct got_error *
927 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
928 const char *input_path)
930 const struct got_error *err = NULL;
931 const char *repo_abspath = NULL;
932 size_t repolen, len;
933 char *canonpath, *path = NULL;
935 *in_repo_path = NULL;
937 canonpath = strdup(input_path);
938 if (canonpath == NULL) {
939 err = got_error_from_errno("strdup");
940 goto done;
942 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
943 if (err)
944 goto done;
946 repo_abspath = got_repo_get_path(repo);
948 if (canonpath[0] == '\0') {
949 path = strdup(canonpath);
950 if (path == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
954 } else {
955 path = realpath(canonpath, NULL);
956 if (path == NULL) {
957 if (errno != ENOENT) {
958 err = got_error_from_errno2("realpath",
959 canonpath);
960 goto done;
962 /*
963 * Path is not on disk.
964 * Assume it is already relative to repository root.
965 */
966 path = strdup(canonpath);
967 if (path == NULL) {
968 err = got_error_from_errno("strdup");
969 goto done;
973 repolen = strlen(repo_abspath);
974 len = strlen(path);
977 if (strcmp(path, repo_abspath) == 0) {
978 free(path);
979 path = strdup("");
980 if (path == NULL) {
981 err = got_error_from_errno("strdup");
982 goto done;
984 } else if (len > repolen &&
985 got_path_is_child(path, repo_abspath, repolen)) {
986 /* Matched an on-disk path inside repository. */
987 if (got_repo_is_bare(repo)) {
988 /*
989 * Matched an on-disk path inside repository
990 * database. Treat input as repository-relative.
991 */
992 free(path);
993 path = canonpath;
994 canonpath = NULL;
995 } else {
996 char *child;
997 /* Strip common prefix with repository path. */
998 err = got_path_skip_common_ancestor(&child,
999 repo_abspath, path);
1000 if (err)
1001 goto done;
1002 free(path);
1003 path = child;
1005 } else {
1007 * Matched unrelated on-disk path.
1008 * Treat input as repository-relative.
1010 free(path);
1011 path = canonpath;
1012 canonpath = NULL;
1016 /* Make in-repository path absolute */
1017 if (path[0] != '/') {
1018 char *abspath;
1019 if (asprintf(&abspath, "/%s", path) == -1) {
1020 err = got_error_from_errno("asprintf");
1021 goto done;
1023 free(path);
1024 path = abspath;
1027 done:
1028 free(canonpath);
1029 if (err)
1030 free(path);
1031 else
1032 *in_repo_path = path;
1033 return err;
1036 static const struct got_error *
1037 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1038 const char *path_packidx)
1040 const struct got_error *err = NULL;
1041 size_t i;
1043 for (i = 0; i < repo->pack_cache_size; i++) {
1044 if (repo->packidx_cache[i] == NULL)
1045 break;
1046 if (strcmp(repo->packidx_cache[i]->path_packidx,
1047 path_packidx) == 0) {
1048 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1051 if (i == repo->pack_cache_size) {
1052 do {
1053 i--;
1054 } while (i > 0 && repo->pinned_packidx >= 0 &&
1055 i == repo->pinned_packidx);
1056 err = got_packidx_close(repo->packidx_cache[i]);
1057 if (err)
1058 return err;
1061 repo->packidx_cache[i] = packidx;
1063 return NULL;
1066 int
1067 got_repo_is_packidx_filename(const char *name, size_t len)
1069 if (len != GOT_PACKIDX_NAMELEN)
1070 return 0;
1072 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1073 return 0;
1075 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1076 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1077 return 0;
1079 return 1;
1082 static struct got_packidx_bloom_filter *
1083 get_packidx_bloom_filter(struct got_repository *repo,
1084 const char *path, size_t path_len)
1086 struct got_packidx_bloom_filter key;
1088 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1089 return NULL; /* XXX */
1090 key.path_len = path_len;
1092 return RB_FIND(got_packidx_bloom_filter_tree,
1093 &repo->packidx_bloom_filters, &key);
1096 int
1097 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1098 const char *path_packidx, struct got_object_id *id)
1100 struct got_packidx_bloom_filter *bf;
1102 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1103 if (bf)
1104 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1106 /* No bloom filter means this pack index must be searched. */
1107 return 1;
1110 static const struct got_error *
1111 add_packidx_bloom_filter(struct got_repository *repo,
1112 struct got_packidx *packidx, const char *path_packidx)
1114 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1115 struct got_packidx_bloom_filter *bf;
1116 size_t len;
1119 * Don't use bloom filters for very large pack index files.
1120 * Large pack files will contain a relatively large fraction
1121 * of our objects so we will likely need to visit them anyway.
1122 * The more objects a pack file contains the higher the probability
1123 * of a false-positive match from the bloom filter. And reading
1124 * all object IDs from a large pack index file can be expensive.
1126 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1127 return NULL;
1129 /* Do we already have a filter for this pack index? */
1130 if (get_packidx_bloom_filter(repo, path_packidx,
1131 strlen(path_packidx)) != NULL)
1132 return NULL;
1134 bf = calloc(1, sizeof(*bf));
1135 if (bf == NULL)
1136 return got_error_from_errno("calloc");
1137 bf->bloom = calloc(1, sizeof(*bf->bloom));
1138 if (bf->bloom == NULL) {
1139 free(bf);
1140 return got_error_from_errno("calloc");
1143 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1144 if (len >= sizeof(bf->path)) {
1145 free(bf->bloom);
1146 free(bf);
1147 return got_error(GOT_ERR_NO_SPACE);
1149 bf->path_len = len;
1151 /* Minimum size supported by our bloom filter is 1000 entries. */
1152 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1153 for (i = 0; i < nobjects; i++) {
1154 struct got_packidx_object_id *id;
1155 id = &packidx->hdr.sorted_ids[i];
1156 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1159 RB_INSERT(got_packidx_bloom_filter_tree,
1160 &repo->packidx_bloom_filters, bf);
1161 return NULL;
1164 const struct got_error *
1165 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1166 struct got_repository *repo, struct got_object_id *id)
1168 const struct got_error *err;
1169 struct got_pathlist_entry *pe;
1170 size_t i;
1172 /* Search pack index cache. */
1173 for (i = 0; i < repo->pack_cache_size; i++) {
1174 if (repo->packidx_cache[i] == NULL)
1175 break;
1176 if (!got_repo_check_packidx_bloom_filter(repo,
1177 repo->packidx_cache[i]->path_packidx, id))
1178 continue; /* object will not be found in this index */
1179 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1180 if (*idx != -1) {
1181 *packidx = repo->packidx_cache[i];
1183 * Move this cache entry to the front. Repeatedly
1184 * searching a wrong pack index can be expensive.
1186 if (i > 0) {
1187 memmove(&repo->packidx_cache[1],
1188 &repo->packidx_cache[0],
1189 i * sizeof(repo->packidx_cache[0]));
1190 repo->packidx_cache[0] = *packidx;
1191 if (repo->pinned_packidx >= 0 &&
1192 repo->pinned_packidx < i)
1193 repo->pinned_packidx++;
1194 else if (repo->pinned_packidx == i)
1195 repo->pinned_packidx = 0;
1197 return NULL;
1200 /* No luck. Search the filesystem. */
1202 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1203 const char *path_packidx = pe->path;
1204 int is_cached = 0;
1206 if (!got_repo_check_packidx_bloom_filter(repo,
1207 pe->path, id))
1208 continue; /* object will not be found in this index */
1210 for (i = 0; i < repo->pack_cache_size; i++) {
1211 if (repo->packidx_cache[i] == NULL)
1212 break;
1213 if (strcmp(repo->packidx_cache[i]->path_packidx,
1214 path_packidx) == 0) {
1215 is_cached = 1;
1216 break;
1219 if (is_cached)
1220 continue; /* already searched */
1222 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1223 path_packidx, 0);
1224 if (err)
1225 goto done;
1227 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1228 if (err)
1229 goto done;
1231 err = cache_packidx(repo, *packidx, path_packidx);
1232 if (err)
1233 goto done;
1235 *idx = got_packidx_get_object_idx(*packidx, id);
1236 if (*idx != -1) {
1237 err = NULL; /* found the object */
1238 goto done;
1242 err = got_error_no_obj(id);
1243 done:
1244 return err;
1247 const struct got_error *
1248 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1249 struct got_repository *repo)
1251 const struct got_error *err = NULL;
1252 DIR *packdir = NULL;
1253 struct dirent *dent;
1254 char *path_packidx = NULL;
1255 int packdir_fd;
1257 packdir_fd = openat(got_repo_get_fd(repo),
1258 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1259 if (packdir_fd == -1) {
1260 return got_error_from_errno_fmt("openat: %s/%s",
1261 got_repo_get_path_git_dir(repo),
1262 GOT_OBJECTS_PACK_DIR);
1265 packdir = fdopendir(packdir_fd);
1266 if (packdir == NULL) {
1267 err = got_error_from_errno("fdopendir");
1268 goto done;
1271 while ((dent = readdir(packdir)) != NULL) {
1272 if (!got_repo_is_packidx_filename(dent->d_name,
1273 strlen(dent->d_name)))
1274 continue;
1276 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1277 dent->d_name) == -1) {
1278 err = got_error_from_errno("asprintf");
1279 path_packidx = NULL;
1280 break;
1283 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1284 if (err)
1285 break;
1287 done:
1288 if (err)
1289 free(path_packidx);
1290 if (packdir && closedir(packdir) != 0 && err == NULL)
1291 err = got_error_from_errno("closedir");
1292 return err;
1295 const struct got_error *
1296 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1297 struct got_repository *repo)
1299 const struct got_error *err;
1300 size_t i;
1302 *packidx = NULL;
1304 /* Search pack index cache. */
1305 for (i = 0; i < repo->pack_cache_size; i++) {
1306 if (repo->packidx_cache[i] == NULL)
1307 break;
1308 if (strcmp(repo->packidx_cache[i]->path_packidx,
1309 path_packidx) == 0) {
1310 *packidx = repo->packidx_cache[i];
1311 return NULL;
1314 /* No luck. Search the filesystem. */
1316 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1317 path_packidx, 0);
1318 if (err)
1319 return err;
1321 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1322 if (err)
1323 goto done;
1325 err = cache_packidx(repo, *packidx, path_packidx);
1326 done:
1327 if (err) {
1328 got_packidx_close(*packidx);
1329 *packidx = NULL;
1331 return err;
1334 static const struct got_error *
1335 read_packfile_hdr(int fd, struct got_packidx *packidx)
1337 const struct got_error *err = NULL;
1338 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1339 struct got_packfile_hdr hdr;
1340 ssize_t n;
1342 n = read(fd, &hdr, sizeof(hdr));
1343 if (n < 0)
1344 return got_error_from_errno("read");
1345 if (n != sizeof(hdr))
1346 return got_error(GOT_ERR_BAD_PACKFILE);
1348 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1349 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1350 be32toh(hdr.nobjects) != totobj)
1351 err = got_error(GOT_ERR_BAD_PACKFILE);
1353 return err;
1356 static const struct got_error *
1357 open_packfile(int *fd, struct got_repository *repo,
1358 const char *relpath, struct got_packidx *packidx)
1360 const struct got_error *err = NULL;
1362 *fd = openat(got_repo_get_fd(repo), relpath,
1363 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1364 if (*fd == -1)
1365 return got_error_from_errno_fmt("openat: %s/%s",
1366 got_repo_get_path_git_dir(repo), relpath);
1368 if (packidx) {
1369 err = read_packfile_hdr(*fd, packidx);
1370 if (err) {
1371 close(*fd);
1372 *fd = -1;
1376 return err;
1379 const struct got_error *
1380 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1381 const char *path_packfile, struct got_packidx *packidx)
1383 const struct got_error *err = NULL;
1384 struct got_pack *pack = NULL;
1385 struct stat sb;
1386 size_t i;
1388 if (packp)
1389 *packp = NULL;
1391 for (i = 0; i < repo->pack_cache_size; i++) {
1392 pack = &repo->packs[i];
1393 if (pack->path_packfile == NULL)
1394 break;
1395 if (strcmp(pack->path_packfile, path_packfile) == 0)
1396 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1399 if (i == repo->pack_cache_size) {
1400 struct got_pack tmp;
1401 do {
1402 i--;
1403 } while (i > 0 && repo->pinned_pack >= 0 &&
1404 i == repo->pinned_pack);
1405 err = got_pack_close(&repo->packs[i]);
1406 if (err)
1407 return err;
1408 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1409 return got_error_from_errno("ftruncate");
1410 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1411 return got_error_from_errno("ftruncate");
1412 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1413 memcpy(&repo->packs[i], &repo->packs[0],
1414 sizeof(repo->packs[i]));
1415 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1416 if (repo->pinned_pack == 0)
1417 repo->pinned_pack = i;
1418 else if (repo->pinned_pack == i)
1419 repo->pinned_pack = 0;
1420 i = 0;
1423 pack = &repo->packs[i];
1425 pack->path_packfile = strdup(path_packfile);
1426 if (pack->path_packfile == NULL) {
1427 err = got_error_from_errno("strdup");
1428 goto done;
1431 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1432 if (err)
1433 goto done;
1435 if (fstat(pack->fd, &sb) != 0) {
1436 err = got_error_from_errno("fstat");
1437 goto done;
1439 pack->filesize = sb.st_size;
1441 pack->privsep_child = NULL;
1443 #ifndef GOT_PACK_NO_MMAP
1444 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1445 pack->fd, 0);
1446 if (pack->map == MAP_FAILED) {
1447 if (errno != ENOMEM) {
1448 err = got_error_from_errno("mmap");
1449 goto done;
1451 pack->map = NULL; /* fall back to read(2) */
1453 #endif
1454 done:
1455 if (err) {
1456 if (pack) {
1457 free(pack->path_packfile);
1458 memset(pack, 0, sizeof(*pack));
1460 } else if (packp)
1461 *packp = pack;
1462 return err;
1465 struct got_pack *
1466 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1468 struct got_pack *pack = NULL;
1469 size_t i;
1471 for (i = 0; i < repo->pack_cache_size; i++) {
1472 pack = &repo->packs[i];
1473 if (pack->path_packfile == NULL)
1474 break;
1475 if (strcmp(pack->path_packfile, path_packfile) == 0)
1476 return pack;
1479 return NULL;
1482 const struct got_error *
1483 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1484 struct got_pack *pack)
1486 size_t i;
1487 int pinned_pack = -1, pinned_packidx = -1;
1489 for (i = 0; i < repo->pack_cache_size; i++) {
1490 if (repo->packidx_cache[i] &&
1491 strcmp(repo->packidx_cache[i]->path_packidx,
1492 packidx->path_packidx) == 0)
1493 pinned_packidx = i;
1494 if (repo->packs[i].path_packfile &&
1495 strcmp(repo->packs[i].path_packfile,
1496 pack->path_packfile) == 0)
1497 pinned_pack = i;
1500 if (pinned_packidx == -1 || pinned_pack == -1)
1501 return got_error(GOT_ERR_PIN_PACK);
1503 repo->pinned_pack = pinned_pack;
1504 repo->pinned_packidx = pinned_packidx;
1505 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1506 return NULL;
1509 struct got_pack *
1510 got_repo_get_pinned_pack(struct got_repository *repo)
1512 if (repo->pinned_pack >= 0 &&
1513 repo->pinned_pack < repo->pack_cache_size)
1514 return &repo->packs[repo->pinned_pack];
1516 return NULL;
1519 void
1520 got_repo_unpin_pack(struct got_repository *repo)
1522 repo->pinned_packidx = -1;
1523 repo->pinned_pack = -1;
1524 repo->pinned_pid = 0;
1527 const struct got_error *
1528 got_repo_init(const char *repo_path)
1530 const struct got_error *err = NULL;
1531 const char *dirnames[] = {
1532 GOT_OBJECTS_DIR,
1533 GOT_OBJECTS_PACK_DIR,
1534 GOT_REFS_DIR,
1536 const char *description_str = "Unnamed repository; "
1537 "edit this file 'description' to name the repository.";
1538 const char *headref_str = "ref: refs/heads/main";
1539 const char *gitconfig_str = "[core]\n"
1540 "\trepositoryformatversion = 0\n"
1541 "\tfilemode = true\n"
1542 "\tbare = true\n";
1543 char *path;
1544 size_t i;
1546 if (!got_path_dir_is_empty(repo_path))
1547 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1549 for (i = 0; i < nitems(dirnames); i++) {
1550 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1551 return got_error_from_errno("asprintf");
1553 err = got_path_mkdir(path);
1554 free(path);
1555 if (err)
1556 return err;
1559 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1560 return got_error_from_errno("asprintf");
1561 err = got_path_create_file(path, description_str);
1562 free(path);
1563 if (err)
1564 return err;
1566 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1567 return got_error_from_errno("asprintf");
1568 err = got_path_create_file(path, headref_str);
1569 free(path);
1570 if (err)
1571 return err;
1573 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1574 return got_error_from_errno("asprintf");
1575 err = got_path_create_file(path, gitconfig_str);
1576 free(path);
1577 if (err)
1578 return err;
1580 return NULL;
1583 static const struct got_error *
1584 match_packed_object(struct got_object_id **unique_id,
1585 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1587 const struct got_error *err = NULL;
1588 struct got_object_id_queue matched_ids;
1589 struct got_pathlist_entry *pe;
1591 STAILQ_INIT(&matched_ids);
1593 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1594 const char *path_packidx = pe->path;
1595 struct got_packidx *packidx;
1596 struct got_object_qid *qid;
1598 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1599 path_packidx, 0);
1600 if (err)
1601 break;
1603 err = got_packidx_match_id_str_prefix(&matched_ids,
1604 packidx, id_str_prefix);
1605 if (err) {
1606 got_packidx_close(packidx);
1607 break;
1609 err = got_packidx_close(packidx);
1610 if (err)
1611 break;
1613 STAILQ_FOREACH(qid, &matched_ids, entry) {
1614 if (obj_type != GOT_OBJ_TYPE_ANY) {
1615 int matched_type;
1616 err = got_object_get_type(&matched_type, repo,
1617 &qid->id);
1618 if (err)
1619 goto done;
1620 if (matched_type != obj_type)
1621 continue;
1623 if (*unique_id == NULL) {
1624 *unique_id = got_object_id_dup(&qid->id);
1625 if (*unique_id == NULL) {
1626 err = got_error_from_errno("malloc");
1627 goto done;
1629 } else {
1630 if (got_object_id_cmp(*unique_id,
1631 &qid->id) == 0)
1632 continue; /* packed multiple times */
1633 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1634 goto done;
1638 done:
1639 got_object_id_queue_free(&matched_ids);
1640 if (err) {
1641 free(*unique_id);
1642 *unique_id = NULL;
1644 return err;
1647 static const struct got_error *
1648 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1649 const char *object_dir, const char *id_str_prefix, int obj_type,
1650 struct got_repository *repo)
1652 const struct got_error *err = NULL;
1653 char *path;
1654 DIR *dir = NULL;
1655 struct dirent *dent;
1656 struct got_object_id id;
1658 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1659 err = got_error_from_errno("asprintf");
1660 goto done;
1663 dir = opendir(path);
1664 if (dir == NULL) {
1665 if (errno == ENOENT) {
1666 err = NULL;
1667 goto done;
1669 err = got_error_from_errno2("opendir", path);
1670 goto done;
1672 while ((dent = readdir(dir)) != NULL) {
1673 char *id_str;
1674 int cmp;
1676 if (strcmp(dent->d_name, ".") == 0 ||
1677 strcmp(dent->d_name, "..") == 0)
1678 continue;
1680 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1681 err = got_error_from_errno("asprintf");
1682 goto done;
1685 if (!got_parse_sha1_digest(id.sha1, id_str))
1686 continue;
1689 * Directory entries do not necessarily appear in
1690 * sorted order, so we must iterate over all of them.
1692 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1693 if (cmp != 0) {
1694 free(id_str);
1695 continue;
1698 if (*unique_id == NULL) {
1699 if (obj_type != GOT_OBJ_TYPE_ANY) {
1700 int matched_type;
1701 err = got_object_get_type(&matched_type, repo,
1702 &id);
1703 if (err)
1704 goto done;
1705 if (matched_type != obj_type)
1706 continue;
1708 *unique_id = got_object_id_dup(&id);
1709 if (*unique_id == NULL) {
1710 err = got_error_from_errno("got_object_id_dup");
1711 free(id_str);
1712 goto done;
1714 } else {
1715 if (got_object_id_cmp(*unique_id, &id) == 0)
1716 continue; /* both packed and loose */
1717 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1718 free(id_str);
1719 goto done;
1722 done:
1723 if (dir && closedir(dir) != 0 && err == NULL)
1724 err = got_error_from_errno("closedir");
1725 if (err) {
1726 free(*unique_id);
1727 *unique_id = NULL;
1729 free(path);
1730 return err;
1733 const struct got_error *
1734 got_repo_match_object_id_prefix(struct got_object_id **id,
1735 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1737 const struct got_error *err = NULL;
1738 char *path_objects = got_repo_get_path_objects(repo);
1739 char *object_dir = NULL;
1740 size_t len;
1741 int i;
1743 *id = NULL;
1745 len = strlen(id_str_prefix);
1746 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1747 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1749 for (i = 0; i < len; i++) {
1750 if (isxdigit((unsigned char)id_str_prefix[i]))
1751 continue;
1752 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1755 if (len >= 2) {
1756 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1757 if (err)
1758 goto done;
1759 object_dir = strndup(id_str_prefix, 2);
1760 if (object_dir == NULL) {
1761 err = got_error_from_errno("strdup");
1762 goto done;
1764 err = match_loose_object(id, path_objects, object_dir,
1765 id_str_prefix, obj_type, repo);
1766 } else if (len == 1) {
1767 int i;
1768 for (i = 0; i < 0xf; i++) {
1769 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1770 == -1) {
1771 err = got_error_from_errno("asprintf");
1772 goto done;
1774 err = match_packed_object(id, repo, object_dir,
1775 obj_type);
1776 if (err)
1777 goto done;
1778 err = match_loose_object(id, path_objects, object_dir,
1779 id_str_prefix, obj_type, repo);
1780 if (err)
1781 goto done;
1783 } else {
1784 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1785 goto done;
1787 done:
1788 free(object_dir);
1789 if (err) {
1790 free(*id);
1791 *id = NULL;
1792 } else if (*id == NULL) {
1793 switch (obj_type) {
1794 case GOT_OBJ_TYPE_BLOB:
1795 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1796 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1797 break;
1798 case GOT_OBJ_TYPE_TREE:
1799 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1800 GOT_OBJ_LABEL_TREE, id_str_prefix);
1801 break;
1802 case GOT_OBJ_TYPE_COMMIT:
1803 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1804 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1805 break;
1806 case GOT_OBJ_TYPE_TAG:
1807 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1808 GOT_OBJ_LABEL_TAG, id_str_prefix);
1809 break;
1810 default:
1811 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1812 break;
1816 return err;
1819 const struct got_error *
1820 got_repo_match_object_id(struct got_object_id **id, char **label,
1821 const char *id_str, int obj_type, struct got_reflist_head *refs,
1822 struct got_repository *repo)
1824 const struct got_error *err;
1825 struct got_tag_object *tag;
1826 struct got_reference *ref = NULL;
1828 *id = NULL;
1829 if (label)
1830 *label = NULL;
1832 if (refs) {
1833 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1834 refs, repo);
1835 if (err == NULL) {
1836 *id = got_object_id_dup(
1837 got_object_tag_get_object_id(tag));
1838 if (*id == NULL)
1839 err = got_error_from_errno("got_object_id_dup");
1840 else if (label && asprintf(label, "refs/tags/%s",
1841 got_object_tag_get_name(tag)) == -1) {
1842 err = got_error_from_errno("asprintf");
1843 free(*id);
1844 *id = NULL;
1846 got_object_tag_close(tag);
1847 return err;
1848 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1849 err->code != GOT_ERR_NO_OBJ)
1850 return err;
1853 err = got_ref_open(&ref, repo, id_str, 0);
1854 if (err == NULL) {
1855 err = got_ref_resolve(id, repo, ref);
1856 if (err)
1857 goto done;
1858 if (label) {
1859 *label = strdup(got_ref_get_name(ref));
1860 if (*label == NULL) {
1861 err = got_error_from_errno("strdup");
1862 goto done;
1865 } else {
1866 if (err->code != GOT_ERR_NOT_REF &&
1867 err->code != GOT_ERR_BAD_REF_NAME)
1868 goto done;
1869 err = got_repo_match_object_id_prefix(id, id_str,
1870 obj_type, repo);
1871 if (err) {
1872 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1873 err = got_error_not_ref(id_str);
1874 goto done;
1876 if (label) {
1877 err = got_object_id_str(label, *id);
1878 if (*label == NULL) {
1879 err = got_error_from_errno("strdup");
1880 goto done;
1884 done:
1885 if (ref)
1886 got_ref_close(ref);
1887 return err;
1890 const struct got_error *
1891 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1892 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1894 const struct got_error *err = NULL;
1895 struct got_reflist_entry *re;
1896 struct got_object_id *tag_id;
1897 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1899 *tag = NULL;
1901 TAILQ_FOREACH(re, refs, entry) {
1902 const char *refname;
1903 refname = got_ref_get_name(re->ref);
1904 if (got_ref_is_symbolic(re->ref))
1905 continue;
1906 if (strncmp(refname, "refs/tags/", 10) != 0)
1907 continue;
1908 if (!name_is_absolute)
1909 refname += strlen("refs/tags/");
1910 if (strcmp(refname, name) != 0)
1911 continue;
1912 err = got_ref_resolve(&tag_id, repo, re->ref);
1913 if (err)
1914 break;
1915 err = got_object_open_as_tag(tag, repo, tag_id);
1916 free(tag_id);
1917 if (err)
1918 break;
1919 if (obj_type == GOT_OBJ_TYPE_ANY ||
1920 got_object_tag_get_object_type(*tag) == obj_type)
1921 break;
1922 got_object_tag_close(*tag);
1923 *tag = NULL;
1926 if (err == NULL && *tag == NULL)
1927 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1928 GOT_OBJ_LABEL_TAG, name);
1929 return err;
1932 static const struct got_error *
1933 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1934 const char *name, mode_t mode, struct got_object_id *blob_id)
1936 const struct got_error *err = NULL;
1938 *new_te = NULL;
1940 *new_te = calloc(1, sizeof(**new_te));
1941 if (*new_te == NULL)
1942 return got_error_from_errno("calloc");
1944 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1945 sizeof((*new_te)->name)) {
1946 err = got_error(GOT_ERR_NO_SPACE);
1947 goto done;
1950 if (S_ISLNK(mode)) {
1951 (*new_te)->mode = S_IFLNK;
1952 } else {
1953 (*new_te)->mode = S_IFREG;
1954 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1956 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1957 done:
1958 if (err && *new_te) {
1959 free(*new_te);
1960 *new_te = NULL;
1962 return err;
1965 static const struct got_error *
1966 import_file(struct got_tree_entry **new_te, struct dirent *de,
1967 const char *path, struct got_repository *repo)
1969 const struct got_error *err;
1970 struct got_object_id *blob_id = NULL;
1971 char *filepath;
1972 struct stat sb;
1974 if (asprintf(&filepath, "%s%s%s", path,
1975 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1976 return got_error_from_errno("asprintf");
1978 if (lstat(filepath, &sb) != 0) {
1979 err = got_error_from_errno2("lstat", path);
1980 goto done;
1983 err = got_object_blob_create(&blob_id, filepath, repo);
1984 if (err)
1985 goto done;
1987 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1988 blob_id);
1989 done:
1990 free(filepath);
1991 if (err)
1992 free(blob_id);
1993 return err;
1996 static const struct got_error *
1997 insert_tree_entry(struct got_tree_entry *new_te,
1998 struct got_pathlist_head *paths)
2000 const struct got_error *err = NULL;
2001 struct got_pathlist_entry *new_pe;
2003 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2004 if (err)
2005 return err;
2006 if (new_pe == NULL)
2007 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2008 return NULL;
2011 static const struct got_error *write_tree(struct got_object_id **,
2012 const char *, struct got_pathlist_head *, struct got_repository *,
2013 got_repo_import_cb progress_cb, void *progress_arg);
2015 static const struct got_error *
2016 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2017 const char *path, struct got_pathlist_head *ignores,
2018 struct got_repository *repo,
2019 got_repo_import_cb progress_cb, void *progress_arg)
2021 const struct got_error *err;
2022 struct got_object_id *id = NULL;
2023 char *subdirpath;
2025 if (asprintf(&subdirpath, "%s%s%s", path,
2026 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2027 return got_error_from_errno("asprintf");
2029 (*new_te) = calloc(1, sizeof(**new_te));
2030 if (*new_te == NULL)
2031 return got_error_from_errno("calloc");
2032 (*new_te)->mode = S_IFDIR;
2033 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2034 sizeof((*new_te)->name)) {
2035 err = got_error(GOT_ERR_NO_SPACE);
2036 goto done;
2038 err = write_tree(&id, subdirpath, ignores, repo,
2039 progress_cb, progress_arg);
2040 if (err)
2041 goto done;
2042 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2044 done:
2045 free(id);
2046 free(subdirpath);
2047 if (err) {
2048 free(*new_te);
2049 *new_te = NULL;
2051 return err;
2054 static const struct got_error *
2055 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2056 struct got_pathlist_head *ignores, struct got_repository *repo,
2057 got_repo_import_cb progress_cb, void *progress_arg)
2059 const struct got_error *err = NULL;
2060 DIR *dir;
2061 struct dirent *de;
2062 int nentries;
2063 struct got_tree_entry *new_te = NULL;
2064 struct got_pathlist_head paths;
2065 struct got_pathlist_entry *pe;
2067 *new_tree_id = NULL;
2069 TAILQ_INIT(&paths);
2071 dir = opendir(path_dir);
2072 if (dir == NULL) {
2073 err = got_error_from_errno2("opendir", path_dir);
2074 goto done;
2077 nentries = 0;
2078 while ((de = readdir(dir)) != NULL) {
2079 int ignore = 0;
2080 int type;
2082 if (strcmp(de->d_name, ".") == 0 ||
2083 strcmp(de->d_name, "..") == 0)
2084 continue;
2086 TAILQ_FOREACH(pe, ignores, entry) {
2087 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2088 ignore = 1;
2089 break;
2092 if (ignore)
2093 continue;
2095 err = got_path_dirent_type(&type, path_dir, de);
2096 if (err)
2097 goto done;
2099 if (type == DT_DIR) {
2100 err = import_subdir(&new_te, de, path_dir,
2101 ignores, repo, progress_cb, progress_arg);
2102 if (err) {
2103 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2104 goto done;
2105 err = NULL;
2106 continue;
2108 } else if (type == DT_REG || type == DT_LNK) {
2109 err = import_file(&new_te, de, path_dir, repo);
2110 if (err)
2111 goto done;
2112 } else
2113 continue;
2115 err = insert_tree_entry(new_te, &paths);
2116 if (err)
2117 goto done;
2118 nentries++;
2121 if (TAILQ_EMPTY(&paths)) {
2122 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2123 "cannot create tree without any entries");
2124 goto done;
2127 TAILQ_FOREACH(pe, &paths, entry) {
2128 struct got_tree_entry *te = pe->data;
2129 char *path;
2130 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2131 continue;
2132 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2133 err = got_error_from_errno("asprintf");
2134 goto done;
2136 err = (*progress_cb)(progress_arg, path);
2137 free(path);
2138 if (err)
2139 goto done;
2142 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2143 done:
2144 if (dir)
2145 closedir(dir);
2146 got_pathlist_free(&paths);
2147 return err;
2150 const struct got_error *
2151 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2152 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2153 struct got_repository *repo, got_repo_import_cb progress_cb,
2154 void *progress_arg)
2156 const struct got_error *err;
2157 struct got_object_id *new_tree_id;
2159 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2160 progress_cb, progress_arg);
2161 if (err)
2162 return err;
2164 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2165 author, time(NULL), author, time(NULL), logmsg, repo);
2166 free(new_tree_id);
2167 return err;
2170 const struct got_error *
2171 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 char *path_objects = NULL, *path = NULL;
2176 DIR *dir = NULL;
2177 struct got_object_id id;
2178 int i;
2180 *nobjects = 0;
2181 *ondisk_size = 0;
2183 path_objects = got_repo_get_path_objects(repo);
2184 if (path_objects == NULL)
2185 return got_error_from_errno("got_repo_get_path_objects");
2187 for (i = 0; i <= 0xff; i++) {
2188 struct dirent *dent;
2190 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2191 err = got_error_from_errno("asprintf");
2192 break;
2195 dir = opendir(path);
2196 if (dir == NULL) {
2197 if (errno == ENOENT) {
2198 err = NULL;
2199 continue;
2201 err = got_error_from_errno2("opendir", path);
2202 break;
2205 while ((dent = readdir(dir)) != NULL) {
2206 char *id_str;
2207 int fd;
2208 struct stat sb;
2210 if (strcmp(dent->d_name, ".") == 0 ||
2211 strcmp(dent->d_name, "..") == 0)
2212 continue;
2214 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2215 err = got_error_from_errno("asprintf");
2216 goto done;
2219 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2220 free(id_str);
2221 continue;
2223 free(id_str);
2225 err = got_object_open_loose_fd(&fd, &id, repo);
2226 if (err)
2227 goto done;
2229 if (fstat(fd, &sb) == -1) {
2230 err = got_error_from_errno("fstat");
2231 close(fd);
2232 goto done;
2234 (*nobjects)++;
2235 (*ondisk_size) += sb.st_size;
2237 if (close(fd) == -1) {
2238 err = got_error_from_errno("close");
2239 goto done;
2243 if (closedir(dir) != 0) {
2244 err = got_error_from_errno("closedir");
2245 goto done;
2247 dir = NULL;
2249 free(path);
2250 path = NULL;
2252 done:
2253 if (dir && closedir(dir) != 0 && err == NULL)
2254 err = got_error_from_errno("closedir");
2256 if (err) {
2257 *nobjects = 0;
2258 *ondisk_size = 0;
2260 free(path_objects);
2261 free(path);
2262 return err;
2265 const struct got_error *
2266 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2267 off_t *total_packsize, struct got_repository *repo)
2269 const struct got_error *err = NULL;
2270 DIR *packdir = NULL;
2271 struct dirent *dent;
2272 struct got_packidx *packidx = NULL;
2273 char *path_packidx;
2274 char *path_packfile;
2275 int packdir_fd;
2276 struct stat sb;
2278 *npackfiles = 0;
2279 *nobjects = 0;
2280 *total_packsize = 0;
2282 packdir_fd = openat(got_repo_get_fd(repo),
2283 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2284 if (packdir_fd == -1) {
2285 return got_error_from_errno_fmt("openat: %s/%s",
2286 got_repo_get_path_git_dir(repo),
2287 GOT_OBJECTS_PACK_DIR);
2290 packdir = fdopendir(packdir_fd);
2291 if (packdir == NULL) {
2292 err = got_error_from_errno("fdopendir");
2293 goto done;
2296 while ((dent = readdir(packdir)) != NULL) {
2297 if (!got_repo_is_packidx_filename(dent->d_name,
2298 strlen(dent->d_name)))
2299 continue;
2301 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2302 dent->d_name) == -1) {
2303 err = got_error_from_errno("asprintf");
2304 goto done;
2307 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2308 path_packidx, 0);
2309 free(path_packidx);
2310 if (err)
2311 goto done;
2313 if (fstat(packidx->fd, &sb) == -1)
2314 goto done;
2315 *total_packsize += sb.st_size;
2317 err = got_packidx_get_packfile_path(&path_packfile,
2318 packidx->path_packidx);
2319 if (err)
2320 goto done;
2322 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2323 0) == -1) {
2324 free(path_packfile);
2325 goto done;
2327 free(path_packfile);
2328 *total_packsize += sb.st_size;
2330 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2332 (*npackfiles)++;
2334 got_packidx_close(packidx);
2335 packidx = NULL;
2337 done:
2338 if (packidx)
2339 got_packidx_close(packidx);
2340 if (packdir && closedir(packdir) != 0 && err == NULL)
2341 err = got_error_from_errno("closedir");
2342 if (err) {
2343 *npackfiles = 0;
2344 *nobjects = 0;
2345 *total_packsize = 0;
2347 return err;
2350 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2351 got_packidx_bloom_filter_cmp);