Skip to content

Commit c8b12fd

Browse files
committed
Implement SPIFFS.exists
1 parent f73d414 commit c8b12fd

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

cores/esp8266/FS.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ File FS::open(const char* path, const char* mode) {
193193
return File(_impl->open(path, om, am));
194194
}
195195

196+
bool FS::exists(const char* path) {
197+
if (!_impl) {
198+
return false;
199+
}
200+
return _impl->exists(path);
201+
}
202+
203+
bool FS::exists(const String& path) {
204+
return exists(path.c_str());
205+
}
206+
196207
Dir FS::openDir(const char* path) {
197208
if (!_impl) {
198209
return Dir();

cores/esp8266/FS.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class FS
9797
File open(const char* path, const char* mode);
9898
File open(const String& path, const char* mode);
9999

100+
bool exists(const char* path);
101+
bool exists(const String& path);
102+
100103
Dir openDir(const char* path);
101104
Dir openDir(const String& path);
102105

cores/esp8266/FSImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class FSImpl {
6565
virtual bool begin() = 0;
6666
virtual bool format() = 0;
6767
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
68+
virtual bool exists(const char* path) = 0;
6869
virtual DirImplPtr openDir(const char* path) = 0;
6970
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
7071
virtual bool remove(const char* path) = 0;

cores/esp8266/spiffs_api.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SPIFFSImpl : public FSImpl {
5959
}
6060

6161
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
62-
62+
bool exists(const char* path) override;
6363
DirImplPtr openDir(const char* path) override;
6464

6565
bool rename(const char* pathFrom, const char* pathTo) override {
@@ -404,6 +404,14 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc
404404
return std::make_shared<SPIFFSFileImpl>(this, fd);
405405
}
406406

407+
bool SPIFFSImpl::exists(const char* path) {
408+
char tmpName[SPIFFS_OBJ_NAME_LEN];
409+
strlcpy(tmpName, path, sizeof(tmpName));
410+
spiffs_stat stat;
411+
int rc = SPIFFS_stat(&_fs, tmpName, &stat);
412+
return rc == SPIFFS_OK;
413+
}
414+
407415
DirImplPtr SPIFFSImpl::openDir(const char* path) {
408416
spiffs_DIR dir;
409417
char tmpName[SPIFFS_OBJ_NAME_LEN];

doc/reference.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ if (!f) {
191191
}
192192
```
193193

194+
#### exists
195+
196+
```c++
197+
SPIFFS.exists(path)
198+
```
199+
200+
Returns *true* if a file with given path exists, *false* otherwise.
201+
194202
#### openDir
195203

196204
```c++

0 commit comments

Comments
 (0)