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 249b945e7a90e..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; - struct littlefs_attr_s attr; int ret; memset(buf, 0, sizeof(*buf)); @@ -746,120 +709,18 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) } buf->st_size = lfs_file_size(&fs->lfs, &priv->file); - if (buf->st_size < 0) - { - ret = littlefs_convert_result(buf->st_size); - goto errout; - } - - ret = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - if (ret != -ENODATA) - { - goto errout; - } - - memset(&attr, 0, sizeof(attr)); - attr.at_mode = S_IRWXG | S_IRWXU | S_IRWXO; - } - - 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; -} -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; - struct littlefs_attr_s attr; - 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 = littlefs_convert_result(lfs_file_getattr(&fs->lfs, &priv->file, - 0, &attr, sizeof(attr))); - 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) + if (buf->st_size < 0) { - attr.at_mtim = 1000000000ull * buf->st_mtim.tv_sec + - buf->st_mtim.tv_nsec; + return littlefs_convert_result(buf->st_size); } - ret = littlefs_convert_result(lfs_file_setattr(&fs->lfs, &priv->file, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - goto errout; - } + 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; -errout: - nxmutex_unlock(&fs->lock); - return ret; + return OK; } /**************************************************************************** @@ -1491,29 +1352,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,37 +1363,12 @@ static int littlefs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = nxmutex_lock(&fs->lock); if (ret < 0) { - goto errout; - } - - ret = littlefs_convert_result(lfs_mkdir(&fs->lfs, path)); - 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, path, 0, - &attr, sizeof(attr))); - if (ret < 0) - { - lfs_remove(&fs->lfs, path); - } + return ret; } + ret = lfs_mkdir(&fs->lfs, relpath); nxmutex_unlock(&fs->lock); -errout: - if (path != relpath) - { - lib_put_pathbuffer(path); - } - return ret; } @@ -1630,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)); @@ -1648,124 +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)); - 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; - 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; - - 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: 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; }