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