blib/lib/MD5Check.pm

filter: Statementstmt 28.5% 18 / 63 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 3 totaltotal 18.3% 22 / 120 CC 23 SCAR 49.1
CC
23
Cov
17%
md5init
240.0
md5_sum
42.0
md5check
20.0
CRAP 135.7
↑ Summary
1 package MD5Check;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10 1 our @EXPORT = qw(md5init md5check);
11  
12 =head1 NAME
13  
14 MD5Check - Use it for init Web files's md5 values of your site(or other dir), and check if it changed!
15  
16 检查web目录(或者其他重要系统目录)md5值,当目录文件变化提醒。用于文件防篡改。
17  
18 =head1 VERSION
19  
20 Version 0.06
21  
22 =cut
23  
24 1 our $VERSION = '0.07';
25  
26 =head1 SYNOPSIS
27  
28 use MD5Check;
29  
30 ## 初始化目录md5值,参数为要监控的目录
31  
32 my $mydir=shift;
33 print md5init($mydir);
34  
35 ## 对目录文件进行检查,只需输入之前保存的md5 文件值。
36  
37 use MD5Check;
38 my $mydir=shift;
39 print md5check($mydir);
40  
41 # oneliner,perl单行程序实现功能。
42  
43 需要安装该模块,简单通过 cpanm MD5Check 安装。
44  
45 ## 相关实例可以直接使用bin/下的init.pl 和 check.pl 单行执行
46  
47 $ perl -MMD5Check -e 'print md5init("/web")' >file
48 $ perl -MMD5Check -e 'print md5check(file) perl -MMD5Check -e 'md5check(file)'..
49  
50  
51 =cut
52  
53 1 my $DEBUG = 0;
54  
55 sub md5_sum {
56  
57 0 ▶ my ( $file_name, $mode ) = @_;
Subroutine md5_sum: not called CC=6 SCAR=37.4
Pod undocumented
58 0 my ( $FD, $ctx, $md5 );
59 0 ▶ eval {open( $FD, $file_name ) or warn "Can't open $file_name !";
Branch: 1 branch
BranchTrueFalseTotal
open $FD, $file_name or warn "Can't open $file_name !" 0 0 0
60 #$ctx = Digest::MD5->new;
61 #binmode($FD) if $mode;
62 #$ctx->addfile($FD) or warn "$!\n";
63 #$md5 = $ctx->hexdigest;
64 };
65 0 ▶ unless($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
66 0 $ctx = Digest::MD5->new;
67 0 ▶ binmode($FD) if $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $mode 0 0 0
68 0 ▶ $ctx->addfile($FD) or warn "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
$ctx->addfile($FD) or warn "$!\n" 0 0 0
69 0 $md5 = $ctx->hexdigest;
70 0 ▶ close $FD if $FD;
Branch: 1 branch
BranchTrueFalseTotal
if $FD 0 0 0
71 0 return $md5;
72 }
73 }
74  
75 sub md5check {
76 0 ▶ my $file = shift;
Subroutine md5check: not called CC=4 SCAR=30.0
Pod undocumented
77 0 ▶ open( my $fd, '<', $file ) or warn "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or warn "${file}: $!\n" 0 0 0
78 0 my $res .= $file . "\n";
79 0 ▶ while (<$fd>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fd)) 0 0 0
80 0 my ( $name, $sum ) = split /\s+/;
81 0 $name =~ s/^\*//;
82 0 ▶ if ( $sum eq md5_sum( $name, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum eq md5_sum($name, 1)) { } 0 0 0
83 0 $res .= "$name OK\n";
84 }
85 else {
86 0 $res .= "$name FAILED\n";
87 }
88 }
89  
90 0 close $fd;
91  
92 0 return $res;
93 }
94  
95 # 遍历目录计算md5值
96 sub md5init {
97  
98 0 ▶ my $fd = shift;
Subroutine md5init: not called CC=15 SCAR=54.8
Pod undocumented
99 0 my $res;
100 0 ▶ if ( -f $fd ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $fd) { } 0 0 0
elsif (-d $fd) { } 0 0 0
101 0 ▶ if ( -T $fd ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-T $fd) { } 0 0 0
elsif (-B $fd) { } 0 0 0
102  
103 #print "按照文本模式进行计算MD5!\n";
104 0 my $md5value = md5_sum( $fd, 0 );
105 0 ▶ return "$fd\t$md5value\n" if $md5value ;
Branch: 1 branch
BranchTrueFalseTotal
if $md5value 0 0 0
106 }
107 elsif ( -B $fd ) {
108  
109 #print "二进制文件用binmod计算MD5!\n";
110 0 my $md5value = md5_sum( $fd, 1 );
111 0 ▶ return "$fd\t$md5value\n" if $md5value;
Branch: 1 branch
BranchTrueFalseTotal
if $md5value 0 0 0
112 }
113 else {
114 #print "其他文件,按照bimmod计算!\n";
115 0 my $md5value = md5_sum( $fd, 1 );
116 0 ▶ return "$fd\t$md5value\n" if $md5value;
Branch: 1 branch
BranchTrueFalseTotal
if $md5value 0 0 0
117 }
118 }
119 elsif ( -d $fd ) {
120 0 my $file_md5;
121 0 print "init for all files $fd:\n";
122 0 ▶ opendir( my $DH, $fd ) or warn "Can't open dir $fd: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $DH, $fd or warn "Can't open dir ${fd}: $!" 0 0 0
123 0 for ( readdir $DH ) {
124 0 my $file = $fd . '/' . $_;
125 # 上级目录..,本目录. 以及连接文件跳过
126 0 ▶ next if ( $file =~ m{/.$} || $file =~ m{/..$} || -l $file );
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[/.$] or $file =~ m[/..$] or -l $file 0 0 0
Condition0%0/6
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
l!l&&r!l&&!r
000
$file =~ m[/.$] or $file =~ m[/..$]
l!l&&r!l&&!r
000
MC/DC0%0/3
$file =~ m[/.$] or $file =~ m[/..$] or -l $file
$file =~ m[/.$] $file =~ m[/..$] -l $file
Truth table0%0/4
A or B or C
A= $file =~ m[/.$]B= $file =~ m[/..$]C= -l $file
ABCresult
1XX1
01X1
0011
0000
127 0 eval {$res .= md5init($file);};
128 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
129 0 print "some wron for init $file\n ";
130 }
131 0 ▶ print "Debug::", $res if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
132 }
133 0 closedir $DH;
134 }
135 0 return $res;
136 }
137  
138 1 1;
139 __END__
↑ Summary