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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#include <BH/IO.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct BH_File
{
BH_IO parent;
int flags;
int handle;
} BH_File;
static int fileOpenFlags(int mode)
{
int flags = 0;
/* Determine read/write flags */
if ((mode & BH_FILE_READWRITE) == BH_FILE_READWRITE)
flags |= O_RDWR;
else if (mode & BH_FILE_WRITE)
flags |= O_WRONLY;
else if (mode & BH_FILE_READ)
flags |= O_RDONLY;
else
return -1;
/* Check if existing file should be opened */
if (!(mode & BH_FILE_EXIST))
{
flags |= O_CREAT;
/* Check if file should be created */
if (mode & BH_FILE_CREATE)
flags |= O_EXCL;
}
/* Check if file should be opened in append mode */
if (mode & BH_FILE_APPEND)
flags |= O_APPEND;
/* Check if file should be truncated */
if (mode & BH_FILE_TRUNCATE)
flags |= O_TRUNC;
return flags;
}
static int fileInit(BH_File *file,
const char *path,
int mode)
{
static const mode_t open_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
int flags;
/* Check if path is valid */
if (!path)
return BH_ERROR;
/* Determine file open flags */
flags = fileOpenFlags(mode);
if (flags == -1)
return BH_ERROR;
/* Open the file */
file->flags = 0;
file->handle = open(path, flags, open_mode);
if (file->handle == -1)
return BH_ERROR;
return BH_OK;
}
static int fileDestroy(BH_File *file)
{
close(file->handle);
free(file);
return BH_OK;
}
static int fileRead(BH_File *file,
BH_IOReadInfo *info)
{
ssize_t readed;
/* Read data from the file */
readed = read(file->handle, info->data, info->size);
if (readed < 0)
goto error;
/* Check for EOF condition */
if (readed > 0)
file->flags &= ~BH_IO_FLAG_EOF;
else
file->flags |= BH_IO_FLAG_EOF;
if (info->actual)
*info->actual = readed;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileWrite(BH_File *file,
BH_IOWriteInfo *info)
{
ssize_t written;
/* Write data to the file */
written = write(file->handle, info->data, info->size);
if (written < 0)
goto error;
/* Check for EOF condition */
if (!written)
file->flags |= BH_IO_FLAG_EOF;
else
file->flags &= ~BH_IO_FLAG_EOF;
if (info->actual)
*info->actual = written;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileFlush(BH_File *file)
{
/* Flush the buffers */
fsync(file->handle);
return BH_OK;
}
static int fileSeek(BH_File *file,
BH_IOSeekInfo *info)
{
/* Seek to the specified position */
if (lseek(file->handle, info->offset, info->whence) == -1)
goto error;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileTell(BH_File *file,
int64_t *pos)
{
/* Get current offset in the file */
if ((*pos = lseek(file->handle, 0, SEEK_CUR)) == -1)
goto error;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileSize(BH_File *file,
int64_t *size)
{
struct stat sb;
/* Get file size from the OS */
if (fstat(file->handle, &sb))
goto error;
*size = sb.st_size;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileFlags(BH_File *file,
int *flags)
{
*flags = file->flags;
return BH_OK;
}
static int fileClear(BH_File *file)
{
/* Clear BH_IO_FLAG_ERROR flag */
file->flags &= ~BH_IO_FLAG_ERROR;
return BH_OK;
}
static int fileCap(BH_File *file,
int *op)
{
BH_UNUSED(file);
/* Return operations supported by the file input/output device */
switch (*op)
{
case BH_IO_CTL_FLAGS:
case BH_IO_CTL_CLEAR:
case BH_IO_CTL_FLUSH:
case BH_IO_CTL_SIZE:
case BH_IO_CTL_TELL:
case BH_IO_CTL_SEEK:
return BH_OK;
default:
return BH_NOIMPL;
}
}
static int fileCtl(BH_File *file,
BH_IOCtlInfo *info)
{
/* Handle supported operations */
switch (info->op)
{
case BH_IO_CTL_FLAGS:
return fileFlags(file, (int *)info->arg);
case BH_IO_CTL_CLEAR:
return fileClear(file);
case BH_IO_CTL_FLUSH:
return fileFlush(file);
case BH_IO_CTL_SIZE:
return fileSize(file, (int64_t *)(info->arg));
case BH_IO_CTL_TELL:
return fileTell(file, (int64_t *)(info->arg));
case BH_IO_CTL_SEEK:
return fileSeek(file, (BH_IOSeekInfo *)(info->arg));
default:
return BH_NOIMPL;
}
}
static int fileCallback(BH_File *file,
int type,
void *arg)
{
/* Handle basic input/output operations */
switch (type)
{
case BH_IO_OP_DESTROY: return fileDestroy(file);
case BH_IO_OP_READ: return fileRead(file, (BH_IOReadInfo *)arg);
case BH_IO_OP_WRITE: return fileWrite(file, (BH_IOWriteInfo *)arg);
case BH_IO_OP_CTL: return fileCtl(file, (BH_IOCtlInfo *)arg);
case BH_IO_OP_CAP: return fileCap(file, (int*)arg);
default: return BH_NOIMPL;
}
}
BH_IO *BH_FileNew(const char *path,
int mode,
int *result)
{
BH_File *file;
int code;
code = BH_OOM;
/* Allocate new file object and initialize it */
if ((file = malloc(sizeof(*file))))
{
file->parent.callback = (BH_IOCallback)fileCallback;
if ((code = fileInit(file, path, mode)))
{
free(file);
file = NULL;
}
}
/* Report error code */
if (result)
*result = code;
return (BH_IO*)file;
}
int BH_IOIsFile(BH_IO *device)
{
if (!device)
return 0;
return device->callback == (BH_IOCallback)fileCallback;
}
|