Added simple custom attributes

A much requested feature (mostly because of littlefs's notable lack of
timestamps), this commits adds support for user-specified custom
attributes.

Planned (though underestimated) since v1, custom attributes provide a
route for OSs and applications to provide their own metadata in
littlefs, without limiting portability.

However, unlike custom attributes that can be found on much more
powerful PC filesystems, these custom attributes are very limited,
intended for only a handful of bytes for very important metadata. Each
attribute has only a single byte to identify the attribute, and the
size of all attributes attached to a file is limited to 64 bytes.

Custom attributes can be accessed through the lfs_getattr, lfs_setattr,
and lfs_removeattr functions.
This commit is contained in:
Christopher Haster
2018-04-05 19:03:58 -05:00
parent 65ea6b3d0f
commit 6ffc8d3480
2 changed files with 224 additions and 32 deletions

32
lfs.h
View File

@@ -89,6 +89,8 @@ enum lfs_error {
LFS_ERR_NOSPC = -28, // No space left on device
LFS_ERR_NOMEM = -12, // No more memory available
LFS_ERR_NAMETOOLONG = -36, // File name too long
LFS_ERR_NODATA = -61, // No data/attr available
LFS_ERR_RANGE = -34, // Result not representable
};
// File types
@@ -253,6 +255,13 @@ typedef struct lfs_entry {
} d;
} lfs_entry_t;
typedef struct lfs_attr {
struct lfs_disk_attr {
uint8_t type;
uint8_t len;
} d;
} lfs_attr_t;
typedef struct lfs_cache {
lfs_block_t block;
lfs_off_t off;
@@ -379,6 +388,29 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
// Returns a negative error code on failure.
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
// Get a custom attribute
//
// Attributes are identified by an 8-bit type and are limited to at
// most LFS_ATTRS_SIZE bytes.
// Returns the size of the attribute, or a negative error code on failure.
int lfs_getattr(lfs_t *lfs, const char *path,
uint8_t type, void *buffer, lfs_size_t size);
// Set a custom attribute
//
// Attributes are identified by an 8-bit type and are limited to at
// most LFS_ATTRS_SIZE bytes.
// Returns a negative error code on failure.
int lfs_setattr(lfs_t *lfs, const char *path,
uint8_t type, const void *buffer, lfs_size_t size);
// Remove a custom attribute
//
// Attributes are identified by an 8-bit type and are limited to at
// most LFS_ATTRS_SIZE bytes.
// Returns a negative error code on failure.
int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
/// File operations ///