You probably don’t care about Unix File Mode & octmode bitwise comparison but let’s talk about it anyway
For some reason during the summer holidays I was browsing the R source code and stumbled upon this piece of code in check_permissions.
## Phase A. Directories at least 700, files at least 400.
if (length(allfiles)) {
mode <- file.mode(allfiles)
bad_files <- allfiles[(mode & "400") < as.octmode("400")]
}And at that exact moment, (mode & "400") < as.octmode("400") became a mystery I needed to understand. (And yes, I ended up going to bed later than expected because of that).
File mode
If you’re unfamiliar with Unix file mode, it’s a way to describe what a user can do with a file (read, write, or execute), depending on its identity: does he own the file and is he a member of the same group as the file owner.
It’s usually represented either with a series of characters, for example rwxrw-r--, where:
- The first three characters represent what the file owner can do (here
read,write, and execute). - The next three what the users of the same group as the owner can do (here only read and write).
- The last three what everybody on the machine can do with the file (here, only read).
It can also be represented as 764, which is an octal notation of the permission, where read is 4, write is 2 and execute is 1. The number represented is the sum of the permissions (7 is rwx, so 4 + 2 + 1, 6 read and write, 5 read and execute… you get the point).
I’ve always wondered why these numbers (4, 2 and 1). So let’s dive into that rabbit hole.
What is the octal notation?
We live in a world that counts in base 10. That is to say, we go from 1 to 10, then 11 to 20, etc. I’m pretty sure I don’t need to develop on that.
But there are other ways to count. For example, binary, which is a base 2 system, i.e. you only have 2 symbols to represent numbers, 0 and 1. So you start with the first number, which is 0. Then use the next available sign, 1. Then you’re out of available signs, so move to 10. Then 11. Then you’re again out of available signs, so 100. And 101, 110, 111…
Anyway, octal is a base 8 system that uses the numbers 0 to 7 to count, and can be represented as triplets of 0 and 1 in binary:
- 000 is 0
- 001 is 1
- 010 is 2
- 011 is 3
- 100 is 4
- 101 is 5
- 110 is 6
- 111 is 7
Meaning that our file mode can be expressed as a triplet of bits: 764 being 111 110 100, a bit attached to the file that describes the permission.
And here is the answer to our “why 4, 2 and 1” question: each permission is one bit of the triplet. Read is the first bit, so 100, which is 4. Write is the second one, 010, which is 2. And execute is the last one, 001, which is 1.
So when you “sum” the permissions, what you’re really doing is switching bits on. rw- is 100 + 010, so 110, so 6. r-x is 100 + 001, so 101, so 5. You get the point.
Back to our (mode & "400") < as.octmode("400")
This piece of code was very weird to me. The comment says files at least 400.
But this broke my brain (mode & "400"). Why the &? Why the “400”?
Well, turns out it’s because this code is doing bitwise comparison. You never knew you needed it, me neither, but here it is: & on octmode object will compare elements bit by bit, and return another series of bits.
Bitwise comparison is quite simple to understand: if 1 & 1 => 1, otherwise 0.
And it turns out that all permissions < 400 will return 0 if & with 400.
400 = 100 000 000
So every permission that doesn’t start with something more than 100 (4), will be zero.
For example:
400 = 100 000 000
&
377 = 011 111 111
-------------------
000 = 000 000 000
On the other hand, every permission >= 400 will start with 1xx xxx xxx so will always be != 0 because
400 = 100 000 000
??? = 1xx xxx xxx
-------------------
400 = 100 000 000
So basically (mode & "400") is a way to do some triage, so that every perm below 400 will be converted to 0, and the others to 400.
< as.octmode("400")
Now that we know what (mode & "400") does, the end of the expression is pretty straightforward.
After the &, we only have two possible values: 000 or 400. And < as.octmode("400") is TRUE only for 000, i.e. for the files the owner can’t read.
modes <- as.octmode(c("764", "400", "377", "644"))
modes & "400"## [1] "400" "400" "000" "400"
(modes & "400") < as.octmode("400")## [1] FALSE FALSE TRUE FALSE
These are the files that end up in bad_files, and if there are any, R CMD check throws an error: “Found the following files with insufficient permissions”.
Why not just mode < 400?
At this point you might wonder (I did): why bother with the &? Why not just do mode < as.octmode("400")?
Well, because the & isolates one bit (the “owner can read” one), and ignores all the others. On the other hand, mode < as.octmode("400") compares the whole number, including bits that have nothing to do with reading the file.
Let’s take 1377. That’s our 377 from before, plus a leading 1: the sticky bit, one of the “special” permissions that can be put in front of the three triplets. The owner still can’t read the file, but as a number, it’s bigger than 400:
mode <- as.octmode("1377")
(mode & "400") < as.octmode("400")## [1] TRUE
mode < as.octmode("400")## [1] FALSE
So the direct comparison would have missed this file.
Directories: at least 700
Remember the comment at the top of the code: “Directories at least 700, files at least 400”. And yes, a few lines below in check.R, the very same trick is used for directories, this time with "700":
if (length(alldirs <- unique(dirname(allfiles)))) {
mode <- file.mode(alldirs)
bad_files <- c(bad_files,
alldirs[(mode & "700") < as.octmode("700")])
}700 is 111 000 000, so the & keeps the three bits of the owner, and drops the rest. This time, the result can be anything between 000 and 700, and the only way to get 700 is to have the three bits on. So < as.octmode("700") is TRUE as soon as the owner misses at least one of read, write or execute:
x <- as.octmode(c("000", "100", "200", "300", "400", "500", "600", "700"))
(x & "700") < as.octmode("700")## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
That’s the general idea behind (mode & x) < x: mode & x can never be bigger than x, so this comparison is a way to say “not all the bits from x are on”. (And you’ll find it a third time a bit further down, with "500" for the configure and cleanup scripts.)
Trying it in R
Let’s play with this on a real file. tempfile() only gives us a path, so we need to create the file first:
tmp <- tempfile()
file.create(tmp)## [1] TRUE
file.mode(tmp)## [1] "644"
(The default mode depends on your system, here it’s 644.)
The owner can read it, so our file is fine:
(file.mode(tmp) & "400") < as.octmode("400")## [1] FALSE
Now let’s change the mode with Sys.chmod(), and only keep the write permission for the owner:
Sys.chmod(tmp, "200")
file.mode(tmp)## [1] "200"
(file.mode(tmp) & "400") < as.octmode("400")## [1] TRUE
And voilà, this file would now end up in bad_files.
Conclusion
To sum up:
- I’m definitely a big nerd.
- You probably still don’t care about bitwise comparison.
But hey, next time you stumble upon a & between two file modes, you’ll know what’s going on 🤘