Berry support for SD card

This commit is contained in:
Stephan Hadinger 2022-04-16 12:29:01 +02:00
parent dafc5d6f45
commit b422274e00
4 changed files with 45 additions and 31 deletions

View File

@ -393,8 +393,26 @@ bool ZipArchive::parse(void) {
** **
********************************************************************/ ********************************************************************/
/* get the FS corresponding to the prefix, typically /sd/ for sdcard */
FS * ZipReadFSImpl::getFS(const char *path) const {
if (_fs == nullptr) { return nullptr; }
if (_alt_fs == nullptr || _alt_fs_prefix == nullptr) { return *_fs; }
/* from now neither _fs not _alt_fs are null
/* if however they point to the same value, then we don't have an alternative FS */
if (*_fs == *_alt_fs) { return *_fs; }
/* check prefix */
if (strncmp(_alt_fs_prefix, path, strlen(_alt_fs_prefix))) {
return *_alt_fs;
} else {
return *_fs;
}
}
FileImplPtr ZipReadFSImpl::open(const char* path, const char* mode, const bool create) { FileImplPtr ZipReadFSImpl::open(const char* path, const char* mode, const bool create) {
if (*_fs == nullptr) { return nullptr; } FS * fs = getFS(path);
if (fs == nullptr) { return nullptr; }
if (strchr(path, '#')) { if (strchr(path, '#')) {
// we don't support any other mode than "r" and no-create // we don't support any other mode than "r" and no-create
@ -414,7 +432,7 @@ FileImplPtr ZipReadFSImpl::open(const char* path, const char* mode, const bool c
if (*suffix == '/') { suffix++; } if (*suffix == '/') { suffix++; }
// AddLog(LOG_LEVEL_DEBUG, "ZIP: prefix=%s suffix=%s", prefix, suffix); // AddLog(LOG_LEVEL_DEBUG, "ZIP: prefix=%s suffix=%s", prefix, suffix);
// parse ZIP archive // parse ZIP archive
File zipfile = (*_fs)->open(prefix, "r", false); File zipfile = fs->open(prefix, "r", false);
if ((bool)zipfile) { if ((bool)zipfile) {
// we could read the file // we could read the file
ZipArchive zip_archive = ZipArchive(&zipfile); ZipArchive zip_archive = ZipArchive(&zipfile);
@ -424,7 +442,7 @@ FileImplPtr ZipReadFSImpl::open(const char* path, const char* mode, const bool c
if (entry.file_name.equals(suffix)) { if (entry.file_name.equals(suffix)) {
// found // found
// AddLog(LOG_LEVEL_DEBUG, "ZIP: file '%s' in archive (start=%i - len=%i - last_mod=%i)", suffix, entry.file_start, entry.file_len, entry.last_mod); // AddLog(LOG_LEVEL_DEBUG, "ZIP: file '%s' in archive (start=%i - len=%i - last_mod=%i)", suffix, entry.file_start, entry.file_len, entry.last_mod);
return ZipItemImplPtr(new ZipItemImpl((*_fs)->open(prefix, "r", false), entry.file_start, entry.file_len, entry.last_mod)); return ZipItemImplPtr(new ZipItemImpl(fs->open(prefix, "r", false), entry.file_start, entry.file_len, entry.last_mod));
} }
} }
return ZipReadFileImplPtr(); // return an error return ZipReadFileImplPtr(); // return an error
@ -434,12 +452,13 @@ FileImplPtr ZipReadFSImpl::open(const char* path, const char* mode, const bool c
} }
} else { } else {
// simple file, do nothing // simple file, do nothing
return ZipReadFileImplPtr(new ZipReadFileImpl((*_fs)->open(path, mode, create))); return ZipReadFileImplPtr(new ZipReadFileImpl(fs->open(path, mode, create)));
} }
} }
bool ZipReadFSImpl::exists(const char* path) { bool ZipReadFSImpl::exists(const char* path) {
if (*_fs == nullptr) { return false; } FS * fs = getFS(path);
if (fs == nullptr) { return false; }
if (strchr(path, '#')) { if (strchr(path, '#')) {
// treat as a ZIP archive // treat as a ZIP archive
@ -451,7 +470,7 @@ bool ZipReadFSImpl::exists(const char* path) {
char *prefix = strtok_r(sub_path, "#", &tok); char *prefix = strtok_r(sub_path, "#", &tok);
char *suffix = strtok_r(NULL, "", &tok); char *suffix = strtok_r(NULL, "", &tok);
// parse ZIP archive // parse ZIP archive
File zipfile = (*_fs)->open(prefix, "r", false); File zipfile = fs->open(prefix, "r", false);
if ((bool)zipfile) { if ((bool)zipfile) {
// we could read the file // we could read the file
ZipArchive zip_archive = ZipArchive(&zipfile); ZipArchive zip_archive = ZipArchive(&zipfile);
@ -466,7 +485,7 @@ bool ZipReadFSImpl::exists(const char* path) {
return false; return false;
} else { } else {
// simple file, do nothing // simple file, do nothing
return (*_fs)->exists(path); return fs->exists(path);
} }
} }

View File

@ -17,40 +17,32 @@ typedef std::shared_ptr<FSImpl> ZipReadFSImplPtr;
class ZipReadFSImpl : public FSImpl { class ZipReadFSImpl : public FSImpl {
public: public:
ZipReadFSImpl(FS **fs) : _fs(fs) {}; ZipReadFSImpl(FS **fs) : _fs(fs), _alt_fs(nullptr), _alt_fs_prefix(nullptr) {};
ZipReadFSImpl(FS **fs, const char *alt_fs_prefix, FS **alt_fs) : _fs(fs), _alt_fs(alt_fs), _alt_fs_prefix(alt_fs_prefix) {};
virtual ~ZipReadFSImpl(); virtual ~ZipReadFSImpl();
/* get the FS corresponding to the prefix, typically /sd/ for sdcard */
FS * getFS(const char *path) const;
FileImplPtr open(const char* path, const char* mode, const bool create); FileImplPtr open(const char* path, const char* mode, const bool create);
bool exists(const char* path); bool exists(const char* path);
bool rename(const char* pathFrom, const char* pathTo) { bool rename(const char* pathFrom, const char* pathTo) {
if (*_fs) { FS * fs = getFS(pathFrom);
return (*_fs)->rename(pathFrom, pathTo); return fs ? fs->rename(pathFrom, pathTo) : false;
} else {
return false;
}
} }
bool remove(const char* path) { bool remove(const char* path) {
if (*_fs) { FS * fs = getFS(path);
return (*_fs)->remove(path); return fs ? fs->remove(path) : false;
} else {
return false;
}
} }
bool mkdir(const char *path) { bool mkdir(const char *path) {
if (*_fs) { FS * fs = getFS(path);
return (*_fs)->mkdir(path); return fs ? fs->mkdir(path) : false;
} else {
return false;
}
} }
bool rmdir(const char *path) { bool rmdir(const char *path) {
if (*_fs) { FS * fs = getFS(path);
return (*_fs)->rmdir(path); return fs ? fs->rmdir(path) : false;
} else {
return false;
}
} }
void mountpoint(const char *) { void mountpoint(const char *) {
}; };
@ -60,6 +52,8 @@ public:
private: private:
FS **_fs; FS **_fs;
FS **_alt_fs;
const char *_alt_fs_prefix;
}; };
#endif // ESP32 #endif // ESP32

View File

@ -110,10 +110,10 @@ void UfsInitOnce(void) {
#ifdef ESP32 #ifdef ESP32
// try lfs first // try lfs first
ffsp = &LittleFS; ffsp = &LittleFS;
if (!LittleFS.begin(true)) { if (!LittleFS.begin(true, "")) { // force empty mount point to make it the fallback FS
// ffat is second // ffat is second
ffsp = &FFat; ffsp = &FFat;
if (!FFat.begin(true)) { if (!FFat.begin(true, "")) {
ffsp = nullptr; ffsp = nullptr;
return; return;
} }

View File

@ -119,7 +119,8 @@ void lv_flush_callback(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *c
#include <FS.h> #include <FS.h>
#include "ZipReadFS.h" #include "ZipReadFS.h"
extern FS *ffsp; extern FS *ffsp;
FS lv_zip_ufsp(ZipReadFSImplPtr(new ZipReadFSImpl(&ffsp))); extern FS *ufsp;
FS lv_zip_ufsp(ZipReadFSImplPtr(new ZipReadFSImpl(&ffsp, "/sd/", &ufsp)));
extern "C" { extern "C" {