Skip to content

Commit 6e883e2

Browse files
me-no-devd-a-v
authored andcommitted
Update spiffs 0.3.7 to and mkspiffs 0.1.3
1 parent 47baf29 commit 6e883e2

11 files changed

+1463
-384
lines changed

cores/esp8266/spiffs/LICENSE

+1-1
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

cores/esp8266/spiffs/README.md

+73-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# SPIFFS (SPI Flash File System)
2-
**V0.3.4**
2+
**V0.3.7**
33

4-
Copyright (c) 2013-2016 Peter Andersson (pelleplutt1976 at gmail.com)
4+
[![Build Status](https://travis-ci.org/pellepl/spiffs.svg?branch=master)](https://travis-ci.org/pellepl/spiffs)
5+
6+
Copyright (c) 2013-2017 Peter Andersson (pelleplutt1976 at gmail.com)
57

68
For legal stuff, see [LICENSE](https://github.com/pellepl/spiffs/blob/master/LICENSE). Basically, you may do whatever you want with the source. Use, modify, sell, print it out, roll it and smoke it - as long as I won't be held responsible.
79

@@ -21,34 +23,98 @@ Spiffs is designed with following characteristics in mind:
2123
- Wear leveling
2224

2325

26+
## BUILDING
27+
28+
`mkdir build; make`
29+
30+
Otherwise, configure the `builddir` variable towards the top of `makefile` as something opposed to the default `build`. Sanity check on the host via `make test` and refer to `.travis.yml` for the official in-depth testing procedure. See the wiki for [integrating](https://github.com/pellepl/spiffs/wiki/Integrate-spiffs) spiffs into projects and [spiffsimg](https://github.com/nodemcu/nodemcu-firmware/tree/master/tools/spiffsimg) from [nodemcu](https://github.com/nodemcu) is a good example on the subject.
31+
32+
2433
## FEATURES
2534

2635
What spiffs does:
2736
- Specifically designed for low ram usage
2837
- Uses statically sized ram buffers, independent of number of files
2938
- Posix-like api: open, close, read, write, seek, stat, etc
30-
- It can be run on any NOR flash, not only SPI flash - theoretically also on embedded flash of an microprocessor
31-
- Multiple spiffs configurations can be run on same target - and even on same SPI flash device
39+
- It can run on any NOR flash, not only SPI flash - theoretically also on embedded flash of a microprocessor
40+
- Multiple spiffs configurations can run on same target - and even on same SPI flash device
3241
- Implements static wear leveling
3342
- Built in file system consistency checks
43+
- Highly configurable
3444

3545
What spiffs does not:
3646
- Presently, spiffs does not support directories. It produces a flat structure. Creating a file with path *tmp/myfile.txt* will create a file called *tmp/myfile.txt* instead of a *myfile.txt* under directory *tmp*.
37-
- It is not a realtime stack. One write operation might take much longer than another.
38-
- Poor scalability. Spiffs is intended for small memory devices - the normal sizes for SPI flashes. Going beyond ~128MB is probably a bad idea. This is a side effect of the design goal to use as little ram as possible.
47+
- It is not a realtime stack. One write operation might last much longer than another.
48+
- Poor scalability. Spiffs is intended for small memory devices - the normal sizes for SPI flashes. Going beyond ~128Mbyte is probably a bad idea. This is a side effect of the design goal to use as little ram as possible.
3949
- Presently, it does not detect or handle bad blocks.
50+
- One configuration, one binary. There's no generic spiffs binary that handles all types of configurations.
4051

4152

4253
## MORE INFO
4354

44-
See the [wiki](https://github.com/pellepl/spiffs/wiki) for configuring, integrating and using spiffs.
55+
See the [wiki](https://github.com/pellepl/spiffs/wiki) for [configuring](https://github.com/pellepl/spiffs/wiki/Configure-spiffs), [integrating](https://github.com/pellepl/spiffs/wiki/Integrate-spiffs), [using](https://github.com/pellepl/spiffs/wiki/Using-spiffs), and [optimizing](https://github.com/pellepl/spiffs/wiki/Performance-and-Optimizing) spiffs.
4556

4657
For design, see [docs/TECH_SPEC](https://github.com/pellepl/spiffs/blob/master/docs/TECH_SPEC).
4758

4859
For a generic spi flash driver, see [this](https://github.com/pellepl/spiflash_driver).
4960

5061
## HISTORY
5162

63+
### 0.3.7
64+
- fixed prevent seeking to negative offsets #158
65+
- fixed file descriptor offsets not updated for multiple fds on same file #157
66+
- fixed cache page not closed for removed files #156
67+
- fixed a lseek bug when seeking exactly to end of a fully indexed first level LUT #148
68+
- fixed wear leveling issue #145
69+
- fixed attempt to write out of bounds in flash #130,
70+
- set file offset when seeking over end #121 (thanks @sensslen)
71+
- fixed seeking in virgin files #120 (thanks @sensslen)
72+
- Optional file metadata #128 (thanks @cesanta)
73+
- AFL testing framework #100 #143 (thanks @pjsg)
74+
- Testframe updates
75+
76+
New API functions:
77+
- `SPIFFS_update_meta, SPIFFS_fupdate_meta` - updates metadata for a file
78+
79+
New config defines:
80+
- `SPIFFS_OBJ_META_LEN` - enable possibility to add extra metadata to files
81+
82+
### 0.3.6
83+
- Fix range bug in index memory mapping #98
84+
- Add index memory mapping #97
85+
- Optimize SPIFFS_read for large files #96
86+
- Add temporal cache for opening files #95
87+
- More robust gc #93 (thanks @dismirlian)
88+
- Fixed a double write of same data in certain cache situations
89+
- Fixed an open bug in READ_ONLY builds
90+
- File not visible in SPIFFS_readdir #90 (thanks @benpicco-tmp)
91+
- Cache load code cleanup #92 (thanks @niclash)
92+
- Fixed lock/unlock asymmetry #88 #87 (thanks @JackJefferson, @dpruessner)
93+
- Testframe updates
94+
95+
New API functions:
96+
- `SPIFFS_ix_map` - map index meta data to memory for a file
97+
- `SPIFFS_ix_unmap` - unmaps index meta data for a file
98+
- `SPIFFS_ix_remap` - changes file offset for index metadata map
99+
- `SPIFFS_bytes_to_ix_map_entries` - utility, get length of needed vector for given amount of bytes
100+
- `SPIFFS_ix_map_entries_to_bytes` - utility, get number of bytes a vector can represent given length
101+
102+
New config defines:
103+
- `SPIFFS_IX_MAP` - enable possibility to map index meta data to memory for reading faster
104+
- `SPIFFS_TEMPORAL_FD_CACHE` - enable temporal cache for opening files faster
105+
- `SPIFFS_TEMPORAL_CACHE_HIT_SCORE` - for tuning the temporal cache
106+
107+
### 0.3.5
108+
- Fixed a bug in fs check
109+
- API returns actual error codes #84) (thanks @Nails)
110+
- Fix compiler warnings for non-gcc #83 #81 (thanks @Nails)
111+
- Unable to recover from full fs #82 (thanks @rojer)
112+
- Define SPIFFS_O_* flags #80
113+
- Problem with long filenames #79 (thanks @psjg)
114+
- Duplicate file name bug fix #74 (thanks @igrr)
115+
- SPIFFS_eof and SPIFFS_tell return wrong value #72 (thanks @ArtemPisarenko)
116+
- Bunch of testframe updates #77 #78 #86 (thanks @dpreussner, @psjg a.o)
117+
52118
### 0.3.4
53119
- Added user callback file func.
54120
- Fixed a stat bug with obj id.

cores/esp8266/spiffs/spiffs.h

+144-9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ extern "C" {
5454
#define SPIFFS_ERR_RO_ABORTED_OPERATION -10033
5555
#define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034
5656
#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+
5766
#define SPIFFS_ERR_INTERNAL -10050
5867

5968
#define SPIFFS_ERR_TEST -10100
@@ -104,7 +113,7 @@ typedef enum {
104113
SPIFFS_CHECK_FIX_LOOKUP,
105114
SPIFFS_CHECK_DELETE_ORPHANED_INDEX,
106115
SPIFFS_CHECK_DELETE_PAGE,
107-
SPIFFS_CHECK_DELETE_BAD_FILE,
116+
SPIFFS_CHECK_DELETE_BAD_FILE
108117
} spiffs_check_report;
109118

110119
/* file system check callback function */
@@ -123,15 +132,15 @@ typedef enum {
123132
/* the file has been updated or moved to another page */
124133
SPIFFS_CB_UPDATED,
125134
/* the file has been deleted */
126-
SPIFFS_CB_DELETED,
135+
SPIFFS_CB_DELETED
127136
} spiffs_fileop_type;
128137

129138
/* file system listener callback function */
130139
typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix);
131140

132141
#ifndef SPIFFS_DBG
133142
#define SPIFFS_DBG(...) \
134-
print(__VA_ARGS__)
143+
printf(__VA_ARGS__)
135144
#endif
136145
#ifndef SPIFFS_GC_DBG
137146
#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
@@ -145,20 +154,28 @@ typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op,
145154

146155
/* Any write to the filehandle is appended to end of the file */
147156
#define SPIFFS_APPEND (1<<0)
157+
#define SPIFFS_O_APPEND SPIFFS_APPEND
148158
/* If the opened file exists, it will be truncated to zero length before opened */
149159
#define SPIFFS_TRUNC (1<<1)
160+
#define SPIFFS_O_TRUNC SPIFFS_TRUNC
150161
/* If the opened file does not exist, it will be created before opened */
151162
#define SPIFFS_CREAT (1<<2)
163+
#define SPIFFS_O_CREAT SPIFFS_CREAT
152164
/* The opened file may only be read */
153165
#define SPIFFS_RDONLY (1<<3)
154-
/* The opened file may only be writted */
166+
#define SPIFFS_O_RDONLY SPIFFS_RDONLY
167+
/* The opened file may only be written */
155168
#define SPIFFS_WRONLY (1<<4)
156-
/* 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 */
157171
#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
158-
/* 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 */
159174
#define SPIFFS_DIRECT (1<<5)
160-
/* 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 */
161177
#define SPIFFS_EXCL (1<<6)
178+
#define SPIFFS_O_EXCL SPIFFS_EXCL
162179

163180
#define SPIFFS_SEEK_SET (0)
164181
#define SPIFFS_SEEK_CUR (1)
@@ -283,6 +300,9 @@ typedef struct {
283300
spiffs_obj_type type;
284301
spiffs_page_ix pix;
285302
u8_t name[SPIFFS_OBJ_NAME_LEN];
303+
#if SPIFFS_OBJ_META_LEN
304+
u8_t meta[SPIFFS_OBJ_META_LEN];
305+
#endif
286306
} spiffs_stat;
287307

288308
struct spiffs_dirent {
@@ -291,6 +311,9 @@ struct spiffs_dirent {
291311
spiffs_obj_type type;
292312
u32_t size;
293313
spiffs_page_ix pix;
314+
#if SPIFFS_OBJ_META_LEN
315+
u8_t meta[SPIFFS_OBJ_META_LEN];
316+
#endif
294317
};
295318

296319
typedef struct {
@@ -299,6 +322,21 @@ typedef struct {
299322
int entry;
300323
} spiffs_DIR;
301324

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+
302340
// functions
303341

304342
#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
@@ -375,8 +413,8 @@ s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);
375413
* @param fs the file system struct
376414
* @param path the path of the new file
377415
* @param flags the flags for the open command, can be combinations of
378-
* SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,
379-
* 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
380418
* @param mode ignored, for posix compliance
381419
*/
382420
spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);
@@ -496,6 +534,24 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
496534
*/
497535
s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);
498536

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+
499555
/**
500556
* Returns last error of last file operation.
501557
* @param fs the file system struct
@@ -648,6 +704,85 @@ s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
648704
*/
649705
s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func);
650706

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+
651786
#if SPIFFS_TEST_VISUALISATION
652787
/**
653788
* Prints out a visualization of the filesystem.

0 commit comments

Comments
 (0)