简体   繁体   English

如何使用Erlang文件:read_file_info权限/模式信息?

[英]How to use Erlang file:read_file_info permissions/mode info?

The Erlang docs for file:read_file_info/1 state "file permissions [are] the sum" and "other bits...may be set", not instilling confidence. 用于file:read_file_info/1的Erlang文档指出“文件许可权是和”和“其他位...可以被设置”,这并不灌输信心。 And, Google has not been my friend here. 而且,Google在这里不是我的朋友。

I'm looking to take the mode returned by file:read_file_info/1 , eg 33188 , on a Linux machine and convert it into something more human readable and/or recognizable, like rw-r--r-- or 644 . 我希望在Linux机器上采用file:read_file_info/1返回的模式,例如33188 ,并将其转换为更易于file:read_file_info/1和/或识别的东西,例如rw-r--r--644

Any tips, links, or directions greatly appreciated. 任何提示,链接或指示,我们将不胜感激。

The short way: 简短的方法:

io_lib:format("~.8B", [Mode]).

... or: ... 要么:

io_lib:format("~.8B", [Mode band 8#777]).

For Mode = 33204 these two will give you respectively: ["100664"] and ["664"] . 对于Mode = 33204这两个将分别给您: ["100664"]["664"]

The long way: 很长的路要走:

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.

This one (function print/1 ) for Mode = 33204 will give you this as result: "rw-rw-r--" . Mode = 33204该代码(功能print/1 )将为您提供结果: "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 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 ). 正如@macintux所提到的, 33204实际上是八进制数100664的十进制表示形式。这三个最低的八进制数字( 664 )可能是您所需要的,因此,我们使用最高的按位和( band )操作来获取它们。可以容纳三个八进制数字的数字( 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. 这就是为什么短途之路如此之短的原因-您只是告诉erlang将Mode转换为字符串,就好像它是八进制数字一样。

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. 您提到的第二个表示形式(如rw-rw-r--ls吐出的东西)可以很容易地从Mode编号的二进制表示中重现。 Note that three octal digits will give you exactly nine binary digits ( 8#644 = 2#110110100 ). 请注意,三个八进制数字将恰好为您提供九个二进制数字( 8#644 = 2#110110100 )。 In fact this is the string rwxrwxrwx where each element replaced by - if corresponding digit equals 0 . 实际上,这是字符串rwxrwxrwx ,其中每个元素都由-替换,如果对应的数字等于0 If digit is 1 the element remains untouched. 如果digit为1该元素保持不变。

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. 如果您有任何疑问,请随时在评论中提出任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM