| 1 | package Dancer::FileUtils; | ||||||||||||
| 2 | 239 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||
| 3 | #ABSTRACT: helper providing file utilities | ||||||||||||
| 4 | 239 | $Dancer::FileUtils::VERSION = '1.3520'; | |||||||||||
| 5 | 239 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 239 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 239 | ▶ | use IO::File; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 239 | ▶ | use File::Basename (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 239 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 239 | ▶ | use File::Temp qw(tempfile); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 239 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 239 | ▶ | use Cwd 'realpath'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 239 | ▶ | use Dancer::Exception qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 239 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 239 | ▶ | use vars '@EXPORT_OK'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | 239 | @EXPORT_OK = qw( | |||||||||||
| 23 | dirname open_file path read_file_content read_glob_content | ||||||||||||
| 24 | path_or_empty set_file_mode normalize_path | ||||||||||||
| 25 | atomic_write | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | # path should not verify paths | ||||||||||||
| 29 | # just normalize | ||||||||||||
| 30 | sub path { | ||||||||||||
| 31 | 2526 | ▶ | my @parts = @_; | ||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 32 | 2526 | my $path = File::Spec->catfile(@parts); | |||||||||||
| 33 | |||||||||||||
| 34 | 2526 | return normalize_path($path); | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | sub path_or_empty { | ||||||||||||
| 38 | 1 | ▶ | my @parts = @_; | ||||||||||
|
Subroutine
path_or_empty: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 39 | 1 | my $path = path(@parts); | |||||||||||
| 40 | |||||||||||||
| 41 | # return empty if it doesn't exist | ||||||||||||
| 42 | 1 | ▶ | return -e $path ? $path : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 12 | ▶ | sub dirname { File::Basename::dirname(@_) } | ||||||||||
|
Subroutine
dirname: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 46 | |||||||||||||
| 47 | sub set_file_mode { | ||||||||||||
| 48 | 143 | ▶ | my $fh = shift; | ||||||||||
|
Subroutine
set_file_mode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 49 | |||||||||||||
| 50 | 143 | require Dancer::Config; | |||||||||||
| 51 | 143 | ▶ | my $charset = Dancer::Config::setting('charset') || 'utf-8'; | ||||||||||
|
Condition100%2/2
Dancer::Config::setting('charset') || 'utf-8'
MC/DC100%1/1
Dancer::Config::setting('charset') || 'utf-8'
Dancer::Config::setting('charset')
Truth table100%2/2
A
A= Dancer::Config::setting('charset')
| |||||||||||||
| 52 | 143 | ▶ | binmode $fh, ":encoding($charset)"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | |||||||||||||
| 54 | 143 | return $fh; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | sub open_file { | ||||||||||||
| 58 | 140 | ▶ | my ( $mode, $filename ) = @_; | ||||||||||
|
Subroutine
open_file: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 59 | |||||||||||||
| 60 | 140 | ▶ | open my $fh, $mode, $filename | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | or raise core_fileutils => "$! while opening '$filename' using mode '$mode'"; | ||||||||||||
| 62 | |||||||||||||
| 63 | 140 | return set_file_mode($fh); | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | sub read_file_content { | ||||||||||||
| 67 | 60 | ▶ | my $file = shift or return; | ||||||||||
|
Subroutine
read_file_content: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
| |||||||||||||
| 68 | 60 | my $fh = open_file( '<', $file ); | |||||||||||
| 69 | |||||||||||||
| 70 | return wantarray ? | ||||||||||||
| 71 | 60 | ▶ | read_glob_content($fh) : | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | scalar read_glob_content($fh); | ||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | sub read_glob_content { | ||||||||||||
| 76 | 64 | ▶ | my $fh = shift; | ||||||||||
|
Subroutine
read_glob_content: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 77 | |||||||||||||
| 78 | # we don't want to do that as we'll encode the stuff later | ||||||||||||
| 79 | # binmode $fh; | ||||||||||||
| 80 | |||||||||||||
| 81 | 64 | my @content = <$fh>; | |||||||||||
| 82 | 64 | close $fh; | |||||||||||
| 83 | |||||||||||||
| 84 | 64 | ▶ | return wantarray ? @content : join '', @content; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | sub normalize_path { | ||||||||||||
| 88 | # this is a revised version of what is described in | ||||||||||||
| 89 | # http://www.linuxjournal.com/content/normalizing-path-names-bash | ||||||||||||
| 90 | # by Mitch Frazier | ||||||||||||
| 91 | 2536 | ▶ | my $path = shift or return; | ||||||||||
|
Subroutine
normalize_path: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 92 | 2536 | my $seqregex = qr{ | |||||||||||
| 93 | [^/]* # anything without a slash | ||||||||||||
| 94 | /\.\./ # that is accompanied by two dots as such | ||||||||||||
| 95 | }x; | ||||||||||||
| 96 | |||||||||||||
| 97 | 2536 | $path =~ s{/\./}{/}g; | |||||||||||
| 98 | 2536 | ▶ | while ( $path =~ s{$seqregex}{} ) {} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | |||||||||||||
| 100 | 2536 | return $path; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | # !! currently unused | ||||||||||||
| 104 | # Undo UNC special-casing catfile-voodoo on cygwin | ||||||||||||
| 105 | sub _trim_UNC { | ||||||||||||
| 106 | 0 | ▶ | my @args = @_; | ||||||||||
|
Subroutine
_trim_UNC: not called CC=7 SCAR=40.3
| |||||||||||||
| 107 | |||||||||||||
| 108 | # if we're using cygwin | ||||||||||||
| 109 | 0 | ▶ | if ( $^O eq 'cygwin' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | # no @args, no problem | ||||||||||||
| 111 | 0 | ▶ | @args or return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | |||||||||||||
| 113 | 0 | my ( $slashes, $part, @parts) = ( 0, undef, @args ); | |||||||||||
| 114 | |||||||||||||
| 115 | # start pulling part from @parts | ||||||||||||
| 116 | 0 | ▶ | while ( defined ( $part = shift @parts ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 0 | ▶ | last if $part; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | $slashes++; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | # count slashes in $part | ||||||||||||
| 122 | 0 | $slashes += ( $part =~ s/^[\/\\]+// ); | |||||||||||
| 123 | |||||||||||||
| 124 | 0 | ▶ | if ( $slashes == 2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 0 | return ( '/' . $part, @parts ); | |||||||||||
| 126 | } else { | ||||||||||||
| 127 | 0 | my $slashstr = ''; | |||||||||||
| 128 | 0 | $slashstr .= '/' for ( 1 .. $slashes ); | |||||||||||
| 129 | |||||||||||||
| 130 | 0 | return ( $slashstr . $part, @parts ); | |||||||||||
| 131 | } | ||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 0 | return @args; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | sub atomic_write { | ||||||||||||
| 138 | 4 | ▶ | my ($path, $file, $data) = @_; | ||||||||||
|
Subroutine
atomic_write: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 139 | 4 | my ($fh, $filename) = tempfile("tmpXXXXXXXXX", DIR => $path); | |||||||||||
| 140 | 3 | set_file_mode($fh); | |||||||||||
| 141 | 3 | print $fh $data; | |||||||||||
| 142 | 3 | ▶ | close $fh or die "Can't close '$file': $!\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 3 | ▶ | rename($filename, $file) or die "Can't move '$filename' to '$file'"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | 239 | 1; | |||||||||||
| 147 | |||||||||||||
| 148 | __END__ | ||||||||||||