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
13 #include "constants.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
,
29 int result
= source
->ops
->read_block(source
->arg
, dest
, off
, size
);
30 dest
->source
= *source
;
34 void block_source_close(struct reftable_block_source
*source
)
40 source
->ops
->close(source
->arg
);
44 static struct reftable_reader_offsets
*
45 reader_offsets_for(struct reftable_reader
*r
, uint8_t typ
)
49 return &r
->ref_offsets
;
51 return &r
->log_offsets
;
53 return &r
->obj_offsets
;
58 static int reader_get_block(struct reftable_reader
*r
,
59 struct reftable_block
*dest
, uint64_t off
,
65 if (off
+ sz
> r
->size
) {
69 return block_source_read_block(&r
->source
, dest
, off
, sz
);
72 uint32_t reftable_reader_hash_id(struct reftable_reader
*r
)
77 const char *reader_name(struct reftable_reader
*r
)
82 static int parse_footer(struct reftable_reader
*r
, uint8_t *footer
,
86 uint8_t first_block_typ
;
88 uint32_t computed_crc
;
91 if (memcmp(f
, "REFT", 4)) {
92 err
= REFTABLE_FORMAT_ERROR
;
97 if (memcmp(footer
, header
, header_size(r
->version
))) {
98 err
= REFTABLE_FORMAT_ERROR
;
103 r
->block_size
= get_be24(f
);
106 r
->min_update_index
= get_be64(f
);
108 r
->max_update_index
= get_be64(f
);
111 if (r
->version
== 1) {
112 r
->hash_id
= GIT_SHA1_FORMAT_ID
;
114 r
->hash_id
= get_be32(f
);
115 switch (r
->hash_id
) {
116 case GIT_SHA1_FORMAT_ID
:
118 case GIT_SHA256_FORMAT_ID
:
121 err
= REFTABLE_FORMAT_ERROR
;
127 r
->ref_offsets
.index_offset
= get_be64(f
);
130 r
->obj_offsets
.offset
= get_be64(f
);
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
);
138 r
->log_offsets
.offset
= get_be64(f
);
140 r
->log_offsets
.index_offset
= get_be64(f
);
143 computed_crc
= crc32(0, footer
, f
- footer
);
144 file_crc
= get_be32(f
);
146 if (computed_crc
!= file_crc
) {
147 err
= REFTABLE_FORMAT_ERROR
;
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
;
167 int init_reader(struct reftable_reader
*r
, struct reftable_block_source
*source
,
170 struct reftable_block footer
= { NULL
};
171 struct reftable_block header
= { NULL
};
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
;
184 err
= block_source_read_block(source
, &header
, 0, read_size
);
185 if (err
!= read_size
) {
186 err
= REFTABLE_IO_ERROR
;
190 if (memcmp(header
.data
, "REFT", 4)) {
191 err
= REFTABLE_FORMAT_ERROR
;
194 r
->version
= header
.data
[4];
195 if (r
->version
!= 1 && r
->version
!= 2) {
196 err
= REFTABLE_FORMAT_ERROR
;
200 r
->size
= file_size
- footer_size(r
->version
);
202 r
->name
= xstrdup(name
);
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
;
212 err
= parse_footer(r
, footer
.data
, header
.data
);
214 reftable_block_done(&footer
);
215 reftable_block_done(&header
);
220 struct reftable_reader
*r
;
223 struct block_iter bi
;
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
)
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
;
251 static void table_iter_block_done(struct table_iter
*ti
)
256 reftable_block_done(&ti
->bi
.br
->block
);
257 FREE_AND_NULL(ti
->bi
.br
);
259 ti
->bi
.last_key
.len
= 0;
263 static int32_t extract_block_size(uint8_t *data
, uint8_t *typ
, uint64_t off
,
269 data
+= header_size(version
);
273 if (reftable_is_block_type(*typ
)) {
274 result
= get_be24(data
+ 1);
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
:
284 struct reftable_block block
= { NULL
};
285 uint8_t block_typ
= 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
)
293 err
= reader_get_block(r
, &block
, next_off
, guess_block_size
);
297 block_size
= extract_block_size(block
.data
, &block_typ
, next_off
,
299 if (block_size
< 0) {
303 if (want_typ
!= BLOCK_TYPE_ANY
&& block_typ
!= want_typ
) {
308 if (block_size
> guess_block_size
) {
309 reftable_block_done(&block
);
310 err
= reader_get_block(r
, &block
, next_off
, block_size
);
316 err
= block_reader_init(br
, &block
, header_off
, r
->block_size
,
317 hash_size(r
->hash_id
));
319 reftable_block_done(&block
);
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 };
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
);
337 dest
->is_finished
= 1;
343 struct block_reader
*brp
=
344 reftable_malloc(sizeof(struct block_reader
));
347 dest
->is_finished
= 0;
348 block_reader_start(brp
, &dest
->bi
);
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
;
359 struct table_iter next
= TABLE_ITER_INIT
;
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
);
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
);
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
)
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
,
420 struct block_reader br
= { 0 };
421 struct block_reader
*brp
= NULL
;
423 int err
= reader_init_block_reader(r
, &br
, off
, typ
);
427 brp
= reftable_malloc(sizeof(struct block_reader
));
430 ti
->typ
= block_reader_type(brp
);
432 block_reader_start(brp
, &ti
->bi
);
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
;
442 off
= offs
->index_offset
;
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
;
461 reftable_record_init(&rec
, reftable_record_type(want
));
462 reftable_record_key(want
, &want_key
);
465 err
= table_iter_next_block(&next
, ti
);
473 err
= block_reader_first_key(next
.bi
.br
, &got_key
);
477 if (strbuf_cmp(&got_key
, &want_key
) > 0) {
478 table_iter_block_done(&next
);
482 table_iter_block_done(ti
);
483 table_iter_copy_from(ti
, &next
);
486 err
= block_iter_seek(&ti
->bi
, &want_key
);
492 block_iter_close(&next
.bi
);
493 reftable_record_release(&rec
);
494 strbuf_release(&want_key
);
495 strbuf_release(&got_key
);
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
;
514 reftable_record_key(rec
, &want_index
.u
.idx
.last_key
);
515 err
= reader_start(r
, &index_iter
, reftable_record_type(rec
), 1);
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
);
530 * Traverse down the levels until we find a non-index entry.
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
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
551 err
= table_iter_next(&index_iter
, &index_result
);
552 table_iter_block_done(&index_iter
);
556 err
= reader_table_iter_at(r
, &next
, index_result
.u
.idx
.offset
,
561 err
= block_iter_seek(&next
.bi
, &want_index
.u
.idx
.last_key
);
565 if (next
.typ
== reftable_record_type(rec
)) {
570 if (next
.typ
!= BLOCK_TYPE_INDEX
) {
571 err
= REFTABLE_FORMAT_ERROR
;
575 table_iter_copy_from(&index_iter
, &next
);
579 struct table_iter empty
= TABLE_ITER_INIT
;
580 struct table_iter
*malloced
= reftable_calloc(1, sizeof(*malloced
));
582 table_iter_copy_from(malloced
, &next
);
583 iterator_from_table_iter(it
, malloced
);
586 block_iter_close(&next
.bi
);
587 table_iter_close(&index_iter
);
588 reftable_record_release(&want_index
);
589 reftable_record_release(&index_result
);
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
;
603 return reader_seek_indexed(r
, it
, rec
);
605 err
= reader_start(r
, &ti
, reftable_record_type(rec
), 0);
608 err
= reader_seek_linear(&ti
, rec
);
612 struct table_iter
*p
=
613 reftable_malloc(sizeof(struct table_iter
));
615 iterator_from_table_iter(it
, p
);
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
);
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
,
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
,
653 .refname
= (char *)name
,
654 .update_index
= update_index
,
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
);
680 block_source_close(src
);
686 void reftable_reader_free(struct reftable_reader
*r
)
694 static int reftable_reader_refs_for_indexed(struct reftable_reader
*r
,
695 struct reftable_iterator
*it
,
698 struct reftable_record want
= {
699 .type
= BLOCK_TYPE_OBJ
,
702 .hash_prefix_len
= r
->object_id_len
,
705 struct reftable_iterator oit
= { NULL
};
706 struct reftable_record got
= {
707 .type
= BLOCK_TYPE_OBJ
,
711 struct indexed_table_ref_iter
*itr
= NULL
;
713 /* Look through the reverse index. */
714 err
= reader_seek(r
, &oit
, &want
);
718 /* read out the reftable_obj_record */
719 err
= iterator_next(&oit
, &got
);
723 if (err
> 0 || memcmp(want
.u
.obj
.hash_prefix
, got
.u
.obj
.hash_prefix
,
725 /* didn't find it; return empty iterator */
726 iterator_set_empty(it
);
731 err
= new_indexed_table_ref_iter(&itr
, r
, oid
, hash_size(r
->hash_id
),
733 got
.u
.obj
.offset_len
);
736 got
.u
.obj
.offsets
= NULL
;
737 iterator_from_indexed_table_ref_iter(it
, itr
);
740 reftable_iterator_destroy(&oit
);
741 reftable_record_release(&got
);
745 static int reftable_reader_refs_for_unindexed(struct reftable_reader
*r
,
746 struct reftable_iterator
*it
,
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
);
757 err
= reader_start(r
, ti
, BLOCK_TYPE_REF
, 0);
763 filter
= reftable_malloc(sizeof(struct filtering_ref_iterator
));
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
);
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
)
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
};
841 err
= reftable_new_reader(&r
, &src
, tablename
);
845 reftable_table_from_reader(&tab
, r
);
846 err
= reftable_table_print(&tab
);
848 reftable_reader_free(r
);