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
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.
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).
You are likely running under a umask restriction
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. 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).
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.
精彩评论