| 1 | package Net::FTPTurboSync::MyFtp; | ||
| 2 | |||
| 3 | use Net::FTP; | ||
| 4 | use Net::FTPTurboSync::PrgOpts; | ||
| 5 | use base qw (Net::FTP); | ||
| 6 | |||
| 7 | sub new () { | ||
| 8 | my ( $class, $ftpserver, $doftpdebug, $ftptimeout) = @_; | ||
| 9 | |||
| 10 | my $self = $class->SUPER::new( $ftpserver, | ||
| 11 | Debug=>$doftpdebug, | ||
| 12 | Timeout=>$ftptimeout, | ||
| 13 | Passive=>1 ) ; | ||
| 14 | return $self; | ||
| 15 | } | ||
| 16 | sub setPerms { | ||
| 17 | my ( $self, $path, $perms ) = @_; | ||
| 18 | $self->quot('SITE', sprintf('CHMOD %04o %s', $perms, $path)); | ||
| 19 | } | ||
| 20 | sub connection() { | ||
| 21 | my ($class) = @_; | ||
| 22 | my $theOpts = $Net::FTPTurboSync::PrgOpts::theOpts; | ||
| 23 | if ($theOpts->{dodebug}) { | ||
| 24 | print "\nFind out if ftp server is online & accessible.\n"; | ||
| 25 | } | ||
| 26 | my $doftpdebug=($theOpts->{doverbose} > 2); | ||
| 27 | my $ftpc = Net::FTPTurboSync::MyFtp->new ( | ||
| 28 | $theOpts->{ftpserver}, | ||
| 29 | $doftpdebug, | ||
| 30 | $theOpts->{ftptimeout} | ||
| 31 | ) || die "Could not connect to $theOpts->{ftpserver}\n"; | ||
| 32 | if ($theOpts->{dodebug}) { | ||
| 33 | print "Logging in as $theOpts->{ftpuser} with password $theOpts->{ftppasswd}.\n" | ||
| 34 | } | ||
| 35 | |||
| 36 | $ftpc->login( $theOpts->{ftpuser}, | ||
| 37 | $theOpts->{ftppasswd} | ||
| 38 | ) || die "Could not login to $theOpts->{ftpserver} as $theOpts->{ftpuser}\n"; | ||
| 39 | my $ftpdefdir = $ftpc->pwd(); | ||
| 40 | if ( $theOpts->{dodebug}) { | ||
| 41 | print "Remote directory is now ".$ftpdefdir."\n"; | ||
| 42 | } | ||
| 43 | # insert remote login directory into relative ftpdir specification | ||
| 44 | if ( $theOpts->{ftpdir} !~ /^\//) | ||
| 45 | { | ||
| 46 | if ($ftpdefdir eq "/") | ||
| 47 | { | ||
| 48 | $theOpts->{ftpdir} = $ftpdefdir . $theOpts->{ftpdir}; | ||
| 49 | }else{ | ||
| 50 | $theOpts->{ftpdir} = $ftpdefdir . "/" . $theOpts->{ftpdir}; | ||
| 51 | } | ||
| 52 | if (!$theOpts->{doquiet}){ | ||
| 53 | print "Absolute remote directory is $theOpts->{ftpdir}\n"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | if ( $theOpts->{dodebug} ) { | ||
| 57 | print "Changing to remote directory $theOpts->{ftpdir}.\n" | ||
| 58 | } | ||
| 59 | |||
| 60 | $ftpc->binary() | ||
| 61 | or die "Cannot set binary mode ", $ftpc->message; | ||
| 62 | $ftpc->cwd($theOpts->{ftpdir}) | ||
| 63 | or die "Cannot cwd to $theOpts->{ftpdir} ", $ftpc->message; | ||
| 64 | if ($ftpc->pwd() ne $theOpts->{ftpdir}) { | ||
| 65 | die "Could not change to remote base directory $theOpts->{ftpdir}\n"; | ||
| 66 | } | ||
| 67 | if ($theOpts->{dodebug}) { | ||
| 68 | print "Remote directory is now " . $ftpc->pwd() . "\n"; | ||
| 69 | } | ||
| 70 | return $ftpc; | ||
| 71 | } | ||
| 72 | sub isConnected { | ||
| 73 | my ( $self ) = @_; | ||
| 74 | # Prepend connection time out while file reading takes | ||
| 75 | # longer than the remote ftp time out | ||
| 76 | # - 421 Connection timed out. | ||
| 77 | # - code=421 or CMD_REJECT=4 | ||
| 78 | if (!$self->pwd()) { | ||
| 79 | # or $self->status == Net::Cmd::CMD_REJECT; | ||
| 80 | return $self->code == 421; | ||
| 81 | } | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | __END__ |