summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2019-12-12 11:18:31 +0100
committerStephan Bergmann <sbergman@redhat.com>2019-12-12 13:19:10 +0100
commit2bf22c8d2642e30fdedab69b7437ae83deaec014 (patch)
tree9d7704f30cc30d4a4ef09a773b1f64e046cdc35f /tools
parentaf35dfb2e31c24dd1a43d9ae3e0f393b75108228 (diff)
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>
Diffstat (limited to 'tools')
-rw-r--r--tools/source/stream/strmunx.cxx105
-rw-r--r--tools/source/stream/strmwnt.cxx44
2 files changed, 34 insertions, 115 deletions
diff --git a/tools/source/stream/strmunx.cxx b/tools/source/stream/strmunx.cxx
index 1b02435db0a3..a0cfd7c09d74 100644
--- a/tools/source/stream/strmunx.cxx
+++ b/tools/source/stream/strmunx.cxx
@@ -17,13 +17,12 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
-#include <algorithm>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <tools/stream.hxx>
-#include <vector>
+#include <map>
#include <osl/mutex.hxx>
#include <osl/thread.h>
@@ -41,51 +40,9 @@ namespace {
struct LockMutex : public rtl::Static< osl::Mutex, LockMutex > {};
-struct InternalStreamLock
-{
- sal_uInt64 m_nStartPos;
- sal_uInt64 m_nEndPos;
- SvFileStream* m_pStream;
- osl::DirectoryItem m_aItem;
-
- InternalStreamLock( sal_uInt64, sal_uInt64, SvFileStream* );
- ~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
-}
+struct Locks : public rtl::Static< std::map<SvFileStream const *, osl::DirectoryItem>, Locks > {};
-bool lockFile( sal_uInt64 const nStart, sal_uInt64 const nEnd, SvFileStream* pStream )
+bool lockFile( SvFileStream* pStream )
{
osl::DirectoryItem aItem;
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;
osl::MutexGuard aGuard( LockMutex::get() );
- std::vector<InternalStreamLock> &rLockList = LockList::get();
- for( const auto& rLock : rLockList )
+ auto &rLocks = Locks::get();
+ 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();
bool bDenyByOptions = (nLockMode & StreamMode::SHARE_DENYALL) ||
( (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( rLock.m_nStartPos == 0 && rLock.m_nEndPos == 0 ) // whole 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;
+ return false; // file is already locked
}
}
}
- rLockList.push_back( InternalStreamLock( nStart, nEnd, pStream ) );
+ rLocks[pStream] = aItem;
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() );
- std::vector<InternalStreamLock> &rLockList = LockList::get();
- rLockList.erase(std::remove_if(rLockList.begin(), rLockList.end(),
- [&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());
+ auto &rLocks = Locks::get();
+ rLocks.erase(pStream);
}
}
@@ -346,7 +291,7 @@ void SvFileStream::FlushData()
// does not exist locally
}
-bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
+bool SvFileStream::LockFile()
{
int nLockMode = 0;
@@ -383,11 +328,11 @@ bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
if (!nLockMode)
return true;
- if( !lockFile( nByteOffset, nByteOffset+nBytes, this ) )
+ if( !lockFile( this ) )
{
#if OSL_DEBUG_LEVEL > 1
- fprintf( stderr, "InternalLock on %s [ %ld ... %ld ] failed\n",
- OUStringToOString(aFilename, osl_getThreadTextEncoding()).getStr(), nByteOffset, nByteOffset+nBytes );
+ fprintf( stderr, "InternalLock on %s failed\n",
+ OUStringToOString(aFilename, osl_getThreadTextEncoding()).getStr() );
#endif
return false;
}
@@ -395,24 +340,12 @@ bool SvFileStream::LockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
return true;
}
-bool SvFileStream::UnlockRange(sal_uInt64 const nByteOffset, std::size_t nBytes)
+void SvFileStream::UnlockFile()
{
if ( ! IsOpen() )
- return false;
-
- unlockFile( nByteOffset, nByteOffset+nBytes, this );
-
- return true;
-}
-
-bool SvFileStream::LockFile()
-{
- return LockRange( 0UL, 0UL );
-}
+ return;
-void SvFileStream::UnlockFile()
-{
- UnlockRange( 0UL, 0UL );
+ unlockFile( this );
}
void SvFileStream::Open( const OUString& rFilename, StreamMode nOpenMode )
diff --git a/tools/source/stream/strmwnt.cxx b/tools/source/stream/strmwnt.cxx
index fc28dad43514..23fa71526447 100644
--- a/tools/source/stream/strmwnt.cxx
+++ b/tools/source/stream/strmwnt.cxx
@@ -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 bRetVal = false;
if( !nLockCounter )
{
- if( LockRange( 0L, LONG_MAX ) )
+ if( IsOpen() )
{
- nLockCounter = 1;
- bRetVal = true;
+ bRetVal = ::LockFile(pInstanceData->hFile,0L,0L,LONG_MAX,0L );
+ if( bRetVal )
+ {
+ nLockCounter = 1;
+ }
+ else
+ SetError(::GetSvError(GetLastError()));
}
}
else
@@ -242,9 +223,14 @@ void SvFileStream::UnlockFile()
{
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