[Perl] create md5 v1.0

作者:reistlin 发布时间:August 3, 2010 分类:原创文章

作者: reistlin
来源: http://www.reistlin.com/blog/33
更新时间: 2010.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

perl.gif

googlecode1.gifgooglecode2.gif
[https://reistlin.googlecode.com/svn/trunk/perl/create_md5.pl]

#!/usr/bin/perl -w 
 
# Name: create md5 v1.0
# Author: reistlin
# Website: www.reistlin.com
# Hotfix: bigyong
# Website: www.bigyong.com
# Date: 2010.08.02
 
use strict;
use Data::Dumper;
use Digest::MD5 qw(md5 md5_hex md5_base64);
 
 
# Debug Switch
my $debug = 0;
 
# Start Time
my $time_1 = time();
# Clear
system "clear";
 
 
# Defined Original Hash File
my $original_file = "check_md5.log";
my %original_hash;
 
# Defined CheckList Directory
my $checklist_dir = "/home/reistlin";
 
# Defined Exclude Directory
my $exclude_tag = 0;
my @exclude_dir = ("/home/reistlin/exclude1", "/home/reistlin/exclude2");
my $exclude_key = join("|", @exclude_dir);
 
 
# Create MD5 Mode
sub createmd5 {
        my $path = shift;
        # clean path end "/"
        $path =~ s/\/+$//;
 
        opendir(DIR, $path) or die "[Error] Can not check directory [$path] \n";
        my @list = readdir(DIR);
        closedir(DIR);
 
        foreach my $tmp (@list) {
                # exclude "." or ".."
                next if ( $tmp =~ m/^\.+$/ );
                # full path
                my $path_sub = $path . "/" . $tmp;
                # exclude directory
		next if ( ( $path_sub =~ /$exclude_key/ ) && ( $exclude_tag == 1 ) );
                # subdirectory recursive
                if ( -d $path_sub ) {
                        &createmd5($path_sub);
                } else {
			$original_hash{$path_sub} = &md5sum($path_sub);
                }
        }
}
 
 
# MD5 Check Mode
sub md5sum {
        my $file = shift;
        open(FILE, $file) or die "[Error] Can not check file [$file] \n";
        binmode(FILE);
 
        my $md5 = Digest::MD5->new();
        $md5->addfile(*FILE);
 
        close(FILE);
 
        $md5->hexdigest;
}
 
 
# File Stat Mode
sub filestat {
        my $file = shift;
        my @file_stat = stat($file);
        my $mtime = localtime $file_stat[9];
        my $ctime = localtime $file_stat[10];
 
        print "\n";
        print "[$file] Modify: $mtime \n";
        print "[$file] Change: $ctime \n";
        print "[$file] UID: $file_stat[4] \n";
        print "[$file] GID: $file_stat[5] \n";
        print "[$file] Size: $file_stat[7] bytes \n";
        print "\n";
}
 
 
# Main Program
&createmd5($checklist_dir);
 
 
# Create MD5 logfile
open(LINE, "+>" . $original_file) or die "[Error] Can not create MD5 logfile [$original_file] \n";
 
while ( my ($file, $md5) = each %original_hash ) {
	print LINE "$md5 $file\n";
}
 
close(LINE);
 
 
# Print Result
if ( -e $original_file ) {
	print "Congratulations! MD5 logfile Create Succeed! \n";
	&filestat($original_file);
}
 
 
# End Time
my $time_2 = time();
my $time = $time_2 - $time_1;
 
 
# Script Runtime
print "Runtime \[$time\] Seconds ... \n";
print "End! Good Luck! :-) \n";
print "\n";

标签: perl

评论已关闭