Miner/Stat.pm

filter: Statementstmt 90.9% 40 / 44 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 87.1% 61 / 70 CC 10 SCAR 15.8
CC
10
Cov
85%
mine
7.6
new
4.0
BEGIN
1.0
CRAP 4.9
« Store/XML.pm ↑ Summary Miner/Ext.pm »
1 package FileMetadata::Miner::Stat;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 our $VERSION = '1.0';
9
10 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
11
12 4 my $self = {};
13 4 bless $self, shift;
14 4 my $config = shift;
15
16 # Set the size units
17
18 4 $self->{'size_format'} = '';
19 4 ▶ if (defined $config->{'size'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"size"}) 3 1 4
20 3 ▶ die "Invalid 'size' format" unless $config->{'size'} =~ /^(MB|KB|bytes)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"size"} =~ /^(MB|KB|bytes)$/ 1 2 3
21 2 $self->{'size_format'} = $config->{'size'};
22 }
23
24 # Set the format for date
25
26 3 ▶ if (defined $config->{'date'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'date'}) { } 0 3 3
27 0 $self->{'time_format'} = $config->{'date'};
28 } else {
29 3 $self->{'time_format'} = '%T-%D';
30 }
31 3 return $self;
32 }
33
34 4 ▶sub mine {
Subroutine mine: called CC=7 SCAR=20.2
Pod documented
35
36 4 my ($self, $path, $meta) = @_;
37 4 my $result = stat ($path);
38 4 my $name = ref ($self); # For efficiency
39
40 4 ▶ return $result unless $result; # Return on error
Branch: 1 branch
BranchTrueFalseTotal
unless $result 1 3 4
41
42 # Generate size in the right format
43 3 my $size = (stat (_))[7];
44
45 3 ▶ if ($self->{'size_format'} eq 'MB') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'size_format'} eq 'MB') { } 1 2 3
elsif ($self->{'size_format'} eq 'KB') { } 1 1 2
elsif ($self->{'size_format'} eq 'bytes') { } 0 1 1
46 1 $meta->{"$name"."::size"} = $size/ (1024*1024) . " MB";
47 } elsif ($self->{'size_format'} eq 'KB') {
48 1 $meta->{"$name"."::size"} = $size/ (1024) . " KB";
49 } elsif ($self->{'size_format'} eq 'bytes') {
50 0 $meta->{"$name"."::size"} = "$size bytes";
51 } else {
52 1 $meta->{"$name"."::size"} = $size;
53 }
54
55 # Generate times in the right format
56
57 3 my $time_str = strftime ($self->{'time_format'},
58 localtime ((stat (_))[9]));
59 3 $meta->{"$name"."::mtime"} = $time_str;
60 3 $time_str = strftime ($self->{'time_format'},
61 localtime ((stat (_))[10]));
62 3 $meta->{"$name"."::ctime"} = $time_str;
63
64 # Figure out the type of file
65
66 3 ▶ if (S_ISREG ((stat (_))[2])) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISREG((stat _)[2])) { } 3 0 3
elsif (S_ISDIR((stat _)[2])) { } 0 0 0
67 3 $meta->{"$name\:\:type"} = 'REG'
68 } elsif (S_ISDIR ((stat (_))[2])) {
69 0 $meta->{"$name\:\:type"} = 'DIR'
70 } else {
71 0 $meta->{"$name\:\:type"} = 'OTHER'
72 }
73
74 3 return $result;
75 }
76
77 1 1;
78 __END__
« Store/XML.pm ↑ Summary Miner/Ext.pm »