APUE学习笔记之文件和目录(上)(5)

4. FIles and Directories (文件和目录)

  4.1 Introduction (介绍)

  We'll start with the stat functions and go through each member of the stat structure, looking at  all the attributes of a file. (我们首先学习stat函数,了解stat结构体的各个成员,了解文件的各种属性。)

  We'll also look in more detail at the structure of a UNIX file system and symbolic links. (我们还将看到更详细UNIX文件系统的结构和符号链接。)

  4.2 stat, fstat, and lstat Functions (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);

              All three returns: 0 if OK, -1 on error;

  Given a pathname, the stat function returns a structure of information about the named file. (提供一个路径名,stat函数返回一个关于文件的结构体信息。)

  The fstat function obtains information about the file that is already open on the descriptor filedes. (fstat函数获得filedes文件的信息。)

  The lstat function is similar to stat, but when the named file is a symbolic link, the lstat returns information about the symbolic link, not the file referenced by the symbolic link. (lstat函数和stat函数相似,但当文件是一个符合链接时,lstat函数返回符合链接的信息,而不是指向文件。)

  The second argument is a pointer to a structure that we must supply. (第二个参数是一个结构体指针,必须提供。)

  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 stat change */

    blksize_t st_blksize; /* best I/O block size */

    blkcnt_t st_blocks;  /* number of disk blocks allocated */

  };

  The biggest user of the stat functions is propably the ls -l command, to learn all the information about a file. (stat函数使用最多的就是“ls -l”这个命令了。它显示拉文件的全部信息。)

  4.3 File Types(文件类型)

  1. Regular file(普通文件): The most common type of file. (大多数类型的文件。)

  2. Directory(目录): A file that contains the names of other files and pointers to information on these files. (一种包含了其它文件名和这些文件信息的指针。)

  3. Block special file(块特殊文件): A type of file providing buffered I/O access in fixed-size units to devices such as disk drivers. (一种提供缓冲I / O访问固定大小的单位设备文件。像如磁盘驱动器。)

  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. (一种提供非缓冲I / O访问可变户型设备的文件。所有的设备文件不是块就是字符特殊文件。)

  5. FIFO: A type of file used for commnication between process. (一种用于进程间通讯的文件。)

  6. Socket: A type of file used for network communication between process. (一种用于进程间网络通讯的文件。)

  7. Symbolic link(符号链接): A type of file that points to another file. (一种指向另一个文件的文件。)

  macros(宏): S_ISREG() S_ISDIR() S_ISBLK() S_ISCHR() S_ISFIFO() S_ISSOCK() S_ISLNK().

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

  Real user ID and real group ID: These two fields are taken from our entry in the password file when we log in. (这俩个字段来自我们登录时的密码文件。)

  The effective user ID and effective group ID and supplementary group IDs determine our file access permissions. (有效用户ID有效组ID和补充组ID决定拉我们访问文件的权限。)

  The saved set-user-ID and seved set-group-ID contain copies of the effective user ID and the effective group ID when a program is executed. (保存设置用户ID和保存设置组ID在一个程序被执行时包含的有效用户ID和有效组ID的副本)

  Normally, the effective user ID equals the real user ID, and the effective group ID equals the real group ID. (通常,有效用户和实际用户相同,有效组ID和实际用户组ID相同。)

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

  The nine file access permission bits, from <sys/stat.h>:

  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

  The chmod(1) command, which is typically used to modify these nine permission bits, allows us to specify u for user, g for group, and o for other. (chmod(1)命令经常用来修改这些权限位,它允许u=user, g=group, o=other。)

  Whenever we want to open any type of file by name, we must have execute permission in each directory mentioned in the name, including the current directory, if it is implied. (当我们想通过文件名打开任何类型的文件,我们必须要有每层目录的执行权限,包括当前目录)

  Read permission lets us read the directory, obtaining a list of all the file names in the directory. (目录的读权限允许我们查看目录下的文件列表。)

  Execute permission lets us pass through the directory when it is a component of a pathname that we are trying to access. (目录的执行权限允许我们进入目录。)

  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. (文件的读权限决定拉是否可以读取。)

  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. (文件的写权限决定拉是否可以写入。)

  We must have write permission for a file to specify the O_TRUNC flag in the open function. (在open函数中指定O_TRUNC标志,我们要有文件的写权限。)

  We can not create a new file in a directory unless we have write permission and executer permission in the directory. (除非我们有这个目录的写权限和执行权限,否则我们不能在这个文件中创建新文件。)

  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. (要删除一个存在的文件,我们需要有包含这个文件的目录的写权限和执行权限。我们不需要这个文件本身的读权限或写权限。)

  Execute permission for a file must be on if we want to execute the file using any of the six exec functions. (如果我们想使用那六个exec函数执行文件,我们必须要有那个文件的执行权限。)

  4.6 Ownership of New Files and Directories (新文件和目录所有权)

  The user ID of a new file is set to the effective user ID of the process. (新文件的用户ID被设置成进程的有效用户ID。)

  The group ID of a new file can be the effective group Id of the process. (新文件的组ID可能被设置成进程的有效组ID。)

  The group ID of a new file can be the group ID of the directory in which the file is being created. (新文件的组ID可能被设置成文件所在目录的组ID。)

  4.7 access Function (access 函数)

  #include <unistd.h>

  int access(const cahr *pathname, int mode);

            Returns: 0 if OK, -1 on error

  
  The access function bases its tests on the real user and group IDs. (access函数基于对真正用户和组ID的测试。 )

  4.8 umask Function (umask 函数)

  #include <sys/stat.h>

  mode_t umask(mdoe_t cmask);

            Returns: previous file mode creation mask

  The umask function sets the file mode creation mask for the process and returns the previous value. (umask函数为进程设置文件的模式,并返回之前的模式值。)

  The cmask argument is formed as the bitwise OR of any of the nine constants. (cmask参数是九个常量之间按位或形成的。)

  The file mode creation mask is used whenever the process creates a new file or a new directory. (每当进程创建一个新的文件或新目录都会使用到文件模式。)

  4.9 chmod and fchmod Functions (chmod 和 fchmod 函数)

  #include <sys/stat.h>

  int chmod(const char *pathname, mode_t mode);

  int fchmod(int filedes, mode_t mdoe);

            Both return: 0 if OK, -1 on error

  The chmod function operates on the specified file, whereas the fchmod funciton operates on a file that has already been opened. (chmod函数操作指定的文件,而fchmod函数操作已经打开的文件。)

  To change the permission bits of a file, the effective user ID of the process must be equal to the owner ID of the file, or the process must have superuser permissions. (要改变文件的权限位,进程的有效用户ID必须等于文件拥有者的ID, 或者进程必须有超级管理员权限。)

  Only the superuser can set the sticky bit of a regular file. (只有超级管理员可以设置普通文件的sticky位。)

  It is possible that the group ID of a newly created file is a group that the calling process does not belong to. (进程的可能不属于新建文件的组ID。有可能是父级目录的组ID。)

  (完)

  

posted @ 2013-09-21 14:28  yanjf  阅读(175)  评论(0编辑  收藏  举报