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