From: Mike Yuan Date: Sat, 14 Sep 2024 17:02:32 +0000 (+0200) Subject: path-lookup: introduce user_search_dirs() (shall replace xdg_user_dirs()) X-Git-Tag: v257-rc1~317^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1f8eedba9d9b2c81455c788d0edca2146d5f30a4;p=thirdparty%2Fsystemd.git path-lookup: introduce user_search_dirs() (shall replace xdg_user_dirs()) xdg_user_dirs() doesn't seem well-organized currently. In all other xdg_user_*() funcs we assume /etc/xdg/systemd to be a symlink to /etc/systemd/, hence it is the odd one out. Also, when the relevant envvar is unset, it only returns the global search dirs. sd_path_lookup() actually covers this nicely with SD_PATH_SEARCH_*, where the combined search paths (from user home and system) are used. Therefore, let's introduce a wrapper for that, and deprecate xdg_user_dirs() (would be removed in later commits). --- diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 63251444076..6200acd8f52 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -17,6 +17,27 @@ #include "tmpfile-util.h" #include "user-util.h" +int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs) { + _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; + int r; + + assert(ret_config_dirs); + assert(ret_data_dirs); + + r = sd_path_lookup_strv(SD_PATH_SEARCH_CONFIGURATION, suffix, &config_dirs); + if (r < 0) + return r; + + r = sd_path_lookup_strv(SD_PATH_SEARCH_SHARED, suffix, &data_dirs); + if (r < 0) + return r; + + *ret_config_dirs = TAKE_PTR(config_dirs); + *ret_data_dirs = TAKE_PTR(data_dirs); + + return 0; +} + int runtime_directory(RuntimeScope scope, const char *suffix, char **ret) { int r; diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index ca901841256..3ef831323a7 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -65,6 +65,7 @@ int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs); /* 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. */ +int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs); static inline int xdg_user_runtime_dir(const char *suffix, char **ret) { return sd_path_lookup(SD_PATH_USER_RUNTIME, suffix, ret); } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 25403325e78..421e465d71b 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -349,49 +349,35 @@ static int log_unresolvable_specifier(const char *filename, unsigned line) { arg_dry_run ? (would) : (doing), \ __VA_ARGS__) -static int user_config_paths(char*** ret) { +static int user_config_paths(char ***ret) { _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; - _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; - _cleanup_strv_free_ char **res = NULL; + _cleanup_free_ char *runtime_config = NULL; int r; - r = xdg_user_dirs(&config_dirs, &data_dirs); - if (r < 0) - return r; + assert(ret); - r = xdg_user_config_dir("/user-tmpfiles.d", &persistent_config); - if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) + /* Combined user-specific and global dirs */ + r = user_search_dirs("/user-tmpfiles.d", &config_dirs, &data_dirs); + if (r < 0) return r; 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("/user-tmpfiles.d", &data_home); - if (r < 0 && !ERRNO_IS_NEG_NOINFO(r)) - return r; - - r = strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/user-tmpfiles.d"); - if (r < 0) - return r; - - r = strv_extend_many( - &res, - persistent_config, - runtime_config, - data_home); + r = strv_consume(&config_dirs, TAKE_PTR(runtime_config)); if (r < 0) return r; - r = strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/user-tmpfiles.d"); + r = strv_extend_strv_consume(&config_dirs, TAKE_PTR(data_dirs), /* filter_duplicates = */ true); if (r < 0) return r; - r = path_strv_make_absolute_cwd(res); + r = path_strv_make_absolute_cwd(config_dirs); if (r < 0) return r; - *ret = TAKE_PTR(res); + *ret = TAKE_PTR(config_dirs); return 0; }