]> git.ipfire.org Git - thirdparty/git.git/blob - object-store.h
Merge branch 'jk/get-oid-error-message-i18n'
[thirdparty/git.git] / object-store.h
1 #ifndef OBJECT_STORE_H
2 #define OBJECT_STORE_H
3
4 #include "cache.h"
5 #include "oidmap.h"
6 #include "list.h"
7 #include "sha1-array.h"
8 #include "strbuf.h"
9 #include "thread-utils.h"
10
11 struct object_directory {
12 struct object_directory *next;
13
14 /*
15 * Used to store the results of readdir(3) calls when we are OK
16 * sacrificing accuracy due to races for speed. That includes
17 * object existence with OBJECT_INFO_QUICK, as well as
18 * our search for unique abbreviated hashes. Don't use it for tasks
19 * requiring greater accuracy!
20 *
21 * Be sure to call odb_load_loose_cache() before using.
22 */
23 char loose_objects_subdir_seen[256];
24 struct oid_array loose_objects_cache[256];
25
26 /*
27 * Path to the alternative object store. If this is a relative path,
28 * it is relative to the current working directory.
29 */
30 char *path;
31 };
32
33 void prepare_alt_odb(struct repository *r);
34 char *compute_alternate_path(const char *path, struct strbuf *err);
35 typedef int alt_odb_fn(struct object_directory *, void *);
36 int foreach_alt_odb(alt_odb_fn, void*);
37 typedef void alternate_ref_fn(const struct object_id *oid, void *);
38 void for_each_alternate_ref(alternate_ref_fn, void *);
39
40 /*
41 * Add the directory to the on-disk alternates file; the new entry will also
42 * take effect in the current process.
43 */
44 void add_to_alternates_file(const char *dir);
45
46 /*
47 * Add the directory to the in-memory list of alternates (along with any
48 * recursive alternates it points to), but do not modify the on-disk alternates
49 * file.
50 */
51 void add_to_alternates_memory(const char *dir);
52
53 /*
54 * Populate and return the loose object cache array corresponding to the
55 * given object ID.
56 */
57 struct oid_array *odb_loose_cache(struct object_directory *odb,
58 const struct object_id *oid);
59
60 /* Empty the loose object cache for the specified object directory. */
61 void odb_clear_loose_cache(struct object_directory *odb);
62
63 struct packed_git {
64 struct hashmap_entry packmap_ent;
65 struct packed_git *next;
66 struct list_head mru;
67 struct pack_window *windows;
68 off_t pack_size;
69 const void *index_data;
70 size_t index_size;
71 uint32_t num_objects;
72 uint32_t num_bad_objects;
73 unsigned char *bad_object_sha1;
74 int index_version;
75 time_t mtime;
76 int pack_fd;
77 int index; /* for builtin/pack-objects.c */
78 unsigned pack_local:1,
79 pack_keep:1,
80 pack_keep_in_core:1,
81 freshened:1,
82 do_not_close:1,
83 pack_promisor:1,
84 multi_pack_index:1;
85 unsigned char hash[GIT_MAX_RAWSZ];
86 struct revindex_entry *revindex;
87 /* something like ".git/objects/pack/xxxxx.pack" */
88 char pack_name[FLEX_ARRAY]; /* more */
89 };
90
91 struct multi_pack_index;
92
93 static inline int pack_map_entry_cmp(const void *unused_cmp_data,
94 const struct hashmap_entry *entry,
95 const struct hashmap_entry *entry2,
96 const void *keydata)
97 {
98 const char *key = keydata;
99 const struct packed_git *pg1, *pg2;
100
101 pg1 = container_of(entry, const struct packed_git, packmap_ent);
102 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
103
104 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
105 }
106
107 struct raw_object_store {
108 /*
109 * Set of all object directories; the main directory is first (and
110 * cannot be NULL after initialization). Subsequent directories are
111 * alternates.
112 */
113 struct object_directory *odb;
114 struct object_directory **odb_tail;
115 int loaded_alternates;
116
117 /*
118 * A list of alternate object directories loaded from the environment;
119 * this should not generally need to be accessed directly, but will
120 * populate the "odb" list when prepare_alt_odb() is run.
121 */
122 char *alternate_db;
123
124 /*
125 * Objects that should be substituted by other objects
126 * (see git-replace(1)).
127 */
128 struct oidmap *replace_map;
129 unsigned replace_map_initialized : 1;
130 pthread_mutex_t replace_mutex; /* protect object replace functions */
131
132 struct commit_graph *commit_graph;
133 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
134
135 /*
136 * private data
137 *
138 * should only be accessed directly by packfile.c and midx.c
139 */
140 struct multi_pack_index *multi_pack_index;
141
142 /*
143 * private data
144 *
145 * should only be accessed directly by packfile.c
146 */
147
148 struct packed_git *packed_git;
149 /* A most-recently-used ordered version of the packed_git list. */
150 struct list_head packed_git_mru;
151
152 /*
153 * A map of packfiles to packed_git structs for tracking which
154 * packs have been loaded already.
155 */
156 struct hashmap pack_map;
157
158 /*
159 * A fast, rough count of the number of objects in the repository.
160 * These two fields are not meant for direct access. Use
161 * approximate_object_count() instead.
162 */
163 unsigned long approximate_object_count;
164 unsigned approximate_object_count_valid : 1;
165
166 /*
167 * Whether packed_git has already been populated with this repository's
168 * packs.
169 */
170 unsigned packed_git_initialized : 1;
171 };
172
173 struct raw_object_store *raw_object_store_new(void);
174 void raw_object_store_clear(struct raw_object_store *o);
175
176 /*
177 * Put in `buf` the name of the file in the local object database that
178 * would be used to store a loose object with the specified oid.
179 */
180 const char *loose_object_path(struct repository *r, struct strbuf *buf,
181 const struct object_id *oid);
182
183 void *map_loose_object(struct repository *r, const struct object_id *oid,
184 unsigned long *size);
185
186 void *read_object_file_extended(struct repository *r,
187 const struct object_id *oid,
188 enum object_type *type,
189 unsigned long *size, int lookup_replace);
190 static inline void *repo_read_object_file(struct repository *r,
191 const struct object_id *oid,
192 enum object_type *type,
193 unsigned long *size)
194 {
195 return read_object_file_extended(r, oid, type, size, 1);
196 }
197 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
198 #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
199 #endif
200
201 /* Read and unpack an object file into memory, write memory to an object file */
202 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
203
204 int hash_object_file(const void *buf, unsigned long len,
205 const char *type, struct object_id *oid);
206
207 int write_object_file(const void *buf, unsigned long len,
208 const char *type, struct object_id *oid);
209
210 int hash_object_file_literally(const void *buf, unsigned long len,
211 const char *type, struct object_id *oid,
212 unsigned flags);
213
214 /*
215 * Add an object file to the in-memory object store, without writing it
216 * to disk.
217 *
218 * Callers are responsible for calling write_object_file to record the
219 * object in persistent storage before writing any other new objects
220 * that reference it.
221 */
222 int pretend_object_file(void *, unsigned long, enum object_type,
223 struct object_id *oid);
224
225 int force_object_loose(const struct object_id *oid, time_t mtime);
226
227 /*
228 * Open the loose object at path, check its hash, and return the contents,
229 * type, and size. If the object is a blob, then "contents" may return NULL,
230 * to allow streaming of large blobs.
231 *
232 * Returns 0 on success, negative on error (details may be written to stderr).
233 */
234 int read_loose_object(const char *path,
235 const struct object_id *expected_oid,
236 enum object_type *type,
237 unsigned long *size,
238 void **contents);
239
240 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
241 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
242 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
243 #endif
244
245 /* Same as the above, except for struct object_id. */
246 int repo_has_object_file(struct repository *r, const struct object_id *oid);
247 int repo_has_object_file_with_flags(struct repository *r,
248 const struct object_id *oid, int flags);
249 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
250 #define has_object_file(oid) repo_has_object_file(the_repository, oid)
251 #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
252 #endif
253
254 /*
255 * Return true iff an alternate object database has a loose object
256 * with the specified name. This function does not respect replace
257 * references.
258 */
259 int has_loose_object_nonlocal(const struct object_id *);
260
261 void assert_oid_type(const struct object_id *oid, enum object_type expect);
262
263 /*
264 * Enabling the object read lock allows multiple threads to safely call the
265 * following functions in parallel: repo_read_object_file(), read_object_file(),
266 * read_object_file_extended(), read_object_with_reference(), read_object(),
267 * oid_object_info() and oid_object_info_extended().
268 *
269 * obj_read_lock() and obj_read_unlock() may also be used to protect other
270 * section which cannot execute in parallel with object reading. Since the used
271 * lock is a recursive mutex, these sections can even contain calls to object
272 * reading functions. However, beware that in these cases zlib inflation won't
273 * be performed in parallel, losing performance.
274 *
275 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
276 * any of its callees end up calling it, this recursive call won't benefit from
277 * parallel inflation.
278 */
279 void enable_obj_read_lock(void);
280 void disable_obj_read_lock(void);
281
282 extern int obj_read_use_lock;
283 extern pthread_mutex_t obj_read_mutex;
284
285 static inline void obj_read_lock(void)
286 {
287 if(obj_read_use_lock)
288 pthread_mutex_lock(&obj_read_mutex);
289 }
290
291 static inline void obj_read_unlock(void)
292 {
293 if(obj_read_use_lock)
294 pthread_mutex_unlock(&obj_read_mutex);
295 }
296
297 struct object_info {
298 /* Request */
299 enum object_type *typep;
300 unsigned long *sizep;
301 off_t *disk_sizep;
302 unsigned char *delta_base_sha1;
303 struct strbuf *type_name;
304 void **contentp;
305
306 /* Response */
307 enum {
308 OI_CACHED,
309 OI_LOOSE,
310 OI_PACKED,
311 OI_DBCACHED
312 } whence;
313 union {
314 /*
315 * struct {
316 * ... Nothing to expose in this case
317 * } cached;
318 * struct {
319 * ... Nothing to expose in this case
320 * } loose;
321 */
322 struct {
323 struct packed_git *pack;
324 off_t offset;
325 unsigned int is_delta;
326 } packed;
327 } u;
328 };
329
330 /*
331 * Initializer for a "struct object_info" that wants no items. You may
332 * also memset() the memory to all-zeroes.
333 */
334 #define OBJECT_INFO_INIT {NULL}
335
336 /* Invoke lookup_replace_object() on the given hash */
337 #define OBJECT_INFO_LOOKUP_REPLACE 1
338 /* Allow reading from a loose object file of unknown/bogus type */
339 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
340 /* Do not retry packed storage after checking packed and loose storage */
341 #define OBJECT_INFO_QUICK 8
342 /* Do not check loose object */
343 #define OBJECT_INFO_IGNORE_LOOSE 16
344 /*
345 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
346 * nonzero).
347 */
348 #define OBJECT_INFO_SKIP_FETCH_OBJECT 32
349 /*
350 * This is meant for bulk prefetching of missing blobs in a partial
351 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
352 */
353 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
354
355 int oid_object_info_extended(struct repository *r,
356 const struct object_id *,
357 struct object_info *, unsigned flags);
358
359 /*
360 * Iterate over the files in the loose-object parts of the object
361 * directory "path", triggering the following callbacks:
362 *
363 * - loose_object is called for each loose object we find.
364 *
365 * - loose_cruft is called for any files that do not appear to be
366 * loose objects. Note that we only look in the loose object
367 * directories "objects/[0-9a-f]{2}/", so we will not report
368 * "objects/foobar" as cruft.
369 *
370 * - loose_subdir is called for each top-level hashed subdirectory
371 * of the object directory (e.g., "$OBJDIR/f0"). It is called
372 * after the objects in the directory are processed.
373 *
374 * Any callback that is NULL will be ignored. Callbacks returning non-zero
375 * will end the iteration.
376 *
377 * In the "buf" variant, "path" is a strbuf which will also be used as a
378 * scratch buffer, but restored to its original contents before
379 * the function returns.
380 */
381 typedef int each_loose_object_fn(const struct object_id *oid,
382 const char *path,
383 void *data);
384 typedef int each_loose_cruft_fn(const char *basename,
385 const char *path,
386 void *data);
387 typedef int each_loose_subdir_fn(unsigned int nr,
388 const char *path,
389 void *data);
390 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
391 struct strbuf *path,
392 each_loose_object_fn obj_cb,
393 each_loose_cruft_fn cruft_cb,
394 each_loose_subdir_fn subdir_cb,
395 void *data);
396 int for_each_loose_file_in_objdir(const char *path,
397 each_loose_object_fn obj_cb,
398 each_loose_cruft_fn cruft_cb,
399 each_loose_subdir_fn subdir_cb,
400 void *data);
401 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
402 each_loose_object_fn obj_cb,
403 each_loose_cruft_fn cruft_cb,
404 each_loose_subdir_fn subdir_cb,
405 void *data);
406
407 /* Flags for for_each_*_object() below. */
408 enum for_each_object_flags {
409 /* Iterate only over local objects, not alternates. */
410 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
411
412 /* Only iterate over packs obtained from the promisor remote. */
413 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
414
415 /*
416 * Visit objects within a pack in packfile order rather than .idx order
417 */
418 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
419 };
420
421 /*
422 * Iterate over all accessible loose objects without respect to
423 * reachability. By default, this includes both local and alternate objects.
424 * The order in which objects are visited is unspecified.
425 *
426 * Any flags specific to packs are ignored.
427 */
428 int for_each_loose_object(each_loose_object_fn, void *,
429 enum for_each_object_flags flags);
430
431 /*
432 * Iterate over all accessible packed objects without respect to reachability.
433 * By default, this includes both local and alternate packs.
434 *
435 * Note that some objects may appear twice if they are found in multiple packs.
436 * Each pack is visited in an unspecified order. By default, objects within a
437 * pack are visited in pack-idx order (i.e., sorted by oid).
438 */
439 typedef int each_packed_object_fn(const struct object_id *oid,
440 struct packed_git *pack,
441 uint32_t pos,
442 void *data);
443 int for_each_object_in_pack(struct packed_git *p,
444 each_packed_object_fn, void *data,
445 enum for_each_object_flags flags);
446 int for_each_packed_object(each_packed_object_fn, void *,
447 enum for_each_object_flags flags);
448
449 #endif /* OBJECT_STORE_H */