码迷,mamicode.com
首页 > 其他好文 > 详细

Notes for Apue —— chapter 4 Files and Directories(文件和目录)

时间:2015-05-04 23:38:09      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

4.1 Introduction

4.2 stat, fstat, fstatat, and lstat Functions

技术分享

The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.

技术分享

4.3 文件类型

1. 普通文件

2. 目录文件

Directory file. A file that contains the names of other files and pointers to
information on these files. Any process that has read permission for a directory
file can read the contents of the directory, but only the kernel can write directly
to a directory file. Processes must use the functions described in this chapter to
make changes to a directory.

目录文件包含了其他文件的名字以及指向与这些文件有关的信息的指针。对一个目录文件具有读权限的任一进程都可以都该目录的内容,但只有内核可以直接写目录文件。进程必须使用本章介绍的函数才能更改目录。

3. 块特殊文件

Block special file. A type of file providing buffered I/O access in fixed-size units
to devices such as disk drives.

可以带缓冲的访问块特殊文件上的固定大小的单元,例如磁盘驱动就是一种属于块特殊文件的设备。

4. 字符特殊文件

Character special file. A type of file providing unbuffered I/O access in
variable-sized units to devices. All devices on a system are either block special
files or character special files.

可以不带缓冲的访问字符特殊文件上的固定大小的单元。一个系统上的所有设备不是块特殊文件就是字符特殊文件。

5. FIFO

这种类型的文件用于进程间通信,有时也称为命名管道(named pipe)。

6. socket

7. 符号链接

4.4 Set-User-ID and Set-Group-ID

与一个进程相关联的 ID 有6个或者更多。

技术分享

  1. real user ID 以及 real group ID 表示我们究竟是谁。
  2. effective user ID、effective group ID 以及 supplementary group IDs 决定了我们的文件访问权限。
  3. 当一个程序被执行时,saved set-user-ID 以及 saved set-group-ID 将分别包含一份 effective user ID 以及 effective group ID 的拷贝。

通常情况下,effective user ID 等于 real user ID,effective group ID 等于 real group ID。

每一个文件有一个 owner 和一个 group owner。stat structure(struct stat) 中的 st_uid 成员指定文件的 owner 是谁,同样,stat structure 中的 st_gid 成员指定文件的 group owner 是谁。

当我们 execute(执行)一个程序文件(program file)时,进程中的 effective user ID 通常是 real user ID,同样,进程中的 effective group ID 通常是 real group ID。但是,我们可以通过设置 struct stat 中 的 st_mode 成员中的一个相关的 flag 来改变这一状况。当进程执行某一个文件时,可以将进程的 effective user ID 设置成该文件的 owner(即该文件的 struct stat 中的 st_uid 成员的值),同样,可以将进程的 effective group ID 设置成该文件的 group owner (即该文件的 struct stat 中的 st_gid 成员的值)。我们称以上两个在 st_mode 中的 bits(flag) 为 set-user-ID bit 以及 set-group-ID bit。  

举个例子,如果某个文件的 owner 是 superuser,并且这个文件的 set-user-ID bit 已经 set 了,那么当进程 run 这个文件时(这个文件本身是一个program,run 起来就是一个进程),这个进程将拥有 superuser 的 privileges 。无论执行这个文件的进程的 real user ID 是谁,上述情况一定会发生。再看一个例子,UNIX 系统程序允许任何用户改变自己的 password ,实际上,passwd(1) 就是一个 set-user-ID 程序,这是我们需要的,因为 /etc/passwd 以及 /etc/shadow 只有 superuser 才能 write (那么当我们的进程运行passwd(1)程序时,进程的 effictive user ID 就会被设置成 superuser,如此一来,该进程就拥有 superuser 的 privileges,就可对 /etc/passwd 或者 /etc/shadow 进行改写)。 一个进程执行 set-user-ID 程序通常会拥有其他 user 的 permissions ,因此,编写这样的代码需要特别谨慎。

再回到 stat 函数,set-user-ID bit 和 set-group-ID bit 包含于文件的 struct stat 结构的 st-mode 成员中。这两个 bit 是否被设置可以通过常量 S_ISUID 和 S_ISGID 来测试。

4.5 文件访问权限(File Access permission)

文件的 struct stat 结构中的 st_mode 成员也包含了文件的 access permission bits (访问权限位)。当提到文件,指的是文件类型(如普通文件、目录文件等等)。所有文件类型都有 permission (权限)。许多人想当然的认为只有普通文件(regular files)才有访问权限。

对于每一个文件有9种 permission ,被分成如下3类:

技术分享

在前3行的术语 user,指的是文件 的 owner。chmod(1) 命令(用于修改这9种 permission bit)允许我们用 u 表示 user(owner),用 g 表示 group,用 o 表示 other。

上图中的3类访问权限(read, write, and execute)被不同的函数通过各种方式来使用。此处将这些使用方式汇总在下面。

1. 第一个规则是,每当我们想通过名字来打开任何类型的文件时,那么对于包含这个文件名字的目录(包括当前目录),必须有 execute permission (执行权限)。这就是为什么一个目录的 execute permission bit 通常被叫做 search bit。

For example, to open the file /usr/include/stdio.h, we need execute
permission in the directory /, execute permission in the directory /usr, and execute
permission in the directory /usr/include. We then need appropriate permission
for the file itself, depending on how we’re trying to open it: read-only, read–write,
and so on.
If the current directory is /usr/include, then we need execute permission in the
current directory to open the file stdio.h. This is an example of the current
directory being implied, not specifically mentioned. It is identical to our opening the
file ./stdio.h.

需要注意的是,对于一个目录文件来说,读权限与执行权限是两回事。读权限允许我们读目录,获得在该目录中所有文件名的列表。当一个目录是我们要访问的路径名的一个组成部分时,对该目录的执行权限使我们可以通过该目录(也就是搜索该目录,寻找一个特定的文件名)。

对于文件来说,从字面上就可以理解,但对于目录来说,执行权限代表什么?它与读、写权限有什么不同呢?

先做一些小实验,然后再总结。

### 实验数据准备 ###
$ mkdir test                         # 创建目录test
$ echo "hello" > test/f1          # 在目录test下创建文件f1
##################

1. 读权限
$ chmod 444 test                   # 修改目录为读权限(包括用户、组和其它)
$ ls test                               # 查看目录test的文件列表
f1                                        # 结果显示
$ cat test/f1                         # 再试下查看一下目录test中的文件f1
cat: test/f1: Permission denied

由此可见,目录的读权限仅允许我们读目录,获得在该目录中所有文件名的列表,但无法查看目录中文件的内容。

2. 执行权限
$ chmod 111 test                  # 修改目录为执行权限(包括用户、组和其它)
$ ls test                              # 查看目录test的文件列表
ls: test/: Permission denied
$ cat test/f1                        # 查看目录test中的文件f1
hello

由此可见,目录的执行权限不允许我们读取目录的文件列表,但可以查看目录中文件的内容。当一个目录是我们要访问文件的路径名的一个组成部分时,对该目录的执行权限使我们可通过该目录。

3. 写权限
$ chmod 222 test                        # 修改目录为写权限(包括用户、组和其它)
$ echo "bye" > test/f1                 # 修改目录test中的文件f1的内容
-bash: test/f1: Permission denied
$ chmod 333 test                        # 修改目录为执行、写权限(包括用户、组和其它)
$ echo "bye" > test/f1
$ cat test/f1
bye

由此可见,要修改目录中的文件内容,不仅仅需要目录的写权限,还需要目录的执行权限(这个很显然)。

2. The read permission for a file determines whether we can open an existing file for reading: the O_RDONLY and O_RDWR flags for the open function.

3. The write permission for a file determines whether we can open an existing file for writing: the O_WRONLY and O_RDWR flags for the open function.

4. We must have write permission for a file to specify the O_TRUNC flag in the open function.

5. We cannot create a new file in a directory unless we have write permission and execute permission in the directory.

6. To delete an existing file, we need write permission and execute permission in the directory containing the file. We do not need read permission or write permission for the file itself.

7. Execute permission for a file must be on if we want to execute the file using any of the seven exec functions (Section 8.10). The file also has to be a regular file.

进程每次open、create 或者 delete 某一个文件时,内核都会进行文件访问权限测试,这种测试将涉及到该文件的 owners(st_uid and st_gid),进程的 effective IDs(effective user ID and effective group ID) 以及进程 supplementary group IDs(如果支持的话)。两种 owner IDs 是文件的属性,而两种 effective IDs 以及 supplementary group IDs 是进程的属性。内核进行的测试具体如下:

1. 如果进程的 effective user ID 是0(the superuser),那么允许访问。

2. 如果进程的 effective user ID 等于文件的 owner(例如,进程拥有该文件),并且合适的 user access permission bit(文件的 struct stat 结构中的 st_mode 成员的一个bit) 被 set,那么允许访问该文件,否则,拒绝访问。此处,合适的 user access permission bit 是指:如果进程打开文件是为了 read,那么 user-read bit 应为1,如果进程打开文件是为了 write,那么 user-write bit 应为1,如果进程打开文件是为了执行,那么 user-execute 应为1。

3. 如果进程的 effective group ID 或者 其中一个 supplementary group ID 等于文件的 group ID,并且合适的 group access permission bit(文件的 struct stat 结构中的 st_mode 成员的一个bit)被set,那么进程允许访问该文件,否则,拒绝访问。

4. 如果合适的 other access permission bit(文件的 struct stat 结构中的 st_mode 成员的一个bit)被set,那么进程允许访问该文件,否则,拒绝访问。

以上4个步骤按顺序尝试进行。注意,如果进程拥有该文件(step 2),那么进程能否访问该文件,将仅仅取决于文件的 user access permission,不会再涉及文件的 group access permission。同样,如果进程并不拥有该文件但是属于一个适合的 group,那么进程能否访问该文件,仅仅取决于该文件的 group access permission,不会再涉及文件的 other permission。

Notes for Apue —— chapter 4 Files and Directories(文件和目录)

标签:

原文地址:http://www.cnblogs.com/jianxinzhou/p/4477684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!