[Bf-blender-cvs] [70fd6a313e7] master: GHOST: Add option to request (user) cache folder.

Jeroen Bakker noreply at git.blender.org
Tue Oct 12 08:42:53 CEST 2021


Commit: 70fd6a313e70ead89f94090d1de3d90032f778d5
Author: Jeroen Bakker
Date:   Tue Oct 12 08:42:08 2021 +0200
Branches: master
https://developer.blender.org/rB70fd6a313e70ead89f94090d1de3d90032f778d5

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

===================================================================

M	intern/ghost/GHOST_Types.h
M	intern/ghost/intern/GHOST_SystemPathsCocoa.mm
M	intern/ghost/intern/GHOST_SystemPathsUnix.cpp
M	intern/ghost/intern/GHOST_SystemPathsWin32.cpp
M	source/blender/blenkernel/BKE_appdir.h
M	source/blender/blenkernel/intern/appdir.c

===================================================================

diff --git a/intern/ghost/GHOST_Types.h b/intern/ghost/GHOST_Types.h
index 221fa140f70..898ee451baf 100644
--- a/intern/ghost/GHOST_Types.h
+++ b/intern/ghost/GHOST_Types.h
@@ -569,6 +569,7 @@ typedef enum {
   GHOST_kUserSpecialDirMusic,
   GHOST_kUserSpecialDirPictures,
   GHOST_kUserSpecialDirVideos,
+  GHOST_kUserSpecialDirCaches,
   /* Can be extended as needed. */
 } GHOST_TUserSpecialDirTypes;
 
diff --git a/intern/ghost/intern/GHOST_SystemPathsCocoa.mm b/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
index 3b29d5106f6..43ce0bb0533 100644
--- a/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
+++ b/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
@@ -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,
diff --git a/intern/ghost/intern/GHOST_SystemPathsUnix.cpp b/intern/ghost/intern/GHOST_SystemPathsUnix.cpp
index 8bc2ff9227d..72396782752 100644
--- a/intern/ghost/intern/GHOST_SystemPathsUnix.cpp
+++ b/intern/ghost/intern/GHOST_SystemPathsUnix.cpp
@@ -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;
 }
diff --git a/intern/ghost/intern/GHOST_SystemPathsWin32.cpp b/intern/ghost/intern/GHOST_SystemPathsWin32.cpp
index 580cfcac7ba..bced552921f 100644
--- a/intern/ghost/intern/GHOST_SystemPathsWin32.cpp
+++ b/intern/ghost/intern/GHOST_SystemPathsWin32.cpp
@@ -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,
diff --git a/source/blender/blenkernel/BKE_appdir.h b/source/blender/blenkernel/BKE_appdir.h
index 0f00d391973..07132201e87 100644
--- a/source/blender/blenkernel/BKE_appdir.h
+++ b/source/blender/blenkernel/BKE_appdir.h
@@ -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,
diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
index eae331fc7d1..0bc8d17fdaf 100644
--- a/source/blender/blenkernel/intern/appdir.c
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -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.
  */



More information about the Bf-blender-cvs mailing list