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