简体   繁体   English

C ++,Linux,如何解释“ stat64”中文件的模式?

[英]c++, linux, how to interpret the mode of a file in `stat64`?

there, I try to determine if a file is a folder or a file inside a folder, 在那里,我尝试确定文件是文件夹还是文件夹中的文件,

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;
}

the cout gives some numbers, like "33188" for "Makefile" or "16877" for ".". cout会给出一些数字,例如“ Makefile”的“ 33188”或“。”的“ 16877”。 So what mean these numbers ? 那么这些数字是什么意思呢? actualy "33188" seems to mean a file, and "16877" a folder, but I would like to know all values st_mode can gives, I failed to locate where st_mode is defined to take a look. 实际上,“ 33188”似乎是一个文件,“ 16877”是一个文件夹,但是我想知道st_mode可以提供的所有值,但我无法找到定义st_mode位置来查看。

The man page for stat has a table of what each flag means. statman页上有一张表格,列出每个标志的含义。

       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

The numerical representations of the modes in your question are output as decimal, however if you convert them to octal, the bitfields make a bit more sense. 问题中模式的数字表示以十进制输出,但是,如果将它们转换为八进制,则位域会更有意义。

33188 for the file converts to 0o100644 which means that it's a regular file with owner read/write and group/other read only. 文件的33188转换为0o100644 ,这意味着它是具有所有者读/写和组/其他只读权限的常规文件。 16877 for the directory converts to 0o40755 which means it's a directory with all owner permissions and read/execute for group and other. 16877的目录转换为0o40755这意味着它的所有所有者权限的目录和读/执行组等。

I would say: RTM :) 我会说: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM