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

APUE学习笔记之文件和目录(中)(6)

2013-09-23 21:48 302 查看
4.10 Sticky Bit (sticky位)

  If the bit is set for a directory, a file in the directory can be removed or renamed only if the user has write permission for the directory and one of the following: Owns the file; Owns the directory; Is the superuser. (如果一个目录设置拉sticky位,只有拥有目录的写权限和文件的所有者,目录的所有者或者超级管理员可以移出或重名名该文件。)

4.11 chown, fchown, and lchown Functions (chown, fchown, 和 lchown 函数)

  #include <unistd.h>

  int chown(const char *pathname, uid_t owner, gid_t group);

  int fchown(int filedes, uid_t owner, gid_t group);

  int lchown(const char *pathname, uid_t owner, gid_t group);

                All three return: 0 if OK, -1 on error

  The chown functions allow us to change the user ID of a file and the group ID of a file. (chown 相关的函数允许我们修改文件的UID和GID。)

  If the file is a symbolic link, lchown changes the owners of the symblic link itself, not the file pointed to by the symbolic link. (对于符号链接文件,lchown函数修改的uid是链接本身而不是指向的文件。)

  If either of the arguments owner or group is -1, the corresponding ID is left unchanged. (如果uid或者gid有一个为-1,相应的id保持不变。)

  When _POSIX_CHOWN_RESTRICTED is in effect, you can't change the user ID of the other user's files. You can change the group ID of files that you own, but only to groups that you belong to. (_POSIX_CHOWN_RESTRICTED生效时,你不能修改其他用户文件的uid,你可以修改你拥有的文件的gid,当只能修改为有属于的组。)

  4.12 File Size (文件大小)

  The st_size member of the stat structure contains the size of the file in bytes. This field is meaningful only for regular files, directories, and symbolic links. (stat结构体中的st_size成员包含拉文件的大小,这个字段只对普通文件目录和链接文件有意义。)

  For a regular file, a file size of 0 is allowed. We'll get an end-of-file indication on the first read of the file. (对于普通文件,文件的大小允许为0, 我们会在第一次读取文件时读到文件结束的指示(EOF)。)

  For a direcoty, the file size is usually a multiple of a number,such as 16 or 512. (对于目录,对于一个direcoty,文件的大小通常是一个数字的倍数。如16或512)

  For a symbolic link, the file size is the number of bytes in the filename(Note that symbolic links do not contain normal C null byte at the end of the name). (对于链接文件,文件的大小是指文件名的字节长度,(注意链接文件名的结尾不包含通常C的null字节。))

  Holes in a File

  Holes are created by seeking past the current end of file and writing some data. (洞是当偏移量超出文件的结尾,并写入一些数据时产生的。)

  The read function returns data bytes of 0 for any byte positions that have not been written. (在未被写入的地方读取到的为0。)

  If we make a copy of this file, all these holes are written out as actual data bytes of 0. (如果我们复制一个这个文件,所有的洞都被写成实际的0数据。)

  4.13 File Truncation (文件截取)

  #include <unistd.h>

  int truncate(const char *pathname, off_t length);

  int ftruncate(int filedes, off_t length);

             Both return: 0 if OK, -1 on error

  The two functions truncate an existing file to length bytes. (两个函数截取文件的前length个字节。)

  If the implementation does extend a file, data between the old end of file and the new end of file will read as 0. (如果扩展拉一个文件,那么新文件结尾和旧文件结尾之间的数据会被读成0。)

  4.14 File Systems (文件系统)

  The i-node are fixed-length entries that contain most of the information about a file. (i-node是固定长度的,它包含拉文件的大多数信息。)

  When renaming a file without changing file system, the actual contents of the file need not be moved--all that needs to be done is to add a new directory entry that point to the existing i-node, and then unlike the old directory entry. (当在同一个文件系统中重命名一个 文件时,事实上不需要移动文件的内容--需要做的只是添加一个新的目录实体指向已经存在的i-node,然后再去掉旧的目录实体。)

  Any leaf directory always has a link count of 2. (任何一个不包含子目录的目录有两个链接。)

  Note that every subdirectory in a parent directory causes the parent directory's link count increased by 1. (每个子目录都会让父母录的链接数加1。)

  4.15 link, unlink, remove and rename Functions (link, unlink, remove 和 rename 函数。)

  #include <unistd.h>

  int link(const char *existingpath, const char *newpath);

  int unlink(const char *pathname);

        Returns: 0 if OK, -1 on error

  The link function creates a new directory entry, newpath, that references the existing file existingpath. (link函数创建一个新的目录实体newpath,它指向已经存在的existingpaht文件。)

  If the newpath already exists, an error is returned. (如果newpaht已经存在,产生一个错误。)

  Only the last component of the newpath is created. The reset of the path must already exist. (只有newpath的最后一级被创建,剩余的必须已经存在。)

  The unlink function removes the direcotry entry and decrements the link count of the file referenced by pathname. (unlink函数移除目录实体并且减少pathname指向的文件的链接数量。)

  The file is not changed if an error occurs. (发生错误时文件不会被改变。)

  To unlink a file, we must hvae write permission and execute permission in the directory containing the directory entry, as it is the directory entry that we will be removing. (去掉一个文件的链接,我们必须要有包含文件的目录的读权限和执行权限才行。)

  Only when the link count reaches 0 can the contents of the file be deleted. (只有链接的数量为0时,文件的内容才能被删除。)

  As long as some process has the file open, its contents with not be deleted. (只有有进程正在打开文件,这个文件的内容就不会被删除。)

  When a file is closed, the kernel first checks the count of the number of process that have the file open, If this count has reached 0, the kernel then checks the link count; If it is 0, the file's contents are deleted. (当文件关闭后,kernel首先检查打开文件的进程的数量,如果数量为0,kernel接着检查链接的数量,如果链接的数量也是0,那么文件的内容才被删除。)

  If pathname is a symbolic link, unlink removes the symbolic link, not the file referenced by the link. (如果pathname是一个链接文件,去掉的是链接而不是执行的实际文件。)

  There is no function to remove the file referenced by a symbolic link given the name of the link. (没有那个函数可以通过链接文件之间删除指向文件。)

  #include <stdio.h>

  int remove(const char *pathname);

  int rename(const char *oldname, const char *newname);

        Returns: 0 if OK, -1 on error

  For a file remove is identical to unlink, For a directory, remove is identical to rmdir. (对于文件来说remove函数等同于unlink,对于目录remve等同于rmdir。)

  If oldname specifies a file that is not a directory, then we are renaming a file or a symbolic. In this case, if newname exists, it cannot refer to a directory. If newname exists and is not a direcotry, it is remove, and oldname is renamed to newname. (如果oldname指向一个文件,那么我们会重命名文件或文件链接,如果newname已经存在,那么它不能指向目录。这样的话,newname会被移除,oldname重命名为newname。)

  We must hava write permission for the directory containg oldname and for the directory containing newname, since we are changing both directoies. (我们必须要有包含oldname和newname文件的目录的写权限,因为我们这俩个目录都修改拉。)

  If oldname specifies a directory, then we are renaming a directory. If newname exists, it must refer to a directory, and that directory must be empty. If newname exists and is an empty directory, it is removed, and oldname is renamed to newname. Additionally, when we're renaming a directory, newname cannot contain a path prefix that names oldname. (如果oldname指定一个目录,那么我们重命名这个目录。如果newname存在,它必须指向一个目录,并且这个目标为空。这样的话,newname目录被移除,oldname重命名为newname目录。另外,当我们重命名目录时,newname不能包含oldname作为前缀。)

  If either oldname or newname refers to a symbolic link, then the link itself is processed, not the file to which it resolves. (如果oldname或者newname其中有指向文件链接的,那么链接文件被操作,不是指向的文件。)

  As a special case, if the oldname and newname refer to the same file, the function returns successfully without changing anything. (作为特殊情况,如果oldname和newname指向同一个文件,不做任何改变成功返回。)

  4.16 Symbolic links (符号链接)

  A symbolic link is an indrect pointer to a file, unlike the hard links, which pointed directly to the i-node of the file. (符合链接指向一个文件,不像硬链接指向文件i-node。)

  Hard links normally require that the link and the file reside in the same file system. (影链接通常需要链接和文件在一个文件系统中。)

  Only the superuser can create a hard link to a directory. (只有超级管理员有权限给目录建立影链接。)

  There are no file system limitations on a symbolic link and what it points to, and anyone can create a symbolic link to a directory. (符合链接没有文件系统限制和指向限制,任何人都可以为目录创建链接。)

  Symbolic links are typically used to move a file or an entire directory hierarchy to another location on a system. (符号链接通常用于移动一个文件或整个目录结构到系统的另一个地方。)

  4.17 symlink and readlink Functions ()

  #include <unistd.h>

  int symlink(const char *actualpath, const char *sympath);

                Returns: 0 if OK, -1 on error

  ssize_t readlink(const char *restrict pathname, char *restrick buf, size_t bufsize);

            Returns: number of bytes read if OK, -1 on error

  It is not required that actualpath exist when the symbolic link is created. Also, actualpath and sympath need not reside in the same file system. (创建符号链接时,不要求actualpath一定存在。另外,actualpath和sympath不一定要在一个文件系统中。)

  The readlink function combines the actions of open, read, and close. (readlink函数结合拉open,read,close三个函数。)

  If the function is successful, it returns the number of bytes placed into the buf. (如果函数执行成功,它返回放入buf中的字节数量。)

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