| 1 | package Net::xFTP::SSH2; | ||
| 2 | |||
| 3 | use Fcntl ':mode'; | ||
| 4 | use File::Copy; | ||
| 5 | |||
| 6 | my $bummer = ($^O =~ /Win/); | ||
| 7 | my @permvec = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx'); | ||
| 8 | |||
| 9 | sub new_ssh2 | ||
| 10 | { | ||
| 11 | my $subclass = shift; | ||
| 12 | my $pkg = shift; | ||
| 13 | my $host = shift; | ||
| 14 | my %args = @_; | ||
| 15 | my %xftp_args; | ||
| 16 | my $xftp = bless { }, $subclass; | ||
| 17 | $xftp->{BlockSize} = 10240; | ||
| 18 | if (defined $args{BlockSize}) | ||
| 19 | { | ||
| 20 | $xftp->{BlockSize} = $args{BlockSize} || 10240; | ||
| 21 | delete($args{BlockSize}); | ||
| 22 | } | ||
| 23 | my $saveEnvHome = $ENV{HOME}; | ||
| 24 | # $ENV{HOME} = $xftp_args{home} if ($xftp_args{home}); | ||
| 25 | $xftp->{xftp} = Net::SSH2->new(); | ||
| 26 | unless (defined $xftp->{xftp}) | ||
| 27 | { | ||
| 28 | $xftp->{xftp_lastmsg} = $@; | ||
| 29 | return; | ||
| 30 | } | ||
| 31 | # $ENV{HOME} = $saveEnvHome || ''; | ||
| 32 | if ($xftp->{xftp}) | ||
| 33 | { | ||
| 34 | my $ok; | ||
| 35 | $args{user} ||= 'anonymous'; | ||
| 36 | if (defined($args{port}) || defined($args{login_timeout}) || defined($args{compress})) | ||
| 37 | { | ||
| 38 | $args{port} ||= 22; | ||
| 39 | my @loginargs; | ||
| 40 | @loginargs = ($host, $args{port}); | ||
| 41 | push (@loginargs, 'Timeout', $args{connect_timeout}) if (defined $args{connect_timeout}); | ||
| 42 | push (@loginargs, 'Compress', $args{compress}) if (defined $args{compress}); | ||
| 43 | $ok = $xftp->{xftp}->connect(@loginargs); | ||
| 44 | delete($args{port}); | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | $ok = $xftp->{xftp}->connect($host); | ||
| 49 | } | ||
| 50 | unless (defined($ok) && $ok) | ||
| 51 | { | ||
| 52 | $xftp->{xftp_lastmsg} = "new:connect($host):Could not connect to host(" | ||
| 53 | .join(' ', $xftp->{xftp}->error()).")!"; | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | if (defined $args{timeout}) | ||
| 57 | { | ||
| 58 | $xftp->{xftp}->timeout($args{timeout}); | ||
| 59 | delete($args{timeout}); | ||
| 60 | } | ||
| 61 | my $authRank; | ||
| 62 | my $auth; | ||
| 63 | my $authTimeout = $args{'login_timeout'} || 0; | ||
| 64 | if (defined $args{rank}) | ||
| 65 | { | ||
| 66 | $authRank = ref($args{rank}) ? $args{rank} : [$args{rank}]; | ||
| 67 | delete($args{rank}); | ||
| 68 | $args{username} ||= $args{user}; | ||
| 69 | $args{hostname} ||= $host; | ||
| 70 | foreach my $arg (keys %args) | ||
| 71 | { | ||
| 72 | delete($args{$arg}) unless ($arg =~ /^(?:username|password|callback|publickey|privatekey|hostname|passphrase|local_username|interact|fallback|cb_keyboard|cb_password)$/o); | ||
| 73 | } | ||
| 74 | $SIG{ALRM} = sub { die "timeout" }; | ||
| 75 | eval | ||
| 76 | { | ||
| 77 | alarm($authTimeout) if ($authTimeout); | ||
| 78 | $auth = $xftp->{xftp}->auth(rank => $authRank, %args); | ||
| 79 | }; | ||
| 80 | my $at = $@; | ||
| 81 | $xftp->{xftp_lastmsg} = $at if ($at); | ||
| 82 | alarm(0); | ||
| 83 | $@ = $at; | ||
| 84 | return if ($at =~ /timeout/io); | ||
| 85 | if ($auth) | ||
| 86 | { | ||
| 87 | $xftp->{sshsftp} = $xftp->{xftp}->sftp(); | ||
| 88 | unless ($xftp->{sshsftp}) | ||
| 89 | { | ||
| 90 | $xftp->{xftp_lastmsg} = 'sftp:auth_password:Could not authenticate, bad password(' | ||
| 91 | .join(' ', $xftp->{xftp}->error()).')?'; | ||
| 92 | $@ = $xftp->{xftp_lastmsg}; | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | my $cwd = $xftp->{sshsftp}->realpath('.'); | ||
| 96 | $xftp->{cwd} = $cwd if ($cwd); | ||
| 97 | return $xftp; | ||
| 98 | } | ||
| 99 | else | ||
| 100 | { | ||
| 101 | $@ ||= join(' ', $xftp->{xftp}->error()); | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | $args{password} ||= 'anonymous@' if ($args{user} eq 'anonymous'); | ||
| 108 | my @loginargs = ($args{user}, $args{password}); | ||
| 109 | push (@loginargs, $args{account}) if (defined $args{account}); | ||
| 110 | $SIG{ALRM} = sub { die "timeout" }; | ||
| 111 | eval | ||
| 112 | { | ||
| 113 | alarm($authTimeout) if ($authTimeout); | ||
| 114 | $auth = $xftp->{xftp}->auth_password(@loginargs); | ||
| 115 | }; | ||
| 116 | my $at = $@; | ||
| 117 | $xftp->{xftp_lastmsg} = $at if ($at); | ||
| 118 | alarm(0); | ||
| 119 | $@ = $at; | ||
| 120 | return if ($at =~ /timeout/io); | ||
| 121 | if ($auth) | ||
| 122 | { | ||
| 123 | $xftp->{sshsftp} = $xftp->{xftp}->sftp(); | ||
| 124 | unless ($xftp->{sshsftp}) | ||
| 125 | { | ||
| 126 | $xftp->{xftp_lastmsg} = 'sftp:auth_password:Could not authenticate, bad password(' | ||
| 127 | .join(' ', $xftp->{xftp}->error()).')?'; | ||
| 128 | $@ = $xftp->{xftp_lastmsg}; | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | my $cwd = $xftp->{sshsftp}->realpath('.'); | ||
| 132 | $xftp->{cwd} = $cwd if ($cwd); | ||
| 133 | return $xftp; | ||
| 134 | } | ||
| 135 | else | ||
| 136 | { | ||
| 137 | $@ ||= join(' ', $xftp->{xftp}->error()); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | $@ ||= 'Invalid Password?'; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | else | ||
| 145 | { | ||
| 146 | $@ ||= 'Could not create Net::SSH2(::new) object?!'; | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | { | ||
| 152 | no warnings 'redefine'; | ||
| 153 | sub cwd #SET THE "CURRENT" DIRECTORY. | ||
| 154 | { | ||
| 155 | my $self = shift; | ||
| 156 | my $cwd = shift || '/'; | ||
| 157 | |||
| 158 | my $ok; | ||
| 159 | $ok = $self->{sshsftp}->realpath($cwd); | ||
| 160 | $self->{cwd} = $ok if ($ok); | ||
| 161 | return $ok ? 1 : undef; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub copy | ||
| 165 | { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | return undef unless (@_ >= 2); | ||
| 169 | my @args = @_; | ||
| 170 | for (my $i=0;$i<=1;$i++) | ||
| 171 | { | ||
| 172 | $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 173 | } | ||
| 174 | if ($self->isadir($args[1])) | ||
| 175 | { | ||
| 176 | my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o); | ||
| 177 | $args[1] .= '/' unless ($args[1] =~ m#\/$#o); | ||
| 178 | $args[1] .= $filename; | ||
| 179 | } | ||
| 180 | |||
| 181 | my $ok; | ||
| 182 | my ($tmp, $t, $bytecnt); | ||
| 183 | my $fromHandle; | ||
| 184 | eval { $fromHandle = $self->{sshsftp}->open($args[0], 0) }; | ||
| 185 | if ($fromHandle) | ||
| 186 | { | ||
| 187 | my $err; | ||
| 188 | $t = ''; | ||
| 189 | my $offset = 0; | ||
| 190 | while (1) | ||
| 191 | { | ||
| 192 | $bytecnt = $fromHandle->read($tmp, $self->{BlockSize}); | ||
| 193 | last unless (defined($bytecnt) && $bytecnt > 0); | ||
| 194 | $t .= $tmp; | ||
| 195 | $offset += $bytecnt; | ||
| 196 | $fromHandle->seek($offset); | ||
| 197 | } | ||
| 198 | return 1 if ($offset); | ||
| 199 | $self->{xftp_lastmsg} = "copy:open($args[0], 0):Could not get data into filehandle(" | ||
| 200 | .join(' ', $self->{sshsftp}->error()).")!"; | ||
| 201 | return undef; | ||
| 202 | } | ||
| 203 | else | ||
| 204 | { | ||
| 205 | $self->{xftp_lastmsg} = "copy:open($args[0], 0):Could not open remote file to copy from(" | ||
| 206 | .join(' ', $self->{sshsftp}->error()).")!"; | ||
| 207 | return undef; | ||
| 208 | } | ||
| 209 | my $toHandle; | ||
| 210 | eval | ||
| 211 | { | ||
| 212 | no strict 'subs'; | ||
| 213 | my $openFlags = O_RDWR|O_CREAT|O_TRUNC; | ||
| 214 | $toHandle = $self->{sshsftp}->open($args[1], $openFlags); | ||
| 215 | }; | ||
| 216 | if ($toHandle) | ||
| 217 | { | ||
| 218 | my $bytecnt = $toHandle->write($t); | ||
| 219 | return 1 if ($bytecnt); | ||
| 220 | $self->{xftp_lastmsg} = "copy:open($args[1]):Could not copy file(" | ||
| 221 | .join(' ', $self->{sshsftp}->error()).")!"; | ||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | else | ||
| 225 | { | ||
| 226 | $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!'; | ||
| 227 | return undef; | ||
| 228 | } | ||
| 229 | return $ok ? 1 : undef; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub move | ||
| 233 | { | ||
| 234 | my $self = shift; | ||
| 235 | |||
| 236 | return undef unless (@_ >= 2); | ||
| 237 | return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | sub ascii | ||
| 242 | { | ||
| 243 | my $self = shift; | ||
| 244 | |||
| 245 | return undef; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub binary | ||
| 249 | { | ||
| 250 | my $self = shift; | ||
| 251 | |||
| 252 | return undef; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub quit | ||
| 256 | { | ||
| 257 | my $self = shift; | ||
| 258 | $self->{xftp} = undef; | ||
| 259 | delete($self->{xftp}); | ||
| 260 | |||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub ls | ||
| 265 | { | ||
| 266 | my $self = shift; | ||
| 267 | my $path = shift || ''; | ||
| 268 | my $showall = shift || 0; | ||
| 269 | my @dirlist; | ||
| 270 | my $realpath = $self->{sshsftp}->realpath($path||$self->{cwd}||'.'); | ||
| 271 | # chomp $realpath; | ||
| 272 | $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 273 | my $sshdir = $self->{sshsftp}->opendir($realpath); | ||
| 274 | if ($sshdir) | ||
| 275 | { | ||
| 276 | my ($h, @tm, $mtimeStr, $filetype); | ||
| 277 | while ($h = $sshdir->read()) | ||
| 278 | { | ||
| 279 | next if ($h->{name} =~ /\d \.\.$/o && $path eq '/'); | ||
| 280 | next if (!$showall && $h->{name} =~ /^\.[^\.]\S*$/o); | ||
| 281 | $filetype = &getPermStr($h->{mode}); | ||
| 282 | @tm = localtime($h->{mtime}); | ||
| 283 | push (@dirlist, $h->{name}); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | else | ||
| 287 | { | ||
| 288 | $self->{xftp_lastmsg} = "ls:opendir($realpath):Could not open directory(" | ||
| 289 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | @dirlist = sort @dirlist; | ||
| 293 | |||
| 294 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 295 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..'); | ||
| 296 | #unshift (@dirlist, '.') unless ($dirlist[0] eq '.'); | ||
| 297 | |||
| 298 | return wantarray ? @dirlist : \@dirlist; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub dir | ||
| 302 | { | ||
| 303 | my $self = shift; | ||
| 304 | my $path = shift || ''; | ||
| 305 | my $showall = shift || 0; | ||
| 306 | my @dirlist; | ||
| 307 | my $realpath = $self->{sshsftp}->realpath($path||$self->{cwd}||'.'); | ||
| 308 | # chomp $realpath; | ||
| 309 | $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 310 | my $sshdir = $self->{sshsftp}->opendir($realpath); | ||
| 311 | if ($sshdir) | ||
| 312 | { | ||
| 313 | my ($h, @tm, $mtimeStr, $filetype); | ||
| 314 | while ($h = $sshdir->read()) | ||
| 315 | { | ||
| 316 | next if ($h->{name} =~ /\d \.\.$/o && $path eq '/'); | ||
| 317 | next if (!$showall && $h->{name} =~ /^\.[^\.]\S*$/o); | ||
| 318 | $filetype = &getPermStr($h->{mode}); | ||
| 319 | @tm = localtime($h->{mtime}); | ||
| 320 | push (@dirlist, sprintf "%s\x02%10s %2d %8s %8s %4d-%2.2d-%2.2d %2.2d:%2.2d %s\n", | ||
| 321 | $h->{name}, $filetype, $h->{uid}, $h->{gid}, $h->{size}, | ||
| 322 | $tm[5]+1900, $tm[4]+1, $tm[3], $tm[2], $tm[1], $h->{name}); | ||
| 323 | } | ||
| 324 | @dirlist = sort(@dirlist); | ||
| 325 | for (my $i=0;$i<=$#dirlist;$i++) | ||
| 326 | { | ||
| 327 | $dirlist[$i] =~ s/^[^\x02]*\x02//so; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | else | ||
| 331 | { | ||
| 332 | $self->{xftp_lastmsg} = "dir:opendir($realpath):Could not open directory(" | ||
| 333 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 334 | return; | ||
| 335 | } | ||
| 336 | |||
| 337 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 338 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/); | ||
| 339 | #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/); | ||
| 340 | |||
| 341 | return wantarray ? @dirlist : \@dirlist; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY. | ||
| 345 | { | ||
| 346 | my $self = shift; | ||
| 347 | |||
| 348 | return $self->{cwd}; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub get #(Remote, => Local) | ||
| 352 | { | ||
| 353 | my $self = shift; | ||
| 354 | |||
| 355 | return undef unless (@_ >= 1); | ||
| 356 | my @args = @_; | ||
| 357 | $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 358 | if (@args >= 2) | ||
| 359 | { | ||
| 360 | $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io); | ||
| 361 | } | ||
| 362 | else | ||
| 363 | { | ||
| 364 | if (ref(\$args[0]) =~ /GLOB/io) | ||
| 365 | { | ||
| 366 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 367 | return undef; | ||
| 368 | } | ||
| 369 | $args[1] = $args[0]; | ||
| 370 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 371 | } | ||
| 372 | my $ok; | ||
| 373 | if (ref(\$_[1]) =~ /GLOB/io) | ||
| 374 | { | ||
| 375 | my $remoteHandle; | ||
| 376 | my $offset = 0; | ||
| 377 | my $buff; | ||
| 378 | my $bytecnt; | ||
| 379 | my $unsubscriptedFH = $_[1]; | ||
| 380 | eval { $remoteHandle = $self->{sshsftp}->open($args[0], 0) }; | ||
| 381 | if ($remoteHandle) | ||
| 382 | { | ||
| 383 | my $err; | ||
| 384 | while (1) | ||
| 385 | { | ||
| 386 | $bytecnt = $remoteHandle->read($buff, $self->{BlockSize}); | ||
| 387 | last unless (defined($bytecnt) && $bytecnt > 0); | ||
| 388 | print $unsubscriptedFH $buff; | ||
| 389 | $offset += $bytecnt; | ||
| 390 | $remoteHandle->seek($offset); | ||
| 391 | } | ||
| 392 | return 1 if ($offset); | ||
| 393 | $self->{xftp_lastmsg} = "get:open($args[0]):Could not get data into filehandle(" | ||
| 394 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 395 | return undef; | ||
| 396 | } | ||
| 397 | else | ||
| 398 | { | ||
| 399 | $self->{xftp_lastmsg} = $@ || "get:open($args[0]):Could not open remote handle for unknown reasons!"; | ||
| 400 | return undef; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | else | ||
| 404 | { | ||
| 405 | $ok = $self->{xftp}->scp_get(@args); | ||
| 406 | return 1 if ($ok); | ||
| 407 | $self->{xftp_lastmsg} = "get:scp_get(".join(',',@args) | ||
| 408 | ."):Could not get file from remote host(" | ||
| 409 | .join(' ', $self->{xftp}->error()).')!'; | ||
| 410 | return undef; | ||
| 411 | } | ||
| 412 | return $ok ? 1 : undef; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS. | ||
| 416 | { | ||
| 417 | my $self = shift; | ||
| 418 | |||
| 419 | return undef unless (@_ >= 1); | ||
| 420 | my @args = @_; | ||
| 421 | if (@args >= 2) | ||
| 422 | { | ||
| 423 | $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io); | ||
| 424 | } | ||
| 425 | else | ||
| 426 | { | ||
| 427 | if (ref(\$args[0]) =~ /GLOB/io) | ||
| 428 | { | ||
| 429 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 430 | return undef; | ||
| 431 | } | ||
| 432 | $args[1] = $args[0]; | ||
| 433 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 434 | } | ||
| 435 | $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 436 | |||
| 437 | my $ok; | ||
| 438 | if (ref(\$_[0]) =~ /GLOB/io) | ||
| 439 | { | ||
| 440 | my $remoteHandle; | ||
| 441 | my $offset = 0; | ||
| 442 | my $buff; | ||
| 443 | my $unsubscriptedFH = $_[0]; | ||
| 444 | eval | ||
| 445 | { | ||
| 446 | no strict 'subs'; | ||
| 447 | my $openFlags = O_RDWR|O_CREAT|O_TRUNC; | ||
| 448 | $remoteHandle = $self->{sshsftp}->open($args[1], $openFlags); | ||
| 449 | }; | ||
| 450 | if ($remoteHandle) | ||
| 451 | { | ||
| 452 | my $t; | ||
| 453 | while ($buff = <$unsubscriptedFH>) | ||
| 454 | { | ||
| 455 | $t .= $buff; | ||
| 456 | } | ||
| 457 | my $bytecnt = $remoteHandle->write($t); | ||
| 458 | return 1 if ($bytecnt); | ||
| 459 | $self->{xftp_lastmsg} = "put:open($args[1]):Could not put data to remote host(" | ||
| 460 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 461 | return undef; | ||
| 462 | } | ||
| 463 | else | ||
| 464 | { | ||
| 465 | $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!'; | ||
| 466 | return undef; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | else | ||
| 470 | { | ||
| 471 | eval { $ok = $self->{xftp}->scp_put(@args) }; | ||
| 472 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 473 | } | ||
| 474 | return $ok ? 1 : undef; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub delete #RETURNED OK=2 WHEN LAST FAILED. | ||
| 478 | { | ||
| 479 | my $self = shift; | ||
| 480 | my $path = shift; | ||
| 481 | |||
| 482 | my $ok; | ||
| 483 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 484 | eval { $ok = $self->{sshsftp}->unlink($path) }; | ||
| 485 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 486 | return $@ ? undef : 1; | ||
| 487 | } | ||
| 488 | |||
| 489 | sub rename | ||
| 490 | { | ||
| 491 | my $self = shift; | ||
| 492 | return undef unless (@_ == 2); | ||
| 493 | |||
| 494 | my ($oldfile, $newfile) = @_; | ||
| 495 | |||
| 496 | my $ok; | ||
| 497 | $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 498 | $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 499 | eval { $ok = $self->{sshsftp}->rename($oldfile, $newfile) }; | ||
| 500 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 501 | return $@ ? undef : 1; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub mkdir | ||
| 505 | { | ||
| 506 | my $self = shift; | ||
| 507 | my $path = shift; | ||
| 508 | my $tryRecursion = shift||0; | ||
| 509 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 510 | |||
| 511 | my @pathStack; | ||
| 512 | my $ok = ''; | ||
| 513 | my $orgPath = $path; | ||
| 514 | my $didRecursion = 0; | ||
| 515 | my $errored = 0; | ||
| 516 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 517 | while ($path) | ||
| 518 | { | ||
| 519 | eval { $ok = $self->{sshsftp}->mkdir($path) }; | ||
| 520 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 521 | last if ($ok); | ||
| 522 | if ($tryRecursion) | ||
| 523 | { | ||
| 524 | push (@pathStack, $path); | ||
| 525 | $path =~ s#[^\/\\]+$##o; | ||
| 526 | $path =~ s#[\/\\]$##o; | ||
| 527 | $didRecursion = 1; | ||
| 528 | } | ||
| 529 | else | ||
| 530 | { | ||
| 531 | $self->{xftp_lastmsg} = "mkdir:mkdir($path):Could not create subdirectory(" | ||
| 532 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 533 | $errored = 1; | ||
| 534 | last; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | if ($didRecursion) | ||
| 538 | { | ||
| 539 | while (@pathStack) | ||
| 540 | { | ||
| 541 | $path = pop @pathStack; | ||
| 542 | next if ($self->{sshsftp}->mkdir($path)); | ||
| 543 | $self->{xftp_lastmsg} = "mkdir:mkdir($path):Could not recursively create subdirectory(" | ||
| 544 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 545 | return undef; | ||
| 546 | |||
| 547 | } | ||
| 548 | return 1; | ||
| 549 | } | ||
| 550 | # return (defined($ok) && $ok) ? 1 : undef; | ||
| 551 | return $errored ? 1 : undef; | ||
| 552 | } | ||
| 553 | |||
| 554 | sub rmdir | ||
| 555 | { | ||
| 556 | my $self = shift; | ||
| 557 | my $path = shift; | ||
| 558 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 559 | |||
| 560 | my $ok; | ||
| 561 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 562 | eval { $ok = $self->{sshsftp}->rmdir($path) }; | ||
| 563 | $self->{xftp_lastmsg} = $@ if ($@); | ||
| 564 | return $@ ? undef : 1; | ||
| 565 | } | ||
| 566 | |||
| 567 | sub message | ||
| 568 | { | ||
| 569 | my $self = shift; | ||
| 570 | |||
| 571 | chomp $self->{xftp_lastmsg}; | ||
| 572 | return $self->{xftp_lastmsg}; | ||
| 573 | } | ||
| 574 | |||
| 575 | sub mdtm | ||
| 576 | { | ||
| 577 | my $self = shift; | ||
| 578 | my $path = shift; | ||
| 579 | |||
| 580 | my $ok; | ||
| 581 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 582 | my %statHash = $self->{sshsftp}->stat($path); | ||
| 583 | unless (defined $statHash{mtime}) | ||
| 584 | { | ||
| 585 | $self->{xftp_lastmsg} = "mdtm:stat($path):Could not fetch file mdtm(" | ||
| 586 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 587 | return undef; | ||
| 588 | } | ||
| 589 | return $statHash{'mtime'}; | ||
| 590 | } | ||
| 591 | |||
| 592 | sub size | ||
| 593 | { | ||
| 594 | my $self = shift; | ||
| 595 | my $path = shift; | ||
| 596 | |||
| 597 | my $ok; | ||
| 598 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 599 | my %statHash = $self->{sshsftp}->stat($path); | ||
| 600 | unless (defined $statHash{'size'}) | ||
| 601 | { | ||
| 602 | $self->{xftp_lastmsg} = "size:stat($path):Could not fetch file size(" | ||
| 603 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 604 | return undef; | ||
| 605 | } | ||
| 606 | return $statHash{'size'}; | ||
| 607 | } | ||
| 608 | |||
| 609 | sub isadir | ||
| 610 | { | ||
| 611 | my $self = shift; | ||
| 612 | my $path = shift; | ||
| 613 | |||
| 614 | my $ok; | ||
| 615 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 616 | eval { $ok = $self->{sshsftp}->opendir($path) }; | ||
| 617 | if (defined($ok) && $ok) | ||
| 618 | { | ||
| 619 | eval { $self->{xftp}->do_close($ok) }; | ||
| 620 | return 1; | ||
| 621 | } | ||
| 622 | return 0; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub chmod | ||
| 626 | { | ||
| 627 | my $self = shift; | ||
| 628 | my $permissions = shift; | ||
| 629 | my $path = shift; | ||
| 630 | |||
| 631 | my ($ok, $attrs); | ||
| 632 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 633 | my %statHash = $self->{sshsftp}->stat($path); | ||
| 634 | unless (defined $statHash{mode}) | ||
| 635 | { | ||
| 636 | $self->{xftp_lastmsg} = "chmod:stat($path):Could not fetch current file permissions(" | ||
| 637 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 638 | return undef; | ||
| 639 | } | ||
| 640 | my $filetype = int($statHash{mode} / 4096); | ||
| 641 | eval "\$permissions = 0$permissions"; | ||
| 642 | $statHash{mode} = ($filetype*4096) + $permissions; | ||
| 643 | $ok = $self->{sshsftp}->setstat($path, mode => $statHash{mode}); | ||
| 644 | unless (defined($ok) && $ok) | ||
| 645 | { | ||
| 646 | $self->{xftp_lastmsg} = "chmod:setstat($path, mode => $statHash{mode}):Invalid permissions (0-777) - $@ (" | ||
| 647 | .join(' ', $self->{sshsftp}->error()).')!'; | ||
| 648 | return undef; | ||
| 649 | } | ||
| 650 | return 1; | ||
| 651 | } | ||
| 652 | |||
| 653 | sub getPermStr | ||
| 654 | { | ||
| 655 | my $mode = shift; | ||
| 656 | |||
| 657 | my (%ftypes); | ||
| 658 | $ftypes{(S_IFDIR)} = "d"; | ||
| 659 | $ftypes{(S_IFCHR)} = "c"; | ||
| 660 | $ftypes{(S_IFREG)} = "-" if (defined S_IFREG); | ||
| 661 | if ($bummer) #FOR SOME REASON WINDOWS FCNTL DOES NOT HAVE THESE, BUT WE NEED - | ||
| 662 | { #THEM WHEN LOOKING AT SPECIAL FILES ON OTHER UNIX SYSTEMS: | ||
| 663 | $ftypes{24576} = "b"; | ||
| 664 | $ftypes{4096} = "p"; | ||
| 665 | $ftypes{40960} = "l"; | ||
| 666 | $ftypes{49152} = "s"; | ||
| 667 | } | ||
| 668 | else | ||
| 669 | { | ||
| 670 | $ftypes{(S_IFBLK)} = "b"; | ||
| 671 | $ftypes{(S_IFIFO)} = "p"; | ||
| 672 | $ftypes{(S_IFLNK)} = "l"; | ||
| 673 | $ftypes{(S_IFSOCK)} = "s"; | ||
| 674 | } | ||
| 675 | my @fsperms = ('----', '---t', '--s-', '--st', '-s--', '-s-t', '-ss-', '-sst'); | ||
| 676 | |||
| 677 | my $permissions = sprintf "%04o", S_IMODE($mode); | ||
| 678 | my @permissions = split(//o, sprintf("%04o", S_IMODE($mode))); | ||
| 679 | my @spermissions = split(//o, $fsperms[$permissions[0]]); | ||
| 680 | my $pstr = $ftypes{S_IFMT($mode)}; | ||
| 681 | my $ps; | ||
| 682 | for (my $i=1;$i<=3;$i++) | ||
| 683 | { | ||
| 684 | $ps = $permvec[$permissions[$i]]; #r-x | ||
| 685 | if ($spermissions[$i] =~ /\w/o) | ||
| 686 | { | ||
| 687 | my $loc = substr($ps,2,2); | ||
| 688 | if ($loc =~ /\w/o) | ||
| 689 | { | ||
| 690 | $loc = $spermissions[$i]; | ||
| 691 | } | ||
| 692 | else | ||
| 693 | { | ||
| 694 | $loc = uc($spermissions[$i]); | ||
| 695 | } | ||
| 696 | $ps =~ s/^(..).$/$1$loc/; | ||
| 697 | } | ||
| 698 | $pstr .= $ps; | ||
| 699 | } | ||
| 700 | return $pstr; | ||
| 701 | } | ||
| 702 | |||
| 703 | sub method | ||
| 704 | { | ||
| 705 | my $self = shift; | ||
| 706 | my $method = shift; | ||
| 707 | |||
| 708 | for (my $i=0;$i<scalar(@_);$i++) | ||
| 709 | { | ||
| 710 | $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o); | ||
| 711 | } | ||
| 712 | my $res; | ||
| 713 | my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")"; | ||
| 714 | eval $xeq; | ||
| 715 | if ($@) | ||
| 716 | { | ||
| 717 | $self->{xftp_lastmsg} = "method Net::SSH2::$method(".join(',',@_).") returned $@"; | ||
| 718 | return undef; | ||
| 719 | } | ||
| 720 | return $res; | ||
| 721 | } | ||
| 722 | |||
| 723 | 1 |