1 |
#____________________________________________________________________
|
2 |
# File: CacheUtilities.pm
|
3 |
#____________________________________________________________________
|
4 |
#
|
5 |
# Author: Shaun Ashby <Shaun.Ashby@cern.ch>
|
6 |
# Update: 2003-10-30 11:51:58+0100
|
7 |
# Revision: $Id: CacheUtilities.pm,v 1.3 2005/02/02 16:31:11 sashby Exp $
|
8 |
#
|
9 |
# Copyright: 2003 (C) Shaun Ashby
|
10 |
#
|
11 |
#--------------------------------------------------------------------
|
12 |
package Cache::CacheUtilities;
|
13 |
require 5.004;
|
14 |
|
15 |
use IO::File;
|
16 |
use English;
|
17 |
use Exporter;
|
18 |
|
19 |
@ISA=qw(Exporter);
|
20 |
|
21 |
#
|
22 |
# Common functions for interacting with caches:
|
23 |
#
|
24 |
|
25 |
sub read()
|
26 |
{
|
27 |
my ($cachefilename) = @_;
|
28 |
my $cachefh = IO::File->new($cachefilename, O_RDONLY)
|
29 |
|| die "Unable to read cached data file $cachefilename: ",$ERRNO,"\n";
|
30 |
my @cacheitems = <$cachefh>;
|
31 |
close $cachefh;
|
32 |
|
33 |
# Copy the new cache object to self and return:
|
34 |
$cache = eval "@cacheitems";
|
35 |
die "Cache load error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR);
|
36 |
return $cache;
|
37 |
}
|
38 |
|
39 |
sub write()
|
40 |
{
|
41 |
my ($cacheobject,$cachefilename) = @_;
|
42 |
|
43 |
use Data::Dumper;
|
44 |
use File::Copy;
|
45 |
|
46 |
print "[ CacheUtilities::write() ] Writing cache ",$cachefilename,"\n", if ($ENV{SCRAM_DEBUG});
|
47 |
|
48 |
# Rename the cache file to make a backup copy:
|
49 |
move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename);
|
50 |
# Dump the cache to file:
|
51 |
my $cachefh = IO::File->new($cachefilename, O_WRONLY|O_CREAT)
|
52 |
or die "Couldn't write to $cachefilename: ",$ERRNO,"\n";
|
53 |
|
54 |
# Name that should replace "VAR1" in the dumped
|
55 |
# representation of the cache object:
|
56 |
$Data::Dumper::Varname='cache';
|
57 |
$Data::Dumper::Purity = 1;
|
58 |
print $cachefh Dumper($cacheobject);
|
59 |
close $cachefh;
|
60 |
|
61 |
# Change the permissions to -rw-r--r--:
|
62 |
my $filemode = 0644;
|
63 |
chmod $filemode, $cachefilename;
|
64 |
|
65 |
return;
|
66 |
}
|
67 |
|
68 |
1;
|