| 1 | package File::Same; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | $File::Same::VERSION = '0.09'; | |||||||||
| 4 | 5 | $File::Same::AUTHORITY = 'cpan:MANWAR'; | |||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | File::Same - Detect which files are the same as a given one. | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 0.09 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 5 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | use Digest::MD5; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 5 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 5 | my %md5s; | |||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | File::Same uses MD5 sums to decide which files are the same in a given directory, | ||||||||||
| 26 | set of directories or set of files. It was originally written to test which files | ||||||||||
| 27 | are the same picture in multiple directories or under multiple filenames, but can | ||||||||||
| 28 | be generally useful for other systems. | ||||||||||
| 29 | |||||||||||
| 30 | File::Same will use an internal cache, for performance reasons. | ||||||||||
| 31 | |||||||||||
| 32 | File::Same will also be careful not to return C<$original> in the list of matched | ||||||||||
| 33 | files. | ||||||||||
| 34 | |||||||||||
| 35 | All of the functions return a list of files that match, with full path expanded. | ||||||||||
| 36 | |||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||
| 38 | |||||||||||
| 39 | use strict; use warnings; | ||||||||||
| 40 | use File::Same; | ||||||||||
| 41 | |||||||||||
| 42 | my @same1 = File::Same::scan_dirs('sample.txt', ['other', '.']); | ||||||||||
| 43 | |||||||||||
| 44 | my @same2 = File::Same::scan_files('sample.txt', ['ex1.txt', 'ex2.txt']); | ||||||||||
| 45 | |||||||||||
| 46 | my @same3 = File::Same::scan_dir('sample.txt', 'somedir'); | ||||||||||
| 47 | |||||||||||
| 48 | =head1 METHODS | ||||||||||
| 49 | |||||||||||
| 50 | =head2 scan_files($original, \@list) | ||||||||||
| 51 | |||||||||||
| 52 | Scan a list of files to compare against a given file. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | sub scan_files { | ||||||||||
| 57 | 6 | ▶ | my ($original, $files) = @_; | ||||||||
|
Subroutine
scan_files: called CC=8 SCAR=21.7
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | 6 | my @results; | |||||||||
| 60 | 6 | my $orig_md5 = $md5s{$original}; | |||||||||
| 61 | |||||||||||
| 62 | 6 | ▶ | if (!$orig_md5) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 6 | my $ctx = Digest::MD5->new(); | |||||||||
| 64 | 6 | ▶ | open(FILE, $original) || die "Cannot open '$original' : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 6 | binmode(FILE); | |||||||||
| 66 | 6 | $ctx->addfile(*FILE); | |||||||||
| 67 | 6 | $orig_md5 = $ctx->hexdigest; | |||||||||
| 68 | 6 | close(FILE); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 6 | foreach my $file (@$files) { | |||||||||
| 72 | 18 | ▶ | if (my $md5 = $md5s{$file}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | ▶ | if ($orig_md5 eq $md5) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | push @results, $file; | |||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | else { | ||||||||||
| 78 | 18 | my $ctx = Digest::MD5->new(); | |||||||||
| 79 | 18 | ▶ | open(FILE, $file) || die "Cannot open '$file' : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 18 | binmode(FILE); | |||||||||
| 81 | 18 | $ctx->addfile(*FILE); | |||||||||
| 82 | 18 | ▶ | if ($orig_md5 eq $ctx->hexdigest) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 5 | push @results, $file; | |||||||||
| 84 | } | ||||||||||
| 85 | 18 | close(FILE); | |||||||||
| 86 | } | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 6 | return grep {_not_same($_, $original)} @results; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | =head2 scan_dir($original, $dir) | ||||||||||
| 93 | |||||||||||
| 94 | Scan an entire directory to find files the same as this one. | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | sub scan_dir { | ||||||||||
| 99 | 5 | ▶ | my ($original, $dir) = @_; | ||||||||
|
Subroutine
scan_dir: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 100 | |||||||||||
| 101 | 5 | ▶ | opendir(DIR, $dir) || die "Cannot opendir '$dir' : $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 5 | my @files = grep { -f } map { File::Spec->catfile($dir, $_) } readdir(DIR); | |||||||||
| 103 | 5 | closedir(DIR); | |||||||||
| 104 | |||||||||||
| 105 | 5 | return scan_files($original, \@files); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =head2 scan_dirs($original, \@dirs) | ||||||||||
| 109 | |||||||||||
| 110 | Scan a list of directories to find files the same as this one. | ||||||||||
| 111 | |||||||||||
| 112 | =cut | ||||||||||
| 113 | |||||||||||
| 114 | sub scan_dirs { | ||||||||||
| 115 | 1 | ▶ | my ($original, $dirs) = @_; | ||||||||
|
Subroutine
scan_dirs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 116 | |||||||||||
| 117 | 1 | my @results; | |||||||||
| 118 | |||||||||||
| 119 | 1 | foreach my $dir (@$dirs) { | |||||||||
| 120 | 4 | push @results, scan_dir($original, $dir); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 1 | return @results; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | # | ||||||||||
| 127 | # | ||||||||||
| 128 | # PRIVATE METHODS | ||||||||||
| 129 | |||||||||||
| 130 | sub _not_same { | ||||||||||
| 131 | 5 | ▶ | my ($file, $orig) = @_; | ||||||||
|
Subroutine
_not_same: called CC=2 SCAR=6.9
| |||||||||||
| 132 | |||||||||||
| 133 | 5 | ▶ | return 0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | if (File::Spec->rel2abs($file) eq File::Spec->rel2abs($orig)); | ||||||||||
| 135 | |||||||||||
| 136 | 3 | return 1; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | =head1 AUTHOR | ||||||||||
| 140 | |||||||||||
| 141 | =over 4 | ||||||||||
| 142 | |||||||||||
| 143 | =item Original author Matt Sergeant, C<< <matt at sergeant.org> >> | ||||||||||
| 144 | |||||||||||
| 145 | =item Currently maintained by Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||
| 146 | |||||||||||
| 147 | =back | ||||||||||
| 148 | |||||||||||
| 149 | =head1 REPOSITORY | ||||||||||
| 150 | |||||||||||
| 151 | L<https://github.com/manwar/File-Same> | ||||||||||
| 152 | |||||||||||
| 153 | =head1 SEE ALSO | ||||||||||
| 154 | |||||||||||
| 155 | L<Digest::MD5> - used to generate a checksum for every file. | ||||||||||
| 156 | |||||||||||
| 157 | L<File::Find::Duplicates> - another that can be used to find duplicates. | ||||||||||
| 158 | |||||||||||
| 159 | =head1 BUGS | ||||||||||
| 160 | |||||||||||
| 161 | Please report any bugs or feature requests to C<bug-file-same at rt.cpan.org>, or | ||||||||||
| 162 | through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Same>. | ||||||||||
| 163 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||
| 164 | bug as I make changes. | ||||||||||
| 165 | |||||||||||
| 166 | =head1 SUPPORT | ||||||||||
| 167 | |||||||||||
| 168 | You can find documentation for this module with the perldoc command. | ||||||||||
| 169 | |||||||||||
| 170 | perldoc File::Same | ||||||||||
| 171 | |||||||||||
| 172 | You can also look for information at: | ||||||||||
| 173 | |||||||||||
| 174 | =over 4 | ||||||||||
| 175 | |||||||||||
| 176 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 177 | |||||||||||
| 178 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Same> | ||||||||||
| 179 | |||||||||||
| 180 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 181 | |||||||||||
| 182 | L<http://annocpan.org/dist/File-Same> | ||||||||||
| 183 | |||||||||||
| 184 | =item * CPAN Ratings | ||||||||||
| 185 | |||||||||||
| 186 | L<http://cpanratings.perl.org/d/File-Same> | ||||||||||
| 187 | |||||||||||
| 188 | =item * Search CPAN | ||||||||||
| 189 | |||||||||||
| 190 | L<http://search.cpan.org/dist/File-Same/> | ||||||||||
| 191 | |||||||||||
| 192 | =back | ||||||||||
| 193 | |||||||||||
| 194 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 195 | |||||||||||
| 196 | Copyright (C) 2001 MessageLabs Limited. | ||||||||||
| 197 | |||||||||||
| 198 | This is free software, you may use it and distribute it under the same terms as | ||||||||||
| 199 | Perl itself. | ||||||||||
| 200 | |||||||||||
| 201 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||
| 202 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||
| 203 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||
| 204 | not accept this license. | ||||||||||
| 205 | |||||||||||
| 206 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||
| 207 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||
| 208 | complies with the requirements of this license. | ||||||||||
| 209 | |||||||||||
| 210 | This license does not grant you the right to use any trademark, service mark, | ||||||||||
| 211 | tradename, or logo of the Copyright Holder. | ||||||||||
| 212 | |||||||||||
| 213 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||
| 214 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||
| 215 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||
| 216 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||
| 217 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||
| 218 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||
| 219 | License to you shall terminate on the date that such litigation is filed. | ||||||||||
| 220 | |||||||||||
| 221 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||
| 222 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||
| 223 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||
| 224 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||
| 225 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||
| 226 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||
| 227 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 228 | |||||||||||
| 229 | =cut | ||||||||||
| 230 | |||||||||||
| 231 | 5 | 1; # End of File::Same | |||||||||