12

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?

Whisperity
  • 2,904
  • 1
  • 15
  • 35
ddinchev
  • 29,115
  • 26
  • 82
  • 124
  • 3
    It's a "mask" -- that's what the word means in this context; something that lets only the unset bits through (think of the masks used for painting, chipmaking, and the like). If it didn't operate this way, it would be called something else. – Charles Duffy Jul 02 '12 at 14:13

1 Answers1

12

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.

Eric Finn
  • 7,726
  • 3
  • 29
  • 42
Whisperity
  • 2,904
  • 1
  • 15
  • 35
  • "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)." Wonderful answer! Now I can live peacefuly! :) – ddinchev Jul 02 '12 at 13:08
  • A "mask" is something you "mask off" or "remove". That's why we use the bitwise AND ("&" in C) to "mask" things. – Fixee Dec 02 '12 at 00:45