Skip to content

Commit bf6c839

Browse files
DS-LKxiaoxiang781216
authored andcommitted
littlefs: Add lfs_file_getattr and lfs_file_setattr
Signed-off-by: zhouliang3 <[email protected]>
1 parent d01280e commit bf6c839

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lfs.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6235,6 +6235,41 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
62356235
return res;
62366236
}
62376237

6238+
lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file,
6239+
uint8_t type, void *buffer, lfs_size_t size)
6240+
{
6241+
int err = LFS_LOCK(lfs->cfg);
6242+
if (err) {
6243+
return err;
6244+
}
6245+
LFS_TRACE("lfs_file_setattr(%p, %p)", (void*)lfs, (void*)file);
6246+
LFS_TRACE("lfs_file_setattr(%"PRIu8", %p, %"PRIu32")",
6247+
type, buffer, size);
6248+
LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
6249+
6250+
return lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x7ff, 0x3ff, 0),
6251+
LFS_MKTAG(LFS_TYPE_USERATTR + type,
6252+
file->id, lfs_min(size, lfs->attr_max)), buffer);
6253+
}
6254+
6255+
#ifndef LFS_READONLY
6256+
int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file,
6257+
uint8_t type, const void *buffer, lfs_size_t size)
6258+
{
6259+
int err = LFS_LOCK(lfs->cfg);
6260+
if (err) {
6261+
return err;
6262+
}
6263+
LFS_TRACE("lfs_file_getattr(%p, %p)", (void*)lfs, (void*)file);
6264+
LFS_TRACE("lfs_file_getattr(%"PRIu8", %p, %"PRIu32")",
6265+
type, buffer, size);
6266+
LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
6267+
6268+
return lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
6269+
{LFS_MKTAG(LFS_TYPE_USERATTR + type, file->id, size), buffer}));
6270+
}
6271+
#endif
6272+
62386273
#ifndef LFS_READONLY
62396274
int lfs_mkdir(lfs_t *lfs, const char *path) {
62406275
int err = LFS_LOCK(lfs->cfg);

lfs.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,33 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
657657
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
658658

659659

660+
// Get a custom attribute of file
661+
//
662+
// Custom attributes are uniquely identified by an 8-bit type and limited
663+
// to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller than
664+
// the buffer, it will be padded with zeros. If the stored attribute is larger,
665+
// then it will be silently truncated. If no attribute is found, the error
666+
// LFS_ERR_NOATTR is returned and the buffer is filled with zeros.
667+
//
668+
// Returns the size of the attribute, or a negative error code on failure.
669+
// Note, the returned size is the size of the attribute on disk, irrespective
670+
// of the size of the buffer. This can be used to dynamically allocate a buffer
671+
// or check for existance.
672+
lfs_ssize_t lfs_file_getattr(lfs_t *lfs, lfs_file_t *file,
673+
uint8_t type, void *buffer, lfs_size_t size);
674+
675+
// Set custom attributes of file
676+
//
677+
// Custom attributes are uniquely identified by an 8-bit type and limited
678+
// to LFS_ATTR_MAX bytes. If an attribute is not found, it will be
679+
// implicitly created.
680+
//
681+
// Returns a negative error code on failure.
682+
#ifndef LFS_READONLY
683+
int lfs_file_setattr(lfs_t *lfs, lfs_file_t *file,
684+
uint8_t type, const void *buffer, lfs_size_t size);
685+
#endif
686+
660687
/// Directory operations ///
661688

662689
#ifndef LFS_READONLY

0 commit comments

Comments
 (0)