blib/lib/MYDan/Util/ReservedSpace/File.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 99 CC 13 SCAR 27.9
CC
13
Cov
0%
_space
42.0
dump
20.0
load
6.0
CRAP 16.3
1 package MYDan::Util::ReservedSpace::File;
2  
3 =head1 NAME
4  
5 MYDan::ReservedSpace::File
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::ReservedSpace::File;
10  
11 MYDan::ReservedSpace::File::dump( 'path/file', \%hash );
12 my $hash = MYDan::ReservedSpace::File::load( 'path/file' );
13  
14
15 =cut
16  
17 use strict;
18 use warnings;
19 use Carp;
20 use Fcntl qw( :flock );
21 use MYDan;
22 use YAML::XS;
23  
24 sub dump
25 {
26 die "file undef" unless my $file = shift;
27 if( $ENV{UseReservedSpace} )
28 {
29 die "no ReservedSpace" unless _space( $file );
30 }
31 YAML::XS::DumpFile( "$MYDan::PATH/tmp/$file", @_ );
32 }
33  
34 sub load
35 {
36 die "file undef" unless my $file = shift;
37 YAML::XS::LoadFile( "$MYDan::PATH/tmp/$file" );
38 }
39  
40 sub unlink
41 {
42 die "file undef" unless my $file = shift;
43 unlink "$MYDan::PATH/tmp/$file";
44 }
45  
46 sub filename
47 {
48 die "file undef" unless my $file = shift;
49 return "$MYDan::PATH/tmp/$file";
50 }
51  
52 sub _space
53 {
54  
55 my ( $file, $fh ) = shift;
56  
57 open( $fh, ">>", "$MYDan::PATH/var/ReservedSpace/lock" ) or die "Can't open lock: $!";
58  
59 die "lock fail" unless flock $fh, LOCK_EX | LOCK_NB;
60  
61 for ( glob "$MYDan::PATH/var/ReservedSpace/*" )
62 {
63 next unless $_ =~ /\/(\d+)$/;
64 my $id = $1;
65 next unless _rsok( $_ );
66 die "ln fail" if system( "ln '$MYDan::PATH/var/ReservedSpace/$id' '$MYDan::PATH/tmp/$file'" );
67 die "clean fail" if system( "echo > '$MYDan::PATH/tmp/$file'" );
68 flock $fh, LOCK_UN;
69 close $fh;
70 return 1;
71 }
72  
73 flock $fh, LOCK_UN;
74 close $fh;
75  
76 return 0;
77 }
78  
79 sub _rsok
80 {
81 my $file = shift;
82 return ( $file && -f $file && ( stat $file )[3] == 1 ) ? 1 : 0;
83 }
84  
85 1;