您的位置:首页 > 产品设计 > UI/UE

读书笔记-APUE第三版-(4)文件和目录

2014-04-25 17:38 302 查看
stat函数返回文件信息,本章内容围绕存储在stat结构体中的各种文件属性展开。(注:ls -l会调用stat函数,显示文件相关信息。)

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 块数量*/
};

文件类型(st_mode)

In UNIX, everything is a file。

普通文件:UNIX不区分文本文件和二进制文件。
目录文件:目录也是文件,包含文件夹下文件名称和指向他们的指针。
 块设备文件:比如硬盘/dev/sda。
字符设备文件:比如终端/dev/tty。
 FIFO:命名管道,用于进程间通信,15章介绍。
Socket:用于网络通信,16章介绍。
符号链接:内容为指向文件的文件名。

下表展示了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

文件访问权限(st_mode)

进程用户ID

以下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

常用方法

access/faccessat:根据真实用户ID判断访问权限(不根据有效ID)。
umask:改变创建文件的默认权限掩码。
chown/fchown/fchownat/lchown:改变文件属主。
chmod/fchmod/fchmodat:改变文件访问权限位。值得注意的是S_ISUID/S_ISGID/S_ISVTX。如前所述,前两个用于执行文件时将进程有效ID设置成可执行文件属主ID。S_ISVTX称之为Sticky位,在设置了Sticky位的目录下,如果要删除或者重命名文件,不仅需要写权限,还需要是文件/文件夹属主或者Root用户。(典型的是/tmp目录)

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)

 

文件大小(st_size/st_blksize/st_blocks)

st_blksize:块大小,I/O操作最小单位
st_blocks:文件实际分配块数。文件可能存在空洞,统计st_blksize时会统计在内,但不会分配文件块。

Unix文件系统概念模型

磁盘-分区-文件系统概念图,其中i-node节点包含了stat结构中的大部分信息。



i-node&data blocks细节图:



如图所示,右下角两个目录项指向同一个i-node,然后i-node再指向data block。几点总结:

每个索引节点有一个硬链接数st-nlink,只有硬链接数为0时,对应文件才会被删除(所以删除目录项的函数名是unlink,从图中也可以知道,硬链接不能跨文件系统)。相关函数有:link/linkat/unlink/unlinkat/remove,其中对于文件,remove=unlink;对于目录remove=rmdir。
目录文件中存储目录项,目录项的内容是i-node号和文件名。如果在同一个文件系统内移动文件,先unlink原目录项,然后在目的目录下增加一个目录项,指向i-node就可以。整个硬连接数不变。相关函数有:rename/rename/mkdir/mkdirat/rmdir
非空目录的i-node硬连接数大于等于3,即至少有三个目录项指向这个i-node,包括父目录中的目录项,自身目录的.目录项和子目录的..目录项。
符号链接:因为硬链接不能跨文件系统等限制,所以引入了符号链接。符号链接类似于Windows的快捷方式,使用ln –s命令创建,其数据内容是被链接的文件名。符号链接可能会导致目录结构死循环,当遇到符号链接文件时,一部分系统函数处理的是符号链接文件本身,另一部分处理的则是链接到的文件,要注意区分。

文件时间(st_atime/st_ctime/st_mtime)

相关函数: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 unix 操作系统