lib/Template/Plugin/File.pm

filter: Statementstmt 100% 59 / 59 Branchbran 85.7% 24 / 28 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 10 / 10 Podpod 50.0% 2 / 4 totaltotal 86.7% 105 / 121 CC 19 SCAR 23.2
CC
19
Cov
91%
new
16.4
rel
4.1
BEGIN
1.0
CRAP 10.2
« lib/Template/Plugin/HTML.pm ↑ Summary lib/Template/Document.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::File
4 #
5 # DESCRIPTION
6 # Plugin for encapsulating information about a system file.
7 #
8 # AUTHOR
9 # Originally written by Michael Stevens <michael@etla.org> as the
10 # Directory plugin, then mutilated by Andy Wardley <abw@kfs.org>
11 # into separate File and Directory plugins, with some additional
12 # code for working with views, etc.
13 #
14 # COPYRIGHT
15 # Copyright 2000-2022 Michael Stevens, Andy Wardley.
16 #
17 # This module is free software; you can redistribute it and/or
18 # modify it under the same terms as Perl itself.
19 #
20 #============================================================================
21  
22 package Template::Plugin::File;
23  
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 our $VERSION = '3.106';
32  
33 2 our @STAT_KEYS = qw(
34 dev ino mode nlink uid gid rdev size
35 atime mtime ctime blksize blocks
36 );
37  
38  
39 #------------------------------------------------------------------------
40 # new($context, $file, \%config)
41 #
42 # Create a new File object. Takes the pathname of the file as
43 # the argument following the context and an optional
44 # hash reference of configuration parameters.
45 #------------------------------------------------------------------------
46  
47 104 ▶sub new {
Subroutine new: called CC=16 SCAR=27.9
Pod documented
48 104 ▶ my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 100 4 104
49 104 my ($class, $context, $path) = @_;
50 104 my ($root, $home, @stat, $abs);
51  
52 104 ▶ return $class->throw('no file specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 103 104
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
01103
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
53 unless defined $path and length $path;
54  
55 # path, dir, name, root, home
56  
57 103 ▶ if (File::Spec->file_name_is_absolute($path)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($path)) { } 2 101 103
elsif ($root = $config->{'root'}) { } 30 71 101
58 2 $root = '';
59 }
60 elsif (($root = $config->{ root })) {
61 # strip any trailing '/' from root
62 30 $root =~ s[/$][];
63 }
64 else {
65 71 $root = '';
66 }
67  
68 103 my ($name, $dir, $ext) = fileparse($path, '\.\w+');
69 # fixup various items
70 103 $dir =~ s[/$][];
71 103 ▶ $dir = '' if $dir eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "." 7 96 103
72 103 $name = $name . $ext;
73 103 $ext =~ s/^\.//g;
74  
75 103 my @fields = File::Spec->splitdir($dir);
76 103 ▶ shift @fields if @fields && ! length $fields[0];
Branch: 1 branch
BranchTrueFalseTotal
if @fields and not length $fields[0] 2 101 103
Condition100%3/3
@fields and not length $fields[0]
!ll&&!rl&&r
7942
MC/DC100%2/2
@fields and not length $fields[0]
@fields not length $fields[0]
Truth table100%3/3
A and not(B)
A= @fieldsB= not length $fields[0]
ABresult
0X0
100
111
77 103 $home = join('/', ('..') x @fields);
78 103 ▶ $abs = File::Spec->catfile($root ? $root : (), $path);
Branch: 1 branch
BranchTrueFalseTotal
$root ? : 30 73 103
79  
80 my $self = {
81 path => $path,
82 name => $name,
83 root => $root,
84 home => $home,
85 dir => $dir,
86 ext => $ext,
87 abs => $abs,
88 user => '',
89 group => '',
90 isdir => '',
91 stat => defined $config->{ stat }
92 ? $config->{ stat }
93 : ! $config->{ nostat },
94 103 ▶ map { ($_ => '') } @STAT_KEYS,
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'stat'} ? : 0 103 103
95 };
96  
97 103 ▶ if ($self->{ stat }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stat"}) 94 9 103
98 94 ▶ (@stat = stat( $abs ))
Branch: 1 branch
BranchTrueFalseTotal
@stat = stat $abs or return $class->throw("${abs}: $!") 93 1 94
99 || return $class->throw("$abs: $!");
100  
101 93 @$self{ @STAT_KEYS } = @stat;
102  
103 93 ▶ unless ($config->{ noid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"noid"}) 93 0 93
104 93 ▶ $self->{ user } = eval { getpwuid( $self->{ uid }) || $self->{ uid } };
Condition33%1/3
getpwuid $self->{'uid'} || $self->{'uid'}
l!l&&r!l&&!r
9300
MC/DC0%0/2
getpwuid $self->{'uid'} || $self->{'uid'}
getpwuid $self->{'uid'} $self->{'uid'}
Truth table33%1/3
A || B
A= getpwuid $self->{'uid'}B= $self->{'uid'}
ABresult
1X1
011
000
105 93 ▶ $self->{ group } = eval { getgrgid( $self->{ gid }) || $self->{ gid } };
Condition33%1/3
getgrgid $self->{'gid'} || $self->{'gid'}
l!l&&r!l&&!r
9300
MC/DC0%0/2
getgrgid $self->{'gid'} || $self->{'gid'}
getgrgid $self->{'gid'} $self->{'gid'}
Truth table33%1/3
A || B
A= getgrgid $self->{'gid'}B= $self->{'gid'}
ABresult
1X1
011
000
106 }
107 93 $self->{ isdir } = -d $abs;
108 }
109  
110 102 bless $self, $class;
111 }
112  
113  
114 #-------------------------------------------------------------------------
115 # rel($file)
116 #
117 # Generate a relative filename for some other file relative to this one.
118 #------------------------------------------------------------------------
119  
120 2 ▶sub rel {
Subroutine rel: called CC=4 SCAR=14.1
Pod documented
121 2 my ($self, $path) = @_;
122 2 ▶ $path = $path->{ path } if ref $path eq ref $self; # assumes same root
Branch: 1 branch
BranchTrueFalseTotal
if ref $path eq ref $self 1 1 2
123 2 ▶ return $path if $path =~ m[^/];
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 0 2 2
124 2 ▶ return $path unless $self->{ home };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"home"} 0 2 2
125 2 return $self->{ home } . '/' . $path;
126 }
127  
128  
129 #------------------------------------------------------------------------
130 # present($view)
131 #
132 # Present self to a Template::View.
133 #------------------------------------------------------------------------
134  
135 7 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
Pod undocumented
136 7 my ($self, $view) = @_;
137 7 $view->view_file($self);
138 }
139  
140  
141 1 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
Pod undocumented
142 1 my ($self, $error) = @_;
143 1 die (Template::Exception->new('File', $error));
144 }
145  
146 2 1;
147  
148 __END__
« lib/Template/Plugin/HTML.pm ↑ Summary lib/Template/Document.pm »