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 | |
parent | 30d0c173d87b92a9ad2f3c1b643463a894abb1d9 (diff) | |
download | skalibs-8218512811aceef63956fb5a41785e4cbc0ffd31.tar.xz |
Remove mkdir_unique, rewrite atomic_symlink with mkltemp
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/unix-transactional.h | 1 | ||||
-rw-r--r-- | src/libunixonacid/atomic_symlink.c | 29 | ||||
-rw-r--r-- | src/libunixonacid/mkdir_unique.c | 25 |
3 files changed, 10 insertions, 45 deletions
diff --git a/src/include/skalibs/unix-transactional.h b/src/include/skalibs/unix-transactional.h index 1037e92..bf4b636 100644 --- a/src/include/skalibs/unix-transactional.h +++ b/src/include/skalibs/unix-transactional.h @@ -36,7 +36,6 @@ extern int opengetlnclose_at (int, char const *, stralloc *, int) ; extern size_t openwritenclose_at (int, char const *, char const *, size_t) ; extern size_t openwritevnclose_at (int, char const *, struct iovec const *, unsigned int) ; -extern int mkdir_unique (stralloc *, char const *, unsigned int) ; extern int atomic_rm_rf (char const *) ; extern int atomic_rm_rf_tmp (char const *, stralloc *) ; extern int atomic_symlink (char const *, char const *, char const *) ; 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 ; } } diff --git a/src/libunixonacid/mkdir_unique.c b/src/libunixonacid/mkdir_unique.c deleted file mode 100644 index 83e240e..0000000 --- a/src/libunixonacid/mkdir_unique.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ISC license. */ - -#include <sys/types.h> -#include <sys/stat.h> -#include <skalibs/stralloc.h> -#include <skalibs/random.h> -#include <skalibs/unix-transactional.h> - -int mkdir_unique (stralloc *sa, char const *fn, unsigned int mode) -{ - size_t base = sa->len ; - int wasnull = !sa->s ; - if (!stralloc_cats(sa, fn)) return 0 ; - if (!stralloc_cats(sa, "/mkdir_unique:")) goto fail ; - if (!random_sauniquename(sa, 64)) goto fail ; - if (!stralloc_0(sa)) goto fail ; - if (mkdir(sa->s + base, mode) < 0) goto fail ; - sa->len-- ; - return 1 ; - - fail: - if (wasnull) stralloc_free(sa) ; - else sa->len = base ; - return 0 ; -} |