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