link, linkat, unlink, unlinkat, and remove Functions
2016-02-14 14:11
344 查看
Linux 4.2.0
原型如下:
If
This new name may be used exactly as the old one for any operation;both names refer to the same file (and so have the same permissions and ownership) and it is impossible to tell which name was the “original”.
If the pathname given in
If oldpath is relative and olddirfd is the special value AT_FDCWD, then oldpath is interpreted relative to the current working directory of the calling process (
If oldpath is absolute, then olddirfd is ignored.
The interpretation of
大部分实现都需要路径名在同一个文件系统上,尽管POSIX.1允许实现支持
这些系统调用会移除目录条目(
As mentioned earlier, to unlink a file, we must have
• Own the file
• Own the directory
• Have superuser privileges
仅仅当
如果
1.
2. clear:
sleep(15),用于睡眠15s,定义在unistd中。
This property of unlink is often used by a program to ensure that a temporary file it creates won’t be left around in case the program crashes. The process creates a file using either
If
The superuser can call
remove - remove a file or directory
link, linkat - make a new name for a file
在之前的章节,我们知道一个文件可以有多个dirctory entries指向它的
i-node。我们可以使用
link , linkat来创建已经存在文件的链接(link)
原型如下:
#include <unistd.h> int link(const char *oldpath, const char *newpath); #include <fcntl.h> /* Definition of AT_* constants */ #include <unistd.h> int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags); //Return: 0 if OK, -1 on error
link
link() creates a new link (also known asa hard link) to an existing file.
If
newpathexists, it will not be overwritten.
This new name may be used exactly as the old one for any operation;both names refer to the same file (and so have the same permissions and ownership) and it is impossible to tell which name was the “original”.
linkat
Thelinkat()system call operates in exactly the same way as
link(), except for the differences described here.:
If the pathname given in
oldpathis relative, then it is interpreted relative to the directory referred to by the file descriptor
olddirfd(rather than relative to the current working directory of the calling process, as is done by link() for a relative pathname).
If oldpath is relative and olddirfd is the special value AT_FDCWD, then oldpath is interpreted relative to the current working directory of the calling process (
like link()).
If oldpath is absolute, then olddirfd is ignored.
The interpretation of
newpathis as for
oldpath, except that a relative pathname is interpreted relative to the directory referred to by the file descriptor
newdirfd.
flags argument
如果flag是AT_SYMLINK_FOLLOW,创建的
link是用于
symbolic link指向的目标。如果
flag被清除,则
link用于
symblic link本身。
Besides
directory entry的新建和
link count的增加必须是原子操作。
大部分实现都需要路径名在同一个文件系统上,尽管POSIX.1允许实现支持
link能跨越文件系统。支持创建
directories的
hard link(硬链接)的实现,都只能让超级用户进行该项操作。这条限制(constraint)是因为
hard link可能在文件系统中造成循环(loops),这是文件系统不法处理的。(在4.17节我们会展示symbolic link的loop实例)。因此,许多文件系统不允许
(目录)directories的硬链接(hard link)。
unlink, unlinkat - delete a name and possibly the file it refers to
原型:#include <unistd.h> int unlink(const char *pathname); #include <fcntl.h> /* Definition of AT_* constants */ #include <unistd.h> int unlinkat(int dirfd, const char *pathname, int flags); //both Return: 0 if OK, -1 on error
这些系统调用会移除目录条目(
directory entry)并且减少
pathename引用的文件的链接数目
link count。如果文件还有其他的链接(link),文件的内容还可以通过其他链接访问。如果产生
error,文件不会发生变化。
As mentioned earlier, to unlink a file, we must have
write permissionand
execute permissionin the directory containing the directory entry, as it is the directory entry that we will be removing. Also, as mentioned in Section 4.10, if
the sticky bitis set in this directory we must have
write permissionfor the directory and meet one of the following criteria:
• Own the file
• Own the directory
• Have superuser privileges
仅仅当
link count达到0,文件的内容才会被删除。另一种阻止文件内容被删除的条件就是,一些进程
open了该文件,文件的内容也不会被删除。当某文件被关闭了(closed),内核会先确定打开(
open)该文件的进程数量,如果为0,此时内核会继续确定
link count,如果还为0,文件内容被删除。
unlinat
pathname为相对路径,
unlinkat会将其看做
fd开的目录的相对路径。
如果
fd值为
AT_FDCWD时,
pathname就是相对路径,相对于调用进程的当前工作目录。如果
pathname是绝对路径,
fd直接被忽略。
flag
用于改变unlinkat默认行为。
1.
AT_REMOVEDIR: 用于删除目录,类似于使用
rmdir命令。
2. clear:
unlinkat操作和
unlink一致。
Example
#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> int main() { if(open("tempfile", O_RDWR|O_CREAT, S_IRWXO) < 0) { fprintf(stderr, "open file error %s\n", strerror(errno)); exit(-1); } if(unlink("tempfile") < 0) { fprintf(stderr, "unlink file error %s\n", strerror(errno)); exit(-1); } printf("file unlinked\n"); sleep(15); printf("done\n"); return 0; }
sleep(15),用于睡眠15s,定义在unistd中。
This property of unlink is often used by a program to ensure that a temporary file it creates won’t be left around in case the program crashes. The process creates a file using either
openor
creatand then immediately calls
unlink. The file is not deleted, however, because it is still open. Only when the process either closes the file or terminates, which causes the kernel to close all its open files, is the file deleted.
If
pathnameis a
symbolic link,
unlinkremoves the symbolic link, not the file referenced by the link. There is no function to remove the file referenced by a symbolic link given the name of the link.
The superuser can call
unlinkwith pathname specifying a directory if the file system supports it, but the function
rmdirshould be used instead to unlink a directory. We describe the
rmdirfunction in Section 4.21.
remove
We can alsounlink a file or a directorywith the
removefunction. For a file, remove is identical to unlink. For a directory, remove is identical to
rmdir.
remove - remove a file or directory
#include <stdio.h> int remove(const char *pathname); //Returns: 0 if OK, -1 on error
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- centos中MySQL连接及基本信息查看命令小结
- 【Xamarin挖墙脚系列:使用Xamarin进行Hybrid应用开发】
- 国内常用NTP服务器地址及IP
- POJ-Best Cow Line
- 《第一行代码》目录
- XML读写之DOM
- BZOJ 4352: Tower|暴力
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- LVM的使用与管理
- Ace 动画应用实例 ------启动欢迎界面
- c# 接口和抽象方法的学习
- jquery 关于 4 种提交的部分归纳
- 网站性能提升思路
- 动态链接库的创建和使用
- 重载和重写的区别
- 获取屏幕宽度
- java学习初体验之课后习题
- 帮舍友解决的电脑问题(由基础看思维)
- JSON 之FastJson解析