Git fork
1#ifndef OBJECT_FILE_H
2#define OBJECT_FILE_H
3
4#include "git-zlib.h"
5#include "object.h"
6#include "odb.h"
7
8struct index_state;
9
10/*
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.
13 *
14 * Its default value is 1.
15 */
16extern int fetch_if_missing;
17
18enum {
19 INDEX_WRITE_OBJECT = (1 << 0),
20 INDEX_FORMAT_CHECK = (1 << 1),
21 INDEX_RENORMALIZE = (1 << 2),
22};
23
24int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
25int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
26
27struct odb_source;
28
29/*
30 * Populate and return the loose object cache array corresponding to the
31 * given object ID.
32 */
33struct oidtree *odb_loose_cache(struct odb_source *source,
34 const struct object_id *oid);
35
36/* Empty the loose object cache for the specified object directory. */
37void odb_clear_loose_cache(struct odb_source *source);
38
39/*
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.
42 */
43const char *odb_loose_path(struct odb_source *source,
44 struct strbuf *buf,
45 const struct object_id *oid);
46
47/*
48 * Return true iff an object database source has a loose object
49 * with the specified name. This function does not respect replace
50 * references.
51 */
52int has_loose_object(struct odb_source *source,
53 const struct object_id *oid);
54
55void *map_loose_object(struct repository *r, const struct object_id *oid,
56 unsigned long *size);
57
58/*
59 * Iterate over the files in the loose-object parts of the object
60 * directory "path", triggering the following callbacks:
61 *
62 * - loose_object is called for each loose object we find.
63 *
64 * - loose_cruft is called for any files that do not appear to be
65 * loose objects. Note that we only look in the loose object
66 * directories "objects/[0-9a-f]{2}/", so we will not report
67 * "objects/foobar" as cruft.
68 *
69 * - loose_subdir is called for each top-level hashed subdirectory
70 * of the object directory (e.g., "$OBJDIR/f0"). It is called
71 * after the objects in the directory are processed.
72 *
73 * Any callback that is NULL will be ignored. Callbacks returning non-zero
74 * will end the iteration.
75 *
76 * In the "buf" variant, "path" is a strbuf which will also be used as a
77 * scratch buffer, but restored to its original contents before
78 * the function returns.
79 */
80typedef int each_loose_object_fn(const struct object_id *oid,
81 const char *path,
82 void *data);
83typedef int each_loose_cruft_fn(const char *basename,
84 const char *path,
85 void *data);
86typedef int each_loose_subdir_fn(unsigned int nr,
87 const char *path,
88 void *data);
89int for_each_loose_file_in_source(struct odb_source *source,
90 each_loose_object_fn obj_cb,
91 each_loose_cruft_fn cruft_cb,
92 each_loose_subdir_fn subdir_cb,
93 void *data);
94
95/*
96 * Iterate over all accessible loose objects without respect to
97 * reachability. By default, this includes both local and alternate objects.
98 * The order in which objects are visited is unspecified.
99 *
100 * Any flags specific to packs are ignored.
101 */
102int for_each_loose_object(struct object_database *odb,
103 each_loose_object_fn, void *,
104 enum for_each_object_flags flags);
105
106
107/**
108 * format_object_header() is a thin wrapper around s xsnprintf() that
109 * writes the initial "<type> <obj-len>" part of the loose object
110 * header. It returns the size that snprintf() returns + 1.
111 */
112int format_object_header(char *str, size_t size, enum object_type type,
113 size_t objsize);
114
115/**
116 * unpack_loose_header() initializes the data stream needed to unpack
117 * a loose object header.
118 *
119 * Returns:
120 *
121 * - ULHR_OK on success
122 * - ULHR_BAD on error
123 * - ULHR_TOO_LONG if the header was too long
124 *
125 * It will only parse up to MAX_HEADER_LEN bytes.
126 */
127enum unpack_loose_header_result {
128 ULHR_OK,
129 ULHR_BAD,
130 ULHR_TOO_LONG,
131};
132enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
133 unsigned char *map,
134 unsigned long mapsize,
135 void *buffer,
136 unsigned long bufsiz);
137
138/**
139 * parse_loose_header() parses the starting "<type> <len>\0" of an
140 * object. If it doesn't follow that format -1 is returned. To check
141 * the validity of the <type> populate the "typep" in the "struct
142 * object_info". It will be OBJ_BAD if the object type is unknown. The
143 * parsed <len> can be retrieved via "oi->sizep", and from there
144 * passed to unpack_loose_rest().
145 */
146struct object_info;
147int parse_loose_header(const char *hdr, struct object_info *oi);
148
149int write_object_file(struct odb_source *source,
150 const void *buf, unsigned long len,
151 enum object_type type, struct object_id *oid,
152 struct object_id *compat_oid_in, unsigned flags);
153
154struct input_stream {
155 const void *(*read)(struct input_stream *, unsigned long *len);
156 void *data;
157 int is_finished;
158};
159
160int stream_loose_object(struct odb_source *source,
161 struct input_stream *in_stream, size_t len,
162 struct object_id *oid);
163
164int force_object_loose(struct odb_source *source,
165 const struct object_id *oid, time_t mtime);
166
167/**
168 * With in-core object data in "buf", rehash it to make sure the
169 * object name actually matches "oid" to detect object corruption.
170 *
171 * A negative value indicates an error, usually that the OID is not
172 * what we expected, but it might also indicate another error.
173 */
174int check_object_signature(struct repository *r, const struct object_id *oid,
175 void *map, unsigned long size,
176 enum object_type type);
177
178/**
179 * A streaming version of check_object_signature().
180 * Try reading the object named with "oid" using
181 * the streaming interface and rehash it to do the same.
182 */
183int stream_object_signature(struct repository *r, const struct object_id *oid);
184
185int loose_object_info(struct repository *r,
186 const struct object_id *oid,
187 struct object_info *oi, int flags);
188
189enum finalize_object_file_flags {
190 FOF_SKIP_COLLISION_CHECK = 1,
191};
192
193int finalize_object_file(struct repository *repo,
194 const char *tmpfile, const char *filename);
195int finalize_object_file_flags(struct repository *repo,
196 const char *tmpfile, const char *filename,
197 enum finalize_object_file_flags flags);
198
199void hash_object_file(const struct git_hash_algo *algo, const void *buf,
200 unsigned long len, enum object_type type,
201 struct object_id *oid);
202
203/* Helper to check and "touch" a file */
204int check_and_freshen_file(const char *fn, int freshen);
205
206/*
207 * Open the loose object at path, check its hash, and return the contents,
208 * use the "oi" argument to assert things about the object, or e.g. populate its
209 * type, and size. If the object is a blob, then "contents" may return NULL,
210 * to allow streaming of large blobs.
211 *
212 * Returns 0 on success, negative on error (details may be written to stderr).
213 */
214int read_loose_object(struct repository *repo,
215 const char *path,
216 const struct object_id *expected_oid,
217 struct object_id *real_oid,
218 void **contents,
219 struct object_info *oi);
220
221struct odb_transaction;
222
223/*
224 * Tell the object database to optimize for adding
225 * multiple objects. object_file_transaction_commit must be called
226 * to make new objects visible. If a transaction is already
227 * pending, NULL is returned.
228 */
229struct odb_transaction *object_file_transaction_begin(struct odb_source *source);
230
231/*
232 * Tell the object database to make any objects from the
233 * current transaction visible.
234 */
235void object_file_transaction_commit(struct odb_transaction *transaction);
236
237#endif /* OBJECT_FILE_H */