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