Merge branch 'ps/reflog-list' into HEAD
[alt-git.git] / reftable / reader.c
blobb113daab77336c240d10f64ce9890ff04f5f366a
1 /*
2 Copyright 2020 Google LLC
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 */
9 #include "reader.h"
11 #include "system.h"
12 #include "block.h"
13 #include "constants.h"
14 #include "generic.h"
15 #include "iter.h"
16 #include "record.h"
17 #include "reftable-error.h"
18 #include "reftable-generic.h"
20 uint64_t block_source_size(struct reftable_block_source *source)
22 return source->ops->size(source->arg);
25 int block_source_read_block(struct reftable_block_source *source,
26 struct reftable_block *dest, uint64_t off,
27 uint32_t size)
29 int result = source->ops->read_block(source->arg, dest, off, size);
30 dest->source = *source;
31 return result;
34 void block_source_close(struct reftable_block_source *source)
36 if (!source->ops) {
37 return;
40 source->ops->close(source->arg);
41 source->ops = NULL;
44 static struct reftable_reader_offsets *
45 reader_offsets_for(struct reftable_reader *r, uint8_t typ)
47 switch (typ) {
48 case BLOCK_TYPE_REF:
49 return &r->ref_offsets;
50 case BLOCK_TYPE_LOG:
51 return &r->log_offsets;
52 case BLOCK_TYPE_OBJ:
53 return &r->obj_offsets;
55 abort();
58 static int reader_get_block(struct reftable_reader *r,
59 struct reftable_block *dest, uint64_t off,
60 uint32_t sz)
62 if (off >= r->size)
63 return 0;
65 if (off + sz > r->size) {
66 sz = r->size - off;
69 return block_source_read_block(&r->source, dest, off, sz);
72 uint32_t reftable_reader_hash_id(struct reftable_reader *r)
74 return r->hash_id;
77 const char *reader_name(struct reftable_reader *r)
79 return r->name;
82 static int parse_footer(struct reftable_reader *r, uint8_t *footer,
83 uint8_t *header)
85 uint8_t *f = footer;
86 uint8_t first_block_typ;
87 int err = 0;
88 uint32_t computed_crc;
89 uint32_t file_crc;
91 if (memcmp(f, "REFT", 4)) {
92 err = REFTABLE_FORMAT_ERROR;
93 goto done;
95 f += 4;
97 if (memcmp(footer, header, header_size(r->version))) {
98 err = REFTABLE_FORMAT_ERROR;
99 goto done;
102 f++;
103 r->block_size = get_be24(f);
105 f += 3;
106 r->min_update_index = get_be64(f);
107 f += 8;
108 r->max_update_index = get_be64(f);
109 f += 8;
111 if (r->version == 1) {
112 r->hash_id = GIT_SHA1_FORMAT_ID;
113 } else {
114 r->hash_id = get_be32(f);
115 switch (r->hash_id) {
116 case GIT_SHA1_FORMAT_ID:
117 break;
118 case GIT_SHA256_FORMAT_ID:
119 break;
120 default:
121 err = REFTABLE_FORMAT_ERROR;
122 goto done;
124 f += 4;
127 r->ref_offsets.index_offset = get_be64(f);
128 f += 8;
130 r->obj_offsets.offset = get_be64(f);
131 f += 8;
133 r->object_id_len = r->obj_offsets.offset & ((1 << 5) - 1);
134 r->obj_offsets.offset >>= 5;
136 r->obj_offsets.index_offset = get_be64(f);
137 f += 8;
138 r->log_offsets.offset = get_be64(f);
139 f += 8;
140 r->log_offsets.index_offset = get_be64(f);
141 f += 8;
143 computed_crc = crc32(0, footer, f - footer);
144 file_crc = get_be32(f);
145 f += 4;
146 if (computed_crc != file_crc) {
147 err = REFTABLE_FORMAT_ERROR;
148 goto done;
151 first_block_typ = header[header_size(r->version)];
152 r->ref_offsets.is_present = (first_block_typ == BLOCK_TYPE_REF);
153 r->ref_offsets.offset = 0;
154 r->log_offsets.is_present = (first_block_typ == BLOCK_TYPE_LOG ||
155 r->log_offsets.offset > 0);
156 r->obj_offsets.is_present = r->obj_offsets.offset > 0;
157 if (r->obj_offsets.is_present && !r->object_id_len) {
158 err = REFTABLE_FORMAT_ERROR;
159 goto done;
162 err = 0;
163 done:
164 return err;
167 int init_reader(struct reftable_reader *r, struct reftable_block_source *source,
168 const char *name)
170 struct reftable_block footer = { NULL };
171 struct reftable_block header = { NULL };
172 int err = 0;
173 uint64_t file_size = block_source_size(source);
175 /* Need +1 to read type of first block. */
176 uint32_t read_size = header_size(2) + 1; /* read v2 because it's larger. */
177 memset(r, 0, sizeof(struct reftable_reader));
179 if (read_size > file_size) {
180 err = REFTABLE_FORMAT_ERROR;
181 goto done;
184 err = block_source_read_block(source, &header, 0, read_size);
185 if (err != read_size) {
186 err = REFTABLE_IO_ERROR;
187 goto done;
190 if (memcmp(header.data, "REFT", 4)) {
191 err = REFTABLE_FORMAT_ERROR;
192 goto done;
194 r->version = header.data[4];
195 if (r->version != 1 && r->version != 2) {
196 err = REFTABLE_FORMAT_ERROR;
197 goto done;
200 r->size = file_size - footer_size(r->version);
201 r->source = *source;
202 r->name = xstrdup(name);
203 r->hash_id = 0;
205 err = block_source_read_block(source, &footer, r->size,
206 footer_size(r->version));
207 if (err != footer_size(r->version)) {
208 err = REFTABLE_IO_ERROR;
209 goto done;
212 err = parse_footer(r, footer.data, header.data);
213 done:
214 reftable_block_done(&footer);
215 reftable_block_done(&header);
216 return err;
219 struct table_iter {
220 struct reftable_reader *r;
221 uint8_t typ;
222 uint64_t block_off;
223 struct block_iter bi;
224 int is_finished;
226 #define TABLE_ITER_INIT { \
227 .bi = BLOCK_ITER_INIT \
230 static void table_iter_copy_from(struct table_iter *dest,
231 struct table_iter *src)
233 dest->r = src->r;
234 dest->typ = src->typ;
235 dest->block_off = src->block_off;
236 dest->is_finished = src->is_finished;
237 block_iter_copy_from(&dest->bi, &src->bi);
240 static int table_iter_next_in_block(struct table_iter *ti,
241 struct reftable_record *rec)
243 int res = block_iter_next(&ti->bi, rec);
244 if (res == 0 && reftable_record_type(rec) == BLOCK_TYPE_REF) {
245 rec->u.ref.update_index += ti->r->min_update_index;
248 return res;
251 static void table_iter_block_done(struct table_iter *ti)
253 if (!ti->bi.br) {
254 return;
256 reftable_block_done(&ti->bi.br->block);
257 FREE_AND_NULL(ti->bi.br);
259 ti->bi.last_key.len = 0;
260 ti->bi.next_off = 0;
263 static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off,
264 int version)
266 int32_t result = 0;
268 if (off == 0) {
269 data += header_size(version);
272 *typ = data[0];
273 if (reftable_is_block_type(*typ)) {
274 result = get_be24(data + 1);
276 return result;
279 int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br,
280 uint64_t next_off, uint8_t want_typ)
282 int32_t guess_block_size = r->block_size ? r->block_size :
283 DEFAULT_BLOCK_SIZE;
284 struct reftable_block block = { NULL };
285 uint8_t block_typ = 0;
286 int err = 0;
287 uint32_t header_off = next_off ? 0 : header_size(r->version);
288 int32_t block_size = 0;
290 if (next_off >= r->size)
291 return 1;
293 err = reader_get_block(r, &block, next_off, guess_block_size);
294 if (err < 0)
295 goto done;
297 block_size = extract_block_size(block.data, &block_typ, next_off,
298 r->version);
299 if (block_size < 0) {
300 err = block_size;
301 goto done;
303 if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) {
304 err = 1;
305 goto done;
308 if (block_size > guess_block_size) {
309 reftable_block_done(&block);
310 err = reader_get_block(r, &block, next_off, block_size);
311 if (err < 0) {
312 goto done;
316 err = block_reader_init(br, &block, header_off, r->block_size,
317 hash_size(r->hash_id));
318 done:
319 reftable_block_done(&block);
321 return err;
324 static int table_iter_next_block(struct table_iter *dest,
325 struct table_iter *src)
327 uint64_t next_block_off = src->block_off + src->bi.br->full_block_size;
328 struct block_reader br = { 0 };
329 int err = 0;
331 dest->r = src->r;
332 dest->typ = src->typ;
333 dest->block_off = next_block_off;
335 err = reader_init_block_reader(src->r, &br, next_block_off, src->typ);
336 if (err > 0) {
337 dest->is_finished = 1;
338 return 1;
340 if (err != 0)
341 return err;
342 else {
343 struct block_reader *brp =
344 reftable_malloc(sizeof(struct block_reader));
345 *brp = br;
347 dest->is_finished = 0;
348 block_reader_start(brp, &dest->bi);
350 return 0;
353 static int table_iter_next(struct table_iter *ti, struct reftable_record *rec)
355 if (reftable_record_type(rec) != ti->typ)
356 return REFTABLE_API_ERROR;
358 while (1) {
359 struct table_iter next = TABLE_ITER_INIT;
360 int err;
362 if (ti->is_finished)
363 return 1;
366 * Check whether the current block still has more records. If
367 * so, return it. If the iterator returns positive then the
368 * current block has been exhausted.
370 err = table_iter_next_in_block(ti, rec);
371 if (err <= 0)
372 return err;
375 * Otherwise, we need to continue to the next block in the
376 * table and retry. If there are no more blocks then the
377 * iterator is drained.
379 err = table_iter_next_block(&next, ti);
380 table_iter_block_done(ti);
381 if (err) {
382 ti->is_finished = 1;
383 return err;
386 table_iter_copy_from(ti, &next);
387 block_iter_close(&next.bi);
391 static int table_iter_next_void(void *ti, struct reftable_record *rec)
393 return table_iter_next(ti, rec);
396 static void table_iter_close(void *p)
398 struct table_iter *ti = p;
399 table_iter_block_done(ti);
400 block_iter_close(&ti->bi);
403 static struct reftable_iterator_vtable table_iter_vtable = {
404 .next = &table_iter_next_void,
405 .close = &table_iter_close,
408 static void iterator_from_table_iter(struct reftable_iterator *it,
409 struct table_iter *ti)
411 assert(!it->ops);
412 it->iter_arg = ti;
413 it->ops = &table_iter_vtable;
416 static int reader_table_iter_at(struct reftable_reader *r,
417 struct table_iter *ti, uint64_t off,
418 uint8_t typ)
420 struct block_reader br = { 0 };
421 struct block_reader *brp = NULL;
423 int err = reader_init_block_reader(r, &br, off, typ);
424 if (err != 0)
425 return err;
427 brp = reftable_malloc(sizeof(struct block_reader));
428 *brp = br;
429 ti->r = r;
430 ti->typ = block_reader_type(brp);
431 ti->block_off = off;
432 block_reader_start(brp, &ti->bi);
433 return 0;
436 static int reader_start(struct reftable_reader *r, struct table_iter *ti,
437 uint8_t typ, int index)
439 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
440 uint64_t off = offs->offset;
441 if (index) {
442 off = offs->index_offset;
443 if (off == 0) {
444 return 1;
446 typ = BLOCK_TYPE_INDEX;
449 return reader_table_iter_at(r, ti, off, typ);
452 static int reader_seek_linear(struct table_iter *ti,
453 struct reftable_record *want)
455 struct strbuf want_key = STRBUF_INIT;
456 struct strbuf got_key = STRBUF_INIT;
457 struct table_iter next = TABLE_ITER_INIT;
458 struct reftable_record rec;
459 int err = -1;
461 reftable_record_init(&rec, reftable_record_type(want));
462 reftable_record_key(want, &want_key);
464 while (1) {
465 err = table_iter_next_block(&next, ti);
466 if (err < 0)
467 goto done;
469 if (err > 0) {
470 break;
473 err = block_reader_first_key(next.bi.br, &got_key);
474 if (err < 0)
475 goto done;
477 if (strbuf_cmp(&got_key, &want_key) > 0) {
478 table_iter_block_done(&next);
479 break;
482 table_iter_block_done(ti);
483 table_iter_copy_from(ti, &next);
486 err = block_iter_seek(&ti->bi, &want_key);
487 if (err < 0)
488 goto done;
489 err = 0;
491 done:
492 block_iter_close(&next.bi);
493 reftable_record_release(&rec);
494 strbuf_release(&want_key);
495 strbuf_release(&got_key);
496 return err;
499 static int reader_seek_indexed(struct reftable_reader *r,
500 struct reftable_iterator *it,
501 struct reftable_record *rec)
503 struct reftable_record want_index = {
504 .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }
506 struct reftable_record index_result = {
507 .type = BLOCK_TYPE_INDEX,
508 .u.idx = { .last_key = STRBUF_INIT },
510 struct table_iter index_iter = TABLE_ITER_INIT;
511 struct table_iter next = TABLE_ITER_INIT;
512 int err = 0;
514 reftable_record_key(rec, &want_index.u.idx.last_key);
515 err = reader_start(r, &index_iter, reftable_record_type(rec), 1);
516 if (err < 0)
517 goto done;
520 * The index may consist of multiple levels, where each level may have
521 * multiple index blocks. We start by doing a linear search in the
522 * highest layer that identifies the relevant index block as well as
523 * the record inside that block that corresponds to our wanted key.
525 err = reader_seek_linear(&index_iter, &want_index);
526 if (err < 0)
527 goto done;
530 * Traverse down the levels until we find a non-index entry.
532 while (1) {
534 * In case we seek a record that does not exist the index iter
535 * will tell us that the iterator is over. This works because
536 * the last index entry of the current level will contain the
537 * last key it knows about. So in case our seeked key is larger
538 * than the last indexed key we know that it won't exist.
540 * There is one subtlety in the layout of the index section
541 * that makes this work as expected: the highest-level index is
542 * at end of the section and will point backwards and thus we
543 * start reading from the end of the index section, not the
544 * beginning.
546 * If that wasn't the case and the order was reversed then the
547 * linear seek would seek into the lower levels and traverse
548 * all levels of the index only to find out that the key does
549 * not exist.
551 err = table_iter_next(&index_iter, &index_result);
552 table_iter_block_done(&index_iter);
553 if (err != 0)
554 goto done;
556 err = reader_table_iter_at(r, &next, index_result.u.idx.offset,
558 if (err != 0)
559 goto done;
561 err = block_iter_seek(&next.bi, &want_index.u.idx.last_key);
562 if (err < 0)
563 goto done;
565 if (next.typ == reftable_record_type(rec)) {
566 err = 0;
567 break;
570 if (next.typ != BLOCK_TYPE_INDEX) {
571 err = REFTABLE_FORMAT_ERROR;
572 break;
575 table_iter_copy_from(&index_iter, &next);
578 if (err == 0) {
579 struct table_iter empty = TABLE_ITER_INIT;
580 struct table_iter *malloced = reftable_calloc(1, sizeof(*malloced));
581 *malloced = empty;
582 table_iter_copy_from(malloced, &next);
583 iterator_from_table_iter(it, malloced);
585 done:
586 block_iter_close(&next.bi);
587 table_iter_close(&index_iter);
588 reftable_record_release(&want_index);
589 reftable_record_release(&index_result);
590 return err;
593 static int reader_seek_internal(struct reftable_reader *r,
594 struct reftable_iterator *it,
595 struct reftable_record *rec)
597 struct reftable_reader_offsets *offs =
598 reader_offsets_for(r, reftable_record_type(rec));
599 uint64_t idx = offs->index_offset;
600 struct table_iter ti = TABLE_ITER_INIT;
601 int err = 0;
602 if (idx > 0)
603 return reader_seek_indexed(r, it, rec);
605 err = reader_start(r, &ti, reftable_record_type(rec), 0);
606 if (err < 0)
607 return err;
608 err = reader_seek_linear(&ti, rec);
609 if (err < 0)
610 return err;
611 else {
612 struct table_iter *p =
613 reftable_malloc(sizeof(struct table_iter));
614 *p = ti;
615 iterator_from_table_iter(it, p);
618 return 0;
621 static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it,
622 struct reftable_record *rec)
624 uint8_t typ = reftable_record_type(rec);
626 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
627 if (!offs->is_present) {
628 iterator_set_empty(it);
629 return 0;
632 return reader_seek_internal(r, it, rec);
635 int reftable_reader_seek_ref(struct reftable_reader *r,
636 struct reftable_iterator *it, const char *name)
638 struct reftable_record rec = {
639 .type = BLOCK_TYPE_REF,
640 .u.ref = {
641 .refname = (char *)name,
644 return reader_seek(r, it, &rec);
647 int reftable_reader_seek_log_at(struct reftable_reader *r,
648 struct reftable_iterator *it, const char *name,
649 uint64_t update_index)
651 struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
652 .u.log = {
653 .refname = (char *)name,
654 .update_index = update_index,
655 } };
656 return reader_seek(r, it, &rec);
659 int reftable_reader_seek_log(struct reftable_reader *r,
660 struct reftable_iterator *it, const char *name)
662 uint64_t max = ~((uint64_t)0);
663 return reftable_reader_seek_log_at(r, it, name, max);
666 void reader_close(struct reftable_reader *r)
668 block_source_close(&r->source);
669 FREE_AND_NULL(r->name);
672 int reftable_new_reader(struct reftable_reader **p,
673 struct reftable_block_source *src, char const *name)
675 struct reftable_reader *rd = reftable_calloc(1, sizeof(*rd));
676 int err = init_reader(rd, src, name);
677 if (err == 0) {
678 *p = rd;
679 } else {
680 block_source_close(src);
681 reftable_free(rd);
683 return err;
686 void reftable_reader_free(struct reftable_reader *r)
688 if (!r)
689 return;
690 reader_close(r);
691 reftable_free(r);
694 static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
695 struct reftable_iterator *it,
696 uint8_t *oid)
698 struct reftable_record want = {
699 .type = BLOCK_TYPE_OBJ,
700 .u.obj = {
701 .hash_prefix = oid,
702 .hash_prefix_len = r->object_id_len,
705 struct reftable_iterator oit = { NULL };
706 struct reftable_record got = {
707 .type = BLOCK_TYPE_OBJ,
708 .u.obj = { 0 },
710 int err = 0;
711 struct indexed_table_ref_iter *itr = NULL;
713 /* Look through the reverse index. */
714 err = reader_seek(r, &oit, &want);
715 if (err != 0)
716 goto done;
718 /* read out the reftable_obj_record */
719 err = iterator_next(&oit, &got);
720 if (err < 0)
721 goto done;
723 if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix,
724 r->object_id_len)) {
725 /* didn't find it; return empty iterator */
726 iterator_set_empty(it);
727 err = 0;
728 goto done;
731 err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id),
732 got.u.obj.offsets,
733 got.u.obj.offset_len);
734 if (err < 0)
735 goto done;
736 got.u.obj.offsets = NULL;
737 iterator_from_indexed_table_ref_iter(it, itr);
739 done:
740 reftable_iterator_destroy(&oit);
741 reftable_record_release(&got);
742 return err;
745 static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
746 struct reftable_iterator *it,
747 uint8_t *oid)
749 struct table_iter ti_empty = TABLE_ITER_INIT;
750 struct table_iter *ti = reftable_calloc(1, sizeof(*ti));
751 struct filtering_ref_iterator *filter = NULL;
752 struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT;
753 int oid_len = hash_size(r->hash_id);
754 int err;
756 *ti = ti_empty;
757 err = reader_start(r, ti, BLOCK_TYPE_REF, 0);
758 if (err < 0) {
759 reftable_free(ti);
760 return err;
763 filter = reftable_malloc(sizeof(struct filtering_ref_iterator));
764 *filter = empty;
766 strbuf_add(&filter->oid, oid, oid_len);
767 reftable_table_from_reader(&filter->tab, r);
768 filter->double_check = 0;
769 iterator_from_table_iter(&filter->it, ti);
771 iterator_from_filtering_ref_iterator(it, filter);
772 return 0;
775 int reftable_reader_refs_for(struct reftable_reader *r,
776 struct reftable_iterator *it, uint8_t *oid)
778 if (r->obj_offsets.is_present)
779 return reftable_reader_refs_for_indexed(r, it, oid);
780 return reftable_reader_refs_for_unindexed(r, it, oid);
783 uint64_t reftable_reader_max_update_index(struct reftable_reader *r)
785 return r->max_update_index;
788 uint64_t reftable_reader_min_update_index(struct reftable_reader *r)
790 return r->min_update_index;
793 /* generic table interface. */
795 static int reftable_reader_seek_void(void *tab, struct reftable_iterator *it,
796 struct reftable_record *rec)
798 return reader_seek(tab, it, rec);
801 static uint32_t reftable_reader_hash_id_void(void *tab)
803 return reftable_reader_hash_id(tab);
806 static uint64_t reftable_reader_min_update_index_void(void *tab)
808 return reftable_reader_min_update_index(tab);
811 static uint64_t reftable_reader_max_update_index_void(void *tab)
813 return reftable_reader_max_update_index(tab);
816 static struct reftable_table_vtable reader_vtable = {
817 .seek_record = reftable_reader_seek_void,
818 .hash_id = reftable_reader_hash_id_void,
819 .min_update_index = reftable_reader_min_update_index_void,
820 .max_update_index = reftable_reader_max_update_index_void,
823 void reftable_table_from_reader(struct reftable_table *tab,
824 struct reftable_reader *reader)
826 assert(!tab->ops);
827 tab->ops = &reader_vtable;
828 tab->table_arg = reader;
832 int reftable_reader_print_file(const char *tablename)
834 struct reftable_block_source src = { NULL };
835 int err = reftable_block_source_from_file(&src, tablename);
836 struct reftable_reader *r = NULL;
837 struct reftable_table tab = { NULL };
838 if (err < 0)
839 goto done;
841 err = reftable_new_reader(&r, &src, tablename);
842 if (err < 0)
843 goto done;
845 reftable_table_from_reader(&tab, r);
846 err = reftable_table_print(&tab);
847 done:
848 reftable_reader_free(r);
849 return err;