diff --git a/ftk/src/ftkfsys.cpp b/ftk/src/ftkfsys.cpp index 4c53998..bedf9d0 100644 --- a/ftk/src/ftkfsys.cpp +++ b/ftk/src/ftkfsys.cpp @@ -2754,6 +2754,7 @@ void F_FileHdl::initCommonData( void) m_bOpenedExclusive = FALSE; m_bOpenedInAsyncMode = FALSE; m_bDoDirectIO = FALSE; + m_numAsyncPending = 0; } /***************************************************************************** @@ -2761,6 +2762,8 @@ Desc: ******************************************************************************/ void F_FileHdl::freeCommonData( void) { + f_assert( !m_numAsyncPending); + if( m_pucAlignedBuff) { f_freeAlignedBuffer( (void **)&m_pucAlignedBuff); @@ -3033,6 +3036,8 @@ RCODE F_FileAsyncClient::prepareForAsync( m_pIOBuffer->setPending(); } + f_atomicInc( &m_pFileHdl->m_numAsyncPending); + Exit: return( rc); @@ -3141,6 +3146,8 @@ void FLMAPI F_FileAsyncClient::notifyComplete( if( m_pFileHdl) { + f_assert( m_pFileHdl->m_numAsyncPending); + f_atomicDec( &m_pFileHdl->m_numAsyncPending); m_pFileHdl->Release(); m_pFileHdl = NULL; } @@ -3268,6 +3275,10 @@ RCODE F_FileHdl::directRead( { ui64ReadOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64ReadOffset; + } // This loop does multiple reads (if necessary) to get all of the // data. It uses aligned buffers and reads at sector offsets. @@ -3423,6 +3434,10 @@ RCODE F_FileHdl::directWrite( { ui64WriteOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64WriteOffset; + } if( pIOBuffer) { diff --git a/ftk/src/ftknlm.cpp b/ftk/src/ftknlm.cpp index d46e8c2..591b442 100644 --- a/ftk/src/ftknlm.cpp +++ b/ftk/src/ftknlm.cpp @@ -3014,6 +3014,10 @@ RCODE F_FileHdl::lowLevelRead( { ui64ReadOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64ReadOffset; + } if( !pvBuffer) { @@ -3335,6 +3339,10 @@ RCODE F_FileHdl::lowLevelWrite( { ui64WriteOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64WriteOffset; + } if( !pvBuffer) { diff --git a/ftk/src/ftksys.h b/ftk/src/ftksys.h index 4a02fd1..3cd21fe 100644 --- a/ftk/src/ftksys.h +++ b/ftk/src/ftksys.h @@ -768,6 +768,7 @@ FLMBOOL m_bOpenedExclusive; FLMBOOL m_bDoDirectIO; FLMBOOL m_bOpenedInAsyncMode; + FLMATOMIC m_numAsyncPending; #if defined( FLM_WIN) HANDLE m_hFile; diff --git a/ftk/src/ftkunix.cpp b/ftk/src/ftkunix.cpp index 8591a30..28b1633 100644 --- a/ftk/src/ftkunix.cpp +++ b/ftk/src/ftkunix.cpp @@ -480,6 +480,10 @@ RCODE F_FileHdl::lowLevelRead( { ui64ReadOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64ReadOffset; + } if( !pvBuffer) { @@ -646,6 +650,7 @@ RCODE F_FileHdl::lowLevelWrite( FLMUINT uiBytesWritten = 0; F_FileAsyncClient * pAsyncClient = NULL; FLMBOOL bWaitForWrite = FALSE; + FLMBYTE * pucExtendBuffer = NULL; if( pIOBuffer && pvBuffer && pvBuffer != pIOBuffer->getBufferPtr()) { @@ -657,8 +662,12 @@ RCODE F_FileHdl::lowLevelWrite( { ui64WriteOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64WriteOffset; + } - if( m_bDoDirectIO) + if( m_bDoDirectIO && !m_numAsyncPending) { FLMUINT64 ui64CurrFileSize; FLMUINT uiTotalBytesToExtend; @@ -698,19 +707,25 @@ RCODE F_FileHdl::lowLevelWrite( if( uiTotalBytesToExtend) { - FLMUINT uiCurrBytesToExtend; FLMINT iBytesWritten; + FLMUINT uiCurrBytesToExtend; + FLMUINT uiExtendBufferSize; - f_memset( m_pucAlignedBuff, 0, m_uiAlignedBuffSize); + uiExtendBufferSize = f_min( uiTotalBytesToExtend, 64 * 1024); + + if( RC_BAD( rc = f_allocAlignedBuffer( + uiExtendBufferSize, &pucExtendBuffer))) + { + goto Exit; + } while( uiTotalBytesToExtend) { uiCurrBytesToExtend = f_min( - uiTotalBytesToExtend, m_uiAlignedBuffSize); + uiTotalBytesToExtend, uiExtendBufferSize); - if( (iBytesWritten = pwrite( m_fd, - m_pucAlignedBuff, uiCurrBytesToExtend, - ui64CurrFileSize)) == -1) + if( (iBytesWritten = pwrite( m_fd, pucExtendBuffer, + uiCurrBytesToExtend, ui64CurrFileSize)) == -1) { if( errno == EINTR) { @@ -894,6 +909,11 @@ Exit: f_assert( RC_BAD( rc)); pIOBuffer->notifyComplete( rc); } + + if( pucExtendBuffer) + { + f_freeAlignedBuffer( &pucExtendBuffer); + } if( puiBytesWritten) { diff --git a/ftk/src/ftkwin.cpp b/ftk/src/ftkwin.cpp index d908791..2647fba 100644 --- a/ftk/src/ftkwin.cpp +++ b/ftk/src/ftkwin.cpp @@ -339,6 +339,10 @@ RCODE F_FileHdl::lowLevelRead( { ui64ReadOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64ReadOffset; + } if( !pvBuffer) { @@ -491,6 +495,10 @@ RCODE F_FileHdl::lowLevelWrite( { ui64WriteOffset = m_ui64CurrentPos; } + else + { + m_ui64CurrentPos = ui64WriteOffset; + } if( m_bDoDirectIO) {