Open()命令不使用正确的权限创建文件
我想运行open()命令,以便如果不存在,则可以创建一个文件。就手册而言,您应该使用o_creat
标志运行它,然后需要一个附加参数,以指定新创建的文件的权限。
我的问题是,它不会给出Outfile
正确的权限。当我运行此代码时:
int get_fd_outfile(char *filename)
{
int fd_outfile;
fd_outfile = open(filename, O_RDWR | O_TRUNC | O_CREAT, 0666); // permissions right?
if (fd_outfile == -1)
pipex_error("Error: Could not open output file for writing");
return (fd_outfile);
}
我将获得一个具有以下权限的文件: -rw-r--r-- [...] Outfile
。但是我希望它已经为用户,组和其他人读/写。为什么八分代代码0666
创建正确的权限?但是o _ [...]
标志确实可以工作...
编辑:也许是因为我在Mac上?我读了一些有关...
I want to run the open() command so that it creates a file if it doesn't exist. As far as the manual goes, you should run it with the O_CREAT
flag which then requires an additional argument that specifies the permissions for the newly created file.
My problem is, that it doesn't give outfile
the right permissions. When I run this code:
int get_fd_outfile(char *filename)
{
int fd_outfile;
fd_outfile = open(filename, O_RDWR | O_TRUNC | O_CREAT, 0666); // permissions right?
if (fd_outfile == -1)
pipex_error("Error: Could not open output file for writing");
return (fd_outfile);
}
I get a file with the following permissions:-rw-r--r-- [...] outfile
. But I want it to have read/write for user, group and others. Why doesn't the octal code 0666
create the right permissions? But the O_[...]
flags do actually work...
Edit: Maybe it's because I'm on a Mac? I read something about that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下您的
umask
值,它还在以下角色中发挥作用:来自
A Mac:
您可以使用
umask
命令来查看当前值。这就是我的样子:掩盖了组和其他人的
w
,并匹配您所看到的内容。您可以使用umask()
系统调用将其在C程序中更改,或chmod()
之后结果文件。Take a look at your
umask
value, it also plays a role in this:From https://www.man7.org/linux/man-pages/man2/open.2.html
Or from
man 2 open
on a Mac:You can use the
umask
command to see your current value. This is what it looks like for me:That masks out the
w
for group and other, and matches what you're seeing. You can use theumask()
system call to change it within the C program, temporarily, orchmod()
the resulting file afterward.