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
|
|
|
|
|
|
|
|
#include "lfs_config.h"
|
|
|
|
#include "lfs_bd.h"
|
|
|
|
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
// Data structures
|
2017-03-12 20:11:52 +00:00
|
|
|
enum lfs_error {
|
2017-03-13 00:41:08 +00:00
|
|
|
LFS_ERROR_OK = 0,
|
|
|
|
LFS_ERROR_CORRUPT = -3,
|
|
|
|
LFS_ERROR_NO_ENTRY = -4,
|
|
|
|
LFS_ERROR_EXISTS = -5,
|
|
|
|
LFS_ERROR_NOT_DIR = -6,
|
2017-03-25 23:11:45 +00:00
|
|
|
LFS_ERROR_IS_DIR = -7,
|
|
|
|
LFS_ERROR_INVALID = -8,
|
|
|
|
LFS_ERROR_NO_SPACE = -9,
|
2017-03-13 00:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum lfs_type {
|
|
|
|
LFS_TYPE_REG = 1,
|
|
|
|
LFS_TYPE_DIR = 2,
|
2017-03-12 20:11:52 +00:00
|
|
|
};
|
|
|
|
|
2017-03-20 03:00:56 +00:00
|
|
|
enum lfs_open_flags {
|
|
|
|
LFS_O_RDONLY = 0,
|
|
|
|
LFS_O_WRONLY = 1,
|
|
|
|
LFS_O_RDWR = 2,
|
2017-03-25 22:02:16 +00:00
|
|
|
LFS_O_CREAT = 0x020,
|
|
|
|
LFS_O_EXCL = 0x040,
|
|
|
|
LFS_O_TRUNC = 0x080,
|
|
|
|
LFS_O_APPEND = 0x100,
|
|
|
|
LFS_O_SYNC = 0x200,
|
2017-03-20 03:00:56 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
|
2017-03-25 23:11:45 +00:00
|
|
|
struct lfs_config {
|
|
|
|
lfs_bd_t *bd;
|
|
|
|
const struct lfs_bd_ops *bd_ops;
|
|
|
|
|
|
|
|
lfs_size_t read_size;
|
|
|
|
lfs_size_t prog_size;
|
|
|
|
|
|
|
|
lfs_size_t block_size;
|
|
|
|
lfs_size_t block_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lfs_info {
|
|
|
|
uint8_t type;
|
|
|
|
lfs_size_t size;
|
|
|
|
char name[LFS_NAME_MAX+1];
|
|
|
|
};
|
2017-03-05 20:11:52 +00:00
|
|
|
|
|
|
|
typedef struct lfs_entry {
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t dir[2];
|
2017-03-05 20:11:52 +00:00
|
|
|
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-03-20 03:00:56 +00:00
|
|
|
typedef struct lfs_file {
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t head;
|
2017-03-20 03:00:56 +00:00
|
|
|
lfs_size_t size;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t wblock;
|
|
|
|
uint32_t windex;
|
2017-03-20 03:00:56 +00:00
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t rblock;
|
|
|
|
uint32_t rindex;
|
2017-03-20 03:00:56 +00:00
|
|
|
lfs_off_t roff;
|
|
|
|
|
|
|
|
struct lfs_entry entry;
|
|
|
|
} lfs_file_t;
|
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
typedef struct lfs_dir {
|
|
|
|
lfs_block_t pair[2];
|
|
|
|
lfs_off_t off;
|
|
|
|
|
|
|
|
struct lfs_disk_dir {
|
|
|
|
uint32_t rev;
|
|
|
|
lfs_size_t size;
|
|
|
|
lfs_block_t tail[2];
|
|
|
|
|
|
|
|
struct lfs_disk_free {
|
|
|
|
uint32_t begin;
|
|
|
|
uint32_t end;
|
|
|
|
} free;
|
|
|
|
} d;
|
|
|
|
} lfs_dir_t;
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
typedef struct lfs_superblock {
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t pair[2];
|
|
|
|
struct lfs_disk_superblock {
|
|
|
|
uint32_t rev;
|
2017-03-12 20:11:52 +00:00
|
|
|
uint32_t size;
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t root[2];
|
2017-03-05 20:11:52 +00:00
|
|
|
char magic[8];
|
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t block_count;
|
|
|
|
} d;
|
|
|
|
} lfs_superblock_t;
|
|
|
|
|
|
|
|
// Little filesystem type
|
2017-02-27 00:05:27 +00:00
|
|
|
typedef struct lfs {
|
|
|
|
lfs_bd_t *bd;
|
2017-03-25 21:20:31 +00:00
|
|
|
const struct lfs_bd_ops *bd_ops;
|
|
|
|
|
2017-03-25 23:11:45 +00:00
|
|
|
lfs_block_t root[2];
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_block_t cwd[2];
|
|
|
|
struct lfs_disk_free free;
|
2017-02-27 00:05:27 +00:00
|
|
|
|
2017-03-25 21:20:31 +00:00
|
|
|
lfs_size_t read_size; // size of read
|
|
|
|
lfs_size_t prog_size; // size of program
|
|
|
|
lfs_size_t block_size; // size of erase (block size)
|
|
|
|
lfs_size_t block_count; // number of erasable blocks
|
|
|
|
lfs_size_t words; // number of 32-bit words that can fit in a block
|
2017-02-27 00:05:27 +00:00
|
|
|
} lfs_t;
|
|
|
|
|
2017-03-05 20:11:52 +00:00
|
|
|
// 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-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-03-13 00:41:08 +00:00
|
|
|
|
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-03-20 03:00:56 +00:00
|
|
|
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|
|
|
const void *buffer, lfs_size_t size);
|
|
|
|
lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|
|
|
void *buffer, lfs_size_t size);
|
2017-03-13 00:41:08 +00:00
|
|
|
|
2017-02-27 00:05:27 +00:00
|
|
|
#endif
|