diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/djbunix.h | 2 | ||||
-rw-r--r-- | src/libenvexec/envdir_clamp.c | 10 | ||||
-rw-r--r-- | src/libenvexec/envdir_noclamp.c | 10 | ||||
-rw-r--r-- | src/libstddjb/hiercopy_tmp.c | 27 | ||||
-rw-r--r-- | src/libstddjb/rm_rf_in_tmp.c | 75 | ||||
-rw-r--r-- | src/libstddjb/sals.c | 44 |
6 files changed, 75 insertions, 93 deletions
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 ; +} |