diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-02-09 15:03:45 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-02-09 15:03:45 +0000 |
commit | a10514072f27ff9f4a6ab308b3bccfd4628ef2aa (patch) | |
tree | 66f21986111030114b7fc1a6c15e3a1bce247f49 /src/libstddjb | |
parent | 4b6f3cf8a8665a17df45518013a15819791ce2bf (diff) | |
download | skalibs-a10514072f27ff9f4a6ab308b3bccfd4628ef2aa.tar.xz |
Add sals, several refactors
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-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 |
3 files changed, 68 insertions, 78 deletions
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 ; +} |