summaryrefslogtreecommitdiff
path: root/reftable/blocksource.c
diff options
context:
space:
mode:
Diffstat (limited to 'reftable/blocksource.c')
-rw-r--r--reftable/blocksource.c80
1 files changed, 60 insertions, 20 deletions
diff --git a/reftable/blocksource.c b/reftable/blocksource.c
index 52e0915a67..573c81287f 100644
--- a/reftable/blocksource.c
+++ b/reftable/blocksource.c
@@ -1,10 +1,10 @@
/*
-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
-*/
+ * 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 "system.h"
@@ -13,19 +13,54 @@ https://developers.google.com/open-source/licenses/bsd
#include "reftable-blocksource.h"
#include "reftable-error.h"
-static void reftable_buf_return_block(void *b UNUSED, struct reftable_block *dest)
+void block_source_release_data(struct reftable_block_data *data)
+{
+ struct reftable_block_source source = data->source;
+ if (data && source.ops)
+ source.ops->release_data(source.arg, data);
+ data->data = NULL;
+ data->len = 0;
+ data->source.ops = NULL;
+ data->source.arg = NULL;
+}
+
+void block_source_close(struct reftable_block_source *source)
+{
+ if (!source->ops) {
+ return;
+ }
+
+ source->ops->close(source->arg);
+ source->ops = NULL;
+}
+
+ssize_t block_source_read_data(struct reftable_block_source *source,
+ struct reftable_block_data *dest, uint64_t off,
+ uint32_t size)
+{
+ ssize_t result = source->ops->read_data(source->arg, dest, off, size);
+ dest->source = *source;
+ return result;
+}
+
+uint64_t block_source_size(struct reftable_block_source *source)
+{
+ return source->ops->size(source->arg);
+}
+
+static void reftable_buf_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest)
{
if (dest->len)
memset(dest->data, 0xff, dest->len);
reftable_free(dest->data);
}
-static void reftable_buf_close(void *b UNUSED)
+static void reftable_buf_close(void *b REFTABLE_UNUSED)
{
}
-static int reftable_buf_read_block(void *v, struct reftable_block *dest,
- uint64_t off, uint32_t size)
+static ssize_t reftable_buf_read_data(void *v, struct reftable_block_data *dest,
+ uint64_t off, uint32_t size)
{
struct reftable_buf *b = v;
assert(off + size <= b->len);
@@ -44,8 +79,8 @@ static uint64_t reftable_buf_size(void *b)
static struct reftable_block_source_vtable reftable_buf_vtable = {
.size = &reftable_buf_size,
- .read_block = &reftable_buf_read_block,
- .return_block = &reftable_buf_return_block,
+ .read_data = &reftable_buf_read_data,
+ .release_data = &reftable_buf_release_data,
.close = &reftable_buf_close,
};
@@ -67,7 +102,7 @@ static uint64_t file_size(void *b)
return ((struct file_block_source *)b)->size;
}
-static void file_return_block(void *b UNUSED, struct reftable_block *dest UNUSED)
+static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest REFTABLE_UNUSED)
{
}
@@ -78,8 +113,8 @@ static void file_close(void *v)
reftable_free(b);
}
-static int file_read_block(void *v, struct reftable_block *dest, uint64_t off,
- uint32_t size)
+static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_t off,
+ uint32_t size)
{
struct file_block_source *b = v;
assert(off + size <= b->size);
@@ -90,15 +125,15 @@ static int file_read_block(void *v, struct reftable_block *dest, uint64_t off,
static struct reftable_block_source_vtable file_vtable = {
.size = &file_size,
- .read_block = &file_read_block,
- .return_block = &file_return_block,
+ .read_data = &file_read_data,
+ .release_data = &file_release_data,
.close = &file_close,
};
int reftable_block_source_from_file(struct reftable_block_source *bs,
const char *name)
{
- struct file_block_source *p;
+ struct file_block_source *p = NULL;
struct stat st;
int fd, err;
@@ -122,7 +157,12 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
}
p->size = st.st_size;
- p->data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ p->data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (p->data == MAP_FAILED) {
+ err = REFTABLE_IO_ERROR;
+ p->data = NULL;
+ goto out;
+ }
assert(!bs->ops);
bs->ops = &file_vtable;
@@ -135,5 +175,5 @@ out:
close(fd);
if (err < 0)
reftable_free(p);
- return 0;
+ return err;
}