Raw
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 #include "odb/source-loose.h"
8
9 /* The maximum size for an object header. */
10 #define MAX_HEADER_LEN 32
11
12 struct index_state;
13
14 enum {
15 INDEX_WRITE_OBJECT = (1 << 0),
16 INDEX_FORMAT_CHECK = (1 << 1),
17 INDEX_RENORMALIZE = (1 << 2),
18 };
19
20 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);
21 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
22
23 struct object_info;
24 struct odb_source;
25
26 /*
27 * Write the given stream into the loose object source. The only difference
28 * from the generic implementation of this function is that we don't perform an
29 * object existence check here.
30 *
31 * TODO: We should stop exposing this function altogether and move it into
32 * "odb/source-loose.c". This requires a couple of refactorings though to make
33 * `force_object_loose()` generic and is thus postponed to a later point in
34 * time.
35 */
36 int odb_source_loose_write_stream(struct odb_source_loose *source,
37 struct odb_write_stream *stream, size_t len,
38 struct object_id *oid);
39
40 /*
41 * Put in `buf` the name of the file in the local object database that
42 * would be used to store a loose object with the specified oid.
43 */
44 const char *odb_loose_path(struct odb_source_loose *source,
45 struct strbuf *buf,
46 const struct object_id *oid);
47
48 /*
49 * Iterate over the files in the loose-object parts of the object
50 * directory "path", triggering the following callbacks:
51 *
52 * - loose_object is called for each loose object we find.
53 *
54 * - loose_cruft is called for any files that do not appear to be
55 * loose objects. Note that we only look in the loose object
56 * directories "objects/[0-9a-f]{2}/", so we will not report
57 * "objects/foobar" as cruft.
58 *
59 * - loose_subdir is called for each top-level hashed subdirectory
60 * of the object directory (e.g., "$OBJDIR/f0"). It is called
61 * after the objects in the directory are processed.
62 *
63 * Any callback that is NULL will be ignored. Callbacks returning non-zero
64 * will end the iteration.
65 *
66 * In the "buf" variant, "path" is a strbuf which will also be used as a
67 * scratch buffer, but restored to its original contents before
68 * the function returns.
69 */
70 typedef int each_loose_object_fn(const struct object_id *oid,
71 const char *path,
72 void *data);
73 typedef int each_loose_cruft_fn(const char *basename,
74 const char *path,
75 void *data);
76 typedef int each_loose_subdir_fn(unsigned int nr,
77 const char *path,
78 void *data);
79 int for_each_loose_file_in_source(struct odb_source *source,
80 each_loose_object_fn obj_cb,
81 each_loose_cruft_fn cruft_cb,
82 each_loose_subdir_fn subdir_cb,
83 void *data);
84 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
85 struct strbuf *path,
86 const struct git_hash_algo *algop,
87 each_loose_object_fn obj_cb,
88 each_loose_cruft_fn cruft_cb,
89 each_loose_subdir_fn subdir_cb,
90 void *data);
91
92 /**
93 * format_object_header() is a thin wrapper around s xsnprintf() that
94 * writes the initial "<type> <obj-len>" part of the loose object
95 * header. It returns the size that snprintf() returns + 1.
96 */
97 int format_object_header(char *str, size_t size, enum object_type type,
98 size_t objsize);
99
100 int force_object_loose(struct odb_source *source,
101 const struct object_id *oid, time_t mtime);
102
103 /**
104 * With in-core object data in "buf", rehash it to make sure the
105 * object name actually matches "oid" to detect object corruption.
106 *
107 * A negative value indicates an error, usually that the OID is not
108 * what we expected, but it might also indicate another error.
109 */
110 int check_object_signature(struct repository *r, const struct object_id *oid,
111 void *map, unsigned long size,
112 enum object_type type);
113
114 /**
115 * A streaming version of check_object_signature().
116 * Try reading the object named with "oid" using
117 * the streaming interface and rehash it to do the same.
118 */
119 int stream_object_signature(struct repository *r,
120 struct odb_read_stream *stream,
121 const struct object_id *oid);
122
123 enum finalize_object_file_flags {
124 FOF_SKIP_COLLISION_CHECK = 1,
125 };
126
127 int finalize_object_file(struct repository *repo,
128 const char *tmpfile, const char *filename);
129 int finalize_object_file_flags(struct repository *repo,
130 const char *tmpfile, const char *filename,
131 enum finalize_object_file_flags flags);
132
133 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
134 unsigned long len, enum object_type type,
135 struct object_id *oid);
136 void write_object_file_prepare(const struct git_hash_algo *algo,
137 const void *buf, unsigned long len,
138 enum object_type type, struct object_id *oid,
139 char *hdr, int *hdrlen);
140 int write_loose_object(struct odb_source_loose *loose,
141 const struct object_id *oid, char *hdr,
142 int hdrlen, const void *buf, unsigned long len,
143 time_t mtime, unsigned flags);
144
145 /* Helper to check and "touch" a file */
146 int check_and_freshen_file(const char *fn, int freshen);
147
148 /*
149 * Open the loose object at path, check its hash, and return the contents,
150 * use the "oi" argument to assert things about the object, or e.g. populate its
151 * type, and size. If the object is a blob, then "contents" may return NULL,
152 * to allow streaming of large blobs.
153 *
154 * Returns 0 on success, negative on error (details may be written to stderr).
155 */
156 int read_loose_object(struct repository *repo,
157 const char *path,
158 const struct object_id *expected_oid,
159 struct object_id *real_oid,
160 void **contents,
161 struct object_info *oi);
162
163 enum unpack_loose_header_result {
164 ULHR_OK,
165 ULHR_BAD,
166 ULHR_TOO_LONG,
167 };
168
169 /**
170 * unpack_loose_header() initializes the data stream needed to unpack
171 * a loose object header.
172 *
173 * Returns:
174 *
175 * - ULHR_OK on success
176 * - ULHR_BAD on error
177 * - ULHR_TOO_LONG if the header was too long
178 *
179 * It will only parse up to MAX_HEADER_LEN bytes.
180 */
181 enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
182 unsigned char *map,
183 unsigned long mapsize,
184 void *buffer,
185 unsigned long bufsiz);
186 void *unpack_loose_rest(git_zstream *stream,
187 void *buffer, unsigned long size,
188 const struct object_id *oid);
189
190 int parse_loose_header(const char *hdr, struct object_info *oi);
191
192 struct odb_transaction;
193
194 /*
195 * Tell the object database to optimize for adding
196 * multiple objects. odb_transaction_files_commit must be called
197 * to make new objects visible. If a transaction is already
198 * pending, NULL is returned.
199 */
200 struct odb_transaction *odb_transaction_files_begin(struct odb_source *source);
201
202 #endif /* OBJECT_FILE_H */