This commit is contained in:
anon 2025-01-26 14:35:47 +01:00
commit 39245a2570
9 changed files with 54 additions and 0 deletions

2
.gitignore vendored Normal file

@ -0,0 +1,2 @@
.gdb_history
*.out

31
filesystem_remove.c Normal file

@ -0,0 +1,31 @@
#include "filesystem_remove.h"
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <ftw.h>
static
int remove_wrapper(
const char * fpath,
const struct stat * sb,
int typeflag,
struct FTW * ftwbuf
) {
if (typeflag == FTW_DNR
|| typeflag == FTW_NS) {
return 1;
}
if (remove(fpath)) { return 2; }
return 0;
}
bool filesystem_remove(const char * const p) {
#define NOPENFD 256
const int result = nftw(p, remove_wrapper, NOPENFD, FTW_DEPTH | FTW_PHYS);
if (result) { return false; }
return true;
#undef NOPENFD
}

10
filesystem_remove.h Normal file

@ -0,0 +1,10 @@
#ifndef FILESYSTEM_REMOVE
#define FILESYSTEM_REMOVE
#include <stdbool.h>
/* C replication of `std::filesystem::remove` of C++17
* Unlike standard C remove(3), it can remove recursively.
*/
bool filesystem_remove(const char * const p);
#endif

1
linked_file.txt Normal file

@ -0,0 +1 @@
testdir/ soft links to this file

6
test.c Normal file

@ -0,0 +1,6 @@
// @BAKE gcc -o $*.out $@ filesystem_remove.c -ggdb
#include "filesystem_remove.h"
signed main(void) {
return !filesystem_remove("testdir/");
}

1
testdir/file.txt Normal file

@ -0,0 +1 @@
this is a text file with contents

@ -0,0 +1 @@
this is a comment

1
testdir/subdir/link Symbolic link

@ -0,0 +1 @@
../../linked_file.txt

1
testdir/subdir/note.txt Normal file

@ -0,0 +1 @@
this is a note