mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-17 12:02:15 +03:00
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.
This commit is contained in:
committed by
GitHub
parent
61a8a6b14e
commit
b1da9eda46
@ -114,6 +114,13 @@ File::operator bool() const {
|
||||
return !!_p;
|
||||
}
|
||||
|
||||
bool File::truncate(uint32_t size) {
|
||||
if (!_p)
|
||||
return false;
|
||||
|
||||
return _p->truncate(size);
|
||||
}
|
||||
|
||||
const char* File::name() const {
|
||||
if (!_p)
|
||||
return nullptr;
|
||||
@ -121,6 +128,43 @@ const char* File::name() const {
|
||||
return _p->name();
|
||||
}
|
||||
|
||||
const char* File::fullName() const {
|
||||
if (!_p)
|
||||
return nullptr;
|
||||
|
||||
return _p->fullName();
|
||||
}
|
||||
|
||||
bool File::isFile() const {
|
||||
if (!_p)
|
||||
return false;
|
||||
|
||||
return _p->isFile();
|
||||
}
|
||||
|
||||
bool File::isDirectory() const {
|
||||
if (!_p)
|
||||
return false;
|
||||
|
||||
return _p->isDirectory();
|
||||
}
|
||||
|
||||
void File::rewindDirectory() {
|
||||
if (!_fakeDir) {
|
||||
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
|
||||
} else {
|
||||
_fakeDir->rewind();
|
||||
}
|
||||
}
|
||||
|
||||
File File::openNextFile() {
|
||||
if (!_fakeDir) {
|
||||
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
|
||||
}
|
||||
_fakeDir->next();
|
||||
return _fakeDir->openFile("r");
|
||||
}
|
||||
|
||||
String File::readString()
|
||||
{
|
||||
String ret;
|
||||
@ -148,7 +192,7 @@ File Dir::openFile(const char* mode) {
|
||||
return File();
|
||||
}
|
||||
|
||||
return File(_impl->openFile(om, am));
|
||||
return File(_impl->openFile(om, am), _baseFS);
|
||||
}
|
||||
|
||||
String Dir::fileName() {
|
||||
@ -167,6 +211,20 @@ size_t Dir::fileSize() {
|
||||
return _impl->fileSize();
|
||||
}
|
||||
|
||||
bool Dir::isFile() const {
|
||||
if (!_impl)
|
||||
return false;
|
||||
|
||||
return _impl->isFile();
|
||||
}
|
||||
|
||||
bool Dir::isDirectory() const {
|
||||
if (!_impl)
|
||||
return false;
|
||||
|
||||
return _impl->isDirectory();
|
||||
}
|
||||
|
||||
bool Dir::next() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
@ -175,6 +233,22 @@ bool Dir::next() {
|
||||
return _impl->next();
|
||||
}
|
||||
|
||||
bool Dir::rewind() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->rewind();
|
||||
}
|
||||
|
||||
bool FS::setConfig(const FSConfig &cfg) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->setConfig(cfg);
|
||||
}
|
||||
|
||||
bool FS::begin() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
@ -217,8 +291,7 @@ File FS::open(const char* path, const char* mode) {
|
||||
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
|
||||
return File();
|
||||
}
|
||||
|
||||
return File(_impl->open(path, om, am));
|
||||
return File(_impl->open(path, om, am), this);
|
||||
}
|
||||
|
||||
bool FS::exists(const char* path) {
|
||||
@ -236,7 +309,8 @@ Dir FS::openDir(const char* path) {
|
||||
if (!_impl) {
|
||||
return Dir();
|
||||
}
|
||||
return Dir(_impl->openDir(path));
|
||||
DirImplPtr p = _impl->openDir(path);
|
||||
return Dir(p, this);
|
||||
}
|
||||
|
||||
Dir FS::openDir(const String& path) {
|
||||
@ -254,6 +328,28 @@ bool FS::remove(const String& path) {
|
||||
return remove(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::rmdir(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->rmdir(path);
|
||||
}
|
||||
|
||||
bool FS::rmdir(const String& path) {
|
||||
return rmdir(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::mkdir(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->mkdir(path);
|
||||
}
|
||||
|
||||
bool FS::mkdir(const String& path) {
|
||||
return mkdir(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::rename(const char* pathFrom, const char* pathTo) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
@ -266,6 +362,7 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
|
||||
switch (mode[0]) {
|
||||
case 'r':
|
||||
|
@ -28,6 +28,7 @@ namespace fs {
|
||||
|
||||
class File;
|
||||
class Dir;
|
||||
class FS;
|
||||
|
||||
class FileImpl;
|
||||
typedef std::shared_ptr<FileImpl> FileImplPtr;
|
||||
@ -48,7 +49,7 @@ enum SeekMode {
|
||||
class File : public Stream
|
||||
{
|
||||
public:
|
||||
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
|
||||
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) { }
|
||||
|
||||
// Print methods:
|
||||
size_t write(uint8_t) override;
|
||||
@ -72,23 +73,44 @@ public:
|
||||
void close();
|
||||
operator bool() const;
|
||||
const char* name() const;
|
||||
const char* fullName() const; // Includes path
|
||||
bool truncate(uint32_t size);
|
||||
|
||||
bool isFile() const;
|
||||
bool isDirectory() const;
|
||||
|
||||
// Arduino "class SD" methods for compatibility
|
||||
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
|
||||
void rewindDirectory();
|
||||
File openNextFile();
|
||||
|
||||
String readString() override;
|
||||
|
||||
protected:
|
||||
FileImplPtr _p;
|
||||
|
||||
// Arduino SD class emulation
|
||||
std::shared_ptr<Dir> _fakeDir;
|
||||
FS *_baseFS;
|
||||
};
|
||||
|
||||
class Dir {
|
||||
public:
|
||||
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
|
||||
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }
|
||||
|
||||
File openFile(const char* mode);
|
||||
|
||||
String fileName();
|
||||
size_t fileSize();
|
||||
bool isFile() const;
|
||||
bool isDirectory() const;
|
||||
|
||||
bool next();
|
||||
bool rewind();
|
||||
|
||||
protected:
|
||||
DirImplPtr _impl;
|
||||
FS *_baseFS;
|
||||
};
|
||||
|
||||
struct FSInfo {
|
||||
@ -100,11 +122,41 @@ struct FSInfo {
|
||||
size_t maxPathLength;
|
||||
};
|
||||
|
||||
class FSConfig
|
||||
{
|
||||
public:
|
||||
FSConfig(bool autoFormat = true) {
|
||||
_type = FSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
|
||||
enum fsid { FSId = 0x00000000 };
|
||||
FSConfig setAutoFormat(bool val = true) {
|
||||
_autoFormat = val;
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint32_t _type;
|
||||
bool _autoFormat;
|
||||
};
|
||||
|
||||
class SPIFFSConfig : public FSConfig
|
||||
{
|
||||
public:
|
||||
SPIFFSConfig(bool autoFormat = true) {
|
||||
_type = SPIFFSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
enum fsid { FSId = 0x53504946 };
|
||||
};
|
||||
|
||||
class FS
|
||||
{
|
||||
public:
|
||||
FS(FSImplPtr impl) : _impl(impl) { }
|
||||
|
||||
bool setConfig(const FSConfig &cfg);
|
||||
|
||||
bool begin();
|
||||
void end();
|
||||
|
||||
@ -126,6 +178,12 @@ public:
|
||||
bool rename(const char* pathFrom, const char* pathTo);
|
||||
bool rename(const String& pathFrom, const String& pathTo);
|
||||
|
||||
bool mkdir(const char* path);
|
||||
bool mkdir(const String& path);
|
||||
|
||||
bool rmdir(const char* path);
|
||||
bool rmdir(const String& path);
|
||||
|
||||
protected:
|
||||
FSImplPtr _impl;
|
||||
};
|
||||
@ -141,6 +199,7 @@ using fs::SeekSet;
|
||||
using fs::SeekCur;
|
||||
using fs::SeekEnd;
|
||||
using fs::FSInfo;
|
||||
using fs::FSConfig;
|
||||
#endif //FS_NO_GLOBALS
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
|
||||
|
@ -34,8 +34,12 @@ public:
|
||||
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
|
||||
virtual size_t position() const = 0;
|
||||
virtual size_t size() const = 0;
|
||||
virtual bool truncate(uint32_t size) = 0;
|
||||
virtual void close() = 0;
|
||||
virtual const char* name() const = 0;
|
||||
virtual const char* fullName() const = 0;
|
||||
virtual bool isFile() const = 0;
|
||||
virtual bool isDirectory() const = 0;
|
||||
};
|
||||
|
||||
enum OpenMode {
|
||||
@ -57,12 +61,16 @@ public:
|
||||
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
|
||||
virtual const char* fileName() = 0;
|
||||
virtual size_t fileSize() = 0;
|
||||
virtual bool isFile() const = 0;
|
||||
virtual bool isDirectory() const = 0;
|
||||
virtual bool next() = 0;
|
||||
virtual bool rewind() = 0;
|
||||
};
|
||||
|
||||
class FSImpl {
|
||||
public:
|
||||
virtual ~FSImpl () { }
|
||||
virtual bool setConfig(const FSConfig &cfg) = 0;
|
||||
virtual bool begin() = 0;
|
||||
virtual void end() = 0;
|
||||
virtual bool format() = 0;
|
||||
@ -72,7 +80,8 @@ public:
|
||||
virtual DirImplPtr openDir(const char* path) = 0;
|
||||
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
|
||||
virtual bool remove(const char* path) = 0;
|
||||
|
||||
virtual bool mkdir(const char* path) = 0;
|
||||
virtual bool rmdir(const char* path) = 0;
|
||||
};
|
||||
|
||||
} // namespace fs
|
||||
|
@ -29,7 +29,10 @@
|
||||
#undef max
|
||||
#undef min
|
||||
#include "FSImpl.h"
|
||||
#include "spiffs/spiffs.h"
|
||||
extern "C" {
|
||||
#include "spiffs/spiffs.h"
|
||||
#include "spiffs/spiffs_nucleus.h"
|
||||
};
|
||||
#include "debug.h"
|
||||
#include "flash_utils.h"
|
||||
|
||||
@ -124,6 +127,27 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mkdir(const char* path) override
|
||||
{
|
||||
(void)path;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool rmdir(const char* path) override
|
||||
{
|
||||
(void)path;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool setConfig(const FSConfig &cfg) override
|
||||
{
|
||||
if ((cfg._type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
|
||||
return false;
|
||||
}
|
||||
_cfg = *static_cast<const SPIFFSConfig *>(&cfg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool begin() override
|
||||
{
|
||||
if (SPIFFS_mounted(&_fs) != 0) {
|
||||
@ -136,12 +160,16 @@ public:
|
||||
if (_tryMount()) {
|
||||
return true;
|
||||
}
|
||||
auto rc = SPIFFS_format(&_fs);
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
|
||||
if (_cfg._autoFormat) {
|
||||
auto rc = SPIFFS_format(&_fs);
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
|
||||
return false;
|
||||
}
|
||||
return _tryMount();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return _tryMount();
|
||||
}
|
||||
|
||||
void end() override
|
||||
@ -279,6 +307,8 @@ protected:
|
||||
std::unique_ptr<uint8_t[]> _workBuf;
|
||||
std::unique_ptr<uint8_t[]> _fdsBuf;
|
||||
std::unique_ptr<uint8_t[]> _cacheBuf;
|
||||
|
||||
SPIFFSConfig _cfg;
|
||||
};
|
||||
|
||||
#define CHECKFD() while (_fd == 0) { panic(); }
|
||||
@ -375,6 +405,29 @@ public:
|
||||
return _stat.size;
|
||||
}
|
||||
|
||||
bool truncate(uint32_t size) override
|
||||
{
|
||||
CHECKFD();
|
||||
spiffs_fd *sfd;
|
||||
if (spiffs_fd_get(_fs->getFs(), _fd, &sfd) == SPIFFS_OK) {
|
||||
return SPIFFS_OK == spiffs_object_truncate(sfd, size, 0);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isFile() const override
|
||||
{
|
||||
// No such thing as directories on SPIFFS
|
||||
return _fd ? true : false;
|
||||
}
|
||||
|
||||
bool isDirectory() const override
|
||||
{
|
||||
// No such thing as directories on SPIFFS
|
||||
return false;
|
||||
}
|
||||
|
||||
void close() override
|
||||
{
|
||||
CHECKFD();
|
||||
@ -390,6 +443,11 @@ public:
|
||||
return (const char*) _stat.name;
|
||||
}
|
||||
|
||||
const char* fullName() const override
|
||||
{
|
||||
return name(); // No dirs, they're the same on SPIFFS
|
||||
}
|
||||
|
||||
protected:
|
||||
void _getStat() const
|
||||
{
|
||||
@ -415,6 +473,7 @@ public:
|
||||
: _pattern(pattern)
|
||||
, _fs(fs)
|
||||
, _dir(dir)
|
||||
, _dirHead(dir)
|
||||
, _valid(false)
|
||||
{
|
||||
}
|
||||
@ -458,6 +517,18 @@ public:
|
||||
return _dirent.size;
|
||||
}
|
||||
|
||||
bool isFile() const override
|
||||
{
|
||||
// No such thing as directories on SPIFFS
|
||||
return _valid;
|
||||
}
|
||||
|
||||
bool isDirectory() const override
|
||||
{
|
||||
// No such thing as directories on SPIFFS
|
||||
return false;
|
||||
}
|
||||
|
||||
bool next() override
|
||||
{
|
||||
const int n = _pattern.length();
|
||||
@ -468,13 +539,20 @@ public:
|
||||
return _valid;
|
||||
}
|
||||
|
||||
bool rewind() override
|
||||
{
|
||||
_dir = _dirHead;
|
||||
_valid = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
String _pattern;
|
||||
SPIFFSImpl* _fs;
|
||||
spiffs_DIR _dir;
|
||||
spiffs_DIR _dirHead; // The pointer to the start of this dir
|
||||
spiffs_dirent _dirent;
|
||||
bool _valid;
|
||||
};
|
||||
|
||||
|
||||
#endif//spiffs_api_h
|
||||
|
Reference in New Issue
Block a user