在xfs文件系统下无法递归监控文件子目录,经过调试发现,xfs文件系统下,readdir无法获取dirent 数据结构的d_type,或者说无论是文件还是目录,获取的d_type类型都为文件。
在大部分linux系统中,获取目录下的文件及其类型代码如下:
DIR* pdir = NULL;
struct dirent *pfile = NULL;
if (!(pdir = opendir(path.c_str()))
{
return false;
}
while ((pfile = readdir(pdir)))
{
if (pfile->d_type==DT_DIR && strcmp(pfile->d_name, ".") && strcmp(pfile->d_name, ".."))
{
}
}
在ext3 nfs ext4文件系统下,上面代码工作正常,当 pfile->d_type == 4 (为目录的时候)进行相应的处理。但在xfs下,无法正确读取d_type。所以采取另一种方法来处理:
bool IsDir(std::string path)
{
struct stat sb;
if (stat(path.c_str(), &sb) == -1) return false;
return S_ISDIR(sb.st_mode);
}
bool IsFile(std::string path)
{
struct stat sb;
if (stat(path.c_str(), &sb) == -1) return false;
return S_ISREG(sb.st_mode);
}
DIR* pdir = NULL;
struct dirent *pfile = NULL;
if (!(pdir = opendir(path.c_str())))
{
return false;
}
while ((pfile = readdir(pdir)))
{
string strFile = path + "/" + pfile->d_name;
if (IsDir(strFile) && strcmp(pfile->d_name, ".") && strcmp(pfile->d_name, ".."))
{
}
}