Blender V5.3
GHOST_SystemPathsUnix.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <mutex>
10#include <optional>
11#include <sstream>
12
14
15#include "GHOST_Debug.hh"
16
17/* For timing. */
18#include <sys/time.h>
19#include <unistd.h>
20
21#include <pwd.h> /* For get home without use `getenv()`. */
22#include <string>
23
24using std::string;
25
26#ifdef PREFIX
27static const char *static_path = PREFIX "/share";
28#else
29static const char *static_path = nullptr;
30#endif
31
32#if defined(PREFIX) && defined(BLENDER_INSTALL_LIBDIR)
33static const char *static_libs_path = PREFIX "/" BLENDER_INSTALL_LIBDIR;
34#else
35static const char *static_libs_path = nullptr;
36#endif
37
39
41
42const char *GHOST_SystemPathsUnix::getSystemDir(int /*version*/, const char *versionstr) const
43{
44 /* no prefix assumes a portable build which only uses bundled scripts */
45 if (static_path) {
46 static string system_path = string(static_path) + "/blender/" + versionstr;
47 return system_path.c_str();
48 }
49
50 return nullptr;
51}
52
53const char *GHOST_SystemPathsUnix::getSystemLibsDir(int /*version*/, const char *versionstr) const
54{
55 if (static_libs_path) {
56 static string system_libs_path = string(static_libs_path) + "/blender/" + versionstr;
57 return system_libs_path.c_str();
58 }
59
60 return nullptr;
61}
62
66static const char *home_dir_get()
67{
68 const char *home_dir = getenv("HOME");
69 if (home_dir == nullptr) {
70 if (const passwd *pwuser = getpwuid(getuid())) {
71 home_dir = pwuser->pw_dir;
72 }
73 }
74 return home_dir;
75}
76
77const char *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionstr) const
78{
79 static string user_path;
80 static int last_version = 0;
81
82 /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
83 * operator works we give a different path depending on the requested version */
84 if (version < 264) {
85 if (user_path.empty() || last_version != version) {
86 const char *home = home_dir_get();
87
88 last_version = version;
89
90 if (home) {
91 user_path = string(home) + "/.blender/" + versionstr;
92 }
93 else {
94 return nullptr;
95 }
96 }
97 return user_path.c_str();
98 }
99 if (user_path.empty() || last_version != version) {
100 const char *home = getenv("XDG_CONFIG_HOME");
101
102 last_version = version;
103
104 if (home) {
105 user_path = string(home) + "/blender/" + versionstr;
106 }
107 else {
108 home = home_dir_get();
109 if (home) {
110 user_path = string(home) + "/.config/blender/" + versionstr;
111 }
112 else {
113 return nullptr;
114 }
115 }
116 }
117
118 return user_path.c_str();
119}
120
121static std::optional<std::string> user_special_dir_query(GHOST_TUserSpecialDirTypes type)
122{
123 const char *type_str;
124
125 switch (type) {
127 type_str = "DESKTOP";
128 break;
130 type_str = "DOCUMENTS";
131 break;
133 type_str = "DOWNLOAD";
134 break;
136 type_str = "MUSIC";
137 break;
139 type_str = "PICTURES";
140 break;
142 type_str = "VIDEOS";
143 break;
145 const char *cache_dir = getenv("XDG_CACHE_HOME");
146 if (cache_dir) {
147 return cache_dir;
148 }
149
150 /* If `XDG_CACHE_HOME` is not set, then `$HOME/.cache is used`. */
151 const char *home_dir = home_dir_get();
152 if (home_dir == nullptr) {
153 return std::nullopt;
154 }
155 return string(home_dir) + "/.cache";
156 }
157 default:
159 false,
160 "GHOST_SystemPathsUnix::getUserSpecialDir(): Invalid enum value for type parameter");
161 return std::nullopt;
162 }
163
164 /* Pipe `stderr` to `/dev/null` to avoid error prints. We will fail gracefully still. */
165 string command = string("xdg-user-dir ") + type_str + " 2> /dev/null";
166
167 FILE *fstream = popen(command.c_str(), "r");
168 if (fstream == nullptr) {
169 return std::nullopt;
170 }
171 std::stringstream path_stream;
172 while (!feof(fstream)) {
173 char c = fgetc(fstream);
174 /* `xdg-user-dir` ends the path with '\n'. */
175 if (c == '\n') {
176 break;
177 }
178 path_stream << c;
179 }
180 if (pclose(fstream) == -1) {
181 perror("GHOST_SystemPathsUnix::getUserSpecialDir failed at pclose()");
182 return std::nullopt;
183 }
184
185 std::string path = path_stream.str();
186 return path[0] ? std::optional(path) : std::nullopt;
187}
188
191{
192 /* Cached result to avoid launching a process on every call. */
193 GHOST_ASSERT(uint(type) < uint(GHOST_kUserSpecialDirType_Num), "Invalid type");
194 UserSpecialDirCache &cached = user_special_dir_cache_[type];
195 if (!cached.resolved) {
196 static std::mutex mutex;
197 std::lock_guard lock(mutex);
198 if (!cached.resolved) {
199 cached.path = user_special_dir_query(type);
200 cached.resolved = true;
201 }
202 }
203 return cached.path;
204}
205
207{
208 return nullptr;
209}
210
211void GHOST_SystemPathsUnix::addToSystemRecentFiles(const char * /*filename*/) const
212{
213 /* TODO: implement for X11 */
214}
#define GHOST_ASSERT(x, info)
static const char * static_libs_path
static const char * static_path
static std::optional< std::string > user_special_dir_query(GHOST_TUserSpecialDirTypes type)
static const char * home_dir_get()
#define GHOST_kUserSpecialDirType_Num
GHOST_TUserSpecialDirTypes
@ GHOST_kUserSpecialDirDesktop
@ GHOST_kUserSpecialDirMusic
@ GHOST_kUserSpecialDirPictures
@ GHOST_kUserSpecialDirVideos
@ GHOST_kUserSpecialDirDownloads
@ GHOST_kUserSpecialDirCaches
@ GHOST_kUserSpecialDirDocuments
volatile int lock
const char * getBinaryDir() const override
const char * getUserDir(int version, const char *versionstr) const override
std::optional< std::string > getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const override
~GHOST_SystemPathsUnix() override
void addToSystemRecentFiles(const char *filepath) const override
const char * getSystemDir(int version, const char *versionstr) const override
const char * getSystemLibsDir(int version, const char *versionstr) const override