| 1 | package Sys::Export::VFAT::Directory; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Represents a case-folded directory in VFAT | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Sys::Export::LogAny '$log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Sys::Export::VFAT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use List::Util qw( min max ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | our @CARP_NOT= qw( Sys::Export::VFAT ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 70 | ▶ | sub new($class, %attrs) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | my $self= bless { | ||||||||||||||||||||||||||||
| 21 | name => delete $attrs{name}, | ||||||||||||||||||||||||||||
| 22 | parent => delete $attrs{parent}, | ||||||||||||||||||||||||||||
| 23 | file => delete $attrs{file}, | ||||||||||||||||||||||||||||
| 24 | 70 | ▶ | entries => delete $attrs{entries} // [], | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $attrs{"entries"} // []
MC/DC0%0/1
delete $attrs{"entries"} // []
delete $attrs{"entries"}
Truth table50%1/2
A
A= delete $attrs{"entries"}
| |||||||||||||||||||||||||||||
| 25 | ent_by_name => {}, | ||||||||||||||||||||||||||||
| 26 | }, $class; | ||||||||||||||||||||||||||||
| 27 | 70 | ▶ | croak "Unknown constructor option ".join(', ', keys %attrs) if keys %attrs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 70 | ▶ | weaken($self->{parent}) if $self->{parent}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 70 | for ($self->entries->@*) { | |||||||||||||||||||||||||||
| 30 | 0 | $self->ent_by_name->{$_->{name}}= $_->{name}; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | $self->ent_by_name->{$_->{shortname}}= $_->{shortname} if defined $_->{shortname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 70 | $self; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 104 | ▶ | sub name { $_[0]{name} } | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 39 | ▶ | sub parent { $_[0]{parent} } | ||||||||||||||||||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 138 | ▶ | sub is_root { !defined $_[0]{parent} } | ||||||||||||||||||||||||||
|
Subroutine
is_root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 11026 | ▶ | sub file { $_[0]{file} } | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 10932 | ▶ | sub entries { $_[0]{entries} } | ||||||||||||||||||||||||||
|
Subroutine
entries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 10661 | ▶ | sub ent_by_name { $_[0]{ent_by_name} } | ||||||||||||||||||||||||||
|
Subroutine
ent_by_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 98363 | ▶ | sub entry { | ||||||||||||||||||||||||||
|
Subroutine
entry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 98363 | $_[0]{ent_by_name}{lc $_[1]} | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 10552 | ▶ | sub add($self, $name, $file, %attrs) { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 10552 | ▶ | croak "Invalid long name" unless $self->is_valid_name($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 10552 | $attrs{name}= $name; | |||||||||||||||||||||||||||
| 53 | 10552 | $attrs{file}= $file; | |||||||||||||||||||||||||||
| 54 | 10552 | ▶ | $attrs{shortname} //= $name if $self->is_valid_shortname($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$attrs{"shortname"} //= $name
MC/DC0%0/2
$attrs{"shortname"} //= $name
$attrs{"shortname"}
$name
Truth table33%1/3
A //= B
A= $attrs{"shortname"}B= $name
| |||||||||||||||||||||||||||||
| 55 | # any conflict? | ||||||||||||||||||||||||||||
| 56 | 10552 | my $by_name= $self->ent_by_name; | |||||||||||||||||||||||||||
| 57 | croak "Path ".$self->name."/$name already exists" | ||||||||||||||||||||||||||||
| 58 | 10552 | ▶ | if defined $by_name->{lc $name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 10552 | ▶ | if (defined $attrs{shortname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 10516 | utf8::downgrade($attrs{shortname}); # must be bytes | |||||||||||||||||||||||||||
| 61 | 10516 | my $slot= \$by_name->{lc $attrs{shortname}}; | |||||||||||||||||||||||||||
| 62 | croak "Path ".$self->name."/$name short name '$attrs{shortname}' conflicts with " | ||||||||||||||||||||||||||||
| 63 | . $self->name."/".$$slot->{name} | ||||||||||||||||||||||||||||
| 64 | 10516 | ▶ | if $$slot; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 10516 | $$slot= \%attrs; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 10552 | $by_name->{lc $name}= \%attrs; | |||||||||||||||||||||||||||
| 68 | 10552 | push $self->entries->@*, \%attrs; | |||||||||||||||||||||||||||
| 69 | 10552 | \%attrs; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # These 3 can be overridden for ISO9660 subclass | ||||||||||||||||||||||||||||
| 74 | 10550 | ▶ | sub is_valid_name($self, $name) { | ||||||||||||||||||||||||||
|
Subroutine
is_valid_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 10550 | Sys::Export::VFAT::is_valid_longname($name) | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 10586 | ▶ | sub is_valid_shortname($self, $name) { | ||||||||||||||||||||||||||
|
Subroutine
is_valid_shortname: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 10586 | Sys::Export::VFAT::is_valid_shortname($name) | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 72 | ▶ | sub remove_invalid_shortname_chars($self, $name, $repl) { | ||||||||||||||||||||||||||
|
Subroutine
remove_invalid_shortname_chars: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 72 | Sys::Export::VFAT::remove_invalid_shortname_chars($name, $repl) | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 36 | ▶ | sub find_unused_shortname($self, $name) { | ||||||||||||||||||||||||||
|
Subroutine
find_unused_shortname: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 36 | ▶ | length $name or croak "name cannot be empty"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 36 | my $by_name= $self->ent_by_name; | |||||||||||||||||||||||||||
| 88 | 36 | my $ext_pos= rindex($name, '.'); | |||||||||||||||||||||||||||
| 89 | 36 | ▶ | my $base= $ext_pos < 0? $name : substr($name, 0, $ext_pos); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 36 | ▶ | my $ext= $ext_pos < 0? '' : substr($name, $ext_pos+1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 36 | for ($base, $ext) { | |||||||||||||||||||||||||||
| 92 | 72 | $_= $self->remove_invalid_shortname_chars($_, '_'); | |||||||||||||||||||||||||||
| 93 | # Now that all high characters have been removed, optimize these as bytes | ||||||||||||||||||||||||||||
| 94 | 72 | utf8::downgrade($_); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 36 | ▶ | $ext= '.'.substr($ext,0,3) if length $ext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 36 | my ($iter, $iter_len, $base_len)= (0,0, length $base); | |||||||||||||||||||||||||||
| 98 | 36 | ▶ | if (!$base_len || $base_len > 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $base_len or $base_len > 8
MC/DC50%1/2
not $base_len or $base_len > 8
not $base_len
$base_len > 8
Truth table66%2/3
not(A) or B
A= not $base_lenB= $base_len > 8
| |||||||||||||||||||||||||||||
| 99 | 1 | substr($base, min($base_len,6), $base_len, '~1'); | |||||||||||||||||||||||||||
| 100 | 1 | ($iter, $iter_len)= (1, 2); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 36 | ▶ | while ($by_name->{lc $base.$ext}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 36 | my $next_iter_len= 1 + length ++$iter; | |||||||||||||||||||||||||||
| 104 | 36 | my $iter_pos= min($base_len, 8 - $next_iter_len); | |||||||||||||||||||||||||||
| 105 | 36 | ▶ | croak "Can't find available ~N suffix for '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | if $iter_pos < 0; | ||||||||||||||||||||||||||||
| 107 | 36 | substr($base, $iter_pos, $next_iter_len, '~'.$iter); | |||||||||||||||||||||||||||
| 108 | 36 | $iter_len= $next_iter_len; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 36 | ▶ | $self->is_valid_shortname($base.$ext) or die "BUG: '$base$ext' is not a valid shortname"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 36 | return $base.$ext; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 73 | ▶ | sub build_shortnames($self) { | ||||||||||||||||||||||||||
|
Subroutine
build_shortnames: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 73 | my $by_name= $self->ent_by_name; | |||||||||||||||||||||||||||
| 117 | 73 | for (sort { lc $a->{name} cmp lc $b->{name} } $self->entries->@*) { | |||||||||||||||||||||||||||
| 118 | 10553 | ▶ | unless (defined $_->{shortname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 36 | $_->{shortname}= $self->find_unused_shortname($_->{name}); | |||||||||||||||||||||||||||
| 120 | 36 | $by_name->{lc $_->{shortname}}= $_; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 73 | return $self; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Avoiding dependency on namespace::clean | ||||||||||||||||||||||||||||
| 127 | 3 | delete @{Sys::Export::VFAT::Directory::}{qw( carp confess croak min max weaken )}; | |||||||||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||