Raw
1 #ifndef ODB_H
2 #define ODB_H
3
4 #include "object.h"
5 #include "oidset.h"
6 #include "oidmap.h"
7 #include "string-list.h"
8 #include "thread-utils.h"
9
10 struct cached_object_entry;
11 struct diff_hunks_store;
12 struct list_objects_filter_options;
13 struct odb_source_inmemory;
14 struct packed_git;
15 struct repository;
16 struct strbuf;
17 struct strvec;
18
19 /*
20 * Set this to 0 to prevent odb_read_object_info_extended() from fetching missing
21 * blobs. This has a difference only if extensions.partialClone is set.
22 *
23 * Its default value is 1.
24 */
25 extern int fetch_if_missing;
26
27 /*
28 * Compute the exact path an alternate is at and returns it. In case of
29 * error NULL is returned and the human readable error is added to `err`
30 * `path` may be relative and should point to $GIT_DIR.
31 * `err` must not be null.
32 */
33 char *compute_alternate_path(const char *path, struct strbuf *err);
34
35 /*
36 * The object database encapsulates access to objects in a repository. It
37 * manages one or more sources that store the actual objects which are
38 * configured via alternates.
39 */
40 struct object_database {
41 /* Repository that owns this database. */
42 struct repository *repo;
43
44 /*
45 * State of current object database transaction. Only one
46 * transaction may be pending at a time. Is NULL when no transaction is
47 * configured.
48 */
49 struct odb_transaction *transaction;
50
51 /*
52 * Set of all object directories; the main directory is first (and
53 * cannot be NULL after initialization). Subsequent directories are
54 * alternates.
55 */
56 struct odb_source *sources;
57 struct odb_source **sources_tail;
58 struct kh_odb_path_map *source_by_path;
59
60 int loaded_alternates;
61
62 /*
63 * A list of alternate object directories loaded from the environment;
64 * this should not generally need to be accessed directly, but will
65 * populate the "sources" list when odb_prepare_alternates() is run.
66 */
67 char *alternate_db;
68
69 /*
70 * Objects that should be substituted by other objects
71 * (see git-replace(1)).
72 */
73 struct oidmap replace_map;
74 unsigned replace_map_initialized : 1;
75 pthread_mutex_t replace_mutex; /* protect object replace functions */
76
77 struct commit_graph *commit_graph;
78 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
79
80 struct diff_hunks_store *diff_hunks_store;
81 unsigned diff_hunks_store_attempted : 1; /* if loading has been attempted */
82
83 /*
84 * This is meant to hold a *small* number of objects that you would
85 * want odb_read_object() to be able to return, but yet you do not want
86 * to write them into the object store (e.g. a browse-only
87 * application).
88 */
89 struct odb_source *inmemory_objects;
90
91 /*
92 * A fast, rough count of the number of objects in the repository.
93 * These two fields are not meant for direct access. Use
94 * odb_count_objects() instead.
95 */
96 unsigned long object_count;
97 unsigned object_count_flags;
98 unsigned object_count_valid : 1;
99
100 /*
101 * Submodule source paths that will be added as additional sources to
102 * allow lookup of submodule objects via the main object database.
103 */
104 struct string_list submodule_source_paths;
105 };
106
107 /*
108 * Create a new object database for the given repository.
109 *
110 * If the primary source parameter is set it will override the usual primary
111 * object directory derived from the repository's common directory. The
112 * alternate sources are expected to be a PATH_SEP-separated list of secondary
113 * sources. Note that these alternate sources will be added in addition to, not
114 * instead of, the alternates identified by the primary source.
115 *
116 * Returns the newly created object database.
117 */
118 struct object_database *odb_new(struct repository *repo,
119 const char *primary_source,
120 const char *alternate_sources);
121
122 /* Free the object database and release all resources. */
123 void odb_free(struct object_database *o);
124
125 enum odb_optimize_strategy {
126 ODB_OPTIMIZE_INCREMENTAL,
127 ODB_OPTIMIZE_GEOMETRIC,
128 };
129
130 enum odb_optimize_flags {
131 /* Enable verbose logging and progress reporting. */
132 ODB_OPTIMIZE_VERBOSE = (1 << 0),
133
134 /* Perform auto-maintenance, only optimizing objects as required. */
135 ODB_OPTIMIZE_AUTO = (1 << 1),
136
137 /* Recompute existing deltas. */
138 ODB_OPTIMIZE_NO_REUSE_DELTAS = (1 << 2),
139 };
140
141 struct odb_optimize_options {
142 enum odb_optimize_strategy strategy;
143 enum odb_optimize_flags flags;
144 const char *prune_expire;
145 const char *expire_to;
146 int depth;
147 int window;
148
149 /* Backend-specific options. */
150 int keep_largest_pack;
151 int cruft_packs;
152 unsigned long max_cruft_size;
153 };
154
155 /*
156 * Optimize the object database. Returns 0 on success, a negative error code
157 * otherwise.
158 */
159 int odb_optimize(struct object_database *odb,
160 const struct odb_optimize_options *opts);
161
162 /*
163 * Check whether optimization of the object database is required given the
164 * provided options. Returns true if optimization should be performed, false
165 * otherwise.
166 */
167 bool odb_optimize_required(struct object_database *odb,
168 const struct odb_optimize_options *opts);
169
170 /*
171 * Close the object database and all of its sources so that any held resources
172 * will be released. The database can still be used after closing it, in which
173 * case these resources may be reallocated.
174 */
175 void odb_close(struct object_database *o);
176
177 enum odb_prepare_flags {
178 /*
179 * Flush caches, reload alternates and then re-prepare each object
180 * source so that new objects may become accessible.
181 */
182 ODB_PREPARE_FLUSH_CACHES = (1 << 0),
183 };
184
185 /*
186 * Prepare the object database for use. Calling this function is generally not
187 * needed, but can be useful in case the caller wants to pre-open individual
188 * sources.
189 */
190 void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
191
192 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
193 void odb_reprepare(struct object_database *o);
194
195 /*
196 * Find source by its object directory path. Returns a `NULL` pointer in case
197 * the source could not be found.
198 */
199 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
200
201 /* Same as `odb_find_source()`, but dies in case the source doesn't exist. */
202 struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir);
203
204 /*
205 * Replace the current writable object directory with the specified temporary
206 * object directory; returns the former primary source.
207 */
208 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
209 const char *dir, int will_destroy);
210
211 /*
212 * Restore the primary source that was previously replaced by
213 * `odb_set_temporary_primary_source()`.
214 */
215 void odb_restore_primary_source(struct object_database *odb,
216 struct odb_source *restore_source,
217 const char *old_path);
218
219 /*
220 * Call odb_add_submodule_source_by_path() to add the submodule at the given
221 * path to a list. The object stores of all submodules in that list will be
222 * added as additional sources in the object store when looking up objects.
223 */
224 void odb_add_submodule_source_by_path(struct object_database *odb,
225 const char *path);
226
227 /*
228 * Iterate through all alternates of the database and execute the provided
229 * callback function for each of them. Stop iterating once the callback
230 * function returns a non-zero value, in which case the value is bubbled up
231 * from the callback.
232 */
233 typedef int odb_for_each_alternate_fn(struct odb_source *, void *);
234 int odb_for_each_alternate(struct object_database *odb,
235 odb_for_each_alternate_fn cb, void *payload);
236
237 /*
238 * Iterate through all alternates of the database and yield their respective
239 * references.
240 */
241 typedef void odb_for_each_alternate_ref_fn(const struct object_id *oid, void *);
242 void odb_for_each_alternate_ref(struct object_database *odb,
243 odb_for_each_alternate_ref_fn cb, void *payload);
244
245 /*
246 * Create a temporary file rooted in the primary alternate's directory, or die
247 * on failure. The filename is taken from "pattern", which should have the
248 * usual "XXXXXX" trailer, and the resulting filename is written into the
249 * "template" buffer. Returns the open descriptor.
250 */
251 int odb_mkstemp(struct object_database *odb,
252 struct strbuf *temp_filename, const char *pattern);
253
254 /*
255 * Prepare alternate object sources for the given database by reading
256 * "objects/info/alternates" and opening the respective sources.
257 */
258 void odb_prepare_alternates(struct object_database *odb);
259
260 /*
261 * Check whether the object database has any alternates. The primary object
262 * source does not count as alternate.
263 */
264 int odb_has_alternates(struct object_database *odb);
265
266 /*
267 * Add the directory to the on-disk alternates file; the new entry will also
268 * take effect in the current process.
269 */
270 void odb_add_to_alternates_file(struct object_database *odb,
271 const char *dir);
272
273 /*
274 * Add the directory to the in-memory list of alternate sources (along with any
275 * recursive alternates it points to), but do not modify the on-disk alternates
276 * file.
277 */
278 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
279 const char *dir);
280
281 /*
282 * Read an object from the database. Returns the object data and assigns object
283 * type and size to the `type` and `size` pointers, if these pointers are
284 * non-NULL. Returns a `NULL` pointer in case the object does not exist.
285 *
286 * This function dies on corrupt objects; the callers who want to deal with
287 * them should arrange to call odb_read_object_info_extended() and give error
288 * messages themselves.
289 */
290 void *odb_read_object(struct object_database *odb,
291 const struct object_id *oid,
292 enum object_type *type,
293 size_t *size);
294
295 void *odb_read_object_peeled(struct object_database *odb,
296 const struct object_id *oid,
297 enum object_type required_type,
298 size_t *size,
299 struct object_id *oid_ret);
300
301 /*
302 * Add an object file to the in-memory object store, without writing it
303 * to disk.
304 *
305 * Callers are responsible for calling write_object_file to record the
306 * object in persistent storage before writing any other new objects
307 * that reference it.
308 */
309 int odb_pretend_object(struct object_database *odb,
310 void *buf, size_t len, enum object_type type,
311 struct object_id *oid);
312
313 /*
314 * Object database source information that can be used to uniquely identify an
315 * object and learn more about how exactly it is stored.
316 */
317 struct odb_source_info {
318 /* The source that this object has been looked up from. */
319 struct odb_source *source;
320
321 /*
322 * Backend-specific information about the specific object. This can be
323 * used for example to uniquely identify a given object in case it
324 * exists multiple times.
325 */
326 union {
327 /*
328 * struct {
329 * ... Nothing to expose in this case
330 * } cached;
331 * struct {
332 * ... Nothing to expose in this case
333 * } loose;
334 */
335 struct {
336 struct packed_git *pack;
337 off_t offset;
338 enum packed_object_type {
339 PACKED_OBJECT_TYPE_UNKNOWN,
340 PACKED_OBJECT_TYPE_FULL,
341 PACKED_OBJECT_TYPE_OFS_DELTA,
342 PACKED_OBJECT_TYPE_REF_DELTA,
343 } type;
344 } packed;
345 } u;
346 };
347
348 /*
349 * The object info contains the query and response that is to be used for
350 * functions that end up reading object information. Callers are expected to
351 * populate pointers whose information they want to request.
352 */
353 struct object_info {
354 /* The object type. */
355 enum object_type *typep;
356
357 /* The inflated object size in bytes. */
358 size_t *sizep;
359
360 /* The object size as stored on disk. */
361 off_t *disk_sizep;
362
363 /*
364 * The base the object is deltified against, in case it is stored as a
365 * delta.
366 */
367 struct object_id *delta_base_oid;
368
369 /* The object contents. Ownership of memory goes over to the caller. */
370 void **contentp;
371
372 /*
373 * The time the given looked-up object has been last modified.
374 *
375 * Note: the mtime may be ambiguous in case the object exists multiple
376 * times in the object database. It is thus _not_ recommended to use
377 * this field outside of contexts where you would read every instance
378 * of the object, like for example with `odb_for_each_object()`. As it
379 * is impossible to say at the ODB level what the intent of the caller
380 * is (e.g. whether to find the oldest or newest object), it is the
381 * responsibility of the caller to disambiguate the mtimes.
382 */
383 time_t *mtimep;
384
385 /*
386 * Backend-specific information that tells the caller where exactly an
387 * object was looked up from. This information should help disambiguate
388 * object lookups in case the same object exists in multiple sources,
389 * or multiple times in the same source.
390 */
391 struct odb_source_info *source_infop;
392
393 /*
394 * object-info protocol specific. Set by the protocol when the remote
395 * does not recognize the requested object.
396 */
397 unsigned int unrecognized:1;
398 };
399
400 /*
401 * Initializer for a "struct object_info" that wants no items. You may
402 * also memset() the memory to all-zeroes.
403 */
404 #define OBJECT_INFO_INIT { 0 }
405
406 /* Flags that can be passed to `odb_read_object_info_extended()`. */
407 enum object_info_flags {
408 /* Invoke lookup_replace_object() on the given hash. */
409 OBJECT_INFO_LOOKUP_REPLACE = (1 << 0),
410
411 /* Do not reprepare object sources when the first lookup has failed. */
412 OBJECT_INFO_QUICK = (1 << 1),
413
414 /*
415 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
416 * nonzero).
417 */
418 OBJECT_INFO_SKIP_FETCH_OBJECT = (1 << 2),
419
420 /* Die if object corruption (not just an object being missing) was detected. */
421 OBJECT_INFO_DIE_IF_CORRUPT = (1 << 3),
422
423 /*
424 * We have already tried reading the object, but it couldn't be found
425 * via any of the attached sources, and are now doing a second read.
426 * This second read asks the individual sources to also evaluate
427 * whether any on-disk state may have changed that may have caused the
428 * object to appear.
429 *
430 * This flag is for internal use, only. The second read only occurs
431 * when `OBJECT_INFO_QUICK` was not passed.
432 */
433 OBJECT_INFO_SECOND_READ = (1 << 4),
434
435 /*
436 * This is meant for bulk prefetching of missing blobs in a partial
437 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK.
438 */
439 OBJECT_INFO_FOR_PREFETCH = (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK),
440 };
441
442 /*
443 * Read object info from the object database and populate the `object_info`
444 * structure. Returns 0 on success, a negative error code otherwise.
445 */
446 int odb_read_object_info_extended(struct object_database *odb,
447 const struct object_id *oid,
448 struct object_info *oi,
449 enum object_info_flags flags);
450
451 /*
452 * Read a subset of object info for the given object ID. Returns an `enum
453 * object_type` on success, a negative error code otherwise. If successful and
454 * `sizep` is non-NULL, then the size of the object will be written to the
455 * pointer.
456 */
457 int odb_read_object_info(struct object_database *odb,
458 const struct object_id *oid,
459 size_t *sizep);
460
461 enum odb_has_object_flags {
462 /* Retry packed storage after checking packed and loose storage */
463 ODB_HAS_OBJECT_RECHECK_PACKED = (1 << 0),
464 /* Allow fetching the object in case the repository has a promisor remote. */
465 ODB_HAS_OBJECT_FETCH_PROMISOR = (1 << 1),
466 };
467
468 /*
469 * Returns 1 if the object exists. This function will not lazily fetch objects
470 * in a partial clone by default.
471 */
472 int odb_has_object(struct object_database *odb,
473 const struct object_id *oid,
474 enum odb_has_object_flags flags);
475
476 int odb_freshen_object(struct object_database *odb,
477 const struct object_id *oid);
478
479 void odb_assert_oid_type(struct object_database *odb,
480 const struct object_id *oid, enum object_type expect);
481
482 /*
483 * Enabling the object read lock allows multiple threads to safely call the
484 * following functions in parallel: odb_read_object(),
485 * odb_read_object_peeled(), odb_read_object_info() and odb().
486 *
487 * obj_read_lock() and obj_read_unlock() may also be used to protect other
488 * section which cannot execute in parallel with object reading. Since the used
489 * lock is a recursive mutex, these sections can even contain calls to object
490 * reading functions. However, beware that in these cases zlib inflation won't
491 * be performed in parallel, losing performance.
492 *
493 * TODO: odb_read_object_info_extended()'s call stack has a recursive behavior. If
494 * any of its callees end up calling it, this recursive call won't benefit from
495 * parallel inflation.
496 */
497 void enable_obj_read_lock(void);
498 void disable_obj_read_lock(void);
499
500 extern int obj_read_use_lock;
501 extern pthread_mutex_t obj_read_mutex;
502
503 static inline void obj_read_lock(void)
504 {
505 if(obj_read_use_lock)
506 pthread_mutex_lock(&obj_read_mutex);
507 }
508
509 static inline void obj_read_unlock(void)
510 {
511 if(obj_read_use_lock)
512 pthread_mutex_unlock(&obj_read_mutex);
513 }
514
515 /* Flags for for_each_*_object(). */
516 enum odb_for_each_object_flags {
517 /* Iterate only over local objects, not alternates. */
518 ODB_FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
519
520 /* Only iterate over packs obtained from the promisor remote. */
521 ODB_FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
522
523 /*
524 * Visit objects within a pack in packfile order rather than .idx order
525 */
526 ODB_FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
527
528 /* Only iterate over packs that are not marked as kept in-core. */
529 ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
530
531 /* Only iterate over packs that do not have .keep files. */
532 ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
533 };
534
535 /*
536 * A callback function that can be used to iterate through objects. If given,
537 * the optional `oi` parameter will be populated the same as if you would call
538 * `odb_read_object_info()`.
539 *
540 * Returning a non-zero error code will cause iteration to abort. The error
541 * code will be propagated.
542 */
543 typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
544 struct object_info *oi,
545 void *cb_data);
546
547 /*
548 * Options that can be passed to `odb_for_each_object()` and its
549 * backend-specific implementations.
550 */
551 struct odb_for_each_object_options {
552 /* A bitfield of `odb_for_each_object_flags`. */
553 enum odb_for_each_object_flags flags;
554
555 /*
556 * If set, only iterate through objects whose first `prefix_hex_len`
557 * hex characters matches the given prefix.
558 */
559 const struct object_id *prefix;
560 size_t prefix_hex_len;
561
562 /*
563 * Optional object filter that allows backends to skip yielding
564 * objects that are excluded by the filter as an optimization. The
565 * filter is a best-effort hint: backends may use it to skip
566 * excluded objects (e.g. by consulting a reachability bitmap), but
567 * are also free to ignore it entirely and yield every object. As a
568 * consequence, callers must re-apply the filter on yielded objects
569 * if they require strict filtering semantics.
570 */
571 const struct list_objects_filter_options *filter;
572 };
573
574 /*
575 * Iterate through all objects contained in the object database. Note that
576 * objects may be iterated over multiple times in case they are either stored
577 * in different backends or in case they are stored in multiple sources.
578 * If an object info request is given, then the object info will be read and
579 * passed to the callback as if `odb_read_object_info()` was called for the
580 * object.
581 *
582 * Returning a non-zero error code from the callback function will cause
583 * iteration to abort. The error code will be propagated.
584 *
585 * Returns 0 on success, a negative error code in case a failure occurred, or
586 * an arbitrary non-zero error code returned by the callback itself.
587 */
588 int odb_for_each_object_ext(struct object_database *odb,
589 const struct object_info *request,
590 odb_for_each_object_cb cb,
591 void *cb_data,
592 const struct odb_for_each_object_options *opts);
593
594 /* Same as `odb_for_each_object_ext()` with `opts.flags` set to the given flags. */
595 int odb_for_each_object(struct object_database *odb,
596 const struct object_info *request,
597 odb_for_each_object_cb cb,
598 void *cb_data,
599 enum odb_for_each_object_flags flags);
600
601 enum odb_count_objects_flags {
602 /*
603 * Instead of providing an accurate count, allow the number of objects
604 * to be approximated. Details of how this approximation works are
605 * subject to the specific source's implementation.
606 */
607 ODB_COUNT_OBJECTS_APPROXIMATE = (1 << 0),
608 };
609
610 /*
611 * Count the number of objects in the given object database. This object count
612 * may double-count objects that are stored in multiple backends, or which are
613 * stored multiple times in a single backend.
614 *
615 * Returns 0 on success, a negative error code otherwise. The number of objects
616 * will be assigned to the `out` pointer on success.
617 */
618 int odb_count_objects(struct object_database *odb,
619 enum odb_count_objects_flags flags,
620 unsigned long *out);
621
622 /*
623 * Given an object ID, find the minimum required length required to make the
624 * object ID unique across the whole object database.
625 *
626 * The `min_len` determines the minimum abbreviated length that'll be returned
627 * by this function. If `min_len < 0`, then the function will set a sensible
628 * default minimum abbreviation length.
629 *
630 * Returns 0 on success, a negative error code otherwise. The computed length
631 * will be assigned to `*out`.
632 */
633 int odb_find_abbrev_len(struct object_database *odb,
634 const struct object_id *oid,
635 int min_len,
636 unsigned *out);
637
638 enum odb_write_object_flags {
639 /*
640 * By default, `odb_write_object()` does not actually write anything
641 * into the object store, but only computes the object ID. This flag
642 * changes that so that the object will be written as a loose object
643 * and persisted.
644 */
645 ODB_WRITE_OBJECT_PERSIST = (1 << 0),
646
647 /*
648 * Do not print an error in case something goes wrong.
649 */
650 ODB_WRITE_OBJECT_SILENT = (1 << 1),
651 };
652
653 /*
654 * Write an object into the object database. The object is being written into
655 * the local alternate of the repository. If provided, the object ID of the
656 * final object is written into `oid`.
657 *
658 * If the caller provides a `compat_oid`, then this compatibility object hash
659 * will be stored instead of computing the compatibility hash ad-hoc.
660 *
661 * Returns 0 on success, a negative error code otherwise.
662 */
663 int odb_write_object_ext(struct object_database *odb,
664 const void *buf, unsigned long len,
665 enum object_type type,
666 struct object_id *oid,
667 const struct object_id *compat_oid,
668 enum odb_write_object_flags flags);
669
670 static inline int odb_write_object(struct object_database *odb,
671 const void *buf, unsigned long len,
672 enum object_type type,
673 struct object_id *oid)
674 {
675 return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
676 }
677
678 struct odb_write_stream;
679
680 int odb_write_object_stream(struct object_database *odb,
681 struct odb_write_stream *stream, size_t len,
682 struct object_id *oid);
683
684 void parse_alternates(const char *string,
685 int sep,
686 const char *relative_base,
687 struct strvec *out);
688
689 #endif /* ODB_H */