12f86921cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file 52f86921cSAndrew Rist * distributed with this work for additional information 62f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 72f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the 82f86921cSAndrew Rist * "License"); you may not use this file except in compliance 92f86921cSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 112f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 132f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing, 142f86921cSAndrew Rist * software distributed under the License is distributed on an 152f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162f86921cSAndrew Rist * KIND, either express or implied. See the License for the 172f86921cSAndrew Rist * specific language governing permissions and limitations 182f86921cSAndrew Rist * under the License. 19cdf0e10cSrcweir * 202f86921cSAndrew Rist *************************************************************/ 212f86921cSAndrew Rist 222f86921cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25*421ed02eSdamjan #include "precompiled_file.hxx" 26cdf0e10cSrcweir #include "com/sun/star/io/IOException.hpp" 27cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp" 28cdf0e10cSrcweir #include "osl/diagnose.h" 29cdf0e10cSrcweir #include "filstr.hxx" 30cdf0e10cSrcweir #include "shell.hxx" 31cdf0e10cSrcweir #include "prov.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir using namespace fileaccess; 35cdf0e10cSrcweir using namespace com::sun::star; 36cdf0e10cSrcweir using namespace com::sun::star::ucb; 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir 40cdf0e10cSrcweir /******************************************************************************/ 41cdf0e10cSrcweir /* */ 42cdf0e10cSrcweir /* XStream_impl implementation */ 43cdf0e10cSrcweir /* */ 44cdf0e10cSrcweir /******************************************************************************/ 45cdf0e10cSrcweir 46cdf0e10cSrcweir 47cdf0e10cSrcweir uno::Any SAL_CALL 48cdf0e10cSrcweir XStream_impl::queryInterface( 49cdf0e10cSrcweir const uno::Type& rType ) 50cdf0e10cSrcweir throw( uno::RuntimeException) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir uno::Any aRet = cppu::queryInterface( rType, 53cdf0e10cSrcweir SAL_STATIC_CAST( lang::XTypeProvider*,this ), 54cdf0e10cSrcweir SAL_STATIC_CAST( io::XStream*,this ), 55cdf0e10cSrcweir SAL_STATIC_CAST( io::XInputStream*,this ), 56cdf0e10cSrcweir SAL_STATIC_CAST( io::XOutputStream*,this ), 57cdf0e10cSrcweir SAL_STATIC_CAST( io::XSeekable*,this ), 58cdf0e10cSrcweir SAL_STATIC_CAST( io::XTruncate*,this ), 59cdf0e10cSrcweir SAL_STATIC_CAST( io::XAsyncOutputMonitor*,this ) ); 60cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir 64cdf0e10cSrcweir void SAL_CALL 65cdf0e10cSrcweir XStream_impl::acquire( 66cdf0e10cSrcweir void ) 67cdf0e10cSrcweir throw() 68cdf0e10cSrcweir { 69cdf0e10cSrcweir OWeakObject::acquire(); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir 73cdf0e10cSrcweir void SAL_CALL 74cdf0e10cSrcweir XStream_impl::release( 75cdf0e10cSrcweir void ) 76cdf0e10cSrcweir throw() 77cdf0e10cSrcweir { 78cdf0e10cSrcweir OWeakObject::release(); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir 82cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////////////// 83cdf0e10cSrcweir // XTypeProvider 84cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////////////// 85cdf0e10cSrcweir 86cdf0e10cSrcweir 87cdf0e10cSrcweir XTYPEPROVIDER_IMPL_7( XStream_impl, 88cdf0e10cSrcweir lang::XTypeProvider, 89cdf0e10cSrcweir io::XStream, 90cdf0e10cSrcweir io::XSeekable, 91cdf0e10cSrcweir io::XInputStream, 92cdf0e10cSrcweir io::XOutputStream, 93cdf0e10cSrcweir io::XTruncate, 94cdf0e10cSrcweir io::XAsyncOutputMonitor ) 95cdf0e10cSrcweir 96cdf0e10cSrcweir 97cdf0e10cSrcweir 98cdf0e10cSrcweir XStream_impl::XStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock ) 99cdf0e10cSrcweir : m_bInputStreamCalled( false ), 100cdf0e10cSrcweir m_bOutputStreamCalled( false ), 101cdf0e10cSrcweir m_pMyShell( pMyShell ), 102cdf0e10cSrcweir m_xProvider( m_pMyShell->m_pProvider ), 103cdf0e10cSrcweir m_bLock( bLock ), 104cdf0e10cSrcweir m_aFile( aUncPath ), 105cdf0e10cSrcweir m_nErrorCode( TASKHANDLER_NO_ERROR ), 106cdf0e10cSrcweir m_nMinorErrorCode( TASKHANDLER_NO_ERROR ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir sal_uInt32 nFlags = ( OpenFlag_Read | OpenFlag_Write ); 109cdf0e10cSrcweir if ( !bLock ) 110cdf0e10cSrcweir nFlags |= OpenFlag_NoLock; 111cdf0e10cSrcweir 112cdf0e10cSrcweir osl::FileBase::RC err = m_aFile.open( nFlags ); 113cdf0e10cSrcweir if( err != osl::FileBase::E_None ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir m_nIsOpen = false; 116cdf0e10cSrcweir m_aFile.close(); 117cdf0e10cSrcweir 118cdf0e10cSrcweir m_nErrorCode = TASKHANDLING_OPEN_FOR_STREAM; 119cdf0e10cSrcweir m_nMinorErrorCode = err; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir else 122cdf0e10cSrcweir m_nIsOpen = true; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir 126cdf0e10cSrcweir XStream_impl::~XStream_impl() 127cdf0e10cSrcweir { 128cdf0e10cSrcweir try 129cdf0e10cSrcweir { 130cdf0e10cSrcweir closeStream(); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir catch (io::IOException const &) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir OSL_ENSURE(false, "unexpected situation"); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir catch (uno::RuntimeException const &) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir OSL_ENSURE(false, "unexpected situation"); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir 143cdf0e10cSrcweir sal_Int32 SAL_CALL XStream_impl::CtorSuccess() 144cdf0e10cSrcweir { 145cdf0e10cSrcweir return m_nErrorCode; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir sal_Int32 SAL_CALL XStream_impl::getMinorError() 151cdf0e10cSrcweir { 152cdf0e10cSrcweir return m_nMinorErrorCode; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir 156cdf0e10cSrcweir 157cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL 158cdf0e10cSrcweir XStream_impl::getInputStream( ) 159cdf0e10cSrcweir throw( uno::RuntimeException) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir { 162cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 163cdf0e10cSrcweir m_bInputStreamCalled = true; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir return uno::Reference< io::XInputStream >( this ); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir 169cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL 170cdf0e10cSrcweir XStream_impl::getOutputStream( ) 171cdf0e10cSrcweir throw( uno::RuntimeException ) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir { 174cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 175cdf0e10cSrcweir m_bOutputStreamCalled = true; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir return uno::Reference< io::XOutputStream >( this ); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir 181cdf0e10cSrcweir void SAL_CALL XStream_impl::truncate(void) 182cdf0e10cSrcweir throw( io::IOException, uno::RuntimeException ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir if (osl::FileBase::E_None != m_aFile.setSize(0)) 185cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 186cdf0e10cSrcweir 187cdf0e10cSrcweir if (osl::FileBase::E_None != m_aFile.setPos(Pos_Absolut,sal_uInt64(0))) 188cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191cdf0e10cSrcweir 192cdf0e10cSrcweir 193cdf0e10cSrcweir //=========================================================================== 194cdf0e10cSrcweir // XStream_impl private non interface methods 195cdf0e10cSrcweir //=========================================================================== 196cdf0e10cSrcweir 197cdf0e10cSrcweir sal_Int32 SAL_CALL 198cdf0e10cSrcweir XStream_impl::readBytes( 199cdf0e10cSrcweir uno::Sequence< sal_Int8 >& aData, 200cdf0e10cSrcweir sal_Int32 nBytesToRead ) 201cdf0e10cSrcweir throw( io::NotConnectedException, 202cdf0e10cSrcweir io::BufferSizeExceededException, 203cdf0e10cSrcweir io::IOException, 204cdf0e10cSrcweir uno::RuntimeException) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir if( ! m_nIsOpen ) 207cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 208cdf0e10cSrcweir 209cdf0e10cSrcweir sal_Int8 * buffer; 210cdf0e10cSrcweir try 211cdf0e10cSrcweir { 212cdf0e10cSrcweir buffer = new sal_Int8[nBytesToRead]; 213cdf0e10cSrcweir } 214cdf0e10cSrcweir catch( std::bad_alloc ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir if( m_nIsOpen ) m_aFile.close(); 217cdf0e10cSrcweir throw io::BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir sal_uInt64 nrc(0); 221cdf0e10cSrcweir if(m_aFile.read( (void* )buffer,sal_uInt64(nBytesToRead),nrc ) 222cdf0e10cSrcweir != osl::FileBase::E_None) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir delete[] buffer; 225cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir aData = uno::Sequence< sal_Int8 > ( buffer, (sal_uInt32)nrc ); 228cdf0e10cSrcweir delete[] buffer; 229cdf0e10cSrcweir return ( sal_Int32 ) nrc; 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir 233cdf0e10cSrcweir sal_Int32 SAL_CALL 234cdf0e10cSrcweir XStream_impl::readSomeBytes( 235cdf0e10cSrcweir uno::Sequence< sal_Int8 >& aData, 236cdf0e10cSrcweir sal_Int32 nMaxBytesToRead ) 237cdf0e10cSrcweir throw( io::NotConnectedException, 238cdf0e10cSrcweir io::BufferSizeExceededException, 239cdf0e10cSrcweir io::IOException, 240cdf0e10cSrcweir uno::RuntimeException) 241cdf0e10cSrcweir { 242cdf0e10cSrcweir return readBytes( aData,nMaxBytesToRead ); 243cdf0e10cSrcweir } 244cdf0e10cSrcweir 245cdf0e10cSrcweir 246cdf0e10cSrcweir void SAL_CALL 247cdf0e10cSrcweir XStream_impl::skipBytes( 248cdf0e10cSrcweir sal_Int32 nBytesToSkip ) 249cdf0e10cSrcweir throw( io::NotConnectedException, 250cdf0e10cSrcweir io::BufferSizeExceededException, 251cdf0e10cSrcweir io::IOException, 252cdf0e10cSrcweir uno::RuntimeException ) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) ); 255cdf0e10cSrcweir } 256cdf0e10cSrcweir 257cdf0e10cSrcweir 258cdf0e10cSrcweir sal_Int32 SAL_CALL 259cdf0e10cSrcweir XStream_impl::available( 260cdf0e10cSrcweir void ) 261cdf0e10cSrcweir throw( io::NotConnectedException, 262cdf0e10cSrcweir io::IOException, 263cdf0e10cSrcweir uno::RuntimeException) 264cdf0e10cSrcweir { 265cdf0e10cSrcweir return 0; 266cdf0e10cSrcweir } 267cdf0e10cSrcweir 268cdf0e10cSrcweir 269cdf0e10cSrcweir void SAL_CALL 270cdf0e10cSrcweir XStream_impl::writeBytes( const uno::Sequence< sal_Int8 >& aData ) 271cdf0e10cSrcweir throw( io::NotConnectedException, 272cdf0e10cSrcweir io::BufferSizeExceededException, 273cdf0e10cSrcweir io::IOException, 274cdf0e10cSrcweir uno::RuntimeException) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir sal_uInt32 length = aData.getLength(); 277cdf0e10cSrcweir if(length) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir sal_uInt64 nWrittenBytes(0); 280cdf0e10cSrcweir const sal_Int8* p = aData.getConstArray(); 281cdf0e10cSrcweir if(osl::FileBase::E_None != m_aFile.write(((void*)(p)),sal_uInt64(length),nWrittenBytes) || 282cdf0e10cSrcweir nWrittenBytes != length ) 283cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 284cdf0e10cSrcweir } 285cdf0e10cSrcweir } 286cdf0e10cSrcweir 287cdf0e10cSrcweir 288cdf0e10cSrcweir void SAL_CALL 289cdf0e10cSrcweir XStream_impl::closeStream( 290cdf0e10cSrcweir void ) 291cdf0e10cSrcweir throw( io::NotConnectedException, 292cdf0e10cSrcweir io::IOException, 293cdf0e10cSrcweir uno::RuntimeException ) 294cdf0e10cSrcweir { 295cdf0e10cSrcweir if( m_nIsOpen ) 296cdf0e10cSrcweir { 297cdf0e10cSrcweir osl::FileBase::RC err = m_aFile.close(); 298cdf0e10cSrcweir 299cdf0e10cSrcweir if( err != osl::FileBase::E_None ) { 300cdf0e10cSrcweir io::IOException ex; 301cdf0e10cSrcweir ex.Message = rtl::OUString::createFromAscii( 302cdf0e10cSrcweir "could not close file"); 303cdf0e10cSrcweir throw ex; 304cdf0e10cSrcweir } 305cdf0e10cSrcweir 306cdf0e10cSrcweir m_nIsOpen = false; 307cdf0e10cSrcweir } 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir void SAL_CALL 311cdf0e10cSrcweir XStream_impl::closeInput( 312cdf0e10cSrcweir void ) 313cdf0e10cSrcweir throw( io::NotConnectedException, 314cdf0e10cSrcweir io::IOException, 315cdf0e10cSrcweir uno::RuntimeException ) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 318cdf0e10cSrcweir m_bInputStreamCalled = false; 319cdf0e10cSrcweir 320cdf0e10cSrcweir if( ! m_bOutputStreamCalled ) 321cdf0e10cSrcweir closeStream(); 322cdf0e10cSrcweir } 323cdf0e10cSrcweir 324cdf0e10cSrcweir 325cdf0e10cSrcweir void SAL_CALL 326cdf0e10cSrcweir XStream_impl::closeOutput( 327cdf0e10cSrcweir void ) 328cdf0e10cSrcweir throw( io::NotConnectedException, 329cdf0e10cSrcweir io::IOException, 330cdf0e10cSrcweir uno::RuntimeException ) 331cdf0e10cSrcweir { 332cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 333cdf0e10cSrcweir m_bOutputStreamCalled = false; 334cdf0e10cSrcweir 335cdf0e10cSrcweir if( ! m_bInputStreamCalled ) 336cdf0e10cSrcweir closeStream(); 337cdf0e10cSrcweir } 338cdf0e10cSrcweir 339cdf0e10cSrcweir 340cdf0e10cSrcweir void SAL_CALL 341cdf0e10cSrcweir XStream_impl::seek( 342cdf0e10cSrcweir sal_Int64 location ) 343cdf0e10cSrcweir throw( lang::IllegalArgumentException, 344cdf0e10cSrcweir io::IOException, 345cdf0e10cSrcweir uno::RuntimeException ) 346cdf0e10cSrcweir { 347cdf0e10cSrcweir if( location < 0 ) 348cdf0e10cSrcweir throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 ); 349cdf0e10cSrcweir if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) ) 350cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 351cdf0e10cSrcweir } 352cdf0e10cSrcweir 353cdf0e10cSrcweir 354cdf0e10cSrcweir sal_Int64 SAL_CALL 355cdf0e10cSrcweir XStream_impl::getPosition( 356cdf0e10cSrcweir void ) 357cdf0e10cSrcweir throw( io::IOException, 358cdf0e10cSrcweir uno::RuntimeException ) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir sal_uInt64 uPos; 361cdf0e10cSrcweir if( osl::FileBase::E_None != m_aFile.getPos( uPos ) ) 362cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 363cdf0e10cSrcweir return sal_Int64( uPos ); 364cdf0e10cSrcweir } 365cdf0e10cSrcweir 366cdf0e10cSrcweir sal_Int64 SAL_CALL 367cdf0e10cSrcweir XStream_impl::getLength( 368cdf0e10cSrcweir void ) 369cdf0e10cSrcweir throw( io::IOException, 370cdf0e10cSrcweir uno::RuntimeException ) 371cdf0e10cSrcweir { 372cdf0e10cSrcweir sal_uInt64 uEndPos; 373cdf0e10cSrcweir if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None ) 374cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 375cdf0e10cSrcweir else 376cdf0e10cSrcweir return sal_Int64( uEndPos ); 377cdf0e10cSrcweir } 378cdf0e10cSrcweir 379cdf0e10cSrcweir void SAL_CALL 380cdf0e10cSrcweir XStream_impl::flush() 381cdf0e10cSrcweir throw( io::NotConnectedException, 382cdf0e10cSrcweir io::BufferSizeExceededException, 383cdf0e10cSrcweir io::IOException, 384cdf0e10cSrcweir uno::RuntimeException ) 385cdf0e10cSrcweir {} 386cdf0e10cSrcweir 387cdf0e10cSrcweir void XStream_impl::waitForCompletion() 388cdf0e10cSrcweir throw (io::IOException, uno::RuntimeException) 389cdf0e10cSrcweir { 390cdf0e10cSrcweir // At least on UNIX, to reliably learn about any errors encountered by 391cdf0e10cSrcweir // asynchronous NFS write operations, without closing the file directly 392cdf0e10cSrcweir // afterwards, there appears to be no cheaper way than to call fsync: 393cdf0e10cSrcweir if (m_nIsOpen && m_aFile.sync() != osl::FileBase::E_None) { 394cdf0e10cSrcweir throw io::IOException( 395cdf0e10cSrcweir rtl::OUString( 396cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 397cdf0e10cSrcweir "could not synchronize file to disc")), 398cdf0e10cSrcweir static_cast< OWeakObject * >(this)); 399cdf0e10cSrcweir } 400cdf0e10cSrcweir } 401