| 1 | package File::HStore; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Digest::SHA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use File::Copy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use File::Path; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | require Exporter; | |||||||||||||||||||
| 10 | 2 | ▶ | use AutoLoader qw(AUTOLOAD); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | our %EXPORT_TAGS = ( | |||||||||||||||||||
| 15 | 'all' => [ | ||||||||||||||||||||
| 16 | qw( | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | ) | ||||||||||||||||||||
| 19 | ] | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | our @EXPORT = qw( ); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | our $VERSION = '0.10'; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | my ( $this, $path, $digest, $prefix ) = @_; | |||||||||||||||||||
| 31 | 0 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition0%0/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 32 | 0 | my $self = {}; | |||||||||||||||||||
| 33 | 0 | bless $self, $class; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | if ( defined($path) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | $self->{path} = $path; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||
| 39 | 0 | $self->{path} = "~/.hstore"; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | if ( defined($digest) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | $self->{digest} = $digest; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||
| 46 | 0 | $self->{digest} = "SHA1"; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | if ( defined($prefix) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 0 | $self->{prefix} = $prefix; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||
| 53 | 0 | $self->{prefix} = "freearchive"; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | if ( !( -e $self->{path} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | mkdir( $self->{path} ) | ||||||||||||||||||||
| 58 | 0 | ▶ | or die "Unable to create directory : $self->{path}"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | return $self; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | my ( $self, $filename ) = @_; | |||||||||||||||||||
| 67 | 0 | my $ldigest; | |||||||||||||||||||
| 68 | my $lSubmitDate; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | if ( $self->{digest} eq "FAT" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | $ldigest = "SHA2"; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||
| 74 | 0 | $ldigest = $self->{digest}; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 0 | ▶ | my $localDigest = _DigestAFile( "$filename", $ldigest ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | or die "Unable to digest the file $filename"; | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | my $SSubDir; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | my $localSubDir = substr( $localDigest, 0, 2 ); | |||||||||||||||||||
| 83 | 0 | $SSubDir = $self->{path} . "/" . $localSubDir; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | $lSubmitDate = _SubmitDate(); | |||||||||||||||||||
| 89 | 0 | $lSubmitDate =~ s/-/\//g; | |||||||||||||||||||
| 90 | 0 | $SSubDir = $self->{path} . "/" . $self->{prefix} . "/" . $lSubmitDate; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | if ( !( -e $SSubDir ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | mkpath($SSubDir) or die "Unable to create subdirectoris $SSubDir in the hstore"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | my $destStoredFile = $SSubDir . "/" . $localDigest; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | ▶ | copy( $filename, $destStoredFile ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | or die "Unable to copy file into hstore as $destStoredFile"; | ||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||
| 105 | 0 | mkpath($destStoredFile); | |||||||||||||||||||
| 106 | 0 | copy( $filename, $destStoredFile); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | return $localDigest; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||
| 113 | 0 | $lSubmitDate =~ s/\//-/g; | |||||||||||||||||||
| 114 | 0 | return $self->{prefix} . "-" . $lSubmitDate . "-" . $localDigest; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | my ( $self, $id ) = @_; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | my $destStoredFile; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # if (!(defined($id))) {die "hash to be removed not defined";} | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | if ( !( defined($id) ) ) { return undef; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | my $localSubDir = substr( $id, 0, 2 ); | |||||||||||||||||||
| 130 | 0 | my $SSubDir = $self->{path} . "/" . $localSubDir; | |||||||||||||||||||
| 131 | 0 | $destStoredFile = $SSubDir . "/" . $id; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||
| 134 | 0 | $id =~ s/-/\//g; | |||||||||||||||||||
| 135 | 0 | $destStoredFile = $self->{path} . "/" . $id; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | if ( -e $destStoredFile ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | ▶ | unlink($destStoredFile) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||
| 144 | 0 | ▶ | rmtree($destStoredFile) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | #die "Unable to delete file from hstore named $destStoredFile"; | ||||||||||||||||||||
| 148 | #return undef; | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||
| 151 | 0 | return; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | ▶ | sub getpath { | ||||||||||||||||||
|
Subroutine
getpath: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | my ( $self, $id ) = @_; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | my $destStoredFile; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | if ( !( $self->{digest} eq "FAT" ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 0 | my $localSubDir = substr( $id, 0, 2 ); | |||||||||||||||||||
| 164 | 0 | my $SSubDir = $self->{path} . "/" . $localSubDir; | |||||||||||||||||||
| 165 | 0 | $destStoredFile = $SSubDir . "/" . $id; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||
| 168 | 0 | $id =~ s/-/\//g; | |||||||||||||||||||
| 169 | 0 | $destStoredFile = $self->{path} . "/" . $id; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 0 | ▶ | if ( -e $destStoredFile ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 0 | return $destStoredFile; | |||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 0 | ▶ | sub _printPath { | ||||||||||||||||||
|
Subroutine
_printPath: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 180 | 0 | my ($self) = @_; | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 0 | return $self->{path}; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | ▶ | sub _DigestAFile { | ||||||||||||||||||
|
Subroutine
_DigestAFile: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | my $file = shift; | |||||||||||||||||||
| 189 | 0 | my $digestdef = shift; | |||||||||||||||||||
| 190 | 0 | my $sha; | |||||||||||||||||||
| 191 | 0 | ▶ | open( FILED, "$file" ) or die "Unable to open file $file"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | 0 | ▶ | if ( $digestdef eq "SHA1" ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 193 | 0 | $sha = Digest::SHA->new("sha1"); | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | elsif ( $digestdef eq "SHA2" ) { | ||||||||||||||||||||
| 196 | 0 | $sha = Digest::SHA->new("sha256"); | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||
| 199 | 0 | print "unknown digest method"; | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | 0 | $sha->addfile(*FILED); | |||||||||||||||||||
| 202 | 0 | close(FILED); | |||||||||||||||||||
| 203 | 0 | return my $digest = $sha->hexdigest; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | # Used only for the Free Archive Toolkit mixed-"hash" format | ||||||||||||||||||||
| 208 | # | ||||||||||||||||||||
| 209 | # FAT is following this format : | ||||||||||||||||||||
| 210 | # | ||||||||||||||||||||
| 211 | # prefix-year-mm-dd-hh-mm-ss-hash | ||||||||||||||||||||
| 212 | # | ||||||||||||||||||||
| 213 | # The format is represented on disk with the following format : | ||||||||||||||||||||
| 214 | # | ||||||||||||||||||||
| 215 | # prefix/year/mm/dd/hh/mm/ss/hash | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | # return the date in FAT format | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 0 | ▶ | sub _SubmitDate { | ||||||||||||||||||
|
Subroutine
_SubmitDate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | my ( $sec, $min, $hour, $day, $month, $year ) = | |||||||||||||||||||
| 222 | (localtime)[ 0, 1, 2, 3, 4, 5 ]; | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 0 | return sprintf( | |||||||||||||||||||
| 225 | "%04d-%02d-%02d-%02d-%02d-%02d", | ||||||||||||||||||||
| 226 | $year + 1900, | ||||||||||||||||||||
| 227 | $month + 1, $day, $hour, $min, $sec | ||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 2 | 1; | |||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||