Merge branch 'hn/reftable-coverity-fixes'

Problems identified by Coverity in the reftable code have been
corrected.

* hn/reftable-coverity-fixes:
  reftable: add print functions to the record types
  reftable: make reftable_record a tagged union
  reftable: remove outdated file reftable.c
  reftable: implement record equality generically
  reftable: make reftable-record.h function signatures const correct
  reftable: handle null refnames in reftable_ref_record_equal
  reftable: drop stray printf in readwrite_test
  reftable: order unittests by complexity
  reftable: all xxx_free() functions accept NULL arguments
  reftable: fix resource warning
  reftable: ignore remove() return value in stack_test.c
  reftable: check reftable_stack_auto_compact() return value
  reftable: fix resource leak blocksource.c
  reftable: fix resource leak in block.c error path
  reftable: fix OOB stack write in print functions
This commit is contained in:
Junio C Hamano 2022-02-16 15:14:27 -08:00
Родитель dd77ff8181 01033de49f
Коммит 34230514b8
19 изменённых файлов: 622 добавлений и 531 удалений

Просмотреть файл

@ -188,13 +188,16 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
uint32_t full_block_size = table_block_size;
uint8_t typ = block->data[header_off];
uint32_t sz = get_be24(block->data + header_off + 1);
int err = 0;
uint16_t restart_count = 0;
uint32_t restart_start = 0;
uint8_t *restart_bytes = NULL;
uint8_t *uncompressed = NULL;
if (!reftable_is_block_type(typ))
return REFTABLE_FORMAT_ERROR;
if (!reftable_is_block_type(typ)) {
err = REFTABLE_FORMAT_ERROR;
goto done;
}
if (typ == BLOCK_TYPE_LOG) {
int block_header_skip = 4 + header_off;
@ -203,7 +206,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
uLongf src_len = block->len - block_header_skip;
/* Log blocks specify the *uncompressed* size in their header.
*/
uint8_t *uncompressed = reftable_malloc(sz);
uncompressed = reftable_malloc(sz);
/* Copy over the block header verbatim. It's not compressed. */
memcpy(uncompressed, block->data, block_header_skip);
@ -212,16 +215,19 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
if (Z_OK !=
uncompress2(uncompressed + block_header_skip, &dst_len,
block->data + block_header_skip, &src_len)) {
reftable_free(uncompressed);
return REFTABLE_ZLIB_ERROR;
err = REFTABLE_ZLIB_ERROR;
goto done;
}
if (dst_len + block_header_skip != sz)
return REFTABLE_FORMAT_ERROR;
if (dst_len + block_header_skip != sz) {
err = REFTABLE_FORMAT_ERROR;
goto done;
}
/* We're done with the input data. */
reftable_block_done(block);
block->data = uncompressed;
uncompressed = NULL;
block->len = sz;
block->source = malloc_block_source();
full_block_size = src_len + block_header_skip;
@ -251,7 +257,9 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
br->restart_count = restart_count;
br->restart_bytes = restart_bytes;
return 0;
done:
reftable_free(uncompressed);
return err;
}
static uint32_t block_reader_restart_offset(struct block_reader *br, int i)
@ -413,7 +421,7 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
done:
strbuf_release(&key);
strbuf_release(&next.last_key);
reftable_record_destroy(&rec);
reftable_record_release(&rec);
return err;
}

Просмотреть файл

@ -26,8 +26,9 @@ static void test_block_read_write(void)
struct block_writer bw = {
.last_key = STRBUF_INIT,
};
struct reftable_ref_record ref = { NULL };
struct reftable_record rec = { NULL };
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
};
int i = 0;
int n;
struct block_reader br = { 0 };
@ -40,7 +41,6 @@ static void test_block_read_write(void)
block.source = malloc_block_source();
block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
header_off, hash_size(GIT_SHA1_FORMAT_ID));
reftable_record_from_ref(&rec, &ref);
for (i = 0; i < N; i++) {
char name[100];
@ -48,14 +48,14 @@ static void test_block_read_write(void)
snprintf(name, sizeof(name), "branch%02d", i);
memset(hash, i, sizeof(hash));
ref.refname = name;
ref.value_type = REFTABLE_REF_VAL1;
ref.value.val1 = hash;
rec.u.ref.refname = name;
rec.u.ref.value_type = REFTABLE_REF_VAL1;
rec.u.ref.value.val1 = hash;
names[i] = xstrdup(name);
n = block_writer_add(&bw, &rec);
ref.refname = NULL;
ref.value_type = REFTABLE_REF_DELETION;
rec.u.ref.refname = NULL;
rec.u.ref.value_type = REFTABLE_REF_DELETION;
EXPECT(n == 0);
}
@ -74,7 +74,7 @@ static void test_block_read_write(void)
if (r > 0) {
break;
}
EXPECT_STREQ(names[j], ref.refname);
EXPECT_STREQ(names[j], rec.u.ref.refname);
j++;
}
@ -92,7 +92,7 @@ static void test_block_read_write(void)
n = block_iter_next(&it, &rec);
EXPECT(n == 0);
EXPECT_STREQ(names[i], ref.refname);
EXPECT_STREQ(names[i], rec.u.ref.refname);
want.len--;
n = block_reader_seek(&br, &it, &want);
@ -100,7 +100,7 @@ static void test_block_read_write(void)
n = block_iter_next(&it, &rec);
EXPECT(n == 0);
EXPECT_STREQ(names[10 * (i / 10)], ref.refname);
EXPECT_STREQ(names[10 * (i / 10)], rec.u.ref.refname);
block_iter_close(&it);
}

Просмотреть файл

@ -134,8 +134,10 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
}
err = fstat(fd, &st);
if (err < 0)
return -1;
if (err < 0) {
close(fd);
return REFTABLE_IO_ERROR;
}
p = reftable_calloc(sizeof(struct file_block_source));
p->size = st.st_size;

Просмотреть файл

@ -7,6 +7,7 @@ https://developers.google.com/open-source/licenses/bsd
*/
#include "basics.h"
#include "constants.h"
#include "record.h"
#include "generic.h"
#include "reftable-iterator.h"
@ -15,23 +16,21 @@ https://developers.google.com/open-source/licenses/bsd
int reftable_table_seek_ref(struct reftable_table *tab,
struct reftable_iterator *it, const char *name)
{
struct reftable_ref_record ref = {
.refname = (char *)name,
};
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, &ref);
struct reftable_record rec = { .type = BLOCK_TYPE_REF,
.u.ref = {
.refname = (char *)name,
} };
return tab->ops->seek_record(tab->table_arg, it, &rec);
}
int reftable_table_seek_log(struct reftable_table *tab,
struct reftable_iterator *it, const char *name)
{
struct reftable_log_record log = {
.refname = (char *)name,
.update_index = ~((uint64_t)0),
};
struct reftable_record rec = { NULL };
reftable_record_from_log(&rec, &log);
struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
.u.log = {
.refname = (char *)name,
.update_index = ~((uint64_t)0),
} };
return tab->ops->seek_record(tab->table_arg, it, &rec);
}
@ -129,17 +128,25 @@ void reftable_iterator_destroy(struct reftable_iterator *it)
int reftable_iterator_next_ref(struct reftable_iterator *it,
struct reftable_ref_record *ref)
{
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, ref);
return iterator_next(it, &rec);
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
.u.ref = *ref,
};
int err = iterator_next(it, &rec);
*ref = rec.u.ref;
return err;
}
int reftable_iterator_next_log(struct reftable_iterator *it,
struct reftable_log_record *log)
{
struct reftable_record rec = { NULL };
reftable_record_from_log(&rec, log);
return iterator_next(it, &rec);
struct reftable_record rec = {
.type = BLOCK_TYPE_LOG,
.u.log = *log,
};
int err = iterator_next(it, &rec);
*log = rec.u.log;
return err;
}
int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)

Просмотреть файл

@ -32,7 +32,7 @@ static int filtering_ref_iterator_next(void *iter_arg,
struct reftable_record *rec)
{
struct filtering_ref_iterator *fri = iter_arg;
struct reftable_ref_record *ref = rec->data;
struct reftable_ref_record *ref = &rec->u.ref;
int err = 0;
while (1) {
err = reftable_iterator_next_ref(&fri->it, ref);
@ -127,7 +127,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
{
struct indexed_table_ref_iter *it = p;
struct reftable_ref_record *ref = rec->data;
struct reftable_ref_record *ref = &rec->u.ref;
while (1) {
int err = block_iter_next(&it->cur, rec);

Просмотреть файл

@ -30,7 +30,7 @@ static int merged_iter_init(struct merged_iter *mi)
if (err > 0) {
reftable_iterator_destroy(&mi->stack[i]);
reftable_record_destroy(&rec);
reftable_record_release(&rec);
} else {
struct pq_entry e = {
.rec = rec,
@ -57,18 +57,17 @@ static void merged_iter_close(void *p)
static int merged_iter_advance_nonnull_subiter(struct merged_iter *mi,
size_t idx)
{
struct reftable_record rec = reftable_new_record(mi->typ);
struct pq_entry e = {
.rec = rec,
.rec = reftable_new_record(mi->typ),
.index = idx,
};
int err = iterator_next(&mi->stack[idx], &rec);
int err = iterator_next(&mi->stack[idx], &e.rec);
if (err < 0)
return err;
if (err > 0) {
reftable_iterator_destroy(&mi->stack[idx]);
reftable_record_destroy(&rec);
reftable_record_release(&e.rec);
return 0;
}
@ -126,11 +125,11 @@ static int merged_iter_next_entry(struct merged_iter *mi,
if (err < 0) {
return err;
}
reftable_record_destroy(&top.rec);
reftable_record_release(&top.rec);
}
reftable_record_copy_from(rec, &entry.rec, hash_size(mi->hash_id));
reftable_record_destroy(&entry.rec);
reftable_record_release(&entry.rec);
strbuf_release(&entry_key);
return 0;
}
@ -290,11 +289,12 @@ int reftable_merged_table_seek_ref(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name)
{
struct reftable_ref_record ref = {
.refname = (char *)name,
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
.u.ref = {
.refname = (char *)name,
},
};
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, &ref);
return merged_table_seek_record(mt, it, &rec);
}
@ -302,12 +302,11 @@ int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name, uint64_t update_index)
{
struct reftable_log_record log = {
.refname = (char *)name,
.update_index = update_index,
};
struct reftable_record rec = { NULL };
reftable_record_from_log(&rec, &log);
struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
.u.log = {
.refname = (char *)name,
.update_index = update_index,
} };
return merged_table_seek_record(mt, it, &rec);
}

Просмотреть файл

@ -74,6 +74,7 @@ struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq)
void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, struct pq_entry e)
{
int i = 0;
if (pq->len == pq->cap) {
pq->cap = 2 * pq->cap + 1;
pq->heap = reftable_realloc(pq->heap,
@ -98,7 +99,7 @@ void merged_iter_pqueue_release(struct merged_iter_pqueue *pq)
{
int i = 0;
for (i = 0; i < pq->len; i++) {
reftable_record_destroy(&pq->heap[i].rec);
reftable_record_release(&pq->heap[i].rec);
}
FREE_AND_NULL(pq->heap);
pq->len = pq->cap = 0;

Просмотреть файл

@ -31,7 +31,7 @@ static void test_pq(void)
int N = ARRAY_SIZE(names) - 1;
struct merged_iter_pqueue pq = { NULL };
const char *last = NULL;
char *last = NULL;
int i = 0;
for (i = 0; i < N; i++) {
@ -42,12 +42,10 @@ static void test_pq(void)
i = 1;
do {
struct reftable_record rec =
reftable_new_record(BLOCK_TYPE_REF);
struct pq_entry e = { 0 };
reftable_record_as_ref(&rec)->refname = names[i];
e.rec = rec;
struct pq_entry e = { .rec = { .type = BLOCK_TYPE_REF,
.u.ref = {
.refname = names[i],
} } };
merged_iter_pqueue_add(&pq, e);
merged_iter_pqueue_check(pq);
i = (i * 7) % N;
@ -55,19 +53,18 @@ static void test_pq(void)
while (!merged_iter_pqueue_is_empty(pq)) {
struct pq_entry e = merged_iter_pqueue_remove(&pq);
struct reftable_ref_record *ref =
reftable_record_as_ref(&e.rec);
struct reftable_record *rec = &e.rec;
merged_iter_pqueue_check(pq);
EXPECT(reftable_record_type(rec) == BLOCK_TYPE_REF);
if (last) {
EXPECT(strcmp(last, ref->refname) < 0);
EXPECT(strcmp(last, rec->u.ref.refname) < 0);
}
last = ref->refname;
ref->refname = NULL;
reftable_free(ref);
// this is names[i], so don't dealloc.
last = rec->u.ref.refname;
rec->u.ref.refname = NULL;
reftable_record_release(rec);
}
for (i = 0; i < N; i++) {
reftable_free(names[i]);
}

Просмотреть файл

@ -239,8 +239,7 @@ static int table_iter_next_in_block(struct table_iter *ti,
{
int res = block_iter_next(&ti->bi, rec);
if (res == 0 && reftable_record_type(rec) == BLOCK_TYPE_REF) {
((struct reftable_ref_record *)rec->data)->update_index +=
ti->r->min_update_index;
rec->u.ref.update_index += ti->r->min_update_index;
}
return res;
@ -290,28 +289,33 @@ int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br,
err = reader_get_block(r, &block, next_off, guess_block_size);
if (err < 0)
return err;
goto done;
block_size = extract_block_size(block.data, &block_typ, next_off,
r->version);
if (block_size < 0)
return block_size;
if (block_size < 0) {
err = block_size;
goto done;
}
if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) {
reftable_block_done(&block);
return 1;
err = 1;
goto done;
}
if (block_size > guess_block_size) {
reftable_block_done(&block);
err = reader_get_block(r, &block, next_off, block_size);
if (err < 0) {
return err;
goto done;
}
}
return block_reader_init(br, &block, header_off, r->block_size,
hash_size(r->hash_id));
err = block_reader_init(br, &block, header_off, r->block_size,
hash_size(r->hash_id));
done:
reftable_block_done(&block);
return err;
}
static int table_iter_next_block(struct table_iter *dest,
@ -475,7 +479,7 @@ static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti,
done:
block_iter_close(&next.bi);
reftable_record_destroy(&rec);
reftable_record_release(&rec);
strbuf_release(&want_key);
strbuf_release(&got_key);
return err;
@ -485,34 +489,35 @@ static int reader_seek_indexed(struct reftable_reader *r,
struct reftable_iterator *it,
struct reftable_record *rec)
{
struct reftable_index_record want_index = { .last_key = STRBUF_INIT };
struct reftable_record want_index_rec = { NULL };
struct reftable_index_record index_result = { .last_key = STRBUF_INIT };
struct reftable_record index_result_rec = { NULL };
struct reftable_record want_index = {
.type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }
};
struct reftable_record index_result = {
.type = BLOCK_TYPE_INDEX,
.u.idx = { .last_key = STRBUF_INIT },
};
struct table_iter index_iter = TABLE_ITER_INIT;
struct table_iter next = TABLE_ITER_INIT;
int err = 0;
reftable_record_key(rec, &want_index.last_key);
reftable_record_from_index(&want_index_rec, &want_index);
reftable_record_from_index(&index_result_rec, &index_result);
reftable_record_key(rec, &want_index.u.idx.last_key);
err = reader_start(r, &index_iter, reftable_record_type(rec), 1);
if (err < 0)
goto done;
err = reader_seek_linear(r, &index_iter, &want_index_rec);
err = reader_seek_linear(r, &index_iter, &want_index);
while (1) {
err = table_iter_next(&index_iter, &index_result_rec);
err = table_iter_next(&index_iter, &index_result);
table_iter_block_done(&index_iter);
if (err != 0)
goto done;
err = reader_table_iter_at(r, &next, index_result.offset, 0);
err = reader_table_iter_at(r, &next, index_result.u.idx.offset,
0);
if (err != 0)
goto done;
err = block_iter_seek(&next.bi, &want_index.last_key);
err = block_iter_seek(&next.bi, &want_index.u.idx.last_key);
if (err < 0)
goto done;
@ -540,8 +545,8 @@ static int reader_seek_indexed(struct reftable_reader *r,
done:
block_iter_close(&next.bi);
table_iter_close(&index_iter);
reftable_record_release(&want_index_rec);
reftable_record_release(&index_result_rec);
reftable_record_release(&want_index);
reftable_record_release(&index_result);
return err;
}
@ -590,11 +595,12 @@ static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it,
int reftable_reader_seek_ref(struct reftable_reader *r,
struct reftable_iterator *it, const char *name)
{
struct reftable_ref_record ref = {
.refname = (char *)name,
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
.u.ref = {
.refname = (char *)name,
},
};
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, &ref);
return reader_seek(r, it, &rec);
}
@ -602,12 +608,11 @@ int reftable_reader_seek_log_at(struct reftable_reader *r,
struct reftable_iterator *it, const char *name,
uint64_t update_index)
{
struct reftable_log_record log = {
.refname = (char *)name,
.update_index = update_index,
};
struct reftable_record rec = { NULL };
reftable_record_from_log(&rec, &log);
struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
.u.log = {
.refname = (char *)name,
.update_index = update_index,
} };
return reader_seek(r, it, &rec);
}
@ -641,6 +646,8 @@ int reftable_new_reader(struct reftable_reader **p,
void reftable_reader_free(struct reftable_reader *r)
{
if (!r)
return;
reader_close(r);
reftable_free(r);
}
@ -649,31 +656,33 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
struct reftable_iterator *it,
uint8_t *oid)
{
struct reftable_obj_record want = {
.hash_prefix = oid,
.hash_prefix_len = r->object_id_len,
struct reftable_record want = {
.type = BLOCK_TYPE_OBJ,
.u.obj = {
.hash_prefix = oid,
.hash_prefix_len = r->object_id_len,
},
};
struct reftable_record want_rec = { NULL };
struct reftable_iterator oit = { NULL };
struct reftable_obj_record got = { NULL };
struct reftable_record got_rec = { NULL };
struct reftable_record got = {
.type = BLOCK_TYPE_OBJ,
.u.obj = { 0 },
};
int err = 0;
struct indexed_table_ref_iter *itr = NULL;
/* Look through the reverse index. */
reftable_record_from_obj(&want_rec, &want);
err = reader_seek(r, &oit, &want_rec);
err = reader_seek(r, &oit, &want);
if (err != 0)
goto done;
/* read out the reftable_obj_record */
reftable_record_from_obj(&got_rec, &got);
err = iterator_next(&oit, &got_rec);
err = iterator_next(&oit, &got);
if (err < 0)
goto done;
if (err > 0 ||
memcmp(want.hash_prefix, got.hash_prefix, r->object_id_len)) {
if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix,
r->object_id_len)) {
/* didn't find it; return empty iterator */
iterator_set_empty(it);
err = 0;
@ -681,15 +690,16 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
}
err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id),
got.offsets, got.offset_len);
got.u.obj.offsets,
got.u.obj.offset_len);
if (err < 0)
goto done;
got.offsets = NULL;
got.u.obj.offsets = NULL;
iterator_from_indexed_table_ref_iter(it, itr);
done:
reftable_iterator_destroy(&oit);
reftable_record_release(&got_rec);
reftable_record_release(&got);
return err;
}

Просмотреть файл

@ -288,6 +288,71 @@ static void test_log_write_read(void)
reader_close(&rd);
}
static void test_log_zlib_corruption(void)
{
struct reftable_write_options opts = {
.block_size = 256,
};
struct reftable_iterator it = { 0 };
struct reftable_reader rd = { 0 };
struct reftable_block_source source = { 0 };
struct strbuf buf = STRBUF_INIT;
struct reftable_writer *w =
reftable_new_writer(&strbuf_add_void, &buf, &opts);
const struct reftable_stats *stats = NULL;
uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 };
uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 };
char message[100] = { 0 };
int err, i, n;
struct reftable_log_record log = {
.refname = "refname",
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.new_hash = hash1,
.old_hash = hash2,
.name = "My Name",
.email = "myname@invalid",
.message = message,
},
},
};
for (i = 0; i < sizeof(message) - 1; i++)
message[i] = (uint8_t)(rand() % 64 + ' ');
reftable_writer_set_limits(w, 1, 1);
err = reftable_writer_add_log(w, &log);
EXPECT_ERR(err);
n = reftable_writer_close(w);
EXPECT(n == 0);
stats = writer_stats(w);
EXPECT(stats->log_stats.blocks > 0);
reftable_writer_free(w);
w = NULL;
/* corrupt the data. */
buf.buf[50] ^= 0x99;
block_source_from_strbuf(&source, &buf);
err = init_reader(&rd, &source, "file.log");
EXPECT_ERR(err);
err = reftable_reader_seek_log(&rd, &it, "refname");
EXPECT(err == REFTABLE_ZLIB_ERROR);
reftable_iterator_destroy(&it);
/* cleanup. */
strbuf_release(&buf);
reader_close(&rd);
}
static void test_table_read_write_sequential(void)
{
char **names;
@ -631,7 +696,6 @@ static void test_write_key_order(void)
err = reftable_writer_add_ref(w, &refs[0]);
EXPECT_ERR(err);
err = reftable_writer_add_ref(w, &refs[1]);
printf("%d\n", err);
EXPECT(err == REFTABLE_API_ERROR);
reftable_writer_close(w);
reftable_writer_free(w);
@ -667,6 +731,7 @@ static void test_corrupt_table(void)
int readwrite_test_main(int argc, const char *argv[])
{
RUN_TEST(test_log_zlib_corruption);
RUN_TEST(test_corrupt_table);
RUN_TEST(test_corrupt_table_empty);
RUN_TEST(test_log_write_read);

Просмотреть файл

@ -15,6 +15,10 @@ https://developers.google.com/open-source/licenses/bsd
#include "reftable-error.h"
#include "basics.h"
static struct reftable_record_vtable *
reftable_record_vtable(struct reftable_record *rec);
static void *reftable_record_data(struct reftable_record *rec);
int get_var_int(uint64_t *dest, struct string_view *in)
{
int ptr = 0;
@ -72,7 +76,7 @@ int reftable_is_block_type(uint8_t typ)
return 0;
}
uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec)
uint8_t *reftable_ref_record_val1(const struct reftable_ref_record *rec)
{
switch (rec->value_type) {
case REFTABLE_REF_VAL1:
@ -84,7 +88,7 @@ uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec)
}
}
uint8_t *reftable_ref_record_val2(struct reftable_ref_record *rec)
uint8_t *reftable_ref_record_val2(const struct reftable_ref_record *rec)
{
switch (rec->value_type) {
case REFTABLE_REF_VAL2:
@ -251,24 +255,24 @@ static void hex_format(char *dest, uint8_t *src, int hash_size)
}
}
void reftable_ref_record_print(struct reftable_ref_record *ref,
uint32_t hash_id)
static void reftable_ref_record_print_sz(const struct reftable_ref_record *ref,
int hash_size)
{
char hex[2 * GIT_SHA256_RAWSZ + 1] = { 0 }; /* BUG */
char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index);
switch (ref->value_type) {
case REFTABLE_REF_SYMREF:
printf("=> %s", ref->value.symref);
break;
case REFTABLE_REF_VAL2:
hex_format(hex, ref->value.val2.value, hash_size(hash_id));
hex_format(hex, ref->value.val2.value, hash_size);
printf("val 2 %s", hex);
hex_format(hex, ref->value.val2.target_value,
hash_size(hash_id));
hash_size);
printf("(T %s)", hex);
break;
case REFTABLE_REF_VAL1:
hex_format(hex, ref->value.val1, hash_size(hash_id));
hex_format(hex, ref->value.val1, hash_size);
printf("val 1 %s", hex);
break;
case REFTABLE_REF_DELETION:
@ -278,6 +282,11 @@ void reftable_ref_record_print(struct reftable_ref_record *ref,
printf("}\n");
}
void reftable_ref_record_print(const struct reftable_ref_record *ref,
uint32_t hash_id) {
reftable_ref_record_print_sz(ref, hash_size(hash_id));
}
static void reftable_ref_record_release_void(void *rec)
{
reftable_ref_record_release(rec);
@ -430,6 +439,21 @@ static int reftable_ref_record_is_deletion_void(const void *p)
(const struct reftable_ref_record *)p);
}
static int reftable_ref_record_equal_void(const void *a,
const void *b, int hash_size)
{
struct reftable_ref_record *ra = (struct reftable_ref_record *) a;
struct reftable_ref_record *rb = (struct reftable_ref_record *) b;
return reftable_ref_record_equal(ra, rb, hash_size);
}
static void reftable_ref_record_print_void(const void *rec,
int hash_size)
{
reftable_ref_record_print_sz((struct reftable_ref_record *) rec, hash_size);
}
static struct reftable_record_vtable reftable_ref_record_vtable = {
.key = &reftable_ref_record_key,
.type = BLOCK_TYPE_REF,
@ -439,6 +463,8 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
.decode = &reftable_ref_record_decode,
.release = &reftable_ref_record_release_void,
.is_deletion = &reftable_ref_record_is_deletion_void,
.equal = &reftable_ref_record_equal_void,
.print = &reftable_ref_record_print_void,
};
static void reftable_obj_record_key(const void *r, struct strbuf *dest)
@ -457,6 +483,21 @@ static void reftable_obj_record_release(void *rec)
memset(obj, 0, sizeof(struct reftable_obj_record));
}
static void reftable_obj_record_print(const void *rec, int hash_size)
{
const struct reftable_obj_record *obj = rec;
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
struct strbuf offset_str = STRBUF_INIT;
int i;
for (i = 0; i < obj->offset_len; i++)
strbuf_addf(&offset_str, "%" PRIu64 " ", obj->offsets[i]);
hex_format(hex, obj->hash_prefix, obj->hash_prefix_len);
printf("prefix %s (len %d), offsets [%s]\n",
hex, obj->hash_prefix_len, offset_str.buf);
strbuf_release(&offset_str);
}
static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
int hash_size)
{
@ -465,12 +506,14 @@ static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
(const struct reftable_obj_record *)src_rec;
reftable_obj_record_release(obj);
*obj = *src;
obj->hash_prefix = reftable_malloc(obj->hash_prefix_len);
memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);
obj->hash_prefix = reftable_malloc(src->hash_prefix_len);
obj->hash_prefix_len = src->hash_prefix_len;
if (src->hash_prefix_len)
memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);
obj->offsets = reftable_malloc(obj->offset_len * sizeof(uint64_t));
COPY_ARRAY(obj->offsets, src->offsets, obj->offset_len);
obj->offsets = reftable_malloc(src->offset_len * sizeof(uint64_t));
obj->offset_len = src->offset_len;
COPY_ARRAY(obj->offsets, src->offsets, src->offset_len);
}
static uint8_t reftable_obj_record_val_type(const void *rec)
@ -572,6 +615,25 @@ static int not_a_deletion(const void *p)
return 0;
}
static int reftable_obj_record_equal_void(const void *a, const void *b, int hash_size)
{
struct reftable_obj_record *ra = (struct reftable_obj_record *) a;
struct reftable_obj_record *rb = (struct reftable_obj_record *) b;
if (ra->hash_prefix_len != rb->hash_prefix_len
|| ra->offset_len != rb->offset_len)
return 0;
if (ra->hash_prefix_len &&
memcmp(ra->hash_prefix, rb->hash_prefix, ra->hash_prefix_len))
return 0;
if (ra->offset_len &&
memcmp(ra->offsets, rb->offsets, ra->offset_len * sizeof(uint64_t)))
return 0;
return 1;
}
static struct reftable_record_vtable reftable_obj_record_vtable = {
.key = &reftable_obj_record_key,
.type = BLOCK_TYPE_OBJ,
@ -580,32 +642,43 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
.encode = &reftable_obj_record_encode,
.decode = &reftable_obj_record_decode,
.release = &reftable_obj_record_release,
.is_deletion = not_a_deletion,
.is_deletion = &not_a_deletion,
.equal = &reftable_obj_record_equal_void,
.print = &reftable_obj_record_print,
};
void reftable_log_record_print(struct reftable_log_record *log,
uint32_t hash_id)
static void reftable_log_record_print_sz(struct reftable_log_record *log,
int hash_size)
{
char hex[GIT_SHA256_RAWSZ + 1] = { 0 };
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
switch (log->value_type) {
case REFTABLE_LOG_DELETION:
printf("log{%s(%" PRIu64 ") delete", log->refname,
printf("log{%s(%" PRIu64 ") delete\n", log->refname,
log->update_index);
break;
case REFTABLE_LOG_UPDATE:
printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
log->refname, log->update_index, log->value.update.name,
log->value.update.email, log->value.update.time,
log->refname, log->update_index,
log->value.update.name ? log->value.update.name : "",
log->value.update.email ? log->value.update.email : "",
log->value.update.time,
log->value.update.tz_offset);
hex_format(hex, log->value.update.old_hash, hash_size(hash_id));
hex_format(hex, log->value.update.old_hash, hash_size);
printf("%s => ", hex);
hex_format(hex, log->value.update.new_hash, hash_size(hash_id));
printf("%s\n\n%s\n}\n", hex, log->value.update.message);
hex_format(hex, log->value.update.new_hash, hash_size);
printf("%s\n\n%s\n}\n", hex,
log->value.update.message ? log->value.update.message : "");
break;
}
}
void reftable_log_record_print(struct reftable_log_record *log,
uint32_t hash_id)
{
reftable_log_record_print_sz(log, hash_size(hash_id));
}
static void reftable_log_record_key(const void *r, struct strbuf *dest)
{
const struct reftable_log_record *rec =
@ -881,8 +954,16 @@ static int zero_hash_eq(uint8_t *a, uint8_t *b, int sz)
return !memcmp(a, b, sz);
}
int reftable_log_record_equal(struct reftable_log_record *a,
struct reftable_log_record *b, int hash_size)
static int reftable_log_record_equal_void(const void *a,
const void *b, int hash_size)
{
return reftable_log_record_equal((struct reftable_log_record *) a,
(struct reftable_log_record *) b,
hash_size);
}
int reftable_log_record_equal(const struct reftable_log_record *a,
const struct reftable_log_record *b, int hash_size)
{
if (!(null_streq(a->refname, b->refname) &&
a->update_index == b->update_index &&
@ -915,6 +996,11 @@ static int reftable_log_record_is_deletion_void(const void *p)
(const struct reftable_log_record *)p);
}
static void reftable_log_record_print_void(const void *rec, int hash_size)
{
reftable_log_record_print_sz((struct reftable_log_record*)rec, hash_size);
}
static struct reftable_record_vtable reftable_log_record_vtable = {
.key = &reftable_log_record_key,
.type = BLOCK_TYPE_LOG,
@ -924,60 +1010,10 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
.decode = &reftable_log_record_decode,
.release = &reftable_log_record_release_void,
.is_deletion = &reftable_log_record_is_deletion_void,
.equal = &reftable_log_record_equal_void,
.print = &reftable_log_record_print_void,
};
struct reftable_record reftable_new_record(uint8_t typ)
{
struct reftable_record rec = { NULL };
switch (typ) {
case BLOCK_TYPE_REF: {
struct reftable_ref_record *r =
reftable_calloc(sizeof(struct reftable_ref_record));
reftable_record_from_ref(&rec, r);
return rec;
}
case BLOCK_TYPE_OBJ: {
struct reftable_obj_record *r =
reftable_calloc(sizeof(struct reftable_obj_record));
reftable_record_from_obj(&rec, r);
return rec;
}
case BLOCK_TYPE_LOG: {
struct reftable_log_record *r =
reftable_calloc(sizeof(struct reftable_log_record));
reftable_record_from_log(&rec, r);
return rec;
}
case BLOCK_TYPE_INDEX: {
struct reftable_index_record empty = { .last_key =
STRBUF_INIT };
struct reftable_index_record *r =
reftable_calloc(sizeof(struct reftable_index_record));
*r = empty;
reftable_record_from_index(&rec, r);
return rec;
}
}
abort();
return rec;
}
/* clear out the record, yielding the reftable_record data that was
* encapsulated. */
static void *reftable_record_yield(struct reftable_record *rec)
{
void *p = rec->data;
rec->data = NULL;
return p;
}
void reftable_record_destroy(struct reftable_record *rec)
{
reftable_record_release(rec);
reftable_free(reftable_record_yield(rec));
}
static void reftable_index_record_key(const void *r, struct strbuf *dest)
{
const struct reftable_index_record *rec = r;
@ -1042,6 +1078,21 @@ static int reftable_index_record_decode(void *rec, struct strbuf key,
return start.len - in.len;
}
static int reftable_index_record_equal(const void *a, const void *b, int hash_size)
{
struct reftable_index_record *ia = (struct reftable_index_record *) a;
struct reftable_index_record *ib = (struct reftable_index_record *) b;
return ia->offset == ib->offset && !strbuf_cmp(&ia->last_key, &ib->last_key);
}
static void reftable_index_record_print(const void *rec, int hash_size)
{
const struct reftable_index_record *idx = rec;
/* TODO: escape null chars? */
printf("\"%s\" %" PRIu64 "\n", idx->last_key.buf, idx->offset);
}
static struct reftable_record_vtable reftable_index_record_vtable = {
.key = &reftable_index_record_key,
.type = BLOCK_TYPE_INDEX,
@ -1051,95 +1102,66 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
.decode = &reftable_index_record_decode,
.release = &reftable_index_record_release,
.is_deletion = &not_a_deletion,
.equal = &reftable_index_record_equal,
.print = &reftable_index_record_print,
};
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest)
{
rec->ops->key(rec->data, dest);
reftable_record_vtable(rec)->key(reftable_record_data(rec), dest);
}
uint8_t reftable_record_type(struct reftable_record *rec)
{
return rec->ops->type;
return rec->type;
}
int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
int hash_size)
{
return rec->ops->encode(rec->data, dest, hash_size);
return reftable_record_vtable(rec)->encode(reftable_record_data(rec),
dest, hash_size);
}
void reftable_record_copy_from(struct reftable_record *rec,
struct reftable_record *src, int hash_size)
{
assert(src->ops->type == rec->ops->type);
assert(src->type == rec->type);
rec->ops->copy_from(rec->data, src->data, hash_size);
reftable_record_vtable(rec)->copy_from(reftable_record_data(rec),
reftable_record_data(src),
hash_size);
}
uint8_t reftable_record_val_type(struct reftable_record *rec)
{
return rec->ops->val_type(rec->data);
return reftable_record_vtable(rec)->val_type(reftable_record_data(rec));
}
int reftable_record_decode(struct reftable_record *rec, struct strbuf key,
uint8_t extra, struct string_view src, int hash_size)
{
return rec->ops->decode(rec->data, key, extra, src, hash_size);
return reftable_record_vtable(rec)->decode(reftable_record_data(rec),
key, extra, src, hash_size);
}
void reftable_record_release(struct reftable_record *rec)
{
rec->ops->release(rec->data);
reftable_record_vtable(rec)->release(reftable_record_data(rec));
}
int reftable_record_is_deletion(struct reftable_record *rec)
{
return rec->ops->is_deletion(rec->data);
return reftable_record_vtable(rec)->is_deletion(
reftable_record_data(rec));
}
void reftable_record_from_ref(struct reftable_record *rec,
struct reftable_ref_record *ref_rec)
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size)
{
assert(!rec->ops);
rec->data = ref_rec;
rec->ops = &reftable_ref_record_vtable;
}
void reftable_record_from_obj(struct reftable_record *rec,
struct reftable_obj_record *obj_rec)
{
assert(!rec->ops);
rec->data = obj_rec;
rec->ops = &reftable_obj_record_vtable;
}
void reftable_record_from_index(struct reftable_record *rec,
struct reftable_index_record *index_rec)
{
assert(!rec->ops);
rec->data = index_rec;
rec->ops = &reftable_index_record_vtable;
}
void reftable_record_from_log(struct reftable_record *rec,
struct reftable_log_record *log_rec)
{
assert(!rec->ops);
rec->data = log_rec;
rec->ops = &reftable_log_record_vtable;
}
struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *rec)
{
assert(reftable_record_type(rec) == BLOCK_TYPE_REF);
return rec->data;
}
struct reftable_log_record *reftable_record_as_log(struct reftable_record *rec)
{
assert(reftable_record_type(rec) == BLOCK_TYPE_LOG);
return rec->data;
if (a->type != b->type)
return 0;
return reftable_record_vtable(a)->equal(
reftable_record_data(a), reftable_record_data(b), hash_size);
}
static int hash_equal(uint8_t *a, uint8_t *b, int hash_size)
@ -1150,13 +1172,15 @@ static int hash_equal(uint8_t *a, uint8_t *b, int hash_size)
return a == b;
}
int reftable_ref_record_equal(struct reftable_ref_record *a,
struct reftable_ref_record *b, int hash_size)
int reftable_ref_record_equal(const struct reftable_ref_record *a,
const struct reftable_ref_record *b, int hash_size)
{
assert(hash_size > 0);
if (!(0 == strcmp(a->refname, b->refname) &&
a->update_index == b->update_index &&
a->value_type == b->value_type))
if (!null_streq(a->refname, b->refname))
return 0;
if (a->update_index != b->update_index ||
a->value_type != b->value_type)
return 0;
switch (a->value_type) {
@ -1210,3 +1234,81 @@ void string_view_consume(struct string_view *s, int n)
s->buf += n;
s->len -= n;
}
static void *reftable_record_data(struct reftable_record *rec)
{
switch (rec->type) {
case BLOCK_TYPE_REF:
return &rec->u.ref;
case BLOCK_TYPE_LOG:
return &rec->u.log;
case BLOCK_TYPE_INDEX:
return &rec->u.idx;
case BLOCK_TYPE_OBJ:
return &rec->u.obj;
}
abort();
}
static struct reftable_record_vtable *
reftable_record_vtable(struct reftable_record *rec)
{
switch (rec->type) {
case BLOCK_TYPE_REF:
return &reftable_ref_record_vtable;
case BLOCK_TYPE_LOG:
return &reftable_log_record_vtable;
case BLOCK_TYPE_INDEX:
return &reftable_index_record_vtable;
case BLOCK_TYPE_OBJ:
return &reftable_obj_record_vtable;
}
abort();
}
struct reftable_record reftable_new_record(uint8_t typ)
{
struct reftable_record clean = {
.type = typ,
};
/* the following is involved, but the naive solution (just return
* `clean` as is, except for BLOCK_TYPE_INDEX), returns a garbage
* clean.u.obj.offsets pointer on Windows VS CI. Go figure.
*/
switch (typ) {
case BLOCK_TYPE_OBJ:
{
struct reftable_obj_record obj = { 0 };
clean.u.obj = obj;
break;
}
case BLOCK_TYPE_INDEX:
{
struct reftable_index_record idx = {
.last_key = STRBUF_INIT,
};
clean.u.idx = idx;
break;
}
case BLOCK_TYPE_REF:
{
struct reftable_ref_record ref = { 0 };
clean.u.ref = ref;
break;
}
case BLOCK_TYPE_LOG:
{
struct reftable_log_record log = { 0 };
clean.u.log = log;
break;
}
}
return clean;
}
void reftable_record_print(struct reftable_record *rec, int hash_size)
{
printf("'%c': ", rec->type);
reftable_record_vtable(rec)->print(reftable_record_data(rec), hash_size);
}

Просмотреть файл

@ -58,18 +58,18 @@ struct reftable_record_vtable {
/* is this a tombstone? */
int (*is_deletion)(const void *rec);
};
/* record is a generic wrapper for different types of records. */
struct reftable_record {
void *data;
struct reftable_record_vtable *ops;
/* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */
int (*equal)(const void *a, const void *b, int hash_size);
/* Print on stdout, for debugging. */
void (*print)(const void *rec, int hash_size);
};
/* returns true for recognized block types. Block start with the block type. */
int reftable_is_block_type(uint8_t typ);
/* creates a malloced record of the given type. Dispose with record_destroy */
/* return an initialized record for the given type */
struct reftable_record reftable_new_record(uint8_t typ);
/* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns
@ -97,8 +97,25 @@ struct reftable_obj_record {
int offset_len;
};
/* see struct record_vtable */
/* record is a generic wrapper for different types of records. It is normally
* created on the stack, or embedded within another struct. If the type is
* known, a fresh instance can be initialized explicitly. Otherwise, use
* reftable_new_record() to initialize generically (as the index_record is not
* valid as 0-initialized structure)
*/
struct reftable_record {
uint8_t type;
union {
struct reftable_ref_record ref;
struct reftable_log_record log;
struct reftable_obj_record obj;
struct reftable_index_record idx;
} u;
};
/* see struct record_vtable */
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
void reftable_record_print(struct reftable_record *rec, int hash_size);
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
uint8_t reftable_record_type(struct reftable_record *rec);
void reftable_record_copy_from(struct reftable_record *rec,
@ -111,25 +128,9 @@ int reftable_record_decode(struct reftable_record *rec, struct strbuf key,
int hash_size);
int reftable_record_is_deletion(struct reftable_record *rec);
/* zeroes out the embedded record */
/* frees and zeroes out the embedded record */
void reftable_record_release(struct reftable_record *rec);
/* clear and deallocate embedded record, and zero `rec`. */
void reftable_record_destroy(struct reftable_record *rec);
/* initialize generic records from concrete records. The generic record should
* be zeroed out. */
void reftable_record_from_obj(struct reftable_record *rec,
struct reftable_obj_record *objrec);
void reftable_record_from_index(struct reftable_record *rec,
struct reftable_index_record *idxrec);
void reftable_record_from_ref(struct reftable_record *rec,
struct reftable_ref_record *refrec);
void reftable_record_from_log(struct reftable_record *rec,
struct reftable_log_record *logrec);
struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *ref);
struct reftable_log_record *reftable_record_as_log(struct reftable_record *ref);
/* for qsort. */
int reftable_ref_record_compare_name(const void *a, const void *b);

Просмотреть файл

@ -16,24 +16,20 @@
static void test_copy(struct reftable_record *rec)
{
struct reftable_record copy =
reftable_new_record(reftable_record_type(rec));
struct reftable_record copy = { 0 };
uint8_t typ;
typ = reftable_record_type(rec);
copy = reftable_new_record(typ);
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
/* do it twice to catch memory leaks */
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
switch (reftable_record_type(&copy)) {
case BLOCK_TYPE_REF:
EXPECT(reftable_ref_record_equal(reftable_record_as_ref(&copy),
reftable_record_as_ref(rec),
GIT_SHA1_RAWSZ));
break;
case BLOCK_TYPE_LOG:
EXPECT(reftable_log_record_equal(reftable_record_as_log(&copy),
reftable_record_as_log(rec),
GIT_SHA1_RAWSZ));
break;
}
reftable_record_destroy(&copy);
EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
puts("testing print coverage:\n");
reftable_record_print(&copy, GIT_SHA1_RAWSZ);
reftable_record_release(&copy);
}
static void test_varint_roundtrip(void)
@ -106,61 +102,58 @@ static void test_reftable_ref_record_roundtrip(void)
int i = 0;
for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
struct reftable_ref_record in = { NULL };
struct reftable_ref_record out = { NULL };
struct reftable_record rec_out = { NULL };
struct reftable_record in = {
.type = BLOCK_TYPE_REF,
};
struct reftable_record out = { .type = BLOCK_TYPE_REF };
struct strbuf key = STRBUF_INIT;
struct reftable_record rec = { NULL };
uint8_t buffer[1024] = { 0 };
struct string_view dest = {
.buf = buffer,
.len = sizeof(buffer),
};
int n, m;
in.value_type = i;
in.u.ref.value_type = i;
switch (i) {
case REFTABLE_REF_DELETION:
break;
case REFTABLE_REF_VAL1:
in.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ);
set_hash(in.value.val1, 1);
in.u.ref.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ);
set_hash(in.u.ref.value.val1, 1);
break;
case REFTABLE_REF_VAL2:
in.value.val2.value = reftable_malloc(GIT_SHA1_RAWSZ);
set_hash(in.value.val2.value, 1);
in.value.val2.target_value =
in.u.ref.value.val2.value =
reftable_malloc(GIT_SHA1_RAWSZ);
set_hash(in.value.val2.target_value, 2);
set_hash(in.u.ref.value.val2.value, 1);
in.u.ref.value.val2.target_value =
reftable_malloc(GIT_SHA1_RAWSZ);
set_hash(in.u.ref.value.val2.target_value, 2);
break;
case REFTABLE_REF_SYMREF:
in.value.symref = xstrdup("target");
in.u.ref.value.symref = xstrdup("target");
break;
}
in.refname = xstrdup("refs/heads/master");
in.u.ref.refname = xstrdup("refs/heads/master");
reftable_record_from_ref(&rec, &in);
test_copy(&rec);
test_copy(&in);
EXPECT(reftable_record_val_type(&rec) == i);
EXPECT(reftable_record_val_type(&in) == i);
reftable_record_key(&rec, &key);
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
reftable_record_key(&in, &key);
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0);
/* decode into a non-zero reftable_record to test for leaks. */
reftable_record_from_ref(&rec_out, &out);
m = reftable_record_decode(&rec_out, key, i, dest,
GIT_SHA1_RAWSZ);
m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ);
EXPECT(n == m);
EXPECT(reftable_ref_record_equal(&in, &out, GIT_SHA1_RAWSZ));
reftable_record_release(&rec_out);
EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
GIT_SHA1_RAWSZ));
reftable_record_release(&in);
strbuf_release(&key);
reftable_ref_record_release(&in);
reftable_record_release(&out);
}
}
@ -187,7 +180,8 @@ static void test_reftable_log_record_equal(void)
static void test_reftable_log_record_roundtrip(void)
{
int i;
struct reftable_log_record in[2] = {
struct reftable_log_record in[] = {
{
.refname = xstrdup("refs/heads/master"),
.update_index = 42,
@ -208,12 +202,26 @@ static void test_reftable_log_record_roundtrip(void)
.refname = xstrdup("refs/heads/master"),
.update_index = 22,
.value_type = REFTABLE_LOG_DELETION,
},
{
.refname = xstrdup("branch"),
.update_index = 33,
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
.new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
/* rest of fields left empty. */
},
},
}
};
set_test_hash(in[0].value.update.new_hash, 1);
set_test_hash(in[0].value.update.old_hash, 2);
set_test_hash(in[2].value.update.new_hash, 3);
set_test_hash(in[2].value.update.old_hash, 4);
for (i = 0; i < ARRAY_SIZE(in); i++) {
struct reftable_record rec = { NULL };
struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
struct strbuf key = STRBUF_INIT;
uint8_t buffer[1024] = { 0 };
struct string_view dest = {
@ -221,23 +229,25 @@ static void test_reftable_log_record_roundtrip(void)
.len = sizeof(buffer),
};
/* populate out, to check for leaks. */
struct reftable_log_record out = {
.refname = xstrdup("old name"),
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.new_hash = reftable_calloc(GIT_SHA1_RAWSZ),
.old_hash = reftable_calloc(GIT_SHA1_RAWSZ),
.name = xstrdup("old name"),
.email = xstrdup("old@email"),
.message = xstrdup("old message"),
struct reftable_record out = {
.type = BLOCK_TYPE_LOG,
.u.log = {
.refname = xstrdup("old name"),
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.new_hash = reftable_calloc(GIT_SHA1_RAWSZ),
.old_hash = reftable_calloc(GIT_SHA1_RAWSZ),
.name = xstrdup("old name"),
.email = xstrdup("old@email"),
.message = xstrdup("old message"),
},
},
},
};
struct reftable_record rec_out = { NULL };
int n, m, valtype;
reftable_record_from_log(&rec, &in[i]);
rec.u.log = in[i];
test_copy(&rec);
@ -245,16 +255,16 @@ static void test_reftable_log_record_roundtrip(void)
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
EXPECT(n >= 0);
reftable_record_from_log(&rec_out, &out);
valtype = reftable_record_val_type(&rec);
m = reftable_record_decode(&rec_out, key, valtype, dest,
m = reftable_record_decode(&out, key, valtype, dest,
GIT_SHA1_RAWSZ);
EXPECT(n == m);
EXPECT(reftable_log_record_equal(&in[i], &out, GIT_SHA1_RAWSZ));
EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
GIT_SHA1_RAWSZ));
reftable_log_record_release(&in[i]);
strbuf_release(&key);
reftable_record_release(&rec_out);
reftable_record_release(&out);
}
}
@ -322,47 +332,43 @@ static void test_reftable_obj_record_roundtrip(void)
} };
int i = 0;
for (i = 0; i < ARRAY_SIZE(recs); i++) {
struct reftable_obj_record in = recs[i];
uint8_t buffer[1024] = { 0 };
struct string_view dest = {
.buf = buffer,
.len = sizeof(buffer),
};
struct reftable_record rec = { NULL };
struct reftable_record in = {
.type = BLOCK_TYPE_OBJ,
.u.obj = recs[i],
};
struct strbuf key = STRBUF_INIT;
struct reftable_obj_record out = { NULL };
struct reftable_record rec_out = { NULL };
struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
int n, m;
uint8_t extra;
reftable_record_from_obj(&rec, &in);
test_copy(&rec);
reftable_record_key(&rec, &key);
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
test_copy(&in);
reftable_record_key(&in, &key);
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0);
extra = reftable_record_val_type(&rec);
reftable_record_from_obj(&rec_out, &out);
m = reftable_record_decode(&rec_out, key, extra, dest,
extra = reftable_record_val_type(&in);
m = reftable_record_decode(&out, key, extra, dest,
GIT_SHA1_RAWSZ);
EXPECT(n == m);
EXPECT(in.hash_prefix_len == out.hash_prefix_len);
EXPECT(in.offset_len == out.offset_len);
EXPECT(!memcmp(in.hash_prefix, out.hash_prefix,
in.hash_prefix_len));
EXPECT(0 == memcmp(in.offsets, out.offsets,
sizeof(uint64_t) * in.offset_len));
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
strbuf_release(&key);
reftable_record_release(&rec_out);
reftable_record_release(&out);
}
}
static void test_reftable_index_record_roundtrip(void)
{
struct reftable_index_record in = {
.offset = 42,
.last_key = STRBUF_INIT,
struct reftable_record in = {
.type = BLOCK_TYPE_INDEX,
.u.idx = {
.offset = 42,
.last_key = STRBUF_INIT,
},
};
uint8_t buffer[1024] = { 0 };
struct string_view dest = {
@ -370,31 +376,30 @@ static void test_reftable_index_record_roundtrip(void)
.len = sizeof(buffer),
};
struct strbuf key = STRBUF_INIT;
struct reftable_record rec = { NULL };
struct reftable_index_record out = { .last_key = STRBUF_INIT };
struct reftable_record out_rec = { NULL };
struct reftable_record out = {
.type = BLOCK_TYPE_INDEX,
.u.idx = { .last_key = STRBUF_INIT },
};
int n, m;
uint8_t extra;
strbuf_addstr(&in.last_key, "refs/heads/master");
reftable_record_from_index(&rec, &in);
reftable_record_key(&rec, &key);
test_copy(&rec);
strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
reftable_record_key(&in, &key);
test_copy(&in);
EXPECT(0 == strbuf_cmp(&key, &in.last_key));
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0);
extra = reftable_record_val_type(&rec);
reftable_record_from_index(&out_rec, &out);
m = reftable_record_decode(&out_rec, key, extra, dest, GIT_SHA1_RAWSZ);
extra = reftable_record_val_type(&in);
m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ);
EXPECT(m == n);
EXPECT(in.offset == out.offset);
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
reftable_record_release(&out_rec);
reftable_record_release(&out);
strbuf_release(&key);
strbuf_release(&in.last_key);
strbuf_release(&in.u.idx.last_key);
}
int record_test_main(int argc, const char *argv[])

Просмотреть файл

@ -49,25 +49,25 @@ struct reftable_ref_record {
/* Returns the first hash, or NULL if `rec` is not of type
* REFTABLE_REF_VAL1 or REFTABLE_REF_VAL2. */
uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec);
uint8_t *reftable_ref_record_val1(const struct reftable_ref_record *rec);
/* Returns the second hash, or NULL if `rec` is not of type
* REFTABLE_REF_VAL2. */
uint8_t *reftable_ref_record_val2(struct reftable_ref_record *rec);
uint8_t *reftable_ref_record_val2(const struct reftable_ref_record *rec);
/* returns whether 'ref' represents a deletion */
int reftable_ref_record_is_deletion(const struct reftable_ref_record *ref);
/* prints a reftable_ref_record onto stdout. Useful for debugging. */
void reftable_ref_record_print(struct reftable_ref_record *ref,
void reftable_ref_record_print(const struct reftable_ref_record *ref,
uint32_t hash_id);
/* frees and nulls all pointer values inside `ref`. */
void reftable_ref_record_release(struct reftable_ref_record *ref);
/* returns whether two reftable_ref_records are the same. Useful for testing. */
int reftable_ref_record_equal(struct reftable_ref_record *a,
struct reftable_ref_record *b, int hash_size);
int reftable_ref_record_equal(const struct reftable_ref_record *a,
const struct reftable_ref_record *b, int hash_size);
/* reftable_log_record holds a reflog entry */
struct reftable_log_record {
@ -104,8 +104,8 @@ int reftable_log_record_is_deletion(const struct reftable_log_record *log);
void reftable_log_record_release(struct reftable_log_record *log);
/* returns whether two records are equal. Useful for testing. */
int reftable_log_record_equal(struct reftable_log_record *a,
struct reftable_log_record *b, int hash_size);
int reftable_log_record_equal(const struct reftable_log_record *a,
const struct reftable_log_record *b, int hash_size);
/* dumps a reftable_log_record on stdout, for debugging/testing. */
void reftable_log_record_print(struct reftable_log_record *log,

Просмотреть файл

@ -1,115 +0,0 @@
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
#include "basics.h"
#include "record.h"
#include "generic.h"
#include "reftable-iterator.h"
#include "reftable-generic.h"
int reftable_table_seek_ref(struct reftable_table *tab,
struct reftable_iterator *it, const char *name)
{
struct reftable_ref_record ref = {
.refname = (char *)name,
};
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, &ref);
return tab->ops->seek_record(tab->table_arg, it, &rec);
}
int reftable_table_read_ref(struct reftable_table *tab, const char *name,
struct reftable_ref_record *ref)
{
struct reftable_iterator it = { NULL };
int err = reftable_table_seek_ref(tab, &it, name);
if (err)
goto done;
err = reftable_iterator_next_ref(&it, ref);
if (err)
goto done;
if (strcmp(ref->refname, name) ||
reftable_ref_record_is_deletion(ref)) {
reftable_ref_record_release(ref);
err = 1;
goto done;
}
done:
reftable_iterator_destroy(&it);
return err;
}
uint64_t reftable_table_max_update_index(struct reftable_table *tab)
{
return tab->ops->max_update_index(tab->table_arg);
}
uint64_t reftable_table_min_update_index(struct reftable_table *tab)
{
return tab->ops->min_update_index(tab->table_arg);
}
uint32_t reftable_table_hash_id(struct reftable_table *tab)
{
return tab->ops->hash_id(tab->table_arg);
}
void reftable_iterator_destroy(struct reftable_iterator *it)
{
if (!it->ops) {
return;
}
it->ops->close(it->iter_arg);
it->ops = NULL;
FREE_AND_NULL(it->iter_arg);
}
int reftable_iterator_next_ref(struct reftable_iterator *it,
struct reftable_ref_record *ref)
{
struct reftable_record rec = { NULL };
reftable_record_from_ref(&rec, ref);
return iterator_next(it, &rec);
}
int reftable_iterator_next_log(struct reftable_iterator *it,
struct reftable_log_record *log)
{
struct reftable_record rec = { NULL };
reftable_record_from_log(&rec, log);
return iterator_next(it, &rec);
}
int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)
{
return it->ops->next(it->iter_arg, rec);
}
static int empty_iterator_next(void *arg, struct reftable_record *rec)
{
return 1;
}
static void empty_iterator_close(void *arg)
{
}
static struct reftable_iterator_vtable empty_vtable = {
.next = &empty_iterator_next,
.close = &empty_iterator_close,
};
void iterator_set_empty(struct reftable_iterator *it)
{
assert(!it->ops);
it->iter_arg = NULL;
it->ops = &empty_vtable;
}

Просмотреть файл

@ -889,7 +889,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
struct strbuf new_table_path = STRBUF_INIT;
int err = 0;
int have_lock = 0;
int lock_file_fd = 0;
int lock_file_fd = -1;
int compact_count = last - first + 1;
char **listp = NULL;
char **delete_on_success =
@ -923,7 +923,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
}
/* Don't want to write to the lock for now. */
close(lock_file_fd);
lock_file_fd = 0;
lock_file_fd = -1;
have_lock = 1;
err = stack_uptodate(st);
@ -1031,7 +1031,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
goto done;
}
err = close(lock_file_fd);
lock_file_fd = 0;
lock_file_fd = -1;
if (err < 0) {
err = REFTABLE_IO_ERROR;
unlink(new_table_path.buf);
@ -1068,9 +1068,9 @@ done:
listp++;
}
free_names(subtable_locks);
if (lock_file_fd > 0) {
if (lock_file_fd >= 0) {
close(lock_file_fd);
lock_file_fd = 0;
lock_file_fd = -1;
}
if (have_lock) {
unlink(lock_file_name.buf);

Просмотреть файл

@ -90,7 +90,7 @@ static void test_read_file(void)
EXPECT(0 == strcmp(want[i], names[i]));
}
free_names(names);
remove(fn);
(void) remove(fn);
}
static void test_parse_names(void)
@ -839,6 +839,7 @@ static void test_reftable_stack_auto_compaction(void)
EXPECT_ERR(err);
err = reftable_stack_auto_compact(st);
EXPECT_ERR(err);
EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i));
}

Просмотреть файл

@ -150,6 +150,8 @@ void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
void reftable_writer_free(struct reftable_writer *w)
{
if (!w)
return;
reftable_free(w->block);
reftable_free(w);
}
@ -254,8 +256,10 @@ done:
int reftable_writer_add_ref(struct reftable_writer *w,
struct reftable_ref_record *ref)
{
struct reftable_record rec = { NULL };
struct reftable_ref_record copy = *ref;
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
.u.ref = *ref,
};
int err = 0;
if (ref->refname == NULL)
@ -264,8 +268,7 @@ int reftable_writer_add_ref(struct reftable_writer *w,
ref->update_index > w->max_update_index)
return REFTABLE_API_ERROR;
reftable_record_from_ref(&rec, &copy);
copy.update_index -= w->min_update_index;
rec.u.ref.update_index -= w->min_update_index;
err = writer_add_record(w, &rec);
if (err < 0)
@ -304,7 +307,10 @@ int reftable_writer_add_refs(struct reftable_writer *w,
static int reftable_writer_add_log_verbatim(struct reftable_writer *w,
struct reftable_log_record *log)
{
struct reftable_record rec = { NULL };
struct reftable_record rec = {
.type = BLOCK_TYPE_LOG,
.u.log = *log,
};
if (w->block_writer &&
block_writer_type(w->block_writer) == BLOCK_TYPE_REF) {
int err = writer_finish_public_section(w);
@ -314,8 +320,6 @@ static int reftable_writer_add_log_verbatim(struct reftable_writer *w,
w->next -= w->pending_padding;
w->pending_padding = 0;
reftable_record_from_log(&rec, log);
return writer_add_record(w, &rec);
}
@ -396,8 +400,10 @@ static int writer_finish_section(struct reftable_writer *w)
w->index_len = 0;
w->index_cap = 0;
for (i = 0; i < idx_len; i++) {
struct reftable_record rec = { NULL };
reftable_record_from_index(&rec, idx + i);
struct reftable_record rec = {
.type = BLOCK_TYPE_INDEX,
.u.idx = idx[i],
};
if (block_writer_add(w->block_writer, &rec) == 0) {
continue;
}
@ -465,17 +471,17 @@ static void write_object_record(void *void_arg, void *key)
{
struct write_record_arg *arg = void_arg;
struct obj_index_tree_node *entry = key;
struct reftable_obj_record obj_rec = {
.hash_prefix = (uint8_t *)entry->hash.buf,
.hash_prefix_len = arg->w->stats.object_id_len,
.offsets = entry->offsets,
.offset_len = entry->offset_len,
};
struct reftable_record rec = { NULL };
struct reftable_record
rec = { .type = BLOCK_TYPE_OBJ,
.u.obj = {
.hash_prefix = (uint8_t *)entry->hash.buf,
.hash_prefix_len = arg->w->stats.object_id_len,
.offsets = entry->offsets,
.offset_len = entry->offset_len,
} };
if (arg->err < 0)
goto done;
reftable_record_from_obj(&rec, &obj_rec);
arg->err = block_writer_add(arg->w->block_writer, &rec);
if (arg->err == 0)
goto done;
@ -488,7 +494,8 @@ static void write_object_record(void *void_arg, void *key)
arg->err = block_writer_add(arg->w->block_writer, &rec);
if (arg->err == 0)
goto done;
obj_rec.offset_len = 0;
rec.u.obj.offset_len = 0;
arg->err = block_writer_add(arg->w->block_writer, &rec);
/* Should be able to write into a fresh block. */

Просмотреть файл

@ -3,15 +3,16 @@
int cmd__reftable(int argc, const char **argv)
{
/* test from simple to complex. */
basics_test_main(argc, argv);
block_test_main(argc, argv);
merged_test_main(argc, argv);
pq_test_main(argc, argv);
record_test_main(argc, argv);
refname_test_main(argc, argv);
readwrite_test_main(argc, argv);
stack_test_main(argc, argv);
block_test_main(argc, argv);
tree_test_main(argc, argv);
pq_test_main(argc, argv);
readwrite_test_main(argc, argv);
merged_test_main(argc, argv);
stack_test_main(argc, argv);
refname_test_main(argc, argv);
return 0;
}