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