Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 在Perl— ;跳过权限被拒绝_File_Perl_Find - Fatal编程技术网

File 在Perl— ;跳过权限被拒绝

File 在Perl— ;跳过权限被拒绝,file,perl,find,File,Perl,Find,尝试使用下面的Perl脚本在子目录中查找脚本完整路径。它需要跳过权限被拒绝的文件夹,而如果权限被拒绝,则它在某个子目录中失败 “权限被拒绝”问题后,我如何继续 use File::Find; my @files; my @dirpath=qw(/app/welogic/); find(sub { push @files,$File::Find::name if (-f $File::Find::name and /\wlst.sh$/); }, @

尝试使用下面的Perl脚本在子目录中查找脚本完整路径。它需要跳过权限被拒绝的文件夹,而如果权限被拒绝,则它在某个子目录中失败

“权限被拒绝”问题后,我如何继续

use File::Find;

my @files;
my @dirpath=qw(/app/welogic/);
 find(sub {
           push @files,$File::Find::name if (-f $File::Find::name      and /\wlst.sh$/);
      }, @dirpath);

权限被拒绝
(对于
cd
)对我来说只是一个警告(使用下面的
$SIG{{uuuuu WARN}
钩子进行管理),因此程序不会被终止

然而,我怀疑,搜索确实受到阻碍,因为

此外,对于找到的每个目录,它将
chdir()
插入该目录并继续搜索,对目录中的每个文件或子目录调用
&want
函数

显然,一旦这个
chdir
被阻止,搜索就会失败,我就没有得到任何结果。(补充说明——在另一个系统上,搜索是有效的,当然没有目录和警告除外。但这显然需要修复。)

我看到了两种方法:使用
preprocess
选项过滤掉目录,或者禁用
chdir

-使用
预处理
选项筛选出权限不正确的目录

preprocess
选项调用用户给定的子例程,该子例程接收条目列表(在当前目录中,待处理),并应返回随后将处理的条目列表

因此,我们可以检查条目的输入列表(在
@
中)中是否有权限不正确的目录,并将其从返回列表中排除,这样就永远不会尝试这些目录

use warnings;
use strict;
use feature 'say';

use File::Find;

my @dirpath = @ARGV ? @ARGV : die "Usage: $0 dir-list\n";

my @files;

find( { 
    wanted => sub { 
        push @files, $File::Find::name 
            if -f $File::Find::name and /\.txt$/ 
    },  
    preprocess => sub { 
        #say "--> Reading: $File::Find::dir";         
        return grep { not (-d and (not -x or not -r)) } @_; 
    }
}, @dirpath); 

say for @files;
return
语句中,我们过滤掉不可执行或不可读的目录。看见该语句可以重写为打印日志等的名称

-通过
no\u chdir
选项禁用
chdir
-ing

当目录的
chdir
被抑制时(在列出目录内容之前),整个搜索工作正常。我不知道为什么整个搜索在使用
chdir
时会失败,但在没有它的情况下可以工作

use warnings;
use strict;
use feature 'say';

use File::Find;

$SIG{__WARN__} = sub { warn "WARN: @_" };  # manage the warnings

sub bad_perm {
    push @files, $File::Find::name 
        if (-f $File::Find::name and /\.txt$/);
};

my @dirpath = @ARGV ? @ARGV : die "Usage: $0 dir-list\n";

my @files;

find( { no_chdir => 1, wanted => \&bad_perm }, @dirpath );

say for @files;
现在搜索运行正常,并汇编正确的(
.txt
)文件列表

同时,还会打印一条警告,现在用于
opendir

Can't opendir(tmp/this_belongs_to_root): Permission denied 看


我使用为此目的而创建的文件和目录的小层次结构来检查这两种方法,其中包含由root用户创建的具有
chmod go rwx
权限的目录(无论我以何种方式尝试触摸它,我都会及时收到错误)


我还调整了其他目录上的
-x
/
-r
权限,代码按预期工作。

当我运行您显示的脚本时,经过调整以处理我的目录结构并查找我的文件,我会收到关于无法访问的目录的错误消息,但代码随后会继续查找其他目录。我正在使用Perl 5.28.2(在运行macOS 10.14.6 Mojave的Mac上),如果这有帮助的话。@JonathanLeffler我还发现这确实只是一个警告,但搜索确实无法生成结果(v5.16和v5.30)。但是,一旦在选项中禁用了chdir-ing,所有功能都可以正常工作。我不知道为什么。我在我的答案中添加了另一种更干净的方法来解决这个问题(通过
预处理
-ing)
$SIG{__WARN__} = sub { 
    warn @_  unless $_[0] =~ /^Can't opendir\(.*?: Permission denied/;
};