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 /*
34 * While in gotd(8) chroot, a repository needs this many temporary files.
35 * This limit sets an upper bound on how many raw objects or blobs can
36 * be kept open in parallel.
37 */
38 #define GOT_REPO_NUM_TEMPFILES 32
40 struct got_packidx_bloom_filter {
41 RB_ENTRY(got_packidx_bloom_filter) entry;
42 char path[PATH_MAX]; /* on-disk path */
43 size_t path_len;
44 struct bloom *bloom;
45 };
47 RB_HEAD(got_packidx_bloom_filter_tree, got_packidx_bloom_filter);
49 static inline int
50 got_packidx_bloom_filter_cmp(const struct got_packidx_bloom_filter *f1,
51 const struct got_packidx_bloom_filter *f2)
52 {
53 return got_path_cmp(f1->path, f2->path, f1->path_len, f2->path_len);
54 }
56 struct got_repo_privsep_child {
57 int imsg_fd;
58 pid_t pid;
59 struct imsgbuf *ibuf;
60 };
62 struct got_repository {
63 char *path;
64 char *path_git_dir;
65 int gitdir_fd;
67 struct got_pathlist_head packidx_paths;
68 time_t pack_path_mtime;
70 /* The pack index cache speeds up search for packed objects. */
71 struct got_packidx *packidx_cache[GOT_PACK_CACHE_SIZE];
73 /*
74 * List of bloom filters for pack index files.
75 * Used to avoid opening a pack index in search of an
76 * object ID which is not contained in this pack index.
77 */
78 struct got_packidx_bloom_filter_tree packidx_bloom_filters;
80 /* Open file handles for pack files. */
81 struct got_pack packs[GOT_PACK_CACHE_SIZE];
83 /* Open file handles for storing temporary data in gotd(8) chroot. */
84 int tempfiles[GOT_REPO_NUM_TEMPFILES];
85 uint32_t tempfile_use_mask;
87 /*
88 * The cache size limit may be lower than GOT_PACK_CACHE_SIZE,
89 * depending on resource limits.
90 */
91 int pack_cache_size;
93 /*
94 * Index to cache entries which are pinned to avoid eviction.
95 * This may be used to keep one got-index-pack process alive
96 * across searches for arbitrary objects which may be stored
97 * in other pack files.
98 */
99 int pinned_pack;
100 pid_t pinned_pid;
101 int pinned_packidx;
103 /* Handles to child processes for reading loose objects. */
104 struct got_repo_privsep_child privsep_children[5];
105 #define GOT_REPO_PRIVSEP_CHILD_OBJECT 0
106 #define GOT_REPO_PRIVSEP_CHILD_COMMIT 1
107 #define GOT_REPO_PRIVSEP_CHILD_TREE 2
108 #define GOT_REPO_PRIVSEP_CHILD_BLOB 3
109 #define GOT_REPO_PRIVSEP_CHILD_TAG 4
111 /* Caches for open objects. */
112 struct got_object_cache objcache;
113 struct got_object_cache treecache;
114 struct got_object_cache commitcache;
115 struct got_object_cache tagcache;
116 struct got_object_cache rawcache;
118 /* Settings read from Git configuration files. */
119 int gitconfig_repository_format_version;
120 char *gitconfig_author_name;
121 char *gitconfig_author_email;
122 char *global_gitconfig_author_name;
123 char *global_gitconfig_author_email;
124 int ngitconfig_remotes;
125 struct got_remote_repo *gitconfig_remotes;
126 char *gitconfig_owner;
127 char **extensions;
128 int nextensions;
130 /* Settings read from got.conf. */
131 struct got_gotconfig *gotconfig;
132 };
134 const struct got_error*got_repo_cache_object(struct got_repository *,
135 struct got_object_id *, struct got_object *);
136 struct got_object *got_repo_get_cached_object(struct got_repository *,
137 struct got_object_id *);
138 const struct got_error*got_repo_cache_tree(struct got_repository *,
139 struct got_object_id *, struct got_tree_object *);
140 struct got_tree_object *got_repo_get_cached_tree(struct got_repository *,
141 struct got_object_id *);
142 const struct got_error*got_repo_cache_commit(struct got_repository *,
143 struct got_object_id *, struct got_commit_object *);
144 struct got_commit_object *got_repo_get_cached_commit(struct got_repository *,
145 struct got_object_id *);
146 const struct got_error*got_repo_cache_tag(struct got_repository *,
147 struct got_object_id *, struct got_tag_object *);
148 struct got_tag_object *got_repo_get_cached_tag(struct got_repository *,
149 struct got_object_id *);
150 const struct got_error*got_repo_cache_raw_object(struct got_repository *,
151 struct got_object_id *, struct got_raw_object *);
152 struct got_raw_object *got_repo_get_cached_raw_object(struct got_repository *,
153 struct got_object_id *);
154 int got_repo_is_packidx_filename(const char *, size_t);
155 int got_repo_check_packidx_bloom_filter(struct got_repository *,
156 const char *, struct got_object_id *);
157 const struct got_error *got_repo_search_packidx(struct got_packidx **, int *,
158 struct got_repository *, struct got_object_id *);
159 const struct got_error *got_repo_list_packidx(struct got_pathlist_head *,
160 struct got_repository *);
161 const struct got_error *got_repo_get_packidx(struct got_packidx **, const char *,
162 struct got_repository *);
163 const struct got_error *got_repo_cache_pack(struct got_pack **,
164 struct got_repository *, const char *, struct got_packidx *);
165 struct got_pack *got_repo_get_cached_pack(struct got_repository *,
166 const char *);
167 const struct got_error *got_repo_pin_pack(struct got_repository *,
168 struct got_packidx *, struct got_pack *);
169 struct got_pack *got_repo_get_pinned_pack(struct got_repository *);
170 void got_repo_unpin_pack(struct got_repository *);
172 const struct got_error *got_repo_read_gitconfig(int *, char **, char **,
173 struct got_remote_repo **, int *, char **, char ***, int *,
174 const char *);
176 const struct got_error *got_repo_temp_fds_get(int *, int *,
177 struct got_repository *);
178 void got_repo_temp_fds_put(int, struct got_repository *);