Git fork
1#ifndef ODB_H
2#define ODB_H
3
4#include "hashmap.h"
5#include "object.h"
6#include "oidset.h"
7#include "oidmap.h"
8#include "string-list.h"
9#include "thread-utils.h"
10
11struct oidmap;
12struct oidtree;
13struct strbuf;
14struct repository;
15struct multi_pack_index;
16
17/*
18 * Compute the exact path an alternate is at and returns it. In case of
19 * error NULL is returned and the human readable error is added to `err`
20 * `path` may be relative and should point to $GIT_DIR.
21 * `err` must not be null.
22 */
23char *compute_alternate_path(const char *path, struct strbuf *err);
24
25/*
26 * The source is the part of the object database that stores the actual
27 * objects. It thus encapsulates the logic to read and write the specific
28 * on-disk format. An object database can have multiple sources:
29 *
30 * - The primary source, which is typically located in "$GIT_DIR/objects".
31 * This is where new objects are usually written to.
32 *
33 * - Alternate sources, which are configured via "objects/info/alternates" or
34 * via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
35 * alternate sources are only used to read objects.
36 */
37struct odb_source {
38 struct odb_source *next;
39
40 /* Object database that owns this object source. */
41 struct object_database *odb;
42
43 /*
44 * Used to store the results of readdir(3) calls when we are OK
45 * sacrificing accuracy due to races for speed. That includes
46 * object existence with OBJECT_INFO_QUICK, as well as
47 * our search for unique abbreviated hashes. Don't use it for tasks
48 * requiring greater accuracy!
49 *
50 * Be sure to call odb_load_loose_cache() before using.
51 */
52 uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
53 struct oidtree *loose_objects_cache;
54
55 /* Map between object IDs for loose objects. */
56 struct loose_object_map *loose_map;
57
58 /*
59 * private data
60 *
61 * should only be accessed directly by packfile.c and midx.c
62 */
63 struct multi_pack_index *midx;
64
65 /*
66 * Figure out whether this is the local source of the owning
67 * repository, which would typically be its ".git/objects" directory.
68 * This local object directory is usually where objects would be
69 * written to.
70 */
71 bool local;
72
73 /*
74 * This is a temporary object store created by the tmp_objdir
75 * facility. Disable ref updates since the objects in the store
76 * might be discarded on rollback.
77 */
78 int disable_ref_updates;
79
80 /*
81 * This object store is ephemeral, so there is no need to fsync.
82 */
83 int will_destroy;
84
85 /*
86 * Path to the source. If this is a relative path, it is relative to
87 * the current working directory.
88 */
89 char *path;
90};
91
92struct packed_git;
93struct packfile_store;
94struct cached_object_entry;
95struct odb_transaction;
96
97/*
98 * The object database encapsulates access to objects in a repository. It
99 * manages one or more sources that store the actual objects which are
100 * configured via alternates.
101 */
102struct object_database {
103 /* Repository that owns this database. */
104 struct repository *repo;
105
106 /*
107 * State of current current object database transaction. Only one
108 * transaction may be pending at a time. Is NULL when no transaction is
109 * configured.
110 */
111 struct odb_transaction *transaction;
112
113 /*
114 * Set of all object directories; the main directory is first (and
115 * cannot be NULL after initialization). Subsequent directories are
116 * alternates.
117 */
118 struct odb_source *sources;
119 struct odb_source **sources_tail;
120 struct kh_odb_path_map *source_by_path;
121
122 int loaded_alternates;
123
124 /*
125 * A list of alternate object directories loaded from the environment;
126 * this should not generally need to be accessed directly, but will
127 * populate the "sources" list when odb_prepare_alternates() is run.
128 */
129 char *alternate_db;
130
131 /*
132 * Objects that should be substituted by other objects
133 * (see git-replace(1)).
134 */
135 struct oidmap replace_map;
136 unsigned replace_map_initialized : 1;
137 pthread_mutex_t replace_mutex; /* protect object replace functions */
138
139 struct commit_graph *commit_graph;
140 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
141
142 /* Should only be accessed directly by packfile.c and midx.c. */
143 struct packfile_store *packfiles;
144
145 /*
146 * This is meant to hold a *small* number of objects that you would
147 * want odb_read_object() to be able to return, but yet you do not want
148 * to write them into the object store (e.g. a browse-only
149 * application).
150 */
151 struct cached_object_entry *cached_objects;
152 size_t cached_object_nr, cached_object_alloc;
153
154 /*
155 * A fast, rough count of the number of objects in the repository.
156 * These two fields are not meant for direct access. Use
157 * repo_approximate_object_count() instead.
158 */
159 unsigned long approximate_object_count;
160 unsigned approximate_object_count_valid : 1;
161
162 /*
163 * Submodule source paths that will be added as additional sources to
164 * allow lookup of submodule objects via the main object database.
165 */
166 struct string_list submodule_source_paths;
167};
168
169struct object_database *odb_new(struct repository *repo);
170void odb_clear(struct object_database *o);
171
172/*
173 * Clear caches, reload alternates and then reload object sources so that new
174 * objects may become accessible.
175 */
176void odb_reprepare(struct object_database *o);
177
178/*
179 * Starts an ODB transaction. Subsequent objects are written to the transaction
180 * and not committed until odb_transaction_commit() is invoked on the
181 * transaction. If the ODB already has a pending transaction, NULL is returned.
182 */
183struct odb_transaction *odb_transaction_begin(struct object_database *odb);
184
185/*
186 * Commits an ODB transaction making the written objects visible. If the
187 * specified transaction is NULL, the function is a no-op.
188 */
189void odb_transaction_commit(struct odb_transaction *transaction);
190
191/*
192 * Find source by its object directory path. Returns a `NULL` pointer in case
193 * the source could not be found.
194 */
195struct 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. */
198struct 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 */
204struct 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 */
211void 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 */
220void 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 */
229typedef int odb_for_each_alternate_fn(struct odb_source *, void *);
230int 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 */
237typedef void odb_for_each_alternate_ref_fn(const struct object_id *oid, void *);
238void 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 */
247int 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 */
254void 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 */
260int 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 */
266void 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 */
274struct 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 */
286void *odb_read_object(struct object_database *odb,
287 const struct object_id *oid,
288 enum object_type *type,
289 unsigned long *size);
290
291void *odb_read_object_peeled(struct object_database *odb,
292 const struct object_id *oid,
293 enum object_type required_type,
294 unsigned long *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 */
305int odb_pretend_object(struct object_database *odb,
306 void *buf, unsigned long len, enum object_type type,
307 struct object_id *oid);
308
309struct object_info {
310 /* Request */
311 enum object_type *typep;
312 unsigned long *sizep;
313 off_t *disk_sizep;
314 struct object_id *delta_base_oid;
315 void **contentp;
316
317 /* Response */
318 enum {
319 OI_CACHED,
320 OI_LOOSE,
321 OI_PACKED,
322 OI_DBCACHED
323 } whence;
324 union {
325 /*
326 * struct {
327 * ... Nothing to expose in this case
328 * } cached;
329 * struct {
330 * ... Nothing to expose in this case
331 * } loose;
332 */
333 struct {
334 struct packed_git *pack;
335 off_t offset;
336 unsigned int is_delta;
337 } packed;
338 } u;
339};
340
341/*
342 * Initializer for a "struct object_info" that wants no items. You may
343 * also memset() the memory to all-zeroes.
344 */
345#define OBJECT_INFO_INIT { 0 }
346
347/* Invoke lookup_replace_object() on the given hash */
348#define OBJECT_INFO_LOOKUP_REPLACE 1
349/* Do not retry packed storage after checking packed and loose storage */
350#define OBJECT_INFO_QUICK 8
351/*
352 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
353 * nonzero).
354 */
355#define OBJECT_INFO_SKIP_FETCH_OBJECT 16
356/*
357 * This is meant for bulk prefetching of missing blobs in a partial
358 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
359 */
360#define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
361
362/* Die if object corruption (not just an object being missing) was detected. */
363#define OBJECT_INFO_DIE_IF_CORRUPT 32
364
365/*
366 * Read object info from the object database and populate the `object_info`
367 * structure. Returns 0 on success, a negative error code otherwise.
368 */
369int odb_read_object_info_extended(struct object_database *odb,
370 const struct object_id *oid,
371 struct object_info *oi,
372 unsigned flags);
373
374/*
375 * Read a subset of object info for the given object ID. Returns an `enum
376 * object_type` on success, a negative error code otherwise. If successful and
377 * `sizep` is non-NULL, then the size of the object will be written to the
378 * pointer.
379 */
380int odb_read_object_info(struct object_database *odb,
381 const struct object_id *oid,
382 unsigned long *sizep);
383
384enum {
385 /* Retry packed storage after checking packed and loose storage */
386 HAS_OBJECT_RECHECK_PACKED = (1 << 0),
387 /* Allow fetching the object in case the repository has a promisor remote. */
388 HAS_OBJECT_FETCH_PROMISOR = (1 << 1),
389};
390
391/*
392 * Returns 1 if the object exists. This function will not lazily fetch objects
393 * in a partial clone by default.
394 */
395int odb_has_object(struct object_database *odb,
396 const struct object_id *oid,
397 unsigned flags);
398
399void odb_assert_oid_type(struct object_database *odb,
400 const struct object_id *oid, enum object_type expect);
401
402/*
403 * Enabling the object read lock allows multiple threads to safely call the
404 * following functions in parallel: odb_read_object(),
405 * odb_read_object_peeled(), odb_read_object_info() and odb().
406 *
407 * obj_read_lock() and obj_read_unlock() may also be used to protect other
408 * section which cannot execute in parallel with object reading. Since the used
409 * lock is a recursive mutex, these sections can even contain calls to object
410 * reading functions. However, beware that in these cases zlib inflation won't
411 * be performed in parallel, losing performance.
412 *
413 * TODO: odb_read_object_info_extended()'s call stack has a recursive behavior. If
414 * any of its callees end up calling it, this recursive call won't benefit from
415 * parallel inflation.
416 */
417void enable_obj_read_lock(void);
418void disable_obj_read_lock(void);
419
420extern int obj_read_use_lock;
421extern pthread_mutex_t obj_read_mutex;
422
423static inline void obj_read_lock(void)
424{
425 if(obj_read_use_lock)
426 pthread_mutex_lock(&obj_read_mutex);
427}
428
429static inline void obj_read_unlock(void)
430{
431 if(obj_read_use_lock)
432 pthread_mutex_unlock(&obj_read_mutex);
433}
434/* Flags for for_each_*_object(). */
435enum for_each_object_flags {
436 /* Iterate only over local objects, not alternates. */
437 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
438
439 /* Only iterate over packs obtained from the promisor remote. */
440 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
441
442 /*
443 * Visit objects within a pack in packfile order rather than .idx order
444 */
445 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
446
447 /* Only iterate over packs that are not marked as kept in-core. */
448 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
449
450 /* Only iterate over packs that do not have .keep files. */
451 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
452};
453
454enum {
455 /*
456 * By default, `odb_write_object()` does not actually write anything
457 * into the object store, but only computes the object ID. This flag
458 * changes that so that the object will be written as a loose object
459 * and persisted.
460 */
461 WRITE_OBJECT_PERSIST = (1 << 0),
462
463 /*
464 * Do not print an error in case something goes wrong.
465 */
466 WRITE_OBJECT_SILENT = (1 << 1),
467};
468
469/*
470 * Write an object into the object database. The object is being written into
471 * the local alternate of the repository. If provided, the converted object ID
472 * as well as the compatibility object ID are written to the respective
473 * pointers.
474 *
475 * Returns 0 on success, a negative error code otherwise.
476 */
477int odb_write_object_ext(struct object_database *odb,
478 const void *buf, unsigned long len,
479 enum object_type type,
480 struct object_id *oid,
481 struct object_id *compat_oid,
482 unsigned flags);
483
484static inline int odb_write_object(struct object_database *odb,
485 const void *buf, unsigned long len,
486 enum object_type type,
487 struct object_id *oid)
488{
489 return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
490}
491
492#endif /* ODB_H */