diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-07 15:49:02 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-07 15:49:02 +0000 |
commit | 933e986a9207d2b61c5119e18603b44b924e7226 (patch) | |
tree | c670bc83141d6aebc70e31c11168e005a31e5180 /src/libunixonacid/atomic_symlink.c | |
parent | 11f210ecaa0e085612058b459523fa2003578aa5 (diff) | |
download | skalibs-933e986a9207d2b61c5119e18603b44b924e7226.tar.xz |
Finally implement sarealpath() over realpath(). Prettier atomic_symlink().
Diffstat (limited to 'src/libunixonacid/atomic_symlink.c')
-rw-r--r-- | src/libunixonacid/atomic_symlink.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/libunixonacid/atomic_symlink.c b/src/libunixonacid/atomic_symlink.c index 660e4d3..d438263 100644 --- a/src/libunixonacid/atomic_symlink.c +++ b/src/libunixonacid/atomic_symlink.c @@ -4,6 +4,7 @@ #include <unistd.h> #include <errno.h> #include <stdio.h> +#include <unistd.h> #include <skalibs/random.h> #include <skalibs/unix-transactional.h> @@ -17,7 +18,7 @@ int atomic_symlink (char const *target, char const *name, char const *suffix) } { size_t namelen = strlen(name) ; - size_t suffixlen = suffix ? strlen(suffix) : 8 ; + size_t suffixlen = suffix ? strlen(suffix) : 25 ; char tmp[namelen + suffixlen + 2] ; memcpy(tmp, name, namelen) ; tmp[namelen] = ':' ; @@ -25,8 +26,14 @@ int atomic_symlink (char const *target, char const *name, char const *suffix) memcpy(tmp + namelen + 1, suffix, suffixlen + 1) ; else { - random_name(tmp + namelen + 1, 8) ; - tmp[namelen + 9] = 0 ; + memcpy(tmp + namelen + 1, "atomic_symlink:", 15) ; + random_name(tmp + namelen + 16, 8) ; + tmp[namelen + 24] = 0 ; + } + { + int e = errno ; + if (unlink(tmp) < 0 && errno != ENOENT) return 0 ; + errno = e ; } if (symlink(target, tmp) < 0) return 0 ; if (rename(tmp, name) < 0) |