blib/lib/File/Dircmp.pm

filter: Statementstmt 27.9% 19 / 68 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 4 / 6 Podpod 0.0% 0 / 2 totaltotal 19.1% 23 / 120 CC 26 SCAR 59.0
CC
26
Cov
18%
compare_dirs
506.0
dircmp
30.0
BEGIN
1.0
CRAP 364.1
↑ Summary
1 # Copyright (c) 2003 Josh Schulte. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2  
3 package File::Dircmp;
4  
5 1 require Exporter;
6  
7 1 @ISA = "Exporter";
8 1 @EXPORT = qw(dircmp);
9  
10 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Glob "bsd_glob";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my @g_diffs;
16 1 my $g_d = 0;
17 1 my $g_s = 0;
18
19 #
20 # TODO: implement switch to compare the contents of files with the same
21 # name in both directories and output a list telling what must be changed
22 # in the two files to bring them into agreement.
23 #
24  
25 ############################## dircmp() ##############################
26 #
27 # print directory differences
28 #
29 # arguments:
30 # first directory
31 # second directory
32 # 1 to show file diffs
33 # 1 to suppress messages about identical files
34 #
35 # return:
36 # list of differences
37 #
38 sub dircmp
39 0 ▶{
Subroutine dircmp: not called CC=5 SCAR=34.0
Pod undocumented
40 0 my $d1 = shift;
41 0 my $d2 = shift;
42 0 my $dodiff = shift;
43 0 my $suppress = shift;
44  
45 # need to reset global vars every time called
46 0 @g_diffs = ();
47 0 $g_d = 0;
48 0 $g_s = 0;
49  
50 0 ▶ $g_d = 1 if $dodiff;
Branch: 1 branch
BranchTrueFalseTotal
if $dodiff 0 0 0
51 0 ▶ $g_s = 1 if $suppress;
Branch: 1 branch
BranchTrueFalseTotal
if $suppress 0 0 0
52
53 0 ▶ unless( -d $d1)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $d1) 0 0 0
54 {
55 0 push(@g_diffs, "$d1 not a directory !");
56 0 return @g_diffs;
57 }
58  
59 0 ▶ unless( -d $d2)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $d2) 0 0 0
60 {
61 0 push(@g_diffs, "$d2 not a directory !");
62 0 return @g_diffs;
63 }
64  
65 0 compare_dirs($d1, $d2);
66
67 0 return @g_diffs;
68 }
69  
70 sub compare_dirs
71 0 ▶{
Subroutine compare_dirs: not called CC=22 SCAR=62.3
Pod undocumented
72 # get args
73 0 my $d1 = shift;
74 0 my $d2 = shift;
75  
76 # find out what files are in directories
77 0 my %d1_files;
78 my %d2_files;
79
80 0 $d1_files{basename($_)} = 0 foreach bsd_glob("$d1/.*");
81 0 $d1_files{basename($_)} = 0 foreach bsd_glob("$d1/*");
82
83 0 delete $d1_files{"."};
84 0 delete $d1_files{".."};
85  
86 0 $d2_files{basename($_)} = 0 foreach bsd_glob("$d2/.*");
87 0 $d2_files{basename($_)} = 0 foreach bsd_glob("$d2/*");
88
89 0 delete $d2_files{"."};
90 0 delete $d2_files{".."};
91  
92 # find out what is common and exclusive to each directory
93 0 my %common;
94 my @d1_only;
95 0 my @d2_only;
96
97 0 foreach my $x (keys(%d1_files))
98 {
99 0 ▶ if(defined $d2_files{$x})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d2_files{$x}) { } 0 0 0
100 {
101 0 $common{$x} = 0;
102 }
103 else
104 {
105 0 push(@d1_only, $x);
106 }
107 }
108  
109 0 foreach my $x (keys(%d2_files))
110 {
111 0 ▶ push(@d2_only, $x) unless defined $common{$x};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $common{$x} 0 0 0
112 }
113  
114 # add missing files to the list
115 0 push(@g_diffs, "Only in $d1: $_") foreach @d1_only;
116 0 push(@g_diffs, "Only in $d2: $_") foreach @d2_only;
117  
118 # compare common files
119 0 foreach my $x (keys %common)
120 {
121 0 my $d1_file = "${d1}/${x}";
122 0 my $d2_file = "${d2}/${x}";
123  
124 0 ▶ if((-f $d1_file) && (-f $d2_file))
Branch: 4 branches
BranchTrueFalseTotal
if (-f $d1_file and -f $d2_file) { } 0 0 0
elsif (-d $d1_file and -d $d2_file) { } 0 0 0
elsif (-f $d1_file and -d $d2_file) { } 0 0 0
elsif (-d $d1_file and -f $d2_file) { } 0 0 0
Condition0%0/3
-f $d1_file and -f $d2_file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $d1_file and -f $d2_file
-f $d1_file -f $d2_file
Truth table0%0/3
A and B
A= -f $d1_fileB= -f $d2_file
ABresult
0X0
100
111
125 {
126 0 ▶ unless(compare($d1_file, $d2_file))
Branch: 1 branch
BranchTrueFalseTotal
if (compare($d1_file, $d2_file)) { } 0 0 0
127 {
128 0 ▶ unless($g_s)
Branch: 1 branch
BranchTrueFalseTotal
unless ($g_s) 0 0 0
129 {
130 0 push(@g_diffs, "Files $d1_file and $d2_file are identical");
131 }
132 }
133 else
134 {
135 0 push(@g_diffs, "Files $d1_file and $d2_file differ");
136 }
137 }
138 ▶ elsif((-d $d1_file) && (-d $d2_file))
Condition0%0/3
-d $d1_file and -d $d2_file
!ll&&!rl&&r
000
MC/DC0%0/2
-d $d1_file and -d $d2_file
-d $d1_file -d $d2_file
Truth table0%0/3
A and B
A= -d $d1_fileB= -d $d2_file
ABresult
0X0
100
111
139 {
140 0 compare_dirs($d1_file, $d2_file);
141 }
142 ▶ elsif((-f $d1_file) && (-d $d2_file))
Condition0%0/3
-f $d1_file and -d $d2_file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $d1_file and -d $d2_file
-f $d1_file -d $d2_file
Truth table0%0/3
A and B
A= -f $d1_fileB= -d $d2_file
ABresult
0X0
100
111
143 {
144 0 push(@g_diffs, "File $d1_file is a regular file while file $d2_file is a directory");
145 }
146 ▶ elsif((-d $d1_file) && (-f $d2_file))
Condition0%0/3
-d $d1_file and -f $d2_file
!ll&&!rl&&r
000
MC/DC0%0/2
-d $d1_file and -f $d2_file
-d $d1_file -f $d2_file
Truth table0%0/3
A and B
A= -d $d1_fileB= -f $d2_file
ABresult
0X0
100
111
147 {
148 0 push(@g_diffs, "File $d1_file is a directory while file $d2_file is a regular file");
149 }
150 }
151 }
152  
153 1 1;
154  
155 =head1 NAME
156  
157 dircmp - directory comparison
158  
159 =head1 SYNOPSIS
160  
161 use File::Dircmp;
162  
163 @r = dircmp($dir1, $dir2, $diff, $suppress);
164  
165 =head1 DESCRIPTION
166  
167 The dircmp command examines dir1 and dir2 and generates various tabulated information about the contents of the directories. Listings of files that are unique to each directory are generated for all the options. If no option is entered, a list is output indicating whether the file names common to both directories have the same contents.
168  
169 The algorithm I use orders the report differently than the unix commands. There is no option to control the length of the output.
170  
171 =head1 OPERANDS
172  
173 $dir1 A path name of a directory to be compared.
174  
175 $dir2 A path name of a directory to be compared.
176  
177 $diff Compare the contents of files with the same name in both directories and output a list telling what must be changed in the two files to bring them into agreement. The list format is described in diff(1).
178  
179 $suppress Suppress messages about identical files.
180  
181 =head1 TODO
182  
183 Implement the $diff argument.
184  
185 =head1 AUTHOR
186  
187 Josh Schulte <josh_schulte@yahoo.com>
188  
↑ Summary