系统调用 creat 的奇怪行为
我正在创建一个文件,如下所示
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你自己说了,你的umask是022,所以省略了写权限;从创建手册页:
You said it yourself, you have a umask of 022, so the write permission is omitted; From the creat man page:
您可能在 umask 限制下运行
应该会让您走上正确的轨道
You are likely running under a umask restriction
should get you onto the right track
umask 与您在
creat
调用中指定的文件权限以这种方式组合起来file_perms & ~umask
以获取实际为文件设置的权限位。 umask 基本上指定了哪些位不应被设置,即使您请求设置它们。chmod
调用会忽略 umask。而且您的目录权限在这里无关紧要(好吧,并非完全无关,如果您对正在创建文件的目录没有写权限,它将完全失败)。The umask is combined with the file permissions you specify in the
creat
call in this wayfile_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).umask 中的权限从 mode 参数到
open
和mkdir
(以及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
andmkdir
(andcreat
and friends, similarly). For example, if themode
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 beS_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
(0644)So, you are correctly seeing write access not being given to the directory you're creating.