파일 :: find :: 규칙을 오염 모드로 사용하려면 어떻게합니까?
-
10-07-2019 - |
문제
다음과 같은 것을 사용하여 주어진 디렉토리에서 하위 디렉토리 목록을 얻으려고합니다.
#!/usr/bin/perl -wT
use strict;
use warnings;
use File::Find::Rule;
use Data::Dumper;
my @subdirs = File::Find::Rule->maxdepth(1)->directory->relative->in('mydir');
print Dumper(@subdirs);
그러나이를 실행하면 결과가 나옵니다.
Insecure dependency in chdir while running with -T switch
나는 이해 File::Find
오염 모드를 다루는 옵션이 있지만 동등한 것을 찾을 수없는 것 같습니다. File::Find::Rule
. 위의 일을 할 수 있습니까? 하위 디렉터를 나열하기 위해 대체 방법을 사용해야합니까? 오 테인트 모드에 대해 정말로 이해해야한다는 것이 분명한 것을 완전히 오해하고 있습니까?
해결책
(편집하다!) 좋아요, 논리는 다음을 던지는 것이 효과가 있다고 제안합니다.
->extras( {untaint => 1, untaint_pattern => $untaint_pattern, untaint_skip => 1} )
이렇게하면 해당 모듈의 인수를 직접 전달하여 파일의 오염 모드 기능을 사용할 수 있습니다. find()
기능. 또한 파일 :: 설정해야한다는 언급을 찾으십시오. $untaint_pattern
사용함으로써 qr//
운영자. 예를 들어, 기본값은입니다
$untaint_pattern = qr|^([-+@\w./]+)$|
하지만, 이것은 작동하지 않습니다! 실제로, 귀하의 문제는 파일에서 알려진 버그입니다 :: 찾기 :: 규칙입니다. (예를 들어, 여기에 있습니다 CPAN 그리고 데비안 버그 보고서.) 버그 문제를 원한다면이 버그 보고서에는 패치가 있습니다.
당신이 제한된 환경에 있다면, 당신이 할 수있는 한 가지는 본질적으로 코드에서 패치를 직접 구현하는 것입니다. 예를 들어, 모든 것을 하나의 파일로 유지하려면 아래에 큰 코드 블록을 추가 할 수 있습니다. use File::Find::Rule
. 이것은 매우 빠른 수정이며 차선책 일 수 있습니다. 그것이 당신에게 효과가 없다면 (예 : 파일 이름에 공백이 있기 때문에) 패턴을 변경하십시오. qr|^([-+@\w./]+)$|
그것은 사용됩니다.
마지막으로 코드 조직이 조금 더 나아지기를 원한다면 항상 MyFileFindruleFix 또는 무언가라고 불리는 별도의 패키지에 이것을 덤프하고 싶을 수도 있습니다. use
~ 후에 File::Find::Rule
그 자체.
package File::Find::Rule;
no warnings qw(redefine);
sub in {
my $self = _force_object shift;
my @found;
my $fragment = $self->_compile( $self->{subs} );
my @subs = @{ $self->{subs} };
warn "relative mode handed multiple paths - that's a bit silly\n"
if $self->{relative} && @_ > 1;
my $topdir;
my $code = 'sub {
(my $path = $File::Find::name) =~ s#^(?:\./+)+##;
$path = "." if ($path eq ""); # See Debian bug #329377
my @args = ($_, $File::Find::dir, $path);
my $maxdepth = $self->{maxdepth};
my $mindepth = $self->{mindepth};
my $relative = $self->{relative};
# figure out the relative path and depth
my $relpath = $File::Find::name;
$relpath =~ s{^\Q$topdir\E/?}{};
my $depth = scalar File::Spec->splitdir($relpath);
#print "name: \'$File::Find::name\' ";
#print "relpath: \'$relpath\' depth: $depth relative: $relative\n";
defined $maxdepth && $depth >= $maxdepth
and $File::Find::prune = 1;
defined $mindepth && $depth < $mindepth
and return;
#print "Testing \'$_\'\n";
my $discarded;
return unless ' . $fragment . ';
return if $discarded;
if ($relative) {
push @found, $relpath if $relpath ne "";
}
else {
push @found, $path;
}
}';
#use Data::Dumper;
#print Dumper \@subs;
#warn "Compiled sub: '$code'\n";
my $sub = eval "$code" or die "compile error '$code' $@";
my $cwd = getcwd;
# Untaint it
if ( $cwd =~ qr|^([-+@\w./]+)$| ) {
$cwd = $1;
} else {
die "Couldn't untaint \$cwd: [$cwd]";
}
for my $path (@_) {
# $topdir is used for relative and maxdepth
$topdir = $path;
# slice off the trailing slash if there is one (the
# maxdepth/mindepth code is fussy)
$topdir =~ s{/?$}{}
unless $topdir eq '/';
$self->_call_find( { %{ $self->{extras} }, wanted => $sub }, $path );
}
chdir $cwd;
return @found;
}
use warnings;
package main;