You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

814 regels
28 KiB

  1. /*
  2. * spiffs.h
  3. *
  4. * Created on: May 26, 2013
  5. * Author: petera
  6. */
  7. #ifndef SPIFFS_H_
  8. #define SPIFFS_H_
  9. #if defined(__cplusplus)
  10. extern "C" {
  11. #endif
  12. #include "spiffs_config.h"
  13. #define SPIFFS_OK 0
  14. #define SPIFFS_ERR_NOT_MOUNTED -10000
  15. #define SPIFFS_ERR_FULL -10001
  16. #define SPIFFS_ERR_NOT_FOUND -10002
  17. #define SPIFFS_ERR_END_OF_OBJECT -10003
  18. #define SPIFFS_ERR_DELETED -10004
  19. #define SPIFFS_ERR_NOT_FINALIZED -10005
  20. #define SPIFFS_ERR_NOT_INDEX -10006
  21. #define SPIFFS_ERR_OUT_OF_FILE_DESCS -10007
  22. #define SPIFFS_ERR_FILE_CLOSED -10008
  23. #define SPIFFS_ERR_FILE_DELETED -10009
  24. #define SPIFFS_ERR_BAD_DESCRIPTOR -10010
  25. #define SPIFFS_ERR_IS_INDEX -10011
  26. #define SPIFFS_ERR_IS_FREE -10012
  27. #define SPIFFS_ERR_INDEX_SPAN_MISMATCH -10013
  28. #define SPIFFS_ERR_DATA_SPAN_MISMATCH -10014
  29. #define SPIFFS_ERR_INDEX_REF_FREE -10015
  30. #define SPIFFS_ERR_INDEX_REF_LU -10016
  31. #define SPIFFS_ERR_INDEX_REF_INVALID -10017
  32. #define SPIFFS_ERR_INDEX_FREE -10018
  33. #define SPIFFS_ERR_INDEX_LU -10019
  34. #define SPIFFS_ERR_INDEX_INVALID -10020
  35. #define SPIFFS_ERR_NOT_WRITABLE -10021
  36. #define SPIFFS_ERR_NOT_READABLE -10022
  37. #define SPIFFS_ERR_CONFLICTING_NAME -10023
  38. #define SPIFFS_ERR_NOT_CONFIGURED -10024
  39. #define SPIFFS_ERR_NOT_A_FS -10025
  40. #define SPIFFS_ERR_MOUNTED -10026
  41. #define SPIFFS_ERR_ERASE_FAIL -10027
  42. #define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028
  43. #define SPIFFS_ERR_NO_DELETED_BLOCKS -10029
  44. #define SPIFFS_ERR_FILE_EXISTS -10030
  45. #define SPIFFS_ERR_NOT_A_FILE -10031
  46. #define SPIFFS_ERR_RO_NOT_IMPL -10032
  47. #define SPIFFS_ERR_RO_ABORTED_OPERATION -10033
  48. #define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034
  49. #define SPIFFS_ERR_PROBE_NOT_A_FS -10035
  50. #define SPIFFS_ERR_NAME_TOO_LONG -10036
  51. #define SPIFFS_ERR_IX_MAP_UNMAPPED -10037
  52. #define SPIFFS_ERR_IX_MAP_MAPPED -10038
  53. #define SPIFFS_ERR_IX_MAP_BAD_RANGE -10039
  54. #define SPIFFS_ERR_INTERNAL -10050
  55. #define SPIFFS_ERR_TEST -10100
  56. // spiffs file descriptor index type. must be signed
  57. typedef s16_t spiffs_file;
  58. // spiffs file descriptor flags
  59. typedef u16_t spiffs_flags;
  60. // spiffs file mode
  61. typedef u16_t spiffs_mode;
  62. // object type
  63. typedef u8_t spiffs_obj_type;
  64. struct spiffs_t;
  65. #if SPIFFS_HAL_CALLBACK_EXTRA
  66. /* spi read call function type */
  67. typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
  68. /* spi write call function type */
  69. typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);
  70. /* spi erase call function type */
  71. typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);
  72. #else // SPIFFS_HAL_CALLBACK_EXTRA
  73. /* spi read call function type */
  74. typedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);
  75. /* spi write call function type */
  76. typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);
  77. /* spi erase call function type */
  78. typedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);
  79. #endif // SPIFFS_HAL_CALLBACK_EXTRA
  80. /* file system check callback report operation */
  81. typedef enum {
  82. SPIFFS_CHECK_LOOKUP = 0,
  83. SPIFFS_CHECK_INDEX,
  84. SPIFFS_CHECK_PAGE
  85. } spiffs_check_type;
  86. /* file system check callback report type */
  87. typedef enum {
  88. SPIFFS_CHECK_PROGRESS = 0,
  89. SPIFFS_CHECK_ERROR,
  90. SPIFFS_CHECK_FIX_INDEX,
  91. SPIFFS_CHECK_FIX_LOOKUP,
  92. SPIFFS_CHECK_DELETE_ORPHANED_INDEX,
  93. SPIFFS_CHECK_DELETE_PAGE,
  94. SPIFFS_CHECK_DELETE_BAD_FILE
  95. } spiffs_check_report;
  96. /* file system check callback function */
  97. #if SPIFFS_HAL_CALLBACK_EXTRA
  98. typedef void (*spiffs_check_callback)(struct spiffs_t *fs, spiffs_check_type type, spiffs_check_report report,
  99. u32_t arg1, u32_t arg2);
  100. #else // SPIFFS_HAL_CALLBACK_EXTRA
  101. typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,
  102. u32_t arg1, u32_t arg2);
  103. #endif // SPIFFS_HAL_CALLBACK_EXTRA
  104. /* file system listener callback operation */
  105. typedef enum {
  106. /* the file has been created */
  107. SPIFFS_CB_CREATED = 0,
  108. /* the file has been updated or moved to another page */
  109. SPIFFS_CB_UPDATED,
  110. /* the file has been deleted */
  111. SPIFFS_CB_DELETED
  112. } spiffs_fileop_type;
  113. /* file system listener callback function */
  114. typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix);
  115. #ifndef SPIFFS_DBG
  116. #define SPIFFS_DBG(...) \
  117. printf(__VA_ARGS__)
  118. #endif
  119. #ifndef SPIFFS_GC_DBG
  120. #define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
  121. #endif
  122. #ifndef SPIFFS_CACHE_DBG
  123. #define SPIFFS_CACHE_DBG(...) printf(__VA_ARGS__)
  124. #endif
  125. #ifndef SPIFFS_CHECK_DBG
  126. #define SPIFFS_CHECK_DBG(...) printf(__VA_ARGS__)
  127. #endif
  128. /* Any write to the filehandle is appended to end of the file */
  129. #define SPIFFS_APPEND (1<<0)
  130. #define SPIFFS_O_APPEND SPIFFS_APPEND
  131. /* If the opened file exists, it will be truncated to zero length before opened */
  132. #define SPIFFS_TRUNC (1<<1)
  133. #define SPIFFS_O_TRUNC SPIFFS_TRUNC
  134. /* If the opened file does not exist, it will be created before opened */
  135. #define SPIFFS_CREAT (1<<2)
  136. #define SPIFFS_O_CREAT SPIFFS_CREAT
  137. /* The opened file may only be read */
  138. #define SPIFFS_RDONLY (1<<3)
  139. #define SPIFFS_O_RDONLY SPIFFS_RDONLY
  140. /* The opened file may only be written */
  141. #define SPIFFS_WRONLY (1<<4)
  142. #define SPIFFS_O_WRONLY SPIFFS_WRONLY
  143. /* The opened file may be both read and written */
  144. #define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
  145. #define SPIFFS_O_RDWR SPIFFS_RDWR
  146. /* Any writes to the filehandle will never be cached but flushed directly */
  147. #define SPIFFS_DIRECT (1<<5)
  148. #define SPIFFS_O_DIRECT SPIFFS_DIRECT
  149. /* If SPIFFS_O_CREAT and SPIFFS_O_EXCL are set, SPIFFS_open() shall fail if the file exists */
  150. #define SPIFFS_EXCL (1<<6)
  151. #define SPIFFS_O_EXCL SPIFFS_EXCL
  152. #define SPIFFS_SEEK_SET (0)
  153. #define SPIFFS_SEEK_CUR (1)
  154. #define SPIFFS_SEEK_END (2)
  155. #define SPIFFS_TYPE_FILE (1)
  156. #define SPIFFS_TYPE_DIR (2)
  157. #define SPIFFS_TYPE_HARD_LINK (3)
  158. #define SPIFFS_TYPE_SOFT_LINK (4)
  159. #ifndef SPIFFS_LOCK
  160. #define SPIFFS_LOCK(fs)
  161. #endif
  162. #ifndef SPIFFS_UNLOCK
  163. #define SPIFFS_UNLOCK(fs)
  164. #endif
  165. // phys structs
  166. // spiffs spi configuration struct
  167. typedef struct {
  168. // physical read function
  169. spiffs_read hal_read_f;
  170. // physical write function
  171. spiffs_write hal_write_f;
  172. // physical erase function
  173. spiffs_erase hal_erase_f;
  174. #if SPIFFS_SINGLETON == 0
  175. // physical size of the spi flash
  176. u32_t phys_size;
  177. // physical offset in spi flash used for spiffs,
  178. // must be on block boundary
  179. u32_t phys_addr;
  180. // physical size when erasing a block
  181. u32_t phys_erase_block;
  182. // logical size of a block, must be on physical
  183. // block size boundary and must never be less than
  184. // a physical block
  185. u32_t log_block_size;
  186. // logical size of a page, must be at least
  187. // log_block_size / 8
  188. u32_t log_page_size;
  189. #endif
  190. #if SPIFFS_FILEHDL_OFFSET
  191. // an integer offset added to each file handle
  192. u16_t fh_ix_offset;
  193. #endif
  194. } spiffs_config;
  195. typedef struct spiffs_t {
  196. // file system configuration
  197. spiffs_config cfg;
  198. // number of logical blocks
  199. u32_t block_count;
  200. // cursor for free blocks, block index
  201. spiffs_block_ix free_cursor_block_ix;
  202. // cursor for free blocks, entry index
  203. int free_cursor_obj_lu_entry;
  204. // cursor when searching, block index
  205. spiffs_block_ix cursor_block_ix;
  206. // cursor when searching, entry index
  207. int cursor_obj_lu_entry;
  208. // primary work buffer, size of a logical page
  209. u8_t *lu_work;
  210. // secondary work buffer, size of a logical page
  211. u8_t *work;
  212. // file descriptor memory area
  213. u8_t *fd_space;
  214. // available file descriptors
  215. u32_t fd_count;
  216. // last error
  217. s32_t err_code;
  218. // current number of free blocks
  219. u32_t free_blocks;
  220. // current number of busy pages
  221. u32_t stats_p_allocated;
  222. // current number of deleted pages
  223. u32_t stats_p_deleted;
  224. // flag indicating that garbage collector is cleaning
  225. u8_t cleaning;
  226. // max erase count amongst all blocks
  227. spiffs_obj_id max_erase_count;
  228. #if SPIFFS_GC_STATS
  229. u32_t stats_gc_runs;
  230. #endif
  231. #if SPIFFS_CACHE
  232. // cache memory
  233. void *cache;
  234. // cache size
  235. u32_t cache_size;
  236. #if SPIFFS_CACHE_STATS
  237. u32_t cache_hits;
  238. u32_t cache_misses;
  239. #endif
  240. #endif
  241. // check callback function
  242. spiffs_check_callback check_cb_f;
  243. // file callback function
  244. spiffs_file_callback file_cb_f;
  245. // mounted flag
  246. u8_t mounted;
  247. // user data
  248. void *user_data;
  249. // config magic
  250. u32_t config_magic;
  251. } spiffs;
  252. /* spiffs file status struct */
  253. typedef struct {
  254. spiffs_obj_id obj_id;
  255. u32_t size;
  256. spiffs_obj_type type;
  257. spiffs_page_ix pix;
  258. u8_t name[SPIFFS_OBJ_NAME_LEN];
  259. #if SPIFFS_OBJ_META_LEN
  260. u8_t meta[SPIFFS_OBJ_META_LEN];
  261. #endif
  262. } spiffs_stat;
  263. struct spiffs_dirent {
  264. spiffs_obj_id obj_id;
  265. u8_t name[SPIFFS_OBJ_NAME_LEN];
  266. spiffs_obj_type type;
  267. u32_t size;
  268. spiffs_page_ix pix;
  269. #if SPIFFS_OBJ_META_LEN
  270. u8_t meta[SPIFFS_OBJ_META_LEN];
  271. #endif
  272. };
  273. typedef struct {
  274. spiffs *fs;
  275. spiffs_block_ix block;
  276. int entry;
  277. } spiffs_DIR;
  278. #if SPIFFS_IX_MAP
  279. typedef struct {
  280. // buffer with looked up data pixes
  281. spiffs_page_ix *map_buf;
  282. // precise file byte offset
  283. u32_t offset;
  284. // start data span index of lookup buffer
  285. spiffs_span_ix start_spix;
  286. // end data span index of lookup buffer
  287. spiffs_span_ix end_spix;
  288. } spiffs_ix_map;
  289. #endif
  290. // functions
  291. #if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  292. /**
  293. * Special function. This takes a spiffs config struct and returns the number
  294. * of blocks this file system was formatted with. This function relies on
  295. * that following info is set correctly in given config struct:
  296. *
  297. * phys_addr, log_page_size, and log_block_size.
  298. *
  299. * Also, hal_read_f must be set in the config struct.
  300. *
  301. * One must be sure of the correct page size and that the physical address is
  302. * correct in the probed file system when calling this function. It is not
  303. * checked if the phys_addr actually points to the start of the file system,
  304. * so one might get a false positive if entering a phys_addr somewhere in the
  305. * middle of the file system at block boundary. In addition, it is not checked
  306. * if the page size is actually correct. If it is not, weird file system sizes
  307. * will be returned.
  308. *
  309. * If this function detects a file system it returns the assumed file system
  310. * size, which can be used to set the phys_size.
  311. *
  312. * Otherwise, it returns an error indicating why it is not regarded as a file
  313. * system.
  314. *
  315. * Note: this function is not protected with SPIFFS_LOCK and SPIFFS_UNLOCK
  316. * macros. It returns the error code directly, instead of as read by
  317. * SPIFFS_errno.
  318. *
  319. * @param config essential parts of the physical and logical
  320. * configuration of the file system.
  321. */
  322. s32_t SPIFFS_probe_fs(spiffs_config *config);
  323. #endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  324. /**
  325. * Initializes the file system dynamic parameters and mounts the filesystem.
  326. * If SPIFFS_USE_MAGIC is enabled the mounting may fail with SPIFFS_ERR_NOT_A_FS
  327. * if the flash does not contain a recognizable file system.
  328. * In this case, SPIFFS_format must be called prior to remounting.
  329. * @param fs the file system struct
  330. * @param config the physical and logical configuration of the file system
  331. * @param work a memory work buffer comprising 2*config->log_page_size
  332. * bytes used throughout all file system operations
  333. * @param fd_space memory for file descriptors
  334. * @param fd_space_size memory size of file descriptors
  335. * @param cache memory for cache, may be null
  336. * @param cache_size memory size of cache
  337. * @param check_cb_f callback function for reporting during consistency checks
  338. */
  339. s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
  340. u8_t *fd_space, u32_t fd_space_size,
  341. void *cache, u32_t cache_size,
  342. spiffs_check_callback check_cb_f);
  343. /**
  344. * Unmounts the file system. All file handles will be flushed of any
  345. * cached writes and closed.
  346. * @param fs the file system struct
  347. */
  348. void SPIFFS_unmount(spiffs *fs);
  349. /**
  350. * Creates a new file.
  351. * @param fs the file system struct
  352. * @param path the path of the new file
  353. * @param mode ignored, for posix compliance
  354. */
  355. s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);
  356. /**
  357. * Opens/creates a file.
  358. * @param fs the file system struct
  359. * @param path the path of the new file
  360. * @param flags the flags for the open command, can be combinations of
  361. * SPIFFS_O_APPEND, SPIFFS_O_TRUNC, SPIFFS_O_CREAT, SPIFFS_O_RDONLY,
  362. * SPIFFS_O_WRONLY, SPIFFS_O_RDWR, SPIFFS_O_DIRECT, SPIFFS_O_EXCL
  363. * @param mode ignored, for posix compliance
  364. */
  365. spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);
  366. /**
  367. * Opens a file by given dir entry.
  368. * Optimization purposes, when traversing a file system with SPIFFS_readdir
  369. * a normal SPIFFS_open would need to traverse the filesystem again to find
  370. * the file, whilst SPIFFS_open_by_dirent already knows where the file resides.
  371. * @param fs the file system struct
  372. * @param e the dir entry to the file
  373. * @param flags the flags for the open command, can be combinations of
  374. * SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
  375. * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.
  376. * SPIFFS_CREAT will have no effect in this case.
  377. * @param mode ignored, for posix compliance
  378. */
  379. spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode);
  380. /**
  381. * Opens a file by given page index.
  382. * Optimization purposes, opens a file by directly pointing to the page
  383. * index in the spi flash.
  384. * If the page index does not point to a file header SPIFFS_ERR_NOT_A_FILE
  385. * is returned.
  386. * @param fs the file system struct
  387. * @param page_ix the page index
  388. * @param flags the flags for the open command, can be combinations of
  389. * SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
  390. * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.
  391. * SPIFFS_CREAT will have no effect in this case.
  392. * @param mode ignored, for posix compliance
  393. */
  394. spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode);
  395. /**
  396. * Reads from given filehandle.
  397. * @param fs the file system struct
  398. * @param fh the filehandle
  399. * @param buf where to put read data
  400. * @param len how much to read
  401. * @returns number of bytes read, or -1 if error
  402. */
  403. s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
  404. /**
  405. * Writes to given filehandle.
  406. * @param fs the file system struct
  407. * @param fh the filehandle
  408. * @param buf the data to write
  409. * @param len how much to write
  410. * @returns number of bytes written, or -1 if error
  411. */
  412. s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
  413. /**
  414. * Moves the read/write file offset. Resulting offset is returned or negative if error.
  415. * lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset.
  416. * @param fs the file system struct
  417. * @param fh the filehandle
  418. * @param offs how much/where to move the offset
  419. * @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes
  420. * if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset
  421. * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative
  422. */
  423. s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);
  424. /**
  425. * Removes a file by path
  426. * @param fs the file system struct
  427. * @param path the path of the file to remove
  428. */
  429. s32_t SPIFFS_remove(spiffs *fs, const char *path);
  430. /**
  431. * Removes a file by filehandle
  432. * @param fs the file system struct
  433. * @param fh the filehandle of the file to remove
  434. */
  435. s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh);
  436. /**
  437. * Gets file status by path
  438. * @param fs the file system struct
  439. * @param path the path of the file to stat
  440. * @param s the stat struct to populate
  441. */
  442. s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s);
  443. /**
  444. * Gets file status by filehandle
  445. * @param fs the file system struct
  446. * @param fh the filehandle of the file to stat
  447. * @param s the stat struct to populate
  448. */
  449. s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s);
  450. /**
  451. * Flushes all pending write operations from cache for given file
  452. * @param fs the file system struct
  453. * @param fh the filehandle of the file to flush
  454. */
  455. s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh);
  456. /**
  457. * Closes a filehandle. If there are pending write operations, these are finalized before closing.
  458. * @param fs the file system struct
  459. * @param fh the filehandle of the file to close
  460. */
  461. s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
  462. /**
  463. * Renames a file
  464. * @param fs the file system struct
  465. * @param old path of file to rename
  466. * @param newPath new path of file
  467. */
  468. s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);
  469. #if SPIFFS_OBJ_META_LEN
  470. /**
  471. * Updates file's metadata
  472. * @param fs the file system struct
  473. * @param path path to the file
  474. * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.
  475. */
  476. s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta);
  477. /**
  478. * Updates file's metadata
  479. * @param fs the file system struct
  480. * @param fh file handle of the file
  481. * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.
  482. */
  483. s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta);
  484. #endif
  485. /**
  486. * Returns last error of last file operation.
  487. * @param fs the file system struct
  488. */
  489. s32_t SPIFFS_errno(spiffs *fs);
  490. /**
  491. * Clears last error.
  492. * @param fs the file system struct
  493. */
  494. void SPIFFS_clearerr(spiffs *fs);
  495. /**
  496. * Opens a directory stream corresponding to the given name.
  497. * The stream is positioned at the first entry in the directory.
  498. * On hydrogen builds the name argument is ignored as hydrogen builds always correspond
  499. * to a flat file structure - no directories.
  500. * @param fs the file system struct
  501. * @param name the name of the directory
  502. * @param d pointer the directory stream to be populated
  503. */
  504. spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d);
  505. /**
  506. * Closes a directory stream
  507. * @param d the directory stream to close
  508. */
  509. s32_t SPIFFS_closedir(spiffs_DIR *d);
  510. /**
  511. * Reads a directory into given spifs_dirent struct.
  512. * @param d pointer to the directory stream
  513. * @param e the dirent struct to be populated
  514. * @returns null if error or end of stream, else given dirent is returned
  515. */
  516. struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e);
  517. /**
  518. * Runs a consistency check on given filesystem.
  519. * @param fs the file system struct
  520. */
  521. s32_t SPIFFS_check(spiffs *fs);
  522. /**
  523. * Returns number of total bytes available and number of used bytes.
  524. * This is an estimation, and depends on if there a many files with little
  525. * data or few files with much data.
  526. * NB: If used number of bytes exceeds total bytes, a SPIFFS_check should
  527. * run. This indicates a power loss in midst of things. In worst case
  528. * (repeated powerlosses in mending or gc) you might have to delete some files.
  529. *
  530. * @param fs the file system struct
  531. * @param total total number of bytes in filesystem
  532. * @param used used number of bytes in filesystem
  533. */
  534. s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used);
  535. /**
  536. * Formats the entire file system. All data will be lost.
  537. * The filesystem must not be mounted when calling this.
  538. *
  539. * NB: formatting is awkward. Due to backwards compatibility, SPIFFS_mount
  540. * MUST be called prior to formatting in order to configure the filesystem.
  541. * If SPIFFS_mount succeeds, SPIFFS_unmount must be called before calling
  542. * SPIFFS_format.
  543. * If SPIFFS_mount fails, SPIFFS_format can be called directly without calling
  544. * SPIFFS_unmount first.
  545. *
  546. * @param fs the file system struct
  547. */
  548. s32_t SPIFFS_format(spiffs *fs);
  549. /**
  550. * Returns nonzero if spiffs is mounted, or zero if unmounted.
  551. * @param fs the file system struct
  552. */
  553. u8_t SPIFFS_mounted(spiffs *fs);
  554. /**
  555. * Tries to find a block where most or all pages are deleted, and erase that
  556. * block if found. Does not care for wear levelling. Will not move pages
  557. * around.
  558. * If parameter max_free_pages are set to 0, only blocks with only deleted
  559. * pages will be selected.
  560. *
  561. * NB: the garbage collector is automatically called when spiffs needs free
  562. * pages. The reason for this function is to give possibility to do background
  563. * tidying when user knows the system is idle.
  564. *
  565. * Use with care.
  566. *
  567. * Setting max_free_pages to anything larger than zero will eventually wear
  568. * flash more as a block containing free pages can be erased.
  569. *
  570. * Will set err_no to SPIFFS_OK if a block was found and erased,
  571. * SPIFFS_ERR_NO_DELETED_BLOCK if no matching block was found,
  572. * or other error.
  573. *
  574. * @param fs the file system struct
  575. * @param max_free_pages maximum number allowed free pages in block
  576. */
  577. s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
  578. /**
  579. * Will try to make room for given amount of bytes in the filesystem by moving
  580. * pages and erasing blocks.
  581. * If it is physically impossible, err_no will be set to SPIFFS_ERR_FULL. If
  582. * there already is this amount (or more) of free space, SPIFFS_gc will
  583. * silently return. It is recommended to call SPIFFS_info before invoking
  584. * this method in order to determine what amount of bytes to give.
  585. *
  586. * NB: the garbage collector is automatically called when spiffs needs free
  587. * pages. The reason for this function is to give possibility to do background
  588. * tidying when user knows the system is idle.
  589. *
  590. * Use with care.
  591. *
  592. * @param fs the file system struct
  593. * @param size amount of bytes that should be freed
  594. */
  595. s32_t SPIFFS_gc(spiffs *fs, u32_t size);
  596. /**
  597. * Check if EOF reached.
  598. * @param fs the file system struct
  599. * @param fh the filehandle of the file to check
  600. */
  601. s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
  602. /**
  603. * Get position in file.
  604. * @param fs the file system struct
  605. * @param fh the filehandle of the file to check
  606. */
  607. s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
  608. /**
  609. * Registers a callback function that keeps track on operations on file
  610. * headers. Do note, that this callback is called from within internal spiffs
  611. * mechanisms. Any operations on the actual file system being callbacked from
  612. * in this callback will mess things up for sure - do not do this.
  613. * This can be used to track where files are and move around during garbage
  614. * collection, which in turn can be used to build location tables in ram.
  615. * Used in conjuction with SPIFFS_open_by_page this may improve performance
  616. * when opening a lot of files.
  617. * Must be invoked after mount.
  618. *
  619. * @param fs the file system struct
  620. * @param cb_func the callback on file operations
  621. */
  622. s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func);
  623. #if SPIFFS_IX_MAP
  624. /**
  625. * Maps the first level index lookup to a given memory map.
  626. * This will make reading big files faster, as the memory map will be used for
  627. * looking up data pages instead of searching for the indices on the physical
  628. * medium. When mapping, all affected indicies are found and the information is
  629. * copied to the array.
  630. * Whole file or only parts of it may be mapped. The index map will cover file
  631. * contents from argument offset until and including arguments (offset+len).
  632. * It is valid to map a longer range than the current file size. The map will
  633. * then be populated when the file grows.
  634. * On garbage collections and file data page movements, the map array will be
  635. * automatically updated. Do not tamper with the map array, as this contains
  636. * the references to the data pages. Modifying it from outside will corrupt any
  637. * future readings using this file descriptor.
  638. * The map will no longer be used when the file descriptor closed or the file
  639. * is unmapped.
  640. * This can be useful to get faster and more deterministic timing when reading
  641. * large files, or when seeking and reading a lot within a file.
  642. * @param fs the file system struct
  643. * @param fh the file handle of the file to map
  644. * @param map a spiffs_ix_map struct, describing the index map
  645. * @param offset absolute file offset where to start the index map
  646. * @param len length of the mapping in actual file bytes
  647. * @param map_buf the array buffer for the look up data - number of required
  648. * elements in the array can be derived from function
  649. * SPIFFS_bytes_to_ix_map_entries given the length
  650. */
  651. s32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,
  652. u32_t offset, u32_t len, spiffs_page_ix *map_buf);
  653. /**
  654. * Unmaps the index lookup from this filehandle. All future readings will
  655. * proceed as normal, requiring reading of the first level indices from
  656. * physical media.
  657. * The map and map buffer given in function SPIFFS_ix_map will no longer be
  658. * referenced by spiffs.
  659. * It is not strictly necessary to unmap a file before closing it, as closing
  660. * a file will automatically unmap it.
  661. * @param fs the file system struct
  662. * @param fh the file handle of the file to unmap
  663. */
  664. s32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh);
  665. /**
  666. * Moves the offset for the index map given in function SPIFFS_ix_map. Parts or
  667. * all of the map buffer will repopulated.
  668. * @param fs the file system struct
  669. * @param fh the mapped file handle of the file to remap
  670. * @param offset new absolute file offset where to start the index map
  671. */
  672. s32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offs);
  673. /**
  674. * Utility function to get number of spiffs_page_ix entries a map buffer must
  675. * contain on order to map given amount of file data in bytes.
  676. * See function SPIFFS_ix_map and SPIFFS_ix_map_entries_to_bytes.
  677. * @param fs the file system struct
  678. * @param bytes number of file data bytes to map
  679. * @return needed number of elements in a spiffs_page_ix array needed to
  680. * map given amount of bytes in a file
  681. */
  682. s32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes);
  683. /**
  684. * Utility function to amount of file data bytes that can be mapped when
  685. * mapping a file with buffer having given number of spiffs_page_ix entries.
  686. * See function SPIFFS_ix_map and SPIFFS_bytes_to_ix_map_entries.
  687. * @param fs the file system struct
  688. * @param map_page_ix_entries number of entries in a spiffs_page_ix array
  689. * @return amount of file data in bytes that can be mapped given a map
  690. * buffer having given amount of spiffs_page_ix entries
  691. */
  692. s32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries);
  693. #endif // SPIFFS_IX_MAP
  694. #if SPIFFS_TEST_VISUALISATION
  695. /**
  696. * Prints out a visualization of the filesystem.
  697. * @param fs the file system struct
  698. */
  699. s32_t SPIFFS_vis(spiffs *fs);
  700. #endif
  701. #if SPIFFS_BUFFER_HELP
  702. /**
  703. * Returns number of bytes needed for the filedescriptor buffer given
  704. * amount of file descriptors.
  705. */
  706. u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs);
  707. #if SPIFFS_CACHE
  708. /**
  709. * Returns number of bytes needed for the cache buffer given
  710. * amount of cache pages.
  711. */
  712. u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
  713. #endif
  714. #endif
  715. #if SPIFFS_CACHE
  716. #endif
  717. #if defined(__cplusplus)
  718. }
  719. #endif
  720. #endif /* SPIFFS_H_ */