1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

enhancements on the FS Api

This commit is contained in:
ficeto
2015-05-16 11:03:39 +03:00
parent c4dca3bd50
commit fe5c6671a8
2 changed files with 31 additions and 27 deletions

View File

@ -144,6 +144,14 @@ void FSFile::close() {
_file = 0; _file = 0;
} }
char * FSFile::name(){
return (char*)_stats.name;
}
bool FSFile::isDirectory(void) {
return _stats.type == SPIFFS_TYPE_DIR;
}
void FSFile::rewindDirectory() { void FSFile::rewindDirectory() {
if (!_file || !isDirectory()) return; if (!_file || !isDirectory()) return;
SPIFFS_closedir(&_dir); SPIFFS_closedir(&_dir);
@ -162,29 +170,35 @@ FSFile FSFile::openNextFile(){
uint32_t FSFile::size() { uint32_t FSFile::size() {
if(!_file) return 0; if(!_file) return 0;
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0) 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; 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) { 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); return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
} }
uint32_t FSFile::position() { uint32_t FSFile::position() {
if (! _file || isDirectory()) return 0; if (!_file) return 0;
return SPIFFS_tell(&_filesystemStorageHandle, _file); return SPIFFS_tell(&_filesystemStorageHandle, _file);
} }
bool FSFile::eof() { bool FSFile::eof() {
if (! _file || isDirectory()) return 0; if (!_file) return 0;
return SPIFFS_eof(&_filesystemStorageHandle, _file); return SPIFFS_eof(&_filesystemStorageHandle, _file);
} }
bool FSFile::isDirectory(void) {
return _stats.type == SPIFFS_TYPE_DIR;
}
int FSFile::read(void *buf, uint16_t nbyte) { int FSFile::read(void *buf, uint16_t nbyte) {
if (! _file || isDirectory()) return -1; if (! _file || isDirectory()) return -1;
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte); return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
@ -204,12 +218,6 @@ int FSFile::peek() {
return c; 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){ size_t FSFile::write(const uint8_t *buf, size_t size){
if (! _file || isDirectory()) return 0; if (! _file || isDirectory()) return 0;
int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size); int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
@ -226,10 +234,10 @@ void FSFile::flush(){
SPIFFS_fflush(&_filesystemStorageHandle, _file); SPIFFS_fflush(&_filesystemStorageHandle, _file);
} }
uint32_t FSFile::remove(){ bool FSFile::remove(){
if (! _file) return 0; if (! _file) return 0;
return SPIFFS_fremove(&_filesystemStorageHandle, _file); close();
_file = 0; return SPIFFS_remove(&_filesystemStorageHandle, (const char *)_stats.name) == 0;
} }
int FSFile::lastError(){ int FSFile::lastError(){
@ -239,7 +247,3 @@ int FSFile::lastError(){
void FSFile::clearError(){ void FSFile::clearError(){
_filesystemStorageHandle.errno = SPIFFS_OK; _filesystemStorageHandle.errno = SPIFFS_OK;
} }
char * FSFile::name(){
return (char*)_stats.name;
}

View File

@ -26,8 +26,8 @@
class String; class String;
#define FSFILE_READ SPIFFS_RDONLY #define FSFILE_READ SPIFFS_RDONLY
#define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT) #define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT) #define FSFILE_OVERWRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
class FSFile : public Stream { class FSFile : public Stream {
private: private:
@ -47,11 +47,11 @@ public:
virtual void flush(); virtual void flush();
int read(void *buf, uint16_t nbyte); int read(void *buf, uint16_t nbyte);
uint32_t seek(uint32_t pos); uint32_t seek(uint32_t pos);
uint32_t remove();
uint32_t position(); uint32_t position();
uint32_t size(); uint32_t size();
bool eof(); bool eof();
void close(); void close();
bool remove();
int lastError(); int lastError();
void clearError(); void clearError();
operator bool() { return _file > 0; } operator bool() { return _file > 0; }