Trying to calculate an incremental md5
digest for all files in deep directory trees, but I cannot "reuse" an already calculated digest.
Here is my test code:
use 5.014;
use warnings;
use Digest::MD5;
use Path::Tiny;
my @filenames = qw(a b);
my $testdir = Path::Tiny->tempdir;
$testdir->child($_)->spew($_) for @filenames;
dirmd5($testdir, @filenames);
exit;
sub dirmd5 {
my($dir, @files) = @_;
my $dirctx = Digest::MD5->new;
for my $fname (@files) {
my $filectx = Digest::MD5->new;
my $fd = $dir->child($fname)->openr_raw;
$filectx->addfile($fd);
close $fd;
say "md5 for $fname : ", $filectx->clone->hexdigest;
$fd = $dir->child($fname)->openr_raw;
$dirctx->addfile($fd);
close $fd;
}
say "md5 for dir: ", $dirctx->hexdigest;
}
The above prints:
md5 for a : 0cc175b9c0f1b6a831c399e269772661
md5 for b : 92eb5ffee6ae2fec3ad71c777531578f
md5 for dir: 187ef4436122d1cc2f40dc2b92f0eba0
which is the correct, but unfortunately inefficient way. (see comments).
Reading docs , I did not find a way to reuse the already calculated md5. for example as stated above $dirctx->add($filectx);
. This is probably not possible.
- -, , / ?
: