]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: make symlink_idempotent() optionally create relative link
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 23 Sep 2018 07:17:03 +0000 (16:17 +0900)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Sep 2018 15:52:53 +0000 (18:52 +0300)
src/basic/fs-util.c
src/basic/fs-util.h
src/core/execute.c
src/core/socket.c
src/nspawn/nspawn-cgroup.c

index 09fcc32e0ef51c4a102f9b1f2971f6da70d7b9f7..1fa76bda3de92d6ce4d4d3030229a5937b8f0015 100644 (file)
@@ -346,12 +346,27 @@ int touch(const char *path) {
         return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
 }
 
-int symlink_idempotent(const char *from, const char *to) {
+int symlink_idempotent(const char *from, const char *to, bool make_relative) {
+        _cleanup_free_ char *relpath = NULL;
         int r;
 
         assert(from);
         assert(to);
 
+        if (make_relative) {
+                _cleanup_free_ char *parent = NULL;
+
+                parent = dirname_malloc(to);
+                if (!parent)
+                        return -ENOMEM;
+
+                r = path_make_relative(parent, from, &relpath);
+                if (r < 0)
+                        return r;
+
+                from = relpath;
+        }
+
         if (symlink(from, to) < 0) {
                 _cleanup_free_ char *p = NULL;
 
index 4b65625861af310161f8ef89d49fbcfe32b18ee1..bc753d5920699b84558e24aa526e4f2e897a51a9 100644 (file)
@@ -37,7 +37,7 @@ int fd_warn_permissions(const char *path, int fd);
 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
 int touch(const char *path);
 
-int symlink_idempotent(const char *from, const char *to);
+int symlink_idempotent(const char *from, const char *to, bool make_relative);
 
 int symlink_atomic(const char *from, const char *to);
 int mknod_atomic(const char *path, mode_t mode, dev_t dev);
index 35dd3898daeed8d20fc764ab5230dcd0b9257b7b..77e3ce8bfdbb2325df480de7af1d5a8fda62b785 100644 (file)
@@ -2027,7 +2027,7 @@ static int setup_exec_directory(
 
                 if (context->dynamic_user &&
                     !IN_SET(type, EXEC_DIRECTORY_RUNTIME, EXEC_DIRECTORY_CONFIGURATION)) {
-                        _cleanup_free_ char *private_root = NULL, *relative = NULL, *parent = NULL;
+                        _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
@@ -2092,18 +2092,8 @@ static int setup_exec_directory(
                                         goto fail;
                         }
 
-                        parent = dirname_malloc(p);
-                        if (!parent) {
-                                r = -ENOMEM;
-                                goto fail;
-                        }
-
-                        r = path_make_relative(parent, pp, &relative);
-                        if (r < 0)
-                                goto fail;
-
                         /* And link it up from the original place */
-                        r = symlink_idempotent(relative, p);
+                        r = symlink_idempotent(pp, p, true);
                         if (r < 0)
                                 goto fail;
 
index 8775bc8a586d8d1c78e247e11e1c041c0f571a9a..aac80f3548eb304ff9aa06f8c8e345e7413dcaf6 100644 (file)
@@ -1313,7 +1313,7 @@ static int socket_symlink(Socket *s) {
         STRV_FOREACH(i, s->symlinks) {
                 (void) mkdir_parents_label(*i, s->directory_mode);
 
-                r = symlink_idempotent(p, *i);
+                r = symlink_idempotent(p, *i, false);
 
                 if (r == -EEXIST && s->remove_on_stop) {
                         /* If there's already something where we want to create the symlink, and the destructive
@@ -1321,7 +1321,7 @@ static int socket_symlink(Socket *s) {
                          * again. */
 
                         if (unlink(*i) >= 0)
-                                r = symlink_idempotent(p, *i);
+                                r = symlink_idempotent(p, *i, false);
                 }
 
                 if (r < 0)
index 4a3cd29094a8a9695622c54eb1e6c1154680d39e..f7ec077f7b8410dd00d1ee085d1bce17dbfd77b7 100644 (file)
@@ -373,7 +373,7 @@ static int mount_legacy_cgns_supported(
                         if (!target)
                                 return log_oom();
 
-                        r = symlink_idempotent(controller, target);
+                        r = symlink_idempotent(controller, target, false);
                         if (r == -EINVAL)
                                 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
                         if (r < 0)
@@ -482,7 +482,7 @@ static int mount_legacy_cgns_unsupported(
                         if (r < 0)
                                 return r;
 
-                        r = symlink_idempotent(combined, target);
+                        r = symlink_idempotent(combined, target, false);
                         if (r == -EINVAL)
                                 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
                         if (r < 0)