Linuxシステムプログラミング21ファイルシステム-ファイル属性st_mode

mode_tst_modeを分析します

st_modeは、ファイルタイプ、ファイルアクセス許可、および特別な許可ビットを示すために使用される16ビットビットマップです。

mhr@ubuntu:~/work/linux/wenjianxitong/20$ ls -l
total 24
-rwxrwxr-x 1 mhr mhr       8936 May  4 09:04 a.out
-rw-rw-r--  1 mhr mhr        436 May  4 09:04 big.c
-rw-------   1 mhr mhr 5368709120 May  4 09:04 bigfile
-rw-------   1 mhr mhr 5368709120 May  4 09:07 bigfile.bak
mhr@ubuntu:~/work/linux/wenjianxitong/20$ 

つまり、-rwxrwxr-xの最初の文字列など、これらの情報はすべてビットマップの形式でst_modeに格納されます。mode_tは16ビット整数です。mode_tは、ファイルタイプ+ファイルパーミッションの2つの部分で構成されています

文件类型  user权限    group 同组用户的权限     other 用户的权限
 -        rwx             rwx                  r-x 

ファイルタイプは、次のカテゴリに分類されます
。dcb-lsp7タイプ

1 普通文件          -
2 目录文件          d
3 块特殊文件        b
4 字符特殊文件      c
5 FIFO            p
6 套接字(socket)   s
7 符号链接(symbolic link) l

次のマクロを使用してファイルタイプをテストします。確立されている場合はtrueを返し、確立されていない場合はfalseを返します。
ここに画像の説明を挿入

ここに画像の説明を挿入

ビットマップ

ここに画像の説明を挿入

 struct stat {
           dev_t     st_dev;         /* ID of device containing file */文件所在设备的ID
           ino_t     st_ino;         /* inode number */节点号
           mode_t    st_mode;        /* protection */文件的类型和存取的权限
           nlink_t   st_nlink;       /* number of hard links */链向此文件的连接数(硬连接)
           uid_t     st_uid;         /* user ID of owner */
           gid_t     st_gid;         /* group ID of owner */
           dev_t     st_rdev;        /* device ID (if special file) */设备ID号,针对设备文件
           off_t     st_size;        /* total size, in bytes */文件大小,字节为单位
           blksize_t st_blksize;     /* blocksize for filesystem I/O */系统块的大小
           blkcnt_t  st_blocks;      /* number of 512B blocks allocated */文件所占块数

           /* Since Linux 2.6, the kernel supports nanosecond
              precision for the following timestamp fields.
              For the details before Linux 2.6, see NOTES. */

           struct timespec st_atim;  /* time of last access */最近存取时间
           struct timespec st_mtim;  /* time of last modification */最近修改时间
           struct timespec st_ctim;  /* time of last status change */

       #define st_atime st_atim.tv_sec      /* Backward compatibility */
       #define st_mtime st_mtim.tv_sec
       #define st_ctime st_ctim.tv_sec
       };

おすすめ

転載: blog.csdn.net/LinuxArmbiggod/article/details/105926359