LCOV - code coverage report
Current view: top level - libreoffice/ucb/source/ucp/file - filstr.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 78 116 67.2 %
Date: 2012-12-27 Functions: 19 26 73.1 %
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 "com/sun/star/io/IOException.hpp"
      21             : #include "com/sun/star/uno/RuntimeException.hpp"
      22             : #include "osl/diagnose.h"
      23             : #include "filstr.hxx"
      24             : #include "shell.hxx"
      25             : #include "prov.hxx"
      26             : 
      27             : 
      28             : using namespace fileaccess;
      29             : using namespace com::sun::star;
      30             : using namespace com::sun::star::ucb;
      31             : 
      32             : 
      33             : 
      34             : /******************************************************************************/
      35             : /*                                                                            */
      36             : /*               XStream_impl implementation                                  */
      37             : /*                                                                            */
      38             : /******************************************************************************/
      39             : 
      40             : 
      41             : uno::Any SAL_CALL
      42        2624 : XStream_impl::queryInterface(
      43             :     const uno::Type& rType )
      44             :     throw( uno::RuntimeException)
      45             : {
      46             :     uno::Any aRet = cppu::queryInterface( rType,
      47             :                                           (static_cast< lang::XTypeProvider* >(this)),
      48             :                                           (static_cast< io::XStream* >(this)),
      49             :                                           (static_cast< io::XInputStream* >(this)),
      50             :                                           (static_cast< io::XOutputStream* >(this)),
      51             :                                           (static_cast< io::XSeekable* >(this)),
      52             :                                           (static_cast< io::XTruncate* >(this)),
      53        2624 :                                           (static_cast< io::XAsyncOutputMonitor* >(this)) );
      54        2624 :     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
      55             : }
      56             : 
      57             : 
      58             : void SAL_CALL
      59       38484 : XStream_impl::acquire(
      60             :     void )
      61             :     throw()
      62             : {
      63       38484 :     OWeakObject::acquire();
      64       38484 : }
      65             : 
      66             : 
      67             : void SAL_CALL
      68       36682 : XStream_impl::release(
      69             :     void )
      70             :     throw()
      71             : {
      72       36682 :     OWeakObject::release();
      73       36682 : }
      74             : 
      75             : 
      76             : //////////////////////////////////////////////////////////////////////////////////////////
      77             : //  XTypeProvider
      78             : //////////////////////////////////////////////////////////////////////////////////////////
      79             : 
      80             : 
      81           0 : XTYPEPROVIDER_IMPL_7( XStream_impl,
      82             :                       lang::XTypeProvider,
      83             :                       io::XStream,
      84             :                       io::XSeekable,
      85             :                       io::XInputStream,
      86             :                       io::XOutputStream,
      87             :                       io::XTruncate,
      88             :                       io::XAsyncOutputMonitor )
      89             : 
      90             : 
      91             : 
      92        1865 : XStream_impl::XStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock )
      93             :     : m_bInputStreamCalled( false ),
      94             :       m_bOutputStreamCalled( false ),
      95             :       m_pMyShell( pMyShell ),
      96             :       m_xProvider( m_pMyShell->m_pProvider ),
      97             :       m_aFile( aUncPath ),
      98             :       m_nErrorCode( TASKHANDLER_NO_ERROR ),
      99        1865 :       m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
     100             : {
     101        1865 :     sal_uInt32 nFlags = ( osl_File_OpenFlag_Read | osl_File_OpenFlag_Write );
     102        1865 :     if ( !bLock )
     103           0 :         nFlags |= osl_File_OpenFlag_NoLock;
     104             : 
     105        1865 :     osl::FileBase::RC err = m_aFile.open( nFlags );
     106        1865 :     if(  err != osl::FileBase::E_None )
     107             :     {
     108           0 :         m_nIsOpen = false;
     109           0 :         m_aFile.close();
     110             : 
     111           0 :         m_nErrorCode = TASKHANDLING_OPEN_FOR_STREAM;
     112           0 :         m_nMinorErrorCode = err;
     113             :     }
     114             :     else
     115        1865 :         m_nIsOpen = true;
     116        1865 : }
     117             : 
     118             : 
     119        4548 : XStream_impl::~XStream_impl()
     120             : {
     121             :     try
     122             :     {
     123        1516 :         closeStream();
     124             :     }
     125           0 :     catch (const io::IOException&)
     126             :     {
     127             :         OSL_FAIL("unexpected situation");
     128             :     }
     129           0 :     catch (const uno::RuntimeException&)
     130             :     {
     131             :         OSL_FAIL("unexpected situation");
     132             :     }
     133        3032 : }
     134             : 
     135             : 
     136        1865 : sal_Int32 SAL_CALL XStream_impl::CtorSuccess()
     137             : {
     138        1865 :     return m_nErrorCode;
     139             : }
     140             : 
     141             : 
     142             : 
     143           0 : sal_Int32 SAL_CALL XStream_impl::getMinorError()
     144             : {
     145           0 :     return m_nMinorErrorCode;
     146             : }
     147             : 
     148             : 
     149             : 
     150             : uno::Reference< io::XInputStream > SAL_CALL
     151        2944 : XStream_impl::getInputStream(  )
     152             :     throw( uno::RuntimeException)
     153             : {
     154             :     {
     155        2944 :         osl::MutexGuard aGuard( m_aMutex );
     156        2944 :         m_bInputStreamCalled = true;
     157             :     }
     158        2944 :     return uno::Reference< io::XInputStream >( this );
     159             : }
     160             : 
     161             : 
     162             : uno::Reference< io::XOutputStream > SAL_CALL
     163        2681 : XStream_impl::getOutputStream(  )
     164             :     throw( uno::RuntimeException )
     165             : {
     166             :     {
     167        2681 :         osl::MutexGuard aGuard( m_aMutex );
     168        2681 :         m_bOutputStreamCalled = true;
     169             :     }
     170        2681 :     return uno::Reference< io::XOutputStream >( this );
     171             : }
     172             : 
     173             : 
     174           0 : void SAL_CALL XStream_impl::truncate(void)
     175             :     throw( io::IOException, uno::RuntimeException )
     176             : {
     177           0 :     if (osl::FileBase::E_None != m_aFile.setSize(0))
     178           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     179             : 
     180           0 :     if (osl::FileBase::E_None != m_aFile.setPos(osl_Pos_Absolut,sal_uInt64(0)))
     181           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     182           0 : }
     183             : 
     184             : 
     185             : 
     186             : //===========================================================================
     187             : // XStream_impl private non interface methods
     188             : //===========================================================================
     189             : 
     190             : sal_Int32 SAL_CALL
     191        5035 : XStream_impl::readBytes(
     192             :     uno::Sequence< sal_Int8 >& aData,
     193             :     sal_Int32 nBytesToRead )
     194             :     throw( io::NotConnectedException,
     195             :            io::BufferSizeExceededException,
     196             :            io::IOException,
     197             :            uno::RuntimeException)
     198             : {
     199        5035 :     if( ! m_nIsOpen )
     200           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     201             : 
     202             :     sal_Int8 * buffer;
     203             :     try
     204             :     {
     205        5035 :         buffer = new sal_Int8[nBytesToRead];
     206             :     }
     207           0 :     catch (const std::bad_alloc&)
     208             :     {
     209           0 :         if( m_nIsOpen ) m_aFile.close();
     210           0 :         throw io::BufferSizeExceededException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     211             :     }
     212             : 
     213        5035 :     sal_uInt64 nrc(0);
     214        5035 :     if(m_aFile.read( (void* )buffer,sal_uInt64(nBytesToRead),nrc )
     215             :        != osl::FileBase::E_None)
     216             :     {
     217           0 :         delete[] buffer;
     218           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     219             :     }
     220        5035 :     aData = uno::Sequence< sal_Int8 > ( buffer, (sal_uInt32)nrc );
     221        5035 :     delete[] buffer;
     222        5035 :     return ( sal_Int32 ) nrc;
     223             : }
     224             : 
     225             : 
     226             : sal_Int32 SAL_CALL
     227           0 : XStream_impl::readSomeBytes(
     228             :     uno::Sequence< sal_Int8 >& aData,
     229             :     sal_Int32 nMaxBytesToRead )
     230             :     throw( io::NotConnectedException,
     231             :            io::BufferSizeExceededException,
     232             :            io::IOException,
     233             :            uno::RuntimeException)
     234             : {
     235           0 :     return readBytes( aData,nMaxBytesToRead );
     236             : }
     237             : 
     238             : 
     239             : void SAL_CALL
     240           0 : XStream_impl::skipBytes(
     241             :     sal_Int32 nBytesToSkip )
     242             :     throw( io::NotConnectedException,
     243             :            io::BufferSizeExceededException,
     244             :            io::IOException,
     245             :            uno::RuntimeException )
     246             : {
     247           0 :     m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
     248           0 : }
     249             : 
     250             : 
     251             : sal_Int32 SAL_CALL
     252           0 : XStream_impl::available(
     253             :     void )
     254             :     throw( io::NotConnectedException,
     255             :            io::IOException,
     256             :            uno::RuntimeException)
     257             : {
     258           0 :     return 0;
     259             : }
     260             : 
     261             : 
     262             : void SAL_CALL
     263        2603 : XStream_impl::writeBytes( const uno::Sequence< sal_Int8 >& aData )
     264             :     throw( io::NotConnectedException,
     265             :            io::BufferSizeExceededException,
     266             :            io::IOException,
     267             :            uno::RuntimeException)
     268             : {
     269        2603 :     sal_uInt32 length = aData.getLength();
     270        2603 :     if(length)
     271             :     {
     272        2602 :         sal_uInt64 nWrittenBytes(0);
     273        2602 :         const sal_Int8* p = aData.getConstArray();
     274        2602 :         if(osl::FileBase::E_None != m_aFile.write(((void*)(p)),sal_uInt64(length),nWrittenBytes) ||
     275             :            nWrittenBytes != length )
     276           0 :             throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     277             :     }
     278        2603 : }
     279             : 
     280             : 
     281             : void SAL_CALL
     282        1621 : XStream_impl::closeStream(
     283             :     void )
     284             :     throw( io::NotConnectedException,
     285             :            io::IOException,
     286             :            uno::RuntimeException )
     287             : {
     288        1621 :     if( m_nIsOpen )
     289             :     {
     290        1516 :         osl::FileBase::RC err = m_aFile.close();
     291             : 
     292        1516 :         if( err != osl::FileBase::E_None ) {
     293           0 :             io::IOException ex;
     294           0 :             ex.Message = rtl::OUString( "could not close file");
     295           0 :             throw ex;
     296             :         }
     297             : 
     298        1516 :         m_nIsOpen = false;
     299             :     }
     300        1621 : }
     301             : 
     302             : void SAL_CALL
     303          99 : XStream_impl::closeInput(
     304             :     void )
     305             :     throw( io::NotConnectedException,
     306             :            io::IOException,
     307             :            uno::RuntimeException )
     308             : {
     309          99 :     osl::MutexGuard aGuard( m_aMutex );
     310          99 :     m_bInputStreamCalled = false;
     311             : 
     312          99 :     if( ! m_bOutputStreamCalled )
     313           1 :         closeStream();
     314          99 : }
     315             : 
     316             : 
     317             : void SAL_CALL
     318         105 : XStream_impl::closeOutput(
     319             :     void )
     320             :     throw( io::NotConnectedException,
     321             :            io::IOException,
     322             :            uno::RuntimeException )
     323             : {
     324         105 :     osl::MutexGuard aGuard( m_aMutex );
     325         105 :     m_bOutputStreamCalled = false;
     326             : 
     327         105 :     if( ! m_bInputStreamCalled )
     328         104 :         closeStream();
     329         105 : }
     330             : 
     331             : 
     332             : void SAL_CALL
     333        3795 : XStream_impl::seek(
     334             :     sal_Int64 location )
     335             :     throw( lang::IllegalArgumentException,
     336             :            io::IOException,
     337             :            uno::RuntimeException )
     338             : {
     339        3795 :     if( location < 0 )
     340           0 :         throw lang::IllegalArgumentException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >(), 0 );
     341        3795 :     if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
     342           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     343        3795 : }
     344             : 
     345             : 
     346             : sal_Int64 SAL_CALL
     347         329 : XStream_impl::getPosition(
     348             :     void )
     349             :     throw( io::IOException,
     350             :            uno::RuntimeException )
     351             : {
     352             :     sal_uInt64 uPos;
     353         329 :     if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
     354           0 :         throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     355         329 :     return sal_Int64( uPos );
     356             : }
     357             : 
     358             : sal_Int64 SAL_CALL
     359        4144 : XStream_impl::getLength(
     360             :     void )
     361             :     throw( io::IOException,
     362             :            uno::RuntimeException )
     363             : {
     364             :         sal_uInt64 uEndPos;
     365        4144 :         if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
     366           0 :                 throw io::IOException( ::rtl::OUString(  OSL_LOG_PREFIX  ), uno::Reference< uno::XInterface >() );
     367             :         else
     368        4144 :                 return sal_Int64( uEndPos );
     369             : }
     370             : 
     371             : void SAL_CALL
     372        1730 : XStream_impl::flush()
     373             :     throw( io::NotConnectedException,
     374             :            io::BufferSizeExceededException,
     375             :            io::IOException,
     376             :            uno::RuntimeException )
     377        1730 : {}
     378             : 
     379           4 : void XStream_impl::waitForCompletion()
     380             :     throw (io::IOException, uno::RuntimeException)
     381             : {
     382             :     // At least on UNIX, to reliably learn about any errors encountered by
     383             :     // asynchronous NFS write operations, without closing the file directly
     384             :     // afterwards, there appears to be no cheaper way than to call fsync:
     385           4 :     if (m_nIsOpen && m_aFile.sync() != osl::FileBase::E_None) {
     386             :         throw io::IOException(
     387             :             rtl::OUString( "could not synchronize file to disc"),
     388           0 :             static_cast< OWeakObject * >(this));
     389             :     }
     390           4 : }
     391             : 
     392             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10