系统调用 creat 的奇怪行为

发布于 2024-11-07 00:05:58 字数 368 浏览 1 评论 0原文

我正在创建一个文件,如下所示

 int fd  = creat(file_path.c_str() ,S_IRWXU|S_IRWXG|S_IRWXO);

虽然我向所有三个实体提供所有权限,但它会创建具有以下权限的文件。

-rwxr-xr-x

我创建的目录的权限设置为

drwxrwxrwx

Umask

0022

你们能建议一下可能出了什么问题吗?

编辑:我可以 chmod 该文件以赋予它我计划的权限。我很好奇为什么上面的方法失败了。

I am creating a file as follows

 int fd  = creat(file_path.c_str() ,S_IRWXU|S_IRWXG|S_IRWXO);

Though i am providing all permissions to all three entities, it creates the files with the below permission.

-rwxr-xr-x

The directory i am creating this in has permissions set as

drwxrwxrwx

Umask

0022

Can you guys please suggest what could be wrong?

Edit : I can chmod the file to give it the permissions i plan to. I am curious why the above is failing.

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

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

发布评论

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

评论(4

月棠 2024-11-14 00:05:58

你自己说了,你的umask是022,所以省略了写权限;从创建手册页:

有效权限按照通常的方式由进程的umask修改:创建的文件的权限是(mode & ~umask)。

You said it yourself, you have a umask of 022, so the write permission is omitted; From the creat man page:

The effective permissions are modified by the process's umask in the usual way: The permissions of the created file are (mode & ~umask).

野心澎湃 2024-11-14 00:05:58

您可能在 umask 限制下运行

man umask

应该会让您走上正确的轨道

You are likely running under a umask restriction

man umask

should get you onto the right track

攀登最高峰 2024-11-14 00:05:58

umask 与您在 creat 调用中指定的文件权限以这种方式组合起来 file_perms & ~umask 以获取实际为文件设置的权限位。 umask 基本上指定了哪些位不应被设置,即使您请求设置它们。

chmod 调用会忽略 umask。而且您的目录权限在这里无关紧要(好吧,并非完全无关,如果您对正在创建文件的目录没有写权限,它将完全失败)。

The umask is combined with the file permissions you specify in the creat call in this way file_perms & ~umask to arrive at the permission bits that are actually set for the file. The umask basically specifies which bits should not be set even if you request they be set.

The chmod call ignores the umask. And your directory permissions are irrelevant here (well, not totally irrelevant, it would fail completely if you didn't have write permission to the directory you're creating the file in).

鸠魁 2024-11-14 00:05:58

umask 中的权限从 mode 参数到 openmkdir (以及 creat 和朋友,类似地)关闭。例如,如果 mode 参数指定为:

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (0666) 当使用典型 umask 0022 创建文件时,结果将为

S_IRUSR | S_IWOTH (0666) S_IWUSR | S_IRGRP | S_IROTH (0644)

因此,您正确地看到没有向您正在创建的目录授予写入访问权限。

Permissions in the umask are turned off from the mode argument to open and mkdir (and creat and friends, similarly). For example, if the mode argument is specified as:

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (0666) when creating a file with the typical umask of 0022, the result will be

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH (0644)

So, you are correctly seeing write access not being given to the directory you're creating.

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