Merge branch 'ds/trace2-regions-in-tests'
[git/debian.git] / reftable / blocksource.c
blob0044eecd9aa39afa476dcfb8e6eeaaa89fb08b86
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 "system.h"
11 #include "basics.h"
12 #include "blocksource.h"
13 #include "reftable-blocksource.h"
14 #include "reftable-error.h"
16 static void strbuf_return_block(void *b, struct reftable_block *dest)
18 memset(dest->data, 0xff, dest->len);
19 reftable_free(dest->data);
22 static void strbuf_close(void *b)
26 static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
27 uint32_t size)
29 struct strbuf *b = v;
30 assert(off + size <= b->len);
31 dest->data = reftable_calloc(size);
32 memcpy(dest->data, b->buf + off, size);
33 dest->len = size;
34 return size;
37 static uint64_t strbuf_size(void *b)
39 return ((struct strbuf *)b)->len;
42 static struct reftable_block_source_vtable strbuf_vtable = {
43 .size = &strbuf_size,
44 .read_block = &strbuf_read_block,
45 .return_block = &strbuf_return_block,
46 .close = &strbuf_close,
49 void block_source_from_strbuf(struct reftable_block_source *bs,
50 struct strbuf *buf)
52 assert(!bs->ops);
53 bs->ops = &strbuf_vtable;
54 bs->arg = buf;
57 static void malloc_return_block(void *b, struct reftable_block *dest)
59 memset(dest->data, 0xff, dest->len);
60 reftable_free(dest->data);
63 static struct reftable_block_source_vtable malloc_vtable = {
64 .return_block = &malloc_return_block,
67 static struct reftable_block_source malloc_block_source_instance = {
68 .ops = &malloc_vtable,
71 struct reftable_block_source malloc_block_source(void)
73 return malloc_block_source_instance;
76 struct file_block_source {
77 int fd;
78 uint64_t size;
81 static uint64_t file_size(void *b)
83 return ((struct file_block_source *)b)->size;
86 static void file_return_block(void *b, struct reftable_block *dest)
88 memset(dest->data, 0xff, dest->len);
89 reftable_free(dest->data);
92 static void file_close(void *b)
94 int fd = ((struct file_block_source *)b)->fd;
95 if (fd > 0) {
96 close(fd);
97 ((struct file_block_source *)b)->fd = 0;
100 reftable_free(b);
103 static int file_read_block(void *v, struct reftable_block *dest, uint64_t off,
104 uint32_t size)
106 struct file_block_source *b = v;
107 assert(off + size <= b->size);
108 dest->data = reftable_malloc(size);
109 if (pread(b->fd, dest->data, size, off) != size)
110 return -1;
111 dest->len = size;
112 return size;
115 static struct reftable_block_source_vtable file_vtable = {
116 .size = &file_size,
117 .read_block = &file_read_block,
118 .return_block = &file_return_block,
119 .close = &file_close,
122 int reftable_block_source_from_file(struct reftable_block_source *bs,
123 const char *name)
125 struct stat st = { 0 };
126 int err = 0;
127 int fd = open(name, O_RDONLY);
128 struct file_block_source *p = NULL;
129 if (fd < 0) {
130 if (errno == ENOENT) {
131 return REFTABLE_NOT_EXIST_ERROR;
133 return -1;
136 err = fstat(fd, &st);
137 if (err < 0)
138 return -1;
140 p = reftable_calloc(sizeof(struct file_block_source));
141 p->size = st.st_size;
142 p->fd = fd;
144 assert(!bs->ops);
145 bs->ops = &file_vtable;
146 bs->arg = p;
147 return 0;