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"
26 #include "path-util.h"
27 #include "string-util.h"
29 #include "user-util.h"
32 static int from_environment(const char *envname
, const char *fallback
, const char **ret
) {
38 e
= secure_getenv(envname
);
39 if (e
&& path_is_absolute(e
)) {
53 static int from_home_dir(const char *envname
, const char *suffix
, char **buffer
, const char **ret
) {
54 _cleanup_free_
char *h
= NULL
;
65 e
= secure_getenv(envname
);
66 if (e
&& path_is_absolute(e
)) {
77 cc
= strappend(h
, suffix
);
79 cc
= strjoin(h
, "/", suffix
, NULL
);
88 static int from_user_dir(const char *field
, char **buffer
, const char **ret
) {
89 _cleanup_fclose_
FILE *f
= NULL
;
90 _cleanup_free_
char *b
= NULL
;
91 const char *fn
= NULL
;
100 r
= from_home_dir(NULL
, ".config/user-dirs.dirs", &b
, &fn
);
112 /* This is an awful parse, but it follows closely what
113 * xdg-user-dirs does upstream */
116 FOREACH_LINE(line
, f
, return -errno
) {
121 if (!strneq(l
, field
, n
))
125 p
+= strspn(p
, WHITESPACE
);
131 p
+= strspn(p
, WHITESPACE
);
142 /* Three syntaxes permitted: relative to $HOME, $HOME itself, and absolute path */
143 if (startswith(p
, "$HOME/")) {
144 _cleanup_free_
char *h
= NULL
;
147 r
= get_home_dir(&h
);
151 cc
= strappend(h
, p
+5);
158 } else if (streq(p
, "$HOME")) {
160 r
= get_home_dir(buffer
);
166 } else if (path_is_absolute(p
)) {
180 /* The desktop directory defaults to $HOME/Desktop, the others to $HOME */
181 if (streq(field
, "XDG_DESKTOP_DIR")) {
182 _cleanup_free_
char *h
= NULL
;
185 r
= get_home_dir(&h
);
189 cc
= strappend(h
, "/Desktop");
197 r
= get_home_dir(buffer
);
207 static int get_path(uint64_t type
, char **buffer
, const char **ret
) {
215 case SD_PATH_TEMPORARY
:
216 return from_environment("TMPDIR", "/tmp", ret
);
218 case SD_PATH_TEMPORARY_LARGE
:
219 return from_environment("TMPDIR", "/var/tmp", ret
);
221 case SD_PATH_SYSTEM_BINARIES
:
225 case SD_PATH_SYSTEM_INCLUDE
:
226 *ret
= "/usr/include";
229 case SD_PATH_SYSTEM_LIBRARY_PRIVATE
:
233 case SD_PATH_SYSTEM_LIBRARY_ARCH
:
237 case SD_PATH_SYSTEM_SHARED
:
241 case SD_PATH_SYSTEM_CONFIGURATION_FACTORY
:
242 *ret
= "/usr/share/factory/etc";
245 case SD_PATH_SYSTEM_STATE_FACTORY
:
246 *ret
= "/usr/share/factory/var";
249 case SD_PATH_SYSTEM_CONFIGURATION
:
253 case SD_PATH_SYSTEM_RUNTIME
:
257 case SD_PATH_SYSTEM_RUNTIME_LOGS
:
261 case SD_PATH_SYSTEM_STATE_PRIVATE
:
265 case SD_PATH_SYSTEM_STATE_LOGS
:
269 case SD_PATH_SYSTEM_STATE_CACHE
:
273 case SD_PATH_SYSTEM_STATE_SPOOL
:
277 case SD_PATH_USER_BINARIES
:
278 return from_home_dir(NULL
, ".local/bin", buffer
, ret
);
280 case SD_PATH_USER_LIBRARY_PRIVATE
:
281 return from_home_dir(NULL
, ".local/lib", buffer
, ret
);
283 case SD_PATH_USER_LIBRARY_ARCH
:
284 return from_home_dir(NULL
, ".local/lib/" LIB_ARCH_TUPLE
, buffer
, ret
);
286 case SD_PATH_USER_SHARED
:
287 return from_home_dir("XDG_DATA_HOME", ".local/share", buffer
, ret
);
289 case SD_PATH_USER_CONFIGURATION
:
290 return from_home_dir("XDG_CONFIG_HOME", ".config", buffer
, ret
);
292 case SD_PATH_USER_RUNTIME
:
293 return from_environment("XDG_RUNTIME_DIR", NULL
, ret
);
295 case SD_PATH_USER_STATE_CACHE
:
296 return from_home_dir("XDG_CACHE_HOME", ".cache", buffer
, ret
);
299 r
= get_home_dir(buffer
);
306 case SD_PATH_USER_DOCUMENTS
:
307 return from_user_dir("XDG_DOCUMENTS_DIR", buffer
, ret
);
309 case SD_PATH_USER_MUSIC
:
310 return from_user_dir("XDG_MUSIC_DIR", buffer
, ret
);
312 case SD_PATH_USER_PICTURES
:
313 return from_user_dir("XDG_PICTURES_DIR", buffer
, ret
);
315 case SD_PATH_USER_VIDEOS
:
316 return from_user_dir("XDG_VIDEOS_DIR", buffer
, ret
);
318 case SD_PATH_USER_DOWNLOAD
:
319 return from_user_dir("XDG_DOWNLOAD_DIR", buffer
, ret
);
321 case SD_PATH_USER_PUBLIC
:
322 return from_user_dir("XDG_PUBLICSHARE_DIR", buffer
, ret
);
324 case SD_PATH_USER_TEMPLATES
:
325 return from_user_dir("XDG_TEMPLATES_DIR", buffer
, ret
);
327 case SD_PATH_USER_DESKTOP
:
328 return from_user_dir("XDG_DESKTOP_DIR", buffer
, ret
);
334 _public_
int sd_path_home(uint64_t type
, const char *suffix
, char **path
) {
335 char *buffer
= NULL
, *cc
;
339 assert_return(path
, -EINVAL
);
342 SD_PATH_SEARCH_BINARIES
,
343 SD_PATH_SEARCH_LIBRARY_PRIVATE
,
344 SD_PATH_SEARCH_LIBRARY_ARCH
,
345 SD_PATH_SEARCH_SHARED
,
346 SD_PATH_SEARCH_CONFIGURATION_FACTORY
,
347 SD_PATH_SEARCH_STATE_FACTORY
,
348 SD_PATH_SEARCH_CONFIGURATION
)) {
350 _cleanup_strv_free_
char **l
= NULL
;
352 r
= sd_path_search(type
, suffix
, &l
);
356 buffer
= strv_join(l
, ":");
364 r
= get_path(type
, &buffer
, &ret
);
370 buffer
= strdup(ret
);
379 suffix
+= strspn(suffix
, "/");
381 if (endswith(ret
, "/"))
382 cc
= strappend(ret
, suffix
);
384 cc
= strjoin(ret
, "/", suffix
, NULL
);
395 static int search_from_environment(
397 const char *env_home
,
398 const char *home_suffix
,
399 const char *env_search
,
400 bool env_search_sufficient
,
401 const char *first
, ...) {
411 e
= secure_getenv(env_search
);
413 l
= strv_split(e
, ":");
417 if (env_search_sufficient
) {
428 l
= strv_new_ap(first
, ap
);
436 e
= secure_getenv(env_home
);
437 if (e
&& path_is_absolute(e
)) {
446 if (!h
&& home_suffix
) {
447 e
= secure_getenv("HOME");
448 if (e
&& path_is_absolute(e
)) {
449 if (endswith(e
, "/"))
450 h
= strappend(e
, home_suffix
);
452 h
= strjoin(e
, "/", home_suffix
, NULL
);
462 r
= strv_consume_prepend(&l
, h
);
473 static int get_search(uint64_t type
, char ***list
) {
479 case SD_PATH_SEARCH_BINARIES
:
480 return search_from_environment(list
,
489 #ifdef HAVE_SPLIT_USR
495 case SD_PATH_SEARCH_LIBRARY_PRIVATE
:
496 return search_from_environment(list
,
503 #ifdef HAVE_SPLIT_USR
508 case SD_PATH_SEARCH_LIBRARY_ARCH
:
509 return search_from_environment(list
,
511 ".local/lib/" LIB_ARCH_TUPLE
,
515 #ifdef HAVE_SPLIT_USR
520 case SD_PATH_SEARCH_SHARED
:
521 return search_from_environment(list
,
530 case SD_PATH_SEARCH_CONFIGURATION_FACTORY
:
531 return search_from_environment(list
,
536 "/usr/local/share/factory/etc",
537 "/usr/share/factory/etc",
540 case SD_PATH_SEARCH_STATE_FACTORY
:
541 return search_from_environment(list
,
546 "/usr/local/share/factory/var",
547 "/usr/share/factory/var",
550 case SD_PATH_SEARCH_CONFIGURATION
:
551 return search_from_environment(list
,
563 _public_
int sd_path_search(uint64_t type
, const char *suffix
, char ***paths
) {
564 char **l
, **i
, **j
, **n
;
567 assert_return(paths
, -EINVAL
);
570 SD_PATH_SEARCH_BINARIES
,
571 SD_PATH_SEARCH_LIBRARY_PRIVATE
,
572 SD_PATH_SEARCH_LIBRARY_ARCH
,
573 SD_PATH_SEARCH_SHARED
,
574 SD_PATH_SEARCH_CONFIGURATION_FACTORY
,
575 SD_PATH_SEARCH_STATE_FACTORY
,
576 SD_PATH_SEARCH_CONFIGURATION
)) {
580 r
= sd_path_home(type
, suffix
, &p
);
597 r
= get_search(type
, &l
);
606 n
= new(char*, strv_length(l
)+1);
615 if (endswith(*i
, "/"))
616 *j
= strappend(*i
, suffix
);
618 *j
= strjoin(*i
, "/", suffix
, NULL
);