欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

apue读书笔记-Ch04

程序员文章站 2022-07-14 20:23:14
...

4.1 Introduction

look at additional features of the file system and the properties of a file. We’ll start with the stat functions and go through each member of the stat structure, looking at all the attributes of a file.

In this process, we’ll also describe each of the funtions that modeify these attributes: change the owner, change the permissions, and so on.

We’ll also look in more detail at the structure of a UNIX file system and symbolic links.

4.2 stat, fstat, fstatat, lstat Functions

int stat(const char *restrice pathname, struct stat *restrict buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *restrict pathname, struct stat *restrict buf);
int fstatat(int fd, const char *restrict pathname, struct stat *restrict buf, int flag);
Given a pathname, the stat function returns a structure of information about the named file. The fstat function obtains information about the file that is already open on the descriptor fd. 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. The buf argument is a pointer to a structure that we must supply. The functions fill in the structure.

c语言中关键字restrict的用法

restrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式.即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其它途径(其它变量或指针)来修改;这样做的好处是,能帮助编译器进行更好的优化代码,生成更有效率的汇编代码.如 int *restrict ptr, ptr 指向的内存单元只能被 ptr 访问到,任何同样指向这个内存单元的其他指针都是未定义的,直白点就是无效指针。restrict 的出现是因为 C 语言本身固有的缺陷,C 程序员应当主动地规避这个缺陷,而编译器也会很配合地优化你的代码.

restrict的定义是 ( restrict type qualifier ):During each execution of a block in which a restricted pointer P is declared (typically each execution of a function body in which P is a function parameter), if some object that is accessible through P (directly or indirectly) is modified, by any means, then all accesses to that object (both reads and writes) in that block must occur through P (directly or indirectly), otherwise the behavior is undefined.现在程序员用restrict修饰一个指针,意思就是“只要这个指针活着,我保证这个指针独享这片内存,没有‘别人’可以修改这个指针指向的这片内存,所有修改都得通过这个指针来”。由于这个指针的生命周期是已知的,编译器可以放心大胆地把这片内存中前若干字节用寄存器cache起来

restrict

definition of the structure can differ among implementations, but it could look like

struct stat
{
    mode_t st_mode;
    ino_t st_ino;
    dev_t st_dev;
    ...
    ..
    .
};

4.3 File Types

  • Regular file. There is no distinction to the UNIX kernel whether this data is text or binary. Any interpretation of the contents of a regular file is left to the application processing the file.
  • Directory file. A file that contains the names of other files and pointers to information on these files.
  • Block special file
  • Character special file
  • FIFO
  • Socket
  • Symbolic

Examples Figure 4.3
在mac上编译运行以下代码。

/*
The program in Figure 4.3 prints the type of file for each command-line argument.
*/

#include "apue.h"

int main(int argc, char *argv[])
{
    int i;
    struct stat buf;
    char *ptr;

    for(i = 1; i < argc; i++){
        // print parameters` name
        printf("%s: ", argv[i]);

        if(lstat(argv[i], &buf) < 0){
            err_ret("lstat error");
            continue;
        }

        if(S_ISREG(buf.st_mode))
            ptr = "regular";
        else if(S_ISDIR(buf.st_mode))
            ptr = "directory";
        else if(S_ISCHR(buf.st_mode))
            ptr = "character special";
        else if(S_ISBLK(buf.st_mode))
            ptr = "block special";
        else if(S_ISFIFO(buf.st_mode))
            ptr = "fifo";
        else if(S_ISLNK(buf.st_mode))
            ptr = "symbolic link";
        else if(S_ISSOCK(buf.st_mode))
            ptr = "socket";
        else
            ptr = "** unknown mode **";
        printf("%s\n", ptr);
    }
    exit(0);
}

结果如图
apue读书笔记-Ch04
书中的结果:
apue读书笔记-Ch04

Set-User-ID and Set-Group-ID

apue读书笔记-Ch04

每一个文件都有一个owner和group owner。The owner is specified by the st_uid member of the stat structure; the group owner, by the st_gid member.

File access permission

There are nine permission bits for each file, divided into three categories.
apue读书笔记-Ch04

apue读书笔记-Ch04

  • 当我们打开一个文件时,那必须有路径上所有文件的execute permission
  • 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.
  • 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.

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.

  • 如果进程的effective user ID是0,superuser,access is allowed。
  • 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.
  • group ID 同上

Note that if the process owns the file (step 2), access is granted or denied based only on the user access permissions; the group permissions are never looked at. Similarly, if the process does not own the file but belongs to an appropriate group, access is granted or denied based only on the group access permissions; the other permissions are not looked at.

4.6 Ownership of New Files and Directories

User ID 被设置为effective user ID

4.7 access and faccessas Functions

4.8 unmask Function

Users can set the umask value to control the default permissions on the files they create.

4.9 chmod, fchmod, fchmodat Functions

相关标签: apue 读书笔记