使用了兩個不同的函數,在執行效率上checkmd5sum.pl 比checkmd5sum_find.pl要更快。
1.checkmd5sum.pl #!/usr/bin/perl # # File: checkmd5sum.pl # Author: amxku.net # Date: 2008-12- ...
兩個使用了不同的函數,在執行效率上checkmd5sum.pl 比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;
$md5sumfile_name=strftime "%Y_%m_%d_%H_%M_%S", localtime;
open MD5FILE,">$md5sumfile_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::dir.'/'.$_."/n";
my $name=$File::Find::dir.'/'.$_;
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);
原文來自連結:http://www.hackbase.com/tech/2009-11-03/57649.html