blob: c4fc9dd74ea81c20830ec1b79788a69faaccaf9a [file] [log] [blame]
Stefan Beller90c62152018-03-23 18:20:55 +01001#ifndef OBJECT_STORE_H
2#define OBJECT_STORE_H
3
Elijah Newrenef3ca952018-08-15 10:54:05 -07004#include "cache.h"
Stefan Bellerd88f9fd2018-04-11 17:21:05 -07005#include "oidmap.h"
Jonathan Tan14727b72018-07-11 15:42:38 -07006#include "list.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -04007#include "oid-array.h"
Jonathan Tan14727b72018-07-11 15:42:38 -07008#include "strbuf.h"
Matheus Tavares31877c92020-01-15 23:39:53 -03009#include "thread-utils.h"
Stefan Bellerd88f9fd2018-04-11 17:21:05 -070010
Jeff King263db402018-11-12 09:48:47 -050011struct object_directory {
12 struct object_directory *next;
Stefan Beller0d4a1322018-03-23 18:20:56 +010013
Stefan Beller0d4a1322018-03-23 18:20:56 +010014 /*
Jeff King3a2e0822018-11-12 09:50:56 -050015 * Used to store the results of readdir(3) calls when we are OK
16 * sacrificing accuracy due to races for speed. That includes
Jeff King61c77112018-11-12 09:54:42 -050017 * object existence with OBJECT_INFO_QUICK, as well as
Jeff King3a2e0822018-11-12 09:50:56 -050018 * 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.
Stefan Beller0d4a1322018-03-23 18:20:56 +010022 */
23 char loose_objects_subdir_seen[256];
René Scharfe4cea1ce2019-01-06 17:45:52 +010024 struct oid_array loose_objects_cache[256];
Stefan Beller0d4a1322018-03-23 18:20:56 +010025
Stefan Beller77f012e2018-03-23 18:21:08 +010026 /*
27 * Path to the alternative object store. If this is a relative path,
28 * it is relative to the current working directory.
29 */
Jeff Kingf0eaf632018-11-12 09:50:39 -050030 char *path;
Stefan Beller031dc922018-03-23 18:20:57 +010031};
Jeff Kingf0eaf632018-11-12 09:50:39 -050032
Stefan Beller13068bf2018-03-23 18:21:09 +010033void prepare_alt_odb(struct repository *r);
Stefan Beller0d4a1322018-03-23 18:20:56 +010034char *compute_alternate_path(const char *path, struct strbuf *err);
Jeff King263db402018-11-12 09:48:47 -050035typedef int alt_odb_fn(struct object_directory *, void *);
Stefan Beller0d4a1322018-03-23 18:20:56 +010036int foreach_alt_odb(alt_odb_fn, void*);
Jeff King709dfa62019-07-01 09:17:40 -040037typedef void alternate_ref_fn(const struct object_id *oid, void *);
38void for_each_alternate_ref(alternate_ref_fn, void *);
Stefan Beller0d4a1322018-03-23 18:20:56 +010039
40/*
Stefan Beller0d4a1322018-03-23 18:20:56 +010041 * 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
Jeff King3a2e0822018-11-12 09:50:56 -050053/*
René Scharfe0000d652019-01-06 17:45:30 +010054 * 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
René Scharfed4e19e52019-01-06 17:45:39 +010060/* Empty the loose object cache for the specified object directory. */
61void odb_clear_loose_cache(struct object_directory *odb);
62
Stefan Bellera80d72d2018-03-23 18:20:59 +010063struct packed_git {
Colin Stolleyec485402019-11-27 16:24:53 -060064 struct hashmap_entry packmap_ent;
Stefan Bellera80d72d2018-03-23 18:20:59 +010065 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;
brian m. carlson629dffc2020-05-25 19:59:10 +000073 uint32_t crc_offset;
Stefan Bellera80d72d2018-03-23 18:20:59 +010074 unsigned char *bad_object_sha1;
75 int index_version;
76 time_t mtime;
77 int pack_fd;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +020078 int index; /* for builtin/pack-objects.c */
Stefan Bellera80d72d2018-03-23 18:20:59 +010079 unsigned pack_local:1,
80 pack_keep:1,
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +020081 pack_keep_in_core:1,
Stefan Bellera80d72d2018-03-23 18:20:59 +010082 freshened:1,
83 do_not_close:1,
Derrick Stoleeaf96fe32019-04-29 09:18:56 -070084 pack_promisor:1,
85 multi_pack_index:1;
brian m. carlson538b1522019-02-19 00:05:03 +000086 unsigned char hash[GIT_MAX_RAWSZ];
Stefan Bellera80d72d2018-03-23 18:20:59 +010087 struct revindex_entry *revindex;
88 /* something like ".git/objects/pack/xxxxx.pack" */
89 char pack_name[FLEX_ARRAY]; /* more */
90};
91
Derrick Stolee4d805602018-07-12 15:39:23 -040092struct multi_pack_index;
93
Colin Stolleyec485402019-11-27 16:24:53 -060094static inline int pack_map_entry_cmp(const void *unused_cmp_data,
95 const struct hashmap_entry *entry,
96 const struct hashmap_entry *entry2,
97 const void *keydata)
98{
99 const char *key = keydata;
100 const struct packed_git *pg1, *pg2;
101
102 pg1 = container_of(entry, const struct packed_git, packmap_ent);
103 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
104
105 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
106}
107
Stefan Beller90c62152018-03-23 18:20:55 +0100108struct raw_object_store {
109 /*
Jeff Kingf0eaf632018-11-12 09:50:39 -0500110 * Set of all object directories; the main directory is first (and
111 * cannot be NULL after initialization). Subsequent directories are
112 * alternates.
Stefan Beller90c62152018-03-23 18:20:55 +0100113 */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500114 struct object_directory *odb;
115 struct object_directory **odb_tail;
116 int loaded_alternates;
Stefan Beller90c62152018-03-23 18:20:55 +0100117
Jeff Kingf0eaf632018-11-12 09:50:39 -0500118 /*
119 * A list of alternate object directories loaded from the environment;
120 * this should not generally need to be accessed directly, but will
121 * populate the "odb" list when prepare_alt_odb() is run.
122 */
Stefan Beller90c62152018-03-23 18:20:55 +0100123 char *alternate_db;
Stefan Beller031dc922018-03-23 18:20:57 +0100124
Stefan Bellera80d72d2018-03-23 18:20:59 +0100125 /*
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700126 * Objects that should be substituted by other objects
127 * (see git-replace(1)).
128 */
Stefan Bellerc1274492018-04-11 17:21:07 -0700129 struct oidmap *replace_map;
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300130 unsigned replace_map_initialized : 1;
131 pthread_mutex_t replace_mutex; /* protect object replace functions */
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700132
Jonathan Tan85277502018-07-11 15:42:41 -0700133 struct commit_graph *commit_graph;
134 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
135
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700136 /*
Stefan Bellera80d72d2018-03-23 18:20:59 +0100137 * private data
138 *
Derrick Stoleec4d25222018-07-12 15:39:33 -0400139 * should only be accessed directly by packfile.c and midx.c
140 */
141 struct multi_pack_index *multi_pack_index;
142
143 /*
144 * private data
145 *
Stefan Bellera80d72d2018-03-23 18:20:59 +0100146 * should only be accessed directly by packfile.c
147 */
148
149 struct packed_git *packed_git;
150 /* A most-recently-used ordered version of the packed_git list. */
151 struct list_head packed_git_mru;
Stefan Beller5508f692018-03-23 18:21:01 +0100152
153 /*
Colin Stolleyec485402019-11-27 16:24:53 -0600154 * A map of packfiles to packed_git structs for tracking which
155 * packs have been loaded already.
156 */
157 struct hashmap pack_map;
158
159 /*
Stefan Beller9a005802018-03-23 18:21:02 +0100160 * A fast, rough count of the number of objects in the repository.
161 * These two fields are not meant for direct access. Use
162 * approximate_object_count() instead.
163 */
164 unsigned long approximate_object_count;
165 unsigned approximate_object_count_valid : 1;
166
167 /*
Stefan Beller5508f692018-03-23 18:21:01 +0100168 * Whether packed_git has already been populated with this repository's
169 * packs.
170 */
171 unsigned packed_git_initialized : 1;
Stefan Beller90c62152018-03-23 18:20:55 +0100172};
173
174struct raw_object_store *raw_object_store_new(void);
175void raw_object_store_clear(struct raw_object_store *o);
176
Stefan Bellercf78ae42018-03-23 18:21:10 +0100177/*
178 * Put in `buf` the name of the file in the local object database that
Jeff King514c5fd2019-01-07 03:35:42 -0500179 * would be used to store a loose object with the specified oid.
Stefan Bellercf78ae42018-03-23 18:21:10 +0100180 */
Jeff King514c5fd2019-01-07 03:35:42 -0500181const char *loose_object_path(struct repository *r, struct strbuf *buf,
182 const struct object_id *oid);
Stefan Bellercf78ae42018-03-23 18:21:10 +0100183
Jeff King514c5fd2019-01-07 03:35:42 -0500184void *map_loose_object(struct repository *r, const struct object_id *oid,
185 unsigned long *size);
Stefan Bellere35454f2018-03-23 18:21:14 +0100186
Denton Liu55454422019-04-29 04:28:14 -0400187void *read_object_file_extended(struct repository *r,
Denton Liuad6dad02019-04-29 04:28:23 -0400188 const struct object_id *oid,
189 enum object_type *type,
190 unsigned long *size, int lookup_replace);
Stefan Bellerafd69dc2018-11-13 16:12:47 -0800191static inline void *repo_read_object_file(struct repository *r,
192 const struct object_id *oid,
193 enum object_type *type,
194 unsigned long *size)
Stefan Bellercbd53a22018-05-15 16:42:15 -0700195{
Stefan Bellerafd69dc2018-11-13 16:12:47 -0800196 return read_object_file_extended(r, oid, type, size, 1);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700197}
Stefan Bellerafd69dc2018-11-13 16:12:47 -0800198#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
199#define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
200#endif
Stefan Bellercbd53a22018-05-15 16:42:15 -0700201
202/* Read and unpack an object file into memory, write memory to an object file */
203int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
204
Matheus Tavares2dcde202020-01-30 17:32:22 -0300205int hash_object_file(const struct git_hash_algo *algo, const void *buf,
206 unsigned long len, const char *type,
207 struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700208
Denton Liu55454422019-04-29 04:28:14 -0400209int write_object_file(const void *buf, unsigned long len,
Denton Liuad6dad02019-04-29 04:28:23 -0400210 const char *type, struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700211
Denton Liu55454422019-04-29 04:28:14 -0400212int hash_object_file_literally(const void *buf, unsigned long len,
Denton Liuad6dad02019-04-29 04:28:23 -0400213 const char *type, struct object_id *oid,
214 unsigned flags);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700215
Jonathan Nieder60440d72020-01-03 16:13:31 -0800216/*
217 * Add an object file to the in-memory object store, without writing it
218 * to disk.
219 *
220 * Callers are responsible for calling write_object_file to record the
221 * object in persistent storage before writing any other new objects
222 * that reference it.
223 */
Denton Liu55454422019-04-29 04:28:14 -0400224int pretend_object_file(void *, unsigned long, enum object_type,
Denton Liuad6dad02019-04-29 04:28:23 -0400225 struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700226
Denton Liu55454422019-04-29 04:28:14 -0400227int force_object_loose(const struct object_id *oid, time_t mtime);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700228
229/*
230 * Open the loose object at path, check its hash, and return the contents,
231 * type, and size. If the object is a blob, then "contents" may return NULL,
232 * to allow streaming of large blobs.
233 *
234 * Returns 0 on success, negative on error (details may be written to stderr).
235 */
236int read_loose_object(const char *path,
237 const struct object_id *expected_oid,
238 enum object_type *type,
239 unsigned long *size,
240 void **contents);
241
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -0700242/* Retry packed storage after checking packed and loose storage */
243#define HAS_OBJECT_RECHECK_PACKED 1
244
245/*
246 * Returns 1 if the object exists. This function will not lazily fetch objects
247 * in a partial clone.
248 */
249int has_object(struct repository *r, const struct object_id *oid,
250 unsigned flags);
251
252/*
253 * These macros and functions are deprecated. If checking existence for an
254 * object that is likely to be missing and/or whose absence is relatively
255 * inconsequential (or is consequential but the caller is prepared to handle
256 * it), use has_object(), which has better defaults (no lazy fetch in a partial
257 * clone and no rechecking of packed storage). In the unlikely event that a
258 * caller needs to assert existence of an object that it fully expects to
259 * exist, and wants to trigger a lazy fetch in a partial clone, use
260 * oid_object_info_extended() with a NULL struct object_info.
261 *
262 * These functions can be removed once all callers have migrated to
263 * has_object() and/or oid_object_info_extended().
264 */
Stefan Beller9b45f492018-11-13 16:12:48 -0800265#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
266#define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
267#define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
268#endif
Stefan Beller9b45f492018-11-13 16:12:48 -0800269int repo_has_object_file(struct repository *r, const struct object_id *oid);
270int repo_has_object_file_with_flags(struct repository *r,
271 const struct object_id *oid, int flags);
272#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
273#define has_object_file(oid) repo_has_object_file(the_repository, oid)
274#define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
275#endif
Stefan Bellercbd53a22018-05-15 16:42:15 -0700276
277/*
278 * Return true iff an alternate object database has a loose object
279 * with the specified name. This function does not respect replace
280 * references.
281 */
Denton Liu55454422019-04-29 04:28:14 -0400282int has_loose_object_nonlocal(const struct object_id *);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700283
Denton Liu55454422019-04-29 04:28:14 -0400284void assert_oid_type(const struct object_id *oid, enum object_type expect);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700285
Matheus Tavares31877c92020-01-15 23:39:53 -0300286/*
287 * Enabling the object read lock allows multiple threads to safely call the
288 * following functions in parallel: repo_read_object_file(), read_object_file(),
289 * read_object_file_extended(), read_object_with_reference(), read_object(),
290 * oid_object_info() and oid_object_info_extended().
291 *
292 * obj_read_lock() and obj_read_unlock() may also be used to protect other
293 * section which cannot execute in parallel with object reading. Since the used
294 * lock is a recursive mutex, these sections can even contain calls to object
295 * reading functions. However, beware that in these cases zlib inflation won't
296 * be performed in parallel, losing performance.
297 *
298 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
299 * any of its callees end up calling it, this recursive call won't benefit from
300 * parallel inflation.
301 */
302void enable_obj_read_lock(void);
303void disable_obj_read_lock(void);
304
305extern int obj_read_use_lock;
306extern pthread_mutex_t obj_read_mutex;
307
308static inline void obj_read_lock(void)
309{
310 if(obj_read_use_lock)
311 pthread_mutex_lock(&obj_read_mutex);
312}
313
314static inline void obj_read_unlock(void)
315{
316 if(obj_read_use_lock)
317 pthread_mutex_unlock(&obj_read_mutex);
318}
319
Stefan Bellercbd53a22018-05-15 16:42:15 -0700320struct object_info {
321 /* Request */
322 enum object_type *typep;
323 unsigned long *sizep;
324 off_t *disk_sizep;
Jeff Kingb99b6bc2020-02-23 23:36:56 -0500325 struct object_id *delta_base_oid;
Stefan Bellercbd53a22018-05-15 16:42:15 -0700326 struct strbuf *type_name;
327 void **contentp;
328
329 /* Response */
330 enum {
331 OI_CACHED,
332 OI_LOOSE,
333 OI_PACKED,
334 OI_DBCACHED
335 } whence;
336 union {
337 /*
338 * struct {
339 * ... Nothing to expose in this case
340 * } cached;
341 * struct {
342 * ... Nothing to expose in this case
343 * } loose;
344 */
345 struct {
346 struct packed_git *pack;
347 off_t offset;
348 unsigned int is_delta;
349 } packed;
350 } u;
351};
352
353/*
354 * Initializer for a "struct object_info" that wants no items. You may
355 * also memset() the memory to all-zeroes.
356 */
357#define OBJECT_INFO_INIT {NULL}
358
359/* Invoke lookup_replace_object() on the given hash */
360#define OBJECT_INFO_LOOKUP_REPLACE 1
361/* Allow reading from a loose object file of unknown/bogus type */
362#define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
Stefan Bellercbd53a22018-05-15 16:42:15 -0700363/* Do not retry packed storage after checking packed and loose storage */
364#define OBJECT_INFO_QUICK 8
365/* Do not check loose object */
366#define OBJECT_INFO_IGNORE_LOOSE 16
Jonathan Tan0f4a4fb2019-03-29 14:39:27 -0700367/*
368 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
Derrick Stolee31f52562019-05-28 08:19:07 -0700369 * nonzero).
Jonathan Tan0f4a4fb2019-03-29 14:39:27 -0700370 */
Derrick Stolee31f52562019-05-28 08:19:07 -0700371#define OBJECT_INFO_SKIP_FETCH_OBJECT 32
372/*
373 * This is meant for bulk prefetching of missing blobs in a partial
374 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
375 */
376#define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
Stefan Bellercbd53a22018-05-15 16:42:15 -0700377
378int oid_object_info_extended(struct repository *r,
379 const struct object_id *,
380 struct object_info *, unsigned flags);
381
Jeff King0889aae2018-08-14 14:21:18 -0400382/*
383 * Iterate over the files in the loose-object parts of the object
384 * directory "path", triggering the following callbacks:
385 *
386 * - loose_object is called for each loose object we find.
387 *
388 * - loose_cruft is called for any files that do not appear to be
389 * loose objects. Note that we only look in the loose object
390 * directories "objects/[0-9a-f]{2}/", so we will not report
391 * "objects/foobar" as cruft.
392 *
393 * - loose_subdir is called for each top-level hashed subdirectory
394 * of the object directory (e.g., "$OBJDIR/f0"). It is called
395 * after the objects in the directory are processed.
396 *
397 * Any callback that is NULL will be ignored. Callbacks returning non-zero
398 * will end the iteration.
399 *
400 * In the "buf" variant, "path" is a strbuf which will also be used as a
401 * scratch buffer, but restored to its original contents before
402 * the function returns.
403 */
404typedef int each_loose_object_fn(const struct object_id *oid,
405 const char *path,
406 void *data);
407typedef int each_loose_cruft_fn(const char *basename,
408 const char *path,
409 void *data);
410typedef int each_loose_subdir_fn(unsigned int nr,
411 const char *path,
412 void *data);
413int for_each_file_in_obj_subdir(unsigned int subdir_nr,
414 struct strbuf *path,
415 each_loose_object_fn obj_cb,
416 each_loose_cruft_fn cruft_cb,
417 each_loose_subdir_fn subdir_cb,
418 void *data);
419int for_each_loose_file_in_objdir(const char *path,
420 each_loose_object_fn obj_cb,
421 each_loose_cruft_fn cruft_cb,
422 each_loose_subdir_fn subdir_cb,
423 void *data);
424int for_each_loose_file_in_objdir_buf(struct strbuf *path,
425 each_loose_object_fn obj_cb,
426 each_loose_cruft_fn cruft_cb,
427 each_loose_subdir_fn subdir_cb,
428 void *data);
429
430/* Flags for for_each_*_object() below. */
431enum for_each_object_flags {
432 /* Iterate only over local objects, not alternates. */
433 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
434
435 /* Only iterate over packs obtained from the promisor remote. */
436 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
437
438 /*
439 * Visit objects within a pack in packfile order rather than .idx order
440 */
441 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
442};
443
444/*
445 * Iterate over all accessible loose objects without respect to
446 * reachability. By default, this includes both local and alternate objects.
447 * The order in which objects are visited is unspecified.
448 *
449 * Any flags specific to packs are ignored.
450 */
451int for_each_loose_object(each_loose_object_fn, void *,
452 enum for_each_object_flags flags);
453
454/*
455 * Iterate over all accessible packed objects without respect to reachability.
456 * By default, this includes both local and alternate packs.
457 *
458 * Note that some objects may appear twice if they are found in multiple packs.
459 * Each pack is visited in an unspecified order. By default, objects within a
460 * pack are visited in pack-idx order (i.e., sorted by oid).
461 */
462typedef int each_packed_object_fn(const struct object_id *oid,
463 struct packed_git *pack,
464 uint32_t pos,
465 void *data);
466int for_each_object_in_pack(struct packed_git *p,
467 each_packed_object_fn, void *data,
468 enum for_each_object_flags flags);
469int for_each_packed_object(each_packed_object_fn, void *,
470 enum for_each_object_flags flags);
471
Stefan Beller90c62152018-03-23 18:20:55 +0100472#endif /* OBJECT_STORE_H */