disk_cache: move cache dir generation into OS specific helper file

This will make windows support easier to add in future. To avoid code
churn this temporarily duplicates the mkdir_if_needed() function, we
will delete the duplicate in a following patch.

Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6197>
This commit is contained in:
Timothy Arceri
2020-07-30 14:50:02 +10:00
committed by Marge Bot
parent 65d0fa0852
commit 4339ecde35
4 changed files with 221 additions and 93 deletions

View File

@@ -32,9 +32,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <dirent.h>
#include <inttypes.h>
@@ -54,6 +52,7 @@
#include "util/compiler.h"
#include "disk_cache.h"
#include "disk_cache_os.h"
/* Number of bits to mask off from a cache key to get an index. */
#define CACHE_INDEX_KEY_BITS 16
@@ -162,33 +161,6 @@ mkdir_if_needed(const char *path)
return -1;
}
/* Concatenate an existing path and a new name to form a new path. If the new
* path does not exist as a directory, create it then return the resulting
* name of the new path (ralloc'ed off of 'ctx').
*
* Returns NULL on any error, such as:
*
* <path> does not exist or is not a directory
* <path>/<name> exists but is not a directory
* <path>/<name> cannot be created as a directory
*/
static char *
concatenate_and_mkdir(void *ctx, const char *path, const char *name)
{
char *new_path;
struct stat sb;
if (stat(path, &sb) != 0 || ! S_ISDIR(sb.st_mode))
return NULL;
new_path = ralloc_asprintf(ctx, "%s/%s", path, name);
if (mkdir_if_needed(new_path) == 0)
return new_path;
else
return NULL;
}
#define DRV_KEY_CPY(_dst, _src, _src_size) \
do { \
memcpy(_dst, _src, _src_size); \
@@ -201,7 +173,7 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
{
void *local;
struct disk_cache *cache = NULL;
char *path, *max_size_str;
char *max_size_str;
uint64_t max_size;
int fd = -1;
struct stat sb;
@@ -230,69 +202,9 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
/* Assume failure. */
cache->path_init_failed = true;
/* Determine path for cache based on the first defined name as follows:
*
* $MESA_GLSL_CACHE_DIR
* $XDG_CACHE_HOME/mesa_shader_cache
* <pwd.pw_dir>/.cache/mesa_shader_cache
*/
path = getenv("MESA_GLSL_CACHE_DIR");
if (path) {
if (mkdir_if_needed(path) == -1)
goto path_fail;
path = concatenate_and_mkdir(local, path, CACHE_DIR_NAME);
if (path == NULL)
goto path_fail;
}
if (path == NULL) {
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
if (xdg_cache_home) {
if (mkdir_if_needed(xdg_cache_home) == -1)
goto path_fail;
path = concatenate_and_mkdir(local, xdg_cache_home, CACHE_DIR_NAME);
if (path == NULL)
goto path_fail;
}
}
if (path == NULL) {
char *buf;
size_t buf_size;
struct passwd pwd, *result;
buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
if (buf_size == -1)
buf_size = 512;
/* Loop until buf_size is large enough to query the directory */
while (1) {
buf = ralloc_size(local, buf_size);
getpwuid_r(getuid(), &pwd, buf, buf_size, &result);
if (result)
break;
if (errno == ERANGE) {
ralloc_free(buf);
buf = NULL;
buf_size *= 2;
} else {
goto path_fail;
}
}
path = concatenate_and_mkdir(local, pwd.pw_dir, ".cache");
if (path == NULL)
goto path_fail;
path = concatenate_and_mkdir(local, path, CACHE_DIR_NAME);
if (path == NULL)
goto path_fail;
}
char *path = disk_cache_generate_cache_dir(local);
if (!path)
goto path_fail;
cache->path = ralloc_strdup(cache, path);
if (cache->path == NULL)