GHOST: Add option to request (user) cache folder.

Introduces `BKE_appdir_folder_caches` to get the folder that
can be used to store caches. On different OS's different folders
are used.

- Linux: `~/.cache/blender/`.
- MacOS: `Library/Caches/Blender/`.
- Windows: `(%USERPROFILE%\AppData\Local)\Blender Foundation\Blender\Cache\`.

Reviewed By: Severin

Differential Revision: https://developer.blender.org/D12822
This commit is contained in:
Jeroen Bakker 2021-10-12 08:42:08 +02:00 committed by Jeroen Bakker
parent 8f66f40318
commit 70fd6a313e
Notes: blender-bot 2023-02-14 08:45:12 +01:00
Referenced by issue #91406, AssetBrowser: Indexing Asset Library
6 changed files with 57 additions and 0 deletions

View File

@ -569,6 +569,7 @@ typedef enum {
GHOST_kUserSpecialDirMusic,
GHOST_kUserSpecialDirPictures,
GHOST_kUserSpecialDirVideos,
GHOST_kUserSpecialDirCaches,
/* Can be extended as needed. */
} GHOST_TUserSpecialDirTypes;

View File

@ -117,6 +117,9 @@ const char *GHOST_SystemPathsCocoa::getUserSpecialDir(GHOST_TUserSpecialDirTypes
case GHOST_kUserSpecialDirVideos:
ns_directory = NSMoviesDirectory;
break;
case GHOST_kUserSpecialDirCaches:
ns_directory = NSCachesDirectory;
break;
default:
GHOST_ASSERT(
false,

View File

@ -114,6 +114,7 @@ const char *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionst
const char *GHOST_SystemPathsUnix::getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
{
const char *type_str;
std::string add_path = "";
switch (type) {
case GHOST_kUserSpecialDirDesktop:
@ -134,6 +135,18 @@ const char *GHOST_SystemPathsUnix::getUserSpecialDir(GHOST_TUserSpecialDirTypes
case GHOST_kUserSpecialDirVideos:
type_str = "VIDEOS";
break;
case GHOST_kUserSpecialDirCaches: {
const char *cache_dir = getenv("XDG_CACHE_HOME");
if (cache_dir) {
return cache_dir;
}
/* Fallback to ~home/.cache/.
* When invoking `xdg-user-dir` without parameters the user folder
* will be read. `.cache` will be appended. */
type_str = "";
add_path = ".cache";
break;
}
default:
GHOST_ASSERT(
false,
@ -163,6 +176,10 @@ const char *GHOST_SystemPathsUnix::getUserSpecialDir(GHOST_TUserSpecialDirTypes
return NULL;
}
if (!add_path.empty()) {
path_stream << '/' << add_path;
}
path = path_stream.str();
return path[0] ? path.c_str() : NULL;
}

View File

@ -100,6 +100,9 @@ const char *GHOST_SystemPathsWin32::getUserSpecialDir(GHOST_TUserSpecialDirTypes
case GHOST_kUserSpecialDirVideos:
folderid = FOLDERID_Videos;
break;
case GHOST_kUserSpecialDirCaches:
folderid = FOLDERID_LocalAppData;
break;
default:
GHOST_ASSERT(
false,

View File

@ -35,6 +35,7 @@ void BKE_appdir_exit(void);
const char *BKE_appdir_folder_default(void);
const char *BKE_appdir_folder_home(void);
bool BKE_appdir_folder_documents(char *dir);
bool BKE_appdir_folder_caches(char *r_path, size_t path_len);
bool BKE_appdir_folder_id_ex(const int folder_id,
const char *subfolder,
char *path,

View File

@ -217,6 +217,38 @@ bool BKE_appdir_folder_documents(char *dir)
return true;
}
/**
* Get the user's cache directory, i.e. $HOME/.cache/blender/ on Linux,
* %USERPROFILE%\AppData\Local\blender\ on Windows.
*
* \returns True if the path is valid. It doesn't create or checks format
* if the `blender` folder exists. It does check if the parent of the
* path exists.
*/
bool BKE_appdir_folder_caches(char *r_path, const size_t path_len)
{
r_path[0] = '\0';
const char *caches_root_path = GHOST_getUserSpecialDir(GHOST_kUserSpecialDirCaches);
if (caches_root_path == NULL || !BLI_is_dir(caches_root_path)) {
caches_root_path = BKE_tempdir_base();
}
if (caches_root_path == NULL || !BLI_is_dir(caches_root_path)) {
return false;
}
#ifdef WIN32
BLI_path_join(
r_path, path_len, caches_root_path, "Blender Foundation", "Blender", "Cache", SEP_STR, NULL);
#elif __APPLE__
BLI_path_join(r_path, path_len, caches_root_path, "Blender", SEP_STR, NULL);
#else /* __linux__ */
BLI_path_join(r_path, path_len, caches_root_path, "blender", SEP_STR, NULL);
#endif
return true;
}
/**
* Gets a good default directory for fonts.
*/