/src/libgit2/deps/reftable/table.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020 Google LLC |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style |
5 | | * license that can be found in the LICENSE file or at |
6 | | * https://developers.google.com/open-source/licenses/bsd |
7 | | */ |
8 | | |
9 | | #include "table.h" |
10 | | |
11 | | #include "system.h" |
12 | | #include "block.h" |
13 | | #include "blocksource.h" |
14 | | #include "constants.h" |
15 | | #include "iter.h" |
16 | | #include "record.h" |
17 | | #include "reftable-error.h" |
18 | | |
19 | | static struct reftable_table_offsets * |
20 | | table_offsets_for(struct reftable_table *t, uint8_t typ) |
21 | 0 | { |
22 | 0 | switch (typ) { |
23 | 0 | case REFTABLE_BLOCK_TYPE_REF: |
24 | 0 | return &t->ref_offsets; |
25 | 0 | case REFTABLE_BLOCK_TYPE_LOG: |
26 | 0 | return &t->log_offsets; |
27 | 0 | case REFTABLE_BLOCK_TYPE_OBJ: |
28 | 0 | return &t->obj_offsets; |
29 | 0 | } |
30 | 0 | abort(); |
31 | 0 | } |
32 | | |
33 | | enum reftable_hash reftable_table_hash_id(struct reftable_table *t) |
34 | 0 | { |
35 | 0 | return t->hash_id; |
36 | 0 | } |
37 | | |
38 | | const char *reftable_table_name(struct reftable_table *t) |
39 | 0 | { |
40 | 0 | return t->name; |
41 | 0 | } |
42 | | |
43 | | static int parse_footer(struct reftable_table *t, uint8_t *footer, |
44 | | uint8_t *header) |
45 | 0 | { |
46 | 0 | uint8_t *f = footer; |
47 | 0 | uint8_t first_block_typ; |
48 | 0 | int err = 0; |
49 | 0 | uint32_t computed_crc; |
50 | 0 | uint32_t file_crc; |
51 | |
|
52 | 0 | if (memcmp(f, "REFT", 4)) { |
53 | 0 | err = REFTABLE_FORMAT_ERROR; |
54 | 0 | goto done; |
55 | 0 | } |
56 | 0 | f += 4; |
57 | |
|
58 | 0 | if (memcmp(footer, header, header_size(t->version))) { |
59 | 0 | err = REFTABLE_FORMAT_ERROR; |
60 | 0 | goto done; |
61 | 0 | } |
62 | | |
63 | 0 | f++; |
64 | 0 | t->block_size = reftable_get_be24(f); |
65 | |
|
66 | 0 | f += 3; |
67 | 0 | t->min_update_index = reftable_get_be64(f); |
68 | 0 | f += 8; |
69 | 0 | t->max_update_index = reftable_get_be64(f); |
70 | 0 | f += 8; |
71 | |
|
72 | 0 | if (t->version == 1) { |
73 | 0 | t->hash_id = REFTABLE_HASH_SHA1; |
74 | 0 | } else { |
75 | 0 | switch (reftable_get_be32(f)) { |
76 | 0 | case REFTABLE_FORMAT_ID_SHA1: |
77 | 0 | t->hash_id = REFTABLE_HASH_SHA1; |
78 | 0 | break; |
79 | 0 | case REFTABLE_FORMAT_ID_SHA256: |
80 | 0 | t->hash_id = REFTABLE_HASH_SHA256; |
81 | 0 | break; |
82 | 0 | default: |
83 | 0 | err = REFTABLE_FORMAT_ERROR; |
84 | 0 | goto done; |
85 | 0 | } |
86 | | |
87 | 0 | f += 4; |
88 | 0 | } |
89 | | |
90 | 0 | t->ref_offsets.index_offset = reftable_get_be64(f); |
91 | 0 | f += 8; |
92 | |
|
93 | 0 | t->obj_offsets.offset = reftable_get_be64(f); |
94 | 0 | f += 8; |
95 | |
|
96 | 0 | t->object_id_len = t->obj_offsets.offset & ((1 << 5) - 1); |
97 | 0 | t->obj_offsets.offset >>= 5; |
98 | |
|
99 | 0 | t->obj_offsets.index_offset = reftable_get_be64(f); |
100 | 0 | f += 8; |
101 | 0 | t->log_offsets.offset = reftable_get_be64(f); |
102 | 0 | f += 8; |
103 | 0 | t->log_offsets.index_offset = reftable_get_be64(f); |
104 | 0 | f += 8; |
105 | |
|
106 | 0 | computed_crc = crc32(0, footer, f - footer); |
107 | 0 | file_crc = reftable_get_be32(f); |
108 | 0 | f += 4; |
109 | 0 | if (computed_crc != file_crc) { |
110 | 0 | err = REFTABLE_FORMAT_ERROR; |
111 | 0 | goto done; |
112 | 0 | } |
113 | | |
114 | 0 | first_block_typ = header[header_size(t->version)]; |
115 | 0 | t->ref_offsets.is_present = (first_block_typ == REFTABLE_BLOCK_TYPE_REF); |
116 | 0 | t->ref_offsets.offset = 0; |
117 | 0 | t->log_offsets.is_present = (first_block_typ == REFTABLE_BLOCK_TYPE_LOG || |
118 | 0 | t->log_offsets.offset > 0); |
119 | 0 | t->obj_offsets.is_present = t->obj_offsets.offset > 0; |
120 | 0 | if (t->obj_offsets.is_present && !t->object_id_len) { |
121 | 0 | err = REFTABLE_FORMAT_ERROR; |
122 | 0 | goto done; |
123 | 0 | } |
124 | | |
125 | 0 | err = 0; |
126 | 0 | done: |
127 | 0 | return err; |
128 | 0 | } |
129 | | |
130 | | struct table_iter { |
131 | | struct reftable_table *table; |
132 | | uint8_t typ; |
133 | | uint64_t block_off; |
134 | | struct reftable_block block; |
135 | | struct block_iter bi; |
136 | | int is_finished; |
137 | | }; |
138 | | |
139 | | static int table_iter_init(struct table_iter *ti, struct reftable_table *t) |
140 | 0 | { |
141 | 0 | struct block_iter bi = BLOCK_ITER_INIT; |
142 | 0 | memset(ti, 0, sizeof(*ti)); |
143 | 0 | reftable_table_incref(t); |
144 | 0 | ti->table = t; |
145 | 0 | ti->bi = bi; |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | | static int table_iter_next_in_block(struct table_iter *ti, |
150 | | struct reftable_record *rec) |
151 | 0 | { |
152 | 0 | int res = block_iter_next(&ti->bi, rec); |
153 | 0 | if (res == 0 && reftable_record_type(rec) == REFTABLE_BLOCK_TYPE_REF) { |
154 | 0 | rec->u.ref.update_index += ti->table->min_update_index; |
155 | 0 | } |
156 | |
|
157 | 0 | return res; |
158 | 0 | } |
159 | | |
160 | | static void table_iter_block_done(struct table_iter *ti) |
161 | 0 | { |
162 | 0 | reftable_block_release(&ti->block); |
163 | 0 | block_iter_reset(&ti->bi); |
164 | 0 | } |
165 | | |
166 | | int table_init_block(struct reftable_table *t, struct reftable_block *block, |
167 | | uint64_t next_off, uint8_t want_typ) |
168 | 0 | { |
169 | 0 | uint32_t header_off = next_off ? 0 : header_size(t->version); |
170 | 0 | int err; |
171 | |
|
172 | 0 | if (next_off >= t->size) |
173 | 0 | return 1; |
174 | | |
175 | 0 | err = reftable_block_init(block, &t->source, next_off, header_off, |
176 | 0 | t->block_size, hash_size(t->hash_id), want_typ); |
177 | 0 | if (err) |
178 | 0 | reftable_block_release(block); |
179 | 0 | return err; |
180 | 0 | } |
181 | | |
182 | | static void table_iter_close(struct table_iter *ti) |
183 | 0 | { |
184 | 0 | table_iter_block_done(ti); |
185 | 0 | block_iter_close(&ti->bi); |
186 | 0 | reftable_table_decref(ti->table); |
187 | 0 | } |
188 | | |
189 | | static int table_iter_next_block(struct table_iter *ti) |
190 | 0 | { |
191 | 0 | uint64_t next_block_off = ti->block_off + ti->block.full_block_size; |
192 | 0 | int err; |
193 | |
|
194 | 0 | err = table_init_block(ti->table, &ti->block, next_block_off, ti->typ); |
195 | 0 | if (err > 0) |
196 | 0 | ti->is_finished = 1; |
197 | 0 | if (err) |
198 | 0 | return err; |
199 | | |
200 | 0 | ti->block_off = next_block_off; |
201 | 0 | ti->is_finished = 0; |
202 | 0 | block_iter_init(&ti->bi, &ti->block); |
203 | |
|
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | | static int table_iter_next(struct table_iter *ti, struct reftable_record *rec) |
208 | 0 | { |
209 | 0 | if (reftable_record_type(rec) != ti->typ) |
210 | 0 | return REFTABLE_API_ERROR; |
211 | | |
212 | 0 | while (1) { |
213 | 0 | int err; |
214 | |
|
215 | 0 | if (ti->is_finished) |
216 | 0 | return 1; |
217 | | |
218 | | /* |
219 | | * Check whether the current block still has more records. If |
220 | | * so, return it. If the iterator returns positive then the |
221 | | * current block has been exhausted. |
222 | | */ |
223 | 0 | err = table_iter_next_in_block(ti, rec); |
224 | 0 | if (err <= 0) |
225 | 0 | return err; |
226 | | |
227 | | /* |
228 | | * Otherwise, we need to continue to the next block in the |
229 | | * table and retry. If there are no more blocks then the |
230 | | * iterator is drained. |
231 | | */ |
232 | 0 | err = table_iter_next_block(ti); |
233 | 0 | if (err) { |
234 | 0 | ti->is_finished = 1; |
235 | 0 | return err; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | | static int table_iter_seek_to(struct table_iter *ti, uint64_t off, uint8_t typ) |
241 | 0 | { |
242 | 0 | int err; |
243 | |
|
244 | 0 | err = table_init_block(ti->table, &ti->block, off, typ); |
245 | 0 | if (err > 0) |
246 | 0 | return REFTABLE_FORMAT_ERROR; |
247 | 0 | if (err != 0) |
248 | 0 | return err; |
249 | | |
250 | 0 | ti->typ = reftable_block_type(&ti->block); |
251 | 0 | ti->block_off = off; |
252 | 0 | block_iter_init(&ti->bi, &ti->block); |
253 | 0 | ti->is_finished = 0; |
254 | 0 | return 0; |
255 | 0 | } |
256 | | |
257 | | static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index) |
258 | 0 | { |
259 | 0 | struct reftable_table_offsets *offs = table_offsets_for(ti->table, typ); |
260 | 0 | uint64_t off = offs->offset; |
261 | 0 | if (index) { |
262 | 0 | off = offs->index_offset; |
263 | 0 | if (off == 0) { |
264 | 0 | return 1; |
265 | 0 | } |
266 | 0 | typ = REFTABLE_BLOCK_TYPE_INDEX; |
267 | 0 | } |
268 | | |
269 | 0 | return table_iter_seek_to(ti, off, typ); |
270 | 0 | } |
271 | | |
272 | | static int table_iter_seek_linear(struct table_iter *ti, |
273 | | struct reftable_record *want) |
274 | 0 | { |
275 | 0 | struct reftable_buf want_key = REFTABLE_BUF_INIT; |
276 | 0 | struct reftable_buf got_key = REFTABLE_BUF_INIT; |
277 | 0 | struct reftable_record rec; |
278 | 0 | int err; |
279 | |
|
280 | 0 | err = reftable_record_init(&rec, reftable_record_type(want)); |
281 | 0 | if (err < 0) |
282 | 0 | goto done; |
283 | | |
284 | 0 | err = reftable_record_key(want, &want_key); |
285 | 0 | if (err < 0) |
286 | 0 | goto done; |
287 | | |
288 | | /* |
289 | | * First we need to locate the block that must contain our record. To |
290 | | * do so we scan through blocks linearly until we find the first block |
291 | | * whose first key is bigger than our wanted key. Once we have found |
292 | | * that block we know that the key must be contained in the preceding |
293 | | * block. |
294 | | * |
295 | | * This algorithm is somewhat unfortunate because it means that we |
296 | | * always have to seek one block too far and then back up. But as we |
297 | | * can only decode the _first_ key of a block but not its _last_ key we |
298 | | * have no other way to do this. |
299 | | */ |
300 | 0 | while (1) { |
301 | 0 | struct table_iter next = *ti; |
302 | | |
303 | | /* |
304 | | * We must be careful to not modify underlying data of `ti` |
305 | | * because we may find that `next` does not contain our desired |
306 | | * block, but that `ti` does. In that case, we would discard |
307 | | * `next` and continue with `ti`. |
308 | | * |
309 | | * This also means that we cannot reuse allocated memory for |
310 | | * `next` here. While it would be great if we could, it should |
311 | | * in practice not be too bad given that we should only ever |
312 | | * end up doing linear seeks with at most three blocks. As soon |
313 | | * as we have more than three blocks we would have an index, so |
314 | | * we would not do a linear search there anymore. |
315 | | */ |
316 | 0 | memset(&next.block.block_data, 0, sizeof(next.block.block_data)); |
317 | 0 | next.block.zstream = NULL; |
318 | 0 | next.block.uncompressed_data = NULL; |
319 | 0 | next.block.uncompressed_cap = 0; |
320 | |
|
321 | 0 | err = table_iter_next_block(&next); |
322 | 0 | if (err < 0) |
323 | 0 | goto done; |
324 | 0 | if (err > 0) |
325 | 0 | break; |
326 | | |
327 | 0 | err = reftable_block_first_key(&next.block, &got_key); |
328 | 0 | if (err < 0) |
329 | 0 | goto done; |
330 | | |
331 | 0 | if (reftable_buf_cmp(&got_key, &want_key) > 0) { |
332 | 0 | table_iter_block_done(&next); |
333 | 0 | break; |
334 | 0 | } |
335 | | |
336 | 0 | table_iter_block_done(ti); |
337 | 0 | *ti = next; |
338 | 0 | } |
339 | | |
340 | | /* |
341 | | * We have located the block that must contain our record, so we seek |
342 | | * the wanted key inside of it. If the block does not contain our key |
343 | | * we know that the corresponding record does not exist. |
344 | | */ |
345 | 0 | block_iter_init(&ti->bi, &ti->block); |
346 | 0 | err = block_iter_seek_key(&ti->bi, &want_key); |
347 | 0 | if (err < 0) |
348 | 0 | goto done; |
349 | 0 | err = 0; |
350 | |
|
351 | 0 | done: |
352 | 0 | reftable_record_release(&rec); |
353 | 0 | reftable_buf_release(&want_key); |
354 | 0 | reftable_buf_release(&got_key); |
355 | 0 | return err; |
356 | 0 | } |
357 | | |
358 | | static int table_iter_seek_indexed(struct table_iter *ti, |
359 | | struct reftable_record *rec) |
360 | 0 | { |
361 | 0 | struct reftable_record want_index = { |
362 | 0 | .type = REFTABLE_BLOCK_TYPE_INDEX, .u.idx = { .last_key = REFTABLE_BUF_INIT } |
363 | 0 | }; |
364 | 0 | struct reftable_record index_result = { |
365 | 0 | .type = REFTABLE_BLOCK_TYPE_INDEX, |
366 | 0 | .u.idx = { .last_key = REFTABLE_BUF_INIT }, |
367 | 0 | }; |
368 | 0 | int err; |
369 | |
|
370 | 0 | err = reftable_record_key(rec, &want_index.u.idx.last_key); |
371 | 0 | if (err < 0) |
372 | 0 | goto done; |
373 | | |
374 | | /* |
375 | | * The index may consist of multiple levels, where each level may have |
376 | | * multiple index blocks. We start by doing a linear search in the |
377 | | * highest layer that identifies the relevant index block as well as |
378 | | * the record inside that block that corresponds to our wanted key. |
379 | | */ |
380 | 0 | err = table_iter_seek_linear(ti, &want_index); |
381 | 0 | if (err < 0) |
382 | 0 | goto done; |
383 | | |
384 | | /* |
385 | | * Traverse down the levels until we find a non-index entry. |
386 | | */ |
387 | 0 | while (1) { |
388 | | /* |
389 | | * In case we seek a record that does not exist the index iter |
390 | | * will tell us that the iterator is over. This works because |
391 | | * the last index entry of the current level will contain the |
392 | | * last key it knows about. So in case our seeked key is larger |
393 | | * than the last indexed key we know that it won't exist. |
394 | | * |
395 | | * There is one subtlety in the layout of the index section |
396 | | * that makes this work as expected: the highest-level index is |
397 | | * at end of the section and will point backwards and thus we |
398 | | * start reading from the end of the index section, not the |
399 | | * beginning. |
400 | | * |
401 | | * If that wasn't the case and the order was reversed then the |
402 | | * linear seek would seek into the lower levels and traverse |
403 | | * all levels of the index only to find out that the key does |
404 | | * not exist. |
405 | | */ |
406 | 0 | err = table_iter_next(ti, &index_result); |
407 | 0 | if (err != 0) |
408 | 0 | goto done; |
409 | | |
410 | 0 | err = table_iter_seek_to(ti, index_result.u.idx.offset, 0); |
411 | 0 | if (err != 0) |
412 | 0 | goto done; |
413 | | |
414 | 0 | block_iter_init(&ti->bi, &ti->block); |
415 | |
|
416 | 0 | err = block_iter_seek_key(&ti->bi, &want_index.u.idx.last_key); |
417 | 0 | if (err < 0) |
418 | 0 | goto done; |
419 | | |
420 | 0 | if (ti->typ == reftable_record_type(rec)) { |
421 | 0 | err = 0; |
422 | 0 | break; |
423 | 0 | } |
424 | | |
425 | 0 | if (ti->typ != REFTABLE_BLOCK_TYPE_INDEX) { |
426 | 0 | err = REFTABLE_FORMAT_ERROR; |
427 | 0 | goto done; |
428 | 0 | } |
429 | 0 | } |
430 | | |
431 | 0 | done: |
432 | 0 | reftable_record_release(&want_index); |
433 | 0 | reftable_record_release(&index_result); |
434 | 0 | return err; |
435 | 0 | } |
436 | | |
437 | | static int table_iter_seek(struct table_iter *ti, |
438 | | struct reftable_record *want) |
439 | 0 | { |
440 | 0 | uint8_t typ = reftable_record_type(want); |
441 | 0 | struct reftable_table_offsets *offs = table_offsets_for(ti->table, typ); |
442 | 0 | int err; |
443 | |
|
444 | 0 | err = table_iter_seek_start(ti, reftable_record_type(want), |
445 | 0 | !!offs->index_offset); |
446 | 0 | if (err < 0) |
447 | 0 | goto out; |
448 | | |
449 | 0 | if (offs->index_offset) |
450 | 0 | err = table_iter_seek_indexed(ti, want); |
451 | 0 | else |
452 | 0 | err = table_iter_seek_linear(ti, want); |
453 | 0 | if (err) |
454 | 0 | goto out; |
455 | | |
456 | 0 | out: |
457 | 0 | return err; |
458 | 0 | } |
459 | | |
460 | | static int table_iter_seek_void(void *ti, struct reftable_record *want) |
461 | 0 | { |
462 | 0 | return table_iter_seek(ti, want); |
463 | 0 | } |
464 | | |
465 | | static int table_iter_next_void(void *ti, struct reftable_record *rec) |
466 | 0 | { |
467 | 0 | return table_iter_next(ti, rec); |
468 | 0 | } |
469 | | |
470 | | static void table_iter_close_void(void *ti) |
471 | 0 | { |
472 | 0 | table_iter_close(ti); |
473 | 0 | } |
474 | | |
475 | | static struct reftable_iterator_vtable table_iter_vtable = { |
476 | | .seek = &table_iter_seek_void, |
477 | | .next = &table_iter_next_void, |
478 | | .close = &table_iter_close_void, |
479 | | }; |
480 | | |
481 | | static void iterator_from_table_iter(struct reftable_iterator *it, |
482 | | struct table_iter *ti) |
483 | 0 | { |
484 | 0 | assert(!it->ops); |
485 | 0 | it->iter_arg = ti; |
486 | 0 | it->ops = &table_iter_vtable; |
487 | 0 | } |
488 | | |
489 | | int table_init_iter(struct reftable_table *t, |
490 | | struct reftable_iterator *it, |
491 | | uint8_t typ) |
492 | 0 | { |
493 | 0 | struct reftable_table_offsets *offs = table_offsets_for(t, typ); |
494 | |
|
495 | 0 | if (offs->is_present) { |
496 | 0 | struct table_iter *ti; |
497 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
498 | 0 | if (!ti) |
499 | 0 | return REFTABLE_OUT_OF_MEMORY_ERROR; |
500 | | |
501 | 0 | table_iter_init(ti, t); |
502 | 0 | iterator_from_table_iter(it, ti); |
503 | 0 | } else { |
504 | 0 | iterator_set_empty(it); |
505 | 0 | } |
506 | | |
507 | 0 | return 0; |
508 | 0 | } |
509 | | |
510 | | int reftable_table_init_ref_iterator(struct reftable_table *t, |
511 | | struct reftable_iterator *it) |
512 | 0 | { |
513 | 0 | return table_init_iter(t, it, REFTABLE_BLOCK_TYPE_REF); |
514 | 0 | } |
515 | | |
516 | | int reftable_table_init_log_iterator(struct reftable_table *t, |
517 | | struct reftable_iterator *it) |
518 | 0 | { |
519 | 0 | return table_init_iter(t, it, REFTABLE_BLOCK_TYPE_LOG); |
520 | 0 | } |
521 | | |
522 | | int reftable_table_new(struct reftable_table **out, |
523 | | struct reftable_block_source *source, char const *name) |
524 | 0 | { |
525 | 0 | struct reftable_block_data footer = { 0 }; |
526 | 0 | struct reftable_block_data header = { 0 }; |
527 | 0 | struct reftable_table *t; |
528 | 0 | uint64_t file_size = block_source_size(source); |
529 | 0 | uint32_t read_size; |
530 | 0 | ssize_t bytes_read; |
531 | 0 | int err; |
532 | |
|
533 | 0 | REFTABLE_CALLOC_ARRAY(t, 1); |
534 | 0 | if (!t) { |
535 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
536 | 0 | goto done; |
537 | 0 | } |
538 | | |
539 | | /* |
540 | | * We need one extra byte to read the type of first block. We also |
541 | | * pretend to always be reading v2 of the format because it is larger. |
542 | | */ |
543 | 0 | read_size = header_size(2) + 1; |
544 | 0 | if (read_size > file_size) { |
545 | 0 | err = REFTABLE_FORMAT_ERROR; |
546 | 0 | goto done; |
547 | 0 | } |
548 | | |
549 | 0 | bytes_read = block_source_read_data(source, &header, 0, read_size); |
550 | 0 | if (bytes_read < 0 || (size_t)bytes_read != read_size) { |
551 | 0 | err = REFTABLE_IO_ERROR; |
552 | 0 | goto done; |
553 | 0 | } |
554 | | |
555 | 0 | if (memcmp(header.data, "REFT", 4)) { |
556 | 0 | err = REFTABLE_FORMAT_ERROR; |
557 | 0 | goto done; |
558 | 0 | } |
559 | 0 | t->version = header.data[4]; |
560 | 0 | if (t->version != 1 && t->version != 2) { |
561 | 0 | err = REFTABLE_FORMAT_ERROR; |
562 | 0 | goto done; |
563 | 0 | } |
564 | | |
565 | 0 | if (file_size < header_size(t->version) + footer_size(t->version)) { |
566 | 0 | err = REFTABLE_FORMAT_ERROR; |
567 | 0 | goto done; |
568 | 0 | } |
569 | | |
570 | 0 | t->size = file_size - footer_size(t->version); |
571 | 0 | t->source = *source; |
572 | 0 | t->name = reftable_strdup(name); |
573 | 0 | if (!t->name) { |
574 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
575 | 0 | goto done; |
576 | 0 | } |
577 | 0 | t->hash_id = 0; |
578 | 0 | t->refcount = 1; |
579 | |
|
580 | 0 | bytes_read = block_source_read_data(source, &footer, t->size, |
581 | 0 | footer_size(t->version)); |
582 | 0 | if (bytes_read < 0 || (size_t)bytes_read != footer_size(t->version)) { |
583 | 0 | err = REFTABLE_IO_ERROR; |
584 | 0 | goto done; |
585 | 0 | } |
586 | | |
587 | 0 | err = parse_footer(t, footer.data, header.data); |
588 | 0 | if (err) |
589 | 0 | goto done; |
590 | | |
591 | 0 | *out = t; |
592 | |
|
593 | 0 | done: |
594 | 0 | block_source_release_data(&footer); |
595 | 0 | block_source_release_data(&header); |
596 | 0 | if (err) { |
597 | 0 | if (t) |
598 | 0 | reftable_free(t->name); |
599 | 0 | reftable_free(t); |
600 | 0 | block_source_close(source); |
601 | 0 | } |
602 | 0 | return err; |
603 | 0 | } |
604 | | |
605 | | void reftable_table_incref(struct reftable_table *t) |
606 | 0 | { |
607 | 0 | t->refcount++; |
608 | 0 | } |
609 | | |
610 | | void reftable_table_decref(struct reftable_table *t) |
611 | 0 | { |
612 | 0 | if (!t) |
613 | 0 | return; |
614 | 0 | if (--t->refcount) |
615 | 0 | return; |
616 | 0 | block_source_close(&t->source); |
617 | 0 | REFTABLE_FREE_AND_NULL(t->name); |
618 | 0 | reftable_free(t); |
619 | 0 | } |
620 | | |
621 | | static int reftable_table_refs_for_indexed(struct reftable_table *t, |
622 | | struct reftable_iterator *it, |
623 | | uint8_t *oid) |
624 | 0 | { |
625 | 0 | struct reftable_record want = { |
626 | 0 | .type = REFTABLE_BLOCK_TYPE_OBJ, |
627 | 0 | .u.obj = { |
628 | 0 | .hash_prefix = oid, |
629 | 0 | .hash_prefix_len = t->object_id_len, |
630 | 0 | }, |
631 | 0 | }; |
632 | 0 | struct reftable_iterator oit = { NULL }; |
633 | 0 | struct reftable_record got = { |
634 | 0 | .type = REFTABLE_BLOCK_TYPE_OBJ, |
635 | 0 | .u.obj = { 0 }, |
636 | 0 | }; |
637 | 0 | int err = 0; |
638 | 0 | struct indexed_table_ref_iter *itr = NULL; |
639 | | |
640 | | /* Look through the reverse index. */ |
641 | 0 | err = table_init_iter(t, &oit, REFTABLE_BLOCK_TYPE_OBJ); |
642 | 0 | if (err < 0) |
643 | 0 | goto done; |
644 | | |
645 | 0 | err = iterator_seek(&oit, &want); |
646 | 0 | if (err != 0) |
647 | 0 | goto done; |
648 | | |
649 | | /* read out the reftable_obj_record */ |
650 | 0 | err = iterator_next(&oit, &got); |
651 | 0 | if (err < 0) |
652 | 0 | goto done; |
653 | | |
654 | 0 | if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix, |
655 | 0 | t->object_id_len)) { |
656 | | /* didn't find it; return empty iterator */ |
657 | 0 | iterator_set_empty(it); |
658 | 0 | err = 0; |
659 | 0 | goto done; |
660 | 0 | } |
661 | | |
662 | 0 | err = indexed_table_ref_iter_new(&itr, t, oid, hash_size(t->hash_id), |
663 | 0 | got.u.obj.offsets, |
664 | 0 | got.u.obj.offset_len); |
665 | 0 | if (err < 0) |
666 | 0 | goto done; |
667 | 0 | got.u.obj.offsets = NULL; |
668 | 0 | iterator_from_indexed_table_ref_iter(it, itr); |
669 | |
|
670 | 0 | done: |
671 | 0 | reftable_iterator_destroy(&oit); |
672 | 0 | reftable_record_release(&got); |
673 | 0 | return err; |
674 | 0 | } |
675 | | |
676 | | static int reftable_table_refs_for_unindexed(struct reftable_table *t, |
677 | | struct reftable_iterator *it, |
678 | | uint8_t *oid) |
679 | 0 | { |
680 | 0 | struct table_iter *ti; |
681 | 0 | struct filtering_ref_iterator *filter = NULL; |
682 | 0 | struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT; |
683 | 0 | uint32_t oid_len = hash_size(t->hash_id); |
684 | 0 | int err; |
685 | |
|
686 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
687 | 0 | if (!ti) { |
688 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
689 | 0 | goto out; |
690 | 0 | } |
691 | | |
692 | 0 | table_iter_init(ti, t); |
693 | 0 | err = table_iter_seek_start(ti, REFTABLE_BLOCK_TYPE_REF, 0); |
694 | 0 | if (err < 0) |
695 | 0 | goto out; |
696 | | |
697 | 0 | filter = reftable_malloc(sizeof(*filter)); |
698 | 0 | if (!filter) { |
699 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
700 | 0 | goto out; |
701 | 0 | } |
702 | 0 | *filter = empty; |
703 | |
|
704 | 0 | err = reftable_buf_add(&filter->oid, oid, oid_len); |
705 | 0 | if (err < 0) |
706 | 0 | goto out; |
707 | | |
708 | 0 | iterator_from_table_iter(&filter->it, ti); |
709 | |
|
710 | 0 | iterator_from_filtering_ref_iterator(it, filter); |
711 | |
|
712 | 0 | err = 0; |
713 | |
|
714 | 0 | out: |
715 | 0 | if (err < 0) { |
716 | 0 | if (ti) |
717 | 0 | table_iter_close(ti); |
718 | 0 | reftable_free(ti); |
719 | 0 | if (filter) { |
720 | 0 | reftable_buf_release(&filter->oid); |
721 | 0 | reftable_free(filter); |
722 | 0 | } |
723 | 0 | } |
724 | 0 | return err; |
725 | 0 | } |
726 | | |
727 | | int reftable_table_refs_for(struct reftable_table *t, |
728 | | struct reftable_iterator *it, uint8_t *oid) |
729 | 0 | { |
730 | 0 | if (t->obj_offsets.is_present) |
731 | 0 | return reftable_table_refs_for_indexed(t, it, oid); |
732 | 0 | return reftable_table_refs_for_unindexed(t, it, oid); |
733 | 0 | } |
734 | | |
735 | | uint64_t reftable_table_max_update_index(struct reftable_table *t) |
736 | 0 | { |
737 | 0 | return t->max_update_index; |
738 | 0 | } |
739 | | |
740 | | uint64_t reftable_table_min_update_index(struct reftable_table *t) |
741 | 0 | { |
742 | 0 | return t->min_update_index; |
743 | 0 | } |
744 | | |
745 | | int reftable_table_iterator_init(struct reftable_table_iterator *it, |
746 | | struct reftable_table *t) |
747 | 0 | { |
748 | 0 | struct table_iter *ti; |
749 | 0 | int err; |
750 | |
|
751 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
752 | 0 | if (!ti) |
753 | 0 | return REFTABLE_OUT_OF_MEMORY_ERROR; |
754 | | |
755 | 0 | err = table_iter_init(ti, t); |
756 | 0 | if (err < 0) |
757 | 0 | goto out; |
758 | | |
759 | 0 | it->iter_arg = ti; |
760 | 0 | err = 0; |
761 | |
|
762 | 0 | out: |
763 | 0 | if (err < 0) |
764 | 0 | reftable_free(ti); |
765 | 0 | return err; |
766 | 0 | } |
767 | | |
768 | | void reftable_table_iterator_release(struct reftable_table_iterator *it) |
769 | 0 | { |
770 | 0 | if (!it->iter_arg) |
771 | 0 | return; |
772 | 0 | table_iter_close(it->iter_arg); |
773 | 0 | reftable_free(it->iter_arg); |
774 | 0 | it->iter_arg = NULL; |
775 | 0 | } |
776 | | |
777 | | int reftable_table_iterator_next(struct reftable_table_iterator *it, |
778 | | const struct reftable_block **out) |
779 | 0 | { |
780 | 0 | struct table_iter *ti = it->iter_arg; |
781 | 0 | int err; |
782 | |
|
783 | 0 | err = table_iter_next_block(ti); |
784 | 0 | if (err) |
785 | 0 | return err; |
786 | | |
787 | 0 | *out = &ti->block; |
788 | |
|
789 | 0 | return 0; |
790 | 0 | } |