如果文件权限大于755,如何检查Perl? [英] How to check in Perl if the file permission is greater than 755?

查看:156
本文介绍了如果文件权限大于755,如何检查Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于UNIX文件,我想知道Group或World是否对该文件具有写许可权.

For a unix file, I want to know if Group or World has write permission on the file.

我一直在考虑以下方面:

I've been thinking on these lines:

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
} 

谢谢.

推荐答案

您很接近您的建议-使用 stat 有点差了(但是,再三考虑,您必须使用

You are close with your proposal - the usage of stat is a little off (but on second thoughts, you must be using File::stat; it helps if your code is complete), the mask constant is faulty, and the comment leaves somewhat to be desired:

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中检入?也就是说,"Group/World"拥有写权限"有点怀疑.


The terminology in the question title 'How to check in Perl if the file permission is greater than 755? i.e. Group/World has write permission' is a little suspect.

该文件可能具有权限022(或更合理的说是622),其中包括组和世界的写许可权,但是没有一个值可以合理地声称大于755".

The file might have permissions 022 (or, more plausibly, 622), and that would include group and world write permission, but neither value can reasonably be claimed to be 'greater than 755'.

我发现有用的一组概念是:

A set of concepts that I've found useful is:

  • 设置位-权限字段中的位必须为1.
  • 重置位-权限字段中的位必须为0.
  • 无关位-可以设置或重置的位.

例如,对于数据文件,我可能会要求:

For example, for a data file, I might require:

  • 设置0644(所有者可以读取和写入;组和其他可以读取).
  • 重置0133(所有者无法执行-这是一个数据文件;组和其他用户无法写入或执行).

对于数据文件,我可能会要求:

More likely, for a data file, I might require:

  • 设置0400(所有者必须能够读取).
  • 重置0133(无人可以执行;组和其他人不能写入).
  • 无需关心0244(所有者是否可以写作;组或其他人是否可以阅读).

目录略有不同:执行权限意味着您可以将目录设置为当前目录,或者在知道目录名称的情况下访问目录中的文件,而读取权限意味着您可以查找目录中的文件,但是您可以未经执行许可也无法访问它们.因此,您可能会:

Directories are slightly different: execute permission means that you can make the directory your current directory, or access files in the directory if you know their name, while read permission means you can find out what files are in the directory, but you can't access them without execute permission too. Hence, you might have:

  • 设置0500(所有者必须能够读取和使用目录中的文件).
  • 重置0022(组和其他人不能修改目录-删除或添加文件).
  • 不需要0255(不必关心用户是否可以创建文件;不必关心组或其他人是否可以列出或使用文件).

请注意,置位和复位位必须不相交(($set & $rst) == 0)),位的总和始终为0777;否则,总之为0777.可以从0777 & ~($set | $rst)计算无关"位.

Note that the set and reset bits must be disjoint (($set & $rst) == 0)), the sum of the bits will always be 0777; the "don't care" bits can be computed from 0777 & ~($set | $rst).

这篇关于如果文件权限大于755,如何检查Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆