【发布时间】:2017-04-23 02:55:11
【问题描述】:
尝试为深层目录树中的所有文件计算增量 md5 摘要,但我无法“重用”已计算的摘要。
这是我的测试代码:
#!/usr/bin/env perl
use 5.014;
use warnings;
use Digest::MD5;
use Path::Tiny;
# create some test-files in the tempdir
my @filenames = qw(a b);
my $testdir = Path::Tiny->tempdir;
$testdir->child($_)->spew($_) for @filenames; #create 2 files
dirmd5($testdir, @filenames);
exit;
sub dirmd5 {
my($dir, @files) = @_;
my $dirctx = Digest::MD5->new; #the md5 for the whole directory
for my $fname (@files) {
# calculate the md5 for one file
my $filectx = Digest::MD5->new;
my $fd = $dir->child($fname)->openr_raw;
$filectx->addfile($fd);
close $fd;
say "md5 for $fname : ", $filectx->clone->hexdigest;
# want somewhat "add" the above file-md5 to the directory md5
# this not work - even if the $filectx isn't reseted (note the "clone" above)
#$dirctx->add($filectx);
# works adding the file as bellow,
# but this calculating the md5 again
# e.g. for each file the calculation is done two times...
# once for the file-alone (above)
# and second time for the directory
# too bad if case of many and large files. ;(
# especially, if i want calculate the md5sum for the whole directory trees
$fd = $dir->child($fname)->openr_raw;
$dirctx->addfile($fd);
close $fd;
}
say "md5 for dir: ", $dirctx->hexdigest;
}
以上打印:
md5 for a : 0cc175b9c0f1b6a831c399e269772661
md5 for b : 92eb5ffee6ae2fec3ad71c777531578f
md5 for dir: 187ef4436122d1cc2f40dc2b92f0eba0
这是正确的,但不幸的是效率低下。 (见 cmets)。
阅读the docs,我没有找到任何方法重用已经计算好的md5。例如如上$dirctx->add($filectx);。可能是不可能的。
存在任何校验和方式,允许在一定程度上重用已经计算的校验和,因此,我可以计算整个目录树的校验和/摘要,而无需为每个文件多次计算摘要?
参考:尝试解决this question
【问题讨论】:
标签: perl