open(名称,O_CREAT | O_DIRECTORY,模式)的预期行为是什么? [英] What's the expected behavior of open(name, O_CREAT|O_DIRECTORY, mode)?
问题描述
尽管仔细阅读了相关的标准文档,但我可以当用包含O_CREAT|O_DIRECTORY
的标志调用open
系统调用时,不了解在POSIX兼容系统中预期的行为是什么.
Despite a careful read of the related standard documentation, I can't understand what's the expected behavior in POSIX compliant systems when a open
system call is invoked with flags including O_CREAT|O_DIRECTORY
.
标准规定
如果将O_CREAT和O_DIRECTORY设置为,并且,所请求的访问模式既不是O_WRONLY也不是O_RDWR,则结果不确定.
If O_CREAT and O_DIRECTORY are set and the requested access mode is neither O_WRONLY nor O_RDWR, the result is unspecified.
但是,它既没有指定(O_CREAT|O_DIRECTORY|O_WRONLY)
也没有指定(O_CREAT|O_DIRECTORY|O_RDWR)
的系统行为.实际上,据我所知,EISDIR
上的行为仅适用于现有目录.
However it does not specify the behavior of the system with neither (O_CREAT|O_DIRECTORY|O_WRONLY)
nor (O_CREAT|O_DIRECTORY|O_RDWR)
. Indeed (as far as I can understand) the behavior on EISDIR
only apply to existing directories.
在与O_CREATE有关的部分中,标准指定了当命名文件不存在时,
In the section related to O_CREATE, the standard specifies that, when the named file does not exist,
如果未设置O_DIRECTORY ,则应将文件创建为常规文件; [...]
if O_DIRECTORY is not set the file shall be created as a regular file; [...]
但是同样,它也没有指定如果设置O_DIRECTORY
也会发生什么.
but again it does not specify what will happen if O_DIRECTORY
is set too.
我查看了 NetBSD的手册页(众所周知,它非常在乎POSIX的合规性)和 Linux (这是广泛使用的系统,尽管实际上不是POSIX),但我找不到任何澄清.
I've looked the manual pages of both NetBSD (which notoriously cares a lot about POSIX compliance) and Linux (which is a widely used system, despite not actually a POSIX one) but I can't find any clarification.
说未指定两个标志的使用是否正确? 如果是这样,最常见的行为是什么?
Is it correct to say that the use of both flags is unspecified? And if so, what's the most common behavior?
在任何符合POSIX的操作系统上,open(name, O_CREAT|O_DIRECTORY, mode)
是否等同于mkdir
?
Is open(name, O_CREAT|O_DIRECTORY, mode)
equivalent to mkdir
on any POSIX compliant OS?
推荐答案
netbsd本身在vn_open中包含以下内容:
netbsd itself contains the following in vn_open:
if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
return EINVAL;
因此与这2个的任何组合都会被直接拒绝.
so any combination with these 2 is straight up rejected.
在Linux中,它有点毛茸茸,但是任何琐碎的测试都会告诉您目录也没有创建,但是您最终可以得到一个文件
in linux it's a little bit more hairy, but any trivial test will show you that the directory is not created either, but you can end up with a file
对于踢球,我还检查了freebsd,它从未以O_DIRECTORY开头创建任何东西
for kicks i also checked freebsd, which never ends up creating anything with O_DIRECTORY in the first place
如果您正在寻找的是可以将fd退还给您的mkdir,恐怕没有任何东西了.另一方面,您应该可以使用O_DIRECTORY安全地打开任何您已创建的内容.
if what you are looking for is a mkdir which gives you back the fd, i'm afraid there is nothing of the sort. on the other hand you should be able to safely open with O_DIRECTORY anything you mkdir'ed.
这篇关于open(名称,O_CREAT | O_DIRECTORY,模式)的预期行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!