@@ -144,14 +144,22 @@ void FSFile::close() {
144
144
_file = 0 ;
145
145
}
146
146
147
+ char * FSFile::name (){
148
+ return (char *)_stats.name ;
149
+ }
150
+
151
+ bool FSFile::isDirectory (void ) {
152
+ return _stats.type == SPIFFS_TYPE_DIR;
153
+ }
154
+
147
155
void FSFile::rewindDirectory () {
148
- if (! _file || !isDirectory ()) return ;
156
+ if (!_file || !isDirectory ()) return ;
149
157
SPIFFS_closedir (&_dir);
150
158
SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
151
159
}
152
160
153
161
FSFile FSFile::openNextFile (){
154
- if (! _file || !isDirectory ()) return FSFile ();
162
+ if (!_file || !isDirectory ()) return FSFile ();
155
163
struct spiffs_dirent e;
156
164
struct spiffs_dirent *pe = &e;
157
165
if ((pe = SPIFFS_readdir (&_dir, pe))){
@@ -161,30 +169,36 @@ FSFile FSFile::openNextFile(){
161
169
}
162
170
163
171
uint32_t FSFile::size () {
164
- if (! _file) return 0 ;
165
- if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ) return 0 ;
172
+ if (!_file) return 0 ;
173
+ if (_stats.size ) return _stats.size ;
174
+ uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
175
+ SPIFFS_lseek (&_filesystemStorageHandle, _file, 0 , SPIFFS_SEEK_END);
176
+ _stats.size = SPIFFS_tell (&_filesystemStorageHandle, _file);
177
+ SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
166
178
return _stats.size ;
167
179
}
168
180
181
+ int FSFile::available () {
182
+ if (!_file) return 0 ;
183
+ uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
184
+ return size () - pos;
185
+ }
186
+
169
187
uint32_t FSFile::seek (uint32_t pos) {
170
- if (! _file || isDirectory () ) return 0 ;
188
+ if (!_file) return 0 ;
171
189
return SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
172
190
}
173
191
174
192
uint32_t FSFile::position () {
175
- if (! _file || isDirectory () ) return 0 ;
193
+ if (!_file) return 0 ;
176
194
return SPIFFS_tell (&_filesystemStorageHandle, _file);
177
195
}
178
196
179
197
bool FSFile::eof () {
180
- if (! _file || isDirectory () ) return 0 ;
198
+ if (!_file) return 0 ;
181
199
return SPIFFS_eof (&_filesystemStorageHandle, _file);
182
200
}
183
201
184
- bool FSFile::isDirectory (void ) {
185
- return _stats.type == SPIFFS_TYPE_DIR;
186
- }
187
-
188
202
int FSFile::read (void *buf, uint16_t nbyte) {
189
203
if (! _file || isDirectory ()) return -1 ;
190
204
return SPIFFS_read (&_filesystemStorageHandle, _file, buf, nbyte);
@@ -204,12 +218,6 @@ int FSFile::peek() {
204
218
return c;
205
219
}
206
220
207
- int FSFile::available () {
208
- if (! _file || isDirectory ()) return 0 ;
209
- uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
210
- return _stats.size - pos;
211
- }
212
-
213
221
size_t FSFile::write (const uint8_t *buf, size_t size){
214
222
if (! _file || isDirectory ()) return 0 ;
215
223
int res = SPIFFS_write (&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
@@ -226,10 +234,10 @@ void FSFile::flush(){
226
234
SPIFFS_fflush (&_filesystemStorageHandle, _file);
227
235
}
228
236
229
- uint32_t FSFile::remove (){
237
+ bool FSFile::remove (){
230
238
if (! _file) return 0 ;
231
- return SPIFFS_fremove (&_filesystemStorageHandle, _file );
232
- _file = 0 ;
239
+ close ( );
240
+ return SPIFFS_remove (&_filesystemStorageHandle, ( const char *)_stats. name ) = = 0 ;
233
241
}
234
242
235
243
int FSFile::lastError (){
@@ -239,7 +247,3 @@ int FSFile::lastError(){
239
247
void FSFile::clearError (){
240
248
_filesystemStorageHandle.errno = SPIFFS_OK;
241
249
}
242
-
243
- char * FSFile::name (){
244
- return (char *)_stats.name ;
245
- }
0 commit comments