The eighth batch
[alt-git.git] / object-store-ll.h
blobcd3bd5bd99f78cd01d914ac86fa94f0fb36853a3
1 #ifndef OBJECT_STORE_LL_H
2 #define OBJECT_STORE_LL_H
4 #include "hashmap.h"
5 #include "object.h"
6 #include "list.h"
7 #include "thread-utils.h"
8 #include "oidset.h"
10 struct oidmap;
11 struct oidtree;
12 struct strbuf;
13 struct repository;
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;
30 /* Map between object IDs for loose objects. */
31 struct loose_object_map *loose_map;
34 * This is a temporary object store created by the tmp_objdir
35 * facility. Disable ref updates since the objects in the store
36 * might be discarded on rollback.
38 int disable_ref_updates;
41 * This object store is ephemeral, so there is no need to fsync.
43 int will_destroy;
46 * Path to the alternative object store. If this is a relative path,
47 * it is relative to the current working directory.
49 char *path;
52 struct input_stream {
53 const void *(*read)(struct input_stream *, unsigned long *len);
54 void *data;
55 int is_finished;
58 void prepare_alt_odb(struct repository *r);
59 int has_alt_odb(struct repository *r);
60 char *compute_alternate_path(const char *path, struct strbuf *err);
61 struct object_directory *find_odb(struct repository *r, const char *obj_dir);
62 typedef int alt_odb_fn(struct object_directory *, void *);
63 int foreach_alt_odb(alt_odb_fn, void*);
64 typedef void alternate_ref_fn(const struct object_id *oid, void *);
65 void for_each_alternate_ref(alternate_ref_fn, void *);
68 * Add the directory to the on-disk alternates file; the new entry will also
69 * take effect in the current process.
71 void add_to_alternates_file(const char *dir);
74 * Add the directory to the in-memory list of alternates (along with any
75 * recursive alternates it points to), but do not modify the on-disk alternates
76 * file.
78 void add_to_alternates_memory(const char *dir);
81 * Replace the current writable object directory with the specified temporary
82 * object directory; returns the former primary object directory.
84 struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy);
87 * Restore a previous ODB replaced by set_temporary_main_odb.
89 void restore_primary_odb(struct object_directory *restore_odb, const char *old_path);
92 * Populate and return the loose object cache array corresponding to the
93 * given object ID.
95 struct oidtree *odb_loose_cache(struct object_directory *odb,
96 const struct object_id *oid);
98 /* Empty the loose object cache for the specified object directory. */
99 void odb_clear_loose_cache(struct object_directory *odb);
101 /* Clear and free the specified object directory */
102 void free_object_directory(struct object_directory *odb);
104 struct packed_git {
105 struct hashmap_entry packmap_ent;
106 struct packed_git *next;
107 struct list_head mru;
108 struct pack_window *windows;
109 off_t pack_size;
110 const void *index_data;
111 size_t index_size;
112 uint32_t num_objects;
113 size_t crc_offset;
114 struct oidset bad_objects;
115 int index_version;
116 time_t mtime;
117 int pack_fd;
118 int index; /* for builtin/pack-objects.c */
119 unsigned pack_local:1,
120 pack_keep:1,
121 pack_keep_in_core:1,
122 freshened:1,
123 do_not_close:1,
124 pack_promisor:1,
125 multi_pack_index:1,
126 is_cruft:1;
127 unsigned char hash[GIT_MAX_RAWSZ];
128 struct revindex_entry *revindex;
129 const uint32_t *revindex_data;
130 const uint32_t *revindex_map;
131 size_t revindex_size;
133 * mtimes_map points at the beginning of the memory mapped region of
134 * this pack's corresponding .mtimes file, and mtimes_size is the size
135 * of that .mtimes file
137 const uint32_t *mtimes_map;
138 size_t mtimes_size;
140 /* repo denotes the repository this packfile belongs to */
141 struct repository *repo;
143 /* something like ".git/objects/pack/xxxxx.pack" */
144 char pack_name[FLEX_ARRAY]; /* more */
147 struct multi_pack_index;
149 static inline int pack_map_entry_cmp(const void *cmp_data UNUSED,
150 const struct hashmap_entry *entry,
151 const struct hashmap_entry *entry2,
152 const void *keydata)
154 const char *key = keydata;
155 const struct packed_git *pg1, *pg2;
157 pg1 = container_of(entry, const struct packed_git, packmap_ent);
158 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
160 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
163 struct raw_object_store {
165 * Set of all object directories; the main directory is first (and
166 * cannot be NULL after initialization). Subsequent directories are
167 * alternates.
169 struct object_directory *odb;
170 struct object_directory **odb_tail;
171 struct kh_odb_path_map *odb_by_path;
173 int loaded_alternates;
176 * A list of alternate object directories loaded from the environment;
177 * this should not generally need to be accessed directly, but will
178 * populate the "odb" list when prepare_alt_odb() is run.
180 char *alternate_db;
183 * Objects that should be substituted by other objects
184 * (see git-replace(1)).
186 struct oidmap *replace_map;
187 unsigned replace_map_initialized : 1;
188 pthread_mutex_t replace_mutex; /* protect object replace functions */
190 struct commit_graph *commit_graph;
191 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
194 * private data
196 * should only be accessed directly by packfile.c and midx.c
198 struct multi_pack_index *multi_pack_index;
201 * private data
203 * should only be accessed directly by packfile.c
206 struct packed_git *packed_git;
207 /* A most-recently-used ordered version of the packed_git list. */
208 struct list_head packed_git_mru;
210 struct {
211 struct packed_git **packs;
212 unsigned flags;
213 } kept_pack_cache;
216 * A map of packfiles to packed_git structs for tracking which
217 * packs have been loaded already.
219 struct hashmap pack_map;
222 * A fast, rough count of the number of objects in the repository.
223 * These two fields are not meant for direct access. Use
224 * repo_approximate_object_count() instead.
226 unsigned long approximate_object_count;
227 unsigned approximate_object_count_valid : 1;
230 * Whether packed_git has already been populated with this repository's
231 * packs.
233 unsigned packed_git_initialized : 1;
236 struct raw_object_store *raw_object_store_new(void);
237 void raw_object_store_clear(struct raw_object_store *o);
240 * Create a temporary file rooted in the object database directory, or
241 * die on failure. The filename is taken from "pattern", which should have the
242 * usual "XXXXXX" trailer, and the resulting filename is written into the
243 * "template" buffer. Returns the open descriptor.
245 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern);
248 * Create a pack .keep file named "name" (which should generally be the output
249 * of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
250 * error.
252 int odb_pack_keep(const char *name);
255 * Put in `buf` the name of the file in the local object database that
256 * would be used to store a loose object with the specified oid.
258 const char *loose_object_path(struct repository *r, struct strbuf *buf,
259 const struct object_id *oid);
261 void *map_loose_object(struct repository *r, const struct object_id *oid,
262 unsigned long *size);
264 void *repo_read_object_file(struct repository *r,
265 const struct object_id *oid,
266 enum object_type *type,
267 unsigned long *size);
269 /* Read and unpack an object file into memory, write memory to an object file */
270 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
272 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
273 unsigned long len, enum object_type type,
274 struct object_id *oid);
276 int write_object_file_flags(const void *buf, unsigned long len,
277 enum object_type type, struct object_id *oid,
278 struct object_id *comapt_oid_in, unsigned flags);
279 static inline int write_object_file(const void *buf, unsigned long len,
280 enum object_type type, struct object_id *oid)
282 return write_object_file_flags(buf, len, type, oid, NULL, 0);
285 int write_object_file_literally(const void *buf, unsigned long len,
286 const char *type, struct object_id *oid,
287 unsigned flags);
288 int stream_loose_object(struct input_stream *in_stream, size_t len,
289 struct object_id *oid);
292 * Add an object file to the in-memory object store, without writing it
293 * to disk.
295 * Callers are responsible for calling write_object_file to record the
296 * object in persistent storage before writing any other new objects
297 * that reference it.
299 int pretend_object_file(void *, unsigned long, enum object_type,
300 struct object_id *oid);
302 int force_object_loose(const struct object_id *oid, time_t mtime);
304 struct object_info {
305 /* Request */
306 enum object_type *typep;
307 unsigned long *sizep;
308 off_t *disk_sizep;
309 struct object_id *delta_base_oid;
310 struct strbuf *type_name;
311 void **contentp;
313 /* Response */
314 enum {
315 OI_CACHED,
316 OI_LOOSE,
317 OI_PACKED,
318 OI_DBCACHED
319 } whence;
320 union {
322 * struct {
323 * ... Nothing to expose in this case
324 * } cached;
325 * struct {
326 * ... Nothing to expose in this case
327 * } loose;
329 struct {
330 struct packed_git *pack;
331 off_t offset;
332 unsigned int is_delta;
333 } packed;
334 } u;
338 * Initializer for a "struct object_info" that wants no items. You may
339 * also memset() the memory to all-zeroes.
341 #define OBJECT_INFO_INIT { 0 }
343 /* Invoke lookup_replace_object() on the given hash */
344 #define OBJECT_INFO_LOOKUP_REPLACE 1
345 /* Allow reading from a loose object file of unknown/bogus type */
346 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
347 /* Do not retry packed storage after checking packed and loose storage */
348 #define OBJECT_INFO_QUICK 8
350 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
351 * nonzero).
353 #define OBJECT_INFO_SKIP_FETCH_OBJECT 16
355 * This is meant for bulk prefetching of missing blobs in a partial
356 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
358 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
360 /* Die if object corruption (not just an object being missing) was detected. */
361 #define OBJECT_INFO_DIE_IF_CORRUPT 32
363 int oid_object_info_extended(struct repository *r,
364 const struct object_id *,
365 struct object_info *, unsigned flags);
368 * Open the loose object at path, check its hash, and return the contents,
369 * use the "oi" argument to assert things about the object, or e.g. populate its
370 * type, and size. If the object is a blob, then "contents" may return NULL,
371 * to allow streaming of large blobs.
373 * Returns 0 on success, negative on error (details may be written to stderr).
375 int read_loose_object(const char *path,
376 const struct object_id *expected_oid,
377 struct object_id *real_oid,
378 void **contents,
379 struct object_info *oi);
381 /* Retry packed storage after checking packed and loose storage */
382 #define HAS_OBJECT_RECHECK_PACKED 1
385 * Returns 1 if the object exists. This function will not lazily fetch objects
386 * in a partial clone.
388 int has_object(struct repository *r, const struct object_id *oid,
389 unsigned flags);
392 * These macros and functions are deprecated. If checking existence for an
393 * object that is likely to be missing and/or whose absence is relatively
394 * inconsequential (or is consequential but the caller is prepared to handle
395 * it), use has_object(), which has better defaults (no lazy fetch in a partial
396 * clone and no rechecking of packed storage). In the unlikely event that a
397 * caller needs to assert existence of an object that it fully expects to
398 * exist, and wants to trigger a lazy fetch in a partial clone, use
399 * oid_object_info_extended() with a NULL struct object_info.
401 * These functions can be removed once all callers have migrated to
402 * has_object() and/or oid_object_info_extended().
404 int repo_has_object_file(struct repository *r, const struct object_id *oid);
405 int repo_has_object_file_with_flags(struct repository *r,
406 const struct object_id *oid, int flags);
409 * Return true iff an alternate object database has a loose object
410 * with the specified name. This function does not respect replace
411 * references.
413 int has_loose_object_nonlocal(const struct object_id *);
415 int has_loose_object(const struct object_id *);
418 * format_object_header() is a thin wrapper around s xsnprintf() that
419 * writes the initial "<type> <obj-len>" part of the loose object
420 * header. It returns the size that snprintf() returns + 1.
422 int format_object_header(char *str, size_t size, enum object_type type,
423 size_t objsize);
425 void assert_oid_type(const struct object_id *oid, enum object_type expect);
428 * Enabling the object read lock allows multiple threads to safely call the
429 * following functions in parallel: repo_read_object_file(),
430 * read_object_with_reference(), oid_object_info() and oid_object_info_extended().
432 * obj_read_lock() and obj_read_unlock() may also be used to protect other
433 * section which cannot execute in parallel with object reading. Since the used
434 * lock is a recursive mutex, these sections can even contain calls to object
435 * reading functions. However, beware that in these cases zlib inflation won't
436 * be performed in parallel, losing performance.
438 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
439 * any of its callees end up calling it, this recursive call won't benefit from
440 * parallel inflation.
442 void enable_obj_read_lock(void);
443 void disable_obj_read_lock(void);
445 extern int obj_read_use_lock;
446 extern pthread_mutex_t obj_read_mutex;
448 static inline void obj_read_lock(void)
450 if(obj_read_use_lock)
451 pthread_mutex_lock(&obj_read_mutex);
454 static inline void obj_read_unlock(void)
456 if(obj_read_use_lock)
457 pthread_mutex_unlock(&obj_read_mutex);
461 * Iterate over the files in the loose-object parts of the object
462 * directory "path", triggering the following callbacks:
464 * - loose_object is called for each loose object we find.
466 * - loose_cruft is called for any files that do not appear to be
467 * loose objects. Note that we only look in the loose object
468 * directories "objects/[0-9a-f]{2}/", so we will not report
469 * "objects/foobar" as cruft.
471 * - loose_subdir is called for each top-level hashed subdirectory
472 * of the object directory (e.g., "$OBJDIR/f0"). It is called
473 * after the objects in the directory are processed.
475 * Any callback that is NULL will be ignored. Callbacks returning non-zero
476 * will end the iteration.
478 * In the "buf" variant, "path" is a strbuf which will also be used as a
479 * scratch buffer, but restored to its original contents before
480 * the function returns.
482 typedef int each_loose_object_fn(const struct object_id *oid,
483 const char *path,
484 void *data);
485 typedef int each_loose_cruft_fn(const char *basename,
486 const char *path,
487 void *data);
488 typedef int each_loose_subdir_fn(unsigned int nr,
489 const char *path,
490 void *data);
491 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
492 struct strbuf *path,
493 each_loose_object_fn obj_cb,
494 each_loose_cruft_fn cruft_cb,
495 each_loose_subdir_fn subdir_cb,
496 void *data);
497 int for_each_loose_file_in_objdir(const char *path,
498 each_loose_object_fn obj_cb,
499 each_loose_cruft_fn cruft_cb,
500 each_loose_subdir_fn subdir_cb,
501 void *data);
502 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
503 each_loose_object_fn obj_cb,
504 each_loose_cruft_fn cruft_cb,
505 each_loose_subdir_fn subdir_cb,
506 void *data);
508 /* Flags for for_each_*_object() below. */
509 enum for_each_object_flags {
510 /* Iterate only over local objects, not alternates. */
511 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
513 /* Only iterate over packs obtained from the promisor remote. */
514 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
517 * Visit objects within a pack in packfile order rather than .idx order
519 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
521 /* Only iterate over packs that are not marked as kept in-core. */
522 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
524 /* Only iterate over packs that do not have .keep files. */
525 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
529 * Iterate over all accessible loose objects without respect to
530 * reachability. By default, this includes both local and alternate objects.
531 * The order in which objects are visited is unspecified.
533 * Any flags specific to packs are ignored.
535 int for_each_loose_object(each_loose_object_fn, void *,
536 enum for_each_object_flags flags);
539 * Iterate over all accessible packed objects without respect to reachability.
540 * By default, this includes both local and alternate packs.
542 * Note that some objects may appear twice if they are found in multiple packs.
543 * Each pack is visited in an unspecified order. By default, objects within a
544 * pack are visited in pack-idx order (i.e., sorted by oid).
546 typedef int each_packed_object_fn(const struct object_id *oid,
547 struct packed_git *pack,
548 uint32_t pos,
549 void *data);
550 int for_each_object_in_pack(struct packed_git *p,
551 each_packed_object_fn, void *data,
552 enum for_each_object_flags flags);
553 int for_each_packed_object(struct repository *repo, each_packed_object_fn cb,
554 void *data, enum for_each_object_flags flags);
556 #endif /* OBJECT_STORE_LL_H */