Fix GNU C++ version check
[LibreOffice.git] / package / source / xstor / owriteablestream.cxx
blob798fafa808a11895989070e56e4e78423f0c558a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <memory>
24 #include <sal/log.hxx>
26 #include <com/sun/star/packages/NoEncryptionException.hpp>
27 #include <com/sun/star/packages/WrongPasswordException.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/lang/XTypeProvider.hpp>
32 #include <com/sun/star/io/NotConnectedException.hpp>
33 #include <com/sun/star/io/TempFile.hpp>
34 #include <com/sun/star/io/XInputStream.hpp>
35 #include <com/sun/star/io/IOException.hpp>
36 #include <com/sun/star/embed/ElementModes.hpp>
37 #include <com/sun/star/embed/StorageFormats.hpp>
38 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
39 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 #include <cppuhelper/typeprovider.hxx>
41 #include <cppuhelper/queryinterface.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
43 #include <osl/diagnose.h>
45 #include <comphelper/processfactory.hxx>
46 #include <comphelper/servicehelper.hxx>
47 #include <comphelper/storagehelper.hxx>
48 #include <comphelper/ofopxmlhelper.hxx>
49 #include <comphelper/refcountedmutex.hxx>
50 #include <comphelper/sequence.hxx>
52 #include <comphelper/diagnose_ex.hxx>
54 #include <PackageConstants.hxx>
55 #include <utility>
57 #include "selfterminatefilestream.hxx"
58 #include "owriteablestream.hxx"
59 #include "oseekinstream.hxx"
60 #include "xstorage.hxx"
62 // since the copying uses 32000 blocks usually, it makes sense to have a smaller size
63 #define MAX_STORCACHE_SIZE 30000
65 using namespace ::com::sun::star;
67 namespace package
70 static void CopyInputToOutput(
71 const css::uno::Reference< css::io::XInputStream >& xInput,
72 SvStream& rOutput )
74 static const sal_Int32 nConstBufferSize = 32000;
76 if (auto pByteReader = dynamic_cast< comphelper::ByteReader* >( xInput.get() ))
78 sal_Int32 nRead;
79 sal_Int8 aTempBuf[ nConstBufferSize ];
82 nRead = pByteReader->readSomeBytes ( aTempBuf, nConstBufferSize );
83 rOutput.WriteBytes ( aTempBuf, nRead );
85 while ( nRead == nConstBufferSize );
87 else
89 sal_Int32 nRead;
90 uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
94 nRead = xInput->readBytes ( aSequence, nConstBufferSize );
95 rOutput.WriteBytes ( aSequence.getConstArray(), nRead );
97 while ( nRead == nConstBufferSize );
101 bool PackageEncryptionDataLessOrEqual( const ::comphelper::SequenceAsHashMap& aHash1, const ::comphelper::SequenceAsHashMap& aHash2 )
103 // tdf#93389: aHash2 may contain more than in aHash1, if it contains also data for other package
104 // formats (as in case of autorecovery)
105 bool bResult = !aHash1.empty() && aHash1.size() <= aHash2.size();
106 for ( ::comphelper::SequenceAsHashMap::const_iterator aIter = aHash1.begin();
107 bResult && aIter != aHash1.end();
108 ++aIter )
110 uno::Sequence< sal_Int8 > aKey1;
111 bResult = ( ( aIter->second >>= aKey1 ) && aKey1.hasElements() );
112 if ( bResult )
114 const uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first.maString, uno::Sequence< sal_Int8 >() );
115 bResult = aKey1.getLength() == aKey2.getLength()
116 && std::equal(std::cbegin(aKey1), std::cend(aKey1), aKey2.begin(), aKey2.end());
120 return bResult;
123 } // namespace package
125 namespace
128 void SetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet,
129 const uno::Sequence< beans::NamedValue >& aKey )
131 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
132 if ( !xPropertySet.is() )
133 throw uno::RuntimeException();
135 try {
136 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY, uno::Any( aKey ) );
138 catch ( const uno::Exception& ex )
140 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't write encryption related properties");
141 throw io::IOException(ex.Message); // TODO
145 uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet )
147 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
148 if ( !xPropertySet.is() )
149 throw uno::RuntimeException();
151 try {
152 return xPropertySet->getPropertyValue(STORAGE_ENCRYPTION_KEYS_PROPERTY);
154 catch ( const uno::Exception& ex )
156 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't get encryption related properties");
157 throw io::IOException(ex.Message); // TODO
161 bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 )
163 return aSequence1.getLength() == aSequence2.getLength()
164 && std::equal(aSequence1.begin(), aSequence1.end(), aSequence2.begin(), aSequence2.end());
167 bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 )
169 if ( aSequence1.getLength() != aSequence2.getLength() )
170 return false;
172 for ( const auto& rProp1 : aSequence1 )
174 bool bHasMember = false;
175 uno::Sequence< sal_Int8 > aMember1;
176 sal_Int32 nMember1 = 0;
177 if ( rProp1.Value >>= aMember1 )
179 for ( const auto& rProp2 : aSequence2 )
181 if ( rProp1.Name == rProp2.Name )
183 bHasMember = true;
185 uno::Sequence< sal_Int8 > aMember2;
186 if ( !( rProp2.Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) )
187 return false;
191 else if ( rProp1.Value >>= nMember1 )
193 for ( const auto& rProp2 : aSequence2 )
195 if ( rProp1.Name == rProp2.Name )
197 bHasMember = true;
199 sal_Int32 nMember2 = 0;
200 if ( !( rProp2.Value >>= nMember2 ) || nMember1 != nMember2 )
201 return false;
205 else
206 return false;
208 if ( !bHasMember )
209 return false;
212 return true;
215 uno::Reference< io::XStream > CreateMemoryStream( const uno::Reference< uno::XComponentContext >& rContext )
217 static constexpr OUStringLiteral sName(u"com.sun.star.comp.MemoryStream");
218 return uno::Reference< io::XStream >(
219 rContext->getServiceManager()->createInstanceWithContext(sName, rContext),
220 uno::UNO_QUERY_THROW);
223 const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName)
225 return std::find_if(rSeq.begin(), rSeq.end(),
226 [&rName](const beans::StringPair& rPair) { return rPair.First == rName; });
229 } // anonymous namespace
231 OWriteStream_Impl::OWriteStream_Impl( OStorage_Impl* pParent,
232 const uno::Reference< packages::XDataSinkEncrSupport >& xPackageStream,
233 const uno::Reference< lang::XSingleServiceFactory >& xPackage,
234 uno::Reference< uno::XComponentContext > xContext,
235 bool bForceEncrypted,
236 sal_Int32 nStorageType,
237 bool bDefaultCompress,
238 uno::Reference< io::XInputStream > xRelInfoStream )
239 : m_xMutex( new comphelper::RefCountedMutex )
240 , m_pAntiImpl( nullptr )
241 , m_bHasDataToFlush( false )
242 , m_bFlushed( false )
243 , m_xPackageStream( xPackageStream )
244 , m_xContext(std::move( xContext ))
245 , m_pParent( pParent )
246 , m_bForceEncrypted( bForceEncrypted )
247 , m_bUseCommonEncryption( !bForceEncrypted && nStorageType == embed::StorageFormats::PACKAGE )
248 , m_bHasCachedEncryptionData( false )
249 , m_bCompressedSetExplicit( !bDefaultCompress )
250 , m_xPackage( xPackage )
251 , m_bHasInsertedStreamOptimization( false )
252 , m_nStorageType( nStorageType )
253 , m_xOrigRelInfoStream(std::move( xRelInfoStream ))
254 , m_bOrigRelInfoBroken( false )
255 , m_nRelInfoStatus( RELINFO_NO_INIT )
256 , m_nRelId( 1 )
258 SAL_WARN_IF( !xPackageStream.is(), "package.xstor", "No package stream is provided!" );
259 SAL_WARN_IF( !xPackage.is(), "package.xstor", "No package component is provided!" );
260 SAL_WARN_IF( !m_xContext.is(), "package.xstor", "No package stream is provided!" );
261 OSL_ENSURE( pParent, "No parent storage is provided!" );
262 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML || !m_xOrigRelInfoStream.is(), "The Relations info makes sense only for OFOPXML format!" );
265 OWriteStream_Impl::~OWriteStream_Impl()
267 DisposeWrappers();
269 m_oTempFile.reset();
271 CleanCacheStream();
274 void OWriteStream_Impl::CleanCacheStream()
276 if ( !m_xCacheStream.is() )
277 return;
281 uno::Reference< io::XInputStream > xInputCache = m_xCacheStream->getInputStream();
282 if ( xInputCache.is() )
283 xInputCache->closeInput();
285 catch( const uno::Exception& )
290 uno::Reference< io::XOutputStream > xOutputCache = m_xCacheStream->getOutputStream();
291 if ( xOutputCache.is() )
292 xOutputCache->closeOutput();
294 catch( const uno::Exception& )
297 m_xCacheStream.clear();
298 m_xCacheSeek.clear();
301 void OWriteStream_Impl::InsertIntoPackageFolder( const OUString& aName,
302 const uno::Reference< container::XNameContainer >& xParentPackageFolder )
304 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
306 SAL_WARN_IF( !m_bFlushed, "package.xstor", "This method must not be called for nonflushed streams!" );
307 if ( m_bFlushed )
309 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "An inserted stream is incomplete!" );
310 uno::Reference< uno::XInterface > xTmp( m_xPackageStream, uno::UNO_QUERY_THROW );
311 xParentPackageFolder->insertByName( aName, uno::Any( xTmp ) );
313 m_bFlushed = false;
314 m_bHasInsertedStreamOptimization = false;
317 bool OWriteStream_Impl::IsEncrypted()
319 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
320 return false;
322 if ( m_bForceEncrypted || m_bHasCachedEncryptionData )
323 return true;
325 if ( m_oTempFile.has_value() || m_xCacheStream.is() )
326 return false;
328 GetStreamProperties();
330 // the following value can not be cached since it can change after root commit
331 bool bWasEncr = false;
332 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY );
333 if ( xPropSet.is() )
335 uno::Any aValue = xPropSet->getPropertyValue(u"WasEncrypted"_ustr);
336 if ( !( aValue >>= bWasEncr ) )
338 SAL_WARN( "package.xstor", "The property WasEncrypted has wrong type!" );
342 bool bToBeEncr = false;
343 for (const auto& rProp : m_aProps)
345 if ( rProp.Name == "Encrypted" )
347 if ( !( rProp.Value >>= bToBeEncr ) )
349 SAL_WARN( "package.xstor", "The property has wrong type!" );
354 // since a new key set to the package stream it should not be removed except the case when
355 // the stream becomes nonencrypted
356 uno::Sequence< beans::NamedValue > aKey;
357 if ( bToBeEncr )
358 GetEncryptionKeyProperty_Impl( xPropSet ) >>= aKey;
360 // If the properties must be investigated the stream is either
361 // was never changed or was changed, the parent was committed
362 // and the stream was closed.
363 // That means that if it is intended to use common storage key
364 // it is already has no encryption but is marked to be stored
365 // encrypted and the key is empty.
366 if ( !bWasEncr && bToBeEncr && !aKey.hasElements() )
368 // the stream is intended to use common storage password
369 m_bUseCommonEncryption = true;
370 return false;
372 else
373 return bToBeEncr;
376 void OWriteStream_Impl::SetDecrypted()
378 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
379 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
380 throw uno::RuntimeException();
382 GetStreamProperties();
384 // let the stream be modified
385 FillTempGetFileName();
386 m_bHasDataToFlush = true;
388 // remove encryption
389 m_bForceEncrypted = false;
390 m_bHasCachedEncryptionData = false;
391 m_aEncryptionData.clear();
393 for ( auto& rProp : asNonConstRange(m_aProps) )
395 if ( rProp.Name == "Encrypted" )
396 rProp.Value <<= false;
400 void OWriteStream_Impl::SetEncrypted( const ::comphelper::SequenceAsHashMap& aEncryptionData )
402 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
403 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
404 throw uno::RuntimeException();
406 if ( aEncryptionData.empty() )
407 throw uno::RuntimeException();
409 GetStreamProperties();
411 // let the stream be modified
412 FillTempGetFileName();
413 m_bHasDataToFlush = true;
415 // introduce encryption info
416 for ( auto& rProp : asNonConstRange(m_aProps) )
418 if ( rProp.Name == "Encrypted" )
419 rProp.Value <<= true;
422 m_bUseCommonEncryption = false; // very important to set it to false
424 m_bHasCachedEncryptionData = true;
425 m_aEncryptionData = aEncryptionData;
428 void OWriteStream_Impl::DisposeWrappers()
430 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
431 if ( m_pAntiImpl )
433 try {
434 m_pAntiImpl->dispose();
436 catch ( const uno::RuntimeException& )
438 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
441 m_pAntiImpl = nullptr;
443 m_pParent = nullptr;
445 if ( m_aInputStreamsVector.empty() )
446 return;
448 for ( auto& pStream : m_aInputStreamsVector )
450 if ( pStream )
452 pStream->InternalDispose();
453 pStream = nullptr;
457 m_aInputStreamsVector.clear();
460 void OWriteStream_Impl::GetFilledTempFileIfNo( const uno::Reference< io::XInputStream >& xStream )
462 if ( !m_oTempFile.has_value() )
464 m_oTempFile.emplace();
466 try {
467 if ( xStream.is() )
469 // the current position of the original stream should be still OK, copy further
470 package::CopyInputToOutput( xStream, *m_oTempFile->GetStream(StreamMode::READWRITE) );
473 catch( const packages::WrongPasswordException& )
475 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
476 m_oTempFile.reset();
477 throw;
479 catch( const uno::Exception& )
481 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
482 m_oTempFile.reset();
483 throw;
486 if ( m_oTempFile.has_value() )
487 CleanCacheStream();
491 void OWriteStream_Impl::FillTempGetFileName()
493 // should try to create cache first, if the amount of contents is too big, the temp file should be taken
494 if ( !m_xCacheStream.is() && !m_oTempFile.has_value() )
496 uno::Reference< io::XInputStream > xOrigStream = m_xPackageStream->getDataStream();
497 if ( !xOrigStream.is() )
499 // in case of new inserted package stream it is possible that input stream still was not set
500 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
501 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
502 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
503 m_xCacheStream = std::move(xCacheStream);
505 else
507 sal_Int32 nRead = 0;
508 uno::Sequence< sal_Int8 > aData( MAX_STORCACHE_SIZE + 1 );
509 nRead = xOrigStream->readBytes( aData, MAX_STORCACHE_SIZE + 1 );
510 if ( aData.getLength() > nRead )
511 aData.realloc( nRead );
513 if ( nRead <= MAX_STORCACHE_SIZE )
515 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
516 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
518 if ( nRead )
520 uno::Reference< io::XOutputStream > xOutStream( xCacheStream->getOutputStream(), uno::UNO_SET_THROW );
521 xOutStream->writeBytes( aData );
523 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
524 m_xCacheStream = std::move(xCacheStream);
525 m_xCacheSeek->seek( 0 );
527 else if ( !m_oTempFile.has_value() )
529 m_oTempFile.emplace();
531 try {
532 // copy stream contents to the file
533 SvStream* pStream = m_oTempFile->GetStream(StreamMode::READWRITE);
534 pStream->WriteBytes( aData.getConstArray(), aData.getLength() );
536 // the current position of the original stream should be still OK, copy further
537 package::CopyInputToOutput( xOrigStream, *pStream );
539 catch( const packages::WrongPasswordException& )
541 m_oTempFile.reset();
542 throw;
544 catch( const uno::Exception& )
546 m_oTempFile.reset();
553 uno::Reference< io::XStream > OWriteStream_Impl::GetTempFileAsStream()
555 uno::Reference< io::XStream > xTempStream;
557 if ( !m_xCacheStream.is() )
559 if ( !m_oTempFile.has_value() )
560 FillTempGetFileName();
562 if ( m_oTempFile.has_value() )
564 // the temporary file is not used if the cache is used
567 SvStream* pStream = m_oTempFile->GetStream(StreamMode::READWRITE);
568 pStream->Seek(0);
569 xTempStream = new utl::OStreamWrapper(pStream, /*bOwner*/false);
571 catch( const uno::Exception& )
573 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
578 if ( m_xCacheStream.is() )
579 xTempStream = m_xCacheStream;
581 // the method must always return a stream
582 // in case the stream can not be open
583 // an exception should be thrown
584 if ( !xTempStream.is() )
585 throw io::IOException(u"no temp stream"_ustr); //TODO:
587 return xTempStream;
590 uno::Reference< io::XInputStream > OWriteStream_Impl::GetTempFileAsInputStream()
592 uno::Reference< io::XInputStream > xInputStream;
594 if ( !m_xCacheStream.is() )
596 if ( !m_oTempFile.has_value() )
597 FillTempGetFileName();
599 if ( m_oTempFile.has_value() )
601 // the temporary file is not used if the cache is used
604 SvStream* pStream = m_oTempFile->GetStream(StreamMode::READWRITE);
605 pStream->Seek(0);
606 xInputStream = new utl::OStreamWrapper(pStream, /*bOwner*/false);
608 catch( const uno::Exception& )
610 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
615 if ( m_xCacheStream.is() )
616 xInputStream = m_xCacheStream->getInputStream();
618 // the method must always return a stream
619 // in case the stream can not be open
620 // an exception should be thrown
621 if ( !xInputStream.is() )
622 throw io::IOException(); // TODO:
624 return xInputStream;
627 void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputStream >& xInStream,
628 const uno::Sequence< beans::PropertyValue >& aProps )
630 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
632 // this call can be made only during parent storage commit
633 // the parent storage is responsible for the correct handling
634 // of deleted and renamed contents
636 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
638 if ( m_bHasDataToFlush )
639 throw io::IOException(u"m_bHasDataToFlush==true"_ustr);
641 OSL_ENSURE( !m_oTempFile.has_value() && !m_xCacheStream.is(), "The temporary must not exist!" );
643 // use new file as current persistent representation
644 // the new file will be removed after it's stream is closed
645 m_xPackageStream->setDataStream( xInStream );
647 // copy properties to the package stream
648 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
650 // The storage-package communication has a problem
651 // the storage caches properties, thus if the package changes one of them itself
652 // the storage does not know about it
654 // Depending from MediaType value the package can change the compressed property itself
655 // Thus if Compressed property is provided it must be set as the latest one
656 bool bCompressedIsSet = false;
657 bool bCompressed = false;
658 OUString aComprPropName( u"Compressed"_ustr );
659 OUString aMedTypePropName( u"MediaType"_ustr );
660 for ( const auto& rProp : aProps )
662 if ( rProp.Name == aComprPropName )
664 bCompressedIsSet = true;
665 rProp.Value >>= bCompressed;
667 else if ( ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
668 && rProp.Name == aMedTypePropName )
670 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
672 else if ( m_nStorageType == embed::StorageFormats::PACKAGE && rProp.Name == "UseCommonStoragePasswordEncryption" )
673 rProp.Value >>= m_bUseCommonEncryption;
674 else
675 throw lang::IllegalArgumentException();
677 // if there are cached properties update them
678 if ( rProp.Name == aMedTypePropName || rProp.Name == aComprPropName )
679 for ( auto& rMemProp : asNonConstRange(m_aProps) )
681 if ( rProp.Name == rMemProp.Name )
682 rMemProp.Value = rProp.Value;
686 if ( bCompressedIsSet )
688 xPropertySet->setPropertyValue( aComprPropName, uno::Any( bCompressed ) );
689 m_bCompressedSetExplicit = true;
692 if ( m_bUseCommonEncryption )
694 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
695 throw uno::RuntimeException();
697 // set to be encrypted but do not use encryption key
698 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
699 uno::Any( uno::Sequence< beans::NamedValue >() ) );
700 xPropertySet->setPropertyValue( u"Encrypted"_ustr, uno::Any( true ) );
703 // the stream should be free soon, after package is stored
704 m_bHasDataToFlush = false;
705 m_bFlushed = true; // will allow to use transaction on stream level if will need it
706 m_bHasInsertedStreamOptimization = true;
709 void OWriteStream_Impl::Commit()
711 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
713 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
715 if ( !m_bHasDataToFlush )
716 return;
718 uno::Reference< packages::XDataSinkEncrSupport > xNewPackageStream;
719 uno::Sequence< uno::Any > aSeq{ uno::Any(false) };
721 if ( m_xCacheStream.is() )
723 if ( m_pAntiImpl )
724 m_pAntiImpl->DeInit();
726 uno::Reference< io::XInputStream > xInStream( m_xCacheStream->getInputStream(), uno::UNO_SET_THROW );
728 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
730 xNewPackageStream->setDataStream( xInStream );
732 m_xCacheStream.clear();
733 m_xCacheSeek.clear();
736 else if ( m_oTempFile.has_value() )
738 if ( m_pAntiImpl )
739 m_pAntiImpl->DeInit();
741 uno::Reference< io::XInputStream > xInStream;
744 xInStream = new OSelfTerminateFileStream(m_xContext, std::move(*m_oTempFile));
746 catch( const uno::Exception& )
748 TOOLS_WARN_EXCEPTION("package", "");
751 if ( !xInStream.is() )
752 throw io::IOException();
754 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
756 // TODO/NEW: Let the temporary file be removed after commit
757 xNewPackageStream->setDataStream( xInStream );
758 m_oTempFile.reset();
760 else // if ( m_bHasInsertedStreamOptimization )
762 // if the optimization is used the stream can be accessed directly
763 xNewPackageStream = m_xPackageStream;
766 // copy properties to the package stream
767 uno::Reference< beans::XPropertySet > xPropertySet( xNewPackageStream, uno::UNO_QUERY_THROW );
769 for ( auto& rProp : asNonConstRange(m_aProps) )
771 if ( rProp.Name == "Size" )
773 if ( m_pAntiImpl && !m_bHasInsertedStreamOptimization && m_pAntiImpl->m_xSeekable.is() )
775 rProp.Value <<= m_pAntiImpl->m_xSeekable->getLength();
776 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
779 else
780 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
783 if ( m_bUseCommonEncryption )
785 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
786 throw uno::RuntimeException();
788 // set to be encrypted but do not use encryption key
789 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
790 uno::Any( uno::Sequence< beans::NamedValue >() ) );
791 xPropertySet->setPropertyValue( u"Encrypted"_ustr,
792 uno::Any( true ) );
794 else if ( m_bHasCachedEncryptionData )
796 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
797 throw uno::RuntimeException();
799 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
800 uno::Any( m_aEncryptionData.getAsConstNamedValueList() ) );
803 // the stream should be free soon, after package is stored
804 m_xPackageStream = std::move(xNewPackageStream);
805 m_bHasDataToFlush = false;
806 m_bFlushed = true; // will allow to use transaction on stream level if will need it
809 void OWriteStream_Impl::Revert()
811 // can be called only from parent storage
812 // means complete reload of the stream
814 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
816 if ( !m_bHasDataToFlush )
817 return; // nothing to do
819 OSL_ENSURE( m_oTempFile.has_value() || m_xCacheStream.is(), "The temporary must exist!" );
821 if ( m_xCacheStream.is() )
823 m_xCacheStream.clear();
824 m_xCacheSeek.clear();
827 m_oTempFile.reset();
829 m_aProps.realloc( 0 );
831 m_bHasDataToFlush = false;
833 m_bUseCommonEncryption = true;
834 m_bHasCachedEncryptionData = false;
835 m_aEncryptionData.clear();
837 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
838 return;
840 // currently the relations storage is changed only on commit
841 m_xNewRelInfoStream.clear();
842 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
843 if ( m_xOrigRelInfoStream.is() )
845 // the original stream is still here, that means that it was not parsed
846 m_aOrigRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
847 m_nRelInfoStatus = RELINFO_NO_INIT;
849 else
851 // the original stream was already parsed
852 if ( !m_bOrigRelInfoBroken )
853 m_nRelInfoStatus = RELINFO_READ;
854 else
855 m_nRelInfoStatus = RELINFO_BROKEN;
859 uno::Sequence< beans::PropertyValue > const & OWriteStream_Impl::GetStreamProperties()
861 if ( !m_aProps.hasElements() )
862 m_aProps = ReadPackageStreamProperties();
864 return m_aProps;
867 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::InsertOwnProps(
868 const uno::Sequence< beans::PropertyValue >& aProps,
869 bool bUseCommonEncryption )
871 uno::Sequence< beans::PropertyValue > aResult( aProps );
872 beans::PropertyValue aPropVal;
874 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
876 aPropVal.Name = "UseCommonStoragePasswordEncryption";
877 aPropVal.Value <<= bUseCommonEncryption;
879 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
881 ReadRelInfoIfNecessary();
883 aPropVal.Name = "RelationsInfo";
884 if ( m_nRelInfoStatus == RELINFO_READ )
885 aPropVal.Value <<= m_aOrigRelInfo;
886 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
887 aPropVal.Value <<= m_aNewRelInfo;
888 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
889 throw io::IOException( u"Wrong relinfo stream!"_ustr );
891 if (!aPropVal.Name.isEmpty())
893 sal_Int32 i = 0;
894 for (auto p = aResult.getConstArray(); i < aResult.getLength(); ++i)
895 if (p[i].Name == aPropVal.Name)
896 break;
897 if (i == aResult.getLength())
898 aResult.realloc(i + 1);
899 aResult.getArray()[i] = std::move(aPropVal);
902 return aResult;
905 bool OWriteStream_Impl::IsTransacted()
907 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
908 return ( m_pAntiImpl && m_pAntiImpl->m_bTransacted );
911 void OWriteStream_Impl::ReadRelInfoIfNecessary()
913 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
914 return;
916 if ( m_nRelInfoStatus == RELINFO_NO_INIT )
920 // Init from original stream
921 if ( m_xOrigRelInfoStream.is() )
922 m_aOrigRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
923 m_xOrigRelInfoStream,
924 u"_rels/*.rels",
925 m_xContext );
927 // in case of success the stream must be thrown away, that means that the OrigRelInfo is initialized
928 // the reason for this is that the original stream might not be seekable ( at the same time the new
929 // provided stream must be seekable ), so it must be read only once
930 m_xOrigRelInfoStream.clear();
931 m_nRelInfoStatus = RELINFO_READ;
933 catch( const uno::Exception& )
935 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
937 m_nRelInfoStatus = RELINFO_BROKEN;
938 m_bOrigRelInfoBroken = true;
941 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
943 // Init from the new stream
946 if ( m_xNewRelInfoStream.is() )
947 m_aNewRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
948 m_xNewRelInfoStream,
949 u"_rels/*.rels",
950 m_xContext );
952 m_nRelInfoStatus = RELINFO_CHANGED_STREAM_READ;
954 catch( const uno::Exception& )
956 m_nRelInfoStatus = RELINFO_CHANGED_BROKEN;
961 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::ReadPackageStreamProperties()
963 sal_Int32 nPropNum = 0;
964 if ( m_nStorageType == embed::StorageFormats::ZIP )
965 nPropNum = 2;
966 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
967 nPropNum = 3;
968 else if ( m_nStorageType == embed::StorageFormats::PACKAGE )
969 nPropNum = 4;
970 assert(nPropNum >= 2);
971 uno::Sequence< beans::PropertyValue > aResult( nPropNum );
972 auto aResultRange = asNonConstRange(aResult);
974 // The "Compressed" property must be set after "MediaType" property,
975 // since the setting of the last one can change the value of the first one
976 static constexpr OUStringLiteral sMediaType = u"MediaType";
977 static constexpr OUString sCompressed = u"Compressed"_ustr;
978 static constexpr OUString sSize = u"Size"_ustr;
979 static constexpr OUStringLiteral sEncrypted = u"Encrypted";
980 if ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
982 aResultRange[0].Name = sMediaType;
983 aResultRange[1].Name = sCompressed;
984 aResultRange[2].Name = sSize;
986 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
987 aResultRange[3].Name = sEncrypted;
989 else
991 aResultRange[0].Name = sCompressed;
992 aResultRange[1].Name = sSize;
995 // TODO: may be also raw stream should be marked
997 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY_THROW );
998 for ( auto& rProp : aResultRange )
1000 try {
1001 rProp.Value = xPropSet->getPropertyValue( rProp.Name );
1003 catch( const uno::Exception& )
1005 TOOLS_WARN_EXCEPTION( "package.xstor", "A property can't be retrieved" );
1009 return aResult;
1012 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream,
1013 const ::comphelper::SequenceAsHashMap& aEncryptionData )
1015 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1017 SAL_WARN_IF( m_bUseCommonEncryption, "package.xstor", "The stream can not be encrypted!" );
1019 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
1020 throw packages::NoEncryptionException();
1022 if ( m_pAntiImpl )
1024 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1026 else
1028 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, aEncryptionData, false );
1029 if ( !xOwnStream.is() )
1030 throw io::IOException(); // TODO
1032 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1035 uno::Reference< embed::XEncryptionProtectedSource2 > xEncr( xDestStream, uno::UNO_QUERY );
1036 if ( xEncr.is() )
1037 xEncr->setEncryptionData( aEncryptionData.getAsConstNamedValueList() );
1040 uno::Sequence< uno::Sequence< beans::StringPair > > OWriteStream_Impl::GetAllRelationshipsIfAny()
1042 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1043 return uno::Sequence< uno::Sequence< beans::StringPair > >();
1045 ReadRelInfoIfNecessary();
1047 if ( m_nRelInfoStatus == RELINFO_READ )
1048 return m_aOrigRelInfo;
1049 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
1050 return m_aNewRelInfo;
1051 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
1052 throw io::IOException( u"Wrong relinfo stream!"_ustr );
1055 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream )
1057 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1059 if ( m_pAntiImpl )
1061 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1063 else
1065 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, false );
1066 if ( !xOwnStream.is() )
1067 throw io::IOException(); // TODO
1069 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1073 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData, bool bHierarchyAccess )
1075 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1077 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1079 if ( m_pAntiImpl )
1080 throw io::IOException(); // TODO:
1082 if ( !IsEncrypted() )
1083 throw packages::NoEncryptionException();
1085 uno::Reference< io::XStream > xResultStream;
1087 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
1089 if ( m_bHasCachedEncryptionData )
1091 if ( !::package::PackageEncryptionDataLessOrEqual( m_aEncryptionData, aEncryptionData ) )
1092 throw packages::WrongPasswordException();
1094 // the correct key must be set already
1095 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1097 else
1099 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1101 try {
1102 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1104 m_bUseCommonEncryption = false; // very important to set it to false
1105 m_bHasCachedEncryptionData = true;
1106 m_aEncryptionData = aEncryptionData;
1108 catch( const packages::WrongPasswordException& )
1110 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
1111 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1112 throw;
1114 catch ( const uno::Exception& ex )
1116 TOOLS_WARN_EXCEPTION("package.xstor", "GetStream: decrypting stream failed");
1117 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1118 throw io::IOException(ex.Message); // TODO:
1122 SAL_WARN_IF( !xResultStream.is(), "package.xstor", "In case stream can not be retrieved an exception must be thrown!" );
1124 return xResultStream;
1127 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, bool bHierarchyAccess )
1129 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1131 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1133 if ( m_pAntiImpl )
1134 throw io::IOException(); // TODO:
1136 uno::Reference< io::XStream > xResultStream;
1138 if ( IsEncrypted() )
1140 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1143 aGlobalEncryptionData = GetCommonRootEncryptionData();
1145 catch( const packages::NoEncryptionException& )
1147 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
1148 throw packages::WrongPasswordException();
1151 xResultStream = GetStream( nStreamMode, aGlobalEncryptionData, bHierarchyAccess );
1153 else
1154 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1156 return xResultStream;
1159 uno::Reference< io::XStream > OWriteStream_Impl::GetStream_Impl( sal_Int32 nStreamMode, bool bHierarchyAccess )
1161 // private method, no mutex is used
1162 GetStreamProperties();
1164 // TODO/LATER: this info might be read later, on demand in future
1165 ReadRelInfoIfNecessary();
1167 if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::READ )
1169 uno::Reference< io::XInputStream > xInStream;
1170 if ( m_xCacheStream.is() || m_oTempFile.has_value() )
1171 xInStream = GetTempFileAsInputStream(); //TODO:
1172 else
1173 xInStream = m_xPackageStream->getDataStream();
1175 // The stream does not exist in the storage
1176 if ( !xInStream.is() )
1177 throw io::IOException();
1179 rtl::Reference<OInputCompStream> pStream = new OInputCompStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1180 m_aInputStreamsVector.push_back( pStream.get() );
1181 return pStream;
1183 else if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::SEEKABLEREAD )
1185 if ( !m_xCacheStream.is() && !m_oTempFile.has_value() && !( m_xPackageStream->getDataStream().is() ) )
1187 // The stream does not exist in the storage
1188 throw io::IOException();
1191 uno::Reference< io::XInputStream > xInStream = GetTempFileAsInputStream(); //TODO:
1193 if ( !xInStream.is() )
1194 throw io::IOException();
1196 rtl::Reference<OInputSeekStream> pStream = new OInputSeekStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1197 m_aInputStreamsVector.push_back( pStream.get() );
1198 return pStream;
1200 else if ( ( nStreamMode & embed::ElementModes::WRITE ) == embed::ElementModes::WRITE )
1202 if ( !m_aInputStreamsVector.empty() )
1203 throw io::IOException(); // TODO:
1205 uno::Reference< io::XStream > xStream;
1206 if ( ( nStreamMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE )
1208 m_oTempFile.reset();
1209 if ( m_xCacheStream.is() )
1210 CleanCacheStream();
1212 m_bHasDataToFlush = true;
1214 // this call is triggered by the parent and it will recognize the change of the state
1215 if ( m_pParent )
1216 m_pParent->m_bIsModified = true;
1218 xStream = CreateMemoryStream( m_xContext );
1219 m_xCacheSeek.set( xStream, uno::UNO_QUERY_THROW );
1220 m_xCacheStream = xStream;
1222 else if ( !m_bHasInsertedStreamOptimization )
1224 if ( !m_oTempFile.has_value() && !m_xCacheStream.is() && !( m_xPackageStream->getDataStream().is() ) )
1226 // The stream does not exist in the storage
1227 m_bHasDataToFlush = true;
1229 // this call is triggered by the parent and it will recognize the change of the state
1230 if ( m_pParent )
1231 m_pParent->m_bIsModified = true;
1232 xStream = GetTempFileAsStream();
1235 // if the stream exists the temporary file is created on demand
1236 // xStream = GetTempFileAsStream();
1239 rtl::Reference<OWriteStream> tmp;
1240 assert(m_xMutex.is() && "No mutex!");
1241 if ( !xStream.is() )
1242 tmp = new OWriteStream( *this, bHierarchyAccess );
1243 else
1244 tmp = new OWriteStream( *this, xStream, bHierarchyAccess );
1246 m_pAntiImpl = tmp.get();
1247 return tmp;
1250 throw lang::IllegalArgumentException(); // TODO
1253 uno::Reference< io::XInputStream > OWriteStream_Impl::GetPlainRawInStream()
1255 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1257 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1259 // this method is used only internally, this stream object should not go outside of this implementation
1260 // if ( m_pAntiImpl )
1261 // throw io::IOException(); // TODO:
1263 return m_xPackageStream->getPlainRawStream();
1266 uno::Reference< io::XInputStream > OWriteStream_Impl::GetRawInStream()
1268 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1270 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1272 if ( m_pAntiImpl )
1273 throw io::IOException(); // TODO:
1275 SAL_WARN_IF( !IsEncrypted(), "package.xstor", "Impossible to get raw representation for nonencrypted stream!" );
1276 if ( !IsEncrypted() )
1277 throw packages::NoEncryptionException();
1279 return m_xPackageStream->getRawStream();
1282 ::comphelper::SequenceAsHashMap OWriteStream_Impl::GetCommonRootEncryptionData()
1284 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1286 if ( m_nStorageType != embed::StorageFormats::PACKAGE || !m_pParent )
1287 throw packages::NoEncryptionException();
1289 return m_pParent->GetCommonRootEncryptionData();
1292 void OWriteStream_Impl::InputStreamDisposed( OInputCompStream* pStream )
1294 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1295 std::erase(m_aInputStreamsVector, pStream);
1298 void OWriteStream_Impl::CreateReadonlyCopyBasedOnData( const uno::Reference< io::XInputStream >& xDataToCopy, const uno::Sequence< beans::PropertyValue >& aProps, uno::Reference< io::XStream >& xTargetStream )
1300 uno::Reference < io::XStream > xTempFile;
1301 if ( !xTargetStream.is() )
1302 xTempFile = new utl::TempFileFastService;
1303 else
1304 xTempFile = xTargetStream;
1306 uno::Reference < io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY_THROW );
1308 uno::Reference < io::XOutputStream > xTempOut(xTempFile->getOutputStream(), uno::UNO_SET_THROW);
1310 if ( xDataToCopy.is() )
1311 ::comphelper::OStorageHelper::CopyInputToOutput( xDataToCopy, xTempOut );
1313 xTempOut->closeOutput();
1314 xTempSeek->seek( 0 );
1316 uno::Reference< io::XInputStream > xInStream = xTempFile->getInputStream();
1317 if ( !xInStream.is() )
1318 throw io::IOException();
1320 // TODO: remember last state of m_bUseCommonEncryption
1321 if ( !xTargetStream.is() )
1322 xTargetStream.set(
1323 new OInputSeekStream( xInStream, InsertOwnProps( aProps, m_bUseCommonEncryption ), m_nStorageType ) );
1326 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream )
1328 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1330 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1331 if ( !m_xPackageStream.is() )
1332 throw uno::RuntimeException();
1334 uno::Reference< io::XInputStream > xDataToCopy;
1335 if ( IsEncrypted() )
1337 // an encrypted stream must contain input stream
1338 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1341 aGlobalEncryptionData = GetCommonRootEncryptionData();
1343 catch( const packages::NoEncryptionException& )
1345 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
1346 throw packages::WrongPasswordException();
1349 GetCopyOfLastCommit( xTargetStream, aGlobalEncryptionData );
1351 else
1353 xDataToCopy = m_xPackageStream->getDataStream();
1355 // in case of new inserted package stream it is possible that input stream still was not set
1356 GetStreamProperties();
1358 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1362 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream, const ::comphelper::SequenceAsHashMap& aEncryptionData )
1364 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1366 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1367 if ( !m_xPackageStream.is() )
1368 throw uno::RuntimeException();
1370 if ( !IsEncrypted() )
1371 throw packages::NoEncryptionException();
1373 uno::Reference< io::XInputStream > xDataToCopy;
1375 if ( m_bHasCachedEncryptionData )
1377 // TODO: introduce last committed cashed password information and use it here
1378 // that means "use common pass" also should be remembered on flash
1379 uno::Sequence< beans::NamedValue > aKey = aEncryptionData.getAsConstNamedValueList();
1381 uno::Reference< beans::XPropertySet > xProps( m_xPackageStream, uno::UNO_QUERY_THROW );
1383 bool bEncr = false;
1384 xProps->getPropertyValue( u"Encrypted"_ustr ) >>= bEncr;
1385 if ( !bEncr )
1386 throw packages::NoEncryptionException();
1388 uno::Sequence< beans::NamedValue > aPackKey;
1389 xProps->getPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY ) >>= aPackKey;
1390 if ( !SequencesEqual( aKey, aPackKey ) )
1391 throw packages::WrongPasswordException();
1393 // the correct key must be set already
1394 xDataToCopy = m_xPackageStream->getDataStream();
1396 else
1398 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
1399 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1401 try {
1402 xDataToCopy = m_xPackageStream->getDataStream();
1404 if ( !xDataToCopy.is() )
1406 SAL_WARN( "package.xstor", "Encrypted ZipStream must already have input stream inside!" );
1407 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1410 catch( const uno::Exception& )
1412 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't open encrypted stream");
1413 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1414 throw;
1417 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1420 // in case of new inserted package stream it is possible that input stream still was not set
1421 GetStreamProperties();
1423 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1426 void OWriteStream_Impl::CommitStreamRelInfo( const uno::Reference< embed::XStorage >& xRelStorage, std::u16string_view aOrigStreamName, std::u16string_view aNewStreamName )
1428 // at this point of time the old stream must be already cleaned
1429 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML, "The method should be used only with OFOPXML format!" );
1431 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1432 return;
1434 OSL_ENSURE( !aOrigStreamName.empty() && !aNewStreamName.empty() && xRelStorage.is(),
1435 "Wrong relation persistence information is provided!" );
1437 if ( !xRelStorage.is() || aOrigStreamName.empty() || aNewStreamName.empty() )
1438 throw uno::RuntimeException();
1440 if ( m_nRelInfoStatus == RELINFO_BROKEN || m_nRelInfoStatus == RELINFO_CHANGED_BROKEN )
1441 throw io::IOException(); // TODO:
1443 OUString aOrigRelStreamName = OUString::Concat(aOrigStreamName) + ".rels";
1444 OUString aNewRelStreamName = OUString::Concat(aNewStreamName) + ".rels";
1446 bool bRenamed = aOrigRelStreamName != aNewRelStreamName;
1447 if ( m_nRelInfoStatus == RELINFO_CHANGED
1448 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1449 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1451 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1452 xRelStorage->removeElement( aOrigRelStreamName );
1454 if ( m_nRelInfoStatus == RELINFO_CHANGED )
1456 if ( m_aNewRelInfo.hasElements() )
1458 uno::Reference< io::XStream > xRelsStream =
1459 xRelStorage->openStreamElement( aNewRelStreamName,
1460 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1462 uno::Reference< io::XOutputStream > xOutStream = xRelsStream->getOutputStream();
1463 if ( !xOutStream.is() )
1464 throw uno::RuntimeException();
1466 ::comphelper::OFOPXMLHelper::WriteRelationsInfoSequence( xOutStream, m_aNewRelInfo, m_xContext );
1468 // set the mediatype
1469 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1470 xPropSet->setPropertyValue(
1471 u"MediaType"_ustr,
1472 uno::Any( u"application/vnd.openxmlformats-package.relationships+xml"_ustr ) );
1474 m_nRelInfoStatus = RELINFO_READ;
1477 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1478 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1480 uno::Reference< io::XStream > xRelsStream =
1481 xRelStorage->openStreamElement( aNewRelStreamName,
1482 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1484 uno::Reference< io::XOutputStream > xOutputStream = xRelsStream->getOutputStream();
1485 if ( !xOutputStream.is() )
1486 throw uno::RuntimeException();
1488 uno::Reference< io::XSeekable > xSeek( m_xNewRelInfoStream, uno::UNO_QUERY_THROW );
1489 xSeek->seek( 0 );
1490 ::comphelper::OStorageHelper::CopyInputToOutput( m_xNewRelInfoStream, xOutputStream );
1491 xSeek->seek( 0 );
1493 // set the mediatype
1494 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1495 xPropSet->setPropertyValue(u"MediaType"_ustr,
1496 uno::Any( u"application/vnd.openxmlformats-package.relationships+xml"_ustr ) );
1498 if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1499 m_nRelInfoStatus = RELINFO_NO_INIT;
1500 else
1502 // the information is already parsed and the stream is stored, no need in temporary stream any more
1503 m_xNewRelInfoStream.clear();
1504 m_nRelInfoStatus = RELINFO_READ;
1508 // the original stream makes no sense after this step
1509 m_xOrigRelInfoStream = m_xNewRelInfoStream;
1510 m_aOrigRelInfo = m_aNewRelInfo;
1511 m_bOrigRelInfoBroken = false;
1512 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1513 m_xNewRelInfoStream.clear();
1515 else
1517 // the stream is not changed but it might be renamed
1518 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1519 xRelStorage->renameElement( aOrigRelStreamName, aNewRelStreamName );
1523 // OWriteStream implementation
1525 OWriteStream::OWriteStream( OWriteStream_Impl& rImpl, bool bTransacted )
1526 : m_pImpl( &rImpl )
1527 , m_xSharedMutex( rImpl.m_xMutex )
1528 , m_aListenersContainer( rImpl.m_xMutex->GetMutex() )
1529 , m_nStorageType( m_pImpl->m_nStorageType )
1530 , m_bInStreamDisconnected( false )
1531 , m_bInitOnDemand( true )
1532 , m_nInitPosition( 0 )
1533 , m_bTransacted( bTransacted )
1537 OWriteStream::OWriteStream( OWriteStream_Impl& rImpl, uno::Reference< io::XStream > const & xStream, bool bTransacted )
1538 : m_pImpl( &rImpl )
1539 , m_xSharedMutex( rImpl.m_xMutex )
1540 , m_aListenersContainer( rImpl.m_xMutex->GetMutex() )
1541 , m_nStorageType( m_pImpl->m_nStorageType )
1542 , m_bInStreamDisconnected( false )
1543 , m_bInitOnDemand( false )
1544 , m_nInitPosition( 0 )
1545 , m_bTransacted( bTransacted )
1547 if ( xStream.is() )
1549 m_xInStream = xStream->getInputStream();
1550 m_xOutStream = xStream->getOutputStream();
1551 m_xSeekable.set( xStream, uno::UNO_QUERY );
1552 OSL_ENSURE( m_xInStream.is() && m_xOutStream.is() && m_xSeekable.is(), "Stream implementation is incomplete!" );
1556 OWriteStream::~OWriteStream()
1558 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1559 if ( m_pImpl )
1561 osl_atomic_increment(&m_refCount);
1562 try {
1563 dispose();
1565 catch( const uno::RuntimeException& )
1567 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
1572 void OWriteStream::DeInit()
1574 if ( !m_pImpl )
1575 return; // do nothing
1577 if ( m_xSeekable.is() )
1578 m_nInitPosition = m_xSeekable->getPosition();
1580 m_xInStream.clear();
1581 m_xOutStream.clear();
1582 m_xSeekable.clear();
1583 m_bInitOnDemand = true;
1586 void OWriteStream::CheckInitOnDemand()
1588 if ( !m_pImpl )
1590 SAL_INFO("package.xstor", "Disposed!");
1591 throw lang::DisposedException();
1594 if ( !m_bInitOnDemand )
1595 return;
1597 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
1598 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
1599 if ( xStream.is() )
1601 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
1602 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
1603 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
1604 m_xSeekable->seek( m_nInitPosition );
1606 m_nInitPosition = 0;
1607 m_bInitOnDemand = false;
1611 void OWriteStream::CopyToStreamInternally_Impl( const uno::Reference< io::XStream >& xDest )
1613 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1615 CheckInitOnDemand();
1617 if ( !m_xInStream.is() )
1618 throw uno::RuntimeException();
1620 if ( !m_xSeekable.is() )
1621 throw uno::RuntimeException();
1623 uno::Reference< beans::XPropertySet > xDestProps( xDest, uno::UNO_QUERY_THROW );
1625 uno::Reference< io::XOutputStream > xDestOutStream = xDest->getOutputStream();
1626 if ( !xDestOutStream.is() )
1627 throw io::IOException(); // TODO
1629 sal_Int64 nCurPos = m_xSeekable->getPosition();
1630 m_xSeekable->seek( 0 );
1632 uno::Exception eThrown;
1633 bool bThrown = false;
1634 try {
1635 ::comphelper::OStorageHelper::CopyInputToOutput( m_xInStream, xDestOutStream );
1637 catch ( const uno::Exception& e )
1639 eThrown = e;
1640 bThrown = true;
1643 // position-related section below is critical
1644 // if it fails the stream will become invalid
1645 try {
1646 m_xSeekable->seek( nCurPos );
1648 catch ( const uno::Exception& )
1650 // TODO: set the stream in invalid state or dispose
1651 TOOLS_WARN_EXCEPTION( "package.xstor", "The stream become invalid during copying" );
1652 throw uno::RuntimeException();
1655 if ( bThrown )
1656 throw eThrown;
1658 // now the properties can be copied
1659 // the order of the properties setting is not important for StorageStream API
1660 OUString aPropName (u"Compressed"_ustr);
1661 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1662 if ( m_nStorageType == embed::StorageFormats::PACKAGE || m_nStorageType == embed::StorageFormats::OFOPXML )
1664 aPropName = "MediaType";
1665 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1667 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1669 aPropName = "UseCommonStoragePasswordEncryption";
1670 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1675 void OWriteStream::ModifyParentUnlockMutex_Impl(osl::ClearableMutexGuard& aGuard)
1677 if ( m_pImpl->m_pParent )
1679 if ( m_pImpl->m_pParent->HasModifiedListener() )
1681 uno::Reference< util::XModifiable > xParentModif( static_cast<util::XModifiable*>(m_pImpl->m_pParent->m_pAntiImpl) );
1682 aGuard.clear();
1683 xParentModif->setModified( true );
1685 else
1686 m_pImpl->m_pParent->m_bIsModified = true;
1690 uno::Any SAL_CALL OWriteStream::queryInterface( const uno::Type& rType )
1692 // common interfaces
1693 uno::Any aReturn = ::cppu::queryInterface
1694 ( rType
1695 , static_cast<lang::XTypeProvider*> ( this )
1696 , static_cast<io::XInputStream*> ( this )
1697 , static_cast<io::XOutputStream*> ( this )
1698 , static_cast<io::XStream*> ( this )
1699 , static_cast<embed::XExtendedStorageStream*> ( this )
1700 , static_cast<io::XSeekable*> ( this )
1701 , static_cast<io::XTruncate*> ( this )
1702 , static_cast<lang::XComponent*> ( this )
1703 , static_cast<beans::XPropertySet*> ( this ) );
1705 if ( aReturn.hasValue() )
1706 return aReturn ;
1708 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1710 aReturn = ::cppu::queryInterface
1711 ( rType
1712 , static_cast<embed::XEncryptionProtectedSource2*> ( this )
1713 , static_cast<embed::XEncryptionProtectedSource*> ( this ) );
1715 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1717 aReturn = ::cppu::queryInterface
1718 ( rType
1719 , static_cast<embed::XRelationshipAccess*> ( this ) );
1722 if ( aReturn.hasValue() )
1723 return aReturn ;
1725 if ( m_bTransacted )
1727 aReturn = ::cppu::queryInterface
1728 ( rType
1729 , static_cast<embed::XTransactedObject*> ( this )
1730 , static_cast<embed::XTransactionBroadcaster*> ( this ) );
1732 if ( aReturn.hasValue() )
1733 return aReturn ;
1736 return OWeakObject::queryInterface( rType );
1739 void SAL_CALL OWriteStream::acquire() noexcept
1741 OWeakObject::acquire();
1744 void SAL_CALL OWriteStream::release() noexcept
1746 OWeakObject::release();
1749 uno::Sequence< uno::Type > SAL_CALL OWriteStream::getTypes()
1751 if (! m_oTypeCollection)
1753 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1755 if (! m_oTypeCollection)
1757 if ( m_bTransacted )
1759 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1761 ::cppu::OTypeCollection aTmpCollection
1762 ( cppu::UnoType<lang::XTypeProvider>::get()
1763 , cppu::UnoType<io::XInputStream>::get()
1764 , cppu::UnoType<io::XOutputStream>::get()
1765 , cppu::UnoType<io::XStream>::get()
1766 , cppu::UnoType<io::XSeekable>::get()
1767 , cppu::UnoType<io::XTruncate>::get()
1768 , cppu::UnoType<lang::XComponent>::get()
1769 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1770 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1771 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1772 , cppu::UnoType<embed::XTransactedObject>::get()
1773 , cppu::UnoType<embed::XTransactionBroadcaster>::get());
1775 m_oTypeCollection.emplace(
1776 cppu::UnoType<beans::XPropertySet>::get()
1777 , aTmpCollection.getTypes());
1779 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1781 m_oTypeCollection.emplace(
1782 cppu::UnoType<lang::XTypeProvider>::get()
1783 , cppu::UnoType<io::XInputStream>::get()
1784 , cppu::UnoType<io::XOutputStream>::get()
1785 , cppu::UnoType<io::XStream>::get()
1786 , cppu::UnoType<io::XSeekable>::get()
1787 , cppu::UnoType<io::XTruncate>::get()
1788 , cppu::UnoType<lang::XComponent>::get()
1789 , cppu::UnoType<embed::XRelationshipAccess>::get()
1790 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1791 , cppu::UnoType<embed::XTransactedObject>::get()
1792 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1793 , cppu::UnoType<beans::XPropertySet>::get());
1795 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1797 m_oTypeCollection.emplace(
1798 cppu::UnoType<lang::XTypeProvider>::get()
1799 , cppu::UnoType<io::XInputStream>::get()
1800 , cppu::UnoType<io::XOutputStream>::get()
1801 , cppu::UnoType<io::XStream>::get()
1802 , cppu::UnoType<io::XSeekable>::get()
1803 , cppu::UnoType<io::XTruncate>::get()
1804 , cppu::UnoType<lang::XComponent>::get()
1805 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1806 , cppu::UnoType<embed::XTransactedObject>::get()
1807 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1808 , cppu::UnoType<beans::XPropertySet>::get());
1811 else
1813 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1815 m_oTypeCollection.emplace(
1816 cppu::UnoType<lang::XTypeProvider>::get()
1817 , cppu::UnoType<io::XInputStream>::get()
1818 , cppu::UnoType<io::XOutputStream>::get()
1819 , cppu::UnoType<io::XStream>::get()
1820 , cppu::UnoType<io::XSeekable>::get()
1821 , cppu::UnoType<io::XTruncate>::get()
1822 , cppu::UnoType<lang::XComponent>::get()
1823 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1824 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1825 , cppu::UnoType<beans::XPropertySet>::get());
1827 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1829 m_oTypeCollection.emplace(
1830 cppu::UnoType<lang::XTypeProvider>::get()
1831 , cppu::UnoType<io::XInputStream>::get()
1832 , cppu::UnoType<io::XOutputStream>::get()
1833 , cppu::UnoType<io::XStream>::get()
1834 , cppu::UnoType<io::XSeekable>::get()
1835 , cppu::UnoType<io::XTruncate>::get()
1836 , cppu::UnoType<lang::XComponent>::get()
1837 , cppu::UnoType<embed::XRelationshipAccess>::get()
1838 , cppu::UnoType<beans::XPropertySet>::get());
1840 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1842 m_oTypeCollection.emplace(
1843 cppu::UnoType<lang::XTypeProvider>::get()
1844 , cppu::UnoType<io::XInputStream>::get()
1845 , cppu::UnoType<io::XOutputStream>::get()
1846 , cppu::UnoType<io::XStream>::get()
1847 , cppu::UnoType<io::XSeekable>::get()
1848 , cppu::UnoType<io::XTruncate>::get()
1849 , cppu::UnoType<lang::XComponent>::get()
1850 , cppu::UnoType<beans::XPropertySet>::get());
1856 return m_oTypeCollection->getTypes() ;
1859 uno::Sequence< sal_Int8 > SAL_CALL OWriteStream::getImplementationId()
1861 static const comphelper::UnoIdInit lcl_ImplId;
1862 return lcl_ImplId.getSeq();
1865 sal_Int32 SAL_CALL OWriteStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
1867 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1869 CheckInitOnDemand();
1871 if ( !m_pImpl )
1873 SAL_INFO("package.xstor", "Disposed!");
1874 throw lang::DisposedException();
1877 if ( !m_xInStream.is() )
1878 throw io::NotConnectedException();
1880 return m_xInStream->readBytes( aData, nBytesToRead );
1883 sal_Int32 SAL_CALL OWriteStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
1885 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1887 CheckInitOnDemand();
1889 if ( !m_pImpl )
1891 SAL_INFO("package.xstor", "Disposed!");
1892 throw lang::DisposedException();
1895 if ( !m_xInStream.is() )
1896 throw io::NotConnectedException();
1898 return m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
1901 void SAL_CALL OWriteStream::skipBytes( sal_Int32 nBytesToSkip )
1903 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1905 CheckInitOnDemand();
1907 if ( !m_pImpl )
1909 SAL_INFO("package.xstor", "Disposed!");
1910 throw lang::DisposedException();
1913 if ( !m_xInStream.is() )
1914 throw io::NotConnectedException();
1916 m_xInStream->skipBytes( nBytesToSkip );
1919 sal_Int32 SAL_CALL OWriteStream::available( )
1921 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1923 CheckInitOnDemand();
1925 if ( !m_pImpl )
1927 SAL_INFO("package.xstor", "Disposed!");
1928 throw lang::DisposedException();
1931 if ( !m_xInStream.is() )
1932 throw io::NotConnectedException();
1934 return m_xInStream->available();
1938 void SAL_CALL OWriteStream::closeInput( )
1940 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1942 if ( !m_pImpl )
1944 SAL_INFO("package.xstor", "Disposed!");
1945 throw lang::DisposedException();
1948 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
1949 throw io::NotConnectedException();
1951 // the input part of the stream stays open for internal purposes (to allow reading during copying)
1952 // since it can not be reopened until output part is closed, it will be closed with output part.
1953 m_bInStreamDisconnected = true;
1954 // m_xInStream->closeInput();
1955 // m_xInStream.clear();
1957 if ( !m_xOutStream.is() )
1958 dispose();
1961 uno::Reference< io::XInputStream > SAL_CALL OWriteStream::getInputStream()
1963 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1965 if ( !m_pImpl )
1967 SAL_INFO("package.xstor", "Disposed!");
1968 throw lang::DisposedException();
1971 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
1972 return uno::Reference< io::XInputStream >();
1974 return this;
1977 uno::Reference< io::XOutputStream > SAL_CALL OWriteStream::getOutputStream()
1979 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
1983 CheckInitOnDemand();
1985 catch( const io::IOException& r )
1987 throw lang::WrappedTargetRuntimeException(u"OWriteStream::getOutputStream: Could not create backing temp file"_ustr,
1988 getXWeak(), css::uno::Any ( r ) );
1991 if ( !m_pImpl )
1993 SAL_INFO("package.xstor", "Disposed!");
1994 throw lang::DisposedException();
1997 if ( !m_xOutStream.is() )
1998 return uno::Reference< io::XOutputStream >();
2000 return this;
2003 void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
2005 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2007 // the write method makes initialization itself, since it depends from the aData length
2008 // NO CheckInitOnDemand()!
2010 if ( !m_pImpl )
2012 SAL_INFO("package.xstor", "Disposed!");
2013 throw lang::DisposedException();
2016 if ( !m_bInitOnDemand )
2018 if ( !m_xOutStream.is() || !m_xSeekable.is())
2019 throw io::NotConnectedException();
2021 if ( m_pImpl->m_xCacheStream.is() )
2023 // check whether the cache should be turned off
2024 sal_Int64 nPos = m_xSeekable->getPosition();
2025 if ( nPos + aData.getLength() > MAX_STORCACHE_SIZE )
2027 // disconnect the cache and copy the data to the temporary file
2028 m_xSeekable->seek( 0 );
2030 // it is enough to copy the cached stream, the cache should already contain everything
2031 m_pImpl->GetFilledTempFileIfNo( m_xInStream );
2032 if ( m_pImpl->m_oTempFile.has_value() )
2034 DeInit();
2035 // the last position is known and it is differs from the current stream position
2036 m_nInitPosition = nPos;
2042 if ( m_bInitOnDemand )
2044 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2045 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
2046 if ( xStream.is() )
2048 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
2049 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
2050 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
2051 m_xSeekable->seek( m_nInitPosition );
2053 m_nInitPosition = 0;
2054 m_bInitOnDemand = false;
2058 if ( !m_xOutStream.is() )
2059 throw io::NotConnectedException();
2061 m_xOutStream->writeBytes( aData );
2062 m_pImpl->m_bHasDataToFlush = true;
2064 ModifyParentUnlockMutex_Impl( aGuard );
2067 void OWriteStream::writeBytes( const sal_Int8* pData, sal_Int32 nBytesToWrite )
2069 assert(nBytesToWrite >= 0);
2071 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2073 // the write method makes initialization itself, since it depends from the aData length
2074 // NO CheckInitOnDemand()!
2076 if ( !m_pImpl )
2078 SAL_INFO("package.xstor", "Disposed!");
2079 throw lang::DisposedException();
2082 if ( !m_bInitOnDemand )
2084 if ( !m_xOutStream.is() || !m_xSeekable.is())
2085 throw io::NotConnectedException();
2087 if ( m_pImpl->m_xCacheStream.is() )
2089 // check whether the cache should be turned off
2090 sal_Int64 nPos = m_xSeekable->getPosition();
2091 if ( nPos + nBytesToWrite > MAX_STORCACHE_SIZE )
2093 // disconnect the cache and copy the data to the temporary file
2094 m_xSeekable->seek( 0 );
2096 // it is enough to copy the cached stream, the cache should already contain everything
2097 m_pImpl->GetFilledTempFileIfNo( m_xInStream );
2098 if ( m_pImpl->m_oTempFile.has_value() )
2100 DeInit();
2101 // the last position is known and it is differs from the current stream position
2102 m_nInitPosition = nPos;
2108 if ( m_bInitOnDemand )
2110 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2111 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
2112 if ( xStream.is() )
2114 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
2115 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
2116 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
2117 m_xSeekable->seek( m_nInitPosition );
2119 m_nInitPosition = 0;
2120 m_bInitOnDemand = false;
2124 if ( !m_xOutStream.is() )
2125 throw io::NotConnectedException();
2127 if (auto pByteWriter = dynamic_cast< comphelper::ByteWriter* >( m_xOutStream.get() ))
2128 pByteWriter->writeBytes(pData, nBytesToWrite);
2129 else
2131 uno::Sequence<sal_Int8> aData(pData, nBytesToWrite);
2132 m_xOutStream->writeBytes( aData );
2134 m_pImpl->m_bHasDataToFlush = true;
2136 ModifyParentUnlockMutex_Impl( aGuard );
2139 void SAL_CALL OWriteStream::flush()
2141 // In case stream is flushed its current version becomes visible
2142 // to the parent storage. Usually parent storage flushes the stream
2143 // during own commit but a user can explicitly flush the stream
2144 // so the changes will be available through cloning functionality.
2146 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2148 if ( !m_pImpl )
2150 SAL_INFO("package.xstor", "Disposed!");
2151 throw lang::DisposedException();
2154 if ( !m_bInitOnDemand )
2156 if ( !m_xOutStream.is() )
2157 throw io::NotConnectedException();
2159 m_xOutStream->flush();
2160 m_pImpl->Commit();
2164 void OWriteStream::CloseOutput_Impl()
2166 // all the checks must be done in calling method
2168 m_xOutStream->closeOutput();
2169 m_xOutStream.clear();
2171 if ( m_bInitOnDemand )
2172 return;
2174 // after the stream is disposed it can be committed
2175 // so transport correct size property
2176 if ( !m_xSeekable.is() )
2177 throw uno::RuntimeException();
2179 for ( auto& rProp : asNonConstRange(m_pImpl->m_aProps) )
2181 if ( rProp.Name == "Size" )
2182 rProp.Value <<= m_xSeekable->getLength();
2186 void SAL_CALL OWriteStream::closeOutput()
2188 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2190 CheckInitOnDemand();
2192 if ( !m_pImpl )
2194 SAL_INFO("package.xstor", "Disposed!");
2195 throw lang::DisposedException();
2198 if ( !m_xOutStream.is() )
2199 throw io::NotConnectedException();
2201 CloseOutput_Impl();
2203 if ( m_bInStreamDisconnected || !m_xInStream.is() )
2204 dispose();
2207 void SAL_CALL OWriteStream::seek( sal_Int64 location )
2209 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2211 CheckInitOnDemand();
2213 if ( !m_pImpl )
2215 SAL_INFO("package.xstor", "Disposed!");
2216 throw lang::DisposedException();
2219 if ( !m_xSeekable.is() )
2220 throw uno::RuntimeException();
2222 m_xSeekable->seek( location );
2225 sal_Int64 SAL_CALL OWriteStream::getPosition()
2227 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2229 CheckInitOnDemand();
2231 if ( !m_pImpl )
2233 SAL_INFO("package.xstor", "Disposed!");
2234 throw lang::DisposedException();
2237 if ( !m_xSeekable.is() )
2238 throw uno::RuntimeException();
2240 return m_xSeekable->getPosition();
2243 sal_Int64 SAL_CALL OWriteStream::getLength()
2245 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2247 CheckInitOnDemand();
2249 if ( !m_pImpl )
2251 SAL_INFO("package.xstor", "Disposed!");
2252 throw lang::DisposedException();
2255 if ( !m_xSeekable.is() )
2256 throw uno::RuntimeException();
2258 return m_xSeekable->getLength();
2261 void SAL_CALL OWriteStream::truncate()
2263 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2265 CheckInitOnDemand();
2267 if ( !m_pImpl )
2269 SAL_INFO("package.xstor", "Disposed!");
2270 throw lang::DisposedException();
2273 if ( !m_xOutStream.is() )
2274 throw uno::RuntimeException();
2276 uno::Reference< io::XTruncate > xTruncate( m_xOutStream, uno::UNO_QUERY_THROW );
2277 xTruncate->truncate();
2279 m_pImpl->m_bHasDataToFlush = true;
2281 ModifyParentUnlockMutex_Impl( aGuard );
2284 void SAL_CALL OWriteStream::dispose()
2286 // should be an internal method since it can be called only from parent storage
2288 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2290 if ( !m_pImpl )
2292 SAL_INFO("package.xstor", "Disposed!");
2293 throw lang::DisposedException();
2296 if ( m_xOutStream.is() )
2297 CloseOutput_Impl();
2299 if ( m_xInStream.is() )
2301 m_xInStream->closeInput();
2302 m_xInStream.clear();
2305 m_xSeekable.clear();
2307 m_pImpl->m_pAntiImpl = nullptr;
2309 if ( !m_bInitOnDemand )
2313 if ( !m_bTransacted )
2315 m_pImpl->Commit();
2317 else
2319 // throw away all the changes
2320 m_pImpl->Revert();
2323 catch( const uno::Exception& )
2325 uno::Any aCaught( ::cppu::getCaughtException() );
2326 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
2327 throw lang::WrappedTargetRuntimeException(u"Can not commit/revert the storage!"_ustr,
2328 getXWeak(),
2329 aCaught );
2333 m_pImpl = nullptr;
2336 // the listener might try to get rid of parent storage, and the storage would delete this object;
2337 // for now the listener is just notified at the end of the method to workaround the problem
2338 // in future a more elegant way should be found
2340 lang::EventObject aSource( getXWeak() );
2341 m_aListenersContainer.disposeAndClear( aSource );
2344 void SAL_CALL OWriteStream::addEventListener(
2345 const uno::Reference< lang::XEventListener >& xListener )
2347 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2349 if ( !m_pImpl )
2351 SAL_INFO("package.xstor", "Disposed!");
2352 throw lang::DisposedException();
2355 m_aListenersContainer.addInterface( cppu::UnoType<lang::XEventListener>::get(),
2356 xListener );
2359 void SAL_CALL OWriteStream::removeEventListener(
2360 const uno::Reference< lang::XEventListener >& xListener )
2362 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2364 if ( !m_pImpl )
2366 SAL_INFO("package.xstor", "Disposed!");
2367 throw lang::DisposedException();
2370 m_aListenersContainer.removeInterface( cppu::UnoType<lang::XEventListener>::get(),
2371 xListener );
2374 void SAL_CALL OWriteStream::setEncryptionPassword( const OUString& aPass )
2376 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2378 CheckInitOnDemand();
2380 if ( !m_pImpl )
2382 SAL_INFO("package.xstor", "Disposed!");
2383 throw lang::DisposedException();
2386 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2388 m_pImpl->SetEncrypted( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aPass ) );
2390 ModifyParentUnlockMutex_Impl( aGuard );
2393 void SAL_CALL OWriteStream::removeEncryption()
2395 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2397 CheckInitOnDemand();
2399 if ( !m_pImpl )
2401 SAL_INFO("package.xstor", "Disposed!");
2402 throw lang::DisposedException();
2405 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2407 m_pImpl->SetDecrypted();
2409 ModifyParentUnlockMutex_Impl( aGuard );
2412 void SAL_CALL OWriteStream::setEncryptionData( const uno::Sequence< beans::NamedValue >& aEncryptionData )
2414 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2416 CheckInitOnDemand();
2418 if ( !m_pImpl )
2420 SAL_INFO("package.xstor", "Disposed!");
2421 throw lang::DisposedException();
2424 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2426 m_pImpl->SetEncrypted( aEncryptionData );
2428 ModifyParentUnlockMutex_Impl( aGuard );
2431 sal_Bool SAL_CALL OWriteStream::hasEncryptionData()
2433 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2435 if (!m_pImpl)
2436 return false;
2438 bool bRet = false;
2442 bRet = m_pImpl->IsEncrypted();
2444 if (!bRet && m_pImpl->m_bUseCommonEncryption && m_pImpl->m_pParent)
2445 bRet = m_pImpl->m_pParent->m_bHasCommonEncryptionData;
2447 catch( const uno::RuntimeException& )
2449 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
2450 throw;
2452 catch( const uno::Exception& )
2454 uno::Any aCaught( ::cppu::getCaughtException() );
2455 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
2456 throw lang::WrappedTargetRuntimeException( u"Problems on hasEncryptionData!"_ustr,
2457 getXWeak(),
2458 aCaught );
2461 return bRet;
2464 sal_Bool SAL_CALL OWriteStream::hasByID( const OUString& sID )
2466 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2468 if ( !m_pImpl )
2470 SAL_INFO("package.xstor", "Disposed!");
2471 throw lang::DisposedException();
2474 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2475 throw uno::RuntimeException();
2479 getRelationshipByID( sID );
2480 return true;
2482 catch( const container::NoSuchElementException& )
2484 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
2487 return false;
2490 OUString SAL_CALL OWriteStream::getTargetByID( const OUString& sID )
2492 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2494 if ( !m_pImpl )
2496 SAL_INFO("package.xstor", "Disposed!");
2497 throw lang::DisposedException();
2500 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2501 throw uno::RuntimeException();
2503 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2504 auto pRel = lcl_findPairByName(aSeq, u"Target"_ustr);
2505 if (pRel != aSeq.end())
2506 return pRel->Second;
2508 return OUString();
2511 OUString SAL_CALL OWriteStream::getTypeByID( const OUString& sID )
2513 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2515 if ( !m_pImpl )
2517 SAL_INFO("package.xstor", "Disposed!");
2518 throw lang::DisposedException();
2521 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2522 throw uno::RuntimeException();
2524 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2525 auto pRel = lcl_findPairByName(aSeq, u"Type"_ustr);
2526 if (pRel != aSeq.end())
2527 return pRel->Second;
2529 return OUString();
2532 uno::Sequence< beans::StringPair > SAL_CALL OWriteStream::getRelationshipByID( const OUString& sID )
2534 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2536 if ( !m_pImpl )
2538 SAL_INFO("package.xstor", "Disposed!");
2539 throw lang::DisposedException();
2542 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2543 throw uno::RuntimeException();
2545 // TODO/LATER: in future the unification of the ID could be checked
2546 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2547 const beans::StringPair aIDRel(u"Id"_ustr, sID);
2548 auto pRel = std::find_if(aSeq.begin(), aSeq.end(),
2549 [&aIDRel](const uno::Sequence<beans::StringPair>& rRel) {
2550 return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
2551 if (pRel != aSeq.end())
2552 return *pRel;
2554 throw container::NoSuchElementException();
2557 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getRelationshipsByType( const OUString& sType )
2559 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2561 if ( !m_pImpl )
2563 SAL_INFO("package.xstor", "Disposed!");
2564 throw lang::DisposedException();
2567 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2568 throw uno::RuntimeException();
2570 // TODO/LATER: in future the unification of the ID could be checked
2571 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2572 const beans::StringPair aTypeRel(u"Type"_ustr, sType);
2573 std::vector< uno::Sequence<beans::StringPair> > aResult;
2574 aResult.reserve(aSeq.getLength());
2576 std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult),
2577 [&aTypeRel](const uno::Sequence<beans::StringPair>& rRel) {
2578 return std::find(rRel.begin(), rRel.end(), aTypeRel) != rRel.end(); });
2580 return comphelper::containerToSequence(aResult);
2583 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getAllRelationships()
2585 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2587 if ( !m_pImpl )
2589 SAL_INFO("package.xstor", "Disposed!");
2590 throw lang::DisposedException();
2593 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2594 throw uno::RuntimeException();
2596 return m_pImpl->GetAllRelationshipsIfAny();
2599 void SAL_CALL OWriteStream::insertRelationshipByID( const OUString& sID, const uno::Sequence< beans::StringPair >& aEntry, sal_Bool bReplace )
2601 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2603 if ( !m_pImpl )
2605 SAL_INFO("package.xstor", "Disposed!");
2606 throw lang::DisposedException();
2609 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2610 throw uno::RuntimeException();
2612 const beans::StringPair aIDRel(u"Id"_ustr, sID);
2614 uno::Sequence<beans::StringPair>* pPair = nullptr;
2616 // TODO/LATER: in future the unification of the ID could be checked
2617 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2618 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2620 const auto& rRel = aSeq[nInd];
2621 if (std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end())
2622 pPair = &aSeq.getArray()[nInd];
2625 if ( pPair && !bReplace )
2626 throw container::ElementExistException(); // TODO
2628 if ( !pPair )
2630 sal_Int32 nIDInd = aSeq.getLength();
2631 aSeq.realloc( nIDInd + 1 );
2632 pPair = &aSeq.getArray()[nIDInd];
2635 std::vector<beans::StringPair> aResult;
2636 aResult.reserve(aEntry.getLength() + 1);
2638 aResult.push_back(aIDRel);
2639 std::copy_if(aEntry.begin(), aEntry.end(), std::back_inserter(aResult),
2640 [](const beans::StringPair& rRel) { return rRel.First != "Id"; });
2642 *pPair = comphelper::containerToSequence(aResult);
2644 m_pImpl->m_aNewRelInfo = std::move(aSeq);
2645 m_pImpl->m_xNewRelInfoStream.clear();
2646 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2649 void SAL_CALL OWriteStream::removeRelationshipByID( const OUString& sID )
2651 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2653 if ( !m_pImpl )
2655 SAL_INFO("package.xstor", "Disposed!");
2656 throw lang::DisposedException();
2659 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2660 throw uno::RuntimeException();
2662 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2663 const beans::StringPair aIDRel(u"Id"_ustr, sID);
2664 auto pRel = std::find_if(std::cbegin(aSeq), std::cend(aSeq),
2665 [&aIDRel](const uno::Sequence< beans::StringPair >& rRel) {
2666 return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
2667 if (pRel != std::cend(aSeq))
2669 auto nInd = static_cast<sal_Int32>(std::distance(std::cbegin(aSeq), pRel));
2670 comphelper::removeElementAt(aSeq, nInd);
2672 m_pImpl->m_aNewRelInfo = std::move(aSeq);
2673 m_pImpl->m_xNewRelInfoStream.clear();
2674 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2676 // TODO/LATER: in future the unification of the ID could be checked
2677 return;
2680 throw container::NoSuchElementException();
2683 void SAL_CALL OWriteStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& aEntries, sal_Bool bReplace )
2685 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2687 if ( !m_pImpl )
2689 SAL_INFO("package.xstor", "Disposed!");
2690 throw lang::DisposedException();
2693 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2694 throw uno::RuntimeException();
2696 OUString aIDTag( u"Id"_ustr );
2697 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2698 std::vector< uno::Sequence<beans::StringPair> > aResultVec;
2699 aResultVec.reserve(aSeq.getLength() + aEntries.getLength());
2701 std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec),
2702 [&aIDTag, &aEntries, bReplace](const uno::Sequence<beans::StringPair>& rTargetRel) {
2703 auto pTargetPair = lcl_findPairByName(rTargetRel, aIDTag);
2704 if (pTargetPair == rTargetRel.end())
2705 return false;
2707 bool bIsSourceSame = std::any_of(aEntries.begin(), aEntries.end(),
2708 [&pTargetPair](const uno::Sequence<beans::StringPair>& rSourceEntry) {
2709 return std::find(rSourceEntry.begin(), rSourceEntry.end(), *pTargetPair) != rSourceEntry.end(); });
2711 if ( bIsSourceSame && !bReplace )
2712 throw container::ElementExistException();
2714 // if no such element in the provided sequence
2715 return !bIsSourceSame;
2718 std::transform(aEntries.begin(), aEntries.end(), std::back_inserter(aResultVec),
2719 [&aIDTag](const uno::Sequence<beans::StringPair>& rEntry) -> uno::Sequence<beans::StringPair> {
2720 auto pPair = lcl_findPairByName(rEntry, aIDTag);
2721 if (pPair == rEntry.end())
2722 throw io::IOException(); // TODO: illegal relation ( no ID )
2724 auto aResult = comphelper::sequenceToContainer<std::vector<beans::StringPair>>(rEntry);
2725 auto nIDInd = std::distance(rEntry.begin(), pPair);
2726 std::rotate(aResult.begin(), std::next(aResult.begin(), nIDInd), std::next(aResult.begin(), nIDInd + 1));
2728 return comphelper::containerToSequence(aResult);
2731 m_pImpl->m_aNewRelInfo = comphelper::containerToSequence(aResultVec);
2732 m_pImpl->m_xNewRelInfoStream.clear();
2733 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2736 void SAL_CALL OWriteStream::clearRelationships()
2738 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2740 if ( !m_pImpl )
2742 SAL_INFO("package.xstor", "Disposed!");
2743 throw lang::DisposedException();
2746 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
2747 throw uno::RuntimeException();
2749 m_pImpl->m_aNewRelInfo.realloc( 0 );
2750 m_pImpl->m_xNewRelInfoStream.clear();
2751 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2754 uno::Reference< beans::XPropertySetInfo > SAL_CALL OWriteStream::getPropertySetInfo()
2756 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2758 //TODO:
2759 return uno::Reference< beans::XPropertySetInfo >();
2762 void SAL_CALL OWriteStream::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
2764 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
2766 if ( !m_pImpl )
2768 SAL_INFO("package.xstor", "Disposed!");
2769 throw lang::DisposedException();
2772 m_pImpl->GetStreamProperties();
2773 static constexpr OUString aCompressedString( u"Compressed"_ustr );
2774 static constexpr OUString aMediaTypeString( u"MediaType"_ustr );
2775 if ( m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == aMediaTypeString )
2777 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2778 bool bCompressedValueFromType = true;
2779 OUString aType;
2780 aValue >>= aType;
2782 if ( !m_pImpl->m_bCompressedSetExplicit )
2784 if ( aType == "image/jpeg" || aType == "image/png" || aType == "image/gif" )
2785 bCompressedValueFromType = false;
2788 for ( auto& rProp : asNonConstRange(m_pImpl->m_aProps) )
2790 if ( aPropertyName == rProp.Name )
2791 rProp.Value = aValue;
2792 else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString == rProp.Name )
2793 rProp.Value <<= bCompressedValueFromType;
2796 else if ( aPropertyName == aCompressedString )
2798 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2799 m_pImpl->m_bCompressedSetExplicit = true;
2800 for ( auto& rProp : asNonConstRange(m_pImpl->m_aProps) )
2802 if ( aPropertyName == rProp.Name )
2803 rProp.Value = aValue;
2806 else if ( m_nStorageType == embed::StorageFormats::PACKAGE
2807 && aPropertyName == "UseCommonStoragePasswordEncryption" )
2809 bool bUseCommonEncryption = false;
2810 if ( !(aValue >>= bUseCommonEncryption) )
2811 throw lang::IllegalArgumentException(); //TODO
2813 if ( m_bInitOnDemand && m_pImpl->m_bHasInsertedStreamOptimization )
2815 // the data stream is provided to the packagestream directly
2816 m_pImpl->m_bUseCommonEncryption = bUseCommonEncryption;
2818 else if ( bUseCommonEncryption )
2820 if ( !m_pImpl->m_bUseCommonEncryption )
2822 m_pImpl->SetDecrypted();
2823 m_pImpl->m_bUseCommonEncryption = true;
2826 else
2827 m_pImpl->m_bUseCommonEncryption = false;
2829 else if ( m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == aMediaTypeString )
2831 for ( auto& rProp : asNonConstRange(m_pImpl->m_aProps) )
2833 if ( aPropertyName == rProp.Name )
2834 rProp.Value = aValue;
2837 else if ( m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfoStream" )
2839 uno::Reference< io::XInputStream > xInRelStream;
2840 if ( !( aValue >>= xInRelStream ) || !xInRelStream.is() )
2841 throw lang::IllegalArgumentException(); // TODO
2843 uno::Reference< io::XSeekable > xSeek( xInRelStream, uno::UNO_QUERY );
2844 if ( !xSeek.is() )
2846 // currently this is an internal property that is used for optimization
2847 // and the stream must support XSeekable interface
2848 // TODO/LATER: in future it can be changed if property is used from outside
2849 throw lang::IllegalArgumentException(); // TODO
2852 m_pImpl->m_xNewRelInfoStream = std::move(xInRelStream);
2853 m_pImpl->m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
2854 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED_STREAM;
2856 else if ( m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfo" )
2858 if ( !(aValue >>= m_pImpl->m_aNewRelInfo) )
2859 throw lang::IllegalArgumentException(); // TODO
2861 else if ( aPropertyName == "Size" )
2862 throw beans::PropertyVetoException(); // TODO
2863 else if ( m_nStorageType == embed::StorageFormats::PACKAGE
2864 && ( aPropertyName == "IsEncrypted" || aPropertyName == "Encrypted" ) )
2865 throw beans::PropertyVetoException(); // TODO
2866 else if ( aPropertyName == "RelId" )
2868 aValue >>= m_pImpl->m_nRelId;
2870 else
2871 throw beans::UnknownPropertyException(aPropertyName); // TODO
2873 m_pImpl->m_bHasDataToFlush = true;
2874 ModifyParentUnlockMutex_Impl( aGuard );
2877 uno::Any SAL_CALL OWriteStream::getPropertyValue( const OUString& aProp )
2879 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2881 if ( !m_pImpl )
2883 SAL_INFO("package.xstor", "Disposed!");
2884 throw lang::DisposedException();
2887 if ( aProp == "RelId" )
2889 return uno::Any( m_pImpl->GetNewRelId() );
2892 OUString aPropertyName;
2893 if ( aProp == "IsEncrypted" )
2894 aPropertyName = "Encrypted";
2895 else
2896 aPropertyName = aProp;
2898 if ( ( ( m_nStorageType == embed::StorageFormats::PACKAGE || m_nStorageType == embed::StorageFormats::OFOPXML )
2899 && aPropertyName == "MediaType" )
2900 || ( m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == "Encrypted" )
2901 || aPropertyName == "Compressed" )
2903 m_pImpl->GetStreamProperties();
2905 auto pProp = std::find_if(std::cbegin(m_pImpl->m_aProps), std::cend(m_pImpl->m_aProps),
2906 [&aPropertyName](const css::beans::PropertyValue& rProp){ return aPropertyName == rProp.Name; });
2907 if (pProp != std::cend(m_pImpl->m_aProps))
2908 return pProp->Value;
2910 else if ( m_nStorageType == embed::StorageFormats::PACKAGE
2911 && aPropertyName == "UseCommonStoragePasswordEncryption" )
2912 return uno::Any( m_pImpl->m_bUseCommonEncryption );
2913 else if ( aPropertyName == "Size" )
2915 bool bThrow = false;
2918 CheckInitOnDemand();
2920 catch (const io::IOException&)
2922 bThrow = true;
2924 if (bThrow || !m_xSeekable.is())
2925 throw uno::RuntimeException();
2927 return uno::Any( m_xSeekable->getLength() );
2930 throw beans::UnknownPropertyException(aPropertyName); // TODO
2933 void SAL_CALL OWriteStream::addPropertyChangeListener(
2934 const OUString& /*aPropertyName*/,
2935 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
2937 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2939 if ( !m_pImpl )
2941 SAL_INFO("package.xstor", "Disposed!");
2942 throw lang::DisposedException();
2945 //TODO:
2948 void SAL_CALL OWriteStream::removePropertyChangeListener(
2949 const OUString& /*aPropertyName*/,
2950 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
2952 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2954 if ( !m_pImpl )
2956 SAL_INFO("package.xstor", "Disposed!");
2957 throw lang::DisposedException();
2960 //TODO:
2963 void SAL_CALL OWriteStream::addVetoableChangeListener(
2964 const OUString& /*PropertyName*/,
2965 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
2967 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2969 if ( !m_pImpl )
2971 SAL_INFO("package.xstor", "Disposed!");
2972 throw lang::DisposedException();
2975 //TODO:
2978 void SAL_CALL OWriteStream::removeVetoableChangeListener(
2979 const OUString& /*PropertyName*/,
2980 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
2982 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
2984 if ( !m_pImpl )
2986 SAL_INFO("package.xstor", "Disposed!");
2987 throw lang::DisposedException();
2990 //TODO:
2993 // XTransactedObject
2995 void OWriteStream::BroadcastTransaction( sal_Int8 nMessage )
2997 1 - preCommit
2998 2 - committed
2999 3 - preRevert
3000 4 - reverted
3003 // no need to lock mutex here for the checking of m_pImpl, and m_pData is alive until the object is destructed
3004 if ( !m_pImpl )
3006 SAL_INFO("package.xstor", "Disposed!");
3007 throw lang::DisposedException();
3010 lang::EventObject aSource( getXWeak() );
3012 comphelper::OInterfaceContainerHelper2* pContainer =
3013 m_aListenersContainer.getContainer(
3014 cppu::UnoType<embed::XTransactionListener>::get());
3015 if ( !pContainer )
3016 return;
3018 comphelper::OInterfaceIteratorHelper2 pIterator( *pContainer );
3019 while ( pIterator.hasMoreElements( ) )
3021 OSL_ENSURE( nMessage >= 1 && nMessage <= 4, "Wrong internal notification code is used!" );
3023 switch( nMessage )
3025 case STOR_MESS_PRECOMMIT:
3026 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preCommit( aSource );
3027 break;
3028 case STOR_MESS_COMMITTED:
3029 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->commited( aSource );
3030 break;
3031 case STOR_MESS_PREREVERT:
3032 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preRevert( aSource );
3033 break;
3034 case STOR_MESS_REVERTED:
3035 static_cast< embed::XTransactionListener*>( pIterator.next( ) )->reverted( aSource );
3036 break;
3040 void SAL_CALL OWriteStream::commit()
3042 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::commit" );
3044 if ( !m_pImpl )
3046 SAL_INFO("package.xstor", "Disposed!");
3047 throw lang::DisposedException();
3050 if ( !m_bTransacted )
3051 throw uno::RuntimeException();
3053 try {
3054 BroadcastTransaction( STOR_MESS_PRECOMMIT );
3056 osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
3058 if ( !m_pImpl )
3060 SAL_INFO("package.xstor", "Disposed!");
3061 throw lang::DisposedException();
3064 m_pImpl->Commit();
3066 // when the storage is committed the parent is modified
3067 ModifyParentUnlockMutex_Impl( aGuard );
3069 catch( const io::IOException& )
3071 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3072 throw;
3074 catch( const embed::StorageWrappedTargetException& )
3076 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3077 throw;
3079 catch( const uno::RuntimeException& )
3081 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3082 throw;
3084 catch( const uno::Exception& )
3086 uno::Any aCaught( ::cppu::getCaughtException() );
3087 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
3088 throw embed::StorageWrappedTargetException( u"Problems on commit!"_ustr,
3089 getXWeak(),
3090 aCaught );
3093 BroadcastTransaction( STOR_MESS_COMMITTED );
3096 void SAL_CALL OWriteStream::revert()
3098 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::revert" );
3100 // the method removes all the changes done after last commit
3102 if ( !m_pImpl )
3104 SAL_INFO("package.xstor", "Disposed!");
3105 throw lang::DisposedException();
3108 if ( !m_bTransacted )
3109 throw uno::RuntimeException();
3111 BroadcastTransaction( STOR_MESS_PREREVERT );
3114 osl::MutexGuard aGuard(m_xSharedMutex->GetMutex());
3116 if (!m_pImpl)
3118 SAL_INFO("package.xstor", "Disposed!");
3119 throw lang::DisposedException();
3122 try {
3123 m_pImpl->Revert();
3125 catch (const io::IOException&)
3127 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3128 throw;
3130 catch (const embed::StorageWrappedTargetException&)
3132 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3133 throw;
3135 catch (const uno::RuntimeException&)
3137 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3138 throw;
3140 catch (const uno::Exception&)
3142 uno::Any aCaught(::cppu::getCaughtException());
3143 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
3144 throw embed::StorageWrappedTargetException(u"Problems on revert!"_ustr,
3145 getXWeak(),
3146 aCaught);
3150 BroadcastTransaction( STOR_MESS_REVERTED );
3153 // XTransactionBroadcaster
3155 void SAL_CALL OWriteStream::addTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3157 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
3159 if ( !m_pImpl )
3161 SAL_INFO("package.xstor", "Disposed!");
3162 throw lang::DisposedException();
3165 if ( !m_bTransacted )
3166 throw uno::RuntimeException();
3168 m_aListenersContainer.addInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3169 aListener );
3172 void SAL_CALL OWriteStream::removeTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3174 ::osl::MutexGuard aGuard( m_xSharedMutex->GetMutex() );
3176 if ( !m_pImpl )
3178 SAL_INFO("package.xstor", "Disposed!");
3179 throw lang::DisposedException();
3182 if ( !m_bTransacted )
3183 throw uno::RuntimeException();
3185 m_aListenersContainer.removeInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3186 aListener );
3189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */