summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-12-07 15:49:02 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-12-07 15:49:02 +0000
commit933e986a9207d2b61c5119e18603b44b924e7226 (patch)
treec670bc83141d6aebc70e31c11168e005a31e5180
parent11f210ecaa0e085612058b459523fa2003578aa5 (diff)
downloadskalibs-933e986a9207d2b61c5119e18603b44b924e7226.tar.gz
skalibs-933e986a9207d2b61c5119e18603b44b924e7226.tar.xz
skalibs-933e986a9207d2b61c5119e18603b44b924e7226.zip
Finally implement sarealpath() over realpath(). Prettier atomic_symlink().
-rw-r--r--package/deps.mak4
-rw-r--r--src/libstddjb/absolutepath.c12
-rw-r--r--src/libstddjb/absolutepath_tmp.c63
-rw-r--r--src/libstddjb/sarealpath.c26
-rw-r--r--src/libstddjb/sarealpath_tmp.c11
-rw-r--r--src/libunixonacid/atomic_symlink.c13
6 files changed, 49 insertions, 80 deletions
diff --git a/package/deps.mak b/package/deps.mak
index 63dec9f..85689b5 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -154,8 +154,6 @@ src/libstdcrypto/sha512_final.o src/libstdcrypto/sha512_final.lo: src/libstdcryp
 src/libstdcrypto/sha512_init.o src/libstdcrypto/sha512_init.lo: src/libstdcrypto/sha512_init.c src/include/skalibs/sha512.h
 src/libstdcrypto/sha512_transform.o src/libstdcrypto/sha512_transform.lo: src/libstdcrypto/sha512_transform.c src/libstdcrypto/sha512-internal.h src/include/skalibs/sha512.h src/include/skalibs/uint64.h
 src/libstdcrypto/sha512_update.o src/libstdcrypto/sha512_update.lo: src/libstdcrypto/sha512_update.c src/libstdcrypto/sha512-internal.h src/include/skalibs/sha512.h
-src/libstddjb/absolutepath.o src/libstddjb/absolutepath.lo: src/libstddjb/absolutepath.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
-src/libstddjb/absolutepath_tmp.o src/libstddjb/absolutepath_tmp.lo: src/libstddjb/absolutepath_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
 src/libstddjb/alarm_deadline.o src/libstddjb/alarm_deadline.lo: src/libstddjb/alarm_deadline.c src/libstddjb/alarm-internal.h src/include/skalibs/alarm.h src/include/skalibs/sysdeps.h src/include/skalibs/tai.h
 src/libstddjb/alarm_disable.o src/libstddjb/alarm_disable.lo: src/libstddjb/alarm_disable.c src/libstddjb/alarm-internal.h src/include/skalibs/alarm.h src/include/skalibs/sysdeps.h
 src/libstddjb/alarm_milliseconds.o src/libstddjb/alarm_milliseconds.lo: src/libstddjb/alarm_milliseconds.c src/libstddjb/alarm-internal.h src/include/skalibs/alarm.h src/include/skalibs/sysdeps.h
@@ -445,6 +443,8 @@ src/libstddjb/sagetcwd.o src/libstddjb/sagetcwd.lo: src/libstddjb/sagetcwd.c src
 src/libstddjb/sagethostname.o src/libstddjb/sagethostname.lo: src/libstddjb/sagethostname.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
 src/libstddjb/sanitize_read.o src/libstddjb/sanitize_read.lo: src/libstddjb/sanitize_read.c src/include/skalibs/allreadwrite.h src/include/skalibs/error.h
 src/libstddjb/sareadlink.o src/libstddjb/sareadlink.lo: src/libstddjb/sareadlink.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
+src/libstddjb/sarealpath.o src/libstddjb/sarealpath.lo: src/libstddjb/sarealpath.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
+src/libstddjb/sarealpath_tmp.o src/libstddjb/sarealpath_tmp.lo: src/libstddjb/sarealpath_tmp.c src/include/skalibs/djbunix.h
 src/libstddjb/satmp.o src/libstddjb/satmp.lo: src/libstddjb/satmp.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
 src/libstddjb/sauniquename.o src/libstddjb/sauniquename.lo: src/libstddjb/sauniquename.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/types.h
 src/libstddjb/selfpipe_finish.o src/libstddjb/selfpipe_finish.lo: src/libstddjb/selfpipe_finish.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/nsig.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h
diff --git a/src/libstddjb/absolutepath.c b/src/libstddjb/absolutepath.c
deleted file mode 100644
index 9a7a19f..0000000
--- a/src/libstddjb/absolutepath.c
+++ /dev/null
@@ -1,12 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <skalibs/stralloc.h>
-#include <skalibs/skamisc.h>
-#include <skalibs/djbunix.h>
-
-int sarealpath (stralloc *sa, char const *path)
-{
-  return sarealpath_tmp(sa, path, &satmp) ;
-}
diff --git a/src/libstddjb/absolutepath_tmp.c b/src/libstddjb/absolutepath_tmp.c
deleted file mode 100644
index 100a7e9..0000000
--- a/src/libstddjb/absolutepath_tmp.c
+++ /dev/null
@@ -1,63 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe (because of chdir) */
-
-#include <unistd.h>
-#include <errno.h>
-#include <skalibs/stralloc.h>
-#include <skalibs/djbunix.h>
-
-int sarealpath_tmp (stralloc *sa, char const *path, stralloc *tmp)
-{
-  size_t tmpbase = tmp->len ;
-  size_t base = sa->len ;
-  unsigned int loop = 48 ;
-  int fdhere ;
-  int wasnull = !sa->s ;
-
-  if (!path) return (errno = EINVAL, -1) ;
-  if (!stralloc_cats(sa, path)) return -1 ;
-  fdhere = open_read(".") ;
-  if (fdhere == -1)
-  {
-    if (wasnull) stralloc_free(sa) ; else sa->len = base ;
-    return -1 ;
-  }
-
-  do
-  {
-    tmp->len = tmpbase ;
-    if (!loop--) { errno = ELOOP ; goto err ; }
-    if (!sadirname(tmp, sa->s + base, sa->len - base)
-     || !stralloc_0(tmp)
-     || (chdir(tmp->s + tmpbase) == -1))
-      goto err ;
-    tmp->len = tmpbase ;
-    if (!sabasename(tmp, sa->s + base, sa->len - base)
-     || !stralloc_0(tmp)) goto err ;
-    sa->len = base ;
-  }
-  while (sareadlink(sa, tmp->s + tmpbase) >= 0) ;
-
-  if ((errno != EINVAL)
-   || (sagetcwd(sa) == -1)
-   || ((sa->len > base + 1) && !stralloc_catb(sa, "/", 1))
-   || ((--tmp->len > tmpbase) && (tmp->s[tmpbase] != '/') && !stralloc_catb(sa, tmp->s + tmpbase, tmp->len - tmpbase)))
-    goto err ;
-
-  tmp->len = tmpbase ;
-  fd_chdir(fdhere) ;
-  fd_close(fdhere) ;
-  return 0 ;
-
-err:
-  {
-    int e = errno ;
-    tmp->len = tmpbase ;
-    fd_chdir(fdhere) ;
-    fd_close(fdhere) ;
-    if (wasnull) stralloc_free(sa) ; else sa->len = base ;
-    errno = e ;
-  }
-  return -1 ;
-}
diff --git a/src/libstddjb/sarealpath.c b/src/libstddjb/sarealpath.c
new file mode 100644
index 0000000..0cf1825
--- /dev/null
+++ b/src/libstddjb/sarealpath.c
@@ -0,0 +1,26 @@
+/* ISC license. */
+
+#include <string.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+int sarealpath (stralloc *sa, char const *path)
+{
+  if (sa->s)
+  {
+    if (!stralloc_readyplus(sa, PATH_MAX)) return -1 ;
+    if (!realpath(path, sa->s + sa->len)) return -1 ;
+    sa->len += strlen(sa->s + sa->len) ;
+  }
+  else
+  {
+    char *p = realpath(path, 0) ;
+    if (!p) return -1 ;
+    sa->s = p ;
+    sa->len = strlen(p) ;
+    sa->a = sa->len + 1 ;
+  }
+  return 0 ;
+}
diff --git a/src/libstddjb/sarealpath_tmp.c b/src/libstddjb/sarealpath_tmp.c
new file mode 100644
index 0000000..31aeeb2
--- /dev/null
+++ b/src/libstddjb/sarealpath_tmp.c
@@ -0,0 +1,11 @@
+/* ISC license. */
+
+/* This function is now obsolete. */
+
+#include <skalibs/djbunix.h>
+
+int sarealpath_tmp (stralloc *sa, char const *path, stralloc *tmp)
+{
+  (void)tmp ;
+  return sarealpath(sa, path) ;
+}
diff --git a/src/libunixonacid/atomic_symlink.c b/src/libunixonacid/atomic_symlink.c
index 660e4d3..d438263 100644
--- a/src/libunixonacid/atomic_symlink.c
+++ b/src/libunixonacid/atomic_symlink.c
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <skalibs/random.h>
 #include <skalibs/unix-transactional.h>
 
@@ -17,7 +18,7 @@ int atomic_symlink (char const *target, char const *name, char const *suffix)
   }
   {
     size_t namelen = strlen(name) ;
-    size_t suffixlen = suffix ? strlen(suffix) : 8 ;
+    size_t suffixlen = suffix ? strlen(suffix) : 25 ;
     char tmp[namelen + suffixlen + 2] ;
     memcpy(tmp, name, namelen) ;
     tmp[namelen] = ':' ;
@@ -25,8 +26,14 @@ int atomic_symlink (char const *target, char const *name, char const *suffix)
       memcpy(tmp + namelen + 1, suffix, suffixlen + 1) ;
     else
     {
-      random_name(tmp + namelen + 1, 8) ;
-      tmp[namelen + 9] = 0 ;
+      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 ;
     }
     if (symlink(target, tmp) < 0) return 0 ;
     if (rename(tmp, name) < 0)