Merge branch 'gh/gpg-doc-markup-fix'
[git/debian.git] / reftable / reader.c
blob006709a645aea2ddbc914673710e66fa2343d569
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 ((struct reftable_ref_record *)rec->data)->update_index +=
243 ti->r->min_update_index;
246 return res;
249 static void table_iter_block_done(struct table_iter *ti)
251 if (!ti->bi.br) {
252 return;
254 reftable_block_done(&ti->bi.br->block);
255 FREE_AND_NULL(ti->bi.br);
257 ti->bi.last_key.len = 0;
258 ti->bi.next_off = 0;
261 static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off,
262 int version)
264 int32_t result = 0;
266 if (off == 0) {
267 data += header_size(version);
270 *typ = data[0];
271 if (reftable_is_block_type(*typ)) {
272 result = get_be24(data + 1);
274 return result;
277 int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br,
278 uint64_t next_off, uint8_t want_typ)
280 int32_t guess_block_size = r->block_size ? r->block_size :
281 DEFAULT_BLOCK_SIZE;
282 struct reftable_block block = { NULL };
283 uint8_t block_typ = 0;
284 int err = 0;
285 uint32_t header_off = next_off ? 0 : header_size(r->version);
286 int32_t block_size = 0;
288 if (next_off >= r->size)
289 return 1;
291 err = reader_get_block(r, &block, next_off, guess_block_size);
292 if (err < 0)
293 return err;
295 block_size = extract_block_size(block.data, &block_typ, next_off,
296 r->version);
297 if (block_size < 0)
298 return block_size;
300 if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) {
301 reftable_block_done(&block);
302 return 1;
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 return err;
313 return block_reader_init(br, &block, header_off, r->block_size,
314 hash_size(r->hash_id));
317 static int table_iter_next_block(struct table_iter *dest,
318 struct table_iter *src)
320 uint64_t next_block_off = src->block_off + src->bi.br->full_block_size;
321 struct block_reader br = { 0 };
322 int err = 0;
324 dest->r = src->r;
325 dest->typ = src->typ;
326 dest->block_off = next_block_off;
328 err = reader_init_block_reader(src->r, &br, next_block_off, src->typ);
329 if (err > 0) {
330 dest->is_finished = 1;
331 return 1;
333 if (err != 0)
334 return err;
335 else {
336 struct block_reader *brp =
337 reftable_malloc(sizeof(struct block_reader));
338 *brp = br;
340 dest->is_finished = 0;
341 block_reader_start(brp, &dest->bi);
343 return 0;
346 static int table_iter_next(struct table_iter *ti, struct reftable_record *rec)
348 if (reftable_record_type(rec) != ti->typ)
349 return REFTABLE_API_ERROR;
351 while (1) {
352 struct table_iter next = TABLE_ITER_INIT;
353 int err = 0;
354 if (ti->is_finished) {
355 return 1;
358 err = table_iter_next_in_block(ti, rec);
359 if (err <= 0) {
360 return err;
363 err = table_iter_next_block(&next, ti);
364 if (err != 0) {
365 ti->is_finished = 1;
367 table_iter_block_done(ti);
368 if (err != 0) {
369 return err;
371 table_iter_copy_from(ti, &next);
372 block_iter_close(&next.bi);
376 static int table_iter_next_void(void *ti, struct reftable_record *rec)
378 return table_iter_next(ti, rec);
381 static void table_iter_close(void *p)
383 struct table_iter *ti = p;
384 table_iter_block_done(ti);
385 block_iter_close(&ti->bi);
388 static struct reftable_iterator_vtable table_iter_vtable = {
389 .next = &table_iter_next_void,
390 .close = &table_iter_close,
393 static void iterator_from_table_iter(struct reftable_iterator *it,
394 struct table_iter *ti)
396 assert(!it->ops);
397 it->iter_arg = ti;
398 it->ops = &table_iter_vtable;
401 static int reader_table_iter_at(struct reftable_reader *r,
402 struct table_iter *ti, uint64_t off,
403 uint8_t typ)
405 struct block_reader br = { 0 };
406 struct block_reader *brp = NULL;
408 int err = reader_init_block_reader(r, &br, off, typ);
409 if (err != 0)
410 return err;
412 brp = reftable_malloc(sizeof(struct block_reader));
413 *brp = br;
414 ti->r = r;
415 ti->typ = block_reader_type(brp);
416 ti->block_off = off;
417 block_reader_start(brp, &ti->bi);
418 return 0;
421 static int reader_start(struct reftable_reader *r, struct table_iter *ti,
422 uint8_t typ, int index)
424 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
425 uint64_t off = offs->offset;
426 if (index) {
427 off = offs->index_offset;
428 if (off == 0) {
429 return 1;
431 typ = BLOCK_TYPE_INDEX;
434 return reader_table_iter_at(r, ti, off, typ);
437 static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti,
438 struct reftable_record *want)
440 struct reftable_record rec =
441 reftable_new_record(reftable_record_type(want));
442 struct strbuf want_key = STRBUF_INIT;
443 struct strbuf got_key = STRBUF_INIT;
444 struct table_iter next = TABLE_ITER_INIT;
445 int err = -1;
447 reftable_record_key(want, &want_key);
449 while (1) {
450 err = table_iter_next_block(&next, ti);
451 if (err < 0)
452 goto done;
454 if (err > 0) {
455 break;
458 err = block_reader_first_key(next.bi.br, &got_key);
459 if (err < 0)
460 goto done;
462 if (strbuf_cmp(&got_key, &want_key) > 0) {
463 table_iter_block_done(&next);
464 break;
467 table_iter_block_done(ti);
468 table_iter_copy_from(ti, &next);
471 err = block_iter_seek(&ti->bi, &want_key);
472 if (err < 0)
473 goto done;
474 err = 0;
476 done:
477 block_iter_close(&next.bi);
478 reftable_record_destroy(&rec);
479 strbuf_release(&want_key);
480 strbuf_release(&got_key);
481 return err;
484 static int reader_seek_indexed(struct reftable_reader *r,
485 struct reftable_iterator *it,
486 struct reftable_record *rec)
488 struct reftable_index_record want_index = { .last_key = STRBUF_INIT };
489 struct reftable_record want_index_rec = { NULL };
490 struct reftable_index_record index_result = { .last_key = STRBUF_INIT };
491 struct reftable_record index_result_rec = { NULL };
492 struct table_iter index_iter = TABLE_ITER_INIT;
493 struct table_iter next = TABLE_ITER_INIT;
494 int err = 0;
496 reftable_record_key(rec, &want_index.last_key);
497 reftable_record_from_index(&want_index_rec, &want_index);
498 reftable_record_from_index(&index_result_rec, &index_result);
500 err = reader_start(r, &index_iter, reftable_record_type(rec), 1);
501 if (err < 0)
502 goto done;
504 err = reader_seek_linear(r, &index_iter, &want_index_rec);
505 while (1) {
506 err = table_iter_next(&index_iter, &index_result_rec);
507 table_iter_block_done(&index_iter);
508 if (err != 0)
509 goto done;
511 err = reader_table_iter_at(r, &next, index_result.offset, 0);
512 if (err != 0)
513 goto done;
515 err = block_iter_seek(&next.bi, &want_index.last_key);
516 if (err < 0)
517 goto done;
519 if (next.typ == reftable_record_type(rec)) {
520 err = 0;
521 break;
524 if (next.typ != BLOCK_TYPE_INDEX) {
525 err = REFTABLE_FORMAT_ERROR;
526 break;
529 table_iter_copy_from(&index_iter, &next);
532 if (err == 0) {
533 struct table_iter empty = TABLE_ITER_INIT;
534 struct table_iter *malloced =
535 reftable_calloc(sizeof(struct table_iter));
536 *malloced = empty;
537 table_iter_copy_from(malloced, &next);
538 iterator_from_table_iter(it, malloced);
540 done:
541 block_iter_close(&next.bi);
542 table_iter_close(&index_iter);
543 reftable_record_release(&want_index_rec);
544 reftable_record_release(&index_result_rec);
545 return err;
548 static int reader_seek_internal(struct reftable_reader *r,
549 struct reftable_iterator *it,
550 struct reftable_record *rec)
552 struct reftable_reader_offsets *offs =
553 reader_offsets_for(r, reftable_record_type(rec));
554 uint64_t idx = offs->index_offset;
555 struct table_iter ti = TABLE_ITER_INIT;
556 int err = 0;
557 if (idx > 0)
558 return reader_seek_indexed(r, it, rec);
560 err = reader_start(r, &ti, reftable_record_type(rec), 0);
561 if (err < 0)
562 return err;
563 err = reader_seek_linear(r, &ti, rec);
564 if (err < 0)
565 return err;
566 else {
567 struct table_iter *p =
568 reftable_malloc(sizeof(struct table_iter));
569 *p = ti;
570 iterator_from_table_iter(it, p);
573 return 0;
576 static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it,
577 struct reftable_record *rec)
579 uint8_t typ = reftable_record_type(rec);
581 struct reftable_reader_offsets *offs = reader_offsets_for(r, typ);
582 if (!offs->is_present) {
583 iterator_set_empty(it);
584 return 0;
587 return reader_seek_internal(r, it, rec);
590 int reftable_reader_seek_ref(struct reftable_reader *r,
591 struct reftable_iterator *it, const char *name)
593 struct reftable_ref_record ref = {
594 .refname = (char *)name,
596 struct reftable_record rec = { NULL };
597 reftable_record_from_ref(&rec, &ref);
598 return reader_seek(r, it, &rec);
601 int reftable_reader_seek_log_at(struct reftable_reader *r,
602 struct reftable_iterator *it, const char *name,
603 uint64_t update_index)
605 struct reftable_log_record log = {
606 .refname = (char *)name,
607 .update_index = update_index,
609 struct reftable_record rec = { NULL };
610 reftable_record_from_log(&rec, &log);
611 return reader_seek(r, it, &rec);
614 int reftable_reader_seek_log(struct reftable_reader *r,
615 struct reftable_iterator *it, const char *name)
617 uint64_t max = ~((uint64_t)0);
618 return reftable_reader_seek_log_at(r, it, name, max);
621 void reader_close(struct reftable_reader *r)
623 block_source_close(&r->source);
624 FREE_AND_NULL(r->name);
627 int reftable_new_reader(struct reftable_reader **p,
628 struct reftable_block_source *src, char const *name)
630 struct reftable_reader *rd =
631 reftable_calloc(sizeof(struct reftable_reader));
632 int err = init_reader(rd, src, name);
633 if (err == 0) {
634 *p = rd;
635 } else {
636 block_source_close(src);
637 reftable_free(rd);
639 return err;
642 void reftable_reader_free(struct reftable_reader *r)
644 reader_close(r);
645 reftable_free(r);
648 static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
649 struct reftable_iterator *it,
650 uint8_t *oid)
652 struct reftable_obj_record want = {
653 .hash_prefix = oid,
654 .hash_prefix_len = r->object_id_len,
656 struct reftable_record want_rec = { NULL };
657 struct reftable_iterator oit = { NULL };
658 struct reftable_obj_record got = { NULL };
659 struct reftable_record got_rec = { NULL };
660 int err = 0;
661 struct indexed_table_ref_iter *itr = NULL;
663 /* Look through the reverse index. */
664 reftable_record_from_obj(&want_rec, &want);
665 err = reader_seek(r, &oit, &want_rec);
666 if (err != 0)
667 goto done;
669 /* read out the reftable_obj_record */
670 reftable_record_from_obj(&got_rec, &got);
671 err = iterator_next(&oit, &got_rec);
672 if (err < 0)
673 goto done;
675 if (err > 0 ||
676 memcmp(want.hash_prefix, got.hash_prefix, r->object_id_len)) {
677 /* didn't find it; return empty iterator */
678 iterator_set_empty(it);
679 err = 0;
680 goto done;
683 err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id),
684 got.offsets, got.offset_len);
685 if (err < 0)
686 goto done;
687 got.offsets = NULL;
688 iterator_from_indexed_table_ref_iter(it, itr);
690 done:
691 reftable_iterator_destroy(&oit);
692 reftable_record_release(&got_rec);
693 return err;
696 static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
697 struct reftable_iterator *it,
698 uint8_t *oid)
700 struct table_iter ti_empty = TABLE_ITER_INIT;
701 struct table_iter *ti = reftable_calloc(sizeof(struct table_iter));
702 struct filtering_ref_iterator *filter = NULL;
703 struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT;
704 int oid_len = hash_size(r->hash_id);
705 int err;
707 *ti = ti_empty;
708 err = reader_start(r, ti, BLOCK_TYPE_REF, 0);
709 if (err < 0) {
710 reftable_free(ti);
711 return err;
714 filter = reftable_malloc(sizeof(struct filtering_ref_iterator));
715 *filter = empty;
717 strbuf_add(&filter->oid, oid, oid_len);
718 reftable_table_from_reader(&filter->tab, r);
719 filter->double_check = 0;
720 iterator_from_table_iter(&filter->it, ti);
722 iterator_from_filtering_ref_iterator(it, filter);
723 return 0;
726 int reftable_reader_refs_for(struct reftable_reader *r,
727 struct reftable_iterator *it, uint8_t *oid)
729 if (r->obj_offsets.is_present)
730 return reftable_reader_refs_for_indexed(r, it, oid);
731 return reftable_reader_refs_for_unindexed(r, it, oid);
734 uint64_t reftable_reader_max_update_index(struct reftable_reader *r)
736 return r->max_update_index;
739 uint64_t reftable_reader_min_update_index(struct reftable_reader *r)
741 return r->min_update_index;
744 /* generic table interface. */
746 static int reftable_reader_seek_void(void *tab, struct reftable_iterator *it,
747 struct reftable_record *rec)
749 return reader_seek(tab, it, rec);
752 static uint32_t reftable_reader_hash_id_void(void *tab)
754 return reftable_reader_hash_id(tab);
757 static uint64_t reftable_reader_min_update_index_void(void *tab)
759 return reftable_reader_min_update_index(tab);
762 static uint64_t reftable_reader_max_update_index_void(void *tab)
764 return reftable_reader_max_update_index(tab);
767 static struct reftable_table_vtable reader_vtable = {
768 .seek_record = reftable_reader_seek_void,
769 .hash_id = reftable_reader_hash_id_void,
770 .min_update_index = reftable_reader_min_update_index_void,
771 .max_update_index = reftable_reader_max_update_index_void,
774 void reftable_table_from_reader(struct reftable_table *tab,
775 struct reftable_reader *reader)
777 assert(!tab->ops);
778 tab->ops = &reader_vtable;
779 tab->table_arg = reader;
783 int reftable_reader_print_file(const char *tablename)
785 struct reftable_block_source src = { NULL };
786 int err = reftable_block_source_from_file(&src, tablename);
787 struct reftable_reader *r = NULL;
788 struct reftable_table tab = { NULL };
789 if (err < 0)
790 goto done;
792 err = reftable_new_reader(&r, &src, tablename);
793 if (err < 0)
794 goto done;
796 reftable_table_from_reader(&tab, r);
797 err = reftable_table_print(&tab);
798 done:
799 reftable_reader_free(r);
800 return err;