struct stat { mode_t st_mode; /* file type & mode (permissions) 文件类型&权限*/ ino_t st_ino; /* i-node number (serial number) i-node号*/ dev_t st_dev; /* device number (file system) 设备号(文件系统)*/ dev_t st_rdev; /* device number for special files 特殊文件设备号*/ nlink_t st_nlink; /* number of links 链接数*/ uid_t st_uid; /* user ID of owner 所属用户ID*/ gid_t st_gid; /* group ID of owner 所属用户组ID*/ off_t st_size; /* size in bytes, for regular files 文件大小*/ struct timespec st_atim; /* time of last access 访问时间*/ struct timespec st_mtim; /* time of last modification 修改时间*/ struct timespec st_ctim; /* time of last file status change 状态更新时间*/ blksize_t st_blksize; /* best I/O block size 块大小*/ blkcnt_t st_blocks; /* number of disk blocks allocated 块数量*/ };
In UNIX, everything is a file。
下表展示了Linux系统各种类型文件比例。
File type |
Count |
Percentage % |
Regular file |
415,803 |
79.77 |
Directory |
62,197 |
11.93 |
Symbolic link |
40,018 |
8.25 |
Character special |
155 |
0.03 |
Block special |
47 |
0.01 |
Socket |
45 |
0.01 |
FIFO |
0 |
0.00 |
以下ID都是进程相关的(不要和文件相关混淆)。
Real user ID Real group ID |
Who we really are |
Effective user ID Effective group ID Supplementary group ID |
用于文件访问权限验证。一般情况下和Real ID相等,除非被执行文件的set-user-ID位被设置,那么进程的有效ID会被设置成被执行文件拥有者ID。比如passwd就是set-user-ID位被设置的可执行文件,属主是root,普通用户才能使用passwd命令更新/etc/passwd文件。 |
Saved set-user-ID Saved set-group-ID |
调用exec函数后,这俩ID从effective user ID和effective group ID拷贝而来,setuid方法中详细介绍 |
文件访问权限分为三组,分别对应User/Group/Other,每组都包括读/写/执行权限。值得注意的是目录的可读权限和可执行权限,可读表示可以读取目录中文件名列表,可执行才能cd进入目录。
st_mode mask |
Meaning |
S_IRUSR |
user-read |
S_IWUSR |
user-write |
S_IXUSR |
user-execute |
S_IRGRP |
group-read |
S_IWGRP |
group-write |
S_IXGRP |
group-execute |
S_IROTH |
other-read |
S_IWOTH |
other-write |
S_IXOTH |
other-execute |
mode Description |
mode Description |
S_ISUID |
set-user-ID on execution |
S_ISGID |
set-group-ID on execution |
S_ISVTX |
saved-text (sticky bit) |
S_IRWXU |
read, write, and execute by user (owner) |
S_IRUSR |
read by user (owner) |
S_IWUSR |
write by user (owner) |
S_IXUSR |
execute by user (owner) |
S_IRWXG |
read, write, and execute by group |
S_IRGRP |
read by group |
S_IWGRP |
write by group |
S_IXGRP |
execute by group |
S_IRWXO |
read, write, and execute by other (world) |
S_IROTH |
read by other (world) |
S_IWOTH |
write by other (world) |
S_IXOTH |
execute by other (world) |
磁盘-分区-文件系统概念图,其中i-node节点包含了stat结构中的大部分信息。
i-node&data blocks细节图:
如图所示,右下角两个目录项指向同一个i-node,然后i-node再指向data block。几点总结:
相关函数:futimens/utimensat/utimes,前两个提供了更高的时间精度,到ns纳秒。对应shell命令为touch。
下面是一个简单的读取目录下文件程序。一个目录中通常包含多个目录项,dirent代表目录项数据结构(directory entry),包含索引节点号d_ino和目录项d_name。
#include "apue.h" #include <dirent.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if (argc != 2) err_quit("usage: ls directory_name"); if ((dp = opendir(argv[1])) == NULL) err_sys("can’t open %s", argv[1]); while ((dirp = readdir(dp)) != NULL) printf("%s\n", dirp->d_name); closedir(dp); exit(0); }
读书笔记-APUE第三版-(4)文件和目录,布布扣,bubuko.com
原文:http://blog.csdn.net/idontwantobe/article/details/24487931