Skip to content
Snippets Groups Projects
Commit d661427e authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

delfiles() now removes files and directories, recursively

parent dc5896a0
No related branches found
No related tags found
No related merge requests found
...@@ -732,16 +732,20 @@ int removecase(const char *path) ...@@ -732,16 +732,20 @@ int removecase(const char *path)
/****************************************************************************/ /****************************************************************************/
/* Deletes all files in dir 'path' that match file spec 'spec' */ /* 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) */ /* Optionally, keep the last so many files (sorted by name) */
/* Returns number of files deleted or negative on error */ /* Returns number of files deleted or negative on error */
/****************************************************************************/ /****************************************************************************/
long delfiles(const char *inpath, const char *spec, size_t keep) long delfiles(const char *inpath, const char *spec, size_t keep)
{ {
char *path; char* path;
char* fpath;
char* fname;
char lastch; char lastch;
size_t i; size_t i;
ulong files = 0; ulong files = 0;
long errors = 0; long errors = 0;
long recursed;
glob_t g; glob_t g;
size_t inpath_len=strlen(inpath); size_t inpath_len=strlen(inpath);
int flags = int flags =
...@@ -769,10 +773,22 @@ long delfiles(const char *inpath, const char *spec, size_t keep) ...@@ -769,10 +773,22 @@ long delfiles(const char *inpath, const char *spec, size_t keep)
if(keep >= g.gl_pathc) if(keep >= g.gl_pathc)
return 0; return 0;
for(i = 0; i < g.gl_pathc && files < g.gl_pathc - keep; i++) { 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; 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++; files++;
else else
errors++; errors++;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment