Skip to content

Commit 814bb23

Browse files
me-no-devigrr
authored andcommitted
Update SPIFFS to V0.3.7 (earlephilhower#23)
1 parent 4c41760 commit 814bb23

File tree

9 files changed

+1704
-356
lines changed

9 files changed

+1704
-356
lines changed

spiffs/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013-2015 Peter Andersson (pelleplutt1976<at>gmail.com)
3+
Copyright (c) 2013-2017 Peter Andersson (pelleplutt1976<at>gmail.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

spiffs/spiffs.h

100644100755
Lines changed: 233 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ extern "C" {
4949

5050
#define SPIFFS_ERR_FILE_EXISTS -10030
5151

52+
#define SPIFFS_ERR_NOT_A_FILE -10031
53+
#define SPIFFS_ERR_RO_NOT_IMPL -10032
54+
#define SPIFFS_ERR_RO_ABORTED_OPERATION -10033
55+
#define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034
56+
#define SPIFFS_ERR_PROBE_NOT_A_FS -10035
57+
#define SPIFFS_ERR_NAME_TOO_LONG -10036
58+
59+
#define SPIFFS_ERR_IX_MAP_UNMAPPED -10037
60+
#define SPIFFS_ERR_IX_MAP_MAPPED -10038
61+
#define SPIFFS_ERR_IX_MAP_BAD_RANGE -10039
62+
63+
#define SPIFFS_ERR_SEEK_BOUNDS -10040
64+
65+
5266
#define SPIFFS_ERR_INTERNAL -10050
5367

5468
#define SPIFFS_ERR_TEST -10100
@@ -63,9 +77,10 @@ typedef u16_t spiffs_mode;
6377
// object type
6478
typedef u8_t spiffs_obj_type;
6579

66-
#if SPIFFS_HAL_CALLBACK_EXTRA
6780
struct spiffs_t;
6881

82+
#if SPIFFS_HAL_CALLBACK_EXTRA
83+
6984
/* spi read call function type */
7085
typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
7186
/* spi write call function type */
@@ -98,7 +113,7 @@ typedef enum {
98113
SPIFFS_CHECK_FIX_LOOKUP,
99114
SPIFFS_CHECK_DELETE_ORPHANED_INDEX,
100115
SPIFFS_CHECK_DELETE_PAGE,
101-
SPIFFS_CHECK_DELETE_BAD_FILE,
116+
SPIFFS_CHECK_DELETE_BAD_FILE
102117
} spiffs_check_report;
103118

104119
/* file system check callback function */
@@ -110,9 +125,22 @@ typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_repor
110125
u32_t arg1, u32_t arg2);
111126
#endif // SPIFFS_HAL_CALLBACK_EXTRA
112127

128+
/* file system listener callback operation */
129+
typedef enum {
130+
/* the file has been created */
131+
SPIFFS_CB_CREATED = 0,
132+
/* the file has been updated or moved to another page */
133+
SPIFFS_CB_UPDATED,
134+
/* the file has been deleted */
135+
SPIFFS_CB_DELETED
136+
} spiffs_fileop_type;
137+
138+
/* file system listener callback function */
139+
typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix);
140+
113141
#ifndef SPIFFS_DBG
114142
#define SPIFFS_DBG(...) \
115-
print(__VA_ARGS__)
143+
printf(__VA_ARGS__)
116144
#endif
117145
#ifndef SPIFFS_GC_DBG
118146
#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
@@ -126,20 +154,28 @@ typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_repor
126154

127155
/* Any write to the filehandle is appended to end of the file */
128156
#define SPIFFS_APPEND (1<<0)
157+
#define SPIFFS_O_APPEND SPIFFS_APPEND
129158
/* If the opened file exists, it will be truncated to zero length before opened */
130159
#define SPIFFS_TRUNC (1<<1)
160+
#define SPIFFS_O_TRUNC SPIFFS_TRUNC
131161
/* If the opened file does not exist, it will be created before opened */
132162
#define SPIFFS_CREAT (1<<2)
163+
#define SPIFFS_O_CREAT SPIFFS_CREAT
133164
/* The opened file may only be read */
134165
#define SPIFFS_RDONLY (1<<3)
135-
/* The opened file may only be writted */
166+
#define SPIFFS_O_RDONLY SPIFFS_RDONLY
167+
/* The opened file may only be written */
136168
#define SPIFFS_WRONLY (1<<4)
137-
/* The opened file may be both read and writted */
169+
#define SPIFFS_O_WRONLY SPIFFS_WRONLY
170+
/* The opened file may be both read and written */
138171
#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
139-
/* Any writes to the filehandle will never be cached */
172+
#define SPIFFS_O_RDWR SPIFFS_RDWR
173+
/* Any writes to the filehandle will never be cached but flushed directly */
140174
#define SPIFFS_DIRECT (1<<5)
141-
/* If SPIFFS_CREAT and SPIFFS_EXCL are set, SPIFFS_open() shall fail if the file exists */
175+
#define SPIFFS_O_DIRECT SPIFFS_DIRECT
176+
/* If SPIFFS_O_CREAT and SPIFFS_O_EXCL are set, SPIFFS_open() shall fail if the file exists */
142177
#define SPIFFS_EXCL (1<<6)
178+
#define SPIFFS_O_EXCL SPIFFS_EXCL
143179

144180
#define SPIFFS_SEEK_SET (0)
145181
#define SPIFFS_SEEK_CUR (1)
@@ -247,7 +283,8 @@ typedef struct spiffs_t {
247283

248284
// check callback function
249285
spiffs_check_callback check_cb_f;
250-
286+
// file callback function
287+
spiffs_file_callback file_cb_f;
251288
// mounted flag
252289
u8_t mounted;
253290
// user data
@@ -261,7 +298,11 @@ typedef struct {
261298
spiffs_obj_id obj_id;
262299
u32_t size;
263300
spiffs_obj_type type;
301+
spiffs_page_ix pix;
264302
u8_t name[SPIFFS_OBJ_NAME_LEN];
303+
#if SPIFFS_OBJ_META_LEN
304+
u8_t meta[SPIFFS_OBJ_META_LEN];
305+
#endif
265306
} spiffs_stat;
266307

267308
struct spiffs_dirent {
@@ -270,6 +311,9 @@ struct spiffs_dirent {
270311
spiffs_obj_type type;
271312
u32_t size;
272313
spiffs_page_ix pix;
314+
#if SPIFFS_OBJ_META_LEN
315+
u8_t meta[SPIFFS_OBJ_META_LEN];
316+
#endif
273317
};
274318

275319
typedef struct {
@@ -278,8 +322,57 @@ typedef struct {
278322
int entry;
279323
} spiffs_DIR;
280324

325+
#if SPIFFS_IX_MAP
326+
327+
typedef struct {
328+
// buffer with looked up data pixes
329+
spiffs_page_ix *map_buf;
330+
// precise file byte offset
331+
u32_t offset;
332+
// start data span index of lookup buffer
333+
spiffs_span_ix start_spix;
334+
// end data span index of lookup buffer
335+
spiffs_span_ix end_spix;
336+
} spiffs_ix_map;
337+
338+
#endif
339+
281340
// functions
282341

342+
#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
343+
/**
344+
* Special function. This takes a spiffs config struct and returns the number
345+
* of blocks this file system was formatted with. This function relies on
346+
* that following info is set correctly in given config struct:
347+
*
348+
* phys_addr, log_page_size, and log_block_size.
349+
*
350+
* Also, hal_read_f must be set in the config struct.
351+
*
352+
* One must be sure of the correct page size and that the physical address is
353+
* correct in the probed file system when calling this function. It is not
354+
* checked if the phys_addr actually points to the start of the file system,
355+
* so one might get a false positive if entering a phys_addr somewhere in the
356+
* middle of the file system at block boundary. In addition, it is not checked
357+
* if the page size is actually correct. If it is not, weird file system sizes
358+
* will be returned.
359+
*
360+
* If this function detects a file system it returns the assumed file system
361+
* size, which can be used to set the phys_size.
362+
*
363+
* Otherwise, it returns an error indicating why it is not regarded as a file
364+
* system.
365+
*
366+
* Note: this function is not protected with SPIFFS_LOCK and SPIFFS_UNLOCK
367+
* macros. It returns the error code directly, instead of as read by
368+
* SPIFFS_errno.
369+
*
370+
* @param config essential parts of the physical and logical
371+
* configuration of the file system.
372+
*/
373+
s32_t SPIFFS_probe_fs(spiffs_config *config);
374+
#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
375+
283376
/**
284377
* Initializes the file system dynamic parameters and mounts the filesystem.
285378
* If SPIFFS_USE_MAGIC is enabled the mounting may fail with SPIFFS_ERR_NOT_A_FS
@@ -320,20 +413,19 @@ s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);
320413
* @param fs the file system struct
321414
* @param path the path of the new file
322415
* @param flags the flags for the open command, can be combinations of
323-
* SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
324-
* SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT
416+
* SPIFFS_O_APPEND, SPIFFS_O_TRUNC, SPIFFS_O_CREAT, SPIFFS_O_RDONLY,
417+
* SPIFFS_O_WRONLY, SPIFFS_O_RDWR, SPIFFS_O_DIRECT, SPIFFS_O_EXCL
325418
* @param mode ignored, for posix compliance
326419
*/
327420
spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);
328421

329-
330422
/**
331423
* Opens a file by given dir entry.
332424
* Optimization purposes, when traversing a file system with SPIFFS_readdir
333425
* a normal SPIFFS_open would need to traverse the filesystem again to find
334426
* the file, whilst SPIFFS_open_by_dirent already knows where the file resides.
335427
* @param fs the file system struct
336-
* @param path the dir entry to the file
428+
* @param e the dir entry to the file
337429
* @param flags the flags for the open command, can be combinations of
338430
* SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
339431
* SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.
@@ -342,6 +434,22 @@ spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs
342434
*/
343435
spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode);
344436

437+
/**
438+
* Opens a file by given page index.
439+
* Optimization purposes, opens a file by directly pointing to the page
440+
* index in the spi flash.
441+
* If the page index does not point to a file header SPIFFS_ERR_NOT_A_FILE
442+
* is returned.
443+
* @param fs the file system struct
444+
* @param page_ix the page index
445+
* @param flags the flags for the open command, can be combinations of
446+
* SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
447+
* SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.
448+
* SPIFFS_CREAT will have no effect in this case.
449+
* @param mode ignored, for posix compliance
450+
*/
451+
spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode);
452+
345453
/**
346454
* Reads from given filehandle.
347455
* @param fs the file system struct
@@ -426,6 +534,24 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
426534
*/
427535
s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);
428536

537+
#if SPIFFS_OBJ_META_LEN
538+
/**
539+
* Updates file's metadata
540+
* @param fs the file system struct
541+
* @param path path to the file
542+
* @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.
543+
*/
544+
s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta);
545+
546+
/**
547+
* Updates file's metadata
548+
* @param fs the file system struct
549+
* @param fh file handle of the file
550+
* @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.
551+
*/
552+
s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta);
553+
#endif
554+
429555
/**
430556
* Returns last error of last file operation.
431557
* @param fs the file system struct
@@ -562,6 +688,101 @@ s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
562688
*/
563689
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
564690

691+
/**
692+
* Registers a callback function that keeps track on operations on file
693+
* headers. Do note, that this callback is called from within internal spiffs
694+
* mechanisms. Any operations on the actual file system being callbacked from
695+
* in this callback will mess things up for sure - do not do this.
696+
* This can be used to track where files are and move around during garbage
697+
* collection, which in turn can be used to build location tables in ram.
698+
* Used in conjuction with SPIFFS_open_by_page this may improve performance
699+
* when opening a lot of files.
700+
* Must be invoked after mount.
701+
*
702+
* @param fs the file system struct
703+
* @param cb_func the callback on file operations
704+
*/
705+
s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func);
706+
707+
#if SPIFFS_IX_MAP
708+
709+
/**
710+
* Maps the first level index lookup to a given memory map.
711+
* This will make reading big files faster, as the memory map will be used for
712+
* looking up data pages instead of searching for the indices on the physical
713+
* medium. When mapping, all affected indicies are found and the information is
714+
* copied to the array.
715+
* Whole file or only parts of it may be mapped. The index map will cover file
716+
* contents from argument offset until and including arguments (offset+len).
717+
* It is valid to map a longer range than the current file size. The map will
718+
* then be populated when the file grows.
719+
* On garbage collections and file data page movements, the map array will be
720+
* automatically updated. Do not tamper with the map array, as this contains
721+
* the references to the data pages. Modifying it from outside will corrupt any
722+
* future readings using this file descriptor.
723+
* The map will no longer be used when the file descriptor closed or the file
724+
* is unmapped.
725+
* This can be useful to get faster and more deterministic timing when reading
726+
* large files, or when seeking and reading a lot within a file.
727+
* @param fs the file system struct
728+
* @param fh the file handle of the file to map
729+
* @param map a spiffs_ix_map struct, describing the index map
730+
* @param offset absolute file offset where to start the index map
731+
* @param len length of the mapping in actual file bytes
732+
* @param map_buf the array buffer for the look up data - number of required
733+
* elements in the array can be derived from function
734+
* SPIFFS_bytes_to_ix_map_entries given the length
735+
*/
736+
s32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,
737+
u32_t offset, u32_t len, spiffs_page_ix *map_buf);
738+
739+
/**
740+
* Unmaps the index lookup from this filehandle. All future readings will
741+
* proceed as normal, requiring reading of the first level indices from
742+
* physical media.
743+
* The map and map buffer given in function SPIFFS_ix_map will no longer be
744+
* referenced by spiffs.
745+
* It is not strictly necessary to unmap a file before closing it, as closing
746+
* a file will automatically unmap it.
747+
* @param fs the file system struct
748+
* @param fh the file handle of the file to unmap
749+
*/
750+
s32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh);
751+
752+
/**
753+
* Moves the offset for the index map given in function SPIFFS_ix_map. Parts or
754+
* all of the map buffer will repopulated.
755+
* @param fs the file system struct
756+
* @param fh the mapped file handle of the file to remap
757+
* @param offset new absolute file offset where to start the index map
758+
*/
759+
s32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offs);
760+
761+
/**
762+
* Utility function to get number of spiffs_page_ix entries a map buffer must
763+
* contain on order to map given amount of file data in bytes.
764+
* See function SPIFFS_ix_map and SPIFFS_ix_map_entries_to_bytes.
765+
* @param fs the file system struct
766+
* @param bytes number of file data bytes to map
767+
* @return needed number of elements in a spiffs_page_ix array needed to
768+
* map given amount of bytes in a file
769+
*/
770+
s32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes);
771+
772+
/**
773+
* Utility function to amount of file data bytes that can be mapped when
774+
* mapping a file with buffer having given number of spiffs_page_ix entries.
775+
* See function SPIFFS_ix_map and SPIFFS_bytes_to_ix_map_entries.
776+
* @param fs the file system struct
777+
* @param map_page_ix_entries number of entries in a spiffs_page_ix array
778+
* @return amount of file data in bytes that can be mapped given a map
779+
* buffer having given amount of spiffs_page_ix entries
780+
*/
781+
s32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries);
782+
783+
#endif // SPIFFS_IX_MAP
784+
785+
565786
#if SPIFFS_TEST_VISUALISATION
566787
/**
567788
* Prints out a visualization of the filesystem.

0 commit comments

Comments
 (0)