Skip to content

Commit fe5c667

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
enhancements on the FS Api
1 parent c4dca3b commit fe5c667

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

hardware/esp8266com/esp8266/cores/esp8266/FileSystem.cpp

+28-24
Original file line numberDiff line numberDiff line change
@@ -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+
147155
void 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

153161
FSFile 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

163171
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);
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+
169187
uint32_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

174192
uint32_t FSFile::position() {
175-
if (! _file || isDirectory()) return 0;
193+
if (!_file) return 0;
176194
return SPIFFS_tell(&_filesystemStorageHandle, _file);
177195
}
178196

179197
bool 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-
188202
int 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-
213221
size_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

235243
int FSFile::lastError(){
@@ -239,7 +247,3 @@ int FSFile::lastError(){
239247
void FSFile::clearError(){
240248
_filesystemStorageHandle.errno = SPIFFS_OK;
241249
}
242-
243-
char * FSFile::name(){
244-
return (char*)_stats.name;
245-
}

hardware/esp8266com/esp8266/cores/esp8266/FileSystem.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
class String;
2727

2828
#define FSFILE_READ SPIFFS_RDONLY
29-
#define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
30-
#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
29+
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
30+
#define FSFILE_OVERWRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
3131

3232
class FSFile : public Stream {
3333
private:
@@ -47,11 +47,11 @@ class FSFile : public Stream {
4747
virtual void flush();
4848
int read(void *buf, uint16_t nbyte);
4949
uint32_t seek(uint32_t pos);
50-
uint32_t remove();
5150
uint32_t position();
5251
uint32_t size();
5352
bool eof();
5453
void close();
54+
bool remove();
5555
int lastError();
5656
void clearError();
5757
operator bool() { return _file > 0; }

0 commit comments

Comments
 (0)