Blender V5.3
fsmenu_system_unix.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_fileops.hh"
17#include "BLI_ghash.hh"
18#include "BLI_listbase.hh"
19#include "BLI_path_utils.hh"
20#include "BLI_string.hh"
21#include "BLI_utildefines.hh"
22
23#include "DNA_userdef_types.h"
24
25#include "BLT_translation.hh"
26
27#include "ED_fileselect.hh"
28
29#include "UI_resources.hh"
30
31#ifdef __linux__
32# include "BLI_fileops_types.hh"
33# include <mntent.h>
34
35# include "CLG_log.h"
36
37static CLG_LogRef LOG = {"system.path"};
38#endif
39
40#include "fsmenu.hh"
41
42namespace blender {
43
44struct FSMenu;
45
46/* -------------------------------------------------------------------- */
51
56static GHash *fsmenu_xdg_user_dirs_parse(const char *home)
57{
58 /* Add to the default for variable, equals & quotes. */
59 char l[128 + FILE_MAXDIR];
60 FILE *fp;
61
62 /* Check if the config file exists. */
63 {
64 char filepath[FILE_MAX];
65 const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
66 if (xdg_config_home != nullptr) {
67 BLI_path_join(filepath, sizeof(filepath), xdg_config_home, "user-dirs.dirs");
68 }
69 else {
70 BLI_path_join(filepath, sizeof(filepath), home, ".config", "user-dirs.dirs");
71 }
72 fp = BLI_fopen(filepath, "r");
73 if (!fp) {
74 return nullptr;
75 }
76 }
77 /* By default there are 8 paths. */
78 GHash *xdg_map = BLI_ghash_str_new_ex(__func__, 8);
79 while (fgets(l, sizeof(l), fp) != nullptr) { /* read a line */
80
81 /* Avoid inserting invalid values. */
82 if (STRPREFIX(l, "XDG_")) {
83 char *l_value = strchr(l, '=');
84 if (l_value != nullptr) {
85 *l_value = '\0';
86 l_value++;
87
88 BLI_str_rstrip(l_value);
89 const uint l_value_len = strlen(l_value);
90 if ((l_value[0] == '"') && (l_value_len > 0) && (l_value[l_value_len - 1] == '"')) {
91 l_value[l_value_len - 1] = '\0';
92 l_value++;
93
94 char l_value_expanded[FILE_MAX];
95 char *l_value_final = l_value;
96
97 /* This is currently the only variable used.
98 * Based on the 'user-dirs.dirs' man page,
99 * there is no need to resolve arbitrary environment variables. */
100 if (STRPREFIX(l_value, "$HOME" SEP_STR)) {
101 BLI_path_join(l_value_expanded, sizeof(l_value_expanded), home, l_value + 6);
102 l_value_final = l_value_expanded;
103 }
104
105 BLI_ghash_insert(xdg_map, BLI_strdup(l), BLI_strdup(l_value_final));
106 }
107 }
108 }
109 }
110 fclose(fp);
111
112 return xdg_map;
113}
114
115static void fsmenu_xdg_user_dirs_free(GHash *xdg_map)
116{
117 if (xdg_map != nullptr) {
119 }
120}
121
130static void fsmenu_xdg_insert_entry(GHash *xdg_map,
131 FSMenu *fsmenu,
132 const char *key,
133 const char *default_path,
134 int icon,
135 const char *home)
136{
137 char xdg_path_buf[FILE_MAXDIR];
138 const char *xdg_path = static_cast<const char *>(xdg_map ? BLI_ghash_lookup(xdg_map, key) :
139 nullptr);
140 if (xdg_path == nullptr) {
141 BLI_path_join(xdg_path_buf, sizeof(xdg_path_buf), home, default_path);
142 xdg_path = xdg_path_buf;
143 }
145 fsmenu, FS_CATEGORY_SYSTEM_BOOKMARKS, xdg_path, default_path, icon, FS_INSERT_LAST);
146}
147
149
150void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
151{
152 const char *home = BLI_dir_home();
153
154 if (read_bookmarks && home) {
156 fsmenu, FS_CATEGORY_SYSTEM_BOOKMARKS, home, N_("Home"), ICON_HOME, FS_INSERT_LAST);
157
158 /* Follow the XDG spec, check if these are available. */
159 GHash *xdg_map = fsmenu_xdg_user_dirs_parse(home);
160
161 struct {
162 const char *key;
163 const char *default_path;
164 BIFIconID icon;
165 } xdg_items[] = {
166 {"XDG_DESKTOP_DIR", "Desktop", ICON_DESKTOP},
167 {"XDG_DOCUMENTS_DIR", "Documents", ICON_DOCUMENTS},
168 {"XDG_DOWNLOAD_DIR", "Downloads", ICON_IMPORT},
169 {"XDG_VIDEOS_DIR", "Videos", ICON_FILE_MOVIE},
170 {"XDG_PICTURES_DIR", "Pictures", ICON_FILE_IMAGE},
171 {"XDG_MUSIC_DIR", "Music", ICON_FILE_SOUND},
172 };
173
174 for (int i = 0; i < ARRAY_SIZE(xdg_items); i++) {
176 xdg_map, fsmenu, xdg_items[i].key, xdg_items[i].default_path, xdg_items[i].icon, home);
177 }
178
180 }
181
182 {
183 bool found = false;
184#ifdef __linux__
185 /* loop over mount points */
186 mntent *mnt;
187 FILE *fp;
188
189 fp = setmntent(MOUNTED, "r");
190 if (fp == nullptr) {
191 CLOG_WARN(&LOG, "Could not get a list of mounted file-systems");
192 }
193 else {
194
195 /* Similar to `STRPREFIX`,
196 * but ensures the prefix precedes a directory separator or null terminator.
197 * Define locally since it's fairly specific to this particular use case. */
198 auto strncmp_dir_delimit = [](const char *a, const char *b, size_t b_len) -> int {
199 const int result = strncmp(a, b, b_len);
200 return (result == 0 && !ELEM(a[b_len], '\0', '/')) ? 1 : result;
201 };
202# define STRPREFIX_DIR_DELIMIT(a, b) (strncmp_dir_delimit((a), (b), strlen(b)) == 0)
203
204 while ((mnt = getmntent(fp))) {
205 if (STRPREFIX_DIR_DELIMIT(mnt->mnt_dir, "/boot") ||
206 /* According to: https://wiki.archlinux.org/title/EFI_system_partition (2025),
207 * this is a common path to mount the EFI partition. */
208 STRPREFIX_DIR_DELIMIT(mnt->mnt_dir, "/efi"))
209 {
210 /* Hide share not usable to the user. */
211 continue;
212 }
213 if (!STRPREFIX_DIR_DELIMIT(mnt->mnt_fsname, "/dev")) {
214 continue;
215 }
216 /* Use non-delimited prefix since a slash isn't expected after loop. */
217 if (STRPREFIX(mnt->mnt_fsname, "/dev/loop")) {
218 /* The `/dev/loop*` entries are SNAPS used by desktop environment
219 * (GNOME) no need for them to show up in the list. */
220 continue;
221 }
222
224 fsmenu, FS_CATEGORY_SYSTEM, mnt->mnt_dir, nullptr, ICON_DISK_DRIVE, FS_INSERT_SORTED);
225
226 found = true;
227 }
228# undef STRPREFIX_DIR_DELIMIT
229
230 if (endmntent(fp) == 0) {
231 CLOG_WARN(&LOG, "Could not close the list of mounted file-systems");
232 }
233 }
234 /* Check `gvfs` shares. */
235 const char *const xdg_runtime_dir = BLI_getenv("XDG_RUNTIME_DIR");
236 if (xdg_runtime_dir != nullptr) {
237 direntry *dirs;
238 char filepath[FILE_MAX];
239 BLI_path_join(filepath, sizeof(filepath), xdg_runtime_dir, "gvfs/");
240 /* Avoid error message if the directory doesn't exist as this isn't a requirement. */
241 if (BLI_is_dir(filepath)) {
242 const uint dirs_num = BLI_filelist_dir_contents(filepath, &dirs);
243 for (uint i = 0; i < dirs_num; i++) {
244 if ((dirs[i].type & S_IFDIR) == 0) {
245 continue;
246 }
247 const char *dirname = dirs[i].relname;
248 if (dirname[0] == '.') {
249 continue;
250 }
251
252 /* Directory names contain a lot of unwanted text.
253 * Assuming every entry ends with the share name. */
254 const char *label = strstr(dirname, "share=");
255 if (label != nullptr) {
256 /* Move pointer so `share=` is trimmed off or use full `dirname` as label. */
257 const char *label_test = label + 6;
258 label = *label_test ? label_test : dirname;
259 }
260 char line[FILE_MAXDIR];
261 SNPRINTF(line, "%s%s", filepath, dirname);
263 fsmenu, FS_CATEGORY_SYSTEM, line, label, ICON_NETWORK_DRIVE, FS_INSERT_SORTED);
264 found = true;
265 }
266 BLI_filelist_free(dirs, dirs_num);
267 }
268 }
269#endif /* __linux__ */
270
271 /* fallback */
272 if (!found) {
274 fsmenu, FS_CATEGORY_SYSTEM, "/", nullptr, ICON_DISK_DRIVE, FS_INSERT_SORTED);
275 }
276 }
277}
278
279} // namespace blender
File and directory operations.
Some types for dealing with directories.
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
const char * dirname(char *path)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Read Blender memory allocation and freeing.
#define STRPREFIX(a, b)
#define ELEM(...)
#define LOG(level)
Definition log.h:97
BMesh Iterators.
unsigned int uint
Definition BKE_text.h:55
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void MEM_delete_void(void *vmemh)
static void fsmenu_xdg_insert_entry(GHash *xdg_map, FSMenu *fsmenu, const char *key, const char *default_path, int icon, const char *home)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
const char * BLI_dir_home()
Definition storage.cc:99
void fsmenu_insert_entry(FSMenu *fsmenu, FSMenuCategory category, const char *path, const char *name, int icon, FSMenuInsert flag)
Definition fsmenu.cc:229
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
GHash * BLI_ghash_str_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
static GHash * fsmenu_xdg_user_dirs_parse(const char *home)
int BIFIconID
Definition ED_asset.hh:30
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
void BLI_str_rstrip(char *str) ATTR_NONNULL(1)
@ FS_CATEGORY_SYSTEM_BOOKMARKS
void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
static void fsmenu_xdg_user_dirs_free(GHash *xdg_map)
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define SEP_STR
Definition unit.cc:41
#define N_(msgid)