Skip to content

Commit b1da9ed

Browse files
SD Filesystem compatible with 8266 File, using latest SdFat (#5525)
* Add a FAT filesystem for SD cards to Arduino FS Arduino forked a copy of SD lib several years ago, put their own wrapper around it, and it's been languishing in our ESP8266 libraries ever since as SD. It doesn't support long file names, has class names which conflict with the ESP8266 internal names, and hasn't been updated in ages. The original author of the SD library has continued work in the meantime, and produced a very feature rich implementation of SdFat. It unfortunately also conflicts with the class names we use in ESP8266 Arduino and has a different API than the internal SPIFFS or proposed LittleFS filesystem objects. This PR puts a wrapper around the latest and greatest SdFat library, by forking it and wrapping its classes in a private namespace "sdfat," and making as thin a wrapper as possible around it to conform to the ESP8266 FS, File, and Dir classes. This PR also removes the Arduino SD.h class library and rewrites it using the new SDFS filesystem to make everything in the ESP8266 Arduino core compatible with each other. By doing so it lets us use a single interface for anything needing a file instead of multiple ones (see SDWebServer and how a different object is needed vs. one serving from SPIFFS even though the logic is all the same). Same for BearSSL's CertStores and probably a few others I've missed, cleaning up our code base significantly. Like LittleFS, silently create directories when a file is created with a subdirectory specifier ("/path/to/file.txt") if they do not yet exist. Adds a blacklist of sketches to skip in the CI process (because SdFat has many examples which do not build properly on the ESP8266). Now that LittleFS and SDFS have directory support, the FS needs to be able to communicate whether a name is one or the other. Add a simple bool FS::isDirectory() and bool FS::isFile() method. SPIFFS doesn't have directories, so if it's valid it's a file and reported as such. Add ::mkdir/::rmdir to the FS class to allow users to make and destroy subdirectories. SPIFFS directory operations will, of course, fail and return false. Emulate a 16MB SD card and allow test runner to exercise it by using a custom SdFat HOST_MOCK-enabled object. Throw out the original Arduino SD.h class and rewrite from scratch using only the ESP8266 native SDFS calls. This makes "SD" based applications compatible with normal ESP8266 "File" and "FS" and "SPIFFS" operations. The only major visible change for users is that long filenames now are fully supported and work without any code changes. If there are static arrays of 11 bytes for old 8.3 names in code, they will need to be adjusted. While it is recommended to use the more powerful SDFS class to access SD cards, this SD.h wrapper allows for use of existing Arduino libraries which are built to only with with that SD class. Additional helper functions added to ESP8266 native Filesystem:: classes to help support this portability. The rewrite is good enough to run the original SDWebServer and SD example code without any changes. * Add a FSConfig and SDFSConfig param to FS.begin() Allows for configuration values to be passed into a filesystem via the begin method. By default, a FS will receive a nullptr and should so whatever is appropriate. The base FSConfig class has one parameter, _autoFormat, set by the default constructor to true. For SPIFFS, you can now disable auto formatting on mount failure by passing in a FSConfig(false) object. For SDFS a SDFSConfig parameter can be passed into config specifying the chip select and SPI configuration. If nothing is passed in, the begin will fail since there are no safe default values here. * Add FS::setConfig to set FS-specific options Add a new call, FS::setConfig(const {SDFS,SPIFFS}Config *cfg), which takes a FS-specific configuration object and copies any special settings on a per-FS basis. The call is only valid on unmounted filesystems, and checks the type of object passed in matches the FS being configured. Updates the docs and tests to utilize this new configuration method. * Add ::truncate to File interface Fixes #3846 * Use polledTimeout for formatting yields, cleanup Use the new polledTimeout class to ensure a yield every 5ms while formatting. Add in default case handling and some debug messages when invalid inputs specified. * Make setConfig take const& ref, cleaner code setConfig now can take a parameter defined directly in the call by using a const &ref to it, leading to one less line of code to write and cleaner reading of the code. Also clean up SDFS implementation pointer definition.
1 parent 61a8a6b commit b1da9ed

36 files changed

+1662
-5540
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
[submodule "libraries/SoftwareSerial"]
88
path = libraries/SoftwareSerial
99
url = https://github.com/plerup/espsoftwareserial.git
10+
[submodule "libraries/ESP8266SdFat"]
11+
path = libraries/ESP8266SdFat
12+
url = https://github.com/earlephilhower/ESP8266SdFat.git
1013
[submodule "tools/pyserial"]
1114
path = tools/pyserial
1215
url = https://github.com/pyserial/pyserial.git

cores/esp8266/FS.cpp

+101-4
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,57 @@ File::operator bool() const {
114114
return !!_p;
115115
}
116116

117+
bool File::truncate(uint32_t size) {
118+
if (!_p)
119+
return false;
120+
121+
return _p->truncate(size);
122+
}
123+
117124
const char* File::name() const {
118125
if (!_p)
119126
return nullptr;
120127

121128
return _p->name();
122129
}
123130

131+
const char* File::fullName() const {
132+
if (!_p)
133+
return nullptr;
134+
135+
return _p->fullName();
136+
}
137+
138+
bool File::isFile() const {
139+
if (!_p)
140+
return false;
141+
142+
return _p->isFile();
143+
}
144+
145+
bool File::isDirectory() const {
146+
if (!_p)
147+
return false;
148+
149+
return _p->isDirectory();
150+
}
151+
152+
void File::rewindDirectory() {
153+
if (!_fakeDir) {
154+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
155+
} else {
156+
_fakeDir->rewind();
157+
}
158+
}
159+
160+
File File::openNextFile() {
161+
if (!_fakeDir) {
162+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
163+
}
164+
_fakeDir->next();
165+
return _fakeDir->openFile("r");
166+
}
167+
124168
String File::readString()
125169
{
126170
String ret;
@@ -148,7 +192,7 @@ File Dir::openFile(const char* mode) {
148192
return File();
149193
}
150194

151-
return File(_impl->openFile(om, am));
195+
return File(_impl->openFile(om, am), _baseFS);
152196
}
153197

154198
String Dir::fileName() {
@@ -167,6 +211,20 @@ size_t Dir::fileSize() {
167211
return _impl->fileSize();
168212
}
169213

214+
bool Dir::isFile() const {
215+
if (!_impl)
216+
return false;
217+
218+
return _impl->isFile();
219+
}
220+
221+
bool Dir::isDirectory() const {
222+
if (!_impl)
223+
return false;
224+
225+
return _impl->isDirectory();
226+
}
227+
170228
bool Dir::next() {
171229
if (!_impl) {
172230
return false;
@@ -175,6 +233,22 @@ bool Dir::next() {
175233
return _impl->next();
176234
}
177235

236+
bool Dir::rewind() {
237+
if (!_impl) {
238+
return false;
239+
}
240+
241+
return _impl->rewind();
242+
}
243+
244+
bool FS::setConfig(const FSConfig &cfg) {
245+
if (!_impl) {
246+
return false;
247+
}
248+
249+
return _impl->setConfig(cfg);
250+
}
251+
178252
bool FS::begin() {
179253
if (!_impl) {
180254
return false;
@@ -217,8 +291,7 @@ File FS::open(const char* path, const char* mode) {
217291
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
218292
return File();
219293
}
220-
221-
return File(_impl->open(path, om, am));
294+
return File(_impl->open(path, om, am), this);
222295
}
223296

224297
bool FS::exists(const char* path) {
@@ -236,7 +309,8 @@ Dir FS::openDir(const char* path) {
236309
if (!_impl) {
237310
return Dir();
238311
}
239-
return Dir(_impl->openDir(path));
312+
DirImplPtr p = _impl->openDir(path);
313+
return Dir(p, this);
240314
}
241315

242316
Dir FS::openDir(const String& path) {
@@ -254,6 +328,28 @@ bool FS::remove(const String& path) {
254328
return remove(path.c_str());
255329
}
256330

331+
bool FS::rmdir(const char* path) {
332+
if (!_impl) {
333+
return false;
334+
}
335+
return _impl->rmdir(path);
336+
}
337+
338+
bool FS::rmdir(const String& path) {
339+
return rmdir(path.c_str());
340+
}
341+
342+
bool FS::mkdir(const char* path) {
343+
if (!_impl) {
344+
return false;
345+
}
346+
return _impl->mkdir(path);
347+
}
348+
349+
bool FS::mkdir(const String& path) {
350+
return mkdir(path.c_str());
351+
}
352+
257353
bool FS::rename(const char* pathFrom, const char* pathTo) {
258354
if (!_impl) {
259355
return false;
@@ -266,6 +362,7 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
266362
}
267363

268364

365+
269366
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
270367
switch (mode[0]) {
271368
case 'r':

cores/esp8266/FS.h

+61-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace fs {
2828

2929
class File;
3030
class Dir;
31+
class FS;
3132

3233
class FileImpl;
3334
typedef std::shared_ptr<FileImpl> FileImplPtr;
@@ -48,7 +49,7 @@ enum SeekMode {
4849
class File : public Stream
4950
{
5051
public:
51-
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
52+
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) { }
5253

5354
// Print methods:
5455
size_t write(uint8_t) override;
@@ -72,23 +73,44 @@ class File : public Stream
7273
void close();
7374
operator bool() const;
7475
const char* name() const;
76+
const char* fullName() const; // Includes path
77+
bool truncate(uint32_t size);
78+
79+
bool isFile() const;
80+
bool isDirectory() const;
81+
82+
// Arduino "class SD" methods for compatibility
83+
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
84+
void rewindDirectory();
85+
File openNextFile();
86+
7587
String readString() override;
7688

7789
protected:
7890
FileImplPtr _p;
91+
92+
// Arduino SD class emulation
93+
std::shared_ptr<Dir> _fakeDir;
94+
FS *_baseFS;
7995
};
8096

8197
class Dir {
8298
public:
83-
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
99+
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }
84100

85101
File openFile(const char* mode);
102+
86103
String fileName();
87104
size_t fileSize();
105+
bool isFile() const;
106+
bool isDirectory() const;
107+
88108
bool next();
109+
bool rewind();
89110

90111
protected:
91112
DirImplPtr _impl;
113+
FS *_baseFS;
92114
};
93115

94116
struct FSInfo {
@@ -100,11 +122,41 @@ struct FSInfo {
100122
size_t maxPathLength;
101123
};
102124

125+
class FSConfig
126+
{
127+
public:
128+
FSConfig(bool autoFormat = true) {
129+
_type = FSConfig::fsid::FSId;
130+
_autoFormat = autoFormat;
131+
}
132+
133+
enum fsid { FSId = 0x00000000 };
134+
FSConfig setAutoFormat(bool val = true) {
135+
_autoFormat = val;
136+
return *this;
137+
}
138+
139+
uint32_t _type;
140+
bool _autoFormat;
141+
};
142+
143+
class SPIFFSConfig : public FSConfig
144+
{
145+
public:
146+
SPIFFSConfig(bool autoFormat = true) {
147+
_type = SPIFFSConfig::fsid::FSId;
148+
_autoFormat = autoFormat;
149+
}
150+
enum fsid { FSId = 0x53504946 };
151+
};
152+
103153
class FS
104154
{
105155
public:
106156
FS(FSImplPtr impl) : _impl(impl) { }
107157

158+
bool setConfig(const FSConfig &cfg);
159+
108160
bool begin();
109161
void end();
110162

@@ -126,6 +178,12 @@ class FS
126178
bool rename(const char* pathFrom, const char* pathTo);
127179
bool rename(const String& pathFrom, const String& pathTo);
128180

181+
bool mkdir(const char* path);
182+
bool mkdir(const String& path);
183+
184+
bool rmdir(const char* path);
185+
bool rmdir(const String& path);
186+
129187
protected:
130188
FSImplPtr _impl;
131189
};
@@ -141,6 +199,7 @@ using fs::SeekSet;
141199
using fs::SeekCur;
142200
using fs::SeekEnd;
143201
using fs::FSInfo;
202+
using fs::FSConfig;
144203
#endif //FS_NO_GLOBALS
145204

146205
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)

cores/esp8266/FSImpl.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ class FileImpl {
3434
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
3535
virtual size_t position() const = 0;
3636
virtual size_t size() const = 0;
37+
virtual bool truncate(uint32_t size) = 0;
3738
virtual void close() = 0;
3839
virtual const char* name() const = 0;
40+
virtual const char* fullName() const = 0;
41+
virtual bool isFile() const = 0;
42+
virtual bool isDirectory() const = 0;
3943
};
4044

4145
enum OpenMode {
@@ -57,12 +61,16 @@ class DirImpl {
5761
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5862
virtual const char* fileName() = 0;
5963
virtual size_t fileSize() = 0;
64+
virtual bool isFile() const = 0;
65+
virtual bool isDirectory() const = 0;
6066
virtual bool next() = 0;
67+
virtual bool rewind() = 0;
6168
};
6269

6370
class FSImpl {
6471
public:
6572
virtual ~FSImpl () { }
73+
virtual bool setConfig(const FSConfig &cfg) = 0;
6674
virtual bool begin() = 0;
6775
virtual void end() = 0;
6876
virtual bool format() = 0;
@@ -72,7 +80,8 @@ class FSImpl {
7280
virtual DirImplPtr openDir(const char* path) = 0;
7381
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
7482
virtual bool remove(const char* path) = 0;
75-
83+
virtual bool mkdir(const char* path) = 0;
84+
virtual bool rmdir(const char* path) = 0;
7685
};
7786

7887
} // namespace fs

0 commit comments

Comments
 (0)