Cmp.pm

filter: Statementstmt 100% 70 / 70 Branchbran 85.4% 41 / 48 Conditioncond 70.0% 14 / 20 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 88.5% 139 / 157 CC 34 SCAR 34.1
CC
34
Cov
91%
fcmp
35.5
BEGIN
1.0
BEGIN
1.0
CRAP 30.3
↑ Summary Cmp/ConfigData.pm »
1 # -*- Perl -*-
2 #
3 # File::Cmp - compare two files character by character like cmp(1)
4  
5 package File::Cmp;
6  
7 1 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15 1 our @ISA = qw(Exporter);
16 1 our @EXPORT_OK = qw/&fcmp/;
17  
18 1 our $VERSION = '1.09';
19  
20 # XXX 'skip' and 'limit' might be good parameters to add, to skip X
21 # initial bytes, limit work to Y bytes of data to check
22 13 ▶sub fcmp {
Subroutine fcmp: called CC=34 SCAR=35.7
Pod documented
23 13 ▶ croak 'fcmp needs two files' if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 1 12 13
24 12 my @files = splice @_, 0, 2;
25 12 ▶ my $param = ( @_ == 1 and ref $_[0] eq 'HASH' ) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 1 11 12
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1011
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
26  
27 12 ▶ $param->{sizecheck} = 1 unless exists $param->{sizecheck};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"sizecheck"} 9 3 12
28 12 ▶ $param->{sizecheck} = 0 if exists $param->{tells};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"tells"} 2 10 12
29  
30 12 ▶ if ( $param->{fscheck} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"fscheck"}) 4 8 12
31 4 my @statbuf;
32 4 for my $f (@files) {
33 # stat has the handy property of chasing symlinks for us
34 7 ▶ my @devino = ( stat $f )[ 0, 1 ] or croak "could not stat: $!";
Branch: 1 branch
BranchTrueFalseTotal
my(@devino) = (stat $f)[0, 1] or croak("could not stat: $!") 6 1 7
35 6 push @statbuf, \@devino;
36 }
37 3 ▶ if ( $statbuf[0][0] == $statbuf[1][0]
Branch: 1 branch
BranchTrueFalseTotal
if ($statbuf[0][0] == $statbuf[1][0] and $statbuf[0][1] == $statbuf[1][1]) 2 1 3
Condition66%2/3
$statbuf[0][0] == $statbuf[1][0] and $statbuf[0][1] == $statbuf[1][1]
!ll&&!rl&&r
012
MC/DC50%1/2
$statbuf[0][0] == $statbuf[1][0] and $statbuf[0][1] == $statbuf[1][1]
$statbuf[0][0] == $statbuf[1][0] $statbuf[0][1] == $statbuf[1][1]
Truth table66%2/3
A and B
A= $statbuf[0][0] == $statbuf[1][0]B= $statbuf[0][1] == $statbuf[1][1]
ABresult
0X0
100
111
38 and $statbuf[0][1] == $statbuf[1][1] ) {
39 2 ▶ ${ $param->{reason} } = 'fscheck' if exists $param->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"reason"} 1 1 2
40 2 return 1; # assume files identical as both dev and inode match
41 }
42 }
43  
44 # The files are probably not identical if they differ in size;
45 # however, offer means to turn this check off if -s for some reason is
46 # incorrect (or if 'tells' is on so we need to find roughly where the
47 # difference is in the files).
48 9 ▶ if ( $param->{sizecheck}
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"sizecheck"} and (-s $files[0] // -1) != (-s $files[1] // -2)) 4 5 9
Condition71%5/7
$param->{"sizecheck"} and (-s $files[0] // -1) != (-s $files[1] // -2)
!ll&&!rl&&r
414
-s $files[0] // -1
l!l
50
-s $files[1] // -2
l!l
50
MC/DC50%1/2
$param->{"sizecheck"} and (-s $files[0] // -1) != (-s $files[1] // -2)
$param->{"sizecheck"} -s $files[0]
MC/DC0%0/1
-s $files[1] // -2
-s $files[1]
Truth table66%2/3
A and B
A= $param->{"sizecheck"}B= -s $files[0]
ABresult
0X0
100
111
Truth table50%1/2
A
A= -s $files[1]
Aresult
11
00
49 and ( ( -s $files[0] ) // -1 ) != ( ( -s $files[1] ) // -2 ) ) {
50 4 ▶ ${ $param->{reason} } = 'size' if exists $param->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"reason"} 2 2 4
51 4 return 0;
52 }
53  
54 5 my @fhs;
55 5 for my $f (@files) {
56 8 ▶ if ( !defined reftype $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined builtin::reftype($f)) { } 7 1 8
57 7 ▶ open my $fh, '<', $f or croak "could not open $f: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $f or croak("could not open ${f}: $!") 6 1 7
58 6 push @fhs, $fh;
59 } else {
60 # Assume is a GLOB or something can readline on, XXX might want to
61 # better check this
62 1 push @fhs, $f;
63 }
64 7 ▶ if ( exists $param->{binmode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"binmode"}) 3 4 7
65 1 ▶ binmode $fhs[-1], $param->{binmode} or croak "binmode failed: $!";
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
binmode $fhs[-1], $param->{"binmode"} or croak("binmode failed: $!") 2 1 3
66 }
67 }
68  
69 3 ▶ local $/ = $param->{RS} if exists $param->{RS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"RS"} 0 3 3
70  
71 3 while (1) {
72 18 my $eof1 = eof $fhs[0];
73 18 my $eof2 = eof $fhs[1];
74 # Done if both files are at EOF; otherwise assume they differ if one
75 # completes before the other (this second case would normally be
76 # optimized away by the -s test, above).
77 18 ▶ last if $eof1 and $eof2;
Branch: 1 branch
BranchTrueFalseTotal
if $eof1 and $eof2 1 17 18
Condition66%2/3
$eof1 and $eof2
!ll&&!rl&&r
1701
MC/DC50%1/2
$eof1 and $eof2
$eof1 $eof2
Truth table66%2/3
A and B
A= $eof1B= $eof2
ABresult
0X0
100
111
78 17 ▶ if ( $eof1 xor $eof2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eof1 xor $eof2) 1 16 17
Condition50%2/4
$eof1 xor $eof2
l&&rl&&!r!l&&r!l&&!r
00116
MC/DC50%1/2
$eof1 xor $eof2
$eof1 $eof2
Truth table50%2/4
A xor B
A= $eof1B= $eof2
ABresult
110
101
011
000
79 1 ▶ ${ $param->{reason} } = 'eof' if exists $param->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"reason"} 1 0 1
80 1 @{ $param->{tells} } = ( tell $fhs[0], tell $fhs[1] )
81 1 ▶ if exists $param->{tells};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"tells"} 1 0 1
82 1 return 0;
83 }
84  
85 16 my $this = readline $fhs[0];
86 16 ▶ croak "error reading from first file: $!" if !defined $this;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 16 16
87 16 my $that = readline $fhs[1];
88 16 ▶ croak "error reading from second file: $!" if !defined $that;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $that 0 16 16
89  
90 16 ▶ if ( $this ne $that ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this ne $that) 1 15 16
91 1 @{ $param->{tells} } = ( tell $fhs[0], tell $fhs[1] )
92 1 ▶ if exists $param->{tells};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"tells"} 1 0 1
93 1 ▶ ${ $param->{reason} } = 'diff' if exists $param->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"reason"} 1 0 1
94 1 return 0;
95 }
96 }
97  
98 1 return 1; # assume files identical if get this far
99 }
100  
101 1 1;
102 __END__
↑ Summary Cmp/ConfigData.pm »