| 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'
MC/DC0%0/1
$OSTypes->{$^O} || 'Unix'
$OSTypes->{$^O}
Truth table50%1/2
A
A= $OSTypes->{$^O}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table66%2/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 73 | 13 | ▶ | $self->device($fstt->dev) if ($FullStatSupport); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 13 | ▶ | $self->inode($fstt->ino) if ($FullStatSupport); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 106 | 22 | ▶ | $self->{'devc'} = shift if (@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 121 | 22 | ▶ | $self->{'inod'} = shift if (@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 224 | if (not -r $path); | ||||||||||||||||||||
| 225 | 1 | ▶ | throw File::Properties::Error("Path $path is not a directory") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 234 | # Skip . and .. directory entries | ||||||||||||||||||||
| 235 | 7 | ▶ | next if ($dp =~ /^\.{1,2}$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 253 | 0 | my $s = ''; | |||||||||||||||||||
| 254 | 0 | my $chsh = $self->children; | |||||||||||||||||||
| 255 | 0 | ▶ | if (defined $chsh) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||