blib/lib/Cache/Repository.pm

filter: Statementstmt 51.3% 78 / 152 Branchbran 43.3% 26 / 60 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 45.0% 9 / 20 Podpod 100% 13 / 13 totaltotal 49.8% 133 / 267 CC 37 SCAR 27.3
CC
37
Cov
49%
__ANON__
56.0
add_files
21.3
retrieve
6.0
CRAP 15.3
↑ Summary blib/lib/Cache/Repository/Filesys.pm »
1 package Cache::Repository;
2  
3 1 our $VERSION = '0.06';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Cache::Repository - Generic repository of files
13  
14 =head1 SYNOPSIS
15  
16 my $rep = Cache::Repository->new(
17 style => 'Filesys',
18 # options for the F::R driver
19 );
20 $rep->add_files(tag => 'groupname',
21 files => \@filenames,
22 basedir => '/tmp',
23 move => 1,
24 );
25 $rep->add_filehandle(tag => 'anothergroup',
26 filename => 'blah',
27 filehandle => $fh,
28 mode => 0755);
29 $rep->set_meta(tag => 'groupname',
30 meta => {
31 title => 'blah',
32 author => 'foo',
33 });
34  
35 $rep->retrieve(tag => 'groupname', dest => '/newdir');
36 my $data = $rep->get_meta(tag => 'groupname');
37  
38 =head1 DESCRIPTION
39  
40 This module is intended to serve as a repository for files, whether those
41 files are local or remote. Different drivers can work independantly to
42 provide differing backing stores. For example, one driver can use a
43 locally-mounted filesystem (even if that is a network filesystem), another
44 could use FTP or HTTP, another could use gmail, and another could use a
45 relational database such as MySQL or DB2.
46  
47 Drivers may choose to compress the repository, unless explicitly told
48 otherwise.
49  
50 Keeping this in mind, the API presented here cannot expose things that are
51 not generic to other possible implementations. That said, some possible
52 implementations may not allow adding ("sending" to a web server) - it is
53 expected that they will either throw an exception, or take extra params for
54 FTP'ing to the server.
55  
56 =head1 FUNCTIONS
57  
58 =over 4
59  
60 =item new
61  
62 Cache::Repository constructor. The constructor will load the driver and
63 return an object of the driver package. All other parameters will be passed
64 to the driver for initialisation.
65  
66 my $r = Cache::Repository->new(
67 style => 'Filesys',
68 # ...
69 );
70  
71 It is up to the underlying driver to determine if the repository created
72 by this is persistant for other processes (e.g., meta-data or even data stored
73 in RAM wouldn't be persistant), or to handle locking issues should multiple
74 processes be accessing the repository simultaneously.
75  
76 Parameters:
77  
78 =over 4
79  
80 =item style
81  
82 This is the name of the driver. The driver is expected to be
83 Cache::Compress::I<style>, e.g., Cache::Compress::Filesys
84  
85 =item (others)
86  
87 As required by the underlying driver.
88  
89 =back
90  
91 Suggested parameters for drivers:
92  
93 =over 4
94  
95 =item clear
96  
97 If true, clear the repository (if it exists) to start anew. Existing files
98 and meta information will all be removed.
99  
100 =item compress
101  
102 If true, the driver should compress the files and/or meta information if it is
103 able to, and if it is capable of doing so (drivers do not need to implement
104 this.) True values may include:
105  
106 =over 4
107  
108 =item C<Z> or C<compress>
109  
110 Compress with the standard compress format.
111  
112 =item C<gz> or C<gzip>
113  
114 Compress with gzip-compatable format.
115  
116 =item C<zip>
117  
118 Compress with InfoZip-compatable format.
119  
120 =item C<bz> or C<bzip2>
121  
122 Compress with bzip2-compatable format.
123  
124 =item any other truth value
125  
126 Compress with any format the driver wishes.
127  
128 =back
129  
130 If the chosen compression format cannot be acheived, the driver may choose
131 another format, or choose to not compress.
132  
133 If false, the driver should not compress the files, even if it can.
134  
135 If unset, the driver may compress or not, as the driver desires. Usually
136 this is the best option for the user since usually whether the repository
137 is compressed or not should not be important. Also, the format of the
138 compression is unimportant.
139  
140 =back
141  
142 Returns: The Cache::Repository-derived object, or undef if the driver failed
143 to initialise.
144  
145 Alternately, you can instantiate the driver directly, e.g.,
146  
147 my $r = Cache::Repository::Filesys->new(%options);
148  
149 =cut
150  
151 sub new
152 {
153 1 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.7
Pod documented
154 1 ▶ $class = ref $class || $class || croak "No class given";
Condition33%2/6
ref $class || $class || croak('No class given')
l!l&&r!l&&!r
100
ref $class or $class
l!l&&r!l&&!r
010
MC/DC0%0/3
ref $class || $class || croak('No class given')
ref $class $class croak('No class given')
Truth table25%1/4
A or B || C
A= ref $classB= $classC= croak('No class given')
ABCresult
1XX1
01X1
0011
0000
155 1 my %opts = @_;
156  
157 1 ▶ if ($class eq __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 1 0 1
158 {
159 1 my $style = delete $opts{style};
160 1 $class .= '::' . $style;
161 1 (my $pm = $class) =~ s[::][/]g;
162 1 $pm .= '.pm';
163 1 require $pm;
164 }
165  
166 1 $class->new(%opts);
167 }
168  
169 =item clear_tag
170  
171 Clears a tag completely from the repository. This includes files and meta
172 information.
173  
174 Parameters:
175  
176 =over 4
177  
178 =item tag
179  
180 The tag to be cleared.
181  
182 =back
183  
184 =cut
185  
186 sub clear_tag
187 {
188 0 ▶ my $self = shift;
Subroutine clear_tag: not called CC=1 SCAR=6.9
Pod documented
189 0 my %opts = @_;
190  
191 0 die "Driver must override clear_tag";
192 }
193  
194 =item add_symlink
195  
196 Adds a symlink to the repository. Note that on systems that do not understand
197 symlinks, this may not actually work. Even if the storage allows it,
198 retrieving a symlink may not do what is expected.
199  
200 Parameters:
201  
202 =over 4
203  
204 =item tag
205  
206 Mandatory identifier for the group of files. If the tag already exists,
207 any files will be added to the tag by default.
208  
209 =item filename
210  
211 Filename that is the symlink
212  
213 =item target
214  
215 The target that the symlink points at. The target need not actually exist -
216 dangling symlinks should work fine.
217  
218 =back
219  
220 =cut
221  
222 sub add_symlink
223 {
224 0 ▶ my $self = shift;
Subroutine add_symlink: not called CC=1 SCAR=6.9
Pod documented
225 0 my %opts = @_;
226  
227 0 die "Driver must provide add_symlink";
228 }
229  
230 =item add_files
231  
232 Adds files to the repository.
233  
234 Parameters:
235  
236 =over 4
237  
238 =item tag
239  
240 Mandatory identifier for the group of files. If the tag already exists,
241 any files will be added to the tag by default.
242  
243 =item files
244  
245 This can be either a single filename, or an array ref of filenames to add.
246 The filenames may include paths, but may not include the equivalent of
247 C<File::Spec-E<gt>updir> in any file. This is largely to keep files
248 from going out of the "current" directory and into parent or sibling
249 directories.
250  
251 =item basedir
252  
253 Where to look for files listed in the C<files> parameter. Default is the
254 current working directory.
255  
256 =item filename_conversion
257  
258 This is a multi-pronged tool which is intended to allow the user to rename
259 files on the way in to the repository. The default is to leave the filenames
260 unchanged.
261  
262 This option may be:
263  
264 =over 4
265  
266 =item a single CODE ref
267  
268 In this case, the code ref should modify $_ to become the new
269 file name. Usually this will be something like:
270  
271 filename_conversion => sub { s!(path)/([^/.]*)!$2/$1! }
272  
273 =item a single or array ref of filenames
274  
275 This works just like the files option. If the list of files passed in
276 to this parameter is not of the same length as the list of files, then an
277 exception is thrown. If a given filename is undef, the filename is left
278 unchanged. For example:
279  
280 files => qw(blah foo bar/baz),
281 filename_conversion => (undef, qw(floo/foo bar/blah))
282  
283 This will read from a file named C<blah>, and put it into the repository
284 without modifying the name. It will read C<foo> from the current directory
285 (or the C<basedir> if specified) and put it into the repository in the
286 C<floo> directory. And it will read C<bar/baz> and put it in the repository
287 as C<bar/blah>.
288  
289 =back
290  
291 =item move
292  
293 If set to true, will remove the file after placing it in the repository.
294 Can also be used for optimisation for a filesystem repository on the same
295 partition.
296  
297 =back
298  
299 Returns: true if all files were added succesfully, false otherwise.
300  
301 =cut
302  
303 # Default behaviour - convert into add_filehandle calls.
304 # Many drivers may find it more efficient to override this directly.
305 sub add_files
306 {
307 3 ▶ my $self = shift;
Subroutine add_files: called CC=16 SCAR=30.6
Pod documented
308 3 my %opts = @_;
309  
310 # We may have a single filename, or an array ref of filenames.
311 my @files =
312 3 ▶ ref $opts{files} ? @{$opts{files}} : $opts{files};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'files'} ? : 0 3 3
313  
314 3 my @renames;
315 my $rename_sub;
316 3 ▶ if (exists $opts{filename_conversion})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"filename_conversion"}) 2 1 3
317 {
318 2 ▶ if (ref $opts{filename_conversion} and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'filename_conversion'} and ref $opts{'filename_conversion'} eq 'CODE') { } 1 1 2
Condition66%2/3
ref $opts{"filename_conversion"} and ref $opts{"filename_conversion"} eq "CODE"
!ll&&!rl&&r
011
MC/DC50%1/2
ref $opts{"filename_conversion"} and ref $opts{"filename_conversion"} eq "CODE"
ref $opts{"filename_conversion"} ref $opts{"filename_conversion"} eq "CODE"
Truth table66%2/3
A and B
A= ref $opts{"filename_conversion"}B= ref $opts{"filename_conversion"} eq "CODE"
ABresult
0X0
100
111
319 ref $opts{filename_conversion} eq 'CODE')
320 {
321 1 $rename_sub = $opts{filename_conversion};
322 }
323 else
324 {
325 @renames =
326 1 ▶ ref $opts{filename_conversion} ? @{$opts{filename_conversion}} : $opts{filename_conversion};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'filename_conversion'} ? : 1 0 1
327 1 ▶ die "filename_conversion is not as long as files"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @files == scalar @renames 1 0 1
328 unless scalar @files == scalar @renames;
329 }
330 }
331  
332 2 require File::stat;
333 2 foreach my $f (@files)
334 {
335 2 my $fullname = $f;
336 2 ▶ if ($opts{basedir})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"basedir"}) 2 0 2
337 {
338 2 $fullname = File::Spec->catfile($opts{basedir}, $fullname);
339 }
340 2 my $repositoryname = $f;
341 2 ▶ if (@renames)
Branch: 2 branches
BranchTrueFalseTotal
if (@renames) { } 0 2 2
elsif ($rename_sub) { } 1 1 2
342 {
343 0 $repositoryname = shift @renames;
344 }
345 elsif ($rename_sub)
346 {
347 1 local $_ = $repositoryname;
348 1 $rename_sub->();
349 1 $repositoryname = $_;
350 }
351  
352 2 ▶ if (-l $fullname)
Branch: 1 branch
BranchTrueFalseTotal
if (-l $fullname) { } 0 2 2
353 {
354 $self->add_symlink(
355 tag => $opts{tag},
356 0 ▶ filename => $repositoryname,
Branch: 1 branch
BranchTrueFalseTotal
$self->add_symlink("tag", $opts{"tag"}, "filename", $repositoryname, "target", readlink $fullname) or return 0 0 0 0
357 target => readlink($fullname),
358 ) or return 0;
359 }
360 else
361 {
362 2 my $s = File::stat::stat($fullname);
363 2 ▶ my $fh = IO::File->new($fullname, 'r') or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($fullname, "r") or do { warn "Can't open ${fullname}: $!"; return 0 } 0 2 2
364 0 warn "Can't open $fullname: $!";
365 0 return 0;
366 };
367 2 binmode $fh;
368 2 my %file_opts = (
369 filename => $repositoryname,
370 filehandle => $fh,
371 mode => $s->mode(),
372 owner => $s->uid(),
373 group => $s->gid(),
374 );
375 2 ▶ $self->add_filehandle(tag => $opts{tag}, %file_opts) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->add_filehandle("tag", $opts{"tag"}, %file_opts) or return 0 0 2 2
376 }
377 2 ▶ unlink($f) if $opts{move};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"move"} 0 2 2
378 }
379 2 1;
380 }
381  
382 =item add_filehandle
383  
384 Adds a file to the repository.
385  
386 Parameters:
387  
388 =over 4
389  
390 =item tag
391  
392 Mandatory identifier for the group of files. If the tag already exists,
393 any files will be added to the tag by default.
394  
395 =item filehandle
396  
397 You can pipe your data directly into
398 the repository. This filehandle can be any perl-ish filehandle object:
399 a GLOB, an IO::Handle (including an IO::String), or anything else that works
400 like a file handle to be read from. Note that perl can open from a string
401 reference in v5.8, so that is viable as well.
402  
403 The filehandle will be read from, and the data written directly to the
404 repository, and should be done in a loop such that the entire file need
405 not be brought into memory. For example, during an FTP transfer, the
406 filehandle will be read so that it can be put directly to the server.
407  
408 The filename that is used is the C<filename> parameter.
409  
410 Note that only one filehandle can be added at a time.
411  
412 =item filename
413  
414 The filename for the filehandle. Again, this filename may include
415 subdirectories, but cannot be an absolute path nor include the updir
416 string.
417  
418 =item mode
419  
420 Attributes for the file. Normally these would be read directly from the input
421 file, but cannot be read from a filehandle, so this will need to be provided.
422  
423 =item owner
424  
425 The UID for the owner of the file. Note that without root authority,
426 this may fail. Default is the file's owner, or the current user if the source
427 is a filehandle.
428  
429 =item group
430  
431 The GID for the owner of the file. Note that without root authority,
432 this may fail. Default is the file's owner, or the current group if the source
433 is a filehandle.
434  
435 =back
436  
437 Returns: true if the repository was successfully added.
438  
439 =cut
440  
441 sub add_filehandle
442 {
443 0 ▶ die __PACKAGE__ . " driver must implement add_filehandle."
Subroutine add_filehandle: not called CC=1 SCAR=6.9
Pod documented
444 }
445  
446 =item retrieve
447  
448 Retrieves all the files associated with the given tag to the location
449 specified.
450  
451 Parameters:
452  
453 =over 4
454  
455 =item tag
456  
457 Required. The tag to retrieve.
458  
459 =item basedir
460  
461 The location to place the file(s). Note that any files that were placed
462 into the repository with subdirectories will be placed in a subdirectory
463 relative to this basedir.
464  
465 =item files
466  
467 The list of files to be retrieved. Defaults to all files. This parameter
468 may be a simple scalar, or an array ref, e.g.,
469  
470 files => 'foo.txt'
471  
472 or
473  
474 files => [ 'foo.txt' ]
475  
476 are both the same.
477  
478 =back
479  
480 Returns undef if the tag doesn't exist, or 0 for any other retrieval error.
481  
482 =cut
483  
484 sub retrieve
485 {
486 0 ▶ my $self = shift;
Subroutine retrieve: not called CC=2 SCAR=17.9
Pod documented
487 0 my %opts = @_;
488  
489 0 my $rc = 1;
490 0 my $fh = undef;
491 0 my $filename;
492  
493 my $callback = sub {
494 0 ▶ my %cb_opts = @_;
Subroutine __ANON__: not called CC=7 SCAR=40.3
495 # filename, data, start, end, error
496  
497 0 ▶ if ($cb_opts{error})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"error"}) 0 0 0
498 {
499 0 $rc = 0;
500 0 warn $cb_opts{error};
501 0 return 0;
502 }
503  
504 0 ▶ if ($cb_opts{target})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"target"}) 0 0 0
505 {
506 0 $filename = File::Spec->catfile($opts{basedir},$cb_opts{filename});
507 0 symlink($cb_opts{target}, $filename);
508 0 return 1;
509 }
510  
511 0 ▶ if ($cb_opts{start})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"start"}) 0 0 0
512 {
513 0 require File::Path;
514 0 require File::Basename;
515  
516 0 $filename = File::Spec->catfile($opts{basedir},$cb_opts{filename});
517 0 File::Path::mkpath(File::Basename::dirname($filename));
518  
519 0 ▶ $fh = IO::File->new($filename, "w") or do {
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($filename, "w") or do { warn "Can't write to ${filename}: $!"; $rc = 0; return 0 } 0 0 0
520 0 warn "Can't write to $filename: $!";
521 0 $rc = 0;
522 0 return 0;
523 };
524 0 binmode $fh;
525 }
526  
527 0 ▶ $fh->print($cb_opts{data}) if defined $cb_opts{data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb_opts{"data"} 0 0 0
528  
529 0 ▶ if ($cb_opts{end})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"end"}) 0 0 0
530 {
531 0 $fh->close();
532 0 $fh = undef;
533  
534 0 chmod $cb_opts{mode}, $filename;
535 0 chown $cb_opts{owner}, $cb_opts{group}, $filename;
536 }
537 0 1;
538 0 };
539  
540 0 ▶ return undef unless $self->retrieve_with_callback(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->retrieve_with_callback(%opts, "callback", $callback) 0 0 0
541 %opts,
542 callback => $callback,
543 );
544  
545 0 $rc;
546 }
547  
548 =item retrieve_as_hash
549  
550 Retrieves all the files associated with the given tag into memory. The
551 hash (or hash-ref in scalar context) is returned. To use a specific hash,
552 pass in a ref to it.
553  
554 Keys to the hash are the filenames. The values are hashes with keys of:
555 C<content> (the file contents), C<mode> (file mode), C<owner> (UID for the
556 file), and C<group> (GID for the file) if the filename is a real file,
557 and a key of C<target> if the file is a symlink.
558  
559 Parameters:
560  
561 =over 4
562  
563 =item tag
564  
565 Required. The tag to retrieve.
566  
567 =item hash
568  
569 If this parameter is specified, this hash ref will be used instead of creating
570 a new hash ref. For example:
571  
572 my %files;
573 my $ref = $rep->retrieve(tag => 'groupname', hash => \%files);
574 # \%files == $ref
575  
576 =item files
577  
578 The list of files to be retrieved. Defaults to all files. This parameter
579 may be a simple scalar, or an array ref, e.g.,
580  
581 files => 'foo.txt'
582  
583 or
584  
585 files => [ 'foo.txt' ]
586  
587 are both the same.
588  
589 =back
590  
591 Returns undef if the retrieval failed.
592  
593 =cut
594  
595 sub retrieve_as_hash
596 {
597 2 ▶ my $self = shift;
Subroutine retrieve_as_hash: called CC=3 SCAR=11.6
Pod documented
598 2 my %opts = @_;
599  
600 2 ▶ my $hash = $opts{hash} || {};
Condition50%1/2
$opts{'hash'} || {}
l!l
02
MC/DC0%0/1
$opts{'hash'} || {}
$opts{'hash'}
Truth table50%1/2
A
A= $opts{'hash'}
Aresult
11
00
601  
602 my $callback = sub {
603 6 ▶ my %cb_opts = @_;
Subroutine __ANON__: called CC=5 SCAR=17.4
604 # filename, data, start, end, error
605  
606 6 ▶ if ($cb_opts{error})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"error"}) 0 6 6
607 {
608 0 $hash = undef;
609 0 warn $cb_opts{error};
610 0 return 0;
611 }
612  
613 6 ▶ if ($cb_opts{target})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"target"}) 0 6 6
614 {
615 0 $hash->{$cb_opts{filename}}{target} = $cb_opts{target};
616 0 return 1;
617 }
618  
619 6 ▶ if ($cb_opts{start})
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_opts{"start"}) 3 3 6
620 {
621 3 my @keys = qw[mode owner group];
622 3 my %h;
623 3 @h{@keys} = @cb_opts{@keys};
624 3 $hash->{$cb_opts{filename}} = \%h;
625 }
626  
627 6 ▶ $hash->{$cb_opts{filename}}{data} .= $cb_opts{data} if $cb_opts{data};
Branch: 1 branch
BranchTrueFalseTotal
if $cb_opts{"data"} 3 3 6
628 6 1;
629 2 };
630  
631 2 ▶ return undef unless $self->retrieve_with_callback(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->retrieve_with_callback(%opts, "callback", $callback) 0 2 2
632 %opts,
633 callback => $callback,
634 );
635  
636 2 $hash;
637  
638 }
639  
640 =item retrieve_with_callback
641  
642 Retrieves each file associated with the given tag by calling back to the
643 specified function.
644  
645 Parameters:
646  
647 =over 4
648  
649 =item tag
650  
651 Required. The tag to retrieve.
652  
653 =item callback
654  
655 This parameter specifies a code ref which will be called for each file.
656 The code ref will be given the following parameters on each call. The
657 code may be called more than once per file if the file is being retrieved
658 in chunks.
659  
660 =over 4
661  
662 =item filename
663  
664 The name of the current file.
665  
666 =item data
667  
668 The contents of the file, or the current chunk of contents of the file.
669 May be empty if the previous call happened to contain the end of the file.
670  
671 =item owner
672  
673 =item group
674  
675 =item mode
676  
677 The owner, group, and mode of the file.
678  
679 =item start
680  
681 True if this is the first call for this file.
682  
683 =item end
684  
685 True if this is the last call for this file. Note that start and end may
686 both be set to true if data contains the entire file. Also note that
687 data may be empty if the previous chunk turned out to be the end of the
688 file.
689  
690 =item target
691  
692 The symlink target if the current file is a symlink. Note that if the
693 storage supports this, but the current filesystem does no, it is up to the
694 callback routine to figure out what to do.
695  
696 =item error
697  
698 If an error happened during retrieval, this will be the driver-defined error
699 (string or number).
700  
701 =back
702  
703 If the callback returns true, processing will continue, false will abort
704 the rest of the retrieval.
705  
706 =item files
707  
708 The list of files to be retrieved. Defaults to all files. This parameter
709 may be a simple scalar, or an array ref, e.g.,
710  
711 files => 'foo.txt'
712  
713 or
714  
715 files => [ 'foo.txt' ]
716  
717 are both the same.
718  
719 =back
720  
721 Returns undef if the tag doesn't exist.
722  
723 =cut
724  
725 sub retrieve_with_callback
726 {
727 0 ▶ my $self = shift;
Subroutine retrieve_with_callback: not called CC=1 SCAR=6.9
Pod documented
728 0 my %opts = @_;
729 0 die "Driver must implement retrieve_with_callback";
730 }
731  
732 =item set_meta
733  
734 Sets some meta-information for the files. For example, storing sizes,
735 MD5s, or other information that your application needs about this group,
736 other than the files themselves.
737  
738 Parameters:
739  
740 =over 4
741  
742 =item tag
743  
744 Mandatory identifier for the group of files.
745  
746 =item meta
747  
748 A hash ref of meta information. This information will be I<added to> the
749 existing meta information. Key collisions will replace. This can be
750 thought of as:
751  
752 %meta = (%old_meta, %new_meta);
753  
754 =item reset
755  
756 If this flag is true, the given meta information will replace existing meta
757 information as in:
758  
759 %meta = %new_meta;
760  
761 completely discarding the old meta information.
762  
763 =back
764  
765 Returns: true if successful.
766  
767 Default implementation stores meta information in memory - this is fine for
768 single-process repositories that don't need to be persistant across
769 invocations, but a generic persistant implementation cannot be written outside
770 of the driver.
771  
772 =cut
773  
774 sub set_meta
775 {
776 0 ▶ my $self = shift;
Subroutine set_meta: not called CC=1 SCAR=6.9
Pod documented
777 0 my %opts = @_;
778  
779 0 my $old_meta = $self->get_meta(%opts);
780 0 my $new_meta = $opts{meta};
781  
782 0 @$old_meta{keys %$new_meta} = values %$new_meta;
783 0 $old_meta;
784 }
785  
786 =item get_meta
787  
788 Retrieves the meta information for a tag.
789  
790 Parameters:
791  
792 =over 4
793  
794 =item tag
795  
796 Mandatory identifier for the group of files.
797  
798 =back
799  
800 Returns: the meta information in hash-ref form.
801  
802 =cut
803  
804 sub get_meta
805 {
806 0 ▶ my $self = shift;
Subroutine get_meta: not called CC=2 SCAR=17.9
Pod documented
807 0 my %opts = @_;
808  
809 0 ▶ unless (exists $self->{meta}{$opts{tag}})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"meta"}{$opts{"tag"}}) 0 0 0
810 {
811 $self->{meta}{$opts{tag}} = {}
812 0 }
813 0 $self->{meta}{$opts{tag}};
814 }
815  
816 =item get_size
817  
818 Returns the total space requirements of the tag. Each file is rounded up
819 to the next K before adding together. If a file list is given, only those
820 files are counted.
821  
822 Symlinks are counted as 1K.
823  
824 Parameters:
825  
826 =over 4
827  
828 =item tag
829  
830 Mandatory identifier for the group of files.
831  
832 =item files
833  
834 Either a single file, or an array ref of files, which is the file or files
835 that should be considered part of the size total.
836  
837 =back
838  
839 =cut
840  
841 sub get_size
842 {
843 0 ▶ my $self = shift;
Subroutine get_size: not called CC=1 SCAR=6.9
Pod documented
844 0 my %opts = @_;
845  
846 0 die "Driver must implement get_size";
847 }
848  
849 =item list_files
850  
851 Returns a list of the files that are currently stored for the given tag.
852 Note that if compression is on, the filenames must be the uncompressed names.
853 If compression is using an archive format (such as C<zip>), this may be
854 a slow operation unless the driver stores the file list externally to the
855 archive, or each file is archived/compressed to a separate compressed archive.
856  
857 Parameters:
858  
859 =over 4
860  
861 =item tag
862  
863 Mandatory identifier for the group of files.
864  
865 =back
866  
867 Returns: An array in array context, array ref in scalar context.
868  
869 =cut
870  
871 sub list_files
872 {
873 0 ▶ my $self = shift;
Subroutine list_files: not called CC=1 SCAR=6.9
Pod documented
874 0 my %opts = @_;
875  
876 0 die "Driver must implement list_files";
877 }
878  
879 =item list_tags
880  
881 Returns a list of all in-use tags. Note that if there is more than one
882 process running from the same repository that by the time you get to use the
883 list, it may have changed (tags being added or removed).
884  
885 Parameters: None.
886  
887 Returns: An array in array context, array ref in scalar context. The
888 order of tags returned is indetertiminate (may be in insert order, may be
889 alphabetical, may be pseudo-random). If a sort order is desired, it is up
890 to the caller to use meta-information on each tag on which to base a sort,
891 and to call sort itself.
892  
893 =cut
894  
895 sub list_tags
896 {
897 0 ▶ my $self = shift;
Subroutine list_tags: not called CC=1 SCAR=6.9
Pod documented
898 0 my %opts = @_;
899  
900 0 die "Driver must implement list_tags";
901 }
902  
903 =item _is_filename_ok
904  
905 Checks a filename to see if it is "safe". We are primarily concerned with
906 filenames that go up the tree to above the current directory, whether that is
907 absolute path or using the C<File::Spec-E<gt>updir>.
908  
909 Returns true if the filename is ok. Primarily used by the drivers.
910  
911 =cut
912  
913 sub _is_filename_ok
914 {
915 3 ▶ my $self = shift;
Subroutine _is_filename_ok: called CC=5 SCAR=17.0
916 3 my $name = shift;
917  
918 3 require File::Spec;
919  
920 # first check if the name is absolute. That is absolutely a no-go.
921 # check both for the current system, and unix-style.
922 return undef
923 3 ▶ if File::Spec->file_name_is_absolute($name) or
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($name) or $name =~ m[^/] 0 3 3
Condition33%1/3
"File::Spec"->file_name_is_absolute($name) or $name =~ m[^/]
l!l&&r!l&&!r
003
MC/DC0%0/2
"File::Spec"->file_name_is_absolute($name) or $name =~ m[^/]
"File::Spec"->file_name_is_absolute($name) $name =~ m[^/]
Truth table33%1/3
A or B
A= "File::Spec"->file_name_is_absolute($name)B= $name =~ m[^/]
ABresult
1X1
011
000
924 $name =~ m.^/.;
925  
926 # check for any updir's. Again, check unix-style explicitly as well.
927 3 my ($volume, $path, $file) = File::Spec->splitpath($name);
928 3 my @split = File::Spec->splitdir($path);
929 3 my $updir = File::Spec->updir();
930 return undef
931 3 ▶ if grep { $_ eq $updir or $_ eq '..' } @split;
Branch: 2 branches
BranchTrueFalseTotal
if grep {$_ eq $updir or $_ eq "..";} @split 0 3 3
$_ eq $updir or $_ eq ".." 4 0 4
932  
933 3 1;
934 }
935  
936 =back
937  
938 =head1 AUTHOR
939  
940 Darin McBride - dmcbride@cpan.org
941  
942 =head1 COPYRIGHT
943  
944 Copyright 2006 Darin McBride.
945  
946 You may distribute under the terms of either the GNU General Public
947 License or the Artistic License, as specified in the Perl README file.
948  
949 =head1 BUGS
950  
951 See TODO file.
952  
953 =head1 SEE ALSO
954  
955 L<http://www.perlmonks.org/index.pl?node_id=589209>
956  
957 =cut
958  
959 1 1;
↑ Summary blib/lib/Cache/Repository/Filesys.pm »