分析 mode_t st_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-lsp 7種類型
1 普通文件 -
2 目錄文件 d
3 塊特殊文件 b
4 字符特殊文件 c
5 FIFO p
6 套接字(socket) s
7 符號鏈接(symbolic link) l
用如下宏測試文件類型,成立返回真,不成立返回假
位圖
.
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
};