123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655 |
- #ifndef LFS_H
- #define LFS_H
- #include <stdint.h>
- #include <stdbool.h>
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #define LFS_VERSION 0x00020002
- #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
- #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
- #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))
- 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;
- #ifndef LFS_NAME_MAX
- #define LFS_NAME_MAX 255
- #endif
- #ifndef LFS_FILE_MAX
- #define LFS_FILE_MAX 2147483647
- #endif
- #ifndef LFS_ATTR_MAX
- #define LFS_ATTR_MAX 1022
- #endif
- enum lfs_error {
- LFS_ERR_OK = 0,
- LFS_ERR_IO = -5,
- LFS_ERR_CORRUPT = -84,
- LFS_ERR_NOENT = -2,
- LFS_ERR_EXIST = -17,
- LFS_ERR_NOTDIR = -20,
- LFS_ERR_ISDIR = -21,
- LFS_ERR_NOTEMPTY = -39,
- LFS_ERR_BADF = -9,
- LFS_ERR_FBIG = -27,
- LFS_ERR_INVAL = -22,
- LFS_ERR_NOSPC = -28,
- LFS_ERR_NOMEM = -12,
- LFS_ERR_NOATTR = -61,
- LFS_ERR_NAMETOOLONG = -36,
- };
- enum lfs_type {
-
- LFS_TYPE_REG = 0x001,
- LFS_TYPE_DIR = 0x002,
-
- 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,
-
- 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,
-
- LFS_FROM_NOOP = 0x000,
- LFS_FROM_MOVE = 0x101,
- LFS_FROM_USERATTRS = 0x102,
- };
- enum lfs_open_flags {
-
- LFS_O_RDONLY = 1,
- LFS_O_WRONLY = 2,
- LFS_O_RDWR = 3,
- LFS_O_CREAT = 0x0100,
- LFS_O_EXCL = 0x0200,
- LFS_O_TRUNC = 0x0400,
- LFS_O_APPEND = 0x0800,
-
- LFS_F_DIRTY = 0x010000,
- LFS_F_WRITING = 0x020000,
- LFS_F_READING = 0x040000,
- LFS_F_ERRED = 0x080000,
- LFS_F_INLINE = 0x100000,
- LFS_F_OPENED = 0x200000,
- };
- enum lfs_whence_flags {
- LFS_SEEK_SET = 0,
- LFS_SEEK_CUR = 1,
- LFS_SEEK_END = 2,
- };
- struct lfs_config {
-
-
- void *context;
-
-
- int (*read)(const struct lfs_config *c, lfs_block_t block,
- lfs_off_t off, void *buffer, lfs_size_t size);
-
-
-
- int (*prog)(const struct lfs_config *c, lfs_block_t block,
- lfs_off_t off, const void *buffer, lfs_size_t size);
-
-
-
-
- int (*erase)(const struct lfs_config *c, lfs_block_t block);
-
-
- int (*sync)(const struct lfs_config *c);
-
-
- lfs_size_t read_size;
-
-
- lfs_size_t prog_size;
-
-
-
-
- lfs_size_t block_size;
-
- lfs_size_t block_count;
-
-
-
-
-
-
- int32_t block_cycles;
-
-
-
-
-
- lfs_size_t cache_size;
-
-
-
-
- lfs_size_t lookahead_size;
-
-
- void *read_buffer;
-
-
- void *prog_buffer;
-
-
-
- void *lookahead_buffer;
-
-
-
-
- lfs_size_t name_max;
-
-
-
- lfs_size_t file_max;
-
-
-
- lfs_size_t attr_max;
- };
- struct lfs_info {
-
- uint8_t type;
-
- lfs_size_t size;
-
-
-
-
- char name[LFS_NAME_MAX+1];
- };
- struct lfs_attr {
-
-
- uint8_t type;
-
- void *buffer;
-
- lfs_size_t size;
- };
- struct lfs_file_config {
-
-
- void *buffer;
-
-
-
-
-
-
-
-
-
-
-
- struct lfs_attr *attrs;
-
- lfs_size_t attr_count;
- };
- 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;
- 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;
- 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;
- const struct lfs_file_config *cfg;
- } lfs_file_t;
- typedef struct lfs_superblock {
- uint32_t version;
- lfs_size_t block_size;
- lfs_size_t block_count;
- lfs_size_t name_max;
- lfs_size_t file_max;
- lfs_size_t attr_max;
- } lfs_superblock_t;
- typedef struct lfs_gstate {
- uint32_t tag;
- lfs_block_t pair[2];
- } lfs_gstate_t;
- 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;
- const struct lfs_config *cfg;
- lfs_size_t name_max;
- lfs_size_t file_max;
- lfs_size_t attr_max;
- #ifdef LFS_MIGRATE
- struct lfs1 *lfs1;
- #endif
- } lfs_t;
- int lfs_format(lfs_t *lfs, const struct lfs_config *config);
- int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
- int lfs_unmount(lfs_t *lfs);
- int lfs_remove(lfs_t *lfs, const char *path);
- int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
- int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
- lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
- uint8_t type, void *buffer, lfs_size_t size);
- int lfs_setattr(lfs_t *lfs, const char *path,
- uint8_t type, const void *buffer, lfs_size_t size);
- int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
- int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
- const char *path, int flags);
- int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
- const char *path, int flags,
- const struct lfs_file_config *config);
- int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
- int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
- lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
- void *buffer, lfs_size_t size);
- lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
- const void *buffer, lfs_size_t size);
- lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
- lfs_soff_t off, int whence);
- int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
- lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
- int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
- lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
- int lfs_mkdir(lfs_t *lfs, const char *path);
- int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
- int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
- int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
- int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
- lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
- int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
- lfs_ssize_t lfs_fs_size(lfs_t *lfs);
- int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
- #ifdef LFS_MIGRATE
- int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg);
- #endif
- #ifdef __cplusplus
- }
- #endif
- #endif
|