1 #include <string>
2 #include <cstring>
3 //#define _XOPEN_SOURCE 500
4 #include <ftw.h>
5 #include <unistd.h>
6
removeFileOrDir(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)7 int removeFileOrDir(const char *fpath,const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
8
9 {//to avoid unused variable warning
10 (void) sb;
11 (void) ftwbuf;
12 };
13
14 if (typeflag==FTW_F) {//file
15 remove(fpath);
16 } else if (typeflag==FTW_DP) {//dir
17 rmdir(fpath);
18 } else {//something went wrong, stop the removal
19 return -1;
20 };
21 return 0;
22 };
23
24
sysRemoveDir(std::string dirName)25 void sysRemoveDir(std::string dirName) {//remove directory and all its contents
26 int nftwFlag=FTW_DEPTH;
27 nftw(dirName.c_str(), removeFileOrDir, 100, nftwFlag);
28 };
29