The Erlang docs for file:read_file_info/1 state “file permissions [are] the sum” and “other bits…may be set”, not instilling confidence. And, Google has not been my friend here.
I’m looking to take the mode returned by file:read_file_info/1, e.g. 33188, on a Linux machine and convert it into something more human readable and/or recognizable, like rw-r--r-- or 644.
Any tips, links, or directions greatly appreciated.
The short way:
… or:
For
Mode = 33204these two will give you respectively:["100664"]and["664"].The long way:
This one (function
print/1) forMode = 33204will give you this as result:"rw-rw-r--".If something was unclear for one, I’ll try to expound basic things behind the snippets which I have provided.
As @macintux mentioned already, the
33204in fact is a decimal representation of the octal number 100664. These three lowest octal digits (664) there is probably what you need, and so we get them with bitwise and (band) operation with the highest number which fits in three octal digits (8#777). That’s why short way is so short – you just tell erlang to convertModeto string as if it was the octal number.The second representation you’ve mentioned (like
rw-rw-r--, something thatlsspits out) is easily reproducible from binary representation of theModenumber. Note that three octal digits will give you exactly nine binary digits (8#644 = 2#110110100). In fact this is the stringrwxrwxrwxwhere each element replaced by-if corresponding digit equals0. If digit is1the element remains untouched.So there is slightly cleaner approach to achieve this:
I hope you got the point. Anyway feel free to ask any questions in comments if you doubt.