diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2019-01-08 13:02:18 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2019-01-08 13:02:18 +0000 |
commit | 8218512811aceef63956fb5a41785e4cbc0ffd31 (patch) | |
tree | 9d48de3fb761e3ad501acece1df17f9094cfba11 /src/libunixonacid/atomic_symlink.c | |
parent | 30d0c173d87b92a9ad2f3c1b643463a894abb1d9 (diff) | |
download | skalibs-8218512811aceef63956fb5a41785e4cbc0ffd31.tar.xz |
Remove mkdir_unique, rewrite atomic_symlink with mkltemp
Diffstat (limited to 'src/libunixonacid/atomic_symlink.c')
-rw-r--r-- | src/libunixonacid/atomic_symlink.c | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/src/libunixonacid/atomic_symlink.c b/src/libunixonacid/atomic_symlink.c index d438263..a4c7ecf 100644 --- a/src/libunixonacid/atomic_symlink.c +++ b/src/libunixonacid/atomic_symlink.c @@ -4,8 +4,8 @@ #include <unistd.h> #include <errno.h> #include <stdio.h> -#include <unistd.h> -#include <skalibs/random.h> + +#include <skalibs/posixplz.h> #include <skalibs/unix-transactional.h> int atomic_symlink (char const *target, char const *name, char const *suffix) @@ -18,29 +18,20 @@ int atomic_symlink (char const *target, char const *name, char const *suffix) } { size_t namelen = strlen(name) ; - size_t suffixlen = suffix ? strlen(suffix) : 25 ; - char tmp[namelen + suffixlen + 2] ; + size_t suffixlen = suffix ? strlen(suffix) + 1 : 0 ; + char tmp[namelen + suffixlen + 8] ; memcpy(tmp, name, namelen) ; - tmp[namelen] = ':' ; if (suffix) - memcpy(tmp + namelen + 1, suffix, suffixlen + 1) ; - else - { - 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 ; + tmp[namelen] = ':' ; + memcpy(tmp + namelen + 1, suffix, suffixlen - 1) ; } - if (symlink(target, tmp) < 0) return 0 ; + memcpy(tmp + namelen + suffixlen, ":XXXXXX", 7) ; + tmp[namelen + suffixlen + 7] = 0 ; + if (mkltemp(target, tmp) == -1) return 0 ; if (rename(tmp, name) < 0) { - int e = errno ; - unlink(tmp) ; - errno = e ; + unlink_void(tmp) ; return 0 ; } } |