summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxqyjlj <[email protected]>2023-02-01 11:00:03 +0800
committerguo <[email protected]>2023-02-07 11:31:15 +0800
commitda8f5c08c29f477fd4eeb6617da64979e43d8bc3 (patch)
tree35ea0f2e645b3ad248bba51d0737899cb6e688cb
parente010d844af5d8a0207f8a4a4414b35ba95a99bca (diff)
🐞 fix(components/dfs/filesystems/tmpfs/dfs_tmpfs.c): cannot open the same file repeatedly in 'w' mode
-rw-r--r--components/dfs/filesystems/tmpfs/dfs_tmpfs.c65
1 files changed, 31 insertions, 34 deletions
diff --git a/components/dfs/filesystems/tmpfs/dfs_tmpfs.c b/components/dfs/filesystems/tmpfs/dfs_tmpfs.c
index 7ed8e8f888..b52b27cb73 100644
--- a/components/dfs/filesystems/tmpfs/dfs_tmpfs.c
+++ b/components/dfs/filesystems/tmpfs/dfs_tmpfs.c
@@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2022-10-24 flybreak the first version
+ * 2023-02-01 xqyjlj fix cannot open the same file repeatedly in 'w' mode
*/
#include <rthw.h>
@@ -382,45 +383,41 @@ int dfs_tmpfs_open(struct dfs_fd *file)
/* Creates a new file. */
if (file->flags & O_CREAT)
{
- if (d_file != NULL)
- return -EEXIST;
+ if (d_file == NULL) {
+ /* find parent file */
+ _path_separate(file->vnode->path, parent_path, file_name);
+ if (file_name[0] == '\0') /* it's root dir */
+ return -ENOENT;
- /* find parent file */
- _path_separate(file->vnode->path, parent_path, file_name);
- if (file_name[0] == '\0') /* it's root dir */
- return -ENOENT;
-
- /* open parent directory */
- p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
- if (p_file == NULL)
- return -ENOENT;
+ /* open parent directory */
+ p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
+ if (p_file == NULL)
+ return -ENOENT;
- /* create a file entry */
- d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
- if (d_file == NULL)
- {
- return -ENOMEM;
- }
- superblock->df_size += sizeof(struct tmpfs_file);
+ /* create a file entry */
+ d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
+ if (d_file == NULL) {
+ return -ENOMEM;
+ }
+ superblock->df_size += sizeof(struct tmpfs_file);
- strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
+ strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
- rt_list_init(&(d_file->subdirs));
- rt_list_init(&(d_file->sibling));
- d_file->data = NULL;
- d_file->size = 0;
- d_file->sb = superblock;
- if (file->flags & O_DIRECTORY)
- {
- d_file->type = TMPFS_TYPE_DIR;
- }
- else
- {
- d_file->type = TMPFS_TYPE_FILE;
+ rt_list_init(&(d_file->subdirs));
+ rt_list_init(&(d_file->sibling));
+ d_file->data = NULL;
+ d_file->size = 0;
+ d_file->sb = superblock;
+ if (file->flags & O_DIRECTORY) {
+ d_file->type = TMPFS_TYPE_DIR;
+ }
+ else {
+ d_file->type = TMPFS_TYPE_FILE;
+ }
+ rt_hw_spin_lock(&lock);
+ rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
+ rt_hw_spin_unlock(&lock);
}
- rt_hw_spin_lock(&lock);
- rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
- rt_hw_spin_unlock(&lock);
}
/* Creates a new file.
* If the file is existing, it is truncated and overwritten.