| 1 | package Skilletor::FSUtil; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: File and path helpers that behave as the reference's Node calls do | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 247 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 247 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 247 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 247 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 247 | ▶ | use Digest::SHA qw( sha256_hex ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 247 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 247 | ▶ | use Errno (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 247 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 247 | ▶ | use Fcntl (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 247 | ▶ | use Path::Tiny qw( path ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 247 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 247 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 19 | read_text decode_utf8_lossy encode_text | ||||||||||||||||||||||||||||||||
| 20 | inner_segments hash_bytes is_executable | ||||||||||||||||||||||||||||||||
| 21 | atomic_write write_text | ||||||||||||||||||||||||||||||||
| 22 | path_join path_normalize path_resolve is_absolute | ||||||||||||||||||||||||||||||||
| 23 | real_or_resolved same_path same_file to_posix | ||||||||||||||||||||||||||||||||
| 24 | node_fs_error | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 247 | ▶ | use constant WIN32 => $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | #### Text ############################################################## | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # readFileSync(path, "utf8"): bytes decoded as UTF-8, every ill-formed sequence one U+FFFD as | ||||||||||||||||||||||||||||||||
| 32 | # the WHATWG decoder (and so V8) replaces it. No BOM is stripped. | ||||||||||||||||||||||||||||||||
| 33 | 24790 | ▶ | sub decode_utf8_lossy { | ||||||||||||||||||||||||||||||
|
Subroutine
decode_utf8_lossy: called CC=19 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 24790 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||||||
| 35 | 24790 | my $copy = $bytes; | |||||||||||||||||||||||||||||||
| 36 | 24790 | my $text = eval { Encode::decode( 'UTF-8', $copy, Encode::FB_CROAK() ) }; | |||||||||||||||||||||||||||||||
| 37 | 24790 | ▶ | return $text if defined $text; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 21 | my $out = ''; | |||||||||||||||||||||||||||||||
| 39 | 21 | my ( $needed, $seen, $cp, $lower, $upper ) = ( 0, 0, 0, 0x80, 0xBF ); | |||||||||||||||||||||||||||||||
| 40 | 21 | my @bytes = unpack 'C*', $bytes; | |||||||||||||||||||||||||||||||
| 41 | 21 | my $i = 0; | |||||||||||||||||||||||||||||||
| 42 | 21 | ▶ | while ( $i < @bytes ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 116007 | my $b = $bytes[$i]; | |||||||||||||||||||||||||||||||
| 44 | 116007 | ▶ | if ( $needed == 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 115931 | $i++; | |||||||||||||||||||||||||||||||
| 46 | 115931 | ▶ | if ( $b <= 0x7F ) { $out .= chr $b; next } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 70 | ▶ | if ( $b >= 0xC2 && $b <= 0xDF ) { $needed = 1; $cp = $b & 0x1F; next } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b >= 194 and $b <= 223
MC/DC100%2/2
$b >= 194 and $b <= 223
$b >= 194
$b <= 223
Truth table100%3/3
A and B
A= $b >= 194B= $b <= 223
| |||||||||||||||||||||||||||||||||
| 48 | 52 | ▶ | if ( $b >= 0xE0 && $b <= 0xEF ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b >= 224 and $b <= 239
MC/DC100%2/2
$b >= 224 and $b <= 239
$b >= 224
$b <= 239
Truth table100%3/3
A and B
A= $b >= 224B= $b <= 239
| |||||||||||||||||||||||||||||||||
| 49 | 23 | ▶ | $lower = 0xA0 if $b == 0xE0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 23 | ▶ | $upper = 0x9F if $b == 0xED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 23 | $needed = 2; | |||||||||||||||||||||||||||||||
| 52 | 23 | $cp = $b & 0xF; | |||||||||||||||||||||||||||||||
| 53 | 23 | next; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | 29 | ▶ | if ( $b >= 0xF0 && $b <= 0xF4 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b >= 240 and $b <= 244
MC/DC100%2/2
$b >= 240 and $b <= 244
$b >= 240
$b <= 244
Truth table100%3/3
A and B
A= $b >= 240B= $b <= 244
| |||||||||||||||||||||||||||||||||
| 56 | 8 | ▶ | $lower = 0x90 if $b == 0xF0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | $upper = 0x8F if $b == 0xF4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 8 | $needed = 3; | |||||||||||||||||||||||||||||||
| 59 | 8 | $cp = $b & 0x7; | |||||||||||||||||||||||||||||||
| 60 | 8 | next; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | 21 | $out .= "\x{FFFD}"; | |||||||||||||||||||||||||||||||
| 63 | 21 | next; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 76 | ▶ | if ( $b < $lower || $b > $upper ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b < $lower or $b > $upper
MC/DC100%2/2
$b < $lower or $b > $upper
$b < $lower
$b > $upper
Truth table100%3/3
A or B
A= $b < $lowerB= $b > $upper
| |||||||||||||||||||||||||||||||||
| 66 | 9 | ( $needed, $seen, $cp, $lower, $upper ) = ( 0, 0, 0, 0x80, 0xBF ); | |||||||||||||||||||||||||||||||
| 67 | 9 | $out .= "\x{FFFD}"; | |||||||||||||||||||||||||||||||
| 68 | 9 | next; # the byte is read again as the start of a sequence | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | 67 | $i++; | |||||||||||||||||||||||||||||||
| 71 | 67 | ( $lower, $upper ) = ( 0x80, 0xBF ); | |||||||||||||||||||||||||||||||
| 72 | 67 | $cp = ( $cp << 6 ) | ( $b & 0x3F ); | |||||||||||||||||||||||||||||||
| 73 | 67 | ▶ | next unless ++$seen == $needed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 36 | $out .= chr $cp; | |||||||||||||||||||||||||||||||
| 75 | 36 | ( $needed, $seen, $cp ) = ( 0, 0, 0 ); | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 21 | ▶ | $out .= "\x{FFFD}" if $needed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 21 | return $out; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 680 | ▶ | sub read_text { | ||||||||||||||||||||||||||||||
|
Subroutine
read_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 82 | 680 | my ( $file ) = @_; | |||||||||||||||||||||||||||||||
| 83 | 680 | return decode_utf8_lossy( path($file)->slurp_raw ); | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | # A JavaScript string as UTF-8, as Buffer.from(s) and writeFileSync encode it: a lone | ||||||||||||||||||||||||||||||||
| 87 | # surrogate becomes U+FFFD. | ||||||||||||||||||||||||||||||||
| 88 | 13827 | ▶ | sub encode_text { | ||||||||||||||||||||||||||||||
|
Subroutine
encode_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 89 | 13827 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||
| 90 | 247 | ▶ | no warnings qw( surrogate utf8 ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 91 | 13827 | ( my $clean = "$text" ) =~ s/[\x{D800}-\x{DFFF}]/\x{FFFD}/g; | |||||||||||||||||||||||||||||||
| 92 | 13827 | return Encode::encode( 'UTF-8', $clean ); | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | #### Paths ############################################################# | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 59142 | ▶ | sub is_absolute { | ||||||||||||||||||||||||||||||
|
Subroutine
is_absolute: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 59142 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||
| 99 | 59142 | ▶ | return 1 if $p =~ m{\A/}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 19336 | return 1 if WIN32 && $p =~ m{\A(?:[\\]|[A-Za-z]:[\\/])}; | |||||||||||||||||||||||||||||||
| 101 | 19336 | return 0; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | # path.normalize (posix): "." segments and doubled slashes go, ".." pops (never above the | ||||||||||||||||||||||||||||||||
| 105 | # root; kept in front of a relative path), a trailing slash stays, "" is ".". | ||||||||||||||||||||||||||||||||
| 106 | 45941 | ▶ | sub path_normalize { | ||||||||||||||||||||||||||||||
|
Subroutine
path_normalize: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 107 | 45941 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||
| 108 | 45941 | ▶ | return '.' if $p eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 45939 | ▶ | my $abs = $p =~ m{\A/} ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 45939 | ▶ | my $trailing = $p =~ m{/\z} ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 45939 | my @out; | |||||||||||||||||||||||||||||||
| 112 | 45939 | for my $seg ( split m{/+}, $p ) { | |||||||||||||||||||||||||||||||
| 113 | 279486 | ▶ | next if $seg eq '' || $seg eq '.'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$seg eq "" or $seg eq "."
MC/DC100%2/2
$seg eq "" or $seg eq "."
$seg eq ""
$seg eq "."
Truth table100%3/3
A or B
A= $seg eq ""B= $seg eq "."
| |||||||||||||||||||||||||||||||||
| 114 | 233740 | ▶ | if ( $seg eq '..' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 19273 | ▶ | if ( @out && $out[-1] ne '..' ) { pop @out } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@out and $out[-1] ne ".."
MC/DC100%2/2
@out and $out[-1] ne ".."
@out
$out[-1] ne ".."
Truth table100%3/3
A and B
A= @outB= $out[-1] ne ".."
| |||||||||||||||||||||||||||||||||
| 116 | 7 | elsif ( !$abs ) { push @out, '..' } | |||||||||||||||||||||||||||||||
| 117 | 19273 | next; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | 214467 | push @out, $seg; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 45939 | my $joined = join '/', @out; | |||||||||||||||||||||||||||||||
| 122 | 45939 | ▶ | return $abs ? '/' : ( $trailing ? './' : '.' ) if $joined eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 123 | 45930 | ▶ | return ( $abs ? '/' : '' ).$joined.( $trailing ? '/' : '' ); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 25959 | ▶ | sub path_join { | ||||||||||||||||||||||||||||||
|
Subroutine
path_join: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 127 | 25959 | my ( @parts ) = @_; | |||||||||||||||||||||||||||||||
| 128 | 25959 | my $joined = join '/', grep { length } @parts; | |||||||||||||||||||||||||||||||
| 129 | 25959 | return path_normalize($joined); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | # path.resolve (posix): right to left until an absolute path, then the cwd; normalized, | ||||||||||||||||||||||||||||||||
| 133 | # without a trailing slash. | ||||||||||||||||||||||||||||||||
| 134 | 19901 | ▶ | sub path_resolve { | ||||||||||||||||||||||||||||||
|
Subroutine
path_resolve: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 135 | 19901 | my ( @parts ) = @_; | |||||||||||||||||||||||||||||||
| 136 | 19901 | my $resolved = ''; | |||||||||||||||||||||||||||||||
| 137 | 19901 | for my $part ( reverse @parts ) { | |||||||||||||||||||||||||||||||
| 138 | 39154 | ▶ | next unless defined $part && length $part; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $part and length $part
MC/DC0%0/2
defined $part and length $part
defined $part
length $part
Truth table33%1/3
A and B
A= defined $partB= length $part
| |||||||||||||||||||||||||||||||||
| 139 | 39154 | ▶ | $resolved = $part.( length $resolved ? '/'.$resolved : '' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 39154 | ▶ | last if is_absolute($part); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | 19901 | ▶ | $resolved = Cwd::getcwd().( length $resolved ? '/'.$resolved : '' ) unless is_absolute($resolved); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 143 | 19901 | my $n = path_normalize($resolved); | |||||||||||||||||||||||||||||||
| 144 | 19901 | $n =~ s{(?<=.)/\z}{}; | |||||||||||||||||||||||||||||||
| 145 | 19901 | return $n; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 54 | ▶ | sub to_posix { | ||||||||||||||||||||||||||||||
|
Subroutine
to_posix: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 149 | 54 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||
| 150 | 54 | return $p unless WIN32; | |||||||||||||||||||||||||||||||
| 151 | 0 | ( my $q = $p ) =~ s{\\}{/}g; | |||||||||||||||||||||||||||||||
| 152 | 0 | return $q; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | # The segments of a path relative to some root ("\" separates like "/", empty and "." | ||||||||||||||||||||||||||||||||
| 156 | # segments dropped), or why it names nothing beneath that root. | ||||||||||||||||||||||||||||||||
| 157 | 59 | ▶ | sub inner_segments { | ||||||||||||||||||||||||||||||
|
Subroutine
inner_segments: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 158 | 59 | my ( $entry ) = @_; | |||||||||||||||||||||||||||||||
| 159 | 59 | ▶ | return 'absolute' if is_absolute($entry) || $entry =~ m{\A[/\\]}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_absolute($entry) or $entry =~ m(\A[/\\])
MC/DC100%2/2
is_absolute($entry) or $entry =~ m(\A[/\\])
is_absolute($entry)
$entry =~ m(\A[/\\])
Truth table100%3/3
A or B
A= is_absolute($entry)B= $entry =~ m(\A[/\\])
| |||||||||||||||||||||||||||||||||
| 160 | 53 | ▶ | my @segments = grep { $_ ne '' && $_ ne '.' } split m{[\\/]}, $entry, -1; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ ne '' && $_ ne '.'
MC/DC100%2/2
$_ ne '' && $_ ne '.'
$_ ne ''
$_ ne '.'
Truth table100%3/3
A && B
A= $_ ne ''B= $_ ne '.'
| |||||||||||||||||||||||||||||||||
| 161 | 53 | ▶ | return 'dotdot' if grep { $_ eq '..' } @segments; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | 46 | ▶ | return @segments ? \@segments : 'root'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 702 | ▶ | sub real_or_resolved { | ||||||||||||||||||||||||||||||
|
Subroutine
real_or_resolved: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 166 | 702 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||
| 167 | 702 | ▶ | my $real = -e $p ? eval { Cwd::abs_path($p) } : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | 702 | ▶ | return defined $real ? $real : path_resolve($p); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 351 | ▶ | sub same_path { | ||||||||||||||||||||||||||||||
|
Subroutine
same_path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 172 | 351 | my ( $a, $b ) = @_; | |||||||||||||||||||||||||||||||
| 173 | 351 | ▶ | return real_or_resolved($a) eq real_or_resolved($b) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub _dirname { my ( $p ) = @_; ( my $d = $p ) =~ s{/[^/]*\z}{}; return length $d ? $d : '/' } | ||||||||||||||||||||||||||||||
|
Subroutine
_dirname: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _basename { my ( $p ) = @_; return ( $p =~ m{([^/]*)\z} )[0] } | ||||||||||||||||||||||||||||||
|
Subroutine
_basename: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | # Where a file path ends up once symlinks are followed, also for a missing file or a dangling | ||||||||||||||||||||||||||||||||
| 180 | # link (then: the real parent dir joined with the final name). | ||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _file_target { | ||||||||||||||||||||||||||||||
|
Subroutine
_file_target: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 182 | 0 | my ( $path ) = @_; | |||||||||||||||||||||||||||||||
| 183 | 0 | my $p = path_resolve($path); | |||||||||||||||||||||||||||||||
| 184 | 0 | for ( 1 .. 40 ) { | |||||||||||||||||||||||||||||||
| 185 | 0 | my $bytes = encode_text($p); | |||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( -e $bytes ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | 0 | my $real = eval { Cwd::abs_path($bytes) }; | |||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | return Encode::decode( 'UTF-8', $real ) if defined $real; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | last unless -l $bytes; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | 0 | my $to = readlink $bytes; | |||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | last unless defined $to; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 0 | $p = path_resolve( _dirname($p), Encode::decode( 'UTF-8', $to ) ); | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | 0 | return path_join( real_or_resolved( _dirname($p) ), _basename($p) ); | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | # True when writing $a would write $b: the same target path, or the same inode. Never throws. | ||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub same_file { | ||||||||||||||||||||||||||||||
|
Subroutine
same_file: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 200 | 0 | my ( $a, $b ) = @_; | |||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return 1 if _file_target($a) eq _file_target($b); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | my @sa = stat encode_text($a) or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my @sb = stat encode_text($b) or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | return ( $sa[0] == $sb[0] && $sa[1] == $sb[1] ) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sa[0] == $sb[0] and $sa[1] == $sb[1]
MC/DC0%0/2
$sa[0] == $sb[0] and $sa[1] == $sb[1]
$sa[0] == $sb[0]
$sa[1] == $sb[1]
Truth table0%0/3
A and B
A= $sa[0] == $sb[0]B= $sa[1] == $sb[1]
| |||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | #### Errors ############################################################ | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | # libuv's words for the errors a file system call meets. | ||||||||||||||||||||||||||||||||
| 210 | 247 | my %UV_MESSAGE = ( | |||||||||||||||||||||||||||||||
| 211 | ENOENT => 'no such file or directory', | ||||||||||||||||||||||||||||||||
| 212 | EACCES => 'permission denied', | ||||||||||||||||||||||||||||||||
| 213 | EPERM => 'operation not permitted', | ||||||||||||||||||||||||||||||||
| 214 | ENOTDIR => 'not a directory', | ||||||||||||||||||||||||||||||||
| 215 | EISDIR => 'illegal operation on a directory', | ||||||||||||||||||||||||||||||||
| 216 | ELOOP => 'too many symbolic links encountered', | ||||||||||||||||||||||||||||||||
| 217 | ENAMETOOLONG => 'name too long', | ||||||||||||||||||||||||||||||||
| 218 | EMFILE => 'too many open files', | ||||||||||||||||||||||||||||||||
| 219 | ENFILE => 'file table overflow', | ||||||||||||||||||||||||||||||||
| 220 | EIO => 'i/o error', | ||||||||||||||||||||||||||||||||
| 221 | ENOMEM => 'not enough memory', | ||||||||||||||||||||||||||||||||
| 222 | EBUSY => 'resource busy or locked', | ||||||||||||||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | # The message Node gives a failed fs call: "ENOENT: no such file or directory, lstat '/x'". A | ||||||||||||||||||||||||||||||||
| 226 | # read of a directory names no path ("EISDIR: illegal operation on a directory, read"). | ||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | sub node_fs_error { | ||||||||||||||||||||||||||||||
|
Subroutine
node_fs_error: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 228 | 1 | my ( $errno, $syscall, $path ) = @_; | |||||||||||||||||||||||||||||||
| 229 | 1 | local $! = $errno; | |||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | my $where = ', '.$syscall.( defined $path ? " '".$path."'" : '' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 1 | for my $code ( sort keys %UV_MESSAGE ) { | |||||||||||||||||||||||||||||||
| 232 | 11 | ▶ | return $code.': '.$UV_MESSAGE{$code}.$where if $!{$code}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | 0 | return "$!".$where; | |||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | #### Content ########################################################### | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 137 | ▶ | sub hash_bytes { | ||||||||||||||||||||||||||||||
|
Subroutine
hash_bytes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 240 | 137 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||||||
| 241 | 137 | return 'sha256:'.sha256_hex($bytes); | |||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | 84 | ▶ | sub is_executable { | ||||||||||||||||||||||||||||||
|
Subroutine
is_executable: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 245 | 84 | my ( $mode ) = @_; | |||||||||||||||||||||||||||||||
| 246 | 84 | ▶ | return ( ( $mode // 0 ) & 0100 ) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$mode // 0
MC/DC0%0/1
$mode // 0
$mode
Truth table50%1/2
A
A= $mode
| |||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 247 | my @BASE36 = ( 0 .. 9, 'a' .. 'z' ); | |||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 135 | ▶ | sub atomic_write { | ||||||||||||||||||||||||||||||
|
Subroutine
atomic_write: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 252 | 135 | my ( $file, $bytes, $executable ) = @_; | |||||||||||||||||||||||||||||||
| 253 | 135 | ▶ | croak __PACKAGE__.'->atomic_write needs bytes, not characters' if $bytes =~ /[^\x{0}-\x{FF}]/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 254 | 134 | my $dir = path($file)->parent; | |||||||||||||||||||||||||||||||
| 255 | 134 | $dir->mkpath; | |||||||||||||||||||||||||||||||
| 256 | 134 | my $tmp = $dir->child( '.skilletor-tmp-'.$$.'-'.join '', map { $BASE36[ int rand 36 ] } 1 .. 11 ); | |||||||||||||||||||||||||||||||
| 257 | 134 | my $ok = eval { | |||||||||||||||||||||||||||||||
| 258 | 134 | ▶ | sysopen( my $fh, "$tmp", Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_TRUNC(), $executable ? 0777 : 0666 ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 259 | or croak 'open '.$tmp.': '.$!; | ||||||||||||||||||||||||||||||||
| 260 | 131 | binmode $fh; | |||||||||||||||||||||||||||||||
| 261 | 131 | ▶ | print {$fh} $bytes or croak 'write '.$tmp.': '.$!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | 131 | ▶ | close $fh or croak 'close '.$tmp.': '.$!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 263 | 131 | ▶ | rename "$tmp", "$file" or croak 'rename '.$tmp.': '.$!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 131 | 1; | |||||||||||||||||||||||||||||||
| 265 | }; | ||||||||||||||||||||||||||||||||
| 266 | 134 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | 3 | my $err = $@; | |||||||||||||||||||||||||||||||
| 268 | 3 | unlink "$tmp"; | |||||||||||||||||||||||||||||||
| 269 | 3 | die $err; | |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | 131 | return; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 87 | ▶ | sub write_text { | ||||||||||||||||||||||||||||||
|
Subroutine
write_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 275 | 87 | my ( $file, $text, $executable ) = @_; | |||||||||||||||||||||||||||||||
| 276 | 87 | return atomic_write( $file, encode_text($text), $executable ); | |||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | 247 | 1; | |||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | __END__ | ||||||||||||||||||||||||||||||||