fs-util: make symlink_idempotent() optionally create relative link

This commit is contained in:
Yu Watanabe 2018-09-23 16:17:03 +09:00 committed by Lennart Poettering
parent bee13f2e49
commit 6c9c51e5e2
5 changed files with 23 additions and 18 deletions

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

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

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

View 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)

View 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)