blib/lib/File/Properties/Generic.pm

filter: Statementstmt 81.3% 96 / 118 Branchbran 58.9% 33 / 56 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 91.3% 21 / 23 Podpod 100% 11 / 11 totaltotal 75.9% 164 / 216 CC 30 SCAR 17.8
CC
30
Cov
74%
string
20.0
_dirstring
20.0
_init
7.3
CRAP 5.9
« lib/File/Properties/Database.pm ↑ Summary blib/lib/File/Properties/Cache.pm »
1 # ----------------------------------------------------------------------------
2 #
3 # This module provides a class representing properties of a generic
4 # disk file.
5 #
6 # Copyright © 2010,2011 Brendt Wohlberg <wohl@cpan.org>
7 # See distribution LICENSE file for license details.
8 #
9 # Most recent modification: 5 November 2011
10 #
11 # ----------------------------------------------------------------------------
12  
13 package File::Properties::Generic;
14 5 our $VERSION = 0.01;
15  
16 5 ▶use File::Properties::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 require 5.005;
19 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use DirHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # This approach copied from File::Spec
29 5 my $OSTypes = {MacOS => 'Mac', MSWin32 => 'Win32',
30 os2 => 'OS2', VMS => 'VMS',
31 epoc => 'Epoc', NetWare => 'Win32',
32 symbian => 'Win32', dos => 'OS2',
33 cygwin => 'Cygwin'};
34 5 ▶my $OSType = $OSTypes->{$^O} || 'Unix';
Condition50%1/2
$OSTypes->{$^O} || 'Unix'
l!l
05
MC/DC0%0/1
$OSTypes->{$^O} || 'Unix'
$OSTypes->{$^O}
Truth table50%1/2
A
A= $OSTypes->{$^O}
Aresult
11
00
35 # Flag indicating whether stat is fully supported. Currently only set
36 # true for Unix, since other operating systems not available for
37 # testing.
38 5 ▶our $FullStatSupport = ($OSType eq 'Unix')?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$OSType eq 'Unix' ? : 5 0 5
39  
40  
41 # ----------------------------------------------------------------------------
42 # Constructor
43 # ----------------------------------------------------------------------------
44 13 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
45 13 my $this = shift;
46 13 ▶ my $clss = ref($this) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
580
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
47 13 my $self = {};
48 13 bless $self, $clss;
49 13 $self->_init(@_);
50 13 return $self;
51 }
52  
53  
54 # ----------------------------------------------------------------------------
55 # Initialiser
56 # ----------------------------------------------------------------------------
57 13 ▶sub _init {
Subroutine _init: called CC=7 SCAR=19.8
58 13 my $self = shift;
59 13 my $path = shift; # File path
60 13 my $fpcr = shift; # File::Properties::Cache reference
61  
62 # Ensure path specified
63 13 ▶ throw File::Properties::Error("Path is not defined")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 13 13
64 if not defined $path;
65 # Ensure specified path is readable
66 13 ▶ throw File::Properties::Error("Path $path is not readable")
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 13 13
67 if not -r $path;
68 # Convert specified path to canonical, absolute path
69 13 $self->path(Cwd::realpath($path));
70 ## Get file properties via stat call
71 13 my $fstt = stat($path);
72 13 ▶ throw File::Properties::Error("Stat failed on $path") if not defined $fstt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fstt 0 13 13
73 13 ▶ $self->device($fstt->dev) if ($FullStatSupport);
Branch: 1 branch
BranchTrueFalseTotal
if $File::Properties::Generic::FullStatSupport 13 0 13
74 13 ▶ $self->inode($fstt->ino) if ($FullStatSupport);
Branch: 1 branch
BranchTrueFalseTotal
if $File::Properties::Generic::FullStatSupport 13 0 13
75 13 $self->size($fstt->size);
76 13 $self->mtime($fstt->mtime);
77 13 $self->mode($fstt->mode);
78 ## If specified path is a directory, construct child properties
79 ## objects for each directory entry
80 13 ▶ if ($self->isdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isdir) { } 1 12 13
81 1 $self->children($self->_scandir($path, $fpcr));
82 } else {
83 12 $self->children(undef);
84 }
85 }
86  
87  
88 # ----------------------------------------------------------------------------
89 # Get file path
90 # ----------------------------------------------------------------------------
91 29 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
92 29 my $self = shift;
93  
94 29 ▶ $self->{'path'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 16 29
95 29 return $self->{'path'};
96 }
97  
98  
99 # ----------------------------------------------------------------------------
100 # Get file device number
101 # ----------------------------------------------------------------------------
102 22 ▶sub device {
Subroutine device: called CC=3 SCAR=11.3
Pod documented
103 22 my $self = shift;
104  
105 22 ▶ if ($FullStatSupport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Properties::Generic::FullStatSupport) { } 22 0 22
106 22 ▶ $self->{'devc'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 9 22
107 22 return $self->{'devc'};
108 } else {
109 0 throw File::Properties::Error("Stat not fully supported");
110 }
111 }
112  
113  
114 # ----------------------------------------------------------------------------
115 # Get file inode number
116 # ----------------------------------------------------------------------------
117 22 ▶sub inode {
Subroutine inode: called CC=3 SCAR=11.3
Pod documented
118 22 my $self = shift;
119  
120 22 ▶ if ($FullStatSupport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Properties::Generic::FullStatSupport) { } 22 0 22
121 22 ▶ $self->{'inod'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 9 22
122 22 return $self->{'inod'};
123 } else {
124 0 throw File::Properties::Error("Stat not fully supported");
125 }
126 }
127  
128  
129 # ----------------------------------------------------------------------------
130 # Get file size
131 # ----------------------------------------------------------------------------
132 28 ▶sub size {
Subroutine size: called CC=2 SCAR=6.9
Pod documented
133 28 my $self = shift;
134  
135 28 ▶ $self->{'size'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 15 28
136 28 return $self->{'size'};
137 }
138  
139  
140 # ----------------------------------------------------------------------------
141 # Get file modification time
142 # ----------------------------------------------------------------------------
143 22 ▶sub mtime {
Subroutine mtime: called CC=2 SCAR=6.9
Pod documented
144 22 my $self = shift;
145  
146 22 ▶ $self->{'mtim'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 9 22
147 22 return $self->{'mtim'};
148 }
149  
150  
151 # ----------------------------------------------------------------------------
152 # Get file mode
153 # ----------------------------------------------------------------------------
154 34 ▶sub mode {
Subroutine mode: called CC=2 SCAR=6.9
Pod documented
155 34 my $self = shift;
156  
157 34 ▶ $self->{'mode'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 21 34
158 34 return $self->{'mode'};
159 }
160  
161  
162 # ----------------------------------------------------------------------------
163 # Get directory content
164 # ----------------------------------------------------------------------------
165 14 ▶sub children {
Subroutine children: called CC=2 SCAR=6.9
Pod documented
166 14 my $self = shift;
167  
168 14 ▶ $self->{'chld'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 1 14
169 14 return $self->{'chld'};
170 }
171  
172  
173  
174 # ----------------------------------------------------------------------------
175 # Test whether file is a regular (plain) file
176 # ----------------------------------------------------------------------------
177 8 ▶sub isreg {
Subroutine isreg: called CC=2 SCAR=7.2
Pod documented
178 8 my $self = shift;
179  
180 8 ▶ return $FullStatSupport?S_ISREG($self->mode):(-f $self->path);
Branch: 1 branch
BranchTrueFalseTotal
$File::Properties::Generic::FullStatSupport ? : 8 0 8
181 }
182  
183  
184 # ----------------------------------------------------------------------------
185 # Test whether file is a directory
186 # ----------------------------------------------------------------------------
187 13 ▶sub isdir {
Subroutine isdir: called CC=2 SCAR=7.2
Pod documented
188 13 my $self = shift;
189  
190 13 ▶ return $FullStatSupport?S_ISDIR($self->mode):(-d $self->path);
Branch: 1 branch
BranchTrueFalseTotal
$File::Properties::Generic::FullStatSupport ? : 13 0 13
191 }
192  
193  
194 # ----------------------------------------------------------------------------
195 # Construct string description of object
196 # ----------------------------------------------------------------------------
197 0 ▶sub string {
Subroutine string: not called CC=4 SCAR=30.0
Pod documented
198 0 my $self = shift;
199 0 my $levl = shift;
200  
201 0 ▶ $levl = 0 if (!defined $levl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levl 0 0 0
202 0 my $lpfx = ' ' x (2*$levl);
203 0 my $s;
204 0 $s = $lpfx . "Path: ".$self->path."\n";
205 0 ▶ $s .= $lpfx . "Device: ".$self->device." Inode: ".$self->inode." "
Branch: 1 branch
BranchTrueFalseTotal
if $File::Properties::Generic::FullStatSupport 0 0 0
206 if $FullStatSupport;
207 0 $s .= "Size: ".$self->size." MTime: ".$self->mtime."\n";
208 0 ▶ $s .= $self->_dirstring($levl) if ($self->isdir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->isdir 0 0 0
209  
210 0 return $s;
211 }
212  
213  
214 # ----------------------------------------------------------------------------
215 # Scan a directory, constructing a hash mapping file basenames to
216 # File::Properties::Generic objects
217 # ----------------------------------------------------------------------------
218 1 ▶sub _scandir {
Subroutine _scandir: called CC=6 SCAR=18.0
219 1 my $self = shift;
220 1 my $path = shift; # Directory path
221 1 my $fpcr = shift; # File::Properties::Cache reference
222  
223 1 ▶ throw File::Properties::Error("Path $path is not readable")
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 1 1
224 if (not -r $path);
225 1 ▶ throw File::Properties::Error("Path $path is not a directory")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 1 1
226 if (not -d $path);
227 1 my $dh = new DirHandle $path;
228 1 ▶ throw File::Properties::Error("Error constructing DirHandle for $path")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dh 0 1 1
229 if (!defined $dh);
230 1 my $dhsh = {};
231 1 my ($dp, $fp);
232 ## Create File::Properties::Generic object for each directory entry
233 1 ▶ while (defined($dp = $dh->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($dp = $dh->read)) 7 1 8
234 # Skip . and .. directory entries
235 7 ▶ next if ($dp =~ /^\.{1,2}$/);
Branch: 1 branch
BranchTrueFalseTotal
if $dp =~ /^\.{1,2}$/ 2 5 7
236 # Total path of current directory entry
237 5 $fp = File::Spec->catdir($path, $dp);
238 # Add hash entry for current directory entry
239 5 $dhsh->{$dp} = $self->new($fp, $fpcr);
240 }
241 1 return $dhsh;
242 }
243  
244  
245 # ----------------------------------------------------------------------------
246 # Construct a string description of an object representing a directory file
247 # ----------------------------------------------------------------------------
248 0 ▶sub _dirstring {
Subroutine _dirstring: not called CC=4 SCAR=30.0
249 0 my $self = shift;
250 0 my $levl = shift;
251  
252 0 ▶ $levl = 0 if (!defined $levl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levl 0 0 0
253 0 my $s = '';
254 0 my $chsh = $self->children;
255 0 ▶ if (defined $chsh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chsh) 0 0 0
256 0 my $chld;
257 0 foreach $chld ( sort keys %$chsh ) {
258 0 $s .= $chsh->{$chld}->string($levl + 1);
259 }
260 }
261 0 return $s;
262 }
263  
264  
265 # ----------------------------------------------------------------------------
266 # End of method definitions
267 # ----------------------------------------------------------------------------
268  
269  
270 5 1;
271 __END__
« lib/File/Properties/Database.pm ↑ Summary blib/lib/File/Properties/Cache.pm »