Skip to content

Commit

Permalink
Refactoring of FS::info (esp8266#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Nov 13, 2015
1 parent d7e340f commit d0a944e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ bool FS::format() {
return _impl->format();
}

bool FS::info(uint32_t *total, uint32_t *used){
bool FS::info(FSInfo& info){
if (!_impl) {
return false;
}
return _impl->info(total,used);
return _impl->info(info);
}

File FS::open(const String& path, const char* mode) {
Expand Down
12 changes: 11 additions & 1 deletion cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class Dir {
DirImplPtr _impl;
};

struct FSInfo {
size_t totalBytes;
size_t usedBytes;
size_t blockSize;
size_t pageSize;
size_t maxOpenFiles;
size_t maxPathLength;
};

class FS
{
public:
Expand All @@ -93,7 +102,7 @@ class FS
bool begin();

bool format();
bool info(uint32_t *total, uint32_t *used);
bool info(FSInfo& info);

File open(const char* path, const char* mode);
File open(const String& path, const char* mode);
Expand Down Expand Up @@ -123,6 +132,7 @@ using fs::SeekMode;
using fs::SeekSet;
using fs::SeekCur;
using fs::SeekEnd;
using fs::FSInfo;

extern FS SPIFFS;

Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FSImpl {
public:
virtual bool begin() = 0;
virtual bool format() = 0;
virtual bool info(uint32_t *total, uint32_t *used) = 0;
virtual bool info(FSInfo& info) = 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;
Expand Down
14 changes: 9 additions & 5 deletions cores/esp8266/spiffs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ class SPIFFSImpl : public FSImpl {
}
return true;
}

bool info(uint32_t *total, uint32_t *used) override{
auto rc = SPIFFS_info(&_fs, total, used);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
bool info(FSInfo& info) override {
info.maxOpenFiles = _maxOpenFds;
info.blockSize = _blockSize;
info.pageSize = _pageSize;
info.maxOpenFiles = _maxOpenFds;
info.maxPathLength = SPIFFS_OBJ_NAME_LEN;
auto rc = SPIFFS_info(&_fs, &info.totalBytes, &info.usedBytes);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_info: rc=%d, err=%d\r\n", rc, _fs.err_code);
return false;
}
return true;
Expand Down
26 changes: 26 additions & 0 deletions doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,32 @@ SPIFFS.rename(pathFrom, pathTo)
Renames file from `pathFrom` to `pathTo`. Paths must be absolute. Returns *true*
if file was renamed successfully.

#### info

```c++
FSInfo fs_info;
SPIFFS.info(fs_info);
```

Fills [FSInfo structure](#filesystem-information-structure) with information about
the file system. Returns `true` is successful, `false` otherwise.

### Filesystem information structure

```c++
struct FSInfo {
size_t totalBytes;
size_t usedBytes;
size_t blockSize;
size_t pageSize;
size_t maxOpenFiles;
size_t maxPathLength;
};
```
This is the structure which may be filled using FS::info method. Field names
are self-explanatory.
### Directory object (Dir)
The purpose of *Dir* object is to iterate over files inside a directory.
Expand Down
15 changes: 15 additions & 0 deletions tests/FSWrapper/FSWrapper.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ void setup() {
}
}

{
FSInfo info;
if (!SPIFFS.info(info)) {
fail("info failed");
}
Serial.printf("Total: %u\nUsed: %u\nBlock: %u\nPage: %u\nMax open files: %u\nMax path len: %u\n",
info.totalBytes,
info.usedBytes,
info.blockSize,
info.pageSize,
info.maxOpenFiles,
info.maxPathLength
);
}

{
if (!SPIFFS.format()) {
fail("format failed");
Expand Down

0 comments on commit d0a944e

Please sign in to comment.