你接近你的建議 - 的stat
的使用是有點過(但轉念一想,你必須使用File::stat
;它幫助,如果你的代碼完成),面具不變的是錯誤的,並註釋葉子有點不理想:
use strict;
use warnings;
use File::stat;
my $fpath = "orion.properties";
my $info = stat($fpath);
my $retMode = $info->mode;
$retMode = $retMode & 0777;
if ($retMode & 002) {
# Code comes here if World has write permission on the file
}
if ($retMode & 020) {
# Code comes here if Group has write permission on the file
}
if ($retMode & 022) {
# Code comes here if Group or World (or both) has write permission on the file
}
if ($retMode & 007) {
# Code comes here if World has read, write *or* execute permission on the file
}
if ($retMode & 006) {
# Code comes here if World has read or write permission on the file
}
if (($retMode & 007) == 007) {
# Code comes here if World has read, write *and* execute permission on the file
}
if (($retMode & 006) == 006) {
# Code comes here if World has read *and* write permission on the file
}
if (($retMode & 022) == 022) {
# Code comes here if Group *and* World both have write permission on the file
}
在提問標題的術語「如何檢查在Perl中,如果該文件的權限比755更大?即集團/世界有寫入許可「有點可疑。
該文件可能擁有權限022(或更合理的是622),並且包含組和世界寫入權限,但這兩個值都不能合理地聲稱爲「大於755」。
我發現有用的一組概念是:
- 集位 - 位在權限字段必須是1
- 復位位 - 位在權限字段必須爲0
- 不關心位 - 可以設置或重置的位。
例如,對於一個數據文件,我可能需要:
- 套裝0644(所有者可以讀,寫,組和其他可以讀取)。
- 重置0133(所有者無法執行 - 它是一個數據文件;組和其他無法寫入或執行)。
更可能的是,對於一個數據文件,我可能需要:
- 套裝0400(所有者必須能夠讀取)。
- 重置0133(沒有人可以執行;組和其他不能寫入)。
- 不在乎0244(無論所有者是否可以寫;無論組或其他人都可以閱讀)。
目錄略有不同:執行權限意味着可以使目錄成爲當前目錄,或訪問目錄中的文件,如果你知道他們的名字,而讀取權限意味着你可以找出哪些文件目錄,但如果沒有執行權限,則無法訪問它們。因此,您可能有:
- 設置0500(所有者必須能夠讀取和使用目錄中的文件)。
- 重置0022(組和其他人不能修改目錄 - 刪除或添加文件)。
- 不在意0255(不在乎用戶是否可以創建文件;不關心羣組或其他人是否可以列出或使用文件)。
請注意,置位和復位位必須是不相交的(($set & $rst) == 0)
),位總和將始終爲0777;可以從0777 & ~($set | $rst)
計算「無關」位。
請使用[Fcntl](http://p3rl.org/Fcntl)模式常量('S_I *')而不是幻數。 – daxim 2011-12-27 17:54:11
對於像我這樣老古板的人來說,這些常數比'Fcntl'中的字母表湯更容易閱讀。單獨地,常量的可讀性更高,但'S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH'比'0755'更難讀取。但是,使用字母湯可能是更好的風格。我想我可以定義一個方便的常量:'使用常量S_I755 =>(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);'? (是的,這是一個笑話。)也許'使用常量S_RWXR_XR_X =>(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);'更可口。 – 2011-12-27 18:10:47