path-lookup: add state_directory_generic() helper inspired by runtime_directory_generic()

This commit is contained in:
Lennart Poettering
2026-04-26 21:21:49 +02:00
parent bad594f79a
commit 45ceb97f6a
2 changed files with 28 additions and 0 deletions
+24
View File
@@ -140,6 +140,30 @@ int runtime_directory_make(RuntimeScope scope, const char *subdir, char **ret_di
return 0;
}
int state_directory_generic(RuntimeScope scope, const char *suffix, char **ret) {
assert(ret);
/* This does not bother with $STATE_DIRECTORY, and hence can be applied to get other service's state
* dir */
switch (scope) {
case RUNTIME_SCOPE_USER:
return xdg_user_state_dir(suffix, ret);
case RUNTIME_SCOPE_SYSTEM: {
char *d = path_join("/var/lib", suffix);
if (!d)
return -ENOMEM;
*ret = d;
return 0;
}
default:
return -EINVAL;
}
}
static const char* const user_data_unit_paths[] = {
"/usr/local/lib/systemd/user",
"/usr/local/share/systemd/user",
+4
View File
@@ -61,6 +61,7 @@ int config_directory_generic(RuntimeScope scope, const char *suffix, char **ret)
int runtime_directory_generic(RuntimeScope scope, const char *suffix, char **ret);
int runtime_directory(RuntimeScope scope, const char *fallback_suffix, char **ret);
int runtime_directory_make(RuntimeScope scope, const char *subdir, char **ret_dir, char **ret_dir_destroy);
int state_directory_generic(RuntimeScope scope, const char *suffix, char **ret);
/* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume
* that is a link to /etc/systemd/ anyway. */
@@ -75,6 +76,9 @@ static inline int xdg_user_config_dir(const char *suffix, char **ret) {
static inline int xdg_user_data_dir(const char *suffix, char **ret) {
return sd_path_lookup(SD_PATH_USER_SHARED, suffix, ret);
}
static inline int xdg_user_state_dir(const char *suffix, char **ret) {
return sd_path_lookup(SD_PATH_USER_STATE_PRIVATE, suffix, ret);
}
bool path_is_user_data_dir(const char *path);
bool path_is_user_config_dir(const char *path);