-
Notifications
You must be signed in to change notification settings - Fork 5k
/
dfs_romfs.c
397 lines (331 loc) · 9.56 KB
/
dfs_romfs.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <rtthread.h>
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_dentry.h>
#include <dfs_file.h>
#include <dfs_mnt.h>
#include "dfs_romfs.h"
#include <errno.h>
#include <fcntl.h>
#include <rtdbg.h>
static const struct dfs_file_ops _rom_fops;
static const mode_t romfs_modemap[] =
{
S_IFREG | 0644, /* regular file */
S_IFDIR | 0644, /* directory */
0, /* hard link */
S_IFLNK | 0777, /* symlink */
S_IFBLK | 0600, /* blockdev */
S_IFCHR | 0600, /* chardev */
S_IFSOCK | 0644, /* socket */
S_IFIFO | 0644 /* FIFO */
};
static int dfs_romfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
{
struct romfs_dirent *root_dirent;
if (data == NULL)
return -1;
root_dirent = (struct romfs_dirent *)data;
mnt->data = root_dirent;
return 0;
}
static int dfs_romfs_umount(struct dfs_mnt *fs)
{
return RT_EOK;
}
int dfs_romfs_ioctl(struct dfs_file *file, int cmd, void *args)
{
int ret = RT_EOK;
struct romfs_dirent *dirent;
dirent = (struct romfs_dirent *)file->data;
RT_ASSERT(dirent != NULL);
switch (cmd)
{
case RT_FIOGETADDR:
{
*(rt_ubase_t*)args = (rt_ubase_t)dirent->data;
break;
}
case RT_FIOFTRUNCATE:
{
break;
}
default:
ret = -RT_EINVAL;
break;
}
return ret;
}
rt_inline int check_dirent(struct romfs_dirent *dirent)
{
if (dirent == NULL
||(dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
|| dirent->size == ~0)
return -1;
return 0;
}
struct romfs_dirent *__dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
{
rt_size_t index, found;
const char *subpath, *subpath_end;
struct romfs_dirent *dirent;
rt_size_t dirent_size;
/* Check the root_dirent. */
if (check_dirent(root_dirent) != 0)
return NULL;
if (path[0] == '/' && path[1] == '\0')
{
*size = root_dirent->size;
return root_dirent;
}
/* goto root directy entries */
dirent = (struct romfs_dirent *)root_dirent->data;
dirent_size = root_dirent->size;
/* get the end position of this subpath */
subpath_end = path;
/* skip /// */
while (*subpath_end && *subpath_end == '/')
subpath_end ++;
subpath = subpath_end;
while ((*subpath_end != '/') && *subpath_end)
subpath_end ++;
while (dirent != NULL)
{
found = 0;
/* search in folder */
for (index = 0; index < dirent_size; index ++)
{
if (check_dirent(&dirent[index]) != 0)
return NULL;
if (rt_strlen(dirent[index].name) == (subpath_end - subpath) &&
rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
{
dirent_size = dirent[index].size;
/* skip /// */
while (*subpath_end && *subpath_end == '/')
subpath_end ++;
subpath = subpath_end;
while ((*subpath_end != '/') && *subpath_end)
subpath_end ++;
if (!(*subpath))
{
*size = dirent_size;
return &dirent[index];
}
if (dirent[index].type == ROMFS_DIRENT_DIR)
{
/* enter directory */
dirent = (struct romfs_dirent *)dirent[index].data;
found = 1;
break;
}
else
{
/* return file dirent */
return &dirent[index];
}
}
}
if (!found)
break; /* not found */
}
/* not found */
return NULL;
}
static struct dfs_vnode *dfs_romfs_lookup (struct dfs_dentry *dentry)
{
rt_size_t size;
struct dfs_vnode *vnode = RT_NULL;
struct romfs_dirent *root_dirent = RT_NULL, *dirent = RT_NULL;
RT_ASSERT(dentry != RT_NULL);
RT_ASSERT(dentry->mnt != RT_NULL);
root_dirent = (struct romfs_dirent *)dentry->mnt->data;
if (check_dirent(root_dirent) == 0)
{
/* create a vnode */
DLOG(msg, "rom", "vnode", DLOG_MSG, "dfs_vnode_create()");
vnode = dfs_vnode_create();
if (vnode)
{
dirent = __dfs_romfs_lookup(root_dirent, dentry->pathname, &size);
if (dirent)
{
vnode->nlink = 1;
vnode->size = dirent->size;
if (dirent->type == ROMFS_DIRENT_DIR)
{
vnode->mode = romfs_modemap[ROMFS_DIRENT_DIR] | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
vnode->type = FT_DIRECTORY;
}
else if (dirent->type == ROMFS_DIRENT_FILE)
{
vnode->mode = romfs_modemap[ROMFS_DIRENT_FILE] | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
vnode->type = FT_REGULAR;
}
DLOG(msg, "rom", "rom", DLOG_MSG, "vnode->data = dirent");
vnode->data = dirent;
vnode->mnt = dentry->mnt;
}
else
{
/* no-entry */
DLOG(msg, "rom", "vnode", DLOG_MSG, "dfs_vnode_destroy, no-dentry");
dfs_vnode_destroy(vnode);
vnode = RT_NULL;
}
}
}
return vnode;
}
static int dfs_romfs_free_vnode(struct dfs_vnode *vnode)
{
/* nothing to be freed */
if (vnode->ref_count <= 1)
{
vnode->data = NULL;
}
return 0;
}
static ssize_t dfs_romfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
{
rt_size_t length;
struct romfs_dirent *dirent;
dirent = (struct romfs_dirent *)file->vnode->data;
RT_ASSERT(dirent != NULL);
if (check_dirent(dirent) != 0)
{
return -EIO;
}
if (count < file->vnode->size - *pos)
length = count;
else
length = file->vnode->size - *pos;
if (length > 0)
memcpy(buf, &(dirent->data[*pos]), length);
/* update file current position */
*pos += length;
return length;
}
static int dfs_romfs_close(struct dfs_file *file)
{
return RT_EOK;
}
int dfs_romfs_open(struct dfs_file *file)
{
rt_size_t size;
struct romfs_dirent *dirent;
struct romfs_dirent *root_dirent;
struct dfs_mnt *mnt;
if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
{
return -EINVAL;
}
mnt = file->dentry->mnt;
RT_ASSERT(mnt != RT_NULL);
root_dirent = (struct romfs_dirent *)mnt->data;
if (check_dirent(root_dirent) != 0)
{
return -EIO;
}
/* get rom dirent */
dirent = __dfs_romfs_lookup(root_dirent, file->dentry->pathname, &size);
if (dirent == NULL)
{
return -ENOENT;
}
file->data = dirent;
file->fops = &_rom_fops;
file->fpos = 0;
return RT_EOK;
}
static int dfs_romfs_stat(struct dfs_dentry *dentry, struct stat *st)
{
rt_err_t ret = dfs_file_lock();
if (ret == RT_EOK)
{
st->st_dev = 0;
st->st_mode = dentry->vnode->mode;
st->st_size = dentry->vnode->size;
st->st_nlink = dentry->vnode->nlink;
st->st_mtime = 0;
dfs_file_unlock();
}
return RT_EOK;
}
static int dfs_romfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
{
rt_size_t index;
const char *name;
struct dirent *d;
struct romfs_dirent *dirent, *sub_dirent;
dirent = (struct romfs_dirent *)file->vnode->data;
if (check_dirent(dirent) != 0)
{
return -EIO;
}
RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
/* enter directory */
dirent = (struct romfs_dirent *)dirent->data;
/* make integer count */
count = (count / sizeof(struct dirent));
if (count == 0)
{
return -EINVAL;
}
index = 0;
for (index = 0; index < count && file->fpos < file->vnode->size; index++)
{
d = dirp + index;
sub_dirent = &dirent[file->fpos];
name = sub_dirent->name;
/* fill dirent */
if (sub_dirent->type == ROMFS_DIRENT_DIR)
d->d_type = DT_DIR;
else
d->d_type = DT_REG;
d->d_namlen = rt_strlen(name);
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, name, DIRENT_NAME_MAX);
/* move to next position */
++ file->fpos;
}
return index * sizeof(struct dirent);
}
static const struct dfs_file_ops _rom_fops =
{
.open = dfs_romfs_open,
.close = dfs_romfs_close,
.lseek = generic_dfs_lseek,
.read = dfs_romfs_read,
.getdents = dfs_romfs_getdents,
};
static const struct dfs_filesystem_ops _romfs_ops =
{
.name ="rom",
.flags = 0,
.default_fops = &_rom_fops,
.mount = dfs_romfs_mount,
.umount = dfs_romfs_umount,
.stat = dfs_romfs_stat,
.lookup = dfs_romfs_lookup,
.free_vnode = dfs_romfs_free_vnode
};
static struct dfs_filesystem_type _romfs =
{
.fs_ops = &_romfs_ops,
};
int dfs_romfs_init(void)
{
/* register rom file system */
dfs_register(&_romfs);
return 0;
}
INIT_COMPONENT_EXPORT(dfs_romfs_init);