[英]Strange behaviour from system call creat
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 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. 编辑:我可以chmod文件,以赋予它我计划的权限。 I am curious why the above is failing.
我很好奇为什么以上失败。
You said it yourself, you have a umask of 022, so the write permission is omitted; 您自己说过,您的umask为022,因此省略了写许可权。 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).
有效权限由进程的umask按常规方式修改:创建的文件的权限为(mode&〜umask)。
You are likely running under a umask restriction 您可能在umask限制下运行
man umask
should get you onto the right track 应该让您走上正确的路
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. 将umask与您在
creat
调用中指定的文件许可权结合使用,以这种方式file_perms & ~umask
到达为该文件实际设置的许可权位。 The umask basically specifies which bits should not be set even if you request they be set. umask基本上指定了即使您请求设置哪些位也不应设置的位。
The chmod
call ignores the umask. chmod
调用将忽略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). 而且您的目录权限在这里无关紧要(嗯,不是完全无关紧要的,如果您没有创建文件所在目录的写权限,它将完全失败)。
Permissions in the umask are turned off from the mode argument to open
and mkdir
(and creat
and friends, similarly). umask中的权限已从mode参数关闭,无法
open
和mkdir
(以及creat
和friends,同样)。 For example, if the mode
argument is specified as: 例如,如果将
mode
参数指定为:
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
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_IWGRP | S_IROTH | S_IWOTH
(0666)创建具有典型umask为0022的文件时,结果为
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
(0644) S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
)
So, you are correctly seeing write access not being given to the directory you're creating. 因此,您正确地看到未对正在创建的目录授予写访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.