at commit 508ec983

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