path-lookup: deduplicate xdg_user_*() with sd_path_lookup()

While at it, place ret param at last.
This commit is contained in:
Mike Yuan
2024-08-23 18:55:24 +02:00
parent f005e267bc
commit 60cd6deb06
8 changed files with 36 additions and 97 deletions

View File

@@ -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,

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -3,6 +3,8 @@
#include <stdbool.h>
#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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);