File Coverage

lib/Rex/Hook/File/Impostor.pm
Criterion Covered Total %
statement 44 44 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 0 3 0.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Rex::Hook::File::Impostor;
2              
3             # ABSTRACT: execute Rex file management commands on a copy of the managed path
4              
5 2     2   248988 use 5.012;
  2         20  
6 2     2   11 use warnings;
  2         5  
  2         62  
7              
8 2     2   11 use Digest::MD5;
  2         4  
  2         97  
9 2     2   12 use English qw( -no_match_vars );
  2         3  
  2         18  
10 2     2   793 use File::Basename;
  2         4  
  2         165  
11 2     2   13 use File::Spec;
  2         5  
  2         57  
12 2     2   661 use Rex 1.013004 -base;
  2         63840  
  2         18  
13 2     2   555481 use Rex::Hook;
  2         6  
  2         134  
14 2     2   44 use Sys::Hostname;
  2         4518  
  2         881  
15              
16             our $VERSION = 'v0.2.0';
17              
18             register_function_hooks { before => { file => \&impostor_hook, }, };
19              
20             sub impostor_hook {
21 5     5 0 214451 my ( $managed_path, @opts ) = @_;
22              
23 5         164 my $impostor_path = get_impostor_for($managed_path);
24              
25 5         842 mkdir dirname($impostor_path);
26              
27 5 100       100146 if ( is_file($managed_path) ) {
28 3         933 Rex::Logger::debug("Copying $managed_path to $impostor_path");
29 3         65 cp $managed_path, $impostor_path;
30             }
31              
32 5         57822 return $impostor_path, @opts;
33             }
34              
35             sub get_impostor_for {
36 7     7 0 5406 my $path = shift;
37              
38 7         734 my ( $volume, $directories, $file ) = File::Spec->splitpath($path);
39 7         78 $volume =~ s/://gmsx;
40              
41 7         93 return File::Spec->join( get_impostor_directory(), $volume, $directories,
42             $file );
43             }
44              
45             sub get_impostor_directory {
46 9     9 0 8368 my $hasher = Digest::MD5->new();
47              
48 9         155 $hasher->add(hostname);
49 9         329 $hasher->add($PID);
50              
51 9         127 my $unique_id = $hasher->hexdigest();
52              
53 9         335 my $impostor_directory = File::Spec->join( Rex::Config->get_tmp_dir(),
54             'rex_hook_file_impostor', $unique_id );
55              
56 9         4869 mkdir $impostor_directory;
57 9         236120 return $impostor_directory;
58             }
59              
60             1;
61              
62             __END__