The sixteenth batch
[git/gitster.git] / object-file.h
blob67b4ffc480833b0dbf1ac77760621d1b5661b740
1 #ifndef OBJECT_FILE_H
2 #define OBJECT_FILE_H
4 #include "git-zlib.h"
5 #include "object.h"
6 #include "odb.h"
8 struct index_state;
11 * Set this to 0 to prevent odb_read_object_info_extended() from fetching missing
12 * blobs. This has a difference only if extensions.partialClone is set.
14 * Its default value is 1.
16 extern int fetch_if_missing;
18 enum {
19 INDEX_WRITE_OBJECT = (1 << 0),
20 INDEX_FORMAT_CHECK = (1 << 1),
21 INDEX_RENORMALIZE = (1 << 2),
24 int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
25 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
27 struct odb_source;
30 * Populate and return the loose object cache array corresponding to the
31 * given object ID.
33 struct oidtree *odb_loose_cache(struct odb_source *source,
34 const struct object_id *oid);
36 /* Empty the loose object cache for the specified object directory. */
37 void odb_clear_loose_cache(struct odb_source *source);
40 * Put in `buf` the name of the file in the local object database that
41 * would be used to store a loose object with the specified oid.
43 const char *odb_loose_path(struct odb_source *source,
44 struct strbuf *buf,
45 const struct object_id *oid);
48 * Return true iff an alternate object database has a loose object
49 * with the specified name. This function does not respect replace
50 * references.
52 int has_loose_object_nonlocal(const struct object_id *);
54 int has_loose_object(const struct object_id *);
56 void *map_loose_object(struct repository *r, const struct object_id *oid,
57 unsigned long *size);
60 * Iterate over the files in the loose-object parts of the object
61 * directory "path", triggering the following callbacks:
63 * - loose_object is called for each loose object we find.
65 * - loose_cruft is called for any files that do not appear to be
66 * loose objects. Note that we only look in the loose object
67 * directories "objects/[0-9a-f]{2}/", so we will not report
68 * "objects/foobar" as cruft.
70 * - loose_subdir is called for each top-level hashed subdirectory
71 * of the object directory (e.g., "$OBJDIR/f0"). It is called
72 * after the objects in the directory are processed.
74 * Any callback that is NULL will be ignored. Callbacks returning non-zero
75 * will end the iteration.
77 * In the "buf" variant, "path" is a strbuf which will also be used as a
78 * scratch buffer, but restored to its original contents before
79 * the function returns.
81 typedef int each_loose_object_fn(const struct object_id *oid,
82 const char *path,
83 void *data);
84 typedef int each_loose_cruft_fn(const char *basename,
85 const char *path,
86 void *data);
87 typedef int each_loose_subdir_fn(unsigned int nr,
88 const char *path,
89 void *data);
90 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
91 struct strbuf *path,
92 each_loose_object_fn obj_cb,
93 each_loose_cruft_fn cruft_cb,
94 each_loose_subdir_fn subdir_cb,
95 void *data);
96 int for_each_loose_file_in_objdir(const char *path,
97 each_loose_object_fn obj_cb,
98 each_loose_cruft_fn cruft_cb,
99 each_loose_subdir_fn subdir_cb,
100 void *data);
101 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
102 each_loose_object_fn obj_cb,
103 each_loose_cruft_fn cruft_cb,
104 each_loose_subdir_fn subdir_cb,
105 void *data);
108 * Iterate over all accessible loose objects without respect to
109 * reachability. By default, this includes both local and alternate objects.
110 * The order in which objects are visited is unspecified.
112 * Any flags specific to packs are ignored.
114 int for_each_loose_object(each_loose_object_fn, void *,
115 enum for_each_object_flags flags);
119 * format_object_header() is a thin wrapper around s xsnprintf() that
120 * writes the initial "<type> <obj-len>" part of the loose object
121 * header. It returns the size that snprintf() returns + 1.
123 int format_object_header(char *str, size_t size, enum object_type type,
124 size_t objsize);
127 * unpack_loose_header() initializes the data stream needed to unpack
128 * a loose object header.
130 * Returns:
132 * - ULHR_OK on success
133 * - ULHR_BAD on error
134 * - ULHR_TOO_LONG if the header was too long
136 * It will only parse up to MAX_HEADER_LEN bytes.
138 enum unpack_loose_header_result {
139 ULHR_OK,
140 ULHR_BAD,
141 ULHR_TOO_LONG,
143 enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
144 unsigned char *map,
145 unsigned long mapsize,
146 void *buffer,
147 unsigned long bufsiz);
150 * parse_loose_header() parses the starting "<type> <len>\0" of an
151 * object. If it doesn't follow that format -1 is returned. To check
152 * the validity of the <type> populate the "typep" in the "struct
153 * object_info". It will be OBJ_BAD if the object type is unknown. The
154 * parsed <len> can be retrieved via "oi->sizep", and from there
155 * passed to unpack_loose_rest().
157 struct object_info;
158 int parse_loose_header(const char *hdr, struct object_info *oi);
160 enum {
162 * By default, `write_object_file()` does not actually write
163 * anything into the object store, but only computes the object ID.
164 * This flag changes that so that the object will be written as a loose
165 * object and persisted.
167 WRITE_OBJECT_FILE_PERSIST = (1 << 0),
170 * Do not print an error in case something gose wrong.
172 WRITE_OBJECT_FILE_SILENT = (1 << 1),
175 int write_object_file_flags(const void *buf, unsigned long len,
176 enum object_type type, struct object_id *oid,
177 struct object_id *compat_oid_in, unsigned flags);
178 static inline int write_object_file(const void *buf, unsigned long len,
179 enum object_type type, struct object_id *oid)
181 return write_object_file_flags(buf, len, type, oid, NULL, 0);
184 struct input_stream {
185 const void *(*read)(struct input_stream *, unsigned long *len);
186 void *data;
187 int is_finished;
190 int stream_loose_object(struct input_stream *in_stream, size_t len,
191 struct object_id *oid);
193 int force_object_loose(const struct object_id *oid, time_t mtime);
196 * With in-core object data in "buf", rehash it to make sure the
197 * object name actually matches "oid" to detect object corruption.
199 * A negative value indicates an error, usually that the OID is not
200 * what we expected, but it might also indicate another error.
202 int check_object_signature(struct repository *r, const struct object_id *oid,
203 void *map, unsigned long size,
204 enum object_type type);
207 * A streaming version of check_object_signature().
208 * Try reading the object named with "oid" using
209 * the streaming interface and rehash it to do the same.
211 int stream_object_signature(struct repository *r, const struct object_id *oid);
213 int loose_object_info(struct repository *r,
214 const struct object_id *oid,
215 struct object_info *oi, int flags);
217 enum finalize_object_file_flags {
218 FOF_SKIP_COLLISION_CHECK = 1,
221 int finalize_object_file(const char *tmpfile, const char *filename);
222 int finalize_object_file_flags(const char *tmpfile, const char *filename,
223 enum finalize_object_file_flags flags);
225 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
226 unsigned long len, enum object_type type,
227 struct object_id *oid);
229 /* Helper to check and "touch" a file */
230 int check_and_freshen_file(const char *fn, int freshen);
233 * Open the loose object at path, check its hash, and return the contents,
234 * use the "oi" argument to assert things about the object, or e.g. populate its
235 * type, and size. If the object is a blob, then "contents" may return NULL,
236 * to allow streaming of large blobs.
238 * Returns 0 on success, negative on error (details may be written to stderr).
240 int read_loose_object(const char *path,
241 const struct object_id *expected_oid,
242 struct object_id *real_oid,
243 void **contents,
244 struct object_info *oi);
246 #endif /* OBJECT_FILE_H */