apue2 阅读笔记--第四章
1.
首先,关于 stat, fstat, lstat
#include <sys/stat.h> int stat(const char *restrict pathname, struct stat *restrict buf);
int fstat(int filedes, struct stat *buf); int lstat(const char *restrict pathname, struct stat *restrict buf);
注意两点,
(1) 对于链接文件,stat返回的是指向的文件的属性, lstat返回的是链接文件本身的属性。
(2) fstat 接收的参数与stat和lstat不同, 第一个参数是打开得文件描述符。
2.
stat 结构体
struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ 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 */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ blksize_t st_blksize; /* best I/O block size */ blkcnt_t st_blocks; /* number of disk blocks allocated */ };
3.
关于实际用户id, 有效用户id,以及内核的权限检查:
The file access tests that the kernel performs each time a process opens, creates, or deletes a file depend on the owners of the file (st_uid and st_gid), the effective IDs of the process (effective user ID and effective group ID), and the supplementary group IDs of the process, if supported. The two owner IDs are properties of the file, whereas the two effective IDs and the supplementary group IDs are properties of the process. The tests performed by the kernel are as follows.
-
If the effective user ID of the process is 0 (the superuser), access is allowed. This gives the superuser free rein throughout the entire file system.
-
If the effective user ID of the process equals the owner ID of the file (i.e., the process owns the file), access is allowed if the appropriate user access permission bit is set. Otherwise, permission is denied. By appropriate access permission bit, we mean that if the process is opening the file for reading, the user-read bit must be on. If the process is opening the file for writing, the user-write bit must be on. If the process is executing the file, the user-execute bit must be on.
-
If the effective group ID of the process or one of the supplementary group IDs of the process equals the group ID of the file, access is allowed if the appropriate group access permission bit is set. Otherwise, permission is denied.
-
If the appropriate other access permission bit is set, access is allowed. Otherwise, permission is denied.
实际用户id是文件属性,有效用户id是进程属性。
注意用access函数测试时,使用的是实际用户id。
4.
创建已有文件的链接用link, 删除文件用unlink,只有最后一个指向该文件inode的链接被删除时,文件才真正的被移除。
int unlink(const char *pathname); ulink和remove函数都只是处理链接文件本身,不跟随。
5.
注意m_time 和 c_time 的区别,虽然都是修改时间,但是前者是内容,后者是对应inode节点。
st_atime last-access time of file data read -u (ls选项)
st_mtime last-modification time of file data write default
st_ctime last-change time of i-node status chmod, chown -c
Note the difference between the modification time (st_mtime) and the changed-status time (st_ctime). The modification time is when the contents of the file were last modified. The changed-status time is when the i-node of the file was last modified. In this chapter, we've described many operations that affect the i-node without changing the actual contents of the file: changing the file access permissions, changing the userID, changing the number of links, and so on. Because all the information in the i-node is stored separately from the actual contents of the file, we need the changed-status time, in addition to the modification time.
6.
操作目录的一组函数, DIR结构体的地位跟文件操作里的FILE差不多:
#include <dirent.h>
DIR *opendir(const char *pathname);
Returns: pointer if OK, NULL on error
struct dirent *readdir(DIR *dp);
Returns: pointer if OK, NULL at end of directory or error
void rewinddir(DIR *dp);
int closedir(DIR *dp);
Returns: 0 if OK, 1 on error
long telldir(DIR *dp);
Returns: current location in directory associated with dp
void seekdir(DIR *dp, long loc);
int chdir(const char *pathname); //改变进程的工作路径
char *getcwd(char *buf, size_t size); //得到当前进程工作的完整路径