参考:http://blog.sina.com.cn/s/blog_a11fcae9010141l8.html
http://www.man7.org/linux/man-pages/man3/ftw.3.html
ftw:意思是文件树遍历。file tree walk.
#includeint nftw(const char * dirpath, int (*fn) (const char *fpath, const struct stat *sb,int typeflag, struct FTW *ftwbuf), int nopenfd, int flags); #includeint ftw(const char * dirpath, int (*fn) (const char *fpath, const struct stat *sb,int typeflag), int nopenfd);
struct FTW {//这个结构体,极有可能在这个头文件中没有,我们可以自己定义一下。 int base;//这个值,指的是目录名的长度,不同的字符编码,可能有区别,需要测试比如中文目录时 int level;//当前扫描的目录的级别,0级是指dirpath,依此累加 };
int typeflag这个参数有以下几种值:
FTW_F fpath is a regular file.表示普通文件
FTW_D fpath is a directory.目录
FTW_DNR fpath is a directory which can't be read 不可读的目录
FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags.
(If FTW_DEPTH was not specified in flags, then directories will always be visited with typeflag set to FTW_D.) All of the files and subdirectories within fpath have been processed.所有的文件和子目录都被处理过了。
FTW_NS The stat(2) call failed on fpath, which is not a symbolic
link. The probable cause for this is that the caller had read permission on the parent directory, so that the filename fpath could be seen, but did not have execute permission, so that the file could not be reached for stat(2).对fpath进行stat时失败,但fpath不是符号连接,导致这种情况的可能原因是,函数调用者有读取父目录的权限,所以这个文件名fpath对调用者是可见的,但不具备执行权限,所以stat时不可以获取信息失败 FTW_SL fpath is a symbolic link, and FTW_PHYS was set in flags.符号连接 FTW_SLN fpath is a symbolic link pointing to a nonexistent file.符号连接,但是指向的源文件已不存在 (This occurs only if FTW_PHYS is not set.)
Because nftw() uses dynamic data structures, the only safe way to exit out of a tree walk is to return a nonzero value from fn(). To allow a signal to terminate the walk without causing a memory leak, have the handler set a global flag that is checked by fn(). Don't use longjmp(3) unless the program is going to terminate.