at commit a0189536

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