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/rm_rf_in_tmp.c | |
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/rm_rf_in_tmp.c')
-rw-r--r-- | src/libstddjb/rm_rf_in_tmp.c | 75 |
1 files changed, 23 insertions, 52 deletions
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 ; |