Question

I was reading about file creation mask in a book and came across this:

For example, if the value of the mask is 04+02+01=07 then the permissions normally indicated by these values are turned off whenever a file is created. So, with this value in effect, the file created with the statement:

fd open (" /tmp/newfile", O_CREAT, 0644); 

will actually be given a mode of 0640. This means that the file owner and members of the group associated with the file will be able to use the file, but other types of user will be denied any access at all.

I'm can't wrap my head around as to why does the mode changes from 0644 to 0640. And what will be the permission be of the file being created?

Was it helpful?

Solution

The value you passed to *open() (call it mode) is modified in this way:

perms = mode & ~umask;

Meaning every bit in umask that is set turns off the corresponding bit in the resulting permission, regardless of whether it was set or not. In your particular example:

0644       = 0b110100100
  07       = 0b000000111
 ~07       = 0b111111000
0644 & ~07 = 0b110100000 = 0640
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top