stat结构如下 :
struct stat {
dev_t st_dev; /* ID of device containing file */
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) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
其中st_mode是32位整型,仅使用了末16位,规定了文件类型和权限,其格式如下[1]:
1. File type占用15-12位,共有7种,分别为:
宏表示 八进制表示(二进制表示)
S_IFSOCK 0140000(1100 000 000000000) socket (套接口文件) S_IFLNK 0120000(1010 000 000000000) symbolic link (符号链接文件) S_IFREG 0100000(1000 000 000000000) regular file (普通文件) S_IFBLK 0060000(0110 000 000000000) block device (块设备) S_IFDIR 0040000(0100 000 000000000) directory (目录文件) S_IFCHR 0020000(0010 000 000000000) character device (字符设备) S_IFIFO 0010000(0001 000 000000000) fifo (管道文件)
2. UGT占用11-9位,共有3种,分别为:
S_ISUID 0004000(0000 100 000000000) set UID bit S_ISGID 0002000(0000 010 000000000) set GID bit S_ISVTX 0001000(0000 001 000000000) sticky bit
set UID bit:设置使文件在执行阶段具有文件所有者的权限. 典型的文件是 /usr/bin/passwd. 如果一般用户执行该文件, 则在执行过程中, 该文件可以获得root权限, 从而可以更改用户的密码。
set GID bit:该权限只对目录有效. 目录被设置该位后, 任何用户在此目录下创建的文件都具有和该目录所属的组相同的组.
sticky bit: 该位可以理解为防删除位. 一个文件是否可以被某用户删除, 主要取决于该文件所属的组是否对该用户具有写权限. 如果没有写权限, 则这个目录下的所有文件都不能被删除, 同时也不能添加新的文件. 如果希望用户能够添加文件但同时不能删除文件, 则可以对文件使用sticky bit位. 设置该位后, 就算用户对目录具有写权限, 也不能删除该文件.
3.User, Group, Other权限占用8-0位:
S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission
S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission
S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permisson S_IXOTH 00001 others have execute permission
参考文献:
[1]http://blog.csdn.net/astrotycoon/article/details/8679676
[2]http://blog.csdn.net/jiqiren007/article/details/6144171
[3]http://www.cnblogs.com/CSU-PL/archive/2013/06/06/3120757.html