@@ -41,6 +41,10 @@ bool FSClass::format() {
41
41
return spiffs_format ();
42
42
}
43
43
44
+ bool FSClass::check () {
45
+ return SPIFFS_check (&_filesystemStorageHandle) == 0 ;
46
+ }
47
+
44
48
bool FSClass::exists (const char *filename) {
45
49
spiffs_stat stat = {0 };
46
50
if (SPIFFS_stat (&_filesystemStorageHandle, filename, &stat) < 0 )
@@ -61,6 +65,7 @@ bool FSClass::rename(const char *filename, const char *newname) {
61
65
}
62
66
63
67
FSFile FSClass::open (const char *filename, uint8_t mode) {
68
+ if (String (filename) == " " || String (filename) == " /" ) return FSFile (" /" );
64
69
int repeats = 0 ;
65
70
bool notExist;
66
71
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
@@ -84,95 +89,140 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
84
89
return FSFile ();
85
90
}
86
91
92
+ FSFile FSClass::open (spiffs_dirent* entry, uint8_t mode){
93
+ int res = SPIFFS_open_by_dirent (&_filesystemStorageHandle, entry, (spiffs_flags)mode, 0 );
94
+ if (res){
95
+ return FSFile (res);
96
+ }
97
+ return FSFile ();
98
+ }
99
+
87
100
FSClass FS;
88
101
89
102
FSFile::FSFile () {
90
103
_file = 0 ;
91
104
_stats = {0 };
92
105
}
93
106
107
+ FSFile::FSFile (String path) {
108
+ if (path == " /" ){
109
+ _file = 0x1 ;
110
+ os_sprintf ((char *)_stats.name , " %s" , (char *)path.c_str ());
111
+ _stats.size = 0 ;
112
+ _stats.type = SPIFFS_TYPE_DIR;
113
+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
114
+ } else {
115
+ _file = SPIFFS_open (&_filesystemStorageHandle, path.c_str (), (spiffs_flags)FSFILE_READ, 0 );
116
+ if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
117
+ debugf (" stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
118
+ }
119
+ debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
120
+ if (_stats.type == SPIFFS_TYPE_DIR){
121
+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
122
+ }
123
+ }
124
+ }
125
+
94
126
FSFile::FSFile (file_t f) {
95
127
_file = f;
96
128
if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
97
- debugf (" mount errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
129
+ debugf (" stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
130
+ }
131
+ debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
132
+ if (_stats.type == SPIFFS_TYPE_DIR){
133
+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
98
134
}
99
135
}
100
136
101
137
void FSFile::close () {
102
138
if (! _file) return ;
103
- SPIFFS_close (&_filesystemStorageHandle, _file);
139
+ if (_stats.type == SPIFFS_TYPE_DIR){
140
+ SPIFFS_closedir (&_dir);
141
+ }
142
+ if (os_strlen ((char *)_stats.name ) > 1 )
143
+ SPIFFS_close (&_filesystemStorageHandle, _file);
104
144
_file = 0 ;
105
145
}
106
146
147
+ void FSFile::rewindDirectory () {
148
+ if (! _file || !isDirectory ()) return ;
149
+ SPIFFS_closedir (&_dir);
150
+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
151
+ }
152
+
153
+ FSFile FSFile::openNextFile (){
154
+ if (! _file || !isDirectory ()) return FSFile ();
155
+ struct spiffs_dirent e;
156
+ struct spiffs_dirent *pe = &e;
157
+ if ((pe = SPIFFS_readdir (&_dir, pe))){
158
+ return FS.open ((char *)pe->name );
159
+ }
160
+ return FSFile ();
161
+ }
162
+
107
163
uint32_t FSFile::size () {
108
164
if (! _file) return 0 ;
109
- uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
110
- SPIFFS_lseek (&_filesystemStorageHandle, _file, 0 , SPIFFS_SEEK_END);
111
- uint32_t size = SPIFFS_tell (&_filesystemStorageHandle, _file);
112
- SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
113
- return size;
165
+ if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ) return 0 ;
166
+ return _stats.size ;
114
167
}
115
168
116
169
uint32_t FSFile::seek (uint32_t pos) {
117
- if (! _file) return 0 ;
170
+ if (! _file || isDirectory () ) return 0 ;
118
171
return SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
119
172
}
120
173
121
174
uint32_t FSFile::position () {
122
- if (! _file) return 0 ;
175
+ if (! _file || isDirectory () ) return 0 ;
123
176
return SPIFFS_tell (&_filesystemStorageHandle, _file);
124
177
}
125
178
126
179
bool FSFile::eof () {
127
- if (! _file) return 0 ;
180
+ if (! _file || isDirectory () ) return 0 ;
128
181
return SPIFFS_eof (&_filesystemStorageHandle, _file);
129
182
}
130
183
131
184
bool FSFile::isDirectory (void ) {
132
- return false ;
185
+ return _stats. type == SPIFFS_TYPE_DIR ;
133
186
}
134
187
135
188
int FSFile::read (void *buf, uint16_t nbyte) {
136
- if (! _file) return -1 ;
189
+ if (! _file || isDirectory () ) return -1 ;
137
190
return SPIFFS_read (&_filesystemStorageHandle, _file, buf, nbyte);
138
191
}
139
192
140
193
int FSFile::read () {
141
- if (! _file) return -1 ;
194
+ if (! _file || isDirectory () ) return -1 ;
142
195
int val;
143
196
if (SPIFFS_read (&_filesystemStorageHandle, _file, &val, 1 ) != 1 ) return -1 ;
144
197
return val;
145
198
}
146
199
147
200
int FSFile::peek () {
148
- if (! _file) return 0 ;
201
+ if (! _file || isDirectory () ) return 0 ;
149
202
int c = read ();
150
203
SPIFFS_lseek (&_filesystemStorageHandle, _file, -1 , SPIFFS_SEEK_CUR);
151
204
return c;
152
205
}
153
206
154
207
int FSFile::available () {
155
- if (! _file) return 0 ;
208
+ if (! _file || isDirectory () ) return 0 ;
156
209
uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
157
- SPIFFS_lseek (&_filesystemStorageHandle, _file, 0 , SPIFFS_SEEK_END);
158
- uint32_t size = SPIFFS_tell (&_filesystemStorageHandle, _file);
159
- SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
160
- return size - pos;
210
+ return _stats.size - pos;
161
211
}
162
212
163
213
size_t FSFile::write (const uint8_t *buf, size_t size){
164
- if (! _file) return 0 ;
214
+ if (! _file || isDirectory () ) return 0 ;
165
215
int res = SPIFFS_write (&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
166
216
return (res > 0 )?(size_t )res:0 ;
167
217
}
168
218
169
219
size_t FSFile::write (uint8_t val) {
170
- if (! _file) return 0 ;
220
+ if (! _file || isDirectory () ) return 0 ;
171
221
return write (&val, 1 );
172
222
}
173
223
174
224
void FSFile::flush (){
175
- if (! _file) return ;
225
+ if (! _file || isDirectory () ) return ;
176
226
SPIFFS_fflush (&_filesystemStorageHandle, _file);
177
227
}
178
228
@@ -191,24 +241,5 @@ void FSFile::clearError(){
191
241
}
192
242
193
243
char * FSFile::name (){
194
- return 0 ;
244
+ return ( char *)_stats. name ;
195
245
}
196
-
197
-
198
-
199
-
200
-
201
-
202
- /*
203
- spiffs_DIR *dirOpen(spiffs_DIR *d){
204
- return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
205
- }
206
-
207
- int dirClose(spiffs_DIR *d){
208
- return SPIFFS_closedir(d);
209
- }
210
-
211
- file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
212
- return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
213
- }
214
- */
0 commit comments