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