From d661427e13f6f454272fe536322052b21e854d95 Mon Sep 17 00:00:00 2001 From: Rob Swindell <rob@synchro.net> Date: Sun, 23 Jan 2022 14:58:22 -0800 Subject: [PATCH] delfiles() now removes files and directories, recursively --- src/xpdev/dirwrap.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/xpdev/dirwrap.c b/src/xpdev/dirwrap.c index 723ba54d9d..dca4cafc1e 100644 --- a/src/xpdev/dirwrap.c +++ b/src/xpdev/dirwrap.c @@ -732,16 +732,20 @@ int removecase(const char *path) /****************************************************************************/ /* Deletes all files in dir 'path' that match file spec 'spec' */ +/* If spec matches a sub-directory, it is traversed and removed recursively */ /* Optionally, keep the last so many files (sorted by name) */ /* Returns number of files deleted or negative on error */ /****************************************************************************/ long delfiles(const char *inpath, const char *spec, size_t keep) { - char *path; + char* path; + char* fpath; + char* fname; char lastch; size_t i; ulong files = 0; long errors = 0; + long recursed; glob_t g; size_t inpath_len=strlen(inpath); int flags = @@ -769,10 +773,22 @@ long delfiles(const char *inpath, const char *spec, size_t keep) if(keep >= g.gl_pathc) return 0; for(i = 0; i < g.gl_pathc && files < g.gl_pathc - keep; i++) { - if(isdir(g.gl_pathv[i])) + fpath = g.gl_pathv[i]; + if(isdir(fpath)) { + fname = getfname(fpath); + if(strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) + continue; + recursed = delfiles(fpath, spec, keep); + if(recursed >= 0) + files += recursed; + else + errors += recursed; + if(rmdir(fpath) != 0) + errors++; continue; - (void)CHMOD(g.gl_pathv[i],S_IWRITE); /* In case it's been marked RDONLY */ - if(remove(g.gl_pathv[i])==0) + } + (void)CHMOD(fpath, S_IWRITE); /* In case it's been marked RDONLY */ + if(remove(fpath)==0) files++; else errors++; -- GitLab