util/disk_cache: Delete the old multifile cache if using the default.

Only after 7 days so people who switch all the time aren't impacted.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22339>
This commit is contained in:
Bas Nieuwenhuizen 2023-07-10 19:14:27 +02:00 committed by Marge Bot
parent bd4fbdf510
commit c3bc6991d2
3 changed files with 72 additions and 1 deletions

View file

@ -273,8 +273,14 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
cache_type = DISK_CACHE_SINGLE_FILE;
else if (debug_get_bool_option("MESA_DISK_CACHE_MULTI_FILE", false))
cache_type = DISK_CACHE_MULTI_FILE;
else
else {
cache_type = DISK_CACHE_DATABASE;
/* Since switching the default cache to <mesa_shader_cache_db>, remove the
* old cache folder if it hasn't been modified for more than 7 days.
*/
if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR"))
disk_cache_delete_old_cache();
}
/* Create main writable cache. */
cache = disk_cache_type_create(gpu_name, driver_id, driver_flags,

View file

@ -1198,6 +1198,68 @@ disk_cache_db_load_cache_index(void *mem_ctx, struct disk_cache *cache)
{
return mesa_cache_db_multipart_open(&cache->cache_db, cache->path);
}
static void
delete_dir(const char* path)
{
DIR *dir = opendir(path);
if (!dir)
return;
struct dirent *p;
char *entry_path = NULL;
while ((p = readdir(dir)) != NULL) {
if (strcmp(p->d_name, ".") == 0 || strcmp(p->d_name, "..") == 0)
continue;
asprintf(&entry_path, "%s/%s", path, p->d_name);
if (!entry_path)
continue;
struct stat st;
if (stat(entry_path, &st)) {
free(entry_path);
continue;
}
if (S_ISDIR(st.st_mode))
delete_dir(entry_path);
else
unlink(entry_path);
free(entry_path);
}
closedir(dir);
rmdir(path);
}
/* Deletes old multi-file caches, to avoid having two default caches taking up disk space. */
void
disk_cache_delete_old_cache(void)
{
void *ctx = ralloc_context(NULL);
char *dirname = disk_cache_generate_cache_dir(ctx, NULL, NULL, DISK_CACHE_MULTI_FILE);
if (!dirname)
goto finish;
/* The directory itself doesn't get updated, so use a marker timestamp */
char *index_path = ralloc_asprintf(ctx, "%s/marker", dirname);
struct stat attr;
if (stat(index_path, &attr) == -1)
goto finish;
time_t now = time(NULL);
/* Do not delete anything if the cache has been modified in the past week */
if (now - attr.st_mtime < 60 * 60 * 24 * 7)
goto finish;
delete_dir(dirname);
finish:
ralloc_free(ctx);
}
#endif
#endif /* ENABLE_SHADER_CACHE */

View file

@ -181,6 +181,9 @@ disk_cache_db_write_item_to_disk(struct disk_cache_put_job *dc_job);
bool
disk_cache_db_load_cache_index(void *mem_ctx, struct disk_cache *cache);
void
disk_cache_delete_old_cache(void);
#ifdef __cplusplus
}
#endif