Add new helper function rmrf

for recursive delete of directories
This commit is contained in:
Mattias Walström
2024-12-19 14:42:17 +01:00
parent 0233fdbf35
commit bea18c2fa9
2 changed files with 26 additions and 0 deletions
+25
View File
@@ -5,6 +5,7 @@
* XXX: removed.
*/
#include <ftw.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
@@ -13,6 +14,7 @@
#include <sys/wait.h>
#include <libite/lite.h>
#include "common.h"
int debug; /* Sets debug level (0:off) */
/* TODO remove once confd / statd lib situation is resolved */
@@ -20,6 +22,29 @@ int debug; /* Sets debug level (0:off) */
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif
static int do_delete(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftw)
{
if (ftw->level == 0)
return 1;
if (remove(fpath) && errno != EBUSY)
WARN("Failed removing %s", fpath);
return 0;
}
int rmrf(const char *path)
{
if (!fisdir(path))
return 0;
nftw(path, do_delete, 64, FTW_DEPTH | FTW_PHYS);
if (remove(path) && errno != ENOENT)
WARN("Failed removing path %s", path);
return 0;
}
char *unquote(char *buf)
{
char q = buf[0];
+1
View File
@@ -7,6 +7,7 @@
int vasprintf(char **strp, const char *fmt, va_list ap);
int rmrf(const char *path);
char *unquote(char *buf);
char *fgetkey(const char *file, const char *key);