]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/blocksource.c
split-index: accept that a base index can be empty
[thirdparty/git.git] / reftable / blocksource.c
1 /*
2 Copyright 2020 Google LLC
3
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 */
8
9 #include "system.h"
10
11 #include "basics.h"
12 #include "blocksource.h"
13 #include "reftable-blocksource.h"
14 #include "reftable-error.h"
15
16 static void strbuf_return_block(void *b, struct reftable_block *dest)
17 {
18 if (dest->len)
19 memset(dest->data, 0xff, dest->len);
20 reftable_free(dest->data);
21 }
22
23 static void strbuf_close(void *b)
24 {
25 }
26
27 static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
28 uint32_t size)
29 {
30 struct strbuf *b = v;
31 assert(off + size <= b->len);
32 dest->data = reftable_calloc(size);
33 memcpy(dest->data, b->buf + off, size);
34 dest->len = size;
35 return size;
36 }
37
38 static uint64_t strbuf_size(void *b)
39 {
40 return ((struct strbuf *)b)->len;
41 }
42
43 static struct reftable_block_source_vtable strbuf_vtable = {
44 .size = &strbuf_size,
45 .read_block = &strbuf_read_block,
46 .return_block = &strbuf_return_block,
47 .close = &strbuf_close,
48 };
49
50 void block_source_from_strbuf(struct reftable_block_source *bs,
51 struct strbuf *buf)
52 {
53 assert(!bs->ops);
54 bs->ops = &strbuf_vtable;
55 bs->arg = buf;
56 }
57
58 static void malloc_return_block(void *b, struct reftable_block *dest)
59 {
60 if (dest->len)
61 memset(dest->data, 0xff, dest->len);
62 reftable_free(dest->data);
63 }
64
65 static struct reftable_block_source_vtable malloc_vtable = {
66 .return_block = &malloc_return_block,
67 };
68
69 static struct reftable_block_source malloc_block_source_instance = {
70 .ops = &malloc_vtable,
71 };
72
73 struct reftable_block_source malloc_block_source(void)
74 {
75 return malloc_block_source_instance;
76 }
77
78 struct file_block_source {
79 int fd;
80 uint64_t size;
81 };
82
83 static uint64_t file_size(void *b)
84 {
85 return ((struct file_block_source *)b)->size;
86 }
87
88 static void file_return_block(void *b, struct reftable_block *dest)
89 {
90 if (dest->len)
91 memset(dest->data, 0xff, dest->len);
92 reftable_free(dest->data);
93 }
94
95 static void file_close(void *b)
96 {
97 int fd = ((struct file_block_source *)b)->fd;
98 if (fd > 0) {
99 close(fd);
100 ((struct file_block_source *)b)->fd = 0;
101 }
102
103 reftable_free(b);
104 }
105
106 static int file_read_block(void *v, struct reftable_block *dest, uint64_t off,
107 uint32_t size)
108 {
109 struct file_block_source *b = v;
110 assert(off + size <= b->size);
111 dest->data = reftable_malloc(size);
112 if (pread(b->fd, dest->data, size, off) != size)
113 return -1;
114 dest->len = size;
115 return size;
116 }
117
118 static struct reftable_block_source_vtable file_vtable = {
119 .size = &file_size,
120 .read_block = &file_read_block,
121 .return_block = &file_return_block,
122 .close = &file_close,
123 };
124
125 int reftable_block_source_from_file(struct reftable_block_source *bs,
126 const char *name)
127 {
128 struct stat st = { 0 };
129 int err = 0;
130 int fd = open(name, O_RDONLY);
131 struct file_block_source *p = NULL;
132 if (fd < 0) {
133 if (errno == ENOENT) {
134 return REFTABLE_NOT_EXIST_ERROR;
135 }
136 return -1;
137 }
138
139 err = fstat(fd, &st);
140 if (err < 0) {
141 close(fd);
142 return REFTABLE_IO_ERROR;
143 }
144
145 p = reftable_calloc(sizeof(struct file_block_source));
146 p->size = st.st_size;
147 p->fd = fd;
148
149 assert(!bs->ops);
150 bs->ops = &file_vtable;
151 bs->arg = p;
152 return 0;
153 }