diff --git a/components/fs/vfs/vfs_config.h b/components/fs/vfs/vfs_config.h index 54fd2ca2..93cc10a3 100644 --- a/components/fs/vfs/vfs_config.h +++ b/components/fs/vfs/vfs_config.h @@ -88,4 +88,9 @@ #define MAX_DIRENT_NUM 14 // 14 means 4096 length buffer can store 14 dirent, see struct DIR +/* max number of open directories */ +#ifndef LOSCFG_MAX_OPEN_DIRS +#define LOSCFG_MAX_OPEN_DIRS 10 +#endif + #endif diff --git a/components/fs/vfs/vfs_fs.c b/components/fs/vfs/vfs_fs.c index 855de691..840b53c2 100644 --- a/components/fs/vfs/vfs_fs.c +++ b/components/fs/vfs/vfs_fs.c @@ -94,6 +94,7 @@ int PollQueryFd(int fd, struct PollTable *table) #define IOV_MAX_CNT 4 UINT32 g_fsMutex; +static UINT32 g_dirNum = 0; int VfsLock(void) { @@ -666,6 +667,13 @@ static DIR *VfsOpendir(const char *path) return NULL; } + if (g_dirNum >= LOSCFG_MAX_OPEN_DIRS) { + VFS_ERRNO_SET(ENFILE); + VfsUnlock(); + free(dir); + return NULL; + } + mp = VfsMpFind(path, &pathInMp); if ((mp == NULL) || (pathInMp == NULL)) { VFS_ERRNO_SET(ENOENT); @@ -687,6 +695,7 @@ static DIR *VfsOpendir(const char *path) ret = (UINT32)mp->mFs->fsFops->opendir(dir, pathInMp); if (ret == 0) { mp->mRefs++; + g_dirNum++; } else { LOSCFG_FS_FREE_HOOK(dir); dir = NULL; @@ -752,6 +761,7 @@ static int VfsClosedir(DIR *d) if (ret == 0) { mp->mRefs--; + g_dirNum--; } else { VFS_ERRNO_SET(EBADF); }