Change symlinks params to target & linkpath

This is what the symlinkat.2 man page uses.

The old naming with 'to' and 'from', where 'to' is the symlink name
and 'from' is the symlink target is very confusing.

Follow-up for 892838911b.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-09-16 16:58:34 +02:00
committed by Luca Boccassi
parent 3b3af5d020
commit 3b54efe78e
3 changed files with 27 additions and 27 deletions

View File

@@ -423,76 +423,76 @@ int touch(const char *path) {
return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
}
int symlinkat_idempotent(const char *from, int atfd, const char *to, bool make_relative) {
int symlinkat_idempotent(const char *target, int atfd, const char *linkpath, bool make_relative) {
_cleanup_free_ char *relpath = NULL;
int r;
assert(from);
assert(to);
assert(target);
assert(linkpath);
if (make_relative) {
r = path_make_relative_parent(to, from, &relpath);
r = path_make_relative_parent(linkpath, target, &relpath);
if (r < 0)
return r;
from = relpath;
target = relpath;
}
if (symlinkat(from, atfd, to) < 0) {
if (symlinkat(target, atfd, linkpath) < 0) {
_cleanup_free_ char *p = NULL;
if (errno != EEXIST)
return -errno;
r = readlinkat_malloc(atfd, to, &p);
r = readlinkat_malloc(atfd, linkpath, &p);
if (r == -EINVAL) /* Not a symlink? In that case return the original error we encountered: -EEXIST */
return -EEXIST;
if (r < 0) /* Any other error? In that case propagate it as is */
return r;
if (!streq(p, from)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
if (!streq(p, target)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
return -EEXIST;
}
return 0;
}
int symlinkat_atomic_full(const char *from, int atfd, const char *to, SymlinkFlags flags) {
int symlinkat_atomic_full(const char *target, int atfd, const char *linkpath, SymlinkFlags flags) {
int r;
assert(from);
assert(to);
assert(target);
assert(linkpath);
_cleanup_free_ char *relpath = NULL;
if (FLAGS_SET(flags, SYMLINK_MAKE_RELATIVE)) {
r = path_make_relative_parent(to, from, &relpath);
r = path_make_relative_parent(linkpath, target, &relpath);
if (r < 0)
return r;
from = relpath;
target = relpath;
}
_cleanup_free_ char *t = NULL;
r = tempfn_random(to, NULL, &t);
r = tempfn_random(linkpath, NULL, &t);
if (r < 0)
return r;
bool call_label_ops_post = false;
if (FLAGS_SET(flags, SYMLINK_LABEL)) {
r = label_ops_pre(atfd, to, S_IFLNK);
r = label_ops_pre(atfd, linkpath, S_IFLNK);
if (r < 0)
return r;
call_label_ops_post = true;
}
r = RET_NERRNO(symlinkat(from, atfd, t));
r = RET_NERRNO(symlinkat(target, atfd, t));
if (call_label_ops_post)
RET_GATHER(r, label_ops_post(atfd, t, /* created= */ r >= 0));
if (r < 0)
return r;
r = RET_NERRNO(renameat(atfd, t, atfd, to));
r = RET_NERRNO(renameat(atfd, t, atfd, linkpath));
if (r < 0) {
(void) unlinkat(atfd, t, 0);
return r;

View File

@@ -47,9 +47,9 @@ int touch_fd(int fd, usec_t stamp);
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 symlinkat_idempotent(const char *from, int atfd, const char *to, bool make_relative);
static inline int symlink_idempotent(const char *from, const char *to, bool make_relative) {
return symlinkat_idempotent(from, AT_FDCWD, to, make_relative);
int symlinkat_idempotent(const char *target, int atfd, const char *linkpath, bool make_relative);
static inline int symlink_idempotent(const char *target, const char *linkpath, bool make_relative) {
return symlinkat_idempotent(target, AT_FDCWD, linkpath, make_relative);
}
typedef enum SymlinkFlags {
@@ -57,9 +57,9 @@ typedef enum SymlinkFlags {
SYMLINK_LABEL = 1 << 1,
} SymlinkFlags;
int symlinkat_atomic_full(const char *from, int atfd, const char *to, SymlinkFlags flags);
static inline int symlink_atomic(const char *from, const char *to) {
return symlinkat_atomic_full(from, AT_FDCWD, to, 0);
int symlinkat_atomic_full(const char *target, int atfd, const char *linkpath, SymlinkFlags flags);
static inline int symlink_atomic(const char *target, const char *linkpath) {
return symlinkat_atomic_full(target, AT_FDCWD, linkpath, 0);
}
int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);

View File

@@ -26,11 +26,11 @@
#include "tmpfile-util.h"
#include "unit-name.h"
static int symlink_unless_exists(const char *from, const char *to) {
(void) mkdir_parents(to, 0755);
static int symlink_unless_exists(const char *target, const char *linkpath) {
(void) mkdir_parents(linkpath, 0755);
if (symlink(from, to) < 0 && errno != EEXIST)
return log_error_errno(errno, "Failed to create symlink %s: %m", to);
if (symlink(target, linkpath) < 0 && errno != EEXIST)
return log_error_errno(errno, "Failed to create symlink %s: %m", linkpath);
return 0;
}