at commit a0189536

1 #include "git-compat-util.h"
2 #include "object-file.h"
3 #include "odb.h"
4 #include "fsck.h"
5 #include "odb/source-inmemory.h"
6 #include "odb/streaming.h"
7 #include "oidtree.h"
8 #include "repository.h"
9
10 struct inmemory_object {
11 enum object_type type;
12 const void *buf;
13 unsigned long size;
14 };
15
16 static const struct inmemory_object *find_cached_object(struct odb_source_inmemory *source,
17 const struct object_id *oid)
18 {
19 static const struct inmemory_object empty_tree = {
20 .type = OBJ_TREE,
21 .buf = "",
22 };
23 const struct inmemory_object *object;
24
25 if (source->objects) {
26 object = oidtree_get(source->objects, oid);
27 if (object)
28 return object;
29 }
30
31 if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree))
32 return &empty_tree;
33
34 return NULL;
35 }
36
37 static void populate_object_info(struct odb_source_inmemory *source,
38 struct object_info *oi,
39 const struct inmemory_object *object)
40 {
41 if (!oi)
42 return;
43
44 if (oi->typep)
45 *(oi->typep) = object->type;
46 if (oi->sizep)
47 *(oi->sizep) = object->size;
48 if (oi->disk_sizep)
49 *(oi->disk_sizep) = 0;
50 if (oi->delta_base_oid)
51 oidclr(oi->delta_base_oid, source->base.odb->repo->hash_algo);
52 if (oi->contentp)
53 *oi->contentp = xmemdupz(object->buf, object->size);
54 if (oi->mtimep)
55 *oi->mtimep = 0;
56 if (oi->source_infop)
57 oi->source_infop->source = &source->base;
58 }
59
60 static enum odb_read_status odb_source_inmemory_read_object_info(struct odb_source *source,
61 const struct object_id *oid,
62 struct object_info *oi,
63 enum object_info_flags flags UNUSED,
64 struct strbuf *errmsg UNUSED)
65 {
66 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
67 const struct inmemory_object *object;
68
69 object = find_cached_object(inmemory, oid);
70 if (!object)
71 return ODB_READ_NOT_FOUND;
72
73 populate_object_info(inmemory, oi, object);
74 return 0;
75 }
76
77 struct odb_read_stream_inmemory {
78 struct odb_stream base;
79 const unsigned char *buf;
80 size_t offset;
81 };
82
83 static ssize_t odb_read_stream_inmemory_read(struct odb_stream *stream,
84 char *buf, size_t buf_len)
85 {
86 struct odb_read_stream_inmemory *inmemory =
87 container_of(stream, struct odb_read_stream_inmemory, base);
88 size_t bytes = buf_len;
89
90 if (buf_len > inmemory->base.size - inmemory->offset)
91 bytes = inmemory->base.size - inmemory->offset;
92
93 memcpy(buf, inmemory->buf + inmemory->offset, bytes);
94 inmemory->offset += bytes;
95
96 return bytes;
97 }
98
99 static int odb_read_stream_inmemory_close(struct odb_stream *stream UNUSED)
100 {
101 return 0;
102 }
103
104 static int odb_source_inmemory_read_object_stream(struct odb_stream **out,
105 struct odb_source *source,
106 const struct object_id *oid)
107 {
108 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
109 struct odb_read_stream_inmemory *stream;
110 const struct inmemory_object *object;
111
112 object = find_cached_object(inmemory, oid);
113 if (!object)
114 return -1;
115
116 CALLOC_ARRAY(stream, 1);
117 stream->base.read = odb_read_stream_inmemory_read;
118 stream->base.close = odb_read_stream_inmemory_close;
119 stream->base.size = object->size;
120 stream->base.type = object->type;
121 stream->buf = object->buf;
122
123 *out = &stream->base;
124 return 0;
125 }
126
127 struct odb_source_inmemory_for_each_object_data {
128 struct odb_source_inmemory *inmemory;
129 const struct object_info *request;
130 odb_for_each_object_cb cb;
131 void *cb_data;
132 };
133
134 static int odb_source_inmemory_for_each_object_cb(const struct object_id *oid,
135 void *node_data, void *cb_data)
136 {
137 struct odb_source_inmemory_for_each_object_data *data = cb_data;
138 struct inmemory_object *object = node_data;
139
140 if (data->request) {
141 struct object_info oi = *data->request;
142 populate_object_info(data->inmemory, &oi, object);
143 return data->cb(oid, &oi, data->cb_data);
144 } else {
145 return data->cb(oid, NULL, data->cb_data);
146 }
147 }
148
149 static int odb_source_inmemory_for_each_object(struct odb_source *source,
150 const struct object_info *request,
151 odb_for_each_object_cb cb,
152 void *cb_data,
153 const struct odb_for_each_object_options *opts)
154 {
155 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
156 struct odb_source_inmemory_for_each_object_data payload = {
157 .inmemory = inmemory,
158 .request = request,
159 .cb = cb,
160 .cb_data = cb_data,
161 };
162 struct object_id null_oid = { 0 };
163
164 if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) ||
165 (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local))
166 return 0;
167 if (!inmemory->objects)
168 return 0;
169
170 return oidtree_each(inmemory->objects,
171 opts->prefix ? opts->prefix : &null_oid, opts->prefix_hex_len,
172 odb_source_inmemory_for_each_object_cb, &payload);
173 }
174
175 struct find_abbrev_len_data {
176 const struct object_id *oid;
177 unsigned len;
178 };
179
180 static int find_abbrev_len_cb(const struct object_id *oid,
181 struct object_info *oi UNUSED,
182 void *cb_data)
183 {
184 struct find_abbrev_len_data *data = cb_data;
185 unsigned len = oid_common_prefix_hexlen(oid, data->oid);
186 if (len != hash_algos[oid->algo].hexsz && len >= data->len)
187 data->len = len + 1;
188 return 0;
189 }
190
191 static int odb_source_inmemory_find_abbrev_len(struct odb_source *source,
192 const struct object_id *oid,
193 unsigned min_len,
194 unsigned *out)
195 {
196 struct odb_for_each_object_options opts = {
197 .prefix = oid,
198 .prefix_hex_len = min_len,
199 };
200 struct find_abbrev_len_data data = {
201 .oid = oid,
202 .len = min_len,
203 };
204 int ret;
205
206 ret = odb_source_inmemory_for_each_object(source, NULL, find_abbrev_len_cb,
207 &data, &opts);
208 *out = data.len;
209
210 return ret;
211 }
212
213 static int count_objects_cb(const struct object_id *oid UNUSED,
214 struct object_info *oi UNUSED,
215 void *cb_data)
216 {
217 unsigned long *counter = cb_data;
218 (*counter)++;
219 return 0;
220 }
221
222 static int odb_source_inmemory_count_objects(struct odb_source *source,
223 enum odb_count_objects_flags flags UNUSED,
224 unsigned long *out)
225 {
226 struct odb_for_each_object_options opts = { 0 };
227 *out = 0;
228 return odb_source_inmemory_for_each_object(source, NULL, count_objects_cb,
229 out, &opts);
230 }
231
232 static int odb_source_inmemory_write_object(struct odb_source *source,
233 const void *buf, size_t len,
234 enum object_type type,
235 const struct object_id *oid,
236 const struct object_id *compat_oid UNUSED,
237 const time_t *mtime UNUSED,
238 enum odb_write_object_flags flags UNUSED)
239 {
240 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
241 struct inmemory_object *object;
242
243 if (!inmemory->objects) {
244 CALLOC_ARRAY(inmemory->objects, 1);
245 oidtree_init(inmemory->objects);
246 } else if (oidtree_contains(inmemory->objects, oid)) {
247 return 0;
248 }
249
250 CALLOC_ARRAY(object, 1);
251 object->size = len;
252 object->type = type;
253 object->buf = xmemdupz(buf, len);
254
255 oidtree_insert(inmemory->objects, oid, object);
256
257 return 0;
258 }
259
260 static int odb_source_inmemory_write_object_stream(struct odb_source *source,
261 struct odb_stream *stream,
262 struct object_id *oid)
263 {
264 char buf[16384];
265 size_t total_read = 0;
266 char *data;
267 int ret;
268
269 CALLOC_ARRAY(data, stream->size);
270 while (1) {
271 ssize_t bytes_read;
272
273 bytes_read = odb_stream_read(stream, buf, sizeof(buf));
274 if (bytes_read < 0) {
275 ret = error("failed to read object stream");
276 goto out;
277 }
278 if (!bytes_read)
279 break;
280
281 if (total_read + bytes_read > stream->size) {
282 ret = error("object stream yielded more bytes than expected");
283 goto out;
284 }
285
286 memcpy(data + total_read, buf, bytes_read);
287 total_read += bytes_read;
288 }
289
290 if (total_read != stream->size) {
291 ret = error("object stream yielded less bytes than expected");
292 goto out;
293 }
294
295 hash_object_file(source->odb->repo->hash_algo, data, total_read,
296 stream->type, oid);
297
298 ret = odb_source_inmemory_write_object(source, data, stream->size,
299 stream->type, oid, NULL, NULL, 0);
300 if (ret < 0)
301 goto out;
302
303 out:
304 free(data);
305 return ret;
306 }
307
308 static int odb_source_inmemory_freshen_object(struct odb_source *source,
309 const struct object_id *oid,
310 const time_t *mtime UNUSED)
311 {
312 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
313 if (find_cached_object(inmemory, oid))
314 return 1;
315 return 0;
316 }
317
318 static int odb_source_inmemory_begin_transaction(struct odb_source *source UNUSED,
319 struct odb_transaction **out UNUSED,
320 enum odb_transaction_flags flags UNUSED)
321 {
322 return error("in-memory source does not support transactions");
323 }
324
325 static int odb_source_inmemory_read_alternates(struct odb_source *source UNUSED,
326 struct strvec *out UNUSED)
327 {
328 return 0;
329 }
330
331 static int odb_source_inmemory_write_alternate(struct odb_source *source UNUSED,
332 const char *alternate UNUSED)
333 {
334 return error("in-memory source does not support alternates");
335 }
336
337 static void odb_source_inmemory_close(struct odb_source *source UNUSED)
338 {
339 }
340
341 static void odb_source_inmemory_prepare(struct odb_source *source UNUSED,
342 enum odb_prepare_flags flags UNUSED)
343 {
344 }
345
346 static int inmemory_object_free(const struct object_id *oid UNUSED,
347 void *node_data,
348 void *cb_data UNUSED)
349 {
350 struct inmemory_object *object = node_data;
351 free((void *) object->buf);
352 free(object);
353 return 0;
354 }
355
356 static void odb_source_inmemory_free(struct odb_source *source)
357 {
358 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
359
360 if (inmemory->objects) {
361 struct object_id null_oid = { 0 };
362
363 oidtree_each(inmemory->objects, &null_oid, 0,
364 inmemory_object_free, NULL);
365 oidtree_clear(inmemory->objects);
366 free(inmemory->objects);
367 }
368
369 free(inmemory->base.path);
370 free(inmemory);
371 }
372
373 static int odb_source_inmemory_fsck(struct odb_source *source UNUSED,
374 struct odb_fsck_options *opts UNUSED)
375 {
376 return 0;
377 }
378
379 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
380 {
381 struct odb_source_inmemory *source;
382
383 CALLOC_ARRAY(source, 1);
384 odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false);
385
386 source->base.free = odb_source_inmemory_free;
387 source->base.close = odb_source_inmemory_close;
388 source->base.prepare = odb_source_inmemory_prepare;
389 source->base.fsck = odb_source_inmemory_fsck;
390 source->base.read_object_info = odb_source_inmemory_read_object_info;
391 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
392 source->base.for_each_object = odb_source_inmemory_for_each_object;
393 source->base.find_abbrev_len = odb_source_inmemory_find_abbrev_len;
394 source->base.count_objects = odb_source_inmemory_count_objects;
395 source->base.write_object = odb_source_inmemory_write_object;
396 source->base.write_object_stream = odb_source_inmemory_write_object_stream;
397 source->base.freshen_object = odb_source_inmemory_freshen_object;
398 source->base.begin_transaction = odb_source_inmemory_begin_transaction;
399 source->base.read_alternates = odb_source_inmemory_read_alternates;
400 source->base.write_alternate = odb_source_inmemory_write_alternate;
401
402 return source;
403 }