From 6f8a035d2091c6f9801cb0773d5e1d82544f5a01 Mon Sep 17 00:00:00 2001 From: angryproton Date: Sun, 12 Jul 2026 19:59:23 +0800 Subject: [PATCH] =?UTF-8?q?[fix][dfs]=20=E5=A2=9E=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F=E6=93=8D=E4=BD=9C=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=A3=80=E6=9F=A5=EF=BC=8C=E9=98=B2=E6=AD=A2=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=B8=8D=E5=8C=B9=E9=85=8D=E5=AF=BC=E8=87=B4=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E4=B8=8D=E4=B8=80=E8=87=B4=E5=BC=95?= =?UTF-8?q?=E5=8F=91=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dfs/dfs_v1/filesystems/elmfat/dfs_elm.c | 16 ++++++++++++++ .../dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c | 10 +++++++++ components/dfs/dfs_v1/src/dfs_file.c | 14 +++++++++++-- .../dfs/dfs_v2/filesystems/elmfat/dfs_elm.c | 16 ++++++++++++++ .../dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c | 21 +++++++++++++++++++ components/dfs/dfs_v2/src/dfs_file.c | 5 +++++ 6 files changed, 80 insertions(+), 2 deletions(-) diff --git a/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c b/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c index afb2a0d46d9..c808f62329e 100644 --- a/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c @@ -505,6 +505,12 @@ int dfs_elm_ioctl(struct dfs_file *file, int cmd, void *args) FIL *fd; FSIZE_t fptr, length; FRESULT result = FR_OK; + + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); @@ -584,6 +590,11 @@ int dfs_elm_flush(struct dfs_file *file) FIL *fd; FRESULT result; + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); @@ -638,6 +649,11 @@ int dfs_elm_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count) rt_uint32_t index; struct dirent *d; + if (file->vnode->type != FT_DIRECTORY) + { + return -ENOTDIR; + } + dir = (DIR *)(file->data); RT_ASSERT(dir != RT_NULL); diff --git a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c index cb971ef30c3..921bb35a768 100644 --- a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c +++ b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c @@ -336,6 +336,11 @@ ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count) rt_size_t length; struct tmpfs_file *d_file; + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + d_file = (struct tmpfs_file *)file->vnode->data; RT_ASSERT(d_file != NULL); @@ -359,6 +364,11 @@ ssize_t dfs_tmpfs_write(struct dfs_file *fd, const void *buf, size_t count) struct tmpfs_file *d_file; struct tmpfs_sb *superblock; + if (fd->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + d_file = (struct tmpfs_file *)fd->vnode->data; RT_ASSERT(d_file != NULL); diff --git a/components/dfs/dfs_v1/src/dfs_file.c b/components/dfs/dfs_v1/src/dfs_file.c index f1c4e7ee4d2..d66bae2f38d 100644 --- a/components/dfs/dfs_v1/src/dfs_file.c +++ b/components/dfs/dfs_v1/src/dfs_file.c @@ -432,6 +432,11 @@ ssize_t dfs_file_read(struct dfs_file *fd, void *buf, size_t len) return -EINVAL; } + if (fd->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + if (fd->vnode->fops->read == NULL) { return -ENOSYS; @@ -544,6 +549,11 @@ ssize_t dfs_file_write(struct dfs_file *fd, const void *buf, size_t len) return -EINVAL; } + if (fd->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + if (fd->vnode->fops->write == NULL) { return -ENOSYS; @@ -738,8 +748,8 @@ int dfs_file_ftruncate(struct dfs_file *fd, off_t length) { int result; - /* fd is null or not a regular file system fd, or length is invalid */ - if (fd == NULL || fd->vnode->type != FT_REGULAR || length < 0) + /* fd is null or a directory , or length is invalid */ + if (fd == NULL || fd->vnode->type == FT_DIRECTORY || length < 0) return -EINVAL; if (fd->vnode->fops->ioctl == NULL) diff --git a/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c b/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c index cb1479f6ca9..bd6d4c4b48b 100644 --- a/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c @@ -619,6 +619,11 @@ int dfs_elm_flush(struct dfs_file *file) FIL *fd; FRESULT result; + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); @@ -689,6 +694,12 @@ static int dfs_elm_truncate(struct dfs_file *file, off_t offset) FIL *fd; FSIZE_t fptr; FRESULT result = FR_OK; + + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + fd = (FIL *)(file->vnode->data); RT_ASSERT(fd != RT_NULL); @@ -716,6 +727,11 @@ int dfs_elm_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count) rt_uint32_t index; struct dirent *d; + if (file->vnode->type != FT_DIRECTORY) + { + return -ENOTDIR; + } + dir = (DIR *)(file->vnode->data); RT_ASSERT(dir != RT_NULL); diff --git a/components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c b/components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c index 1dc1fd54f9e..da27b187f8c 100644 --- a/components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c +++ b/components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c @@ -290,6 +290,12 @@ static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, of { ssize_t length; struct tmpfs_file *d_file; + + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + d_file = (struct tmpfs_file *)file->vnode->data; RT_ASSERT(d_file != NULL); @@ -352,6 +358,11 @@ static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t co { struct tmpfs_file *d_file; + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + d_file = (struct tmpfs_file *)file->vnode->data; RT_ASSERT(d_file != NULL); @@ -500,6 +511,11 @@ static int dfs_tmpfs_getdents(struct dfs_file *file, struct tmpfs_file *d_file, *n_file, *tmp; struct tmpfs_sb *superblock; + if (file->vnode->type != FT_DIRECTORY) + { + return -ENOTDIR; + } + d_file = (struct tmpfs_file *)file->vnode->data; rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER); @@ -828,6 +844,11 @@ static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset) struct tmpfs_sb *superblock = RT_NULL; rt_uint8_t *ptr = RT_NULL; + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + d_file = (struct tmpfs_file *)file->vnode->data; RT_ASSERT(d_file != RT_NULL); diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index 9c6e383e1b4..5ce9c4f5759 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -2214,6 +2214,11 @@ int dfs_file_ftruncate(struct dfs_file *file, off_t length) if (file) { + if (file->vnode->type == FT_DIRECTORY) + { + return -EISDIR; + } + if (file->fops->truncate) { if (dfs_is_mounted(file->vnode->mnt) == 0)