diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index d7217df01..e3158713d 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -193,6 +193,17 @@ File FS::open(const char* path, const char* mode) { return File(_impl->open(path, om, am)); } +bool FS::exists(const char* path) { + if (!_impl) { + return false; + } + return _impl->exists(path); +} + +bool FS::exists(const String& path) { + return exists(path.c_str()); +} + Dir FS::openDir(const char* path) { if (!_impl) { return Dir(); diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 1301a631d..d7a170d1d 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -97,6 +97,9 @@ public: File open(const char* path, const char* mode); File open(const String& path, const char* mode); + bool exists(const char* path); + bool exists(const String& path); + Dir openDir(const char* path); Dir openDir(const String& path); diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index b6bed005e..b95bafcfc 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -65,6 +65,7 @@ public: virtual bool begin() = 0; virtual bool format() = 0; virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0; + virtual bool exists(const char* path) = 0; virtual DirImplPtr openDir(const char* path) = 0; virtual bool rename(const char* pathFrom, const char* pathTo) = 0; virtual bool remove(const char* path) = 0; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index db8311876..4de08c405 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -59,7 +59,7 @@ public: } FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override; - + bool exists(const char* path) override; DirImplPtr openDir(const char* path) override; bool rename(const char* pathFrom, const char* pathTo) override { @@ -404,6 +404,14 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc return std::make_shared(this, fd); } +bool SPIFFSImpl::exists(const char* path) { + char tmpName[SPIFFS_OBJ_NAME_LEN]; + strlcpy(tmpName, path, sizeof(tmpName)); + spiffs_stat stat; + int rc = SPIFFS_stat(&_fs, tmpName, &stat); + return rc == SPIFFS_OK; +} + DirImplPtr SPIFFSImpl::openDir(const char* path) { spiffs_DIR dir; char tmpName[SPIFFS_OBJ_NAME_LEN]; diff --git a/doc/reference.md b/doc/reference.md index 94a303625..f3d5bea8a 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -191,6 +191,14 @@ if (!f) { } ``` +#### exists + +```c++ +SPIFFS.exists(path) +``` + +Returns *true* if a file with given path exists, *false* otherwise. + #### openDir ```c++