From 90899f5859d4bc81816329a711fa9d8e841923f3 Mon Sep 17 00:00:00 2001 From: JackFan <635555698@qq.com> Date: Mon, 14 Oct 2024 19:00:52 +0800 Subject: [PATCH] Fix cache mismatch error when cache.cfg is lost (#2527) Co-authored-by: fanmingyi Co-authored-by: jackfan --- src/rendering/caches/DiskCache.cpp | 13 +++++++++---- src/rendering/caches/DiskCache.h | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/rendering/caches/DiskCache.cpp b/src/rendering/caches/DiskCache.cpp index ab064a0860..eb418716c2 100644 --- a/src/rendering/caches/DiskCache.cpp +++ b/src/rendering/caches/DiskCache.cpp @@ -73,7 +73,10 @@ DiskCache::DiskCache() { if (!cacheDir.empty()) { configPath = Directory::JoinPath(cacheDir, "cache.cfg"); cacheFolder = Directory::JoinPath(cacheDir, "files"); - readConfig(); + if (!readConfig()) { + Directory::VisitFiles(cacheFolder, + [&](const std::string& path, size_t) { remove(path.c_str()); }); + } } } @@ -268,16 +271,16 @@ void DiskCache::moveToBeforeOpenedFiles(std::shared_ptr fileInfo) { } } -void DiskCache::readConfig() { +bool DiskCache::readConfig() { auto file = fopen(configPath.c_str(), "rb"); if (file == nullptr) { - return; + return false; } fseek(file, 0, SEEK_END); auto size = ftell(file); if (size == 0) { fclose(file); - return; + return false; } fseek(file, 0, SEEK_SET); tgfx::Buffer buffer(size); @@ -321,6 +324,7 @@ void DiskCache::readConfig() { if (checkDiskSpace(maxDiskSize) || !expiredFiles.empty()) { saveConfig(); } + return true; } void DiskCache::saveConfig() { @@ -413,4 +417,5 @@ void DiskCache::notifyFileSizeChanged(uint32_t fileID, size_t fileSize) { } } } + } // namespace pag diff --git a/src/rendering/caches/DiskCache.h b/src/rendering/caches/DiskCache.h index b404378198..0e81c1939f 100644 --- a/src/rendering/caches/DiskCache.h +++ b/src/rendering/caches/DiskCache.h @@ -79,7 +79,7 @@ class DiskCache { void removeFromCachedFiles(std::shared_ptr fileInfo); void moveToFront(std::shared_ptr fileInfo); void moveToBeforeOpenedFiles(std::shared_ptr fileInfo); - void readConfig(); + bool readConfig(); void saveConfig(); uint32_t getFileID(const std::string& key); void changeToTemporary(uint32_t fileID);