Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the argument of os.umask() inverted? (umask 0o000 makes chmod 0o777)

In most places, permissions are defined as an octal number in the format of 0777. But UNIX's umask command (thus os.umask()) needs 0o000 to produce the permission bits of 0o777 and 0o022 equals to 0o755 in my understanding.

I heard that UNIX's umask is inverted for some reason and I do not understand the reason behind it. Could someone please explain this inconsistancy?

like image 821
ddinchev Avatar asked Jul 02 '12 12:07

ddinchev


People also ask

Why is umask inverted?

Using this example, umask 777 creates a file with chmod 000 , umask 112 will be equal to chmod 664 . As far as I know, this happened because the umask command was originally created to indicate what permission bits the file will NOT have after it's created (hence the invertion).

What is the difference between umask and chmod?

The difference between umask and chmod is that umask changes the default permissions and thus the permissions for all newly created files and folders, while chmod sets permissions for files and folders that already exist.

Why is umask 0022?

A umask of 022 allows only you to write data, but anyone can read data. A umask of 077 is good for a completely private system. No other user can read or write your data if umask is set to 077. A umask of 002 is good when you share data with other users in the same group.

What is OS umask?

umask() method in Python is used to set the current numeric umask value and get the previous umask value. umask stands for user file-creation mode mask. This is used to determine the file permission for newly created files or directories. Syntax: os.umask(mask)


1 Answers

There is no real inconsistency, as the relation between umask and chmod can purely be written down with equations. Apparently, umask sets the opposite of chmod, it was created like this back in the old days.

Example: 022 (the default usual umask) creates 755. It works like this:

  • 7 - 0 = 7 becomes the first byte
  • 7 - 2 = 5 becomes the second and third bytes

Using this example, umask 777 creates a file with chmod 000, umask 112 will be equal to chmod 664. As far as I know, this happened because the umask command was originally created to indicate what permission bits the file will NOT have after it's created (hence the invertion).

While it could be annoying, it's really not hard to get used to it. Just think how you would chmod your files, and subtract the byte you want from 7, and you will get the umask value. Or, when you are at the IDE, writing your code, don't use umask, but rather create the file (with the default umask of course) and then use, in Python, os.chmod() instead.

like image 161
Whisperity Avatar answered Oct 09 '22 23:10

Whisperity