c ++,linux,如何解释`stat64`中文件的模式?

时间:2013-07-03 22:54:24

标签: c++ linux

在那里,我尝试确定文件是文件夹还是文件夹中的文件,

struct dirent **name_list;
int n, i;    
n = scandir(".", &name_list, NULL, alphasort);

for(i=0;i<n;i++){
  struct stat64 stat_list
  stat64(name_list[i]->d_name, &stat_list);
  cout << stat_list.st_mode << endl;
}

cout给出了一些数字,例如“Makefile”的“33188”或“。”的“16877”。那么这些数字是什么意思? actualy“33188”似乎是一个文件,“16877”是一个文件夹,但我想知道st_mode可以给出的所有值,我找不到定义st_mode的位置。

2 个答案:

答案 0 :(得分:2)

man的{​​{1}}页面包含每个标志的含义表。

stat

您问题中模式的数字表示以十进制形式输出,但是如果将它们转换为八进制,则位域更有意义。

该文件的

S_IFMT 0170000 bit mask for the file type bit fields S_IFSOCK 0140000 socket S_IFLNK 0120000 symbolic link S_IFREG 0100000 regular file S_IFBLK 0060000 block device S_IFDIR 0040000 directory S_IFCHR 0020000 character device S_IFIFO 0010000 FIFO S_ISUID 0004000 set UID bit S_ISGID 0002000 set-group-ID bit (see below) S_ISVTX 0001000 sticky bit (see below) S_IRWXU 00700 mask for file owner permissions S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission S_IRWXG 00070 mask for group permissions S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 mask for permissions for others (not in group) S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission 转换为33188,这意味着它是一个包含所有者读/写和组/其他只读的常规文件。该目录的0o100644转换为16877,这意味着它是一个拥有所有者权限的目录,并且可以读取/执行该组和其他目录。

答案 1 :(得分:1)

我会说:RTM:)

switch (sb.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            break;
    case S_IFCHR:  printf("character device\n");        break;
    case S_IFDIR:  printf("directory\n");               break;
    case S_IFIFO:  printf("FIFO/pipe\n");               break;
    case S_IFLNK:  printf("symlink\n");                 break;
    case S_IFREG:  printf("regular file\n");            break;
    case S_IFSOCK: printf("socket\n");                  break;
    default:       printf("unknown?\n");                break;
}