From d0a944e4178b6264080bf3ccdb5b737af903ca41 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 13 Nov 2015 13:23:16 +0300 Subject: [PATCH] Refactoring of FS::info (#779) --- cores/esp8266/FS.cpp | 4 ++-- cores/esp8266/FS.h | 12 +++++++++++- cores/esp8266/FSImpl.h | 2 +- cores/esp8266/spiffs_api.cpp | 14 +++++++++----- doc/reference.md | 26 ++++++++++++++++++++++++++ tests/FSWrapper/FSWrapper.ino | 15 +++++++++++++++ 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index 5691c2573..55bae8f5c 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -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) { diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index c03e50619..07b58c040 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -85,6 +85,15 @@ protected: 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: @@ -93,7 +102,7 @@ public: 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); @@ -123,6 +132,7 @@ using fs::SeekMode; using fs::SeekSet; using fs::SeekCur; using fs::SeekEnd; +using fs::FSInfo; extern FS SPIFFS; diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index a1124b285..c96ea0721 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -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; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 2d0d90b83..978e2ea0d 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -71,11 +71,15 @@ public: } 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; diff --git a/doc/reference.md b/doc/reference.md index a648d5743..10df55219 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -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. diff --git a/tests/FSWrapper/FSWrapper.ino b/tests/FSWrapper/FSWrapper.ino index 2fefc4ccb..b152093d5 100644 --- a/tests/FSWrapper/FSWrapper.ino +++ b/tests/FSWrapper/FSWrapper.ino @@ -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");