SvFileStream::Lock/UnlockRange are only called from within SvFileStream itself
...and only for whole-file locking, so simplify the implementations in strmunx.cx and strmwnt.cxx accordingly Change-Id: I973e0ea41f246ad614232b107c8bf152073867be Reviewed-on: https://gerrit.libreoffice.org/85039 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
@@ -582,8 +582,6 @@ private:
|
|||||||
SvFileStream (const SvFileStream&) = delete;
|
SvFileStream (const SvFileStream&) = delete;
|
||||||
SvFileStream & operator= (const SvFileStream&) = delete;
|
SvFileStream & operator= (const SvFileStream&) = delete;
|
||||||
|
|
||||||
bool LockRange( sal_uInt64 nByteOffset, std::size_t nBytes );
|
|
||||||
bool UnlockRange( sal_uInt64 nByteOffset, std::size_t nBytes );
|
|
||||||
bool LockFile();
|
bool LockFile();
|
||||||
void UnlockFile();
|
void UnlockFile();
|
||||||
|
|
||||||
|
@@ -17,13 +17,12 @@
|
|||||||
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <tools/stream.hxx>
|
#include <tools/stream.hxx>
|
||||||
#include <vector>
|
#include <map>
|
||||||
|
|
||||||
#include <osl/mutex.hxx>
|
#include <osl/mutex.hxx>
|
||||||
#include <osl/thread.h>
|
#include <osl/thread.h>
|
||||||
@@ -41,51 +40,9 @@ namespace {
|
|||||||
|
|
||||||
struct LockMutex : public rtl::Static< osl::Mutex, LockMutex > {};
|
struct LockMutex : public rtl::Static< osl::Mutex, LockMutex > {};
|
||||||
|
|
||||||
struct InternalStreamLock
|
struct Locks : public rtl::Static< std::map<SvFileStream const *, osl::DirectoryItem>, Locks > {};
|
||||||
{
|
|
||||||
sal_uInt64 m_nStartPos;
|
|
||||||
sal_uInt64 m_nEndPos;
|
|
||||||
SvFileStream* m_pStream;
|
|
||||||
osl::DirectoryItem m_aItem;
|
|
||||||
|
|
||||||
InternalStreamLock( sal_uInt64, sal_uInt64, SvFileStream* );
|
bool lockFile( SvFileStream* pStream )
|
||||||
~InternalStreamLock();
|
|
||||||
};
|
|
||||||
|
|
||||||
struct LockList : public rtl::Static< std::vector<InternalStreamLock>, LockList > {};
|
|
||||||
|
|
||||||
InternalStreamLock::InternalStreamLock(
|
|
||||||
sal_uInt64 const nStart,
|
|
||||||
sal_uInt64 const nEnd,
|
|
||||||
SvFileStream* pStream ) :
|
|
||||||
m_nStartPos( nStart ),
|
|
||||||
m_nEndPos( nEnd ),
|
|
||||||
m_pStream( pStream )
|
|
||||||
{
|
|
||||||
(void)osl::DirectoryItem::get( m_pStream->GetFileName(), m_aItem );
|
|
||||||
#if OSL_DEBUG_LEVEL > 1
|
|
||||||
OString aFileName(OUStringToOString(m_pStream->GetFileName(),
|
|
||||||
osl_getThreadTextEncoding()));
|
|
||||||
fprintf( stderr, "locked %s", aFileName.getStr() );
|
|
||||||
if( m_nStartPos || m_nEndPos )
|
|
||||||
fprintf(stderr, " [ %ld ... %ld ]", m_nStartPos, m_nEndPos );
|
|
||||||
fprintf( stderr, "\n" );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
InternalStreamLock::~InternalStreamLock()
|
|
||||||
{
|
|
||||||
#if OSL_DEBUG_LEVEL > 1
|
|
||||||
OString aFileName(OUStringToOString(m_pStream->GetFileName(),
|
|
||||||
osl_getThreadTextEncoding()));
|
|
||||||
fprintf( stderr, "unlocked %s", aFileName.getStr() );
|
|
||||||
if( m_nStartPos || m_nEndPos )
|
|
||||||
fprintf(stderr, " [ %ld ... %ld ]", m_nStartPos, m_nEndPos );
|
|
||||||
fprintf( stderr, "\n" );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
bool lockFile( sal_uInt64 const nStart, sal_uInt64 const nEnd, SvFileStream* pStream )
|
|
||||||
{
|
{
|
||||||
osl::DirectoryItem aItem;
|
osl::DirectoryItem aItem;
|
||||||
if (osl::DirectoryItem::get( pStream->GetFileName(), aItem) != osl::FileBase::E_None )
|
if (osl::DirectoryItem::get( pStream->GetFileName(), aItem) != osl::FileBase::E_None )
|
||||||
@@ -104,12 +61,12 @@ bool lockFile( sal_uInt64 const nStart, sal_uInt64 const nEnd, SvFileStream* pSt
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
osl::MutexGuard aGuard( LockMutex::get() );
|
osl::MutexGuard aGuard( LockMutex::get() );
|
||||||
std::vector<InternalStreamLock> &rLockList = LockList::get();
|
auto &rLocks = Locks::get();
|
||||||
for( const auto& rLock : rLockList )
|
for( const auto& [rLockStream, rLockItem] : rLocks )
|
||||||
{
|
{
|
||||||
if( aItem.isIdenticalTo( rLock.m_aItem ) )
|
if( aItem.isIdenticalTo( rLockItem ) )
|
||||||
{
|
{
|
||||||
StreamMode nLockMode = rLock.m_pStream->GetStreamMode();
|
StreamMode nLockMode = rLockStream->GetStreamMode();
|
||||||
StreamMode nNewMode = pStream->GetStreamMode();
|
StreamMode nNewMode = pStream->GetStreamMode();
|
||||||
bool bDenyByOptions = (nLockMode & StreamMode::SHARE_DENYALL) ||
|
bool bDenyByOptions = (nLockMode & StreamMode::SHARE_DENYALL) ||
|
||||||
( (nLockMode & StreamMode::SHARE_DENYWRITE) && (nNewMode & StreamMode::WRITE) ) ||
|
( (nLockMode & StreamMode::SHARE_DENYWRITE) && (nNewMode & StreamMode::WRITE) ) ||
|
||||||
@@ -117,31 +74,19 @@ bool lockFile( sal_uInt64 const nStart, sal_uInt64 const nEnd, SvFileStream* pSt
|
|||||||
|
|
||||||
if( bDenyByOptions )
|
if( bDenyByOptions )
|
||||||
{
|
{
|
||||||
if( rLock.m_nStartPos == 0 && rLock.m_nEndPos == 0 ) // whole file is already locked
|
return false; // file is already locked
|
||||||
return false;
|
|
||||||
if( nStart == 0 && nEnd == 0) // cannot lock whole file
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if( ( nStart < rLock.m_nStartPos && nEnd > rLock.m_nStartPos ) ||
|
|
||||||
( nStart < rLock.m_nEndPos && nEnd > rLock.m_nEndPos ) )
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rLockList.push_back( InternalStreamLock( nStart, nEnd, pStream ) );
|
rLocks[pStream] = aItem;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unlockFile( sal_uInt64 const nStart, sal_uInt64 const nEnd, SvFileStream const * pStream )
|
void unlockFile( SvFileStream const * pStream )
|
||||||
{
|
{
|
||||||
osl::MutexGuard aGuard( LockMutex::get() );
|
osl::MutexGuard aGuard( LockMutex::get() );
|
||||||
std::vector<InternalStreamLock> &rLockList = LockList::get();
|
auto &rLocks = Locks::get();
|
||||||
rLockList.erase(std::remove_if(rLockList.begin(), rLockList.end(),
|
rLocks.erase(pStream);
|
||||||
[&pStream, &nStart, &nEnd](const InternalStreamLock& rLock) {
|
|
||||||
return rLock.m_pStream == pStream
|
|
||||||
&& ((nStart == 0 && nEnd == 0)
|
|
||||||
|| (rLock.m_nStartPos == nStart && rLock.m_nEndPos == nEnd));
|
|
||||||
}), rLockList.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -346,7 +291,7 @@ void SvFileStream::FlushData()
|
|||||||
// does not exist locally
|
// does not exist locally
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
bool SvFileStream::LockFile()
|
||||||
{
|
{
|
||||||
int nLockMode = 0;
|
int nLockMode = 0;
|
||||||
|
|
||||||
@@ -383,11 +328,11 @@ bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
|||||||
if (!nLockMode)
|
if (!nLockMode)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if( !lockFile( nByteOffset, nByteOffset+nBytes, this ) )
|
if( !lockFile( this ) )
|
||||||
{
|
{
|
||||||
#if OSL_DEBUG_LEVEL > 1
|
#if OSL_DEBUG_LEVEL > 1
|
||||||
fprintf( stderr, "InternalLock on %s [ %ld ... %ld ] failed\n",
|
fprintf( stderr, "InternalLock on %s failed\n",
|
||||||
OUStringToOString(aFilename, osl_getThreadTextEncoding()).getStr(), nByteOffset, nByteOffset+nBytes );
|
OUStringToOString(aFilename, osl_getThreadTextEncoding()).getStr() );
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -395,24 +340,12 @@ bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SvFileStream::UnlockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
|
||||||
{
|
|
||||||
if ( ! IsOpen() )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
unlockFile( nByteOffset, nByteOffset+nBytes, this );
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SvFileStream::LockFile()
|
|
||||||
{
|
|
||||||
return LockRange( 0UL, 0UL );
|
|
||||||
}
|
|
||||||
|
|
||||||
void SvFileStream::UnlockFile()
|
void SvFileStream::UnlockFile()
|
||||||
{
|
{
|
||||||
UnlockRange( 0UL, 0UL );
|
if ( ! IsOpen() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
unlockFile( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
void SvFileStream::Open( const OUString& rFilename, StreamMode nOpenMode )
|
void SvFileStream::Open( const OUString& rFilename, StreamMode nOpenMode )
|
||||||
|
@@ -193,39 +193,20 @@ void SvFileStream::FlushData()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
|
||||||
{
|
|
||||||
bool bRetVal = false;
|
|
||||||
if( IsOpen() )
|
|
||||||
{
|
|
||||||
bRetVal = ::LockFile(pInstanceData->hFile,nByteOffset,0L,nBytes,0L );
|
|
||||||
if( !bRetVal )
|
|
||||||
SetError(::GetSvError(GetLastError()));
|
|
||||||
}
|
|
||||||
return bRetVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SvFileStream::UnlockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
|
|
||||||
{
|
|
||||||
bool bRetVal = false;
|
|
||||||
if( IsOpen() )
|
|
||||||
{
|
|
||||||
bRetVal = ::UnlockFile(pInstanceData->hFile,nByteOffset,0L,nBytes,0L );
|
|
||||||
if( !bRetVal )
|
|
||||||
SetError(::GetSvError(GetLastError()));
|
|
||||||
}
|
|
||||||
return bRetVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SvFileStream::LockFile()
|
bool SvFileStream::LockFile()
|
||||||
{
|
{
|
||||||
bool bRetVal = false;
|
bool bRetVal = false;
|
||||||
if( !nLockCounter )
|
if( !nLockCounter )
|
||||||
{
|
{
|
||||||
if( LockRange( 0L, LONG_MAX ) )
|
if( IsOpen() )
|
||||||
{
|
{
|
||||||
nLockCounter = 1;
|
bRetVal = ::LockFile(pInstanceData->hFile,0L,0L,LONG_MAX,0L );
|
||||||
bRetVal = true;
|
if( bRetVal )
|
||||||
|
{
|
||||||
|
nLockCounter = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
SetError(::GetSvError(GetLastError()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -242,9 +223,14 @@ void SvFileStream::UnlockFile()
|
|||||||
{
|
{
|
||||||
if( nLockCounter == 1)
|
if( nLockCounter == 1)
|
||||||
{
|
{
|
||||||
if( UnlockRange( 0L, LONG_MAX ) )
|
if( IsOpen() )
|
||||||
{
|
{
|
||||||
nLockCounter = 0;
|
if( ::UnlockFile(pInstanceData->hFile,0L,0L,LONG_MAX,0L ) )
|
||||||
|
{
|
||||||
|
nLockCounter = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
SetError(::GetSvError(GetLastError()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user