Blob


1 /*
2 * Copyright (c) 2018 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 #define GOT_GIT_DIR ".git"
19 /* Mandatory files and directories inside the git directory. */
20 #define GOT_OBJECTS_DIR "objects"
21 #define GOT_REFS_DIR "refs"
22 #define GOT_HEAD_FILE "HEAD"
23 #define GOT_GITCONFIG "config"
25 /* Other files and directories inside the git directory. */
26 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
27 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
28 #define GOT_OBJECTS_PACK_DIR "objects/pack"
29 #define GOT_PACKED_REFS_FILE "packed-refs"
31 #define GOT_PACK_CACHE_SIZE 32
33 struct got_packidx_bloom_filter {
34 RB_ENTRY(got_packidx_bloom_filter) entry;
35 char path[PATH_MAX]; /* on-disk path */
36 size_t path_len;
37 struct bloom *bloom;
38 };
40 RB_HEAD(got_packidx_bloom_filter_tree, got_packidx_bloom_filter);
42 static inline int
43 got_packidx_bloom_filter_cmp(const struct got_packidx_bloom_filter *f1,
44 const struct got_packidx_bloom_filter *f2)
45 {
46 return got_path_cmp(f1->path, f2->path, f1->path_len, f2->path_len);
47 }
49 struct got_repository {
50 char *path;
51 char *path_git_dir;
52 int gitdir_fd;
54 struct got_pathlist_head packidx_paths;
56 /* The pack index cache speeds up search for packed objects. */
57 struct got_packidx *packidx_cache[GOT_PACK_CACHE_SIZE];
59 /*
60 * List of bloom filters for pack index files.
61 * Used to avoid opening a pack index in search of an
62 * object ID which is not contained in this pack index.
63 */
64 struct got_packidx_bloom_filter_tree packidx_bloom_filters;
66 /* Open file handles for pack files. */
67 struct got_pack packs[GOT_PACK_CACHE_SIZE];
69 /*
70 * The cache size limit may be lower than GOT_PACK_CACHE_SIZE,
71 * depending on resource limits.
72 */
73 int pack_cache_size;
75 /*
76 * Index to cache entries which are pinned to avoid eviction.
77 * This may be used to keep one got-index-pack process alive
78 * across searches for arbitrary objects which may be stored
79 * in other pack files.
80 */
81 int pinned_pack;
82 pid_t pinned_pid;
83 int pinned_packidx;
85 /* Handles to child processes for reading loose objects. */
86 struct got_privsep_child privsep_children[5];
87 #define GOT_REPO_PRIVSEP_CHILD_OBJECT 0
88 #define GOT_REPO_PRIVSEP_CHILD_COMMIT 1
89 #define GOT_REPO_PRIVSEP_CHILD_TREE 2
90 #define GOT_REPO_PRIVSEP_CHILD_BLOB 3
91 #define GOT_REPO_PRIVSEP_CHILD_TAG 4
93 /* Caches for open objects. */
94 struct got_object_cache objcache;
95 struct got_object_cache treecache;
96 struct got_object_cache commitcache;
97 struct got_object_cache tagcache;
98 struct got_object_cache rawcache;
100 /* Settings read from Git configuration files. */
101 int gitconfig_repository_format_version;
102 char *gitconfig_author_name;
103 char *gitconfig_author_email;
104 char *global_gitconfig_author_name;
105 char *global_gitconfig_author_email;
106 int ngitconfig_remotes;
107 struct got_remote_repo *gitconfig_remotes;
108 char *gitconfig_owner;
109 char **extensions;
110 int nextensions;
112 /* Settings read from got.conf. */
113 struct got_gotconfig *gotconfig;
114 };
116 const struct got_error*got_repo_cache_object(struct got_repository *,
117 struct got_object_id *, struct got_object *);
118 struct got_object *got_repo_get_cached_object(struct got_repository *,
119 struct got_object_id *);
120 const struct got_error*got_repo_cache_tree(struct got_repository *,
121 struct got_object_id *, struct got_tree_object *);
122 struct got_tree_object *got_repo_get_cached_tree(struct got_repository *,
123 struct got_object_id *);
124 const struct got_error*got_repo_cache_commit(struct got_repository *,
125 struct got_object_id *, struct got_commit_object *);
126 struct got_commit_object *got_repo_get_cached_commit(struct got_repository *,
127 struct got_object_id *);
128 const struct got_error*got_repo_cache_tag(struct got_repository *,
129 struct got_object_id *, struct got_tag_object *);
130 struct got_tag_object *got_repo_get_cached_tag(struct got_repository *,
131 struct got_object_id *);
132 const struct got_error*got_repo_cache_raw_object(struct got_repository *,
133 struct got_object_id *, struct got_raw_object *);
134 struct got_raw_object *got_repo_get_cached_raw_object(struct got_repository *,
135 struct got_object_id *);
136 int got_repo_is_packidx_filename(const char *, size_t);
137 int got_repo_check_packidx_bloom_filter(struct got_repository *,
138 const char *, struct got_object_id *);
139 const struct got_error *got_repo_search_packidx(struct got_packidx **, int *,
140 struct got_repository *, struct got_object_id *);
141 const struct got_error *got_repo_list_packidx(struct got_pathlist_head *,
142 struct got_repository *);
143 const struct got_error *got_repo_get_packidx(struct got_packidx **, const char *,
144 struct got_repository *);
145 const struct got_error *got_repo_cache_pack(struct got_pack **,
146 struct got_repository *, const char *, struct got_packidx *);
147 struct got_pack *got_repo_get_cached_pack(struct got_repository *,
148 const char *);
149 const struct got_error *got_repo_pin_pack(struct got_repository *,
150 struct got_packidx *, struct got_pack *);
151 struct got_pack *got_repo_get_pinned_pack(struct got_repository *);
152 void got_repo_unpin_pack(struct got_repository *);