From da6d8944d7f00c1fddaa136f9f9b2ab126a24b44 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 23 Mar 2005 21:26:07 +0000 Subject: [PATCH] Fix RageFileObjDeflate::FlushInternal not flushing correctly when 4k or more needs to be flushed. --- stepmania/src/RageFileDriverDeflate.cpp | 32 ++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/stepmania/src/RageFileDriverDeflate.cpp b/stepmania/src/RageFileDriverDeflate.cpp index c2fa386cb2..b1ef8d6161 100644 --- a/stepmania/src/RageFileDriverDeflate.cpp +++ b/stepmania/src/RageFileDriverDeflate.cpp @@ -227,10 +227,13 @@ RageFileObjDeflate::~RageFileObjDeflate() int RageFileObjDeflate::WriteInternal( const void *pBuffer, size_t iBytes ) { + if( iBytes == 0 ) + return 0; + m_pDeflate->next_in = (Bytef*) pBuffer; m_pDeflate->avail_in = iBytes; - while( m_pDeflate->avail_in > 0 ) + while( 1 ) { char buf[1024*4]; m_pDeflate->next_out = (Bytef *) buf; @@ -243,13 +246,22 @@ int RageFileObjDeflate::WriteInternal( const void *pBuffer, size_t iBytes ) if( m_pDeflate->avail_out < sizeof(buf) ) { - int iRet = m_pFile->Write( buf, sizeof(buf)-m_pDeflate->avail_out ); + int iBytes = sizeof(buf)-m_pDeflate->avail_out; + int iRet = m_pFile->Write( buf, iBytes ); if( iRet == -1 ) { SetError( m_pFile->GetError() ); return -1; } + if( iRet < iBytes ) + { + SetError( "Partial write" ); + return -1; + } } + + if( m_pDeflate->avail_in == 0 && m_pDeflate->avail_out != 0 ) + break; } return iBytes; @@ -272,17 +284,24 @@ int RageFileObjDeflate::FlushInternal() if( err != Z_OK && err != Z_STREAM_END ) FAIL_M( ssprintf("deflate: err %i", err) ); - if( m_pDeflate->avail_out > 0 ) + if( m_pDeflate->avail_out < sizeof(buf) ) { - int iRet = m_pFile->Write( buf, sizeof(buf)-m_pDeflate->avail_out ); + int iBytes = sizeof(buf)-m_pDeflate->avail_out; + int iRet = m_pFile->Write( buf, iBytes ); if( iRet == -1 ) { SetError( m_pFile->GetError() ); return -1; } + LOG->Trace("FlushInternal: wrote %i/%i", iRet, iBytes); + if( iRet < iBytes ) + { + SetError( "Partial write" ); + return -1; + } } - if( err == Z_STREAM_END ) + if( err == Z_STREAM_END && m_pDeflate->avail_out != 0 ) return m_pFile->Flush(); } } @@ -484,7 +503,8 @@ int RageFileObjGzip::Finish() return -1; } - return this->Flush(); + /* Flush the CRC and wize that we just wrote directly to the file. */ + return m_pFile->Flush(); } #include "RageFileDriverMemory.h"