Ext: MD5 Check program code (PERL)

Source: Internet
Author: User
Tags md5 posix sprintf

Using two different functions, checkmd5sum.pl is faster than checkmd5sum_find.pl in terms of execution efficiency.

1.checkmd5sum.pl #!/usr/bin/perl # # File:checkmd5sum.pl # Author:amxku.net # date:2008-12-...
The two use different functions, and the execution efficiency is checkmd5sum.pl faster than checkmd5sum_find.pl.
1.checkmd5sum.pl

#!/usr/bin/perl
# File:checkmd5sum.pl
# Author:amxku.net
# date:2008-12-02
# License:gpl-2

Use DIGEST::MD5;
Use Io::file;
Use POSIX QW (strftime);

if (@ARGV! = 1) {
print "Check files md5sum/nusage:<rootdir>/nexample:c://n";
Exit
}

$now _time=strftime "%y-%m-%d%h:%m:%s", localtime;
$MD 5sumfile_name=strftime "%y_%m_%d_%h_%m_%s", localtime;

Open Md5file, "> $md 5sumfile_name.txt" or Die "$!/n/n";
Print Md5file "# Check Files md5sum/n/n";
Print Md5file "# Created on $now _time/n";
Print Md5file "# amxku_at_msn.com/n/n";
My $root = $ARGV [0];
Sub Lsr_s ($) {
my $cwd = shift;
My @dirs = ($cwd. ' /');

My ($dir, $file);
while ($dir = Pop (@dirs)) {
Local *DH;
if (!opendir (DH, $dir)) {
Warn "Cannot opendir $dir: $! $^e ";
Next
}
foreach (Readdir (DH)) {
if ($_ eq '. ' | | $_ eq ') {
Next
}
$file = $dir. $_;
if (!-l $file &&-D _) {
$file. = '/';
Push (@dirs, $file);
}
Process ($file, $dir);
}
Closedir (DH);
}
}

My ($size, $dircnt, $filecnt) = (0, 0, 0);

Sub Process ($$) {
my $file = shift;
Print $file, "/n";
if (substr ($file, Length ($file)-1, 1) EQ '/') {
$dircnt + +;
}
else {
$filecnt + +;
$size + =-S $file;
My $fileinfo = Genchksuminfo ($file);
Print Md5file "$fileinfo/n";
}
}

Sub Genchksuminfo
{
My ($file) = @_;
My $chk = Digest::md5->new ();
My (@statinfo) = stat ($file);
$chk->add (@statinfo [0,1,2,3,4,5,7,9,10]);
$chk->addfile (Io::file->new ($file));
Return sprintf ("md5:%s/t%s", $chk->hexdigest, $file);
}

lsr_s ("$root");
Print "/n$filecnt files, $dircnt directory. $size bytes./n ";
My $dateinfo = "/n$filecnt files, $dircnt directory. $size bytes./n ";
Print Md5file "$dateinfo/n";
Close (Md5file);
2.checkmd5sum_find.pl

#!/usr/bin/perl
#
# File:checkmd5sum_find.pl
# author:p0150 & Amxku
# License:gpl-2

Use File::find;
Use DIGEST::MD5;
Use Io::file;
Use POSIX QW (strftime);

if (@ARGV! = 1) {
Print "Usage: <rootdir>/n";
print "example:c://n";
Exit
}
$now _time=strftime "%y-%m-%d%h:%m:%s", localtime;
$now _string=strftime "%y_%m_%d_%h_%m_%s", localtime;

Open Md5file, "> $now _string.txt" or Die "$!/n";
Print Md5file "#/n";
Print Md5file "# Created on $now _time/n";
Print Md5file "#/n";
Print Md5file "/n";
Chomp (My $dir = $ARGV [0]);
Find (/&wanted, $dir);
Sub Wanted
{
Unless (-D $_)
{
Print $File:: Find::d ir. '/'. $_. " /n ";
My $name = $File:: Find::d ir. '/'. $_;
My $fileinfo = Genchksuminfo ($name);
Print Md5file "$fileinfo/n";
}
}

Sub Genchksuminfo
{
My ($file) = @_;
My $chk = Digest::md5->new ();
My (@statinfo) = stat ($file);
$chk->add (@statinfo [0,1,2,3,4,5,7,9,10]);
$chk->addfile (Io::file->new ($file));
Return sprintf ("md5:%s/t%s", $chk->hexdigest, $file);
}
Close (Md5file);

SOURCE from Link: http://www.hackbase.com/tech/2009-11-03/57649.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.