lib/File/UStore.pm

filter: Statementstmt 30.3% 31 / 102 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 100% 5 / 5 totaltotal 28.5% 44 / 154 SCAR 53.0
CC
20
Cov
23%
new
72.0
add
30.0
remove
30.0
CRAP 200.4
↑ Summary blib/lib/File/UStore.pm »
1 package File::UStore;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # PODNAME: File::UStore
7  
8 # ABSTRACT: UUID based File Storage Module.
9  
10 3 our $VERSION = '0.15'; # VERSION
11  
12 # Dependencies
13  
14 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 require Exporter;
21 3 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 our @ISA = qw(Exporter);
24  
25 3 our %EXPORT_TAGS = (
26 'all' => [
27 qw(
28  
29 )
30 ]
31 );
32  
33 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
34  
35 3 our @EXPORT = qw( );
36  
37  
38 sub new {
39  
40 0 ▶ my ( $this, %params ) = @_;
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
41  
42 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
43 0 my $self = {};
44 0 bless $self, $class;
45  
46 0 ▶ if ( exists( $params{path} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'path'}) { } 0 0 0
47 0 $self->{path} = $params{path};
48 }
49 else {
50 0 $self->{path} = "~/.ustore";
51 }
52  
53 0 ▶ if ( exists( $params{prefix} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'prefix'}) { } 0 0 0
54 0 $self->{prefix} = $params{prefix};
55 }
56 else {
57 0 $self->{prefix} = "";
58 }
59  
60 0 ▶ if ( exists( $params{depth} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'depth'}) { } 0 0 0
61 0 $self->{depth} = $params{depth};
62 0 ▶ $self->{depth} = 35 if ( $params{depth} > 35 );
Branch: 1 branch
BranchTrueFalseTotal
if $params{"depth"} > 35 0 0 0
63  
64 }
65 else {
66 0 $self->{depth} = 3;
67 }
68  
69 0 ▶ if ( !( -e $self->{path} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"path"}) 0 0 0
70 mkdir( $self->{path} )
71 0 ▶ or die "Unable to create directory : $self->{path}";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->{"path"} or die qq[Unable to create directory : $self->{"path"}] 0 0 0
72 }
73  
74 0 return $self;
75 }
76  
77  
78 sub add {
79  
80 0 ▶ my ( $self, $file ) = @_;
Subroutine add: not called CC=5 SCAR=34.0
Pod documented
81  
82 0 my ( $uuid, $uuidString );
83 0 UUID::generate($uuid);
84 0 UUID::unparse( $uuid, $uuidString );
85  
86 0 my $SSubDir;
87 0 my @tempstr = split( //, $uuidString );
88 0 my @dirtree;
89 0 for ( 1 .. $self->{depth} ) {
90 0 push @dirtree, shift @tempstr;
91 }
92 0 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
93  
94 0 ▶ if ( !( -e $SSubDir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $SSubDir) 0 0 0
95  
96 0 ▶ mkpath($SSubDir)
Branch: 1 branch
BranchTrueFalseTotal
mkpath($SSubDir) or die "Unable to create subdirectories $SSubDir in the store" 0 0 0
97 or die "Unable to create subdirectories $SSubDir in the store";
98 }
99  
100 my $destStoredFile =
101 0 File::Spec->catfile( $SSubDir, $self->{prefix} . $uuidString );
102  
103 0 ▶ copy( $file, $destStoredFile )
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $destStoredFile) or die "Unable to copy file into ustore as $destStoredFile" 0 0 0
104 or die "Unable to copy file into ustore as $destStoredFile";
105  
106 0 return $uuidString;
107 }
108  
109  
110 sub remove {
111  
112 0 ▶ my ( $self, $id ) = @_;
Subroutine remove: not called CC=5 SCAR=34.0
Pod documented
113  
114 0 my $destStoredFile;
115  
116 0 ▶ if ( !( defined($id) ) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
117  
118 0 my $SSubDir;
119 0 my @tempstr = split( //, $id );
120 0 my @dirtree;
121 0 for ( 1 .. $self->{depth} ) {
122 0 push @dirtree, shift @tempstr;
123 }
124 0 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
125  
126 0 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
127  
128 0 ▶ if ( -e $destStoredFile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destStoredFile) { } 0 0 0
129  
130 0 ▶ unlink($destStoredFile) or return;
Branch: 1 branch
BranchTrueFalseTotal
unlink $destStoredFile or return 0 0 0
131  
132 }
133 else {
134 0 return;
135 }
136  
137 }
138  
139  
140 sub get {
141  
142 0 ▶ my ( $self, $id ) = @_;
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
143  
144 0 my $destStoredFile;
145  
146 my $SSubDir;
147 0 my @tempstr = split( //, $id );
148 0 my @dirtree;
149 0 for ( 1 .. $self->{depth} ) {
150 0 push @dirtree, shift @tempstr;
151 }
152 0 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
153  
154 0 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
155  
156 0 ▶ if ( -e $destStoredFile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destStoredFile) { } 0 0 0
157 0 open( my $fh, '<', $destStoredFile );
158 0 return *$fh;
159 }
160 else {
161 0 return;
162 }
163 }
164  
165  
166 sub getPath {
167  
168 0 ▶ my ( $self, $id ) = @_;
Subroutine getPath: not called CC=3 SCAR=24.8
Pod documented
169  
170 0 my $destStoredFile;
171  
172 my $SSubDir;
173 0 my @tempstr = split( //, $id );
174 0 my @dirtree;
175 0 for ( 1 .. $self->{depth} ) {
176 0 push @dirtree, shift @tempstr;
177 }
178 0 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
179  
180 0 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
181  
182 0 ▶ if ( -e $destStoredFile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destStoredFile) { } 0 0 0
183 0 return $destStoredFile;
184 }
185 else {
186 0 return;
187 }
188 }
189  
190 # local Function
191 sub _printPath {
192 0 ▶ my ($self) = @_;
Subroutine _printPath: not called CC=1 SCAR=6.9
193  
194 0 return $self->{path};
195  
196 }
197  
198 3 1;
199  
200 __END__
↑ Summary blib/lib/File/UStore.pm »