| 1 | package Net::xFTP::SFTP; | ||
| 2 | |||
| 3 | sub new_sftp | ||
| 4 | { | ||
| 5 | my $subclass = shift; | ||
| 6 | my $pkg = shift; | ||
| 7 | my $host = shift; | ||
| 8 | my %args = @_; | ||
| 9 | my %xftp_args; | ||
| 10 | my $xftp = bless { }, $subclass; | ||
| 11 | $xftp->{BlockSize} = 10240; | ||
| 12 | if (defined $args{BlockSize}) | ||
| 13 | { | ||
| 14 | $xftp->{BlockSize} = $args{BlockSize} || 10240; | ||
| 15 | delete($args{BlockSize}); | ||
| 16 | } | ||
| 17 | my $saveEnvHome = $ENV{HOME}; | ||
| 18 | $ENV{HOME} = $xftp_args{home} if ($xftp_args{home}); | ||
| 19 | eval { $xftp->{xftp} = Net::SFTP->new($host, %args, warn => \&sftpWarnings); }; | ||
| 20 | $xftp->{xftp_lastmsg} = $@ if ($@); | ||
| 21 | $ENV{HOME} = $saveEnvHome || ''; | ||
| 22 | if ($xftp->{xftp}) | ||
| 23 | { | ||
| 24 | my $cwd; | ||
| 25 | eval { $cwd = $xftp->{xftp}->do_realpath('.') }; | ||
| 26 | $xftp->{cwd} = $cwd if ($cwd); | ||
| 27 | $xftp->{protocol} = 'Net::SFTP'; | ||
| 28 | return $xftp; | ||
| 29 | } | ||
| 30 | return undef; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub protocol | ||
| 34 | { | ||
| 35 | return shift->{protocol}; | ||
| 36 | } | ||
| 37 | |||
| 38 | { | ||
| 39 | no warnings 'redefine'; | ||
| 40 | sub cwd #SET THE "CURRENT" DIRECTORY. | ||
| 41 | { | ||
| 42 | my $self = shift; | ||
| 43 | my $cwd = shift || '/'; | ||
| 44 | |||
| 45 | my $ok; | ||
| 46 | my $fullwd; | ||
| 47 | #NEXT 22 ADDED 20060815 TO FIX RELATIVE PATH CHANGES. | ||
| 48 | if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o) | ||
| 49 | { | ||
| 50 | if ($self->{cwd} =~ m#\/$#o) | ||
| 51 | { | ||
| 52 | $cwd = $self->{cwd} . $cwd; | ||
| 53 | } | ||
| 54 | else | ||
| 55 | { | ||
| 56 | $cwd = $self->{cwd} . '/' . $cwd; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | elsif ($cwd eq '..') | ||
| 60 | { | ||
| 61 | $cwd = $self->{xftp}->do_realpath($self->{cwd}); | ||
| 62 | chop($cwd) if ($cwd =~ m#\/$#o); | ||
| 63 | $cwd =~ s#\/[^\/]+$##o; | ||
| 64 | $cwd ||= '/'; | ||
| 65 | } | ||
| 66 | elsif ($cwd eq '.') | ||
| 67 | { | ||
| 68 | $cwd = $self->{xftp}->do_realpath($self->{cwd}); | ||
| 69 | } | ||
| 70 | eval { $fullwd = $self->{xftp}->do_realpath($cwd) }; | ||
| 71 | if ($fullwd) | ||
| 72 | { | ||
| 73 | $self->{cwd} = $fullwd; | ||
| 74 | $ok = 1; | ||
| 75 | } | ||
| 76 | return $ok ? 1 : undef; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub copy | ||
| 80 | { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | return undef unless (@_ >= 2); | ||
| 84 | my @args = @_; | ||
| 85 | for (my $i=0;$i<=1;$i++) | ||
| 86 | { | ||
| 87 | $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 88 | } | ||
| 89 | if ($self->isadir($args[1])) | ||
| 90 | { | ||
| 91 | my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o); | ||
| 92 | $args[1] .= '/' unless ($args[1] =~ m#\/$#o); | ||
| 93 | $args[1] .= $filename; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $ok; | ||
| 97 | unless (defined($self->{haveSFTPConstants}) && $self->{haveSFTPConstants}) | ||
| 98 | { | ||
| 99 | $self->{xftp_lastmsg} = | ||
| 100 | "Copy failed (You must install the Net::SFTP::Constants Perl module!)"; | ||
| 101 | return undef; | ||
| 102 | } | ||
| 103 | my ($tmp, $t); | ||
| 104 | my $fromHandle; | ||
| 105 | eval { $fromHandle = $self->{xftp}->do_open($args[0], 0) }; | ||
| 106 | unless (defined($fromHandle) && $fromHandle) | ||
| 107 | { | ||
| 108 | $self->{xftp_lastmsg} = "Copy failed (". ($@||'do_open1 failed - Unknown reason') | ||
| 109 | . ')!'; | ||
| 110 | return undef; | ||
| 111 | } | ||
| 112 | my $offset = 0; | ||
| 113 | my $err; | ||
| 114 | while (1) | ||
| 115 | { | ||
| 116 | ($tmp, $err) = $self->{xftp}->do_read($fromHandle, $offset, $self->{BlockSize}); | ||
| 117 | last if (defined $err); | ||
| 118 | $t .= $tmp; | ||
| 119 | $offset += $self->{BlockSize}; | ||
| 120 | } | ||
| 121 | $self->{xftp}->do_close($fromHandle); | ||
| 122 | my $toHandle; | ||
| 123 | eval { no strict 'subs'; $toHandle = $self->{xftp}->do_open($args[1], | ||
| 124 | SSH2_FXF_WRITE | SSH2_FXF_CREAT | SSH2_FXF_TRUNC) }; | ||
| 125 | unless (defined($toHandle) && $toHandle) | ||
| 126 | { | ||
| 127 | $self->{xftp_lastmsg} = "Copy failed (". ($@||'do_open2 failed - Unknown reason') | ||
| 128 | . ')!'; | ||
| 129 | return undef; | ||
| 130 | } | ||
| 131 | eval { $self->{xftp}->do_write($toHandle, 0, $t) }; | ||
| 132 | if ($@) | ||
| 133 | { | ||
| 134 | $self->{xftp_lastmsg} = "Copy failed (". ($@||'write failed - Unknown reason') | ||
| 135 | . ')!'; | ||
| 136 | return undef; | ||
| 137 | } | ||
| 138 | $self->{xftp}->do_close($toHandle); | ||
| 139 | $ok = 1; | ||
| 140 | return $ok ? 1 : undef; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub move | ||
| 144 | { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | return undef unless (@_ >= 2); | ||
| 148 | return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | sub ascii | ||
| 153 | { | ||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | return undef; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub binary | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | return undef; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub quit | ||
| 167 | { | ||
| 168 | my $self = shift; | ||
| 169 | $self->{xftp} = undef; | ||
| 170 | delete($self->{xftp}); | ||
| 171 | |||
| 172 | return; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub ls | ||
| 176 | { | ||
| 177 | my $self = shift; | ||
| 178 | my $path = shift || ''; | ||
| 179 | my $showall = shift || 0; | ||
| 180 | my @dirlist; | ||
| 181 | my $realpath; | ||
| 182 | eval { $realpath = $self->{xftp}->do_realpath($path||$self->{cwd}||'.') }; | ||
| 183 | chomp $realpath; | ||
| 184 | $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 185 | my @dirHash; | ||
| 186 | eval { @dirHash = $self->{xftp}->ls($realpath) }; | ||
| 187 | if ($@) | ||
| 188 | { | ||
| 189 | $self->{xftp_lastmsg} = $@; | ||
| 190 | my $err = $self->{xftp}->status; | ||
| 191 | return if ($err); | ||
| 192 | } | ||
| 193 | return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY. | ||
| 194 | shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE! | ||
| 195 | my $t; | ||
| 196 | @dirlist = (); | ||
| 197 | for (my $i=0;$i<=$#dirHash;$i++) | ||
| 198 | { | ||
| 199 | $t = $dirHash[$i]->{filename}; | ||
| 200 | next if ($t eq '..' && $path eq '/'); | ||
| 201 | next if (!$showall && $t =~ /^\.[^\.]/o); | ||
| 202 | push (@dirlist, $t); | ||
| 203 | } | ||
| 204 | @dirlist = sort @dirlist; | ||
| 205 | |||
| 206 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 207 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..'); | ||
| 208 | #unshift (@dirlist, '.') unless ($dirlist[0] eq '.'); | ||
| 209 | |||
| 210 | return wantarray ? @dirlist : \@dirlist; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub dir | ||
| 214 | { | ||
| 215 | my $self = shift; | ||
| 216 | my $path = shift || ''; | ||
| 217 | my $showall = shift || 0; | ||
| 218 | my @dirlist; | ||
| 219 | my $realpath; | ||
| 220 | eval { $realpath = $self->{xftp}->do_realpath($path||$self->{cwd}||'.') }; | ||
| 221 | chomp $realpath; | ||
| 222 | $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 223 | my @dirHash; | ||
| 224 | eval { @dirHash = $self->{xftp}->ls($realpath) }; | ||
| 225 | #return if ($@); | ||
| 226 | if ($@) | ||
| 227 | { | ||
| 228 | $self->{xftp_lastmsg} = $@; | ||
| 229 | my $err = $self->{xftp}->status; | ||
| 230 | return if ($err); | ||
| 231 | } | ||
| 232 | return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY. | ||
| 233 | shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE! | ||
| 234 | my $t; | ||
| 235 | @dirlist = (); | ||
| 236 | #for (my $i=0;$i<=$#dirHash;$i++) | ||
| 237 | foreach my $i (sort { $a->{filename} cmp $b->{filename} } @dirHash) | ||
| 238 | { | ||
| 239 | #$t = $dirHash[$i]->{longname}; | ||
| 240 | $t = $i->{longname}; | ||
| 241 | next if ($t =~ /\d \.\.$/o && $path eq '/'); | ||
| 242 | next if (!$showall && $t =~ /\d \.[^\.]\S*$/o); | ||
| 243 | push (@dirlist, $t); | ||
| 244 | } | ||
| 245 | |||
| 246 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 247 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/); | ||
| 248 | #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/); | ||
| 249 | |||
| 250 | return wantarray ? @dirlist : \@dirlist; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY. | ||
| 254 | { | ||
| 255 | my $self = shift; | ||
| 256 | |||
| 257 | return $self->{cwd}; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub get #(Remote, => Local) | ||
| 261 | { | ||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | return undef unless (@_ >= 1); | ||
| 265 | my @args = @_; | ||
| 266 | $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 267 | if (@args >= 2) | ||
| 268 | { | ||
| 269 | $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io); | ||
| 270 | } | ||
| 271 | else | ||
| 272 | { | ||
| 273 | if (ref(\$args[0]) eq 'GLOB') | ||
| 274 | { | ||
| 275 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 276 | return undef; | ||
| 277 | } | ||
| 278 | $args[1] = $args[0]; | ||
| 279 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 280 | } | ||
| 281 | my $ok; | ||
| 282 | if (ref(\$_[1]) eq 'GLOB') | ||
| 283 | { | ||
| 284 | my $remoteHandle; | ||
| 285 | my $offset = 0; | ||
| 286 | my $buff; | ||
| 287 | my $unsubscriptedFH = $_[1]; | ||
| 288 | eval { $remoteHandle = $self->{xftp}->do_open($args[0], 0) }; | ||
| 289 | if ($remoteHandle) | ||
| 290 | { | ||
| 291 | my $err; | ||
| 292 | while (1) | ||
| 293 | { | ||
| 294 | ($buff, $err) = $self->{xftp}->do_read($remoteHandle, $offset, $self->{BlockSize}); | ||
| 295 | last if (defined $err); | ||
| 296 | print $unsubscriptedFH $buff; | ||
| 297 | $offset += $self->{BlockSize}; | ||
| 298 | } | ||
| 299 | $self->{xftp}->do_close($remoteHandle); | ||
| 300 | return 1; | ||
| 301 | } | ||
| 302 | else | ||
| 303 | { | ||
| 304 | $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!'; | ||
| 305 | return undef; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | else | ||
| 309 | { | ||
| 310 | eval { $self->{xftp}->get(@args) }; | ||
| 311 | if ($@) | ||
| 312 | { | ||
| 313 | $self->{xftp_lastmsg} = $@; | ||
| 314 | $ok = $self->{xftp}->status; | ||
| 315 | return $ok ? undef : 1; | ||
| 316 | } | ||
| 317 | else | ||
| 318 | { | ||
| 319 | return 1; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | return $ok ? 1 : undef; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS. | ||
| 326 | { | ||
| 327 | my $self = shift; | ||
| 328 | |||
| 329 | return undef unless (@_ >= 1); | ||
| 330 | my @args = @_; | ||
| 331 | if (@args >= 2) | ||
| 332 | { | ||
| 333 | $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io); | ||
| 334 | } | ||
| 335 | else | ||
| 336 | { | ||
| 337 | if (ref(\$args[0]) =~ /GLOB/io) | ||
| 338 | { | ||
| 339 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 340 | return undef; | ||
| 341 | } | ||
| 342 | $args[1] = $args[0]; | ||
| 343 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 344 | } | ||
| 345 | $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 346 | |||
| 347 | my $ok; | ||
| 348 | if (ref(\$_[0]) eq 'GLOB') | ||
| 349 | { | ||
| 350 | unless (defined($self->{haveSFTPConstants}) && $self->{haveSFTPConstants}) | ||
| 351 | { | ||
| 352 | $self->{xftp_lastmsg} = | ||
| 353 | "Copy failed (You must install the Net::SFTP::Constants Perl module!)"; | ||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | my $remoteHandle; | ||
| 357 | my $offset = 0; | ||
| 358 | my $buff; | ||
| 359 | my $unsubscriptedFH = $_[0]; | ||
| 360 | eval { no strict 'subs'; $remoteHandle = $self->{xftp}->do_open($args[1], | ||
| 361 | SSH2_FXF_WRITE | SSH2_FXF_CREAT | SSH2_FXF_TRUNC) }; | ||
| 362 | if ($remoteHandle) | ||
| 363 | { | ||
| 364 | my $t; | ||
| 365 | while ($buff = <$unsubscriptedFH>) | ||
| 366 | { | ||
| 367 | $t .= $buff; | ||
| 368 | } | ||
| 369 | eval { $self->{xftp}->do_write($remoteHandle, 0, $t) }; | ||
| 370 | if ($@) | ||
| 371 | { | ||
| 372 | $self->{xftp_lastmsg} = $@; | ||
| 373 | $ok = $self->{xftp}->status; | ||
| 374 | return $ok ? undef : 1; | ||
| 375 | } | ||
| 376 | else | ||
| 377 | { | ||
| 378 | return 1; | ||
| 379 | } | ||
| 380 | $self->{xftp}->do_close($remoteHandle); | ||
| 381 | return 1; | ||
| 382 | } | ||
| 383 | else | ||
| 384 | { | ||
| 385 | $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!'; | ||
| 386 | return undef; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | else | ||
| 390 | { | ||
| 391 | eval { $ok = $self->{xftp}->put(@args) }; | ||
| 392 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 393 | } | ||
| 394 | return $ok ? 1 : undef; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub delete #RETURNED OK=2 WHEN LAST FAILED. | ||
| 398 | { | ||
| 399 | my $self = shift; | ||
| 400 | my $path = shift; | ||
| 401 | |||
| 402 | my $ok; | ||
| 403 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 404 | eval { $ok = $self->{xftp}->do_remove($path) }; | ||
| 405 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 406 | return $@ ? undef : 1; | ||
| 407 | } | ||
| 408 | |||
| 409 | sub rename | ||
| 410 | { | ||
| 411 | my $self = shift; | ||
| 412 | return undef unless (@_ == 2); | ||
| 413 | |||
| 414 | my ($oldfile, $newfile) = @_; | ||
| 415 | |||
| 416 | my $ok; | ||
| 417 | $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 418 | $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 419 | eval { $ok = $self->{xftp}->do_rename($oldfile, $newfile) }; | ||
| 420 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 421 | return $@ ? undef : 1; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub mkdir | ||
| 425 | { | ||
| 426 | my $self = shift; | ||
| 427 | my $path = shift; | ||
| 428 | my $tryRecursion = shift||0; | ||
| 429 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 430 | |||
| 431 | my @pathStack; | ||
| 432 | my $ok = ''; | ||
| 433 | my $orgPath = $path; | ||
| 434 | my $didRecursion = 0; | ||
| 435 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 436 | while ($path) | ||
| 437 | { | ||
| 438 | $path =~ s#[^\/\\]+$##o; | ||
| 439 | $path =~ s#[\/\\]$##o; | ||
| 440 | $path = '/' unless ($path); | ||
| 441 | last if ($self->isadir($path)); | ||
| 442 | if ($tryRecursion) | ||
| 443 | { | ||
| 444 | push (@pathStack, $path); | ||
| 445 | $didRecursion = 1; | ||
| 446 | last if ($path eq '/'); | ||
| 447 | } | ||
| 448 | else | ||
| 449 | { | ||
| 450 | $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!"; | ||
| 451 | return undef; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | if ($didRecursion) | ||
| 455 | { | ||
| 456 | while (@pathStack) | ||
| 457 | { | ||
| 458 | $path = pop @pathStack; | ||
| 459 | eval { $ok = $self->{xftp}->do_mkdir($path, Net::SFTP::Attributes->new()) }; | ||
| 460 | if ($@) | ||
| 461 | { | ||
| 462 | $self->{xftp_lastmsg} = $@; | ||
| 463 | return undef; | ||
| 464 | } | ||
| 465 | next; | ||
| 466 | } | ||
| 467 | } | ||
| 468 | eval { $ok = $self->{xftp}->do_mkdir($orgPath, Net::SFTP::Attributes->new()) }; | ||
| 469 | if ($@) | ||
| 470 | { | ||
| 471 | $self->{xftp_lastmsg} = $@; | ||
| 472 | return undef; | ||
| 473 | } | ||
| 474 | return 1; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub rmdir | ||
| 478 | { | ||
| 479 | my $self = shift; | ||
| 480 | my $path = shift; | ||
| 481 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 482 | |||
| 483 | my $ok; | ||
| 484 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 485 | eval { $ok = $self->{xftp}->do_rmdir($path) }; | ||
| 486 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 487 | return $@ ? undef : 1; | ||
| 488 | } | ||
| 489 | |||
| 490 | sub message | ||
| 491 | { | ||
| 492 | my $self = shift; | ||
| 493 | |||
| 494 | chomp $self->{xftp_lastmsg}; | ||
| 495 | my @res = $self->{xftp}->status; | ||
| 496 | return ($self->{xftp_lastmsg} =~ /^\s*$res[0]/) ? $self->{xftp_lastmsg} : "$res[0]: $res[1] - $self->{xftp_lastmsg}"; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub sftpWarnings #ONLY WAY TO GET NON-FATAL WARNINGS INTO $@ INSTEAD OF STDERR IS TO USE THIS CALLBACK! | ||
| 500 | { #(WE ALWAYS WRAP SFTP->METHODS W/AN EVAL)! | ||
| 501 | my $self = shift; | ||
| 502 | my @res = $self->status; | ||
| 503 | die "$res[0]: $res[1] - ".join(' ', @_)."\n"; | ||
| 504 | } | ||
| 505 | |||
| 506 | sub mdtm | ||
| 507 | { | ||
| 508 | my $self = shift; | ||
| 509 | my $path = shift; | ||
| 510 | |||
| 511 | my $ok; | ||
| 512 | $path = $self->{cwd} . '/' . $path unless ($path =~ | ||
| 513 | m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 514 | eval { $ok = $self->{xftp}->do_stat($path) }; | ||
| 515 | unless (defined($ok) && $ok) | ||
| 516 | { | ||
| 517 | $self->{xftp_lastmsg} = $@; | ||
| 518 | return undef; | ||
| 519 | } | ||
| 520 | return $ok->mtime(); | ||
| 521 | } | ||
| 522 | |||
| 523 | sub size | ||
| 524 | { | ||
| 525 | my $self = shift; | ||
| 526 | my $path = shift; | ||
| 527 | |||
| 528 | my $ok; | ||
| 529 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 530 | eval { $ok = $self->{xftp}->do_stat($path) }; | ||
| 531 | unless (defined($ok) && $ok) | ||
| 532 | { | ||
| 533 | $self->{xftp_lastmsg} = $@; | ||
| 534 | return undef; | ||
| 535 | } | ||
| 536 | return $ok->size(); | ||
| 537 | } | ||
| 538 | |||
| 539 | sub isadir | ||
| 540 | { | ||
| 541 | my $self = shift; | ||
| 542 | my $path = shift; | ||
| 543 | |||
| 544 | my $ok; | ||
| 545 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 546 | eval { $ok = $self->{xftp}->do_opendir($path) }; | ||
| 547 | if (defined($ok) && $ok) | ||
| 548 | { | ||
| 549 | eval { $self->{xftp}->do_close($ok) }; | ||
| 550 | return 1; | ||
| 551 | } | ||
| 552 | return 0; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub chmod | ||
| 556 | { | ||
| 557 | my $self = shift; | ||
| 558 | my $permissions = shift; | ||
| 559 | my $path = shift; | ||
| 560 | |||
| 561 | my ($ok, $attrs); | ||
| 562 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 563 | eval { $attrs = $self->{xftp}->do_stat($path) }; | ||
| 564 | unless (defined($attrs) && $attrs) | ||
| 565 | { | ||
| 566 | $self->{xftp_lastmsg} = $@; | ||
| 567 | return undef; | ||
| 568 | } | ||
| 569 | eval "\$permissions = 0$permissions"; | ||
| 570 | if ($@) | ||
| 571 | { | ||
| 572 | $self->{xftp_lastmsg} = "Invalid permissions (000-777) - $@"; | ||
| 573 | return undef; | ||
| 574 | } | ||
| 575 | $attrs->perm($permissions); | ||
| 576 | eval { $ok = $self->{xftp}->do_setstat($path, $attrs) }; | ||
| 577 | if ($@ || !defined($ok)) | ||
| 578 | { | ||
| 579 | $self->{xftp_lastmsg} = $@; | ||
| 580 | return undef; | ||
| 581 | } | ||
| 582 | return 1; | ||
| 583 | } | ||
| 584 | |||
| 585 | sub method | ||
| 586 | { | ||
| 587 | my $self = shift; | ||
| 588 | my $method = shift; | ||
| 589 | |||
| 590 | for (my $i=0;$i<scalar(@_);$i++) | ||
| 591 | { | ||
| 592 | $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o); | ||
| 593 | } | ||
| 594 | my $res; | ||
| 595 | my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")"; | ||
| 596 | eval $xeq; | ||
| 597 | if ($@) | ||
| 598 | { | ||
| 599 | $self->{xftp_lastmsg} = "method Net::SFTP::$method(".join(',',@_).") returned $@"; | ||
| 600 | return undef; | ||
| 601 | } | ||
| 602 | return $res; | ||
| 603 | } | ||
| 604 | |||
| 605 | 1 |