]> git.ipfire.org Git - thirdparty/git.git/blob - object-store.h
make object_directory.loose_objects_subdir_seen a bitmap
[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 "oid-array.h"
8 #include "strbuf.h"
9 #include "thread-utils.h"
10 #include "khash.h"
11 #include "dir.h"
12
13 struct object_directory {
14 struct object_directory *next;
15
16 /*
17 * Used to store the results of readdir(3) calls when we are OK
18 * sacrificing accuracy due to races for speed. That includes
19 * object existence with OBJECT_INFO_QUICK, as well as
20 * our search for unique abbreviated hashes. Don't use it for tasks
21 * requiring greater accuracy!
22 *
23 * Be sure to call odb_load_loose_cache() before using.
24 */
25 uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
26 struct oid_array loose_objects_cache[256];
27
28 /*
29 * Path to the alternative object store. If this is a relative path,
30 * it is relative to the current working directory.
31 */
32 char *path;
33 };
34
35 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
36 struct object_directory *, 1, fspathhash, fspatheq);
37
38 void prepare_alt_odb(struct repository *r);
39 char *compute_alternate_path(const char *path, struct strbuf *err);
40 typedef int alt_odb_fn(struct object_directory *, void *);
41 int foreach_alt_odb(alt_odb_fn, void*);
42 typedef void alternate_ref_fn(const struct object_id *oid, void *);
43 void for_each_alternate_ref(alternate_ref_fn, void *);
44
45 /*
46 * Add the directory to the on-disk alternates file; the new entry will also
47 * take effect in the current process.
48 */
49 void add_to_alternates_file(const char *dir);
50
51 /*
52 * Add the directory to the in-memory list of alternates (along with any
53 * recursive alternates it points to), but do not modify the on-disk alternates
54 * file.
55 */
56 void add_to_alternates_memory(const char *dir);
57
58 /*
59 * Populate and return the loose object cache array corresponding to the
60 * given object ID.
61 */
62 struct oid_array *odb_loose_cache(struct object_directory *odb,
63 const struct object_id *oid);
64
65 /* Empty the loose object cache for the specified object directory. */
66 void odb_clear_loose_cache(struct object_directory *odb);
67
68 struct packed_git {
69 struct hashmap_entry packmap_ent;
70 struct packed_git *next;
71 struct list_head mru;
72 struct pack_window *windows;
73 off_t pack_size;
74 const void *index_data;
75 size_t index_size;
76 uint32_t num_objects;
77 uint32_t num_bad_objects;
78 uint32_t crc_offset;
79 unsigned char *bad_object_sha1;
80 int index_version;
81 time_t mtime;
82 int pack_fd;
83 int index; /* for builtin/pack-objects.c */
84 unsigned pack_local:1,
85 pack_keep:1,
86 pack_keep_in_core:1,
87 freshened:1,
88 do_not_close:1,
89 pack_promisor:1,
90 multi_pack_index:1;
91 unsigned char hash[GIT_MAX_RAWSZ];
92 struct revindex_entry *revindex;
93 const uint32_t *revindex_data;
94 const uint32_t *revindex_map;
95 size_t revindex_size;
96 /* something like ".git/objects/pack/xxxxx.pack" */
97 char pack_name[FLEX_ARRAY]; /* more */
98 };
99
100 struct multi_pack_index;
101
102 static inline int pack_map_entry_cmp(const void *unused_cmp_data,
103 const struct hashmap_entry *entry,
104 const struct hashmap_entry *entry2,
105 const void *keydata)
106 {
107 const char *key = keydata;
108 const struct packed_git *pg1, *pg2;
109
110 pg1 = container_of(entry, const struct packed_git, packmap_ent);
111 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
112
113 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
114 }
115
116 struct raw_object_store {
117 /*
118 * Set of all object directories; the main directory is first (and
119 * cannot be NULL after initialization). Subsequent directories are
120 * alternates.
121 */
122 struct object_directory *odb;
123 struct object_directory **odb_tail;
124 kh_odb_path_map_t *odb_by_path;
125
126 int loaded_alternates;
127
128 /*
129 * A list of alternate object directories loaded from the environment;
130 * this should not generally need to be accessed directly, but will
131 * populate the "odb" list when prepare_alt_odb() is run.
132 */
133 char *alternate_db;
134
135 /*
136 * Objects that should be substituted by other objects
137 * (see git-replace(1)).
138 */
139 struct oidmap *replace_map;
140 unsigned replace_map_initialized : 1;
141 pthread_mutex_t replace_mutex; /* protect object replace functions */
142
143 struct commit_graph *commit_graph;
144 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
145
146 /*
147 * private data
148 *
149 * should only be accessed directly by packfile.c and midx.c
150 */
151 struct multi_pack_index *multi_pack_index;
152
153 /*
154 * private data
155 *
156 * should only be accessed directly by packfile.c
157 */
158
159 struct packed_git *packed_git;
160 /* A most-recently-used ordered version of the packed_git list. */
161 struct list_head packed_git_mru;
162
163 struct {
164 struct packed_git **packs;
165 unsigned flags;
166 } kept_pack_cache;
167
168 /*
169 * A map of packfiles to packed_git structs for tracking which
170 * packs have been loaded already.
171 */
172 struct hashmap pack_map;
173
174 /*
175 * A fast, rough count of the number of objects in the repository.
176 * These two fields are not meant for direct access. Use
177 * approximate_object_count() instead.
178 */
179 unsigned long approximate_object_count;
180 unsigned approximate_object_count_valid : 1;
181
182 /*
183 * Whether packed_git has already been populated with this repository's
184 * packs.
185 */
186 unsigned packed_git_initialized : 1;
187 };
188
189 struct raw_object_store *raw_object_store_new(void);
190 void raw_object_store_clear(struct raw_object_store *o);
191
192 /*
193 * Put in `buf` the name of the file in the local object database that
194 * would be used to store a loose object with the specified oid.
195 */
196 const char *loose_object_path(struct repository *r, struct strbuf *buf,
197 const struct object_id *oid);
198
199 void *map_loose_object(struct repository *r, const struct object_id *oid,
200 unsigned long *size);
201
202 void *read_object_file_extended(struct repository *r,
203 const struct object_id *oid,
204 enum object_type *type,
205 unsigned long *size, int lookup_replace);
206 static inline void *repo_read_object_file(struct repository *r,
207 const struct object_id *oid,
208 enum object_type *type,
209 unsigned long *size)
210 {
211 return read_object_file_extended(r, oid, type, size, 1);
212 }
213 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
214 #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
215 #endif
216
217 /* Read and unpack an object file into memory, write memory to an object file */
218 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
219
220 int hash_object_file(const struct git_hash_algo *algo, const void *buf,
221 unsigned long len, const char *type,
222 struct object_id *oid);
223
224 int write_object_file(const void *buf, unsigned long len,
225 const char *type, struct object_id *oid);
226
227 int hash_object_file_literally(const void *buf, unsigned long len,
228 const char *type, struct object_id *oid,
229 unsigned flags);
230
231 /*
232 * Add an object file to the in-memory object store, without writing it
233 * to disk.
234 *
235 * Callers are responsible for calling write_object_file to record the
236 * object in persistent storage before writing any other new objects
237 * that reference it.
238 */
239 int pretend_object_file(void *, unsigned long, enum object_type,
240 struct object_id *oid);
241
242 int force_object_loose(const struct object_id *oid, time_t mtime);
243
244 /*
245 * Open the loose object at path, check its hash, and return the contents,
246 * type, and size. If the object is a blob, then "contents" may return NULL,
247 * to allow streaming of large blobs.
248 *
249 * Returns 0 on success, negative on error (details may be written to stderr).
250 */
251 int read_loose_object(const char *path,
252 const struct object_id *expected_oid,
253 enum object_type *type,
254 unsigned long *size,
255 void **contents);
256
257 /* Retry packed storage after checking packed and loose storage */
258 #define HAS_OBJECT_RECHECK_PACKED 1
259
260 /*
261 * Returns 1 if the object exists. This function will not lazily fetch objects
262 * in a partial clone.
263 */
264 int has_object(struct repository *r, const struct object_id *oid,
265 unsigned flags);
266
267 /*
268 * These macros and functions are deprecated. If checking existence for an
269 * object that is likely to be missing and/or whose absence is relatively
270 * inconsequential (or is consequential but the caller is prepared to handle
271 * it), use has_object(), which has better defaults (no lazy fetch in a partial
272 * clone and no rechecking of packed storage). In the unlikely event that a
273 * caller needs to assert existence of an object that it fully expects to
274 * exist, and wants to trigger a lazy fetch in a partial clone, use
275 * oid_object_info_extended() with a NULL struct object_info.
276 *
277 * These functions can be removed once all callers have migrated to
278 * has_object() and/or oid_object_info_extended().
279 */
280 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
281 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
282 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
283 #endif
284 int repo_has_object_file(struct repository *r, const struct object_id *oid);
285 int repo_has_object_file_with_flags(struct repository *r,
286 const struct object_id *oid, int flags);
287 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
288 #define has_object_file(oid) repo_has_object_file(the_repository, oid)
289 #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
290 #endif
291
292 /*
293 * Return true iff an alternate object database has a loose object
294 * with the specified name. This function does not respect replace
295 * references.
296 */
297 int has_loose_object_nonlocal(const struct object_id *);
298
299 void assert_oid_type(const struct object_id *oid, enum object_type expect);
300
301 /*
302 * Enabling the object read lock allows multiple threads to safely call the
303 * following functions in parallel: repo_read_object_file(), read_object_file(),
304 * read_object_file_extended(), read_object_with_reference(), read_object(),
305 * oid_object_info() and oid_object_info_extended().
306 *
307 * obj_read_lock() and obj_read_unlock() may also be used to protect other
308 * section which cannot execute in parallel with object reading. Since the used
309 * lock is a recursive mutex, these sections can even contain calls to object
310 * reading functions. However, beware that in these cases zlib inflation won't
311 * be performed in parallel, losing performance.
312 *
313 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
314 * any of its callees end up calling it, this recursive call won't benefit from
315 * parallel inflation.
316 */
317 void enable_obj_read_lock(void);
318 void disable_obj_read_lock(void);
319
320 extern int obj_read_use_lock;
321 extern pthread_mutex_t obj_read_mutex;
322
323 static inline void obj_read_lock(void)
324 {
325 if(obj_read_use_lock)
326 pthread_mutex_lock(&obj_read_mutex);
327 }
328
329 static inline void obj_read_unlock(void)
330 {
331 if(obj_read_use_lock)
332 pthread_mutex_unlock(&obj_read_mutex);
333 }
334
335 struct object_info {
336 /* Request */
337 enum object_type *typep;
338 unsigned long *sizep;
339 off_t *disk_sizep;
340 struct object_id *delta_base_oid;
341 struct strbuf *type_name;
342 void **contentp;
343
344 /* Response */
345 enum {
346 OI_CACHED,
347 OI_LOOSE,
348 OI_PACKED,
349 OI_DBCACHED
350 } whence;
351 union {
352 /*
353 * struct {
354 * ... Nothing to expose in this case
355 * } cached;
356 * struct {
357 * ... Nothing to expose in this case
358 * } loose;
359 */
360 struct {
361 struct packed_git *pack;
362 off_t offset;
363 unsigned int is_delta;
364 } packed;
365 } u;
366 };
367
368 /*
369 * Initializer for a "struct object_info" that wants no items. You may
370 * also memset() the memory to all-zeroes.
371 */
372 #define OBJECT_INFO_INIT {NULL}
373
374 /* Invoke lookup_replace_object() on the given hash */
375 #define OBJECT_INFO_LOOKUP_REPLACE 1
376 /* Allow reading from a loose object file of unknown/bogus type */
377 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
378 /* Do not retry packed storage after checking packed and loose storage */
379 #define OBJECT_INFO_QUICK 8
380 /* Do not check loose object */
381 #define OBJECT_INFO_IGNORE_LOOSE 16
382 /*
383 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
384 * nonzero).
385 */
386 #define OBJECT_INFO_SKIP_FETCH_OBJECT 32
387 /*
388 * This is meant for bulk prefetching of missing blobs in a partial
389 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
390 */
391 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
392
393 int oid_object_info_extended(struct repository *r,
394 const struct object_id *,
395 struct object_info *, unsigned flags);
396
397 /*
398 * Iterate over the files in the loose-object parts of the object
399 * directory "path", triggering the following callbacks:
400 *
401 * - loose_object is called for each loose object we find.
402 *
403 * - loose_cruft is called for any files that do not appear to be
404 * loose objects. Note that we only look in the loose object
405 * directories "objects/[0-9a-f]{2}/", so we will not report
406 * "objects/foobar" as cruft.
407 *
408 * - loose_subdir is called for each top-level hashed subdirectory
409 * of the object directory (e.g., "$OBJDIR/f0"). It is called
410 * after the objects in the directory are processed.
411 *
412 * Any callback that is NULL will be ignored. Callbacks returning non-zero
413 * will end the iteration.
414 *
415 * In the "buf" variant, "path" is a strbuf which will also be used as a
416 * scratch buffer, but restored to its original contents before
417 * the function returns.
418 */
419 typedef int each_loose_object_fn(const struct object_id *oid,
420 const char *path,
421 void *data);
422 typedef int each_loose_cruft_fn(const char *basename,
423 const char *path,
424 void *data);
425 typedef int each_loose_subdir_fn(unsigned int nr,
426 const char *path,
427 void *data);
428 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
429 struct strbuf *path,
430 each_loose_object_fn obj_cb,
431 each_loose_cruft_fn cruft_cb,
432 each_loose_subdir_fn subdir_cb,
433 void *data);
434 int for_each_loose_file_in_objdir(const char *path,
435 each_loose_object_fn obj_cb,
436 each_loose_cruft_fn cruft_cb,
437 each_loose_subdir_fn subdir_cb,
438 void *data);
439 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
440 each_loose_object_fn obj_cb,
441 each_loose_cruft_fn cruft_cb,
442 each_loose_subdir_fn subdir_cb,
443 void *data);
444
445 /* Flags for for_each_*_object() below. */
446 enum for_each_object_flags {
447 /* Iterate only over local objects, not alternates. */
448 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
449
450 /* Only iterate over packs obtained from the promisor remote. */
451 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
452
453 /*
454 * Visit objects within a pack in packfile order rather than .idx order
455 */
456 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
457 };
458
459 /*
460 * Iterate over all accessible loose objects without respect to
461 * reachability. By default, this includes both local and alternate objects.
462 * The order in which objects are visited is unspecified.
463 *
464 * Any flags specific to packs are ignored.
465 */
466 int for_each_loose_object(each_loose_object_fn, void *,
467 enum for_each_object_flags flags);
468
469 /*
470 * Iterate over all accessible packed objects without respect to reachability.
471 * By default, this includes both local and alternate packs.
472 *
473 * Note that some objects may appear twice if they are found in multiple packs.
474 * Each pack is visited in an unspecified order. By default, objects within a
475 * pack are visited in pack-idx order (i.e., sorted by oid).
476 */
477 typedef int each_packed_object_fn(const struct object_id *oid,
478 struct packed_git *pack,
479 uint32_t pos,
480 void *data);
481 int for_each_object_in_pack(struct packed_git *p,
482 each_packed_object_fn, void *data,
483 enum for_each_object_flags flags);
484 int for_each_packed_object(each_packed_object_fn, void *,
485 enum for_each_object_flags flags);
486
487 #endif /* OBJECT_STORE_H */