Skip to content

Commit d0a944e

Browse files
committed
Refactoring of FS::info (#779)
1 parent d7e340f commit d0a944e

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

cores/esp8266/FS.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ bool FS::format() {
174174
return _impl->format();
175175
}
176176

177-
bool FS::info(uint32_t *total, uint32_t *used){
177+
bool FS::info(FSInfo& info){
178178
if (!_impl) {
179179
return false;
180180
}
181-
return _impl->info(total,used);
181+
return _impl->info(info);
182182
}
183183

184184
File FS::open(const String& path, const char* mode) {

cores/esp8266/FS.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ class Dir {
8585
DirImplPtr _impl;
8686
};
8787

88+
struct FSInfo {
89+
size_t totalBytes;
90+
size_t usedBytes;
91+
size_t blockSize;
92+
size_t pageSize;
93+
size_t maxOpenFiles;
94+
size_t maxPathLength;
95+
};
96+
8897
class FS
8998
{
9099
public:
@@ -93,7 +102,7 @@ class FS
93102
bool begin();
94103

95104
bool format();
96-
bool info(uint32_t *total, uint32_t *used);
105+
bool info(FSInfo& info);
97106

98107
File open(const char* path, const char* mode);
99108
File open(const String& path, const char* mode);
@@ -123,6 +132,7 @@ using fs::SeekMode;
123132
using fs::SeekSet;
124133
using fs::SeekCur;
125134
using fs::SeekEnd;
135+
using fs::FSInfo;
126136

127137
extern FS SPIFFS;
128138

cores/esp8266/FSImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class FSImpl {
6464
public:
6565
virtual bool begin() = 0;
6666
virtual bool format() = 0;
67-
virtual bool info(uint32_t *total, uint32_t *used) = 0;
67+
virtual bool info(FSInfo& info) = 0;
6868
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
6969
virtual bool exists(const char* path) = 0;
7070
virtual DirImplPtr openDir(const char* path) = 0;

cores/esp8266/spiffs_api.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ class SPIFFSImpl : public FSImpl {
7171
}
7272
return true;
7373
}
74-
75-
bool info(uint32_t *total, uint32_t *used) override{
76-
auto rc = SPIFFS_info(&_fs, total, used);
77-
if (rc != SPIFFS_OK) {
78-
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
74+
bool info(FSInfo& info) override {
75+
info.maxOpenFiles = _maxOpenFds;
76+
info.blockSize = _blockSize;
77+
info.pageSize = _pageSize;
78+
info.maxOpenFiles = _maxOpenFds;
79+
info.maxPathLength = SPIFFS_OBJ_NAME_LEN;
80+
auto rc = SPIFFS_info(&_fs, &info.totalBytes, &info.usedBytes);
81+
if (rc != SPIFFS_OK) {
82+
DEBUGV("SPIFFS_info: rc=%d, err=%d\r\n", rc, _fs.err_code);
7983
return false;
8084
}
8185
return true;

doc/reference.md

+26
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,32 @@ SPIFFS.rename(pathFrom, pathTo)
266266
Renames file from `pathFrom` to `pathTo`. Paths must be absolute. Returns *true*
267267
if file was renamed successfully.
268268

269+
#### info
270+
271+
```c++
272+
FSInfo fs_info;
273+
SPIFFS.info(fs_info);
274+
```
275+
276+
Fills [FSInfo structure](#filesystem-information-structure) with information about
277+
the file system. Returns `true` is successful, `false` otherwise.
278+
279+
### Filesystem information structure
280+
281+
```c++
282+
struct FSInfo {
283+
size_t totalBytes;
284+
size_t usedBytes;
285+
size_t blockSize;
286+
size_t pageSize;
287+
size_t maxOpenFiles;
288+
size_t maxPathLength;
289+
};
290+
```
291+
292+
This is the structure which may be filled using FS::info method. Field names
293+
are self-explanatory.
294+
269295
### Directory object (Dir)
270296
271297
The purpose of *Dir* object is to iterate over files inside a directory.

tests/FSWrapper/FSWrapper.ino

+15
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ void setup() {
111111
}
112112
}
113113

114+
{
115+
FSInfo info;
116+
if (!SPIFFS.info(info)) {
117+
fail("info failed");
118+
}
119+
Serial.printf("Total: %u\nUsed: %u\nBlock: %u\nPage: %u\nMax open files: %u\nMax path len: %u\n",
120+
info.totalBytes,
121+
info.usedBytes,
122+
info.blockSize,
123+
info.pageSize,
124+
info.maxOpenFiles,
125+
info.maxPathLength
126+
);
127+
}
128+
114129
{
115130
if (!SPIFFS.format()) {
116131
fail("format failed");

0 commit comments

Comments
 (0)