line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MetaStore::StoreDir; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
MetaStore::StoreDir - Simple store/restore data to files in dirs. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use MetaStore::StoreDir; |
10
|
|
|
|
|
|
|
my $fz = IO::Zlib->new($tmp_file, "rb"); |
11
|
|
|
|
|
|
|
my $dir = tempdir( CLEANUP => 0 ); |
12
|
|
|
|
|
|
|
my $temp_store = new MetaStore::StoreDir:: $dir; |
13
|
|
|
|
|
|
|
$temp_store->putRaw("file.dat",$fz); |
14
|
|
|
|
|
|
|
$fz->close; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Simple store/restore data to files in dirs. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 METHODS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
1
|
|
24298
|
use IO::File; |
|
1
|
|
|
|
|
7136
|
|
|
1
|
|
|
|
|
101
|
|
25
|
1
|
|
|
1
|
|
6
|
use File::Path; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
26
|
1
|
|
|
1
|
|
4
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
27
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
28
|
1
|
|
|
1
|
|
533
|
use Encode; |
|
1
|
|
|
|
|
7407
|
|
|
1
|
|
|
|
|
64
|
|
29
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
51
|
|
30
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
31
|
|
|
|
|
|
|
our $VERSION = '0.1'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $attrs = { _dir => undef }; |
34
|
|
|
|
|
|
|
### install get/set accessors for this object. |
35
|
|
|
|
|
|
|
for my $key ( keys %$attrs ) { |
36
|
1
|
|
|
1
|
|
3
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
847
|
|
37
|
|
|
|
|
|
|
*{ __PACKAGE__ . "::$key" } = sub { |
38
|
16
|
|
|
16
|
|
15
|
my $self = shift; |
39
|
16
|
100
|
|
|
|
33
|
$self->{$key} = $_[0] if @_; |
40
|
16
|
|
|
|
|
39
|
return $self->{$key}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
45
|
2
|
|
|
2
|
0
|
1154
|
my $class = shift; |
46
|
2
|
|
|
|
|
3
|
my $obj; |
47
|
2
|
50
|
|
|
|
5
|
if ( ref $class ) { |
48
|
0
|
|
|
|
|
0
|
$obj = $class; |
49
|
0
|
|
|
|
|
0
|
$class = ref $obj; |
50
|
|
|
|
|
|
|
} |
51
|
2
|
|
|
|
|
4
|
my $self = bless( {}, $class ); |
52
|
2
|
50
|
|
|
|
6
|
if (@_) { |
53
|
2
|
|
|
|
|
2
|
my $dir = shift; |
54
|
2
|
50
|
|
|
|
4
|
if ($obj) { |
55
|
0
|
|
|
|
|
0
|
$dir =~ s%^/%%; |
56
|
0
|
|
|
|
|
0
|
$dir = $obj->_dir . $dir; |
57
|
|
|
|
|
|
|
} |
58
|
2
|
50
|
|
|
|
10
|
$dir .= "/" unless $dir =~ m%/$%; |
59
|
2
|
|
|
|
|
4
|
$self->_dir($dir); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
0
|
|
|
|
|
0
|
carp "need path to dir"; |
63
|
0
|
|
|
|
|
0
|
return; |
64
|
|
|
|
|
|
|
} |
65
|
2
|
|
|
|
|
11
|
return $self; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _store_data { |
69
|
3
|
|
|
3
|
|
6
|
my ( $self, $mode, $name, $val ) = @_; |
70
|
3
|
50
|
|
|
|
5
|
return unless defined $val; |
71
|
3
|
|
|
|
|
7
|
my $file_name = $self->_get_path . $name; |
72
|
3
|
50
|
|
|
|
20
|
my $out = new IO::File:: "> $file_name" or die $!; |
73
|
3
|
|
|
|
|
276
|
local $/; |
74
|
3
|
|
|
|
|
19
|
$/ = undef; |
75
|
3
|
|
|
|
|
3
|
my ($atime, $mtime); |
76
|
3
|
100
|
|
|
|
7
|
if ( ref $val ) { |
77
|
1
|
50
|
33
|
|
|
9
|
if ( UNIVERSAL::isa( $val, 'IO::Handle' ) |
|
|
|
33
|
|
|
|
|
78
|
|
|
|
|
|
|
or ( ref $val eq 'GLOB' ) |
79
|
|
|
|
|
|
|
or UNIVERSAL::isa( $val, 'Tie::Handle' ) ) |
80
|
|
|
|
|
|
|
{ |
81
|
1
|
|
|
|
|
15
|
$out->print(<$val>); |
82
|
|
|
|
|
|
|
#set atime and mtime |
83
|
1
|
|
|
|
|
15
|
($atime, $mtime) = (stat $val )[8,9]; |
84
|
1
|
|
|
|
|
4
|
$val->close; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
0
|
0
|
|
|
|
0
|
$out->print( |
88
|
|
|
|
|
|
|
( $mode =~ /utf8/ ) ? $self->_utfx2utf($$val) : $$val ); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
else { |
92
|
2
|
50
|
|
|
|
11
|
$out->print( ( $mode =~ /utf8/ ) ? $self->_utfx2utf($val) : $val ); |
93
|
|
|
|
|
|
|
} |
94
|
3
|
50
|
|
|
|
30
|
$out->close or die $!; |
95
|
3
|
100
|
66
|
|
|
115
|
if ( $atime && $mtime) { |
96
|
1
|
|
|
|
|
30
|
utime $atime, $mtime, $file_name; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _utfx2utf { |
102
|
2
|
|
|
2
|
|
3
|
my ( $self, $str ) = @_; |
103
|
2
|
50
|
|
|
|
5
|
$str = encode( 'utf8', $str ) if utf8::is_utf8($str); |
104
|
2
|
|
|
|
|
11
|
return $str; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _utf2utfx { |
108
|
1
|
|
|
1
|
|
2
|
my ( $self, $str ) = @_; |
109
|
1
|
50
|
|
|
|
7
|
$str = decode( 'utf8', $str ) unless utf8::is_utf8($str); |
110
|
1
|
|
|
|
|
61
|
return $str; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _get_path { |
114
|
3
|
|
|
3
|
|
2
|
my $self = shift; |
115
|
3
|
|
|
|
|
3
|
my $key = shift; |
116
|
3
|
|
|
|
|
6
|
my $dir = $self->_dir; |
117
|
3
|
100
|
|
|
|
209
|
mkpath( $dir, 0 ) unless -e $dir; |
118
|
3
|
|
|
|
|
8
|
return $dir; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub putText { |
122
|
3
|
|
|
3
|
0
|
4
|
my $self = shift; |
123
|
3
|
|
|
|
|
8
|
return $self->_store_data( ">:utf8", @_ ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub putRaw { |
127
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
128
|
0
|
|
|
|
|
0
|
return $self->_store_data( ">", @_ ); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub getRaw_fh { |
132
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
133
|
2
|
|
|
|
|
3
|
my $key = shift; |
134
|
2
|
50
|
|
|
|
3
|
my $fh = new IO::File:: "< " . $self->_dir . $key or return; |
135
|
2
|
|
|
|
|
113
|
return $fh; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub getRaw { |
139
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
140
|
1
|
50
|
|
|
|
3
|
if ( my $fd = $self->getRaw_fh(@_) ) { |
141
|
1
|
|
|
|
|
1
|
my $data; |
142
|
|
|
|
|
|
|
{ |
143
|
1
|
|
|
|
|
1
|
local $/; |
|
1
|
|
|
|
|
3
|
|
144
|
1
|
|
|
|
|
2
|
undef $/; |
145
|
1
|
|
|
|
|
12
|
$data = <$fd>; |
146
|
|
|
|
|
|
|
} |
147
|
1
|
|
|
|
|
3
|
$fd->close; |
148
|
1
|
|
|
|
|
11
|
return $data; |
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
0
|
else { return } |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub getText { |
154
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
155
|
1
|
|
|
|
|
3
|
return $self->_utf2utfx( $self->getRaw(@_) ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub getText_fh { |
159
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
160
|
1
|
|
|
|
|
3
|
return $self->getRaw_fh(@_); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub get_path_to_key { |
164
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
165
|
0
|
|
|
|
|
0
|
my $key = shift; |
166
|
0
|
|
|
|
|
0
|
my $dir = $self->_dir; |
167
|
0
|
|
|
|
|
0
|
return $dir . $key; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub get_keys { |
171
|
5
|
|
|
5
|
0
|
8
|
my $self = shift; |
172
|
5
|
|
|
|
|
9
|
my $dir = $self->_dir; |
173
|
5
|
50
|
|
|
|
64
|
return [] unless -e $dir; |
174
|
5
|
50
|
|
|
|
89
|
opendir DIR, $dir or die $!; |
175
|
5
|
|
|
|
|
7
|
my @keys = (); |
176
|
5
|
|
|
|
|
75
|
while ( my $key = readdir DIR ) { |
177
|
15
|
100
|
100
|
|
|
128
|
next if $key =~ /^\.\.?$/ or -d "$dir/$key"; |
178
|
3
|
|
|
|
|
11
|
push @keys, $key; |
179
|
|
|
|
|
|
|
} |
180
|
5
|
|
|
|
|
24
|
return \@keys; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head3 delete_keys [,[,]] |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Delete files from dir |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
sub delete_keys { |
189
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
190
|
1
|
|
|
|
|
2
|
my $dir = $self->_dir; |
191
|
1
|
|
|
|
|
80
|
unlink "$dir/$_" for (@_) |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub clean { |
195
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
196
|
2
|
|
|
|
|
4
|
my $dir = $self->_dir; |
197
|
2
|
|
|
|
|
636
|
rmtree( $dir, 0 ); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
1; |
200
|
|
|
|
|
|
|
__END__ |