ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Cache/CacheUtilities.pm
Revision: 1.9.2.2.2.2
Committed: Mon Apr 21 16:25:53 2008 UTC (17 years ago) by muzaffar
Content type: text/plain
Branch: SCRAM_V2_0
CVS Tags: V2_0_6, V2_0_5, V2_0_4, V2_0_4_relcand2, V2_0_4_relcand1, V2_0_3, V2_0_3_relcand3, V2_0_3_relcand2, V2_0_3_relcand1, V2_0_2, V2_0_2_relcand1, V2_0_1, V2_0_1_relcand4, V2_0_1_relcand3, V2_0_1_relcand2, V2_0_1_relcand1, V2_0_0_relcand4
Changes since 1.9.2.2.2.1: +78 -23 lines
Log Message:
switch to use gzip if Compress::Zlib not available

File Contents

# User Rev Content
1 sashby 1.2 package Cache::CacheUtilities;
2     require 5.004;
3 muzaffar 1.9.2.2.2.1 use Storable qw(nfreeze thaw retrieve nstore);
4     BEGIN {
5     eval "use Compress::Zlib qw(gzopen);";
6 muzaffar 1.9.2.2.2.2 if ($@){$Cache::CacheUtilities::zipUntility="GZip";}
7     else{$Cache::CacheUtilities::zipUntility="CompressZLib";}
8 muzaffar 1.9.2.2.2.1 }
9 sashby 1.5
10 sashby 1.2 sub read()
11     {
12 muzaffar 1.9.2.2.2.2 my $data=();
13     my $func="read${zipUntility}";
14     &$func(@_,\$data);
15     my $cache=eval {thaw($data);};
16     if ($EVAL_ERROR){die "Cache loading error: ",$EVAL_ERROR,"\n";}
17 sashby 1.2 return $cache;
18     }
19 muzaffar 1.9.2.2.2.2
20 sashby 1.2 sub write()
21     {
22 muzaffar 1.9.2.2.2.2 my $cache=shift;
23     my $file=shift;
24 sashby 1.2 use File::Copy;
25 muzaffar 1.9.2.2.2.1 if (-r $file){move($file,"${file}.bak");}
26     my $ok=1;
27 muzaffar 1.9.2.2.2.2 my $fcache=();
28     eval {$fcache=nfreeze($cache);};
29     if ($EVAL_ERROR){$ok=0;}
30     else
31     {
32     my $func="write${zipUntility}";
33     $ok = &$func($fcache,$file);
34     }
35 muzaffar 1.9.2.2.2.1 if ($ok)
36     {
37     unlink ("${file}.bak");
38     my $mode=0644;
39     chmod $mode,$file;
40     }
41     else
42     {
43     if (-r "${file}.bak"){move("${file}.bak",$file);}
44 muzaffar 1.9.2.2.2.2 die "ERROR: Writing Cache file: $file\n";
45     }
46     return;
47     }
48    
49     ###### Using gzip in case Compress::Zlib failed #################
50     sub readGZip()
51     {
52     my $file = shift;
53     my $data = shift;
54     my $gz;
55     if (open($gz,"gzip -cd $file |"))
56     {
57     binmode $gz;
58     my $buf;
59     while (read($gz,$buf,1024*1024) > 0){${$data}.=$buf;}
60     close($gz);
61 muzaffar 1.9.2.2.2.1 }
62 muzaffar 1.9.2.2.2.2 else{die "Can not open file for reading using \"gzip\": $file\n";}
63 sashby 1.2 return;
64 muzaffar 1.9.2.2.2.2 }
65    
66     sub writeGZip()
67     {
68     my ($cache,$file) = @_;
69     my $gz;
70     if (open($gz,"| gzip >$file"))
71     {
72     binmode $gz;
73     print $gz $cache;
74     close($gz);
75     }
76     else{die "Can not open file for reading using \"gzip\": $file\n";}
77     return 1;
78     }
79    
80     ###### Using Compress::Zlib #################
81     sub readCompressZLib()
82     {
83     my $file = shift;
84     my $data = shift;
85     if (my $gz = gzopen($file, "rb"))
86     {
87     my $buf;
88     while ($gz->gzread($buf,1024*1024) > 0){${$data}.=$buf;}
89     $gz->gzclose();
90     }
91     else{die "Can not open file \"$file\" for reading: $!\n";}
92     return;
93     }
94    
95     sub writeCompressZLib()
96     {
97     my ($cache,$file) = @_;
98     my $gz = gzopen($file, "wb");
99     if ($gz)
100     {
101     $gz->gzwrite($cache);
102     $gz->gzclose();
103     return 1;
104     }
105     return 0;
106 sashby 1.2 }
107    
108     1;