到底什么……? Python C 代码创建的文件的文件权限
如果我有以下 C 代码:
int main(int argc, char **arg)
{
int x = open("testfilefromc", O_RDWR | O_CREAT);
return 0;
}
当我编译并运行时,它不会无理地创建这个:
-rw-r----- 1 joewass staff 0B 31 Jan 21:17 testfilefromc
但是以下 C 代码,编译成 Python 模块:
const char *filename = "testfilefrompython";
context->fd = open(filename, O_RDWR | O_CREAT);
会这样做:
---------- 1 joewass staff 165B 31 Jan 21:09 testfilefrompython
毫不奇怪,创建该文件的代码下次无法打开它圆形的!
到底为什么要以零权限创建该文件?为什么 C 语言编译成 Python 模块时行为会有所不同?我正在运行 python 程序,该程序以我的身份运行代码。
不管怎样,我稍后会 mmap
该文件。
谢谢!
Joe
编辑:我知道我可以chmod
来解决这个问题,问题是为什么?
编辑2:感谢Rosh Oxymoron指出我错过了不太好的东西-可选的可选参数。 TRWTF 是第一个示例完全有效!
If I have the following C code:
int main(int argc, char **arg)
{
int x = open("testfilefromc", O_RDWR | O_CREAT);
return 0;
}
which when I compile and run not unreasonably creates this:
-rw-r----- 1 joewass staff 0B 31 Jan 21:17 testfilefromc
But the following C code, compiled into a Python module:
const char *filename = "testfilefrompython";
context->fd = open(filename, O_RDWR | O_CREAT);
does this:
---------- 1 joewass staff 165B 31 Jan 21:09 testfilefrompython
And not surprisingly the code that created the file can't open it next time round!
Why on earth would the file be created with zero permissions? And why would the behaviour be different in C compiled into a Python module? I'm running the python program that runs the code as me.
For what it's worth, I'm mmap
ing the file later on.
Thanks!
Joe
EDIT :I know I can chmod
to fix this, the question is why?
EDIT 2: Thanks to Rosh Oxymoron for pointing out that I missed the not-so-optional optional argument. TRWTF is that the first example worked at all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数
open
采用三个参数。如果指定O_CREAT
标志,则需要使用以下签名调用它:否则行为未定义。在第一个示例中创建文件根本不可能起作用。另请查看
umask
,它始终与您指定的模式进行 AND 运算。The function
open
takes three arguments. If you specify theO_CREAT
flag, you need to call it with this signature:Otherwise the behaviour is undefined. It's very unlikely for the creation of the file in your first example to work at all. Also take a look at
umask
which is always ANDed with the mode that you specify.