Blender V4.5
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 <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
48static const char *home_dir_get()
49{
50 const char *home_dir = getenv("HOME");
51 if (home_dir == nullptr) {
52 if (const passwd *pwuser = getpwuid(getuid())) {
53 home_dir = pwuser->pw_dir;
54 }
55 }
56 return home_dir;
57}
58
59const char *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionstr) const
60{
61 static string user_path;
62 static int last_version = 0;
63
64 /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
65 * operator works we give a different path depending on the requested version */
66 if (version < 264) {
67 if (user_path.empty() || last_version != version) {
68 const char *home = home_dir_get();
69
70 last_version = version;
71
72 if (home) {
73 user_path = string(home) + "/.blender/" + versionstr;
74 }
75 else {
76 return nullptr;
77 }
78 }
79 return user_path.c_str();
80 }
81 if (user_path.empty() || last_version != version) {
82 const char *home = getenv("XDG_CONFIG_HOME");
83
84 last_version = version;
85
86 if (home) {
87 user_path = string(home) + "/blender/" + versionstr;
88 }
89 else {
90 home = home_dir_get();
91 if (home) {
92 user_path = string(home) + "/.config/blender/" + versionstr;
93 }
94 else {
95 return nullptr;
96 }
97 }
98 }
99
100 return user_path.c_str();
101}
102
104{
105 const char *type_str;
106 static string path;
107
108 switch (type) {
110 type_str = "DESKTOP";
111 break;
113 type_str = "DOCUMENTS";
114 break;
116 type_str = "DOWNLOAD";
117 break;
119 type_str = "MUSIC";
120 break;
122 type_str = "PICTURES";
123 break;
125 type_str = "VIDEOS";
126 break;
128 const char *cache_dir = getenv("XDG_CACHE_HOME");
129 if (cache_dir) {
130 return cache_dir;
131 }
132
133 /* If `XDG_CACHE_HOME` is not set, then `$HOME/.cache is used`. */
134 const char *home_dir = home_dir_get();
135 if (home_dir == nullptr) {
136 return nullptr;
137 }
138 path = string(home_dir) + "/.cache";
139 return path.c_str();
140 }
141 default:
143 false,
144 "GHOST_SystemPathsUnix::getUserSpecialDir(): Invalid enum value for type parameter");
145 return nullptr;
146 }
147
148 /* Pipe `stderr` to `/dev/null` to avoid error prints. We will fail gracefully still. */
149 string command = string("xdg-user-dir ") + type_str + " 2> /dev/null";
150
151 FILE *fstream = popen(command.c_str(), "r");
152 if (fstream == nullptr) {
153 return nullptr;
154 }
155 std::stringstream path_stream;
156 while (!feof(fstream)) {
157 char c = fgetc(fstream);
158 /* `xdg-user-dir` ends the path with '\n'. */
159 if (c == '\n') {
160 break;
161 }
162 path_stream << c;
163 }
164 if (pclose(fstream) == -1) {
165 perror("GHOST_SystemPathsUnix::getUserSpecialDir failed at pclose()");
166 return nullptr;
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 override
const char * getUserDir(int version, const char *versionstr) const override
~GHOST_SystemPathsUnix() override
const char * getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const override
void addToSystemRecentFiles(const char *filepath) const override
const char * getSystemDir(int version, const char *versionstr) const override