AnyEvent-FTP
view release on metacpan or search on metacpan
lib/AnyEvent/FTP/Client.pm view on Meta::CPAN
client => $self,
);
}
sub stou
{
my($self, $filename, $local) = @_;
my $xfer;
my $cb = sub {
my $name = shift->get_file;
$xfer->{remote_name} = $name if defined $name;
return;
};
$xfer = $self->_store->new(
command => [STOU => $filename, $cb],
local => $local,
client => $self,
);
}
# for this to work under ProFTPd: AllowStoreRestart off
sub appe
{
my($self, $filename, $local) = @_;
$self->_store->new(
command => [APPE => $filename],
local => $local,
client => $self,
);
}
sub list
{
my($self, $location, $verb) = @_;
$verb //= 'LIST';
my @lines;
my $cv = AnyEvent->condvar;
$self->_list->new(
command => [ $verb => $location ],
local => \@lines,
client => $self,
)->cb(sub {
my $res = eval { shift->recv };
$cv->croak($@) if $@;
$cv->send(\@lines);
});
$cv;
}
sub nlst
{
my($self, $location) = @_;
$self->list($location, 'NLST');
}
sub rename
{
my($self, $from, $to) = @_;
$self->push_command(
[ RNFR => $from ],
[ RNTO => $to ],
);
}
sub pwd
{
my($self) = @_;
my $cv = AnyEvent->condvar;
$self->push_command(['PWD'])->cb(sub {
my $res = eval { shift->recv } // $@;
my $dir = $res->get_dir;
if($dir) { $cv->send($dir) }
else { $cv->croak($res) }
});
$cv;
}
sub size
{
my($self, $path) = @_;
my $cv = AnyEvent->condvar;
$self->push_command(['SIZE', $path])->cb(sub {
my $res = eval { shift->recv };
if(my $error = $@)
{ $cv->croak($error) }
else
{ $cv->send($res->message->[0]) }
});
$cv;
}
(eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_)) // die $@
for qw( CWD CDUP NOOP ALLO SYST TYPE STRU MODE REST MKD RMD STAT HELP DELE RNFR RNTO USER PASS ACCT MDTM );
sub quit
{
my($self) = @_;
my $cv = AnyEvent->condvar;
my $res;
$self->push_command(['QUIT'])->cb(sub {
$res = eval { shift->recv } // $@;
});
my $save = $self->{event}->{close};
$self->{event}->{close} = [ sub {
if(defined $res && $res->is_success)
{ $cv->send($res) }
elsif(defined $res)
{ $cv->croak($res) }
else
lib/AnyEvent/FTP/Client.pm view on Meta::CPAN
$client->appe($filename, $local);
Works exactly like the C<stor> method, except use the FTP C<APPE> command instead of
C<STOR>. This method will append C<$local> to the remote file. One way to resume an
upload to the remote FTP server would be to open the local file, determine the remote
file's size and seek to that position in the local file and use the C<appe> method
with C<$local> as that file handle, as in this example:
# assume that foo.txt is in the current local dir
# and the remote local dir
my $filename = "foo.txt";
$client->size($filename)->cb(sub {
my $size = shift->recv;
open my $fh, '<', $filename;
binmode $fh;
seek $fh, $size, 0;
$client->appe($filename, $fh);
});
Note that the C<SIZE> command is an extension to FTP, and may not be available on all
servers.
=head2 list
$client->list($location)
Execute the FTP C<LIST> command. The results will be sent as a list reference
(instead of a L<AnyEvent::FTP::Client::Response> object) to the returned condition
variable.
use strict;
use warnings;
use AnyEvent;
use AnyEvent::FTP::Client;
my $client = AnyEvent::FTP::Client->new;
my $cv = AnyEvent->condvar;
# connect to CPAN ftp server
$client->connect('ftp://ftp.cpan.org/pub/CPAN/src')->cb(sub {
# execute LIST command and print results to stdout
$client->list->cb(sub {
my $list = shift->recv;
print "$_\n" for @$list;
$cv->send;
});
});
$cv->recv;
=head2 nlst
$client->nlst($location);
Works exactly like the C<list> method, except the FTP C<NLST> command is used.
The main difference is that this method returns filenames only.
=head2 rename
$client->rename($from, $to);
This method renames the remote file from C<$from> to C<$to>.
It uses the FTP C<RNFR> and C<RNTO> commands and thus this:
my $cv = $client->rename($from, $to);
is a short cut for:
my $cv;
$client->rnfr($from)->cb(sub {
$cv = $client->rnto($to);
});
Although C<$cv> may not be defined right away, so use the second with care.
=head2 cwd
$client->cwd( $dir );
Change to the given directory on the remote server.
=head2 pwd
$client->pwd;
Gets the current working directory on the remote server. This gets just the string
representing the directory path instead of a L<AnyEvent::FTP::Client::Response> object.
=head2 cdup
$client->cdup
Change to the parent directory on the remote server. This is usually the same
as
$client->cwd('..');
=head2 type
$client->type
Set the transfer type. You almost always want to set to binary mode immediately
after logging on:
$client->type('I');
=head2 rest
$client->rest
This command is used to resume a download transfer. Typically you would
not use this method directly, but instead add a C<restart> option on
the C<retr> method instead.
=head2 mkd
$client->mkd( $path );
Create a directory on the remote server.
=head2 rmd
$client->rmd( $path );
Remove a directory on the remote server.
=head2 help
$client->help;
Gets a list of commands understood by the server.
The actual format depends on the server.
=head2 dele
$client->dele( $path );
Delete the file on the remote server.
=head2 rnfr
$client->rnfr;
Specify the old name for renaming a file. See C<rename> method for a shortcut.
=head2 rnto
$client->rnto;
Specify the new name for renaming a file. See C<rename> method for a shortcut.
=head2 noop
$client->noop;
Don't do anything. The server will send an OK reply.
=head2 allo
$client->allo( $size );
Send the FTP C<ALLO> command. Is not used by modern FTP servers. See RFC959 for details.
=head2 syst
$client->syst;
Returns the type of operating system used by the server.
=head2 stru
$client->stru;
Specify the file structure mode. This is not used by modern FTP servers. See RFC959 for details.
=head2 mode
$client->mode
Specify the transfer mode. This is not used by modern FTP servers. See RFC959 for details.
=head2 stat
$client->stat;
$client->stat($path);
Get information about a file or directory on the remote server. The actual format is totally
server dependent.
=head2 user
$client->user( $username );
Specify the user to login as. See C<connect> or C<login> methods for a shortcut.
=head2 pass
$client->pass( $pass );
Specify the password to use for login. See C<connect> or C<login> methods for a shortcut.
=head2 acct
$client->acct( $acct );
Specify user's account. This is sometimes used for authentication and authorization when you login
to some servers, but is seldom used today in practice. See RFC959 for details.
=head2 size
( run in 0.751 second using v1.01-cache-2.11-cpan-39bf76dae61 )