ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Cache/CacheUtilities.pm
Revision: 1.9.2.2.2.3
Committed: Mon Feb 16 11:16:46 2009 UTC (16 years, 2 months ago) by muzaffar
Content type: text/plain
Branch: SCRAM_V2_0
CVS Tags: V2_2_2, V2_2_2_pre4, V2_2_2_pre3, V2_2_2_pre2, V2_2_2_pre1, V2_2_2-pre1, V2_2_1, forV2_2_1, V2_2_0, sm100112, V2_1_4, V2_1_3, V2_1_2, V2_1_1, V2_1_0
Changes since 1.9.2.2.2.2: +11 -10 lines
Log Message:
Make use of Data::Dumper to store scram cahces

File Contents

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