0

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

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”。那么这些数字是什么意思呢?实际上“33188”似乎是一个文件,“16877”是一个文件夹,但我想知道所有的值st_mode都可以给出,我找不到st_mode定义的地方看看。

4

2 回答 2

3

man页面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对于目录转换为0o40755这意味着它是一个具有所有所有者权限和组和其他读取/执行的目录。

于 2013-07-03T23:00:00.007 回答
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;
}
于 2013-07-03T22:59:29.020 回答