From fe5c6671a8cfc17c7ef598e20dcd590573dd1f52 Mon Sep 17 00:00:00 2001 From: ficeto Date: Sat, 16 May 2015 11:03:39 +0300 Subject: [PATCH] enhancements on the FS Api --- .../esp8266/cores/esp8266/FileSystem.cpp | 52 ++++++++++--------- .../esp8266/cores/esp8266/FileSystem.h | 6 +-- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.cpp b/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.cpp index 1abd5dc9f..802029a8d 100755 --- a/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.cpp +++ b/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.cpp @@ -144,14 +144,22 @@ void FSFile::close() { _file = 0; } +char * FSFile::name(){ + return (char*)_stats.name; +} + +bool FSFile::isDirectory(void) { + return _stats.type == SPIFFS_TYPE_DIR; +} + void FSFile::rewindDirectory() { - if (! _file || !isDirectory()) return; + if (!_file || !isDirectory()) return; SPIFFS_closedir(&_dir); SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir); } FSFile FSFile::openNextFile(){ - if (! _file || !isDirectory()) return FSFile(); + if (!_file || !isDirectory()) return FSFile(); struct spiffs_dirent e; struct spiffs_dirent *pe = &e; if ((pe = SPIFFS_readdir(&_dir, pe))){ @@ -161,30 +169,36 @@ FSFile FSFile::openNextFile(){ } uint32_t FSFile::size() { - if(! _file) return 0; - if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0) return 0; + if(!_file) return 0; + if(_stats.size) return _stats.size; + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); + SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); + _stats.size = SPIFFS_tell(&_filesystemStorageHandle, _file); + SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); return _stats.size; } +int FSFile::available() { + if (!_file) return 0; + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); + return size() - pos; +} + uint32_t FSFile::seek(uint32_t pos) { - if (! _file || isDirectory()) return 0; + if (!_file) return 0; return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); } uint32_t FSFile::position() { - if (! _file || isDirectory()) return 0; + if (!_file) return 0; return SPIFFS_tell(&_filesystemStorageHandle, _file); } bool FSFile::eof() { - if (! _file || isDirectory()) return 0; + if (!_file) return 0; return SPIFFS_eof(&_filesystemStorageHandle, _file); } -bool FSFile::isDirectory(void) { - return _stats.type == SPIFFS_TYPE_DIR; -} - int FSFile::read(void *buf, uint16_t nbyte) { if (! _file || isDirectory()) return -1; return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte); @@ -204,12 +218,6 @@ int FSFile::peek() { return c; } -int FSFile::available() { - if (! _file || isDirectory()) return 0; - uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); - return _stats.size - pos; -} - size_t FSFile::write(const uint8_t *buf, size_t size){ if (! _file || isDirectory()) return 0; int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size); @@ -226,10 +234,10 @@ void FSFile::flush(){ SPIFFS_fflush(&_filesystemStorageHandle, _file); } -uint32_t FSFile::remove(){ +bool FSFile::remove(){ if (! _file) return 0; - return SPIFFS_fremove(&_filesystemStorageHandle, _file); - _file = 0; + close(); + return SPIFFS_remove(&_filesystemStorageHandle, (const char *)_stats.name) == 0; } int FSFile::lastError(){ @@ -239,7 +247,3 @@ int FSFile::lastError(){ void FSFile::clearError(){ _filesystemStorageHandle.errno = SPIFFS_OK; } - -char * FSFile::name(){ - return (char*)_stats.name; -} diff --git a/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.h b/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.h index 8956300c1..29173cb4e 100755 --- a/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.h +++ b/hardware/esp8266com/esp8266/cores/esp8266/FileSystem.h @@ -26,8 +26,8 @@ class String; #define FSFILE_READ SPIFFS_RDONLY -#define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT) -#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT) +#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT) +#define FSFILE_OVERWRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT) class FSFile : public Stream { private: @@ -47,11 +47,11 @@ public: virtual void flush(); int read(void *buf, uint16_t nbyte); uint32_t seek(uint32_t pos); - uint32_t remove(); uint32_t position(); uint32_t size(); bool eof(); void close(); + bool remove(); int lastError(); void clearError(); operator bool() { return _file > 0; }