如果文件权限大于755,如何在Perl中检查?

发布于 2024-12-22 19:59:05 字数 305 浏览 3 评论 0原文

对于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
} 

谢谢。

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
} 

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-12-29 19:59:05

您已经接近您的建议 - 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)

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
}

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.

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:

  • Set bits - bits in the permissions field that must be 1.
  • Reset bits - bits in the permissions field that must be 0.
  • Don't care bits - bits that can be set or reset.

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

  • Set 0644 (owner can read and write; group and other can read).
  • Reset 0133 (owner cannot execute - it is a data file; group and other cannot write or execute).

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

  • Set 0400 (owner must be able to read).
  • Reset 0133 (no-one can execute; group and other cannot write).
  • Don't care 0244 (does not matter whether the owner can write; does not matter whether group or others can read).

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:

  • Set 0500 (owner must be able to read and use files in the directory).
  • Reset 0022 (group and others must not be able to modify the directory - delete or add files).
  • Don't care 0255 (don't care whether user can create files; don't care whether group or other can list or use files).

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).

感受沵的脚步 2024-12-29 19:59:05
#!/usr/bin/perl

use warnings;
use strict;

chomp (my $filename = <STDIN>);

my $lsOutput = `ls -l $filename`;

my @fields = split (/ /,$lsOutput);

my @per = split (//,$fields[0]);

print "group has write permission \n" if ($per[5] eq 'w');

print "world has write permission" if ($per[8] eq 'w');
#!/usr/bin/perl

use warnings;
use strict;

chomp (my $filename = <STDIN>);

my $lsOutput = `ls -l $filename`;

my @fields = split (/ /,$lsOutput);

my @per = split (//,$fields[0]);

print "group has write permission \n" if ($per[5] eq 'w');

print "world has write permission" if ($per[8] eq 'w');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文