[systemd-devel] [PATCH 1/2] util: add symlink_atomic()

Shawn Landen shawnlandden at gmail.com
Thu Aug 9 16:16:39 PDT 2012


---
 src/shared/util.c |   19 +++++++++++++++++--
 src/shared/util.h |    1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index 946b7d5..876eb62 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4851,7 +4851,7 @@ finish:
         return r;
 }
 
-int symlink_or_copy_atomic(const char *from, const char *to) {
+static int symlink_atomic_raw(const char *from, const char *to, bool allow_copy) {
         char *t, *x;
         const char *fn;
         size_t k;
@@ -4880,7 +4880,14 @@ int symlink_or_copy_atomic(const char *from, const char *to) {
 
         *x = 0;
 
-        r = symlink_or_copy(from, t);
+        if (allow_copy)
+                r = symlink_or_copy(from, t);
+        else {
+                r = symlink(from, t);
+                if (r < 0)
+                        r = -errno;
+        }
+
         if (r < 0) {
                 unlink(t);
                 free(t);
@@ -4898,6 +4905,14 @@ int symlink_or_copy_atomic(const char *from, const char *to) {
         return r;
 }
 
+int symlink_or_copy_atomic(const char *from, const char *to) {
+        return symlink_atomic_raw(from, to, true);
+}
+
+int symlink_atomic(const char *from, const char *to) {
+        return symlink_atomic_raw(from, to, false);
+}
+
 bool display_is_local(const char *display) {
         assert(display);
 
diff --git a/src/shared/util.h b/src/shared/util.h
index d5a48eb..24fa240 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -436,6 +436,7 @@ int vt_disallocate(const char *name);
 int copy_file(const char *from, const char *to);
 int symlink_or_copy(const char *from, const char *to);
 int symlink_or_copy_atomic(const char *from, const char *to);
+int symlink_atomic(const char *from, const char *to);
 
 int fchmod_umask(int fd, mode_t mode);
 
-- 
1.7.10.4



More information about the systemd-devel mailing list