您的位置:首页 > 产品设计 > UI/UE

APUE学习笔记之文件和目录(下)(7)

2013-09-28 19:50 337 查看
  4.18 File Times (文件时间)

    Field       Description        Example        s(1) option    
  st_atime  last-access time of file data  read  -u
  st_mtime  last-modification time of file data  write  default
  st_ctime  last-change time of i-node status  chmod, chown  -c
  Note that the system does not maintain the last-access time for an i-node. (系统没有为i-node维护一个最后访问时间。)

  The access time is often used by system adminstrators to delete files that have not been accessed for a certain amount of time. (访问时间通常是用于系统管理员删除长时间没访问过的文件的。)

  Creating a new file affects the directory and that contains the new file, and it affects the i-node for the new file. (新建文件影响包含新文件的那个目录,并且影响新文件的i-node。)

  Reading or writing a file, however, affects only the i-node of the file and has no effect on the directory. (读取或写入文件,只影响i-node,不影响目录。)

  4.19 utime Function (utime 函数)

  #include <utime.h>

  int utime(const char *pathname, const struct utimbuf *times);

              Returns: 0 if OK, -1 on error

  struct utimebuf {

    time_t actime; /* access time */

    time_ modtime; /* modification time */

  }

  The access time and the modification time of a file can be changed with the utime function. (utime函数可以改变文件的访问时间和修改时间。)

  The two time values in the structure are calendar times, which count seconds since the Epoch. (结构体的两个时间值是日历时间,从大纪元开始按秒计数。)

  If times is a null pointer, the access time and the modification time are both set to the current time. To do this, either the effective user ID of the porcess must equal the owner ID of the file, or the process must have write permission for the file. (如果times是一个空指针,访问时间和修改时间被设置为当前时间。这时,进程的有效用户ID必须和文件的拥有者ID一致,或者进程必须拥有文件的写权限。)

  If times is a non-null pointer, the access time and the modification time are set to the values in the structure pointed to by times. For this case, the effective user ID of the process must equal the owner ID of the file, or the process must be a superuser process. Merely having write permission for the file is not adequate. (如过times是一个非空指针,访问时间和修改时间被设置为结构体指定的时间值。这种情况下,进程的有效用户ID等于文件拥有者的ID,或者进程是超级进程,仅仅有文件的写权限是不够的。)

  Note that we are ubable to specify a value for the changed-status time,st_ctime-- the time i-node was last changed-- as this field is automatically update when the utime function is called. (我们不能为st_ctime(i-node最后修改时间)设定一个值,因为这个字段是当utime函数被调用后,自动更新的。)

  4.20 mkdir and rmdir Functions (mkdir 和 rmdir 函数)

  #include <sys/stat.h>

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

          Returns: 0 if OK, -1 on error

  A common mistake is to specify the same mode as for a file: read and write permissions only. But for directory we normally want at leaset one of the execute bits enabled, to allow access to filename within the directory. (一个常犯的通病:为目录指定和文件一样的模式:只有读和写权限。但是,对于目录来说至少要指定执行权限来允许访问目录内的文件名。)

  #include <unistd.h>

  int rmdir(const char *pathname);

          Returns: 0 if Ok, -1 on error

  A empty directory is deleted with the rmdir function. (rmdir 函数删除空目录。)

  4.21 Reading Directories (读取目录)

  Directories can be read by anyone who has access permission to read the directory. But only the kernel can write to a directory, to preserve file system sanity. (任何人只要有读权限,都可以读取目录。但是只有内核可以写入到一个目录,保存文件系统合理。)

  #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 the 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);

  struct dirent {

    ino_t d_ino;             /* i-node number */

    char d_name[NAME_MAX + 1]; /* null-terminated filename */

  }

  The DIR struct is an internal structure used by these six functions to maintain information about the directory being read. (DIR 结构体是一个内部的结构体,它是六个函数用来维护正在读取目录的信息的。)

  The pointer to a DIR structure that is returned by opendir is then used with the other five functions. (指向DIR结构体的指针是opendir函数返回的,然后用于其它五个函数。)

  The opendir function initializes things so that the first readdir reads the first entry in the directory. (opendir函数初始化数据,因此第一个readdir函数读出到目录内的第一个entry。)

  The ordering of entries within the directory is implementation dependent and is usually not alphabetical. (在该目录中的条目顺序是依赖于实现,通常是不按字母顺序排列的。)

  4.22 chdir, fchdir, and getcwd Functions ()

  #include <unistd.h>

  int chdir(const char *pathname);

  int fchdir(int filedes);

              Both return: 0 if OK, -1 on error

  We can change the current working directory of the calling process by calling the chdir or fchdir functions. (我们可以通过进程调用chdir或者fchdir函数修改当前的工作目录。)

  We can specify new current working directory either as a pathname or though an open file descriptor. (我们可以通过文件路径或打开文件的描述符来指定新的工作目录。)

  The current working directory is an attribute of a process; the home directory is an attribute of a login name; (当前工作目录是进程的一个属性,主目录是登录用户名的一个属性。)

  Beacuse it is a attribute of a process, the current working directory cannot affect processes that invoke the process that executes the chdir. (因为当前工作目录是进程的属性,所以它执行chdir不影响进程调用的过程。)

  Each program is run in a separate process, so the current working directory of the shell is unaffected by the call to chdir in the program. (每个程序运行在一个单独的进程,所以在程序中调用chdir将不受影响shell的当前工作目录。)

  For this reason, the chdir function has to be called directly from the shell, so the cd cmd is built into the shells. (由于这个原因,chdir函数必须被shell直接调用,所以cd命令在shell内部构建。)

  The kernel doesn't maintain the full pathname of the directory. Instead, the kernel keeps information about the directory, such as a pointer to the directory's v-node. (核心没有维护目录的全路径,相反,核心保持拉目录的信息,像指向目录的v-node的指针。)

  #include <unistd.h>

  char *getcwd(char *buf, size_t size);

      Returns: buf is OK, NULL on error;

  The buffer must be large enough to accommodate the absolute pathname plus a terminationg null byte, or an error is returned. (buf必须大到足以容纳的绝对路径加上终止空字节,否则返回一个错误)

  The getcwd function is useful when we have an application that needs to return to the location in the file system where it started out. (当我们有个程序需要返回到它开始的地方时,getcwd函数很有用。)

  We can save the starting location by calling getcwd before we change our working directoty. After we complete our processing, we can pass the pathname obtained from getcwd to chdir to return to our starting location in the file system. (我们可以通过在改变工作目录之前调用getcwd函数保存开始位置。在程序完成后,我们可以通过getcwd保存的路径返回到开始的位置。)

  The fchdir function provides us with an easy way to accomplish this task. When we want to return to where we started, we can simply pass the file descriptor to fchdir. (fchdir函数提供给我们一个方便的方法来完成这个任务,当我们想要返回我们开始的位置时,我们可以通过向fchdir函数传递一个文件描述符。)

  4.23 Device Special Files (设备专用文件)

  Every file system is known by its major and minor device numbers, which are encoded in the primitive system data type dev_t. (每个文件系统都通过它的主要和次要设备编号被大家所知。)

  The major number identifies the deivce driver and somethings encodes which peripheral board to communicate with; the minor number identifies the specific subdievice. (主要设备编号标识拉设备驱动和一些硬编码,它们和外围设备同学。次要设备编号标识拉特殊的子设备。)

  Each file system on the same disk would usually have the same major number, but a different minor number. (在同一个硬盘上的每个文件系统通常有相同的主要编号,但次要编号却不一样。)

  We can usually access the major and minor device numbers through two macros defined by most implementations: major and minor. (我们可以通过两宏(major 和 minor)来访问主要和次要编号。大多数实现都定义了它们。)

  The st_dev value for every filename on a system is the device number of the file system containing that filename and its corresponding i-node. (系统上的每个文件名的st_dev值是文件系统的设备号,包含了文件名和它相对应的i-node节点。)

  Only character special files and block special files have an st_rdev value. This value contains the device number for the actual device. (只有字符特殊文件和块特殊文件有一个st_rdev值。这个值包含了每个实际设备的设备编码。)

  Note that the filenames and i-nodes for the two terminal devices(st_dev) are on device0/7-- the devfs pseudo file system, which implements the /dev -- but that their actual device numbers are 4/0 and 4/1. (两个终端设备的文件名和i-nodes是device0/7--devfs的伪文件系统, 实现了/dev,--但它们的实际设备编号是4/0和4/1)

  4.24 Summary of File Access Permission Bits (文件访问权限位总结)

ConstantDescriptionEffect on regular fileEffect on directory
S_ISUID

S_ISGID

set-user-ID

set-group-ID

set effective user ID on execution

if group-execute set then set effective group ID on execution;

otherwise enable mandatory record locking(if supported)

(not used)

set group ID of new files created in directory to group ID of directory

S_ISVTXsticky bitcontrol caching of file contents(if supported)restrict removal and renaming of files in directory
S_IRUSR

S_IWUSR

S_XUSR

user-read

user-write

user-execute

user permission on read file

user permission on write file

user permission on execute file
user permission to read directory entries

user permission to remove and create files in directory

user permission to search for given pathname in pathname

S_IRGRP

S_IWGRP

S_IXGRP

group-read

group-write

group-execute

group permission on read file

group permission on write file

group permission on execute file
group permission to read directory entries

group permission to remove and create files in directory

group permission to search for given pathname in pathname
S_IROTH

S_IWOTH

S_IXOTH

other-read

other-write

other-execute

other permission on read file

other permission on write file

other permission on execute file
other permission to read directory entries

other permission to remove and create files in directory

other permission to search for given pathname in pathname

    S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR 

    S_IRWXG = S_IRGRP | S_IWGRP | S_IXGRP

    S_IRWXO = S_IROTH | S_IWOTH | S_IXOTH

  4.25 Summary (总结)

  A thorough understanding of all the properites of a file and all the funcitons that operate on files is essential to UNIX programming. (全面的理解一个文件的所有属性和操作文件的所有方法是unix编程最基本的。)

  (完)  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: