LCOV - code coverage report
Current view: top level - tools/source/stream - strmunx.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 149 230 64.8 %
Date: 2014-11-03 Functions: 20 25 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stdio.h>
      21             : #include <string.h>
      22             : #include <sys/stat.h>
      23             : #include <fcntl.h>
      24             : #include <errno.h>
      25             : #include <unistd.h>
      26             : #include <limits.h>
      27             : 
      28             : #include <tools/stream.hxx>
      29             : #include <vector>
      30             : 
      31             : #include <osl/mutex.hxx>
      32             : #include <osl/thread.h>
      33             : 
      34             : // class FileBase
      35             : #include <osl/file.hxx>
      36             : #include <osl/detail/file.h>
      37             : #include <rtl/instance.hxx>
      38             : #include <rtl/strbuf.hxx>
      39             : 
      40             : using namespace osl;
      41             : 
      42             : // InternalLock ----------------------------------------------------------------
      43             : 
      44             : namespace {
      45             : 
      46             : struct LockMutex : public rtl::Static< osl::Mutex, LockMutex > {};
      47             : 
      48           0 : struct InternalStreamLock
      49             : {
      50             :     sal_Size           m_nStartPos;
      51             :     sal_Size           m_nEndPos;
      52             :     SvFileStream*      m_pStream;
      53             :     osl::DirectoryItem m_aItem;
      54             : 
      55             :     InternalStreamLock( sal_Size, sal_Size, SvFileStream* );
      56             :     ~InternalStreamLock();
      57             : };
      58             : 
      59             : struct LockList : public rtl::Static< std::vector<InternalStreamLock>, LockList > {};
      60             : 
      61           0 : InternalStreamLock::InternalStreamLock(
      62             :     sal_Size nStart,
      63             :     sal_Size nEnd,
      64             :     SvFileStream* pStream ) :
      65             :         m_nStartPos( nStart ),
      66             :         m_nEndPos( nEnd ),
      67           0 :         m_pStream( pStream )
      68             : {
      69           0 :     osl::DirectoryItem::get( m_pStream->GetFileName(), m_aItem );
      70             : #if OSL_DEBUG_LEVEL > 1
      71             :     OString aFileName(OUStringToOString(m_pStream->GetFileName(),
      72             :                                                   osl_getThreadTextEncoding()));
      73             :     fprintf( stderr, "locked %s", aFileName.getStr() );
      74             :     if( m_nStartPos || m_nEndPos )
      75             :         fprintf(stderr, " [ %ld ... %ld ]", m_nStartPos, m_nEndPos );
      76             :     fprintf( stderr, "\n" );
      77             : #endif
      78           0 : }
      79             : 
      80           0 : InternalStreamLock::~InternalStreamLock()
      81             : {
      82             : #if OSL_DEBUG_LEVEL > 1
      83             :     OString aFileName(OUStringToOString(m_pStream->GetFileName(),
      84             :                                                   osl_getThreadTextEncoding()));
      85             :     fprintf( stderr, "unlocked %s", aFileName.getStr() );
      86             :     if( m_nStartPos || m_nEndPos )
      87             :         fprintf(stderr, " [ %ld ... %ld ]", m_nStartPos, m_nEndPos );
      88             :     fprintf( stderr, "\n" );
      89             : #endif
      90           0 : }
      91             : 
      92        2959 : bool lockFile( sal_Size nStart, sal_Size nEnd, SvFileStream* pStream )
      93             : {
      94        2959 :     osl::DirectoryItem aItem;
      95        2959 :     if (osl::DirectoryItem::get( pStream->GetFileName(), aItem) != osl::FileBase::E_None )
      96             :     {
      97             :         SAL_INFO("tools.stream", "Failed to lookup stream for locking");
      98        2959 :         return true;
      99             :     }
     100             : 
     101           0 :     osl::FileStatus aStatus( osl_FileStatus_Mask_Type );
     102           0 :     if ( aItem.getFileStatus( aStatus ) != osl::FileBase::E_None )
     103             :     {
     104             :         SAL_INFO("tools.stream", "Failed to stat stream for locking");
     105           0 :         return true;
     106             :     }
     107           0 :     if( aStatus.getFileType() == osl::FileStatus::Directory )
     108           0 :         return true;
     109             : 
     110           0 :     osl::MutexGuard aGuard( LockMutex::get() );
     111           0 :     std::vector<InternalStreamLock> &rLockList = LockList::get();
     112           0 :     for( std::vector<InternalStreamLock>::const_iterator i = rLockList.begin();
     113           0 :          i != rLockList.end(); )
     114             :     {
     115           0 :         if( aItem.isIdenticalTo( i->m_aItem ) )
     116             :         {
     117           0 :             bool bDenyByOptions = false;
     118           0 :             StreamMode nLockMode = i->m_pStream->GetStreamMode();
     119           0 :             StreamMode nNewMode = pStream->GetStreamMode();
     120             : 
     121           0 :             if( nLockMode & STREAM_SHARE_DENYALL )
     122           0 :                 bDenyByOptions = true;
     123           0 :             else if( ( nLockMode & STREAM_SHARE_DENYWRITE ) &&
     124           0 :                      ( nNewMode & STREAM_WRITE ) )
     125           0 :                 bDenyByOptions = true;
     126           0 :             else if( ( nLockMode & STREAM_SHARE_DENYREAD ) &&
     127           0 :                      ( nNewMode & STREAM_READ ) )
     128           0 :                 bDenyByOptions = true;
     129             : 
     130           0 :             if( bDenyByOptions )
     131             :             {
     132           0 :                 if( i->m_nStartPos == 0 && i->m_nEndPos == 0 ) // whole file is already locked
     133           0 :                     return false;
     134           0 :                 if( nStart == 0 && nEnd == 0) // cannot lock whole file
     135           0 :                     return false;
     136             : 
     137           0 :                 if( ( nStart < i->m_nStartPos && nEnd > i->m_nStartPos ) ||
     138           0 :                     ( nStart < i->m_nEndPos && nEnd > i->m_nEndPos ) )
     139           0 :                     return false;
     140             :             }
     141             :         }
     142             :     }
     143           0 :     rLockList.push_back( InternalStreamLock( nStart, nEnd, pStream ) );
     144        2959 :     return true;
     145             : }
     146             : 
     147       19738 : void unlockFile( sal_Size nStart, sal_Size nEnd, SvFileStream* pStream )
     148             : {
     149       19738 :     osl::MutexGuard aGuard( LockMutex::get() );
     150       19738 :     std::vector<InternalStreamLock> &rLockList = LockList::get();
     151       59214 :     for( std::vector<InternalStreamLock>::iterator i = rLockList.begin();
     152       39476 :          i != rLockList.end(); )
     153             :     {
     154           0 :         if ( i->m_pStream == pStream
     155           0 :              && ( ( nStart == 0 && nEnd == 0 )
     156           0 :                   || ( i->m_nStartPos == nStart && i->m_nEndPos == nEnd ) ) )
     157             :         {
     158           0 :             i = rLockList.erase(i);
     159             :         }
     160             :         else
     161             :         {
     162           0 :             ++i;
     163             :         }
     164       19738 :     }
     165       19738 : }
     166             : 
     167             : }
     168             : 
     169             : // StreamData ------------------------------------------------------------------
     170             : 
     171             : class StreamData
     172             : {
     173             : public:
     174             :     oslFileHandle rHandle;
     175             : 
     176       11634 :     StreamData() : rHandle( 0 ) { }
     177             : };
     178             : 
     179           0 : static sal_uInt32 GetSvError( int nErrno )
     180             : {
     181             :     static struct { int nErr; sal_uInt32 sv; } errArr[] =
     182             :     {
     183             :         { 0,            SVSTREAM_OK },
     184             :         { EACCES,       SVSTREAM_ACCESS_DENIED },
     185             :         { EBADF,        SVSTREAM_INVALID_HANDLE },
     186             : #if defined(NETBSD) || \
     187             :     defined(FREEBSD) || defined(MACOSX) || defined(OPENBSD) || \
     188             :     defined(__FreeBSD_kernel__) || defined (AIX) || defined(DRAGONFLY) || \
     189             :     defined(IOS)
     190             :         { EDEADLK,      SVSTREAM_LOCKING_VIOLATION },
     191             : #else
     192             :         { EDEADLOCK,    SVSTREAM_LOCKING_VIOLATION },
     193             : #endif
     194             :         { EINVAL,       SVSTREAM_INVALID_PARAMETER },
     195             :         { EMFILE,       SVSTREAM_TOO_MANY_OPEN_FILES },
     196             :         { ENFILE,       SVSTREAM_TOO_MANY_OPEN_FILES },
     197             :         { ENOENT,       SVSTREAM_FILE_NOT_FOUND },
     198             :         { EPERM,        SVSTREAM_ACCESS_DENIED },
     199             :         { EROFS,        SVSTREAM_ACCESS_DENIED },
     200             :         { EAGAIN,       SVSTREAM_LOCKING_VIOLATION },
     201             :         { EISDIR,       SVSTREAM_PATH_NOT_FOUND },
     202             :         { ELOOP,        SVSTREAM_PATH_NOT_FOUND },
     203             : #if !defined(NETBSD) && !defined (FREEBSD) && \
     204             :     !defined(MACOSX) && !defined(OPENBSD) && !defined(__FreeBSD_kernel__) && \
     205             :     !defined(DRAGONFLY)
     206             :         { EMULTIHOP,    SVSTREAM_PATH_NOT_FOUND },
     207             :         { ENOLINK,      SVSTREAM_PATH_NOT_FOUND },
     208             : #endif
     209             :         { ENOTDIR,      SVSTREAM_PATH_NOT_FOUND },
     210             :         { ETXTBSY,      SVSTREAM_ACCESS_DENIED  },
     211             :         { EEXIST,       SVSTREAM_CANNOT_MAKE    },
     212             :         { ENOSPC,       SVSTREAM_DISK_FULL      },
     213             :         { (int)0xFFFF,  SVSTREAM_GENERALERROR }
     214             :     };
     215             : 
     216           0 :     sal_uInt32 nRetVal = SVSTREAM_GENERALERROR; // default error
     217           0 :     int i=0;
     218           0 :     do
     219             :     {
     220           0 :         if ( errArr[i].nErr == nErrno )
     221             :         {
     222           0 :             nRetVal = errArr[i].sv;
     223           0 :             break;
     224             :         }
     225           0 :         ++i;
     226             :     }
     227           0 :     while( errArr[i].nErr != 0xFFFF );
     228           0 :     return nRetVal;
     229             : }
     230             : 
     231        2884 : static sal_uInt32 GetSvError( oslFileError nErrno )
     232             : {
     233             :     static struct { oslFileError nErr; sal_uInt32 sv; } errArr[] =
     234             :     {
     235             :         { osl_File_E_None,        SVSTREAM_OK },
     236             :         { osl_File_E_ACCES,       SVSTREAM_ACCESS_DENIED },
     237             :         { osl_File_E_BADF,        SVSTREAM_INVALID_HANDLE },
     238             :         { osl_File_E_DEADLK,      SVSTREAM_LOCKING_VIOLATION },
     239             :         { osl_File_E_INVAL,       SVSTREAM_INVALID_PARAMETER },
     240             :         { osl_File_E_MFILE,       SVSTREAM_TOO_MANY_OPEN_FILES },
     241             :         { osl_File_E_NFILE,       SVSTREAM_TOO_MANY_OPEN_FILES },
     242             :         { osl_File_E_NOENT,       SVSTREAM_FILE_NOT_FOUND },
     243             :         { osl_File_E_PERM,        SVSTREAM_ACCESS_DENIED },
     244             :         { osl_File_E_ROFS,        SVSTREAM_ACCESS_DENIED },
     245             :         { osl_File_E_AGAIN,       SVSTREAM_LOCKING_VIOLATION },
     246             :         { osl_File_E_ISDIR,       SVSTREAM_PATH_NOT_FOUND },
     247             :         { osl_File_E_LOOP,        SVSTREAM_PATH_NOT_FOUND },
     248             :         { osl_File_E_MULTIHOP,    SVSTREAM_PATH_NOT_FOUND },
     249             :         { osl_File_E_NOLINK,      SVSTREAM_PATH_NOT_FOUND },
     250             :         { osl_File_E_NOTDIR,      SVSTREAM_PATH_NOT_FOUND },
     251             :         { osl_File_E_EXIST,       SVSTREAM_CANNOT_MAKE    },
     252             :         { osl_File_E_NOSPC,       SVSTREAM_DISK_FULL      },
     253             :         { (oslFileError)0xFFFF,   SVSTREAM_GENERALERROR }
     254             :     };
     255             : 
     256        2884 :     sal_uInt32 nRetVal = SVSTREAM_GENERALERROR; // default error
     257        2884 :     int i=0;
     258       20188 :     do
     259             :     {
     260       23072 :         if ( errArr[i].nErr == nErrno )
     261             :         {
     262        2884 :             nRetVal = errArr[i].sv;
     263        2884 :             break;
     264             :         }
     265       20188 :         ++i;
     266             :     }
     267       20188 :     while( errArr[i].nErr != (oslFileError)0xFFFF );
     268        2884 :     return nRetVal;
     269             : }
     270             : 
     271       11004 : SvFileStream::SvFileStream( const OUString& rFileName, StreamMode nOpenMode )
     272             : {
     273       11004 :     bIsOpen             = false;
     274       11004 :     nLockCounter        = 0;
     275       11004 :     bIsWritable         = false;
     276       11004 :     pInstanceData       = new StreamData;
     277             : 
     278       11004 :     SetBufferSize( 1024 );
     279             :     // convert URL to SystemPath, if necessary
     280       11004 :     OUString aSystemFileName;
     281       11004 :     if( FileBase::getSystemPathFromFileURL( rFileName , aSystemFileName )
     282             :         != FileBase::E_None )
     283             :     {
     284        5895 :         aSystemFileName = rFileName;
     285             :     }
     286       11004 :     Open( aSystemFileName, nOpenMode );
     287       11004 : }
     288             : 
     289         630 : SvFileStream::SvFileStream()
     290             : {
     291         630 :     bIsOpen             = false;
     292         630 :     nLockCounter        = 0;
     293         630 :     bIsWritable         = false;
     294         630 :     pInstanceData       = new StreamData;
     295         630 :     SetBufferSize( 1024 );
     296         630 : }
     297             : 
     298       31654 : SvFileStream::~SvFileStream()
     299             : {
     300       11590 :     Close();
     301             : 
     302       11590 :     unlockFile( 0, 0, this );
     303             : 
     304       11590 :     if (pInstanceData)
     305       11590 :         delete pInstanceData;
     306       20064 : }
     307             : 
     308    20273603 : sal_Size SvFileStream::GetData( void* pData, sal_Size nSize )
     309             : {
     310             : #ifdef DBG_UTIL
     311             :     OStringBuffer aTraceStr("SvFileStream::GetData(): ");
     312             :     aTraceStr.append(static_cast<sal_Int64>(nSize));
     313             :     aTraceStr.append(" Bytes from ");
     314             :     aTraceStr.append(OUStringToOString(aFilename,
     315             :         osl_getThreadTextEncoding()));
     316             :     OSL_TRACE("%s", aTraceStr.getStr());
     317             : #endif
     318             : 
     319    20273603 :     sal_uInt64 nRead = 0;
     320    20273603 :     if ( IsOpen() )
     321             :     {
     322    20273603 :         oslFileError rc = osl_readFile(pInstanceData->rHandle,pData,(sal_uInt64)nSize,&nRead);
     323    20273603 :         if ( rc != osl_File_E_None )
     324             :         {
     325           0 :             SetError( ::GetSvError( rc ));
     326           0 :             return -1;
     327             :         }
     328             :     }
     329    20273603 :     return (sal_Size)nRead;
     330             : }
     331             : 
     332       44849 : sal_Size SvFileStream::PutData( const void* pData, sal_Size nSize )
     333             : {
     334             : #ifdef DBG_UTIL
     335             :     OStringBuffer aTraceStr("SvFileStream::PutData(): ");
     336             :     aTraceStr.append(static_cast<sal_Int64>(nSize));
     337             :     aTraceStr.append(" Bytes to ");
     338             :     aTraceStr.append(OUStringToOString(aFilename,
     339             :         osl_getThreadTextEncoding()));
     340             :     OSL_TRACE("%s", aTraceStr.getStr());
     341             : #endif
     342             : 
     343       44849 :     sal_uInt64 nWrite = 0;
     344       44849 :     if ( IsOpen() )
     345             :     {
     346       44849 :         oslFileError rc = osl_writeFile(pInstanceData->rHandle,pData,(sal_uInt64)nSize,&nWrite);
     347       44849 :         if ( rc != osl_File_E_None )
     348             :         {
     349           0 :             SetError( ::GetSvError( rc ) );
     350           0 :             return -1;
     351             :         }
     352       44849 :         else if( !nWrite )
     353           0 :             SetError( SVSTREAM_DISK_FULL );
     354             :     }
     355       44849 :     return (sal_Size)nWrite;
     356             : }
     357             : 
     358    20381734 : sal_uInt64 SvFileStream::SeekPos(sal_uInt64 const nPos)
     359             : {
     360             :     // check if a truncated STREAM_SEEK_TO_END was passed
     361             :     assert(nPos != (sal_uInt64)(sal_uInt32)STREAM_SEEK_TO_END);
     362    20381734 :     if ( IsOpen() )
     363             :     {
     364             :         oslFileError rc;
     365             :         sal_uInt64 nNewPos;
     366    20381734 :         if ( nPos != STREAM_SEEK_TO_END )
     367    20375291 :             rc = osl_setFilePos( pInstanceData->rHandle, osl_Pos_Absolut, nPos );
     368             :         else
     369        6443 :             rc = osl_setFilePos( pInstanceData->rHandle, osl_Pos_End, 0 );
     370             : 
     371    20381734 :         if ( rc != osl_File_E_None )
     372             :         {
     373           0 :             SetError( SVSTREAM_SEEK_ERROR );
     374           0 :             return 0L;
     375             :         }
     376    20381734 :         rc = osl_getFilePos( pInstanceData->rHandle, &nNewPos );
     377    20381734 :         return (sal_Size) nNewPos;
     378             :     }
     379           0 :     SetError( SVSTREAM_GENERALERROR );
     380           0 :     return 0L;
     381             : }
     382             : 
     383        6461 : void SvFileStream::FlushData()
     384             : {
     385             :     // does not exist locally
     386        6461 : }
     387             : 
     388        8192 : bool SvFileStream::LockRange( sal_Size nByteOffset, sal_Size nBytes )
     389             : {
     390        8192 :     int nLockMode = 0;
     391             : 
     392        8192 :     if ( ! IsOpen() )
     393           0 :         return false;
     394             : 
     395        8192 :     if ( eStreamMode & STREAM_SHARE_DENYALL )
     396             :         {
     397        2957 :         if (bIsWritable)
     398        2957 :             nLockMode = F_WRLCK;
     399             :         else
     400           0 :             nLockMode = F_RDLCK;
     401             :         }
     402             : 
     403        8192 :     if ( eStreamMode & STREAM_SHARE_DENYREAD )
     404             :         {
     405           0 :         if (bIsWritable)
     406           0 :             nLockMode = F_WRLCK;
     407             :         else
     408             :         {
     409           0 :             SetError(SVSTREAM_LOCKING_VIOLATION);
     410           0 :             return false;
     411             :         }
     412             :         }
     413             : 
     414        8192 :     if ( eStreamMode & STREAM_SHARE_DENYWRITE )
     415             :         {
     416        2941 :         if (bIsWritable)
     417           2 :             nLockMode = F_WRLCK;
     418             :         else
     419        2939 :             nLockMode = F_RDLCK;
     420             :         }
     421             : 
     422        8192 :     if (!nLockMode)
     423        5233 :         return true;
     424             : 
     425        2959 :     if( !lockFile( nByteOffset, nByteOffset+nBytes, this ) )
     426             :     {
     427             : #if OSL_DEBUG_LEVEL > 1
     428             :         fprintf( stderr, "InternalLock on %s [ %ld ... %ld ] failed\n",
     429             :                  OUStringToOString(aFilename, osl_getThreadTextEncoding()).getStr(), nByteOffset, nByteOffset+nBytes );
     430             : #endif
     431           0 :         return false;
     432             :     }
     433             : 
     434        2959 :     return true;
     435             : }
     436             : 
     437       22686 : bool SvFileStream::UnlockRange( sal_Size nByteOffset, sal_Size nBytes )
     438             : {
     439       22686 :     if ( ! IsOpen() )
     440       14538 :         return false;
     441             : 
     442        8148 :     unlockFile( nByteOffset, nByteOffset+nBytes, this );
     443             : 
     444        8148 :     return true;
     445             : }
     446             : 
     447        8192 : bool SvFileStream::LockFile()
     448             : {
     449        8192 :   return LockRange( 0UL, 0UL );
     450             : }
     451             : 
     452       22686 : bool SvFileStream::UnlockFile()
     453             : {
     454       22686 :     return UnlockRange( 0UL, 0UL );
     455             : }
     456             : 
     457       11076 : void SvFileStream::Open( const OUString& rFilename, StreamMode nOpenMode )
     458             : {
     459             :     sal_uInt32 uFlags;
     460             :     oslFileHandle nHandleTmp;
     461             : 
     462       11076 :     Close();
     463       11076 :     errno = 0;
     464       11076 :     eStreamMode = nOpenMode;
     465       11076 :     eStreamMode &= ~STREAM_TRUNC; // don't truncat on reopen
     466             : 
     467       11076 :     aFilename = rFilename;
     468             : 
     469             : #ifdef DBG_UTIL
     470             :     OString aLocalFilename(OUStringToOString(aFilename, osl_getThreadTextEncoding()));
     471             :     OStringBuffer aTraceStr("SvFileStream::Open(): ");
     472             :     aTraceStr.append(aLocalFilename);
     473             :     OSL_TRACE( "%s", aTraceStr.getStr() );
     474             : #endif
     475             : 
     476       11076 :     OUString aFileURL;
     477       22152 :     osl::DirectoryItem aItem;
     478       22152 :     osl::FileStatus aStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_LinkTargetURL );
     479             : 
     480             :     // FIXME: we really need to switch to a pure URL model ...
     481       11076 :     if ( osl::File::getFileURLFromSystemPath( aFilename, aFileURL ) != osl::FileBase::E_None )
     482           0 :         aFileURL = aFilename;
     483       19184 :     bool bStatValid = ( osl::DirectoryItem::get( aFileURL, aItem) == osl::FileBase::E_None &&
     484       19184 :                         aItem.getFileStatus( aStatus ) == osl::FileBase::E_None );
     485             : 
     486             :     // SvFileStream can't open a directory
     487       11076 :     if( bStatValid && aStatus.getFileType() == osl::FileStatus::Directory )
     488             :     {
     489           0 :         SetError( ::GetSvError( EISDIR ) );
     490       11076 :         return;
     491             :     }
     492             : 
     493       11076 :     if ( !( nOpenMode & STREAM_WRITE ) )
     494        7945 :         uFlags = osl_File_OpenFlag_Read;
     495        3131 :     else if ( !( nOpenMode & STREAM_READ ) )
     496         128 :         uFlags = osl_File_OpenFlag_Write;
     497             :     else
     498        3003 :         uFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
     499             : 
     500             :     // Fix (MDA, 18.01.95): Don't open with O_CREAT upon RD_ONLY
     501             :     // Important for Read-Only-Filesystems (e.g,  CDROM)
     502       11076 :     if ( (!( nOpenMode & STREAM_NOCREATE )) && ( uFlags != osl_File_OpenFlag_Read ) )
     503        3131 :         uFlags |= osl_File_OpenFlag_Create;
     504       11076 :     if ( nOpenMode & STREAM_TRUNC )
     505         152 :         uFlags |= osl_File_OpenFlag_Trunc;
     506             : 
     507       11076 :     uFlags |= osl_File_OpenFlag_NoExcl | osl_File_OpenFlag_NoLock;
     508             : 
     509       11076 :     if ( nOpenMode & STREAM_WRITE)
     510             :     {
     511        3131 :         if ( nOpenMode & STREAM_COPY_ON_SYMLINK )
     512             :         {
     513           0 :             if ( bStatValid && aStatus.getFileType() == osl::FileStatus::Link &&
     514           0 :                  aStatus.getLinkTargetURL().getLength() > 0 )
     515             :             {
     516             :                 // delete the symbolic link, and replace it with the contents of the link
     517           0 :                 if (osl::File::remove( aFileURL ) == osl::FileBase::E_None )
     518             :                 {
     519           0 :                     File::copy( aStatus.getLinkTargetURL(), aFileURL );
     520             : #if OSL_DEBUG_LEVEL > 0
     521             :                     fprintf( stderr,
     522             :                              "Removing link and replacing with file contents (%s) -> (%s).\n",
     523             :                              OUStringToOString( aStatus.getLinkTargetURL(),
     524             :                                                      RTL_TEXTENCODING_UTF8).getStr(),
     525             :                              OUStringToOString( aFileURL,
     526             :                                                      RTL_TEXTENCODING_UTF8).getStr() );
     527             : #endif
     528             :                 }
     529             :             }
     530             :         }
     531             :     }
     532             : 
     533       11076 :     oslFileError rc = osl_openFile( aFileURL.pData, &nHandleTmp, uFlags );
     534       11076 :     if ( rc != osl_File_E_None )
     535             :     {
     536        2884 :         if ( uFlags & osl_File_OpenFlag_Write )
     537             :         {
     538             :             // Change to read-only
     539           0 :             uFlags &= ~osl_File_OpenFlag_Write;
     540           0 :             rc = osl_openFile( aFileURL.pData, &nHandleTmp, uFlags );
     541             :         }
     542             :     }
     543       11076 :     if ( rc == osl_File_E_None )
     544             :     {
     545        8192 :         pInstanceData->rHandle = nHandleTmp;
     546        8192 :         bIsOpen = true;
     547        8192 :         if ( uFlags & osl_File_OpenFlag_Write )
     548        3131 :             bIsWritable = true;
     549             : 
     550        8192 :         if ( !LockFile() ) // whole file
     551             :         {
     552           0 :             rc = osl_closeFile( nHandleTmp );
     553           0 :             bIsOpen = false;
     554           0 :             bIsWritable = false;
     555           0 :             pInstanceData->rHandle = 0;
     556             :         }
     557             :     }
     558             :     else
     559       13960 :         SetError( ::GetSvError( rc ) );
     560             : }
     561             : 
     562       22686 : void SvFileStream::Close()
     563             : {
     564       22686 :     UnlockFile();
     565             : 
     566       22686 :     if ( IsOpen() )
     567             :     {
     568             : #ifdef DBG_UTIL
     569             :         OStringBuffer aTraceStr("SvFileStream::Close(): ");
     570             :         aTraceStr.append(OUStringToOString(aFilename,
     571             :             osl_getThreadTextEncoding()));
     572             :         OSL_TRACE("%s", aTraceStr.getStr());
     573             : #endif
     574             : 
     575        8148 :         Flush();
     576        8148 :         osl_closeFile( pInstanceData->rHandle );
     577        8148 :         pInstanceData->rHandle = 0;
     578             :     }
     579             : 
     580       22686 :     bIsOpen     = false;
     581       22686 :     bIsWritable = false;
     582       22686 :     SvStream::ClearBuffer();
     583       22686 :     SvStream::ClearError();
     584       22686 : }
     585             : 
     586             : /// set filepointer to beginning of file
     587         262 : void SvFileStream::ResetError()
     588             : {
     589         262 :     SvStream::ClearError();
     590         262 : }
     591             : 
     592         612 : void SvFileStream::SetSize (sal_uInt64 const nSize)
     593             : {
     594         612 :     if (IsOpen())
     595             :     {
     596         612 :         oslFileError rc = osl_setFileSize( pInstanceData->rHandle, nSize );
     597         612 :         if (rc != osl_File_E_None )
     598             :         {
     599           0 :             SetError ( ::GetSvError( rc ));
     600             :         }
     601             :     }
     602         612 : }
     603             : 
     604             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10