THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

451 lignes
14 KiB

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