Hoz*_*ozy 5 perl file-permissions
对于unix文件,我想知道Group或World是否对该文件具有写入权限.
我一直在考虑这些问题:
my $fpath = "orion.properties";
my $info = stat($fpath) ;
my $retMode = $info->mode;
$retMode = $retMode & 0777;
if(($retMode & 006)) {
# Code comes here if World has r/w/x on the file
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Jon*_*ler 13
你接近你的提议 - 使用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
}
Run Code Online (Sandbox Code Playgroud)
问题标题中的术语"如果文件权限大于755,如何检入Perl?即Group/World有写权限'有点怀疑.
该文件可能具有权限022(或更合理地,622),并且将包括组和世界写入权限,但这两个值都不能合理地声称为"大于755".
我发现有用的一组概念是:
例如,对于数据文件,我可能需要:
更有可能的是,对于数据文件,我可能需要:
目录略有不同:执行权限意味着您可以将目录设为当前目录,或者如果您知道其名称则访问目录中的文件,而读取权限意味着您可以找到目录中的文件,但是您不能没有执行权限也可以访问它们.因此,你可能有:
注意,置位和复位位必须是不相交的(($set & $rst) == 0)),位的总和将始终为0777; 可以从中计算"不关心"位0777 & ~($set | $rst).