Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

littlefs: Add lfs_file_getattr and lfs_file_setattr #1045

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6235,6 +6235,41 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
return res;
}

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);
Expand Down
27 changes: 27 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,33 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);


// 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
Expand Down
Loading