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