line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Collection::Utl::StoreDir; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Collection::Utl::StoreDir - Simple store/restore data to files in dirs. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Collection::Utl::StoreDir; |
10
|
|
|
|
|
|
|
my $fz = IO::Zlib->new($tmp_file, "rb"); |
11
|
|
|
|
|
|
|
my $dir = tempdir( CLEANUP => 0 ); |
12
|
|
|
|
|
|
|
my $temp_store = new Collection::Utl::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
|
|
31901
|
use IO::File; |
|
1
|
|
|
|
|
10751
|
|
|
1
|
|
|
|
|
136
|
|
25
|
1
|
|
|
1
|
|
7
|
use File::Path; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
49
|
|
26
|
1
|
|
|
1
|
|
4
|
use Data::Dumper; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
27
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
28
|
1
|
|
|
1
|
|
1098
|
use Encode; |
|
1
|
|
|
|
|
16539
|
|
|
1
|
|
|
|
|
121
|
|
29
|
1
|
|
|
1
|
|
9
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
30
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
31
|
|
|
|
|
|
|
my $attrs = { _dir => undef }; |
32
|
|
|
|
|
|
|
### install get/set accessors for this object. |
33
|
|
|
|
|
|
|
for my $key ( keys %$attrs ) { |
34
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1152
|
|
35
|
|
|
|
|
|
|
*{ __PACKAGE__ . "::$key" } = sub { |
36
|
16
|
|
|
16
|
|
27
|
my $self = shift; |
37
|
16
|
100
|
|
|
|
47
|
$self->{$key} = $_[0] if @_; |
38
|
16
|
|
|
|
|
50
|
return $self->{$key}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub new { |
43
|
2
|
|
|
2
|
0
|
1536
|
my $class = shift; |
44
|
2
|
|
|
|
|
4
|
my $obj; |
45
|
2
|
50
|
|
|
|
8
|
if ( ref $class ) { |
46
|
0
|
|
|
|
|
0
|
$obj = $class; |
47
|
0
|
|
|
|
|
0
|
$class = ref $obj; |
48
|
|
|
|
|
|
|
} |
49
|
2
|
|
|
|
|
6
|
my $self = bless( {}, $class ); |
50
|
2
|
50
|
|
|
|
7
|
if (@_) { |
51
|
2
|
|
|
|
|
3
|
my $dir = shift; |
52
|
2
|
50
|
|
|
|
8
|
if ($obj) { |
53
|
0
|
|
|
|
|
0
|
$dir =~ s%^/%%; |
54
|
0
|
|
|
|
|
0
|
$dir = $obj->_dir . $dir; |
55
|
|
|
|
|
|
|
} |
56
|
2
|
50
|
|
|
|
12
|
$dir .= "/" unless $dir =~ m%/$%; |
57
|
2
|
|
|
|
|
8
|
$self->_dir($dir); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
0
|
carp "need path to dir"; |
61
|
0
|
|
|
|
|
0
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
2
|
|
|
|
|
15
|
return $self; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _store_data { |
67
|
3
|
|
|
3
|
|
10
|
my ( $self, $mode, $name, $val ) = @_; |
68
|
3
|
50
|
|
|
|
8
|
return unless defined $val; |
69
|
3
|
|
|
|
|
8
|
my $file_name = $self->_get_path . $name; |
70
|
3
|
50
|
|
|
|
27
|
my $out = new IO::File:: "> $file_name" or die $!; |
71
|
3
|
|
|
|
|
431
|
local $/; |
72
|
3
|
|
|
|
|
7
|
$/ = undef; |
73
|
3
|
|
|
|
|
24
|
my ($atime, $mtime); |
74
|
3
|
100
|
|
|
|
11
|
if ( ref $val ) { |
75
|
1
|
50
|
33
|
|
|
14
|
if ( UNIVERSAL::isa( $val, 'IO::Handle' ) |
|
|
|
33
|
|
|
|
|
76
|
|
|
|
|
|
|
or ( ref $val eq 'GLOB' ) |
77
|
|
|
|
|
|
|
or UNIVERSAL::isa( $val, 'Tie::Handle' ) ) |
78
|
|
|
|
|
|
|
{ |
79
|
1
|
|
|
|
|
32
|
$out->print(<$val>); |
80
|
|
|
|
|
|
|
#set atime and mtime |
81
|
1
|
|
|
|
|
26
|
($atime, $mtime) = (stat $val )[8,9]; |
82
|
1
|
|
|
|
|
5
|
$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
|
|
|
|
14
|
$out->print( ( $mode =~ /utf8/ ) ? $self->_utfx2utf($val) : $val ); |
91
|
|
|
|
|
|
|
} |
92
|
3
|
50
|
|
|
|
49
|
$out->close or die $!; |
93
|
3
|
100
|
66
|
|
|
193
|
if ( $atime && $mtime) { |
94
|
1
|
|
|
|
|
47
|
utime $atime, $mtime, $file_name; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _utfx2utf { |
100
|
2
|
|
|
2
|
|
5
|
my ( $self, $str ) = @_; |
101
|
2
|
50
|
|
|
|
8
|
$str = encode( 'utf8', $str ) if utf8::is_utf8($str); |
102
|
2
|
|
|
|
|
15
|
return $str; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _utf2utfx { |
106
|
1
|
|
|
1
|
|
4
|
my ( $self, $str ) = @_; |
107
|
1
|
50
|
|
|
|
10
|
$str = decode( 'utf8', $str ) unless utf8::is_utf8($str); |
108
|
1
|
|
|
|
|
75
|
return $str; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _get_path { |
112
|
3
|
|
|
3
|
|
6
|
my $self = shift; |
113
|
3
|
|
|
|
|
5
|
my $key = shift; |
114
|
3
|
|
|
|
|
6
|
my $dir = $self->_dir; |
115
|
3
|
100
|
|
|
|
350
|
mkpath( $dir, 0 ) unless -e $dir; |
116
|
3
|
|
|
|
|
10
|
return $dir; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub putText { |
120
|
3
|
|
|
3
|
0
|
5
|
my $self = shift; |
121
|
3
|
|
|
|
|
10
|
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
|
3
|
my $self = shift; |
131
|
2
|
|
|
|
|
3
|
my $key = shift; |
132
|
2
|
50
|
|
|
|
7
|
my $fh = new IO::File:: "< " . $self->_dir . $key or return; |
133
|
2
|
|
|
|
|
172
|
return $fh; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub getRaw { |
137
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
138
|
1
|
50
|
|
|
|
4
|
if ( my $fd = $self->getRaw_fh(@_) ) { |
139
|
1
|
|
|
|
|
2
|
my $data; |
140
|
|
|
|
|
|
|
{ |
141
|
1
|
|
|
|
|
2
|
local $/; |
|
1
|
|
|
|
|
5
|
|
142
|
1
|
|
|
|
|
2
|
undef $/; |
143
|
1
|
|
|
|
|
25
|
$data = <$fd>; |
144
|
|
|
|
|
|
|
} |
145
|
1
|
|
|
|
|
4
|
$fd->close; |
146
|
1
|
|
|
|
|
18
|
return $data; |
147
|
|
|
|
|
|
|
} |
148
|
0
|
|
|
|
|
0
|
else { return } |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub getText { |
152
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
153
|
1
|
|
|
|
|
5
|
return $self->_utf2utfx( $self->getRaw(@_) ); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub getText_fh { |
157
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
158
|
1
|
|
|
|
|
4
|
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
|
9
|
my $self = shift; |
170
|
5
|
|
|
|
|
12
|
my $dir = $self->_dir; |
171
|
5
|
50
|
|
|
|
111
|
return [] unless -e $dir; |
172
|
5
|
50
|
|
|
|
172
|
opendir DIR, $dir or die $!; |
173
|
5
|
|
|
|
|
12
|
my @keys = (); |
174
|
5
|
|
|
|
|
115
|
while ( my $key = readdir DIR ) { |
175
|
15
|
100
|
100
|
|
|
261
|
next if $key =~ /^\.\.?$/ or -d "$dir/$key"; |
176
|
3
|
|
|
|
|
16
|
push @keys, $key; |
177
|
|
|
|
|
|
|
} |
178
|
5
|
|
|
|
|
36
|
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
|
|
|
|
|
3
|
my $dir = $self->_dir; |
189
|
1
|
|
|
|
|
125
|
unlink "$dir/$_" for (@_) |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub clean { |
193
|
2
|
|
|
2
|
0
|
7
|
my $self = shift; |
194
|
2
|
|
|
|
|
6
|
my $dir = $self->_dir; |
195
|
2
|
|
|
|
|
976
|
rmtree( $dir, 0 ); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
1; |
198
|
|
|
|
|
|
|
__END__ |