| 1 | package Net::SFTP::Foreign::Local; | ||
| 2 | |||
| 3 | our $VERSION = '1.57'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | use File::Spec; | ||
| 9 | |||
| 10 | use Net::SFTP::Foreign::Attributes; | ||
| 11 | use Net::SFTP::Foreign::Constants qw(:error); | ||
| 12 | use Net::SFTP::Foreign::Helpers qw(_sort_entries _gen_wanted _do_nothing); | ||
| 13 | require Net::SFTP::Foreign::Common; | ||
| 14 | our @ISA = qw(Net::SFTP::Foreign::Common); | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my $self = { status => 0, | ||
| 19 | error => 0 }; | ||
| 20 | bless $self, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub realpath { | ||
| 24 | $! = 0; | ||
| 25 | File::Spec->rel2abs($_[1]) | ||
| 26 | } | ||
| 27 | |||
| 28 | sub stat { | ||
| 29 | $! = 0; | ||
| 30 | my $a = Net::SFTP::Foreign::Attributes->new_from_stat(CORE::stat($_[1])); | ||
| 31 | unless ($a) { | ||
| 32 | $_[0]->_set_error(SFTP_ERR_LOCAL_STAT_FAILED, "Couldn't stat local file '$_[1]'", $!); | ||
| 33 | } | ||
| 34 | $a | ||
| 35 | } | ||
| 36 | |||
| 37 | sub lstat { | ||
| 38 | $! = 0; | ||
| 39 | my $a = Net::SFTP::Foreign::Attributes->new_from_stat(CORE::lstat($_[1])); | ||
| 40 | unless ($a) { | ||
| 41 | $_[0]->_set_error(SFTP_ERR_LOCAL_STAT_FAILED, "Couldn't stat local file '$_[1]'", $!); | ||
| 42 | } | ||
| 43 | $a | ||
| 44 | } | ||
| 45 | |||
| 46 | sub readlink { | ||
| 47 | $! = 0; | ||
| 48 | my $target = readlink $_[1]; | ||
| 49 | unless (defined $target) { | ||
| 50 | $_[0]->_set_error(SFTP_ERR_LOCAL_READLINK_FAILED, "Couldn't read link '$_[1]'", $!); | ||
| 51 | } | ||
| 52 | $target | ||
| 53 | } | ||
| 54 | |||
| 55 | sub join { | ||
| 56 | shift; | ||
| 57 | my $path = File::Spec->join(@_); | ||
| 58 | $path = File::Spec->canonpath($path); | ||
| 59 | # print 'lfs->join("'.join('", "', @_)."\") => $path\n"; | ||
| 60 | $path | ||
| 61 | } | ||
| 62 | |||
| 63 | sub ls { | ||
| 64 | my ($self, $dir, %opts) = @_; | ||
| 65 | |||
| 66 | my $ordered = delete $opts{ordered}; | ||
| 67 | my $follow_links = delete $opts{follow_links}; | ||
| 68 | my $atomic_readdir = delete $opts{atomic_readdir}; | ||
| 69 | |||
| 70 | my $wanted = delete $opts{_wanted} || | ||
| 71 | _gen_wanted(delete $opts{wanted}, | ||
| 72 | delete $opts{no_wanted}); | ||
| 73 | |||
| 74 | %opts and croak "invalid option(s) '".CORE::join("', '", keys %opts)."'"; | ||
| 75 | |||
| 76 | $! = 0; | ||
| 77 | |||
| 78 | opendir(my $ldh, $dir) | ||
| 79 | or return undef; | ||
| 80 | |||
| 81 | my @dir; | ||
| 82 | while (defined(my $part = readdir $ldh)) { | ||
| 83 | my $fn = File::Spec->join($dir, $part); | ||
| 84 | my $a = $self->lstat($fn); | ||
| 85 | if ($a and $follow_links and S_ISLNK($a->perm)) { | ||
| 86 | if (my $fa = $self->stat($fn)) { | ||
| 87 | $a = $fa; | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $! = 0; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | my $entry = { filename => $part, | ||
| 94 | a => $a }; | ||
| 95 | if ($atomic_readdir or !$wanted or $wanted->($self, $entry)) { | ||
| 96 | push @dir, $entry; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | if ($atomic_readdir and $wanted) { | ||
| 101 | @dir = grep { $wanted->($self, $_) } @dir; | ||
| 102 | } | ||
| 103 | |||
| 104 | _sort_entries(\@dir) if $ordered; | ||
| 105 | |||
| 106 | return \@dir; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |