1
0
mirror of https://github.com/systemd/systemd synced 2024-07-09 04:26:06 +00:00

core: Add %D specifier for $XDG_DATA_HOME

We already have specifiers that resolve to $XDG_STATE_HOME, and
$XDG_CONFIG_HOME. $XDG_DATA_HOME is in a similar vein.

It allows units belonging to the user service manager to correctly look
into ~/.local/share. I imagine this would be most useful inside of
condition checks (i.e. only run a service on session startup if some
data is not found in ~/.local/share) or in the inotify monitoring of a
.path unit
This commit is contained in:
Adrian Vovk 2023-12-28 18:12:06 -05:00 committed by Lennart Poettering
parent 2743854540
commit cc51085a41
2 changed files with 16 additions and 0 deletions

View File

@ -2302,6 +2302,11 @@
<entry>Credentials directory</entry>
<entry>This is the value of the <literal>$CREDENTIALS_DIRECTORY</literal> environment variable if available. See section "Credentials" in <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more information.</entry>
</row>
<row>
<entry><literal>%D</literal></entry>
<entry>Shared data directory</entry>
<entry>This is either <filename>/usr/share/</filename> (for the system manager) or the path <literal>$XDG_DATA_HOME</literal> resolves to (for user managers).</entry>
</row>
<row>
<entry><literal>%E</literal></entry>
<entry>Configuration directory root</entry>

View File

@ -4,6 +4,7 @@
#include "cgroup-util.h"
#include "format-util.h"
#include "macro.h"
#include "sd-path.h"
#include "specifier.h"
#include "string-util.h"
#include "strv.h"
@ -164,6 +165,14 @@ static int specifier_credentials_dir(char specifier, const void *data, const cha
return 0;
}
static int specifier_shared_data_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
const Unit *u = ASSERT_PTR(userdata);
assert(ret);
return sd_path_lookup(MANAGER_IS_SYSTEM(u->manager) ? SD_PATH_SYSTEM_SHARED : SD_PATH_USER_SHARED, NULL, ret);
}
int unit_name_printf(const Unit *u, const char* format, char **ret) {
/*
* This will use the passed string as format string and replace the following specifiers (which should all be
@ -208,6 +217,7 @@ int unit_full_printf_full(const Unit *u, const char *format, size_t max_length,
*
* %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
* %d: the credentials directory ($CREDENTIALS_DIRECTORY)
* %D: the shared data root (e.g. /usr/share or $XDG_DATA_HOME)
* %E: the configuration directory root (e.g. /etc or $XDG_CONFIG_HOME)
* %L: the log directory root (e.g. /var/log or $XDG_STATE_HOME/log)
* %S: the state directory root (e.g. /var/lib or $XDG_STATE_HOME)
@ -245,6 +255,7 @@ int unit_full_printf_full(const Unit *u, const char *format, size_t max_length,
{ 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE) },
{ 'd', specifier_credentials_dir, NULL },
{ 'D', specifier_shared_data_dir, NULL },
{ 'E', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CONFIGURATION) },
{ 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS) },
{ 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE) },