`

unix环境高级编程-4.15-link,unlink,remove和rename函数

 
阅读更多

如上节所述,任何一个文件可以有多个目录项指向其i节点。创建一个指向现有文件的链接的方法是使用link函数。


若成功返回0,出错则返回-1;

int link (const char *oldname, const char *newname) [Function]
The link function makes a new link to the existing file named by oldname, under the
new name newname.
This function returns a value of 0 if it is successful and -1 on failure. In addition
to the usual file name errors (see Section 11.2.3 [File Name Errors], page 224) for
both oldname and newname, the following errno error conditions are defined for this
function:
EACCES You are not allowed to write to the directory in which the new link is to
be written.
EEXIST There is already a file named newname. If you want to replace this link
with a new link, you must remove the old link explicitly first.
EMLINK There are already too many links to the file named by oldname. (The
maximum number of links to a file is LINK_MAX; see Section 31.6 [Limits
on File System Capacity], page 770.)
ENOENT The file named by oldname doesn’t exist. You can’t make a link to a file
that doesn’t exist.
ENOSPC The directory or file system that would contain the new link is full and
cannot be extended.
EPERM In the GNU system and some others, you cannot make links to directories.
Many systems allow only privileged users to do so. This error is used to
report the problem.

EROFS The directory containing the new link can’t be modified because it’s on
a read-only file system.
EXDEV The directory specified in newname is on a different file system than the
existing file.
EIO A hardware error occurred while trying to read or write the to filesystem.

此函数创建一个新目录项newpath,他引用现有的目录项文件,如果newpath已经存在,则返回出错。只创建newpath中的最后一个分量,路径中的其他不部分应当已经存在。

创建新目录项和增加链接计数应当是个原子操作。虽然POSIX.1允许实现支持跨文件系统的链接,但是大多数实现要求这两个路径名在同一个文件系统中。如果实现支持创建指向一个穆拉德硬链接。那么也仅限于超级用户才可以这样做。其理由是这样做可能在文件系统中形成的循环。大多数处理文件系统的实用程序都不能处理这种情况。因此很多文件系统实现不允许对于目录硬链接。

为了删除一个现有的目录项,可以调用unlink函数

int unlink (const char *filename) [Function]
The unlink function deletes the file name filename. If this is a file’s sole name, the
file itself is also deleted. (Actually, if any process has the file open when this happens,
deletion is postponed until all processes have closed the file.)
The function unlink is declared in the header file ‘unistd.h’.
This function returns 0 on successful completion, and -1 on error. In addition to the
usual file name errors (see Section 11.2.3 [File Name Errors], page 224), the following
errno error conditions are defined for this function:

EACCES Write permission is denied for the directory from which the file is to be
removed, or the directory has the sticky bit set and you do not own the
file.
EBUSY This error indicates that the file is being used by the system in such a
way that it can’t be unlinked. For example, you might see this error if the
file name specifies the root directory or a mount point for a file system.
ENOENT The file name to be deleted doesn’t exist.
EPERM On some systems unlink cannot be used to delete the name of a directory,
or at least can only be used this way by a privileged user. To avoid such
problems, use rmdir to delete directories. (In the GNU system unlink
can never delete the name of a directory.)
EROFS The directory containing the file name to be deleted is on a read-only file
system and can’t be modified.

此次函数删除目录项,并将由pathname所引用的文件的链接计数减1,如果还有指向该文件的其他链接,则任然可以通过其他链接访问该文件的数据。如果出错,则不对该文件做任何更改。

我们在前边已经提及到为了解除对文件的链接,必须对包含该目录项的目录具有写和执行权限。如果对目录设置了年诸位,则对该目录必须具有写权限,并且具备下面三个条件之一。

  • 拥有该文件
  • 拥有该目录
  • 具有超级用户特权

只有当链接计数减到0的时候,该文件的内容才可以被删除。另一条件也会阻止删除文件的内容,只要有劲唱那个打开了该文件,其内容也不能删除。变关闭一个文件的时候,内核首先检查打开该文件的进程数。如果该数达到了0,然后内核检查器连接输,如果这个数是0,那么就删除该文件的内容。

实例

下面的程序打开一个文件,然后解除对它的链接。执行改成的进程然后休眠15秒钟,接着终止。


运行结果如图:

unlink的这种性质京城被程序用来确保及时是在该程序崩溃的时候他所创建的临时文件也会不会遗留下来。进程open或者creat创建一个文件然后立即调用unlink。因为该文件仁就是打开的。所以不会讲内容删除。

只有当进程关闭该文件或终止的时候,该文件的内容才会被删除。

如果pathname是符号连接,那么unlink阐述该符号链接。而不会删除有该链接所引用的文件。给出符号连接名情况下,没有一个函数能删除由该链接所引用的文件。

超级用户可以调用unlink,其参数pathname,指定一个目录,但是通常是应当使用rmdir函数,而不是使用这种范式。我们也可以是使用rmove函数解除对一个文件或目录的链接,对于稳健remove的功能与unlink相同,对于目录,remove与rmdir相同。


int remove(const char* pathname)

int remove (const char *filename) [Function]
This is the ISO C function to remove a file. It works like unlink for files and like
rmdir for directories. remove is declared in ‘stdio.h’.

文件或者目录用rename函数更名。

下面是GNU C手册

int rename (const char *oldname, const char *newname) [Function]
The rename function renames the file oldname to newname. The file formerly accessible
under the name oldname is afterwards accessible as newname instead. (If the
file had any other names aside from oldname, it continues to have those names.)
The directory containing the name newname must be on the same file system as the
directory containing the name oldname.
One special case for rename is when oldname and newname are two names for the
same file. The consistent way to handle this case is to delete oldname. However,
in this case POSIX requires that rename do nothing and report success—which is
inconsistent. We don’t know what your operating system will do.

If oldname is not a directory, then any existing file named newname is removed during
the renaming operation. However, if newname is the name of a directory, rename fails
in this case.
If oldname is a directory, then either newname must not exist or it must name a
directory that is empty. In the latter case, the existing directory named newname is
deleted first. The name newname must not specify a subdirectory of the directory
oldname which is being renamed.
One useful feature of rename is that the meaning of newname changes “atomically”
from any previously existing file by that name to its new meaning (i.e., the file that
was called oldname). There is no instant at which newname is non-existent “in
between” the old meaning and the new meaning. If there is a system crash during
the operation, it is possible for both names to still exist; but newname will always be
intact if it exists at all.
If rename fails, it returns -1. In addition to the usual file name errors (see
Section 11.2.3 [File Name Errors], page 224), the following errno error conditions
are defined for this function:
EACCES One of the directories containing newname or oldname refuses write permission;
or newname and oldname are directories and write permission
is refused for one of them.
EBUSY A directory named by oldname or newname is being used by the system in
a way that prevents the renaming from working. This includes directories
that are mount points for filesystems, and directories that are the current
working directories of processes.

ENOTEMPTY
EEXIST The directory newname isn’t empty. The GNU system always returns
ENOTEMPTY for this, but some other systems return EEXIST.
EINVAL oldname is a directory that contains newname.
EISDIR newname is a directory but the oldname isn’t.
EMLINK The parent directory of newname would have too many links (entries).
ENOENT The file oldname doesn’t exist.
ENOSPC The directory that would contain newname has no room for another entry,
and there is no space left in the file system to expand it.
EROFS The operation would involve writing to a directory on a read-only file
system.
EXDEV The two file names newname and oldname are on different file systems.

根据oldname是指定文件还是目录,有几种情况要加以说明,我们也应该说明如果newname已经存在将会发生什么。

(1) 如果oldname值得是一个文件而不是目录,那么该文件或符号链接更名,在这种情况下,如果newname已经存在,则他不能饮用一个目录。如果newname已经存在,而且不是一个目录,则先将该目录项删除,然后将oldname更名为newname,对包含oldname的目录以及包含的newname的目录,调用进程必须要具有写权限,因为将更改这两个目录。

(2)如果oldname 指的是一个目录,那么为该目录更名。过newname已经存在,则它必须引用一个目录,而且该目录应的那个是空目录。

(3)如若oldname或newname引用符号链接,则处理的是符号链接本身,而不是他所引用的文件。

(4)作为一个特例,如果oldname和newname引用同一文件,则函数不做任何改动二成功返回。

更多内容请访问:http://blog.csdn.net/wallwind

分享到:
评论

相关推荐

    UNIX环境高级编程

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程第二版

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程.pdf

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程(中文版+英文版+源代码)

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程(第四章)

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程_第2版.part2

    4.15 link、unlink、remove和rename 函数89 4.16 符号链接91 4.17 symlink和readlink函数94 4.18 文件的时间94 4.19 utime函数95 4.20 mkdir和rmdir函数97 4.21 读目录98 4.22 chdir、fchdir和getcwd函数102...

    UNIX环境高级编程(PDF)

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    Unix环境高级编程电子书

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程中文版

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 84...

    UNIX环境高级编程和源代码

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程_第二版中文

    4.15 link、unlink、remove和rename函数  4.16 符号链接  4.17 symlink和readlink函数  4.18 文件的时间  4.19 utime函数  4.20 mkdir和rmdir函数  4.21 读目录  4.22 chdir、fchdir和getcwd函数  ...

    UNIX环境高级编程 不扣分哦

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    UNIX环境高级编程(第二版中文)

    4.15 link、unlink、remove和rename函数 89 4.16 符号链接 91 4.17 symlink和readlink函数 94 4.18 文件的时间 94 4.19 utime函数 95 4.20 mkdir和rmdir函数 97 4.21 读目录 98 4.22 chdir、fchdir和...

    UNIX环境高级编程(第二版中文).pdf

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

    中文第一版-UNIX环境高级编程

    4.15 link, unlink, remove和rename 函数 71 4.16 符号连接 73 4.17 symlink 和readlink函数 76 4.18 文件的时间 76 4.19 utime函数 78 4.20 mkdir和rmdir函数 79 4.21 读目录 80 4.22 chdir, fchdir和getcwd函数 ...

Global site tag (gtag.js) - Google Analytics