2013-05-05 07:16:49 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-05-05 07:16:49 +08:00
|
|
|
*
|
2018-10-15 01:46:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-05-05 07:16:49 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2013-04-15 Bernard the first version
|
|
|
|
* 2013-05-05 Bernard remove CRC for ramfs persistence
|
2013-06-03 06:44:35 +08:00
|
|
|
* 2013-05-22 Bernard fix the no entry issue.
|
2013-05-05 07:16:49 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_fs.h>
|
2017-10-25 18:41:53 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
#include "dfs_ramfs.h"
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
int dfs_ramfs_mount(struct dfs_filesystem *fs,
|
|
|
|
unsigned long rwflag,
|
|
|
|
const void *data)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
struct dfs_ramfs *ramfs;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (data == NULL)
|
|
|
|
return -EIO;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
ramfs = (struct dfs_ramfs *)data;
|
2013-05-05 07:16:49 +08:00
|
|
|
fs->data = ramfs;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_unmount(struct dfs_filesystem *fs)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
fs->data = NULL;
|
2013-06-29 10:43:03 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_statfs(struct dfs_filesystem *fs, struct statfs *buf)
|
|
|
|
{
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
|
|
|
|
ramfs = (struct dfs_ramfs *)fs->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(ramfs != NULL);
|
|
|
|
RT_ASSERT(buf != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
buf->f_bsize = 512;
|
2019-04-03 18:09:52 +08:00
|
|
|
buf->f_blocks = ramfs->memheap.pool_size / 512;
|
|
|
|
buf->f_bfree = ramfs->memheap.available_size / 512;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_ioctl(struct dfs_fd *file, int cmd, void *args)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
struct ramfs_dirent *dfs_ramfs_lookup(struct dfs_ramfs *ramfs,
|
|
|
|
const char *path,
|
|
|
|
rt_size_t *size)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
const char *subpath;
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
|
|
|
|
subpath = path;
|
2013-06-29 10:43:03 +08:00
|
|
|
while (*subpath == '/' && *subpath)
|
|
|
|
subpath ++;
|
2013-05-05 07:16:49 +08:00
|
|
|
if (! *subpath) /* is root directory */
|
|
|
|
{
|
|
|
|
*size = 0;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
return &(ramfs->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (dirent = rt_list_entry(ramfs->root.list.next, struct ramfs_dirent, list);
|
2013-06-26 22:30:40 +08:00
|
|
|
dirent != &(ramfs->root);
|
|
|
|
dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list))
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
if (rt_strcmp(dirent->name, subpath) == 0)
|
|
|
|
{
|
|
|
|
*size = dirent->size;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
return dirent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
2017-10-15 22:44:53 +08:00
|
|
|
return NULL;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
int dfs_ramfs_read(struct dfs_fd *file, void *buf, size_t count)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
rt_size_t length;
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = (struct ramfs_dirent *)file->vnode->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(dirent != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (count < file->vnode->size - file->pos)
|
2013-05-05 07:16:49 +08:00
|
|
|
length = count;
|
|
|
|
else
|
2022-12-03 12:07:44 +08:00
|
|
|
length = file->vnode->size - file->pos;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
if (length > 0)
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(buf, &(dirent->data[file->pos]), length);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
/* update file current position */
|
|
|
|
file->pos += length;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = (struct ramfs_dirent *)fd->vnode->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(dirent != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-25 18:41:53 +08:00
|
|
|
ramfs = dirent->fs;
|
|
|
|
RT_ASSERT(ramfs != NULL);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (count + fd->pos > fd->vnode->size)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t *ptr;
|
|
|
|
ptr = rt_memheap_realloc(&(ramfs->memheap), dirent->data, fd->pos + count);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (ptr == NULL)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update dirent and file size */
|
|
|
|
dirent->data = ptr;
|
|
|
|
dirent->size = fd->pos + count;
|
2022-12-03 12:07:44 +08:00
|
|
|
fd->vnode->size = dirent->size;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0)
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(dirent->data + fd->pos, buf, count);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
/* update file current position */
|
|
|
|
fd->pos += count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
int dfs_ramfs_lseek(struct dfs_fd *file, off_t offset)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (offset <= (off_t)file->vnode->size)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
file->pos = offset;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
return file->pos;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_close(struct dfs_fd *file)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->vnode->data = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_open(struct dfs_fd *file)
|
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
struct ramfs_dirent *dirent;
|
2018-01-13 11:35:45 +08:00
|
|
|
struct dfs_filesystem *fs;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
if (file->vnode->type == FT_DIRECTORY
|
|
|
|
&& !(file->flags & O_DIRECTORY))
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
file->pos = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = file->vnode->fs;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2018-01-13 11:35:45 +08:00
|
|
|
ramfs = (struct dfs_ramfs *)fs->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(ramfs != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_DIRECTORY)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_CREAT)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSPC;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open directory */
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = dfs_ramfs_lookup(ramfs, file->vnode->path, &size);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
if (dirent == &(ramfs->root)) /* it's root directory */
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (!(file->flags & O_DIRECTORY))
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->type = FT_DIRECTORY;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = dfs_ramfs_lookup(ramfs, file->vnode->path, &size);
|
2013-05-05 07:16:49 +08:00
|
|
|
if (dirent == &(ramfs->root)) /* it's root directory */
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_CREAT || file->flags & O_WRONLY)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
char *name_ptr;
|
|
|
|
|
|
|
|
/* create a file entry */
|
2013-06-29 10:43:03 +08:00
|
|
|
dirent = (struct ramfs_dirent *)
|
|
|
|
rt_memheap_alloc(&(ramfs->memheap),
|
|
|
|
sizeof(struct ramfs_dirent));
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove '/' separator */
|
2022-12-03 12:07:44 +08:00
|
|
|
name_ptr = file->vnode->path;
|
2013-06-26 22:30:40 +08:00
|
|
|
while (*name_ptr == '/' && *name_ptr)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
name_ptr++;
|
|
|
|
}
|
2013-05-05 07:16:49 +08:00
|
|
|
strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX);
|
|
|
|
|
|
|
|
rt_list_init(&(dirent->list));
|
2017-10-15 22:44:53 +08:00
|
|
|
dirent->data = NULL;
|
2013-05-05 07:16:49 +08:00
|
|
|
dirent->size = 0;
|
2017-10-25 18:41:53 +08:00
|
|
|
dirent->fs = ramfs;
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->type = FT_DIRECTORY;
|
2017-10-25 18:41:53 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
/* add to the root directory */
|
|
|
|
rt_list_insert_after(&(ramfs->root.list), &(dirent->list));
|
|
|
|
}
|
2013-06-26 22:30:40 +08:00
|
|
|
else
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
/* Creates a new file.
|
|
|
|
* If the file is existing, it is truncated and overwritten.
|
|
|
|
*/
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_TRUNC)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
dirent->size = 0;
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent->data != NULL)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
rt_memheap_free(dirent->data);
|
2017-10-15 22:44:53 +08:00
|
|
|
dirent->data = NULL;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->data = dirent;
|
|
|
|
file->vnode->size = dirent->size;
|
2017-10-25 18:41:53 +08:00
|
|
|
if (file->flags & O_APPEND)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
file->pos = file->vnode->size;
|
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
else
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-25 18:41:53 +08:00
|
|
|
file->pos = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return 0;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
int dfs_ramfs_stat(struct dfs_filesystem *fs,
|
|
|
|
const char *path,
|
|
|
|
struct stat *st)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
|
|
|
|
ramfs = (struct dfs_ramfs *)fs->data;
|
|
|
|
dirent = dfs_ramfs_lookup(ramfs, path, &size);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
st->st_dev = 0;
|
2017-10-15 22:44:53 +08:00
|
|
|
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
|
|
|
|
S_IWUSR | S_IWGRP | S_IWOTH;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
st->st_size = dirent->size;
|
|
|
|
st->st_mtime = 0;
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
int dfs_ramfs_getdents(struct dfs_fd *file,
|
|
|
|
struct dirent *dirp,
|
2017-10-15 22:44:53 +08:00
|
|
|
uint32_t count)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
rt_size_t index, end;
|
|
|
|
struct dirent *d;
|
|
|
|
struct ramfs_dirent *dirent;
|
2013-06-29 10:43:03 +08:00
|
|
|
struct dfs_ramfs *ramfs;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = (struct ramfs_dirent *)file->vnode->data;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2017-10-25 18:41:53 +08:00
|
|
|
ramfs = dirent->fs;
|
|
|
|
RT_ASSERT(ramfs != RT_NULL);
|
|
|
|
|
2017-10-31 09:53:22 +08:00
|
|
|
if (dirent != &(ramfs->root))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
/* make integer count */
|
|
|
|
count = (count / sizeof(struct dirent));
|
2013-06-29 10:43:03 +08:00
|
|
|
if (count == 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
end = file->pos + count;
|
|
|
|
index = 0;
|
|
|
|
count = 0;
|
|
|
|
for (dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list);
|
2013-06-26 22:30:40 +08:00
|
|
|
dirent != &(ramfs->root) && index < end;
|
|
|
|
dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list))
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
if (index >= (rt_size_t)file->pos)
|
|
|
|
{
|
|
|
|
d = dirp + count;
|
2017-10-25 18:41:53 +08:00
|
|
|
d->d_type = DT_REG;
|
2013-05-05 07:16:49 +08:00
|
|
|
d->d_namlen = RT_NAME_MAX;
|
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
|
|
|
rt_strncpy(d->d_name, dirent->name, RAMFS_NAME_MAX);
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
file->pos += 1;
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count * sizeof(struct dirent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_ramfs_unlink(struct dfs_filesystem *fs, const char *path)
|
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
|
|
|
|
ramfs = (struct dfs_ramfs *)fs->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(ramfs != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
dirent = dfs_ramfs_lookup(ramfs, path, &size);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
rt_list_remove(&(dirent->list));
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent->data != NULL)
|
2013-05-05 07:16:49 +08:00
|
|
|
rt_memheap_free(dirent->data);
|
|
|
|
rt_memheap_free(dirent);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
int dfs_ramfs_rename(struct dfs_filesystem *fs,
|
|
|
|
const char *oldpath,
|
|
|
|
const char *newpath)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
struct ramfs_dirent *dirent;
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
rt_size_t size;
|
|
|
|
|
|
|
|
ramfs = (struct dfs_ramfs *)fs->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(ramfs != NULL);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
dirent = dfs_ramfs_lookup(ramfs, newpath, &size);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent != NULL)
|
|
|
|
return -EEXIST;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
dirent = dfs_ramfs_lookup(ramfs, oldpath, &size);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
|
|
|
return -ENOENT;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
strncpy(dirent->name, newpath, RAMFS_NAME_MAX);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-05-05 07:16:49 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static const struct dfs_file_ops _ram_fops =
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
dfs_ramfs_open,
|
|
|
|
dfs_ramfs_close,
|
|
|
|
dfs_ramfs_ioctl,
|
|
|
|
dfs_ramfs_read,
|
|
|
|
dfs_ramfs_write,
|
2017-10-15 22:44:53 +08:00
|
|
|
NULL, /* flush */
|
2013-05-05 07:16:49 +08:00
|
|
|
dfs_ramfs_lseek,
|
|
|
|
dfs_ramfs_getdents,
|
2017-10-15 22:44:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfs_filesystem_ops _ramfs =
|
|
|
|
{
|
|
|
|
"ram",
|
|
|
|
DFS_FS_FLAG_DEFAULT,
|
|
|
|
&_ram_fops,
|
|
|
|
|
|
|
|
dfs_ramfs_mount,
|
|
|
|
dfs_ramfs_unmount,
|
|
|
|
NULL, /* mkfs */
|
|
|
|
dfs_ramfs_statfs,
|
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
dfs_ramfs_unlink,
|
|
|
|
dfs_ramfs_stat,
|
|
|
|
dfs_ramfs_rename,
|
|
|
|
};
|
|
|
|
|
|
|
|
int dfs_ramfs_init(void)
|
|
|
|
{
|
|
|
|
/* register ram file system */
|
|
|
|
dfs_register(&_ramfs);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2013-05-05 07:16:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-11-03 11:44:37 +08:00
|
|
|
INIT_COMPONENT_EXPORT(dfs_ramfs_init);
|
2013-05-05 07:16:49 +08:00
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
struct dfs_ramfs *dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size)
|
2013-05-05 07:16:49 +08:00
|
|
|
{
|
|
|
|
struct dfs_ramfs *ramfs;
|
|
|
|
rt_uint8_t *data_ptr;
|
|
|
|
rt_err_t result;
|
|
|
|
|
2013-06-29 10:43:03 +08:00
|
|
|
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
|
|
|
ramfs = (struct dfs_ramfs *)pool;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
data_ptr = (rt_uint8_t *)(ramfs + 1);
|
|
|
|
size = size - sizeof(struct dfs_ramfs);
|
|
|
|
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
|
|
|
|
|
|
|
result = rt_memheap_init(&ramfs->memheap, "ramfs", data_ptr, size);
|
2013-06-26 22:30:40 +08:00
|
|
|
if (result != RT_EOK)
|
2017-10-15 22:44:53 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
/* detach this memheap object from the system */
|
2019-04-03 18:09:52 +08:00
|
|
|
rt_object_detach((rt_object_t) & (ramfs->memheap));
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
/* initialize ramfs object */
|
|
|
|
ramfs->magic = RAMFS_MAGIC;
|
2018-09-20 12:03:29 +08:00
|
|
|
ramfs->memheap.parent.type = RT_Object_Class_MemHeap | RT_Object_Class_Static;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
/* initialize root directory */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&(ramfs->root), 0x00, sizeof(ramfs->root));
|
2013-05-05 07:16:49 +08:00
|
|
|
rt_list_init(&(ramfs->root.list));
|
|
|
|
ramfs->root.size = 0;
|
|
|
|
strcpy(ramfs->root.name, ".");
|
2018-01-13 11:35:45 +08:00
|
|
|
ramfs->root.fs = ramfs;
|
2013-05-05 07:16:49 +08:00
|
|
|
|
|
|
|
return ramfs;
|
|
|
|
}
|
2017-10-25 18:41:53 +08:00
|
|
|
|