-static int
-remove_dir(const char *fpath, const struct stat *sb,
- int typeflag, struct FTW *ftwbuf)
-{
- if (S_ISREG(sb->st_mode))
- unlink(fpath);
- else if (S_ISDIR(sb->st_mode))
- rmdir(fpath);
-
- return 0;
-}
-
-static void
-remove_old_cache_directories(void *mem_ctx, const char *path,
- const char *timestamp)
-{
- DIR *dir = opendir(path);
-
- struct dirent* d_entry;
- while((d_entry = readdir(dir)) != NULL)
- {
- char *full_path =
- ralloc_asprintf(mem_ctx, "%s/%s", path, d_entry->d_name);
-
- struct stat sb;
- if (stat(full_path, &sb) == 0 && S_ISDIR(sb.st_mode) &&
- strcmp(d_entry->d_name, timestamp) != 0 &&
- strcmp(d_entry->d_name, "..") != 0 &&
- strcmp(d_entry->d_name, ".") != 0) {
- nftw(full_path, remove_dir, 20, FTW_DEPTH);
- }
- }
-
- closedir(dir);
-}
-
-static char *
-create_mesa_cache_dir(void *mem_ctx, const char *path, const char *timestamp,
- const char *gpu_name)
-{
- char *new_path = concatenate_and_mkdir(mem_ctx, path, "mesa");
- if (new_path == NULL)
- return NULL;
-
- /* Create a parent architecture directory so that we don't remove cache
- * files for other architectures. In theory we could share the cache
- * between architectures but we have no way of knowing if they were created
- * by a compatible Mesa version.
- */
- new_path = concatenate_and_mkdir(mem_ctx, new_path, get_arch_bitness_str());
- if (new_path == NULL)
- return NULL;
-
- /* Remove cache directories for old Mesa versions */
- remove_old_cache_directories(mem_ctx, new_path, timestamp);
-
- new_path = concatenate_and_mkdir(mem_ctx, new_path, timestamp);
- if (new_path == NULL)
- return NULL;
-
- new_path = concatenate_and_mkdir(mem_ctx, new_path, gpu_name);
- if (new_path == NULL)
- return NULL;
-
- return new_path;
-}