2017-02-27 00:05:27 +00:00
|
|
|
/*
|
|
|
|
* The little filesystem
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Christopher Haster
|
|
|
|
* Distributed under the MIT license
|
|
|
|
*/
|
|
|
|
#ifndef LFS_H
|
|
|
|
#define LFS_H
|
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2017-02-27 00:05:27 +00:00
|
|
|
|
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
|
|
// Configurable littlefs constants
|
|
|
|
#ifndef LFS_NAME_MAX
|
|
|
|
#define LFS_NAME_MAX 255
|
|
|
|
#endif
|
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
// The littefs constants
|
2017-03-12 20:11:52 +00:00
|
|
|
enum lfs_error {
|
2017-04-24 03:10:16 +00:00
|
|
|
LFS_ERR_OK = 0,
|
|
|
|
LFS_ERR_IO = -5,
|
|
|
|
LFS_ERR_CORRUPT = -77,
|
|
|
|
LFS_ERR_NOENT = -2,
|
|
|
|
LFS_ERR_EXISTS = -17,
|
|
|
|
LFS_ERR_NOTDIR = -20,
|
|
|
|
LFS_ERR_ISDIR = -21,
|
|
|
|
LFS_ERR_INVAL = -22,
|
|
|
|
LFS_ERR_NOSPC = -28,
|
|
|
|
LFS_ERR_NOMEM = -12,
|
2017-03-13 00:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum lfs_type {
|
2017-04-18 03:27:06 +00:00
|
|
|
LFS_TYPE_REG = 0x01,
|
|
|
|
LFS_TYPE_DIR = 0x02,
|
2017-05-14 17:01:45 +00:00
|
|
|
LFS_TYPE_SUPERBLOCK = 0x12,
|
2017-03-12 20:11:52 +00:00
|
|
|
};
|
|
|
|
|
2017-03-20 03:00:56 +00:00
|
|
|
enum lfs_open_flags {
|
2017-04-30 16:19:37 +00:00
|
|
|
// 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,
|
|
|
|
|
|
|
|
// internally used flags
|
|
|
|
LFS_F_DIRTY = 0x10000,
|
|
|
|
LFS_F_WRITING = 0x20000,
|
|
|
|
LFS_F_READING = 0x40000,
|
2017-03-20 03:00:56 +00:00
|
|
|
};
|
|
|
|
|
2017-04-23 04:11:13 +00:00
|
|
|
enum lfs_whence_flags {
|
|
|
|
LFS_SEEK_SET = 0,
|
|
|
|
LFS_SEEK_CUR = 1,
|
|
|
|
LFS_SEEK_END = 2,
|
|
|
|
};
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
// Configuration provided during initialization of the littlefs
|
2017-03-25 23:11:45 +00:00
|
|
|
struct lfs_config {
|
2017-04-22 16:42:05 +00:00
|
|
|
// Opaque user provided context
|
|
|
|
void *context;
|
2017-03-25 23:11:45 +00:00
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
// Read a region in a block
|
|
|
|
int (*read)(const struct lfs_config *c, lfs_block_t block,
|
2017-04-24 04:49:21 +00:00
|
|
|
lfs_off_t off, void *buffer, lfs_size_t size);
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Program a region in a block. The block must have previously
|
|
|
|
// been erased.
|
|
|
|
int (*prog)(const struct lfs_config *c, lfs_block_t block,
|
2017-04-24 04:49:21 +00:00
|
|
|
lfs_off_t off, const void *buffer, lfs_size_t size);
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Erase a block. A block must be erased before being programmed.
|
|
|
|
// The state of an erased block is undefined.
|
|
|
|
int (*erase)(const struct lfs_config *c, lfs_block_t block);
|
|
|
|
|
|
|
|
// Sync the state of the underlying block device
|
|
|
|
int (*sync)(const struct lfs_config *c);
|
|
|
|
|
|
|
|
// Minimum size of a read. This may be larger than the physical
|
|
|
|
// read size to cache reads from the block device.
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_size_t read_size;
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Minimum size of a program. This may be larger than the physical
|
|
|
|
// program size to cache programs to the block device.
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_size_t prog_size;
|
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
// Size of an erasable block.
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_size_t block_size;
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Number of erasable blocks on the device.
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_size_t block_count;
|
2017-04-22 18:30:40 +00:00
|
|
|
|
2017-04-22 19:56:12 +00:00
|
|
|
// Number of blocks to lookahead during block allocation.
|
|
|
|
lfs_size_t lookahead;
|
|
|
|
|
2017-04-22 18:30:40 +00:00
|
|
|
// Optional, statically allocated read buffer. Must be read sized.
|
|
|
|
void *read_buffer;
|
|
|
|
|
|
|
|
// Optional, statically allocated program buffer. Must be program sized.
|
|
|
|
void *prog_buffer;
|
2017-04-22 19:56:12 +00:00
|
|
|
|
|
|
|
// Optional, statically allocated lookahead buffer.
|
|
|
|
// Must be 1 bit per lookahead block.
|
|
|
|
void *lookahead_buffer;
|
2017-04-30 16:19:37 +00:00
|
|
|
|
|
|
|
// Optional, statically allocated buffer for files. Must be program sized.
|
|
|
|
// If enabled, only one file may be opened at a time
|
|
|
|
void *file_buffer;
|
2017-03-25 23:11:45 +00:00
|
|
|
};
|
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
// File info structure
|
2017-03-25 23:11:45 +00:00
|
|
|
struct lfs_info {
|
2017-04-22 16:42:05 +00:00
|
|
|
// Type of the file, either REG or DIR
|
2017-03-25 23:11:45 +00:00
|
|
|
uint8_t type;
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Size of the file, only valid for REG files
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_size_t size;
|
2017-04-22 16:42:05 +00:00
|
|
|
|
|
|
|
// Name of the file stored as a null-terminated string
|
2017-03-25 23:11:45 +00:00
|
|
|
char name[LFS_NAME_MAX+1];
|
|
|
|
};
|
2017-03-05 20:11:52 +00:00
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// littlefs data structures
|
2017-03-05 20:11:52 +00:00
|
|
|
typedef struct lfs_entry {
|
|
|
|
lfs_off_t off;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
struct lfs_disk_entry {
|
2017-03-05 20:11:52 +00:00
|
|
|
uint16_t type;
|
|
|
|
uint16_t len;
|
|
|
|
union {
|
2017-03-25 21:20:31 +00:00
|
|
|
struct {
|
|
|
|
lfs_block_t head;
|
2017-03-05 20:11:52 +00:00
|
|
|
lfs_size_t size;
|
|
|
|
} file;
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t dir[2];
|
2017-03-13 00:41:08 +00:00
|
|
|
} u;
|
2017-03-05 20:11:52 +00:00
|
|
|
} d;
|
|
|
|
} lfs_entry_t;
|
|
|
|
|
2017-04-30 16:19:37 +00:00
|
|
|
typedef struct lfs_cache {
|
|
|
|
lfs_block_t block;
|
|
|
|
lfs_off_t off;
|
|
|
|
uint8_t *buffer;
|
|
|
|
} lfs_cache_t;
|
|
|
|
|
2017-03-20 03:00:56 +00:00
|
|
|
typedef struct lfs_file {
|
2017-04-29 15:22:01 +00:00
|
|
|
struct lfs_file *next;
|
2017-04-24 04:39:50 +00:00
|
|
|
lfs_block_t pair[2];
|
2017-04-30 16:19:37 +00:00
|
|
|
lfs_off_t poff;
|
|
|
|
|
2017-04-24 04:39:50 +00:00
|
|
|
lfs_block_t head;
|
|
|
|
lfs_size_t size;
|
|
|
|
|
|
|
|
uint32_t flags;
|
|
|
|
lfs_off_t pos;
|
2017-04-30 16:19:37 +00:00
|
|
|
lfs_block_t block;
|
|
|
|
lfs_off_t off;
|
|
|
|
lfs_cache_t cache;
|
2017-03-20 03:00:56 +00:00
|
|
|
} lfs_file_t;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
typedef struct lfs_dir {
|
|
|
|
lfs_block_t pair[2];
|
|
|
|
lfs_off_t off;
|
|
|
|
|
2017-04-23 04:11:13 +00:00
|
|
|
lfs_block_t head[2];
|
|
|
|
lfs_off_t pos;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
struct lfs_disk_dir {
|
|
|
|
uint32_t rev;
|
|
|
|
lfs_size_t size;
|
|
|
|
lfs_block_t tail[2];
|
|
|
|
} d;
|
|
|
|
} lfs_dir_t;
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
typedef struct lfs_superblock {
|
2017-04-18 03:27:06 +00:00
|
|
|
lfs_off_t off;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
struct lfs_disk_superblock {
|
2017-04-18 03:27:06 +00:00
|
|
|
uint16_t type;
|
|
|
|
uint16_t len;
|
2017-05-14 17:01:45 +00:00
|
|
|
lfs_block_t root[2];
|
2017-04-18 03:27:06 +00:00
|
|
|
uint32_t version;
|
2017-03-05 20:11:52 +00:00
|
|
|
char magic[8];
|
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t block_count;
|
|
|
|
} d;
|
|
|
|
} lfs_superblock_t;
|
|
|
|
|
2017-04-30 16:19:37 +00:00
|
|
|
typedef struct lfs_free {
|
2017-05-14 17:01:45 +00:00
|
|
|
lfs_block_t end;
|
2017-04-30 16:19:37 +00:00
|
|
|
lfs_block_t start;
|
|
|
|
lfs_block_t off;
|
|
|
|
uint32_t *lookahead;
|
|
|
|
} lfs_free_t;
|
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// littlefs type
|
2017-02-27 00:05:27 +00:00
|
|
|
typedef struct lfs {
|
2017-04-22 16:42:05 +00:00
|
|
|
const struct lfs_config *cfg;
|
2017-04-01 15:44:17 +00:00
|
|
|
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_block_t root[2];
|
2017-04-29 15:22:01 +00:00
|
|
|
lfs_file_t *files;
|
2017-05-14 17:01:45 +00:00
|
|
|
bool deorphaned;
|
2017-02-27 00:05:27 +00:00
|
|
|
|
2017-04-30 16:19:37 +00:00
|
|
|
lfs_cache_t rcache;
|
|
|
|
lfs_cache_t pcache;
|
|
|
|
|
|
|
|
lfs_free_t free;
|
2017-02-27 00:05:27 +00:00
|
|
|
} lfs_t;
|
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// filesystem functions
|
2017-03-25 23:11:45 +00:00
|
|
|
int lfs_format(lfs_t *lfs, const struct lfs_config *config);
|
|
|
|
int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
|
2017-03-25 21:20:31 +00:00
|
|
|
int lfs_unmount(lfs_t *lfs);
|
2017-02-27 00:05:27 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// general operations
|
2017-04-01 15:44:17 +00:00
|
|
|
int lfs_remove(lfs_t *lfs, const char *path);
|
2017-04-15 17:35:20 +00:00
|
|
|
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
|
2017-04-01 17:52:41 +00:00
|
|
|
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
|
2017-04-01 15:44:17 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// directory operations
|
2017-03-25 21:20:31 +00:00
|
|
|
int lfs_mkdir(lfs_t *lfs, const char *path);
|
2017-03-25 23:11:45 +00:00
|
|
|
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);
|
2017-04-23 04:11:13 +00:00
|
|
|
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);
|
2017-03-13 00:41:08 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// file operations
|
2017-03-25 21:20:31 +00:00
|
|
|
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
2017-03-20 03:00:56 +00:00
|
|
|
const char *path, int flags);
|
2017-03-25 21:20:31 +00:00
|
|
|
int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
|
2017-04-23 02:42:22 +00:00
|
|
|
int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
|
2017-03-20 03:00:56 +00:00
|
|
|
lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|
|
|
void *buffer, lfs_size_t size);
|
2017-04-24 02:40:03 +00:00
|
|
|
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|
|
|
const void *buffer, lfs_size_t size);
|
2017-04-23 04:11:13 +00:00
|
|
|
lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
|
|
|
lfs_soff_t off, int whence);
|
|
|
|
lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
|
|
|
|
int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
|
2017-04-24 02:40:03 +00:00
|
|
|
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
|
2017-03-13 00:41:08 +00:00
|
|
|
|
2017-04-24 02:40:03 +00:00
|
|
|
// miscellaneous lfs specific operations
|
2017-04-14 22:33:36 +00:00
|
|
|
int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
|
2017-05-14 17:01:45 +00:00
|
|
|
int lfs_deorphan(lfs_t *lfs);
|
2017-04-14 22:33:36 +00:00
|
|
|
|
2017-04-22 16:42:05 +00:00
|
|
|
|
2017-02-27 00:05:27 +00:00
|
|
|
#endif
|