SpiffsParticleRK
spiffs.h
1 /*
2  * spiffs.h
3  *
4  * Created on: May 26, 2013
5  * Author: petera
6  *
7  * Retrieved from https://github.com/pellepl/spiffs/
8  * Jume 8, 2018
9  *
10  * License: MIT
11  */
12 
13 #ifndef SPIFFS_H_
14 #define SPIFFS_H_
15 #if defined(__cplusplus)
16 extern "C" {
17 #endif
18 
19 #include "spiffs_config.h"
20 
21 #define SPIFFS_OK 0
22 #define SPIFFS_ERR_NOT_MOUNTED -10000
23 #define SPIFFS_ERR_FULL -10001
24 #define SPIFFS_ERR_NOT_FOUND -10002
25 #define SPIFFS_ERR_END_OF_OBJECT -10003
26 #define SPIFFS_ERR_DELETED -10004
27 #define SPIFFS_ERR_NOT_FINALIZED -10005
28 #define SPIFFS_ERR_NOT_INDEX -10006
29 #define SPIFFS_ERR_OUT_OF_FILE_DESCS -10007
30 #define SPIFFS_ERR_FILE_CLOSED -10008
31 #define SPIFFS_ERR_FILE_DELETED -10009
32 #define SPIFFS_ERR_BAD_DESCRIPTOR -10010
33 #define SPIFFS_ERR_IS_INDEX -10011
34 #define SPIFFS_ERR_IS_FREE -10012
35 #define SPIFFS_ERR_INDEX_SPAN_MISMATCH -10013
36 #define SPIFFS_ERR_DATA_SPAN_MISMATCH -10014
37 #define SPIFFS_ERR_INDEX_REF_FREE -10015
38 #define SPIFFS_ERR_INDEX_REF_LU -10016
39 #define SPIFFS_ERR_INDEX_REF_INVALID -10017
40 #define SPIFFS_ERR_INDEX_FREE -10018
41 #define SPIFFS_ERR_INDEX_LU -10019
42 #define SPIFFS_ERR_INDEX_INVALID -10020
43 #define SPIFFS_ERR_NOT_WRITABLE -10021
44 #define SPIFFS_ERR_NOT_READABLE -10022
45 #define SPIFFS_ERR_CONFLICTING_NAME -10023
46 #define SPIFFS_ERR_NOT_CONFIGURED -10024
47 
48 #define SPIFFS_ERR_NOT_A_FS -10025
49 #define SPIFFS_ERR_MOUNTED -10026
50 #define SPIFFS_ERR_ERASE_FAIL -10027
51 #define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028
52 
53 #define SPIFFS_ERR_NO_DELETED_BLOCKS -10029
54 
55 #define SPIFFS_ERR_FILE_EXISTS -10030
56 
57 #define SPIFFS_ERR_NOT_A_FILE -10031
58 #define SPIFFS_ERR_RO_NOT_IMPL -10032
59 #define SPIFFS_ERR_RO_ABORTED_OPERATION -10033
60 #define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034
61 #define SPIFFS_ERR_PROBE_NOT_A_FS -10035
62 #define SPIFFS_ERR_NAME_TOO_LONG -10036
63 
64 #define SPIFFS_ERR_IX_MAP_UNMAPPED -10037
65 #define SPIFFS_ERR_IX_MAP_MAPPED -10038
66 #define SPIFFS_ERR_IX_MAP_BAD_RANGE -10039
67 
68 #define SPIFFS_ERR_SEEK_BOUNDS -10040
69 
70 
71 #define SPIFFS_ERR_INTERNAL -10050
72 
73 #define SPIFFS_ERR_OUT_OF_MEMORY -10060
74 
75 #define SPIFFS_ERR_TEST -10100
76 
77 
78 // spiffs file descriptor index type. must be signed
79 typedef s16_t spiffs_file;
80 // spiffs file descriptor flags
81 typedef u16_t spiffs_flags;
82 // spiffs file mode
83 typedef u16_t spiffs_mode;
84 // object type
85 typedef u8_t spiffs_obj_type;
86 
87 struct spiffs_t;
88 
89 #if SPIFFS_HAL_CALLBACK_EXTRA
90 
91 /* spi read call function type */
92 typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
93 /* spi write call function type */
94 typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);
95 /* spi erase call function type */
96 typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);
97 
98 #else // SPIFFS_HAL_CALLBACK_EXTRA
99 
100 /* spi read call function type */
101 typedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);
102 /* spi write call function type */
103 typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);
104 /* spi erase call function type */
105 typedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);
106 #endif // SPIFFS_HAL_CALLBACK_EXTRA
107 
108 /* file system check callback report operation */
109 typedef enum {
110  SPIFFS_CHECK_LOOKUP = 0,
111  SPIFFS_CHECK_INDEX,
112  SPIFFS_CHECK_PAGE
113 } spiffs_check_type;
114 
115 /* file system check callback report type */
116 typedef enum {
117  SPIFFS_CHECK_PROGRESS = 0,
118  SPIFFS_CHECK_ERROR,
119  SPIFFS_CHECK_FIX_INDEX,
120  SPIFFS_CHECK_FIX_LOOKUP,
121  SPIFFS_CHECK_DELETE_ORPHANED_INDEX,
122  SPIFFS_CHECK_DELETE_PAGE,
123  SPIFFS_CHECK_DELETE_BAD_FILE
124 } spiffs_check_report;
125 
126 /* file system check callback function */
127 #if SPIFFS_HAL_CALLBACK_EXTRA
128 typedef void (*spiffs_check_callback)(struct spiffs_t *fs, spiffs_check_type type, spiffs_check_report report,
129  u32_t arg1, u32_t arg2);
130 #else // SPIFFS_HAL_CALLBACK_EXTRA
131 typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,
132  u32_t arg1, u32_t arg2);
133 #endif // SPIFFS_HAL_CALLBACK_EXTRA
134 
135 /* file system listener callback operation */
136 typedef enum {
137  /* the file has been created */
138  SPIFFS_CB_CREATED = 0,
139  /* the file has been updated or moved to another page */
140  SPIFFS_CB_UPDATED,
141  /* the file has been deleted */
142  SPIFFS_CB_DELETED
143 } spiffs_fileop_type;
144 
145 /* file system listener callback function */
146 typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix);
147 
148 #ifndef SPIFFS_DBG
149 #define SPIFFS_DBG(...) \
150  printf(__VA_ARGS__)
151 #endif
152 #ifndef SPIFFS_GC_DBG
153 #define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
154 #endif
155 #ifndef SPIFFS_CACHE_DBG
156 #define SPIFFS_CACHE_DBG(...) printf(__VA_ARGS__)
157 #endif
158 #ifndef SPIFFS_CHECK_DBG
159 #define SPIFFS_CHECK_DBG(...) printf(__VA_ARGS__)
160 #endif
161 
162 /* Any write to the filehandle is appended to end of the file */
163 #define SPIFFS_APPEND (1<<0)
164 #define SPIFFS_O_APPEND SPIFFS_APPEND
165 /* If the opened file exists, it will be truncated to zero length before opened */
166 #define SPIFFS_TRUNC (1<<1)
167 #define SPIFFS_O_TRUNC SPIFFS_TRUNC
168 /* If the opened file does not exist, it will be created before opened */
169 #define SPIFFS_CREAT (1<<2)
170 #define SPIFFS_O_CREAT SPIFFS_CREAT
171 /* The opened file may only be read */
172 #define SPIFFS_RDONLY (1<<3)
173 #define SPIFFS_O_RDONLY SPIFFS_RDONLY
174 /* The opened file may only be written */
175 #define SPIFFS_WRONLY (1<<4)
176 #define SPIFFS_O_WRONLY SPIFFS_WRONLY
177 /* The opened file may be both read and written */
178 #define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
179 #define SPIFFS_O_RDWR SPIFFS_RDWR
180 /* Any writes to the filehandle will never be cached but flushed directly */
181 #define SPIFFS_DIRECT (1<<5)
182 #define SPIFFS_O_DIRECT SPIFFS_DIRECT
183 /* If SPIFFS_O_CREAT and SPIFFS_O_EXCL are set, SPIFFS_open() shall fail if the file exists */
184 #define SPIFFS_EXCL (1<<6)
185 #define SPIFFS_O_EXCL SPIFFS_EXCL
186 
187 #define SPIFFS_SEEK_SET (0)
188 #define SPIFFS_SEEK_CUR (1)
189 #define SPIFFS_SEEK_END (2)
190 
191 #define SPIFFS_TYPE_FILE (1)
192 #define SPIFFS_TYPE_DIR (2)
193 #define SPIFFS_TYPE_HARD_LINK (3)
194 #define SPIFFS_TYPE_SOFT_LINK (4)
195 
196 #ifndef SPIFFS_LOCK
197 #define SPIFFS_LOCK(fs)
198 #endif
199 
200 #ifndef SPIFFS_UNLOCK
201 #define SPIFFS_UNLOCK(fs)
202 #endif
203 
204 // phys structs
205 
206 // spiffs spi configuration struct
207 typedef struct {
208  // physical read function
209  spiffs_read hal_read_f;
210  // physical write function
211  spiffs_write hal_write_f;
212  // physical erase function
213  spiffs_erase hal_erase_f;
214 #if SPIFFS_SINGLETON == 0
215  // physical size of the spi flash
216  u32_t phys_size;
217  // physical offset in spi flash used for spiffs,
218  // must be on block boundary
219  u32_t phys_addr;
220  // physical size when erasing a block
221  u32_t phys_erase_block;
222 
223  // logical size of a block, must be on physical
224  // block size boundary and must never be less than
225  // a physical block
226  u32_t log_block_size;
227  // logical size of a page, must be at least
228  // log_block_size / 8
229  u32_t log_page_size;
230 
231 #endif
232 #if SPIFFS_FILEHDL_OFFSET
233  // an integer offset added to each file handle
234  u16_t fh_ix_offset;
235 #endif
236 } spiffs_config;
237 
238 typedef struct spiffs_t {
239  // file system configuration
240  spiffs_config cfg;
241  // number of logical blocks
242  u32_t block_count;
243 
244  // cursor for free blocks, block index
245  spiffs_block_ix free_cursor_block_ix;
246  // cursor for free blocks, entry index
247  int free_cursor_obj_lu_entry;
248  // cursor when searching, block index
249  spiffs_block_ix cursor_block_ix;
250  // cursor when searching, entry index
251  int cursor_obj_lu_entry;
252 
253  // primary work buffer, size of a logical page
254  u8_t *lu_work;
255  // secondary work buffer, size of a logical page
256  u8_t *work;
257  // file descriptor memory area
258  u8_t *fd_space;
259  // available file descriptors
260  u32_t fd_count;
261 
262  // last error
263  s32_t err_code;
264 
265  // current number of free blocks
266  u32_t free_blocks;
267  // current number of busy pages
268  u32_t stats_p_allocated;
269  // current number of deleted pages
270  u32_t stats_p_deleted;
271  // flag indicating that garbage collector is cleaning
272  u8_t cleaning;
273  // max erase count amongst all blocks
274  spiffs_obj_id max_erase_count;
275 
276 #if SPIFFS_GC_STATS
277  u32_t stats_gc_runs;
278 #endif
279 
280 #if SPIFFS_CACHE
281  // cache memory
282  void *cache;
283  // cache size
284  u32_t cache_size;
285 #if SPIFFS_CACHE_STATS
286  u32_t cache_hits;
287  u32_t cache_misses;
288 #endif
289 #endif
290 
291  // check callback function
292  spiffs_check_callback check_cb_f;
293  // file callback function
294  spiffs_file_callback file_cb_f;
295  // mounted flag
296  u8_t mounted;
297  // user data
298  void *user_data;
299  // config magic
300  u32_t config_magic;
301 } spiffs;
302 
303 /* spiffs file status struct */
304 typedef struct {
305  spiffs_obj_id obj_id;
306  u32_t size;
307  spiffs_obj_type type;
308  spiffs_page_ix pix;
309  u8_t name[SPIFFS_OBJ_NAME_LEN];
310 #if SPIFFS_OBJ_META_LEN
311  u8_t meta[SPIFFS_OBJ_META_LEN];
312 #endif
313 } spiffs_stat;
314 
316  spiffs_obj_id obj_id;
317  u8_t name[SPIFFS_OBJ_NAME_LEN];
318  spiffs_obj_type type;
319  u32_t size;
320  spiffs_page_ix pix;
321 #if SPIFFS_OBJ_META_LEN
322  u8_t meta[SPIFFS_OBJ_META_LEN];
323 #endif
324 };
325 
326 typedef struct {
327  spiffs *fs;
328  spiffs_block_ix block;
329  int entry;
330 } spiffs_DIR;
331 
332 #if SPIFFS_IX_MAP
333 
334 typedef struct {
335  // buffer with looked up data pixes
336  spiffs_page_ix *map_buf;
337  // precise file byte offset
338  u32_t offset;
339  // start data span index of lookup buffer
340  spiffs_span_ix start_spix;
341  // end data span index of lookup buffer
342  spiffs_span_ix end_spix;
343 } spiffs_ix_map;
344 
345 #endif
346 
347 // functions
348 
349 #if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
350 
380 s32_t SPIFFS_probe_fs(spiffs_config *config);
381 #endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
382 
398 s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
399  u8_t *fd_space, u32_t fd_space_size,
400  void *cache, u32_t cache_size,
401  spiffs_check_callback check_cb_f);
402 
408 void SPIFFS_unmount(spiffs *fs);
409 
415 void SPIFFS_flush(spiffs *fs);
416 
423 s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);
424 
434 spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);
435 
449 spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode);
450 
465 spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode);
466 
475 s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
476 
485 s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
486 
497 s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);
498 
504 s32_t SPIFFS_remove(spiffs *fs, const char *path);
505 
511 s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh);
512 
522 s32_t SPIFFS_truncate(spiffs *fs, const char *path, s32_t len);
523 
534 s32_t SPIFFS_ftruncate(spiffs *fs, spiffs_file fh, s32_t len);
535 
542 s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s);
543 
550 s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s);
551 
557 s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh);
558 
564 s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
565 
572 s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);
573 
574 #if SPIFFS_OBJ_META_LEN
575 
581 s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta);
582 
589 s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta);
590 #endif
591 
596 s32_t SPIFFS_errno(spiffs *fs);
597 
602 void SPIFFS_clearerr(spiffs *fs);
603 
613 spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d);
614 
619 s32_t SPIFFS_closedir(spiffs_DIR *d);
620 
627 struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e);
628 
633 s32_t SPIFFS_check(spiffs *fs);
634 
647 s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used);
648 
662 s32_t SPIFFS_format(spiffs *fs);
663 
668 u8_t SPIFFS_mounted(spiffs *fs);
669 
693 s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
694 
712 s32_t SPIFFS_gc(spiffs *fs, u32_t size);
713 
719 s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
720 
726 s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
727 
742 s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func);
743 
744 #if SPIFFS_IX_MAP
745 
773 s32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,
774  u32_t offset, u32_t len, spiffs_page_ix *map_buf);
775 
787 s32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh);
788 
796 s32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offs);
797 
807 s32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes);
808 
818 s32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries);
819 
820 #endif // SPIFFS_IX_MAP
821 
822 
823 #if SPIFFS_TEST_VISUALISATION
824 
828 s32_t SPIFFS_vis(spiffs *fs);
829 #endif
830 
831 #if SPIFFS_BUFFER_HELP
832 
836 u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs);
837 
838 #if SPIFFS_CACHE
839 
843 u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
844 #endif
845 #endif
846 
847 #if SPIFFS_CACHE
848 #endif
849 #if defined(__cplusplus)
850 }
851 #endif
852 
853 #endif /* SPIFFS_H_ */
Definition: spiffs.h:207
Definition: spiffs.h:326
Definition: spiffs.h:334
Definition: spiffs.h:304
Definition: spiffs.h:238
Definition: spiffs.h:315