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