| 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.07'; | |||||||||||||||||||
| 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 | 5 | ▶ | sub _children { | ||||||||||||||||||
|
Subroutine
_children: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 5 | my $self = shift; | |||||||||||||||||||
| 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 | ▶ | if ( $entry->{type} eq 'd' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | $obj | ||||||||||||||||||||
| 43 | 4 | = 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 | path => catfile( $self->path, $entry->{name} ) ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | warn( "ignoring $entry->{name}; unknown type $_\n" ); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 13 | push @children, $obj; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 5 | 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 | 1 | ▶ | sub _retrieve_attrs { | ||||||||||||||||||
|
Subroutine
_retrieve_attrs: called CC=10 SCAR=29.0
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | my $self = shift; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | ▶ | return if $self->_has_attrs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | my $server = $self->server; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | my $pwd = $server->pwd; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | my $entry = {}; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | ▶ | $server->cwd( $self->path ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | my $err; | |||||||||||||||||||
| 86 | 1 | ▶ | eval { | ||||||||||||||||||
|
Condition33%1/3
eval {
do {
$server->cdup;
if ($pwd ne $server->pwd) {
my $entries = $self->_get_entries(".");
($entry) = grep({$self->name eq $_->{"name"};} @$entries);
croak("unable to find attributes for ", $self->path) unless $entry;
croak($self->path, qq[: expected directory, got $entry->{"type"}]) unless $entry->{"type"} eq "d";
}
else {
$entry = {"size", 0, "mtime", 0, "mode", 365, "type", "d", "_has_attrs", 1};
}
}
} // ($err = $@)
MC/DC0%0/2
eval {
do {
$server->cdup;
if ($pwd ne $server->pwd) {
my $entries = $self->_get_entries(".");
($entry) = grep({$self->name eq $_->{"name"};} @$entries);
croak("unable to find attributes for ", $self->path) unless $entry;
croak($self->path, qq[: expected directory, got $entry->{"type"}]) unless $entry->{"type"} eq "d";
}
else {
$entry = {"size", 0, "mtime", 0, "mode", 365, "type", "d", "_has_attrs", 1};
}
}
} // ($err = $@)
eval {
do {
$server->cdup;
if ($pwd ne $server->pwd) {
my $entries = $self->_get_entries(".");
($entry) = grep({$self->name eq $_->{"name"};} @$entries);
croak("unable to find attributes for ", $self->path) unless $entry;
croak($self->path, qq[: expected directory, got $entry->{"type"}]) unless $entry->{"type"} eq "d";
}
else {
$entry = {"size", 0, "mtime", 0, "mode", 365, "type", "d", "_has_attrs", 1};
}
}
}
($err = $@)
Truth table33%1/3
A // B
A= eval {
do {
$server->cdup;
if ($pwd ne $server->pwd) {
my $entries = $self->_get_entries(".");
($entry) = grep({$self->name eq $_->{"name"};} @$entries);
croak("unable to find attributes for ", $self->path) unless $entry;
croak($self->path, qq[: expected directory, got $entry->{"type"}]) unless $entry->{"type"} eq "d";
}
else {
$entry = {"size", 0, "mtime", 0, "mode", 365, "type", "d", "_has_attrs", 1};
}
}
}B= ($err = $@)
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # cdup sometimes returns ok even if it didn't work | ||||||||||||||||||||
| 89 | 1 | $server->cdup; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | ▶ | if ( $pwd ne $server->pwd ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | my $entries = $self->_get_entries( q{.} ); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ( $entry ) = grep { $self->name eq $_->{name} } @$entries; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | croak( 'unable to find attributes for ', $self->path ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | if !$entry; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | croak( $self->path, ": expected directory, got $entry->{type}" ) | ||||||||||||||||||||
| 101 | 0 | ▶ | unless $entry->{type} eq 'd'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # couldn't go up a directory; at the top? | ||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # fake it. | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | $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 | ▶ | $server->cwd( $pwd ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | or croak( "unable to return to directory: $pwd" ); | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | ▶ | croak( $err ) if defined $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | $self->$_( $entry->{$_} ) for keys %$entry; | |||||||||||||||||||
| 128 | 1 | 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 | 3 | 1; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||