Skip to content

Commit

Permalink
littlefs:remove the '/' in the end of relpath in mkdir
Browse files Browse the repository at this point in the history
mkdir /data/log   success
mkdir /data/log/  failed  in littlefs (but fatfs/yaffs/tmpfs success)

Signed-off-by: guohao15 <[email protected]>
  • Loading branch information
guohao15 authored and GUIDINGLI committed Oct 11, 2024
1 parent da5839c commit 69f3774
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions fs/littlefs/lfs_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 69f3774

Please sign in to comment.