From 8218512811aceef63956fb5a41785e4cbc0ffd31 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 8 Jan 2019 13:02:18 +0000 Subject: Remove mkdir_unique, rewrite atomic_symlink with mkltemp --- NEWS | 1 + package/deps.mak | 3 +-- src/include/skalibs/unix-transactional.h | 1 - src/libunixonacid/atomic_symlink.c | 29 ++++++++++------------------- src/libunixonacid/mkdir_unique.c | 25 ------------------------- 5 files changed, 12 insertions(+), 47 deletions(-) delete mode 100644 src/libunixonacid/mkdir_unique.c diff --git a/NEWS b/NEWS index bdbdc12..3933097 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ In 2.8.0.0 GNU C, not C99.) The AVLTREEN_DECLARE_AND_INIT() macro should be used instead. - New functions: env_dump(), mk?temp(), autosurf(), autosurf_name() + - mkdir_unique() removed. (Duplicate functionality with mkdtemp.) In 2.7.0.0 diff --git a/package/deps.mak b/package/deps.mak index 44d15cc..4a3db74 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -725,7 +725,7 @@ src/libstddjb/xpathexec_run.o src/libstddjb/xpathexec_run.lo: src/libstddjb/xpat src/libunixonacid/access_at.o src/libunixonacid/access_at.lo: src/libunixonacid/access_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h src/libunixonacid/atomic_rm_rf.o src/libunixonacid/atomic_rm_rf.lo: src/libunixonacid/atomic_rm_rf.c src/include/skalibs/skamisc.h src/include/skalibs/unix-transactional.h src/libunixonacid/atomic_rm_rf_tmp.o src/libunixonacid/atomic_rm_rf_tmp.lo: src/libunixonacid/atomic_rm_rf_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/random.h src/include/skalibs/stralloc.h src/include/skalibs/unix-transactional.h -src/libunixonacid/atomic_symlink.o src/libunixonacid/atomic_symlink.lo: src/libunixonacid/atomic_symlink.c src/include/skalibs/random.h src/include/skalibs/unix-transactional.h +src/libunixonacid/atomic_symlink.o src/libunixonacid/atomic_symlink.lo: src/libunixonacid/atomic_symlink.c src/include/skalibs/posixplz.h src/include/skalibs/unix-transactional.h src/libunixonacid/bufalloc_timed_flush.o src/libunixonacid/bufalloc_timed_flush.lo: src/libunixonacid/bufalloc_timed_flush.c src/include/skalibs/bufalloc.h src/include/skalibs/functypes.h src/include/skalibs/unix-timed.h src/libunixonacid/buffer_timed_fill.o src/libunixonacid/buffer_timed_fill.lo: src/libunixonacid/buffer_timed_fill.c src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/unix-timed.h src/libunixonacid/buffer_timed_flush.o src/libunixonacid/buffer_timed_flush.lo: src/libunixonacid/buffer_timed_flush.c src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/unix-timed.h @@ -743,7 +743,6 @@ src/libunixonacid/kolbak_call.o src/libunixonacid/kolbak_call.lo: src/libunixona src/libunixonacid/kolbak_enqueue.o src/libunixonacid/kolbak_enqueue.lo: src/libunixonacid/kolbak_enqueue.c src/include/skalibs/kolbak.h src/include/skalibs/unixmessage.h src/libunixonacid/kolbak_queue_init.o src/libunixonacid/kolbak_queue_init.lo: src/libunixonacid/kolbak_queue_init.c src/include/skalibs/kolbak.h src/libunixonacid/kolbak_unenqueue.o src/libunixonacid/kolbak_unenqueue.lo: src/libunixonacid/kolbak_unenqueue.c src/include/skalibs/kolbak.h -src/libunixonacid/mkdir_unique.o src/libunixonacid/mkdir_unique.lo: src/libunixonacid/mkdir_unique.c src/include/skalibs/random.h src/include/skalibs/stralloc.h src/include/skalibs/unix-transactional.h src/libunixonacid/netstring_timed_get.o src/libunixonacid/netstring_timed_get.lo: src/libunixonacid/netstring_timed_get.c src/include/skalibs/buffer.h src/include/skalibs/iopause.h src/include/skalibs/netstring.h src/include/skalibs/unix-timed.h src/libunixonacid/open2_at.o src/libunixonacid/open2_at.lo: src/libunixonacid/open2_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h src/libunixonacid/open3_at.o src/libunixonacid/open3_at.lo: src/libunixonacid/open3_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h 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 #include #include -#include -#include + +#include #include 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 -#include -#include -#include -#include - -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 ; -} -- cgit v1.2.3