Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复cache.cfg丢时缓存映射错误 #2527

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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