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

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

commit 69f3774f30c01efb439df9413b0640df11d840a6
Author: guohao15 <guoha...@xiaomi.com>
AuthorDate: Tue Aug 6 21:31:09 2024 +0800

    littlefs:remove the '/' in the end of relpath in mkdir
    
    mkdir /data/log   success
    mkdir /data/log/  failed  in littlefs (but fatfs/yaffs/tmpfs success)
    
    Signed-off-by: guohao15 <guoha...@xiaomi.com>
---
 fs/littlefs/lfs_vfs.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c
index 942fa5ff89..249b945e7a 100644
--- a/fs/littlefs/lfs_vfs.c
+++ b/fs/littlefs/lfs_vfs.c
@@ -1491,8 +1491,29 @@ static int littlefs_mkdir(FAR struct inode *mountpt, FAR 
const char *relpath,
                           mode_t mode)
 {
   FAR struct littlefs_mountpt_s *fs;
+  FAR char *path = (FAR char *)relpath;
+  size_t len = strlen(relpath);
   int ret;
 
+  /* We need remove all the '/' in the end of relpath */
+
+  if (len > 0 && relpath[len - 1] == '/')
+    {
+      path = lib_get_pathbuffer();
+      if (path == NULL)
+        {
+          return -ENOMEM;
+        }
+
+      while (len > 0 && relpath[len - 1] == '/')
+        {
+          len--;
+        }
+
+      memcpy(path, relpath, len);
+      path[len] = '\0';
+    }
+
   /* Get the mountpoint private data from the inode structure */
 
   fs = mountpt->i_private;
@@ -1502,10 +1523,10 @@ static int littlefs_mkdir(FAR struct inode *mountpt, 
FAR const char *relpath,
   ret = nxmutex_lock(&fs->lock);
   if (ret < 0)
     {
-      return ret;
+      goto errout;
     }
 
-  ret = lfs_mkdir(&fs->lfs, relpath);
+  ret = littlefs_convert_result(lfs_mkdir(&fs->lfs, path));
   if (ret >= 0)
     {
       struct littlefs_attr_s attr;
@@ -1517,16 +1538,22 @@ static int littlefs_mkdir(FAR struct inode *mountpt, 
FAR const char *relpath,
       attr.at_ctim = 1000000000ull * time.tv_sec + time.tv_nsec;
       attr.at_atim = attr.at_ctim;
       attr.at_mtim = attr.at_ctim;
-      ret = littlefs_convert_result(lfs_setattr(&fs->lfs, relpath, 0,
+      ret = littlefs_convert_result(lfs_setattr(&fs->lfs, path, 0,
                                                 &attr, sizeof(attr)));
       if (ret < 0)
         {
-          lfs_remove(&fs->lfs, relpath);
+          lfs_remove(&fs->lfs, path);
         }
     }
 
   nxmutex_unlock(&fs->lock);
 
+errout:
+  if (path != relpath)
+    {
+      lib_put_pathbuffer(path);
+    }
+
   return ret;
 }
 

Reply via email to