]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-lookup: deduplicate xdg_user_*() with sd_path_lookup()
authorMike Yuan <me@yhndnzj.com>
Fri, 23 Aug 2024 16:55:24 +0000 (18:55 +0200)
committerMike Yuan <me@yhndnzj.com>
Sun, 6 Oct 2024 17:27:12 +0000 (19:27 +0200)
While at it, place ret param at last.

src/core/main.c
src/core/manager.c
src/core/unit.c
src/libsystemd/sd-path/path-lookup.c
src/libsystemd/sd-path/path-lookup.h
src/tmpfiles/tmpfiles.c
src/vmspawn/vmspawn-util.c
src/xdg-autostart-generator/xdg-autostart-generator.c

index e526c37e6a5cc1d5122eff17bfefe32d57a895f6..0c171a788d317465efdf9a03a25a3cb7c29f5dde 100644 (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,
index 2dddc797222686472135417ebee221bd6e121869..18fb8fdaf8cffe95b7aedd8e08a71d6e42693c74 100644 (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;
 
index ae3cefcddea1e55ec41213c8b066ede8d941ee96..c303ce9282a86d7771645d1932945a49129e46cb 100644 (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;
 
index d4d888c362085dcc968376f198a06de92b14393c..5d0d66914c9561d327596a047091c1b018d6339e 100644 (file)
 #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;
index 1176ad8871cadd7c81d112f83aa0f5d9bf74ab69..c325fa6041a2fad9cb04b3fbde55452091e0bda1 100644 (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);
index a7655aad1c7970adcd4ef17d2a38d1807445202e..25403325e783b535d2245c26dac314f7a29dad05 100644 (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;
 
index 8a6122dd9dddcaf671b78a6f798cd8f40be32c77..22cfa289fae8a27db9a9481092a620dc534cd4fb 100644 (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;
 
index 71e1a664351ffeb048ae540008e56d6c45e7221a..455f371fa8da16c7ff11949a8be837e0c0f65ea5 100644 (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);