From 425e9485aaf171864e4e0ad7a327f9a60378a33d Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 8 Jan 2015 16:36:45 -0500 Subject: [PATCH] Re-added File-hash() - accidentally deleted it when working on owner(). --- lib/BackRest/File.pm | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/lib/BackRest/File.pm b/lib/BackRest/File.pm index 8a9fd7d06..f96fd75ed 100644 --- a/lib/BackRest/File.pm +++ b/lib/BackRest/File.pm @@ -770,6 +770,79 @@ sub remove return $bRemoved; } +#################################################################################################################################### +# HASH +#################################################################################################################################### +sub hash +{ + my $self = shift; + my $strPathType = shift; + my $strFile = shift; + my $bCompressed = shift; + my $strHashType = shift; + + # Set defaults + $bCompressed = defined($bCompressed) ? $bCompressed : false; + $strHashType = defined($strHashType) ? $strHashType : 'sha1'; + + # Set operation variables + my $strFileOp = $self->path_get($strPathType, $strFile); + my $strHash; + + # Set operation and debug strings + my $strOperation = OP_FILE_HASH; + my $strDebug = "${strPathType}:${strFileOp}, " . + 'compressed = ' . ($bCompressed ? 'true' : 'false') . ', ' . + "hash_type = ${strHashType}"; + &log(DEBUG, "${strOperation}: ${strDebug}"); + + if ($self->is_remote($strPathType)) + { + confess &log(ASSERT, "${strDebug}: remote operation not supported"); + } + else + { + my $hFile; + + if (!open($hFile, '<', $strFileOp)) + { + my $strError = "${strFileOp} could not be read: " . $!; + my $iErrorCode = 2; + + if (!$self->exists($strPathType, $strFile)) + { + $strError = "${strFileOp} does not exist"; + $iErrorCode = 1; + } + + if ($strPathType eq PATH_ABSOLUTE) + { + confess &log(ERROR, $strError, $iErrorCode); + } + + confess &log(ERROR, "${strDebug}: " . $strError); + } + + my $oSHA = Digest::SHA->new($strHashType); + + if ($bCompressed) + { + $oSHA->addfile($self->process_async_get()->process_begin('decompress', $hFile)); + $self->process_async_get()->process_end(); + } + else + { + $oSHA->addfile($hFile); + } + + close($hFile); + + $strHash = $oSHA->hexdigest(); + } + + return $strHash; +} + #################################################################################################################################### # OWNER ####################################################################################################################################