| 1 | package Net::FTP::Path::Iter::Dir; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Class representing a Directory | ||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use experimental 'switch'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | our $VERSION = '0.06'; | |||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Fcntl qw[ :mode ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 3 | ▶ | use File::Spec::Functions qw[ catdir catfile ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 3 | ▶ | use parent 'Net::FTP::Path::Iter::Entry'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | use Net::FTP::Path::Iter::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 3 | ▶ | use constant is_file => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 3 | ▶ | use constant is_dir => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | sub _children { | ||||||||||
| 27 | |||||||||||
| 28 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_children: called CC=3 SCAR=11.1
| |||||||||||
| 29 | |||||||||||
| 30 | 5 | my %attr = ( server => $self->server, ); | |||||||||
| 31 | |||||||||||
| 32 | 5 | my $entries = $self->_get_entries( $self->path ); | |||||||||
| 33 | |||||||||||
| 34 | 5 | my @children; | |||||||||
| 35 | |||||||||||
| 36 | 5 | for my $entry ( @$entries ) { | |||||||||
| 37 | |||||||||||
| 38 | 13 | my $obj; | |||||||||
| 39 | |||||||||||
| 40 | 13 | for ( $entry->{type} ) { | |||||||||
| 41 | |||||||||||
| 42 | 13 | when ( 'd' ) { | |||||||||
| 43 | |||||||||||
| 44 | $obj = Net::FTP::Path::Iter::Dir->new( %$entry, %attr, | ||||||||||
| 45 | 4 | path => catdir( $self->path, $entry->{name} ) ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 9 | when ( 'f' ) { | |||||||||
| 49 | |||||||||||
| 50 | $obj = Net::FTP::Path::Iter::File->new( %$entry, %attr, | ||||||||||
| 51 | 9 | path => catfile( $self->path, $entry->{name} ) ); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | default { | |||||||||
| 55 | |||||||||||
| 56 | 0 | warn( "ignoring $entry->{name}; unknown type $_\n" ); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 13 | push @children, $obj; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 5 | 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 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_retrieve_attrs: called CC=9 SCAR=26.8
| |||||||||||
| 74 | |||||||||||
| 75 | 1 | ▶ | return if $self->_has_attrs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 1 | my $server = $self->server; | |||||||||
| 78 | |||||||||||
| 79 | 1 | my $pwd = $server->pwd; | |||||||||
| 80 | |||||||||||
| 81 | 1 | my $entry = {}; | |||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | $server->cwd( $self->path ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | eval { | |||||||||
| 90 | |||||||||||
| 91 | # cdup sometimes returns ok even if it didn't work | ||||||||||
| 92 | 1 | $server->cdup; | |||||||||
| 93 | |||||||||||
| 94 | 1 | ▶ | if ( $pwd ne $server->pwd ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | my $entries = $self->_get_entries( '.' ); | |||||||||
| 97 | |||||||||||
| 98 | 0 | ( $entry ) = grep { $self->name eq $_->{name} } @$entries; | |||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | croak( "unable to find attributes for ", $self->path, "\n" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | if !$entry; | ||||||||||
| 102 | |||||||||||
| 103 | croak( $self->path, ": expected directory, got $entry->{type}\n" ) | ||||||||||
| 104 | 0 | ▶ | unless $entry->{type} eq 'd'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | # couldn't go up a directory; at the top? | ||||||||||
| 109 | else { | ||||||||||
| 110 | |||||||||||
| 111 | # fake it. | ||||||||||
| 112 | |||||||||||
| 113 | 1 | $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 | my $err = $@; | |||||||||
| 127 | |||||||||||
| 128 | 1 | ▶ | $server->cwd( $pwd ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | or croak( "unable to return to directory: $pwd\n" ); | ||||||||||
| 130 | |||||||||||
| 131 | 1 | ▶ | croak( $err ) if $err; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | |||||||||||
| 133 | 1 | $self->$_( $entry->{$_} ) for keys %$entry; | |||||||||
| 134 | 1 | 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 | 3 | 1; | |||||||||
| 147 | |||||||||||
| 148 | __END__ | ||||||||||