From 2e0fe2565025d0408e3d0f740fe1e5916d3ebbc0 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 5 Mar 2020 18:34:59 -0500 Subject: [PATCH] Remove dependency on LibC hash filter. Perl provides Digest::SHA for hashing so there is no need to expose this via LibC anymore. --- lib/pgBackRest/Common/Ini.pm | 4 +-- lib/pgBackRest/LibCAuto.pm | 5 ---- lib/pgBackRest/Storage/Base.pm | 2 -- lib/pgBackRest/Storage/Storage.pm | 22 ++++++++------- libc/LibC.xs | 3 --- libc/build/lib/pgBackRestLibC/Build.pm | 7 ----- libc/xs/crypto/hash.xs | 31 ---------------------- libc/xs/crypto/hash.xsh | 4 --- libc/xs/storage/storage.xsh | 16 ++--------- test/lib/pgBackRestTest/Env/HostEnvTest.pm | 6 ++--- 10 files changed, 20 insertions(+), 80 deletions(-) delete mode 100644 libc/xs/crypto/hash.xs delete mode 100644 libc/xs/crypto/hash.xsh diff --git a/lib/pgBackRest/Common/Ini.pm b/lib/pgBackRest/Common/Ini.pm index 697ef9c0a..594fb387e 100644 --- a/lib/pgBackRest/Common/Ini.pm +++ b/lib/pgBackRest/Common/Ini.pm @@ -8,6 +8,7 @@ use warnings FATAL => qw(all); use Carp qw(confess); use English '-no_match_vars'; +use Digest::SHA qw(sha1_hex); use Exporter qw(import); our @EXPORT = qw(); use File::Basename qw(dirname); @@ -17,7 +18,6 @@ use Storable qw(dclone); use pgBackRest::Common::Exception; use pgBackRest::Common::Log; use pgBackRest::Common::String; -use pgBackRest::LibC qw(:crypto); use pgBackRest::Version; #################################################################################################################################### @@ -563,7 +563,7 @@ sub hash # Set the new checksum $self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = - cryptoHashOne('sha1', JSON::PP->new()->canonical()->allow_nonref()->encode($self->{oContent})); + sha1_hex(JSON::PP->new()->canonical()->allow_nonref()->encode($self->{oContent})); return $self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM}; } diff --git a/lib/pgBackRest/LibCAuto.pm b/lib/pgBackRest/LibCAuto.pm index 6415a61d1..46ccc8da6 100644 --- a/lib/pgBackRest/LibCAuto.pm +++ b/lib/pgBackRest/LibCAuto.pm @@ -323,11 +323,6 @@ sub libcAutoExportTag 'cfgOptionTotal', ], - crypto => - [ - 'cryptoHashOne', - ], - debug => [ 'libcUvSize', diff --git a/lib/pgBackRest/Storage/Base.pm b/lib/pgBackRest/Storage/Base.pm index 885cc3add..f62be3908 100644 --- a/lib/pgBackRest/Storage/Base.pm +++ b/lib/pgBackRest/Storage/Base.pm @@ -52,8 +52,6 @@ use constant STORAGE_FILTER_CIPHER_BLOCK => 'pgBackRe push @EXPORT, qw(STORAGE_FILTER_CIPHER_BLOCK); use constant STORAGE_FILTER_GZ => 'pgBackRest::Storage::Filter::Gz'; push @EXPORT, qw(STORAGE_FILTER_GZ); -use constant STORAGE_FILTER_SHA => 'pgBackRest::Storage::Filter::Sha'; - push @EXPORT, qw(STORAGE_FILTER_SHA); #################################################################################################################################### # Capability constants diff --git a/lib/pgBackRest/Storage/Storage.pm b/lib/pgBackRest/Storage/Storage.pm index 4f560fd62..df1d7d893 100644 --- a/lib/pgBackRest/Storage/Storage.pm +++ b/lib/pgBackRest/Storage/Storage.pm @@ -9,6 +9,7 @@ use warnings FATAL => qw(all); use Carp qw(confess); use English '-no_match_vars'; +use Digest::SHA qw(sha1_hex); use File::Basename qw(dirname); use Fcntl qw(:mode); use File::stat qw{lstat}; @@ -172,17 +173,20 @@ sub hashSize my $lSize; # Is this an IO object or a file expression? - my $oFileIo = ref($xFileExp) ? $xFileExp : $self->openRead($xFileExp, {bIgnoreMissing => $bIgnoreMissing}); + my $rtContent = $self->get($xFileExp, {bIgnoreMissing => $bIgnoreMissing}); - # Add size and sha filters - $oFileIo->{oStorageCRead}->filterAdd(COMMON_IO_HANDLE, undef); - $oFileIo->{oStorageCRead}->filterAdd(STORAGE_FILTER_SHA, undef); - - # Read the file and set results if it exists - if ($self->{oStorageC}->readDrain($oFileIo->{oStorageCRead})) + if (defined($rtContent)) { - $strHash = $oFileIo->result(STORAGE_FILTER_SHA); - $lSize = $oFileIo->result(COMMON_IO_HANDLE); + if (defined($$rtContent)) + { + $strHash = sha1_hex($$rtContent); + $lSize = length($$rtContent); + } + else + { + $strHash = sha1_hex(''); + $lSize = 0; + } } # Return from function and log return values if any diff --git a/libc/LibC.xs b/libc/LibC.xs index 8eab85def..197762347 100644 --- a/libc/LibC.xs +++ b/libc/LibC.xs @@ -48,7 +48,6 @@ C includes These includes are from the src directory. There is no Perl-specific code in them. ***********************************************************************************************************************************/ -#include "common/crypto/common.h" #include "common/error.h" #include "common/io/io.h" #include "config/config.h" @@ -67,7 +66,6 @@ XSH includes These includes define data structures that are required for the C to Perl interface but are not part of the regular C source. ***********************************************************************************************************************************/ -#include "xs/crypto/hash.xsh" #include "xs/config/configTest.xsh" #include "xs/postgres/client.xsh" #include "xs/storage/storage.xsh" @@ -96,7 +94,6 @@ OUTPUT: INCLUDE: xs/config/config.xs INCLUDE: xs/config/configTest.xs INCLUDE: xs/config/define.xs -INCLUDE: xs/crypto/hash.xs INCLUDE: xs/postgres/client.xs INCLUDE: xs/storage/storage.xs INCLUDE: xs/storage/storageRead.xs diff --git a/libc/build/lib/pgBackRestLibC/Build.pm b/libc/build/lib/pgBackRestLibC/Build.pm index 34d5fc39f..edd6be115 100644 --- a/libc/build/lib/pgBackRestLibC/Build.pm +++ b/libc/build/lib/pgBackRestLibC/Build.pm @@ -94,13 +94,6 @@ my $rhExport = )], }, - 'crypto' => - { - &BLD_EXPORTTYPE_SUB => [qw( - cryptoHashOne - )], - }, - 'debug' => { &BLD_EXPORTTYPE_SUB => [qw( diff --git a/libc/xs/crypto/hash.xs b/libc/xs/crypto/hash.xs deleted file mode 100644 index 171735ab7..000000000 --- a/libc/xs/crypto/hash.xs +++ /dev/null @@ -1,31 +0,0 @@ -#################################################################################################################################### -# Cryptographic Hashes Perl Exports -# -# XS wrapper for functions in cipher/hash.c. -#################################################################################################################################### - -MODULE = pgBackRest::LibC PACKAGE = pgBackRest::LibC - -#################################################################################################################################### -SV * -cryptoHashOne(type, message) - const char *type - SV *message -CODE: - RETVAL = NULL; - - MEM_CONTEXT_XS_TEMP_BEGIN() - { - STRLEN messageSize; - const void *messagePtr = SvPV(message, messageSize); - - String *hash = bufHex(cryptoHashOne(strNew(type), BUF(messagePtr, messageSize))); - - RETVAL = newSV(strSize(hash)); - SvPOK_only(RETVAL); - strcpy((char *)SvPV_nolen(RETVAL), strPtr(hash)); - SvCUR_set(RETVAL, strSize(hash)); - } - MEM_CONTEXT_XS_TEMP_END(); -OUTPUT: - RETVAL diff --git a/libc/xs/crypto/hash.xsh b/libc/xs/crypto/hash.xsh deleted file mode 100644 index 98b367e48..000000000 --- a/libc/xs/crypto/hash.xsh +++ /dev/null @@ -1,4 +0,0 @@ -/*********************************************************************************************************************************** -Cryptographic Hashes XS Header -***********************************************************************************************************************************/ -#include "common/crypto/hash.h" diff --git a/libc/xs/storage/storage.xsh b/libc/xs/storage/storage.xsh index a00050f77..8271feb60 100644 --- a/libc/xs/storage/storage.xsh +++ b/libc/xs/storage/storage.xsh @@ -124,10 +124,6 @@ storageFilterXsAdd(IoFilterGroup *filterGroup, const String *filter, const Strin varUInt64Force(varLstGet(paramList, 0)) ? cipherModeEncrypt : cipherModeDecrypt, cipherType(varStr(varLstGet(paramList, 1))), BUFSTR(varStr(varLstGet(paramList, 2))), NULL)); } - else if (strEqZ(filter, "pgBackRest::Storage::Filter::Sha")) - { - ioFilterGroupAdd(filterGroup, cryptoHashNew(HASH_TYPE_SHA1_STR)); - } else if (strEqZ(filter, "pgBackRest::Common::Io::Handle")) { ioFilterGroupAdd(filterGroup, ioSizeNew()); @@ -151,11 +147,7 @@ storageFilterXsResult(const IoFilterGroup *filterGroup, const String *filter) { const Variant *result; - if (strEqZ(filter, "pgBackRest::Storage::Filter::Sha")) - { - result = ioFilterGroupResult(filterGroup, CRYPTO_HASH_FILTER_TYPE_STR); - } - else if (strEqZ(filter, "pgBackRest::Common::Io::Handle")) + if (strEqZ(filter, "pgBackRest::Common::Io::Handle")) { result = ioFilterGroupResult(filterGroup, SIZE_FILTER_TYPE_STR); } @@ -182,11 +174,7 @@ storageFilterXsResultAll(const IoFilterGroup *filterGroup) const String *filter = varStr(varLstGet(filterList, filterIdx)); const String *filterPerl = NULL; - if (strEq(filter, CRYPTO_HASH_FILTER_TYPE_STR)) - { - filterPerl = strNew("pgBackRest::Storage::Filter::Sha"); - } - else if (strEq(filter, SIZE_FILTER_TYPE_STR)) + if (strEq(filter, SIZE_FILTER_TYPE_STR)) { filterPerl = strNew("pgBackRest::Common::Io::Handle"); } diff --git a/test/lib/pgBackRestTest/Env/HostEnvTest.pm b/test/lib/pgBackRestTest/Env/HostEnvTest.pm index 4588a88b3..de3fe3fe9 100644 --- a/test/lib/pgBackRestTest/Env/HostEnvTest.pm +++ b/test/lib/pgBackRestTest/Env/HostEnvTest.pm @@ -11,6 +11,7 @@ use strict; use warnings FATAL => qw(all); use Carp qw(confess); +use Digest::SHA qw(sha1_hex); use Exporter qw(import); our @EXPORT = qw(); use Storable qw(dclone); @@ -19,7 +20,6 @@ use pgBackRest::Archive::Common; use pgBackRest::Common::Log; use pgBackRest::Config::Config; use pgBackRest::DbVersion; -use pgBackRest::LibC qw(:crypto); use pgBackRest::Protocol::Storage::Helper; use pgBackRestTest::Env::Host::HostBackupTest; @@ -492,7 +492,7 @@ sub walGenerateContentChecksum {name => 'hParam', required => false, trace => true}, ); - return cryptoHashOne('sha1', ${$self->walGenerateContent($strPgVersion, $hParam)}); + return sha1_hex(${$self->walGenerateContent($strPgVersion, $hParam)}); } #################################################################################################################################### @@ -513,7 +513,7 @@ sub walGenerate my $rtWalContent = $self->walGenerateContent($strPgVersion, {iSourceNo => $iSourceNo}); my $strWalFile = - "${strWalPath}/${strWalSegment}" . ($bChecksum ? '-' . cryptoHashOne('sha1', $rtWalContent) : '') . + "${strWalPath}/${strWalSegment}" . ($bChecksum ? '-' . sha1_hex($rtWalContent) : '') . (defined($bPartial) && $bPartial ? '.partial' : ''); # Put the WAL segment and the ready file