dirent.h 目录检查centos6 centos7区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <dirent.h>

std::vector<string> AVMMutiScanner::AVMMutiScannerImpl::GetModelList(const std::string &dirname)
{
DIR *dp;
struct dirent *dirp;
std::vector<string> dirnames;

if((dp = opendir(dirname.c_str())) == NULL)
{
//cout << "Can't open " << dirname << endl;
SetError( "read file failed." );
return dirnames;
}

while((dirp = readdir(dp)) != NULL){
std::cout << "d_name" <<dirp->d_name << ",d_type:" << int(dirp->d_type) << std::endl;
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0) ///current dir OR parrent dir
{
continue;
// dirp->d_type == 4或者dirp->d_type == DT_DIR 在centos6下可用,在centos7下不可用,centos7下=0(DT_UNKNOWN)
}else if(dirp->d_type == DT_DIR)
{
std::cout << "d_name" <<dirp->d_name << ",d_type:" << int(dirp->d_type) << std::endl;
dirnames.push_back(dirp->d_name);
}
}
closedir(dp);
return dirnames;
}

centos6

1
2
3
4
5
6
7
8
[jinri@17 model]$ ls -lrt

drwxr-xr-x 2 jinri jinri 4096 Jul 14 16:58 model1
[jinri@17 model]$ uname -r
2.6.32-220.7.1.el6.x86_64

[jinri@17 test]$ ./00_basic_test sample/ff2cb5aa81f4eba371e13d6095293b7d
d_name:model1,d_type:4

centos7

1
2
3
4
5
6
7
8
9
10:59 $ ls -lrt
drwxr-xr-x 2 jinri jinri 32 Sep 28 12:01 mo
10:59 $ uname -r
3.10.0-327.36.3.el7.x86_64

10:52 $ ./00_basic_test sample/38810e2b94b625ab8b8fb861871602c9a415ac059c31fd1c093a20b35e26205f5bf4009024a78579c2061ad97e453a08a8a841d93fa36411af870b12c88c42a3296ae266
d_name:.,d_type:4
d_name:..,d_type:4
d_name:mo,d_type:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};

centos6下识别的目录d_type 为4,即DT_DIR

centos7下识别的目录d_type 为0,即DT_UNKNOWN