《Unix环境高级编程》读书笔记 第4章-文件和目录

1. stat结构的基本形式:

  1. struct stat {
  2. mode_t st_mode; /* file type & mode (permissions), suid, sgid */
  3. ino_t st_ino; /* i-node number (serial number) */
  4. dev_t st_dev; /* device number (file system) */
  5. dev_t st_rdev; /* device number for special files */
  6. nlink_t st_nlink; /* number of links */
  7. uid_t st_uid; /* user ID of owner */
  8. gid_t st_gid; /* group ID of owner */
  9. off_t st_size; /* size in bytes, for regular files */
  10. struct timespec st_atim; /* time of last access */
  11. struct timespec st_mtim; /* time of last modification */
  12. struct timespec st_ctim; /* time of last file status change */
  13. blksize_t st_blksize; /* best I/O block size */
  14. blkcnt_t st_blocks; /* number of disk blocks allocated , 512B */
  15. };

  16. struct timespec {
  17. time_t tv_sec;
  18. long tv_nsec;
  19. }

2. 函数stat、fstat、fstatat、lstat

  • 函数名首位的“f”常常代表“该函数是通过文件描述符fd(而不是路径名)对文件进行操作的”
  1. #include <sys/stat.h>
  2. int stat(const char *restrict pathname, struct stat *restrict buf );
  3. int fstat(int fd, struct stat *buf );
  4. int lstat(const char *restrict pathname, struct stat *restrict buf ); // 返回该符号链接本身的有关信息
  5. int fstatat(int fd, const char *restrict pathname, struct stat *restrict buf, int flag);
  6. All four return: 0 if OK, 1 on error

3. 文件类型

  • 普通文件 -
  • 目录文件 d
  • 块特殊文件 b
  • 字符特殊文件 c
  • FIFO p
  • 套接字 s
  • 符号链接 l

  • 文件类型宏:参数为stat结构中的st_mode成员(文件模式字)

  • IPC类型宏:参数为stat结构的指针

4. 设置用户ID(SUID) 和 设置组ID(SGID)

  • 每个进程关联的用户ID和组ID,包括实际用户ID、有效用户ID、保存的设置用户ID。
  • 实际用户ID指的是执行该程序的用户的ID。注意区别于文件的所有者

  • 每个文件有一个所有者和组所有者,由stat结构中的st_uid, st_gid指定。

  • 当执行一个程序文件时,进程的有效用户ID通常就是实际用户ID。但是,可以在文件模式字(st_mode)中设置一个标志,使其“当执行该程序文件时,将进程的有效用户ID设置为文件所有者的ID(st_uid)”。进程的有效组ID类似,在文件模式字st_mode中的这两位标志被称为SUID和SGID。可使用常量S_ISUID和S_ISGID测试。
  • 应用:passwd命令

5. 文件访问权限

  • 所有文件类型都有访问权限

  • 进程每次打开、创建或删除一个文件时,内核就进行文件访问权限测试,这种测试可能涉及文件的所有者(st_uid和st_gid)、进程的有效ID(有效用户ID和有效组ID)、进程的附属组ID。两个所有者ID是文件的性质,而两个有效ID和附属组ID则是进程的性质。

  • 内核进行的测试具体如下:
  1. 若进程的有效用户ID是0(超级用户),则运行访问
  2. 若进程的有效用户ID等于文件的所有者ID(即进程拥有此文件),那么如果所有者适当的访问权限位被设置,则运行访问;否则拒绝访问。适当的访问权限位指的是:若进程为读而打开该文件,则用户读位应为1;若进程为写而打开该文件,则用户写位应为1;若进程将执行该文件,则用户执行位应为1。
  3. 若进程的有效组ID或进程的附属组ID之一等于文件的组ID,那么如果组适当的访问权限位被设置,则允许访问;否则拒绝访问。
  4. 若其他用户适当的访问权限位被设置,则允许访问;否则拒绝访问。
    按顺序执行这4步。一旦前面的被拒绝了,即使后面的组、其他用户拥有相应权限也白搭。

6. 新文件和新目录的所有权

  • 新文件的用户ID设置为进程的有效用户ID
  • 新目录的组ID有两种选择:1. 设置为进程的有效组ID;2. 设置为它的父目录的组ID。

    Linux下如果新目录的父目录的SUID被设置,则选择2

7. 函数access 和 faccessat

当open函数打开一个文件时,内核以进程的有效用户ID和有效组ID为基础执行其访问权限测试。
有时,进程希望以进程的实际用户ID和实际组ID为基础来执行其访问权限测试。这使用以下两个函数:

  1. #include <unistd.h>
  2. int access(const char *pathname, int mode);
  3. int faccessat(int fd, const char *pathname, int mode, int flag);
  4. Both return: 0 if OK, 1 on error

测试文件是否存在,mode为F_OK;测试读/写/执行权限,mode为R_OK、W_OK、X_OK的按位与

8. 函数umask

  • 在进程创建一个新文件或新目录时,一定会使用文件模式创建屏蔽字。在文件模式创建屏蔽字中为1的位,在文件mode中的相应位一定被关闭。
  • 常用的几种umask值是002、022、027
  • 命令umask [-S]
  1. #include <sys/stat.h>
  2. mode_t umask(mode_t cmask);
  3. Returns: previous file mode creation mask

9. 函数chmod、fchmod和fchmodat

  1. #include <sys/stat.h>
  2. int chmod(const char *pathname, mode_t mode);
  3. int fchmod(int fd, mode_t mode);
  4. int fchmodat(int fd, const char *pathname, mode_t mode, int flag);
  5. All three return: 0 if OK, 1 on error
  • 为了改变一个文件的权限位,进程的有效用户ID必须等于文件的所有者ID,或者该进程具有root权限

10. 粘着位(Sticky bit)

  • 针对文件
    历史上,一个设置了粘着位的程序文件在其终止时,程序正文部分的一个副本仍保存在交换区,这使得下次执行该程序时能较快地将其载入内存。因为在系统再次重启前,文件的正文部分总是在交换区中,这正是名字“粘着”的由来。现今较新的UNIX系统大多配置了虚拟存储系统以及快速文件系统,所以不再需要该技术。
  • 针对目录
    如果对一个目录设置了粘着位,只有对该目录具有写权限的用户并且满足下列条件之一,才能删除或重命名该目录下的文件:
    1. 拥有此文件;2. 拥有此目录;3. 是超级用户
    • 目录/tmp 和 /var/tmp 是设置粘着位的典型候选者。

11. 函数chown、fchown、fchownat和lchown

  1. #include <unistd.h>
  2. int chown(const char *pathname, uid_t owner, gid_t group);
  3. int fchown(int fd, uid_t owner, gid_t group);
  4. int fchownat(int fd, const char *pathname, uid_t owner, gid_t group, int flag);
  5. int lchown(const char *pathname, uid_t owner, gid_t group);
  6. All four return: 0 if OK, 1 on error
  • 如果这些函数由非root进程调用,则在成功返回时,该文件的SUID和SGID位都被清除。

12. 文件长度

  • stat结构成为st_size表示以字节为单位的文件的长度。此字段只对普通文件、目录文件和符号链接有意义。
  • 对于普通文件,其文件长度可以是0
  • 对于目录,文件长度通常是一个数(如512,4096)的整数倍
  • 对于符号链接,文件长度是文件名中的实际字节数,不包含通常C语言用作名字结尾的null字节
  • 大多数现代的UNIX系统提供字段st_blksize和st_blocks。第一个是对文件I/O较合适的块长度,第二个是所分配的实际512字节块块数。

  • 文件中的空洞

    空洞是由所设置的偏移量超过文件尾端,并写入了某些数据后造成的。对于没有写过的字节位置,read函数读到的字节是0

13. 文件截断

  1. #include <unistd.h>
  2. int truncate(const char *pathname, off_t length);
  3. int ftruncate(int fd, off_t length);
  4. Both return: 0 if OK, 1 on error

14. 文件系统

15. 函数link(硬链接)、linkat、unlink、unlinkat、remove

  1. #include <unistd.h>
  2. int link(const char *existingpath, const char *newpath); // 只创建newpath中的最后一个分量,路径中的其他部分应当已经存在
  3. int linkat(int efd, const char *existingpath, int nfd, const char *newpath, int flag);
  4. Both return: 0 if OK, 1 on error
  • 大多数实现要求现有的和新建的两个路径名在同一个文件系统中。
  • 很多文件系统实现不允许对于目录的硬链接。
  1. #include <unistd.h>
  2. int unlink(const char *pathname); // 若其父目录设置了粘着位,参见上面“粘着位”
  3. int unlinkat(int fd, const char *pathname, int flag);
  4. Both return: 0 if OK, 1 on error
  • 关闭一个文件时,内核首先检查打开盖文件的进程个数;如果这个计数达到0,内核再去检查其链接计数;如果计数也是0,那么就删除该文件的内容。
  • unlink的这种特性进程被程序用来确保即使在程序崩溃时,它所创建的临时文件也不会遗留下来。进程用open或creat创建一个文件,然后立即调用unlink
  1. #include <stdio.h>
  2. int remove(const char *pathname);
  3. Returns: 0 if OK, 1 on error
  • remove函数解除对一个文件或目录的链接。对于文件,等同于unlink;对于目录,等同于rmdir。

16. 函数rename和renameat

  1. #include <stdio.h>
  2. int rename(const char *oldname, const char *newname);
  3. int renameat(int oldfd, const char *oldname, int newfd, const char *newname);
  4. Both return: 0 if OK, 1 on error

17. 符号链接(软链接)

  • 引入符号链接是为了避开硬链接的一些限制:

    1. 硬链接通常要求链接和文件位于同一个文件系统中
    2. 只有超级用户才能创建指向目录的硬链接(在底层文件系统支持的情况下)
  • 各函数对符号链接的处理:follow 或 not follow

18. 创建和读取符号链接

  1. #include <unistd.h>
  2. int symlink(const char *actualpath, const char *sympath);
  3. int symlinkat(const char *actualpath, int fd, const char *sympath);
  4. Both return: 0 if OK, 1 on error
  1. #include <unistd.h>
  2. ssize_t readlink(const char* restrict pathname, char *restrict buf, size_t bufsize);
  3. ssize_t readlinkat(int fd, const char* restrict pathname, char *restrict buf, size_t bufsize);
  4. Both return: number of bytes read if OK, 1 on error

19. 文件的时间

  • 各函数对3个文件时间的影响,包括:所操作的文件或目录本身、所操作的文件或目录的父目录

20. 函数futimens、utimensat、utimes

  1. #include <sys/stat.h>
  2. int futimens(int fd, const struct timespec times[2]);
  3. int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
  4. Both return: 0 if OK, 1 on error
  • times数组参数的第一个元素包含访问时间,第二个元素包含修改时间
  • futimens函数需要打开文件来更改它的时间;utimensat函数提供了一种使用文件名更改文件时间的方法。
  1. #include <sys/time.h>
  2. int utimes(const char *pathname, const struct timeval times[2]);
  3. Returns: 0 if OK, 1 on error
  • utimes函数对路径名进行操作,times参数指向访问时间和修改时间,状态更改时间自动更新

21. 函数mkdir、mkdirat、rmdir

  1. #include <sys/stat.h>
  2. int mkdir(const char *pathname, mode_t mode);
  3. int mkdirat(int fd, const char *pathname, mode_t mode);
  4. Both return: 0 if OK, 1 on error
  1. #include <unistd.h>
  2. int rmdir(const char *pathname);
  3. Returns: 0 if OK, 1 on error

22. 读目录

  1. #include <dirent.h>
  2. DIR *opendir(const char *pathname);
  3. DIR *fdopendir(int fd);
  4. Both return: pointer if OK, NULL on error

  5. struct dirent *readdir(DIR *dp);
  6. Returns: pointer if OK, NULL at end of directory or error

  7. void rewinddir(DIR *dp);
  8. int closedir(DIR *dp);
  9. Returns: 0 if OK, 1 on error

  10. long telldir(DIR *dp);
  11. Returns: current location in directory associated with dp

  12. void seekdir(DIR *dp, long loc);
  1. dirent结构至少包含以下两个成员:

  2. struct dirent {
  3. ino_t d_ino; /* i-node number */
  4. char d_name[]; /* null-terminated filename */
  5. }

23. 函数chdir、fchdir、getcwd

  • 每个进程都有一个当前工作目录。
  • 当前工作目录是进程的一个属性,起始目录则是登陆名的一个属性。
  1. #include <unistd.h>
  2. int chdir(const char *pathname);
  3. int fchdir(int fd);
  4. Both return: 0 if OK, 1 on error

24. 设备特殊文件

  • 每个文件系统所在的存储设备都由其主、次设备号表示。
  • 设备号所用的数据类型是基本系统数据类型dev_t。
  • 主设备号标识设备驱动程序,有时编码为与其通信的外设板;次设备号标识特定的子设备。
  • 在同一磁盘驱动器上的各文件系统通常具有相同的主设备号,但是次设备号却不同。
  • 可以使用两个宏:major和minor来访问主、次设备号。
  • 系统中与每个文件名关联的st_dev值是文件系统的设备号,该文件系统包含了这一文件名以及其对应的i节点。
  • 只有字符特殊文件和块特殊文件才有st_rdev值。此值包含实际设备的设备号。

25. 文件访问权限位小结

原文地址:https://www.cnblogs.com/DayByDay/p/3891592.html