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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* file.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#define SQFS_BUILDING_DLL
#include "config.h"
#include "sqfs/io.h"
#include "sqfs/error.h"
#include "compat.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if !defined(_WIN32) && !defined(__WINDOWS__)
#include <unistd.h>
#define ERROR_NOT_ENOUGH_MEMORY ENOMEM
#define ERROR_NOT_SUPPORTED ENOTSUP
#define SetLastError(x) errno = (x)
#endif
typedef struct {
sqfs_file_t base;
bool readonly;
sqfs_u64 size;
sqfs_file_handle_t fd;
char name[];
} sqfs_file_stdio_t;
static void stdio_destroy(sqfs_object_t *base)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
sqfs_native_file_close(file->fd);
free(file);
}
static sqfs_object_t *stdio_copy(const sqfs_object_t *base)
{
const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base;
sqfs_file_stdio_t *copy;
size_t size;
if (!file->readonly) {
SetLastError(ERROR_NOT_SUPPORTED);
return NULL;
}
size = sizeof(*file) + strlen(file->name) + 1;
copy = calloc(1, size);
if (copy == NULL)
return NULL;
memcpy(copy, file, size);
if (sqfs_native_file_duplicate(file->fd, ©->fd)) {
os_error_t error = get_os_error_state();
free(copy);
copy = NULL;
set_os_error_state(error);
}
return (sqfs_object_t *)copy;
}
#if defined(_WIN32) || defined(__WINDOWS__)
static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset,
void *buffer, size_t size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
DWORD actually_read;
int ret;
if (offset >= file->size)
return SQFS_ERROR_OUT_OF_BOUNDS;
if (size == 0)
return 0;
if ((offset + size - 1) >= file->size)
return SQFS_ERROR_OUT_OF_BOUNDS;
ret = sqfs_native_file_seek(file->fd, offset, SQFS_FILE_SEEK_START);
if (ret)
return ret;
while (size > 0) {
if (!ReadFile(file->fd, buffer, size, &actually_read, NULL))
return SQFS_ERROR_IO;
size -= actually_read;
buffer = (char *)buffer + actually_read;
}
return 0;
}
static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset,
const void *buffer, size_t size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
DWORD actually_read;
int ret;
if (size == 0)
return 0;
ret = sqfs_native_file_seek(file->fd, offset, SQFS_FILE_SEEK_START);
if (ret)
return ret;
while (size > 0) {
if (!WriteFile(file->fd, buffer, size, &actually_read, NULL))
return SQFS_ERROR_IO;
size -= actually_read;
buffer = (char *)buffer + actually_read;
offset += actually_read;
if (offset > file->size)
file->size = offset;
}
return 0;
}
#else
static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset,
void *buffer, size_t size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
ssize_t ret;
while (size > 0) {
ret = pread(file->fd, buffer, size, offset);
if (ret < 0) {
if (errno == EINTR)
continue;
return SQFS_ERROR_IO;
}
if (ret == 0)
return SQFS_ERROR_OUT_OF_BOUNDS;
buffer = (char *)buffer + ret;
size -= ret;
offset += ret;
}
return 0;
}
static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset,
const void *buffer, size_t size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
ssize_t ret;
while (size > 0) {
ret = pwrite(file->fd, buffer, size, offset);
if (ret < 0) {
if (errno == EINTR)
continue;
return SQFS_ERROR_IO;
}
if (ret == 0)
return SQFS_ERROR_OUT_OF_BOUNDS;
buffer = (const char *)buffer + ret;
size -= ret;
offset += ret;
}
if (offset >= file->size)
file->size = offset;
return 0;
}
#endif
static sqfs_u64 stdio_get_size(const sqfs_file_t *base)
{
const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base;
return file->size;
}
static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
int ret;
ret = sqfs_native_file_seek(file->fd, size, SQFS_FILE_SEEK_START |
SQFS_FILE_SEEK_TRUNCATE);
if (ret)
return ret;
file->size = size;
return 0;
}
static const char *stdio_get_filename(sqfs_file_t *file)
{
return ((sqfs_file_stdio_t *)file)->name;
}
int sqfs_file_open_handle(sqfs_file_t **out, const char *filename,
sqfs_file_handle_t fd, sqfs_u32 flags)
{
sqfs_file_stdio_t *file;
size_t size, namelen;
sqfs_file_t *base;
os_error_t err;
int ret;
*out = NULL;
namelen = strlen(filename);
size = sizeof(*file) + 1;
if (SZ_ADD_OV(size, namelen, &size))
goto fail_no_mem;
file = calloc(1, size);
base = (sqfs_file_t *)file;
if (file == NULL)
goto fail_no_mem;
sqfs_object_init(file, stdio_destroy, stdio_copy);
memcpy(file->name, filename, namelen);
ret = sqfs_native_file_get_size(fd, &file->size);
if (ret)
goto fail_free;
ret = sqfs_native_file_duplicate(fd, &file->fd);
if (ret)
goto fail_free;
sqfs_native_file_close(fd);
file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0;
base->read_at = stdio_read_at;
base->write_at = stdio_write_at;
base->get_size = stdio_get_size;
base->truncate = stdio_truncate;
base->get_filename = stdio_get_filename;
*out = base;
return 0;
fail_free:
err = get_os_error_state();
free(file);
set_os_error_state(err);
return ret;
fail_no_mem:
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return SQFS_ERROR_ALLOC;
}
int sqfs_file_open(sqfs_file_t **out, const char *filename, sqfs_u32 flags)
{
sqfs_file_handle_t fd;
int ret;
ret = sqfs_native_file_open(&fd, filename, flags);
if (ret) {
*out = NULL;
return ret;
}
ret = sqfs_file_open_handle(out, filename, fd, flags);
if (ret) {
os_error_t err = get_os_error_state();
sqfs_native_file_close(fd);
set_os_error_state(err);
return ret;
}
return 0;
}
|