| 1 | package Directory::Scanner::Stream; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Streaming directory iterator | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use UNIVERSAL::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Directory::Scanner::API::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 15 | 8 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use constant DEBUG => $ENV{DIR_SCANNER_STREAM_DEBUG} // 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DIR_SCANNER_STREAM_DEBUG"} // 0
MC/DC0%0/1
$ENV{"DIR_SCANNER_STREAM_DEBUG"} // 0
$ENV{"DIR_SCANNER_STREAM_DEBUG"}
Truth table50%1/2
A
A= $ENV{"DIR_SCANNER_STREAM_DEBUG"}
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ## ... | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | our @ISA; BEGIN { @ISA = ('UNIVERSAL::Object', 'Directory::Scanner::API::Stream') } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | our %HAS; BEGIN { | |||||||||||||||||||||||||||
| 23 | %HAS = ( | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | origin => sub { die 'You must supply a `origin` directory path' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 25 | # internal state ... | ||||||||||||||||||||||||||||
| 26 | ▶ | _head => sub {}, | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | ▶ | _handle => sub {}, | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 56 | ▶ | _is_done => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 56 | ▶ | _is_closed => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | ) | ||||||||||||||||||||||||||||
| 31 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ## ... | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub BUILD { | ||||||||||||||||||||||||||||
| 36 | 56 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 56 | my $dir = $self->{origin}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # upgrade this to a Path:Tiny | ||||||||||||||||||||||||||||
| 41 | # object if needed | ||||||||||||||||||||||||||||
| 42 | 56 | ▶ | $self->{origin} = $dir = Path::Tiny::path( $dir ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($dir) and $dir->isa("Path::Tiny")
MC/DC50%1/2
builtin::blessed($dir) and $dir->isa("Path::Tiny")
builtin::blessed($dir)
$dir->isa("Path::Tiny")
Truth table66%2/3
A and B
A= builtin::blessed($dir)B= $dir->isa("Path::Tiny")
| |||||||||||||||||||||||||||||
| 43 | unless Scalar::Util::blessed( $dir ) | ||||||||||||||||||||||||||||
| 44 | && $dir->isa('Path::Tiny'); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # make sure the directory is | ||||||||||||||||||||||||||||
| 47 | # fit to be streamed | ||||||||||||||||||||||||||||
| 48 | 56 | ▶ | (-d $dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | || Carp::confess 'Supplied path value must be a directory ('.$dir.')'; | ||||||||||||||||||||||||||||
| 50 | 56 | ▶ | (-r $dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | || Carp::confess 'Supplied path value must be a readable directory ('.$dir.')'; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 56 | my $handle; | |||||||||||||||||||||||||||
| 54 | 56 | ▶ | opendir( $handle, $dir ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | || Carp::confess 'Unable to open handle for directory('.$dir.') because: ' . $!; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 56 | $self->{_handle} = $handle; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub clone { | ||||||||||||||||||||||||||||
| 61 | 41 | ▶ | my ($self, $dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 41 | ▶ | $dir ||= $self->{origin}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$dir ||= $self->{"origin"}
MC/DC0%0/2
$dir ||= $self->{"origin"}
$dir
$self->{"origin"}
Truth table33%1/3
A ||= B
A= $dirB= $self->{"origin"}
| |||||||||||||||||||||||||||||
| 63 | 41 | return $self->new( origin => $dir ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ## accessor | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub origin { $_[0]->{_origin} } | ||||||||||||||||||||||||||
|
Subroutine
origin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ## API::Stream ... | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 8 | ▶ | sub head { $_[0]->{_head} } | ||||||||||||||||||||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 8 | ▶ | sub is_done { $_[0]->{_is_done} } | ||||||||||||||||||||||||||
|
Subroutine
is_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 59 | ▶ | sub is_closed { $_[0]->{_is_closed} } | ||||||||||||||||||||||||||
|
Subroutine
is_closed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub close { | ||||||||||||||||||||||||||||
| 77 | closedir( $_[0]->{_handle} ) | ||||||||||||||||||||||||||||
| 78 | 56 | ▶ | || Carp::confess 'Unable to close handle for directory because: ' . $!; | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=7.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 56 | $_[0]->{_is_closed} = 1; | |||||||||||||||||||||||||||
| 80 | 56 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub next { | ||||||||||||||||||||||||||||
| 84 | 157 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 157 | ▶ | return if $self->{_is_done}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Carp::confess 'Cannot call `next` on a closed stream' | ||||||||||||||||||||||||||||
| 89 | 157 | ▶ | if $self->{_is_closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 157 | my $next; | |||||||||||||||||||||||||||
| 92 | 157 | while (1) { | |||||||||||||||||||||||||||
| 93 | 269 | undef $next; # clear any previous values, just cause ... | |||||||||||||||||||||||||||
| 94 | 269 | $self->_log('Entering loop ... ') if DEBUG; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 269 | $self->_log('About to read directory ...') if DEBUG; | |||||||||||||||||||||||||||
| 97 | 269 | ▶ | if ( my $name = readdir( $self->{_handle} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 213 | $self->_log('Read directory ...') if DEBUG; | |||||||||||||||||||||||||||
| 100 | 213 | ▶ | next unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 213 | $self->_log('Got ('.$name.') from directory read ...') if DEBUG; | |||||||||||||||||||||||||||
| 103 | 213 | ▶ | next if $name eq '.' || $name eq '..'; # skip these ... | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "." or $name eq ".."
MC/DC100%2/2
$name eq "." or $name eq ".."
$name eq "."
$name eq ".."
Truth table100%3/3
A or B
A= $name eq "."B= $name eq ".."
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 101 | $next = $self->{origin}->child( $name ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # directory is not readable or has been removed, so skip it | ||||||||||||||||||||||||||||
| 108 | 101 | ▶ | if ( ! -r $next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $self->_log('Directory/File not readable ...') if DEBUG; | |||||||||||||||||||||||||||
| 110 | 0 | next; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 101 | $self->_log('Value is good, ready to return it') if DEBUG; | |||||||||||||||||||||||||||
| 114 | 101 | last; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||
| 118 | 56 | $self->_log('Exiting loop ... DONE') if DEBUG; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # cleanup ... | ||||||||||||||||||||||||||||
| 121 | 56 | $self->{_head} = undef; | |||||||||||||||||||||||||||
| 122 | 56 | $self->{_is_done} = 1; | |||||||||||||||||||||||||||
| 123 | 56 | last; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | $self->_log('... looping') if DEBUG; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 157 | $self->_log('Got next value('.$next.')') if DEBUG; | |||||||||||||||||||||||||||
| 129 | 157 | return $self->{_head} = $next; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 8 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||