Virtual/HandleCache.pm

filter: Statementstmt 8.0% 11 / 136 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 18.7% 3 / 16 Podpod 100% 8 / 8 totaltotal 9.0% 22 / 243 CC 38 SCAR 39.8
CC
38
Cov
5%
_read_seek
132.0
_write_seek
90.0
is_busy
30.0
CRAP 53.4
↑ Summary Virtual/FSWrapper.pm »
1 #
2 # Fuse::Filesys::Virtual::HandleCache
3 #
4 #
5  
6 package Fuse::Filesys::Virtual::HandleCache;
7  
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Fuse::Filesys::Virtual::HandleCache - R/W handle cache manager
14  
15 =head1 VERSION
16  
17 Version 0.02
18  
19 =cut
20  
21 1 our $VERSION = '0.02';
22  
23 =head1 SYNOPSIS
24  
25 In Fuse::Filesys::Virtual, read and write handle is cached for
26 performance reason.
27  
28 =head1 EXPORT
29  
30 Nothing.
31  
32 =cut
33  
34 1 ▶use POSIX qw(:errno_h :fcntl_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 =head1 FUNCTIONS
38  
39 =head2 new
40  
41 constractor
42  
43 =cut
44  
45 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
46 0 my $class = shift;
47 0 my ($filesys) = shift;
48  
49 0 bless {
50 _filesys => $filesys,
51 _read => {},
52 _write => {},
53 }, $class;
54 }
55  
56 =head2 is_busy
57  
58 returns file or directory is used or not.
59  
60 =cut
61  
62 0 ▶sub is_busy {
Subroutine is_busy: not called CC=5 SCAR=34.0
Pod documented
63 0 my $self = shift;
64 0 my ($fname) = @_;
65  
66 0 ▶ return 1 if ($self->{_read}->{$fname});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_read"}{$fname} 0 0 0
67 0 ▶ return 1 if ($self->{_write}->{$fname});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_write"}{$fname} 0 0 0
68  
69 # directory is busy?
70 0 for (keys %{$self->{_read}}, keys %{$self->{_write}}) {
71 0 ▶ return 1 if (/^\Q$fname\E\//);
Branch: 1 branch
BranchTrueFalseTotal
if m[^\Q$fname\E/] 0 0 0
72 }
73  
74 0 return undef;
75 }
76  
77 0 ▶sub _file_exist {
Subroutine _file_exist: not called CC=3 SCAR=24.8
78 0 my $self = shift;
79 0 my ($fname) = @_;
80  
81 # by stat
82 0 my $ret;
83 0 eval {
84 0 $ret = $self->{_filesys}->stat($fname);
85 };
86 0 ▶ return $ret unless ($@ =~ /Unimplemented/);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Unimplemented/ 0 0 0
87  
88 0 eval {
89 0 $ret = $self->{_filesys}->test('f', $fname);
90 };
91 0 ▶ return $ret unless ($@ =~ /Unimplemented/);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Unimplemented/ 0 0 0
92  
93 0 die "cannot determine file status";
94 }
95  
96 0 ▶sub _read_open {
Subroutine _read_open: not called CC=5 SCAR=34.0
97 0 my $self = shift;
98 0 my ($fname) = @_;
99  
100 # already opened?
101 0 ▶ return $self->{_read}->{$fname} if ($self->{_read}->{$fname});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_read"}{$fname} 0 0 0
102  
103 0 my $fh = $self->{_filesys}->open_read($fname);
104  
105 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
106 0 my $eno = $! + 0;
107 0 ▶ if ($self->_file_exist($fname)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_file_exist($fname)) { } 0 0 0
108 0 ▶ $! = $eno || EPERM;
Condition0%0/2
$eno || 1
l!l
00
MC/DC0%0/1
$eno || 1
$eno
Truth table0%0/2
A
A= $eno
Aresult
11
00
109 0 die "cannot open file for reading: $!";
110 }
111 else {
112 0 $! = ENOENT;
113 0 die "file not found";
114 }
115 }
116  
117 0 return $self->{_read}->{$fname} = { fh => $fh, pos => 0 };
118 }
119  
120 0 ▶sub _read_seek {
Subroutine _read_seek: not called CC=11 SCAR=48.8
121 0 my $self = shift;
122 0 my ($fname, $offset) = @_;
123  
124 0 my $c = $self->_read_open($fname);
125 0 ▶ return $c if ($c->{pos} == $offset);
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"pos"} == $offset 0 0 0
126  
127 0 my $ret = eval {
128 0 ▶ if ($self->{_filesys}->seek($c->{fh}, $offset, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_filesys"}->seek($c->{"fh"}, $offset, 0)) 0 0 0
129 0 $c->{pos} = $offset;
130 0 return $c;
131 }
132 0 die "seek error";
133 };
134 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
135 0 ▶ die $@ unless ($@ =~ /Unimplemented/);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Unimplemented/ 0 0 0
136 }
137 0 ▶ return $ret if ($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
138  
139 # change current position by reading...
140 0 $self->read_release($fname);
141 0 $c = $self->_read_open($fname);
142  
143 0 my $pos = $c->{pos};
144 0 my $buf;
145 0 my $buflen = 4096;
146  
147 0 ▶ while($pos != $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != $offset) 0 0 0
148 0 my $rlen = ($offset - $pos);
149 0 ▶ $rlen = $buflen if ($rlen > $buflen);
Branch: 1 branch
BranchTrueFalseTotal
if $rlen > $buflen 0 0 0
150  
151 0 my $n = read($c->{fh}, $buf, $rlen);
152 0 ▶ if (!defined($n) || $n < $rlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n or $n < $rlen) 0 0 0
Condition0%0/3
not defined $n or $n < $rlen
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $n or $n < $rlen
not defined $n $n < $rlen
Truth table0%0/3
not(A) or B
A= not defined $nB= $n < $rlen
ABresult
1X1
011
000
153 0 ▶ $! = EINVAL if ($! == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 0 0 0 0
154 0 die "too large offset";
155 }
156 0 $pos += $n;
157 }
158  
159 0 $c->{pos} = $pos;
160  
161 0 return $c;
162 }
163  
164 =head2 read (FNAME, SIZE, OFFSET)
165  
166 read data from cached file handle.
167  
168 =cut
169  
170 0 ▶sub read {
Subroutine read: not called CC=2 SCAR=17.9
Pod documented
171 0 my $self = shift;
172 0 my ($fname, $size, $offset) = @_;
173  
174 0 my $c = $self->_read_seek($fname, $offset);
175 0 my $buf;
176 0 my $n = $c->{fh}->read($buf, $size);
177 0 $c->{pos} += $n;
178 0 ▶ $c->{pos} ++ if ($n > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n > 0 0 0 0
179  
180 0 my $len = length($buf);
181  
182 0 return $buf;
183 }
184  
185 0 ▶sub _write_open {
Subroutine _write_open: not called CC=5 SCAR=34.0
186 0 my $self = shift;
187 0 my ($fname, $append) = @_;
188  
189 0 $self->read_release($fname);
190 0 $self->write_release($fname);
191  
192 0 my $fh = $self->{_filesys}->open_write($fname, $append);
193 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
194 0 ▶ $! = EPERM if ($! == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 0 0 0 0
195 0 die "cannot open file for writing: $!";
196 }
197  
198 0 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 0 0 0
199 0 ▶ my $size = $self->{_filesys}->size($fname) || 0;
Condition0%0/2
$self->{'_filesys'}->size($fname) || 0
l!l
00
MC/DC0%0/1
$self->{'_filesys'}->size($fname) || 0
$self->{'_filesys'}->size($fname)
Truth table0%0/2
A
A= $self->{'_filesys'}->size($fname)
Aresult
11
00
200 0 $self->{_write}->{$fname} = { fh => $fh, pos => $size };
201 }
202 else {
203 0 $self->{_write}->{$fname} = { fh => $fh, pos => 0 };
204 }
205  
206 0 return $self->{_write}->{$fname};
207 }
208  
209 0 ▶sub _write_seek {
Subroutine _write_seek: not called CC=9 SCAR=45.0
210 0 my $self = shift;
211 0 my ($fname, $offset) = @_;
212  
213 0 my $c = $self->{_write}->{$fname};
214 0 ▶ return $c if ($c && $c->{pos} == $offset);
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c->{"pos"} == $offset 0 0 0
Condition0%0/3
$c and $c->{"pos"} == $offset
!ll&&!rl&&r
000
MC/DC0%0/2
$c and $c->{"pos"} == $offset
$c $c->{"pos"} == $offset
Truth table0%0/3
A and B
A= $cB= $c->{"pos"} == $offset
ABresult
0X0
100
111
215  
216 # if NOT append mode open, already truncated.
217 0 $c = $self->_write_open($fname, 1);
218 0 ▶ return $c if ($c && $c->{pos} == $offset);
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c->{"pos"} == $offset 0 0 0
Condition0%0/3
$c and $c->{"pos"} == $offset
!ll&&!rl&&r
000
MC/DC0%0/2
$c and $c->{"pos"} == $offset
$c $c->{"pos"} == $offset
Truth table0%0/3
A and B
A= $cB= $c->{"pos"} == $offset
ABresult
0X0
100
111
219  
220 0 eval {
221 0 ▶ if ($self->{_filesys}->seek($c->{fh}, $offset, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_filesys'}->seek($c->{'fh'}, $offset, 0)) { } 0 0 0
222 0 $c->{pos} = $offset;
223 0 return $c;
224 }
225 else {
226 0 die "seek error";
227 }
228 };
229 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
230 0 ▶ $! = EINVAL if ($! == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 0 0 0 0
231 0 die $@;
232 }
233  
234 0 ▶ return $c unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
235 }
236  
237 =head2 write (FNAME, BUFF, OFFSET)
238  
239 write data to cached file handle.
240  
241 =cut
242  
243 0 ▶sub write {
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
244 0 my $self = shift;
245 0 my ($fname, $buf, $offset) = @_;
246  
247 0 my $c = $self->_write_seek($fname, $offset);
248 0 my $fh = $c->{fh};
249  
250 0 ▶ print $fh $buf or return 0;
Branch: 1 branch
BranchTrueFalseTotal
print $fh $buf or return 0 0 0 0
251 0 $c->{pos} += length($buf);
252 0 ▶ $c->{pos} ++ if (length($buf) > 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $buf > 0 0 0 0
253  
254 0 return length($buf);
255 }
256  
257 =head2 truncate (FNAME)
258  
259 truncate a file.
260  
261 =cut
262  
263 0 ▶sub truncate {
Subroutine truncate: not called CC=1 SCAR=6.9
Pod documented
264 0 my $self = shift;
265 0 my ($fname) = @_;
266  
267 0 $self->read_release($fname);
268 0 $self->write_release($fname);
269  
270 0 my $c = $self->_write_open($fname, undef);
271 }
272  
273 =head2 read_release
274  
275 release file handle for read
276  
277 =cut
278  
279 0 ▶sub read_release {
Subroutine read_release: not called CC=2 SCAR=17.9
Pod documented
280 0 my $self = shift;
281 0 my ($fname) = @_;
282  
283 0 ▶ if ($self->{_read}->{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_read"}{$fname}) 0 0 0
284 0 $self->{_filesys}->close_read($self->{_read}->{$fname}->{fh});
285 0 delete $self->{_read}->{$fname};
286 }
287 }
288  
289 =head2 write_release
290  
291 release file handle for write
292  
293 =cut
294  
295 0 ▶sub write_release {
Subroutine write_release: not called CC=2 SCAR=17.9
Pod documented
296 0 my $self = shift;
297 0 my ($fname) = @_;
298  
299 0 ▶ if ($self->{_write}->{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_write"}{$fname}) 0 0 0
300 0 $self->{_filesys}->close_write($self->{_write}->{$fname}->{fh});
301 0 delete $self->{_write}->{$fname};
302 }
303 }
304  
305 =head2 release
306  
307 release file handle
308  
309 =cut
310  
311 0 ▶sub release {
Subroutine release: not called CC=1 SCAR=6.9
Pod documented
312 0 my $self = shift;
313 0 my ($fname) = @_;
314  
315 0 $self->write_release($fname);
316 0 $self->read_release($fname);
317 }
318  
319 1 1;
↑ Summary Virtual/FSWrapper.pm »