| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Net::FTP::Simple - Simplified Net::FTP interface encapsulating a few simple | ||||||||||||||||||||||||||||
| 3 | # operations. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Written by Wil Cooley <wcooley@nakedape.cc> | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # $Id: Simple.pm 758 2008-10-11 04:29:18Z wcooley $ | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Net::FTP::Simple; | ||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use File::Basename qw( basename dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Net::FTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # FIXME MakeMaker handles it okay if it's all on one line, but Perl::Critic | ||||||||||||||||||||||||||||
| 19 | # pukes :( | ||||||||||||||||||||||||||||
| 20 | #eval q{ use version; our $VERSION = qv(0.0.5) }; our $VERSION = '0.0005' if ($EVAL_ERROR); | ||||||||||||||||||||||||||||
| 21 | 3 | our $VERSION = '0.0007'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | sub send_files { | ||||||||||||||||||||||||||
|
Subroutine
send_files: called CC=11 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | # Allow calls either as Net::FTP::Simple->send_files or | ||||||||||||||||||||||||||||
| 26 | # Net::FTP::Simple::send_files | ||||||||||||||||||||||||||||
| 27 | 5 | my ($opt_ref) = $_[-1]; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | my @successful_transfers; | |||||||||||||||||||||||||||
| 30 | 5 | my $ftp = Net::FTP::Simple->_new($opt_ref); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $ftp->_create_and_cwd_remote_dir() | ||||||||||||||||||||||||||||
| 33 | 5 | ▶ | if ($ftp->{'remote_dir'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | FILE_TO_TRANSFER: | ||||||||||||||||||||||||||||
| 36 | 5 | for my $file (@{ $ftp->{'files'} }){ | |||||||||||||||||||||||||||
| 37 | 13 | my $try_count; | |||||||||||||||||||||||||||
| 38 | 13 | my $basename = basename($file); | |||||||||||||||||||||||||||
| 39 | 13 | my $tmpname = $basename . '.tmp'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 13 | ▶ | unless ( -r $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | carp $ftp->_error("Local file '$file' unreadable; unable to transfer") | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | unless ($ftp->{'quiet_mode'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | next FILE_TO_TRANSFER; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 13 | ▶ | unless ( $ftp->_conn()->put($file, $tmpname) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | carp $ftp->_error("Error transferring file '$file' to '$tmpname'") | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | unless ($ftp->{'quiet_mode'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | next FILE_TO_TRANSFER; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 13 | eval { | |||||||||||||||||||||||||||
| 54 | 13 | $try_count = $ftp->_op_retry('rename', $tmpname, $basename); | |||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 13 | ▶ | if ($EVAL_ERROR =~ m/'rename' failed after \d+ attempts/ms) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 2 | carp "Error renaming '$tmpname' to '$basename'"; | |||||||||||||||||||||||||||
| 59 | 2 | next FILE_TO_TRANSFER; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | elsif($EVAL_ERROR) { | ||||||||||||||||||||||||||||
| 62 | # Rethrow unexpected exceptions | ||||||||||||||||||||||||||||
| 63 | 0 | croak $EVAL_ERROR; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 11 | ▶ | if ($try_count > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 4 | carp "Transfer of file '$file' succeeded after $try_count tries"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | push @successful_transfers, $file; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | wantarray ? return @successful_transfers | ||||||||||||||||||||||||||||
| 74 | 5 | ▶ | : return \@successful_transfers | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | ▶ | sub rename_files { | ||||||||||||||||||||||||||
|
Subroutine
rename_files: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | # Allow calls either as Net::FTP::Simple->rename_files or | ||||||||||||||||||||||||||||
| 81 | # Net::FTP::Simple::rename_files | ||||||||||||||||||||||||||||
| 82 | 4 | my ($opt_ref) = $_[-1]; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | my @successful_renames; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4 | my $ftp = Net::FTP::Simple->_new($opt_ref); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | if (exists $ftp->{'remote_dir'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $ftp->_conn()->cwd($ftp->{'remote_dir'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | or croak $ftp->_error("Error changing to remote directory", | ||||||||||||||||||||||||||||
| 91 | "'$ftp->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | FILE_TO_RENAME: | ||||||||||||||||||||||||||||
| 95 | 4 | for my $src (sort keys %{ $ftp->{'rename_files'} }) { | |||||||||||||||||||||||||||
| 96 | 12 | my $dst = $ftp->{'rename_files'}{ $src }; | |||||||||||||||||||||||||||
| 97 | 12 | my $try_count; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 12 | eval { | |||||||||||||||||||||||||||
| 100 | 12 | $try_count = $ftp->_op_retry('rename', $src, $dst); | |||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 12 | ▶ | if ($EVAL_ERROR =~ m/'rename' failed after \d+ attempts/ms) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | 4 | carp "Error renaming '$src' to '$dst'"; | |||||||||||||||||||||||||||
| 105 | 4 | next FILE_TO_RENAME; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | elsif ($EVAL_ERROR) { | ||||||||||||||||||||||||||||
| 108 | # Rethrow the exception if it's not recognized | ||||||||||||||||||||||||||||
| 109 | 0 | croak $EVAL_ERROR; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 8 | ▶ | if ($try_count > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 4 | carp "Rename of file from '$src' to '$dst' succeeded after" | |||||||||||||||||||||||||||
| 114 | . " $try_count tries"; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | push @successful_renames, $src; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | @successful_renames = sort @successful_renames; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | wantarray ? return @successful_renames | ||||||||||||||||||||||||||||
| 124 | 4 | ▶ | : return \@successful_renames | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | ; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub retrieve_files { | ||||||||||||||||||||||||||
|
Subroutine
retrieve_files: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | # Allow calls either as Net::FTP::Simple->retrieve_files or | ||||||||||||||||||||||||||||
| 131 | # Net::FTP::Simple::retrieve_files | ||||||||||||||||||||||||||||
| 132 | 0 | my ($opt_ref) = $_[-1]; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my @successful_transfers; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $ftp = Net::FTP::Simple->_new($opt_ref); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($ftp->{'remote_dir'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $ftp->_conn()->cwd($ftp->{'remote_dir'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | or croak $ftp->_error("Error changing to remote directory", | ||||||||||||||||||||||||||||
| 142 | "'$ftp->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (not exists $ftp->{'files'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (exists $ftp->{'file_filter'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $ftp->_list_and_filter(); # Populate $ftp->{'files'} | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | # Punt if we have neither files nor a file filter | ||||||||||||||||||||||||||||
| 151 | 0 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | FILES_TO_TRANSFER: | ||||||||||||||||||||||||||||
| 156 | 0 | for my $file (@{ $ftp->{'files'} }) { | |||||||||||||||||||||||||||
| 157 | 0 | my $basename = basename($file); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | unless ( $ftp->_conn()->get($file, $basename) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | carp $ftp->_error("Error getting file '$file'") | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | unless ($ftp->{'quiet_mode'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | next FILES_TO_TRANSFER; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | push @successful_transfers, $basename; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ($ftp->{'delete_after'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $ftp->_conn()->delete($file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | or carp $ftp->_error("Error deleting remote file '$file'"); | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | wantarray ? return @successful_transfers | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | : return \@successful_transfers | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | ; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | ▶ | sub list_files { | ||||||||||||||||||||||||||
|
Subroutine
list_files: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | # Allow calls either as Net::FTP::Simple->list_files or | ||||||||||||||||||||||||||||
| 181 | # Net::FTP::Simple::list_files | ||||||||||||||||||||||||||||
| 182 | 4 | my ($opt_ref) = $_[-1]; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 4 | my @remote_files; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | my $ftp = Net::FTP::Simple->_new($opt_ref); | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 4 | ▶ | if ($ftp->{'remote_dir'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | $ftp->_conn()->cwd($ftp->{'remote_dir'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | or croak $ftp->_error("Error changing to remote directory", | ||||||||||||||||||||||||||||
| 192 | "'$ftp->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 4 | @remote_files = $ftp->_list_and_filter(); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | wantarray ? return @remote_files | ||||||||||||||||||||||||||||
| 198 | 4 | ▶ | : return \@remote_files | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | ; | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | ####################################################################### | ||||||||||||||||||||||||||||
| 204 | # Private data | ||||||||||||||||||||||||||||
| 205 | ####################################################################### | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Error messages which indicate a possibly temporary error condition | ||||||||||||||||||||||||||||
| 208 | 3 | our %retryable_errors = ( | |||||||||||||||||||||||||||
| 209 | rename => [ | ||||||||||||||||||||||||||||
| 210 | qq/The process cannot access the file because / | ||||||||||||||||||||||||||||
| 211 | . qq/it is being used by another process/, | ||||||||||||||||||||||||||||
| 212 | ], | ||||||||||||||||||||||||||||
| 213 | ); | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # Maximum number of times to retry an operation. | ||||||||||||||||||||||||||||
| 216 | 3 | our %retry_max = ( | |||||||||||||||||||||||||||
| 217 | default => 0, | ||||||||||||||||||||||||||||
| 218 | rename => 3, | ||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # Time to wait on retry in seconds | ||||||||||||||||||||||||||||
| 222 | 3 | our %retry_wait = ( | |||||||||||||||||||||||||||
| 223 | default => 10, | ||||||||||||||||||||||||||||
| 224 | rename => 10, | ||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | ####################################################################### | ||||||||||||||||||||||||||||
| 229 | # Private class below here | ||||||||||||||||||||||||||||
| 230 | ####################################################################### | ||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | # Private constructor! | ||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||
| 234 | 22 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 235 | 22 | my ($class, $opt_ref) = @_; | |||||||||||||||||||||||||||
| 236 | 22 | my $obj = bless $opt_ref, $class; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Capture which of the wrapper subs called us so we can be identified as | ||||||||||||||||||||||||||||
| 239 | # that instead of the actual object. | ||||||||||||||||||||||||||||
| 240 | 22 | $obj->_set_caller( (caller(1))[3] ); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # mmm required options | ||||||||||||||||||||||||||||
| 243 | croak $obj->_caller(), " requires at least 'server' parameter" | ||||||||||||||||||||||||||||
| 244 | 22 | ▶ | unless($obj->{'server'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # | ||||||||||||||||||||||||||||
| 247 | # Allow the user to pass in an object instead of creating a new instance. | ||||||||||||||||||||||||||||
| 248 | # This allows test scripts to use a mock Net::FTP object and simulate a | ||||||||||||||||||||||||||||
| 249 | # number of different cases. | ||||||||||||||||||||||||||||
| 250 | # | ||||||||||||||||||||||||||||
| 251 | 21 | ▶ | unless ($obj->{'conn'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | my $ftpconn = Net::FTP->new($obj->{'server'}, | ||||||||||||||||||||||||||||
| 253 | 1 | ▶ | Debug => $obj->{'debug_ftp'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | or croak $obj->_error("Error creating Net::FTP object:", | ||||||||||||||||||||||||||||
| 255 | "'$EVAL_ERROR'"); | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | $obj->_set_conn($ftpconn); | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | else { | ||||||||||||||||||||||||||||
| 260 | 20 | $obj->_set_conn($obj->{'conn'}); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 20 | ▶ | $obj->_conn()->login( @{ $obj }{ qw( username password ) } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | or croak $obj->_error("Error logging in to '$obj->{'server'}'"); | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 20 | $obj->_setup_mode(); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 20 | return $obj; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 22 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 272 | 22 | my ($self) = shift; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 22 | ▶ | if($self->_conn()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 19 | ▶ | $self->_conn()->quit() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | or croak $self->_error("Error closing FTP connection:", | ||||||||||||||||||||||||||||
| 277 | "'$EVAL_ERROR'"); | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 22 | $self->_set_conn(undef); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 20 | ▶ | sub _setup_mode { | ||||||||||||||||||||||||||
|
Subroutine
_setup_mode: called CC=5 SCAR=21.9
| |||||||||||||||||||||||||||||
| 284 | 20 | my ($self) = shift; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 20 | ▶ | if (exists $self->{'mode'} and $self->{'mode'} eq 'ascii') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"mode"} and $self->{"mode"} eq "ascii"
MC/DC0%0/2
exists $self->{"mode"} and $self->{"mode"} eq "ascii"
exists $self->{"mode"}
$self->{"mode"} eq "ascii"
Truth table33%1/3
A and B
A= exists $self->{"mode"}B= $self->{"mode"} eq "ascii"
| |||||||||||||||||||||||||||||
| 287 | 0 | ▶ | $self->_conn()->ascii() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | or croak $self->_error('Error setting transfer mode to ascii'); | ||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||
| 291 | 20 | ▶ | $self->_conn()->binary() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | or croak $self->_error('Error setting transfer mode to binary'); | ||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | #sub _setup_connection { | ||||||||||||||||||||||||||||
| 297 | #my ($self) = shift; | ||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||
| 299 | #} | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | ####################################################################### | ||||||||||||||||||||||||||||
| 302 | # Accessors | ||||||||||||||||||||||||||||
| 303 | ####################################################################### | ||||||||||||||||||||||||||||
| 304 | 177 | ▶ | sub _conn { | ||||||||||||||||||||||||||
|
Subroutine
_conn: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 305 | 177 | my ($self) = shift; | |||||||||||||||||||||||||||
| 306 | 177 | return $self->{'connection'}; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 43 | ▶ | sub _set_conn { | ||||||||||||||||||||||||||
|
Subroutine
_set_conn: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 310 | 43 | my ($self) = shift; | |||||||||||||||||||||||||||
| 311 | 43 | my ($conn) = @_; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 43 | return $self->{'connection'} = $conn; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 5 | ▶ | sub _caller { | ||||||||||||||||||||||||||
|
Subroutine
_caller: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 317 | 5 | my ($self) = shift; | |||||||||||||||||||||||||||
| 318 | 5 | return $self->{'caller'}; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 22 | ▶ | sub _set_caller { | ||||||||||||||||||||||||||
|
Subroutine
_set_caller: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 322 | 22 | my ($self) = shift; | |||||||||||||||||||||||||||
| 323 | 22 | my ($caller) = @_; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # It should always be something; if _new is called directly (as it usually | ||||||||||||||||||||||||||||
| 326 | # shouldn't be, but may be in test scripts) | ||||||||||||||||||||||||||||
| 327 | 22 | ▶ | $caller = 'main' unless ($caller); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 22 | return $self->{'caller'} = $caller; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # | ||||||||||||||||||||||||||||
| 333 | # _error - Format the FTP error string to include the error code. | ||||||||||||||||||||||||||||
| 334 | # | ||||||||||||||||||||||||||||
| 335 | 2 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=3 SCAR=13.6
| |||||||||||||||||||||||||||||
| 336 | 2 | my ($self) = shift; | |||||||||||||||||||||||||||
| 337 | 2 | my ($msg) = join(" ", @_); | |||||||||||||||||||||||||||
| 338 | 2 | my $ftp_err = q{}; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # This may be called for errors other than those from Net::FTP | ||||||||||||||||||||||||||||
| 341 | # and there may not be a connection object | ||||||||||||||||||||||||||||
| 342 | 2 | ▶ | if ($self->_conn() and not $self->_conn()->ok()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_conn and not $self->_conn->ok
MC/DC0%0/2
$self->_conn and not $self->_conn->ok
$self->_conn
not $self->_conn->ok
Truth table33%1/3
A and not(B)
A= $self->_connB= not $self->_conn->ok
| |||||||||||||||||||||||||||||
| 343 | 0 | my $msg = $self->_conn()->message(); | |||||||||||||||||||||||||||
| 344 | 0 | chomp $msg; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 0 | $ftp_err = sprintf(q(: '%d %s'), $self->_conn()->code(), $msg); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 2 | return sprintf(q(%s: %s%s), | |||||||||||||||||||||||||||
| 350 | $self->_caller(), | ||||||||||||||||||||||||||||
| 351 | $msg, | ||||||||||||||||||||||||||||
| 352 | $ftp_err, | ||||||||||||||||||||||||||||
| 353 | ); | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub _create_and_cwd_remote_dir { | ||||||||||||||||||||||||||
|
Subroutine
_create_and_cwd_remote_dir: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 358 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | # Try first change to the directory | ||||||||||||||||||||||||||||
| 361 | 0 | ▶ | unless ($self->_conn()->cwd($self->{'remote_dir'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Give up now if user requested _not_ creating the remote | ||||||||||||||||||||||||||||
| 364 | # directory | ||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if ($self->{'disable_create_remote_dir'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | croak $self->_error("Error changing to remote directory", | |||||||||||||||||||||||||||
| 367 | "'$self->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Try to create the output path if it doesn't exist | ||||||||||||||||||||||||||||
| 371 | 0 | ▶ | $self->_conn()->mkdir($self->{'remote_dir'}, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | or croak $self->_error("Error making remote directory", | ||||||||||||||||||||||||||||
| 373 | "'$self->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | $self->_conn()->cwd($self->{'remote_dir'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | or croak $self->_error("Error changing to remote directory after", | ||||||||||||||||||||||||||||
| 377 | "creating '$self->{'remote_dir'}'"); | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 31 | ▶ | sub _op_retry { | ||||||||||||||||||||||||||
|
Subroutine
_op_retry: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 384 | 31 | my ($self, $op, @op_args) = @_; | |||||||||||||||||||||||||||
| 385 | 31 | my $conn = $self->_conn(); | |||||||||||||||||||||||||||
| 386 | 31 | my $try_count = 1; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 31 | ▶ | croak ref $conn, " cannot do '$op'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | unless ($conn->can($op)); | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | OP_TRY: | ||||||||||||||||||||||||||||
| 392 | 31 | ▶ | while(not $conn->$op(@op_args)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 35 | $try_count += 1; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 35 | ▶ | croak "'$op' failed after $try_count attempts" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | unless ( $self->_is_retryable_op($op, $try_count) ); | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 27 | $self->_sleep_for_op($op); | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 23 | return $try_count; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | # | ||||||||||||||||||||||||||||
| 405 | # Sleep for operation; returns nothing useful. | ||||||||||||||||||||||||||||
| 406 | # | ||||||||||||||||||||||||||||
| 407 | 27 | ▶ | sub _sleep_for_op { | ||||||||||||||||||||||||||
|
Subroutine
_sleep_for_op: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 408 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 409 | 27 | my ($op) = @_; | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | my $retry_wait = exists $retry_wait{ $op } ? $retry_wait{ $op } | ||||||||||||||||||||||||||||
| 412 | 27 | ▶ | : $retry_wait{ 'default' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | ; | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 27 | sleep $retry_wait; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | # | ||||||||||||||||||||||||||||
| 420 | # _is_retryable_op - Tests if a failing operation is retryable, | ||||||||||||||||||||||||||||
| 421 | # comparing both the error message and the retry count. | ||||||||||||||||||||||||||||
| 422 | # | ||||||||||||||||||||||||||||
| 423 | 35 | ▶ | sub _is_retryable_op { | ||||||||||||||||||||||||||
|
Subroutine
_is_retryable_op: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||
| 424 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 425 | 35 | my ($op, $count) = @_; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 35 | my $caller_error_message = $self->_conn()->message(); | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | my $retry_max = exists $retry_max{ $op } ? $retry_max{ $op } | ||||||||||||||||||||||||||||
| 430 | 35 | ▶ | : $retry_max{ 'default' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | ; | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 35 | ▶ | return unless (exists $retryable_errors{ $op }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 35 | ▶ | return if ($count > $retry_max); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 27 | for my $msg (@{ $retryable_errors{ $op } }) { | |||||||||||||||||||||||||||
| 437 | 27 | ▶ | if ($caller_error_message =~ m/$msg/ms) { # No 'x'! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | 27 | return 1; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # False if we fall through the loop | ||||||||||||||||||||||||||||
| 443 | 0 | return; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | # Assume everything else is set up | ||||||||||||||||||||||||||||
| 447 | 14 | ▶ | sub _list_and_filter { | ||||||||||||||||||||||||||
|
Subroutine
_list_and_filter: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 448 | 14 | my ($self) = shift; | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 14 | ▶ | my $filter = $self->{'file_filter'} || qr/./xms; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'file_filter'} || qr/./msx
MC/DC100%1/1
$self->{'file_filter'} || qr/./msx
$self->{'file_filter'}
Truth table100%2/2
A
A= $self->{'file_filter'}
| |||||||||||||||||||||||||||||
| 451 | 14 | my @remote_list; | |||||||||||||||||||||||||||
| 452 | my @remote_files; | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # FIXME I need to figure out how to distinguish between an empty list (no | ||||||||||||||||||||||||||||
| 455 | # files in the directory) and an error. Hopefully, if the directory | ||||||||||||||||||||||||||||
| 456 | # permissions are such that the directory cannot be listed, the cwd will | ||||||||||||||||||||||||||||
| 457 | # also fail (assuming one is done prior to this :\). Of course, it's | ||||||||||||||||||||||||||||
| 458 | # possible to have +x-r, but for now, just hope for the best :) | ||||||||||||||||||||||||||||
| 459 | 14 | ▶ | return unless (@remote_list = $self->_conn()->dir()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | REMOTE_LIST: | ||||||||||||||||||||||||||||
| 462 | 10 | for my $entry (@remote_list) { | |||||||||||||||||||||||||||
| 463 | 52 | chomp $entry; | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | # This correctly splits a line where the filename has spaces; the '9' | ||||||||||||||||||||||||||||
| 466 | # collects the 9th field and everything after into one item | ||||||||||||||||||||||||||||
| 467 | 52 | my ($mode, $filename) = (split /\s+/, $entry, 9)[0,-1]; | |||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | # Skip non-file things | ||||||||||||||||||||||||||||
| 470 | 52 | ▶ | next REMOTE_LIST unless ($mode && $mode =~ /\A-/xms); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode and $mode =~ /\A-/msx
MC/DC50%1/2
$mode and $mode =~ /\A-/msx
$mode
$mode =~ /\A-/msx
Truth table66%2/3
A and B
A= $modeB= $mode =~ /\A-/msx
| |||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | # Skip files not matching the filter | ||||||||||||||||||||||||||||
| 473 | 22 | ▶ | next REMOTE_LIST unless ($filename =~ m/$filter/xms); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 19 | push @remote_files, $filename; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 10 | push @{ $self->{'files'} }, @remote_files; | |||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | wantarray ? return @remote_files | ||||||||||||||||||||||||||||
| 481 | 10 | ▶ | : return \@remote_files | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | ; | ||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 3 | 1; | |||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | __END__ | ||||||||||||||||||||||||||||