Skip to content

Commit 44bda41

Browse files
Add FS::info64 call for filesystems > 4GB (esp8266#6154)
Fixes esp8266#6082 Add an info64() call which returns used and total sizes as 64 bit quantities. A default wrapper that just copies the 32-bit values is included for LittleFS/SPIFFS which can't hit those capacities.
1 parent 69311c8 commit 44bda41

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-5
lines changed

cores/esp8266/FS.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ bool FS::info(FSInfo& info){
283283
return _impl->info(info);
284284
}
285285

286+
bool FS::info64(FSInfo64& info){
287+
if (!_impl) {
288+
return false;
289+
}
290+
return _impl->info64(info);
291+
}
292+
286293
File FS::open(const String& path, const char* mode) {
287294
return open(path.c_str(), mode);
288295
}

cores/esp8266/FS.h

+13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Dir {
137137
FS *_baseFS;
138138
};
139139

140+
// Backwards compatible, <4GB filesystem usage
140141
struct FSInfo {
141142
size_t totalBytes;
142143
size_t usedBytes;
@@ -146,6 +147,17 @@ struct FSInfo {
146147
size_t maxPathLength;
147148
};
148149

150+
// Support > 4GB filesystems (SD, etc.)
151+
struct FSInfo64 {
152+
uint64_t totalBytes;
153+
uint64_t usedBytes;
154+
size_t blockSize;
155+
size_t pageSize;
156+
size_t maxOpenFiles;
157+
size_t maxPathLength;
158+
};
159+
160+
149161
class FSConfig
150162
{
151163
public:
@@ -186,6 +198,7 @@ class FS
186198

187199
bool format();
188200
bool info(FSInfo& info);
201+
bool info64(FSInfo64& info);
189202

190203
File open(const char* path, const char* mode);
191204
File open(const String& path, const char* mode);

cores/esp8266/FSImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <stddef.h>
2424
#include <stdint.h>
25+
#include <FS.h>
2526

2627
namespace fs {
2728

@@ -75,6 +76,7 @@ class FSImpl {
7576
virtual void end() = 0;
7677
virtual bool format() = 0;
7778
virtual bool info(FSInfo& info) = 0;
79+
virtual bool info64(FSInfo64& info) = 0;
7880
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
7981
virtual bool exists(const char* path) = 0;
8082
virtual DirImplPtr openDir(const char* path) = 0;

cores/esp8266/spiffs_api.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class SPIFFSImpl : public FSImpl
8585

8686
bool info(FSInfo& info) override
8787
{
88-
info.maxOpenFiles = _maxOpenFds;
8988
info.blockSize = _blockSize;
9089
info.pageSize = _pageSize;
9190
info.maxOpenFiles = _maxOpenFds;
@@ -101,6 +100,20 @@ class SPIFFSImpl : public FSImpl
101100
return true;
102101
}
103102

103+
virtual bool info64(FSInfo64& info64) {
104+
FSInfo i;
105+
if (!info(i)) {
106+
return false;
107+
}
108+
info64.blockSize = i.blockSize;
109+
info64.pageSize = i.pageSize;
110+
info64.maxOpenFiles = i.maxOpenFiles;
111+
info64.maxPathLength = i.maxPathLength;
112+
info64.totalBytes = i.totalBytes;
113+
info64.usedBytes = i.usedBytes;
114+
return true;
115+
}
116+
104117
bool remove(const char* path) override
105118
{
106119
if (!isSpiffsFilenameValid(path)) {

libraries/LittleFS/src/LittleFS.h

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ class LittleFSImpl : public FSImpl
126126
return true;
127127
}
128128

129+
virtual bool info64(FSInfo64& info64) {
130+
FSInfo i;
131+
if (!info(i)) {
132+
return false;
133+
}
134+
info64.blockSize = i.blockSize;
135+
info64.pageSize = i.pageSize;
136+
info64.maxOpenFiles = i.maxOpenFiles;
137+
info64.maxPathLength = i.maxPathLength;
138+
info64.totalBytes = i.totalBytes;
139+
info64.usedBytes = i.usedBytes;
140+
return true;
141+
}
142+
129143
bool remove(const char* path) override {
130144
if (!_mounted || !path || !path[0]) {
131145
return false;

libraries/SDFS/src/SDFS.h

+24-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SDFSImpl : public FSImpl
9494

9595
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
9696

97-
bool exists(const char* path) {
97+
bool exists(const char* path) override {
9898
return _mounted ? _fs.exists(path) : false;
9999
}
100100

@@ -104,7 +104,7 @@ class SDFSImpl : public FSImpl
104104
return _mounted ? _fs.rename(pathFrom, pathTo) : false;
105105
}
106106

107-
bool info(FSInfo& info) override {
107+
bool info64(FSInfo64& info) override {
108108
if (!_mounted) {
109109
DEBUGV("SDFS::info: FS not mounted\n");
110110
return false;
@@ -113,8 +113,28 @@ class SDFSImpl : public FSImpl
113113
info.blockSize = _fs.vol()->blocksPerCluster() * 512;
114114
info.pageSize = 0; // TODO ?
115115
info.maxPathLength = 255; // TODO ?
116-
info.totalBytes =_fs.vol()->volumeBlockCount() * 512;
117-
info.usedBytes = info.totalBytes - (_fs.vol()->freeClusterCount() * _fs.vol()->blocksPerCluster() * 512);
116+
info.totalBytes =_fs.vol()->volumeBlockCount() * 512LL;
117+
info.usedBytes = info.totalBytes - (_fs.vol()->freeClusterCount() * _fs.vol()->blocksPerCluster() * 512LL);
118+
return true;
119+
}
120+
121+
bool info(FSInfo& info) override {
122+
FSInfo64 i;
123+
if (!info64(i)) {
124+
return false;
125+
}
126+
info.blockSize = i.blockSize;
127+
info.pageSize = i.pageSize;
128+
info.maxOpenFiles = i.maxOpenFiles;
129+
info.maxPathLength = i.maxPathLength;
130+
#ifdef DEBUG_ESP_PORT
131+
if (i.totalBytes > (uint64_t)SIZE_MAX) {
132+
// This catches both total and used cases, since used must always be < total.
133+
DEBUG_ESP_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld>= 4GB). Please update source to use info64().\n"), i.totalBytes);
134+
}
135+
#endif
136+
info.totalBytes = (size_t)i.totalBytes;
137+
info.usedBytes = (size_t)i.usedBytes;
118138
return true;
119139
}
120140

0 commit comments

Comments
 (0)