1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
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
;
70 static void CopyInputToOutput(
71 const css::uno::Reference
< css::io::XInputStream
>& xInput
,
74 static const sal_Int32 nConstBufferSize
= 32000;
76 if (auto pByteReader
= dynamic_cast< comphelper::ByteReader
* >( xInput
.get() ))
79 sal_Int8 aTempBuf
[ nConstBufferSize
];
82 nRead
= pByteReader
->readSomeBytes ( aTempBuf
, nConstBufferSize
);
83 rOutput
.WriteBytes ( aTempBuf
, nRead
);
85 while ( nRead
== nConstBufferSize
);
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();
110 uno::Sequence
< sal_Int8
> aKey1
;
111 bResult
= ( ( aIter
->second
>>= aKey1
) && aKey1
.hasElements() );
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());
123 } // namespace package
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();
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();
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() )
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
)
185 uno::Sequence
< sal_Int8
> aMember2
;
186 if ( !( rProp2
.Value
>>= aMember2
) || !SequencesEqual( aMember1
, aMember2
) )
191 else if ( rProp1
.Value
>>= nMember1
)
193 for ( const auto& rProp2
: aSequence2
)
195 if ( rProp1
.Name
== rProp2
.Name
)
199 sal_Int32 nMember2
= 0;
200 if ( !( rProp2
.Value
>>= nMember2
) || nMember1
!= nMember2
)
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
)
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()
274 void OWriteStream_Impl::CleanCacheStream()
276 if ( !m_xCacheStream
.is() )
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!" );
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
) );
314 m_bHasInsertedStreamOptimization
= false;
317 bool OWriteStream_Impl::IsEncrypted()
319 if ( m_nStorageType
!= embed::StorageFormats::PACKAGE
)
322 if ( m_bForceEncrypted
|| m_bHasCachedEncryptionData
)
325 if ( m_oTempFile
.has_value() || m_xCacheStream
.is() )
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
);
335 uno::Any aValue
= xPropSet
->getPropertyValue("WasEncrypted");
336 if ( !( aValue
>>= bWasEncr
) )
338 SAL_WARN( "package.xstor", "The property WasEncrypted has wrong type!" );
342 bool bToBeEncr
= false;
343 for ( const auto& rProp
: std::as_const(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
;
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;
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;
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() );
434 m_pAntiImpl
->dispose();
436 catch ( const uno::RuntimeException
& )
438 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
441 m_pAntiImpl
= nullptr;
445 if ( m_aInputStreamsVector
.empty() )
448 for ( auto& pStream
: m_aInputStreamsVector
)
452 pStream
->InternalDispose();
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();
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");
479 catch( const uno::Exception
& )
481 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
486 if ( m_oTempFile
.has_value() )
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
= xCacheStream
;
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!" );
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
= xCacheStream
;
525 m_xCacheSeek
->seek( 0 );
527 else if ( !m_oTempFile
.has_value() )
529 m_oTempFile
.emplace();
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
& )
544 catch( const uno::Exception
& )
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
);
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("no temp stream"); //TODO:
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
);
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:
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("m_bHasDataToFlush==true");
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( "Compressed" );
659 OUString
aMedTypePropName( "MediaType" );
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
;
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( "Encrypted", 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
)
718 uno::Reference
< packages::XDataSinkEncrSupport
> xNewPackageStream
;
719 uno::Sequence
< uno::Any
> aSeq
{ uno::Any(false) };
721 if ( m_xCacheStream
.is() )
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() )
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
);
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
);
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( "Encrypted",
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
= 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();
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
)
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
;
851 // the original stream was already parsed
852 if ( !m_bOrigRelInfoBroken
)
853 m_nRelInfoStatus
= RELINFO_READ
;
855 m_nRelInfoStatus
= RELINFO_BROKEN
;
859 uno::Sequence
< beans::PropertyValue
> const & OWriteStream_Impl::GetStreamProperties()
861 if ( !m_aProps
.hasElements() )
862 m_aProps
= ReadPackageStreamProperties();
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( "Wrong relinfo stream!" );
891 if (!aPropVal
.Name
.isEmpty())
894 for (auto p
= aResult
.getConstArray(); i
< aResult
.getLength(); ++i
)
895 if (p
[i
].Name
== aPropVal
.Name
)
897 if (i
== aResult
.getLength())
898 aResult
.realloc(i
+ 1);
899 aResult
.getArray()[i
] = aPropVal
;
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
)
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
,
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(
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
)
966 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
)
968 else if ( m_nStorageType
== embed::StorageFormats::PACKAGE
)
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 OUStringLiteral sCompressed
= u
"Compressed";
978 static constexpr OUStringLiteral sSize
= u
"Size";
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
;
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
)
1001 rProp
.Value
= xPropSet
->getPropertyValue( rProp
.Name
);
1003 catch( const uno::Exception
& )
1005 TOOLS_WARN_EXCEPTION( "package.xstor", "A property can't be retrieved" );
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();
1024 m_pAntiImpl
->CopyToStreamInternally_Impl( xDestStream
);
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
);
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( "Wrong relinfo stream!" );
1055 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference
< io::XStream
>& xDestStream
)
1057 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() ) ;
1061 m_pAntiImpl
->CopyToStreamInternally_Impl( xDestStream
);
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!" );
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
);
1099 SetEncryptionKeyProperty_Impl( xPropertySet
, aEncryptionData
.getAsConstNamedValueList() );
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
>() );
1114 catch ( const uno::Exception
& ex
)
1116 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't write encryption related properties");
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!" );
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
);
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:
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() );
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() );
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() )
1212 m_bHasDataToFlush
= true;
1214 // this call is triggered by the parent and it will recognize the change of the state
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
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
);
1244 tmp
= new OWriteStream( *this, xStream
, bHierarchyAccess
);
1246 m_pAntiImpl
= tmp
.get();
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!" );
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 m_aInputStreamsVector
.erase(std::remove(m_aInputStreamsVector
.begin(), m_aInputStreamsVector
.end(), 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
;
1304 xTempFile
= xTargetStream
;
1306 uno::Reference
< io::XSeekable
> xTempSeek( xTempFile
, uno::UNO_QUERY_THROW
);
1308 uno::Reference
< io::XOutputStream
> xTempOut
= xTempFile
->getOutputStream();
1309 if ( !xTempOut
.is() )
1310 throw uno::RuntimeException();
1312 if ( xDataToCopy
.is() )
1313 ::comphelper::OStorageHelper::CopyInputToOutput( xDataToCopy
, xTempOut
);
1315 xTempOut
->closeOutput();
1316 xTempSeek
->seek( 0 );
1318 uno::Reference
< io::XInputStream
> xInStream
= xTempFile
->getInputStream();
1319 if ( !xInStream
.is() )
1320 throw io::IOException();
1322 // TODO: remember last state of m_bUseCommonEncryption
1323 if ( !xTargetStream
.is() )
1325 static_cast< ::cppu::OWeakObject
* >(
1326 new OInputSeekStream( xInStream
, InsertOwnProps( aProps
, m_bUseCommonEncryption
), m_nStorageType
) ),
1327 uno::UNO_QUERY_THROW
);
1330 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference
< io::XStream
>& xTargetStream
)
1332 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
1334 SAL_WARN_IF( !m_xPackageStream
.is(), "package.xstor", "The source stream for copying is incomplete!" );
1335 if ( !m_xPackageStream
.is() )
1336 throw uno::RuntimeException();
1338 uno::Reference
< io::XInputStream
> xDataToCopy
;
1339 if ( IsEncrypted() )
1341 // an encrypted stream must contain input stream
1342 ::comphelper::SequenceAsHashMap aGlobalEncryptionData
;
1345 aGlobalEncryptionData
= GetCommonRootEncryptionData();
1347 catch( const packages::NoEncryptionException
& )
1349 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
1350 throw packages::WrongPasswordException();
1353 GetCopyOfLastCommit( xTargetStream
, aGlobalEncryptionData
);
1357 xDataToCopy
= m_xPackageStream
->getDataStream();
1359 // in case of new inserted package stream it is possible that input stream still was not set
1360 GetStreamProperties();
1362 CreateReadonlyCopyBasedOnData( xDataToCopy
, m_aProps
, xTargetStream
);
1366 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference
< io::XStream
>& xTargetStream
, const ::comphelper::SequenceAsHashMap
& aEncryptionData
)
1368 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
1370 SAL_WARN_IF( !m_xPackageStream
.is(), "package.xstor", "The source stream for copying is incomplete!" );
1371 if ( !m_xPackageStream
.is() )
1372 throw uno::RuntimeException();
1374 if ( !IsEncrypted() )
1375 throw packages::NoEncryptionException();
1377 uno::Reference
< io::XInputStream
> xDataToCopy
;
1379 if ( m_bHasCachedEncryptionData
)
1381 // TODO: introduce last committed cashed password information and use it here
1382 // that means "use common pass" also should be remembered on flash
1383 uno::Sequence
< beans::NamedValue
> aKey
= aEncryptionData
.getAsConstNamedValueList();
1385 uno::Reference
< beans::XPropertySet
> xProps( m_xPackageStream
, uno::UNO_QUERY_THROW
);
1388 xProps
->getPropertyValue( "Encrypted" ) >>= bEncr
;
1390 throw packages::NoEncryptionException();
1392 uno::Sequence
< beans::NamedValue
> aPackKey
;
1393 xProps
->getPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY
) >>= aPackKey
;
1394 if ( !SequencesEqual( aKey
, aPackKey
) )
1395 throw packages::WrongPasswordException();
1397 // the correct key must be set already
1398 xDataToCopy
= m_xPackageStream
->getDataStream();
1402 uno::Reference
< beans::XPropertySet
> xPropertySet( m_xPackageStream
, uno::UNO_QUERY
);
1403 SetEncryptionKeyProperty_Impl( xPropertySet
, aEncryptionData
.getAsConstNamedValueList() );
1406 xDataToCopy
= m_xPackageStream
->getDataStream();
1408 if ( !xDataToCopy
.is() )
1410 SAL_WARN( "package.xstor", "Encrypted ZipStream must already have input stream inside!" );
1411 SetEncryptionKeyProperty_Impl( xPropertySet
, uno::Sequence
< beans::NamedValue
>() );
1414 catch( const uno::Exception
& )
1416 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't open encrypted stream");
1417 SetEncryptionKeyProperty_Impl( xPropertySet
, uno::Sequence
< beans::NamedValue
>() );
1421 SetEncryptionKeyProperty_Impl( xPropertySet
, uno::Sequence
< beans::NamedValue
>() );
1424 // in case of new inserted package stream it is possible that input stream still was not set
1425 GetStreamProperties();
1427 CreateReadonlyCopyBasedOnData( xDataToCopy
, m_aProps
, xTargetStream
);
1430 void OWriteStream_Impl::CommitStreamRelInfo( const uno::Reference
< embed::XStorage
>& xRelStorage
, std::u16string_view aOrigStreamName
, std::u16string_view aNewStreamName
)
1432 // at this point of time the old stream must be already cleaned
1433 OSL_ENSURE( m_nStorageType
== embed::StorageFormats::OFOPXML
, "The method should be used only with OFOPXML format!" );
1435 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
1438 OSL_ENSURE( !aOrigStreamName
.empty() && !aNewStreamName
.empty() && xRelStorage
.is(),
1439 "Wrong relation persistence information is provided!" );
1441 if ( !xRelStorage
.is() || aOrigStreamName
.empty() || aNewStreamName
.empty() )
1442 throw uno::RuntimeException();
1444 if ( m_nRelInfoStatus
== RELINFO_BROKEN
|| m_nRelInfoStatus
== RELINFO_CHANGED_BROKEN
)
1445 throw io::IOException(); // TODO:
1447 OUString aOrigRelStreamName
= OUString::Concat(aOrigStreamName
) + ".rels";
1448 OUString aNewRelStreamName
= OUString::Concat(aNewStreamName
) + ".rels";
1450 bool bRenamed
= aOrigRelStreamName
!= aNewRelStreamName
;
1451 if ( m_nRelInfoStatus
== RELINFO_CHANGED
1452 || m_nRelInfoStatus
== RELINFO_CHANGED_STREAM_READ
1453 || m_nRelInfoStatus
== RELINFO_CHANGED_STREAM
)
1455 if ( bRenamed
&& xRelStorage
->hasByName( aOrigRelStreamName
) )
1456 xRelStorage
->removeElement( aOrigRelStreamName
);
1458 if ( m_nRelInfoStatus
== RELINFO_CHANGED
)
1460 if ( m_aNewRelInfo
.hasElements() )
1462 uno::Reference
< io::XStream
> xRelsStream
=
1463 xRelStorage
->openStreamElement( aNewRelStreamName
,
1464 embed::ElementModes::TRUNCATE
| embed::ElementModes::READWRITE
);
1466 uno::Reference
< io::XOutputStream
> xOutStream
= xRelsStream
->getOutputStream();
1467 if ( !xOutStream
.is() )
1468 throw uno::RuntimeException();
1470 ::comphelper::OFOPXMLHelper::WriteRelationsInfoSequence( xOutStream
, m_aNewRelInfo
, m_xContext
);
1472 // set the mediatype
1473 uno::Reference
< beans::XPropertySet
> xPropSet( xRelsStream
, uno::UNO_QUERY_THROW
);
1474 xPropSet
->setPropertyValue(
1476 uno::Any( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1478 m_nRelInfoStatus
= RELINFO_READ
;
1481 else if ( m_nRelInfoStatus
== RELINFO_CHANGED_STREAM_READ
1482 || m_nRelInfoStatus
== RELINFO_CHANGED_STREAM
)
1484 uno::Reference
< io::XStream
> xRelsStream
=
1485 xRelStorage
->openStreamElement( aNewRelStreamName
,
1486 embed::ElementModes::TRUNCATE
| embed::ElementModes::READWRITE
);
1488 uno::Reference
< io::XOutputStream
> xOutputStream
= xRelsStream
->getOutputStream();
1489 if ( !xOutputStream
.is() )
1490 throw uno::RuntimeException();
1492 uno::Reference
< io::XSeekable
> xSeek( m_xNewRelInfoStream
, uno::UNO_QUERY_THROW
);
1494 ::comphelper::OStorageHelper::CopyInputToOutput( m_xNewRelInfoStream
, xOutputStream
);
1497 // set the mediatype
1498 uno::Reference
< beans::XPropertySet
> xPropSet( xRelsStream
, uno::UNO_QUERY_THROW
);
1499 xPropSet
->setPropertyValue("MediaType",
1500 uno::Any( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1502 if ( m_nRelInfoStatus
== RELINFO_CHANGED_STREAM
)
1503 m_nRelInfoStatus
= RELINFO_NO_INIT
;
1506 // the information is already parsed and the stream is stored, no need in temporary stream any more
1507 m_xNewRelInfoStream
.clear();
1508 m_nRelInfoStatus
= RELINFO_READ
;
1512 // the original stream makes no sense after this step
1513 m_xOrigRelInfoStream
= m_xNewRelInfoStream
;
1514 m_aOrigRelInfo
= m_aNewRelInfo
;
1515 m_bOrigRelInfoBroken
= false;
1516 m_aNewRelInfo
= uno::Sequence
< uno::Sequence
< beans::StringPair
> >();
1517 m_xNewRelInfoStream
.clear();
1521 // the stream is not changed but it might be renamed
1522 if ( bRenamed
&& xRelStorage
->hasByName( aOrigRelStreamName
) )
1523 xRelStorage
->renameElement( aOrigRelStreamName
, aNewRelStreamName
);
1527 // OWriteStream implementation
1529 OWriteStream::OWriteStream( OWriteStream_Impl
& rImpl
, bool bTransacted
)
1531 , m_xSharedMutex( rImpl
.m_xMutex
)
1532 , m_aListenersContainer( rImpl
.m_xMutex
->GetMutex() )
1533 , m_nStorageType( m_pImpl
->m_nStorageType
)
1534 , m_bInStreamDisconnected( false )
1535 , m_bInitOnDemand( true )
1536 , m_nInitPosition( 0 )
1537 , m_bTransacted( bTransacted
)
1541 OWriteStream::OWriteStream( OWriteStream_Impl
& rImpl
, uno::Reference
< io::XStream
> const & xStream
, bool bTransacted
)
1543 , m_xSharedMutex( rImpl
.m_xMutex
)
1544 , m_aListenersContainer( rImpl
.m_xMutex
->GetMutex() )
1545 , m_nStorageType( m_pImpl
->m_nStorageType
)
1546 , m_bInStreamDisconnected( false )
1547 , m_bInitOnDemand( false )
1548 , m_nInitPosition( 0 )
1549 , m_bTransacted( bTransacted
)
1553 m_xInStream
= xStream
->getInputStream();
1554 m_xOutStream
= xStream
->getOutputStream();
1555 m_xSeekable
.set( xStream
, uno::UNO_QUERY
);
1556 OSL_ENSURE( m_xInStream
.is() && m_xOutStream
.is() && m_xSeekable
.is(), "Stream implementation is incomplete!" );
1560 OWriteStream::~OWriteStream()
1562 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1565 osl_atomic_increment(&m_refCount
);
1569 catch( const uno::RuntimeException
& )
1571 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
1576 void OWriteStream::DeInit()
1579 return; // do nothing
1581 if ( m_xSeekable
.is() )
1582 m_nInitPosition
= m_xSeekable
->getPosition();
1584 m_xInStream
.clear();
1585 m_xOutStream
.clear();
1586 m_xSeekable
.clear();
1587 m_bInitOnDemand
= true;
1590 void OWriteStream::CheckInitOnDemand()
1594 SAL_INFO("package.xstor", "Disposed!");
1595 throw lang::DisposedException();
1598 if ( !m_bInitOnDemand
)
1601 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
1602 uno::Reference
< io::XStream
> xStream
= m_pImpl
->GetTempFileAsStream();
1605 m_xInStream
.set( xStream
->getInputStream(), uno::UNO_SET_THROW
);
1606 m_xOutStream
.set( xStream
->getOutputStream(), uno::UNO_SET_THROW
);
1607 m_xSeekable
.set( xStream
, uno::UNO_QUERY_THROW
);
1608 m_xSeekable
->seek( m_nInitPosition
);
1610 m_nInitPosition
= 0;
1611 m_bInitOnDemand
= false;
1615 void OWriteStream::CopyToStreamInternally_Impl( const uno::Reference
< io::XStream
>& xDest
)
1617 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1619 CheckInitOnDemand();
1621 if ( !m_xInStream
.is() )
1622 throw uno::RuntimeException();
1624 if ( !m_xSeekable
.is() )
1625 throw uno::RuntimeException();
1627 uno::Reference
< beans::XPropertySet
> xDestProps( xDest
, uno::UNO_QUERY_THROW
);
1629 uno::Reference
< io::XOutputStream
> xDestOutStream
= xDest
->getOutputStream();
1630 if ( !xDestOutStream
.is() )
1631 throw io::IOException(); // TODO
1633 sal_Int64 nCurPos
= m_xSeekable
->getPosition();
1634 m_xSeekable
->seek( 0 );
1636 uno::Exception eThrown
;
1637 bool bThrown
= false;
1639 ::comphelper::OStorageHelper::CopyInputToOutput( m_xInStream
, xDestOutStream
);
1641 catch ( const uno::Exception
& e
)
1647 // position-related section below is critical
1648 // if it fails the stream will become invalid
1650 m_xSeekable
->seek( nCurPos
);
1652 catch ( const uno::Exception
& )
1654 // TODO: set the stream in invalid state or dispose
1655 TOOLS_WARN_EXCEPTION( "package.xstor", "The stream become invalid during copying" );
1656 throw uno::RuntimeException();
1662 // now the properties can be copied
1663 // the order of the properties setting is not important for StorageStream API
1664 OUString
aPropName ("Compressed");
1665 xDestProps
->setPropertyValue( aPropName
, getPropertyValue( aPropName
) );
1666 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
|| m_nStorageType
== embed::StorageFormats::OFOPXML
)
1668 aPropName
= "MediaType";
1669 xDestProps
->setPropertyValue( aPropName
, getPropertyValue( aPropName
) );
1671 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
)
1673 aPropName
= "UseCommonStoragePasswordEncryption";
1674 xDestProps
->setPropertyValue( aPropName
, getPropertyValue( aPropName
) );
1679 void OWriteStream::ModifyParentUnlockMutex_Impl(osl::ClearableMutexGuard
& aGuard
)
1681 if ( m_pImpl
->m_pParent
)
1683 if ( m_pImpl
->m_pParent
->HasModifiedListener() )
1685 uno::Reference
< util::XModifiable
> xParentModif( static_cast<util::XModifiable
*>(m_pImpl
->m_pParent
->m_pAntiImpl
) );
1687 xParentModif
->setModified( true );
1690 m_pImpl
->m_pParent
->m_bIsModified
= true;
1694 uno::Any SAL_CALL
OWriteStream::queryInterface( const uno::Type
& rType
)
1696 // common interfaces
1697 uno::Any aReturn
= ::cppu::queryInterface
1699 , static_cast<lang::XTypeProvider
*> ( this )
1700 , static_cast<io::XInputStream
*> ( this )
1701 , static_cast<io::XOutputStream
*> ( this )
1702 , static_cast<io::XStream
*> ( this )
1703 , static_cast<embed::XExtendedStorageStream
*> ( this )
1704 , static_cast<io::XSeekable
*> ( this )
1705 , static_cast<io::XTruncate
*> ( this )
1706 , static_cast<lang::XComponent
*> ( this )
1707 , static_cast<beans::XPropertySet
*> ( this ) );
1709 if ( aReturn
.hasValue() )
1712 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
)
1714 aReturn
= ::cppu::queryInterface
1716 , static_cast<embed::XEncryptionProtectedSource2
*> ( this )
1717 , static_cast<embed::XEncryptionProtectedSource
*> ( this ) );
1719 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
)
1721 aReturn
= ::cppu::queryInterface
1723 , static_cast<embed::XRelationshipAccess
*> ( this ) );
1726 if ( aReturn
.hasValue() )
1729 if ( m_bTransacted
)
1731 aReturn
= ::cppu::queryInterface
1733 , static_cast<embed::XTransactedObject
*> ( this )
1734 , static_cast<embed::XTransactionBroadcaster
*> ( this ) );
1736 if ( aReturn
.hasValue() )
1740 return OWeakObject::queryInterface( rType
);
1743 void SAL_CALL
OWriteStream::acquire() noexcept
1745 OWeakObject::acquire();
1748 void SAL_CALL
OWriteStream::release() noexcept
1750 OWeakObject::release();
1753 uno::Sequence
< uno::Type
> SAL_CALL
OWriteStream::getTypes()
1755 if (! m_oTypeCollection
)
1757 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1759 if (! m_oTypeCollection
)
1761 if ( m_bTransacted
)
1763 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
)
1765 ::cppu::OTypeCollection aTmpCollection
1766 ( cppu::UnoType
<lang::XTypeProvider
>::get()
1767 , cppu::UnoType
<io::XInputStream
>::get()
1768 , cppu::UnoType
<io::XOutputStream
>::get()
1769 , cppu::UnoType
<io::XStream
>::get()
1770 , cppu::UnoType
<io::XSeekable
>::get()
1771 , cppu::UnoType
<io::XTruncate
>::get()
1772 , cppu::UnoType
<lang::XComponent
>::get()
1773 , cppu::UnoType
<embed::XEncryptionProtectedSource2
>::get()
1774 , cppu::UnoType
<embed::XEncryptionProtectedSource
>::get()
1775 , cppu::UnoType
<embed::XExtendedStorageStream
>::get()
1776 , cppu::UnoType
<embed::XTransactedObject
>::get()
1777 , cppu::UnoType
<embed::XTransactionBroadcaster
>::get());
1779 m_oTypeCollection
.emplace(
1780 cppu::UnoType
<beans::XPropertySet
>::get()
1781 , aTmpCollection
.getTypes());
1783 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
)
1785 m_oTypeCollection
.emplace(
1786 cppu::UnoType
<lang::XTypeProvider
>::get()
1787 , cppu::UnoType
<io::XInputStream
>::get()
1788 , cppu::UnoType
<io::XOutputStream
>::get()
1789 , cppu::UnoType
<io::XStream
>::get()
1790 , cppu::UnoType
<io::XSeekable
>::get()
1791 , cppu::UnoType
<io::XTruncate
>::get()
1792 , cppu::UnoType
<lang::XComponent
>::get()
1793 , cppu::UnoType
<embed::XRelationshipAccess
>::get()
1794 , cppu::UnoType
<embed::XExtendedStorageStream
>::get()
1795 , cppu::UnoType
<embed::XTransactedObject
>::get()
1796 , cppu::UnoType
<embed::XTransactionBroadcaster
>::get()
1797 , cppu::UnoType
<beans::XPropertySet
>::get());
1799 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1801 m_oTypeCollection
.emplace(
1802 cppu::UnoType
<lang::XTypeProvider
>::get()
1803 , cppu::UnoType
<io::XInputStream
>::get()
1804 , cppu::UnoType
<io::XOutputStream
>::get()
1805 , cppu::UnoType
<io::XStream
>::get()
1806 , cppu::UnoType
<io::XSeekable
>::get()
1807 , cppu::UnoType
<io::XTruncate
>::get()
1808 , cppu::UnoType
<lang::XComponent
>::get()
1809 , cppu::UnoType
<embed::XExtendedStorageStream
>::get()
1810 , cppu::UnoType
<embed::XTransactedObject
>::get()
1811 , cppu::UnoType
<embed::XTransactionBroadcaster
>::get()
1812 , cppu::UnoType
<beans::XPropertySet
>::get());
1817 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
)
1819 m_oTypeCollection
.emplace(
1820 cppu::UnoType
<lang::XTypeProvider
>::get()
1821 , cppu::UnoType
<io::XInputStream
>::get()
1822 , cppu::UnoType
<io::XOutputStream
>::get()
1823 , cppu::UnoType
<io::XStream
>::get()
1824 , cppu::UnoType
<io::XSeekable
>::get()
1825 , cppu::UnoType
<io::XTruncate
>::get()
1826 , cppu::UnoType
<lang::XComponent
>::get()
1827 , cppu::UnoType
<embed::XEncryptionProtectedSource2
>::get()
1828 , cppu::UnoType
<embed::XEncryptionProtectedSource
>::get()
1829 , cppu::UnoType
<beans::XPropertySet
>::get());
1831 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
)
1833 m_oTypeCollection
.emplace(
1834 cppu::UnoType
<lang::XTypeProvider
>::get()
1835 , cppu::UnoType
<io::XInputStream
>::get()
1836 , cppu::UnoType
<io::XOutputStream
>::get()
1837 , cppu::UnoType
<io::XStream
>::get()
1838 , cppu::UnoType
<io::XSeekable
>::get()
1839 , cppu::UnoType
<io::XTruncate
>::get()
1840 , cppu::UnoType
<lang::XComponent
>::get()
1841 , cppu::UnoType
<embed::XRelationshipAccess
>::get()
1842 , cppu::UnoType
<beans::XPropertySet
>::get());
1844 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1846 m_oTypeCollection
.emplace(
1847 cppu::UnoType
<lang::XTypeProvider
>::get()
1848 , cppu::UnoType
<io::XInputStream
>::get()
1849 , cppu::UnoType
<io::XOutputStream
>::get()
1850 , cppu::UnoType
<io::XStream
>::get()
1851 , cppu::UnoType
<io::XSeekable
>::get()
1852 , cppu::UnoType
<io::XTruncate
>::get()
1853 , cppu::UnoType
<lang::XComponent
>::get()
1854 , cppu::UnoType
<beans::XPropertySet
>::get());
1860 return m_oTypeCollection
->getTypes() ;
1863 uno::Sequence
< sal_Int8
> SAL_CALL
OWriteStream::getImplementationId()
1865 static const comphelper::UnoIdInit lcl_ImplId
;
1866 return lcl_ImplId
.getSeq();
1869 sal_Int32 SAL_CALL
OWriteStream::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
1871 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1873 CheckInitOnDemand();
1877 SAL_INFO("package.xstor", "Disposed!");
1878 throw lang::DisposedException();
1881 if ( !m_xInStream
.is() )
1882 throw io::NotConnectedException();
1884 return m_xInStream
->readBytes( aData
, nBytesToRead
);
1887 sal_Int32 SAL_CALL
OWriteStream::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
1889 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1891 CheckInitOnDemand();
1895 SAL_INFO("package.xstor", "Disposed!");
1896 throw lang::DisposedException();
1899 if ( !m_xInStream
.is() )
1900 throw io::NotConnectedException();
1902 return m_xInStream
->readSomeBytes( aData
, nMaxBytesToRead
);
1905 void SAL_CALL
OWriteStream::skipBytes( sal_Int32 nBytesToSkip
)
1907 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1909 CheckInitOnDemand();
1913 SAL_INFO("package.xstor", "Disposed!");
1914 throw lang::DisposedException();
1917 if ( !m_xInStream
.is() )
1918 throw io::NotConnectedException();
1920 m_xInStream
->skipBytes( nBytesToSkip
);
1923 sal_Int32 SAL_CALL
OWriteStream::available( )
1925 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1927 CheckInitOnDemand();
1931 SAL_INFO("package.xstor", "Disposed!");
1932 throw lang::DisposedException();
1935 if ( !m_xInStream
.is() )
1936 throw io::NotConnectedException();
1938 return m_xInStream
->available();
1942 void SAL_CALL
OWriteStream::closeInput( )
1944 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1948 SAL_INFO("package.xstor", "Disposed!");
1949 throw lang::DisposedException();
1952 if ( !m_bInitOnDemand
&& ( m_bInStreamDisconnected
|| !m_xInStream
.is() ) )
1953 throw io::NotConnectedException();
1955 // the input part of the stream stays open for internal purposes (to allow reading during copying)
1956 // since it can not be reopened until output part is closed, it will be closed with output part.
1957 m_bInStreamDisconnected
= true;
1958 // m_xInStream->closeInput();
1959 // m_xInStream.clear();
1961 if ( !m_xOutStream
.is() )
1965 uno::Reference
< io::XInputStream
> SAL_CALL
OWriteStream::getInputStream()
1967 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1971 SAL_INFO("package.xstor", "Disposed!");
1972 throw lang::DisposedException();
1975 if ( !m_bInitOnDemand
&& ( m_bInStreamDisconnected
|| !m_xInStream
.is() ) )
1976 return uno::Reference
< io::XInputStream
>();
1981 uno::Reference
< io::XOutputStream
> SAL_CALL
OWriteStream::getOutputStream()
1983 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
1987 CheckInitOnDemand();
1989 catch( const io::IOException
& r
)
1991 throw lang::WrappedTargetRuntimeException("OWriteStream::getOutputStream: Could not create backing temp file",
1992 static_cast < OWeakObject
* > ( this ), css::uno::Any ( r
) );
1997 SAL_INFO("package.xstor", "Disposed!");
1998 throw lang::DisposedException();
2001 if ( !m_xOutStream
.is() )
2002 return uno::Reference
< io::XOutputStream
>();
2007 void SAL_CALL
OWriteStream::writeBytes( const uno::Sequence
< sal_Int8
>& aData
)
2009 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2011 // the write method makes initialization itself, since it depends from the aData length
2012 // NO CheckInitOnDemand()!
2016 SAL_INFO("package.xstor", "Disposed!");
2017 throw lang::DisposedException();
2020 if ( !m_bInitOnDemand
)
2022 if ( !m_xOutStream
.is() || !m_xSeekable
.is())
2023 throw io::NotConnectedException();
2025 if ( m_pImpl
->m_xCacheStream
.is() )
2027 // check whether the cache should be turned off
2028 sal_Int64 nPos
= m_xSeekable
->getPosition();
2029 if ( nPos
+ aData
.getLength() > MAX_STORCACHE_SIZE
)
2031 // disconnect the cache and copy the data to the temporary file
2032 m_xSeekable
->seek( 0 );
2034 // it is enough to copy the cached stream, the cache should already contain everything
2035 m_pImpl
->GetFilledTempFileIfNo( m_xInStream
);
2036 if ( m_pImpl
->m_oTempFile
.has_value() )
2039 // the last position is known and it is differs from the current stream position
2040 m_nInitPosition
= nPos
;
2046 if ( m_bInitOnDemand
)
2048 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2049 uno::Reference
< io::XStream
> xStream
= m_pImpl
->GetTempFileAsStream();
2052 m_xInStream
.set( xStream
->getInputStream(), uno::UNO_SET_THROW
);
2053 m_xOutStream
.set( xStream
->getOutputStream(), uno::UNO_SET_THROW
);
2054 m_xSeekable
.set( xStream
, uno::UNO_QUERY_THROW
);
2055 m_xSeekable
->seek( m_nInitPosition
);
2057 m_nInitPosition
= 0;
2058 m_bInitOnDemand
= false;
2062 if ( !m_xOutStream
.is() )
2063 throw io::NotConnectedException();
2065 m_xOutStream
->writeBytes( aData
);
2066 m_pImpl
->m_bHasDataToFlush
= true;
2068 ModifyParentUnlockMutex_Impl( aGuard
);
2071 void OWriteStream::writeBytes( const sal_Int8
* pData
, sal_Int32 nBytesToWrite
)
2073 assert(nBytesToWrite
>= 0);
2075 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2077 // the write method makes initialization itself, since it depends from the aData length
2078 // NO CheckInitOnDemand()!
2082 SAL_INFO("package.xstor", "Disposed!");
2083 throw lang::DisposedException();
2086 if ( !m_bInitOnDemand
)
2088 if ( !m_xOutStream
.is() || !m_xSeekable
.is())
2089 throw io::NotConnectedException();
2091 if ( m_pImpl
->m_xCacheStream
.is() )
2093 // check whether the cache should be turned off
2094 sal_Int64 nPos
= m_xSeekable
->getPosition();
2095 if ( nPos
+ nBytesToWrite
> MAX_STORCACHE_SIZE
)
2097 // disconnect the cache and copy the data to the temporary file
2098 m_xSeekable
->seek( 0 );
2100 // it is enough to copy the cached stream, the cache should already contain everything
2101 m_pImpl
->GetFilledTempFileIfNo( m_xInStream
);
2102 if ( m_pImpl
->m_oTempFile
.has_value() )
2105 // the last position is known and it is differs from the current stream position
2106 m_nInitPosition
= nPos
;
2112 if ( m_bInitOnDemand
)
2114 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2115 uno::Reference
< io::XStream
> xStream
= m_pImpl
->GetTempFileAsStream();
2118 m_xInStream
.set( xStream
->getInputStream(), uno::UNO_SET_THROW
);
2119 m_xOutStream
.set( xStream
->getOutputStream(), uno::UNO_SET_THROW
);
2120 m_xSeekable
.set( xStream
, uno::UNO_QUERY_THROW
);
2121 m_xSeekable
->seek( m_nInitPosition
);
2123 m_nInitPosition
= 0;
2124 m_bInitOnDemand
= false;
2128 if ( !m_xOutStream
.is() )
2129 throw io::NotConnectedException();
2131 if (auto pByteWriter
= dynamic_cast< comphelper::ByteWriter
* >( m_xOutStream
.get() ))
2132 pByteWriter
->writeBytes(pData
, nBytesToWrite
);
2135 uno::Sequence
<sal_Int8
> aData(pData
, nBytesToWrite
);
2136 m_xOutStream
->writeBytes( aData
);
2138 m_pImpl
->m_bHasDataToFlush
= true;
2140 ModifyParentUnlockMutex_Impl( aGuard
);
2143 void SAL_CALL
OWriteStream::flush()
2145 // In case stream is flushed its current version becomes visible
2146 // to the parent storage. Usually parent storage flushes the stream
2147 // during own commit but a user can explicitly flush the stream
2148 // so the changes will be available through cloning functionality.
2150 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2154 SAL_INFO("package.xstor", "Disposed!");
2155 throw lang::DisposedException();
2158 if ( !m_bInitOnDemand
)
2160 if ( !m_xOutStream
.is() )
2161 throw io::NotConnectedException();
2163 m_xOutStream
->flush();
2168 void OWriteStream::CloseOutput_Impl()
2170 // all the checks must be done in calling method
2172 m_xOutStream
->closeOutput();
2173 m_xOutStream
.clear();
2175 if ( m_bInitOnDemand
)
2178 // after the stream is disposed it can be committed
2179 // so transport correct size property
2180 if ( !m_xSeekable
.is() )
2181 throw uno::RuntimeException();
2183 for ( auto& rProp
: asNonConstRange(m_pImpl
->m_aProps
) )
2185 if ( rProp
.Name
== "Size" )
2186 rProp
.Value
<<= m_xSeekable
->getLength();
2190 void SAL_CALL
OWriteStream::closeOutput()
2192 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2194 CheckInitOnDemand();
2198 SAL_INFO("package.xstor", "Disposed!");
2199 throw lang::DisposedException();
2202 if ( !m_xOutStream
.is() )
2203 throw io::NotConnectedException();
2207 if ( m_bInStreamDisconnected
|| !m_xInStream
.is() )
2211 void SAL_CALL
OWriteStream::seek( sal_Int64 location
)
2213 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2215 CheckInitOnDemand();
2219 SAL_INFO("package.xstor", "Disposed!");
2220 throw lang::DisposedException();
2223 if ( !m_xSeekable
.is() )
2224 throw uno::RuntimeException();
2226 m_xSeekable
->seek( location
);
2229 sal_Int64 SAL_CALL
OWriteStream::getPosition()
2231 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2233 CheckInitOnDemand();
2237 SAL_INFO("package.xstor", "Disposed!");
2238 throw lang::DisposedException();
2241 if ( !m_xSeekable
.is() )
2242 throw uno::RuntimeException();
2244 return m_xSeekable
->getPosition();
2247 sal_Int64 SAL_CALL
OWriteStream::getLength()
2249 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2251 CheckInitOnDemand();
2255 SAL_INFO("package.xstor", "Disposed!");
2256 throw lang::DisposedException();
2259 if ( !m_xSeekable
.is() )
2260 throw uno::RuntimeException();
2262 return m_xSeekable
->getLength();
2265 void SAL_CALL
OWriteStream::truncate()
2267 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2269 CheckInitOnDemand();
2273 SAL_INFO("package.xstor", "Disposed!");
2274 throw lang::DisposedException();
2277 if ( !m_xOutStream
.is() )
2278 throw uno::RuntimeException();
2280 uno::Reference
< io::XTruncate
> xTruncate( m_xOutStream
, uno::UNO_QUERY_THROW
);
2281 xTruncate
->truncate();
2283 m_pImpl
->m_bHasDataToFlush
= true;
2285 ModifyParentUnlockMutex_Impl( aGuard
);
2288 void SAL_CALL
OWriteStream::dispose()
2290 // should be an internal method since it can be called only from parent storage
2292 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2296 SAL_INFO("package.xstor", "Disposed!");
2297 throw lang::DisposedException();
2300 if ( m_xOutStream
.is() )
2303 if ( m_xInStream
.is() )
2305 m_xInStream
->closeInput();
2306 m_xInStream
.clear();
2309 m_xSeekable
.clear();
2311 m_pImpl
->m_pAntiImpl
= nullptr;
2313 if ( !m_bInitOnDemand
)
2317 if ( !m_bTransacted
)
2323 // throw away all the changes
2327 catch( const uno::Exception
& )
2329 uno::Any
aCaught( ::cppu::getCaughtException() );
2330 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught
));
2331 throw lang::WrappedTargetRuntimeException("Can not commit/revert the storage!",
2332 static_cast< OWeakObject
* >( this ),
2340 // the listener might try to get rid of parent storage, and the storage would delete this object;
2341 // for now the listener is just notified at the end of the method to workaround the problem
2342 // in future a more elegant way should be found
2344 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >(this) );
2345 m_aListenersContainer
.disposeAndClear( aSource
);
2348 void SAL_CALL
OWriteStream::addEventListener(
2349 const uno::Reference
< lang::XEventListener
>& xListener
)
2351 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2355 SAL_INFO("package.xstor", "Disposed!");
2356 throw lang::DisposedException();
2359 m_aListenersContainer
.addInterface( cppu::UnoType
<lang::XEventListener
>::get(),
2363 void SAL_CALL
OWriteStream::removeEventListener(
2364 const uno::Reference
< lang::XEventListener
>& xListener
)
2366 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2370 SAL_INFO("package.xstor", "Disposed!");
2371 throw lang::DisposedException();
2374 m_aListenersContainer
.removeInterface( cppu::UnoType
<lang::XEventListener
>::get(),
2378 void SAL_CALL
OWriteStream::setEncryptionPassword( const OUString
& aPass
)
2380 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2382 CheckInitOnDemand();
2386 SAL_INFO("package.xstor", "Disposed!");
2387 throw lang::DisposedException();
2390 OSL_ENSURE( m_pImpl
->m_xPackageStream
.is(), "No package stream is set!" );
2392 m_pImpl
->SetEncrypted( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aPass
) );
2394 ModifyParentUnlockMutex_Impl( aGuard
);
2397 void SAL_CALL
OWriteStream::removeEncryption()
2399 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2401 CheckInitOnDemand();
2405 SAL_INFO("package.xstor", "Disposed!");
2406 throw lang::DisposedException();
2409 OSL_ENSURE( m_pImpl
->m_xPackageStream
.is(), "No package stream is set!" );
2411 m_pImpl
->SetDecrypted();
2413 ModifyParentUnlockMutex_Impl( aGuard
);
2416 void SAL_CALL
OWriteStream::setEncryptionData( const uno::Sequence
< beans::NamedValue
>& aEncryptionData
)
2418 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2420 CheckInitOnDemand();
2424 SAL_INFO("package.xstor", "Disposed!");
2425 throw lang::DisposedException();
2428 OSL_ENSURE( m_pImpl
->m_xPackageStream
.is(), "No package stream is set!" );
2430 m_pImpl
->SetEncrypted( aEncryptionData
);
2432 ModifyParentUnlockMutex_Impl( aGuard
);
2435 sal_Bool SAL_CALL
OWriteStream::hasEncryptionData()
2437 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2446 bRet
= m_pImpl
->IsEncrypted();
2448 if (!bRet
&& m_pImpl
->m_bUseCommonEncryption
&& m_pImpl
->m_pParent
)
2449 bRet
= m_pImpl
->m_pParent
->m_bHasCommonEncryptionData
;
2451 catch( const uno::RuntimeException
& )
2453 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
2456 catch( const uno::Exception
& )
2458 uno::Any
aCaught( ::cppu::getCaughtException() );
2459 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught
));
2460 throw lang::WrappedTargetRuntimeException( "Problems on hasEncryptionData!",
2461 static_cast< ::cppu::OWeakObject
* >( this ),
2468 sal_Bool SAL_CALL
OWriteStream::hasByID( const OUString
& sID
)
2470 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2474 SAL_INFO("package.xstor", "Disposed!");
2475 throw lang::DisposedException();
2478 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2479 throw uno::RuntimeException();
2483 getRelationshipByID( sID
);
2486 catch( const container::NoSuchElementException
& )
2488 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
2494 OUString SAL_CALL
OWriteStream::getTargetByID( const OUString
& sID
)
2496 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2500 SAL_INFO("package.xstor", "Disposed!");
2501 throw lang::DisposedException();
2504 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2505 throw uno::RuntimeException();
2507 const uno::Sequence
< beans::StringPair
> aSeq
= getRelationshipByID( sID
);
2508 auto pRel
= lcl_findPairByName(aSeq
, "Target");
2509 if (pRel
!= aSeq
.end())
2510 return pRel
->Second
;
2515 OUString SAL_CALL
OWriteStream::getTypeByID( const OUString
& sID
)
2517 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2521 SAL_INFO("package.xstor", "Disposed!");
2522 throw lang::DisposedException();
2525 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2526 throw uno::RuntimeException();
2528 const uno::Sequence
< beans::StringPair
> aSeq
= getRelationshipByID( sID
);
2529 auto pRel
= lcl_findPairByName(aSeq
, "Type");
2530 if (pRel
!= aSeq
.end())
2531 return pRel
->Second
;
2536 uno::Sequence
< beans::StringPair
> SAL_CALL
OWriteStream::getRelationshipByID( const OUString
& sID
)
2538 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2542 SAL_INFO("package.xstor", "Disposed!");
2543 throw lang::DisposedException();
2546 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2547 throw uno::RuntimeException();
2549 // TODO/LATER: in future the unification of the ID could be checked
2550 const uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
2551 const beans::StringPair
aIDRel("Id", sID
);
2552 auto pRel
= std::find_if(aSeq
.begin(), aSeq
.end(),
2553 [&aIDRel
](const uno::Sequence
<beans::StringPair
>& rRel
) {
2554 return std::find(rRel
.begin(), rRel
.end(), aIDRel
) != rRel
.end(); });
2555 if (pRel
!= aSeq
.end())
2558 throw container::NoSuchElementException();
2561 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OWriteStream::getRelationshipsByType( const OUString
& sType
)
2563 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2567 SAL_INFO("package.xstor", "Disposed!");
2568 throw lang::DisposedException();
2571 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2572 throw uno::RuntimeException();
2574 // TODO/LATER: in future the unification of the ID could be checked
2575 const uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
2576 const beans::StringPair
aTypeRel("Type", sType
);
2577 std::vector
< uno::Sequence
<beans::StringPair
> > aResult
;
2578 aResult
.reserve(aSeq
.getLength());
2580 std::copy_if(aSeq
.begin(), aSeq
.end(), std::back_inserter(aResult
),
2581 [&aTypeRel
](const uno::Sequence
<beans::StringPair
>& rRel
) {
2582 return std::find(rRel
.begin(), rRel
.end(), aTypeRel
) != rRel
.end(); });
2584 return comphelper::containerToSequence(aResult
);
2587 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OWriteStream::getAllRelationships()
2589 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2593 SAL_INFO("package.xstor", "Disposed!");
2594 throw lang::DisposedException();
2597 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2598 throw uno::RuntimeException();
2600 return m_pImpl
->GetAllRelationshipsIfAny();
2603 void SAL_CALL
OWriteStream::insertRelationshipByID( const OUString
& sID
, const uno::Sequence
< beans::StringPair
>& aEntry
, sal_Bool bReplace
)
2605 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2609 SAL_INFO("package.xstor", "Disposed!");
2610 throw lang::DisposedException();
2613 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2614 throw uno::RuntimeException();
2616 const beans::StringPair
aIDRel("Id", sID
);
2618 uno::Sequence
<beans::StringPair
>* pPair
= nullptr;
2620 // TODO/LATER: in future the unification of the ID could be checked
2621 uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
2622 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
2624 const auto& rRel
= aSeq
[nInd
];
2625 if (std::find(rRel
.begin(), rRel
.end(), aIDRel
) != rRel
.end())
2626 pPair
= &aSeq
.getArray()[nInd
];
2629 if ( pPair
&& !bReplace
)
2630 throw container::ElementExistException(); // TODO
2634 sal_Int32 nIDInd
= aSeq
.getLength();
2635 aSeq
.realloc( nIDInd
+ 1 );
2636 pPair
= &aSeq
.getArray()[nIDInd
];
2639 std::vector
<beans::StringPair
> aResult
;
2640 aResult
.reserve(aEntry
.getLength() + 1);
2642 aResult
.push_back(aIDRel
);
2643 std::copy_if(aEntry
.begin(), aEntry
.end(), std::back_inserter(aResult
),
2644 [](const beans::StringPair
& rRel
) { return rRel
.First
!= "Id"; });
2646 *pPair
= comphelper::containerToSequence(aResult
);
2648 m_pImpl
->m_aNewRelInfo
= aSeq
;
2649 m_pImpl
->m_xNewRelInfoStream
.clear();
2650 m_pImpl
->m_nRelInfoStatus
= RELINFO_CHANGED
;
2653 void SAL_CALL
OWriteStream::removeRelationshipByID( const OUString
& sID
)
2655 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2659 SAL_INFO("package.xstor", "Disposed!");
2660 throw lang::DisposedException();
2663 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2664 throw uno::RuntimeException();
2666 uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
2667 const beans::StringPair
aIDRel("Id", sID
);
2668 auto pRel
= std::find_if(std::cbegin(aSeq
), std::cend(aSeq
),
2669 [&aIDRel
](const uno::Sequence
< beans::StringPair
>& rRel
) {
2670 return std::find(rRel
.begin(), rRel
.end(), aIDRel
) != rRel
.end(); });
2671 if (pRel
!= std::cend(aSeq
))
2673 auto nInd
= static_cast<sal_Int32
>(std::distance(std::cbegin(aSeq
), pRel
));
2674 comphelper::removeElementAt(aSeq
, nInd
);
2676 m_pImpl
->m_aNewRelInfo
= aSeq
;
2677 m_pImpl
->m_xNewRelInfoStream
.clear();
2678 m_pImpl
->m_nRelInfoStatus
= RELINFO_CHANGED
;
2680 // TODO/LATER: in future the unification of the ID could be checked
2684 throw container::NoSuchElementException();
2687 void SAL_CALL
OWriteStream::insertRelationships( const uno::Sequence
< uno::Sequence
< beans::StringPair
> >& aEntries
, sal_Bool bReplace
)
2689 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2693 SAL_INFO("package.xstor", "Disposed!");
2694 throw lang::DisposedException();
2697 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2698 throw uno::RuntimeException();
2700 OUString
aIDTag( "Id" );
2701 const uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
2702 std::vector
< uno::Sequence
<beans::StringPair
> > aResultVec
;
2703 aResultVec
.reserve(aSeq
.getLength() + aEntries
.getLength());
2705 std::copy_if(aSeq
.begin(), aSeq
.end(), std::back_inserter(aResultVec
),
2706 [&aIDTag
, &aEntries
, bReplace
](const uno::Sequence
<beans::StringPair
>& rTargetRel
) {
2707 auto pTargetPair
= lcl_findPairByName(rTargetRel
, aIDTag
);
2708 if (pTargetPair
== rTargetRel
.end())
2711 bool bIsSourceSame
= std::any_of(aEntries
.begin(), aEntries
.end(),
2712 [&pTargetPair
](const uno::Sequence
<beans::StringPair
>& rSourceEntry
) {
2713 return std::find(rSourceEntry
.begin(), rSourceEntry
.end(), *pTargetPair
) != rSourceEntry
.end(); });
2715 if ( bIsSourceSame
&& !bReplace
)
2716 throw container::ElementExistException();
2718 // if no such element in the provided sequence
2719 return !bIsSourceSame
;
2722 std::transform(aEntries
.begin(), aEntries
.end(), std::back_inserter(aResultVec
),
2723 [&aIDTag
](const uno::Sequence
<beans::StringPair
>& rEntry
) -> uno::Sequence
<beans::StringPair
> {
2724 auto pPair
= lcl_findPairByName(rEntry
, aIDTag
);
2725 if (pPair
== rEntry
.end())
2726 throw io::IOException(); // TODO: illegal relation ( no ID )
2728 auto aResult
= comphelper::sequenceToContainer
<std::vector
<beans::StringPair
>>(rEntry
);
2729 auto nIDInd
= std::distance(rEntry
.begin(), pPair
);
2730 std::rotate(aResult
.begin(), std::next(aResult
.begin(), nIDInd
), std::next(aResult
.begin(), nIDInd
+ 1));
2732 return comphelper::containerToSequence(aResult
);
2735 m_pImpl
->m_aNewRelInfo
= comphelper::containerToSequence(aResultVec
);
2736 m_pImpl
->m_xNewRelInfoStream
.clear();
2737 m_pImpl
->m_nRelInfoStatus
= RELINFO_CHANGED
;
2740 void SAL_CALL
OWriteStream::clearRelationships()
2742 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2746 SAL_INFO("package.xstor", "Disposed!");
2747 throw lang::DisposedException();
2750 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
2751 throw uno::RuntimeException();
2753 m_pImpl
->m_aNewRelInfo
.realloc( 0 );
2754 m_pImpl
->m_xNewRelInfoStream
.clear();
2755 m_pImpl
->m_nRelInfoStatus
= RELINFO_CHANGED
;
2758 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
OWriteStream::getPropertySetInfo()
2760 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2763 return uno::Reference
< beans::XPropertySetInfo
>();
2766 void SAL_CALL
OWriteStream::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& aValue
)
2768 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
2772 SAL_INFO("package.xstor", "Disposed!");
2773 throw lang::DisposedException();
2776 m_pImpl
->GetStreamProperties();
2777 static constexpr OUStringLiteral
aCompressedString( u
"Compressed" );
2778 static constexpr OUStringLiteral
aMediaTypeString( u
"MediaType" );
2779 if ( m_nStorageType
== embed::StorageFormats::PACKAGE
&& aPropertyName
== aMediaTypeString
)
2781 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2782 bool bCompressedValueFromType
= true;
2786 if ( !m_pImpl
->m_bCompressedSetExplicit
)
2788 if ( aType
== "image/jpeg" || aType
== "image/png" || aType
== "image/gif" )
2789 bCompressedValueFromType
= false;
2792 for ( auto& rProp
: asNonConstRange(m_pImpl
->m_aProps
) )
2794 if ( aPropertyName
== rProp
.Name
)
2795 rProp
.Value
= aValue
;
2796 else if ( !m_pImpl
->m_bCompressedSetExplicit
&& aCompressedString
== rProp
.Name
)
2797 rProp
.Value
<<= bCompressedValueFromType
;
2800 else if ( aPropertyName
== aCompressedString
)
2802 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2803 m_pImpl
->m_bCompressedSetExplicit
= true;
2804 for ( auto& rProp
: asNonConstRange(m_pImpl
->m_aProps
) )
2806 if ( aPropertyName
== rProp
.Name
)
2807 rProp
.Value
= aValue
;
2810 else if ( m_nStorageType
== embed::StorageFormats::PACKAGE
2811 && aPropertyName
== "UseCommonStoragePasswordEncryption" )
2813 bool bUseCommonEncryption
= false;
2814 if ( !(aValue
>>= bUseCommonEncryption
) )
2815 throw lang::IllegalArgumentException(); //TODO
2817 if ( m_bInitOnDemand
&& m_pImpl
->m_bHasInsertedStreamOptimization
)
2819 // the data stream is provided to the packagestream directly
2820 m_pImpl
->m_bUseCommonEncryption
= bUseCommonEncryption
;
2822 else if ( bUseCommonEncryption
)
2824 if ( !m_pImpl
->m_bUseCommonEncryption
)
2826 m_pImpl
->SetDecrypted();
2827 m_pImpl
->m_bUseCommonEncryption
= true;
2831 m_pImpl
->m_bUseCommonEncryption
= false;
2833 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
&& aPropertyName
== aMediaTypeString
)
2835 for ( auto& rProp
: asNonConstRange(m_pImpl
->m_aProps
) )
2837 if ( aPropertyName
== rProp
.Name
)
2838 rProp
.Value
= aValue
;
2841 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
&& aPropertyName
== "RelationsInfoStream" )
2843 uno::Reference
< io::XInputStream
> xInRelStream
;
2844 if ( !( aValue
>>= xInRelStream
) || !xInRelStream
.is() )
2845 throw lang::IllegalArgumentException(); // TODO
2847 uno::Reference
< io::XSeekable
> xSeek( xInRelStream
, uno::UNO_QUERY
);
2850 // currently this is an internal property that is used for optimization
2851 // and the stream must support XSeekable interface
2852 // TODO/LATER: in future it can be changed if property is used from outside
2853 throw lang::IllegalArgumentException(); // TODO
2856 m_pImpl
->m_xNewRelInfoStream
= xInRelStream
;
2857 m_pImpl
->m_aNewRelInfo
= uno::Sequence
< uno::Sequence
< beans::StringPair
> >();
2858 m_pImpl
->m_nRelInfoStatus
= RELINFO_CHANGED_STREAM
;
2860 else if ( m_nStorageType
== embed::StorageFormats::OFOPXML
&& aPropertyName
== "RelationsInfo" )
2862 if ( !(aValue
>>= m_pImpl
->m_aNewRelInfo
) )
2863 throw lang::IllegalArgumentException(); // TODO
2865 else if ( aPropertyName
== "Size" )
2866 throw beans::PropertyVetoException(); // TODO
2867 else if ( m_nStorageType
== embed::StorageFormats::PACKAGE
2868 && ( aPropertyName
== "IsEncrypted" || aPropertyName
== "Encrypted" ) )
2869 throw beans::PropertyVetoException(); // TODO
2870 else if ( aPropertyName
== "RelId" )
2872 aValue
>>= m_pImpl
->m_nRelId
;
2875 throw beans::UnknownPropertyException(aPropertyName
); // TODO
2877 m_pImpl
->m_bHasDataToFlush
= true;
2878 ModifyParentUnlockMutex_Impl( aGuard
);
2881 uno::Any SAL_CALL
OWriteStream::getPropertyValue( const OUString
& aProp
)
2883 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2887 SAL_INFO("package.xstor", "Disposed!");
2888 throw lang::DisposedException();
2891 if ( aProp
== "RelId" )
2893 return uno::Any( m_pImpl
->GetNewRelId() );
2896 OUString aPropertyName
;
2897 if ( aProp
== "IsEncrypted" )
2898 aPropertyName
= "Encrypted";
2900 aPropertyName
= aProp
;
2902 if ( ( ( m_nStorageType
== embed::StorageFormats::PACKAGE
|| m_nStorageType
== embed::StorageFormats::OFOPXML
)
2903 && aPropertyName
== "MediaType" )
2904 || ( m_nStorageType
== embed::StorageFormats::PACKAGE
&& aPropertyName
== "Encrypted" )
2905 || aPropertyName
== "Compressed" )
2907 m_pImpl
->GetStreamProperties();
2909 auto pProp
= std::find_if(std::cbegin(m_pImpl
->m_aProps
), std::cend(m_pImpl
->m_aProps
),
2910 [&aPropertyName
](const css::beans::PropertyValue
& rProp
){ return aPropertyName
== rProp
.Name
; });
2911 if (pProp
!= std::cend(m_pImpl
->m_aProps
))
2912 return pProp
->Value
;
2914 else if ( m_nStorageType
== embed::StorageFormats::PACKAGE
2915 && aPropertyName
== "UseCommonStoragePasswordEncryption" )
2916 return uno::Any( m_pImpl
->m_bUseCommonEncryption
);
2917 else if ( aPropertyName
== "Size" )
2919 bool bThrow
= false;
2922 CheckInitOnDemand();
2924 catch (const io::IOException
&)
2928 if (bThrow
|| !m_xSeekable
.is())
2929 throw uno::RuntimeException();
2931 return uno::Any( m_xSeekable
->getLength() );
2934 throw beans::UnknownPropertyException(aPropertyName
); // TODO
2937 void SAL_CALL
OWriteStream::addPropertyChangeListener(
2938 const OUString
& /*aPropertyName*/,
2939 const uno::Reference
< beans::XPropertyChangeListener
>& /*xListener*/ )
2941 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2945 SAL_INFO("package.xstor", "Disposed!");
2946 throw lang::DisposedException();
2952 void SAL_CALL
OWriteStream::removePropertyChangeListener(
2953 const OUString
& /*aPropertyName*/,
2954 const uno::Reference
< beans::XPropertyChangeListener
>& /*aListener*/ )
2956 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2960 SAL_INFO("package.xstor", "Disposed!");
2961 throw lang::DisposedException();
2967 void SAL_CALL
OWriteStream::addVetoableChangeListener(
2968 const OUString
& /*PropertyName*/,
2969 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
2971 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2975 SAL_INFO("package.xstor", "Disposed!");
2976 throw lang::DisposedException();
2982 void SAL_CALL
OWriteStream::removeVetoableChangeListener(
2983 const OUString
& /*PropertyName*/,
2984 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
2986 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
2990 SAL_INFO("package.xstor", "Disposed!");
2991 throw lang::DisposedException();
2997 // XTransactedObject
2999 void OWriteStream::BroadcastTransaction( sal_Int8 nMessage
)
3007 // no need to lock mutex here for the checking of m_pImpl, and m_pData is alive until the object is destructed
3010 SAL_INFO("package.xstor", "Disposed!");
3011 throw lang::DisposedException();
3014 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >(this) );
3016 comphelper::OInterfaceContainerHelper2
* pContainer
=
3017 m_aListenersContainer
.getContainer(
3018 cppu::UnoType
<embed::XTransactionListener
>::get());
3022 comphelper::OInterfaceIteratorHelper2
pIterator( *pContainer
);
3023 while ( pIterator
.hasMoreElements( ) )
3025 OSL_ENSURE( nMessage
>= 1 && nMessage
<= 4, "Wrong internal notification code is used!" );
3029 case STOR_MESS_PRECOMMIT
:
3030 static_cast<embed::XTransactionListener
*>( pIterator
.next( ) )->preCommit( aSource
);
3032 case STOR_MESS_COMMITTED
:
3033 static_cast<embed::XTransactionListener
*>( pIterator
.next( ) )->commited( aSource
);
3035 case STOR_MESS_PREREVERT
:
3036 static_cast<embed::XTransactionListener
*>( pIterator
.next( ) )->preRevert( aSource
);
3038 case STOR_MESS_REVERTED
:
3039 static_cast< embed::XTransactionListener
*>( pIterator
.next( ) )->reverted( aSource
);
3044 void SAL_CALL
OWriteStream::commit()
3046 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::commit" );
3050 SAL_INFO("package.xstor", "Disposed!");
3051 throw lang::DisposedException();
3054 if ( !m_bTransacted
)
3055 throw uno::RuntimeException();
3058 BroadcastTransaction( STOR_MESS_PRECOMMIT
);
3060 osl::ClearableMutexGuard
aGuard(m_xSharedMutex
->GetMutex());
3064 SAL_INFO("package.xstor", "Disposed!");
3065 throw lang::DisposedException();
3070 // when the storage is committed the parent is modified
3071 ModifyParentUnlockMutex_Impl( aGuard
);
3073 catch( const io::IOException
& )
3075 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3078 catch( const embed::StorageWrappedTargetException
& )
3080 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3083 catch( const uno::RuntimeException
& )
3085 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3088 catch( const uno::Exception
& )
3090 uno::Any
aCaught( ::cppu::getCaughtException() );
3091 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught
));
3092 throw embed::StorageWrappedTargetException( "Problems on commit!",
3093 static_cast< ::cppu::OWeakObject
* >( this ),
3097 BroadcastTransaction( STOR_MESS_COMMITTED
);
3100 void SAL_CALL
OWriteStream::revert()
3102 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::revert" );
3104 // the method removes all the changes done after last commit
3108 SAL_INFO("package.xstor", "Disposed!");
3109 throw lang::DisposedException();
3112 if ( !m_bTransacted
)
3113 throw uno::RuntimeException();
3115 BroadcastTransaction( STOR_MESS_PREREVERT
);
3118 osl::MutexGuard
aGuard(m_xSharedMutex
->GetMutex());
3122 SAL_INFO("package.xstor", "Disposed!");
3123 throw lang::DisposedException();
3129 catch (const io::IOException
&)
3131 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3134 catch (const embed::StorageWrappedTargetException
&)
3136 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3139 catch (const uno::RuntimeException
&)
3141 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3144 catch (const uno::Exception
&)
3146 uno::Any
aCaught(::cppu::getCaughtException());
3147 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught
));
3148 throw embed::StorageWrappedTargetException("Problems on revert!",
3149 static_cast<::cppu::OWeakObject
*>(this),
3154 BroadcastTransaction( STOR_MESS_REVERTED
);
3157 // XTransactionBroadcaster
3159 void SAL_CALL
OWriteStream::addTransactionListener( const uno::Reference
< embed::XTransactionListener
>& aListener
)
3161 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
3165 SAL_INFO("package.xstor", "Disposed!");
3166 throw lang::DisposedException();
3169 if ( !m_bTransacted
)
3170 throw uno::RuntimeException();
3172 m_aListenersContainer
.addInterface( cppu::UnoType
<embed::XTransactionListener
>::get(),
3176 void SAL_CALL
OWriteStream::removeTransactionListener( const uno::Reference
< embed::XTransactionListener
>& aListener
)
3178 ::osl::MutexGuard
aGuard( m_xSharedMutex
->GetMutex() );
3182 SAL_INFO("package.xstor", "Disposed!");
3183 throw lang::DisposedException();
3186 if ( !m_bTransacted
)
3187 throw uno::RuntimeException();
3189 m_aListenersContainer
.removeInterface( cppu::UnoType
<embed::XTransactionListener
>::get(),
3193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */