看起来,可以使用openat()
to re打开一个已经打开的目录。例如,在我的 Linux 系统上我可以执行以下操作:
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
int fd1 = open(".", O_PATH);
if (fd1 == -1) {
perror("open");
return 1;
}
int fd2 = openat(fd1, ".", O_RDONLY);
if (fd2 == -1) {
perror("openat");
close(fd1);
return 1;
}
close(fd1);
// do fancy things with fd2, now opened
// with access mode read-only
return 0;
}
我在任何地方都找不到这个记录,感觉有点像边缘案例。我也没有找到其他代码执行此操作。这是明确定义的行为吗?
编辑:更改了标题:文件->目录
这与调用相同open
在同一个文件上两次,您可以这样做:
int fd1 = open("filename", flags1);
int fd2 = open("filename", flags2);
where filename
引用现有文件(任何类型)并且flags1
and flags2
是任意一组O_
可以有效应用于该类型文件并且不会破坏其内容的标志。 (特别是,我们假设它们不包括O_CREAT
, O_TRUNC
, or O_EXCL
.)
fd1
and fd2
将引用单独的“打开文件描述 https://unix.stackexchange.com/a/195097/21167“,例如lseek
一个不会影响另一个,flock
上一个会阻塞flock
另一边等等
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)