Skip to content

Commit cdb76be

Browse files
fanmingyifanmingyijackfan
authored andcommitted
Fix cache mismatch error when cache.cfg is lost (#2527)
Co-authored-by: fanmingyi <[email protected]> Co-authored-by: jackfan <[email protected]> (cherry picked from commit 90899f5)
1 parent dc87416 commit cdb76be

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/rendering/caches/DiskCache.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ DiskCache::DiskCache() {
7373
if (!cacheDir.empty()) {
7474
configPath = Directory::JoinPath(cacheDir, "cache.cfg");
7575
cacheFolder = Directory::JoinPath(cacheDir, "files");
76-
readConfig();
76+
if (!readConfig()) {
77+
Directory::VisitFiles(cacheFolder,
78+
[&](const std::string& path, size_t) { remove(path.c_str()); });
79+
}
7780
}
7881
}
7982

@@ -268,16 +271,16 @@ void DiskCache::moveToBeforeOpenedFiles(std::shared_ptr<FileInfo> fileInfo) {
268271
}
269272
}
270273

271-
void DiskCache::readConfig() {
274+
bool DiskCache::readConfig() {
272275
auto file = fopen(configPath.c_str(), "rb");
273276
if (file == nullptr) {
274-
return;
277+
return false;
275278
}
276279
fseek(file, 0, SEEK_END);
277280
auto size = ftell(file);
278281
if (size == 0) {
279282
fclose(file);
280-
return;
283+
return false;
281284
}
282285
fseek(file, 0, SEEK_SET);
283286
tgfx::Buffer buffer(size);
@@ -321,6 +324,7 @@ void DiskCache::readConfig() {
321324
if (checkDiskSpace(maxDiskSize) || !expiredFiles.empty()) {
322325
saveConfig();
323326
}
327+
return true;
324328
}
325329

326330
void DiskCache::saveConfig() {
@@ -413,4 +417,5 @@ void DiskCache::notifyFileSizeChanged(uint32_t fileID, size_t fileSize) {
413417
}
414418
}
415419
}
420+
416421
} // namespace pag

src/rendering/caches/DiskCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class DiskCache {
7979
void removeFromCachedFiles(std::shared_ptr<FileInfo> fileInfo);
8080
void moveToFront(std::shared_ptr<FileInfo> fileInfo);
8181
void moveToBeforeOpenedFiles(std::shared_ptr<FileInfo> fileInfo);
82-
void readConfig();
82+
bool readConfig();
8383
void saveConfig();
8484
uint32_t getFileID(const std::string& key);
8585
void changeToTemporary(uint32_t fileID);

0 commit comments

Comments
 (0)