diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index 938fe55..44e3469 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -249,7 +249,53 @@ bool Epub::load() { return true; } -void Epub::clearCache() const { SD.rmdir(cachePath.c_str()); } +bool removeDir(const char *path) { + // 1. Open the directory + File dir = SD.open(path); + if (!dir) { + return false; + } + if (!dir.isDirectory()) { + return false; + } + + File file = dir.openNextFile(); + while (file) { + String filePath = path; + if (!filePath.endsWith("/")) { + filePath += "/"; + } + filePath += file.name(); + + if (file.isDirectory()) { + if (!removeDir(filePath.c_str())) { + return false; + } + } else { + if (!SD.remove(filePath.c_str())) { + return false; + } + } + file = dir.openNextFile(); + } + + return SD.rmdir(path); +} + +bool Epub::clearCache() const { + if (!SD.exists(cachePath.c_str())) { + Serial.printf("[%lu] [EPB] Cache does not exist, no action needed\n", millis()); + return true; + } + + if (!removeDir(cachePath.c_str())) { + Serial.printf("[%lu] [EPB] Failed to clear cache\n", millis()); + return false; + } + + Serial.printf("[%lu] [EPB] Cache cleared successfully\n", millis()); + return true; +} void Epub::setupCacheDir() const { if (SD.exists(cachePath.c_str())) { diff --git a/lib/Epub/Epub.h b/lib/Epub/Epub.h index b1cf4a9..5cdfee4 100644 --- a/lib/Epub/Epub.h +++ b/lib/Epub/Epub.h @@ -50,7 +50,7 @@ class Epub { ~Epub() = default; std::string& getBasePath() { return contentBasePath; } bool load(); - void clearCache() const; + bool clearCache() const; void setupCacheDir() const; const std::string& getCachePath() const; const std::string& getPath() const; diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 26c668a..ff7b337 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -55,8 +55,8 @@ bool Section::loadCacheMetadata(const int fontId, const float lineCompression, c serialization::readPod(inputFile, version); if (version != SECTION_FILE_VERSION) { inputFile.close(); - clearCache(); Serial.printf("[%lu] [SCT] Deserialization failed: Unknown version %u\n", millis(), version); + clearCache(); return false; } @@ -72,8 +72,8 @@ bool Section::loadCacheMetadata(const int fontId, const float lineCompression, c if (fontId != fileFontId || lineCompression != fileLineCompression || marginTop != fileMarginTop || marginRight != fileMarginRight || marginBottom != fileMarginBottom || marginLeft != fileMarginLeft) { inputFile.close(); - clearCache(); Serial.printf("[%lu] [SCT] Deserialization failed: Parameters do not match\n", millis()); + clearCache(); return false; } } @@ -89,7 +89,54 @@ void Section::setupCacheDir() const { SD.mkdir(cachePath.c_str()); } -void Section::clearCache() const { SD.rmdir(cachePath.c_str()); } +bool removeDir(const char *path) { + // 1. Open the directory + File dir = SD.open(path); + if (!dir) { + return false; + } + if (!dir.isDirectory()) { + return false; + } + + File file = dir.openNextFile(); + while (file) { + String filePath = path; + if (!filePath.endsWith("/")) { + filePath += "/"; + } + filePath += file.name(); + + if (file.isDirectory()) { + if (!removeDir(filePath.c_str())) { + return false; + } + } else { + if (!SD.remove(filePath.c_str())) { + return false; + } + } + file = dir.openNextFile(); + } + + return SD.rmdir(path); +} + +// Your updated class method (assuming you are using the 'SD' object, which is a wrapper for a specific filesystem) +bool Section::clearCache() const { + if (!SD.exists(cachePath.c_str())) { + Serial.printf("[%lu] [SCT] Cache does not exist, no action needed\n", millis()); + return true; + } + + if (!removeDir(cachePath.c_str())) { + Serial.printf("[%lu] [SCT] Failed to clear cache\n", millis()); + return false; + } + + Serial.printf("[%lu] [SCT] Cache cleared successfully\n", millis()); + return true; +} bool Section::persistPageDataToSD(const int fontId, const float lineCompression, const int marginTop, const int marginRight, const int marginBottom, const int marginLeft) { diff --git a/lib/Epub/Epub/Section.h b/lib/Epub/Epub/Section.h index 6885d00..4c98fbe 100644 --- a/lib/Epub/Epub/Section.h +++ b/lib/Epub/Epub/Section.h @@ -28,7 +28,7 @@ class Section { bool loadCacheMetadata(int fontId, float lineCompression, int marginTop, int marginRight, int marginBottom, int marginLeft); void setupCacheDir() const; - void clearCache() const; + bool clearCache() const; bool persistPageDataToSD(int fontId, float lineCompression, int marginTop, int marginRight, int marginBottom, int marginLeft); std::unique_ptr loadPageFromSD() const; diff --git a/src/screens/EpubReaderScreen.cpp b/src/screens/EpubReaderScreen.cpp index 91b8a93..07858c5 100644 --- a/src/screens/EpubReaderScreen.cpp +++ b/src/screens/EpubReaderScreen.cpp @@ -208,6 +208,12 @@ void EpubReaderScreen::renderScreen() { { auto p = section->loadPageFromSD(); + if (!p) { + Serial.printf("[%lu] [ERS] Failed to load page from SD - clearing section cache\n", millis()); + section->clearCache(); + section.reset(); + return renderScreen(); + } const auto start = millis(); renderContents(std::move(p)); Serial.printf("[%lu] [ERS] Rendered page in %dms\n", millis(), millis() - start);