]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: make use of path_extend() at many places 19749/head
authorLennart Poettering <lennart@poettering.net>
Thu, 27 May 2021 14:27:06 +0000 (16:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 27 May 2021 15:05:38 +0000 (17:05 +0200)
This is not a comprehensive port, but mostly some low-hanging fruit.

src/basic/path-lookup.c
src/basic/path-util.c
src/basic/selinux-util.c
src/basic/tmpfile-util.c
src/core/execute.c
src/libsystemd/sd-path/sd-path.c

index ba5c2c47b946f0d90d48797f2fc5a8df669ff9c1..05eb17d66ccbb5f83056a5daa161c7776b31858f 100644 (file)
@@ -36,35 +36,33 @@ int xdg_user_runtime_dir(char **ret, const char *suffix) {
 }
 
 int xdg_user_config_dir(char **ret, const char *suffix) {
+        _cleanup_free_ char *j = NULL;
         const char *e;
-        char *j;
         int r;
 
         assert(ret);
 
         e = getenv("XDG_CONFIG_HOME");
-        if (e)
+        if (e) {
                 j = path_join(e, suffix);
-        else {
-                _cleanup_free_ char *home = NULL;
-
-                r = get_home_dir(&home);
+                if (!j)
+                        return -ENOMEM;
+        } else {
+                r = get_home_dir(&j);
                 if (r < 0)
                         return r;
 
-                j = path_join(home, "/.config", suffix);
+                if (!path_extend(&j, "/.config", suffix))
+                        return -ENOMEM;
         }
 
-        if (!j)
-                return -ENOMEM;
-
-        *ret = j;
+        *ret = TAKE_PTR(j);
         return 0;
 }
 
 int xdg_user_data_dir(char **ret, const char *suffix) {
+        _cleanup_free_ char *j = NULL;
         const char *e;
-        char *j;
         int r;
 
         assert(ret);
@@ -75,21 +73,20 @@ int xdg_user_data_dir(char **ret, const char *suffix) {
          * /etc/systemd/ anyway. */
 
         e = getenv("XDG_DATA_HOME");
-        if (e)
+        if (e) {
                 j = path_join(e, suffix);
-        else {
-                _cleanup_free_ char *home = NULL;
-
-                r = get_home_dir(&home);
+                if (!j)
+                        return -ENOMEM;
+        } else {
+                r = get_home_dir(&j);
                 if (r < 0)
                         return r;
 
-                j = path_join(home, "/.local/share", suffix);
+                if (!path_extend(&j, "/.local/share", suffix))
+                        return -ENOMEM;
         }
-        if (!j)
-                return -ENOMEM;
 
-        *ret = j;
+        *ret = TAKE_PTR(j);
         return 1;
 }
 
index f06858da6cb63fa9d342e3da8f8ccb0701fb6981..28b2c66b6efdb5549cc500cd8121ebca1f768933 100644 (file)
@@ -681,7 +681,7 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
 
         /* Resolve a single-component name to a full path */
         for (;;) {
-                _cleanup_free_ char *j = NULL, *element = NULL;
+                _cleanup_free_ char *element = NULL;
                 _cleanup_close_ int fd = -1;
 
                 r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
@@ -693,11 +693,10 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
                 if (!path_is_absolute(element))
                         continue;
 
-                j = path_join(element, name);
-                if (!j)
+                if (!path_extend(&element, name))
                         return -ENOMEM;
 
-                r = check_x_access(j, ret_fd ? &fd : NULL);
+                r = check_x_access(element, ret_fd ? &fd : NULL);
                 if (r < 0) {
                         /* PATH entries which we don't have access to are ignored, as per tradition. */
                         if (r != -EACCES)
@@ -707,7 +706,7 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
 
                 /* Found it! */
                 if (ret_filename)
-                        *ret_filename = path_simplify(TAKE_PTR(j), false);
+                        *ret_filename = path_simplify(TAKE_PTR(element), false);
                 if (ret_fd)
                         *ret_fd = TAKE_FD(fd);
 
index 0af541d0b1909e588dad5d29727558341a1c6f2c..30229509b35c5f9ec118b7633ae87b410f8500c5 100644 (file)
@@ -524,18 +524,17 @@ int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode)
                 return 0;
 
         if (!path_is_absolute(path)) {
-                _cleanup_free_ char *p = NULL;
-
                 if (dirfd == AT_FDCWD)
-                        r = safe_getcwd(&p);
+                        r = safe_getcwd(&abspath);
                 else
-                        r = fd_get_path(dirfd, &p);
+                        r = fd_get_path(dirfd, &abspath);
                 if (r < 0)
                         return r;
 
-                path = abspath = path_join(p, path);
-                if (!path)
+                if (!path_extend(&abspath, path))
                         return -ENOMEM;
+
+                path = abspath;
         }
 
         return selinux_create_file_prepare_abspath(path, mode);
index c5ae92f985fb5f6db862ea37bb459fbcc175c4b4..9eb654c1407bf18180f5caab0bca91c9de4efaca 100644 (file)
@@ -123,13 +123,10 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
                 return -EINVAL;
 
         if (d)  {
-                char *j;
-
-                j = path_join(d, nf);
-                if (!j)
+                if (!path_extend(&d, nf))
                         return -ENOMEM;
 
-                *ret = path_simplify(j, false);
+                *ret = path_simplify(TAKE_PTR(d), false);
         } else
                 *ret = TAKE_PTR(nf);
 
@@ -168,13 +165,10 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
                 return -EINVAL;
 
         if (d) {
-                char *j;
-
-                j = path_join(d, nf);
-                if (!j)
+                if (!path_extend(&d, nf))
                         return -ENOMEM;
 
-                *ret = path_simplify(j, false);
+                *ret = path_simplify(TAKE_PTR(d), false);
         } else
                 *ret = TAKE_PTR(nf);
 
index f420186010314999cc1ce503046f6deabeaa0c83..f577b790e6afdd1c76f1e37c0f367bdcb61960e8 100644 (file)
@@ -2287,8 +2287,6 @@ static int setup_exec_directory(
                         goto fail;
 
                 if (exec_directory_is_private(context, type)) {
-                        _cleanup_free_ char *private_root = NULL;
-
                         /* So, here's one extra complication when dealing with DynamicUser=1 units. In that
                          * case we want to avoid leaving a directory around fully accessible that is owned by
                          * a dynamic user whose UID is later on reused. To lock this down we use the same
@@ -2314,19 +2312,18 @@ static int setup_exec_directory(
                          * Also, note that we don't do this for EXEC_DIRECTORY_RUNTIME as that's often used
                          * for sharing files or sockets with other services. */
 
-                        private_root = path_join(params->prefix[type], "private");
-                        if (!private_root) {
+                        pp = path_join(params->prefix[type], "private");
+                        if (!pp) {
                                 r = -ENOMEM;
                                 goto fail;
                         }
 
                         /* First set up private root if it doesn't exist yet, with access mode 0700 and owned by root:root */
-                        r = mkdir_safe_label(private_root, 0700, 0, 0, MKDIR_WARN_MODE);
+                        r = mkdir_safe_label(pp, 0700, 0, 0, MKDIR_WARN_MODE);
                         if (r < 0)
                                 goto fail;
 
-                        pp = path_join(private_root, *rt);
-                        if (!pp) {
+                        if (!path_extend(&pp, *rt)) {
                                 r = -ENOMEM;
                                 goto fail;
                         }
index 4fe0903c1d612d326cea0efab9eda36dc626c20a..ff1e0d5f8e464a29da5ef558820b112bb572cca5 100644 (file)
@@ -37,7 +37,6 @@ static int from_environment(const char *envname, const char *fallback, const cha
 
 static int from_home_dir(const char *envname, const char *suffix, char **buffer, const char **ret) {
         _cleanup_free_ char *h = NULL;
-        char *cc = NULL;
         int r;
 
         assert(suffix);
@@ -58,12 +57,11 @@ static int from_home_dir(const char *envname, const char *suffix, char **buffer,
         if (r < 0)
                 return r;
 
-        cc = path_join(h, suffix);
-        if (!cc)
+        if (!path_extend(&h, suffix))
                 return -ENOMEM;
 
-        *buffer = cc;
-        *ret = cc;
+        *buffer = h;
+        *ret = TAKE_PTR(h);
         return 0;
 }
 
@@ -135,18 +133,16 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
                 /* Three syntaxes permitted: relative to $HOME, $HOME itself, and absolute path */
                 if (startswith(p, "$HOME/")) {
                         _cleanup_free_ char *h = NULL;
-                        char *cc;
 
                         r = get_home_dir(&h);
                         if (r < 0)
                                 return r;
 
-                        cc = path_join(h, p+5);
-                        if (!cc)
+                        if (!path_extend(&h, p+5))
                                 return -ENOMEM;
 
-                        *buffer = cc;
-                        *ret = cc;
+                        *buffer = h;
+                        *ret = TAKE_PTR(h);
                         return 0;
                 } else if (streq(p, "$HOME")) {
 
@@ -173,20 +169,17 @@ fallback:
         /* The desktop directory defaults to $HOME/Desktop, the others to $HOME */
         if (streq(field, "XDG_DESKTOP_DIR")) {
                 _cleanup_free_ char *h = NULL;
-                char *cc;
 
                 r = get_home_dir(&h);
                 if (r < 0)
                         return r;
 
-                cc = path_join(h, "Desktop");
-                if (!cc)
+                if (!path_extend(&h, "Desktop"))
                         return -ENOMEM;
 
-                *buffer = cc;
-                *ret = cc;
+                *buffer = h;
+                *ret = TAKE_PTR(h);
         } else {
-
                 r = get_home_dir(buffer);
                 if (r < 0)
                         return r;