forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
382 lines (312 loc) · 5.52 KB
/
fs.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <kernel.h>
#include <limits.h>
#include <posix/unistd.h>
#include <posix/dirent.h>
#include <string.h>
#include <sys/fdtable.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fs/fs.h>
BUILD_ASSERT(PATH_MAX >= MAX_FILE_NAME, "PATH_MAX is less than MAX_FILE_NAME");
struct posix_fs_desc {
union {
struct fs_file_t file;
struct fs_dir_t dir;
};
bool is_dir;
bool used;
};
static struct posix_fs_desc desc_array[CONFIG_POSIX_MAX_OPEN_FILES];
static struct fs_dirent fdirent;
static struct dirent pdirent;
static struct fd_op_vtable fs_fd_op_vtable;
static struct posix_fs_desc *posix_fs_alloc_obj(bool is_dir)
{
int i;
struct posix_fs_desc *ptr = NULL;
unsigned int key = irq_lock();
for (i = 0; i < CONFIG_POSIX_MAX_OPEN_FILES; i++) {
if (desc_array[i].used == false) {
ptr = &desc_array[i];
ptr->used = true;
ptr->is_dir = is_dir;
break;
}
}
irq_unlock(key);
return ptr;
}
static inline void posix_fs_free_obj(struct posix_fs_desc *ptr)
{
ptr->used = false;
}
static int posix_mode_to_zephyr(int mf)
{
int mode = (mf & O_CREAT) ? FS_O_CREATE : 0;
mode |= (mf & O_APPEND) ? FS_O_APPEND : 0;
switch (mf & O_ACCMODE) {
case O_RDONLY:
mode |= FS_O_READ;
break;
case O_WRONLY:
mode |= FS_O_WRITE;
break;
case O_RDWR:
mode |= FS_O_RDWR;
break;
default:
break;
}
return mode;
}
/**
* @brief Open a file.
*
* See IEEE 1003.1
*/
int open(const char *name, int flags, ...)
{
int rc, fd;
struct posix_fs_desc *ptr = NULL;
int zmode = posix_mode_to_zephyr(flags);
if (zmode < 0) {
return zmode;
}
fd = z_reserve_fd();
if (fd < 0) {
return -1;
}
ptr = posix_fs_alloc_obj(false);
if (ptr == NULL) {
z_free_fd(fd);
errno = EMFILE;
return -1;
}
fs_file_t_init(&ptr->file);
rc = fs_open(&ptr->file, name, zmode);
if (rc < 0) {
posix_fs_free_obj(ptr);
z_free_fd(fd);
errno = -rc;
return -1;
}
z_finalize_fd(fd, ptr, &fs_fd_op_vtable);
return fd;
}
FUNC_ALIAS(open, _open, int);
static int fs_close_vmeth(void *obj)
{
struct posix_fs_desc *ptr = obj;
int rc;
rc = fs_close(&ptr->file);
posix_fs_free_obj(ptr);
return rc;
}
static int fs_ioctl_vmeth(void *obj, unsigned int request, va_list args)
{
int rc = 0;
struct posix_fs_desc *ptr = obj;
switch (request) {
case ZFD_IOCTL_LSEEK: {
off_t offset;
int whence;
offset = va_arg(args, off_t);
whence = va_arg(args, int);
rc = fs_seek(&ptr->file, offset, whence);
if (rc == 0) {
rc = fs_tell(&ptr->file);
}
break;
}
default:
errno = EOPNOTSUPP;
return -1;
}
if (rc < 0) {
errno = -rc;
return -1;
}
return rc;
}
/**
* @brief Write to a file.
*
* See IEEE 1003.1
*/
static ssize_t fs_write_vmeth(void *obj, const void *buffer, size_t count)
{
ssize_t rc;
struct posix_fs_desc *ptr = obj;
rc = fs_write(&ptr->file, buffer, count);
if (rc < 0) {
errno = -rc;
return -1;
}
return rc;
}
/**
* @brief Read from a file.
*
* See IEEE 1003.1
*/
static ssize_t fs_read_vmeth(void *obj, void *buffer, size_t count)
{
ssize_t rc;
struct posix_fs_desc *ptr = obj;
rc = fs_read(&ptr->file, buffer, count);
if (rc < 0) {
errno = -rc;
return -1;
}
return rc;
}
static struct fd_op_vtable fs_fd_op_vtable = {
.read = fs_read_vmeth,
.write = fs_write_vmeth,
.close = fs_close_vmeth,
.ioctl = fs_ioctl_vmeth,
};
/**
* @brief Open a directory stream.
*
* See IEEE 1003.1
*/
DIR *opendir(const char *dirname)
{
int rc;
struct posix_fs_desc *ptr;
ptr = posix_fs_alloc_obj(true);
if (ptr == NULL) {
errno = EMFILE;
return NULL;
}
fs_dir_t_init(&ptr->dir);
rc = fs_opendir(&ptr->dir, dirname);
if (rc < 0) {
posix_fs_free_obj(ptr);
errno = -rc;
return NULL;
}
return ptr;
}
/**
* @brief Close a directory stream.
*
* See IEEE 1003.1
*/
int closedir(DIR *dirp)
{
int rc;
struct posix_fs_desc *ptr = dirp;
if (dirp == NULL) {
errno = EBADF;
return -1;
}
rc = fs_closedir(&ptr->dir);
posix_fs_free_obj(ptr);
if (rc < 0) {
errno = -rc;
return -1;
}
return 0;
}
/**
* @brief Read a directory.
*
* See IEEE 1003.1
*/
struct dirent *readdir(DIR *dirp)
{
int rc;
struct posix_fs_desc *ptr = dirp;
if (dirp == NULL) {
errno = EBADF;
return NULL;
}
rc = fs_readdir(&ptr->dir, &fdirent);
if (rc < 0) {
errno = -rc;
return NULL;
}
rc = strlen(fdirent.name);
rc = (rc < MAX_FILE_NAME) ? rc : (MAX_FILE_NAME - 1);
(void)memcpy(pdirent.d_name, fdirent.name, rc);
/* Make sure the name is NULL terminated */
pdirent.d_name[rc] = '\0';
return &pdirent;
}
/**
* @brief Rename a file.
*
* See IEEE 1003.1
*/
int rename(const char *old, const char *new)
{
int rc;
rc = fs_rename(old, new);
if (rc < 0) {
errno = -rc;
return -1;
}
return 0;
}
/**
* @brief Remove a directory entry.
*
* See IEEE 1003.1
*/
int unlink(const char *path)
{
int rc;
rc = fs_unlink(path);
if (rc < 0) {
errno = -rc;
return -1;
}
return 0;
}
/**
* @brief Get file status.
*
* See IEEE 1003.1
*/
int stat(const char *path, struct stat *buf)
{
int rc;
struct fs_statvfs stat;
if (buf == NULL) {
errno = EBADF;
return -1;
}
rc = fs_statvfs(path, &stat);
if (rc < 0) {
errno = -rc;
return -1;
}
buf->st_size = stat.f_bsize * stat.f_blocks;
buf->st_blksize = stat.f_bsize;
buf->st_blocks = stat.f_blocks;
return 0;
}
/**
* @brief Make a directory.
*
* See IEEE 1003.1
*/
int mkdir(const char *path, mode_t mode)
{
int rc;
ARG_UNUSED(mode);
rc = fs_mkdir(path);
if (rc < 0) {
errno = -rc;
return -1;
}
return 0;
}