ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/IndexedFileStore.pm
Revision: 1.6
Committed: Wed Feb 2 20:16:46 2005 UTC (20 years, 3 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: v102p1, V1_0_1
Changes since 1.5: +5 -0 lines
Log Message:
Some more tidying of file permissions for downloaded/cached files.

File Contents

# User Rev Content
1 williamc 1.1 #
2     # IndexedFileStore.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     #
8     # Interface
9     # ---------
10     # new(storedir) : A new IndexedFileStore object based in storedir
11     # store(key,file) : store a key/file combination
12     # filename(key) : return the filename corresponding to a given key
13     # - generate one if it dosn't already exist
14     # file(key) : return the filename only if it already exists
15     # delete(key) : remove from cache key
16     # clear() : clear cache
17     # filestore() : Return the directory to download files to
18     # updatenumber(key) : return an integer number indicating update sequence
19     # - this number increased by one each time store is
20     # called
21    
22     package Utilities::IndexedFileStore;
23     require 5.004;
24     use Utilities::HashDB;
25    
26     sub new {
27     my $class=shift;
28     $self={};
29     bless $self, $class;
30     $self->_init(@_);
31     return $self;
32     }
33    
34     sub store {
35     my $self=shift;
36     my $key=shift;
37     my $file=shift;
38    
39 williamc 1.3 # store as relative to filestore if applicable - relocatable stores
40     my $filestore=$self->filestore();
41     $file=~s/^\Q$filestore\E\///;
42 williamc 1.1 $self->{keyDB}->deletedata($key);
43     $self->{keyDB}->setdata($file,$key);
44     $self->{keyDB}->store($self->{cacheindex});
45    
46     # Keep a track of changes
47     my ($sequencenumber)=$self->{keyDBupdate}->getdata($key);
48     if ( ! defined $sequencenumber ) { $sequencenumber=0 };
49     $sequencenumber=$sequencenumber+1;
50     $self->{keyDBupdate}->deletedata($key);
51     $self->{keyDBupdate}->setdata($sequencenumber,$key);
52     $self->{keyDBupdate}->store($self->{cacheseqindex});
53     }
54    
55     sub updatenumber {
56     my $self=shift;
57     my $key=shift;
58    
59     my ($rv)=$self->{keyDBupdate}->getdata($key);
60     return ((defined $rv)?$rv:0);
61     }
62    
63     sub filestore {
64     my $self=shift;
65     return $self->{cachedir};
66     }
67    
68     sub filename {
69     my $self=shift;
70     my $key=shift;
71    
72     my $filenumber;
73    
74     my $file=$self->file($key);
75     if ( $file eq "" ) {
76     # need to generate a new filename - a random number will do
77     srand();
78     do {
79     $filenumber=int(rand 99999999)+1;
80     $file=$self->filestore()."/".$filenumber;
81     } until ( ! ( -e $file ) );
82     }
83     return $file;
84     }
85    
86     sub file {
87     my $self=shift;
88     my $key=shift;
89    
90     my @found=$self->{keyDB}->getdata($key);
91 williamc 1.2 my $file=( ($#found == -1)?"":$found[0]);
92     # expand any files to include the path (unless complete path names)
93     if ( ($file ne "") && ($file!~/^\//) ) {
94     $file=$self->filestore()."/".$file;
95     }
96     return $file;
97 williamc 1.1 }
98    
99     sub clear {
100     my $self=shift;
101     foreach $item ( $self->{keyDB}->match() ) {
102     $self->delete($item->keys());
103     }
104     }
105    
106     sub delete {
107     my $self=shift;
108     my $key=shift;
109     unlink ($self->{keyDB}->getdata($URL));
110     $self->{keyDB}->deletedata($key);
111     $self->{keyDB}->store($self->{cacheindex});
112     }
113    
114 williamc 1.4 sub location {
115     my $self=shift;
116     return $self->{cachetop};
117     }
118    
119 williamc 1.1 sub _init {
120     my $self=shift;
121     $self->{cachetop}=shift;
122     $self->{cachedir}=$self->{cachetop}."/files";
123 sashby 1.5 AddDir::adddir($self->{cachetop});
124 williamc 1.1 AddDir::adddir($self->{cachedir});
125    
126     $self->{cacheindex}=$self->{cachetop}."/index.db";
127     $self->{cacheseqindex}=$self->{cachetop}."/seqnumb.db";
128     $self->{keyDB}=Utilities::HashDB->new();
129     $self->{keyDBupdate}=Utilities::HashDB->new();
130 sashby 1.6
131 williamc 1.1 if ( -f $self->{cacheindex} ) {
132     $self->{keyDB}->restore($self->{cacheindex});
133     }
134     else {
135     AddDir::adddir($self->{cachedir});
136     }
137     if ( -f $self->{cacheseqindex} ) {
138     $self->{keyDBupdate}->restore($self->{cacheseqindex});
139     }
140 sashby 1.6
141     my $mode = 0644;
142     chmod $mode, $self->{cacheindex}, $self->{cacheseqindex};
143    
144 williamc 1.1 }