forked from xerub/img4lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs_mem.c
257 lines (243 loc) · 5.49 KB
/
vfs_mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "vfs.h"
#include "vfs_internal.h"
static int
memory_fsync(FHANDLE fd_)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
if (!fd) {
return -1;
}
return 0;
}
int
memory_close(FHANDLE fd_)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
if (!fd) {
return -1;
}
fd->free(fd->buf);
free(fd);
return 0;
}
static ssize_t
memory_read(FHANDLE fd_, void *buf, size_t count)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
if (!fd) {
return -1;
}
if (fd->position > fd->size) {
return 0;
}
if (count > fd->size - fd->position) {
count = fd->size - fd->position;
}
memmove(buf, fd->buf + fd->position, count);
fd->position += count;
return count;
}
static ssize_t
memory_write(FHANDLE fd_, const void *buf, size_t count)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
size_t end;
if (!fd || fd_->flags == O_RDONLY) {
return -1;
}
end = fd->position + count;
if (end > fd->size) {
unsigned char *tmp = fd->realloc(fd->buf, end);
if (!tmp) {
count = fd->size - fd->position;
} else {
fd->buf = tmp;
fd->size = end;
}
}
fd->dirty = 1;
memmove(fd->buf + fd->position, buf, count);
fd->position += count;
return count;
}
static off_t
memory_lseek(FHANDLE fd_, off_t offset, int whence)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
off_t position;
if (!fd) {
return -1;
}
switch (whence) {
case SEEK_SET:
position = offset;
break;
case SEEK_CUR:
position = fd->position + offset;
break;
case SEEK_END:
position = fd->size + offset;
break;
default:
return -1;
}
if (position < 0) {
return -1;
}
if ((size_t)position > fd->size) {
size_t gap;
unsigned char *tmp;
if (fd_->flags == O_RDONLY) {
return -1;
}
gap = position - fd->size;
tmp = fd->realloc(fd->buf, position);
if (!tmp) {
return -1;
}
fd->dirty = 1;
memset(tmp + fd->size, 0, gap);
fd->buf = tmp;
fd->size = position;
}
fd->position = position;
return fd->position;
}
static int
memory_ioctl(FHANDLE fd_, unsigned long req, ...)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
int rv = -1;
va_list ap;
if (!fd) {
return -1;
}
va_start(ap, req);
switch (req) {
case IOCTL_MEM_GET_DATAPTR:
case IOCTL_MEM_GET_BACKING: {
void **buf = va_arg(ap, void **);
size_t *sz = va_arg(ap, size_t *);
*buf = fd->buf;
*sz = fd->size;
rv = 0;
break;
}
case IOCTL_MEM_SET_FUNCS: {
fd->realloc = va_arg(ap, realloc_t);
fd->free = va_arg(ap, free_t);
rv = 0;
break;
}
}
va_end(ap);
return rv;
}
int
memory_ftruncate(FHANDLE fd_, off_t length)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
void *tmp;
if (!fd || fd_->flags == O_RDONLY) {
return -1;
}
fd->size = length;
fd->dirty = 1;
if (length == 0) {
free(fd->buf);
fd->buf = NULL;
return 0;
}
tmp = fd->realloc(fd->buf, length);
if (tmp) {
fd->buf = tmp;
}
return 0;
}
static ssize_t
memory_length(FHANDLE fd_)
{
struct file_ops_memory *fd = (struct file_ops_memory *)fd_;
if (!fd) {
return -1;
}
return fd->size;
}
FHANDLE
memory_openex(struct file_ops_memory *ops, int flags, void *buf, size_t size)
{
if (!ops) {
return NULL;
}
ops->buf = buf;
if (!buf && size) {
size_t allocated_size;
if (size > 100 * 1024 * 1024) {
allocated_size = size + 30 * 1024 * 1024;
} else {
allocated_size = size;
}
ops->buf = calloc(1, allocated_size);
if (!ops->buf) {
free(ops);
return NULL;
}
}
ops->size = size;
ops->position = 0;
ops->dirty = 0;
ops->realloc = realloc;
ops->free = free;
ops->ops.flags = flags & O_ACCMODE;
ops->ops.read = memory_read;
ops->ops.write = memory_write;
ops->ops.lseek = memory_lseek;
ops->ops.ioctl = memory_ioctl;
ops->ops.ftruncate = memory_ftruncate;
ops->ops.fsync = memory_fsync;
ops->ops.close = memory_close;
ops->ops.length = memory_length;
return (FHANDLE)ops;
}
FHANDLE
memory_open(int flags, void *buf, size_t size)
{
return memory_openex(malloc(sizeof(struct file_ops_memory)), flags, buf, size);
}
FHANDLE
memory_open_from_file(const char *filename, int flags)
{
FHANDLE pfd;
size_t n, size;
unsigned char *buf;
FHANDLE fd = file_open(filename, O_RDONLY);
if (!fd) {
return NULL;
}
size = fd->length(fd);
if ((ssize_t)size < 0) {
fd->close(fd);
return NULL;
}
buf = malloc(size);
if (!buf) {
fd->close(fd);
return NULL;
}
n = fd->read(fd, buf, size);
fd->close(fd);
if (n != size) {
free(buf);
return NULL;
}
pfd = memory_open(flags, buf, size);
if (!pfd) {
free(buf);
}
return pfd;
}