diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libunixonacid/openwritenclose_devino_tmp.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libunixonacid/openwritenclose_devino_tmp.c')
-rw-r--r-- | src/libunixonacid/openwritenclose_devino_tmp.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/libunixonacid/openwritenclose_devino_tmp.c b/src/libunixonacid/openwritenclose_devino_tmp.c new file mode 100644 index 0000000..343da65 --- /dev/null +++ b/src/libunixonacid/openwritenclose_devino_tmp.c @@ -0,0 +1,35 @@ +/* ISC license. */ + +#include <errno.h> +#include <unistd.h> +#include <stdio.h> /* for rename() */ +#include <skalibs/uint64.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> +#include <skalibs/random.h> +#include <skalibs/unix-transactional.h> + +int openwritenclose_devino_tmp (char const *fn, char const *s, unsigned int len, uint64 *dev, uint64 *ino, stralloc *tmp) +{ + uint64 tmpdev, tmpino ; + unsigned int base = tmp->len ; + if (!stralloc_cats(tmp, fn)) return 0 ; + if (random_sauniquename(tmp, 8) < 0) goto fail ; + if (!stralloc_0(tmp)) goto fail ; + if (!openwritenclose_unsafe_devino_sync(tmp->s + base, s, len, &tmpdev, &tmpino)) goto fail ; + if (rename(tmp->s + base, fn) < 0) + { + register int e = errno ; + unlink(tmp->s + base) ; + errno = e ; + goto fail ; + } + tmp->len = base ; + *dev = tmpdev ; + *ino = tmpino ; + return 1 ; + + fail: + tmp->len = base ; + return 0 ; +} |