From 5ffab7f2784fef5c0ab05a81c0fb693c7fe1426f Mon Sep 17 00:00:00 2001 From: Brian Cox Date: Mon, 9 May 2016 10:34:53 -0700 Subject: [PATCH 1/2] Only posix_fadvise() on close if stream is nonnull, since fileno() segfaults (on Linux) if you try that. --- src/core/file_unix.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/file_unix.cpp b/src/core/file_unix.cpp index 5526025..c969398 100644 --- a/src/core/file_unix.cpp +++ b/src/core/file_unix.cpp @@ -1,4 +1,4 @@ -// + // The developer of the original code and/or files is Tripwire, Inc. // Portions created by Tripwire, Inc. are copyright (C) 2000 Tripwire, // Inc. Tripwire is a registered trademark of Tripwire, Inc. All rights @@ -229,12 +229,12 @@ void cFile::Open( const TSTRING& sFileNameC, uint32 flags ) /////////////////////////////////////////////////////////////////////////// void cFile::Close() //throw(eFile) { -#ifdef HAVE_POSIX_FADVISE - posix_fadvise(fileno(mpData->mpCurrStream),0,0, POSIX_FADV_DONTNEED); -#endif - if(mpData->mpCurrStream != NULL) { +#ifdef HAVE_POSIX_FADVISE + posix_fadvise(fileno(mpData->mpCurrStream),0,0, POSIX_FADV_DONTNEED); +#endif + fclose( mpData->mpCurrStream ); mpData->mpCurrStream = NULL; } From 9214316bbdd007d4eba05bd6d4ef219084f3085a Mon Sep 17 00:00:00 2001 From: Brian Cox Date: Mon, 16 May 2016 19:32:38 -0700 Subject: [PATCH 2/2] Align buffer on page boundary if user enables HASH_DIRECT (formerly DIRECT_IO); let user specify num blocks per read. --- src/fco/signature.cpp | 30 +++++++++++++++++++++++------- src/fco/signature.h | 11 ++++++++++- src/tripwire/twcmdline.cpp | 16 +++++++++++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/fco/signature.cpp b/src/fco/signature.cpp index 15991fc..836e8a5 100644 --- a/src/fco/signature.cpp +++ b/src/fco/signature.cpp @@ -97,7 +97,22 @@ iFCOProp::CmpResult iSignature::Compare(const iFCOProp* rhs, Op op) const return (op == iFCOProp::OP_NE) ? iFCOProp::CMP_TRUE : iFCOProp::CMP_FALSE; } -bool cArchiveSigGen::mHex = false; +bool cArchiveSigGen::s_hex = false; +int32 cArchiveSigGen::s_blocks = 1; +int32 cArchiveSigGen::s_bytes = iSignature::SUGGESTED_BLOCK_SIZE; +byte* cArchiveSigGen::s_buf = 0; +byte* cArchiveSigGen::s_base = 0; +bool cArchiveSigGen::s_direct = false; + + +void cArchiveSigGen::SetBlocks( int32 n ) +{ + s_blocks=n; + s_base = new byte[iSignature::SUGGESTED_BLOCK_SIZE * (s_blocks+1)]; + unsigned long nMod = (unsigned long)s_base % iSignature::SUGGESTED_BLOCK_SIZE; + s_buf = s_base + (iSignature::SUGGESTED_BLOCK_SIZE - nMod); + s_bytes = iSignature::SUGGESTED_BLOCK_SIZE * s_blocks; +} void cArchiveSigGen::AddSig( iSignature* pSig ) { @@ -107,10 +122,11 @@ void cArchiveSigGen::AddSig( iSignature* pSig ) void cArchiveSigGen::CalculateSignatures( cArchive& a ) { byte abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; - const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; int cbRead; container_type::size_type i; + byte* pBuf = s_buf ? s_buf : abBuf; + // init hash for( i = 0; i < mSigList.size(); i++ ) mSigList[i]->Init(); @@ -118,12 +134,12 @@ void cArchiveSigGen::CalculateSignatures( cArchive& a ) // hash data do { - cbRead = a.ReadBlob( abBuf, cbToRead ); + cbRead = a.ReadBlob( pBuf, s_bytes ); for( i = 0; i < mSigList.size(); i++ ) - mSigList[i]->Update( abBuf, cbRead ); + mSigList[i]->Update( pBuf, cbRead ); } - while( cbRead == cbToRead ); + while( cbRead == s_bytes ); // finalize hash for( i = 0; i < mSigList.size(); i++ ) @@ -132,12 +148,12 @@ void cArchiveSigGen::CalculateSignatures( cArchive& a ) bool cArchiveSigGen::Hex() { - return mHex; + return s_hex; } void cArchiveSigGen::SetHex(bool hex) { - mHex = hex; + s_hex = hex; } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/fco/signature.h b/src/fco/signature.h index 61fd50d..cbd07c9 100644 --- a/src/fco/signature.h +++ b/src/fco/signature.h @@ -150,6 +150,10 @@ public: static bool Hex(); static void SetHex(bool); + static void SetBlocks( int32 n ); + static bool UseDirectIO() { return s_direct; } + static void SetUseDirectIO( bool b ) { s_direct = b; } + private: // don't let C++ create these functions cArchiveSigGen( const cArchiveSigGen& ); @@ -158,7 +162,12 @@ private: typedef std::vector< iSignature* > container_type; container_type mSigList; - static bool mHex; + static bool s_hex; + static int32 s_blocks; + static int32 s_bytes; + static byte* s_buf; + static byte* s_base; + static bool s_direct; }; diff --git a/src/tripwire/twcmdline.cpp b/src/tripwire/twcmdline.cpp index 10c6369..f6fb97b 100644 --- a/src/tripwire/twcmdline.cpp +++ b/src/tripwire/twcmdline.cpp @@ -433,14 +433,24 @@ static void FillOutConfigInfo(cTWModeCommon* pModeInfo, const cConfigFile& cf) pModeInfo->mbCrossFileSystems = false; } - if(cf.Lookup(TSTRING(_T("DIRECT_IO")), str)) + int blocks = 0; + if (cf.Lookup(TSTRING(_T("HASH_DIRECT")), str)) { if (_tcsicmp(str.c_str(), _T("true")) == 0) + { pModeInfo->mbDirectIO = true; - else - pModeInfo->mbDirectIO = false; + cArchiveSigGen::SetUseDirectIO(true); + blocks = 1; + } } + if (cf.Lookup(TSTRING(_T("HASH_BLOCKS")), str)) + { + blocks = _ttoi( str.c_str() ); + } + + if( blocks > 0 ) + cArchiveSigGen::SetBlocks(blocks); // // turn all of the file names into full paths (they're relative to the exe dir) //