]> git.ipfire.org Git - thirdparty/git.git/blob - object-store.h
object-store: provide helpers for loose_objects_cache
[thirdparty/git.git] / object-store.h
1 #ifndef OBJECT_STORE_H
2 #define OBJECT_STORE_H
3
4 #include "cache.h"
5 #include "oidmap.h"
6 #include "list.h"
7 #include "sha1-array.h"
8 #include "strbuf.h"
9
10 struct object_directory {
11 struct object_directory *next;
12
13 /*
14 * Used to store the results of readdir(3) calls when we are OK
15 * sacrificing accuracy due to races for speed. That includes
16 * our search for unique abbreviated hashes. Don't use it for tasks
17 * requiring greater accuracy!
18 *
19 * Be sure to call odb_load_loose_cache() before using.
20 */
21 char loose_objects_subdir_seen[256];
22 struct oid_array loose_objects_cache;
23
24 /*
25 * Path to the alternative object store. If this is a relative path,
26 * it is relative to the current working directory.
27 */
28 char *path;
29 };
30
31 void prepare_alt_odb(struct repository *r);
32 char *compute_alternate_path(const char *path, struct strbuf *err);
33 typedef int alt_odb_fn(struct object_directory *, void *);
34 int foreach_alt_odb(alt_odb_fn, void*);
35
36 /*
37 * Add the directory to the on-disk alternates file; the new entry will also
38 * take effect in the current process.
39 */
40 void add_to_alternates_file(const char *dir);
41
42 /*
43 * Add the directory to the in-memory list of alternates (along with any
44 * recursive alternates it points to), but do not modify the on-disk alternates
45 * file.
46 */
47 void add_to_alternates_memory(const char *dir);
48
49 /*
50 * Populate an odb's loose object cache for one particular subdirectory (i.e.,
51 * the one that corresponds to the first byte of objects you're interested in,
52 * from 0 to 255 inclusive).
53 */
54 void odb_load_loose_cache(struct object_directory *odb, int subdir_nr);
55
56 struct packed_git {
57 struct packed_git *next;
58 struct list_head mru;
59 struct pack_window *windows;
60 off_t pack_size;
61 const void *index_data;
62 size_t index_size;
63 uint32_t num_objects;
64 uint32_t num_bad_objects;
65 unsigned char *bad_object_sha1;
66 int index_version;
67 time_t mtime;
68 int pack_fd;
69 int index; /* for builtin/pack-objects.c */
70 unsigned pack_local:1,
71 pack_keep:1,
72 pack_keep_in_core:1,
73 freshened:1,
74 do_not_close:1,
75 pack_promisor:1;
76 unsigned char sha1[20];
77 struct revindex_entry *revindex;
78 /* something like ".git/objects/pack/xxxxx.pack" */
79 char pack_name[FLEX_ARRAY]; /* more */
80 };
81
82 struct multi_pack_index;
83
84 struct raw_object_store {
85 /*
86 * Set of all object directories; the main directory is first (and
87 * cannot be NULL after initialization). Subsequent directories are
88 * alternates.
89 */
90 struct object_directory *odb;
91 struct object_directory **odb_tail;
92 int loaded_alternates;
93
94 /*
95 * A list of alternate object directories loaded from the environment;
96 * this should not generally need to be accessed directly, but will
97 * populate the "odb" list when prepare_alt_odb() is run.
98 */
99 char *alternate_db;
100
101 /*
102 * Objects that should be substituted by other objects
103 * (see git-replace(1)).
104 */
105 struct oidmap *replace_map;
106
107 struct commit_graph *commit_graph;
108 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
109
110 /*
111 * private data
112 *
113 * should only be accessed directly by packfile.c and midx.c
114 */
115 struct multi_pack_index *multi_pack_index;
116
117 /*
118 * private data
119 *
120 * should only be accessed directly by packfile.c
121 */
122
123 struct packed_git *packed_git;
124 /* A most-recently-used ordered version of the packed_git list. */
125 struct list_head packed_git_mru;
126
127 /*
128 * A linked list containing all packfiles, starting with those
129 * contained in the multi_pack_index.
130 */
131 struct packed_git *all_packs;
132
133 /*
134 * A fast, rough count of the number of objects in the repository.
135 * These two fields are not meant for direct access. Use
136 * approximate_object_count() instead.
137 */
138 unsigned long approximate_object_count;
139 unsigned approximate_object_count_valid : 1;
140
141 /*
142 * Whether packed_git has already been populated with this repository's
143 * packs.
144 */
145 unsigned packed_git_initialized : 1;
146 };
147
148 struct raw_object_store *raw_object_store_new(void);
149 void raw_object_store_clear(struct raw_object_store *o);
150
151 /*
152 * Put in `buf` the name of the file in the local object database that
153 * would be used to store a loose object with the specified sha1.
154 */
155 const char *loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
156
157 void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
158
159 extern void *read_object_file_extended(const struct object_id *oid,
160 enum object_type *type,
161 unsigned long *size, int lookup_replace);
162 static inline void *read_object_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
163 {
164 return read_object_file_extended(oid, type, size, 1);
165 }
166
167 /* Read and unpack an object file into memory, write memory to an object file */
168 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
169
170 extern int hash_object_file(const void *buf, unsigned long len,
171 const char *type, struct object_id *oid);
172
173 extern int write_object_file(const void *buf, unsigned long len,
174 const char *type, struct object_id *oid);
175
176 extern int hash_object_file_literally(const void *buf, unsigned long len,
177 const char *type, struct object_id *oid,
178 unsigned flags);
179
180 extern int pretend_object_file(void *, unsigned long, enum object_type,
181 struct object_id *oid);
182
183 extern int force_object_loose(const struct object_id *oid, time_t mtime);
184
185 /*
186 * Open the loose object at path, check its hash, and return the contents,
187 * type, and size. If the object is a blob, then "contents" may return NULL,
188 * to allow streaming of large blobs.
189 *
190 * Returns 0 on success, negative on error (details may be written to stderr).
191 */
192 int read_loose_object(const char *path,
193 const struct object_id *expected_oid,
194 enum object_type *type,
195 unsigned long *size,
196 void **contents);
197
198 /*
199 * Convenience for sha1_object_info_extended() with a NULL struct
200 * object_info. OBJECT_INFO_SKIP_CACHED is automatically set; pass
201 * nonzero flags to also set other flags.
202 */
203 extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags);
204 static inline int has_sha1_file(const unsigned char *sha1)
205 {
206 return has_sha1_file_with_flags(sha1, 0);
207 }
208
209 /* Same as the above, except for struct object_id. */
210 extern int has_object_file(const struct object_id *oid);
211 extern int has_object_file_with_flags(const struct object_id *oid, int flags);
212
213 /*
214 * Return true iff an alternate object database has a loose object
215 * with the specified name. This function does not respect replace
216 * references.
217 */
218 extern int has_loose_object_nonlocal(const struct object_id *);
219
220 extern void assert_oid_type(const struct object_id *oid, enum object_type expect);
221
222 struct object_info {
223 /* Request */
224 enum object_type *typep;
225 unsigned long *sizep;
226 off_t *disk_sizep;
227 unsigned char *delta_base_sha1;
228 struct strbuf *type_name;
229 void **contentp;
230
231 /* Response */
232 enum {
233 OI_CACHED,
234 OI_LOOSE,
235 OI_PACKED,
236 OI_DBCACHED
237 } whence;
238 union {
239 /*
240 * struct {
241 * ... Nothing to expose in this case
242 * } cached;
243 * struct {
244 * ... Nothing to expose in this case
245 * } loose;
246 */
247 struct {
248 struct packed_git *pack;
249 off_t offset;
250 unsigned int is_delta;
251 } packed;
252 } u;
253 };
254
255 /*
256 * Initializer for a "struct object_info" that wants no items. You may
257 * also memset() the memory to all-zeroes.
258 */
259 #define OBJECT_INFO_INIT {NULL}
260
261 /* Invoke lookup_replace_object() on the given hash */
262 #define OBJECT_INFO_LOOKUP_REPLACE 1
263 /* Allow reading from a loose object file of unknown/bogus type */
264 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
265 /* Do not check cached storage */
266 #define OBJECT_INFO_SKIP_CACHED 4
267 /* Do not retry packed storage after checking packed and loose storage */
268 #define OBJECT_INFO_QUICK 8
269 /* Do not check loose object */
270 #define OBJECT_INFO_IGNORE_LOOSE 16
271
272 int oid_object_info_extended(struct repository *r,
273 const struct object_id *,
274 struct object_info *, unsigned flags);
275
276 /*
277 * Iterate over the files in the loose-object parts of the object
278 * directory "path", triggering the following callbacks:
279 *
280 * - loose_object is called for each loose object we find.
281 *
282 * - loose_cruft is called for any files that do not appear to be
283 * loose objects. Note that we only look in the loose object
284 * directories "objects/[0-9a-f]{2}/", so we will not report
285 * "objects/foobar" as cruft.
286 *
287 * - loose_subdir is called for each top-level hashed subdirectory
288 * of the object directory (e.g., "$OBJDIR/f0"). It is called
289 * after the objects in the directory are processed.
290 *
291 * Any callback that is NULL will be ignored. Callbacks returning non-zero
292 * will end the iteration.
293 *
294 * In the "buf" variant, "path" is a strbuf which will also be used as a
295 * scratch buffer, but restored to its original contents before
296 * the function returns.
297 */
298 typedef int each_loose_object_fn(const struct object_id *oid,
299 const char *path,
300 void *data);
301 typedef int each_loose_cruft_fn(const char *basename,
302 const char *path,
303 void *data);
304 typedef int each_loose_subdir_fn(unsigned int nr,
305 const char *path,
306 void *data);
307 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
308 struct strbuf *path,
309 each_loose_object_fn obj_cb,
310 each_loose_cruft_fn cruft_cb,
311 each_loose_subdir_fn subdir_cb,
312 void *data);
313 int for_each_loose_file_in_objdir(const char *path,
314 each_loose_object_fn obj_cb,
315 each_loose_cruft_fn cruft_cb,
316 each_loose_subdir_fn subdir_cb,
317 void *data);
318 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
319 each_loose_object_fn obj_cb,
320 each_loose_cruft_fn cruft_cb,
321 each_loose_subdir_fn subdir_cb,
322 void *data);
323
324 /* Flags for for_each_*_object() below. */
325 enum for_each_object_flags {
326 /* Iterate only over local objects, not alternates. */
327 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
328
329 /* Only iterate over packs obtained from the promisor remote. */
330 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
331
332 /*
333 * Visit objects within a pack in packfile order rather than .idx order
334 */
335 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
336 };
337
338 /*
339 * Iterate over all accessible loose objects without respect to
340 * reachability. By default, this includes both local and alternate objects.
341 * The order in which objects are visited is unspecified.
342 *
343 * Any flags specific to packs are ignored.
344 */
345 int for_each_loose_object(each_loose_object_fn, void *,
346 enum for_each_object_flags flags);
347
348 /*
349 * Iterate over all accessible packed objects without respect to reachability.
350 * By default, this includes both local and alternate packs.
351 *
352 * Note that some objects may appear twice if they are found in multiple packs.
353 * Each pack is visited in an unspecified order. By default, objects within a
354 * pack are visited in pack-idx order (i.e., sorted by oid).
355 */
356 typedef int each_packed_object_fn(const struct object_id *oid,
357 struct packed_git *pack,
358 uint32_t pos,
359 void *data);
360 int for_each_object_in_pack(struct packed_git *p,
361 each_packed_object_fn, void *data,
362 enum for_each_object_flags flags);
363 int for_each_packed_object(each_packed_object_fn, void *,
364 enum for_each_object_flags flags);
365
366 #endif /* OBJECT_STORE_H */