Skip to content

Commit 4556403

Browse files
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.
1 parent 3a36501 commit 4556403

34 files changed

+1453
-5533
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
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

cores/esp8266/FS.cpp

+87-4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,44 @@ const char* File::name() const {
121121
return _p->name();
122122
}
123123

124+
const char* File::fullName() const {
125+
if (!_p)
126+
return nullptr;
127+
128+
return _p->fullName();
129+
}
130+
131+
bool File::isFile() const {
132+
if (!_p)
133+
return false;
134+
135+
return _p->isFile();
136+
}
137+
138+
bool File::isDirectory() const {
139+
if (!_p)
140+
return false;
141+
142+
return _p->isDirectory();
143+
}
144+
145+
void File::rewindDirectory() {
146+
if (!_fakeDir) {
147+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
148+
} else {
149+
_fakeDir->rewind();
150+
}
151+
}
152+
153+
File File::openNextFile() {
154+
if (!_fakeDir) {
155+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
156+
}
157+
_fakeDir->next();
158+
return _fakeDir->openFile("r");
159+
}
160+
161+
124162
File Dir::openFile(const char* mode) {
125163
if (!_impl) {
126164
return File();
@@ -133,7 +171,7 @@ File Dir::openFile(const char* mode) {
133171
return File();
134172
}
135173

136-
return File(_impl->openFile(om, am));
174+
return File(_impl->openFile(om, am), _baseFS);
137175
}
138176

139177
String Dir::fileName() {
@@ -152,6 +190,20 @@ size_t Dir::fileSize() {
152190
return _impl->fileSize();
153191
}
154192

193+
bool Dir::isFile() const {
194+
if (!_impl)
195+
return false;
196+
197+
return _impl->isFile();
198+
}
199+
200+
bool Dir::isDirectory() const {
201+
if (!_impl)
202+
return false;
203+
204+
return _impl->isDirectory();
205+
}
206+
155207
bool Dir::next() {
156208
if (!_impl) {
157209
return false;
@@ -160,6 +212,14 @@ bool Dir::next() {
160212
return _impl->next();
161213
}
162214

215+
bool Dir::rewind() {
216+
if (!_impl) {
217+
return false;
218+
}
219+
220+
return _impl->rewind();
221+
}
222+
163223
bool FS::begin() {
164224
if (!_impl) {
165225
return false;
@@ -202,8 +262,7 @@ File FS::open(const char* path, const char* mode) {
202262
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
203263
return File();
204264
}
205-
206-
return File(_impl->open(path, om, am));
265+
return File(_impl->open(path, om, am), this);
207266
}
208267

209268
bool FS::exists(const char* path) {
@@ -221,7 +280,8 @@ Dir FS::openDir(const char* path) {
221280
if (!_impl) {
222281
return Dir();
223282
}
224-
return Dir(_impl->openDir(path));
283+
DirImplPtr p = _impl->openDir(path);
284+
return Dir(p, this);
225285
}
226286

227287
Dir FS::openDir(const String& path) {
@@ -239,6 +299,28 @@ bool FS::remove(const String& path) {
239299
return remove(path.c_str());
240300
}
241301

302+
bool FS::rmdir(const char* path) {
303+
if (!_impl) {
304+
return false;
305+
}
306+
return _impl->rmdir(path);
307+
}
308+
309+
bool FS::rmdir(const String& path) {
310+
return rmdir(path.c_str());
311+
}
312+
313+
bool FS::mkdir(const char* path) {
314+
if (!_impl) {
315+
return false;
316+
}
317+
return _impl->mkdir(path);
318+
}
319+
320+
bool FS::mkdir(const String& path) {
321+
return mkdir(path.c_str());
322+
}
323+
242324
bool FS::rename(const char* pathFrom, const char* pathTo) {
243325
if (!_impl) {
244326
return false;
@@ -251,6 +333,7 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
251333
}
252334

253335

336+
254337
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
255338
switch (mode[0]) {
256339
case 'r':

cores/esp8266/FS.h

+28-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,22 +73,41 @@ class File : public Stream
7273
void close();
7374
operator bool() const;
7475
const char* name() const;
76+
const char* fullName() const; // Includes path
77+
78+
bool isFile() const;
79+
bool isDirectory() const;
80+
81+
// Arduino "class SD" methods for compatibility
82+
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
83+
void rewindDirectory();
84+
File openNextFile();
7585

7686
protected:
7787
FileImplPtr _p;
88+
89+
// Arduino SD class emulation
90+
std::shared_ptr<Dir> _fakeDir;
91+
FS *_baseFS;
7892
};
7993

8094
class Dir {
8195
public:
82-
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
96+
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }
8397

8498
File openFile(const char* mode);
99+
85100
String fileName();
86101
size_t fileSize();
102+
bool isFile() const;
103+
bool isDirectory() const;
104+
87105
bool next();
106+
bool rewind();
88107

89108
protected:
90109
DirImplPtr _impl;
110+
FS *_baseFS;
91111
};
92112

93113
struct FSInfo {
@@ -125,6 +145,12 @@ class FS
125145
bool rename(const char* pathFrom, const char* pathTo);
126146
bool rename(const String& pathFrom, const String& pathTo);
127147

148+
bool mkdir(const char* path);
149+
bool mkdir(const String& path);
150+
151+
bool rmdir(const char* path);
152+
bool rmdir(const String& path);
153+
128154
protected:
129155
FSImplPtr _impl;
130156
};

cores/esp8266/FSImpl.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class FileImpl {
3636
virtual size_t size() const = 0;
3737
virtual void close() = 0;
3838
virtual const char* name() const = 0;
39+
virtual const char* fullName() const = 0;
40+
virtual bool isFile() const = 0;
41+
virtual bool isDirectory() const = 0;
3942
};
4043

4144
enum OpenMode {
@@ -57,7 +60,10 @@ class DirImpl {
5760
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5861
virtual const char* fileName() = 0;
5962
virtual size_t fileSize() = 0;
63+
virtual bool isFile() const = 0;
64+
virtual bool isDirectory() const = 0;
6065
virtual bool next() = 0;
66+
virtual bool rewind() = 0;
6167
};
6268

6369
class FSImpl {
@@ -72,7 +78,8 @@ class FSImpl {
7278
virtual DirImplPtr openDir(const char* path) = 0;
7379
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
7480
virtual bool remove(const char* path) = 0;
75-
81+
virtual bool mkdir(const char* path) = 0;
82+
virtual bool rmdir(const char* path) = 0;
7683
};
7784

7885
} // namespace fs

cores/esp8266/spiffs_api.h

+50
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ class SPIFFSImpl : public FSImpl
124124
return true;
125125
}
126126

127+
bool mkdir(const char* path) override
128+
{
129+
(void)path;
130+
return false;
131+
}
132+
133+
bool rmdir(const char* path) override
134+
{
135+
(void)path;
136+
return false;
137+
}
138+
127139
bool begin() override
128140
{
129141
#if defined(ARDUINO) && !defined(CORE_MOCK)
@@ -376,6 +388,18 @@ class SPIFFSFileImpl : public FileImpl
376388
return _stat.size;
377389
}
378390

391+
bool isFile() const override
392+
{
393+
// No such thing as directories on SPIFFS
394+
return _fd ? true : false;
395+
}
396+
397+
bool isDirectory() const override
398+
{
399+
// No such thing as directories on SPIFFS
400+
return false;
401+
}
402+
379403
void close() override
380404
{
381405
CHECKFD();
@@ -391,6 +415,11 @@ class SPIFFSFileImpl : public FileImpl
391415
return (const char*) _stat.name;
392416
}
393417

418+
const char* fullName() const override
419+
{
420+
return name(); // No dirs, they're the same on SPIFFS
421+
}
422+
394423
protected:
395424
void _getStat() const
396425
{
@@ -416,6 +445,7 @@ class SPIFFSDirImpl : public DirImpl
416445
: _pattern(pattern)
417446
, _fs(fs)
418447
, _dir(dir)
448+
, _dirHead(dir)
419449
, _valid(false)
420450
{
421451
}
@@ -459,6 +489,18 @@ class SPIFFSDirImpl : public DirImpl
459489
return _dirent.size;
460490
}
461491

492+
bool isFile() const override
493+
{
494+
// No such thing as directories on SPIFFS
495+
return _valid;
496+
}
497+
498+
bool isDirectory() const override
499+
{
500+
// No such thing as directories on SPIFFS
501+
return false;
502+
}
503+
462504
bool next() override
463505
{
464506
const int n = _pattern.length();
@@ -469,10 +511,18 @@ class SPIFFSDirImpl : public DirImpl
469511
return _valid;
470512
}
471513

514+
bool rewind() override
515+
{
516+
_dir = _dirHead;
517+
_valid = false;
518+
return true;
519+
}
520+
472521
protected:
473522
String _pattern;
474523
SPIFFSImpl* _fs;
475524
spiffs_DIR _dir;
525+
spiffs_DIR _dirHead; // The pointer to the start of this dir
476526
spiffs_dirent _dirent;
477527
bool _valid;
478528
};

libraries/ESP8266SdFat

Submodule ESP8266SdFat added at ff64b2d

libraries/SD/README.adoc

-24
This file was deleted.

libraries/SD/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Arduino "class SD" shim wrapper
2+
3+
This is a simple wrapper class to replace the ancient Arduino SD.h
4+
access method for SD cards. It calls the underlying SDFS and the latest
5+
SdFat lib to do all the work, and is now compatible with the rest of the
6+
ESP8266 filesystem things.
7+
8+
-Earle F. Philhower, III
9+

0 commit comments

Comments
 (0)