/* * The little filesystem * * Copyright (c) 2017, Arm Limited. All rights reserved. * SPDX-License-Identifier: BSD-3-Clause */ #ifndef LFS_H #define LFS_H #include "lfs_util.h" #ifdef __cplusplus extern "C" { #endif /// Version info /// // Software library version // Major (top-nibble), incremented on backwards incompatible changes // Minor (bottom-nibble), incremented on feature additions #define LFS_VERSION 0x00020002 #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16)) #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0)) // Version of On-disk data structures // Major (top-nibble), incremented on backwards incompatible changes // Minor (bottom-nibble), incremented on feature additions #define LFS_DISK_VERSION 0x00020000 #define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16)) #define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0)) /// Definitions /// // Type definitions typedef uint32_t lfs_size_t; typedef uint32_t lfs_off_t; typedef int32_t lfs_ssize_t; typedef int32_t lfs_soff_t; typedef uint32_t lfs_block_t; // Maximum name size in bytes, may be redefined to reduce the size of the // info struct. Limited to <= 1022. Stored in superblock and must be // respected by other littlefs drivers. #ifndef LFS_NAME_MAX #if defined(LFS_NAME_LIMIT) && \ LFS_NAME_LIMIT > 0 && LFS_NAME_MAX <= 1022 #define LFS_NAME_MAX LFS_NAME_LIMIT #else #define LFS_NAME_MAX 255 #endif #endif // Maximum size of a file in bytes, may be redefined to limit to support other // drivers. Limited on disk to <= 4294967296. However, above 2147483647 the // functions lfs_file_seek, lfs_file_size, and lfs_file_tell will return // incorrect values due to using signed integers. Stored in superblock and // must be respected by other littlefs drivers. #ifndef LFS_FILE_MAX #if defined(LFS_FILE_LIMIT) && \ LFS_FILE_LIMIT > 0 && LFS_FILE_LIMIT <= 4294967296 #define LFS_FILE_MAX LFS_FILE_LIMIT #else #define LFS_FILE_MAX 2147483647 #endif #endif // Maximum size of custom attributes in bytes, may be redefined, but there is // no real benefit to using a smaller LFS_ATTR_MAX. Limited to <= 1022. #ifndef LFS_ATTR_MAX #if defined(LFS_ATTR_LIMIT) && \ LFS_ATTR_LIMIT > 0 && LFS_ATTR_LIMIT <= 1022 #define LFS_ATTR_MAX LFS_FILE_LIMIT #else #define LFS_ATTR_MAX 1022 #endif #endif // File types enum lfs_type { // file types LFS_TYPE_REG = 0x001, LFS_TYPE_DIR = 0x002, // internally used types LFS_TYPE_SPLICE = 0x400, LFS_TYPE_NAME = 0x000, LFS_TYPE_STRUCT = 0x200, LFS_TYPE_USERATTR = 0x300, LFS_TYPE_FROM = 0x100, LFS_TYPE_TAIL = 0x600, LFS_TYPE_GLOBALS = 0x700, LFS_TYPE_CRC = 0x500, // internally used type specializations LFS_TYPE_CREATE = 0x401, LFS_TYPE_DELETE = 0x4ff, LFS_TYPE_SUPERBLOCK = 0x0ff, LFS_TYPE_DIRSTRUCT = 0x200, LFS_TYPE_CTZSTRUCT = 0x202, LFS_TYPE_INLINESTRUCT = 0x201, LFS_TYPE_SOFTTAIL = 0x600, LFS_TYPE_HARDTAIL = 0x601, LFS_TYPE_MOVESTATE = 0x7ff, // internal chip sources LFS_FROM_NOOP = 0x000, LFS_FROM_MOVE = 0x101, LFS_FROM_USERATTRS = 0x102, }; // File open flags enum lfs_open_flags { // open flags LFS_O_RDONLY = 1, // Open a file as read only LFS_O_WRONLY = 2, // Open a file as write only LFS_O_RDWR = 3, // Open a file as read and write LFS_O_CREAT = 0x0100, // Create a file if it does not exist LFS_O_EXCL = 0x0200, // Fail if a file already exists LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size LFS_O_APPEND = 0x0800, // Move to end of file on every write // internally used flags LFS_F_DIRTY = 0x010000, // File does not match storage LFS_F_WRITING = 0x020000, // File has been written since last flush LFS_F_READING = 0x040000, // File has been read since last flush LFS_F_ERRED = 0x080000, // An error occured during write LFS_F_INLINE = 0x100000, // Currently inlined in directory entry LFS_F_OPENED = 0x200000, // File has been opened }; // File seek flags enum lfs_whence_flags { LFS_SEEK_SET = 0, // Seek relative to an absolute position LFS_SEEK_CUR = 1, // Seek relative to the current file position LFS_SEEK_END = 2, // Seek relative to the end of the file }; // Configuration provided during initialization of the littlefs // If every config option is provided at compile time, littlefs switches // to "LFS_STATICCFG" mode. The dynamic lfs_cfg struct is not included in // the lfs_t struct, and *cfg functions are no longer available. #if defined(LFS_BD_READ) && \ defined(LFS_BD_PROG) && \ defined(LFS_BD_ERASE) && \ defined(LFS_BD_SYNC) && \ defined(LFS_READ_SIZE) && \ defined(LFS_PROG_SIZE) && \ defined(LFS_BLOCK_SIZE) && \ defined(LFS_BLOCK_COUNT) && \ defined(LFS_BLOCK_CYCLES) && \ defined(LFS_BUFFER_SIZE) && \ defined(LFS_LOOKAHEAD_SIZE) && \ defined(LFS_READ_BUFFER) && \ defined(LFS_PROG_BUFFER) && \ defined(LFS_LOOKAHEAD_BUFFER) && \ defined(LFS_NAME_LIMIT) && \ defined(LFS_FILE_LIMIT) && \ defined(LFS_ATTR_LIMIT) #define LFS_STATICCFG #endif // Dynamic config struct #ifndef LFS_STATICCFG struct lfs_cfg { #endif // Opaque user provided context that can be used to pass // information to the block device operations #if !(defined(LFS_BD_READ) && \ defined(LFS_BD_PROG) && \ defined(LFS_BD_ERASE) && \ defined(LFS_BD_SYNC)) void *bd_ctx; #endif // Read a region in a block. Negative error codes are propogated // to the user. #ifndef LFS_BD_READ int (*bd_read)(void *ctx, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); #define LFS_CFG_BD_READ(cfg, block, off, buffer, size) \ (cfg)->bd_read((cfg)->bd_ctx, block, off, buffer, size) #else #define LFS_CFG_BD_READ(cfg, block, off, buffer, size) \ lfs_bd_read(block, off, buffer, size) #endif // Program a region in a block. The block must have previously // been erased. Negative error codes are propogated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. #ifndef LFS_BD_PROG int (*bd_prog)(void *ctx, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); #define LFS_CFG_BD_PROG(cfg, block, off, buffer, size) \ (cfg)->bd_prog((cfg)->bd_ctx, block, off, buffer, size) #else #define LFS_CFG_BD_PROG(cfg, block, off, buffer, size) \ lfs_bd_prog(block, off, buffer, size) #endif // Erase a block. A block must be erased before being programmed. // The state of an erased block is undefined. Negative error codes // are propogated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. #ifndef LFS_BD_ERASE int (*bd_erase)(void *ctx, lfs_block_t block); #define LFS_CFG_BD_ERASE(cfg, block) \ (cfg)->bd_erase((cfg)->bd_ctx, block) #else #define LFS_CFG_BD_ERASE(cfg, block) \ lfs_bd_erase(block) #endif // Sync the state of the underlying block device. Negative error codes // are propogated to the user. #ifndef LFS_BD_SYNC int (*bd_sync)(void *ctx); #define LFS_CFG_BD_SYNC(cfg) \ (cfg)->bd_sync((cfg)->bd_ctx) #else #define LFS_CFG_BD_SYNC(cfg) \ lfs_bd_sync() #endif // Minimum size of a block read. All read operations will be a // multiple of this value. #ifndef LFS_READ_SIZE lfs_size_t read_size; #define LFS_CFG_READ_SIZE(cfg) (cfg)->read_size #else #define LFS_CFG_READ_SIZE(cfg) LFS_READ_SIZE #endif // Minimum size of a block program. All program operations will be a // multiple of this value. #ifndef LFS_PROG_SIZE lfs_size_t prog_size; #define LFS_CFG_PROG_SIZE(cfg) (cfg)->prog_size #else #define LFS_CFG_PROG_SIZE(cfg) LFS_PROG_SIZE #endif // Size of an erasable block. This does not impact ram consumption and // may be larger than the physical erase size. However, non-inlined files // take up at minimum one block. Must be a multiple of the read // and program sizes. #ifndef LFS_BLOCK_SIZE lfs_size_t block_size; #define LFS_CFG_BLOCK_SIZE(cfg) (cfg)->block_size #else #define LFS_CFG_BLOCK_SIZE(cfg) LFS_BLOCK_SIZE #endif // Number of erasable blocks on the device. #ifndef LFS_BLOCK_COUNT lfs_size_t block_count; #define LFS_CFG_BLOCK_COUNT(cfg) (cfg)->block_count #else #define LFS_CFG_BLOCK_COUNT(cfg) LFS_BLOCK_COUNT #endif // Number of erase cycles before littlefs evicts metadata logs and moves // the metadata to another block. Suggested values are in the // range 100-1000, with large values having better performance at the cost // of less consistent wear distribution. // // Set to -1 to disable block-level wear-leveling. #ifndef LFS_BLOCK_CYCLES int32_t block_cycles; #define LFS_CFG_BLOCK_CYCLES(cfg) (cfg)->block_cycles #else #define LFS_CFG_BLOCK_CYCLES(cfg) LFS_BLOCK_CYCLES #endif // Size of internal buffers used to cache slices of blocks in RAM. // The littlefs needs a read buffer, a program buffer, and one additional // buffer per file. Larger buffers can improve performance by storing more // data and reducing the number of disk accesses. Must be a multiple of // the read and program sizes, and a factor of the block size. #ifndef LFS_BUFFER_SIZE lfs_size_t buffer_size; #define LFS_CFG_BUFFER_SIZE(cfg) (cfg)->buffer_size #else #define LFS_CFG_BUFFER_SIZE(cfg) LFS_BUFFER_SIZE #endif // Size of the lookahead buffer in bytes. A larger lookahead buffer // increases the number of blocks found during an allocation pass. The // lookahead buffer is stored as a compact bitmap, so each byte of RAM // can track 8 blocks. Must be a multiple of 8. #ifndef LFS_LOOKAHEAD_SIZE lfs_size_t lookahead_size; #define LFS_CFG_LOOKAHEAD_SIZE(cfg) (cfg)->lookahead_size #else #define LFS_CFG_LOOKAHEAD_SIZE(cfg) LFS_LOOKAHEAD_SIZE #endif // Optional statically allocated read buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. #ifndef LFS_READ_BUFFER void *read_buffer; #define LFS_CFG_READ_BUFFER(cfg) (cfg)->read_buffer #else #define LFS_CFG_READ_BUFFER(cfg) LFS_READ_BUFFER #endif // Optional statically allocated program buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. #ifndef LFS_PROG_BUFFER void *prog_buffer; #define LFS_CFG_PROG_BUFFER(cfg) (cfg)->prog_buffer #else #define LFS_CFG_PROG_BUFFER(cfg) LFS_PROG_BUFFER #endif // Optional statically allocated lookahead buffer. Must be lookahead_size // and aligned to a 32-bit boundary. By default lfs_malloc is used to // allocate this buffer. #ifndef LFS_LOOKAHEAD_BUFFER void *lookahead_buffer; #define LFS_CFG_LOOKAHEAD_BUFFER(cfg) (cfg)->lookahead_buffer #else #define LFS_CFG_LOOKAHEAD_BUFFER(cfg) LFS_LOOKAHEAD_BUFFER #endif // Optional upper limit on length of file names in bytes. No downside for // larger names except the size of the info struct which is controlled by // the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in // superblock and must be respected by other littlefs drivers. #ifndef LFS_NAME_LIMIT lfs_size_t name_limit; #define LFS_CFG_NAME_LIMIT(cfg) (cfg)->name_limit #else #define LFS_CFG_NAME_LIMIT(cfg) LFS_NAME_LIMIT #endif // Optional upper limit on files in bytes. No downside for larger files // but must be <= LFS_FILE_MAX. Defaults to LFS_FILE_MAX when zero. Stored // in superblock and must be respected by other littlefs drivers. #ifndef LFS_FILE_LIMIT lfs_size_t file_limit; #define LFS_CFG_FILE_LIMIT(cfg) (cfg)->file_limit #else #define LFS_CFG_FILE_LIMIT(cfg) LFS_FILE_LIMIT #endif // Optional upper limit on custom attributes in bytes. No downside for // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to // LFS_ATTR_MAX when zero. #ifndef LFS_ATTR_LIMIT lfs_size_t attr_limit; #define LFS_CFG_ATTR_LIMIT(cfg) (cfg)->attr_limit #else #define LFS_CFG_ATTR_LIMIT(cfg) LFS_ATTR_LIMIT #endif #ifndef LFS_STATICCFG }; #endif // Configurable callbacks are a bit special, when LFS_BD_* is defined, // LFS_CFG_* instead expands into a call to an extern lfs_bd_*, which // must be defined by the user. This preserves type-safety of the // callbacks. #ifdef LFS_BD_READ extern int lfs_bd_read(lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); #endif #ifdef LFS_BD_PROG extern int lfs_bd_prog(lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); #endif #ifdef LFS_BD_ERASE extern int lfs_bd_erase(lfs_block_t block); #endif #ifdef LFS_BD_SYNC extern int lfs_bd_sync(void); #endif // If every config option is provided at compile time, littlefs switches // to "LFS_FILE_STATICCFG" mode. The dynamic lfs_file_cfg struct is not // included in the lfs_file_t struct, and *cfg functions are no longer // available. #if defined(LFS_FILE_BUFFER) && \ defined(LFS_FILE_ATTRS) && \ defined(LFS_FILE_ATTR_COUNT) #define LFS_STATICCFG #endif #ifndef LFS_FILE_STATICCFG // Optional configuration provided during lfs_file_opencfg struct lfs_file_cfg { #endif // Optional statically allocated file buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. #ifndef LFS_FILE_BUFFER void *buffer; #define LFS_FILE_CFG_BUFFER(cfg) (cfg)->buffer #else #define LFS_FILE_CFG_BUFFER(cfg) LFS_FILE_BUFFER #endif // Optional list of custom attributes related to the file. If the file // is opened with read access, these attributes will be read from disk // during the open call. If the file is opened with write access, the // attributes will be written to disk every file sync or close. This // write occurs atomically with update to the file's contents. // // 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 the attribute is not // found, it will be created implicitly. #ifndef LFS_FILE_ATTRS struct lfs_attr *attrs; #define LFS_FILE_CFG_ATTRS(cfg) (cfg)->attrs #else #define LFS_FILE_CFG_ATTRS(cfg) LFS_FILE_ATTRS #endif // Number of custom attributes in the list #ifndef LFS_FILE_ATTR_COUNT lfs_size_t attr_count; #define LFS_FILE_CFG_ATTR_COUNT(cfg) (cfg)->attr_count #else #define LFS_FILE_CFG_ATTR_COUNT(cfg) LFS_FILE_ATTR_COUNT #endif #ifndef LFS_FILE_STATICCFG }; #endif // File info structure struct lfs_info { // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR uint8_t type; // Size of the file, only valid for REG files. Limited to 32-bits. lfs_size_t size; // Name of the file stored as a null-terminated string. Limited to // LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to // reduce RAM. LFS_NAME_MAX is stored in superblock and must be // respected by other littlefs drivers. char name[LFS_NAME_MAX+1]; }; // Custom attribute structure, used to describe custom attributes // committed atomically during file writes. struct lfs_attr { // 8-bit type of attribute, provided by user and used to // identify the attribute uint8_t type; // Pointer to buffer containing the attribute void *buffer; // Size of attribute in bytes, limited to LFS_ATTR_MAX lfs_size_t size; }; /// internal littlefs data structures /// typedef struct lfs_cache { lfs_block_t block; lfs_off_t off; lfs_size_t size; uint8_t *buffer; } lfs_cache_t; typedef struct lfs_mdir { lfs_block_t pair[2]; uint32_t rev; lfs_off_t off; uint32_t etag; uint16_t count; bool erased; bool split; lfs_block_t tail[2]; } lfs_mdir_t; // littlefs directory type typedef struct lfs_dir { struct lfs_dir *next; uint16_t id; uint8_t type; lfs_mdir_t m; lfs_off_t pos; lfs_block_t head[2]; } lfs_dir_t; // littlefs file type typedef struct lfs_file { struct lfs_file *next; uint16_t id; uint8_t type; lfs_mdir_t m; struct lfs_ctz { lfs_block_t head; lfs_size_t size; } ctz; uint32_t flags; lfs_off_t pos; lfs_block_t block; lfs_off_t off; lfs_cache_t cache; #ifndef LFS_FILE_STATICCFG const struct lfs_file_cfg *cfg; #endif } lfs_file_t; typedef struct lfs_superblock { uint32_t version; lfs_size_t block_size; lfs_size_t block_count; lfs_size_t name_limit; lfs_size_t file_limit; lfs_size_t attr_limit; } lfs_superblock_t; typedef struct lfs_gstate { uint32_t tag; lfs_block_t pair[2]; } lfs_gstate_t; // The littlefs filesystem type typedef struct lfs { lfs_cache_t rcache; lfs_cache_t pcache; lfs_block_t root[2]; struct lfs_mlist { struct lfs_mlist *next; uint16_t id; uint8_t type; lfs_mdir_t m; } *mlist; uint32_t seed; lfs_gstate_t gstate; lfs_gstate_t gdisk; lfs_gstate_t gdelta; struct lfs_free { lfs_block_t off; lfs_block_t size; lfs_block_t i; lfs_block_t ack; uint32_t *buffer; } free; #ifndef LFS_STATICCFG const struct lfs_cfg *cfg; #endif lfs_size_t name_limit; lfs_size_t file_limit; lfs_size_t attr_limit; #ifdef LFS_MIGRATE struct lfs1 *lfs1; #endif } lfs_t; /// Filesystem functions /// #if defined(LFS_STATICCFG) // Format a block device with littlefs // // Requires a littlefs object. This clobbers the littlefs object, and does // not leave the filesystem mounted. // // Returns a negative error code on failure. int lfs_format(lfs_t *lfs); #endif #if !defined(LFS_STATICCFG) // Format a block device with littlefs with per-filesystem configuration // // Requires a littlefs object and config struct. This clobbers the littlefs // object, and does not leave the filesystem mounted. The config struct must // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. int lfs_formatcfg(lfs_t *lfs, const struct lfs_cfg *config); #endif #if defined(LFS_STATICCFG) // Mounts littlefs // // Requires a littlefs object and static configuration. // // Returns a negative error code on failure. int lfs_mount(lfs_t *lfs); #endif #if !defined(LFS_STATICCFG) // Mounts a littlefs with per-filesystem configuration // // Requires a littlefs object and config struct. Multiple filesystems // may be mounted simultaneously with multiple littlefs objects. Both // lfs and config must be allocated while mounted. The config struct must // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. int lfs_mountcfg(lfs_t *lfs, const struct lfs_cfg *config); #endif // Unmounts a littlefs // // Does nothing besides releasing any allocated resources. // Returns a negative error code on failure. int lfs_unmount(lfs_t *lfs); /// General operations /// // Removes a file or directory // // If removing a directory, the directory must be empty. // Returns a negative error code on failure. int lfs_remove(lfs_t *lfs, const char *path); // Rename or move a file or directory // // If the destination exists, it must match the source in type. // If the destination is a directory, the directory must be empty. // // Returns a negative error code on failure. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); // Find info about a file or directory // // Fills out the info structure, based on the specified file or directory. // Returns a negative error code on failure. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); // Get a custom attribute // // 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_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); // Set custom attributes // // 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. int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); // Removes a custom attribute // // If an attribute is not found, nothing happens. // // Returns a negative error code on failure. int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); /// File operations /// // Open a file // // The mode that the file is opened in is determined by the flags, which // are values from the enum lfs_open_flags that are bitwise-ored together. // // Returns a negative error code on failure. int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); #if !defined(LFS_FILE_STATICCFG) // Open a file with per-file configuration // // The mode that the file is opened in is determined by the flags, which // are values from the enum lfs_open_flags that are bitwise-ored together. // // The config struct provides additional config options per file as described // above. The config struct must be allocated while the file is open, and the // config struct must be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_cfg *config); #endif // Close a file // // Any pending writes are written out to storage as though // sync had been called and releases any allocated resources. // // Returns a negative error code on failure. int lfs_file_close(lfs_t *lfs, lfs_file_t *file); // Synchronize a file on storage // // Any pending writes are written out to storage. // Returns a negative error code on failure. int lfs_file_sync(lfs_t *lfs, lfs_file_t *file); // Read data from file // // Takes a buffer and size indicating where to store the read data. // Returns the number of bytes read, or a negative error code on failure. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); // Write data to file // // Takes a buffer and size indicating the data to write. The file will not // actually be updated on the storage until either sync or close is called. // // Returns the number of bytes written, or a negative error code on failure. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); // Change the position of the file // // The change in position is determined by the offset and whence flag. // Returns the new position of the file, or a negative error code on failure. lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); // Truncates the size of the file to the specified size // // Returns a negative error code on failure. int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); // Return the position of the file // // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR) // Returns the position of the file, or a negative error code on failure. lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file); // Change the position of the file to the beginning of the file // // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET) // Returns a negative error code on failure. int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); // Return the size of the file // // Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END) // Returns the size of the file, or a negative error code on failure. lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); /// Directory operations /// // Create a directory // // Returns a negative error code on failure. int lfs_mkdir(lfs_t *lfs, const char *path); // Open a directory // // Once open a directory can be used with read to iterate over files. // Returns a negative error code on failure. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); // Close a directory // // Releases any allocated resources. // Returns a negative error code on failure. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); // Read an entry in the directory // // Fills out the info structure, based on the specified file or directory. // Returns a positive value on success, 0 at the end of directory, // or a negative error code on failure. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); // Change the position of the directory // // The new off must be a value previous returned from tell and specifies // an absolute offset in the directory seek. // // Returns a negative error code on failure. int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); // Return the position of the directory // // The returned offset is only meant to be consumed by seek and may not make // sense, but does indicate the current position in the directory iteration. // // Returns the position of the directory, or a negative error code on failure. lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); // Change the position of the directory to the beginning of the directory // // Returns a negative error code on failure. int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); /// Filesystem-level filesystem operations // Finds the current size of the filesystem // // Note: Result is best effort. If files share COW structures, the returned // size may be larger than the filesystem actually is. // // Returns the number of allocated blocks, or a negative error code on failure. lfs_ssize_t lfs_fs_size(lfs_t *lfs); // Traverse through all blocks in use by the filesystem // // The provided callback will be called with each block address that is // currently in use by the filesystem. This can be used to determine which // blocks are in use or how much of the storage is available. // // Returns a negative error code on failure. int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); #if defined(LFS_MIGRATE) && defined(LFS_STATICCFG) // Attempts to migrate a previous version of littlefs // // Behaves similarly to the lfs_format function. Attempts to mount // the previous version of littlefs and update the filesystem so it can be // mounted with the current version of littlefs. // // Requires a littlefs object. This clobbers the littlefs object, and does // not leave the filesystem mounted. // // Returns a negative error code on failure. int lfs_migrate(lfs_t *lfs, const struct lfs_cfg *cfg); #endif #if defined(LFS_MIGRATE) && !defined(LFS_STATICCFG) // Attempts to migrate a previous version of littlefs with per-filesystem // configuration // // Behaves similarly to the lfs_format function. Attempts to mount // the previous version of littlefs and update the filesystem so it can be // mounted with the current version of littlefs. // // Requires a littlefs object and config struct. This clobbers the littlefs // object, and does not leave the filesystem mounted. The config struct must // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. int lfs_migratecfg(lfs_t *lfs, const struct lfs_cfg *cfg); #endif #ifdef __cplusplus } #endif #endif