File Coverage

blib/lib/File/Dircmp.pm
Criterion Covered Total %
statement 12 61 19.6
branch 0 24 0.0
condition 0 12 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 16 105 15.2


line stmt bran cond sub pod time code
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             require Exporter;
6              
7             @ISA = "Exporter";
8             @EXPORT = qw(dircmp);
9              
10 1     1   10288 use File::Basename;
  1         3  
  1         137  
11 1     1   1370 use File::Compare;
  1         1035  
  1         60  
12 1     1   5 use File::Glob "bsd_glob";
  1         7  
  1         91  
13 1     1   4 use strict;
  1         1  
  1         617  
14              
15             my @g_diffs;
16             my $g_d = 0;
17             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             {
40 0     0 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 0         $g_d = 1 if $dodiff;
51 0 0         $g_s = 1 if $suppress;
52            
53 0 0         unless( -d $d1)
54             {
55 0           push(@g_diffs, "$d1 not a directory !");
56 0           return @g_diffs;
57             }
58              
59 0 0         unless( -d $d2)
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             {
72             # get args
73 0     0 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 0         if(defined $d2_files{$x})
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 0         push(@d2_only, $x) unless defined $common{$x};
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 0 0       if((-f $d1_file) && (-f $d2_file))
    0 0        
    0 0        
    0 0        
125             {
126 0 0         unless(compare($d1_file, $d2_file))
127             {
128 0 0         unless($g_s)
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))
139             {
140 0           compare_dirs($d1_file, $d2_file);
141             }
142             elsif((-f $d1_file) && (-d $d2_file))
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))
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;
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
188