2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "architecture.h"
24 #include "path-util.h"
25 #include "string-util.h"
29 static int from_environment(const char *envname
, const char *fallback
, const char **ret
) {
35 e
= secure_getenv(envname
);
36 if (e
&& path_is_absolute(e
)) {
50 static int from_home_dir(const char *envname
, const char *suffix
, char **buffer
, const char **ret
) {
51 _cleanup_free_
char *h
= NULL
;
62 e
= secure_getenv(envname
);
63 if (e
&& path_is_absolute(e
)) {
74 cc
= strappend(h
, suffix
);
76 cc
= strjoin(h
, "/", suffix
, NULL
);
85 static int from_user_dir(const char *field
, char **buffer
, const char **ret
) {
86 _cleanup_fclose_
FILE *f
= NULL
;
87 _cleanup_free_
char *b
= NULL
;
88 const char *fn
= NULL
;
97 r
= from_home_dir(NULL
, ".config/user-dirs.dirs", &b
, &fn
);
109 /* This is an awful parse, but it follows closely what
110 * xdg-user-dirs does upstream */
113 FOREACH_LINE(line
, f
, return -errno
) {
118 if (!strneq(l
, field
, n
))
122 p
+= strspn(p
, WHITESPACE
);
128 p
+= strspn(p
, WHITESPACE
);
139 /* Three syntaxes permitted: relative to $HOME, $HOME itself, and absolute path */
140 if (startswith(p
, "$HOME/")) {
141 _cleanup_free_
char *h
= NULL
;
144 r
= get_home_dir(&h
);
148 cc
= strappend(h
, p
+5);
155 } else if (streq(p
, "$HOME")) {
157 r
= get_home_dir(buffer
);
163 } else if (path_is_absolute(p
)) {
177 /* The desktop directory defaults to $HOME/Desktop, the others to $HOME */
178 if (streq(field
, "XDG_DESKTOP_DIR")) {
179 _cleanup_free_
char *h
= NULL
;
182 r
= get_home_dir(&h
);
186 cc
= strappend(h
, "/Desktop");
194 r
= get_home_dir(buffer
);
204 static int get_path(uint64_t type
, char **buffer
, const char **ret
) {
212 case SD_PATH_TEMPORARY
:
213 return from_environment("TMPDIR", "/tmp", ret
);
215 case SD_PATH_TEMPORARY_LARGE
:
216 return from_environment("TMPDIR", "/var/tmp", ret
);
218 case SD_PATH_SYSTEM_BINARIES
:
222 case SD_PATH_SYSTEM_INCLUDE
:
223 *ret
= "/usr/include";
226 case SD_PATH_SYSTEM_LIBRARY_PRIVATE
:
230 case SD_PATH_SYSTEM_LIBRARY_ARCH
:
234 case SD_PATH_SYSTEM_SHARED
:
238 case SD_PATH_SYSTEM_CONFIGURATION_FACTORY
:
239 *ret
= "/usr/share/factory/etc";
242 case SD_PATH_SYSTEM_STATE_FACTORY
:
243 *ret
= "/usr/share/factory/var";
246 case SD_PATH_SYSTEM_CONFIGURATION
:
250 case SD_PATH_SYSTEM_RUNTIME
:
254 case SD_PATH_SYSTEM_RUNTIME_LOGS
:
258 case SD_PATH_SYSTEM_STATE_PRIVATE
:
262 case SD_PATH_SYSTEM_STATE_LOGS
:
266 case SD_PATH_SYSTEM_STATE_CACHE
:
270 case SD_PATH_SYSTEM_STATE_SPOOL
:
274 case SD_PATH_USER_BINARIES
:
275 return from_home_dir(NULL
, ".local/bin", buffer
, ret
);
277 case SD_PATH_USER_LIBRARY_PRIVATE
:
278 return from_home_dir(NULL
, ".local/lib", buffer
, ret
);
280 case SD_PATH_USER_LIBRARY_ARCH
:
281 return from_home_dir(NULL
, ".local/lib/" LIB_ARCH_TUPLE
, buffer
, ret
);
283 case SD_PATH_USER_SHARED
:
284 return from_home_dir("XDG_DATA_HOME", ".local/share", buffer
, ret
);
286 case SD_PATH_USER_CONFIGURATION
:
287 return from_home_dir("XDG_CONFIG_HOME", ".config", buffer
, ret
);
289 case SD_PATH_USER_RUNTIME
:
290 return from_environment("XDG_RUNTIME_DIR", NULL
, ret
);
292 case SD_PATH_USER_STATE_CACHE
:
293 return from_home_dir("XDG_CACHE_HOME", ".cache", buffer
, ret
);
296 r
= get_home_dir(buffer
);
303 case SD_PATH_USER_DOCUMENTS
:
304 return from_user_dir("XDG_DOCUMENTS_DIR", buffer
, ret
);
306 case SD_PATH_USER_MUSIC
:
307 return from_user_dir("XDG_MUSIC_DIR", buffer
, ret
);
309 case SD_PATH_USER_PICTURES
:
310 return from_user_dir("XDG_PICTURES_DIR", buffer
, ret
);
312 case SD_PATH_USER_VIDEOS
:
313 return from_user_dir("XDG_VIDEOS_DIR", buffer
, ret
);
315 case SD_PATH_USER_DOWNLOAD
:
316 return from_user_dir("XDG_DOWNLOAD_DIR", buffer
, ret
);
318 case SD_PATH_USER_PUBLIC
:
319 return from_user_dir("XDG_PUBLICSHARE_DIR", buffer
, ret
);
321 case SD_PATH_USER_TEMPLATES
:
322 return from_user_dir("XDG_TEMPLATES_DIR", buffer
, ret
);
324 case SD_PATH_USER_DESKTOP
:
325 return from_user_dir("XDG_DESKTOP_DIR", buffer
, ret
);
331 _public_
int sd_path_home(uint64_t type
, const char *suffix
, char **path
) {
332 char *buffer
= NULL
, *cc
;
336 assert_return(path
, -EINVAL
);
339 SD_PATH_SEARCH_BINARIES
,
340 SD_PATH_SEARCH_LIBRARY_PRIVATE
,
341 SD_PATH_SEARCH_LIBRARY_ARCH
,
342 SD_PATH_SEARCH_SHARED
,
343 SD_PATH_SEARCH_CONFIGURATION_FACTORY
,
344 SD_PATH_SEARCH_STATE_FACTORY
,
345 SD_PATH_SEARCH_CONFIGURATION
)) {
347 _cleanup_strv_free_
char **l
= NULL
;
349 r
= sd_path_search(type
, suffix
, &l
);
353 buffer
= strv_join(l
, ":");
361 r
= get_path(type
, &buffer
, &ret
);
367 buffer
= strdup(ret
);
376 suffix
+= strspn(suffix
, "/");
378 if (endswith(ret
, "/"))
379 cc
= strappend(ret
, suffix
);
381 cc
= strjoin(ret
, "/", suffix
, NULL
);
392 static int search_from_environment(
394 const char *env_home
,
395 const char *home_suffix
,
396 const char *env_search
,
397 bool env_search_sufficient
,
398 const char *first
, ...) {
408 e
= secure_getenv(env_search
);
410 l
= strv_split(e
, ":");
414 if (env_search_sufficient
) {
425 l
= strv_new_ap(first
, ap
);
433 e
= secure_getenv(env_home
);
434 if (e
&& path_is_absolute(e
)) {
443 if (!h
&& home_suffix
) {
444 e
= secure_getenv("HOME");
445 if (e
&& path_is_absolute(e
)) {
446 if (endswith(e
, "/"))
447 h
= strappend(e
, home_suffix
);
449 h
= strjoin(e
, "/", home_suffix
, NULL
);
459 r
= strv_consume_prepend(&l
, h
);
470 static int get_search(uint64_t type
, char ***list
) {
476 case SD_PATH_SEARCH_BINARIES
:
477 return search_from_environment(list
,
486 #ifdef HAVE_SPLIT_USR
492 case SD_PATH_SEARCH_LIBRARY_PRIVATE
:
493 return search_from_environment(list
,
500 #ifdef HAVE_SPLIT_USR
505 case SD_PATH_SEARCH_LIBRARY_ARCH
:
506 return search_from_environment(list
,
508 ".local/lib/" LIB_ARCH_TUPLE
,
512 #ifdef HAVE_SPLIT_USR
517 case SD_PATH_SEARCH_SHARED
:
518 return search_from_environment(list
,
527 case SD_PATH_SEARCH_CONFIGURATION_FACTORY
:
528 return search_from_environment(list
,
533 "/usr/local/share/factory/etc",
534 "/usr/share/factory/etc",
537 case SD_PATH_SEARCH_STATE_FACTORY
:
538 return search_from_environment(list
,
543 "/usr/local/share/factory/var",
544 "/usr/share/factory/var",
547 case SD_PATH_SEARCH_CONFIGURATION
:
548 return search_from_environment(list
,
560 _public_
int sd_path_search(uint64_t type
, const char *suffix
, char ***paths
) {
561 char **l
, **i
, **j
, **n
;
564 assert_return(paths
, -EINVAL
);
567 SD_PATH_SEARCH_BINARIES
,
568 SD_PATH_SEARCH_LIBRARY_PRIVATE
,
569 SD_PATH_SEARCH_LIBRARY_ARCH
,
570 SD_PATH_SEARCH_SHARED
,
571 SD_PATH_SEARCH_CONFIGURATION_FACTORY
,
572 SD_PATH_SEARCH_STATE_FACTORY
,
573 SD_PATH_SEARCH_CONFIGURATION
)) {
577 r
= sd_path_home(type
, suffix
, &p
);
594 r
= get_search(type
, &l
);
603 n
= new(char*, strv_length(l
)+1);
612 if (endswith(*i
, "/"))
613 *j
= strappend(*i
, suffix
);
615 *j
= strjoin(*i
, "/", suffix
, NULL
);