]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: Introduce symlinkat_idempotent
authorAdrian Vovk <adrianvovk@gmail.com>
Fri, 18 Oct 2024 21:57:42 +0000 (17:57 -0400)
committerAdrian Vovk <adrianvovk@gmail.com>
Fri, 18 Oct 2024 21:58:45 +0000 (17:58 -0400)
src/basic/fs-util.c
src/basic/fs-util.h

index 9292e567c87edcc77768812ad01f28a3f024f30b..97f36df8e7dfcd7b0ab5a35f7e5c9adbb40d3f54 100644 (file)
@@ -416,7 +416,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi
         return RET_GATHER(ret, r);
 }
 
-int symlink_idempotent(const char *from, const char *to, bool make_relative) {
+int symlinkat_idempotent(const char *from, int atfd, const char *to, bool make_relative) {
         _cleanup_free_ char *relpath = NULL;
         int r;
 
@@ -431,13 +431,13 @@ int symlink_idempotent(const char *from, const char *to, bool make_relative) {
                 from = relpath;
         }
 
-        if (symlink(from, to) < 0) {
+        if (symlinkat(from, atfd, to) < 0) {
                 _cleanup_free_ char *p = NULL;
 
                 if (errno != EEXIST)
                         return -errno;
 
-                r = readlink_malloc(to, &p);
+                r = readlinkat_malloc(atfd, to, &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 */
index 702b6010e2ac243b21e37d882278fdcce1a07a0b..06c95a3b080c4872e1d9ce25c82c48a678bc82c6 100644 (file)
@@ -60,7 +60,10 @@ static inline 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, bool make_relative);
+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_atomic_full(const char *from, int atfd, const char *to, bool make_relative);
 static inline int symlink_atomic(const char *from, const char *to) {