如何使用Erlang文件:read_file_info权限/模式信息? [英] How to use Erlang file:read_file_info permissions/mode info?
问题描述
file:read_file_info/1
的Erlang文档指出文件许可权是和"和其他位...可以被设置",而不是灌输信心.而且,Google并不是我的朋友.
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.
我希望采用file:read_file_info/1
返回的模式,例如33188
,在Linux机器上,并将其转换为更易于理解和/或识别的内容,例如rw-r--r--
或644
.
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.
推荐答案
简而言之:
io_lib:format("~.8B", [Mode]).
...或:
io_lib:format("~.8B", [Mode band 8#777]).
对于Mode = 33204
,这两个将分别为您提供:["100664"]
和["664"]
.
For Mode = 33204
these two will give you respectively: ["100664"]
and ["664"]
.
很长的路要走
print(Mode) ->
print(Mode band 8#777, []).
print(0, Acc) when length(Acc) =:= 9 ->
Acc;
print(N, Acc) ->
Char = perm(N band 1, length(Acc) rem 3),
print(N bsr 1, [Char | Acc]).
perm(0, _) ->
$-;
perm(1, 0) ->
$x;
perm(1, 1) ->
$w;
perm(1, 2) ->
$r.
Mode = 33204
的这个(功能print/1
)将为您提供结果:"rw-rw-r--"
.
This one (function print/1
) for Mode = 33204
will 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.
正如@macintux已经提到的,33204
实际上是八进制数100664的十进制表示形式.这三个最低的八进制数字(664
)可能是您所需要的,因此我们按位和具有最大数字的(band
)操作,该数字适合三个八进制数字(8#777
).这就是为什么短途之路如此之短的原因-您只是告诉erlang将Mode
转换为字符串,就好像它是八进制数字一样.
As @macintux mentioned already, the 33204
in 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 convert Mode
to string as if it was the octal number.
您提到的第二个表示形式(如rw-rw-r--
,是ls
吐出的东西)可以很容易地从Mode
数字的二进制表示形式再现.请注意,三个八进制数字将恰好为您提供九个二进制数字(8#644 = 2#110110100
).实际上,这是字符串rwxrwxrwx
,如果相应的数字等于0
,则每个元素都用-
替换.如果digit是1
,则该元素保持不变.
The second representation you've mentioned (like rw-rw-r--
, something that ls
spits out) is easily reproducible from binary representation of the Mode
number. Note that three octal digits will give you exactly nine binary digits (8#644 = 2#110110100
). In fact this is the string rwxrwxrwx
where each element replaced by -
if corresponding digit equals 0
. If digit is 1
the element remains untouched.
因此,有一种更简洁的方法可以实现这一目标:
So there is slightly cleaner approach to achieve this:
print(Mode) ->
print(Mode band 8#777, lists:reverse("rwxrwxrwx"), []).
print(0, [], Acc) ->
Acc;
print(N, [Char0 | Rest], Acc) ->
Char = char(N band 1, Char0),
print(N bsr 1, Rest, [Char | Acc]).
char(0, _) ->
$-;
char(1, C) ->
C.
我希望你明白这一点.如果您有任何疑问,请随时在评论中提出任何问题.
I hope you got the point. Anyway feel free to ask any questions in comments if you doubt.
这篇关于如何使用Erlang文件:read_file_info权限/模式信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!