mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2025-12-22 22:10:10 +01:00
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:
parent
65d0fa0852
commit
4339ecde35
4 changed files with 221 additions and 93 deletions
|
|
@ -32,9 +32,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pwd.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
@ -54,6 +52,7 @@
|
||||||
#include "util/compiler.h"
|
#include "util/compiler.h"
|
||||||
|
|
||||||
#include "disk_cache.h"
|
#include "disk_cache.h"
|
||||||
|
#include "disk_cache_os.h"
|
||||||
|
|
||||||
/* Number of bits to mask off from a cache key to get an index. */
|
/* Number of bits to mask off from a cache key to get an index. */
|
||||||
#define CACHE_INDEX_KEY_BITS 16
|
#define CACHE_INDEX_KEY_BITS 16
|
||||||
|
|
@ -162,33 +161,6 @@ mkdir_if_needed(const char *path)
|
||||||
return -1;
|
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) \
|
#define DRV_KEY_CPY(_dst, _src, _src_size) \
|
||||||
do { \
|
do { \
|
||||||
memcpy(_dst, _src, _src_size); \
|
memcpy(_dst, _src, _src_size); \
|
||||||
|
|
@ -201,7 +173,7 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
|
||||||
{
|
{
|
||||||
void *local;
|
void *local;
|
||||||
struct disk_cache *cache = NULL;
|
struct disk_cache *cache = NULL;
|
||||||
char *path, *max_size_str;
|
char *max_size_str;
|
||||||
uint64_t max_size;
|
uint64_t max_size;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
@ -230,70 +202,10 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
|
||||||
/* Assume failure. */
|
/* Assume failure. */
|
||||||
cache->path_init_failed = true;
|
cache->path_init_failed = true;
|
||||||
|
|
||||||
/* Determine path for cache based on the first defined name as follows:
|
char *path = disk_cache_generate_cache_dir(local);
|
||||||
*
|
if (!path)
|
||||||
* $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;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
cache->path = ralloc_strdup(cache, path);
|
cache->path = ralloc_strdup(cache, path);
|
||||||
if (cache->path == NULL)
|
if (cache->path == NULL)
|
||||||
goto path_fail;
|
goto path_fail;
|
||||||
|
|
|
||||||
176
src/util/disk_cache_os.c
Normal file
176
src/util/disk_cache_os.c
Normal file
|
|
@ -0,0 +1,176 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2014 Intel Corporation
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
|
* to deal in the Software without restriction, including without limitation
|
||||||
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice (including the next
|
||||||
|
* paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
* Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
* IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef ENABLE_SHADER_CACHE
|
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS
|
||||||
|
/* TODO: implement disk cache support on windows */
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "util/disk_cache.h"
|
||||||
|
#include "util/disk_cache_os.h"
|
||||||
|
#include "util/ralloc.h"
|
||||||
|
|
||||||
|
/* Create a directory named 'path' if it does not already exist.
|
||||||
|
*
|
||||||
|
* Returns: 0 if path already exists as a directory or if created.
|
||||||
|
* -1 in all other cases.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mkdir_if_needed(const char *path)
|
||||||
|
{
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
|
/* If the path exists already, then our work is done if it's a
|
||||||
|
* directory, but it's an error if it is not.
|
||||||
|
*/
|
||||||
|
if (stat(path, &sb) == 0) {
|
||||||
|
if (S_ISDIR(sb.st_mode)) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Cannot use %s for shader cache (not a directory)"
|
||||||
|
"---disabling.\n", path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = mkdir(path, 0755);
|
||||||
|
if (ret == 0 || (ret == -1 && errno == EEXIST))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fprintf(stderr, "Failed to create %s for shader cache (%s)---disabling.\n",
|
||||||
|
path, strerror(errno));
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
disk_cache_generate_cache_dir(void *mem_ctx)
|
||||||
|
{
|
||||||
|
char *path = getenv("MESA_GLSL_CACHE_DIR");
|
||||||
|
if (path) {
|
||||||
|
if (mkdir_if_needed(path) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
path = concatenate_and_mkdir(mem_ctx, path, CACHE_DIR_NAME);
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path == NULL) {
|
||||||
|
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
|
||||||
|
|
||||||
|
if (xdg_cache_home) {
|
||||||
|
if (mkdir_if_needed(xdg_cache_home) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
path = concatenate_and_mkdir(mem_ctx, xdg_cache_home, CACHE_DIR_NAME);
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!path) {
|
||||||
|
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(mem_ctx, 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 {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
path = concatenate_and_mkdir(mem_ctx, pwd.pw_dir, ".cache");
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
path = concatenate_and_mkdir(mem_ctx, path, CACHE_DIR_NAME);
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* ENABLE_SHADER_CACHE */
|
||||||
38
src/util/disk_cache_os.h
Normal file
38
src/util/disk_cache_os.h
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2014 Intel Corporation
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
|
* to deal in the Software without restriction, including without limitation
|
||||||
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice (including the next
|
||||||
|
* paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
* Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
* IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DISK_CACHE_OS_H
|
||||||
|
#define DISK_CACHE_OS_H
|
||||||
|
|
||||||
|
#if DETECT_OS_WINDOWS
|
||||||
|
|
||||||
|
/* TODO: implement disk cache support on windows */
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
char *
|
||||||
|
disk_cache_generate_cache_dir(void *mem_ctx);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* DISK_CACHE_OS_H */
|
||||||
|
|
@ -41,6 +41,8 @@ files_mesa_util = files(
|
||||||
'debug.h',
|
'debug.h',
|
||||||
'disk_cache.c',
|
'disk_cache.c',
|
||||||
'disk_cache.h',
|
'disk_cache.h',
|
||||||
|
'disk_cache_os.c',
|
||||||
|
'disk_cache_os.h',
|
||||||
'double.c',
|
'double.c',
|
||||||
'double.h',
|
'double.h',
|
||||||
'enum_operators.h',
|
'enum_operators.h',
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue