1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-17 12:02:15 +03:00

Filesystem: fix File::available, add File::name

This commit is contained in:
Ivan Grokhotkov
2015-08-05 07:41:12 -04:00
parent 90efba073f
commit b5d9db91aa
4 changed files with 19 additions and 3 deletions

View File

@ -41,7 +41,7 @@ int File::available() {
if (!_p) if (!_p)
return false; return false;
return _p->position() < _p->size(); return _p->size() - _p->position();
} }
int File::read() { int File::read() {
@ -112,6 +112,13 @@ File::operator bool() const {
return !!_p; return !!_p;
} }
const char* File::name() const {
if (!_p)
return nullptr;
return _p->name();
}
File Dir::openFile(const char* mode) { File Dir::openFile(const char* mode) {
if (!_impl) { if (!_impl) {
return File(); return File();

View File

@ -64,6 +64,7 @@ public:
size_t size() const; size_t size() const;
void close(); void close();
operator bool() const; operator bool() const;
const char* name() const;
protected: protected:
FileImplPtr _p; FileImplPtr _p;

View File

@ -33,6 +33,7 @@ public:
virtual size_t position() const = 0; virtual size_t position() const = 0;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual void close() = 0; virtual void close() = 0;
virtual const char* name() const = 0;
}; };
enum OpenMode { enum OpenMode {

View File

@ -259,6 +259,12 @@ public:
DEBUGV("SPIFFS_close: fd=%d\r\n", _fd); DEBUGV("SPIFFS_close: fd=%d\r\n", _fd);
} }
const char* name() const override {
CHECKFD();
return (const char*) _stat.name;
}
protected: protected:
SPIFFSImpl* _fs; SPIFFSImpl* _fs;
spiffs_file _fd; spiffs_file _fd;
@ -283,10 +289,11 @@ public:
return FileImplPtr(); return FileImplPtr();
} }
int mode = getSpiffsMode(openMode, accessMode); int mode = getSpiffsMode(openMode, accessMode);
spiffs_file fd = SPIFFS_open_by_dirent(_fs->getFs(), &_dirent, mode, 0); auto fs = _fs->getFs();
spiffs_file fd = SPIFFS_open_by_dirent(fs, &_dirent, mode, 0);
if (fd < 0) { if (fd < 0) {
DEBUGV("SPIFFSDirImpl::openFile: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n", DEBUGV("SPIFFSDirImpl::openFile: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
fd, _dirent.name, openMode, accessMode, _fs.err_code); fd, _dirent.name, openMode, accessMode, fs->err_code);
return FileImplPtr(); return FileImplPtr();
} }
return std::make_shared<SPIFFSFileImpl>(_fs, fd); return std::make_shared<SPIFFSFileImpl>(_fs, fd);