summaryrefslogtreecommitdiffstats
path: root/src/contrib/files.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/files.c')
-rw-r--r--src/contrib/files.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/contrib/files.c b/src/contrib/files.c
index 5b38469..f3a4b78 100644
--- a/src/contrib/files.c
+++ b/src/contrib/files.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2023 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/* Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -92,16 +92,29 @@ static int remove_file(const char *path, const struct stat *stat, int type, stru
{
(void)stat;
(void)ftw;
- if (type == FTW_DP) {
+
+ switch (type) {
+ case FTW_D:
+ case FTW_DNR:
+ case FTW_DP:
return rmdir(path);
- } else {
+ default:
return unlink(path);
}
}
-bool remove_path(const char *path)
+static int remove_in_dir(const char *path, const struct stat *stat, int type, struct FTW *ftw)
+{
+ return (ftw->level > 0) ? remove_file(path, stat, type, ftw) : 0;
+}
+
+int remove_path(const char *path, bool keep_apex)
{
- return (0 == nftw(path, remove_file, 1, FTW_DEPTH | FTW_PHYS));
+ if (0 != nftw(path, keep_apex ? remove_in_dir : remove_file,
+ 1, FTW_DEPTH | FTW_PHYS)) {
+ return knot_map_errno();
+ }
+ return KNOT_EOK;
}
int make_dir(const char *path, mode_t mode, bool ignore_existing)