fs/directory: move private directory information to filesystem

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong
2022-08-05 12:31:25 +08:00
committed by Xiang Xiao
parent 90db4daca9
commit fe17f747a7
34 changed files with 939 additions and 921 deletions
+47 -10
View File
@@ -40,7 +40,6 @@
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/dirent.h>
#include <nuttx/fs/ioctl.h>
#include "cromfs.h"
@@ -57,6 +56,13 @@
* Private Types
****************************************************************************/
struct cromfs_dir_s
{
struct fs_dirent_s cr_base; /* VFS directory structure */
uint32_t cr_firstoffset; /* Offset to the first entry in the directory */
uint32_t cr_curroffset; /* Current offset into the directory contents */
};
/* This structure represents an open, regular file */
struct cromfs_file_s
@@ -142,7 +148,9 @@ static int cromfs_fstat(FAR const struct file *filep,
FAR struct stat *buf);
static int cromfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath, FAR struct fs_dirent_s *dir);
FAR const char *relpath, FAR struct fs_dirent_s **dir);
static int cromfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int cromfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
@@ -184,7 +192,7 @@ const struct mountpt_operations cromfs_operations =
NULL, /* truncate */
cromfs_opendir, /* opendir */
NULL, /* closedir */
cromfs_closedir, /* closedir */
cromfs_readdir, /* readdir */
cromfs_rewinddir, /* rewinddir */
@@ -1181,9 +1189,10 @@ static int cromfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
****************************************************************************/
static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct fs_dirent_s *dir)
FAR struct fs_dirent_s **dir)
{
FAR const struct cromfs_volume_s *fs;
FAR struct cromfs_dir_s *cdir;
FAR struct cromfs_nodeinfo_s info;
uint32_t offset;
int ret;
@@ -1217,13 +1226,35 @@ static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
return -ENOTDIR;
}
cdir = kmm_zalloc(sizeof(*cdir));
if (cdir == NULL)
{
return -ENOMEM;
}
/* Set the start node and next node to the first entry in the directory */
dir->u.cromfs.cr_firstoffset = info.ci_child;
dir->u.cromfs.cr_curroffset = info.ci_child;
cdir->cr_firstoffset = info.ci_child;
cdir->cr_curroffset = info.ci_child;
*dir = &cdir->cr_base;
return OK;
}
/****************************************************************************
* Name: cromfs_closedir
*
* Description: close directory.
*
****************************************************************************/
static int cromfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(mountpt != NULL);
kmm_free(dir);
return 0;
}
/****************************************************************************
* Name: cromfs_readdir
*
@@ -1237,6 +1268,7 @@ static int cromfs_readdir(FAR struct inode *mountpt,
{
FAR const struct cromfs_volume_s *fs;
FAR const struct cromfs_node_s *node;
FAR struct cromfs_dir_s *cdir;
struct cromfs_node_s newnode;
FAR char *name;
uint32_t offset;
@@ -1251,10 +1283,11 @@ static int cromfs_readdir(FAR struct inode *mountpt,
/* Recover our private data from the inode instance */
fs = mountpt->i_private;
cdir = (FAR struct cromfs_dir_s *)dir;
/* Have we reached the end of the directory */
offset = dir->u.cromfs.cr_curroffset;
offset = cdir->cr_curroffset;
if (offset == 0)
{
/* We signal the end of the directory by returning the
@@ -1346,7 +1379,7 @@ static int cromfs_readdir(FAR struct inode *mountpt,
* standard f_pos instead of our own private fb_index.
*/
dir->u.cromfs.cr_curroffset = node->cn_peer;
cdir->cr_curroffset = node->cn_peer;
return OK;
}
@@ -1357,11 +1390,15 @@ static int cromfs_readdir(FAR struct inode *mountpt,
*
****************************************************************************/
static int cromfs_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir)
static int cromfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
FAR struct cromfs_dir_s *cdir;
finfo("mountpt: %p dir: %p\n", mountpt, dir);
dir->u.cromfs.cr_curroffset = dir->u.cromfs.cr_firstoffset;
cdir = (FAR struct cromfs_dir_s *)dir;
cdir->cr_curroffset = cdir->cr_firstoffset;
return OK;
}