Derrick Stolee | 570df42 | 2021-02-18 14:07:24 +0000 | [diff] [blame] | 1 | #ifndef CHUNK_FORMAT_H |
| 2 | #define CHUNK_FORMAT_H |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | |
| 6 | struct hashfile; |
| 7 | struct chunkfile; |
| 8 | |
| 9 | #define CHUNK_TOC_ENTRY_SIZE (sizeof(uint32_t) + sizeof(uint64_t)) |
| 10 | |
Derrick Stolee | 5f0879f | 2021-02-18 14:07:34 +0000 | [diff] [blame] | 11 | /* |
| 12 | * Initialize a 'struct chunkfile' for writing _or_ reading a file |
| 13 | * with the chunk format. |
| 14 | * |
| 15 | * If writing a file, supply a non-NULL 'struct hashfile *' that will |
| 16 | * be used to write. |
| 17 | * |
| 18 | * If reading a file, use a NULL 'struct hashfile *' and then call |
| 19 | * read_table_of_contents(). Supply the memory-mapped data to the |
| 20 | * pair_chunk() or read_chunk() methods, as appropriate. |
| 21 | * |
| 22 | * DO NOT MIX THESE MODES. Use different 'struct chunkfile' instances |
| 23 | * for reading and writing. |
| 24 | */ |
Derrick Stolee | 570df42 | 2021-02-18 14:07:24 +0000 | [diff] [blame] | 25 | struct chunkfile *init_chunkfile(struct hashfile *f); |
| 26 | void free_chunkfile(struct chunkfile *cf); |
| 27 | int get_num_chunks(struct chunkfile *cf); |
| 28 | typedef int (*chunk_write_fn)(struct hashfile *f, void *data); |
| 29 | void add_chunk(struct chunkfile *cf, |
| 30 | uint32_t id, |
| 31 | size_t size, |
| 32 | chunk_write_fn fn); |
| 33 | int write_chunkfile(struct chunkfile *cf, void *data); |
| 34 | |
Derrick Stolee | 5f0879f | 2021-02-18 14:07:34 +0000 | [diff] [blame] | 35 | int read_table_of_contents(struct chunkfile *cf, |
| 36 | const unsigned char *mfile, |
| 37 | size_t mfile_size, |
| 38 | uint64_t toc_offset, |
| 39 | int toc_length); |
| 40 | |
| 41 | #define CHUNK_NOT_FOUND (-2) |
| 42 | |
| 43 | /* |
| 44 | * Find 'chunk_id' in the given chunkfile and assign the |
| 45 | * given pointer to the position in the mmap'd file where |
| 46 | * that chunk begins. |
| 47 | * |
| 48 | * Returns CHUNK_NOT_FOUND if the chunk does not exist. |
| 49 | */ |
| 50 | int pair_chunk(struct chunkfile *cf, |
| 51 | uint32_t chunk_id, |
| 52 | const unsigned char **p); |
| 53 | |
| 54 | typedef int (*chunk_read_fn)(const unsigned char *chunk_start, |
| 55 | size_t chunk_size, void *data); |
| 56 | /* |
| 57 | * Find 'chunk_id' in the given chunkfile and call the |
| 58 | * given chunk_read_fn method with the information for |
| 59 | * that chunk. |
| 60 | * |
| 61 | * Returns CHUNK_NOT_FOUND if the chunk does not exist. |
| 62 | */ |
| 63 | int read_chunk(struct chunkfile *cf, |
| 64 | uint32_t chunk_id, |
| 65 | chunk_read_fn fn, |
| 66 | void *data); |
| 67 | |
Derrick Stolee | 570df42 | 2021-02-18 14:07:24 +0000 | [diff] [blame] | 68 | #endif |