Skip to content

Commit ca88cb2

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
Update to the latest SPIFFS git and cleanup
Almost unmodified spiffs from github lots of NodeMCU leftovers are removed More proper names given to platform related files Adjusting the FS class for the changes
1 parent 93c4566 commit ca88cb2

20 files changed

+875
-693
lines changed

Diff for: cores/esp8266/FileSystem.cpp

100755100644
+46-24
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,71 @@
2222
#include "Arduino.h"
2323

2424
bool FSClass::mount() {
25-
if (_mounted)
26-
return true;
27-
28-
_mounted = spiffs_mount();
29-
return _mounted;
25+
if(SPIFFS_mounted(&_filesystemStorageHandle)) return true;
26+
int res = spiffs_mount();
27+
if(res != 0){
28+
int formated = SPIFFS_format(&_filesystemStorageHandle);
29+
if(formated != 0) return false;
30+
res = spiffs_mount();
31+
}
32+
return (res == 0);
3033
}
3134

3235
void FSClass::unmount() {
33-
if (!_mounted)
34-
return;
35-
36-
spiffs_unmount();
37-
_mounted = false;
36+
if(SPIFFS_mounted(&_filesystemStorageHandle))
37+
SPIFFS_unmount(&_filesystemStorageHandle);
3838
}
3939

4040
bool FSClass::format() {
41-
return spiffs_format();
41+
if(!SPIFFS_mounted(&_filesystemStorageHandle)){
42+
spiffs_mount();
43+
}
44+
SPIFFS_unmount(&_filesystemStorageHandle);
45+
int formated = SPIFFS_format(&_filesystemStorageHandle);
46+
if(formated != 0) return false;
47+
return (spiffs_mount() == 0);
4248
}
4349

4450
bool FSClass::check() {
4551
return SPIFFS_check(&_filesystemStorageHandle) == 0;
4652
}
4753

48-
bool FSClass::exists(const char *filename) {
54+
bool FSClass::exists(char *filename) {
4955
spiffs_stat stat = {0};
5056
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0)
5157
return false;
5258
return stat.name[0] != '\0';
5359
}
5460

55-
bool FSClass::create(const char *filepath){
61+
bool FSClass::create(char *filepath){
5662
return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0;
5763
}
5864

59-
bool FSClass::remove(const char *filepath){
65+
bool FSClass::remove(char *filepath){
6066
return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0;
6167
}
6268

63-
bool FSClass::rename(const char *filename, const char *newname) {
69+
bool FSClass::rename(char *filename, char *newname) {
6470
return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0;
6571
}
6672

67-
FSFile FSClass::open(const char *filename, uint8_t mode) {
73+
size_t FSClass::totalBytes(){
74+
u32_t totalBytes;
75+
u32_t usedBytes;
76+
if(SPIFFS_info(&_filesystemStorageHandle, &totalBytes, &usedBytes) == 0)
77+
return totalBytes;
78+
return 0;
79+
}
80+
81+
size_t FSClass::usedBytes(){
82+
u32_t totalBytes;
83+
u32_t usedBytes;
84+
if(SPIFFS_info(&_filesystemStorageHandle, &totalBytes, &usedBytes) == 0)
85+
return usedBytes;
86+
return 0;
87+
}
88+
89+
FSFile FSClass::open(char *filename, uint8_t mode) {
6890
if(String(filename) == "" || String(filename) == "/") return FSFile("/");
6991
int repeats = 0;
7092
bool notExist;
@@ -76,7 +98,7 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
7698
res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0);
7799
int code = SPIFFS_errno(&_filesystemStorageHandle);
78100
if (res < 0){
79-
debugf("open errno %d\n", code);
101+
SPIFFS_API_DBG_E("open errno %d\n", code);
80102
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
81103
if (notExist && canRecreate)
82104
remove(filename); // fix for deleted files
@@ -112,11 +134,11 @@ FSFile::FSFile(String path) {
112134
_stats.type = SPIFFS_TYPE_DIR;
113135
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
114136
} else {
115-
_file = SPIFFS_open(&_filesystemStorageHandle, path.c_str(), (spiffs_flags)FSFILE_READ, 0);
137+
_file = SPIFFS_open(&_filesystemStorageHandle, (char *)path.c_str(), (spiffs_flags)FSFILE_READ, 0);
116138
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
117-
debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
139+
SPIFFS_API_DBG_E("fstat errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
118140
}
119-
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
141+
//debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
120142
if(_stats.type == SPIFFS_TYPE_DIR){
121143
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
122144
}
@@ -126,9 +148,9 @@ FSFile::FSFile(String path) {
126148
FSFile::FSFile(file_t f) {
127149
_file = f;
128150
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
129-
debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
151+
SPIFFS_API_DBG_E("fstat errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
130152
}
131-
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
153+
//debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
132154
if(_stats.type == SPIFFS_TYPE_DIR){
133155
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
134156
}
@@ -237,13 +259,13 @@ void FSFile::flush(){
237259
bool FSFile::remove(){
238260
if (! _file) return 0;
239261
close();
240-
return SPIFFS_remove(&_filesystemStorageHandle, (const char *)_stats.name) == 0;
262+
return SPIFFS_remove(&_filesystemStorageHandle, (char *)_stats.name) == 0;
241263
}
242264

243265
int FSFile::lastError(){
244266
return SPIFFS_errno(&_filesystemStorageHandle);
245267
}
246268

247269
void FSFile::clearError(){
248-
_filesystemStorageHandle.errno = SPIFFS_OK;
270+
_filesystemStorageHandle.err_code = SPIFFS_OK;
249271
}

Diff for: cores/esp8266/FileSystem.h

100755100644
+7-6
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,18 @@ class FSFile : public Stream {
8585
class FSClass {
8686

8787
private:
88-
bool _mounted = false;
8988

9089
public:
9190
bool mount();
9291
void unmount();
9392
bool format();
9493
bool check();
95-
bool exists(const char *filename);
96-
bool create(const char *filepath);
97-
bool remove(const char *filepath);
98-
bool rename(const char *filename, const char *newname);
94+
bool exists(char *filename);
95+
bool create(char *filepath);
96+
bool remove(char *filepath);
97+
bool rename(char *filename, char *newname);
98+
size_t totalBytes();
99+
size_t usedBytes();
99100
size_t size(){ return _filesystemStorageHandle.cfg.phys_size; }
100101
size_t blockSize(){ return _filesystemStorageHandle.cfg.log_block_size; }
101102
size_t totalBlocks(){ return _filesystemStorageHandle.block_count; }
@@ -104,7 +105,7 @@ class FSClass {
104105
size_t allocatedPages(){ return _filesystemStorageHandle.stats_p_allocated; }
105106
size_t deletedPages(){ return _filesystemStorageHandle.stats_p_deleted; }
106107

107-
FSFile open(const char *filename, uint8_t mode = FSFILE_READ);
108+
FSFile open(char *filename, uint8_t mode = FSFILE_READ);
108109
FSFile open(spiffs_dirent* entry, uint8_t mode = FSFILE_READ);
109110

110111
private:

Diff for: cores/esp8266/spiffs/docs/INTEGRATION renamed to cores/esp8266/spiffs/INTEGRATION

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,4 @@ Having these figures you can disable SPIFFS_BUFFER_HELP again to save flash.
303303

304304
* HOW TO CONFIG
305305

306-
TODO
306+
TODO

Diff for: cores/esp8266/spiffs/Makefile

-44
This file was deleted.

Diff for: cores/esp8266/spiffs/README

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
SPIFFS (SPI Flash File System)
2+
V0.3.0
3+
4+
Copyright (c) 2013-2015 Peter Andersson (pelleplutt1976<at>gmail.com)
5+
6+
For legal stuff, see LICENCE in this directory. Basically, you may do whatever
7+
you want with the source. Use, modify, sell, print it out, roll it and smoke it
8+
- as long as I won't be held responsible.
9+
10+
Love to hear feedback though!
11+
12+
13+
* INTRODUCTION
14+
15+
Spiffs is a file system intended for SPI NOR flash devices on embedded targets.
16+
17+
Spiffs is designed with following characteristics in mind:
18+
- Small (embedded) targets, sparse RAM without heap
19+
- Only big areas of data (blocks) can be erased
20+
- An erase will reset all bits in block to ones
21+
- Writing pulls one to zeroes
22+
- Zeroes can only be pulled to ones by erase
23+
- Wear leveling
24+
25+
26+
* FEATURES
27+
28+
What spiffs does:
29+
- Specifically designed for low ram usage
30+
- Uses statically sized ram buffers, independent of number of files
31+
- Posix-like api: open, close, read, write, seek, stat, etc
32+
- It can be run on any NOR flash, not only SPI flash - theoretically also on
33+
embedded flash of an microprocessor
34+
- Multiple spiffs configurations can be run on same target - and even on same
35+
SPI flash device
36+
- Implements static wear leveling
37+
- Built in file system consistency checks
38+
39+
What spiffs does not:
40+
- Presently, spiffs does not support directories. It produces a flat
41+
structure. Creating a file with path "tmp/myfile.txt" will create a file
42+
called "tmp/myfile.txt" instead of a "myfile.txt" under directory "tmp".
43+
- It is not a realtime stack. One write operation might take much longer than
44+
another.
45+
- Poor scalability. Spiffs is intended for small memory devices - the normal
46+
sizes for SPI flashes. Going beyond ~128MB is probably a bad idea. This is
47+
a side effect of the design goal to use as little ram as possible.
48+
- Presently, it does not detect or handle bad blocks.
49+
50+
51+
* MORE INFO
52+
53+
For integration, see the docs/INTEGRATION file.
54+
55+
For use and design, see the docs/TECH_SPEC file.
56+
57+
For testing and contributions, see the docs/IMPLEMENTING file.
58+
59+
* HISTORY
60+
61+
0.3.0
62+
Added existing namecheck when creating files
63+
Lots of static analysis bugs #6
64+
Added rename func
65+
Fix SPIFFS_read length when reading beyond file size
66+
Added reading beyond file length testcase
67+
Made build a bit more configurable
68+
Changed name in spiffs from "errno" to "err_code" due to conflicts compiling
69+
in mingw
70+
Improved GC checks, fixed an append bug, more robust truncate for very special
71+
case
72+
GC checks preempts GC, truncate even less picky
73+
Struct alignment needed for some targets, define in spiffs config #10
74+
Spiffs filesystem magic, definable in config
75+
76+
New config defines:
77+
SPIFFS_USE_MAGIC - enable or disable magic check upon mount
78+
SPIFFS_ALIGNED_OBJECT_INDEX_TABLES - alignment for certain targets
79+
New API functions:
80+
SPIFFS_rename - rename files
81+
SPIFFS_clearerr - clears last errno
82+
SPIFFS_info - returns info on used and total bytes in fs
83+
SPIFFS_format - formats the filesystem
84+
SPIFFS_mounted - checks if filesystem is mounted
85+
86+
File renamed without changes.

Diff for: cores/esp8266/spiffs/TODO

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
* When mending lost pages, also see if they fit into length specified in object index header
2+
3+
SPIFFS2 thoughts
4+
5+
* Instead of exact object id:s in the object lookup tables, use a hash of span index and object id.
6+
Eg. object id xor:ed with bit-reversed span index.
7+
This should decrease number of actual pages that needs to be visited when looking thru the obj lut.
8+
9+
* Logical number of each block. When moving stuff in a garbage collected page, the free
10+
page is assigned the same number as the garbage collected. Thus, object index pages do not have to
11+
be rewritten.
12+
13+
* Steal one page, use as a bit parity page. When starting an fs modification operation, write one bit
14+
as zero. When ending, write another bit as zero. On mount, if number of zeroes in page is uneven, a
15+
check is automatically run.

0 commit comments

Comments
 (0)