1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Check file path when doing SPIFFS_readdir (#746)

SPIFFS is actually a flat file system, so opendir/readdir always iterate over all files. This adds explicit check that file name returned after readdir starts with the requested pattern.
This commit is contained in:
Ivan Grokhotkov 2015-09-09 01:46:40 +03:00
parent a9d4e6c3e6
commit cd9791eebe

View File

@ -318,8 +318,9 @@ protected:
class SPIFFSDirImpl : public DirImpl {
public:
SPIFFSDirImpl(SPIFFSImpl* fs, spiffs_DIR& dir)
: _fs(fs)
SPIFFSDirImpl(const String& pattern, SPIFFSImpl* fs, spiffs_DIR& dir)
: _pattern(pattern)
, _fs(fs)
, _dir(dir)
, _valid(false)
{
@ -359,12 +360,16 @@ public:
}
bool next() override {
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
_valid = (result != nullptr);
const int n = _pattern.length();
do {
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
_valid = (result != nullptr);
} while(_valid && strncmp((const char*) _dirent.name, _pattern.c_str(), n) != 0);
return _valid;
}
protected:
String _pattern;
SPIFFSImpl* _fs;
spiffs_DIR _dir;
spiffs_dirent _dirent;
@ -394,7 +399,7 @@ DirImplPtr SPIFFSImpl::openDir(const char* path) {
DEBUGV("SPIFFSImpl::openDir: path=`%s` err=%d\r\n", path, _fs.err_code);
return DirImplPtr();
}
return std::make_shared<SPIFFSDirImpl>(this, dir);
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
}
int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {