mirror of
https://github.com/apache/nuttx.git
synced 2026-05-28 11:56:10 +08:00
fs/tmpfs: Iterate the entry reversely in readdir
to avoid readdir return the wrong entry in the following code:
void rmdir_recursive(FAR const char *path)
{
FAR DIR *dir = opendir(path);
while (1)
{
char fullpath[MAX_PATH];
FAR dirent *ent = readdir(dir);
if (ent == NULL)
{
break;
}
sprintf(fullpath, "%s/%s", path, ent->d_name);
if (DIRENT_ISDIRECTORY(ent->d_type))
{
rmdir_recursive(fullpath);
}
else
{
unlink(fullpath);
}
}
}
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Ide60fe8db6aada88ad3d8e45367f11599a6f33b1
This commit is contained in:
committed by
Alan Carvalho de Assis
parent
4bbc17454c
commit
b33d967548
+13
-6
@@ -1962,7 +1962,7 @@ static int tmpfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
|||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
dir->u.tmpfs.tf_tdo = tdo;
|
dir->u.tmpfs.tf_tdo = tdo;
|
||||||
dir->u.tmpfs.tf_index = 0;
|
dir->u.tmpfs.tf_index = tdo->tdo_nentries;
|
||||||
|
|
||||||
tmpfs_unlock_directory(tdo);
|
tmpfs_unlock_directory(tdo);
|
||||||
}
|
}
|
||||||
@@ -2022,7 +2022,7 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
|
|||||||
/* Have we reached the end of the directory? */
|
/* Have we reached the end of the directory? */
|
||||||
|
|
||||||
index = dir->u.tmpfs.tf_index;
|
index = dir->u.tmpfs.tf_index;
|
||||||
if (index >= tdo->tdo_nentries)
|
if (index-- == 0)
|
||||||
{
|
{
|
||||||
/* We signal the end of the directory by returning the special error:
|
/* We signal the end of the directory by returning the special error:
|
||||||
* -ENOENT
|
* -ENOENT
|
||||||
@@ -2059,9 +2059,9 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
|
|||||||
|
|
||||||
strncpy(dir->fd_dir.d_name, tde->tde_name, NAME_MAX + 1);
|
strncpy(dir->fd_dir.d_name, tde->tde_name, NAME_MAX + 1);
|
||||||
|
|
||||||
/* Increment the index for next time */
|
/* Save the index for next time */
|
||||||
|
|
||||||
dir->u.tmpfs.tf_index = index + 1;
|
dir->u.tmpfs.tf_index = index;
|
||||||
ret = OK;
|
ret = OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2076,12 +2076,19 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
|
|||||||
static int tmpfs_rewinddir(FAR struct inode *mountpt,
|
static int tmpfs_rewinddir(FAR struct inode *mountpt,
|
||||||
FAR struct fs_dirent_s *dir)
|
FAR struct fs_dirent_s *dir)
|
||||||
{
|
{
|
||||||
|
FAR struct tmpfs_directory_s *tdo;
|
||||||
|
|
||||||
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
||||||
DEBUGASSERT(mountpt != NULL && dir != NULL);
|
DEBUGASSERT(mountpt != NULL && dir != NULL);
|
||||||
|
|
||||||
/* Set the readdir index to zero */
|
/* Get the directory structure from the dir argument and lock it */
|
||||||
|
|
||||||
dir->u.tmpfs.tf_index = 0;
|
tdo = dir->u.tmpfs.tf_tdo;
|
||||||
|
DEBUGASSERT(tdo != NULL);
|
||||||
|
|
||||||
|
/* Set the readdir index pass the end */
|
||||||
|
|
||||||
|
dir->u.tmpfs.tf_index = tdo->tdo_nentries;
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user