You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/10/19 13:14:23 UTC
(nuttx) branch master updated (074b7dd375c -> c7cba680a10)
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
from 074b7dd375c Documentation/platforms: use full board names in Supported Platforms
new 86cd4da877d fs/tmpfs: Fix the indentation that nxstyle reports.
new 0a3d7e56a70 fs/tmpfs: Replace an existing entry in rename().
new d661061c1d0 fs/mnemofs: Return EEXIST from rename() when the new path exists.
new e0a3baf73c3 fs/userfs: Return EEXIST from rename() when the new path exists.
new 13ceae9eae1 fs/vfs: Let the file system replace the target of rename().
new c7cba680a10 fs/vfs: Return EINVAL when rename() moves a directory below itself.
The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
fs/mnemofs/mnemofs.c | 11 +++
fs/tmpfs/fs_tmpfs.c | 187 ++++++++++++++++++++++++++++++++++++++------------
fs/userfs/fs_userfs.c | 10 +++
fs/vfs/fs_rename.c | 113 ++++++++++++++++--------------
include/nuttx/fs/fs.h | 5 ++
5 files changed, 232 insertions(+), 94 deletions(-)
(nuttx) 04/06: fs/userfs: Return EEXIST from rename() when the new path exists.
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit e0a3baf73c347ebe4fe7ff150a0b507bd52f5239
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 00:02:57 2026 +0200
fs/userfs: Return EEXIST from rename() when the new path exists.
The user file system server receives the rename request. The server
can be any program, and it does not have to replace an existing entry
with the new name. The VFS removed such an entry before the rename, so
the servers did not have to do it.
userfs_rename() now asks the server for the status of the new path. If
the path exists, it returns EEXIST. The VFS can then remove the entry
and call rename() again, as before.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/userfs/fs_userfs.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c
index 427e903a893..cc7a83abfe1 100644
--- a/fs/userfs/fs_userfs.c
+++ b/fs/userfs/fs_userfs.c
@@ -2061,6 +2061,7 @@ static int userfs_rename(FAR struct inode *mountpt,
FAR struct userfs_state_s *priv;
FAR struct userfs_rename_request_s *req;
FAR struct userfs_rename_response_s *resp;
+ struct stat buf;
int oldpathlen;
int newpathlen;
ssize_t nsent;
@@ -2071,6 +2072,15 @@ static int userfs_rename(FAR struct inode *mountpt,
mountpt->i_private != NULL);
priv = mountpt->i_private;
+ /* The server may not replace an existing newrelpath. EEXIST makes the
+ * VFS remove it first.
+ */
+
+ if (userfs_stat(mountpt, newrelpath, &buf) >= 0)
+ {
+ return -EEXIST;
+ }
+
/* Check the path lengths */
DEBUGASSERT(oldrelpath != NULL && newrelpath != NULL);
(nuttx) 03/06: fs/mnemofs: Return EEXIST from rename() when the new path exists.
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit d661061c1d05bc7407d0e115ad64a95edceba848
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 00:02:57 2026 +0200
fs/mnemofs: Return EEXIST from rename() when the new path exists.
mfs_dir_rename() adds a directory entry with the new name. It does not
look for an entry that already has this name. The VFS removed such an
entry before the rename, so this was not visible.
mnemofs_rename() now returns EEXIST when the new path exists. The VFS
can then remove the entry and call rename() again.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/mnemofs/mnemofs.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/fs/mnemofs/mnemofs.c b/fs/mnemofs/mnemofs.c
index 136579b06a3..1bcd7219d7c 100644
--- a/fs/mnemofs/mnemofs.c
+++ b/fs/mnemofs/mnemofs.c
@@ -1851,6 +1851,7 @@ static int mnemofs_rename(FAR struct inode *mountpt,
FAR const char *newrelpath)
{
FAR struct mfs_sb_s *sb;
+ struct stat buf;
int ret;
sb = mnemofs_mountpt_to_sb(mountpt);
@@ -1861,6 +1862,16 @@ static int mnemofs_rename(FAR struct inode *mountpt,
return ret;
}
+ /* mfs_dir_rename() cannot replace an existing newrelpath. EEXIST makes
+ * the VFS remove it first.
+ */
+
+ if (mfs_dir_stat(sb, newrelpath, &buf) == OK)
+ {
+ mnemofs_unlock(sb);
+ return -EEXIST;
+ }
+
ret = mfs_dir_rename(sb, oldrelpath, newrelpath);
if (ret < 0)
{
(nuttx) 05/06: fs/vfs: Let the file system replace the target of rename().
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 13ceae9eae182a8f6557c85f684a394a1fed73f4
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 00:02:58 2026 +0200
fs/vfs: Let the file system replace the target of rename().
When the new path existed on a mounted volume, mountptrename() removed
it and then called the rename method of the file system. Between the
two calls, another task could find no file at the new path. POSIX
requires that the new path always refers to the old file or to the new
file.
mountptrename() now calls the rename method first. v9fs, hostfs, NFS,
littlefs, rpmsgfs and tmpfs replace the target in one step. If the
rename method fails, mountptrename() removes the target and calls the
rename method again, as before. FAT, SMARTFS, SPIFFS, mnemofs and
userfs use this second path. If the removal fails, rename() returns
the error of the first rename call.
The pseudo file system path holds the inode tree lock for the whole
operation and does not change.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/vfs/fs_rename.c | 73 +++++++++++++++++++++++++--------------------------
include/nuttx/fs/fs.h | 5 ++++
2 files changed, 41 insertions(+), 37 deletions(-)
diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c
index 7bfb580c2c2..21c594fd4f8 100644
--- a/fs/vfs/fs_rename.c
+++ b/fs/vfs/fs_rename.c
@@ -286,6 +286,7 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
FAR struct inode *newinode;
FAR const char *newrelpath;
FAR char *subdir = NULL;
+ bool newexists = false;
bool newisdir = false;
bool oldisdir = false;
int ret;
@@ -347,14 +348,8 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
/* Does a directory entry already exist at the 'newrelpath'? And is it
* not the same directory entry that we are moving?
*
- * If the directory entry at the newrelpath is a regular file, then that
- * file should be removed first.
- *
- * If the directory entry at the newrelpath is an empty directory, then it
- * can be removed without issue.
- *
- * If the directory entry at the newrelpath is a non-empty directory,
- * then the rename should fail with the error ENOTEMPTY.
+ * The rename replaces a regular file or an empty directory at the
+ * newrelpath. It fails with ENOTEMPTY for a non-empty directory.
*/
#ifdef CONFIG_FS_LINKS
@@ -447,19 +442,6 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
ret = -EINVAL;
goto errout_with_newinode;
}
-
- /* Remove the newrelpath which already exists.
- * rmdir will handle the error cases.
- */
-
- if (oldinode->u.i_mops->rmdir)
- {
- ret = oldinode->u.i_mops->rmdir(oldinode, newrelpath);
- if (ret < 0)
- {
- goto errout_with_newinode;
- }
- }
}
else
{
@@ -472,30 +454,47 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
ret = -ENOTDIR;
goto errout_with_newinode;
}
-
- if (oldinode->u.i_mops->unlink)
- {
- /* Attempt to remove the file before doing the rename.
- *
- * NOTE that errors are not handled here. If we failed
- * to remove the file, then the file system 'rename'
- * method should check that.
- */
-
- oldinode->u.i_mops->unlink(oldinode, newrelpath);
-#ifdef CONFIG_FS_NOTIFY
- notify_unlink(newrelpath);
-#endif
- }
}
+
+ newexists = true;
}
}
/* Perform the rename operation using the relative paths at the common
- * mountpoint.
+ * mountpoint. The file system replaces an existing newrelpath in one
+ * step if it can.
*/
ret = oldinode->u.i_mops->rename(oldinode, oldrelpath, newrelpath);
+ if (ret < 0 && newexists)
+ {
+ int ret2 = ret;
+
+ /* It cannot. Remove newrelpath first, then rename again. If the
+ * removal fails, return the error of the rename.
+ */
+
+ if (newisdir && oldinode->u.i_mops->rmdir)
+ {
+ ret2 = oldinode->u.i_mops->rmdir(oldinode, newrelpath);
+ }
+ else if (!newisdir && oldinode->u.i_mops->unlink)
+ {
+ ret2 = oldinode->u.i_mops->unlink(oldinode, newrelpath);
+#ifdef CONFIG_FS_NOTIFY
+ if (ret2 >= 0)
+ {
+ notify_unlink(newrelpath);
+ }
+#endif
+ }
+
+ if (ret2 >= 0)
+ {
+ ret = oldinode->u.i_mops->rename(oldinode, oldrelpath,
+ newrelpath);
+ }
+ }
#ifdef CONFIG_FS_NOTIFY
if (ret >= 0)
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 0fd791509fe..7a33f4dea2a 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -352,6 +352,11 @@ struct mountpt_operations
CODE int (*mkdir)(FAR struct inode *mountpt, FAR const char *relpath,
mode_t mode);
CODE int (*rmdir)(FAR struct inode *mountpt, FAR const char *relpath);
+
+ /* rename() replaces an existing newrelpath in one step, or fails (for
+ * example with -EEXIST). The VFS then removes newrelpath and retries.
+ */
+
CODE int (*rename)(FAR struct inode *mountpt,
FAR const char *oldrelpath,
FAR const char *newrelpath);
(nuttx) 01/06: fs/tmpfs: Fix the indentation that nxstyle reports.
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 86cd4da877dfd570295cf8f74b2db9c674e2c38d
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Sun Oct 18 23:51:15 2026 +0200
fs/tmpfs: Fix the indentation that nxstyle reports.
nxstyle reports bad alignment and missing blank lines in fs_tmpfs.c.
These errors are older than the next change. This commit changes only
white space.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/tmpfs/fs_tmpfs.c | 50 ++++++++++++++++++++++++++------------------------
1 file changed, 26 insertions(+), 24 deletions(-)
diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 28c76b17d47..8e0244aef0d 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -1115,7 +1115,7 @@ static int tmpfs_find_object(FAR struct tmpfs_s *fs,
{
/* Then we can break out of the loop now */
- break;
+ break;
}
/* No, this was not the final segment of the relpath.
@@ -1301,6 +1301,7 @@ static int tmpfs_getpath(FAR struct tmpfs_object_s *to,
{
int ret = tmpfs_getpath((FAR struct tmpfs_object_s *)to->to_parent,
path, len);
+
if (ret < 0)
{
return ret;
@@ -1509,29 +1510,29 @@ static int tmpfs_foreach(FAR struct tmpfs_directory_s *tdo,
ret = callout(tdo, index, arg);
switch (ret)
{
- case TMPFS_CONTINUE: /* Continue enumeration */
+ case TMPFS_CONTINUE: /* Continue enumeration */
- /* Release the object and index to the next entry */
+ /* Release the object and index to the next entry */
- tmpfs_release_lockedobject(to);
- index++;
- break;
+ tmpfs_release_lockedobject(to);
+ index++;
+ break;
- case TMPFS_HALT: /* Stop enumeration */
+ case TMPFS_HALT: /* Stop enumeration */
- /* Release the object and cancel the traversal */
+ /* Release the object and cancel the traversal */
- tmpfs_release_lockedobject(to);
- return -ECANCELED;
+ tmpfs_release_lockedobject(to);
+ return -ECANCELED;
- case TMPFS_UNLINKED: /* Only the directory entry was deleted */
+ case TMPFS_UNLINKED: /* Only the directory entry was deleted */
- /* Release the object and continue with the same index */
+ /* Release the object and continue with the same index */
- tmpfs_release_lockedobject(to);
+ tmpfs_release_lockedobject(to);
- case TMPFS_DELETED: /* Object and directory entry deleted */
- break; /* Continue with the same index */
+ case TMPFS_DELETED: /* Object and directory entry deleted */
+ break; /* Continue with the same index */
}
}
@@ -1900,21 +1901,21 @@ static off_t tmpfs_seek(FAR struct file *filep, off_t offset, int whence)
switch (whence)
{
case SEEK_SET: /* The offset is set to offset bytes. */
- position = offset;
- break;
+ position = offset;
+ break;
case SEEK_CUR: /* The offset is set to its current location plus
* offset bytes. */
- position = offset + filep->f_pos;
- break;
+ position = offset + filep->f_pos;
+ break;
case SEEK_END: /* The offset is set to the size of the file plus
* offset bytes. */
- position = offset + tfo->tfo_size;
- break;
+ position = offset + tfo->tfo_size;
+ break;
default:
- return -EINVAL;
+ return -EINVAL;
}
/* Save the new file position */
@@ -2026,6 +2027,7 @@ static int tmpfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
if (cmd == FIOC_FILEPATH)
{
FAR char *ptr = (FAR char *)((uintptr_t)arg);
+
ret = inode_getpath(filep->f_inode, ptr, PATH_MAX);
if (ret < 0)
{
@@ -2376,13 +2378,13 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
{
/* A directory */
- entry->d_type = DTYPE_DIRECTORY;
+ entry->d_type = DTYPE_DIRECTORY;
}
else /* to->to_type == TMPFS_REGULAR) */
{
/* A regular file */
- entry->d_type = DTYPE_FILE;
+ entry->d_type = DTYPE_FILE;
}
/* Copy the entry name */
(nuttx) 06/06: fs/vfs: Return EINVAL when rename() moves a directory below itself.
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit c7cba680a10a735317bb2cddb6a12914a55e8d58
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 00:03:36 2026 +0200
fs/vfs: Return EINVAL when rename() moves a directory below itself.
rename() must fail with EINVAL when the new path is below the old
directory. mountptrename() checked this only when the new path
existed. When it did not exist, the file system got the request, and
tmpfs moved the directory into itself. pseudorename() did not check it
at all and moved a pseudo directory into itself. In both cases
rename() returned 0, and the directory could no longer be found.
mountptrename() now does this check for every directory, before it
looks for the new path. pseudorename() does it before it changes the
inode tree.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/vfs/fs_rename.c | 40 ++++++++++++++++++++++++++--------------
1 file changed, 26 insertions(+), 14 deletions(-)
diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c
index 21c594fd4f8..05a3b689b7d 100644
--- a/fs/vfs/fs_rename.c
+++ b/fs/vfs/fs_rename.c
@@ -76,6 +76,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode,
#ifdef CONFIG_FS_NOTIFY
bool isdir = INODE_IS_PSEUDODIR(oldinode);
#endif
+ size_t oldlen;
int ret;
/* Hold the inode tree lock across resolve / permission checks / mutate
@@ -102,6 +103,15 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode,
goto errout_with_newsearch;
}
+ /* It is an error to rename an inode to a path below itself */
+
+ oldlen = strlen(oldpath);
+ if (strncmp(newpath, oldpath, oldlen) == 0 && newpath[oldlen] == '/')
+ {
+ ret = -EINVAL;
+ goto errout_with_newsearch;
+ }
+
/* According to POSIX, any new inode at this path should be removed
* first, provided that it is not a directory.
*/
@@ -381,6 +391,22 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
oldisdir = S_ISDIR(oldbuf.st_mode);
+ /* It is an error to rename a directory into one of its own
+ * subdirectories (new is below old), whether new exists or not.
+ */
+
+ if (oldisdir)
+ {
+ size_t oldlen = strlen(oldrelpath);
+
+ if (strncmp(newrelpath, oldrelpath, oldlen) == 0 &&
+ newrelpath[oldlen] == '/')
+ {
+ ret = -EINVAL;
+ goto errout_with_newinode;
+ }
+ }
+
#ifdef CONFIG_FS_LINKS
/* Use lstat if available to avoid dereferencing symlinks */
@@ -421,8 +447,6 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
if (newisdir)
{
- size_t oldlen;
-
/* It is an error to rename a file to a directory */
if (!oldisdir)
@@ -430,18 +454,6 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
ret = -EISDIR;
goto errout_with_newinode;
}
-
- /* It is an error to rename a directory into one of its
- * own subdirectories (new is below old).
- */
-
- oldlen = strlen(oldrelpath);
- if (strncmp(newrelpath, oldrelpath, oldlen) == 0 &&
- newrelpath[oldlen] == '/')
- {
- ret = -EINVAL;
- goto errout_with_newinode;
- }
}
else
{
(nuttx) 02/06: fs/tmpfs: Replace an existing entry in rename().
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0a3d7e56a7036f3f1b2448e7eab43f43864a3d1b
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 00:02:57 2026 +0200
fs/tmpfs: Replace an existing entry in rename().
tmpfs_rename() returned EEXIST when an entry with the new name existed.
The VFS then had to remove that entry before the rename, so another
task could find no entry at the new path between the two operations.
tmpfs_rename() now replaces the entry in one step, under the file system
lock. The entry refers to the renamed object, then the old entry is
removed. An open file that is replaced stays until its last close. A
directory replaces only an empty directory that is not open; otherwise
the rename fails with ENOTEMPTY or EBUSY. A file cannot replace a
directory (EISDIR), and a directory cannot replace a file (ENOTDIR).
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
fs/tmpfs/fs_tmpfs.c | 137 ++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 118 insertions(+), 19 deletions(-)
diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 8e0244aef0d..5df8d369b7d 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -105,6 +105,8 @@ static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,
FAR const char *name);
static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
FAR struct tmpfs_object_s *to, FAR const char *name);
+static int tmpfs_replace_dirent(FAR struct tmpfs_directory_s *tdo,
+ int index, FAR struct tmpfs_object_s *to);
static FAR struct tmpfs_file_s *
tmpfs_alloc_file(FAR struct tmpfs_directory_s *parent, mode_t mode);
static int tmpfs_create_file(FAR struct tmpfs_s *fs,
@@ -572,6 +574,96 @@ static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
return OK;
}
+/****************************************************************************
+ * Name: tmpfs_replace_dirent
+ *
+ * Description:
+ * Make the entry at 'index' refer to 'to' and free the object that it
+ * referred to. An open file stays until its last close. Return 1 if
+ * the entry already refers to 'to'.
+ *
+ ****************************************************************************/
+
+static int tmpfs_replace_dirent(FAR struct tmpfs_directory_s *tdo,
+ int index, FAR struct tmpfs_object_s *to)
+{
+ FAR struct tmpfs_object_s *old = tdo->tdo_entry[index].tde_object;
+ int ret;
+
+ if (old == to)
+ {
+ return 1;
+ }
+
+ ret = tmpfs_lock_object(old);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (old->to_type == TMPFS_DIRECTORY)
+ {
+ FAR struct tmpfs_directory_s *olddir =
+ (FAR struct tmpfs_directory_s *)old;
+
+ if (to->to_type != TMPFS_DIRECTORY)
+ {
+ ret = -EISDIR;
+ }
+ else if (olddir->tdo_nentries > 0)
+ {
+ ret = -ENOTEMPTY;
+ }
+ else if (olddir->tdo_refs > 0)
+ {
+ ret = -EBUSY;
+ }
+ }
+ else if (to->to_type == TMPFS_DIRECTORY)
+ {
+ ret = -ENOTDIR;
+ }
+
+ if (ret < 0)
+ {
+ tmpfs_unlock_object(old);
+ return ret;
+ }
+
+ tdo->tdo_entry[index].tde_object = to;
+ to->to_parent = tdo;
+
+ if (old->to_type == TMPFS_DIRECTORY)
+ {
+ FAR struct tmpfs_directory_s *olddir =
+ (FAR struct tmpfs_directory_s *)old;
+
+ tmpfs_unlock_directory(olddir);
+ nxrmutex_destroy(&olddir->tdo_lock);
+ fs_heap_free(olddir->tdo_entry);
+ fs_heap_free(olddir);
+ }
+ else
+ {
+ FAR struct tmpfs_file_s *oldfile = (FAR struct tmpfs_file_s *)old;
+
+ if (oldfile->tfo_refs > 0)
+ {
+ oldfile->tfo_flags |= TFO_FLAG_UNLINKED;
+ tmpfs_unlock_file(oldfile);
+ }
+ else
+ {
+ tmpfs_unlock_file(oldfile);
+ nxrmutex_destroy(&oldfile->tfo_lock);
+ fs_heap_free(oldfile->tfo_data);
+ fs_heap_free(oldfile);
+ }
+ }
+
+ return OK;
+}
+
#ifdef CONFIG_FS_PERMISSION
/****************************************************************************
@@ -2848,6 +2940,7 @@ static int tmpfs_rename(FAR struct inode *mountpt,
FAR struct tmpfs_s *fs;
FAR const char *oldname;
FAR const char *newname;
+ int index;
int ret;
finfo("mountpt: %p oldrelpath: %s newrelpath: %s\n",
@@ -2908,25 +3001,6 @@ static int tmpfs_rename(FAR struct inode *mountpt,
newname++;
}
- /* Verify that no object of this name already exists in the destination
- * directory.
- */
-
- ret = tmpfs_find_dirent(newparent, newname, strlen(newname));
- if (ret != -ENOENT)
- {
- /* Something with this name already exists in the directory.
- * OR perhaps some fatal error occurred.
- */
-
- if (ret >= 0)
- {
- ret = -EEXIST;
- }
-
- goto errout_with_newparent;
- }
-
/* Find the old object at oldpath. If successful, tmpfs_find_object()
* will lock both the object and the parent directory and will increment
* the reference count on both.
@@ -2962,6 +3036,31 @@ static int tmpfs_rename(FAR struct inode *mountpt,
oldname = oldrelpath;
}
+ /* Is there an object of this name in the destination directory? */
+
+ index = tmpfs_find_dirent(newparent, newname, strlen(newname));
+ if (index >= 0)
+ {
+ /* Yes.. replace it in one step, then remove the old entry */
+
+ ret = tmpfs_replace_dirent(newparent, index, to);
+ if (ret > 0)
+ {
+ ret = OK;
+ }
+ else if (ret == OK)
+ {
+ ret = tmpfs_remove_dirent(oldparent, oldname);
+ }
+
+ goto errout_with_oldparent;
+ }
+ else if (index != -ENOENT)
+ {
+ ret = index;
+ goto errout_with_oldparent;
+ }
+
/* Remove the entry from the parent directory */
ret = tmpfs_remove_dirent(oldparent, oldname);