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