#include "spiffs_config.h"
Go to the source code of this file.
Data Structures | |
struct | spiffs_config |
struct | spiffs_t |
struct | spiffs_stat |
struct | spiffs_dirent |
struct | spiffs_DIR |
Typedefs | |
typedef s16_t | spiffs_file |
typedef u16_t | spiffs_flags |
typedef u16_t | spiffs_mode |
typedef u8_t | spiffs_obj_type |
typedef s32_t(* | spiffs_read) (u32_t addr, u32_t size, u8_t *dst) |
typedef s32_t(* | spiffs_write) (u32_t addr, u32_t size, u8_t *src) |
typedef s32_t(* | spiffs_erase) (u32_t addr, u32_t size) |
typedef void(* | spiffs_check_callback) (spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2) |
typedef void(* | spiffs_file_callback) (struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix) |
typedef struct spiffs_t | spiffs |
#define SPIFFS_OK 0 |
#define SPIFFS_ERR_NOT_MOUNTED -10000 |
#define SPIFFS_ERR_FULL -10001 |
#define SPIFFS_ERR_NOT_FOUND -10002 |
#define SPIFFS_ERR_END_OF_OBJECT -10003 |
#define SPIFFS_ERR_DELETED -10004 |
#define SPIFFS_ERR_NOT_FINALIZED -10005 |
#define SPIFFS_ERR_NOT_INDEX -10006 |
#define SPIFFS_ERR_OUT_OF_FILE_DESCS -10007 |
#define SPIFFS_ERR_FILE_CLOSED -10008 |
#define SPIFFS_ERR_FILE_DELETED -10009 |
#define SPIFFS_ERR_BAD_DESCRIPTOR -10010 |
#define SPIFFS_ERR_IS_INDEX -10011 |
#define SPIFFS_ERR_IS_FREE -10012 |
#define SPIFFS_ERR_INDEX_SPAN_MISMATCH -10013 |
#define SPIFFS_ERR_DATA_SPAN_MISMATCH -10014 |
#define SPIFFS_ERR_INDEX_REF_FREE -10015 |
#define SPIFFS_ERR_INDEX_REF_LU -10016 |
#define SPIFFS_ERR_INDEX_REF_INVALID -10017 |
#define SPIFFS_ERR_INDEX_FREE -10018 |
#define SPIFFS_ERR_INDEX_LU -10019 |
#define SPIFFS_ERR_INDEX_INVALID -10020 |
#define SPIFFS_ERR_NOT_WRITABLE -10021 |
#define SPIFFS_ERR_NOT_READABLE -10022 |
#define SPIFFS_ERR_CONFLICTING_NAME -10023 |
#define SPIFFS_ERR_NOT_CONFIGURED -10024 |
#define SPIFFS_ERR_NOT_A_FS -10025 |
#define SPIFFS_ERR_MOUNTED -10026 |
#define SPIFFS_ERR_ERASE_FAIL -10027 |
#define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028 |
#define SPIFFS_ERR_NO_DELETED_BLOCKS -10029 |
#define SPIFFS_ERR_FILE_EXISTS -10030 |
#define SPIFFS_ERR_NOT_A_FILE -10031 |
#define SPIFFS_ERR_RO_NOT_IMPL -10032 |
#define SPIFFS_ERR_RO_ABORTED_OPERATION -10033 |
#define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034 |
#define SPIFFS_ERR_PROBE_NOT_A_FS -10035 |
#define SPIFFS_ERR_NAME_TOO_LONG -10036 |
#define SPIFFS_ERR_IX_MAP_UNMAPPED -10037 |
#define SPIFFS_ERR_IX_MAP_MAPPED -10038 |
#define SPIFFS_ERR_IX_MAP_BAD_RANGE -10039 |
#define SPIFFS_ERR_SEEK_BOUNDS -10040 |
#define SPIFFS_ERR_INTERNAL -10050 |
#define SPIFFS_ERR_TEST -10100 |
#define SPIFFS_DBG | ( | ... | ) | printf(__VA_ARGS__) |
#define SPIFFS_GC_DBG | ( | ... | ) | printf(__VA_ARGS__) |
#define SPIFFS_CACHE_DBG | ( | ... | ) | printf(__VA_ARGS__) |
#define SPIFFS_CHECK_DBG | ( | ... | ) | printf(__VA_ARGS__) |
#define SPIFFS_APPEND (1<<0) |
#define SPIFFS_O_APPEND SPIFFS_APPEND |
#define SPIFFS_TRUNC (1<<1) |
#define SPIFFS_O_TRUNC SPIFFS_TRUNC |
#define SPIFFS_CREAT (1<<2) |
#define SPIFFS_O_CREAT SPIFFS_CREAT |
#define SPIFFS_RDONLY (1<<3) |
#define SPIFFS_O_RDONLY SPIFFS_RDONLY |
#define SPIFFS_WRONLY (1<<4) |
#define SPIFFS_O_WRONLY SPIFFS_WRONLY |
#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY) |
#define SPIFFS_O_RDWR SPIFFS_RDWR |
#define SPIFFS_DIRECT (1<<5) |
#define SPIFFS_O_DIRECT SPIFFS_DIRECT |
#define SPIFFS_EXCL (1<<6) |
#define SPIFFS_O_EXCL SPIFFS_EXCL |
#define SPIFFS_SEEK_SET (0) |
#define SPIFFS_SEEK_CUR (1) |
#define SPIFFS_SEEK_END (2) |
#define SPIFFS_TYPE_FILE (1) |
#define SPIFFS_TYPE_DIR (2) |
#define SPIFFS_TYPE_HARD_LINK (3) |
#define SPIFFS_TYPE_SOFT_LINK (4) |
#define SPIFFS_LOCK | ( | fs | ) |
#define SPIFFS_UNLOCK | ( | fs | ) |
typedef s16_t spiffs_file |
typedef u16_t spiffs_flags |
typedef u16_t spiffs_mode |
typedef u8_t spiffs_obj_type |
typedef s32_t(* spiffs_read) (u32_t addr, u32_t size, u8_t *dst) |
typedef s32_t(* spiffs_write) (u32_t addr, u32_t size, u8_t *src) |
typedef s32_t(* spiffs_erase) (u32_t addr, u32_t size) |
typedef void(* spiffs_check_callback) (spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2) |
typedef void(* spiffs_file_callback) (struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix) |
enum spiffs_check_type |
enum spiffs_check_report |
enum spiffs_fileop_type |
s32_t SPIFFS_mount | ( | spiffs * | fs, |
spiffs_config * | config, | ||
u8_t * | work, | ||
u8_t * | fd_space, | ||
u32_t | fd_space_size, | ||
void * | cache, | ||
u32_t | cache_size, | ||
spiffs_check_callback | check_cb_f | ||
) |
Initializes the file system dynamic parameters and mounts the filesystem. If SPIFFS_USE_MAGIC is enabled the mounting may fail with SPIFFS_ERR_NOT_A_FS if the flash does not contain a recognizable file system. In this case, SPIFFS_format must be called prior to remounting.
fs | the file system struct |
config | the physical and logical configuration of the file system |
work | a memory work buffer comprising 2*config->log_page_size bytes used throughout all file system operations |
fd_space | memory for file descriptors |
fd_space_size | memory size of file descriptors |
cache | memory for cache, may be null |
cache_size | memory size of cache |
check_cb_f | callback function for reporting during consistency checks |
void SPIFFS_unmount | ( | spiffs * | fs | ) |
Unmounts the file system. All file handles will be flushed of any cached writes and closed.
fs | the file system struct |
s32_t SPIFFS_creat | ( | spiffs * | fs, |
const char * | path, | ||
spiffs_mode | mode | ||
) |
Creates a new file.
fs | the file system struct |
path | the path of the new file |
mode | ignored, for posix compliance |
spiffs_file SPIFFS_open | ( | spiffs * | fs, |
const char * | path, | ||
spiffs_flags | flags, | ||
spiffs_mode | mode | ||
) |
Opens/creates a file.
fs | the file system struct |
path | the path of the new file |
flags | the flags for the open command, can be combinations of SPIFFS_O_APPEND, SPIFFS_O_TRUNC, SPIFFS_O_CREAT, SPIFFS_O_RDONLY, SPIFFS_O_WRONLY, SPIFFS_O_RDWR, SPIFFS_O_DIRECT, SPIFFS_O_EXCL |
mode | ignored, for posix compliance |
spiffs_file SPIFFS_open_by_dirent | ( | spiffs * | fs, |
struct spiffs_dirent * | e, | ||
spiffs_flags | flags, | ||
spiffs_mode | mode | ||
) |
Opens a file by given dir entry. Optimization purposes, when traversing a file system with SPIFFS_readdir a normal SPIFFS_open would need to traverse the filesystem again to find the file, whilst SPIFFS_open_by_dirent already knows where the file resides.
fs | the file system struct |
e | the dir entry to the file |
flags | the flags for the open command, can be combinations of SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY, SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT. SPIFFS_CREAT will have no effect in this case. |
mode | ignored, for posix compliance |
spiffs_file SPIFFS_open_by_page | ( | spiffs * | fs, |
spiffs_page_ix | page_ix, | ||
spiffs_flags | flags, | ||
spiffs_mode | mode | ||
) |
Opens a file by given page index. Optimization purposes, opens a file by directly pointing to the page index in the spi flash. If the page index does not point to a file header SPIFFS_ERR_NOT_A_FILE is returned.
fs | the file system struct |
page_ix | the page index |
flags | the flags for the open command, can be combinations of SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY, SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT. SPIFFS_CREAT will have no effect in this case. |
mode | ignored, for posix compliance |
s32_t SPIFFS_read | ( | spiffs * | fs, |
spiffs_file | fh, | ||
void * | buf, | ||
s32_t | len | ||
) |
Reads from given filehandle.
fs | the file system struct |
fh | the filehandle |
buf | where to put read data |
len | how much to read |
s32_t SPIFFS_write | ( | spiffs * | fs, |
spiffs_file | fh, | ||
void * | buf, | ||
s32_t | len | ||
) |
Writes to given filehandle.
fs | the file system struct |
fh | the filehandle |
buf | the data to write |
len | how much to write |
s32_t SPIFFS_lseek | ( | spiffs * | fs, |
spiffs_file | fh, | ||
s32_t | offs, | ||
int | whence | ||
) |
Moves the read/write file offset. Resulting offset is returned or negative if error. lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset.
fs | the file system struct |
fh | the filehandle |
offs | how much/where to move the offset |
whence | if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative |
s32_t SPIFFS_remove | ( | spiffs * | fs, |
const char * | path | ||
) |
Removes a file by path
fs | the file system struct |
path | the path of the file to remove |
s32_t SPIFFS_fremove | ( | spiffs * | fs, |
spiffs_file | fh | ||
) |
Removes a file by filehandle
fs | the file system struct |
fh | the filehandle of the file to remove |
s32_t SPIFFS_stat | ( | spiffs * | fs, |
const char * | path, | ||
spiffs_stat * | s | ||
) |
Gets file status by path
fs | the file system struct |
path | the path of the file to stat |
s | the stat struct to populate |
s32_t SPIFFS_fstat | ( | spiffs * | fs, |
spiffs_file | fh, | ||
spiffs_stat * | s | ||
) |
Gets file status by filehandle
fs | the file system struct |
fh | the filehandle of the file to stat |
s | the stat struct to populate |
s32_t SPIFFS_fflush | ( | spiffs * | fs, |
spiffs_file | fh | ||
) |
Flushes all pending write operations from cache for given file
fs | the file system struct |
fh | the filehandle of the file to flush |
s32_t SPIFFS_close | ( | spiffs * | fs, |
spiffs_file | fh | ||
) |
Closes a filehandle. If there are pending write operations, these are finalized before closing.
fs | the file system struct |
fh | the filehandle of the file to close |
s32_t SPIFFS_rename | ( | spiffs * | fs, |
const char * | old, | ||
const char * | newPath | ||
) |
Renames a file
fs | the file system struct |
old | path of file to rename |
newPath | new path of file |
s32_t SPIFFS_errno | ( | spiffs * | fs | ) |
Returns last error of last file operation.
fs | the file system struct |
void SPIFFS_clearerr | ( | spiffs * | fs | ) |
Clears last error.
fs | the file system struct |
spiffs_DIR* SPIFFS_opendir | ( | spiffs * | fs, |
const char * | name, | ||
spiffs_DIR * | d | ||
) |
Opens a directory stream corresponding to the given name. The stream is positioned at the first entry in the directory. On hydrogen builds the name argument is ignored as hydrogen builds always correspond to a flat file structure - no directories.
fs | the file system struct |
name | the name of the directory |
d | pointer the directory stream to be populated |
s32_t SPIFFS_closedir | ( | spiffs_DIR * | d | ) |
Closes a directory stream
d | the directory stream to close |
struct spiffs_dirent* SPIFFS_readdir | ( | spiffs_DIR * | d, |
struct spiffs_dirent * | e | ||
) |
Reads a directory into given spifs_dirent struct.
d | pointer to the directory stream |
e | the dirent struct to be populated |
s32_t SPIFFS_check | ( | spiffs * | fs | ) |
Runs a consistency check on given filesystem.
fs | the file system struct |
s32_t SPIFFS_info | ( | spiffs * | fs, |
u32_t * | total, | ||
u32_t * | used | ||
) |
Returns number of total bytes available and number of used bytes. This is an estimation, and depends on if there a many files with little data or few files with much data. NB: If used number of bytes exceeds total bytes, a SPIFFS_check should run. This indicates a power loss in midst of things. In worst case (repeated powerlosses in mending or gc) you might have to delete some files.
fs | the file system struct |
total | total number of bytes in filesystem |
used | used number of bytes in filesystem |
s32_t SPIFFS_format | ( | spiffs * | fs | ) |
Formats the entire file system. All data will be lost. The filesystem must not be mounted when calling this.
NB: formatting is awkward. Due to backwards compatibility, SPIFFS_mount MUST be called prior to formatting in order to configure the filesystem. If SPIFFS_mount succeeds, SPIFFS_unmount must be called before calling SPIFFS_format. If SPIFFS_mount fails, SPIFFS_format can be called directly without calling SPIFFS_unmount first.
fs | the file system struct |
u8_t SPIFFS_mounted | ( | spiffs * | fs | ) |
Returns nonzero if spiffs is mounted, or zero if unmounted.
fs | the file system struct |
s32_t SPIFFS_gc_quick | ( | spiffs * | fs, |
u16_t | max_free_pages | ||
) |
Tries to find a block where most or all pages are deleted, and erase that block if found. Does not care for wear levelling. Will not move pages around. If parameter max_free_pages are set to 0, only blocks with only deleted pages will be selected.
NB: the garbage collector is automatically called when spiffs needs free pages. The reason for this function is to give possibility to do background tidying when user knows the system is idle.
Use with care.
Setting max_free_pages to anything larger than zero will eventually wear flash more as a block containing free pages can be erased.
Will set err_no to SPIFFS_OK if a block was found and erased, SPIFFS_ERR_NO_DELETED_BLOCK if no matching block was found, or other error.
fs | the file system struct |
max_free_pages | maximum number allowed free pages in block |
s32_t SPIFFS_gc | ( | spiffs * | fs, |
u32_t | size | ||
) |
Will try to make room for given amount of bytes in the filesystem by moving pages and erasing blocks. If it is physically impossible, err_no will be set to SPIFFS_ERR_FULL. If there already is this amount (or more) of free space, SPIFFS_gc will silently return. It is recommended to call SPIFFS_info before invoking this method in order to determine what amount of bytes to give.
NB: the garbage collector is automatically called when spiffs needs free pages. The reason for this function is to give possibility to do background tidying when user knows the system is idle.
Use with care.
fs | the file system struct |
size | amount of bytes that should be freed |
s32_t SPIFFS_eof | ( | spiffs * | fs, |
spiffs_file | fh | ||
) |
Check if EOF reached.
fs | the file system struct |
fh | the filehandle of the file to check |
s32_t SPIFFS_tell | ( | spiffs * | fs, |
spiffs_file | fh | ||
) |
Get position in file.
fs | the file system struct |
fh | the filehandle of the file to check |
s32_t SPIFFS_set_file_callback_func | ( | spiffs * | fs, |
spiffs_file_callback | cb_func | ||
) |
Registers a callback function that keeps track on operations on file headers. Do note, that this callback is called from within internal spiffs mechanisms. Any operations on the actual file system being callbacked from in this callback will mess things up for sure - do not do this. This can be used to track where files are and move around during garbage collection, which in turn can be used to build location tables in ram. Used in conjuction with SPIFFS_open_by_page this may improve performance when opening a lot of files. Must be invoked after mount.
fs | the file system struct |
cb_func | the callback on file operations |