File Coverage

blib/lib/File/ShareDir/Tarball.pm
Criterion Covered Total %
statement 27 34 79.4
branch 0 6 0.0
condition n/a
subroutine 9 10 90.0
pod 1 1 100.0
total 37 51 72.5


line stmt bran cond sub pod time code
1             package File::ShareDir::Tarball;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Deal transparently with shared files distributed as tarballs
4             $File::ShareDir::Tarball::VERSION = '0.3.0';
5              
6 2     2   421964 use strict;
  2         4  
  2         74  
7 2     2   10 use warnings;
  2         43  
  2         132  
8              
9 2     2   11 use parent qw/ Exporter /;
  2         3  
  2         14  
10              
11 2     2   127 use Carp;
  2         6  
  2         137  
12              
13 2     2   739 use File::ShareDir;
  2         27957  
  2         71  
14 2     2   1349 use Archive::Tar;
  2         192540  
  2         193  
15 2     2   1247 use File::Temp qw/ tempdir /;
  2         14358  
  2         171  
16 2     2   1244 use File::chdir;
  2         6460  
  2         249  
17 2     2   1528 use Memoize;
  2         5389  
  2         806  
18              
19             our @EXPORT_OK = qw{
20             dist_dir dist_file
21             };
22             our %EXPORT_TAGS = (
23             all => [ @EXPORT_OK ],
24             );
25              
26             my $shared_files_tarball = 'shared-files.tar.gz';
27              
28             # we don't want to extract the same dirs again and
29             # again within a single program
30             memoize('dist_dir');
31              
32             sub dist_dir {
33             my $dist = shift;
34              
35             my $dir = File::ShareDir::dist_dir($dist);
36              
37             # no tarball? Assume regular shared dir
38             return $dir unless -f "$dir/$shared_files_tarball";
39              
40             my $archive = Archive::Tar->new;
41             $archive->read("$dir/$shared_files_tarball");
42              
43             # because that would be a veeery bad idea
44             croak "archive '$shared_files_tarball' contains files with absolute path, aborting"
45             if grep { m#^/# } $archive->list_files;
46              
47             my $tmpdir = tempdir( CLEANUP => 1 );
48             local $CWD = $tmpdir;
49              
50             $archive->extract;
51              
52             return $tmpdir;
53             }
54              
55             sub dist_file {
56 0     0 1   my $dist = File::ShareDir::_DIST(shift);
57 0           my $file = File::ShareDir::_FILE(shift);
58              
59 0           my $path = dist_dir($dist).'/'.$file;
60              
61 0 0         return undef unless -e $path;
62              
63 0 0         croak("Found dist_file '$path', but not a file")
64             unless -f $path;
65              
66 0 0         croak("File '$path', no read permissions")
67             unless -r $path;
68              
69 0           return $path;
70             }
71              
72             1;
73              
74             __END__