/src/git/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 err; |
247 | | |
248 | 0 | ti->typ = reftable_block_type(&ti->block); |
249 | 0 | ti->block_off = off; |
250 | 0 | block_iter_init(&ti->bi, &ti->block); |
251 | 0 | ti->is_finished = 0; |
252 | 0 | return 0; |
253 | 0 | } |
254 | | |
255 | | static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index) |
256 | 0 | { |
257 | 0 | struct reftable_table_offsets *offs = table_offsets_for(ti->table, typ); |
258 | 0 | uint64_t off = offs->offset; |
259 | 0 | if (index) { |
260 | 0 | off = offs->index_offset; |
261 | 0 | if (off == 0) { |
262 | 0 | return 1; |
263 | 0 | } |
264 | 0 | typ = REFTABLE_BLOCK_TYPE_INDEX; |
265 | 0 | } |
266 | | |
267 | 0 | return table_iter_seek_to(ti, off, typ); |
268 | 0 | } |
269 | | |
270 | | static int table_iter_seek_linear(struct table_iter *ti, |
271 | | struct reftable_record *want) |
272 | 0 | { |
273 | 0 | struct reftable_buf want_key = REFTABLE_BUF_INIT; |
274 | 0 | struct reftable_buf got_key = REFTABLE_BUF_INIT; |
275 | 0 | struct reftable_record rec; |
276 | 0 | int err; |
277 | |
|
278 | 0 | err = reftable_record_init(&rec, reftable_record_type(want)); |
279 | 0 | if (err < 0) |
280 | 0 | goto done; |
281 | | |
282 | 0 | err = reftable_record_key(want, &want_key); |
283 | 0 | if (err < 0) |
284 | 0 | goto done; |
285 | | |
286 | | /* |
287 | | * First we need to locate the block that must contain our record. To |
288 | | * do so we scan through blocks linearly until we find the first block |
289 | | * whose first key is bigger than our wanted key. Once we have found |
290 | | * that block we know that the key must be contained in the preceding |
291 | | * block. |
292 | | * |
293 | | * This algorithm is somewhat unfortunate because it means that we |
294 | | * always have to seek one block too far and then back up. But as we |
295 | | * can only decode the _first_ key of a block but not its _last_ key we |
296 | | * have no other way to do this. |
297 | | */ |
298 | 0 | while (1) { |
299 | 0 | struct table_iter next = *ti; |
300 | | |
301 | | /* |
302 | | * We must be careful to not modify underlying data of `ti` |
303 | | * because we may find that `next` does not contain our desired |
304 | | * block, but that `ti` does. In that case, we would discard |
305 | | * `next` and continue with `ti`. |
306 | | * |
307 | | * This also means that we cannot reuse allocated memory for |
308 | | * `next` here. While it would be great if we could, it should |
309 | | * in practice not be too bad given that we should only ever |
310 | | * end up doing linear seeks with at most three blocks. As soon |
311 | | * as we have more than three blocks we would have an index, so |
312 | | * we would not do a linear search there anymore. |
313 | | */ |
314 | 0 | memset(&next.block.block_data, 0, sizeof(next.block.block_data)); |
315 | 0 | next.block.zstream = NULL; |
316 | 0 | next.block.uncompressed_data = NULL; |
317 | 0 | next.block.uncompressed_cap = 0; |
318 | |
|
319 | 0 | err = table_iter_next_block(&next); |
320 | 0 | if (err < 0) |
321 | 0 | goto done; |
322 | 0 | if (err > 0) |
323 | 0 | break; |
324 | | |
325 | 0 | err = reftable_block_first_key(&next.block, &got_key); |
326 | 0 | if (err < 0) |
327 | 0 | goto done; |
328 | | |
329 | 0 | if (reftable_buf_cmp(&got_key, &want_key) > 0) { |
330 | 0 | table_iter_block_done(&next); |
331 | 0 | break; |
332 | 0 | } |
333 | | |
334 | 0 | table_iter_block_done(ti); |
335 | 0 | *ti = next; |
336 | 0 | } |
337 | | |
338 | | /* |
339 | | * We have located the block that must contain our record, so we seek |
340 | | * the wanted key inside of it. If the block does not contain our key |
341 | | * we know that the corresponding record does not exist. |
342 | | */ |
343 | 0 | block_iter_init(&ti->bi, &ti->block); |
344 | 0 | err = block_iter_seek_key(&ti->bi, &want_key); |
345 | 0 | if (err < 0) |
346 | 0 | goto done; |
347 | 0 | err = 0; |
348 | |
|
349 | 0 | done: |
350 | 0 | reftable_record_release(&rec); |
351 | 0 | reftable_buf_release(&want_key); |
352 | 0 | reftable_buf_release(&got_key); |
353 | 0 | return err; |
354 | 0 | } |
355 | | |
356 | | static int table_iter_seek_indexed(struct table_iter *ti, |
357 | | struct reftable_record *rec) |
358 | 0 | { |
359 | 0 | struct reftable_record want_index = { |
360 | 0 | .type = REFTABLE_BLOCK_TYPE_INDEX, .u.idx = { .last_key = REFTABLE_BUF_INIT } |
361 | 0 | }; |
362 | 0 | struct reftable_record index_result = { |
363 | 0 | .type = REFTABLE_BLOCK_TYPE_INDEX, |
364 | 0 | .u.idx = { .last_key = REFTABLE_BUF_INIT }, |
365 | 0 | }; |
366 | 0 | int err; |
367 | |
|
368 | 0 | err = reftable_record_key(rec, &want_index.u.idx.last_key); |
369 | 0 | if (err < 0) |
370 | 0 | goto done; |
371 | | |
372 | | /* |
373 | | * The index may consist of multiple levels, where each level may have |
374 | | * multiple index blocks. We start by doing a linear search in the |
375 | | * highest layer that identifies the relevant index block as well as |
376 | | * the record inside that block that corresponds to our wanted key. |
377 | | */ |
378 | 0 | err = table_iter_seek_linear(ti, &want_index); |
379 | 0 | if (err < 0) |
380 | 0 | goto done; |
381 | | |
382 | | /* |
383 | | * Traverse down the levels until we find a non-index entry. |
384 | | */ |
385 | 0 | while (1) { |
386 | | /* |
387 | | * In case we seek a record that does not exist the index iter |
388 | | * will tell us that the iterator is over. This works because |
389 | | * the last index entry of the current level will contain the |
390 | | * last key it knows about. So in case our seeked key is larger |
391 | | * than the last indexed key we know that it won't exist. |
392 | | * |
393 | | * There is one subtlety in the layout of the index section |
394 | | * that makes this work as expected: the highest-level index is |
395 | | * at end of the section and will point backwards and thus we |
396 | | * start reading from the end of the index section, not the |
397 | | * beginning. |
398 | | * |
399 | | * If that wasn't the case and the order was reversed then the |
400 | | * linear seek would seek into the lower levels and traverse |
401 | | * all levels of the index only to find out that the key does |
402 | | * not exist. |
403 | | */ |
404 | 0 | err = table_iter_next(ti, &index_result); |
405 | 0 | if (err != 0) |
406 | 0 | goto done; |
407 | | |
408 | 0 | err = table_iter_seek_to(ti, index_result.u.idx.offset, 0); |
409 | 0 | if (err != 0) |
410 | 0 | goto done; |
411 | | |
412 | 0 | block_iter_init(&ti->bi, &ti->block); |
413 | |
|
414 | 0 | err = block_iter_seek_key(&ti->bi, &want_index.u.idx.last_key); |
415 | 0 | if (err < 0) |
416 | 0 | goto done; |
417 | | |
418 | 0 | if (ti->typ == reftable_record_type(rec)) { |
419 | 0 | err = 0; |
420 | 0 | break; |
421 | 0 | } |
422 | | |
423 | 0 | if (ti->typ != REFTABLE_BLOCK_TYPE_INDEX) { |
424 | 0 | err = REFTABLE_FORMAT_ERROR; |
425 | 0 | goto done; |
426 | 0 | } |
427 | 0 | } |
428 | | |
429 | 0 | done: |
430 | 0 | reftable_record_release(&want_index); |
431 | 0 | reftable_record_release(&index_result); |
432 | 0 | return err; |
433 | 0 | } |
434 | | |
435 | | static int table_iter_seek(struct table_iter *ti, |
436 | | struct reftable_record *want) |
437 | 0 | { |
438 | 0 | uint8_t typ = reftable_record_type(want); |
439 | 0 | struct reftable_table_offsets *offs = table_offsets_for(ti->table, typ); |
440 | 0 | int err; |
441 | |
|
442 | 0 | err = table_iter_seek_start(ti, reftable_record_type(want), |
443 | 0 | !!offs->index_offset); |
444 | 0 | if (err < 0) |
445 | 0 | goto out; |
446 | | |
447 | 0 | if (offs->index_offset) |
448 | 0 | err = table_iter_seek_indexed(ti, want); |
449 | 0 | else |
450 | 0 | err = table_iter_seek_linear(ti, want); |
451 | 0 | if (err) |
452 | 0 | goto out; |
453 | | |
454 | 0 | out: |
455 | 0 | return err; |
456 | 0 | } |
457 | | |
458 | | static int table_iter_seek_void(void *ti, struct reftable_record *want) |
459 | 0 | { |
460 | 0 | return table_iter_seek(ti, want); |
461 | 0 | } |
462 | | |
463 | | static int table_iter_next_void(void *ti, struct reftable_record *rec) |
464 | 0 | { |
465 | 0 | return table_iter_next(ti, rec); |
466 | 0 | } |
467 | | |
468 | | static void table_iter_close_void(void *ti) |
469 | 0 | { |
470 | 0 | table_iter_close(ti); |
471 | 0 | } |
472 | | |
473 | | static struct reftable_iterator_vtable table_iter_vtable = { |
474 | | .seek = &table_iter_seek_void, |
475 | | .next = &table_iter_next_void, |
476 | | .close = &table_iter_close_void, |
477 | | }; |
478 | | |
479 | | static void iterator_from_table_iter(struct reftable_iterator *it, |
480 | | struct table_iter *ti) |
481 | 0 | { |
482 | 0 | assert(!it->ops); |
483 | 0 | it->iter_arg = ti; |
484 | 0 | it->ops = &table_iter_vtable; |
485 | 0 | } |
486 | | |
487 | | int table_init_iter(struct reftable_table *t, |
488 | | struct reftable_iterator *it, |
489 | | uint8_t typ) |
490 | 0 | { |
491 | 0 | struct reftable_table_offsets *offs = table_offsets_for(t, typ); |
492 | |
|
493 | 0 | if (offs->is_present) { |
494 | 0 | struct table_iter *ti; |
495 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
496 | 0 | if (!ti) |
497 | 0 | return REFTABLE_OUT_OF_MEMORY_ERROR; |
498 | | |
499 | 0 | table_iter_init(ti, t); |
500 | 0 | iterator_from_table_iter(it, ti); |
501 | 0 | } else { |
502 | 0 | iterator_set_empty(it); |
503 | 0 | } |
504 | | |
505 | 0 | return 0; |
506 | 0 | } |
507 | | |
508 | | int reftable_table_init_ref_iterator(struct reftable_table *t, |
509 | | struct reftable_iterator *it) |
510 | 0 | { |
511 | 0 | return table_init_iter(t, it, REFTABLE_BLOCK_TYPE_REF); |
512 | 0 | } |
513 | | |
514 | | int reftable_table_init_log_iterator(struct reftable_table *t, |
515 | | struct reftable_iterator *it) |
516 | 0 | { |
517 | 0 | return table_init_iter(t, it, REFTABLE_BLOCK_TYPE_LOG); |
518 | 0 | } |
519 | | |
520 | | int reftable_table_new(struct reftable_table **out, |
521 | | struct reftable_block_source *source, char const *name) |
522 | 0 | { |
523 | 0 | struct reftable_block_data footer = { 0 }; |
524 | 0 | struct reftable_block_data header = { 0 }; |
525 | 0 | struct reftable_table *t; |
526 | 0 | uint64_t file_size = block_source_size(source); |
527 | 0 | uint32_t read_size; |
528 | 0 | ssize_t bytes_read; |
529 | 0 | int err; |
530 | |
|
531 | 0 | REFTABLE_CALLOC_ARRAY(t, 1); |
532 | 0 | if (!t) { |
533 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
534 | 0 | goto done; |
535 | 0 | } |
536 | | |
537 | | /* |
538 | | * We need one extra byte to read the type of first block. We also |
539 | | * pretend to always be reading v2 of the format because it is larger. |
540 | | */ |
541 | 0 | read_size = header_size(2) + 1; |
542 | 0 | if (read_size > file_size) { |
543 | 0 | err = REFTABLE_FORMAT_ERROR; |
544 | 0 | goto done; |
545 | 0 | } |
546 | | |
547 | 0 | bytes_read = block_source_read_data(source, &header, 0, read_size); |
548 | 0 | if (bytes_read < 0 || (size_t)bytes_read != read_size) { |
549 | 0 | err = REFTABLE_IO_ERROR; |
550 | 0 | goto done; |
551 | 0 | } |
552 | | |
553 | 0 | if (memcmp(header.data, "REFT", 4)) { |
554 | 0 | err = REFTABLE_FORMAT_ERROR; |
555 | 0 | goto done; |
556 | 0 | } |
557 | 0 | t->version = header.data[4]; |
558 | 0 | if (t->version != 1 && t->version != 2) { |
559 | 0 | err = REFTABLE_FORMAT_ERROR; |
560 | 0 | goto done; |
561 | 0 | } |
562 | | |
563 | 0 | t->size = file_size - footer_size(t->version); |
564 | 0 | t->source = *source; |
565 | 0 | t->name = reftable_strdup(name); |
566 | 0 | if (!t->name) { |
567 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
568 | 0 | goto done; |
569 | 0 | } |
570 | 0 | t->hash_id = 0; |
571 | 0 | t->refcount = 1; |
572 | |
|
573 | 0 | bytes_read = block_source_read_data(source, &footer, t->size, |
574 | 0 | footer_size(t->version)); |
575 | 0 | if (bytes_read < 0 || (size_t)bytes_read != footer_size(t->version)) { |
576 | 0 | err = REFTABLE_IO_ERROR; |
577 | 0 | goto done; |
578 | 0 | } |
579 | | |
580 | 0 | err = parse_footer(t, footer.data, header.data); |
581 | 0 | if (err) |
582 | 0 | goto done; |
583 | | |
584 | 0 | *out = t; |
585 | |
|
586 | 0 | done: |
587 | 0 | block_source_release_data(&footer); |
588 | 0 | block_source_release_data(&header); |
589 | 0 | if (err) { |
590 | 0 | if (t) |
591 | 0 | reftable_free(t->name); |
592 | 0 | reftable_free(t); |
593 | 0 | block_source_close(source); |
594 | 0 | } |
595 | 0 | return err; |
596 | 0 | } |
597 | | |
598 | | void reftable_table_incref(struct reftable_table *t) |
599 | 0 | { |
600 | 0 | t->refcount++; |
601 | 0 | } |
602 | | |
603 | | void reftable_table_decref(struct reftable_table *t) |
604 | 0 | { |
605 | 0 | if (!t) |
606 | 0 | return; |
607 | 0 | if (--t->refcount) |
608 | 0 | return; |
609 | 0 | block_source_close(&t->source); |
610 | 0 | REFTABLE_FREE_AND_NULL(t->name); |
611 | 0 | reftable_free(t); |
612 | 0 | } |
613 | | |
614 | | static int reftable_table_refs_for_indexed(struct reftable_table *t, |
615 | | struct reftable_iterator *it, |
616 | | uint8_t *oid) |
617 | 0 | { |
618 | 0 | struct reftable_record want = { |
619 | 0 | .type = REFTABLE_BLOCK_TYPE_OBJ, |
620 | 0 | .u.obj = { |
621 | 0 | .hash_prefix = oid, |
622 | 0 | .hash_prefix_len = t->object_id_len, |
623 | 0 | }, |
624 | 0 | }; |
625 | 0 | struct reftable_iterator oit = { NULL }; |
626 | 0 | struct reftable_record got = { |
627 | 0 | .type = REFTABLE_BLOCK_TYPE_OBJ, |
628 | 0 | .u.obj = { 0 }, |
629 | 0 | }; |
630 | 0 | int err = 0; |
631 | 0 | struct indexed_table_ref_iter *itr = NULL; |
632 | | |
633 | | /* Look through the reverse index. */ |
634 | 0 | err = table_init_iter(t, &oit, REFTABLE_BLOCK_TYPE_OBJ); |
635 | 0 | if (err < 0) |
636 | 0 | goto done; |
637 | | |
638 | 0 | err = iterator_seek(&oit, &want); |
639 | 0 | if (err != 0) |
640 | 0 | goto done; |
641 | | |
642 | | /* read out the reftable_obj_record */ |
643 | 0 | err = iterator_next(&oit, &got); |
644 | 0 | if (err < 0) |
645 | 0 | goto done; |
646 | | |
647 | 0 | if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix, |
648 | 0 | t->object_id_len)) { |
649 | | /* didn't find it; return empty iterator */ |
650 | 0 | iterator_set_empty(it); |
651 | 0 | err = 0; |
652 | 0 | goto done; |
653 | 0 | } |
654 | | |
655 | 0 | err = indexed_table_ref_iter_new(&itr, t, oid, hash_size(t->hash_id), |
656 | 0 | got.u.obj.offsets, |
657 | 0 | got.u.obj.offset_len); |
658 | 0 | if (err < 0) |
659 | 0 | goto done; |
660 | 0 | got.u.obj.offsets = NULL; |
661 | 0 | iterator_from_indexed_table_ref_iter(it, itr); |
662 | |
|
663 | 0 | done: |
664 | 0 | reftable_iterator_destroy(&oit); |
665 | 0 | reftable_record_release(&got); |
666 | 0 | return err; |
667 | 0 | } |
668 | | |
669 | | static int reftable_table_refs_for_unindexed(struct reftable_table *t, |
670 | | struct reftable_iterator *it, |
671 | | uint8_t *oid) |
672 | 0 | { |
673 | 0 | struct table_iter *ti; |
674 | 0 | struct filtering_ref_iterator *filter = NULL; |
675 | 0 | struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT; |
676 | 0 | uint32_t oid_len = hash_size(t->hash_id); |
677 | 0 | int err; |
678 | |
|
679 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
680 | 0 | if (!ti) { |
681 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
682 | 0 | goto out; |
683 | 0 | } |
684 | | |
685 | 0 | table_iter_init(ti, t); |
686 | 0 | err = table_iter_seek_start(ti, REFTABLE_BLOCK_TYPE_REF, 0); |
687 | 0 | if (err < 0) |
688 | 0 | goto out; |
689 | | |
690 | 0 | filter = reftable_malloc(sizeof(*filter)); |
691 | 0 | if (!filter) { |
692 | 0 | err = REFTABLE_OUT_OF_MEMORY_ERROR; |
693 | 0 | goto out; |
694 | 0 | } |
695 | 0 | *filter = empty; |
696 | |
|
697 | 0 | err = reftable_buf_add(&filter->oid, oid, oid_len); |
698 | 0 | if (err < 0) |
699 | 0 | goto out; |
700 | | |
701 | 0 | iterator_from_table_iter(&filter->it, ti); |
702 | |
|
703 | 0 | iterator_from_filtering_ref_iterator(it, filter); |
704 | |
|
705 | 0 | err = 0; |
706 | |
|
707 | 0 | out: |
708 | 0 | if (err < 0) { |
709 | 0 | if (ti) |
710 | 0 | table_iter_close(ti); |
711 | 0 | reftable_free(ti); |
712 | 0 | } |
713 | 0 | return err; |
714 | 0 | } |
715 | | |
716 | | int reftable_table_refs_for(struct reftable_table *t, |
717 | | struct reftable_iterator *it, uint8_t *oid) |
718 | 0 | { |
719 | 0 | if (t->obj_offsets.is_present) |
720 | 0 | return reftable_table_refs_for_indexed(t, it, oid); |
721 | 0 | return reftable_table_refs_for_unindexed(t, it, oid); |
722 | 0 | } |
723 | | |
724 | | uint64_t reftable_table_max_update_index(struct reftable_table *t) |
725 | 0 | { |
726 | 0 | return t->max_update_index; |
727 | 0 | } |
728 | | |
729 | | uint64_t reftable_table_min_update_index(struct reftable_table *t) |
730 | 0 | { |
731 | 0 | return t->min_update_index; |
732 | 0 | } |
733 | | |
734 | | int reftable_table_iterator_init(struct reftable_table_iterator *it, |
735 | | struct reftable_table *t) |
736 | 0 | { |
737 | 0 | struct table_iter *ti; |
738 | 0 | int err; |
739 | |
|
740 | 0 | REFTABLE_ALLOC_ARRAY(ti, 1); |
741 | 0 | if (!ti) |
742 | 0 | return REFTABLE_OUT_OF_MEMORY_ERROR; |
743 | | |
744 | 0 | err = table_iter_init(ti, t); |
745 | 0 | if (err < 0) |
746 | 0 | goto out; |
747 | | |
748 | 0 | it->iter_arg = ti; |
749 | 0 | err = 0; |
750 | |
|
751 | 0 | out: |
752 | 0 | if (err < 0) |
753 | 0 | reftable_free(ti); |
754 | 0 | return err; |
755 | 0 | } |
756 | | |
757 | | void reftable_table_iterator_release(struct reftable_table_iterator *it) |
758 | 0 | { |
759 | 0 | if (!it->iter_arg) |
760 | 0 | return; |
761 | 0 | table_iter_close(it->iter_arg); |
762 | 0 | reftable_free(it->iter_arg); |
763 | 0 | it->iter_arg = NULL; |
764 | 0 | } |
765 | | |
766 | | int reftable_table_iterator_next(struct reftable_table_iterator *it, |
767 | | const struct reftable_block **out) |
768 | 0 | { |
769 | 0 | struct table_iter *ti = it->iter_arg; |
770 | 0 | int err; |
771 | |
|
772 | 0 | err = table_iter_next_block(ti); |
773 | 0 | if (err) |
774 | 0 | return err; |
775 | | |
776 | 0 | *out = &ti->block; |
777 | |
|
778 | 0 | return 0; |
779 | 0 | } |