Open()命令不使用正确的权限创建文件

发布于 2025-02-09 08:05:07 字数 643 浏览 1 评论 0原文

我想运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2025-02-16 08:05:07

看一下您的umask值,它还在以下角色中发挥作用:

              The effective mode is modified by the process's umask in
              the usual way: in the absence of a default ACL, the mode
              of the created file is (mode & ~umask).

来自

​A Mac:

     The oflag argument may indicate that the file is to be created if it does not exist (by specifying
     the O_CREAT flag).  In this case, open() and openat() require an additional argument mode_t mode; the
     file is created with mode mode as described in chmod(2) and modified by the process' umask value (see
     umask(2)).

您可以使用umask命令来查看当前值。这就是我的样子:

$ umask
0022

掩盖了组和其他人的w,并匹配您所看到的内容。您可以使用umask()系统调用将其在C程序中更改,或chmod()之后结果文件。

Take a look at your umask value, it also plays a role in this:

              The effective mode is modified by the process's umask in
              the usual way: in the absence of a default ACL, the mode
              of the created file is (mode & ~umask).

From https://www.man7.org/linux/man-pages/man2/open.2.html

Or from man 2 open on a Mac:

     The oflag argument may indicate that the file is to be created if it does not exist (by specifying
     the O_CREAT flag).  In this case, open() and openat() require an additional argument mode_t mode; the
     file is created with mode mode as described in chmod(2) and modified by the process' umask value (see
     umask(2)).

You can use the umask command to see your current value. This is what it looks like for me:

$ umask
0022

That masks out the w for group and other, and matches what you're seeing. You can use the umask() system call to change it within the C program, temporarily, or chmod() the resulting file afterward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文