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