This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 969a06331f fs/vfs: fix case when file to rename does not exist
969a06331f is described below

commit 969a06331fd0900b4ba349a5bef841e243183562
Author: Petro Karashchenko <petro.karashche...@gmail.com>
AuthorDate: Fri Oct 14 16:44:26 2022 +0200

    fs/vfs: fix case when file to rename does not exist
    
    Signed-off-by: Petro Karashchenko <petro.karashche...@gmail.com>
---
 fs/vfs/fs_rename.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c
index 22577ed7c9..df40cc6856 100644
--- a/fs/vfs/fs_rename.c
+++ b/fs/vfs/fs_rename.c
@@ -397,17 +397,30 @@ next_subdir:
 
                   goto next_subdir;
                 }
-              else if (oldinode->u.i_mops->unlink)
+              else
                 {
-                  /* No.. newrelpath must refer to a regular file.  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.
+                  /* No.. newrelpath must refer to a regular file.  Make sure
+                   * that the file at the olrelpath actually exists before
+                   * performing any further actions with newrelpath
                    */
 
-                   oldinode->u.i_mops->unlink(oldinode, newrelpath);
+                  ret = oldinode->u.i_mops->stat(oldinode, oldrelpath, &buf);
+                  if (ret < 0)
+                    {
+                      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);
+                    }
                 }
             }
         }

Reply via email to