2010-09-02 16:46:46 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_fs.h>
|
|
|
|
#include "dfs_romfs.h"
|
|
|
|
|
|
|
|
int dfs_romfs_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data)
|
|
|
|
{
|
|
|
|
struct romfs_dirent* root_dirent;
|
|
|
|
|
|
|
|
if (data == RT_NULL) return -DFS_STATUS_EIO;
|
|
|
|
|
|
|
|
root_dirent = (struct romfs_dirent*)data;
|
|
|
|
fs->data = root_dirent;
|
|
|
|
|
|
|
|
return DFS_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_unmount(struct dfs_filesystem* fs)
|
|
|
|
{
|
|
|
|
return DFS_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_ioctl(struct dfs_fd* file, int cmd, void* args)
|
|
|
|
{
|
|
|
|
return -DFS_STATUS_EIO;
|
|
|
|
}
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
struct romfs_dirent* dfs_romfs_lookup(struct romfs_dirent* root_dirent, const char* path, rt_size_t *size)
|
2010-09-02 16:46:46 +08:00
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
rt_size_t index, found;
|
2010-09-02 16:46:46 +08:00
|
|
|
const char *subpath, *subpath_end;
|
|
|
|
struct romfs_dirent* dirent;
|
2010-10-31 21:52:58 +08:00
|
|
|
rt_size_t dirent_size;
|
2010-09-02 16:46:46 +08:00
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
if (path[0] == '/' && path[1] == '\0')
|
|
|
|
{
|
|
|
|
*size = root_dirent->size;
|
|
|
|
return root_dirent;
|
|
|
|
}
|
2010-09-02 16:46:46 +08:00
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
/* goto root directy entries */
|
|
|
|
dirent = (struct romfs_dirent*)root_dirent->data;
|
|
|
|
dirent_size = root_dirent->size;
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
/* 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 != RT_NULL)
|
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
found = 0;
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
/* search in folder */
|
2010-10-31 21:52:58 +08:00
|
|
|
for (index = 0; index < dirent_size; index ++)
|
2010-09-02 16:46:46 +08:00
|
|
|
{
|
|
|
|
if (rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
|
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
dirent_size = dirent[index].size;
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
/* skip /// */
|
|
|
|
while (*subpath_end && *subpath_end == '/') subpath_end ++;
|
|
|
|
subpath = subpath_end;
|
|
|
|
while ((*subpath_end != '/') && *subpath_end) subpath_end ++;
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
if (!(*subpath))
|
|
|
|
{
|
|
|
|
*size = dirent_size;
|
|
|
|
return &dirent[index];
|
|
|
|
}
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
if (dirent[index].type == ROMFS_DIRENT_DIR)
|
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
/* enter directory */
|
2010-09-02 16:46:46 +08:00
|
|
|
dirent = (struct romfs_dirent*)dirent[index].data;
|
2010-10-31 21:52:58 +08:00
|
|
|
found = 1;
|
2010-09-02 16:46:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-10-31 21:52:58 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* return file dirent */
|
|
|
|
return &dirent[index];
|
|
|
|
}
|
2010-09-02 16:46:46 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-31 21:52:58 +08:00
|
|
|
|
|
|
|
if (!found) break; /* not found */
|
2010-09-02 16:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_read(struct dfs_fd* file, void *buf, rt_size_t count)
|
|
|
|
{
|
|
|
|
rt_size_t length;
|
|
|
|
struct romfs_dirent* dirent;
|
|
|
|
|
|
|
|
dirent = (struct romfs_dirent *)file->data;
|
|
|
|
RT_ASSERT(dirent != RT_NULL);
|
|
|
|
|
|
|
|
if (count < file->size - file->pos)
|
|
|
|
length = count;
|
|
|
|
else
|
|
|
|
length = file->size - file->pos;
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
memcpy(buf, &(dirent->data[file->pos]), length);
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
/* update file current position */
|
|
|
|
file->pos += length;
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_lseek(struct dfs_fd* file, rt_off_t offset)
|
|
|
|
{
|
|
|
|
if (offset < file->size)
|
|
|
|
{
|
|
|
|
file->pos = offset;
|
|
|
|
return file->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -DFS_STATUS_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_close(struct dfs_fd* file)
|
|
|
|
{
|
|
|
|
file->data = RT_NULL;
|
|
|
|
return DFS_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_open(struct dfs_fd* file)
|
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
rt_size_t size;
|
2010-09-02 16:46:46 +08:00
|
|
|
struct romfs_dirent* dirent;
|
2010-10-31 21:52:58 +08:00
|
|
|
struct romfs_dirent* root_dirent;
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
root_dirent = (struct romfs_dirent*)file->fs->data;
|
|
|
|
|
2010-09-14 12:45:45 +08:00
|
|
|
if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
|
2010-09-02 16:46:46 +08:00
|
|
|
return -DFS_STATUS_EINVAL;
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
|
2010-09-02 16:46:46 +08:00
|
|
|
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT;
|
|
|
|
|
2011-03-06 23:01:13 +08:00
|
|
|
/* is a directory but not with O_DIRECTORY flag */
|
|
|
|
if ((dirent->type == ROMFS_DIRENT_DIR) && !(file->flags & DFS_O_DIRECTORY))
|
|
|
|
return -DFS_STATUS_ENOENT;
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
file->data = dirent;
|
|
|
|
file->size = size;
|
2010-09-02 16:46:46 +08:00
|
|
|
file->pos = 0;
|
2010-10-31 21:52:58 +08:00
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
return DFS_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
int dfs_romfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
|
2010-09-02 16:46:46 +08:00
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
rt_size_t size;
|
2010-09-02 16:46:46 +08:00
|
|
|
struct romfs_dirent* dirent;
|
2010-10-31 21:52:58 +08:00
|
|
|
struct romfs_dirent* root_dirent;
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
root_dirent = (struct romfs_dirent*)fs->data;
|
2010-10-31 21:52:58 +08:00
|
|
|
dirent = dfs_romfs_lookup(root_dirent, path, &size);
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT;
|
|
|
|
|
|
|
|
st->st_dev = 0;
|
2011-02-22 08:14:23 +08:00
|
|
|
st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
|
|
|
|
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
|
2010-10-31 21:52:58 +08:00
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
if (dirent->type == ROMFS_DIRENT_DIR)
|
2011-02-22 08:14:23 +08:00
|
|
|
{
|
2010-10-31 21:52:58 +08:00
|
|
|
st->st_mode &= ~DFS_S_IFREG;
|
|
|
|
st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
|
2010-09-02 16:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
st->st_size = dirent->size;
|
|
|
|
st->st_mtime = 0;
|
|
|
|
st->st_blksize = 512;
|
|
|
|
|
|
|
|
return DFS_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
int dfs_romfs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count)
|
2010-09-02 16:46:46 +08:00
|
|
|
{
|
|
|
|
rt_size_t index;
|
|
|
|
const char *name;
|
2010-10-31 21:52:58 +08:00
|
|
|
struct dirent* d;
|
2010-09-02 16:46:46 +08:00
|
|
|
struct romfs_dirent *dirent, *sub_dirent;
|
|
|
|
|
|
|
|
dirent = (struct romfs_dirent*) file->data;
|
2010-10-31 21:52:58 +08:00
|
|
|
RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
|
2010-09-02 16:46:46 +08:00
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
/* enter directory */
|
|
|
|
dirent = (struct romfs_dirent*) dirent->data;
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
/* make integer count */
|
2010-10-31 21:52:58 +08:00
|
|
|
count = (count / sizeof(struct dirent));
|
2010-09-02 16:46:46 +08:00
|
|
|
if ( count == 0 ) return -DFS_STATUS_EINVAL;
|
|
|
|
|
|
|
|
index = 0;
|
2010-10-31 21:52:58 +08:00
|
|
|
for (index = 0; index < count && file->pos < file->size; index ++)
|
2010-09-02 16:46:46 +08:00
|
|
|
{
|
|
|
|
d = dirp + index;
|
2011-02-22 08:14:23 +08:00
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
sub_dirent = &dirent[file->pos];
|
|
|
|
name = sub_dirent->name;
|
|
|
|
|
|
|
|
/* fill dirent */
|
2010-10-31 21:52:58 +08:00
|
|
|
if (sub_dirent->type == ROMFS_DIRENT_DIR)
|
|
|
|
d->d_type = DFS_DT_DIR;
|
|
|
|
else
|
|
|
|
d->d_type = DFS_DT_REG;
|
|
|
|
|
2011-02-22 08:14:23 +08:00
|
|
|
d->d_namlen = rt_strlen(name);
|
2010-10-31 21:52:58 +08:00
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
|
|
|
rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
/* move to next position */
|
|
|
|
++ file->pos;
|
|
|
|
}
|
2010-10-31 21:52:58 +08:00
|
|
|
|
|
|
|
return index * sizeof(struct dirent);
|
2010-09-02 16:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dfs_filesystem_operation _romfs =
|
|
|
|
{
|
|
|
|
"rom",
|
|
|
|
dfs_romfs_mount,
|
|
|
|
dfs_romfs_unmount,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
|
|
|
|
dfs_romfs_open,
|
|
|
|
dfs_romfs_close,
|
|
|
|
dfs_romfs_ioctl,
|
|
|
|
dfs_romfs_read,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
dfs_romfs_lseek,
|
|
|
|
dfs_romfs_getdents,
|
|
|
|
RT_NULL,
|
|
|
|
dfs_romfs_stat,
|
|
|
|
RT_NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
int dfs_romfs_init(void)
|
|
|
|
{
|
|
|
|
/* register rom file system */
|
|
|
|
dfs_register(&_romfs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|