at commit a51b77aa

1 #ifndef ODB_SOURCE_H
2 #define ODB_SOURCE_H
3
4 #include "hashmap.h"
5 #include "object.h"
6 #include "odb.h"
7 #include "odb/transaction.h"
8
9 enum odb_source_type {
10 /*
11 * The "unknown" type, which should never be in use. This type mostly
12 * exists to catch cases where the type field remains zeroed out.
13 */
14 ODB_SOURCE_UNKNOWN,
15
16 /* The "files" backend that uses loose objects and packfiles. */
17 ODB_SOURCE_FILES,
18
19 /* The "loose" backend that uses loose objects, only. */
20 ODB_SOURCE_LOOSE,
21
22 /* The "packed" backend that uses packfiles. */
23 ODB_SOURCE_PACKED,
24
25 /* The "in-memory" backend that stores objects in memory. */
26 ODB_SOURCE_INMEMORY,
27 };
28
29 /*
30 * Convert between the enum and its name. Returns the equivalent of "unknown"
31 * for unknown types.
32 */
33 const char *odb_source_type_to_name(enum odb_source_type type);
34
35 struct object_id;
36 struct odb_stream;
37 struct strvec;
38
39 /*
40 * The source is the part of the object database that stores the actual
41 * objects. It thus encapsulates the logic to read and write the specific
42 * on-disk format. An object database can have multiple sources:
43 *
44 * - The primary source, which is typically located in "$GIT_DIR/objects".
45 * This is where new objects are usually written to.
46 *
47 * - Alternate sources, which are configured via "objects/info/alternates" or
48 * via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
49 * alternate sources are only used to read objects.
50 */
51 struct odb_source {
52 struct odb_source *next;
53
54 /*
55 * Entry in the object database's map of sources, keyed by this
56 * source's path.
57 */
58 struct hashmap_entry by_path_entry;
59
60 /* Object database that owns this object source. */
61 struct object_database *odb;
62
63 /* The type used by this source. */
64 enum odb_source_type type;
65
66 /*
67 * Figure out whether this is the local source of the owning
68 * repository, which would typically be its ".git/objects" directory.
69 * This local object directory is usually where objects would be
70 * written to.
71 */
72 bool local;
73
74 /*
75 * This object store is ephemeral, so there is no need to fsync.
76 */
77 int will_destroy;
78
79 /*
80 * Path to the source. If this is a relative path, it is relative to
81 * the current working directory.
82 */
83 char *path;
84
85 /*
86 * This callback is expected to free the underlying object database source and
87 * all associated resources. The function will never be called with a NULL pointer.
88 */
89 void (*free)(struct odb_source *source);
90
91 /*
92 * This callback is expected to close any open resources, like for
93 * example file descriptors or connections. The source is expected to
94 * still be usable after it has been closed. Closed resources may need
95 * to be reopened in that case.
96 */
97 void (*close)(struct odb_source *source);
98
99 /*
100 * This callback is expected to create on-disk data structures that are
101 * required for this source to operate.
102 *
103 * The callback is expected to return 0 on success, a negative error
104 * code otherwise.
105 *
106 * This callback may be NULL in case the source does not need any
107 * on-disk setup.
108 */
109 int (*create_on_disk)(struct odb_source *source);
110
111 /*
112 * This callback is expected to prepare the source so that it becomes
113 * ready for use. It optionally clears underlying caches of the object
114 * database source.
115 */
116 void (*prepare)(struct odb_source *source,
117 enum odb_prepare_flags flags);
118
119 /*
120 * This callback is expected to read object information from the object
121 * database source. The object info will be partially populated with
122 * pointers for each bit of information that was requested by the
123 * caller.
124 *
125 * The flags field is a combination of `OBJECT_INFO` flags. Only the
126 * following fields need to be handled by the backend:
127 *
128 * - `OBJECT_INFO_QUICK` indicates it is fine to use caches without
129 * re-verifying the data.
130 *
131 * - `OBJECT_INFO_SECOND_READ` indicates that the initial object
132 * lookup has failed and that the object sources should check
133 * whether any of its on-disk state has changed that may have
134 * caused the object to appear. Sources are free to ignore the
135 * second read in case they know that the first read would have
136 * already surfaced the object without reloading any on-disk state.
137 *
138 * The callback is expected to return a negative error code in case
139 * reading the object has failed, 0 otherwise.
140 */
141 int (*read_object_info)(struct odb_source *source,
142 const struct object_id *oid,
143 struct object_info *oi,
144 enum object_info_flags flags);
145
146 /*
147 * This callback is expected to create a new read stream that can be
148 * used to stream the object identified by the given ID.
149 *
150 * The callback is expected to return a negative error code in case
151 * creating the object stream has failed, 0 otherwise.
152 */
153 int (*read_object_stream)(struct odb_stream **out,
154 struct odb_source *source,
155 const struct object_id *oid);
156
157 /*
158 * This callback is expected to iterate over all objects stored in this
159 * source and invoke the callback function for each of them. It is
160 * valid to yield the same object multiple time. A non-zero exit code
161 * from the object callback shall abort iteration.
162 *
163 * The optional `request` structure should serve as a template for
164 * looking up object info for every individual iterated object. It
165 * should not be modified directly and should instead be copied into a
166 * separate `struct object_info` that gets passed to the callback. If
167 * the caller passes a `NULL` pointer then the object itself shall not
168 * be read.
169 *
170 * The callback is expected to return a negative error code in case the
171 * iteration has failed to read all objects, 0 otherwise. When the
172 * callback function returns a non-zero error code then that error code
173 * should be returned.
174 */
175 int (*for_each_object)(struct odb_source *source,
176 const struct object_info *request,
177 odb_for_each_object_cb cb,
178 void *cb_data,
179 const struct odb_for_each_object_options *opts);
180
181 /*
182 * This callback is expected to count objects in the given object
183 * database source. The callback function does not have to guarantee
184 * that only unique objects are counted. The result shall be assigned
185 * to the `out` pointer.
186 *
187 * Accepts `enum odb_count_objects_flag` flags to alter the behaviour.
188 *
189 * The callback is expected to return 0 on success, or a negative error
190 * code otherwise.
191 */
192 int (*count_objects)(struct odb_source *source,
193 enum odb_count_objects_flags flags,
194 unsigned long *out);
195
196 /*
197 * This callback is expected to find the minimum required length to
198 * make the given object ID unique.
199 *
200 * The callback is expected to return a negative error code in case it
201 * failed, 0 otherwise.
202 */
203 int (*find_abbrev_len)(struct odb_source *source,
204 const struct object_id *oid,
205 unsigned min_length,
206 unsigned *out);
207
208 /*
209 * This callback is expected to freshen the given object so that its
210 * last access time is set to the current time. This is used to ensure
211 * that objects that are recent will not get garbage collected even if
212 * they were unreachable.
213 *
214 * Returns 0 in case the object does not exist, 1 in case the object
215 * has been freshened.
216 */
217 int (*freshen_object)(struct odb_source *source,
218 const struct object_id *oid,
219 const time_t *mtime);
220
221 /*
222 * This callback is expected to persist the given object into the
223 * object source. In case the object already exists it shall be
224 * freshened.
225 *
226 * The flags field is a combination of `WRITE_OBJECT` flags.
227 *
228 * The resulting object ID (and optionally the compatibility object ID)
229 * shall be written into the out pointers. The callback is expected to
230 * return 0 on success, a negative error code otherwise.
231 */
232 int (*write_object)(struct odb_source *source,
233 const void *buf, size_t len,
234 enum object_type type,
235 const struct object_id *oid,
236 const struct object_id *compat_oid,
237 const time_t *mtime,
238 enum odb_write_object_flags flags);
239
240 /*
241 * This callback is expected to persist the given object stream into
242 * the object source.
243 *
244 * The resulting object ID shall be written into the out pointer. The
245 * callback is expected to return 0 on success, a negative error code
246 * otherwise.
247 */
248 int (*write_object_stream)(struct odb_source *source,
249 struct odb_stream *stream,
250 struct object_id *oid);
251
252 /*
253 * This callback is expected to create a new transaction that can be
254 * used to write objects to. The objects shall only be persisted into
255 * the object database when the transcation's commit function is
256 * called. Otherwise, the objects shall be discarded.
257 *
258 * Returns 0 on success, in which case the `*out` pointer will have
259 * been populated with the object database transaction. Returns a
260 * negative error code otherwise.
261 */
262 int (*begin_transaction)(struct odb_source *source,
263 struct odb_transaction **out,
264 enum odb_transaction_flags flags);
265
266 /*
267 * This callback is expected to read the list of alternate object
268 * database sources connected to it and write them into the `strvec`.
269 *
270 * The result is expected to be paths to the alternates. All paths must
271 * be resolved to absolute paths.
272 *
273 * The callback is expected to return 0 on success, a negative error
274 * code otherwise.
275 */
276 int (*read_alternates)(struct odb_source *source,
277 struct strvec *out);
278
279 /*
280 * This callback is expected to persist the singular alternate passed
281 * to it into its list of alternates. Any pre-existing alternates are
282 * expected to remain active. Subsequent calls to `read_alternates` are
283 * thus expected to yield the pre-existing list of alternates plus the
284 * newly added alternate appended to its end.
285 *
286 * The callback is expected to return 0 on success, a negative error
287 * code otherwise.
288 */
289 int (*write_alternate)(struct odb_source *source,
290 const char *alternate);
291
292 /*
293 * This callback is expected to optimize the object database source.
294 * Returns 0 on success, a negative error code otherwise.
295 */
296 int (*optimize)(struct odb_source *source,
297 const struct odb_optimize_options *opts);
298
299 /*
300 * This callback is expected to check whether optimization of the
301 * object database source is required given the provided options.
302 * Returns true if optimization should be performed, false otherwise.
303 */
304 bool (*optimize_required)(struct odb_source *source,
305 const struct odb_optimize_options *opts);
306
307 /*
308 * This callback is expected to start generating a packfile with the
309 * given options. The pack shall be generated asynchronously so that
310 * the caller can consume the pack data and progress output while the
311 * pack is being generated.
312 *
313 * This callback is optional. Sources that cannot generate packfiles
314 * shall leave it unset.
315 *
316 * The callback is expected to return 0 on success and populate the
317 * `out` pointer with the pack generator, a negative error code
318 * otherwise.
319 */
320 int (*generate_pack)(struct odb_source *source,
321 struct odb_pack_generator **out,
322 const struct odb_generate_pack_options *opts);
323
324 /*
325 * This callback is expected to check the integrity of the object source
326 * and report any errors found via the fsck options. The checks performed
327 * are backend-specific.
328 *
329 * The callback is expected to return 0 on success, a negative error
330 * code otherwise.
331 */
332 int (*fsck)(struct odb_source *source,
333 struct odb_fsck_options *options);
334 };
335
336 /*
337 * Allocate and initialize a new source for the given object database located
338 * at `path`. `local` indicates whether or not the source is the local and thus
339 * primary object source of the object database.
340 */
341 struct odb_source *odb_source_new(struct object_database *odb,
342 const char *path,
343 bool local);
344
345 /*
346 * Initialize the source for the given object database located at `path`.
347 * `local` indicates whether or not the source is the local and thus primary
348 * object source of the object database.
349 *
350 * This function is only supposed to be called by specific object source
351 * implementations.
352 */
353 void odb_source_init(struct odb_source *source,
354 struct object_database *odb,
355 enum odb_source_type type,
356 const char *path,
357 bool local);
358
359 /*
360 * Free the object database source, releasing all associated resources and
361 * freeing the structure itself.
362 */
363 void odb_source_free(struct odb_source *source);
364
365 /*
366 * Release the object database source, releasing all associated resources.
367 *
368 * This function is only supposed to be called by specific object source
369 * implementations.
370 */
371 void odb_source_release(struct odb_source *source);
372
373 /*
374 * Close the object database source without releasing he underlying data. The
375 * source can still be used going forward, but it first needs to be reopened.
376 * This can be useful to reduce resource usage.
377 */
378 static inline void odb_source_close(struct odb_source *source)
379 {
380 source->close(source);
381 }
382
383 /*
384 * Create on-disk data structures that are required for this source to operate
385 * correctly. Returns 0 on success, a negative error code otherwise.
386 */
387 static inline int odb_source_create_on_disk(struct odb_source *source)
388 {
389 if (!source->create_on_disk)
390 return 0;
391 return source->create_on_disk(source);
392 }
393
394 /*
395 * Prepare the object database source and clear any caches. Depending on the
396 * backend used this may have the effect that concurrently-written objects
397 * become visible.
398 */
399 static inline void odb_source_prepare(struct odb_source *source,
400 enum odb_prepare_flags flags)
401 {
402 source->prepare(source, flags);
403 }
404
405 /*
406 * Read an object from the object database source identified by its object ID.
407 * Returns 0 on success, a negative error code otherwise.
408 */
409 static inline int odb_source_read_object_info(struct odb_source *source,
410 const struct object_id *oid,
411 struct object_info *oi,
412 enum object_info_flags flags)
413 {
414 return source->read_object_info(source, oid, oi, flags);
415 }
416
417 /*
418 * Create a new read stream for the given object ID. Returns 0 on success, a
419 * negative error code otherwise.
420 */
421 static inline int odb_source_read_object_stream(struct odb_stream **out,
422 struct odb_source *source,
423 const struct object_id *oid)
424 {
425 return source->read_object_stream(out, source, oid);
426 }
427
428 /*
429 * Iterate through all objects contained in the given source and invoke the
430 * callback function for each of them. Returning a non-zero code from the
431 * callback function aborts iteration. There is no guarantee that objects
432 * are only iterated over once.
433 *
434 * The optional `request` structure serves as a template for retrieving the
435 * object info for each individual iterated object and will be populated as if
436 * `odb_source_read_object_info()` was called on the object. It will not be
437 * modified, the callback will instead be invoked with a separate `struct
438 * object_info` for every object. Object info will not be read when passing a
439 * `NULL` pointer.
440 *
441 * The flags is a bitfield of `ODB_FOR_EACH_OBJECT_*` flags. Not all flags may
442 * apply to a specific backend, so whether or not they are honored is defined
443 * by the implementation.
444 *
445 * Returns 0 when all objects have been iterated over, a negative error code in
446 * case iteration has failed, or a non-zero value returned from the callback.
447 */
448 static inline int odb_source_for_each_object(struct odb_source *source,
449 const struct object_info *request,
450 odb_for_each_object_cb cb,
451 void *cb_data,
452 const struct odb_for_each_object_options *opts)
453 {
454 return source->for_each_object(source, request, cb, cb_data, opts);
455 }
456
457 /*
458 * Count the number of objects in the given object database source.
459 *
460 * Returns 0 on success, a negative error code otherwise.
461 */
462 static inline int odb_source_count_objects(struct odb_source *source,
463 enum odb_count_objects_flags flags,
464 unsigned long *out)
465 {
466 return source->count_objects(source, flags, out);
467 }
468
469 /*
470 * Determine the minimum required length to make the given object ID unique in
471 * the given source. Returns 0 on success, a negative error code otherwise.
472 */
473 static inline int odb_source_find_abbrev_len(struct odb_source *source,
474 const struct object_id *oid,
475 unsigned min_len,
476 unsigned *out)
477 {
478 return source->find_abbrev_len(source, oid, min_len, out);
479 }
480
481 /*
482 * Freshen an object in the object database by updating its timestamp.
483 * Returns 1 in case the object has been freshened, 0 in case the object does
484 * not exist.
485 */
486 static inline int odb_source_freshen_object(struct odb_source *source,
487 const struct object_id *oid,
488 const time_t *mtime)
489 {
490 return source->freshen_object(source, oid, mtime);
491 }
492
493 /*
494 * Write an object into the object database source. Returns 0 on success, a
495 * negative error code otherwise. Populates the given out pointers for the
496 * object ID and the compatibility object ID, if non-NULL.
497 */
498 static inline int odb_source_write_object(struct odb_source *source,
499 const void *buf, unsigned long len,
500 enum object_type type,
501 const struct object_id *oid,
502 const struct object_id *compat_oid,
503 const time_t *mtime,
504 enum odb_write_object_flags flags)
505 {
506 return source->write_object(source, buf, len, type, oid,
507 compat_oid, mtime, flags);
508 }
509
510 /*
511 * Write an object into the object database source via a stream. The overall
512 * length of the object must be known in advance.
513 *
514 * Return 0 on success, a negative error code otherwise. Populates the given
515 * out pointer for the object ID.
516 */
517 static inline int odb_source_write_object_stream(struct odb_source *source,
518 struct odb_stream *stream,
519 struct object_id *oid)
520 {
521 return source->write_object_stream(source, stream, oid);
522 }
523
524 /*
525 * Read the list of alternative object database sources from the given backend
526 * and populate the `strvec` with them. The listing is not recursive -- that
527 * is, if any of the yielded alternate sources has alternates itself, those
528 * will not be yielded as part of this function call.
529 *
530 * Return 0 on success, a negative error code otherwise.
531 */
532 static inline int odb_source_read_alternates(struct odb_source *source,
533 struct strvec *out)
534 {
535 return source->read_alternates(source, out);
536 }
537
538 /*
539 * Write and persist a new alternate object database source for the given
540 * source. Any preexisting alternates are expected to stay valid, and the new
541 * alternate shall be appended to the end of the list.
542 *
543 * Returns 0 on success, a negative error code otherwise.
544 */
545 static inline int odb_source_write_alternate(struct odb_source *source,
546 const char *alternate)
547 {
548 return source->write_alternate(source, alternate);
549 }
550
551 /*
552 * Create a new transaction that can be used to write objects into a temporary
553 * staging area. The objects will only be persisted when the transaction is
554 * committed.
555 *
556 * Returns 0 on success, a negative error code otherwise.
557 */
558 static inline int odb_source_begin_transaction(struct odb_source *source,
559 struct odb_transaction **out,
560 enum odb_transaction_flags flags)
561 {
562 return source->begin_transaction(source, out, flags);
563 }
564
565 /*
566 * Optimize the object database source. Returns 0 on success, a negative error
567 * code otherwise.
568 */
569 static inline int odb_source_optimize(struct odb_source *source,
570 const struct odb_optimize_options *opts)
571 {
572 return source->optimize(source, opts);
573 }
574
575 /*
576 * Check whether optimization of the object database source is required given
577 * the provided options. Returns true if optimization should be performed,
578 * false otherwise.
579 */
580 static inline bool odb_source_optimize_required(struct odb_source *source,
581 const struct odb_optimize_options *opts)
582 {
583 return source->optimize_required(source, opts);
584 }
585
586 /*
587 * Start generating a packfile from the given source with the given options.
588 * The pack is generated asynchronously; the caller is expected to consume the
589 * file descriptors exposed via the pack generator and to then wait for
590 * completion via `odb_pack_generator_finish()`.
591 *
592 * Returns 0 on success and populates the `out` pointer with the pack
593 * generator, a negative error code otherwise.
594 */
595 static inline int odb_source_generate_pack(struct odb_source *source,
596 struct odb_pack_generator **out,
597 const struct odb_generate_pack_options *opts)
598 {
599 return source->generate_pack(source, out, opts);
600 }
601
602 /*
603 * Check the integrity of the object database source. The checks performed
604 * are backend-specific. Returns 0 on success, a negative error code otherwise.
605 */
606 static inline int odb_source_fsck(struct odb_source *source,
607 struct odb_fsck_options *opts)
608 {
609 return source->fsck(source, opts);
610 }
611
612 #endif