Skip to content

Commit

Permalink
[dfs] Update dfs v1 synchronously (#15)
Browse files Browse the repository at this point in the history
* [dfs] Update dfs v1 synchronously
  • Loading branch information
kurisaW authored Dec 27, 2023
1 parent b06e525 commit d8d10b4
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions dfs_lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,17 +539,29 @@ static int _dfs_lfs_rename(struct dfs_filesystem* dfs, const char* from, const c
/******************************************************************************
* file operations
******************************************************************************/
static int _dfs_lfs_open(struct dfs_fd* file)
static int _dfs_lfs_open(struct dfs_file* file)
{
struct dfs_filesystem* dfs;
dfs_lfs_t* dfs_lfs;
int result;
int flags = 0;

RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

dfs = (struct dfs_filesystem*)file->data;
dfs = (struct dfs_filesystem*)file->vnode->fs;

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;
}

dfs_lfs = (dfs_lfs_t*)dfs->data;

if (file->flags & O_DIRECTORY)
Expand All @@ -568,7 +580,7 @@ static int _dfs_lfs_open(struct dfs_fd* file)
if (file->flags & O_CREAT)
{
#ifndef LFS_READONLY
result = lfs_mkdir(dfs_lfs_fd->lfs, file->path);
result = lfs_mkdir(dfs_lfs_fd->lfs, file->vnode->path);
#else
result = -EINVAL;
#endif
Expand All @@ -578,7 +590,7 @@ static int _dfs_lfs_open(struct dfs_fd* file)
}
}

result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->path);
result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->vnode->path);
if (result != LFS_ERR_OK)
{
goto _error_dir;
Expand Down Expand Up @@ -625,7 +637,7 @@ static int _dfs_lfs_open(struct dfs_fd* file)
if (file->flags & O_APPEND)
flags |= LFS_O_APPEND;

result = lfs_file_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->path, flags);
result = lfs_file_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->vnode->path, flags);
if (result != LFS_ERR_OK)
{
goto _error_file;
Expand All @@ -634,7 +646,7 @@ static int _dfs_lfs_open(struct dfs_fd* file)
{
file->data = (void*)dfs_lfs_fd;
file->pos = dfs_lfs_fd->u.file.pos;
file->size = dfs_lfs_fd->u.file.ctz.size;
file->vnode->size = dfs_lfs_fd->u.file.ctz.size;
return RT_EOK;
}

Expand All @@ -648,16 +660,22 @@ static int _dfs_lfs_open(struct dfs_fd* file)
}
}

static int _dfs_lfs_close(struct dfs_fd* file)
static int _dfs_lfs_close(struct dfs_file* file)
{
int result;
dfs_lfs_fd_t* dfs_lfs_fd;
RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

RT_ASSERT(file->vnode->ref_count > 0);
if (file->vnode->ref_count > 1)
{
return 0;
}

dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;

if (file->type == FT_DIRECTORY)
if (file->vnode->type == FT_DIRECTORY)
{
result = lfs_dir_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir);
}
Expand All @@ -671,20 +689,20 @@ static int _dfs_lfs_close(struct dfs_fd* file)
return _lfs_result_to_dfs(result);
}

static int _dfs_lfs_ioctl(struct dfs_fd* file, int cmd, void* args)
static int _dfs_lfs_ioctl(struct dfs_file* file, int cmd, void* args)
{
return -ENOSYS;
}

static int _dfs_lfs_read(struct dfs_fd* file, void* buf, size_t len)
static int _dfs_lfs_read(struct dfs_file* file, void* buf, size_t len)
{
lfs_ssize_t ssize;
dfs_lfs_fd_t* dfs_lfs_fd;

RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

if (file->type == FT_DIRECTORY)
if (file->vnode->type == FT_DIRECTORY)
{
return -EISDIR;
}
Expand Down Expand Up @@ -715,14 +733,14 @@ static int _dfs_lfs_read(struct dfs_fd* file, void* buf, size_t len)
}

#ifndef LFS_READONLY
static int _dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)
static int _dfs_lfs_write(struct dfs_file* file, const void* buf, size_t len)
{
lfs_ssize_t ssize;
dfs_lfs_fd_t* dfs_lfs_fd;
RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

if (file->type == FT_DIRECTORY)
if (file->vnode->type == FT_DIRECTORY)
{
return -EISDIR;
}
Expand All @@ -748,13 +766,13 @@ static int _dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)

/* update position and file size */
file->pos = dfs_lfs_fd->u.file.pos;
file->size = dfs_lfs_fd->u.file.ctz.size;
file->vnode->size = dfs_lfs_fd->u.file.ctz.size;

return ssize;
}
#endif

static int _dfs_lfs_flush(struct dfs_fd* file)
static int _dfs_lfs_flush(struct dfs_file* file)
{
int result;
dfs_lfs_fd_t* dfs_lfs_fd;
Expand All @@ -768,7 +786,7 @@ static int _dfs_lfs_flush(struct dfs_fd* file)
return _lfs_result_to_dfs(result);
}

static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)
static int _dfs_lfs_lseek(struct dfs_file* file, rt_off_t offset)
{
dfs_lfs_fd_t* dfs_lfs_fd;

Expand All @@ -777,7 +795,7 @@ static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)

dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;

if (file->type == FT_REGULAR)
if (file->vnode->type == FT_REGULAR)
{
lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, offset, LFS_SEEK_SET);
if (soff < 0)
Expand All @@ -787,7 +805,7 @@ static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)

file->pos = dfs_lfs_fd->u.file.pos;
}
else if (file->type == FT_DIRECTORY)
else if (file->vnode->type == FT_DIRECTORY)
{
lfs_soff_t soff = lfs_dir_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, offset);
if (soff < 0)
Expand All @@ -801,7 +819,7 @@ static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)
return (file->pos);
}

static int _dfs_lfs_getdents(struct dfs_fd* file, struct dirent* dirp, uint32_t count)
static int _dfs_lfs_getdents(struct dfs_file* file, struct dirent* dirp, uint32_t count)
{
dfs_lfs_fd_t* dfs_lfs_fd;
int result;
Expand Down

0 comments on commit d8d10b4

Please sign in to comment.