如何忽略umask以创建具有给定权限的文件

发布于 2025-01-07 07:59:57 字数 663 浏览 16 评论 0原文

我正在使用 open 函数并使用 O_CREAT | 创建一个文件O_EXCEL。我已将模式传递为“0666”。但通过屏蔽最终分配给它的权限是 -rw-r--r-- 而不是 -rw-rw-rw- 。有人告诉我我可以使用 umask (011),然后再次重置原始掩码。 但我不知道如何在 C++ 程序中传递它。这是我正在做的事情的一小段。

   # include <iostream>
   # include <stdio.h>
   # include <conio.h>
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>

   using namespace std;

   int main()
   {
    int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);   
    getch();
    return 0;   
  } 

使用权限 -rw-r--r-- 创建文件 C:\Users\Ritesh\Music\music.txt 我希望它是 -rw -rw-rw-

I am creating a file using open function and using O_CREAT | O_EXCEL . I have passed the mode as "0666" . But by masking finally the permission allotted to it is -rw-r--r-- and not the
-rw-rw-rw- . Someone told me i can use umask (011) and then reset the original mask again .
But i dont know how to pass this in c++ program. This is the small snippet of What i am doing .

   # include <iostream>
   # include <stdio.h>
   # include <conio.h>
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>

   using namespace std;

   int main()
   {
    int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);   
    getch();
    return 0;   
  } 

creates file C:\Users\Ritesh\Music\music.txt with permission -rw-r--r-- . I want it to be -rw-rw-rw-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一笔一画续写前缘 2025-01-14 07:59:57
mode_t old_mask;

old_mask = umask(011);
open( ... );
umask(old_mask);
mode_t old_mask;

old_mask = umask(011);
open( ... );
umask(old_mask);
碍人泪离人颜 2025-01-14 07:59:57

umask 表示默认情况下您不想授予文件的权限。因此,如果你想在创建文件时完全控制权限,请将umask设置为0,这告诉操作系统不要保留任何权限,让你做主。像这样:

int main()
{
    mode_t oldmask = umask(0);
    int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
    close(fd);
    umask(oldmask);
    getch();
    return 0;   
} 

umask means permissions that you don't want to give to files by default. So if you want to control the permissions completely while creating a file, set umask to 0, which tells the os don't reserve any permissions and let you call the shot. like this:

int main()
{
    mode_t oldmask = umask(0);
    int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
    close(fd);
    umask(oldmask);
    getch();
    return 0;   
} 
迷离° 2025-01-14 07:59:57

将文件权限设置为您想要的唯一线程安全方法是使用 chmod()fchmod() 创建文件后(示例没有错误检查):

int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
fchmod(fd, 0666 );

如果您使用umask(),您将更改整个过程的umask值。如果任何其他线程正在运行,您将面临使用意外权限创建文件的风险,这可能会导致安全问题或其他问题。当您更改的 umask 值生效时创建的任何子进程都将使用意外的 umask 值创建。

The only thread-safe way to set file permissions to be what you want is to set them explicitly with chmod() or fchmod() after creating the file (example without error checking):

int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
fchmod(fd, 0666 );

If you use umask(), you will change the umask value for the entire process. If any other threads are running you risk files getting created with unexpected permissions, which could lead to a security issue or other problems. And any child process created while your changed umask value is in effect will be created with an unexpected umask value.

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