实现Linux下清空多级目录(C++)

#include
#include
#include
#include

#define _XOPEN_SOURCE 500

int deal( const char *file ,const struct stat* sb, int flags, struct FTW *f);

int main(){
using namespace std;
int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
// FTW_DEPTH: 深度优先遍例
// FTW_MOUNT: 防止遍历过程中穿越文件系统
// FTW_PHYS: 不跟随符号链接的路径遍例
int rc;
rc = nftw( "./a",rmdirHelper,100,flags );
if( rc == 0 )
cout << "successed" << endl;
else
cout << "failed" << endl;
return 0;
}

int rmdirHelper( const char *file, const struct stat* sb, int flags, struct FTW *f ){
using namespace std;
if( f->level < 0 ) return 0;
if( f->level > INT_MAX ) return 0;
remove( file );
if( flags == FTW_DNR ){// add code
cout << "find: " << file << "permission denied "< return -1;
}else if( flags == FTW_NS ){
cout << "find: stat() failed " << endl;
return -1;
}else{
cout << "delete " << file << endl;
return 0;
}
}

相关推荐