diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-07 15:49:02 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-07 15:49:02 +0000 |
commit | 933e986a9207d2b61c5119e18603b44b924e7226 (patch) | |
tree | c670bc83141d6aebc70e31c11168e005a31e5180 /src/libstddjb | |
parent | 11f210ecaa0e085612058b459523fa2003578aa5 (diff) | |
download | skalibs-933e986a9207d2b61c5119e18603b44b924e7226.tar.xz |
Finally implement sarealpath() over realpath(). Prettier atomic_symlink().
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/absolutepath.c | 12 | ||||
-rw-r--r-- | src/libstddjb/absolutepath_tmp.c | 63 | ||||
-rw-r--r-- | src/libstddjb/sarealpath.c | 26 | ||||
-rw-r--r-- | src/libstddjb/sarealpath_tmp.c | 11 |
4 files changed, 37 insertions, 75 deletions
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) ; +} |