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