From 0d30be405cb06387a04e0c211e033007149d760c Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 1/7] Revert "littlefs:remove the '/' in the end of relpath in mkdir" This reverts commit 69f3774f30c01efb439df9413b0640df11d840a6. --- fs/littlefs/lfs_vfs.c | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 249b945e7a90e..942fa5ff899ea 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -1491,29 +1491,8 @@ 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; @@ -1523,10 +1502,10 @@ static int littlefs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = nxmutex_lock(&fs->lock); if (ret < 0) { - goto errout; + return ret; } - ret = littlefs_convert_result(lfs_mkdir(&fs->lfs, path)); + ret = lfs_mkdir(&fs->lfs, relpath); if (ret >= 0) { struct littlefs_attr_s attr; @@ -1538,22 +1517,16 @@ 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, path, 0, + ret = littlefs_convert_result(lfs_setattr(&fs->lfs, relpath, 0, &attr, sizeof(attr))); if (ret < 0) { - lfs_remove(&fs->lfs, path); + lfs_remove(&fs->lfs, relpath); } } nxmutex_unlock(&fs->lock); -errout: - if (path != relpath) - { - lib_put_pathbuffer(path); - } - return ret; } From 8f9efd50b4bf424a7d7e101d399d7d93bc77f0dc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 2/7] Revert "littlefs: fix warnings" This reverts commit da5839c6f2b3ce0c05dd92fb3050fc7906343a10. --- fs/littlefs/lfs_vfs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 942fa5ff899ea..5db1ae89d3907 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -727,6 +727,7 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) FAR struct littlefs_file_s *priv; FAR struct inode *inode; struct littlefs_attr_s attr; + char path[LFS_NAME_MAX]; int ret; memset(buf, 0, sizeof(*buf)); @@ -791,6 +792,7 @@ static int littlefs_fchstat(FAR const struct file *filep, FAR struct littlefs_file_s *priv; FAR struct inode *inode; struct littlefs_attr_s attr; + char path[LFS_NAME_MAX]; int ret; /* Recover our private data from the struct file instance */ From 72ed32b6e8e2b49bdf7c5cc8410dd1f4749c933b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 3/7] Revert "fs/littlefs: revert fstat and use lfs_file_attr function" This reverts commit 8f6ea8896f4cf09f704576b067bc29593e8c5bf4. --- fs/littlefs/CMakeLists.txt | 6 +-- fs/littlefs/Make.defs | 1 - fs/littlefs/lfs_getsetattr.patch | 80 --------------------------- fs/littlefs/lfs_vfs.c | 93 +++++--------------------------- 4 files changed, 16 insertions(+), 164 deletions(-) delete mode 100644 fs/littlefs/lfs_getsetattr.patch diff --git a/fs/littlefs/CMakeLists.txt b/fs/littlefs/CMakeLists.txt index cfae1838ebeb9..8c487497dda14 100644 --- a/fs/littlefs/CMakeLists.txt +++ b/fs/littlefs/CMakeLists.txt @@ -31,10 +31,8 @@ if(CONFIG_FS_LITTLEFS) ${CMAKE_BINARY_DIR}/fs/littlefs/littlefs PATCH_COMMAND patch -p2 -d ${CMAKE_CURRENT_LIST_DIR} < - ${CMAKE_CURRENT_LIST_DIR}/lfs_util.patch && patch -p2 -d - ${CMAKE_CURRENT_LIST_DIR} < ${CMAKE_CURRENT_LIST_DIR}/lfs_getpath.patch - && patch -p2 -d ${CMAKE_CURRENT_LIST_DIR} < - ${CMAKE_CURRENT_LIST_DIR}/lfs_getsetattr.patch) + ${CMAKE_CURRENT_LIST_DIR}/lfs_util.patch COMMAND patch -p2 -d + ${CMAKE_CURRENT_LIST_DIR} < ${CMAKE_CURRENT_LIST_DIR}/lfs_getpath.patch) FetchContent_MakeAvailable(littlefs) endif() diff --git a/fs/littlefs/Make.defs b/fs/littlefs/Make.defs index 992574b1302ed..4a174ca99c8e7 100644 --- a/fs/littlefs/Make.defs +++ b/fs/littlefs/Make.defs @@ -56,7 +56,6 @@ $(LITTLEFS_TARBALL): $(Q) mv littlefs/littlefs-$(LITTLEFS_VERSION) littlefs/littlefs $(Q) git apply littlefs/lfs_util.patch $(Q) git apply littlefs/lfs_getpath.patch - $(Q) git apply littlefs/lfs_getsetattr.patch $(Q) touch littlefs/.littlefsunpack # Download and unpack tarball if no git repo found diff --git a/fs/littlefs/lfs_getsetattr.patch b/fs/littlefs/lfs_getsetattr.patch deleted file mode 100644 index 60d77412bea10..0000000000000 --- a/fs/littlefs/lfs_getsetattr.patch +++ /dev/null @@ -1,80 +0,0 @@ ---- ./littlefs/littlefs/lfs.c -+++ ./littlefs/littlefs/lfs.c -@@ -5717,6 +5717,41 @@ int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size) { - return err < 0 ? err : 0; - } - -+lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file, -+ uint8_t type, void *buffer, lfs_size_t size) -+{ -+ int err = LFS_LOCK(lfs->cfg); -+ if (err) { -+ return err; -+ } -+ LFS_TRACE("lfs_file_setattr(%p, %p)", (void*)lfs, (void*)file); -+ LFS_TRACE("lfs_file_setattr(%"PRIu8", %p, %"PRIu32")", -+ type, buffer, size); -+ LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); -+ -+ return lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x7ff, 0x3ff, 0), -+ LFS_MKTAG(LFS_TYPE_USERATTR + type, -+ file->id, lfs_min(size, lfs->attr_max)), buffer); -+} -+ -+#ifndef LFS_READONLY -+int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file, -+ uint8_t type, const void *buffer, lfs_size_t size) -+{ -+ int err = LFS_LOCK(lfs->cfg); -+ if (err) { -+ return err; -+ } -+ LFS_TRACE("lfs_file_getattr(%p, %p)", (void*)lfs, (void*)file); -+ LFS_TRACE("lfs_file_getattr(%"PRIu8", %p, %"PRIu32")", -+ type, buffer, size); -+ LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); -+ -+ return lfs_dir_commit(lfs, &file->m, LFS_MKATTRS( -+ {LFS_MKTAG(LFS_TYPE_USERATTR + type, file->id, size), buffer})); -+} -+#endif -+ - #ifndef LFS_READONLY - int lfs_mkdir(lfs_t *lfs, const char *path) { - int err = LFS_LOCK(lfs->cfg); ---- ./littlefs/littlefs/lfs.h -+++ ./littlefs/littlefs/lfs.h -@@ -611,6 +611,33 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); - // Returns a negative error code on failure. - int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size); - -+// Get a custom attribute of file -+// -+// Custom attributes are uniquely identified by an 8-bit type and limited -+// to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller than -+// the buffer, it will be padded with zeros. If the stored attribute is larger, -+// then it will be silently truncated. If no attribute is found, the error -+// LFS_ERR_NOATTR is returned and the buffer is filled with zeros. -+// -+// Returns the size of the attribute, or a negative error code on failure. -+// Note, the returned size is the size of the attribute on disk, irrespective -+// of the size of the buffer. This can be used to dynamically allocate a buffer -+// or check for existance. -+lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file, -+ uint8_t type, void *buffer, lfs_size_t size); -+ -+// Set custom attributes of file -+// -+// Custom attributes are uniquely identified by an 8-bit type and limited -+// to LFS_ATTR_MAX bytes. If an attribute is not found, it will be -+// implicitly created. -+// -+// Returns a negative error code on failure. -+#ifndef LFS_READONLY -+int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file, -+ uint8_t type, const void *buffer, lfs_size_t size); -+#endif -+ - /// Directory operations /// - - #ifndef LFS_READONLY diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 5db1ae89d3907..46c494ef49614 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -726,7 +726,6 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) FAR struct littlefs_mountpt_s *fs; FAR struct littlefs_file_s *priv; FAR struct inode *inode; - struct littlefs_attr_s attr; char path[LFS_NAME_MAX]; int ret; @@ -746,43 +745,20 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) return ret; } - buf->st_size = lfs_file_size(&fs->lfs, &priv->file); - if (buf->st_size < 0) + ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path)); + nxmutex_unlock(&fs->lock); + if (ret < 0) { - ret = littlefs_convert_result(buf->st_size); - goto errout; + return ret; } - ret = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file, 0, - &attr, sizeof(attr))); + ret = littlefs_stat(inode, path, buf); if (ret < 0) { - if (ret != -ENODATA) - { - goto errout; - } - - memset(&attr, 0, sizeof(attr)); - attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO; + return ret; } - ret = 0; - buf->st_mode = attr.at_mode | S_IFREG; - buf->st_uid = attr.at_uid; - buf->st_gid = attr.at_gid; - buf->st_atim.tv_sec = attr.at_atim / 1000000000ull; - buf->st_atim.tv_nsec = attr.at_atim % 1000000000ull; - buf->st_mtim.tv_sec = attr.at_mtim / 1000000000ull; - buf->st_mtim.tv_nsec = attr.at_mtim % 1000000000ull; - buf->st_ctim.tv_sec = attr.at_ctim / 1000000000ull; - buf->st_ctim.tv_nsec = attr.at_ctim % 1000000000ull; - buf->st_blksize = fs->cfg.block_size; - buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / - buf->st_blksize; - -errout: - nxmutex_unlock(&fs->lock); - return ret; + return OK; } static int littlefs_fchstat(FAR const struct file *filep, @@ -791,7 +767,6 @@ static int littlefs_fchstat(FAR const struct file *filep, FAR struct littlefs_mountpt_s *fs; FAR struct littlefs_file_s *priv; FAR struct inode *inode; - struct littlefs_attr_s attr; char path[LFS_NAME_MAX]; int ret; @@ -809,59 +784,20 @@ static int littlefs_fchstat(FAR const struct file *filep, return ret; } - ret = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file, - 0, &attr, sizeof(attr))); + ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path)); + nxmutex_unlock(&fs->lock); if (ret < 0) { - if (ret != -ENODATA) - { - goto errout; - } - - memset(&attr, 0, sizeof(attr)); - attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO; - } - - if ((CH_STAT_MODE & flags) == CH_STAT_MODE) - { - attr.at_mode = buf->st_mode; - } - - if ((CH_STAT_UID & flags) == CH_STAT_UID) - { - attr.at_uid = buf->st_uid; - } - - if ((CH_STAT_GID & flags) == CH_STAT_GID) - { - attr.at_gid = buf->st_gid; - } - - attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec + - buf->st_ctim.tv_nsec; - - if ((CH_STAT_ATIME & flags) == CH_STAT_ATIME) - { - attr.at_atim = 1000000000ull * buf->st_atim.tv_sec + - buf->st_atim.tv_nsec; - } - - if ((CH_STAT_MTIME & flags) == CH_STAT_MTIME) - { - attr.at_mtim = 1000000000ull * buf->st_mtim.tv_sec + - buf->st_mtim.tv_nsec; + return ret; } - ret = littlefs_convert_result(lfs_file_setattr(&fs->lfs, &priv->file, 0, - &attr, sizeof(attr))); + ret = littlefs_chstat(inode, path, buf, flags); if (ret < 0) { - goto errout; + return ret; } -errout: - nxmutex_unlock(&fs->lock); - return ret; + return OK; } /**************************************************************************** @@ -1651,6 +1587,7 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, buf->st_mtim.tv_nsec = attr.at_mtim % 1000000000ull; buf->st_ctim.tv_sec = attr.at_ctim / 1000000000ull; buf->st_ctim.tv_nsec = attr.at_ctim % 1000000000ull; + buf->st_size = info.size; buf->st_blksize = fs->cfg.block_size; buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize; @@ -1658,12 +1595,10 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (info.type == LFS_TYPE_REG) { buf->st_mode |= S_IFREG; - buf->st_size = info.size; } else { buf->st_mode |= S_IFDIR; - buf->st_size = 0; } errout: From 74be149192c22432937031d30363da1fe5ccd0d0 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 4/7] Revert "littlefs/stat: return OK when stat success" This reverts commit c5779297cf28f3e0d7d1d61597b1bfd29730138e. --- fs/littlefs/lfs_vfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 46c494ef49614..dfaf17440a80c 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -1573,11 +1573,11 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, goto errout; } + ret = 0; memset(&attr, 0, sizeof(attr)); attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO; } - ret = 0; buf->st_mode = attr.at_mode; buf->st_uid = attr.at_uid; buf->st_gid = attr.at_gid; @@ -1635,6 +1635,7 @@ static int littlefs_chstat(FAR struct inode *mountpt, goto errout; } + ret = 0; memset(&attr, 0, sizeof(attr)); } From d43247dcf421d6a2fdb79418a4368b9c079135dd Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 5/7] Revert "fs/littlefs: Add attr to dirA" This reverts commit 0ab637f57e58bd0e3c62ea9b69af330e69c63327. --- fs/littlefs/lfs_vfs.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index dfaf17440a80c..5c7c7b8592567 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -1444,25 +1444,6 @@ static int littlefs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, } ret = lfs_mkdir(&fs->lfs, relpath); - if (ret >= 0) - { - struct littlefs_attr_s attr; - struct timespec time; - - clock_gettime(CLOCK_REALTIME, &time); - memset(&attr, 0, sizeof(attr)); - attr.at_mode = mode; - 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, - &attr, sizeof(attr))); - if (ret < 0) - { - lfs_remove(&fs->lfs, relpath); - } - } - nxmutex_unlock(&fs->lock); return ret; @@ -1575,7 +1556,6 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = 0; memset(&attr, 0, sizeof(attr)); - attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO; } buf->st_mode = attr.at_mode; From dc81889b251e44688806844df4a1aabe5fbe3a1e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 6/7] Revert "fs/littlefs: Fix the bug of missing file types" This reverts commit f70de1c65f15eadbcdeb70b85effd3a9000c993c. --- fs/littlefs/lfs_vfs.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index 5c7c7b8592567..b83f71c69f605 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -1554,7 +1554,6 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, goto errout; } - ret = 0; memset(&attr, 0, sizeof(attr)); } @@ -1572,15 +1571,6 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize; - if (info.type == LFS_TYPE_REG) - { - buf->st_mode |= S_IFREG; - } - else - { - buf->st_mode |= S_IFDIR; - } - errout: nxmutex_unlock(&fs->lock); return ret; @@ -1615,7 +1605,6 @@ static int littlefs_chstat(FAR struct inode *mountpt, goto errout; } - ret = 0; memset(&attr, 0, sizeof(attr)); } From 708181efc1dd89ba87507ecce2bb855f6c094f22 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 23 Oct 2024 18:24:45 +0900 Subject: [PATCH 7/7] Revert "fs/littlefs: Implement fchstat and chstat of littlefs" This reverts commit 44992c8821250f66e4f184d2511215e8f9510f39. --- fs/littlefs/lfs_vfs.c | 206 +++++------------------------------------- 1 file changed, 22 insertions(+), 184 deletions(-) diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index b83f71c69f605..64163be740c51 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -80,17 +80,6 @@ struct littlefs_mountpt_s struct lfs lfs; }; -struct littlefs_attr_s -{ - uint32_t at_ver; /* For the later extension */ - uint32_t at_mode; /* File type, attributes, and access mode bits */ - uint32_t at_uid; /* User ID of file */ - uint32_t at_gid; /* Group ID of file */ - uint64_t at_atim; /* Time of last access */ - uint64_t at_mtim; /* Time of last modification */ - uint64_t at_ctim; /* Time of last status change */ -}; - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -112,8 +101,6 @@ static int littlefs_dup(FAR const struct file *oldp, FAR struct file *newp); static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf); -static int littlefs_fchstat(FAR const struct file *filep, - FAR const struct stat *buf, int flags); static int littlefs_truncate(FAR struct file *filep, off_t length); @@ -146,9 +133,6 @@ static int littlefs_rename(FAR struct inode *mountpt, FAR const char *newrelpath); static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, FAR struct stat *buf); -static int littlefs_chstat(FAR struct inode *mountpt, - FAR const char *relpath, - FAR const struct stat *buf, int flags); /**************************************************************************** * Public Data @@ -174,7 +158,7 @@ const struct mountpt_operations g_littlefs_operations = littlefs_sync, /* sync */ littlefs_dup, /* dup */ littlefs_fstat, /* fstat */ - littlefs_fchstat, /* fchstat */ + NULL, /* fchstat */ littlefs_opendir, /* opendir */ littlefs_closedir, /* closedir */ @@ -190,7 +174,7 @@ const struct mountpt_operations g_littlefs_operations = littlefs_rmdir, /* rmdir */ littlefs_rename, /* rename */ littlefs_stat, /* stat */ - littlefs_chstat /* chstat */ + NULL /* chstat */ }; /**************************************************************************** @@ -343,26 +327,6 @@ static int littlefs_open(FAR struct file *filep, FAR const char *relpath, goto errout; } - if (oflags & LFS_O_CREAT) - { - struct littlefs_attr_s attr; - struct timespec time; - - clock_gettime(CLOCK_REALTIME, &time); - memset(&attr, 0, sizeof(attr)); - attr.at_mode = mode; - 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, - &attr, sizeof(attr))); - if (ret < 0) - { - lfs_remove(&fs->lfs, relpath); - goto errout_with_file; - } - } - /* In append mode, we need to set the file pointer to the end of the * file. */ @@ -726,7 +690,6 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) FAR struct littlefs_mountpt_s *fs; FAR struct littlefs_file_s *priv; FAR struct inode *inode; - char path[LFS_NAME_MAX]; int ret; memset(buf, 0, sizeof(*buf)); @@ -745,57 +708,17 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) return ret; } - ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path)); + buf->st_size = lfs_file_size(&fs->lfs, &priv->file); nxmutex_unlock(&fs->lock); - if (ret < 0) - { - return ret; - } - ret = littlefs_stat(inode, path, buf); - if (ret < 0) + if (buf->st_size < 0) { - return ret; + return littlefs_convert_result(buf->st_size); } - return OK; -} - -static int littlefs_fchstat(FAR const struct file *filep, - FAR const struct stat *buf, int flags) -{ - FAR struct littlefs_mountpt_s *fs; - FAR struct littlefs_file_s *priv; - FAR struct inode *inode; - char path[LFS_NAME_MAX]; - int ret; - - /* Recover our private data from the struct file instance */ - - priv = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - /* Call LFS to get file size */ - - ret = nxmutex_lock(&fs->lock); - if (ret < 0) - { - return ret; - } - - ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path)); - nxmutex_unlock(&fs->lock); - if (ret < 0) - { - return ret; - } - - ret = littlefs_chstat(inode, path, buf, flags); - if (ret < 0) - { - return ret; - } + buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFREG; + buf->st_blksize = fs->cfg.block_size; + buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize; return OK; } @@ -1522,7 +1445,6 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, { FAR struct littlefs_mountpt_s *fs; struct lfs_info info; - struct littlefs_attr_s attr; int ret; memset(buf, 0, sizeof(*buf)); @@ -1540,112 +1462,28 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, } ret = lfs_stat(&fs->lfs, relpath, &info); - if (ret < 0) - { - goto errout; - } - - ret = littlefs_convert_result(lfs_getattr(&fs->lfs, relpath, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - if (ret != -ENODATA) - { - goto errout; - } - - memset(&attr, 0, sizeof(attr)); - } - - buf->st_mode = attr.at_mode; - buf->st_uid = attr.at_uid; - buf->st_gid = attr.at_gid; - buf->st_atim.tv_sec = attr.at_atim / 1000000000ull; - buf->st_atim.tv_nsec = attr.at_atim % 1000000000ull; - buf->st_mtim.tv_sec = attr.at_mtim / 1000000000ull; - buf->st_mtim.tv_nsec = attr.at_mtim % 1000000000ull; - buf->st_ctim.tv_sec = attr.at_ctim / 1000000000ull; - buf->st_ctim.tv_nsec = attr.at_ctim % 1000000000ull; - buf->st_size = info.size; - buf->st_blksize = fs->cfg.block_size; - buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / - buf->st_blksize; - -errout: nxmutex_unlock(&fs->lock); - return ret; -} - -static int littlefs_chstat(FAR struct inode *mountpt, - FAR const char *relpath, - FAR const struct stat *buf, int flags) -{ - FAR struct littlefs_mountpt_s *fs; - struct littlefs_attr_s attr; - int ret; - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Call LFS to get file size */ - ret = nxmutex_lock(&fs->lock); - if (ret < 0) + if (ret >= 0) { - return ret; - } + /* Convert info to stat */ - ret = littlefs_convert_result(lfs_getattr(&fs->lfs, relpath, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - if (ret != -ENODATA) + buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU; + if (info.type == LFS_TYPE_REG) { - goto errout; + buf->st_mode |= S_IFREG; + buf->st_size = info.size; + } + else + { + buf->st_mode |= S_IFDIR; + buf->st_size = 0; } - memset(&attr, 0, sizeof(attr)); - } - - if ((CH_STAT_MODE & flags) == CH_STAT_MODE) - { - attr.at_mode = buf->st_mode; - } - - if ((CH_STAT_UID & flags) == CH_STAT_UID) - { - attr.at_uid = buf->st_uid; - } - - if ((CH_STAT_GID & flags) == CH_STAT_GID) - { - attr.at_gid = buf->st_gid; - } - - attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec + - buf->st_ctim.tv_nsec; - - if ((CH_STAT_ATIME & flags) == CH_STAT_ATIME) - { - attr.at_atim = 1000000000ull * buf->st_atim.tv_sec + - buf->st_atim.tv_nsec; - } - - if ((CH_STAT_MTIME & flags) == CH_STAT_MTIME) - { - attr.at_mtim = 1000000000ull * buf->st_mtim.tv_sec + - buf->st_mtim.tv_nsec; - } - - ret = littlefs_convert_result(lfs_setattr(&fs->lfs, relpath, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - goto errout; + buf->st_blksize = fs->cfg.block_size; + buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / + buf->st_blksize; } -errout: - nxmutex_unlock(&fs->lock); return ret; }