| 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package File::Slurp::Remote::SmartOpen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::Slurp::Remote::BrokenDNS qw($myfqdn %fqdnify); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Tie::Function::Examples qw(%q_shell); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our @EXPORT = qw(smartopen); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | our $ssh = "ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.48'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | sub smartopen($\$$) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 13 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
smartopen: called CC=28 SCAR=36.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 13 | my ($file, $fdref, $mode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | my $unzip = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 13 | my $zip = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | if ($file =~ /\.gz$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $unzip = "zcat"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | $zip = "gzip"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } elsif ($file =~ /\.bz2$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | $unzip = "bzcat"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $zip = "bzip2"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | my $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 13 | my $fd = $$fdref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 13 | my ($user, $host); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # As with scp, a colon before any slash separates the host from the path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | if ($file =~ s/^([^\/]+?)://) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | $host = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Allow user@host; like ssh, split at the last @ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 12 | ▶ | if ($host =~ /^(.*)@([^@]*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 10 | ($user, $host) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 10 | ▶ | confess "smartopen: invalid user '$user'" if $user eq '' || $user =~ /^-/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$user eq "" or $user =~ /^-/
MC/DC100%2/2
$user eq "" or $user =~ /^-/
$user eq ""
$user =~ /^-/
Truth table100%3/3
A or B
A= $user eq ""B= $user =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | confess "smartopen: invalid host '$host'" if $host eq '' || $host =~ /^-/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$host eq "" or $host =~ /^-/
MC/DC100%2/2
$host eq "" or $host =~ /^-/
$host eq ""
$host =~ /^-/
Truth table100%3/3
A or B
A= $host eq ""B= $host =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | ▶ | if (defined($host) && ($fqdnify{$host} ne $myfqdn || (defined($user) && $user ne _local_user()))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $host and $File::Slurp::Remote::SmartOpen::fqdnify{$host} ne $File::Slurp::Remote::SmartOpen::myfqdn || defined $user && $user ne _local_user()
$File::Slurp::Remote::SmartOpen::fqdnify{$host} ne $File::Slurp::Remote::SmartOpen::myfqdn or defined $user and $user ne _local_user()
defined $user and $user ne _local_user()
MC/DC75%3/4
defined $host and $File::Slurp::Remote::SmartOpen::fqdnify{$host} ne $File::Slurp::Remote::SmartOpen::myfqdn || defined $user && $user ne _local_user()
defined $host
$File::Slurp::Remote::SmartOpen::fqdnify{$host} ne $File::Slurp::Remote::SmartOpen::myfqdn
defined $user
$user ne _local_user()
Truth table80%4/5
A and B or C and D
A= defined $hostB= $File::Slurp::Remote::SmartOpen::fqdnify{$host} ne $File::Slurp::Remote::SmartOpen::myfqdnC= defined $userD= $user ne _local_user()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | ▶ | my $dest = defined($user) ? "-l $q_shell{$user} $q_shell{$host}" : $q_shell{$host}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 7 | ▶ | $unzip = "|$unzip" if $unzip; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | ▶ | $zip = "$zip|" if $zip; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 7 | ▶ | if ($mode && $mode eq 'w') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode and $mode eq "w"
MC/DC50%1/2
$mode and $mode eq "w"
$mode
$mode eq "w"
Truth table66%2/3
A and B
A= $modeB= $mode eq "w"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | my $cat = $q_shell{"cat > $q_shell{$file}"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | $pid = open $fd, "|$zip $ssh $dest $cat" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | or confess "open |$zip $ssh $dest $cat: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | ▶ | $pid = open $fd, "$ssh -n $dest cat $q_shell{$file} $unzip|" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | or confess "open $ssh $dest cat $q_shell{$file} $unzip|: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | if ($mode && $mode eq 'w') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode and $mode eq "w"
MC/DC50%1/2
$mode and $mode eq "w"
$mode
$mode eq "w"
Truth table66%2/3
A and B
A= $modeB= $mode eq "w"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if ($zip) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $pid = open $fd, "|$zip > $q_shell{$file}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | or die "open |$zip > $q_shell{$file}: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | open $fd, ">", $file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | or die "open >$file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | $pid = "0butTrue"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | if ($unzip) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $pid = open $fd, "$unzip < $q_shell{$file}|" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | or die "open $unzip < $q_shell{$file}|: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | open $fd, "<", $file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | or die "open <$file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | $pid = "0buttrue"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 9 | $$fdref = $fd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | return $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # The login name of the user running this process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | sub _local_user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_local_user: called CC=4 SCAR=21.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | my $name = eval { scalar getpwuid($<) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | return $name if defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | return $ENV{USER} if defined $ENV{USER}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $ENV{USERNAME} if defined $ENV{USERNAME}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||