summaryrefslogtreecommitdiff
path: root/src/libunixonacid
diff options
context:
space:
mode:
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/atomic_symlink.c29
-rw-r--r--src/libunixonacid/mkdir_unique.c25
2 files changed, 10 insertions, 44 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 ;
}
}
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 ;
-}