| 1 | # Connector::Builtin::File::Path | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Proxy class for accessing files | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Written by Oliver Welter for the OpenXPKI project 2012 | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | package Connector::Builtin::File::Path; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use English; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | extends 'Connector::Builtin'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | with 'Connector::Role::LocalPath'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | has content => ( | |||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||
| 23 | isa => 'Str', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | has ifexists => ( | |||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | isa => 'Str', | ||||||||||||||||||||||||||||
| 29 | default => 'replace' | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | has user => ( | |||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||
| 34 | isa => 'Str', | ||||||||||||||||||||||||||||
| 35 | default => '' | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | has group => ( | |||||||||||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||||||||||||
| 40 | isa => 'Str', | ||||||||||||||||||||||||||||
| 41 | default => '' | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | has mode => ( | |||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||
| 46 | isa => 'Str', | ||||||||||||||||||||||||||||
| 47 | default => '' | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _build_config { | ||||||||||||||||||||||||||
|
Subroutine
_build_config: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (! -d $self->{LOCATION}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | confess("Cannot open directory " . $self->{LOCATION} ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return 1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # return the content of the file | ||||||||||||||||||||||||||||
| 61 | 6 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 6 | my $path = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 6 | my $filename = $self->_sanitize_path( $path ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | if (! -r $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | return $self->_node_not_exists( $path ); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | my $content = do { | |||||||||||||||||||||||||||
| 73 | 6 | local $INPUT_RECORD_SEPARATOR; | |||||||||||||||||||||||||||
| 74 | 6 | open my $fh, '<', $filename; | |||||||||||||||||||||||||||
| 75 | 6 | <$fh>; | |||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||
| 77 | 6 | return $content; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub get_meta { | ||||||||||||||||||||||||||
|
Subroutine
get_meta: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # If we have no path, we tell the caller that we are a connector | ||||||||||||||||||||||||||||
| 84 | 2 | my @path = $self->_build_path_with_prefix( shift ); | |||||||||||||||||||||||||||
| 85 | 2 | ▶ | if (scalar @path == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | return { TYPE => "connector" }; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | return {TYPE => "scalar" }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # No path = connector root which always exists | ||||||||||||||||||||||||||||
| 98 | 4 | my @path = $self->_build_path_with_prefix( shift ); | |||||||||||||||||||||||||||
| 99 | 4 | ▶ | if (scalar @path == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | return 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | my $filename = $self->_sanitize_path( \@path ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | return -r $filename; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # return the content of the file | ||||||||||||||||||||||||||||
| 110 | 13 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=22 SCAR=34.1
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | 13 | my $file = shift; | |||||||||||||||||||||||||||
| 114 | 13 | my $data = shift; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 13 | my $filename = $self->_sanitize_path( $file, $data ); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 13 | my $content; | |||||||||||||||||||||||||||
| 119 | 13 | ▶ | if ($self->content()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 11 | $self->log()->debug('Process template for content ' . $self->content()); | |||||||||||||||||||||||||||
| 121 | 11 | my $template = Template->new({}); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 11 | ▶ | $data = { DATA => $data } if (ref $data eq ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 11 | ▶ | $template->process( \$self->content(), $data, \$content) || die "Error processing content template."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | if (ref $data ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | die "You need to define a content template if data is not a scalar"; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 2 | $content = $data; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 13 | my $mode = $self->ifexists(); | |||||||||||||||||||||||||||
| 134 | 13 | ▶ | if ($mode eq 'fail' && -f $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode eq "fail" and -f $filename
MC/DC50%1/2
$mode eq "fail" and -f $filename
$mode eq "fail"
-f $filename
Truth table66%2/3
A and B
A= $mode eq "fail"B= -f $filename
| |||||||||||||||||||||||||||||
| 135 | 1 | die "File $filename exists"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 12 | ▶ | if ($mode eq 'silent' && -f $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode eq "silent" and -f $filename
MC/DC50%1/2
$mode eq "silent" and -f $filename
$mode eq "silent"
-f $filename
Truth table66%2/3
A and B
A= $mode eq "silent"B= -f $filename
| |||||||||||||||||||||||||||||
| 139 | 1 | return; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 11 | my $uid = -1; | |||||||||||||||||||||||||||
| 143 | 11 | my $gid; | |||||||||||||||||||||||||||
| 144 | 11 | ▶ | if (my $user = $self->user()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $uid = getpwnam($user) or die "$user not known"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $gid = -1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 11 | ▶ | if (my $group = $self->group()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $gid = getgrnam($group) or die "$group not known"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 11 | ▶ | if ($mode eq 'append' && -f $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode eq "append" and -f $filename
MC/DC50%1/2
$mode eq "append" and -f $filename
$mode eq "append"
-f $filename
Truth table66%2/3
A and B
A= $mode eq "append"B= -f $filename
| |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | open (FILE, ">>",$filename) || die "Unable to open file for appending"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||
| 156 | 10 | ▶ | open (FILE, ">", $filename) || die "Unable to open file for writing"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 9 | print FILE $content; | |||||||||||||||||||||||||||
| 160 | 9 | close FILE; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 9 | ▶ | if (my $filemode = $self->mode()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | if ($filemode =~ m{\A[0-7]{4}\z}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | chmod (oct($filemode), $filename) || die "Unable to change mode to $filemode"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 1 | die "Given mode string '$filemode' is not valid"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 8 | ▶ | if ($gid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | chown ($uid, $gid, $filename) || die "Unable to chown $filename to $uid/$gid"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | #FIXME - some error handling might not hurt | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 8 | return 1; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 22 | ▶ | sub _sanitize_path { | ||||||||||||||||||||||||||
|
Subroutine
_sanitize_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 183 | 22 | my $inargs = shift; | |||||||||||||||||||||||||||
| 184 | 22 | my $data = shift; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 22 | my @args = $self->_build_path_with_prefix( $inargs ); | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 22 | my $file = $self->_render_local_path( \@args, $data ); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 22 | my $filename = $self->{LOCATION}.'/'.$file; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 22 | $self->log()->debug('Filename evaluated to ' . $filename); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 22 | return $filename; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 198 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | 1; | |||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||