Skip to content

Commit

Permalink
Fix cache mismatch error when cache.cfg is lost (#2527)
Browse files Browse the repository at this point in the history
Co-authored-by: fanmingyi <[email protected]>
Co-authored-by: jackfan <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent c77c593 commit 90899f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/rendering/caches/DiskCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()); });
}
}
}

Expand Down Expand Up @@ -268,16 +271,16 @@ void DiskCache::moveToBeforeOpenedFiles(std::shared_ptr<FileInfo> 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);
Expand Down Expand Up @@ -321,6 +324,7 @@ void DiskCache::readConfig() {
if (checkDiskSpace(maxDiskSize) || !expiredFiles.empty()) {
saveConfig();
}
return true;
}

void DiskCache::saveConfig() {
Expand Down Expand Up @@ -413,4 +417,5 @@ void DiskCache::notifyFileSizeChanged(uint32_t fileID, size_t fileSize) {
}
}
}

} // namespace pag
2 changes: 1 addition & 1 deletion src/rendering/caches/DiskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DiskCache {
void removeFromCachedFiles(std::shared_ptr<FileInfo> fileInfo);
void moveToFront(std::shared_ptr<FileInfo> fileInfo);
void moveToBeforeOpenedFiles(std::shared_ptr<FileInfo> fileInfo);
void readConfig();
bool readConfig();
void saveConfig();
uint32_t getFileID(const std::string& key);
void changeToTemporary(uint32_t fileID);
Expand Down

0 comments on commit 90899f5

Please sign in to comment.