sparse-index: prevent repo root from becoming sparse
[alt-git.git] / object-store.h
blob6f89482df030cb28c91a7d2620903473e661370f
1 #ifndef OBJECT_STORE_H
2 #define OBJECT_STORE_H
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 #include "oidtree.h"
13 #include "oidset.h"
15 struct object_directory {
16 struct object_directory *next;
19 * Used to store the results of readdir(3) calls when we are OK
20 * sacrificing accuracy due to races for speed. That includes
21 * object existence with OBJECT_INFO_QUICK, as well as
22 * our search for unique abbreviated hashes. Don't use it for tasks
23 * requiring greater accuracy!
25 * Be sure to call odb_load_loose_cache() before using.
27 uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
28 struct oidtree *loose_objects_cache;
31 * This is a temporary object store created by the tmp_objdir
32 * facility. Disable ref updates since the objects in the store
33 * might be discarded on rollback.
35 int disable_ref_updates;
38 * This object store is ephemeral, so there is no need to fsync.
40 int will_destroy;
43 * Path to the alternative object store. If this is a relative path,
44 * it is relative to the current working directory.
46 char *path;
49 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
50 struct object_directory *, 1, fspathhash, fspatheq)
52 void prepare_alt_odb(struct repository *r);
53 char *compute_alternate_path(const char *path, struct strbuf *err);
54 struct object_directory *find_odb(struct repository *r, const char *obj_dir);
55 typedef int alt_odb_fn(struct object_directory *, void *);
56 int foreach_alt_odb(alt_odb_fn, void*);
57 typedef void alternate_ref_fn(const struct object_id *oid, void *);
58 void for_each_alternate_ref(alternate_ref_fn, void *);
61 * Add the directory to the on-disk alternates file; the new entry will also
62 * take effect in the current process.
64 void add_to_alternates_file(const char *dir);
67 * Add the directory to the in-memory list of alternates (along with any
68 * recursive alternates it points to), but do not modify the on-disk alternates
69 * file.
71 void add_to_alternates_memory(const char *dir);
74 * Replace the current writable object directory with the specified temporary
75 * object directory; returns the former primary object directory.
77 struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy);
80 * Restore a previous ODB replaced by set_temporary_main_odb.
82 void restore_primary_odb(struct object_directory *restore_odb, const char *old_path);
85 * Populate and return the loose object cache array corresponding to the
86 * given object ID.
88 struct oidtree *odb_loose_cache(struct object_directory *odb,
89 const struct object_id *oid);
91 /* Empty the loose object cache for the specified object directory. */
92 void odb_clear_loose_cache(struct object_directory *odb);
94 /* Clear and free the specified object directory */
95 void free_object_directory(struct object_directory *odb);
97 struct packed_git {
98 struct hashmap_entry packmap_ent;
99 struct packed_git *next;
100 struct list_head mru;
101 struct pack_window *windows;
102 off_t pack_size;
103 const void *index_data;
104 size_t index_size;
105 uint32_t num_objects;
106 uint32_t crc_offset;
107 struct oidset bad_objects;
108 int index_version;
109 time_t mtime;
110 int pack_fd;
111 int index; /* for builtin/pack-objects.c */
112 unsigned pack_local:1,
113 pack_keep:1,
114 pack_keep_in_core:1,
115 freshened:1,
116 do_not_close:1,
117 pack_promisor:1,
118 multi_pack_index:1;
119 unsigned char hash[GIT_MAX_RAWSZ];
120 struct revindex_entry *revindex;
121 const uint32_t *revindex_data;
122 const uint32_t *revindex_map;
123 size_t revindex_size;
124 /* something like ".git/objects/pack/xxxxx.pack" */
125 char pack_name[FLEX_ARRAY]; /* more */
128 struct multi_pack_index;
130 static inline int pack_map_entry_cmp(const void *unused_cmp_data,
131 const struct hashmap_entry *entry,
132 const struct hashmap_entry *entry2,
133 const void *keydata)
135 const char *key = keydata;
136 const struct packed_git *pg1, *pg2;
138 pg1 = container_of(entry, const struct packed_git, packmap_ent);
139 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
141 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
144 struct raw_object_store {
146 * Set of all object directories; the main directory is first (and
147 * cannot be NULL after initialization). Subsequent directories are
148 * alternates.
150 struct object_directory *odb;
151 struct object_directory **odb_tail;
152 kh_odb_path_map_t *odb_by_path;
154 int loaded_alternates;
157 * A list of alternate object directories loaded from the environment;
158 * this should not generally need to be accessed directly, but will
159 * populate the "odb" list when prepare_alt_odb() is run.
161 char *alternate_db;
164 * Objects that should be substituted by other objects
165 * (see git-replace(1)).
167 struct oidmap *replace_map;
168 unsigned replace_map_initialized : 1;
169 pthread_mutex_t replace_mutex; /* protect object replace functions */
171 struct commit_graph *commit_graph;
172 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
175 * private data
177 * should only be accessed directly by packfile.c and midx.c
179 struct multi_pack_index *multi_pack_index;
182 * private data
184 * should only be accessed directly by packfile.c
187 struct packed_git *packed_git;
188 /* A most-recently-used ordered version of the packed_git list. */
189 struct list_head packed_git_mru;
191 struct {
192 struct packed_git **packs;
193 unsigned flags;
194 } kept_pack_cache;
197 * A map of packfiles to packed_git structs for tracking which
198 * packs have been loaded already.
200 struct hashmap pack_map;
203 * A fast, rough count of the number of objects in the repository.
204 * These two fields are not meant for direct access. Use
205 * approximate_object_count() instead.
207 unsigned long approximate_object_count;
208 unsigned approximate_object_count_valid : 1;
211 * Whether packed_git has already been populated with this repository's
212 * packs.
214 unsigned packed_git_initialized : 1;
217 struct raw_object_store *raw_object_store_new(void);
218 void raw_object_store_clear(struct raw_object_store *o);
221 * Put in `buf` the name of the file in the local object database that
222 * would be used to store a loose object with the specified oid.
224 const char *loose_object_path(struct repository *r, struct strbuf *buf,
225 const struct object_id *oid);
227 void *map_loose_object(struct repository *r, const struct object_id *oid,
228 unsigned long *size);
230 void *read_object_file_extended(struct repository *r,
231 const struct object_id *oid,
232 enum object_type *type,
233 unsigned long *size, int lookup_replace);
234 static inline void *repo_read_object_file(struct repository *r,
235 const struct object_id *oid,
236 enum object_type *type,
237 unsigned long *size)
239 return read_object_file_extended(r, oid, type, size, 1);
241 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
242 #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
243 #endif
245 /* Read and unpack an object file into memory, write memory to an object file */
246 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
248 int hash_object_file(const struct git_hash_algo *algo, const void *buf,
249 unsigned long len, const char *type,
250 struct object_id *oid);
252 int write_object_file_flags(const void *buf, unsigned long len,
253 const char *type, struct object_id *oid,
254 unsigned flags);
255 static inline int write_object_file(const void *buf, unsigned long len,
256 const char *type, struct object_id *oid)
258 return write_object_file_flags(buf, len, type, oid, 0);
261 int hash_object_file_literally(const void *buf, unsigned long len,
262 const char *type, struct object_id *oid,
263 unsigned flags);
266 * Add an object file to the in-memory object store, without writing it
267 * to disk.
269 * Callers are responsible for calling write_object_file to record the
270 * object in persistent storage before writing any other new objects
271 * that reference it.
273 int pretend_object_file(void *, unsigned long, enum object_type,
274 struct object_id *oid);
276 int force_object_loose(const struct object_id *oid, time_t mtime);
279 * Open the loose object at path, check its hash, and return the contents,
280 * use the "oi" argument to assert things about the object, or e.g. populate its
281 * type, and size. If the object is a blob, then "contents" may return NULL,
282 * to allow streaming of large blobs.
284 * Returns 0 on success, negative on error (details may be written to stderr).
286 int read_loose_object(const char *path,
287 const struct object_id *expected_oid,
288 struct object_id *real_oid,
289 void **contents,
290 struct object_info *oi);
292 /* Retry packed storage after checking packed and loose storage */
293 #define HAS_OBJECT_RECHECK_PACKED 1
296 * Returns 1 if the object exists. This function will not lazily fetch objects
297 * in a partial clone.
299 int has_object(struct repository *r, const struct object_id *oid,
300 unsigned flags);
303 * These macros and functions are deprecated. If checking existence for an
304 * object that is likely to be missing and/or whose absence is relatively
305 * inconsequential (or is consequential but the caller is prepared to handle
306 * it), use has_object(), which has better defaults (no lazy fetch in a partial
307 * clone and no rechecking of packed storage). In the unlikely event that a
308 * caller needs to assert existence of an object that it fully expects to
309 * exist, and wants to trigger a lazy fetch in a partial clone, use
310 * oid_object_info_extended() with a NULL struct object_info.
312 * These functions can be removed once all callers have migrated to
313 * has_object() and/or oid_object_info_extended().
315 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
316 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
317 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
318 #endif
319 int repo_has_object_file(struct repository *r, const struct object_id *oid);
320 int repo_has_object_file_with_flags(struct repository *r,
321 const struct object_id *oid, int flags);
322 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
323 #define has_object_file(oid) repo_has_object_file(the_repository, oid)
324 #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
325 #endif
328 * Return true iff an alternate object database has a loose object
329 * with the specified name. This function does not respect replace
330 * references.
332 int has_loose_object_nonlocal(const struct object_id *);
334 void assert_oid_type(const struct object_id *oid, enum object_type expect);
337 * Enabling the object read lock allows multiple threads to safely call the
338 * following functions in parallel: repo_read_object_file(), read_object_file(),
339 * read_object_file_extended(), read_object_with_reference(), read_object(),
340 * oid_object_info() and oid_object_info_extended().
342 * obj_read_lock() and obj_read_unlock() may also be used to protect other
343 * section which cannot execute in parallel with object reading. Since the used
344 * lock is a recursive mutex, these sections can even contain calls to object
345 * reading functions. However, beware that in these cases zlib inflation won't
346 * be performed in parallel, losing performance.
348 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
349 * any of its callees end up calling it, this recursive call won't benefit from
350 * parallel inflation.
352 void enable_obj_read_lock(void);
353 void disable_obj_read_lock(void);
355 extern int obj_read_use_lock;
356 extern pthread_mutex_t obj_read_mutex;
358 static inline void obj_read_lock(void)
360 if(obj_read_use_lock)
361 pthread_mutex_lock(&obj_read_mutex);
364 static inline void obj_read_unlock(void)
366 if(obj_read_use_lock)
367 pthread_mutex_unlock(&obj_read_mutex);
370 struct object_info {
371 /* Request */
372 enum object_type *typep;
373 unsigned long *sizep;
374 off_t *disk_sizep;
375 struct object_id *delta_base_oid;
376 struct strbuf *type_name;
377 void **contentp;
379 /* Response */
380 enum {
381 OI_CACHED,
382 OI_LOOSE,
383 OI_PACKED,
384 OI_DBCACHED
385 } whence;
386 union {
388 * struct {
389 * ... Nothing to expose in this case
390 * } cached;
391 * struct {
392 * ... Nothing to expose in this case
393 * } loose;
395 struct {
396 struct packed_git *pack;
397 off_t offset;
398 unsigned int is_delta;
399 } packed;
400 } u;
404 * Initializer for a "struct object_info" that wants no items. You may
405 * also memset() the memory to all-zeroes.
407 #define OBJECT_INFO_INIT { 0 }
409 /* Invoke lookup_replace_object() on the given hash */
410 #define OBJECT_INFO_LOOKUP_REPLACE 1
411 /* Allow reading from a loose object file of unknown/bogus type */
412 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
413 /* Do not retry packed storage after checking packed and loose storage */
414 #define OBJECT_INFO_QUICK 8
415 /* Do not check loose object */
416 #define OBJECT_INFO_IGNORE_LOOSE 16
418 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
419 * nonzero).
421 #define OBJECT_INFO_SKIP_FETCH_OBJECT 32
423 * This is meant for bulk prefetching of missing blobs in a partial
424 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
426 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
428 int oid_object_info_extended(struct repository *r,
429 const struct object_id *,
430 struct object_info *, unsigned flags);
433 * Iterate over the files in the loose-object parts of the object
434 * directory "path", triggering the following callbacks:
436 * - loose_object is called for each loose object we find.
438 * - loose_cruft is called for any files that do not appear to be
439 * loose objects. Note that we only look in the loose object
440 * directories "objects/[0-9a-f]{2}/", so we will not report
441 * "objects/foobar" as cruft.
443 * - loose_subdir is called for each top-level hashed subdirectory
444 * of the object directory (e.g., "$OBJDIR/f0"). It is called
445 * after the objects in the directory are processed.
447 * Any callback that is NULL will be ignored. Callbacks returning non-zero
448 * will end the iteration.
450 * In the "buf" variant, "path" is a strbuf which will also be used as a
451 * scratch buffer, but restored to its original contents before
452 * the function returns.
454 typedef int each_loose_object_fn(const struct object_id *oid,
455 const char *path,
456 void *data);
457 typedef int each_loose_cruft_fn(const char *basename,
458 const char *path,
459 void *data);
460 typedef int each_loose_subdir_fn(unsigned int nr,
461 const char *path,
462 void *data);
463 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
464 struct strbuf *path,
465 each_loose_object_fn obj_cb,
466 each_loose_cruft_fn cruft_cb,
467 each_loose_subdir_fn subdir_cb,
468 void *data);
469 int for_each_loose_file_in_objdir(const char *path,
470 each_loose_object_fn obj_cb,
471 each_loose_cruft_fn cruft_cb,
472 each_loose_subdir_fn subdir_cb,
473 void *data);
474 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
475 each_loose_object_fn obj_cb,
476 each_loose_cruft_fn cruft_cb,
477 each_loose_subdir_fn subdir_cb,
478 void *data);
480 /* Flags for for_each_*_object() below. */
481 enum for_each_object_flags {
482 /* Iterate only over local objects, not alternates. */
483 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
485 /* Only iterate over packs obtained from the promisor remote. */
486 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
489 * Visit objects within a pack in packfile order rather than .idx order
491 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
493 /* Only iterate over packs that are not marked as kept in-core. */
494 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
496 /* Only iterate over packs that do not have .keep files. */
497 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
501 * Iterate over all accessible loose objects without respect to
502 * reachability. By default, this includes both local and alternate objects.
503 * The order in which objects are visited is unspecified.
505 * Any flags specific to packs are ignored.
507 int for_each_loose_object(each_loose_object_fn, void *,
508 enum for_each_object_flags flags);
511 * Iterate over all accessible packed objects without respect to reachability.
512 * By default, this includes both local and alternate packs.
514 * Note that some objects may appear twice if they are found in multiple packs.
515 * Each pack is visited in an unspecified order. By default, objects within a
516 * pack are visited in pack-idx order (i.e., sorted by oid).
518 typedef int each_packed_object_fn(const struct object_id *oid,
519 struct packed_git *pack,
520 uint32_t pos,
521 void *data);
522 int for_each_object_in_pack(struct packed_git *p,
523 each_packed_object_fn, void *data,
524 enum for_each_object_flags flags);
525 int for_each_packed_object(each_packed_object_fn, void *,
526 enum for_each_object_flags flags);
528 #endif /* OBJECT_STORE_H */