Blender  V3.3
GHOST_SystemPathsUnix.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2010 Blender Foundation. All rights reserved. */
3 
8 #include <cstdio>
9 #include <sstream>
10 
11 #include "GHOST_SystemPathsUnix.h"
12 
13 #include "GHOST_Debug.h"
14 
15 // For timing
16 
17 #include <sys/time.h>
18 #include <unistd.h>
19 
20 #include <cstdio> /* for fprintf only */
21 #include <cstdlib> /* for exit */
22 
23 #include <pwd.h> /* for get home without use getenv() */
24 #include <string>
25 
26 using std::string;
27 
28 #ifdef PREFIX
29 static const char *static_path = PREFIX "/share";
30 #else
31 static const char *static_path = nullptr;
32 #endif
33 
35 {
36 }
37 
39 {
40 }
41 
42 const 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 
53 const char *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionstr) const
54 {
55  static string user_path = "";
56  static int last_version = 0;
57 
58  /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
59  * operator works we give a different path depending on the requested version */
60  if (version < 264) {
61  if (user_path.empty() || last_version != version) {
62  const char *home = getenv("HOME");
63 
64  last_version = version;
65 
66  if (home) {
67  user_path = string(home) + "/.blender/" + versionstr;
68  }
69  else {
70  return nullptr;
71  }
72  }
73  return user_path.c_str();
74  }
75  if (user_path.empty() || last_version != version) {
76  const char *home = getenv("XDG_CONFIG_HOME");
77 
78  last_version = version;
79 
80  if (home) {
81  user_path = string(home) + "/blender/" + versionstr;
82  }
83  else {
84  home = getenv("HOME");
85  if (home == nullptr) {
86  home = getpwuid(getuid())->pw_dir;
87  }
88  user_path = string(home) + "/.config/blender/" + versionstr;
89  }
90  }
91 
92  return user_path.c_str();
93 }
94 
96 {
97  const char *type_str;
98  std::string add_path = "";
99 
100  switch (type) {
102  type_str = "DESKTOP";
103  break;
105  type_str = "DOCUMENTS";
106  break;
108  type_str = "DOWNLOAD";
109  break;
111  type_str = "MUSIC";
112  break;
114  type_str = "PICTURES";
115  break;
117  type_str = "VIDEOS";
118  break;
120  const char *cache_dir = getenv("XDG_CACHE_HOME");
121  if (cache_dir) {
122  return cache_dir;
123  }
124  /* Fallback to ~home/.cache/.
125  * When invoking `xdg-user-dir` without parameters the user folder
126  * will be read. `.cache` will be appended. */
127  type_str = "";
128  add_path = ".cache";
129  break;
130  }
131  default:
132  GHOST_ASSERT(
133  false,
134  "GHOST_SystemPathsUnix::getUserSpecialDir(): Invalid enum value for type parameter");
135  return nullptr;
136  }
137 
138  static string path = "";
139  /* Pipe `stderr` to `/dev/null` to avoid error prints. We will fail gracefully still. */
140  string command = string("xdg-user-dir ") + type_str + " 2> /dev/null";
141 
142  FILE *fstream = popen(command.c_str(), "r");
143  if (fstream == nullptr) {
144  return nullptr;
145  }
146  std::stringstream path_stream;
147  while (!feof(fstream)) {
148  char c = fgetc(fstream);
149  /* `xdg-user-dir` ends the path with '\n'. */
150  if (c == '\n') {
151  break;
152  }
153  path_stream << c;
154  }
155  if (pclose(fstream) == -1) {
156  perror("GHOST_SystemPathsUnix::getUserSpecialDir failed at pclose()");
157  return nullptr;
158  }
159 
160  if (!add_path.empty()) {
161  path_stream << '/' << add_path;
162  }
163 
164  path = path_stream.str();
165  return path[0] ? path.c_str() : nullptr;
166 }
167 
169 {
170  return nullptr;
171 }
172 
173 void GHOST_SystemPathsUnix::addToSystemRecentFiles(const char * /*filename*/) const
174 {
175  /* TODO: implement for X11 */
176 }
#define GHOST_ASSERT(x, info)
Definition: GHOST_Debug.h:54
static const char * static_path
GHOST_TUserSpecialDirTypes
Definition: GHOST_Types.h:562
@ GHOST_kUserSpecialDirDesktop
Definition: GHOST_Types.h:563
@ GHOST_kUserSpecialDirMusic
Definition: GHOST_Types.h:566
@ GHOST_kUserSpecialDirPictures
Definition: GHOST_Types.h:567
@ GHOST_kUserSpecialDirVideos
Definition: GHOST_Types.h:568
@ GHOST_kUserSpecialDirDownloads
Definition: GHOST_Types.h:565
@ GHOST_kUserSpecialDirCaches
Definition: GHOST_Types.h:569
@ GHOST_kUserSpecialDirDocuments
Definition: GHOST_Types.h:564
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
const char * getBinaryDir() const
const char * getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
void addToSystemRecentFiles(const char *filename) const
const char * getSystemDir(int version, const char *versionstr) const
const char * getUserDir(int version, const char *versionstr) const
static unsigned c
Definition: RandGen.cpp:83