| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 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 | 81 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 81 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 81 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 81 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 81 | require Rex::Commands; | |||||||||||||||||||||||||||
| 33 | 81 | require Rex::Commands::Run; | |||||||||||||||||||||||||||
| 34 | 81 | ▶ | use Rex::Interface::Exec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 81 | ▶ | use Rex::Interface::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 81 | ▶ | use Rex::Interface::Fs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 81 | ▶ | use Rex::Helper::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 81 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 81 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 81 | require Rex::Exporter; | |||||||||||||||||||||||||||
| 42 | 81 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 81 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 81 | @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 | 127 | ▶ | sub md5 { | ||||||||||||||||||||||||||
|
Subroutine
md5: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 127 | my ($file) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 127 | my $fs = Rex::Interface::Fs->create; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 127 | ▶ | if ( $fs->is_file($file) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 112 | Rex::Logger::debug("Calculating checksum (MD5) of $file"); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 112 | ▶ | my $md5 = _digest_md5($file) // _binary_md5($file); | ||||||||||||||||||||||||||
|
Condition33%1/3
_digest_md5($file) // _binary_md5($file)
MC/DC0%0/2
_digest_md5($file) // _binary_md5($file)
_digest_md5($file)
_binary_md5($file)
Truth table33%1/3
A // B
A= _digest_md5($file)B= _binary_md5($file)
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 112 | ▶ | if ( !$md5 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $message = "Unable to get MD5 checksum of $file: $!"; | |||||||||||||||||||||||||||
| 69 | 0 | Rex::Logger::info($message); | |||||||||||||||||||||||||||
| 70 | 0 | die($message); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 112 | Rex::Logger::debug("MD5 checksum of $file: $md5"); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 112 | return $md5; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 15 | my $message = "File not found: $file"; | |||||||||||||||||||||||||||
| 79 | 15 | Rex::Logger::debug($message); | |||||||||||||||||||||||||||
| 80 | 15 | die($message); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 113 | ▶ | sub _digest_md5 { | ||||||||||||||||||||||||||
|
Subroutine
_digest_md5: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||
| 85 | 113 | my $file = shift; | |||||||||||||||||||||||||||
| 86 | 113 | my $md5; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 113 | ▶ | my $perl = Rex::is_local() ? $EXECUTABLE_NAME : 'perl'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 113 | ▶ | my $command = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O =~ /^MSWin/iu and Rex::is_local()
MC/DC0%0/2
$^O =~ /^MSWin/iu and Rex::is_local()
$^O =~ /^MSWin/iu
Rex::is_local()
Truth table33%1/3
A and B
A= $^O =~ /^MSWin/iuB= Rex::is_local()
| |||||||||||||||||||||||||||||
| 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 | my $result = i_run( $command, fail_ok => 1 ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 113 | ▶ | if ( $? == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 113 | $md5 = $result; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 113 | return $md5; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | sub _binary_md5 { | ||||||||||||||||||||||||||
|
Subroutine
_binary_md5: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 105 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 106 | 1 | my $md5; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | my $exec = Rex::Interface::Exec->create; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | ▶ | if ( Rex::Commands::Run::can_run('md5') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | 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 | ($md5) = $exec->exec("md5sum '$file'") =~ qr{^\\?([a-f0-9]{32})\s+}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | return $md5; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 81 | 1; | |||||||||||||||||||||||||||