| 1 | #ifndef OBJECT_FILE_H |
| 2 | #define OBJECT_FILE_H |
| 3 | |
| 4 | #include "git-zlib.h" |
| 5 | #include "object.h" |
| 6 | #include "odb.h" |
| 7 | #include "odb/source-loose.h" |
| 8 | #include "odb/transaction.h" |
| 9 | |
| 10 | /* The maximum size for an object header. */ |
| 11 | #define MAX_HEADER_LEN 32 |
| 12 | |
| 13 | struct index_state; |
| 14 | |
| 15 | enum { |
| 16 | INDEX_WRITE_OBJECT = (1 << 0), |
| 17 | INDEX_FORMAT_CHECK = (1 << 1), |
| 18 | INDEX_RENORMALIZE = (1 << 2), |
| 19 | }; |
| 20 | |
| 21 | int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags); |
| 22 | int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags); |
| 23 | |
| 24 | struct object_info; |
| 25 | struct odb_source; |
| 26 | |
| 27 | /* |
| 28 | * Put in `buf` the name of the file in the local object database that |
| 29 | * would be used to store a loose object with the specified oid. |
| 30 | */ |
| 31 | const char *odb_loose_path(struct odb_source_loose *source, |
| 32 | struct strbuf *buf, |
| 33 | const struct object_id *oid); |
| 34 | |
| 35 | /* |
| 36 | * Iterate over the files in the loose-object parts of the object |
| 37 | * directory "path", triggering the following callbacks: |
| 38 | * |
| 39 | * - loose_object is called for each loose object we find. |
| 40 | * |
| 41 | * - loose_cruft is called for any files that do not appear to be |
| 42 | * loose objects. Note that we only look in the loose object |
| 43 | * directories "objects/[0-9a-f]{2}/", so we will not report |
| 44 | * "objects/foobar" as cruft. |
| 45 | * |
| 46 | * - loose_subdir is called for each top-level hashed subdirectory |
| 47 | * of the object directory (e.g., "$OBJDIR/f0"). It is called |
| 48 | * after the objects in the directory are processed. |
| 49 | * |
| 50 | * Any callback that is NULL will be ignored. Callbacks returning non-zero |
| 51 | * will end the iteration. |
| 52 | * |
| 53 | * In the "buf" variant, "path" is a strbuf which will also be used as a |
| 54 | * scratch buffer, but restored to its original contents before |
| 55 | * the function returns. |
| 56 | */ |
| 57 | typedef int each_loose_object_fn(const struct object_id *oid, |
| 58 | const char *path, |
| 59 | void *data); |
| 60 | typedef int each_loose_cruft_fn(const char *basename, |
| 61 | const char *path, |
| 62 | void *data); |
| 63 | typedef int each_loose_subdir_fn(unsigned int nr, |
| 64 | const char *path, |
| 65 | void *data); |
| 66 | int for_each_loose_file_in_source(struct odb_source *source, |
| 67 | each_loose_object_fn obj_cb, |
| 68 | each_loose_cruft_fn cruft_cb, |
| 69 | each_loose_subdir_fn subdir_cb, |
| 70 | void *data); |
| 71 | int for_each_file_in_obj_subdir(unsigned int subdir_nr, |
| 72 | struct strbuf *path, |
| 73 | const struct git_hash_algo *algop, |
| 74 | each_loose_object_fn obj_cb, |
| 75 | each_loose_cruft_fn cruft_cb, |
| 76 | each_loose_subdir_fn subdir_cb, |
| 77 | void *data); |
| 78 | |
| 79 | /** |
| 80 | * format_object_header() is a thin wrapper around s xsnprintf() that |
| 81 | * writes the initial "<type> <obj-len>" part of the loose object |
| 82 | * header. It returns the size that snprintf() returns + 1. |
| 83 | */ |
| 84 | int format_object_header(char *str, size_t size, enum object_type type, |
| 85 | size_t objsize); |
| 86 | |
| 87 | /** |
| 88 | * With in-core object data in "buf", rehash it to make sure the |
| 89 | * object name actually matches "oid" to detect object corruption. |
| 90 | * |
| 91 | * A negative value indicates an error, usually that the OID is not |
| 92 | * what we expected, but it might also indicate another error. |
| 93 | */ |
| 94 | int check_object_signature(struct repository *r, const struct object_id *oid, |
| 95 | void *map, unsigned long size, |
| 96 | enum object_type type); |
| 97 | |
| 98 | /** |
| 99 | * A streaming version of check_object_signature(). |
| 100 | * Try reading the object named with "oid" using |
| 101 | * the streaming interface and rehash it to do the same. |
| 102 | */ |
| 103 | int stream_object_signature(struct repository *r, |
| 104 | struct odb_read_stream *stream, |
| 105 | const struct object_id *oid); |
| 106 | |
| 107 | enum finalize_object_file_flags { |
| 108 | FOF_SKIP_COLLISION_CHECK = 1, |
| 109 | }; |
| 110 | |
| 111 | int finalize_object_file(struct repository *repo, |
| 112 | const char *tmpfile, const char *filename); |
| 113 | int finalize_object_file_flags(struct repository *repo, |
| 114 | const char *tmpfile, const char *filename, |
| 115 | enum finalize_object_file_flags flags); |
| 116 | |
| 117 | void hash_object_file(const struct git_hash_algo *algo, const void *buf, |
| 118 | size_t len, enum object_type type, |
| 119 | struct object_id *oid); |
| 120 | |
| 121 | /* Helper to check and "touch" a file */ |
| 122 | int check_and_freshen_file(const char *fn, int freshen, |
| 123 | const time_t *mtime); |
| 124 | |
| 125 | /* |
| 126 | * Open the loose object at path, check its hash, and return the contents, |
| 127 | * use the "oi" argument to assert things about the object, or e.g. populate its |
| 128 | * type, and size. If the object is a blob, then "contents" may return NULL, |
| 129 | * to allow streaming of large blobs. |
| 130 | * |
| 131 | * Returns 0 on success, negative on error (details may be written to stderr). |
| 132 | */ |
| 133 | int read_loose_object(struct repository *repo, |
| 134 | const char *path, |
| 135 | const struct object_id *expected_oid, |
| 136 | struct object_id *real_oid, |
| 137 | void **contents, |
| 138 | struct object_info *oi); |
| 139 | |
| 140 | enum unpack_loose_header_result { |
| 141 | ULHR_OK, |
| 142 | ULHR_BAD, |
| 143 | ULHR_TOO_LONG, |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * unpack_loose_header() initializes the data stream needed to unpack |
| 148 | * a loose object header. |
| 149 | * |
| 150 | * Returns: |
| 151 | * |
| 152 | * - ULHR_OK on success |
| 153 | * - ULHR_BAD on error |
| 154 | * - ULHR_TOO_LONG if the header was too long |
| 155 | * |
| 156 | * It will only parse up to MAX_HEADER_LEN bytes. |
| 157 | */ |
| 158 | enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, |
| 159 | unsigned char *map, |
| 160 | unsigned long mapsize, |
| 161 | void *buffer, |
| 162 | unsigned long bufsiz); |
| 163 | void *unpack_loose_rest(git_zstream *stream, |
| 164 | void *buffer, unsigned long size, |
| 165 | const struct object_id *oid); |
| 166 | |
| 167 | int parse_loose_header(const char *hdr, struct object_info *oi); |
| 168 | |
| 169 | struct odb_transaction; |
| 170 | |
| 171 | /* |
| 172 | * Tell the object database to optimize for adding |
| 173 | * multiple objects. odb_transaction_files_commit must be called |
| 174 | * to make new objects visible. |
| 175 | */ |
| 176 | int odb_transaction_files_begin(struct odb_source *source, |
| 177 | struct odb_transaction **out, |
| 178 | enum odb_transaction_flags flags); |
| 179 | |
| 180 | int odb_transaction_files_prepare(struct odb_transaction *base); |
| 181 | void odb_transaction_files_fsync(struct odb_transaction *base, |
| 182 | int fd, const char *filename); |
| 183 | |
| 184 | #endif /* OBJECT_FILE_H */ |