From: Mike Yuan Date: Fri, 23 Aug 2024 16:55:24 +0000 (+0200) Subject: path-lookup: deduplicate xdg_user_*() with sd_path_lookup() X-Git-Tag: v257-rc1~317^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60cd6deb06d5c70beed1c91fd6c79e2f9e1d13cd;p=thirdparty%2Fsystemd.git path-lookup: deduplicate xdg_user_*() with sd_path_lookup() While at it, place ret param at last. --- diff --git a/src/core/main.c b/src/core/main.c index e526c37e6a5..0c171a788d3 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -182,7 +182,7 @@ static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) { _cleanup_strv_free_ char **files = NULL, **dirs = NULL; int r; - r = xdg_user_config_dir(&base, "/systemd"); + r = xdg_user_config_dir("/systemd", &base); if (r < 0) return r; @@ -2479,7 +2479,7 @@ static int initialize_runtime( /* Create the runtime directory and place the inaccessible device nodes there, if we run in * user mode. In system mode mount_setup() already did that. */ - r = xdg_user_runtime_dir(&p, "/systemd"); + r = xdg_user_runtime_dir("/systemd", &p); if (r < 0) { *ret_error_message = "$XDG_RUNTIME_DIR is not set"; return log_struct_errno(LOG_EMERG, r, diff --git a/src/core/manager.c b/src/core/manager.c index 2dddc797222..18fb8fdaf8c 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1031,7 +1031,7 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, r = mkdir_label("/run/systemd/units", 0755); else { _cleanup_free_ char *units_path = NULL; - r = xdg_user_runtime_dir(&units_path, "/systemd/units"); + r = xdg_user_runtime_dir("/systemd/units", &units_path); if (r < 0) return r; diff --git a/src/core/unit.c b/src/core/unit.c index ae3cefcddea..c303ce9282a 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -5576,12 +5576,13 @@ static int unit_get_invocation_path(Unit *u, char **ret) { p = strjoin("/run/systemd/units/invocation:", u->id); else { _cleanup_free_ char *user_path = NULL; - r = xdg_user_runtime_dir(&user_path, "/systemd/units/invocation:"); + + r = xdg_user_runtime_dir("/systemd/units/invocation:", &user_path); if (r < 0) return r; + p = strjoin(user_path, u->id); } - if (!p) return -ENOMEM; diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index d4d888c3620..5d0d66914c9 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -17,80 +17,6 @@ #include "tmpfile-util.h" #include "user-util.h" -int xdg_user_runtime_dir(char **ret, const char *suffix) { - const char *e; - char *j; - - assert(ret); - assert(suffix); - - e = getenv("XDG_RUNTIME_DIR"); - if (!e) - return -ENXIO; - - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - - *ret = j; - return 0; -} - -int xdg_user_config_dir(char **ret, const char *suffix) { - _cleanup_free_ char *j = NULL; - const char *e; - int r; - - assert(ret); - - e = getenv("XDG_CONFIG_HOME"); - if (e) { - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - } else { - r = get_home_dir(&j); - if (r < 0) - return r; - - if (!path_extend(&j, "/.config", suffix)) - return -ENOMEM; - } - - *ret = TAKE_PTR(j); - return 0; -} - -int xdg_user_data_dir(char **ret, const char *suffix) { - _cleanup_free_ char *j = NULL; - const char *e; - int r; - - assert(ret); - assert(suffix); - - /* We don't treat /etc/xdg/systemd here as the spec - * suggests because we assume that is a link to - * /etc/systemd/ anyway. */ - - e = getenv("XDG_DATA_HOME"); - if (e) { - j = path_join(e, suffix); - if (!j) - return -ENOMEM; - } else { - r = get_home_dir(&j); - if (r < 0) - return r; - - if (!path_extend(&j, "/.local/share", suffix)) - return -ENOMEM; - } - - *ret = TAKE_PTR(j); - return 1; -} - int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { int r; @@ -109,7 +35,7 @@ int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) { return strdup_to(ret, e); if (scope == RUNTIME_SCOPE_USER) { - r = xdg_user_runtime_dir(ret, suffix); + r = xdg_user_runtime_dir(suffix, ret); if (r < 0) return r; } else { @@ -193,7 +119,7 @@ static char** user_dirs( if (r < 0) return NULL; - r = xdg_user_data_dir(&data_home, "/systemd/user"); + r = xdg_user_data_dir("/systemd/user", &data_home); if (r < 0 && r != -ENXIO) return NULL; @@ -326,7 +252,7 @@ static int acquire_transient_dir( else if (scope == RUNTIME_SCOPE_SYSTEM) transient = strdup("/run/systemd/transient"); else - return xdg_user_runtime_dir(ret, "/systemd/transient"); + return xdg_user_runtime_dir("/systemd/transient", ret); if (!transient) return -ENOMEM; @@ -354,11 +280,11 @@ static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **run break; case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir(&a, "/systemd/user"); + r = xdg_user_config_dir("/systemd/user", &a); if (r < 0 && r != -ENXIO) return r; - r = xdg_user_runtime_dir(runtime, "/systemd/user"); + r = xdg_user_runtime_dir("/systemd/user", runtime); if (r < 0) { if (r != -ENXIO) return r; @@ -411,11 +337,11 @@ static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **ru } case RUNTIME_SCOPE_USER: - r = xdg_user_config_dir(&a, "/systemd/user.control"); + r = xdg_user_config_dir("/systemd/user.control", &a); if (r < 0 && r != -ENXIO) return r; - r = xdg_user_runtime_dir(runtime, "/systemd/user.control"); + r = xdg_user_runtime_dir("/systemd/user.control", runtime); if (r < 0) { if (r != -ENXIO) return r; diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index 1176ad8871c..c325fa6041a 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -3,6 +3,8 @@ #include +#include "sd-path.h" + #include "runtime-scope.h" typedef enum LookupPathsFlags { @@ -53,17 +55,27 @@ typedef struct LookupPaths { int lookup_paths_init(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir); int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir); +void lookup_paths_log(LookupPaths *p); +void lookup_paths_done(LookupPaths *p); + int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); -int xdg_user_runtime_dir(char **ret, const char *suffix); -int xdg_user_config_dir(char **ret, const char *suffix); -int xdg_user_data_dir(char **ret, const char *suffix); + +/* 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. */ + +static inline int xdg_user_runtime_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_RUNTIME, suffix, ret); +} +static inline int xdg_user_config_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_CONFIGURATION, suffix, ret); +} +static inline int xdg_user_data_dir(const char *suffix, char **ret) { + return sd_path_lookup(SD_PATH_USER_SHARED, suffix, ret); +} int runtime_directory(char **ret, RuntimeScope scope, const char *suffix); bool path_is_user_data_dir(const char *path); bool path_is_user_config_dir(const char *path); -void lookup_paths_log(LookupPaths *p); -void lookup_paths_done(LookupPaths *p); - char **generator_binary_paths(RuntimeScope scope); char **env_generator_binary_paths(RuntimeScope scope); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index a7655aad1c7..25403325e78 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -359,15 +359,15 @@ static int user_config_paths(char*** ret) { if (r < 0) return r; - r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); + r = xdg_user_config_dir("/user-tmpfiles.d", &persistent_config); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; - r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); + r = xdg_user_runtime_dir("/user-tmpfiles.d", &runtime_config); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; - r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); + r = xdg_user_data_dir("/user-tmpfiles.d", &data_home); if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) return r; diff --git a/src/vmspawn/vmspawn-util.c b/src/vmspawn/vmspawn-util.c index 8a6122dd9dd..22cfa289fae 100644 --- a/src/vmspawn/vmspawn-util.c +++ b/src/vmspawn/vmspawn-util.c @@ -212,7 +212,7 @@ static int get_firmware_search_dirs(char ***ret) { * Prioritising entries in "more specific" directories */ _cleanup_free_ char *user_firmware_dir = NULL; - r = xdg_user_config_dir(&user_firmware_dir, "/qemu/firmware"); + r = xdg_user_config_dir("/qemu/firmware", &user_firmware_dir); if (r < 0) return r; diff --git a/src/xdg-autostart-generator/xdg-autostart-generator.c b/src/xdg-autostart-generator/xdg-autostart-generator.c index 71e1a664351..455f371fa8d 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -27,7 +27,7 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { _cleanup_free_ char *user_config_autostart_dir = NULL; int r; - r = xdg_user_config_dir(&user_config_autostart_dir, "/autostart"); + r = xdg_user_config_dir("/autostart", &user_config_autostart_dir); if (r < 0) return r; r = strv_extend(&autostart_dirs, user_config_autostart_dir);