ISO: Clean up orphaned cache entries after game list scan

This commit is contained in:
Vishrut Sachan 2026-04-11 02:13:16 +05:30
parent 5903612adc
commit 1c5bd20d80
4 changed files with 40 additions and 0 deletions

View File

@ -6,6 +6,8 @@
#include "util/fnv_hash.hpp"
#include "Utilities/File.h"
#include <unordered_set>
LOG_CHANNEL(iso_cache_log, "ISOCache");
namespace
@ -128,4 +130,30 @@ namespace iso_cache
}
}
}
void cleanup(const std::unordered_set<std::string>& valid_iso_paths)
{
const std::string dir = get_cache_dir();
// Build a set of stems that should exist.
std::unordered_set<std::string> valid_stems;
for (const std::string& path : valid_iso_paths)
{
valid_stems.insert(get_cache_stem(path));
}
// Delete any cache files whose stem is not in the valid set.
fs::dir cache_dir(dir);
fs::dir_entry entry{};
while (cache_dir.read(entry))
{
if (entry.name == "." || entry.name == "..") continue;
const std::string stem = entry.name.substr(0, entry.name.find_last_of('.'));
if (valid_stems.find(stem) == valid_stems.end())
{
fs::remove_file(dir + entry.name);
}
}
}
}

View File

@ -5,6 +5,7 @@
#include "util/types.hpp"
#include <string>
#include <unordered_set>
#include <vector>
// Cached metadata extracted from an ISO during game list scanning.
@ -25,4 +26,7 @@ namespace iso_cache
// Persists a populated cache entry to disk.
void save(const std::string& iso_path, const iso_metadata_cache_entry& entry);
// Remove cache entries for ISOs that are no longer in the scanned set.
void cleanup(const std::unordered_set<std::string>& valid_iso_paths);
}

View File

@ -542,6 +542,9 @@ void game_list_frame::OnParsingFinished()
{
archive = std::make_unique<iso_archive>(dir_or_elf);
}
// Track this ISO path for cache cleanup after scan completes.
std::lock_guard lock(m_path_mutex);
m_scanned_iso_paths.insert(dir_or_elf);
}
const auto file_exists = [&archive, &cache_entry](const std::string& path)
@ -892,6 +895,9 @@ void game_list_frame::OnRefreshFinished()
WaitAndAbortSizeCalcThreads();
WaitAndAbortRepaintThreads();
// Remove cache entries for ISOs that are no longer present in the scanned paths.
iso_cache::cleanup(m_scanned_iso_paths);
for (auto&& g : m_games.pop_all())
{
m_game_data.push_back(g);
@ -978,6 +984,7 @@ void game_list_frame::OnRefreshFinished()
m_serials.clear();
m_path_list.clear();
m_path_entries.clear();
m_scanned_iso_paths.clear();
Refresh();

View File

@ -181,6 +181,7 @@ private:
std::vector<path_entry> m_path_entries;
shared_mutex m_path_mutex;
std::set<std::string> m_path_list;
std::unordered_set<std::string> m_scanned_iso_paths;
QSet<QString> m_serials;
QMutex m_games_mutex;
lf_queue<game_info> m_games;