| 27 | } |
| 28 | |
| 29 | void limit_cache_size() |
| 30 | { |
| 31 | const std::string cache_location = rpcs3::utils::get_hdd1_dir() + "/caches"; |
| 32 | |
| 33 | if (!fs::is_dir(cache_location)) |
| 34 | { |
| 35 | sys_log.warning("Cache does not exist (%s)", cache_location); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | const u64 size = fs::get_dir_size(cache_location); |
| 40 | |
| 41 | if (size == umax) |
| 42 | { |
| 43 | sys_log.error("Could not calculate cache directory '%s' size (%s)", cache_location, fs::g_tls_error); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | const u64 max_size = static_cast<u64>(g_cfg.vfs.cache_max_size) * 1024 * 1024; |
| 48 | |
| 49 | if (max_size == 0) // Everything must go, so no need to do checks |
| 50 | { |
| 51 | fs::remove_all(cache_location, false); |
| 52 | sys_log.success("Cleared disk cache"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (size <= max_size) |
| 57 | { |
| 58 | sys_log.trace("Cache size below limit: %llu/%llu", size, max_size); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | sys_log.success("Cleaning disk cache..."); |
| 63 | std::vector<fs::dir_entry> file_list{}; |
| 64 | fs::dir cache_dir(cache_location); |
| 65 | if (!cache_dir) |
| 66 | { |
| 67 | sys_log.error("Could not open cache directory '%s' (%s)", cache_location, fs::g_tls_error); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // retrieve items to delete |
| 72 | for (const auto& item : cache_dir) |
| 73 | { |
| 74 | if (item.name != "." && item.name != "..") |
| 75 | file_list.push_back(item); |
| 76 | } |
| 77 | |
| 78 | cache_dir.close(); |
| 79 | |
| 80 | // sort oldest first |
| 81 | std::ranges::sort(file_list, FN(x.mtime < y.mtime)); |
| 82 | |
| 83 | // keep removing until cache is empty or enough bytes have been cleared |
| 84 | // cache is cleared down to 80% of limit to increase interval between clears |
| 85 | const u64 to_remove = static_cast<u64>(size - max_size * 0.8); |
| 86 | u64 removed = 0; |