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