fetch: fetch unpopulated, changed submodules
[git.git] / reftable / reader.c
blob00906e7a2de218085eaec3c8488365cb2da0a0a8
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"
19 #include "tree.h"
21 uint64_t block_source_size(struct reftable_block_source *source)
23 return source->ops->size(source->arg);
26 int block_source_read_block(struct reftable_block_source *source,
27 struct reftable_block *dest, uint64_t off,
28 uint32_t size)
30 int result = source->ops->read_block(source->arg, dest, off, size);
31 dest->source = *source;
32 return result;
35 void block_source_close(struct reftable_block_source *source)
37 if (!source->ops) {
38 return;
41 source->ops->close(source->arg);
42 source->ops = NULL;
45 static struct reftable_reader_offsets *
46 reader_offsets_for(struct reftable_reader *r, uint8_t typ)
48 switch (typ) {
49 case BLOCK_TYPE_REF:
50 return &r->ref_offsets;
51 case BLOCK_TYPE_LOG:
52 return &r->log_offsets;
53 case BLOCK_TYPE_OBJ:
54 return &r->obj_offsets;
56 abort();
59 static int reader_get_block(struct reftable_reader *r,
60 struct reftable_block *dest, uint64_t off,
61 uint32_t sz)
63 if (off >= r->size)
64 return 0;
66 if (off + sz > r->size) {
67 sz = r->size - off;
70 return block_source_read_block(&r->source, dest, off, sz);
73 uint32_t reftable_reader_hash_id(struct reftable_reader *r)
75 return r->hash_id;
78 const char *reader_name(struct reftable_reader *r)
80 return r->name;
83 static int parse_footer(struct reftable_reader *r, uint8_t *footer,
84 uint8_t *header)
86 uint8_t *f = footer;
87 uint8_t first_block_typ;
88 int err = 0;
89 uint32_t computed_crc;
90 uint32_t file_crc;
92 if (memcmp(f, "REFT", 4)) {
93 err = REFTABLE_FORMAT_ERROR;
94 goto done;
96 f += 4;
98 if (memcmp(footer, header, header_size(r->version))) {
99 err = REFTABLE_FORMAT_ERROR;
100 goto done;
103 f++;
104 r->block_size = get_be24(f);
106 f += 3;
107 r->min_update_index = get_be64(f);
108 f += 8;
109 r->max_update_index = get_be64(f);
110 f += 8;
112 if (r->version == 1) {
113 r->hash_id = GIT_SHA1_FORMAT_ID;
114 } else {
115 r->hash_id = get_be32(f);
116 switch (r->hash_id) {
117 case GIT_SHA1_FORMAT_ID:
118 break;
119 case GIT_SHA256_FORMAT_ID:
120 break;
121 default:
122 err = REFTABLE_FORMAT_ERROR;
123 goto done;
125 f += 4;
128 r->ref_offsets.index_offset = get_be64(f);
129 f += 8;
131 r->obj_offsets.offset = get_be64(f);
132 f += 8;
134 r->object_id_len = r->obj_offsets.offset & ((1 << 5) - 1);
135 r->obj_offsets.offset >>= 5;
137 r->obj_offsets.index_offset = get_be64(f);
138 f += 8;
139 r->log_offsets.offset = get_be64(f);
140 f += 8;
141 r->log_offsets.index_offset = get_be64(f);
142 f += 8;
144 computed_crc = crc32(0, footer, f - footer);
145 file_crc = get_be32(f);
146 f += 4;
147 if (computed_crc != file_crc) {
148 err = REFTABLE_FORMAT_ERROR;
149 goto done;
152 first_block_typ = header[header_size(r->version)];
153 r->ref_offsets.is_present = (first_block_typ == BLOCK_TYPE_REF);
154 r->ref_offsets.offset = 0;
155 r->log_offsets.is_present = (first_block_typ == BLOCK_TYPE_LOG ||
156 r->log_offsets.offset > 0);
157 r->obj_offsets.is_present = r->obj_offsets.offset > 0;
158 err = 0;
159 done:
160 return err;
163 int init_reader(struct reftable_reader *r, struct reftable_block_source *source,
164 const char *name)
166 struct reftable_block footer = { NULL };
167 struct reftable_block header = { NULL };
168 int err = 0;
169 uint64_t file_size = block_source_size(source);
171 /* Need +1 to read type of first block. */
172 uint32_t read_size = header_size(2) + 1; /* read v2 because it's larger. */
173 memset(r, 0, sizeof(struct reftable_reader));
175 if (read_size > file_size) {
176 err = REFTABLE_FORMAT_ERROR;
177 goto done;
180 err = block_source_read_block(source, &header, 0, read_size);
181 if (err != read_size) {
182 err = REFTABLE_IO_ERROR;
183 goto done;
186 if (memcmp(header.data, "REFT", 4)) {
187 err = REFTABLE_FORMAT_ERROR;
188 goto done;
190 r->version = header.data[4];
191 if (r->version != 1 && r->version != 2) {
192 err = REFTABLE_FORMAT_ERROR;
193 goto done;
196 r->size = file_size - footer_size(r->version);
197 r->source = *source;
198 r->name = xstrdup(name);
199 r->hash_id = 0;
201 err = block_source_read_block(source, &footer, r->size,
202 footer_size(r->version));
203 if (err != footer_size(r->version)) {
204 err = REFTABLE_IO_ERROR;
205 goto done;
208 err = parse_footer(r, footer.data, header.data);
209 done:
210 reftable_block_done(&footer);
211 reftable_block_done(&header);
212 return err;
215 struct table_iter {
216 struct reftable_reader *r;
217 uint8_t typ;
218 uint64_t block_off;
219 struct block_iter bi;
220 int is_finished;
222 #define TABLE_ITER_INIT \
224 .bi = {.last_key = STRBUF_INIT } \
227 static void table_iter_copy_from(struct table_iter *dest,
228 struct table_iter *src)
230 dest->r = src->r;
231 dest->typ = src->typ;
232 dest->block_off = src->block_off;
233 dest->is_finished = src->is_finished;
234 block_iter_copy_from(&dest->bi, &src->bi);
237 static int table_iter_next_in_block(struct table_iter *ti,
238 struct reftable_record *rec)
240 int res = block_iter_next(&ti->bi, rec);
241 if (res == 0 && reftable_record_type(rec) == BLOCK_TYPE_REF) {
242 rec->u.ref.update_index += ti->r->min_update_index;
245 return res;
248 static void table_iter_block_done(struct table_iter *ti)
250 if (!ti->bi.br) {
251 return;
253 reftable_block_done(&ti->bi.br->block);
254 FREE_AND_NULL(ti->bi.br);
256 ti->bi.last_key.len = 0;
257 ti->bi.next_off = 0;
260 static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off,
261 int version)
263 int32_t result = 0;
265 if (off == 0) {
266 data += header_size(version);
269 *typ = data[0];
270 if (reftable_is_block_type(*typ)) {
271 result = get_be24(data + 1);
273 return result;
276 int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br,
277 uint64_t next_off, uint8_t want_typ)
279 int32_t guess_block_size = r->block_size ? r->block_size :
280 DEFAULT_BLOCK_SIZE;
281 struct reftable_block block = { NULL };
282 uint8_t block_typ = 0;
283 int err = 0;
284 uint32_t header_off = next_off ? 0 : header_size(r->version);
285 int32_t block_size = 0;
287 if (next_off >= r->size)
288 return 1;
290 err = reader_get_block(r, &block, next_off, guess_block_size);
291 if (err < 0)
292 goto done;
294 block_size = extract_block_size(block.data, &block_typ, next_off,
295 r->version);
296 if (block_size < 0) {
297 err = block_size;
298 goto done;
300 if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) {
301 err = 1;
302 goto done;
305 if (block_size > guess_block_size) {
306 reftable_block_done(&block);
307 err = reader_get_block(r, &block, next_off, block_size);
308 if (err < 0) {
309 goto done;
313 err = block_reader_init(br, &block, header_off, r->block_size,
314 hash_size(r->hash_id));
315 done:
316 reftable_block_done(&block);
318 return err;
321 static int table_iter_next_block(struct table_iter *dest,
322 struct table_iter *src)
324 uint64_t next_block_off = src->block_off + src->bi.br->full_block_size;
325 struct block_reader br = { 0 };
326 int err = 0;
328 dest->r = src->r;
329 dest->typ = src->typ;
330 dest->block_off = next_block_off;
332 err = reader_init_block_reader(src->r, &br, next_block_off, src->typ);
333 if (err > 0) {
334 dest->is_finished = 1;
335 return 1;
337 if (err != 0)
338 return err;
339 else {
340 struct block_reader *brp =
341 reftable_malloc(sizeof(struct block_reader));
342 *brp = br;
344 dest->is_finished = 0;
345 block_reader_start(brp, &dest->bi);
347 return 0;
350 static int table_iter_next(struct table_iter *ti, struct reftable_record *rec)
352 if (reftable_record_type(rec) != ti->typ)
353 return REFTABLE_API_ERROR;
355 while (1) {
356 struct table_iter next = TABLE_ITER_INIT;
357 int err = 0;
358 if (ti->is_finished) {
359 return 1;
362 err = table_iter_next_in_block(ti, rec);
363 if (err <= 0) {
364 return err;
367 err = table_iter_next_block(&next, ti);
368 if (err != 0) {
369 ti->is_finished = 1;
371 table_iter_block_done(ti);
372 if (err != 0) {
373 return err;
375 table_iter_copy_from(ti, &next);
376 block_iter_close(&next.bi);
380 static int table_iter_next_void(void *ti, struct reftable_record *rec)
382 return table_iter_next(ti, rec);
385 static void table_iter_close(void *p)
387 struct table_iter *ti = p;
388 table_iter_block_done(ti);
389 block_iter_close(&ti->bi);
392 static struct reftable_iterator_vtable table_iter_vtable = {
393 .next = &table_iter_next_void,
394 .close = &table_iter_close,
397 static void iterator_from_table_iter(struct reftable_iterator *it,
398 struct table_iter *ti)
400 assert(!it->ops);
401 it->iter_arg = ti;
402 it->ops = &table_iter_vtable;
405 static int reader_table_iter_at(struct reftable_reader *r,
406 struct table_iter *ti, uint64_t off,
407 uint8_t typ)
409 struct block_reader br = { 0 };
410 struct block_reader *brp = NULL;
412 int err = reader_init_block_reader(r, &br, off, typ);
413 if (err != 0)
414 return err;
416 brp = reftable_malloc(sizeof(struct block_reader));
417 *brp = br;
418 ti->r = r;
419 ti->typ = block_reader_type(brp);
420 ti->block_off = off;
421 block_reader_start(brp, &ti->bi);
422 return 0;
425 static int reader_start(struct reftable_reader *r, struct table_iter *ti,
426 uint8_t typ, int index)
428 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
429 uint64_t off = offs->offset;
430 if (index) {
431 off = offs->index_offset;
432 if (off == 0) {
433 return 1;
435 typ = BLOCK_TYPE_INDEX;
438 return reader_table_iter_at(r, ti, off, typ);
441 static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti,
442 struct reftable_record *want)
444 struct reftable_record rec =
445 reftable_new_record(reftable_record_type(want));
446 struct strbuf want_key = STRBUF_INIT;
447 struct strbuf got_key = STRBUF_INIT;
448 struct table_iter next = TABLE_ITER_INIT;
449 int err = -1;
451 reftable_record_key(want, &want_key);
453 while (1) {
454 err = table_iter_next_block(&next, ti);
455 if (err < 0)
456 goto done;
458 if (err > 0) {
459 break;
462 err = block_reader_first_key(next.bi.br, &got_key);
463 if (err < 0)
464 goto done;
466 if (strbuf_cmp(&got_key, &want_key) > 0) {
467 table_iter_block_done(&next);
468 break;
471 table_iter_block_done(ti);
472 table_iter_copy_from(ti, &next);
475 err = block_iter_seek(&ti->bi, &want_key);
476 if (err < 0)
477 goto done;
478 err = 0;
480 done:
481 block_iter_close(&next.bi);
482 reftable_record_release(&rec);
483 strbuf_release(&want_key);
484 strbuf_release(&got_key);
485 return err;
488 static int reader_seek_indexed(struct reftable_reader *r,
489 struct reftable_iterator *it,
490 struct reftable_record *rec)
492 struct reftable_record want_index = {
493 .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }
495 struct reftable_record index_result = {
496 .type = BLOCK_TYPE_INDEX,
497 .u.idx = { .last_key = STRBUF_INIT },
499 struct table_iter index_iter = TABLE_ITER_INIT;
500 struct table_iter next = TABLE_ITER_INIT;
501 int err = 0;
503 reftable_record_key(rec, &want_index.u.idx.last_key);
504 err = reader_start(r, &index_iter, reftable_record_type(rec), 1);
505 if (err < 0)
506 goto done;
508 err = reader_seek_linear(r, &index_iter, &want_index);
509 while (1) {
510 err = table_iter_next(&index_iter, &index_result);
511 table_iter_block_done(&index_iter);
512 if (err != 0)
513 goto done;
515 err = reader_table_iter_at(r, &next, index_result.u.idx.offset,
517 if (err != 0)
518 goto done;
520 err = block_iter_seek(&next.bi, &want_index.u.idx.last_key);
521 if (err < 0)
522 goto done;
524 if (next.typ == reftable_record_type(rec)) {
525 err = 0;
526 break;
529 if (next.typ != BLOCK_TYPE_INDEX) {
530 err = REFTABLE_FORMAT_ERROR;
531 break;
534 table_iter_copy_from(&index_iter, &next);
537 if (err == 0) {
538 struct table_iter empty = TABLE_ITER_INIT;
539 struct table_iter *malloced =
540 reftable_calloc(sizeof(struct table_iter));
541 *malloced = empty;
542 table_iter_copy_from(malloced, &next);
543 iterator_from_table_iter(it, malloced);
545 done:
546 block_iter_close(&next.bi);
547 table_iter_close(&index_iter);
548 reftable_record_release(&want_index);
549 reftable_record_release(&index_result);
550 return err;
553 static int reader_seek_internal(struct reftable_reader *r,
554 struct reftable_iterator *it,
555 struct reftable_record *rec)
557 struct reftable_reader_offsets *offs =
558 reader_offsets_for(r, reftable_record_type(rec));
559 uint64_t idx = offs->index_offset;
560 struct table_iter ti = TABLE_ITER_INIT;
561 int err = 0;
562 if (idx > 0)
563 return reader_seek_indexed(r, it, rec);
565 err = reader_start(r, &ti, reftable_record_type(rec), 0);
566 if (err < 0)
567 return err;
568 err = reader_seek_linear(r, &ti, rec);
569 if (err < 0)
570 return err;
571 else {
572 struct table_iter *p =
573 reftable_malloc(sizeof(struct table_iter));
574 *p = ti;
575 iterator_from_table_iter(it, p);
578 return 0;
581 static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it,
582 struct reftable_record *rec)
584 uint8_t typ = reftable_record_type(rec);
586 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
587 if (!offs->is_present) {
588 iterator_set_empty(it);
589 return 0;
592 return reader_seek_internal(r, it, rec);
595 int reftable_reader_seek_ref(struct reftable_reader *r,
596 struct reftable_iterator *it, const char *name)
598 struct reftable_record rec = {
599 .type = BLOCK_TYPE_REF,
600 .u.ref = {
601 .refname = (char *)name,
604 return reader_seek(r, it, &rec);
607 int reftable_reader_seek_log_at(struct reftable_reader *r,
608 struct reftable_iterator *it, const char *name,
609 uint64_t update_index)
611 struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
612 .u.log = {
613 .refname = (char *)name,
614 .update_index = update_index,
615 } };
616 return reader_seek(r, it, &rec);
619 int reftable_reader_seek_log(struct reftable_reader *r,
620 struct reftable_iterator *it, const char *name)
622 uint64_t max = ~((uint64_t)0);
623 return reftable_reader_seek_log_at(r, it, name, max);
626 void reader_close(struct reftable_reader *r)
628 block_source_close(&r->source);
629 FREE_AND_NULL(r->name);
632 int reftable_new_reader(struct reftable_reader **p,
633 struct reftable_block_source *src, char const *name)
635 struct reftable_reader *rd =
636 reftable_calloc(sizeof(struct reftable_reader));
637 int err = init_reader(rd, src, name);
638 if (err == 0) {
639 *p = rd;
640 } else {
641 block_source_close(src);
642 reftable_free(rd);
644 return err;
647 void reftable_reader_free(struct reftable_reader *r)
649 if (!r)
650 return;
651 reader_close(r);
652 reftable_free(r);
655 static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
656 struct reftable_iterator *it,
657 uint8_t *oid)
659 struct reftable_record want = {
660 .type = BLOCK_TYPE_OBJ,
661 .u.obj = {
662 .hash_prefix = oid,
663 .hash_prefix_len = r->object_id_len,
666 struct reftable_iterator oit = { NULL };
667 struct reftable_record got = {
668 .type = BLOCK_TYPE_OBJ,
669 .u.obj = { 0 },
671 int err = 0;
672 struct indexed_table_ref_iter *itr = NULL;
674 /* Look through the reverse index. */
675 err = reader_seek(r, &oit, &want);
676 if (err != 0)
677 goto done;
679 /* read out the reftable_obj_record */
680 err = iterator_next(&oit, &got);
681 if (err < 0)
682 goto done;
684 if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix,
685 r->object_id_len)) {
686 /* didn't find it; return empty iterator */
687 iterator_set_empty(it);
688 err = 0;
689 goto done;
692 err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id),
693 got.u.obj.offsets,
694 got.u.obj.offset_len);
695 if (err < 0)
696 goto done;
697 got.u.obj.offsets = NULL;
698 iterator_from_indexed_table_ref_iter(it, itr);
700 done:
701 reftable_iterator_destroy(&oit);
702 reftable_record_release(&got);
703 return err;
706 static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
707 struct reftable_iterator *it,
708 uint8_t *oid)
710 struct table_iter ti_empty = TABLE_ITER_INIT;
711 struct table_iter *ti = reftable_calloc(sizeof(struct table_iter));
712 struct filtering_ref_iterator *filter = NULL;
713 struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT;
714 int oid_len = hash_size(r->hash_id);
715 int err;
717 *ti = ti_empty;
718 err = reader_start(r, ti, BLOCK_TYPE_REF, 0);
719 if (err < 0) {
720 reftable_free(ti);
721 return err;
724 filter = reftable_malloc(sizeof(struct filtering_ref_iterator));
725 *filter = empty;
727 strbuf_add(&filter->oid, oid, oid_len);
728 reftable_table_from_reader(&filter->tab, r);
729 filter->double_check = 0;
730 iterator_from_table_iter(&filter->it, ti);
732 iterator_from_filtering_ref_iterator(it, filter);
733 return 0;
736 int reftable_reader_refs_for(struct reftable_reader *r,
737 struct reftable_iterator *it, uint8_t *oid)
739 if (r->obj_offsets.is_present)
740 return reftable_reader_refs_for_indexed(r, it, oid);
741 return reftable_reader_refs_for_unindexed(r, it, oid);
744 uint64_t reftable_reader_max_update_index(struct reftable_reader *r)
746 return r->max_update_index;
749 uint64_t reftable_reader_min_update_index(struct reftable_reader *r)
751 return r->min_update_index;
754 /* generic table interface. */
756 static int reftable_reader_seek_void(void *tab, struct reftable_iterator *it,
757 struct reftable_record *rec)
759 return reader_seek(tab, it, rec);
762 static uint32_t reftable_reader_hash_id_void(void *tab)
764 return reftable_reader_hash_id(tab);
767 static uint64_t reftable_reader_min_update_index_void(void *tab)
769 return reftable_reader_min_update_index(tab);
772 static uint64_t reftable_reader_max_update_index_void(void *tab)
774 return reftable_reader_max_update_index(tab);
777 static struct reftable_table_vtable reader_vtable = {
778 .seek_record = reftable_reader_seek_void,
779 .hash_id = reftable_reader_hash_id_void,
780 .min_update_index = reftable_reader_min_update_index_void,
781 .max_update_index = reftable_reader_max_update_index_void,
784 void reftable_table_from_reader(struct reftable_table *tab,
785 struct reftable_reader *reader)
787 assert(!tab->ops);
788 tab->ops = &reader_vtable;
789 tab->table_arg = reader;
793 int reftable_reader_print_file(const char *tablename)
795 struct reftable_block_source src = { NULL };
796 int err = reftable_block_source_from_file(&src, tablename);
797 struct reftable_reader *r = NULL;
798 struct reftable_table tab = { NULL };
799 if (err < 0)
800 goto done;
802 err = reftable_new_reader(&r, &src, tablename);
803 if (err < 0)
804 goto done;
806 reftable_table_from_reader(&tab, r);
807 err = reftable_table_print(&tab);
808 done:
809 reftable_reader_free(r);
810 return err;