File Coverage

lib/Rex/Commands/MD5.pm
Criterion Covered Total %
statement 57 61 93.4
branch 8 14 57.1
condition 2 6 33.3
subroutine 14 14 100.0
pod 1 1 100.0
total 82 96 85.4


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::MD5 - Calculate MD5 sum of files
8              
9             =head1 DESCRIPTION
10              
11             With this module you calculate the md5 sum of a file.
12              
13             This is just a helper function and will not be reported.
14              
15             =head1 SYNOPSIS
16              
17             use Rex::Commands::MD5;
18             my $md5 = md5($file);
19              
20             =head1 EXPORTED FUNCTIONS
21              
22             =cut
23              
24             package Rex::Commands::MD5;
25              
26 78     78   75194 use v5.12.5;
  78         287  
27 78     78   419 use warnings;
  78         166  
  78         3491  
28              
29             our $VERSION = '1.14.3'; # VERSION
30              
31 78     78   838 use Rex::Logger;
  78         159  
  78         1845  
32             require Rex::Commands;
33             require Rex::Commands::Run;
34 78     78   3472 use Rex::Interface::Exec;
  78         227  
  78         1013  
35 78     78   1887 use Rex::Interface::File;
  78         205  
  78         476  
36 78     78   2228 use Rex::Interface::Fs;
  78         190  
  78         543  
37 78     78   2059 use Rex::Helper::Path;
  78         201  
  78         4861  
38 78     78   520 use Rex::Helper::Run;
  78         191  
  78         5501  
39 78     78   537 use English qw(-no_match_vars);
  78         187  
  78         846  
40              
41             require Rex::Exporter;
42 78     78   30921 use base qw(Rex::Exporter);
  78         174  
  78         6402  
43 78     78   544 use vars qw(@EXPORT);
  78         295  
  78         43996  
44              
45             @EXPORT = qw(md5);
46              
47             =head2 md5($file)
48              
49             This function will return the MD5 checksum (hexadecimal) for the given file.
50              
51             task "checksum", "server01", sub {
52             say md5("/etc/passwd");
53             };
54              
55             =cut
56              
57             sub md5 {
58 127     127 1 1808 my ($file) = @_;
59              
60 127         2421 my $fs = Rex::Interface::Fs->create;
61              
62 127 100       1300 if ( $fs->is_file($file) ) {
63 112         1265 Rex::Logger::debug("Calculating checksum (MD5) of $file");
64              
65 112   33     553 my $md5 = _digest_md5($file) // _binary_md5($file);
66              
67 112 50       549 if ( !$md5 ) {
68 0         0 my $message = "Unable to get MD5 checksum of $file: $!";
69 0         0 Rex::Logger::info($message);
70 0         0 die($message);
71             }
72              
73 112         1182 Rex::Logger::debug("MD5 checksum of $file: $md5");
74              
75 112         2567 return $md5;
76             }
77             else {
78 15         110 my $message = "File not found: $file";
79 15         98 Rex::Logger::debug($message);
80 15         254 die($message);
81             }
82             }
83              
84             sub _digest_md5 {
85 113     113   308 my $file = shift;
86 113         210 my $md5;
87              
88 113 50       441 my $perl = Rex::is_local() ? $EXECUTABLE_NAME : 'perl';
89              
90 113 50 33     2277 my $command =
91             ( $^O =~ m/^MSWin/i && Rex::is_local() )
92             ? qq("$perl" -MDigest::MD5 -e "open my \$fh, '<', \$ARGV[0] or die 'Cannot open ' . \$ARGV[0]; binmode \$fh; print Digest::MD5->new->addfile(\$fh)->hexdigest;" "$file")
93             : qq('$perl' -MDigest::MD5 -e 'open my \$fh, "<", \$ARGV[0] or die "Cannot open " . \$ARGV[0]; binmode \$fh; print Digest::MD5->new->addfile(\$fh)->hexdigest;' '$file');
94              
95 113         1033 my $result = i_run( $command, fail_ok => 1 );
96              
97 113 50       981 if ( $? == 0 ) {
98 113         405 $md5 = $result;
99             }
100              
101 113         1184 return $md5;
102             }
103              
104             sub _binary_md5 {
105 1     1   9 my $file = shift;
106 1         3 my $md5;
107              
108 1         23 my $exec = Rex::Interface::Exec->create;
109              
110 1 50       26 if ( Rex::Commands::Run::can_run('md5') ) {
    50          
111 0         0 ($md5) = $exec->exec("md5 '$file'") =~ qr{\s+=\s+([a-f0-9]{32})\s*$};
112             }
113             elsif ( Rex::Commands::Run::can_run('md5sum') ) {
114 1         9 ($md5) = $exec->exec("md5sum '$file'") =~ qr{^\\?([a-f0-9]{32})\s+};
115             }
116              
117 1         36 return $md5;
118             }
119              
120             1;