Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/reftable/blocksource.c
Line
Count
Source
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
void block_source_release_data(struct reftable_block_data *data)
17
0
{
18
0
  struct reftable_block_source source = data->source;
19
0
  if (data && source.ops)
20
0
    source.ops->release_data(source.arg, data);
21
0
  data->data = NULL;
22
0
  data->len = 0;
23
0
  data->source.ops = NULL;
24
0
  data->source.arg = NULL;
25
0
}
26
27
void block_source_close(struct reftable_block_source *source)
28
0
{
29
0
  if (!source->ops) {
30
0
    return;
31
0
  }
32
33
0
  source->ops->close(source->arg);
34
0
  source->ops = NULL;
35
0
}
36
37
ssize_t block_source_read_data(struct reftable_block_source *source,
38
             struct reftable_block_data *dest, uint64_t off,
39
             uint32_t size)
40
0
{
41
0
  ssize_t result = source->ops->read_data(source->arg, dest, off, size);
42
0
  dest->source = *source;
43
0
  return result;
44
0
}
45
46
uint64_t block_source_size(struct reftable_block_source *source)
47
0
{
48
0
  return source->ops->size(source->arg);
49
0
}
50
51
static void reftable_buf_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest)
52
0
{
53
0
  if (dest->len)
54
0
    memset(dest->data, 0xff, dest->len);
55
0
  reftable_free(dest->data);
56
0
}
57
58
static void reftable_buf_close(void *b REFTABLE_UNUSED)
59
0
{
60
0
}
61
62
static ssize_t reftable_buf_read_data(void *v, struct reftable_block_data *dest,
63
              uint64_t off, uint32_t size)
64
0
{
65
0
  struct reftable_buf *b = v;
66
0
  assert(off + size <= b->len);
67
0
  REFTABLE_CALLOC_ARRAY(dest->data, size);
68
0
  if (!dest->data)
69
0
    return -1;
70
0
  memcpy(dest->data, b->buf + off, size);
71
0
  dest->len = size;
72
0
  return size;
73
0
}
74
75
static uint64_t reftable_buf_size(void *b)
76
0
{
77
0
  return ((struct reftable_buf *)b)->len;
78
0
}
79
80
static struct reftable_block_source_vtable reftable_buf_vtable = {
81
  .size = &reftable_buf_size,
82
  .read_data = &reftable_buf_read_data,
83
  .release_data = &reftable_buf_release_data,
84
  .close = &reftable_buf_close,
85
};
86
87
void block_source_from_buf(struct reftable_block_source *bs,
88
         struct reftable_buf *buf)
89
0
{
90
0
  assert(!bs->ops);
91
0
  bs->ops = &reftable_buf_vtable;
92
0
  bs->arg = buf;
93
0
}
94
95
struct file_block_source {
96
  uint64_t size;
97
  unsigned char *data;
98
};
99
100
static uint64_t file_size(void *b)
101
0
{
102
0
  return ((struct file_block_source *)b)->size;
103
0
}
104
105
static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest REFTABLE_UNUSED)
106
0
{
107
0
}
108
109
static void file_close(void *v)
110
0
{
111
0
  struct file_block_source *b = v;
112
0
  munmap(b->data, b->size);
113
0
  reftable_free(b);
114
0
}
115
116
static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_t off,
117
            uint32_t size)
118
0
{
119
0
  struct file_block_source *b = v;
120
0
  assert(off + size <= b->size);
121
0
  dest->data = b->data + off;
122
0
  dest->len = size;
123
0
  return size;
124
0
}
125
126
static struct reftable_block_source_vtable file_vtable = {
127
  .size = &file_size,
128
  .read_data = &file_read_data,
129
  .release_data = &file_release_data,
130
  .close = &file_close,
131
};
132
133
int reftable_block_source_from_file(struct reftable_block_source *bs,
134
            const char *name)
135
0
{
136
0
  struct file_block_source *p = NULL;
137
0
  struct stat st;
138
0
  int fd, err;
139
140
0
  fd = open(name, O_RDONLY);
141
0
  if (fd < 0) {
142
0
    if (errno == ENOENT)
143
0
      return REFTABLE_NOT_EXIST_ERROR;
144
0
    err = -1;
145
0
    goto out;
146
0
  }
147
148
0
  if (fstat(fd, &st) < 0) {
149
0
    err = REFTABLE_IO_ERROR;
150
0
    goto out;
151
0
  }
152
153
0
  REFTABLE_CALLOC_ARRAY(p, 1);
154
0
  if (!p) {
155
0
    err = REFTABLE_OUT_OF_MEMORY_ERROR;
156
0
    goto out;
157
0
  }
158
159
0
  p->size = st.st_size;
160
0
  p->data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
161
0
  if (p->data == MAP_FAILED) {
162
0
    err = REFTABLE_IO_ERROR;
163
0
    p->data = NULL;
164
0
    goto out;
165
0
  }
166
167
0
  assert(!bs->ops);
168
0
  bs->ops = &file_vtable;
169
0
  bs->arg = p;
170
171
0
  err = 0;
172
173
0
out:
174
0
  if (fd >= 0)
175
0
    close(fd);
176
0
  if (err < 0)
177
0
    reftable_free(p);
178
0
  return err;
179
0
}