Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 1 | #ifndef OBJECT_STORE_H |
| 2 | #define OBJECT_STORE_H |
| 3 | |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 4 | #include "cache.h" |
Stefan Beller | d88f9fd | 2018-04-11 17:21:05 -0700 | [diff] [blame] | 5 | #include "oidmap.h" |
Jonathan Tan | 14727b7 | 2018-07-11 15:42:38 -0700 | [diff] [blame] | 6 | #include "list.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 7 | #include "oid-array.h" |
Jonathan Tan | 14727b7 | 2018-07-11 15:42:38 -0700 | [diff] [blame] | 8 | #include "strbuf.h" |
Matheus Tavares | 31877c9 | 2020-01-15 23:39:53 -0300 | [diff] [blame] | 9 | #include "thread-utils.h" |
Eric Wong | cf2dc1c | 2021-07-07 23:10:15 +0000 | [diff] [blame] | 10 | #include "khash.h" |
| 11 | #include "dir.h" |
Eric Wong | 92d8ed8 | 2021-07-07 23:10:19 +0000 | [diff] [blame] | 12 | #include "oidtree.h" |
René Scharfe | 09ef661 | 2021-09-11 22:43:26 +0200 | [diff] [blame] | 13 | #include "oidset.h" |
Stefan Beller | d88f9fd | 2018-04-11 17:21:05 -0700 | [diff] [blame] | 14 | |
Jeff King | 263db40 | 2018-11-12 09:48:47 -0500 | [diff] [blame] | 15 | struct object_directory { |
| 16 | struct object_directory *next; |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 17 | |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 18 | /* |
Jeff King | 3a2e082 | 2018-11-12 09:50:56 -0500 | [diff] [blame] | 19 | * Used to store the results of readdir(3) calls when we are OK |
| 20 | * sacrificing accuracy due to races for speed. That includes |
Jeff King | 61c7711 | 2018-11-12 09:54:42 -0500 | [diff] [blame] | 21 | * object existence with OBJECT_INFO_QUICK, as well as |
Jeff King | 3a2e082 | 2018-11-12 09:50:56 -0500 | [diff] [blame] | 22 | * our search for unique abbreviated hashes. Don't use it for tasks |
| 23 | * requiring greater accuracy! |
| 24 | * |
| 25 | * Be sure to call odb_load_loose_cache() before using. |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 26 | */ |
Eric Wong | 33f379e | 2021-07-07 23:10:17 +0000 | [diff] [blame] | 27 | uint32_t loose_objects_subdir_seen[8]; /* 256 bits */ |
Eric Wong | 92d8ed8 | 2021-07-07 23:10:19 +0000 | [diff] [blame] | 28 | struct oidtree *loose_objects_cache; |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 29 | |
Stefan Beller | 77f012e | 2018-03-23 18:21:08 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Path to the alternative object store. If this is a relative path, |
| 32 | * it is relative to the current working directory. |
| 33 | */ |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 34 | char *path; |
Stefan Beller | 031dc92 | 2018-03-23 18:20:57 +0100 | [diff] [blame] | 35 | }; |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 36 | |
Eric Wong | cf2dc1c | 2021-07-07 23:10:15 +0000 | [diff] [blame] | 37 | KHASH_INIT(odb_path_map, const char * /* key: odb_path */, |
Carlo Marcelo Arenas Belón | dd3c8a7 | 2021-08-08 18:38:32 -0700 | [diff] [blame] | 38 | struct object_directory *, 1, fspathhash, fspatheq) |
Eric Wong | cf2dc1c | 2021-07-07 23:10:15 +0000 | [diff] [blame] | 39 | |
Stefan Beller | 13068bf | 2018-03-23 18:21:09 +0100 | [diff] [blame] | 40 | void prepare_alt_odb(struct repository *r); |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 41 | char *compute_alternate_path(const char *path, struct strbuf *err); |
Taylor Blau | f57a739 | 2021-09-01 16:34:01 -0400 | [diff] [blame] | 42 | struct object_directory *find_odb(struct repository *r, const char *obj_dir); |
Jeff King | 263db40 | 2018-11-12 09:48:47 -0500 | [diff] [blame] | 43 | typedef int alt_odb_fn(struct object_directory *, void *); |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 44 | int foreach_alt_odb(alt_odb_fn, void*); |
Jeff King | 709dfa6 | 2019-07-01 09:17:40 -0400 | [diff] [blame] | 45 | typedef void alternate_ref_fn(const struct object_id *oid, void *); |
| 46 | void for_each_alternate_ref(alternate_ref_fn, void *); |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 47 | |
| 48 | /* |
Stefan Beller | 0d4a132 | 2018-03-23 18:20:56 +0100 | [diff] [blame] | 49 | * Add the directory to the on-disk alternates file; the new entry will also |
| 50 | * take effect in the current process. |
| 51 | */ |
| 52 | void add_to_alternates_file(const char *dir); |
| 53 | |
| 54 | /* |
| 55 | * Add the directory to the in-memory list of alternates (along with any |
| 56 | * recursive alternates it points to), but do not modify the on-disk alternates |
| 57 | * file. |
| 58 | */ |
| 59 | void add_to_alternates_memory(const char *dir); |
| 60 | |
Jeff King | 3a2e082 | 2018-11-12 09:50:56 -0500 | [diff] [blame] | 61 | /* |
René Scharfe | 0000d65 | 2019-01-06 17:45:30 +0100 | [diff] [blame] | 62 | * Populate and return the loose object cache array corresponding to the |
| 63 | * given object ID. |
| 64 | */ |
Eric Wong | 92d8ed8 | 2021-07-07 23:10:19 +0000 | [diff] [blame] | 65 | struct oidtree *odb_loose_cache(struct object_directory *odb, |
René Scharfe | 0000d65 | 2019-01-06 17:45:30 +0100 | [diff] [blame] | 66 | const struct object_id *oid); |
| 67 | |
René Scharfe | d4e19e5 | 2019-01-06 17:45:39 +0100 | [diff] [blame] | 68 | /* Empty the loose object cache for the specified object directory. */ |
| 69 | void odb_clear_loose_cache(struct object_directory *odb); |
| 70 | |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 71 | struct packed_git { |
Colin Stolley | ec48540 | 2019-11-27 16:24:53 -0600 | [diff] [blame] | 72 | struct hashmap_entry packmap_ent; |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 73 | struct packed_git *next; |
| 74 | struct list_head mru; |
| 75 | struct pack_window *windows; |
| 76 | off_t pack_size; |
| 77 | const void *index_data; |
| 78 | size_t index_size; |
| 79 | uint32_t num_objects; |
brian m. carlson | 629dffc | 2020-05-25 19:59:10 +0000 | [diff] [blame] | 80 | uint32_t crc_offset; |
René Scharfe | 09ef661 | 2021-09-11 22:43:26 +0200 | [diff] [blame] | 81 | struct oidset bad_objects; |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 82 | int index_version; |
| 83 | time_t mtime; |
| 84 | int pack_fd; |
Nguyễn Thái Ngọc Duy | 43fa44f | 2018-04-14 17:35:05 +0200 | [diff] [blame] | 85 | int index; /* for builtin/pack-objects.c */ |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 86 | unsigned pack_local:1, |
| 87 | pack_keep:1, |
Nguyễn Thái Ngọc Duy | ed7e5fc | 2018-04-15 17:36:13 +0200 | [diff] [blame] | 88 | pack_keep_in_core:1, |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 89 | freshened:1, |
| 90 | do_not_close:1, |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 91 | pack_promisor:1, |
| 92 | multi_pack_index:1; |
brian m. carlson | 538b152 | 2019-02-19 00:05:03 +0000 | [diff] [blame] | 93 | unsigned char hash[GIT_MAX_RAWSZ]; |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 94 | struct revindex_entry *revindex; |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 95 | const uint32_t *revindex_data; |
| 96 | const uint32_t *revindex_map; |
| 97 | size_t revindex_size; |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 98 | /* something like ".git/objects/pack/xxxxx.pack" */ |
| 99 | char pack_name[FLEX_ARRAY]; /* more */ |
| 100 | }; |
| 101 | |
Derrick Stolee | 4d80560 | 2018-07-12 15:39:23 -0400 | [diff] [blame] | 102 | struct multi_pack_index; |
| 103 | |
Colin Stolley | ec48540 | 2019-11-27 16:24:53 -0600 | [diff] [blame] | 104 | static inline int pack_map_entry_cmp(const void *unused_cmp_data, |
| 105 | const struct hashmap_entry *entry, |
| 106 | const struct hashmap_entry *entry2, |
| 107 | const void *keydata) |
| 108 | { |
| 109 | const char *key = keydata; |
| 110 | const struct packed_git *pg1, *pg2; |
| 111 | |
| 112 | pg1 = container_of(entry, const struct packed_git, packmap_ent); |
| 113 | pg2 = container_of(entry2, const struct packed_git, packmap_ent); |
| 114 | |
| 115 | return strcmp(pg1->pack_name, key ? key : pg2->pack_name); |
| 116 | } |
| 117 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 118 | struct raw_object_store { |
| 119 | /* |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 120 | * Set of all object directories; the main directory is first (and |
| 121 | * cannot be NULL after initialization). Subsequent directories are |
| 122 | * alternates. |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 123 | */ |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 124 | struct object_directory *odb; |
| 125 | struct object_directory **odb_tail; |
Eric Wong | cf2dc1c | 2021-07-07 23:10:15 +0000 | [diff] [blame] | 126 | kh_odb_path_map_t *odb_by_path; |
| 127 | |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 128 | int loaded_alternates; |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 129 | |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 130 | /* |
| 131 | * A list of alternate object directories loaded from the environment; |
| 132 | * this should not generally need to be accessed directly, but will |
| 133 | * populate the "odb" list when prepare_alt_odb() is run. |
| 134 | */ |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 135 | char *alternate_db; |
Stefan Beller | 031dc92 | 2018-03-23 18:20:57 +0100 | [diff] [blame] | 136 | |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 137 | /* |
Stefan Beller | d88f9fd | 2018-04-11 17:21:05 -0700 | [diff] [blame] | 138 | * Objects that should be substituted by other objects |
| 139 | * (see git-replace(1)). |
| 140 | */ |
Stefan Beller | c127449 | 2018-04-11 17:21:07 -0700 | [diff] [blame] | 141 | struct oidmap *replace_map; |
Matheus Tavares | b1fc9da | 2020-01-15 23:39:52 -0300 | [diff] [blame] | 142 | unsigned replace_map_initialized : 1; |
| 143 | pthread_mutex_t replace_mutex; /* protect object replace functions */ |
Stefan Beller | d88f9fd | 2018-04-11 17:21:05 -0700 | [diff] [blame] | 144 | |
Jonathan Tan | 8527750 | 2018-07-11 15:42:41 -0700 | [diff] [blame] | 145 | struct commit_graph *commit_graph; |
| 146 | unsigned commit_graph_attempted : 1; /* if loading has been attempted */ |
| 147 | |
Stefan Beller | d88f9fd | 2018-04-11 17:21:05 -0700 | [diff] [blame] | 148 | /* |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 149 | * private data |
| 150 | * |
Derrick Stolee | c4d2522 | 2018-07-12 15:39:33 -0400 | [diff] [blame] | 151 | * should only be accessed directly by packfile.c and midx.c |
| 152 | */ |
| 153 | struct multi_pack_index *multi_pack_index; |
| 154 | |
| 155 | /* |
| 156 | * private data |
| 157 | * |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 158 | * should only be accessed directly by packfile.c |
| 159 | */ |
| 160 | |
| 161 | struct packed_git *packed_git; |
| 162 | /* A most-recently-used ordered version of the packed_git list. */ |
| 163 | struct list_head packed_git_mru; |
Stefan Beller | 5508f69 | 2018-03-23 18:21:01 +0100 | [diff] [blame] | 164 | |
Jeff King | 20b031f | 2021-02-22 21:25:23 -0500 | [diff] [blame] | 165 | struct { |
| 166 | struct packed_git **packs; |
| 167 | unsigned flags; |
| 168 | } kept_pack_cache; |
| 169 | |
Stefan Beller | 5508f69 | 2018-03-23 18:21:01 +0100 | [diff] [blame] | 170 | /* |
Colin Stolley | ec48540 | 2019-11-27 16:24:53 -0600 | [diff] [blame] | 171 | * A map of packfiles to packed_git structs for tracking which |
| 172 | * packs have been loaded already. |
| 173 | */ |
| 174 | struct hashmap pack_map; |
| 175 | |
| 176 | /* |
Stefan Beller | 9a00580 | 2018-03-23 18:21:02 +0100 | [diff] [blame] | 177 | * A fast, rough count of the number of objects in the repository. |
| 178 | * These two fields are not meant for direct access. Use |
| 179 | * approximate_object_count() instead. |
| 180 | */ |
| 181 | unsigned long approximate_object_count; |
| 182 | unsigned approximate_object_count_valid : 1; |
| 183 | |
| 184 | /* |
Stefan Beller | 5508f69 | 2018-03-23 18:21:01 +0100 | [diff] [blame] | 185 | * Whether packed_git has already been populated with this repository's |
| 186 | * packs. |
| 187 | */ |
| 188 | unsigned packed_git_initialized : 1; |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | struct raw_object_store *raw_object_store_new(void); |
| 192 | void raw_object_store_clear(struct raw_object_store *o); |
| 193 | |
Stefan Beller | cf78ae4 | 2018-03-23 18:21:10 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Put in `buf` the name of the file in the local object database that |
Jeff King | 514c5fd | 2019-01-07 03:35:42 -0500 | [diff] [blame] | 196 | * would be used to store a loose object with the specified oid. |
Stefan Beller | cf78ae4 | 2018-03-23 18:21:10 +0100 | [diff] [blame] | 197 | */ |
Jeff King | 514c5fd | 2019-01-07 03:35:42 -0500 | [diff] [blame] | 198 | const char *loose_object_path(struct repository *r, struct strbuf *buf, |
| 199 | const struct object_id *oid); |
Stefan Beller | cf78ae4 | 2018-03-23 18:21:10 +0100 | [diff] [blame] | 200 | |
Jeff King | 514c5fd | 2019-01-07 03:35:42 -0500 | [diff] [blame] | 201 | void *map_loose_object(struct repository *r, const struct object_id *oid, |
| 202 | unsigned long *size); |
Stefan Beller | e35454f | 2018-03-23 18:21:14 +0100 | [diff] [blame] | 203 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 204 | void *read_object_file_extended(struct repository *r, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame] | 205 | const struct object_id *oid, |
| 206 | enum object_type *type, |
| 207 | unsigned long *size, int lookup_replace); |
Stefan Beller | afd69dc | 2018-11-13 16:12:47 -0800 | [diff] [blame] | 208 | static inline void *repo_read_object_file(struct repository *r, |
| 209 | const struct object_id *oid, |
| 210 | enum object_type *type, |
| 211 | unsigned long *size) |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 212 | { |
Stefan Beller | afd69dc | 2018-11-13 16:12:47 -0800 | [diff] [blame] | 213 | return read_object_file_extended(r, oid, type, size, 1); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 214 | } |
Stefan Beller | afd69dc | 2018-11-13 16:12:47 -0800 | [diff] [blame] | 215 | #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS |
| 216 | #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size) |
| 217 | #endif |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 218 | |
| 219 | /* Read and unpack an object file into memory, write memory to an object file */ |
| 220 | int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); |
| 221 | |
Matheus Tavares | 2dcde20 | 2020-01-30 17:32:22 -0300 | [diff] [blame] | 222 | int hash_object_file(const struct git_hash_algo *algo, const void *buf, |
| 223 | unsigned long len, const char *type, |
| 224 | struct object_id *oid); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 225 | |
Ævar Arnfjörð Bjarmason | 4ef91a2 | 2021-10-12 16:30:49 +0200 | [diff] [blame] | 226 | int write_object_file_flags(const void *buf, unsigned long len, |
| 227 | const char *type, struct object_id *oid, |
| 228 | unsigned flags); |
| 229 | static inline int write_object_file(const void *buf, unsigned long len, |
| 230 | const char *type, struct object_id *oid) |
| 231 | { |
| 232 | return write_object_file_flags(buf, len, type, oid, 0); |
| 233 | } |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 234 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 235 | int hash_object_file_literally(const void *buf, unsigned long len, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame] | 236 | const char *type, struct object_id *oid, |
| 237 | unsigned flags); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 238 | |
Jonathan Nieder | 60440d7 | 2020-01-03 16:13:31 -0800 | [diff] [blame] | 239 | /* |
| 240 | * Add an object file to the in-memory object store, without writing it |
| 241 | * to disk. |
| 242 | * |
| 243 | * Callers are responsible for calling write_object_file to record the |
| 244 | * object in persistent storage before writing any other new objects |
| 245 | * that reference it. |
| 246 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 247 | int pretend_object_file(void *, unsigned long, enum object_type, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame] | 248 | struct object_id *oid); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 249 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 250 | int force_object_loose(const struct object_id *oid, time_t mtime); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * Open the loose object at path, check its hash, and return the contents, |
Ævar Arnfjörð Bjarmason | 31deb28 | 2021-10-01 11:16:52 +0200 | [diff] [blame] | 254 | * use the "oi" argument to assert things about the object, or e.g. populate its |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 255 | * type, and size. If the object is a blob, then "contents" may return NULL, |
| 256 | * to allow streaming of large blobs. |
| 257 | * |
| 258 | * Returns 0 on success, negative on error (details may be written to stderr). |
| 259 | */ |
| 260 | int read_loose_object(const char *path, |
| 261 | const struct object_id *expected_oid, |
Ævar Arnfjörð Bjarmason | 96e41f5 | 2021-10-01 11:16:53 +0200 | [diff] [blame] | 262 | struct object_id *real_oid, |
Ævar Arnfjörð Bjarmason | 31deb28 | 2021-10-01 11:16:52 +0200 | [diff] [blame] | 263 | void **contents, |
| 264 | struct object_info *oi); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 265 | |
Jonathan Tan | 1d8d9cb | 2020-08-05 16:06:49 -0700 | [diff] [blame] | 266 | /* Retry packed storage after checking packed and loose storage */ |
| 267 | #define HAS_OBJECT_RECHECK_PACKED 1 |
| 268 | |
| 269 | /* |
| 270 | * Returns 1 if the object exists. This function will not lazily fetch objects |
| 271 | * in a partial clone. |
| 272 | */ |
| 273 | int has_object(struct repository *r, const struct object_id *oid, |
| 274 | unsigned flags); |
| 275 | |
| 276 | /* |
| 277 | * These macros and functions are deprecated. If checking existence for an |
| 278 | * object that is likely to be missing and/or whose absence is relatively |
| 279 | * inconsequential (or is consequential but the caller is prepared to handle |
| 280 | * it), use has_object(), which has better defaults (no lazy fetch in a partial |
| 281 | * clone and no rechecking of packed storage). In the unlikely event that a |
| 282 | * caller needs to assert existence of an object that it fully expects to |
| 283 | * exist, and wants to trigger a lazy fetch in a partial clone, use |
| 284 | * oid_object_info_extended() with a NULL struct object_info. |
| 285 | * |
| 286 | * These functions can be removed once all callers have migrated to |
| 287 | * has_object() and/or oid_object_info_extended(). |
| 288 | */ |
Stefan Beller | 9b45f49 | 2018-11-13 16:12:48 -0800 | [diff] [blame] | 289 | #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS |
| 290 | #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags) |
| 291 | #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1) |
| 292 | #endif |
Stefan Beller | 9b45f49 | 2018-11-13 16:12:48 -0800 | [diff] [blame] | 293 | int repo_has_object_file(struct repository *r, const struct object_id *oid); |
| 294 | int repo_has_object_file_with_flags(struct repository *r, |
| 295 | const struct object_id *oid, int flags); |
| 296 | #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS |
| 297 | #define has_object_file(oid) repo_has_object_file(the_repository, oid) |
| 298 | #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags) |
| 299 | #endif |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Return true iff an alternate object database has a loose object |
| 303 | * with the specified name. This function does not respect replace |
| 304 | * references. |
| 305 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 306 | int has_loose_object_nonlocal(const struct object_id *); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 307 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 308 | void assert_oid_type(const struct object_id *oid, enum object_type expect); |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 309 | |
Matheus Tavares | 31877c9 | 2020-01-15 23:39:53 -0300 | [diff] [blame] | 310 | /* |
| 311 | * Enabling the object read lock allows multiple threads to safely call the |
| 312 | * following functions in parallel: repo_read_object_file(), read_object_file(), |
| 313 | * read_object_file_extended(), read_object_with_reference(), read_object(), |
| 314 | * oid_object_info() and oid_object_info_extended(). |
| 315 | * |
| 316 | * obj_read_lock() and obj_read_unlock() may also be used to protect other |
| 317 | * section which cannot execute in parallel with object reading. Since the used |
| 318 | * lock is a recursive mutex, these sections can even contain calls to object |
| 319 | * reading functions. However, beware that in these cases zlib inflation won't |
| 320 | * be performed in parallel, losing performance. |
| 321 | * |
| 322 | * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If |
| 323 | * any of its callees end up calling it, this recursive call won't benefit from |
| 324 | * parallel inflation. |
| 325 | */ |
| 326 | void enable_obj_read_lock(void); |
| 327 | void disable_obj_read_lock(void); |
| 328 | |
| 329 | extern int obj_read_use_lock; |
| 330 | extern pthread_mutex_t obj_read_mutex; |
| 331 | |
| 332 | static inline void obj_read_lock(void) |
| 333 | { |
| 334 | if(obj_read_use_lock) |
| 335 | pthread_mutex_lock(&obj_read_mutex); |
| 336 | } |
| 337 | |
| 338 | static inline void obj_read_unlock(void) |
| 339 | { |
| 340 | if(obj_read_use_lock) |
| 341 | pthread_mutex_unlock(&obj_read_mutex); |
| 342 | } |
| 343 | |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 344 | struct object_info { |
| 345 | /* Request */ |
| 346 | enum object_type *typep; |
| 347 | unsigned long *sizep; |
| 348 | off_t *disk_sizep; |
Jeff King | b99b6bc | 2020-02-23 23:36:56 -0500 | [diff] [blame] | 349 | struct object_id *delta_base_oid; |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 350 | struct strbuf *type_name; |
| 351 | void **contentp; |
| 352 | |
| 353 | /* Response */ |
| 354 | enum { |
| 355 | OI_CACHED, |
| 356 | OI_LOOSE, |
| 357 | OI_PACKED, |
| 358 | OI_DBCACHED |
| 359 | } whence; |
| 360 | union { |
| 361 | /* |
| 362 | * struct { |
| 363 | * ... Nothing to expose in this case |
| 364 | * } cached; |
| 365 | * struct { |
| 366 | * ... Nothing to expose in this case |
| 367 | * } loose; |
| 368 | */ |
| 369 | struct { |
| 370 | struct packed_git *pack; |
| 371 | off_t offset; |
| 372 | unsigned int is_delta; |
| 373 | } packed; |
| 374 | } u; |
| 375 | }; |
| 376 | |
| 377 | /* |
| 378 | * Initializer for a "struct object_info" that wants no items. You may |
| 379 | * also memset() the memory to all-zeroes. |
| 380 | */ |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 381 | #define OBJECT_INFO_INIT { 0 } |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 382 | |
| 383 | /* Invoke lookup_replace_object() on the given hash */ |
| 384 | #define OBJECT_INFO_LOOKUP_REPLACE 1 |
| 385 | /* Allow reading from a loose object file of unknown/bogus type */ |
| 386 | #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2 |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 387 | /* Do not retry packed storage after checking packed and loose storage */ |
| 388 | #define OBJECT_INFO_QUICK 8 |
| 389 | /* Do not check loose object */ |
| 390 | #define OBJECT_INFO_IGNORE_LOOSE 16 |
Jonathan Tan | 0f4a4fb | 2019-03-29 14:39:27 -0700 | [diff] [blame] | 391 | /* |
| 392 | * Do not attempt to fetch the object if missing (even if fetch_is_missing is |
Derrick Stolee | 31f5256 | 2019-05-28 08:19:07 -0700 | [diff] [blame] | 393 | * nonzero). |
Jonathan Tan | 0f4a4fb | 2019-03-29 14:39:27 -0700 | [diff] [blame] | 394 | */ |
Derrick Stolee | 31f5256 | 2019-05-28 08:19:07 -0700 | [diff] [blame] | 395 | #define OBJECT_INFO_SKIP_FETCH_OBJECT 32 |
| 396 | /* |
| 397 | * This is meant for bulk prefetching of missing blobs in a partial |
| 398 | * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK |
| 399 | */ |
| 400 | #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK) |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 401 | |
| 402 | int oid_object_info_extended(struct repository *r, |
| 403 | const struct object_id *, |
| 404 | struct object_info *, unsigned flags); |
| 405 | |
Jeff King | 0889aae | 2018-08-14 14:21:18 -0400 | [diff] [blame] | 406 | /* |
| 407 | * Iterate over the files in the loose-object parts of the object |
| 408 | * directory "path", triggering the following callbacks: |
| 409 | * |
| 410 | * - loose_object is called for each loose object we find. |
| 411 | * |
| 412 | * - loose_cruft is called for any files that do not appear to be |
| 413 | * loose objects. Note that we only look in the loose object |
| 414 | * directories "objects/[0-9a-f]{2}/", so we will not report |
| 415 | * "objects/foobar" as cruft. |
| 416 | * |
| 417 | * - loose_subdir is called for each top-level hashed subdirectory |
| 418 | * of the object directory (e.g., "$OBJDIR/f0"). It is called |
| 419 | * after the objects in the directory are processed. |
| 420 | * |
| 421 | * Any callback that is NULL will be ignored. Callbacks returning non-zero |
| 422 | * will end the iteration. |
| 423 | * |
| 424 | * In the "buf" variant, "path" is a strbuf which will also be used as a |
| 425 | * scratch buffer, but restored to its original contents before |
| 426 | * the function returns. |
| 427 | */ |
| 428 | typedef int each_loose_object_fn(const struct object_id *oid, |
| 429 | const char *path, |
| 430 | void *data); |
| 431 | typedef int each_loose_cruft_fn(const char *basename, |
| 432 | const char *path, |
| 433 | void *data); |
| 434 | typedef int each_loose_subdir_fn(unsigned int nr, |
| 435 | const char *path, |
| 436 | void *data); |
| 437 | int for_each_file_in_obj_subdir(unsigned int subdir_nr, |
| 438 | struct strbuf *path, |
| 439 | each_loose_object_fn obj_cb, |
| 440 | each_loose_cruft_fn cruft_cb, |
| 441 | each_loose_subdir_fn subdir_cb, |
| 442 | void *data); |
| 443 | int for_each_loose_file_in_objdir(const char *path, |
| 444 | each_loose_object_fn obj_cb, |
| 445 | each_loose_cruft_fn cruft_cb, |
| 446 | each_loose_subdir_fn subdir_cb, |
| 447 | void *data); |
| 448 | int for_each_loose_file_in_objdir_buf(struct strbuf *path, |
| 449 | each_loose_object_fn obj_cb, |
| 450 | each_loose_cruft_fn cruft_cb, |
| 451 | each_loose_subdir_fn subdir_cb, |
| 452 | void *data); |
| 453 | |
| 454 | /* Flags for for_each_*_object() below. */ |
| 455 | enum for_each_object_flags { |
| 456 | /* Iterate only over local objects, not alternates. */ |
| 457 | FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0), |
| 458 | |
| 459 | /* Only iterate over packs obtained from the promisor remote. */ |
| 460 | FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1), |
| 461 | |
| 462 | /* |
| 463 | * Visit objects within a pack in packfile order rather than .idx order |
| 464 | */ |
| 465 | FOR_EACH_OBJECT_PACK_ORDER = (1<<2), |
Taylor Blau | a241878 | 2021-08-29 22:48:52 -0400 | [diff] [blame] | 466 | |
| 467 | /* Only iterate over packs that are not marked as kept in-core. */ |
| 468 | FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3), |
| 469 | |
| 470 | /* Only iterate over packs that do not have .keep files. */ |
| 471 | FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4), |
Jeff King | 0889aae | 2018-08-14 14:21:18 -0400 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | /* |
| 475 | * Iterate over all accessible loose objects without respect to |
| 476 | * reachability. By default, this includes both local and alternate objects. |
| 477 | * The order in which objects are visited is unspecified. |
| 478 | * |
| 479 | * Any flags specific to packs are ignored. |
| 480 | */ |
| 481 | int for_each_loose_object(each_loose_object_fn, void *, |
| 482 | enum for_each_object_flags flags); |
| 483 | |
| 484 | /* |
| 485 | * Iterate over all accessible packed objects without respect to reachability. |
| 486 | * By default, this includes both local and alternate packs. |
| 487 | * |
| 488 | * Note that some objects may appear twice if they are found in multiple packs. |
| 489 | * Each pack is visited in an unspecified order. By default, objects within a |
| 490 | * pack are visited in pack-idx order (i.e., sorted by oid). |
| 491 | */ |
| 492 | typedef int each_packed_object_fn(const struct object_id *oid, |
| 493 | struct packed_git *pack, |
| 494 | uint32_t pos, |
| 495 | void *data); |
| 496 | int for_each_object_in_pack(struct packed_git *p, |
| 497 | each_packed_object_fn, void *data, |
| 498 | enum for_each_object_flags flags); |
| 499 | int for_each_packed_object(each_packed_object_fn, void *, |
| 500 | enum for_each_object_flags flags); |
| 501 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 502 | #endif /* OBJECT_STORE_H */ |