Dackup/Target/SSH.pm

filter: Statementstmt 23.6% 27 / 114 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 5 totaltotal 15.9% 34 / 213 CC 36 SCAR 63.8
CC
36
Cov
15%
entries
930.0
update
42.0
delete
6.0
CRAP 587.0
↑ Summary Dackup/Target/CloudFiles.pm »
1 package Dackup::Target::SSH;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::MD5::File qw(file_md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Dackup::Target';
11  
12 1 has 'ssh' => (
13 is => 'ro',
14 isa => 'Net::OpenSSH',
15 required => 1,
16 );
17  
18 1 has 'prefix' => (
19 is => 'ro',
20 isa => 'Path::Class::Dir',
21 required => 1,
22 coerce => 1,
23 );
24  
25 has 'directories' => (
26 is => 'rw',
27 isa => 'HashRef',
28 required => 0,
29 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 1 );
31  
32 1 __PACKAGE__->meta->make_immutable;
33  
34 0 ▶sub entries {
Subroutine entries: not called CC=30 SCAR=68.4
Pod undocumented
35 0 my $self = shift;
36 0 my $ssh = $self->ssh;
37 0 my $dackup = $self->dackup;
38 0 my $prefix = $self->prefix;
39 0 my $cache = $dackup->cache;
40 0 my $directories = $self->directories;
41  
42 0 my ( $type, $type_err )
43 = $ssh->capture2(qq{perl -e 'print "directory\\n" if -d "$prefix"'});
44 0 chomp $type;
45 0 ▶ return [] if $type ne 'directory';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "directory" 0 0 0
46  
47 0 my $code = <<'EOF';
48 #!perl
49 use strict;
50 use warnings;
51 use File::Find;
52  
53 my $root = 'XXX';
54 find( \&wanted, $root );
55  
56 sub wanted {
57 my $filename = $File::Find::name;
58 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
59 $size, $atime, $mtime, $ctime, $blksize, $blocks
60 ) = stat($filename);
61 my $type;
62 if ( -f _ ) {
63 $type = 'file';
64 } elsif ( -d _ ) {
65 $type = 'directory';
66 } else {
67 $type = 'other';
68 }
69 print "$type:$ctime:$mtime:$size:$ino:$filename\n";
70 }
71 EOF
72 0 $code =~ s/XXX/$prefix/;
73  
74 0 ▶ my ($tmpnam)
Condition0%0/2
$ssh->capture(q[perl -e 'use File::Temp qw/:POSIX/; print scalar tmpnam() . "\\n"']) || die('ssh failed: ' . $ssh->error)
l!l
00
MC/DC0%0/1
$ssh->capture(q[perl -e 'use File::Temp qw/:POSIX/; print scalar tmpnam() . "\\n"']) || die('ssh failed: ' . $ssh->error)
$ssh->capture(q[perl -e 'use File::Temp qw/:POSIX/; print scalar tmpnam() . "\\n"'])
Truth table0%0/2
A
A= $ssh->capture(q[perl -e 'use File::Temp qw/:POSIX/; print scalar tmpnam() . "\\n"'])
Aresult
11
00
75 = $ssh->capture(
76 q{perl -e 'use File::Temp qw/:POSIX/; print scalar tmpnam() . "\n"'})
77 || die "ssh failed: " . $ssh->error;
78 0 chomp $tmpnam;
79  
80 0 ▶ my ( $rin, $in_pid ) = $ssh->pipe_in("cat > $tmpnam")
Branch: 1 branch
BranchTrueFalseTotal
() = $ssh->pipe_in("cat > $tmpnam") or die 'pipe_in method failed: ' . $ssh->error 0 0 0
81 or die "pipe_in method failed: " . $ssh->error;
82 0 ▶ $rin->print("$code") || die $ssh->error;
Branch: 1 branch
BranchTrueFalseTotal
$rin->print("$code") or die $ssh->error 0 0 0
83 0 ▶ $rin->close || die $ssh->error;
Branch: 1 branch
BranchTrueFalseTotal
$rin->close or die $ssh->error 0 0 0
84 0 waitpid( $in_pid, 0 );
85  
86 0 ▶ my ($output) = $ssh->capture2("perl $tmpnam")
Condition0%0/2
$ssh->capture2("perl $tmpnam") || die('ssh failed: ' . $ssh->error)
l!l
00
MC/DC0%0/1
$ssh->capture2("perl $tmpnam") || die('ssh failed: ' . $ssh->error)
$ssh->capture2("perl $tmpnam")
Truth table0%0/2
A
A= $ssh->capture2("perl $tmpnam")
Aresult
11
00
87 || die "ssh failed: " . $ssh->error;
88 0 ▶ $ssh->system("rm $tmpnam")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->system("rm $tmpnam") or die 'remote command failed: ' . $ssh->error 0 0 0
89 or die "remote command failed: " . $ssh->error;
90 0 ▶ return [] unless $output;
Branch: 1 branch
BranchTrueFalseTotal
unless $output 0 0 0
91  
92 0 my @entries;
93 my @not_in_cache;
94 0 foreach my $line ( split "\n", $output ) {
95 0 my ( $type, $ctime, $mtime, $size, $inodenum, $filename ) = split ':',
96 $line, 6;
97 0 ▶ next if $type eq 'other';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "other" 0 0 0
98 0 ▶ confess "Error with stat: $line"
Branch: 1 branch
BranchTrueFalseTotal
unless $type and defined $filename and $ctime and $mtime and defined $size and defined $inodenum 0 0 0
Condition0%0/15
$type and defined $filename and $ctime and $mtime and defined $size and defined $inodenum
!ll&&!rl&&r
000
$type and defined $filename and $ctime and $mtime and defined $size
!ll&&!rl&&r
000
$type and defined $filename and $ctime and $mtime
!ll&&!rl&&r
000
$type and defined $filename and $ctime
!ll&&!rl&&r
000
$type and defined $filename
!ll&&!rl&&r
000
MC/DC0%0/6
$type and defined $filename and $ctime and $mtime and defined $size and defined $inodenum
$type defined $filename $ctime $mtime defined $size defined $inodenum
Truth table0%0/7
A and B and C and D and E and F
A= $typeB= defined $filenameC= $ctimeD= $mtimeE= defined $sizeF= defined $inodenum
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
99 unless $type
100 && defined($filename)
101 && $ctime
102 && $mtime
103 && defined($size)
104 && defined($inodenum);
105  
106 0 ▶ if ( $type eq 'directory' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "directory") 0 0 0
107 0 $directories->{$filename} = 1;
108 0 next;
109 }
110  
111 0 my $key = file($filename)->relative($prefix)->stringify;
112 my $cachekey
113 0 = 'ssh:' . $ssh->{_user} . ':' . $ssh->{_host} . ':' . $line;
114  
115 0 my $md5_hex = $cache->get($cachekey);
116 0 ▶ if ($md5_hex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5_hex) { } 0 0 0
117 0 push @entries,
118 Dackup::Entry->new(
119 { key => $key,
120 md5_hex => $md5_hex,
121 size => $size,
122 }
123 );
124 } else {
125 0 push @not_in_cache,
126 {
127 key => $key,
128 cachekey => $cachekey,
129 filename => $filename,
130 size => $size,
131 };
132 }
133 }
134 0 ▶ if (@not_in_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (@not_in_cache) 0 0 0
135  
136 0 my $code = <<'EOF';
137 #!perl
138 use strict;
139 use warnings;
140 use Digest::MD5;
141 use IO::File;
142  
143 my XXX
144 foreach my $filename (@$filenames) {
145 my $fh = IO::File->new($filename) || die $!;
146 my $md5 = Digest::MD5->new;
147 $md5->addfile($fh);
148 print $md5->hexdigest . ' ' . $filename . "\n";
149 $fh->close;
150 }
151 EOF
152  
153 my $files
154 = Data::Dumper->Dump(
155 0 [ [ map { $_->{filename} } @not_in_cache ] ],
156 ['filenames'] );
157 0 $code =~ s/XXX/$files/;
158  
159 0 ▶ my ( $rin, $in_pid ) = $ssh->pipe_in("cat > $tmpnam")
Branch: 1 branch
BranchTrueFalseTotal
() = $ssh->pipe_in("cat > $tmpnam") or die 'pipe_in method failed: ' . $ssh->error 0 0 0
160 or die "pipe_in method failed: " . $ssh->error;
161 0 ▶ $rin->print($code) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
$rin->print($code) or die $! 0 0 0
162 0 ▶ $rin->close || die $ssh->error;
Branch: 1 branch
BranchTrueFalseTotal
$rin->close or die $ssh->error 0 0 0
163 0 waitpid( $in_pid, 0 );
164  
165 0 my %filename_to_d;
166 0 foreach my $d (@not_in_cache) {
167 0 my $filename = $d->{filename};
168 0 $filename_to_d{$filename} = $d;
169 }
170  
171 0 ▶ my ($lines) = $ssh->capture2("perl $tmpnam")
Condition0%0/2
$ssh->capture2("perl $tmpnam") || die('ssh failed: ' . $ssh->error)
l!l
00
MC/DC0%0/1
$ssh->capture2("perl $tmpnam") || die('ssh failed: ' . $ssh->error)
$ssh->capture2("perl $tmpnam")
Truth table0%0/2
A
A= $ssh->capture2("perl $tmpnam")
Aresult
11
00
172 || die "ssh failed: " . $ssh->error;
173  
174 0 foreach my $line ( split "\n", $lines ) {
175  
176 # chomp $line;
177 #warn "[$line]";
178 0 my ( $md5_hex, $filename ) = split / +/, $line, 2;
179  
180 #warn "[$md5_hex, $filename]";
181 0 ▶ confess "Error with $line"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $md5_hex and defined $filename 0 0 0
Condition0%0/3
defined $md5_hex and defined $filename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $md5_hex and defined $filename
defined $md5_hex defined $filename
Truth table0%0/3
A and B
A= defined $md5_hexB= defined $filename
ABresult
0X0
100
111
182 unless defined $md5_hex && defined $filename;
183 0 my $d = $filename_to_d{$filename};
184 0 ▶ confess "Missing d for $filename" unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 0 0
185 push @entries,
186 Dackup::Entry->new(
187 { key => $d->{key},
188 md5_hex => $md5_hex,
189 size => $d->{size},
190 }
191 0 );
192 0 $cache->set( $d->{cachekey}, $md5_hex );
193 }
194 0 ▶ $ssh->system("rm $tmpnam")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->system("rm $tmpnam") or die 'remote command failed: ' . $ssh->error 0 0 0
195 or die "remote command failed: " . $ssh->error;
196 }
197 0 return \@entries;
198 }
199  
200 0 ▶sub filename {
Subroutine filename: not called CC=1 SCAR=6.9
Pod undocumented
201 0 my ( $self, $entry ) = @_;
202 0 return file( $self->prefix, $entry->key );
203 }
204  
205 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my ( $self, $entry ) = @_;
207 0 my $ssh = $self->ssh;
208 return
209 'ssh://'
210 . $ssh->{_user} . '@'
211 . $ssh->{_host}
212 0 . file( $self->prefix, $entry->key );
213 }
214  
215 0 ▶sub update {
Subroutine update: not called CC=6 SCAR=37.4
Pod undocumented
216 0 my ( $self, $source, $entry ) = @_;
217 0 my $ssh = $self->ssh;
218 0 my $source_type = ref($source);
219 0 my $destination_filename = $self->filename($entry);
220 0 my $destination_directory = $destination_filename->parent;
221 0 my $directories = $self->directories;
222  
223 0 ▶ if ( $source_type eq 'Dackup::Target::Filesystem' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_type eq 'Dackup::Target::Filesystem') { } 0 0 0
224 0 my $source_filename = $source->filename($entry);
225  
226 0 ▶ unless ( $directories->{$destination_directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($directories->{$destination_directory}) 0 0 0
227  
228 0 my $quoted_destination_directory
229 = $ssh->shell_quote("$destination_directory");
230  
231 # warn "mkdir -p $quoted_destination_directory";
232 0 ▶ $ssh->system("mkdir -p $quoted_destination_directory")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->system("mkdir -p $quoted_destination_directory") or die "mkdir -p $quoted_destination_directory failed: " . $ssh->error 0 0 0
233 || die "mkdir -p $quoted_destination_directory failed: "
234 . $ssh->error;
235 0 $directories->{$destination_directory} = 1;
236 }
237  
238 #warn "$source_filename -> $destination_filename";
239  
240 0 my $scp_options = {};
241 0 my $throttle = $self->dackup->throttle;
242 0 ▶ if ($throttle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throttle) 0 0 0
243 0 my $data_rate = Number::DataRate->new;
244 0 my $bits_per_second = $data_rate->to_bits_per_second($throttle);
245 0 $scp_options->{bwlimit} = $bits_per_second / 1000; # in Kbit/s
246 }
247  
248 0 ▶ $ssh->scp_put( $scp_options, "$source_filename",
Branch: 1 branch
BranchTrueFalseTotal
$ssh->scp_put($scp_options, "$source_filename", "$destination_filename") or die 'scp failed: ' . $ssh->error 0 0 0
249 "$destination_filename" )
250 || die "scp failed: " . $ssh->error;
251 } else {
252 0 confess "Do not know how to update from $source_type";
253 }
254 }
255  
256 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
257 0 my ( $self, $entry ) = @_;
258 0 my $ssh = $self->ssh;
259 0 my $filename = $self->filename($entry);
260  
261 0 ▶ $ssh->system("rm -f $filename")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->system("rm -f $filename") or die "rm -f $filename failed: " . $ssh->error 0 0 0
262 || die "rm -f $filename failed: " . $ssh->error;
263 }
264  
265 1 1;
266  
267 __END__
↑ Summary Dackup/Target/CloudFiles.pm »