@@ -144,14 +144,22 @@ void FSFile::close() {
144144 _file = 0 ;
145145}
146146
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+
147155void FSFile::rewindDirectory () {
148- if (! _file || !isDirectory ()) return ;
156+ if (!_file || !isDirectory ()) return ;
149157 SPIFFS_closedir (&_dir);
150158 SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
151159}
152160
153161FSFile FSFile::openNextFile (){
154- if (! _file || !isDirectory ()) return FSFile ();
162+ if (!_file || !isDirectory ()) return FSFile ();
155163 struct spiffs_dirent e;
156164 struct spiffs_dirent *pe = &e;
157165 if ((pe = SPIFFS_readdir (&_dir, pe))){
@@ -161,30 +169,36 @@ FSFile FSFile::openNextFile(){
161169}
162170
163171uint32_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);
166178 return _stats.size ;
167179}
168180
181+ int FSFile::available () {
182+ if (!_file) return 0 ;
183+ uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
184+ return size () - pos;
185+ }
186+
169187uint32_t FSFile::seek (uint32_t pos) {
170- if (! _file || isDirectory () ) return 0 ;
188+ if (!_file) return 0 ;
171189 return SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
172190}
173191
174192uint32_t FSFile::position () {
175- if (! _file || isDirectory () ) return 0 ;
193+ if (!_file) return 0 ;
176194 return SPIFFS_tell (&_filesystemStorageHandle, _file);
177195}
178196
179197bool FSFile::eof () {
180- if (! _file || isDirectory () ) return 0 ;
198+ if (!_file) return 0 ;
181199 return SPIFFS_eof (&_filesystemStorageHandle, _file);
182200}
183201
184- bool FSFile::isDirectory (void ) {
185- return _stats.type == SPIFFS_TYPE_DIR;
186- }
187-
188202int FSFile::read (void *buf, uint16_t nbyte) {
189203 if (! _file || isDirectory ()) return -1 ;
190204 return SPIFFS_read (&_filesystemStorageHandle, _file, buf, nbyte);
@@ -204,12 +218,6 @@ int FSFile::peek() {
204218 return c;
205219}
206220
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-
213221size_t FSFile::write (const uint8_t *buf, size_t size){
214222 if (! _file || isDirectory ()) return 0 ;
215223 int res = SPIFFS_write (&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
@@ -226,10 +234,10 @@ void FSFile::flush(){
226234 SPIFFS_fflush (&_filesystemStorageHandle, _file);
227235}
228236
229- uint32_t FSFile::remove (){
237+ bool FSFile::remove (){
230238 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 ;
233241}
234242
235243int FSFile::lastError (){
@@ -239,7 +247,3 @@ int FSFile::lastError(){
239247void FSFile::clearError (){
240248 _filesystemStorageHandle.errno = SPIFFS_OK;
241249}
242-
243- char * FSFile::name (){
244- return (char *)_stats.name ;
245- }
0 commit comments