#!/usr/bin/perl -w # # CD Check - Compares files burned to CD with the original files. # # # This utility should be used directly after burning the CD # and before archival to check if the data on the CD is # legitimate or some kind of buffer error caused it to become # corrupt or changed. # # Some CD Recording software contain such a utility by default # for example Nero Burning ROM, but some don't. There's where # this script comes in handy. # ################################################################## use strict; use File::Find; use Digest::MD5; use vars qw/$base1 $base2 @errors/; get_path($base2, "Enter path to original files: "); get_path($base1, "Enter CD path: "); find(\&check, $base1); if (@errors > 0) { print "\n\n\nGot ", scalar @errors, ".\nThe following files were changed, possibly a badly burned CD.\n\n"; print join "\n", @errors, "\n"; } else { print "\n====================\n"; print "CD burned correctly.\n"; print "====================\n\n"; } # END sub get_path { { print $_[1]; chomp($_[0] = ); unless (-d $_[0]) { print "Not a directory\n"; redo; } # to save some headache later, let's # standardize the path. $_[0] =~ s[\\][/]g; $_[0] .= '/' unless $_[0] =~ m{/$}; } } sub check { return unless -f $File::Find::name; # don't check folders. my $path = $File::Find::name; $path =~ s/^\Q$base1\E//; my $file2 = $base2 . $path; # that's the file in the original folder. print '=' x 50 , "\n+ Checking $File::Find::name\n\t"; my $MD5_1 = get_md5($File::Find::name); print $MD5_1, "\n"; my $MD5_2 = get_md5($file2); unless ($MD5_1 eq $MD5_2) { warn "\n\n*** Different files \n$MD5_1 * $File::Find::name\n$MD5_2 * $file2\n\n"; push @errors, $File::Find::name; } } sub get_md5 { my $file = shift; open FILE, "<", $file or die "Can't open $file : $!\n"; binmode FILE; return Digest::MD5->new->addfile(*FILE)->hexdigest; }