at commit aae63be1

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