summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-02-09 15:03:45 +0000
committerLaurent Bercot <ska@appnovation.com>2023-02-09 15:03:45 +0000
commita10514072f27ff9f4a6ab308b3bccfd4628ef2aa (patch)
tree66f21986111030114b7fc1a6c15e3a1bce247f49
parent4b6f3cf8a8665a17df45518013a15819791ce2bf (diff)
downloadskalibs-a10514072f27ff9f4a6ab308b3bccfd4628ef2aa.tar.xz
Add sals, several refactors
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--NEWS1
-rw-r--r--doc/libstddjb/djbunix.html11
-rw-r--r--package/deps.mak3
-rw-r--r--src/include/skalibs/djbunix.h2
-rw-r--r--src/libenvexec/envdir_clamp.c10
-rw-r--r--src/libenvexec/envdir_noclamp.c10
-rw-r--r--src/libstddjb/hiercopy_tmp.c27
-rw-r--r--src/libstddjb/rm_rf_in_tmp.c75
-rw-r--r--src/libstddjb/sals.c44
9 files changed, 89 insertions, 94 deletions
diff --git a/NEWS b/NEWS
index 854bceb..82c273b 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ In 2.13.1.0
- New macro: SKALIBS_PATH_MAX, in posixishard.h.
Defined to 4096 on systems that do not define PATH_MAX.
+ - New function: sals().
In 2.13.0.0
diff --git a/doc/libstddjb/djbunix.html b/doc/libstddjb/djbunix.html
index 8232209..a48a57f 100644
--- a/doc/libstddjb/djbunix.html
+++ b/doc/libstddjb/djbunix.html
@@ -670,6 +670,17 @@ Same as above, using the <tt>satmp</tt> global stralloc as
heap-allocated temporary space.
</p>
+<h3> Directory listing </h3>
+
+<p>
+<code> int sals (char const *dir, stralloc *sa, size_t *maxlen) </code> <br />
+Appends the base names of all the files (save <tt>.</tt> and <tt>..</tt>) in
+<em>dir</em> to the stralloc <em>*sa</em>; each name is null-terminated.
+On error, returns -1 and sets errno. On success, returns the number of files
+it found, and writes to <em>*maxlen</em> the size of the largest file name
+it found (0 for an empty directory).
+</p>
+
<h3> Variable length wrappers around Single Unix calls </h3>
<p>
diff --git a/package/deps.mak b/package/deps.mak
index 86415a3..f792c63 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -451,13 +451,14 @@ src/libstddjb/pipe_internal.o src/libstddjb/pipe_internal.lo: src/libstddjb/pipe
src/libstddjb/prog.o src/libstddjb/prog.lo: src/libstddjb/prog.c src/include/skalibs/strerr.h
src/libstddjb/readnclose.o src/libstddjb/readnclose.lo: src/libstddjb/readnclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h
src/libstddjb/rm_rf.o src/libstddjb/rm_rf.lo: src/libstddjb/rm_rf.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h
-src/libstddjb/rm_rf_in_tmp.o src/libstddjb/rm_rf_in_tmp.lo: src/libstddjb/rm_rf_in_tmp.c src/include/skalibs/direntry.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
+src/libstddjb/rm_rf_in_tmp.o src/libstddjb/rm_rf_in_tmp.lo: src/libstddjb/rm_rf_in_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/rm_rf_tmp.o src/libstddjb/rm_rf_tmp.lo: src/libstddjb/rm_rf_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/rmstar.o src/libstddjb/rmstar.lo: src/libstddjb/rmstar.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h
src/libstddjb/sabasename.o src/libstddjb/sabasename.lo: src/libstddjb/sabasename.c src/include/skalibs/bytestr.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/sadirname.o src/libstddjb/sadirname.lo: src/libstddjb/sadirname.c src/include/skalibs/bytestr.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/sagetcwd.o src/libstddjb/sagetcwd.lo: src/libstddjb/sagetcwd.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
src/libstddjb/sagethostname.o src/libstddjb/sagethostname.lo: src/libstddjb/sagethostname.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h
+src/libstddjb/sals.o src/libstddjb/sals.lo: src/libstddjb/sals.c src/include/skalibs/direntry.h 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
diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h
index 449368f..cae59b4 100644
--- a/src/include/skalibs/djbunix.h
+++ b/src/include/skalibs/djbunix.h
@@ -145,10 +145,10 @@ extern int rmstar_tmp (char const *, stralloc *) ;
extern int filecopy_unsafe (char const *, char const *, unsigned int) ;
extern int filecopy_suffix (char const *, char const *, unsigned int, char const *) ;
+extern int sals (char const *, stralloc *, size_t *) ;
extern int hiercopy (char const *, char const *) ;
extern int hiercopy_tmp (char const *, char const *, stralloc *) ;
-
/* Simple spawn functions with 0 or 1 communicating fds. */
extern pid_t child_spawn0 (char const *, char const *const *, char const *const *) ;
diff --git a/src/libenvexec/envdir_clamp.c b/src/libenvexec/envdir_clamp.c
index b63ed97..092ede1 100644
--- a/src/libenvexec/envdir_clamp.c
+++ b/src/libenvexec/envdir_clamp.c
@@ -75,11 +75,7 @@ int envdir_internal_clamp (char const *path, stralloc *modifs, unsigned int opti
return n ;
err:
- {
- int e = errno ;
- dir_close(dir) ;
- if (wasnull) stralloc_free(modifs) ; else modifs->len = modifbase ;
- errno = e ;
- return -1 ;
- }
+ dir_close(dir) ;
+ if (wasnull) stralloc_free(modifs) ; else modifs->len = modifbase ;
+ return -1 ;
}
diff --git a/src/libenvexec/envdir_noclamp.c b/src/libenvexec/envdir_noclamp.c
index 0741c9d..f0e6796 100644
--- a/src/libenvexec/envdir_noclamp.c
+++ b/src/libenvexec/envdir_noclamp.c
@@ -93,11 +93,7 @@ int envdir_internal_noclamp (char const *path, stralloc *modifs, unsigned int op
errfd:
fd_close(fd) ;
err:
- {
- int e = errno ;
- dir_close(dir) ;
- if (wasnull) stralloc_free(modifs) ; else modifs->len = modifbase ;
- errno = e ;
- return -1 ;
- }
+ dir_close(dir) ;
+ if (wasnull) stralloc_free(modifs) ; else modifs->len = modifbase ;
+ return -1 ;
}
diff --git a/src/libstddjb/hiercopy_tmp.c b/src/libstddjb/hiercopy_tmp.c
index 4e69bbe..3e147be 100644
--- a/src/libstddjb/hiercopy_tmp.c
+++ b/src/libstddjb/hiercopy_tmp.c
@@ -14,32 +14,7 @@ static int dircopy (char const *src, char const *dst, mode_t mode, stralloc *tmp
{
size_t tmpbase = tmp->len ;
size_t maxlen = 0 ;
- {
- DIR *dir = opendir(src) ;
- if (!dir) return 0 ;
- for (;;)
- {
- direntry *d ;
- size_t n ;
- errno = 0 ;
- d = readdir(dir) ;
- if (!d) break ;
- if (d->d_name[0] == '.')
- if (((d->d_name[1] == '.') && !d->d_name[2]) || !d->d_name[1])
- continue ;
- n = strlen(d->d_name) ;
- if (n > maxlen) maxlen = n ;
- if (!stralloc_catb(tmp, d->d_name, n+1)) break ;
- }
- if (errno)
- {
- int e = errno ;
- dir_close(dir) ;
- errno = e ;
- goto err ;
- }
- dir_close(dir) ;
- }
+ if (sals(src, tmp, &maxlen) == -1) return 0 ;
if (mkdir(dst, S_IRWXU) < 0)
{
diff --git a/src/libstddjb/rm_rf_in_tmp.c b/src/libstddjb/rm_rf_in_tmp.c
index cc13e95..fa9095c 100644
--- a/src/libstddjb/rm_rf_in_tmp.c
+++ b/src/libstddjb/rm_rf_in_tmp.c
@@ -1,55 +1,33 @@
/* ISC license. */
-#include <unistd.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
+
#include <skalibs/stralloc.h>
-#include <skalibs/direntry.h>
#include <skalibs/djbunix.h>
-static int rmstarindir (DIR *dir, stralloc *tmp, size_t ipos) /* WARNING: closes dir */
+static int rmstar_in_tmp (stralloc *tmp, size_t ipos)
{
- size_t tmpbase = tmp->len ;
- for (;;)
+ size_t tmpbase = tmp->len, tmpstop ;
+ size_t fnbase = strlen(tmp->s + ipos) ;
+ size_t i ;
+ if (sals(tmp->s + ipos, tmp, &i) == -1) return -1 ;
+ tmpstop = tmp->len ;
+ if (!stralloc_readyplus(tmp, fnbase + 2 + i)) goto err ;
+ stralloc_catb(tmp, tmp->s + ipos, fnbase) ;
+ stralloc_catb(tmp, "/", 1) ;
+ fnbase = tmp->len ;
+ for (i = tmpbase ; i < tmpstop ; i += tmp->len - fnbase)
{
- direntry *d ;
- errno = 0 ;
- d = readdir(dir) ;
- if (!d) break ;
- if (d->d_name[0] == '.')
- if (((d->d_name[1] == '.') && (d->d_name[2] == 0)) || (d->d_name[1] == 0))
- continue ;
- if (!stralloc_cats(tmp, d->d_name) || !stralloc_0(tmp)) goto closeanderr ;
- }
- if (errno) goto closeanderr ;
- dir_close(dir) ;
-
- {
- size_t tmpstop = tmp->len ;
- size_t fnbase = strlen(tmp->s + ipos) ;
- size_t i = tmpbase ;
- if (!stralloc_readyplus(tmp, fnbase+1)) goto err ;
- stralloc_catb(tmp, tmp->s + ipos, fnbase) ;
- stralloc_catb(tmp, "/", 1) ;
- fnbase = tmp->len ;
- for (; i < tmpstop ; i += tmp->len - fnbase)
- {
- size_t n = strlen(tmp->s + i) ;
- tmp->len = fnbase ;
- if (!stralloc_readyplus(tmp, n+1)) goto err ;
- stralloc_catb(tmp, tmp->s + i, n+1) ;
- if (rm_rf_in_tmp(tmp, tmpstop) == -1) goto err ;
- }
+ size_t n = strlen(tmp->s + i) ;
+ tmp->len = fnbase ;
+ stralloc_catb(tmp, tmp->s + i, n+1) ;
+ if (rm_rf_in_tmp(tmp, tmpstop) == -1) goto err ;
}
tmp->len = tmpbase ;
return 0 ;
-closeanderr:
- {
- int e = errno ;
- dir_close(dir) ;
- errno = e ;
- }
err:
tmp->len = tmpbase ;
return -1 ;
@@ -57,18 +35,15 @@ err:
int rm_rf_in_tmp (stralloc *tmp, size_t ipos)
{
+ int isadir ;
if (unlink(tmp->s + ipos) == 0) return 0 ;
if (errno == ENOENT) return 0 ;
if ((errno != EISDIR) && (errno != EPERM)) return -1 ;
+ isadir = errno == EPERM ;
+ if (rmstar_in_tmp(tmp, ipos) == -1)
{
- int h = (errno == EPERM) ;
- DIR *dir = opendir(tmp->s + ipos) ;
- if (!dir)
- {
- if (h && (errno == ENOTDIR)) errno = EPERM ;
- return -1 ;
- }
- if (rmstarindir(dir, tmp, ipos) == -1) return -1 ;
+ if (isadir && errno == ENOTDIR) errno = EPERM ;
+ return -1 ;
}
return rmdir(tmp->s + ipos) ;
}
@@ -78,11 +53,7 @@ int rmstar_tmp (char const *dirname, stralloc *tmp)
size_t tmpbase = tmp->len ;
if (!stralloc_cats(tmp, dirname)) return -1 ;
if (!stralloc_0(tmp)) goto err ;
- {
- DIR *dir = opendir(dirname) ;
- if (!dir) goto err ;
- if (rmstarindir(dir, tmp, tmpbase) == -1) goto err ;
- }
+ if (rmstar_in_tmp(tmp, tmpbase) == -1) goto err ;
tmp->len = tmpbase ;
return 0 ;
diff --git a/src/libstddjb/sals.c b/src/libstddjb/sals.c
new file mode 100644
index 0000000..839df6d
--- /dev/null
+++ b/src/libstddjb/sals.c
@@ -0,0 +1,44 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <string.h>
+
+#include <skalibs/direntry.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+int sals (char const *fn, stralloc *sa, size_t *x)
+{
+ int n = 0 ;
+ size_t sabase = sa->len ;
+ size_t maxlen = 0 ;
+ DIR *dir = opendir(fn) ;
+ if (dir)
+ {
+ for (;;)
+ {
+ direntry *d ;
+ size_t len ;
+ errno = 0 ;
+ d = readdir(dir) ;
+ if (!d) break ;
+ if (d->d_name[0] == '.')
+ if (((d->d_name[1] == '.') && !d->d_name[2]) || !d->d_name[1])
+ continue ;
+ len = strlen(d->d_name) ;
+ if (len > maxlen) maxlen = len ;
+ if (!stralloc_catb(sa, d->d_name, len+1)) goto err ;
+ n++ ;
+ }
+ if (errno) goto err ;
+ dir_close(dir) ;
+ }
+ else if (errno != ENOENT) return -1 ;
+ if (x) *x = maxlen ;
+ return n ;
+
+ err:
+ dir_close(dir) ;
+ sa->len = sabase ;
+ return -1 ;
+}