lib/Sub/Slice/Backend/Filesystem.pm

filter: Statementstmt 99.2% 128 / 129 Branchbran 86.9% 40 / 46 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 13 totaltotal 90.0% 209 / 232 CC 29 SCAR 12.4
CC
29
Cov
96%
__ANON__
9.0
new
5.0
store_blob
4.0
CRAP 3.5
« blib/lib/Sub/Slice.pm ↑ Summary
1 ###############################################################################
2 # Purpose : Filesystem (default) backend for Sub::Slice
3 # Author : John Alden
4 # Created : Nov 2004
5 # CVS : $Header: /home/cvs/software/cvsroot/sub_slice/lib/Sub/Slice/Backend/Filesystem.pm,v 1.13 2005/01/12 16:51:19 simonf Exp $
6 ###############################################################################
7  
8 package Sub::Slice::Backend::Filesystem;
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Storable();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use constant JOBFILE_PREFIX => 'Sub__Slice__';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant MASK_LENGTH => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use constant TOKEN_DB => 'sub_slice_job.store';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 $VERSION = sprintf"%d.%03d", q$Revision: 1.13 $ =~ /: (\d+)\.(\d+)/;
23  
24 27 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
25 27 my($class, $options) = @_;
26
27 # Use a subdir within the temp directory by default, so cleanup
28 # can walk the tree beneath it rather than having to match
29 # everything in the temp dir against a mask
30 27 my $path = $class->default_path($options->{path});
31 27 ▶ File::Path::mkpath($path) unless (-d $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 1 26 27
32
33 my $self = {
34 path => $path,
35 prefix => $options->{prefix} || JOBFILE_PREFIX,
36 storable_filename => $options->{job_filename} || TOKEN_DB,
37 mask_length => $options->{unique_key_length} || MASK_LENGTH,
38 lax => $options->{lax}
39 27 ▶ };
Condition100%6/6
$options->{'prefix'} || 'Sub__Slice__'
l!l
324
$options->{'job_filename'} || 'sub_slice_job.store'
l!l
324
$options->{'unique_key_length'} || 12
l!l
324
MC/DC100%1/1
$options->{'prefix'} || 'Sub__Slice__'
$options->{'prefix'}
MC/DC100%1/1
$options->{'job_filename'} || 'sub_slice_job.store'
$options->{'job_filename'}
MC/DC100%1/1
$options->{'unique_key_length'} || 12
$options->{'unique_key_length'}
Truth table100%2/2
A
A= $options->{'prefix'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'job_filename'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'unique_key_length'}
Aresult
11
00
40 27 return bless($self, $class);
41 }
42  
43 # Given a path (for our temp dir), do any required canonicalization
44 # eg. make sure there is always a trailing /.
45 # Use a default path if one is not specified.
46 29 ▶sub default_path {
Subroutine default_path: called CC=2 SCAR=6.9
Pod undocumented
47 29 my ($class, $path) = @_;
48 29 ▶ $path = $path || File::Spec::Functions::tmpdir()."/sub_slice";
Condition100%2/2
$path || File::Spec::Functions::tmpdir() . '/sub_slice'
l!l
281
MC/DC100%1/1
$path || File::Spec::Functions::tmpdir() . '/sub_slice'
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
49 29 $path =~ s!([^/])$!$1/!; # add a trailing slash
50 29 $path;
51 }
52  
53 7 ▶sub new_id {
Subroutine new_id: called CC=1 SCAR=0.0
Pod undocumented
54 7 my ($self) = @_;
55 7 my $mask = "X" x $self->{mask_length};
56 7 my ($dir) = File::Temp::mkdtemp($self->{path} . $self->{prefix} . $mask);
57 7 my $id = scalar File::Spec::Functions::splitpath( $dir );
58 7 TRACE("Created new ID: $id");
59 7 return $id;
60 }
61  
62 21 ▶sub load_job {
Subroutine load_job: called CC=1 SCAR=0.0
Pod undocumented
63 21 my ($self, $id) = @_;
64 21 my $filename = $self->_db_from_id( $self->_check_id($id) );
65 18 TRACE("loading job '$id' from '$filename'");
66 18 return Storable::retrieve( $filename );
67 }
68  
69 20 ▶sub save_job {
Subroutine save_job: called CC=3 SCAR=11.0
Pod undocumented
70 20 my ($self, $job) = @_;
71 20 ▶ croak("job should be a Sub::Slice object") unless(UNIVERSAL::isa($job, 'Sub::Slice'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($job, "Sub::Slice") 1 19 20
72 19 my $filename = $self->_db_from_id( $job->id );
73 19 my $job_id = $job->id;
74 19 TRACE("saving job '$job_id' to '$filename' ($$)");
75 19 ▶ TRACE("job_file for '$job_id' already exists and will be overwritten") if (-e $filename);
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 12 7 19
76 19 Storable::store( $job, $filename );
77 }
78  
79 5 ▶sub delete_job {
Subroutine delete_job: called CC=2 SCAR=6.9
Pod undocumented
80 5 my ($self, $id) = @_;
81 5 my $dir = $self->_dir_from_id( $self->_check_id($id) );
82 4 ▶ die("Job $id does not exist") unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 3 4
83 3 TRACE("deleting directory $dir");
84 3 rmtree $dir;
85 }
86  
87 26 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
88 26 my ($self, $job, $key, $value) = @_;
89 26 $job->{'data'}{$key} = $value;
90 }
91  
92 42 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod undocumented
93 42 my ($self, $job, $key) = @_;
94 42 return $job->{'data'}{$key};
95 }
96  
97 7 ▶sub store_blob {
Subroutine store_blob: called CC=4 SCAR=13.9
Pod undocumented
98 7 my ($self, $job, $key, $value) = @_;
99 7 ▶ croak("job should be a Sub::Slice object") unless(UNIVERSAL::isa($job, 'Sub::Slice'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($job, "Sub::Slice") 1 6 7
100 6 ▶ croak("you must supply a key to store the blob against") unless(defined $key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 5 6
101 5 ▶ if (my $data_file = $job->{'.blobs'}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data_file = $job->{'.blobs'}{$key}) { } 1 4 5
102 1 TRACE("Updating blob for $key in $data_file");
103 1 _write_file($data_file, $value);
104 } else {
105 4 my $dir = $self->_dir_from_id( $job->id );
106 4 my ($fh, $data_file) = File::Temp::tempfile(DIR => $dir, UNLINK => 0);
107 4 TRACE("Writing blob for $key in $data_file");
108 4 print $fh $value;
109 4 close $fh;
110 4 $job->{'.blobs'}{$key} = $data_file;
111 }
112 5 return 1;
113 }
114  
115 10 ▶sub fetch_blob {
Subroutine fetch_blob: called CC=4 SCAR=13.9
Pod undocumented
116 10 my ($self, $job, $key) = @_;
117 10 ▶ croak("job should be a Sub::Slice object") unless(UNIVERSAL::isa($job, 'Sub::Slice'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($job, "Sub::Slice") 1 9 10
118 9 ▶ croak("you must supply a key to fetch the blob") unless(defined $key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 8 9
119 8 ▶ if (my $data_file = $job->{'.blobs'}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data_file = $job->{".blobs"}{$key}) 6 2 8
120 6 TRACE("Fetching blob for $key from $data_file");
121 6 return _read_file($data_file);
122 }
123 }
124  
125 2 ▶sub cleanup {
Subroutine cleanup: called CC=3 SCAR=11.1
Pod undocumented
126 2 my ($self, $maxage) = @_;
127 2 ▶ $maxage = 1 if !defined $maxage;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxage 1 1 2
128 2 local $^T = time();
129 2 my $deleted = 0;
130 3 ▶ my $cleaner = sub {
Subroutine __ANON__: called CC=8 SCAR=22.0
131 3 ▶ return if /^\.{1,2}$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\.{1,2}$/ 1 2 3
132 2 my $mtime = -M $_;
133 2 TRACE("file $_ mtime $mtime");
134  
135 # it may have *just* disappeared
136 2 ▶ return unless defined $mtime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mtime 0 2 2
137  
138 # only want to clean up if it's old.
139 2 ▶ return unless $mtime >= $maxage;
Branch: 1 branch
BranchTrueFalseTotal
unless $mtime >= $maxage 0 2 2
140 2 $deleted++;
141 2 ▶ if (-f $_) { unlink $_ || die "can't delete $_: $!" }
Branch: 2 branches
BranchTrueFalseTotal
if (-f $_) { } 1 1 2
elsif (-d $_) { } 1 0 1
Condition50%1/2
$_ || die("can't delete ${_}: $!")
l!l
10
MC/DC0%0/1
$_ || die("can't delete ${_}: $!")
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
142 1 ▶ elsif (-d $_) { rmdir $_ || die "can't rmdir $_: $!" }
Branch: 1 branch
BranchTrueFalseTotal
rmdir $_ or die "can't rmdir ${_}: $!" 1 0 1
143 0 else { $deleted-- };
144 2 };
145 2 my $p = $self->{path};
146 2 ▶ return if (!-d $p);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $p 1 1 2
147 1 require File::Find;
148 1 TRACE ("Cleaning up ".$p);
149 1 File::Find::finddepth ($cleaner, $self->{path});
150 1 $deleted;
151 }
152  
153 #
154 # Private functions encapsulating:
155 # - creating the dir from an ID
156 # - creating the storable db filename from an ID
157 # - file IO for blob data
158 #
159  
160 8 ▶sub _dir_from_id {
Subroutine _dir_from_id: called CC=1 SCAR=0.0
161 8 my($self, $id) = @_;
162 8 return File::Spec::Functions::catfile($self->{path}, $id);
163 }
164  
165 37 ▶sub _db_from_id {
Subroutine _db_from_id: called CC=1 SCAR=0.0
166 37 my($self, $id) = @_;
167 37 return File::Spec::Functions::catfile($self->{path}, $id, $self->{storable_filename});
168 }
169  
170 26 ▶sub _check_id {
Subroutine _check_id: called CC=4 SCAR=13.9
171 26 my($self, $id) = @_;
172 26 ▶ confess("Called without an id") unless(defined $id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 2 24 26
173 24 ▶ unless($self->{lax}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lax"}) 23 1 24
174 23 my $regex = quotemeta($self->{prefix}) . ('\w' x $self->{mask_length});
175 23 ▶ confess("Format of ID '$id' is invalid") unless($id =~ /\A$regex\Z/);
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A$regex\Z/ 2 21 23
176 }
177 22 return $id;
178 }
179  
180 6 ▶sub _read_file {
Subroutine _read_file: called CC=2 SCAR=7.0
181 6 my $filename = shift;
182 6 ▶ open (FH, $filename) || die("unable to open $filename - $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, $filename or die "unable to open $filename - $!" 6 0 6
183 6 local $/ = undef;
184 6 my $data = <FH>;
185 6 close FH;
186 6 return $data;
187 }
188  
189 1 ▶sub _write_file {
Subroutine _write_file: called CC=2 SCAR=7.0
190 1 my ($filename, $data) = @_;
191 1 local *FH;
192 1 ▶ open(FH, ">$filename") or die("Unable to open $filename - $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$filename" or die "Unable to open $filename - $!" 1 0 1
193 1 binmode FH;
194 1 print FH $data;
195 1 close FH;
196 }
197  
198  
199 #Log::Trace stubs
200 73 ▶sub TRACE{}
Subroutine TRACE: called CC=1 SCAR=0.0
Pod undocumented
201 0 ▶sub DUMP{}
Subroutine DUMP: not called CC=1 SCAR=0.0
Pod undocumented
202  
203 2 1;
204  
205 =head1 NAME
206  
207 Sub::Slice::Backend::Filesystem - Default backend for Sub::Slice
208  
209 =head1 SYNOPSIS
210  
211 See L<Sub::Slice::Backend>.
212  
213 =head1 DESCRIPTION
214  
215 Implementation of the Sub::Slice::Backend API using Filesystem & Storable.
216 See L<Sub::Slice> and L<Sub::Slice::Backend> for more information.
217  
218 Data is stored in one directory per job corresponding to the unique job ID.
219 Within this directory there is a single storable file containing the job data and possibly other uniquely-named files
220 containing BLOB data. The mapping of key to unique filename for BLOBs is stored within the job.
221  
222 =head1 STORAGE OPTIONS
223  
224 =over 4
225  
226 =item path
227  
228 The directory in which Sub::Slice tokens are stored. Default is File::Spec::Functions::tmpdir()."/sub_slice". Sub::Slice will create that directory if it
229 does not exist already.
230  
231 NB. Beware of running Sub::Slice under multiple users using the default
232 path. Unless you are careful with umask settings, you may create a
233 directory that only some Sub::Slice users can write to.
234  
235 =item prefix
236  
237 Prefix for all IDs generated by the module. Default is "Sub__Slice__".
238  
239 =item unique_key_length
240  
241 Length of the unique part of the key. Default is 12 characters.
242  
243 =item job_filename
244  
245 Filename containing the job data. The default is "sub_slice_job.store".
246  
247 =item lax
248  
249 Relaxes the check that enforces that job ids match the prefix and unique key length specified in the constructor.
250 This normally prevents you loading a valid Sub::Slice token from another application if 2 applications
251 share the same $path but use a different prefix.
252  
253 =back
254  
255 =head1 TODO
256  
257 =over 4
258  
259 =item locking functionality
260  
261 This may be added in a future version and should default to something reasonably safe (ie. only one process should be able to work on a job at any point in time)
262  
263 =back
264
265 =head1 VERSION
266  
267 $Revision: 1.13 $ on $Date: 2005/01/12 16:51:19 $ by $Author: simonf $
268  
269 =head1 AUTHOR
270  
271 John Alden and Simon Flack <cpan _at_ bbc _dot_ co _dot_ uk>
272  
273 =head1 COPYRIGHT
274  
275 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
276  
277 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
278  
279 =cut
280  
« blib/lib/Sub/Slice.pm ↑ Summary