File Coverage

blib/lib/Net/FTP/Path/Iter/Dir.pm
Criterion Covered Total %
statement 61 67 91.0
branch 8 18 44.4
condition 1 3 33.3
subroutine 14 14 100.0
pod n/a
total 84 102 82.3


line stmt bran cond sub pod time code
1             package Net::FTP::Path::Iter::Dir;
2              
3             # ABSTRACT: Class representing a Directory
4              
5 3     3   184957 use 5.010;
  3         17  
6 3     3   16 use strict;
  3         6  
  3         89  
7 3     3   30 use warnings;
  3         6  
  3         194  
8 3     3   1476 use experimental 'switch';
  3         6834  
  3         15  
9              
10             our $VERSION = '0.07';
11              
12 3     3   605 use Carp;
  3         6  
  3         182  
13 3     3   17 use Fcntl qw[ :mode ];
  3         5  
  3         795  
14              
15 3     3   460 use File::Spec::Functions qw[ catdir catfile ];
  3         566  
  3         221  
16              
17 3     3   1671 use namespace::clean;
  3         44680  
  3         19  
18              
19 3     3   2390 use parent 'Net::FTP::Path::Iter::Entry';
  3         7  
  3         23  
20              
21 3     3   1675 use Net::FTP::Path::Iter::File;
  3         9  
  3         139  
22              
23 3     3   18 use constant is_file => 0;
  3         6  
  3         194  
24 3     3   15 use constant is_dir => 1;
  3         5  
  3         2214  
25              
26             sub _children {
27              
28 5     5   12 my $self = shift;
29              
30 5         182 my %attr = ( server => $self->server, );
31              
32 5         121 my $entries = $self->_get_entries( $self->path );
33              
34 5         35 my @children;
35              
36 5         59 for my $entry ( @$entries ) {
37              
38 13         26 my $obj;
39              
40 13 100       80 if ( $entry->{type} eq 'd' ) {
    50          
41              
42             $obj
43 4         246 = Net::FTP::Path::Iter::Dir->new( %$entry, %attr, path => catdir( $self->path, $entry->{name} ) );
44             }
45              
46             elsif ( $entry->{type} eq 'f' ) {
47              
48             $obj = Net::FTP::Path::Iter::File->new( %$entry, %attr,
49 9         441 path => catfile( $self->path, $entry->{name} ) );
50             }
51              
52             else {
53              
54 0         0 warn( "ignoring $entry->{name}; unknown type $_\n" );
55             }
56              
57 13         262 push @children, $obj;
58             }
59              
60 5         64 return @children;
61              
62             }
63              
64             # if an entity doesn't have attributes, it didn't get loaded
65             # from a directory listing. Try to get one. This should
66             # happen rarely, so do this slowly but correctly.
67             sub _retrieve_attrs {
68              
69 1     1   105 my $self = shift;
70              
71 1 50       34 return if $self->_has_attrs;
72              
73 1         32 my $server = $self->server;
74              
75 1         17 my $pwd = $server->pwd;
76              
77 1         124 my $entry = {};
78              
79 1 50       34 $server->cwd( $self->path )
80             or croak( 'unable to chdir to ', $self->path );
81              
82             # File::Listing doesn't return . or .. (and some FTP servers
83             # don't return that info anyway), so try to go up a dir and
84             # look for the name
85 1         217 my $err;
86 1   33     3 eval {
87              
88             # cdup sometimes returns ok even if it didn't work
89 1         14 $server->cdup;
90              
91 1 50       197 if ( $pwd ne $server->pwd ) {
92              
93 0         0 my $entries = $self->_get_entries( q{.} );
94              
95 0         0 ( $entry ) = grep { $self->name eq $_->{name} } @$entries;
  0         0  
96              
97 0 0       0 croak( 'unable to find attributes for ', $self->path )
98             if !$entry;
99              
100             croak( $self->path, ": expected directory, got $entry->{type}" )
101 0 0       0 unless $entry->{type} eq 'd';
102              
103             }
104              
105             # couldn't go up a directory; at the top?
106             else {
107              
108             # fake it.
109              
110 1         118 $entry = {
111             size => 0,
112             mtime => 0,
113             mode => S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
114             type => 'd',
115             _has_attrs => 1,
116             };
117              
118             }
119              
120             } // ( $err = $@ );
121              
122 1 50       7 $server->cwd( $pwd )
123             or croak( "unable to return to directory: $pwd" );
124              
125 1 50       243 croak( $err ) if defined $err;
126              
127 1         46 $self->$_( $entry->{$_} ) for keys %$entry;
128 1         159 return;
129             }
130              
131             #
132             # This file is part of Net-FTP-Path-Iter
133             #
134             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
135             #
136             # This is free software, licensed under:
137             #
138             # The GNU General Public License, Version 3, June 2007
139             #
140             1;
141              
142             __END__