Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

cdcheck

by Chady (Priest)
on Sep 21, 2003 at 06:28 UTC ( #292966=sourcecode: print w/ replies, xml ) Need Help??

Category:
Author/Contact Info Chady - chady@perlmonk.org
Description:

I've been burned badly too many times by too many badly burned backup CDs. Sometimes it's my drive, sometimes it's the blank CDs themselves, and sometimes it's just pure bad luck.

Nero Burning Rom, among others, provides a tool to check the CD after it has been burned. Unfortunately, the program didn't work for me, so here's a small utility to compare a CD to the original files, and report any differences.

Improvements are much welcomed.

Update: Two minutes after I posted this, PodMaster pointed out Verify Your Debian Potatoe - This really is a coincidence ;-)

#!/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 b
+urned 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] = <STDIN>);
                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;
}

Comment on cdcheck
Download Code
Re: cdcheck
by Anonymous Monk on Sep 21, 2003 at 17:11 UTC
    It should be faster/more correct to compare the data of the two files directly rather than taking a checksum.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://292966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-08-31 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls