| 1 | package Net::xFTP::FSP; | ||
| 2 | |||
| 3 | sub new_fsp | ||
| 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 | |||
| 20 | $xftp->{xftp} = Net::FSP->new($host, \%args); | ||
| 21 | $ENV{HOME} = $saveEnvHome || ''; | ||
| 22 | if ($xftp->{xftp}) | ||
| 23 | { | ||
| 24 | my $cwd = $xftp->{xftp}->current_dir(); | ||
| 25 | $xftp->{cwd} = $cwd if ($cwd); | ||
| 26 | $xftp->{protocol} = 'Net::FSP'; | ||
| 27 | return $xftp; | ||
| 28 | } | ||
| 29 | return undef; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub protocol | ||
| 33 | { | ||
| 34 | return shift->{protocol}; | ||
| 35 | } | ||
| 36 | |||
| 37 | { | ||
| 38 | no warnings 'redefine'; | ||
| 39 | sub cwd #SET THE "CURRENT" DIRECTORY. | ||
| 40 | { | ||
| 41 | my $self = shift; | ||
| 42 | my $cwd = shift || '/'; | ||
| 43 | |||
| 44 | my $ok; | ||
| 45 | my $fullwd; | ||
| 46 | if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o) | ||
| 47 | { | ||
| 48 | if ($self->{cwd} =~ m#\/$#o) | ||
| 49 | { | ||
| 50 | $cwd = $self->{cwd} . $cwd; | ||
| 51 | } | ||
| 52 | else | ||
| 53 | { | ||
| 54 | $cwd = $self->{cwd} . '/' . $cwd; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | $fullwd = $self->{xftp}->change_dir($cwd); | ||
| 58 | $ok = $self->{xftp}->current_dir(); | ||
| 59 | if ($ok eq $cwd) # fullwd is set to the PREVIOUS directory! | ||
| 60 | { | ||
| 61 | $self->{cwd} = $cwd; | ||
| 62 | $ok = 1; | ||
| 63 | } | ||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub copy | ||
| 68 | { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | return undef unless (@_ >= 2); | ||
| 72 | my @args = @_; | ||
| 73 | for (my $i=0;$i<=1;$i++) | ||
| 74 | { | ||
| 75 | $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 76 | } | ||
| 77 | if ($self->isadir($args[1])) | ||
| 78 | { | ||
| 79 | my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o); | ||
| 80 | $args[1] .= '/' unless ($args[1] =~ m#\/$#o); | ||
| 81 | $args[1] .= $filename; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $ok; | ||
| 85 | my ($buff); | ||
| 86 | my $fromHandle = $self->{xftp}->open_file($args[0], '<'); | ||
| 87 | unless (defined($fromHandle) && $fromHandle) | ||
| 88 | { | ||
| 89 | $self->{xftp_lastmsg} = "Copy failed (". ($!||'open1 failed - Unknown reason') | ||
| 90 | . ')!'; | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | my $toHandle; | ||
| 94 | $toHandle = $self->{xftp}->open_file($args[1], '>'); | ||
| 95 | unless (defined($toHandle) && $toHandle) | ||
| 96 | { | ||
| 97 | $self->{xftp_lastmsg} = "Copy failed (". ($!||'do_open2 failed - Unknown reason') | ||
| 98 | . ')!'; | ||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | my $offset = 0; | ||
| 102 | my $err; | ||
| 103 | while ($buff = <$fromHandle>) | ||
| 104 | { | ||
| 105 | print $toHandle $buff; | ||
| 106 | $offset += length($buff); | ||
| 107 | } | ||
| 108 | close $toHandle; | ||
| 109 | close $fromHandle; | ||
| 110 | $ok = 1; | ||
| 111 | return $ok ? 1 : undef; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub move | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | |||
| 118 | return undef unless (@_ >= 2); | ||
| 119 | # return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef; | ||
| 120 | $self->{xftp}->move_file($_[0], $_[1]); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | sub ascii | ||
| 125 | { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | return undef; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub binary | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub quit | ||
| 139 | { | ||
| 140 | my $self = shift; | ||
| 141 | $self->{xftp}->say_bye(); | ||
| 142 | $self->{xftp} = undef; | ||
| 143 | delete($self->{xftp}); | ||
| 144 | |||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub ls | ||
| 149 | { | ||
| 150 | my $self = shift; | ||
| 151 | my $path = shift || ''; | ||
| 152 | my $showall = shift || 0; | ||
| 153 | my @dirHash = $self->{xftp}->list_dir($path|'.'); | ||
| 154 | return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY. | ||
| 155 | my $t; | ||
| 156 | my @dirlist = (); | ||
| 157 | for (my $i=0;$i<=$#dirHash;$i++) | ||
| 158 | { | ||
| 159 | $t = $dirHash[$i]->{name}; | ||
| 160 | next if ($t eq '..' && $path eq '/'); | ||
| 161 | next if (!$showall && $t =~ /^\.[^\.]/o); | ||
| 162 | push (@dirlist, $t); | ||
| 163 | } | ||
| 164 | @dirlist = sort @dirlist; | ||
| 165 | |||
| 166 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 167 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..'); | ||
| 168 | #unshift (@dirlist, '.') unless ($dirlist[0] eq '.'); | ||
| 169 | |||
| 170 | return wantarray ? @dirlist : \@dirlist; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub dir | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my $path = shift || ''; | ||
| 177 | my $showall = shift || 0; | ||
| 178 | my @dirlist; | ||
| 179 | # $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 180 | my @dirHash = $self->{xftp}->list_dir($path||'.'); | ||
| 181 | return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY. | ||
| 182 | my ($t, @tm, $tp); | ||
| 183 | @dirlist = (); | ||
| 184 | foreach my $i (@dirHash) | ||
| 185 | { | ||
| 186 | #$t = $dirHash[$i]->{longname}; | ||
| 187 | $t = $i->short_name(); | ||
| 188 | next if ($t =~ /\d \.\.$/o && $path eq '/'); | ||
| 189 | next if (!$showall && $t =~ /\d \.[^\.]\S*$/o); | ||
| 190 | $tp = substr($i->type(),0,1); | ||
| 191 | $tp = '-' if ($tp =~ /f/io); | ||
| 192 | @tm = localtime($i->time()); | ||
| 193 | $_ = sprintf "%1s----------unknown- -unknown- %8s %4d-%2.2d-%2.2d %2.2d:%2.2d %s\n", | ||
| 194 | $tp, $i->{size}, $tm[5]+1900, $tm[4]+1, $tm[3], $tm[2], $tm[1], $t; | ||
| 195 | push (@dirlist, $_); | ||
| 196 | } | ||
| 197 | |||
| 198 | ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE! | ||
| 199 | #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/); | ||
| 200 | #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/); | ||
| 201 | |||
| 202 | return wantarray ? @dirlist : \@dirlist; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY. | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | return $self->{cwd}; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub get #(Remote, => Local) | ||
| 213 | { | ||
| 214 | my $self = shift; | ||
| 215 | |||
| 216 | return undef unless (@_ >= 1); | ||
| 217 | my @args = @_; | ||
| 218 | if (@args >= 2) | ||
| 219 | { | ||
| 220 | $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io); | ||
| 221 | } | ||
| 222 | else | ||
| 223 | { | ||
| 224 | if (ref(\$args[0]) =~ /GLOB/io) | ||
| 225 | { | ||
| 226 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 227 | return undef; | ||
| 228 | } | ||
| 229 | $args[1] = $args[0]; | ||
| 230 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 231 | } | ||
| 232 | eval { $self->{xftp}->download_file(@args); }; | ||
| 233 | if ($@) | ||
| 234 | { | ||
| 235 | $self->{xftp_lastmsg} = $@; | ||
| 236 | return undef; | ||
| 237 | } | ||
| 238 | return 1; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS. | ||
| 242 | { | ||
| 243 | my $self = shift; | ||
| 244 | return undef unless (@_ >= 1); | ||
| 245 | my @args = @_; | ||
| 246 | if (@args >= 2) | ||
| 247 | { | ||
| 248 | $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io); | ||
| 249 | } | ||
| 250 | else | ||
| 251 | { | ||
| 252 | if (ref(\$args[0]) =~ /GLOB/io) | ||
| 253 | { | ||
| 254 | $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!'; | ||
| 255 | return undef; | ||
| 256 | } | ||
| 257 | $args[1] = $args[0]; | ||
| 258 | $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o); | ||
| 259 | } | ||
| 260 | eval { $self->{xftp}->upload_file($args[1], $args[0]); }; #THEY REVERSED THE ARGS ON THIS (READS FROM 2ND ARG - ARGS[0])?!?!?! | ||
| 261 | if ($@) | ||
| 262 | { | ||
| 263 | $self->{xftp_lastmsg} = $@; | ||
| 264 | return undef; | ||
| 265 | } | ||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub delete #RETURNED OK=2 WHEN LAST FAILED. | ||
| 270 | { | ||
| 271 | my $self = shift; | ||
| 272 | my $path = shift; | ||
| 273 | |||
| 274 | eval { $self->{xftp}->remove_file($path) }; | ||
| 275 | if ($@) | ||
| 276 | { | ||
| 277 | $self->{xftp_lastmsg} = $@; | ||
| 278 | return undef; | ||
| 279 | } | ||
| 280 | return 1; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub rename | ||
| 284 | { | ||
| 285 | my $self = shift; | ||
| 286 | return undef unless (@_ == 2); | ||
| 287 | |||
| 288 | my ($oldfile, $newfile) = @_; | ||
| 289 | |||
| 290 | # $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 291 | # $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 292 | eval { $self->{xftp}->move_file($oldfile, $newfile) }; | ||
| 293 | if ($@) | ||
| 294 | { | ||
| 295 | $self->{xftp_lastmsg} = $@; | ||
| 296 | return undef; | ||
| 297 | } | ||
| 298 | return 1; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub mkdir | ||
| 302 | { | ||
| 303 | my $self = shift; | ||
| 304 | my $path = shift; | ||
| 305 | my $tryRecursion = shift||0; | ||
| 306 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 307 | |||
| 308 | my @pathStack; | ||
| 309 | my $orgPath = $path; | ||
| 310 | my $didRecursion = 0; | ||
| 311 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 312 | while ($path) | ||
| 313 | { | ||
| 314 | $path =~ s#[^\/\\]+$##o; | ||
| 315 | $path =~ s#[\/\\]$##o; | ||
| 316 | $path = '/' unless ($path); | ||
| 317 | last if ($self->isadir($path)); | ||
| 318 | if ($tryRecursion) | ||
| 319 | { | ||
| 320 | push (@pathStack, $path); | ||
| 321 | $didRecursion = 1; | ||
| 322 | last if ($path eq '/'); | ||
| 323 | } | ||
| 324 | else | ||
| 325 | { | ||
| 326 | $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!"; | ||
| 327 | return undef; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | if ($didRecursion) | ||
| 331 | { | ||
| 332 | while (@pathStack) | ||
| 333 | { | ||
| 334 | $path = pop @pathStack; | ||
| 335 | eval { $self->{xftp}->make_dir($path) }; | ||
| 336 | if ($@) | ||
| 337 | { | ||
| 338 | $self->{xftp_lastmsg} = $@; | ||
| 339 | return undef; | ||
| 340 | } | ||
| 341 | next; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | eval { $self->{xftp}->make_dir($orgPath) }; | ||
| 345 | if ($@) | ||
| 346 | { | ||
| 347 | $self->{xftp_lastmsg} = $@; | ||
| 348 | return undef; | ||
| 349 | } | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub rmdir | ||
| 354 | { | ||
| 355 | my $self = shift; | ||
| 356 | my $path = shift; | ||
| 357 | |||
| 358 | $path =~ s#[\/\\]$##o unless ($path eq '/'); | ||
| 359 | |||
| 360 | $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 361 | eval { $self->{xftp}->remove_dir($path) }; | ||
| 362 | if ($@) | ||
| 363 | { | ||
| 364 | $self->{xftp_lastmsg} = $@; | ||
| 365 | return undef; | ||
| 366 | } | ||
| 367 | return 1; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub message | ||
| 371 | { | ||
| 372 | my $self = shift; | ||
| 373 | |||
| 374 | chomp $self->{xftp_lastmsg}; | ||
| 375 | return $self->{xftp_lastmsg}; | ||
| 376 | } | ||
| 377 | |||
| 378 | sub mdtm | ||
| 379 | { | ||
| 380 | my $self = shift; | ||
| 381 | my $path = shift; | ||
| 382 | |||
| 383 | my $ok; | ||
| 384 | # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 385 | eval { $ok = $self->{xftp}->stat_file($path) }; | ||
| 386 | if ($@ || !defined($ok)) | ||
| 387 | { | ||
| 388 | $self->{xftp_lastmsg} = $@ || "xFTP: size() failed for unknown reason!"; | ||
| 389 | return undef; | ||
| 390 | } | ||
| 391 | return $ok->time(); | ||
| 392 | } | ||
| 393 | |||
| 394 | sub size | ||
| 395 | { | ||
| 396 | my $self = shift; | ||
| 397 | my $path = shift; | ||
| 398 | |||
| 399 | my $ok; | ||
| 400 | # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o); | ||
| 401 | eval { $ok = $self->{xftp}->stat_file($path) }; | ||
| 402 | if ($@ || !defined($ok)) | ||
| 403 | { | ||
| 404 | $self->{xftp_lastmsg} = $@ || "xFTP: size() failed for unknown reason!"; | ||
| 405 | return undef; | ||
| 406 | } | ||
| 407 | return $ok->size(); | ||
| 408 | } | ||
| 409 | |||
| 410 | sub isadir | ||
| 411 | { | ||
| 412 | my $self = shift; | ||
| 413 | my $path = shift; | ||
| 414 | |||
| 415 | my $ok; | ||
| 416 | eval { $ok = $self->{xftp}->stat_file($path) }; | ||
| 417 | if ($@ || !defined($ok)) | ||
| 418 | { | ||
| 419 | $self->{xftp_lastmsg} = $@ || "xFTP: isadir() failed for unknown reason!"; | ||
| 420 | return undef; | ||
| 421 | } | ||
| 422 | return ($ok->type() =~ /d/io) ? 1 : 0; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub chmod | ||
| 426 | { | ||
| 427 | my $self = shift; | ||
| 428 | my $permissions = shift; | ||
| 429 | my $path = shift; | ||
| 430 | |||
| 431 | $self->{xftp_lastmsg} = 'Net::FSP: chmod function not supported!'; | ||
| 432 | return undef; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub method | ||
| 436 | { | ||
| 437 | my $self = shift; | ||
| 438 | my $method = shift; | ||
| 439 | |||
| 440 | for (my $i=0;$i<scalar(@_);$i++) | ||
| 441 | { | ||
| 442 | $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o); | ||
| 443 | } | ||
| 444 | my $res; | ||
| 445 | my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")"; | ||
| 446 | eval $xeq; | ||
| 447 | if ($@) | ||
| 448 | { | ||
| 449 | $self->{xftp_lastmsg} = "method Net::FSP::$method(".join(',',@_).") returned $@"; | ||
| 450 | return undef; | ||
| 451 | } | ||
| 452 | return $res; | ||
| 453 | } | ||
| 454 | |||
| 455 | 1 |