[Libreoffice-commits] core.git: include/tools tools/source

Stephan Bergmann (via logerrit) logerrit at kemper.freedesktop.org
Thu Dec 12 12:20:43 UTC 2019


 include/tools/stream.hxx        |    2 
 tools/source/stream/strmunx.cxx |  105 +++++++---------------------------------
 tools/source/stream/strmwnt.cxx |   44 +++++-----------
 3 files changed, 34 insertions(+), 117 deletions(-)

New commits:
commit 2bf22c8d2642e30fdedab69b7437ae83deaec014
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Thu Dec 12 11:18:31 2019 +0100
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Thu Dec 12 13:19:10 2019 +0100

    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 at redhat.com>

diff --git a/include/tools/stream.hxx b/include/tools/stream.hxx
index 33b2e986b573..0f17c37c4958 100644
--- a/include/tools/stream.hxx
+++ b/include/tools/stream.hxx
@@ -582,8 +582,6 @@ private:
     SvFileStream (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();
     void UnlockFile();
 
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


More information about the Libreoffice-commits mailing list