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