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