File Coverage

blib/lib/XML/Filter/Cache/File.pm
Criterion Covered Total %
statement 52 64 81.2
branch 10 30 33.3
condition 1 3 33.3
subroutine 11 11 100.0
pod 0 3 0.0
total 74 111 66.6


line stmt bran cond sub pod time code
1             # $Id: File.pm,v 1.2 2002/01/28 15:17:27 matt Exp $
2            
3             package XML::Filter::Cache::File;
4 3     3   17 use strict;
  3         6  
  3         127  
5            
6 3     3   15 use vars qw($VERSION @ISA);
  3         5  
  3         189  
7             $VERSION = '0.02';
8            
9 3     3   15 use XML::Filter::Cache ();
  3         13  
  3         98  
10             @ISA = qw(XML::Filter::Cache);
11            
12 3     3   15 use Digest::MD5 qw(md5_hex);
  3         11  
  3         174  
13 3     3   16 use File::Spec ();
  3         5  
  3         57  
14 3     3   829 use Symbol ();
  3         780  
  3         2045  
15            
16             sub new {
17 3     3 0 7 my $class = shift;
18 3 50       16 my $opts = (@_ == 1) ? { %{shift(@_)} } : {@_};
  3         18  
19            
20 3   33     15 $opts->{CacheRoot} ||= File::Spec->tmpdir;
21            
22 3         70 return bless $opts, $class;
23             }
24            
25             sub open {
26 4     4 0 12 my ($self, $mode) = @_;
27            
28 4         39 my $key = md5_hex($self->{Key});
29 4         20 my $primary = substr($key, 0, 2, '');
30 4         12 my $secondary = substr($key, 0, 2, '');
31 4         11 my $cacheroot = $self->{CacheRoot};
32 4         67 my $filename = File::Spec->catdir($cacheroot, $primary, $secondary, $key);
33 4         20 my $fh = Symbol::gensym();
34            
35 4 100       73 if ($mode eq 'w') {
    50          
36 2 50       368 if (!open($fh, ">$filename")) {
37 0 0       0 if (!-e $cacheroot) {
38 0 0       0 if (!mkdir($cacheroot, 0777)) {
39 0         0 die "Cannot create cache directory '$cacheroot': $!";
40             }
41             }
42            
43 0 0       0 if (!-e File::Spec->catdir($cacheroot, $primary)) {
44 0 0       0 if (!mkdir(File::Spec->catdir($cacheroot, $primary), 0777)) {
45 0         0 die "Cannot create primary directory '$cacheroot/$primary': $!";
46             }
47             }
48            
49 0 0       0 if (!-e File::Spec->catdir($cacheroot, $primary, $secondary)) {
50 0 0       0 if (!mkdir(File::Spec->catdir($cacheroot, $primary, $secondary), 0777)) {
51 0         0 die "Cannot create secondary directory '$cacheroot/$primary/$secondary': $!";
52             }
53             }
54            
55 0 0       0 open($fh, ">$filename")
56             || die "Cannot write to cache file '$filename': $!";
57             }
58            
59 2         8 binmode($fh);
60             }
61             elsif ($mode eq 'r') {
62 2 50       147 open($fh, "<$filename") || die "Cannot read cache file '$filename': $!";
63 2         10 binmode($fh);
64             }
65 4         10 $self->{fh} = $fh;
66 4         24 $self->{filename} = $filename;
67             }
68            
69             sub close {
70 4     4 0 6 my $self = shift;
71 4         252 close($self->{fh});
72             }
73            
74             sub _read {
75 1347     1347   77504 my $self = shift;
76 1347         1807 my $fh = $self->{fh};
77 1347 100       2987 return if eof($fh);
78 1345         1390 my $buff;
79 1345 50       3388 if (read($fh, $buff, 4) != 4) {
80 0         0 die "Broken cache file '$self->{filename}'";
81             }
82 1345         2181 my $length = unpack("L", $buff);
83 1345 50       3519 if (read($fh, $buff, $length) != $length) {
84 0         0 die "Broken cache file '$self->{filename}'";
85             }
86 1345         2665 my $record = unpack("a*", $buff);
87 1345         5282 return $record;
88             }
89            
90             sub _write {
91 1345     1345   1965 my ($self, $frozen) = @_;
92 1345         2315 my $fh = $self->{fh};
93 1345         3357 my $out = pack("La*", length($frozen), $frozen);
94 1345         38851 print $fh $out;
95             }
96            
97             1;
98             __END__