Blender V4.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
9#include <sstream>
10
12
13#include "GHOST_Debug.hh"
14
15/* For timing. */
16#include <sys/time.h>
17#include <unistd.h>
18
19#include <pwd.h> /* For get home without use `getenv()`. */
20#include <string>
21
22using std::string;
23
24#ifdef PREFIX
25static const char *static_path = PREFIX "/share";
26#else
27static const char *static_path = nullptr;
28#endif
29
31
33
34const char *GHOST_SystemPathsUnix::getSystemDir(int /*version*/, const char *versionstr) const
35{
36 /* no prefix assumes a portable build which only uses bundled scripts */
37 if (static_path) {
38 static string system_path = string(static_path) + "/blender/" + versionstr;
39 return system_path.c_str();
40 }
41
42 return nullptr;
43}
44
45static const char *home_dir_get()
46{
47 const char *home_dir = getenv("HOME");
48 if (home_dir == nullptr) {
49 if (const passwd *pwuser = getpwuid(getuid())) {
50 home_dir = pwuser->pw_dir;
51 }
52 }
53 return home_dir;
54}
55
56const char *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionstr) const
57{
58 static string user_path = "";
59 static int last_version = 0;
60
61 /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
62 * operator works we give a different path depending on the requested version */
63 if (version < 264) {
64 if (user_path.empty() || last_version != version) {
65 const char *home = home_dir_get();
66
67 last_version = version;
68
69 if (home) {
70 user_path = string(home) + "/.blender/" + versionstr;
71 }
72 else {
73 return nullptr;
74 }
75 }
76 return user_path.c_str();
77 }
78 if (user_path.empty() || last_version != version) {
79 const char *home = getenv("XDG_CONFIG_HOME");
80
81 last_version = version;
82
83 if (home) {
84 user_path = string(home) + "/blender/" + versionstr;
85 }
86 else {
87 home = home_dir_get();
88 if (home) {
89 user_path = string(home) + "/.config/blender/" + versionstr;
90 }
91 else {
92 return nullptr;
93 }
94 }
95 }
96
97 return user_path.c_str();
98}
99
101{
102 const char *type_str;
103 std::string add_path = "";
104
105 switch (type) {
107 type_str = "DESKTOP";
108 break;
110 type_str = "DOCUMENTS";
111 break;
113 type_str = "DOWNLOAD";
114 break;
116 type_str = "MUSIC";
117 break;
119 type_str = "PICTURES";
120 break;
122 type_str = "VIDEOS";
123 break;
125 const char *cache_dir = getenv("XDG_CACHE_HOME");
126 if (cache_dir) {
127 return cache_dir;
128 }
129 /* Fallback to ~home/.cache/.
130 * When invoking `xdg-user-dir` without parameters the user folder
131 * will be read. `.cache` will be appended. */
132 type_str = "";
133 add_path = ".cache";
134 break;
135 }
136 default:
138 false,
139 "GHOST_SystemPathsUnix::getUserSpecialDir(): Invalid enum value for type parameter");
140 return nullptr;
141 }
142
143 static string path = "";
144 /* Pipe `stderr` to `/dev/null` to avoid error prints. We will fail gracefully still. */
145 string command = string("xdg-user-dir ") + type_str + " 2> /dev/null";
146
147 FILE *fstream = popen(command.c_str(), "r");
148 if (fstream == nullptr) {
149 return nullptr;
150 }
151 std::stringstream path_stream;
152 while (!feof(fstream)) {
153 char c = fgetc(fstream);
154 /* `xdg-user-dir` ends the path with '\n'. */
155 if (c == '\n') {
156 break;
157 }
158 path_stream << c;
159 }
160 if (pclose(fstream) == -1) {
161 perror("GHOST_SystemPathsUnix::getUserSpecialDir failed at pclose()");
162 return nullptr;
163 }
164
165 if (!add_path.empty()) {
166 path_stream << '/' << add_path;
167 }
168
169 path = path_stream.str();
170 return path[0] ? path.c_str() : nullptr;
171}
172
174{
175 return nullptr;
176}
177
178void GHOST_SystemPathsUnix::addToSystemRecentFiles(const char * /*filename*/) const
179{
180 /* TODO: implement for X11 */
181}
#define GHOST_ASSERT(x, info)
static const char * static_path
static const char * home_dir_get()
GHOST_TUserSpecialDirTypes
@ GHOST_kUserSpecialDirDesktop
@ GHOST_kUserSpecialDirMusic
@ GHOST_kUserSpecialDirPictures
@ GHOST_kUserSpecialDirVideos
@ GHOST_kUserSpecialDirDownloads
@ GHOST_kUserSpecialDirCaches
@ GHOST_kUserSpecialDirDocuments
const char * getBinaryDir() const
void addToSystemRecentFiles(const char *filepath) const
const char * getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
const char * getSystemDir(int version, const char *versionstr) const
const char * getUserDir(int version, const char *versionstr) const