]> git.ipfire.org Git - thirdparty/git.git/blame - object-store.h
object-store: provide helpers for loose_objects_cache
[thirdparty/git.git] / object-store.h
CommitLineData
90c62155
SB
1#ifndef OBJECT_STORE_H
2#define OBJECT_STORE_H
3
ef3ca954 4#include "cache.h"
d88f9fdf 5#include "oidmap.h"
14727b7f
JT
6#include "list.h"
7#include "sha1-array.h"
8#include "strbuf.h"
d88f9fdf 9
263db403
JK
10struct object_directory {
11 struct object_directory *next;
0d4a1321 12
0d4a1321 13 /*
3a2e0824
JK
14 * Used to store the results of readdir(3) calls when we are OK
15 * sacrificing accuracy due to races for speed. That includes
16 * our search for unique abbreviated hashes. Don't use it for tasks
17 * requiring greater accuracy!
18 *
19 * Be sure to call odb_load_loose_cache() before using.
0d4a1321
SB
20 */
21 char loose_objects_subdir_seen[256];
22 struct oid_array loose_objects_cache;
23
77f012e8
SB
24 /*
25 * Path to the alternative object store. If this is a relative path,
26 * it is relative to the current working directory.
27 */
f0eaf638 28 char *path;
031dc927 29};
f0eaf638 30
13068bf0 31void prepare_alt_odb(struct repository *r);
0d4a1321 32char *compute_alternate_path(const char *path, struct strbuf *err);
263db403 33typedef int alt_odb_fn(struct object_directory *, void *);
0d4a1321
SB
34int foreach_alt_odb(alt_odb_fn, void*);
35
0d4a1321
SB
36/*
37 * Add the directory to the on-disk alternates file; the new entry will also
38 * take effect in the current process.
39 */
40void add_to_alternates_file(const char *dir);
41
42/*
43 * Add the directory to the in-memory list of alternates (along with any
44 * recursive alternates it points to), but do not modify the on-disk alternates
45 * file.
46 */
47void add_to_alternates_memory(const char *dir);
48
3a2e0824
JK
49/*
50 * Populate an odb's loose object cache for one particular subdirectory (i.e.,
51 * the one that corresponds to the first byte of objects you're interested in,
52 * from 0 to 255 inclusive).
53 */
54void odb_load_loose_cache(struct object_directory *odb, int subdir_nr);
55
a80d72db
SB
56struct packed_git {
57 struct packed_git *next;
58 struct list_head mru;
59 struct pack_window *windows;
60 off_t pack_size;
61 const void *index_data;
62 size_t index_size;
63 uint32_t num_objects;
64 uint32_t num_bad_objects;
65 unsigned char *bad_object_sha1;
66 int index_version;
67 time_t mtime;
68 int pack_fd;
43fa44fa 69 int index; /* for builtin/pack-objects.c */
a80d72db
SB
70 unsigned pack_local:1,
71 pack_keep:1,
ed7e5fc3 72 pack_keep_in_core:1,
a80d72db
SB
73 freshened:1,
74 do_not_close:1,
75 pack_promisor:1;
76 unsigned char sha1[20];
77 struct revindex_entry *revindex;
78 /* something like ".git/objects/pack/xxxxx.pack" */
79 char pack_name[FLEX_ARRAY]; /* more */
80};
81
4d80560c
DS
82struct multi_pack_index;
83
90c62155
SB
84struct raw_object_store {
85 /*
f0eaf638
JK
86 * Set of all object directories; the main directory is first (and
87 * cannot be NULL after initialization). Subsequent directories are
88 * alternates.
90c62155 89 */
f0eaf638
JK
90 struct object_directory *odb;
91 struct object_directory **odb_tail;
92 int loaded_alternates;
90c62155 93
f0eaf638
JK
94 /*
95 * A list of alternate object directories loaded from the environment;
96 * this should not generally need to be accessed directly, but will
97 * populate the "odb" list when prepare_alt_odb() is run.
98 */
90c62155 99 char *alternate_db;
031dc927 100
d88f9fdf
SB
101 /*
102 * Objects that should be substituted by other objects
103 * (see git-replace(1)).
104 */
c1274495 105 struct oidmap *replace_map;
d88f9fdf 106
85277506
JT
107 struct commit_graph *commit_graph;
108 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
109
c4d25228
DS
110 /*
111 * private data
112 *
113 * should only be accessed directly by packfile.c and midx.c
114 */
115 struct multi_pack_index *multi_pack_index;
116
a80d72db
SB
117 /*
118 * private data
119 *
120 * should only be accessed directly by packfile.c
121 */
122
123 struct packed_git *packed_git;
124 /* A most-recently-used ordered version of the packed_git list. */
125 struct list_head packed_git_mru;
5508f693 126
0bff5269
DS
127 /*
128 * A linked list containing all packfiles, starting with those
129 * contained in the multi_pack_index.
130 */
131 struct packed_git *all_packs;
132
9a00580d
SB
133 /*
134 * A fast, rough count of the number of objects in the repository.
135 * These two fields are not meant for direct access. Use
136 * approximate_object_count() instead.
137 */
138 unsigned long approximate_object_count;
139 unsigned approximate_object_count_valid : 1;
140
5508f693
SB
141 /*
142 * Whether packed_git has already been populated with this repository's
143 * packs.
144 */
145 unsigned packed_git_initialized : 1;
90c62155
SB
146};
147
148struct raw_object_store *raw_object_store_new(void);
149void raw_object_store_clear(struct raw_object_store *o);
150
cf78ae4f
SB
151/*
152 * Put in `buf` the name of the file in the local object database that
153 * would be used to store a loose object with the specified sha1.
154 */
f3f043a1 155const char *loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
cf78ae4f 156
bd27f50c 157void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
e35454fa 158
cbd53a21
SB
159extern void *read_object_file_extended(const struct object_id *oid,
160 enum object_type *type,
161 unsigned long *size, int lookup_replace);
162static inline void *read_object_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
163{
164 return read_object_file_extended(oid, type, size, 1);
165}
166
167/* Read and unpack an object file into memory, write memory to an object file */
168int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
169
170extern int hash_object_file(const void *buf, unsigned long len,
171 const char *type, struct object_id *oid);
172
173extern int write_object_file(const void *buf, unsigned long len,
174 const char *type, struct object_id *oid);
175
176extern int hash_object_file_literally(const void *buf, unsigned long len,
177 const char *type, struct object_id *oid,
178 unsigned flags);
179
180extern int pretend_object_file(void *, unsigned long, enum object_type,
181 struct object_id *oid);
182
183extern int force_object_loose(const struct object_id *oid, time_t mtime);
184
185/*
186 * Open the loose object at path, check its hash, and return the contents,
187 * type, and size. If the object is a blob, then "contents" may return NULL,
188 * to allow streaming of large blobs.
189 *
190 * Returns 0 on success, negative on error (details may be written to stderr).
191 */
192int read_loose_object(const char *path,
193 const struct object_id *expected_oid,
194 enum object_type *type,
195 unsigned long *size,
196 void **contents);
197
198/*
199 * Convenience for sha1_object_info_extended() with a NULL struct
200 * object_info. OBJECT_INFO_SKIP_CACHED is automatically set; pass
201 * nonzero flags to also set other flags.
202 */
203extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags);
204static inline int has_sha1_file(const unsigned char *sha1)
205{
206 return has_sha1_file_with_flags(sha1, 0);
207}
208
209/* Same as the above, except for struct object_id. */
210extern int has_object_file(const struct object_id *oid);
211extern int has_object_file_with_flags(const struct object_id *oid, int flags);
212
213/*
214 * Return true iff an alternate object database has a loose object
215 * with the specified name. This function does not respect replace
216 * references.
217 */
b16b60f7 218extern int has_loose_object_nonlocal(const struct object_id *);
cbd53a21
SB
219
220extern void assert_oid_type(const struct object_id *oid, enum object_type expect);
221
222struct object_info {
223 /* Request */
224 enum object_type *typep;
225 unsigned long *sizep;
226 off_t *disk_sizep;
227 unsigned char *delta_base_sha1;
228 struct strbuf *type_name;
229 void **contentp;
230
231 /* Response */
232 enum {
233 OI_CACHED,
234 OI_LOOSE,
235 OI_PACKED,
236 OI_DBCACHED
237 } whence;
238 union {
239 /*
240 * struct {
241 * ... Nothing to expose in this case
242 * } cached;
243 * struct {
244 * ... Nothing to expose in this case
245 * } loose;
246 */
247 struct {
248 struct packed_git *pack;
249 off_t offset;
250 unsigned int is_delta;
251 } packed;
252 } u;
253};
254
255/*
256 * Initializer for a "struct object_info" that wants no items. You may
257 * also memset() the memory to all-zeroes.
258 */
259#define OBJECT_INFO_INIT {NULL}
260
261/* Invoke lookup_replace_object() on the given hash */
262#define OBJECT_INFO_LOOKUP_REPLACE 1
263/* Allow reading from a loose object file of unknown/bogus type */
264#define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
265/* Do not check cached storage */
266#define OBJECT_INFO_SKIP_CACHED 4
267/* Do not retry packed storage after checking packed and loose storage */
268#define OBJECT_INFO_QUICK 8
269/* Do not check loose object */
270#define OBJECT_INFO_IGNORE_LOOSE 16
271
272int oid_object_info_extended(struct repository *r,
273 const struct object_id *,
274 struct object_info *, unsigned flags);
275
0889aae1
JK
276/*
277 * Iterate over the files in the loose-object parts of the object
278 * directory "path", triggering the following callbacks:
279 *
280 * - loose_object is called for each loose object we find.
281 *
282 * - loose_cruft is called for any files that do not appear to be
283 * loose objects. Note that we only look in the loose object
284 * directories "objects/[0-9a-f]{2}/", so we will not report
285 * "objects/foobar" as cruft.
286 *
287 * - loose_subdir is called for each top-level hashed subdirectory
288 * of the object directory (e.g., "$OBJDIR/f0"). It is called
289 * after the objects in the directory are processed.
290 *
291 * Any callback that is NULL will be ignored. Callbacks returning non-zero
292 * will end the iteration.
293 *
294 * In the "buf" variant, "path" is a strbuf which will also be used as a
295 * scratch buffer, but restored to its original contents before
296 * the function returns.
297 */
298typedef int each_loose_object_fn(const struct object_id *oid,
299 const char *path,
300 void *data);
301typedef int each_loose_cruft_fn(const char *basename,
302 const char *path,
303 void *data);
304typedef int each_loose_subdir_fn(unsigned int nr,
305 const char *path,
306 void *data);
307int for_each_file_in_obj_subdir(unsigned int subdir_nr,
308 struct strbuf *path,
309 each_loose_object_fn obj_cb,
310 each_loose_cruft_fn cruft_cb,
311 each_loose_subdir_fn subdir_cb,
312 void *data);
313int for_each_loose_file_in_objdir(const char *path,
314 each_loose_object_fn obj_cb,
315 each_loose_cruft_fn cruft_cb,
316 each_loose_subdir_fn subdir_cb,
317 void *data);
318int for_each_loose_file_in_objdir_buf(struct strbuf *path,
319 each_loose_object_fn obj_cb,
320 each_loose_cruft_fn cruft_cb,
321 each_loose_subdir_fn subdir_cb,
322 void *data);
323
324/* Flags for for_each_*_object() below. */
325enum for_each_object_flags {
326 /* Iterate only over local objects, not alternates. */
327 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
328
329 /* Only iterate over packs obtained from the promisor remote. */
330 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
331
332 /*
333 * Visit objects within a pack in packfile order rather than .idx order
334 */
335 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
336};
337
338/*
339 * Iterate over all accessible loose objects without respect to
340 * reachability. By default, this includes both local and alternate objects.
341 * The order in which objects are visited is unspecified.
342 *
343 * Any flags specific to packs are ignored.
344 */
345int for_each_loose_object(each_loose_object_fn, void *,
346 enum for_each_object_flags flags);
347
348/*
349 * Iterate over all accessible packed objects without respect to reachability.
350 * By default, this includes both local and alternate packs.
351 *
352 * Note that some objects may appear twice if they are found in multiple packs.
353 * Each pack is visited in an unspecified order. By default, objects within a
354 * pack are visited in pack-idx order (i.e., sorted by oid).
355 */
356typedef int each_packed_object_fn(const struct object_id *oid,
357 struct packed_git *pack,
358 uint32_t pos,
359 void *data);
360int for_each_object_in_pack(struct packed_git *p,
361 each_packed_object_fn, void *data,
362 enum for_each_object_flags flags);
363int for_each_packed_object(each_packed_object_fn, void *,
364 enum for_each_object_flags flags);
365
90c62155 366#endif /* OBJECT_STORE_H */