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