From 583fe91fe47015cdedfce5e12c83f8a8d8b752c2 Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Fri, 12 Dec 2025 22:06:16 +1100 Subject: [PATCH] Combine removeDir implementations in Epub --- lib/Epub/Epub.cpp | 37 +++--------------------------------- lib/Epub/Epub/FsHelpers.cpp | 36 +++++++++++++++++++++++++++++++++++ lib/Epub/Epub/FsHelpers.h | 6 ++++++ lib/Epub/Epub/Section.cpp | 38 +++---------------------------------- 4 files changed, 48 insertions(+), 69 deletions(-) create mode 100644 lib/Epub/Epub/FsHelpers.cpp create mode 100644 lib/Epub/Epub/FsHelpers.h diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index 44e3469..dcfbe1e 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -6,6 +6,8 @@ #include +#include "Epub/FsHelpers.h" + bool Epub::findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile) { // open up the meta data to find where the content.opf file lives size_t s; @@ -249,46 +251,13 @@ bool Epub::load() { return true; } -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())) { + if (!FsHelpers::removeDir(cachePath.c_str())) { Serial.printf("[%lu] [EPB] Failed to clear cache\n", millis()); return false; } diff --git a/lib/Epub/Epub/FsHelpers.cpp b/lib/Epub/Epub/FsHelpers.cpp new file mode 100644 index 0000000..5287252 --- /dev/null +++ b/lib/Epub/Epub/FsHelpers.cpp @@ -0,0 +1,36 @@ +#include "FsHelpers.h" + +#include + +bool FsHelpers::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); +} diff --git a/lib/Epub/Epub/FsHelpers.h b/lib/Epub/Epub/FsHelpers.h new file mode 100644 index 0000000..bc5204b --- /dev/null +++ b/lib/Epub/Epub/FsHelpers.h @@ -0,0 +1,6 @@ +#pragma once + +class FsHelpers { + public: + static bool removeDir(const char* path); +}; diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 17e6ef4..b5eadf4 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -1,12 +1,13 @@ #include "Section.h" #include +#include #include #include "EpubHtmlParserSlim.h" +#include "FsHelpers.h" #include "Page.h" -#include "Serialization.h" constexpr uint8_t SECTION_FILE_VERSION = 4; @@ -89,39 +90,6 @@ void Section::setupCacheDir() const { SD.mkdir(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())) { @@ -129,7 +97,7 @@ bool Section::clearCache() const { return true; } - if (!removeDir(cachePath.c_str())) { + if (!FsHelpers::removeDir(cachePath.c_str())) { Serial.printf("[%lu] [SCT] Failed to clear cache\n", millis()); return false; }