From 9bbadc5fecc04ff09dc0de084b5ce71a589f737a Mon Sep 17 00:00:00 2001 From: Andreas Romeyke Date: Thu, 14 Jan 2021 15:04:42 +0100 Subject: [PATCH] - refactoring, extracted __handle_nonportable_local_entry() from __file_find() --- lib/Archive/BagIt.pm | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/Archive/BagIt.pm b/lib/Archive/BagIt.pm index 0fb1a6f..515c391 100644 --- a/lib/Archive/BagIt.pm +++ b/lib/Archive/BagIt.pm @@ -802,6 +802,30 @@ sub _build_tagmanifest_files { return \@tagmanifest_files; } +sub __handle_nonportable_local_entry { + my $self = shift; + my $local_entry = shift; + my $dir = shift; + my $rx_portable = qr/^[a-zA-Z0-9._-]+$/; + my $is_portable = $local_entry =~ m/$rx_portable/; + if (! $is_portable) { + my $local_entry_utf8 = decode("UTF-8", $local_entry); + if ((!$self->has_force_utf8)) { + my $hexdump = "0x" . unpack('H*', $local_entry); + $local_entry =~m/[^a-zA-Z0-9._-]/; # to find PREMATCH, needed nextline + my $prematch_position = $`; + carp "possible non portable pathname detected in $dir,\n", + "got path (hexdump)='$hexdump'(hex),\n", + "decoded path='$local_entry_utf8'\n", + " "." "x length($prematch_position)."^"."------- first non portable char\n"; + } + $local_entry = $local_entry_utf8; + } + return $local_entry; +} + + + sub __file_find { # own implementation, because File::Find has problems with UTF8 encoded Paths under MSWin32 # finds recursively all files in given directory. # if $excludedir is defined, the content will be excluded @@ -810,7 +834,7 @@ sub __file_find { # own implementation, because File::Find has problems with UTF $excludedir = File::Spec->rel2abs( $excludedir); } my @file_paths; - my $rx_portable = qr/^[a-zA-Z0-9._-]+$/; + my $finder; $finder = sub { my ($current_dir) = @_; #absolute path @@ -820,21 +844,7 @@ sub __file_find { # own implementation, because File::Find has problems with UTF my @paths = File::Spec->no_upwards ( readdir $dh ); closedir $dh; foreach my $local_entry (@paths) { - my $is_portable = $local_entry =~ m/$rx_portable/; - if (! $is_portable) { - my $local_entry_utf8 = decode("UTF-8", $local_entry); - if ((!$self->has_force_utf8)) { - my $hexdump = "0x" . unpack('H*', $local_entry); - $local_entry =~m/[^a-zA-Z0-9._-]/; # to find PREMATCH, needed nextline - - carp "possible non portable pathname detected in $dir,\n", - "got path (hexdump)='$hexdump'(hex),\n", - "decoded path='$local_entry_utf8'\n", - " "." "x length($`)."^"."------- first non portable char\n"; #$` eq $PREMATCH - } - $local_entry = $local_entry_utf8; - } - my $path_entry = File::Spec->catdir($current_dir, $local_entry); + my $path_entry = File::Spec->catdir($current_dir, $self->__handle_nonportable_local_entry($local_entry, $dir)); if (-f $path_entry) { push @tmp_file_paths, $path_entry; } elsif (-d $path_entry) {