Adds HashFile(), a method that uses openssl to provide md5/sha1 sums of files. Much of the code was taken from tinderbox's implementation; openssl was chosen because it's just about guaranteed to exist on all the platforms we care about, and thus elimintes platform-specifics-parsing- and trying to figure out platform/which-binary-to-call-issues.

This commit is contained in:
preed%mozilla.com 2006-12-04 23:19:01 +00:00
parent 286a2436f7
commit b19e29f2c4

View File

@ -90,8 +90,7 @@ sub RunShellCommand {
## it created in the pursuit of your request, and keeps its actual success
## status in $@.
sub MkdirWithPath
{
sub MkdirWithPath {
my %args = @_;
my $dirToCreate = $args{'dir'};
@ -108,4 +107,47 @@ sub MkdirWithPath
return defined($@);
}
sub HashFile {
my %args = @_;
die "ASSERT: null file to hash\n" if (not defined($args{'file'}));
my $fileToHash = $args{'file'};
my $hashFunction = $args{'type'} || 'md5';
my $dumpOutput = $args{'output'} || 0;
my $ignoreErrors = $args{'ignoreErrors'} || 0;
die "ASSERT: unknown hashFunction; use 'md5' or 'sha1': $hashFunction\n" if
($hashFunction ne 'md5' && $hashFunction ne 'sha1');
if (not(-f $fileToHash) || not(-r $fileToHash)) {
if ($ignoreErrors) {
return '';
} else {
die "ASSERT: unusable/unreadable file to hash\n";
}
}
# We use openssl because that's pretty much guaranteed to be on all the
# platforms we want; md5sum and sha1sum aren't.
my $rv = RunShellCommand(command => "openssl dgst -$hashFunction " .
"\"$fileToHash\"",
output => $dumpOutput);
if ($rv->{'exitValue'} != 0) {
if ($ignoreErrors) {
return '';
} else {
die "MozUtil::HashFile(): hash call failed: $rv->{'exitValue'}\n";
}
}
my $hashValue = $rv->{'output'};
chomp($hashValue);
# Expects input like MD5(mozconfig)= d7433cc4204b4f3c65d836fe483fa575
# Removes everything up to and including the "= "
$hashValue =~ s/^.+\s+(\w+)$/$1/;
return $hashValue;
}
1;