对于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
}
Thanks.
你很接近你的建议 - 使用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
}
问题标题“如果文件权限大于755,如何在Perl中检查?”中的术语即“组/世界具有写权限”有点可疑。
该文件可能具有权限 022(或者更合理的是 622),并且这将包括组和世界写入权限,但这两个值都不能合理地声称“大于 755”。
我发现有用的一组概念是:
- 设置位 - 权限字段中必须为 1 的位。
- 重置位 - 权限字段中的位必须为 0。
- 不关心位 - 可以设置或重置的位。
例如,对于数据文件,我可能需要:
- 设置0644(所有者可以读写;组和其他可以读取)。
- 重置0133(所有者无法执行-它是一个数据文件;组和其他人无法写入或执行)。
更有可能的是,对于数据文件,我可能需要:
- 设置 0400(所有者必须能够读取)。
- 复位0133(无人可以执行;组和其他人不能写入)。
- 不关心0244(不关心所有者是否可以写入;不关心组或其他人是否可以读取)。
目录略有不同:执行权限意味着您可以将该目录设为当前目录,或者如果您知道目录中的文件名称,则可以访问该目录中的文件,而读取权限意味着您可以找出该目录中的文件,但不能也无需执行权限即可访问它们。因此,您可能有:
- 设置0500(所有者必须能够读取和使用目录中的文件)。
- 重置0022(组和其他人不得修改目录-删除或添加文件)。
- 不关心0255(不关心用户是否可以创建文件;不关心组或其他是否可以列出或使用文件)。
请注意,设置位和复位位必须不相交(($set & $rst) == 0)
),位总和将始终为 0777; “不关心”位可以通过以下方式计算0777 & ~($set | $rst)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)