From a10514072f27ff9f4a6ab308b3bccfd4628ef2aa Mon Sep 17 00:00:00 2001
From: Laurent Bercot
+ int sals (char const *dir, stralloc *sa, size_t *maxlen)
+Appends the base names of all the files (save . and ..) in
+dir to the stralloc *sa; each name is null-terminated.
+On error, returns -1 and sets errno. On success, returns the number of files
+it found, and writes to *maxlen the size of the largest file name
+it found (0 for an empty directory).
+
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