| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2003-2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Chj::IO::Dir | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | See L<Chj::xopendir>. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NOTE | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 23 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | package Chj::IO::Dir; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 25 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 25 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 25 | ▶ | use Chj::singlequote (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 25 | ▶ | use POSIX qw(EEXIST EBADF ENOENT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 25 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 25 | my %metadata; # -> [ is_open, path ] | |||||||||||||||||||||||||||
| 40 | 25 | $foo::foo = \%metadata; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub path { | ||||||||||||||||||||||||||
|
Subroutine
path: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 0 | $metadata{ pack "I", $self }[1] | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub xopendir { | ||||||||||||||||||||||||||
|
Subroutine
xopendir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 49 | 0 | my $hdl = gensym; | |||||||||||||||||||||||||||
| 50 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (opendir $hdl, $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | bless $hdl, $class; | |||||||||||||||||||||||||||
| 53 | 0 | $metadata{ pack "I", $hdl } = [1, $_[0]]; | |||||||||||||||||||||||||||
| 54 | 0 | return $hdl; | |||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 0 | croak "xopendir " . Chj::singlequote::singlequote_many(@_) . ": $!"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # *new = \&xopendir; really? no. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub opendir { | ||||||||||||||||||||||||||
|
Subroutine
opendir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 64 | 0 | my $hdl = gensym; | |||||||||||||||||||||||||||
| 65 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (opendir $hdl, $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | bless $hdl, $class; | |||||||||||||||||||||||||||
| 68 | 0 | $metadata{ pack "I", $hdl } = [1, $_[0]]; | |||||||||||||||||||||||||||
| 69 | 0 | return $hdl; | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | undef | ||||||||||||||||||||||||||||
| 72 | 0 | } | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub perhaps_opendir { | ||||||||||||||||||||||||||
|
Subroutine
perhaps_opendir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 77 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (defined(my $fh = $class->opendir(@_))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $fh | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | () | ||||||||||||||||||||||||||||
| 82 | 0 | } | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # (adapted copy of perhaps_xopen of File.pm) | ||||||||||||||||||||||||||||
| 86 | # die on all errors except ENOENT | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub perhaps_xopendir { | ||||||||||||||||||||||||||
|
Subroutine
perhaps_xopendir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (my ($fh) = $proto->perhaps_opendir(@_)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 90 | 0 | $fh | |||||||||||||||||||||||||||
| 91 | } elsif ($! == ENOENT) { | ||||||||||||||||||||||||||||
| 92 | () | ||||||||||||||||||||||||||||
| 93 | 0 | } else { | |||||||||||||||||||||||||||
| 94 | 0 | croak "xopen @_: $!"; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 100 | 0 | my $self = gensym; | |||||||||||||||||||||||||||
| 101 | 0 | bless $self, $class | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 107 | 0 | readdir $self | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub xread { | ||||||||||||||||||||||||||
|
Subroutine
xread: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # ^ Needed, CORE::readdir will not set it to 0. Thus maybe it will | ||||||||||||||||||||||||||||
| 115 | # not even set any error? Hm, well, at least on end of dir it sets | ||||||||||||||||||||||||||||
| 116 | # it to Bad file descriptor. | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if (wantarray) { ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | my $res = [CORE::readdir $self]; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # we *hope* that [ ] will never copy until the end as opposed | ||||||||||||||||||||||||||||
| 121 | # to @res = which *might* (well probably (or I think IIRC I've | ||||||||||||||||||||||||||||
| 122 | # even tested and confirmed it) does) copy all elements. | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | croak "xread: $!"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | @$res | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | my $res = CORE::readdir $self; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($! and $! != EBADF) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! and $! != 9
MC/DC0%0/2
$! and $! != 9
$!
$! != 9
Truth table0%0/3
A and B
A= $!B= $! != 9
| |||||||||||||||||||||||||||||
| 130 | 0 | croak "xread: $!"; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #croak "xread: $! (".($!+0).")"; ## exception objects would still be coool | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | $res | ||||||||||||||||||||||||||||
| 135 | 0 | } | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub nread { # ignore . and .. entries | ||||||||||||||||||||||||||
|
Subroutine
nread: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (wantarray) { ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | grep { $_ ne '.' and $_ ne '..' } readdir $self | ||||||||||||||||||||||||||
|
Condition0%0/3
$_ ne '.' && $_ ne '..'
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | while (defined(my $item = readdir $self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | return $item unless $item eq '.' or $item eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$item eq "." or $item eq ".."
MC/DC0%0/2
$item eq "." or $item eq ".."
$item eq "."
$item eq ".."
Truth table0%0/3
A or B
A= $item eq "."B= $item eq ".."
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | undef | ||||||||||||||||||||||||||||
| 148 | 0 | } | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub xnread { | ||||||||||||||||||||||||||
|
Subroutine
xnread: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if (wantarray) { ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $res = [grep { $_ ne '.' and $_ ne '..' } readdir $self]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$_ ne '.' && $_ ne '..'
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||
| 156 | 0 | @$res | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | while (defined(my $item = readdir $self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return $item unless $item eq '.' or $item eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$item eq "." or $item eq ".."
MC/DC0%0/2
$item eq "." or $item eq ".."
$item eq "."
$item eq ".."
Truth table0%0/3
A or B
A= $item eq "."B= $item eq ".."
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | undef | ||||||||||||||||||||||||||||
| 162 | 0 | } | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub telldir { | ||||||||||||||||||||||||||
|
Subroutine
telldir: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 168 | 0 | CORE::telldir $self | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub seekdir { | ||||||||||||||||||||||||||
|
Subroutine
seekdir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | my ($pos) = @_; | |||||||||||||||||||||||||||
| 175 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 176 | 0 | CORE::seekdir $self, $pos | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub xseekdir { | ||||||||||||||||||||||||||
|
Subroutine
xseekdir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | my ($pos) = @_; | |||||||||||||||||||||||||||
| 183 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | CORE::seekdir $self, $pos or croak "xseekdir (UNTESTED): $!"; ## | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub xrewind { | ||||||||||||||||||||||||||
|
Subroutine
xrewind: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 189 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 190 | 0 | ▶ | CORE::seekdir $self, 0 or croak "xrewind (UNTESTED): $!"; ## | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub xclose { | ||||||||||||||||||||||||||
|
Subroutine
xclose: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 194 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | #(maybe check metadata is_open first? not really useful) | ||||||||||||||||||||||||||||
| 197 | 0 | $! = undef; | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | closedir $self or croak "xclose: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | $metadata{ pack "I", $self }[0] = 0 | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 204 | 0 | local ($@, $!, $?, $_); | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ($metadata{ pack "I", $self }[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | closedir $self or carp "$self DESTROY: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 0 | delete $metadata{ pack "I", $self }; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 25 | 1; | |||||||||||||||||||||||||||