Skip to content

Commit 8f6ea88

Browse files
DS-LKGUIDINGLI
authored andcommitted
fs/littlefs: revert fstat and use lfs_file_attr function
Signed-off-by: zhouliang3 <zhouliang3@xiaomi.com>
1 parent c577929 commit 8f6ea88

File tree

4 files changed

+164
-16
lines changed

4 files changed

+164
-16
lines changed

fs/littlefs/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ if(CONFIG_FS_LITTLEFS)
3131
${CMAKE_BINARY_DIR}/fs/littlefs/littlefs
3232
PATCH_COMMAND
3333
patch -p2 -d ${CMAKE_CURRENT_LIST_DIR} <
34-
${CMAKE_CURRENT_LIST_DIR}/lfs_util.patch COMMAND patch -p2 -d
35-
${CMAKE_CURRENT_LIST_DIR} < ${CMAKE_CURRENT_LIST_DIR}/lfs_getpath.patch)
34+
${CMAKE_CURRENT_LIST_DIR}/lfs_util.patch && patch -p2 -d
35+
${CMAKE_CURRENT_LIST_DIR} < ${CMAKE_CURRENT_LIST_DIR}/lfs_getpath.patch
36+
&& patch -p2 -d ${CMAKE_CURRENT_LIST_DIR} <
37+
${CMAKE_CURRENT_LIST_DIR}/lfs_getsetattr.patch)
3638
FetchContent_MakeAvailable(littlefs)
3739
endif()
3840

fs/littlefs/Make.defs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ $(LITTLEFS_TARBALL):
5656
$(Q) mv littlefs/littlefs-$(LITTLEFS_VERSION) littlefs/littlefs
5757
$(Q) git apply littlefs/lfs_util.patch
5858
$(Q) git apply littlefs/lfs_getpath.patch
59+
$(Q) git apply littlefs/lfs_getsetattr.patch
5960
$(Q) touch littlefs/.littlefsunpack
6061

6162
# Download and unpack tarball if no git repo found

fs/littlefs/lfs_getsetattr.patch

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--- ./littlefs/littlefs/lfs.c
2+
+++ ./littlefs/littlefs/lfs.c
3+
@@ -5717,6 +5717,41 @@ int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size) {
4+
return err < 0 ? err : 0;
5+
}
6+
7+
+lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file,
8+
+ uint8_t type, void *buffer, lfs_size_t size)
9+
+{
10+
+ int err = LFS_LOCK(lfs->cfg);
11+
+ if (err) {
12+
+ return err;
13+
+ }
14+
+ LFS_TRACE("lfs_file_setattr(%p, %p)", (void*)lfs, (void*)file);
15+
+ LFS_TRACE("lfs_file_setattr(%"PRIu8", %p, %"PRIu32")",
16+
+ type, buffer, size);
17+
+ LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
18+
+
19+
+ return lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x7ff, 0x3ff, 0),
20+
+ LFS_MKTAG(LFS_TYPE_USERATTR + type,
21+
+ file->id, lfs_min(size, lfs->attr_max)), buffer);
22+
+}
23+
+
24+
+#ifndef LFS_READONLY
25+
+int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file,
26+
+ uint8_t type, const void *buffer, lfs_size_t size)
27+
+{
28+
+ int err = LFS_LOCK(lfs->cfg);
29+
+ if (err) {
30+
+ return err;
31+
+ }
32+
+ LFS_TRACE("lfs_file_getattr(%p, %p)", (void*)lfs, (void*)file);
33+
+ LFS_TRACE("lfs_file_getattr(%"PRIu8", %p, %"PRIu32")",
34+
+ type, buffer, size);
35+
+ LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
36+
+
37+
+ return lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
38+
+ {LFS_MKTAG(LFS_TYPE_USERATTR + type, file->id, size), buffer}));
39+
+}
40+
+#endif
41+
+
42+
#ifndef LFS_READONLY
43+
int lfs_mkdir(lfs_t *lfs, const char *path) {
44+
int err = LFS_LOCK(lfs->cfg);
45+
--- ./littlefs/littlefs/lfs.h
46+
+++ ./littlefs/littlefs/lfs.h
47+
@@ -611,6 +611,33 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
48+
// Returns a negative error code on failure.
49+
int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size);
50+
51+
+// Get a custom attribute of file
52+
+//
53+
+// Custom attributes are uniquely identified by an 8-bit type and limited
54+
+// to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller than
55+
+// the buffer, it will be padded with zeros. If the stored attribute is larger,
56+
+// then it will be silently truncated. If no attribute is found, the error
57+
+// LFS_ERR_NOATTR is returned and the buffer is filled with zeros.
58+
+//
59+
+// Returns the size of the attribute, or a negative error code on failure.
60+
+// Note, the returned size is the size of the attribute on disk, irrespective
61+
+// of the size of the buffer. This can be used to dynamically allocate a buffer
62+
+// or check for existance.
63+
+lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file,
64+
+ uint8_t type, void *buffer, lfs_size_t size);
65+
+
66+
+// Set custom attributes of file
67+
+//
68+
+// Custom attributes are uniquely identified by an 8-bit type and limited
69+
+// to LFS_ATTR_MAX bytes. If an attribute is not found, it will be
70+
+// implicitly created.
71+
+//
72+
+// Returns a negative error code on failure.
73+
+#ifndef LFS_READONLY
74+
+int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file,
75+
+ uint8_t type, const void *buffer, lfs_size_t size);
76+
+#endif
77+
+
78+
/// Directory operations ///
79+
80+
#ifndef LFS_READONLY

fs/littlefs/lfs_vfs.c

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf)
726726
FAR struct littlefs_mountpt_s *fs;
727727
FAR struct littlefs_file_s *priv;
728728
FAR struct inode *inode;
729+
struct littlefs_attr_s attr;
729730
char path[LFS_NAME_MAX];
730731
int ret;
731732

@@ -745,20 +746,43 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf)
745746
return ret;
746747
}
747748

748-
ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path));
749-
nxmutex_unlock(&fs->lock);
750-
if (ret < 0)
749+
buf->st_size = lfs_file_size(&fs->lfs, &priv->file);
750+
if (buf->st_size < 0)
751751
{
752-
return ret;
752+
ret = littlefs_convert_result(buf->st_size);
753+
goto errout;
753754
}
754755

755-
ret = littlefs_stat(inode, path, buf);
756+
ret = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file, 0,
757+
&attr, sizeof(attr)));
756758
if (ret < 0)
757759
{
758-
return ret;
760+
if (ret != -ENODATA)
761+
{
762+
goto errout;
763+
}
764+
765+
memset(&attr, 0, sizeof(attr));
766+
attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO;
759767
}
760768

761-
return OK;
769+
ret = 0;
770+
buf->st_mode = attr.at_mode | S_IFREG;
771+
buf->st_uid = attr.at_uid;
772+
buf->st_gid = attr.at_gid;
773+
buf->st_atim.tv_sec = attr.at_atim / 1000000000ull;
774+
buf->st_atim.tv_nsec = attr.at_atim % 1000000000ull;
775+
buf->st_mtim.tv_sec = attr.at_mtim / 1000000000ull;
776+
buf->st_mtim.tv_nsec = attr.at_mtim % 1000000000ull;
777+
buf->st_ctim.tv_sec = attr.at_ctim / 1000000000ull;
778+
buf->st_ctim.tv_nsec = attr.at_ctim % 1000000000ull;
779+
buf->st_blksize = fs->cfg.block_size;
780+
buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
781+
buf->st_blksize;
782+
783+
errout:
784+
nxmutex_unlock(&fs->lock);
785+
return ret;
762786
}
763787

764788
static int littlefs_fchstat(FAR const struct file *filep,
@@ -767,6 +791,7 @@ static int littlefs_fchstat(FAR const struct file *filep,
767791
FAR struct littlefs_mountpt_s *fs;
768792
FAR struct littlefs_file_s *priv;
769793
FAR struct inode *inode;
794+
struct littlefs_attr_s attr;
770795
char path[LFS_NAME_MAX];
771796
int ret;
772797

@@ -784,20 +809,59 @@ static int littlefs_fchstat(FAR const struct file *filep,
784809
return ret;
785810
}
786811

787-
ret = lfs_file_path(&fs->lfs, &priv->file, path, sizeof(path));
788-
nxmutex_unlock(&fs->lock);
812+
ret = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file,
813+
0, &attr, sizeof(attr)));
789814
if (ret < 0)
790815
{
791-
return ret;
816+
if (ret != -ENODATA)
817+
{
818+
goto errout;
819+
}
820+
821+
memset(&attr, 0, sizeof(attr));
822+
attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO;
823+
}
824+
825+
if ((CH_STAT_MODE & flags) == CH_STAT_MODE)
826+
{
827+
attr.at_mode = buf->st_mode;
828+
}
829+
830+
if ((CH_STAT_UID & flags) == CH_STAT_UID)
831+
{
832+
attr.at_uid = buf->st_uid;
833+
}
834+
835+
if ((CH_STAT_GID & flags) == CH_STAT_GID)
836+
{
837+
attr.at_gid = buf->st_gid;
838+
}
839+
840+
attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec +
841+
buf->st_ctim.tv_nsec;
842+
843+
if ((CH_STAT_ATIME & flags) == CH_STAT_ATIME)
844+
{
845+
attr.at_atim = 1000000000ull * buf->st_atim.tv_sec +
846+
buf->st_atim.tv_nsec;
792847
}
793848

794-
ret = littlefs_chstat(inode, path, buf, flags);
849+
if ((CH_STAT_MTIME & flags) == CH_STAT_MTIME)
850+
{
851+
attr.at_mtim = 1000000000ull * buf->st_mtim.tv_sec +
852+
buf->st_mtim.tv_nsec;
853+
}
854+
855+
ret = littlefs_convert_result(lfs_file_setattr(&fs->lfs, &priv->file, 0,
856+
&attr, sizeof(attr)));
795857
if (ret < 0)
796858
{
797-
return ret;
859+
goto errout;
798860
}
799861

800-
return OK;
862+
errout:
863+
nxmutex_unlock(&fs->lock);
864+
return ret;
801865
}
802866

803867
/****************************************************************************
@@ -1587,18 +1651,19 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath,
15871651
buf->st_mtim.tv_nsec = attr.at_mtim % 1000000000ull;
15881652
buf->st_ctim.tv_sec = attr.at_ctim / 1000000000ull;
15891653
buf->st_ctim.tv_nsec = attr.at_ctim % 1000000000ull;
1590-
buf->st_size = info.size;
15911654
buf->st_blksize = fs->cfg.block_size;
15921655
buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
15931656
buf->st_blksize;
15941657

15951658
if (info.type == LFS_TYPE_REG)
15961659
{
15971660
buf->st_mode |= S_IFREG;
1661+
buf->st_size = info.size;
15981662
}
15991663
else
16001664
{
16011665
buf->st_mode |= S_IFDIR;
1666+
buf->st_size = 0;
16021667
}
16031668

16041669
errout:

0 commit comments

Comments
 (0)