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