skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit 11f210ecaa0e085612058b459523fa2003578aa5
parent 74e6ee2aad79665806facb88fca3185cb664582c
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Sun,  3 Dec 2017 20:09:22 +0000

 Add atomic_symlink(), prepare for 2.6.3.0

Diffstat:
MNEWS | 6++++++
Mdoc/index.html | 2+-
Mdoc/license.html | 2+-
Mdoc/upgrade.html | 6++++++
Mpackage/deps.mak | 1+
Mpackage/info | 2+-
Msrc/include/skalibs/unix-transactional.h | 1+
Asrc/libunixonacid/atomic_symlink.c | 41+++++++++++++++++++++++++++++++++++++++++
8 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS @@ -1,5 +1,11 @@ Changelog for skalibs. +In 2.6.3.0 +---------- + + - New function: atomic_symlink(). + + In 2.6.2.0 ---------- diff --git a/doc/index.html b/doc/index.html @@ -60,7 +60,7 @@ with a standard C development environment </li> <h3> Download </h3> <ul> - <li> The current released version of skalibs is <a href="skalibs-2.6.2.0.tar.gz">2.6.2.0</a>. </li> + <li> The current released version of skalibs is <a href="skalibs-2.6.3.0.tar.gz">2.6.3.0</a>. </li> <li> Alternatively, you can checkout a copy of the <a href="//git.skarnet.org/cgi-bin/cgit.cgi/skalibs/">skalibs git repository</a>: diff --git a/doc/license.html b/doc/license.html @@ -74,7 +74,7 @@ color, or different text font. </li> <p> <em>I am aware that the previous restrictions sound completely ridiculous while the official skalibs documentation is incomplete. -As of 2.6.2.0, I'm not going to enforce those restrictions, but if you're +As of 2.6.3.0, I'm not going to enforce those restrictions, but if you're going to provide documentation for skalibs, don't keep it to yourself, please send it to me instead. :-) </em> </p> diff --git a/doc/upgrade.html b/doc/upgrade.html @@ -16,6 +16,12 @@ <a href="//skarnet.org/">skarnet.org</a> </p> +<h2> in 2.6.3.0 </h2> + +<ul> + <li> New function: atomic_symlink(). </li> +</ul> + <h2> in 2.6.2.0 </h2> <ul> diff --git a/package/deps.mak b/package/deps.mak @@ -690,6 +690,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/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 diff --git a/package/info b/package/info @@ -1,4 +1,4 @@ package=skalibs -version=2.6.2.0 +version=2.6.3.0 category=prog package_macro_name=SKALIBS diff --git a/src/include/skalibs/unix-transactional.h b/src/include/skalibs/unix-transactional.h @@ -39,6 +39,7 @@ extern size_t openwritevnclose_at (int, char const *, struct iovec const *, unsi 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 *) ; typedef struct dirdescriptor_s dirdescriptor_t, *dirdescriptor_t_ref ; diff --git a/src/libunixonacid/atomic_symlink.c b/src/libunixonacid/atomic_symlink.c @@ -0,0 +1,41 @@ +/* ISC license. */ + +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <stdio.h> +#include <skalibs/random.h> +#include <skalibs/unix-transactional.h> + +int atomic_symlink (char const *target, char const *name, char const *suffix) +{ + { + int e = errno ; + if (symlink(target, name) == 0) return 1 ; + if (errno != EEXIST) return 0 ; + errno = e ; + } + { + size_t namelen = strlen(name) ; + size_t suffixlen = suffix ? strlen(suffix) : 8 ; + char tmp[namelen + suffixlen + 2] ; + memcpy(tmp, name, namelen) ; + tmp[namelen] = ':' ; + if (suffix) + memcpy(tmp + namelen + 1, suffix, suffixlen + 1) ; + else + { + random_name(tmp + namelen + 1, 8) ; + tmp[namelen + 9] = 0 ; + } + if (symlink(target, tmp) < 0) return 0 ; + if (rename(tmp, name) < 0) + { + int e = errno ; + unlink(tmp) ; + errno = e ; + return 0 ; + } + } + return 1 ; +}