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