1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <memory>
21 #include <sal/config.h>
22 #include <sal/log.hxx>
23
24 #include <com/sun/star/packages/WrongPasswordException.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
27 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include <com/sun/star/lang/XTypeProvider.hpp>
31 #include <com/sun/star/io/NotConnectedException.hpp>
32 #include <com/sun/star/io/TempFile.hpp>
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/io/IOException.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/embed/StorageFormats.hpp>
37 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
38 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
39 #include <cppuhelper/typeprovider.hxx>
40 #include <cppuhelper/queryinterface.hxx>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include <osl/diagnose.h>
43
44 #include <comphelper/processfactory.hxx>
45 #include <comphelper/storagehelper.hxx>
46 #include <comphelper/ofopxmlhelper.hxx>
47 #include <comphelper/refcountedmutex.hxx>
48 #include <comphelper/sequence.hxx>
49
50 #include <rtl/digest.h>
51 #include <rtl/instance.hxx>
52 #include <tools/diagnose_ex.h>
53
54 #include <PackageConstants.hxx>
55
56 #include "selfterminatefilestream.hxx"
57 #include "owriteablestream.hxx"
58 #include "oseekinstream.hxx"
59 #include "xstorage.hxx"
60
61 // since the copying uses 32000 blocks usually, it makes sense to have a smaller size
62 #define MAX_STORCACHE_SIZE 30000
63
64 using namespace ::com::sun::star;
65
66 struct WSInternalData_Impl
67 {
68 rtl::Reference<comphelper::RefCountedMutex> m_xSharedMutex;
69 ::std::unique_ptr< ::cppu::OTypeCollection> m_pTypeCollection;
70 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenersContainer; // list of listeners
71 sal_Int32 m_nStorageType;
72
73 // the mutex reference MUST NOT be empty
WSInternalData_ImplWSInternalData_Impl74 WSInternalData_Impl( const rtl::Reference<comphelper::RefCountedMutex>& rMutexRef, sal_Int32 nStorageType )
75 : m_xSharedMutex( rMutexRef )
76 , m_pTypeCollection()
77 , m_aListenersContainer( rMutexRef->GetMutex() )
78 , m_nStorageType( nStorageType )
79 {}
80 };
81
82 namespace package
83 {
84
PackageEncryptionDataLessOrEqual(const::comphelper::SequenceAsHashMap & aHash1,const::comphelper::SequenceAsHashMap & aHash2)85 bool PackageEncryptionDataLessOrEqual( const ::comphelper::SequenceAsHashMap& aHash1, const ::comphelper::SequenceAsHashMap& aHash2 )
86 {
87 // tdf#93389: aHash2 may contain more than in aHash1, if it contains also data for other package
88 // formats (as in case of autorecovery)
89 bool bResult = !aHash1.empty() && aHash1.size() <= aHash2.size();
90 for ( ::comphelper::SequenceAsHashMap::const_iterator aIter = aHash1.begin();
91 bResult && aIter != aHash1.end();
92 ++aIter )
93 {
94 uno::Sequence< sal_Int8 > aKey1;
95 bResult = ( ( aIter->second >>= aKey1 ) && aKey1.hasElements() );
96 if ( bResult )
97 {
98 const uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first, uno::Sequence< sal_Int8 >() );
99 bResult = aKey1.getLength() == aKey2.getLength()
100 && std::equal(std::cbegin(aKey1), std::cend(aKey1), aKey2.begin(), aKey2.end());
101 }
102 }
103
104 return bResult;
105 }
106
107 } // namespace package
108
109 namespace
110 {
111
SetEncryptionKeyProperty_Impl(const uno::Reference<beans::XPropertySet> & xPropertySet,const uno::Sequence<beans::NamedValue> & aKey)112 void SetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet,
113 const uno::Sequence< beans::NamedValue >& aKey )
114 {
115 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
116 if ( !xPropertySet.is() )
117 throw uno::RuntimeException();
118
119 try {
120 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY, uno::makeAny( aKey ) );
121 }
122 catch ( const uno::Exception& ex )
123 {
124 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't write encryption related properties");
125 throw io::IOException(ex.Message); // TODO
126 }
127 }
128
GetEncryptionKeyProperty_Impl(const uno::Reference<beans::XPropertySet> & xPropertySet)129 uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet )
130 {
131 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
132 if ( !xPropertySet.is() )
133 throw uno::RuntimeException();
134
135 try {
136 return xPropertySet->getPropertyValue(STORAGE_ENCRYPTION_KEYS_PROPERTY);
137 }
138 catch ( const uno::Exception& ex )
139 {
140 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't get encryption related properties");
141 throw io::IOException(ex.Message); // TODO
142 }
143 }
144
SequencesEqual(const uno::Sequence<sal_Int8> & aSequence1,const uno::Sequence<sal_Int8> & aSequence2)145 bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 )
146 {
147 return aSequence1.getLength() == aSequence2.getLength()
148 && std::equal(aSequence1.begin(), aSequence1.end(), aSequence2.begin(), aSequence2.end());
149 }
150
SequencesEqual(const uno::Sequence<beans::NamedValue> & aSequence1,const uno::Sequence<beans::NamedValue> & aSequence2)151 bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 )
152 {
153 if ( aSequence1.getLength() != aSequence2.getLength() )
154 return false;
155
156 for ( const auto& rProp1 : aSequence1 )
157 {
158 bool bHasMember = false;
159 uno::Sequence< sal_Int8 > aMember1;
160 sal_Int32 nMember1 = 0;
161 if ( rProp1.Value >>= aMember1 )
162 {
163 for ( const auto& rProp2 : aSequence2 )
164 {
165 if ( rProp1.Name == rProp2.Name )
166 {
167 bHasMember = true;
168
169 uno::Sequence< sal_Int8 > aMember2;
170 if ( !( rProp2.Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) )
171 return false;
172 }
173 }
174 }
175 else if ( rProp1.Value >>= nMember1 )
176 {
177 for ( const auto& rProp2 : aSequence2 )
178 {
179 if ( rProp1.Name == rProp2.Name )
180 {
181 bHasMember = true;
182
183 sal_Int32 nMember2 = 0;
184 if ( !( rProp2.Value >>= nMember2 ) || nMember1 != nMember2 )
185 return false;
186 }
187 }
188 }
189 else
190 return false;
191
192 if ( !bHasMember )
193 return false;
194 }
195
196 return true;
197 }
198
KillFile(const OUString & aURL,const uno::Reference<uno::XComponentContext> & xContext)199 bool KillFile( const OUString& aURL, const uno::Reference< uno::XComponentContext >& xContext )
200 {
201 if ( !xContext.is() )
202 return false;
203
204 bool bRet = false;
205
206 try
207 {
208 uno::Reference < ucb::XSimpleFileAccess3 > xAccess( ucb::SimpleFileAccess::create( xContext ) );
209
210 xAccess->kill( aURL );
211 bRet = true;
212 }
213 catch( const uno::Exception& )
214 {
215 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
216 }
217
218 return bRet;
219 }
220
GetNewTempFileURL(const uno::Reference<uno::XComponentContext> & rContext)221 OUString GetNewTempFileURL( const uno::Reference< uno::XComponentContext >& rContext )
222 {
223 OUString aTempURL;
224
225 uno::Reference < beans::XPropertySet > xTempFile(
226 io::TempFile::create(rContext),
227 uno::UNO_QUERY_THROW );
228
229 try {
230 xTempFile->setPropertyValue( "RemoveFile", uno::makeAny( false ) );
231 uno::Any aUrl = xTempFile->getPropertyValue( "Uri" );
232 aUrl >>= aTempURL;
233 }
234 catch ( const uno::Exception& )
235 {
236 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
237 }
238
239 if ( aTempURL.isEmpty() )
240 throw uno::RuntimeException("Cannot create tempfile.");
241
242 return aTempURL;
243 }
244
CreateMemoryStream(const uno::Reference<uno::XComponentContext> & rContext)245 uno::Reference< io::XStream > CreateMemoryStream( const uno::Reference< uno::XComponentContext >& rContext )
246 {
247 return uno::Reference< io::XStream >(
248 rContext->getServiceManager()->createInstanceWithContext("com.sun.star.comp.MemoryStream", rContext),
249 uno::UNO_QUERY_THROW);
250 }
251
lcl_findPairByName(const uno::Sequence<beans::StringPair> & rSeq,const OUString & rName)252 const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName)
253 {
254 return std::find_if(rSeq.begin(), rSeq.end(),
255 [&rName](const beans::StringPair& rPair) { return rPair.First == rName; });
256 }
257
258 } // anonymous namespace
259
OWriteStream_Impl(OStorage_Impl * pParent,const uno::Reference<packages::XDataSinkEncrSupport> & xPackageStream,const uno::Reference<lang::XSingleServiceFactory> & xPackage,const uno::Reference<uno::XComponentContext> & rContext,bool bForceEncrypted,sal_Int32 nStorageType,bool bDefaultCompress,const uno::Reference<io::XInputStream> & xRelInfoStream)260 OWriteStream_Impl::OWriteStream_Impl( OStorage_Impl* pParent,
261 const uno::Reference< packages::XDataSinkEncrSupport >& xPackageStream,
262 const uno::Reference< lang::XSingleServiceFactory >& xPackage,
263 const uno::Reference< uno::XComponentContext >& rContext,
264 bool bForceEncrypted,
265 sal_Int32 nStorageType,
266 bool bDefaultCompress,
267 const uno::Reference< io::XInputStream >& xRelInfoStream )
268 : m_xMutex( new comphelper::RefCountedMutex )
269 , m_pAntiImpl( nullptr )
270 , m_bHasDataToFlush( false )
271 , m_bFlushed( false )
272 , m_xPackageStream( xPackageStream )
273 , m_xContext( rContext )
274 , m_pParent( pParent )
275 , m_bForceEncrypted( bForceEncrypted )
276 , m_bUseCommonEncryption( !bForceEncrypted && nStorageType == embed::StorageFormats::PACKAGE )
277 , m_bHasCachedEncryptionData( false )
278 , m_bCompressedSetExplicit( !bDefaultCompress )
279 , m_xPackage( xPackage )
280 , m_bHasInsertedStreamOptimization( false )
281 , m_nStorageType( nStorageType )
282 , m_xOrigRelInfoStream( xRelInfoStream )
283 , m_bOrigRelInfoBroken( false )
284 , m_nRelInfoStatus( RELINFO_NO_INIT )
285 , m_nRelId( 1 )
286 {
287 SAL_WARN_IF( !xPackageStream.is(), "package.xstor", "No package stream is provided!" );
288 SAL_WARN_IF( !xPackage.is(), "package.xstor", "No package component is provided!" );
289 SAL_WARN_IF( !m_xContext.is(), "package.xstor", "No package stream is provided!" );
290 OSL_ENSURE( pParent, "No parent storage is provided!" );
291 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML || !m_xOrigRelInfoStream.is(), "The Relations info makes sense only for OFOPXML format!" );
292 }
293
~OWriteStream_Impl()294 OWriteStream_Impl::~OWriteStream_Impl()
295 {
296 DisposeWrappers();
297
298 if ( !m_aTempURL.isEmpty() )
299 {
300 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
301 m_aTempURL.clear();
302 }
303
304 CleanCacheStream();
305 }
306
CleanCacheStream()307 void OWriteStream_Impl::CleanCacheStream()
308 {
309 if ( !m_xCacheStream.is() )
310 return;
311
312 try
313 {
314 uno::Reference< io::XInputStream > xInputCache = m_xCacheStream->getInputStream();
315 if ( xInputCache.is() )
316 xInputCache->closeInput();
317 }
318 catch( const uno::Exception& )
319 {}
320
321 try
322 {
323 uno::Reference< io::XOutputStream > xOutputCache = m_xCacheStream->getOutputStream();
324 if ( xOutputCache.is() )
325 xOutputCache->closeOutput();
326 }
327 catch( const uno::Exception& )
328 {}
329
330 m_xCacheStream.clear();
331 m_xCacheSeek.clear();
332 }
333
InsertIntoPackageFolder(const OUString & aName,const uno::Reference<container::XNameContainer> & xParentPackageFolder)334 void OWriteStream_Impl::InsertIntoPackageFolder( const OUString& aName,
335 const uno::Reference< container::XNameContainer >& xParentPackageFolder )
336 {
337 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
338
339 SAL_WARN_IF( !m_bFlushed, "package.xstor", "This method must not be called for nonflushed streams!" );
340 if ( m_bFlushed )
341 {
342 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "An inserted stream is incomplete!" );
343 uno::Reference< lang::XUnoTunnel > xTunnel( m_xPackageStream, uno::UNO_QUERY_THROW );
344 xParentPackageFolder->insertByName( aName, uno::makeAny( xTunnel ) );
345
346 m_bFlushed = false;
347 m_bHasInsertedStreamOptimization = false;
348 }
349 }
IsEncrypted()350 bool OWriteStream_Impl::IsEncrypted()
351 {
352 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
353 return false;
354
355 if ( m_bForceEncrypted || m_bHasCachedEncryptionData )
356 return true;
357
358 if ( !m_aTempURL.isEmpty() || m_xCacheStream.is() )
359 return false;
360
361 GetStreamProperties();
362
363 // the following value can not be cached since it can change after root commit
364 bool bWasEncr = false;
365 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY );
366 if ( xPropSet.is() )
367 {
368 uno::Any aValue = xPropSet->getPropertyValue("WasEncrypted");
369 if ( !( aValue >>= bWasEncr ) )
370 {
371 SAL_WARN( "package.xstor", "The property WasEncrypted has wrong type!" );
372 }
373 }
374
375 bool bToBeEncr = false;
376 for ( const auto& rProp : std::as_const(m_aProps) )
377 {
378 if ( rProp.Name == "Encrypted" )
379 {
380 if ( !( rProp.Value >>= bToBeEncr ) )
381 {
382 SAL_WARN( "package.xstor", "The property has wrong type!" );
383 }
384 }
385 }
386
387 // since a new key set to the package stream it should not be removed except the case when
388 // the stream becomes nonencrypted
389 uno::Sequence< beans::NamedValue > aKey;
390 if ( bToBeEncr )
391 GetEncryptionKeyProperty_Impl( xPropSet ) >>= aKey;
392
393 // If the properties must be investigated the stream is either
394 // was never changed or was changed, the parent was committed
395 // and the stream was closed.
396 // That means that if it is intended to use common storage key
397 // it is already has no encryption but is marked to be stored
398 // encrypted and the key is empty.
399 if ( !bWasEncr && bToBeEncr && !aKey.hasElements() )
400 {
401 // the stream is intended to use common storage password
402 m_bUseCommonEncryption = true;
403 return false;
404 }
405 else
406 return bToBeEncr;
407 }
408
SetDecrypted()409 void OWriteStream_Impl::SetDecrypted()
410 {
411 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
412 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
413 throw uno::RuntimeException();
414
415 GetStreamProperties();
416
417 // let the stream be modified
418 FillTempGetFileName();
419 m_bHasDataToFlush = true;
420
421 // remove encryption
422 m_bForceEncrypted = false;
423 m_bHasCachedEncryptionData = false;
424 m_aEncryptionData.clear();
425
426 for ( auto& rProp : m_aProps )
427 {
428 if ( rProp.Name == "Encrypted" )
429 rProp.Value <<= false;
430 }
431 }
432
SetEncrypted(const::comphelper::SequenceAsHashMap & aEncryptionData)433 void OWriteStream_Impl::SetEncrypted( const ::comphelper::SequenceAsHashMap& aEncryptionData )
434 {
435 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
436 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
437 throw uno::RuntimeException();
438
439 if ( aEncryptionData.empty() )
440 throw uno::RuntimeException();
441
442 GetStreamProperties();
443
444 // let the stream be modified
445 FillTempGetFileName();
446 m_bHasDataToFlush = true;
447
448 // introduce encryption info
449 for ( auto& rProp : m_aProps )
450 {
451 if ( rProp.Name == "Encrypted" )
452 rProp.Value <<= true;
453 }
454
455 m_bUseCommonEncryption = false; // very important to set it to false
456
457 m_bHasCachedEncryptionData = true;
458 m_aEncryptionData = aEncryptionData;
459 }
460
DisposeWrappers()461 void OWriteStream_Impl::DisposeWrappers()
462 {
463 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
464 if ( m_pAntiImpl )
465 {
466 try {
467 m_pAntiImpl->dispose();
468 }
469 catch ( const uno::RuntimeException& )
470 {
471 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
472 }
473
474 m_pAntiImpl = nullptr;
475 }
476 m_pParent = nullptr;
477
478 if ( m_aInputStreamsVector.empty() )
479 return;
480
481 for ( auto& pStream : m_aInputStreamsVector )
482 {
483 if ( pStream )
484 {
485 pStream->InternalDispose();
486 pStream = nullptr;
487 }
488 }
489
490 m_aInputStreamsVector.clear();
491 }
492
GetFilledTempFileIfNo(const uno::Reference<io::XInputStream> & xStream)493 OUString const & OWriteStream_Impl::GetFilledTempFileIfNo( const uno::Reference< io::XInputStream >& xStream )
494 {
495 if ( !m_aTempURL.getLength() )
496 {
497 OUString aTempURL = GetNewTempFileURL( m_xContext );
498
499 try {
500 if ( !aTempURL.isEmpty() && xStream.is() )
501 {
502 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
503
504 uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( aTempURL );
505 if ( !xTempOutStream.is() )
506 throw io::IOException("no temp stream"); // TODO:
507 // the current position of the original stream should be still OK, copy further
508 ::comphelper::OStorageHelper::CopyInputToOutput( xStream, xTempOutStream );
509 xTempOutStream->closeOutput();
510 xTempOutStream.clear();
511 }
512 }
513 catch( const packages::WrongPasswordException& )
514 {
515 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
516 KillFile( aTempURL, comphelper::getProcessComponentContext() );
517 throw;
518 }
519 catch( const uno::Exception& )
520 {
521 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
522 KillFile( aTempURL, comphelper::getProcessComponentContext() );
523 throw;
524 }
525
526 if ( !aTempURL.isEmpty() )
527 CleanCacheStream();
528
529 m_aTempURL = aTempURL;
530 }
531
532 return m_aTempURL;
533 }
534
FillTempGetFileName()535 OUString const & OWriteStream_Impl::FillTempGetFileName()
536 {
537 // should try to create cache first, if the amount of contents is too big, the temp file should be taken
538 if ( !m_xCacheStream.is() && m_aTempURL.isEmpty() )
539 {
540 uno::Reference< io::XInputStream > xOrigStream = m_xPackageStream->getDataStream();
541 if ( !xOrigStream.is() )
542 {
543 // in case of new inserted package stream it is possible that input stream still was not set
544 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
545 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
546 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
547 m_xCacheStream = xCacheStream;
548 }
549 else
550 {
551 sal_Int32 nRead = 0;
552 uno::Sequence< sal_Int8 > aData( MAX_STORCACHE_SIZE + 1 );
553 nRead = xOrigStream->readBytes( aData, MAX_STORCACHE_SIZE + 1 );
554 if ( aData.getLength() > nRead )
555 aData.realloc( nRead );
556
557 if ( nRead <= MAX_STORCACHE_SIZE )
558 {
559 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
560 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
561
562 if ( nRead )
563 {
564 uno::Reference< io::XOutputStream > xOutStream( xCacheStream->getOutputStream(), uno::UNO_SET_THROW );
565 xOutStream->writeBytes( aData );
566 }
567 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
568 m_xCacheStream = xCacheStream;
569 m_xCacheSeek->seek( 0 );
570 }
571 else if ( m_aTempURL.isEmpty() )
572 {
573 m_aTempURL = GetNewTempFileURL( m_xContext );
574
575 try {
576 if ( !m_aTempURL.isEmpty() )
577 {
578 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
579
580 uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( m_aTempURL );
581 if ( !xTempOutStream.is() )
582 throw io::IOException("no temp stream"); // TODO:
583
584 // copy stream contents to the file
585 xTempOutStream->writeBytes( aData );
586
587 // the current position of the original stream should be still OK, copy further
588 ::comphelper::OStorageHelper::CopyInputToOutput( xOrigStream, xTempOutStream );
589 xTempOutStream->closeOutput();
590 xTempOutStream.clear();
591 }
592 }
593 catch( const packages::WrongPasswordException& )
594 {
595 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
596 m_aTempURL.clear();
597
598 throw;
599 }
600 catch( const uno::Exception& )
601 {
602 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
603 m_aTempURL.clear();
604 }
605 }
606 }
607 }
608
609 return m_aTempURL;
610 }
611
GetTempFileAsStream()612 uno::Reference< io::XStream > OWriteStream_Impl::GetTempFileAsStream()
613 {
614 uno::Reference< io::XStream > xTempStream;
615
616 if ( !m_xCacheStream.is() )
617 {
618 if ( m_aTempURL.isEmpty() )
619 m_aTempURL = FillTempGetFileName();
620
621 if ( !m_aTempURL.isEmpty() )
622 {
623 // the temporary file is not used if the cache is used
624 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
625
626 try
627 {
628 xTempStream = xTempAccess->openFileReadWrite( m_aTempURL );
629 }
630 catch( const uno::Exception& )
631 {
632 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
633 }
634 }
635 }
636
637 if ( m_xCacheStream.is() )
638 xTempStream = m_xCacheStream;
639
640 // the method must always return a stream
641 // in case the stream can not be open
642 // an exception should be thrown
643 if ( !xTempStream.is() )
644 throw io::IOException("no temp stream"); //TODO:
645
646 return xTempStream;
647 }
648
GetTempFileAsInputStream()649 uno::Reference< io::XInputStream > OWriteStream_Impl::GetTempFileAsInputStream()
650 {
651 uno::Reference< io::XInputStream > xInputStream;
652
653 if ( !m_xCacheStream.is() )
654 {
655 if ( m_aTempURL.isEmpty() )
656 m_aTempURL = FillTempGetFileName();
657
658 if ( !m_aTempURL.isEmpty() )
659 {
660 // the temporary file is not used if the cache is used
661 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
662
663 try
664 {
665 xInputStream = xTempAccess->openFileRead( m_aTempURL );
666 }
667 catch( const uno::Exception& )
668 {
669 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
670 }
671 }
672 }
673
674 if ( m_xCacheStream.is() )
675 xInputStream = m_xCacheStream->getInputStream();
676
677 // the method must always return a stream
678 // in case the stream can not be open
679 // an exception should be thrown
680 if ( !xInputStream.is() )
681 throw io::IOException(); // TODO:
682
683 return xInputStream;
684 }
685
InsertStreamDirectly(const uno::Reference<io::XInputStream> & xInStream,const uno::Sequence<beans::PropertyValue> & aProps)686 void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputStream >& xInStream,
687 const uno::Sequence< beans::PropertyValue >& aProps )
688 {
689 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
690
691 // this call can be made only during parent storage commit
692 // the parent storage is responsible for the correct handling
693 // of deleted and renamed contents
694
695 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
696
697 if ( m_bHasDataToFlush )
698 throw io::IOException("m_bHasDataToFlush==true");
699
700 OSL_ENSURE( m_aTempURL.isEmpty() && !m_xCacheStream.is(), "The temporary must not exist!" );
701
702 // use new file as current persistent representation
703 // the new file will be removed after it's stream is closed
704 m_xPackageStream->setDataStream( xInStream );
705
706 // copy properties to the package stream
707 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
708
709 // The storage-package communication has a problem
710 // the storage caches properties, thus if the package changes one of them itself
711 // the storage does not know about it
712
713 // Depending from MediaType value the package can change the compressed property itself
714 // Thus if Compressed property is provided it must be set as the latest one
715 bool bCompressedIsSet = false;
716 bool bCompressed = false;
717 OUString aComprPropName( "Compressed" );
718 OUString aMedTypePropName( "MediaType" );
719 for ( const auto& rProp : aProps )
720 {
721 if ( rProp.Name == aComprPropName )
722 {
723 bCompressedIsSet = true;
724 rProp.Value >>= bCompressed;
725 }
726 else if ( ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
727 && rProp.Name == aMedTypePropName )
728 {
729 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
730 }
731 else if ( m_nStorageType == embed::StorageFormats::PACKAGE && rProp.Name == "UseCommonStoragePasswordEncryption" )
732 rProp.Value >>= m_bUseCommonEncryption;
733 else
734 throw lang::IllegalArgumentException();
735
736 // if there are cached properties update them
737 if ( rProp.Name == aMedTypePropName || rProp.Name == aComprPropName )
738 for ( auto& rMemProp : m_aProps )
739 {
740 if ( rProp.Name == rMemProp.Name )
741 rMemProp.Value = rProp.Value;
742 }
743 }
744
745 if ( bCompressedIsSet )
746 {
747 xPropertySet->setPropertyValue( aComprPropName, uno::makeAny( bCompressed ) );
748 m_bCompressedSetExplicit = true;
749 }
750
751 if ( m_bUseCommonEncryption )
752 {
753 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
754 throw uno::RuntimeException();
755
756 // set to be encrypted but do not use encryption key
757 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
758 uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
759 xPropertySet->setPropertyValue( "Encrypted", uno::makeAny( true ) );
760 }
761
762 // the stream should be free soon, after package is stored
763 m_bHasDataToFlush = false;
764 m_bFlushed = true; // will allow to use transaction on stream level if will need it
765 m_bHasInsertedStreamOptimization = true;
766 }
767
Commit()768 void OWriteStream_Impl::Commit()
769 {
770 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
771
772 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
773
774 if ( !m_bHasDataToFlush )
775 return;
776
777 uno::Reference< packages::XDataSinkEncrSupport > xNewPackageStream;
778 uno::Sequence< uno::Any > aSeq( 1 );
779 aSeq[0] <<= false;
780
781 if ( m_xCacheStream.is() )
782 {
783 if ( m_pAntiImpl )
784 m_pAntiImpl->DeInit();
785
786 uno::Reference< io::XInputStream > xInStream( m_xCacheStream->getInputStream(), uno::UNO_SET_THROW );
787
788 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
789
790 xNewPackageStream->setDataStream( xInStream );
791
792 m_xCacheStream.clear();
793 m_xCacheSeek.clear();
794
795 }
796 else if ( !m_aTempURL.isEmpty() )
797 {
798 if ( m_pAntiImpl )
799 m_pAntiImpl->DeInit();
800
801 uno::Reference< io::XInputStream > xInStream;
802 try
803 {
804 xInStream = new OSelfTerminateFileStream(m_xContext, m_aTempURL);
805 }
806 catch( const uno::Exception& )
807 {
808 }
809
810 if ( !xInStream.is() )
811 throw io::IOException();
812
813 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
814
815 // TODO/NEW: Let the temporary file be removed after commit
816 xNewPackageStream->setDataStream( xInStream );
817 m_aTempURL.clear();
818 }
819 else // if ( m_bHasInsertedStreamOptimization )
820 {
821 // if the optimization is used the stream can be accessed directly
822 xNewPackageStream = m_xPackageStream;
823 }
824
825 // copy properties to the package stream
826 uno::Reference< beans::XPropertySet > xPropertySet( xNewPackageStream, uno::UNO_QUERY_THROW );
827
828 for ( auto& rProp : m_aProps )
829 {
830 if ( rProp.Name == "Size" )
831 {
832 if ( m_pAntiImpl && !m_bHasInsertedStreamOptimization && m_pAntiImpl->m_xSeekable.is() )
833 {
834 rProp.Value <<= m_pAntiImpl->m_xSeekable->getLength();
835 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
836 }
837 }
838 else
839 xPropertySet->setPropertyValue( rProp.Name, rProp.Value );
840 }
841
842 if ( m_bUseCommonEncryption )
843 {
844 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
845 throw uno::RuntimeException();
846
847 // set to be encrypted but do not use encryption key
848 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
849 uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
850 xPropertySet->setPropertyValue( "Encrypted",
851 uno::makeAny( true ) );
852 }
853 else if ( m_bHasCachedEncryptionData )
854 {
855 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
856 throw uno::RuntimeException();
857
858 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
859 uno::makeAny( m_aEncryptionData.getAsConstNamedValueList() ) );
860 }
861
862 // the stream should be free soon, after package is stored
863 m_xPackageStream = xNewPackageStream;
864 m_bHasDataToFlush = false;
865 m_bFlushed = true; // will allow to use transaction on stream level if will need it
866 }
867
Revert()868 void OWriteStream_Impl::Revert()
869 {
870 // can be called only from parent storage
871 // means complete reload of the stream
872
873 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
874
875 if ( !m_bHasDataToFlush )
876 return; // nothing to do
877
878 OSL_ENSURE( !m_aTempURL.isEmpty() || m_xCacheStream.is(), "The temporary must exist!" );
879
880 if ( m_xCacheStream.is() )
881 {
882 m_xCacheStream.clear();
883 m_xCacheSeek.clear();
884 }
885
886 if ( !m_aTempURL.isEmpty() )
887 {
888 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
889 m_aTempURL.clear();
890 }
891
892 m_aProps.realloc( 0 );
893
894 m_bHasDataToFlush = false;
895
896 m_bUseCommonEncryption = true;
897 m_bHasCachedEncryptionData = false;
898 m_aEncryptionData.clear();
899
900 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
901 return;
902
903 // currently the relations storage is changed only on commit
904 m_xNewRelInfoStream.clear();
905 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
906 if ( m_xOrigRelInfoStream.is() )
907 {
908 // the original stream is still here, that means that it was not parsed
909 m_aOrigRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
910 m_nRelInfoStatus = RELINFO_NO_INIT;
911 }
912 else
913 {
914 // the original stream was already parsed
915 if ( !m_bOrigRelInfoBroken )
916 m_nRelInfoStatus = RELINFO_READ;
917 else
918 m_nRelInfoStatus = RELINFO_BROKEN;
919 }
920 }
921
GetStreamProperties()922 uno::Sequence< beans::PropertyValue > const & OWriteStream_Impl::GetStreamProperties()
923 {
924 if ( !m_aProps.hasElements() )
925 m_aProps = ReadPackageStreamProperties();
926
927 return m_aProps;
928 }
929
InsertOwnProps(const uno::Sequence<beans::PropertyValue> & aProps,bool bUseCommonEncryption)930 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::InsertOwnProps(
931 const uno::Sequence< beans::PropertyValue >& aProps,
932 bool bUseCommonEncryption )
933 {
934 uno::Sequence< beans::PropertyValue > aResult( aProps );
935 beans::PropertyValue aPropVal;
936
937 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
938 {
939 aPropVal.Name = "UseCommonStoragePasswordEncryption";
940 aPropVal.Value <<= bUseCommonEncryption;
941 }
942 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
943 {
944 ReadRelInfoIfNecessary();
945
946 aPropVal.Name = "RelationsInfo";
947 if ( m_nRelInfoStatus == RELINFO_READ )
948 aPropVal.Value <<= m_aOrigRelInfo;
949 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
950 aPropVal.Value <<= m_aNewRelInfo;
951 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
952 throw io::IOException( "Wrong relinfo stream!" );
953 }
954 if (!aPropVal.Name.isEmpty())
955 {
956 sal_Int32 i = 0;
957 for (auto p = aResult.getConstArray(); i < aResult.getLength(); ++i)
958 if (p[i].Name == aPropVal.Name)
959 break;
960 if (i == aResult.getLength())
961 aResult.realloc(i + 1);
962 aResult[i] = aPropVal;
963 }
964
965 return aResult;
966 }
967
IsTransacted()968 bool OWriteStream_Impl::IsTransacted()
969 {
970 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
971 return ( m_pAntiImpl && m_pAntiImpl->m_bTransacted );
972 }
973
ReadRelInfoIfNecessary()974 void OWriteStream_Impl::ReadRelInfoIfNecessary()
975 {
976 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
977 return;
978
979 if ( m_nRelInfoStatus == RELINFO_NO_INIT )
980 {
981 try
982 {
983 // Init from original stream
984 if ( m_xOrigRelInfoStream.is() )
985 m_aOrigRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
986 m_xOrigRelInfoStream,
987 u"_rels/*.rels",
988 m_xContext );
989
990 // in case of success the stream must be thrown away, that means that the OrigRelInfo is initialized
991 // the reason for this is that the original stream might not be seekable ( at the same time the new
992 // provided stream must be seekable ), so it must be read only once
993 m_xOrigRelInfoStream.clear();
994 m_nRelInfoStatus = RELINFO_READ;
995 }
996 catch( const uno::Exception& )
997 {
998 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
999
1000 m_nRelInfoStatus = RELINFO_BROKEN;
1001 m_bOrigRelInfoBroken = true;
1002 }
1003 }
1004 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1005 {
1006 // Init from the new stream
1007 try
1008 {
1009 if ( m_xNewRelInfoStream.is() )
1010 m_aNewRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
1011 m_xNewRelInfoStream,
1012 u"_rels/*.rels",
1013 m_xContext );
1014
1015 m_nRelInfoStatus = RELINFO_CHANGED_STREAM_READ;
1016 }
1017 catch( const uno::Exception& )
1018 {
1019 m_nRelInfoStatus = RELINFO_CHANGED_BROKEN;
1020 }
1021 }
1022 }
1023
ReadPackageStreamProperties()1024 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::ReadPackageStreamProperties()
1025 {
1026 sal_Int32 nPropNum = 0;
1027 if ( m_nStorageType == embed::StorageFormats::ZIP )
1028 nPropNum = 2;
1029 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1030 nPropNum = 3;
1031 else if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1032 nPropNum = 4;
1033 uno::Sequence< beans::PropertyValue > aResult( nPropNum );
1034
1035 // The "Compressed" property must be set after "MediaType" property,
1036 // since the setting of the last one can change the value of the first one
1037
1038 if ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
1039 {
1040 aResult[0].Name = "MediaType";
1041 aResult[1].Name = "Compressed";
1042 aResult[2].Name = "Size";
1043
1044 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1045 aResult[3].Name = "Encrypted";
1046 }
1047 else
1048 {
1049 aResult[0].Name = "Compressed";
1050 aResult[1].Name = "Size";
1051 }
1052
1053 // TODO: may be also raw stream should be marked
1054
1055 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY_THROW );
1056 for ( auto& rProp : aResult )
1057 {
1058 try {
1059 rProp.Value = xPropSet->getPropertyValue( rProp.Name );
1060 }
1061 catch( const uno::Exception& )
1062 {
1063 TOOLS_WARN_EXCEPTION( "package.xstor", "A property can't be retrieved" );
1064 }
1065 }
1066
1067 return aResult;
1068 }
1069
CopyInternallyTo_Impl(const uno::Reference<io::XStream> & xDestStream,const::comphelper::SequenceAsHashMap & aEncryptionData)1070 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream,
1071 const ::comphelper::SequenceAsHashMap& aEncryptionData )
1072 {
1073 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1074
1075 SAL_WARN_IF( m_bUseCommonEncryption, "package.xstor", "The stream can not be encrypted!" );
1076
1077 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
1078 throw packages::NoEncryptionException();
1079
1080 if ( m_pAntiImpl )
1081 {
1082 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1083 }
1084 else
1085 {
1086 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, aEncryptionData, false );
1087 if ( !xOwnStream.is() )
1088 throw io::IOException(); // TODO
1089
1090 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1091 }
1092
1093 uno::Reference< embed::XEncryptionProtectedSource2 > xEncr( xDestStream, uno::UNO_QUERY );
1094 if ( xEncr.is() )
1095 xEncr->setEncryptionData( aEncryptionData.getAsConstNamedValueList() );
1096 }
1097
GetAllRelationshipsIfAny()1098 uno::Sequence< uno::Sequence< beans::StringPair > > OWriteStream_Impl::GetAllRelationshipsIfAny()
1099 {
1100 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1101 return uno::Sequence< uno::Sequence< beans::StringPair > >();
1102
1103 ReadRelInfoIfNecessary();
1104
1105 if ( m_nRelInfoStatus == RELINFO_READ )
1106 return m_aOrigRelInfo;
1107 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
1108 return m_aNewRelInfo;
1109 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
1110 throw io::IOException( "Wrong relinfo stream!" );
1111 }
1112
CopyInternallyTo_Impl(const uno::Reference<io::XStream> & xDestStream)1113 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream )
1114 {
1115 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1116
1117 if ( m_pAntiImpl )
1118 {
1119 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1120 }
1121 else
1122 {
1123 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, false );
1124 if ( !xOwnStream.is() )
1125 throw io::IOException(); // TODO
1126
1127 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1128 }
1129 }
1130
GetStream(sal_Int32 nStreamMode,const::comphelper::SequenceAsHashMap & aEncryptionData,bool bHierarchyAccess)1131 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData, bool bHierarchyAccess )
1132 {
1133 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1134
1135 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1136
1137 if ( m_pAntiImpl )
1138 throw io::IOException(); // TODO:
1139
1140 if ( !IsEncrypted() )
1141 throw packages::NoEncryptionException();
1142
1143 uno::Reference< io::XStream > xResultStream;
1144
1145 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
1146
1147 if ( m_bHasCachedEncryptionData )
1148 {
1149 if ( !::package::PackageEncryptionDataLessOrEqual( m_aEncryptionData, aEncryptionData ) )
1150 throw packages::WrongPasswordException();
1151
1152 // the correct key must be set already
1153 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1154 }
1155 else
1156 {
1157 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1158
1159 try {
1160 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1161
1162 m_bUseCommonEncryption = false; // very important to set it to false
1163 m_bHasCachedEncryptionData = true;
1164 m_aEncryptionData = aEncryptionData;
1165 }
1166 catch( const packages::WrongPasswordException& )
1167 {
1168 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
1169 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1170 throw;
1171 }
1172 catch ( const uno::Exception& ex )
1173 {
1174 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't write encryption related properties");
1175 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1176 throw io::IOException(ex.Message); // TODO:
1177 }
1178 }
1179
1180 SAL_WARN_IF( !xResultStream.is(), "package.xstor", "In case stream can not be retrieved an exception must be thrown!" );
1181
1182 return xResultStream;
1183 }
1184
GetStream(sal_Int32 nStreamMode,bool bHierarchyAccess)1185 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, bool bHierarchyAccess )
1186 {
1187 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1188
1189 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1190
1191 if ( m_pAntiImpl )
1192 throw io::IOException(); // TODO:
1193
1194 uno::Reference< io::XStream > xResultStream;
1195
1196 if ( IsEncrypted() )
1197 {
1198 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1199 try
1200 {
1201 aGlobalEncryptionData = GetCommonRootEncryptionData();
1202 }
1203 catch( const packages::NoEncryptionException& )
1204 {
1205 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
1206 throw packages::WrongPasswordException();
1207 }
1208
1209 xResultStream = GetStream( nStreamMode, aGlobalEncryptionData, bHierarchyAccess );
1210 }
1211 else
1212 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1213
1214 return xResultStream;
1215 }
1216
GetStream_Impl(sal_Int32 nStreamMode,bool bHierarchyAccess)1217 uno::Reference< io::XStream > OWriteStream_Impl::GetStream_Impl( sal_Int32 nStreamMode, bool bHierarchyAccess )
1218 {
1219 // private method, no mutex is used
1220 GetStreamProperties();
1221
1222 // TODO/LATER: this info might be read later, on demand in future
1223 ReadRelInfoIfNecessary();
1224
1225 if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::READ )
1226 {
1227 uno::Reference< io::XInputStream > xInStream;
1228 if ( m_xCacheStream.is() || !m_aTempURL.isEmpty() )
1229 xInStream = GetTempFileAsInputStream(); //TODO:
1230 else
1231 xInStream = m_xPackageStream->getDataStream();
1232
1233 // The stream does not exist in the storage
1234 if ( !xInStream.is() )
1235 throw io::IOException();
1236
1237 rtl::Reference<OInputCompStream> pStream = new OInputCompStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1238 m_aInputStreamsVector.push_back( pStream.get() );
1239 return pStream;
1240 }
1241 else if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::SEEKABLEREAD )
1242 {
1243 if ( !m_xCacheStream.is() && m_aTempURL.isEmpty() && !( m_xPackageStream->getDataStream().is() ) )
1244 {
1245 // The stream does not exist in the storage
1246 throw io::IOException();
1247 }
1248
1249 uno::Reference< io::XInputStream > xInStream = GetTempFileAsInputStream(); //TODO:
1250
1251 if ( !xInStream.is() )
1252 throw io::IOException();
1253
1254 rtl::Reference<OInputSeekStream> pStream = new OInputSeekStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1255 m_aInputStreamsVector.push_back( pStream.get() );
1256 return pStream;
1257 }
1258 else if ( ( nStreamMode & embed::ElementModes::WRITE ) == embed::ElementModes::WRITE )
1259 {
1260 if ( !m_aInputStreamsVector.empty() )
1261 throw io::IOException(); // TODO:
1262
1263 uno::Reference< io::XStream > xStream;
1264 if ( ( nStreamMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE )
1265 {
1266 if ( !m_aTempURL.isEmpty() )
1267 {
1268 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
1269 m_aTempURL.clear();
1270 }
1271 if ( m_xCacheStream.is() )
1272 CleanCacheStream();
1273
1274 m_bHasDataToFlush = true;
1275
1276 // this call is triggered by the parent and it will recognize the change of the state
1277 if ( m_pParent )
1278 m_pParent->m_bIsModified = true;
1279
1280 xStream = CreateMemoryStream( m_xContext );
1281 m_xCacheSeek.set( xStream, uno::UNO_QUERY_THROW );
1282 m_xCacheStream = xStream;
1283 }
1284 else if ( !m_bHasInsertedStreamOptimization )
1285 {
1286 if ( m_aTempURL.isEmpty() && !m_xCacheStream.is() && !( m_xPackageStream->getDataStream().is() ) )
1287 {
1288 // The stream does not exist in the storage
1289 m_bHasDataToFlush = true;
1290
1291 // this call is triggered by the parent and it will recognize the change of the state
1292 if ( m_pParent )
1293 m_pParent->m_bIsModified = true;
1294 xStream = GetTempFileAsStream();
1295 }
1296
1297 // if the stream exists the temporary file is created on demand
1298 // xStream = GetTempFileAsStream();
1299 }
1300
1301 rtl::Reference<OWriteStream> tmp;
1302 if ( !xStream.is() )
1303 tmp = new OWriteStream( this, bHierarchyAccess );
1304 else
1305 tmp = new OWriteStream( this, xStream, bHierarchyAccess );
1306
1307 m_pAntiImpl = tmp.get();
1308 return tmp;
1309 }
1310
1311 throw lang::IllegalArgumentException(); // TODO
1312 }
1313
GetPlainRawInStream()1314 uno::Reference< io::XInputStream > OWriteStream_Impl::GetPlainRawInStream()
1315 {
1316 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1317
1318 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1319
1320 // this method is used only internally, this stream object should not go outside of this implementation
1321 // if ( m_pAntiImpl )
1322 // throw io::IOException(); // TODO:
1323
1324 return m_xPackageStream->getPlainRawStream();
1325 }
1326
GetRawInStream()1327 uno::Reference< io::XInputStream > OWriteStream_Impl::GetRawInStream()
1328 {
1329 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1330
1331 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1332
1333 if ( m_pAntiImpl )
1334 throw io::IOException(); // TODO:
1335
1336 SAL_WARN_IF( !IsEncrypted(), "package.xstor", "Impossible to get raw representation for nonencrypted stream!" );
1337 if ( !IsEncrypted() )
1338 throw packages::NoEncryptionException();
1339
1340 return m_xPackageStream->getRawStream();
1341 }
1342
GetCommonRootEncryptionData()1343 ::comphelper::SequenceAsHashMap OWriteStream_Impl::GetCommonRootEncryptionData()
1344 {
1345 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1346
1347 if ( m_nStorageType != embed::StorageFormats::PACKAGE || !m_pParent )
1348 throw packages::NoEncryptionException();
1349
1350 return m_pParent->GetCommonRootEncryptionData();
1351 }
1352
InputStreamDisposed(OInputCompStream * pStream)1353 void OWriteStream_Impl::InputStreamDisposed( OInputCompStream* pStream )
1354 {
1355 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1356 m_aInputStreamsVector.erase(std::remove(m_aInputStreamsVector.begin(), m_aInputStreamsVector.end(), pStream ));
1357 }
1358
CreateReadonlyCopyBasedOnData(const uno::Reference<io::XInputStream> & xDataToCopy,const uno::Sequence<beans::PropertyValue> & aProps,uno::Reference<io::XStream> & xTargetStream)1359 void OWriteStream_Impl::CreateReadonlyCopyBasedOnData( const uno::Reference< io::XInputStream >& xDataToCopy, const uno::Sequence< beans::PropertyValue >& aProps, uno::Reference< io::XStream >& xTargetStream )
1360 {
1361 uno::Reference < io::XStream > xTempFile;
1362 if ( !xTargetStream.is() )
1363 xTempFile = io::TempFile::create(m_xContext);
1364 else
1365 xTempFile = xTargetStream;
1366
1367 uno::Reference < io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY_THROW );
1368
1369 uno::Reference < io::XOutputStream > xTempOut = xTempFile->getOutputStream();
1370 if ( !xTempOut.is() )
1371 throw uno::RuntimeException();
1372
1373 if ( xDataToCopy.is() )
1374 ::comphelper::OStorageHelper::CopyInputToOutput( xDataToCopy, xTempOut );
1375
1376 xTempOut->closeOutput();
1377 xTempSeek->seek( 0 );
1378
1379 uno::Reference< io::XInputStream > xInStream = xTempFile->getInputStream();
1380 if ( !xInStream.is() )
1381 throw io::IOException();
1382
1383 // TODO: remember last state of m_bUseCommonEncryption
1384 if ( !xTargetStream.is() )
1385 xTargetStream.set(
1386 static_cast< ::cppu::OWeakObject* >(
1387 new OInputSeekStream( xInStream, InsertOwnProps( aProps, m_bUseCommonEncryption ), m_nStorageType ) ),
1388 uno::UNO_QUERY_THROW );
1389 }
1390
GetCopyOfLastCommit(uno::Reference<io::XStream> & xTargetStream)1391 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream )
1392 {
1393 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1394
1395 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1396 if ( !m_xPackageStream.is() )
1397 throw uno::RuntimeException();
1398
1399 uno::Reference< io::XInputStream > xDataToCopy;
1400 if ( IsEncrypted() )
1401 {
1402 // an encrypted stream must contain input stream
1403 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1404 try
1405 {
1406 aGlobalEncryptionData = GetCommonRootEncryptionData();
1407 }
1408 catch( const packages::NoEncryptionException& )
1409 {
1410 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
1411 throw packages::WrongPasswordException();
1412 }
1413
1414 GetCopyOfLastCommit( xTargetStream, aGlobalEncryptionData );
1415 }
1416 else
1417 {
1418 xDataToCopy = m_xPackageStream->getDataStream();
1419
1420 // in case of new inserted package stream it is possible that input stream still was not set
1421 GetStreamProperties();
1422
1423 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1424 }
1425 }
1426
GetCopyOfLastCommit(uno::Reference<io::XStream> & xTargetStream,const::comphelper::SequenceAsHashMap & aEncryptionData)1427 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream, const ::comphelper::SequenceAsHashMap& aEncryptionData )
1428 {
1429 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1430
1431 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1432 if ( !m_xPackageStream.is() )
1433 throw uno::RuntimeException();
1434
1435 if ( !IsEncrypted() )
1436 throw packages::NoEncryptionException();
1437
1438 uno::Reference< io::XInputStream > xDataToCopy;
1439
1440 if ( m_bHasCachedEncryptionData )
1441 {
1442 // TODO: introduce last committed cashed password information and use it here
1443 // that means "use common pass" also should be remembered on flash
1444 uno::Sequence< beans::NamedValue > aKey = aEncryptionData.getAsConstNamedValueList();
1445
1446 uno::Reference< beans::XPropertySet > xProps( m_xPackageStream, uno::UNO_QUERY_THROW );
1447
1448 bool bEncr = false;
1449 xProps->getPropertyValue( "Encrypted" ) >>= bEncr;
1450 if ( !bEncr )
1451 throw packages::NoEncryptionException();
1452
1453 uno::Sequence< beans::NamedValue > aPackKey;
1454 xProps->getPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY ) >>= aPackKey;
1455 if ( !SequencesEqual( aKey, aPackKey ) )
1456 throw packages::WrongPasswordException();
1457
1458 // the correct key must be set already
1459 xDataToCopy = m_xPackageStream->getDataStream();
1460 }
1461 else
1462 {
1463 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
1464 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1465
1466 try {
1467 xDataToCopy = m_xPackageStream->getDataStream();
1468
1469 if ( !xDataToCopy.is() )
1470 {
1471 SAL_WARN( "package.xstor", "Encrypted ZipStream must already have input stream inside!" );
1472 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1473 }
1474 }
1475 catch( const uno::Exception& )
1476 {
1477 TOOLS_WARN_EXCEPTION( "package.xstor", "Can't open encrypted stream");
1478 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1479 throw;
1480 }
1481
1482 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1483 }
1484
1485 // in case of new inserted package stream it is possible that input stream still was not set
1486 GetStreamProperties();
1487
1488 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1489 }
1490
CommitStreamRelInfo(const uno::Reference<embed::XStorage> & xRelStorage,std::u16string_view aOrigStreamName,std::u16string_view aNewStreamName)1491 void OWriteStream_Impl::CommitStreamRelInfo( const uno::Reference< embed::XStorage >& xRelStorage, std::u16string_view aOrigStreamName, std::u16string_view aNewStreamName )
1492 {
1493 // at this point of time the old stream must be already cleaned
1494 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML, "The method should be used only with OFOPXML format!" );
1495
1496 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1497 return;
1498
1499 OSL_ENSURE( !aOrigStreamName.empty() && !aNewStreamName.empty() && xRelStorage.is(),
1500 "Wrong relation persistence information is provided!" );
1501
1502 if ( !xRelStorage.is() || aOrigStreamName.empty() || aNewStreamName.empty() )
1503 throw uno::RuntimeException();
1504
1505 if ( m_nRelInfoStatus == RELINFO_BROKEN || m_nRelInfoStatus == RELINFO_CHANGED_BROKEN )
1506 throw io::IOException(); // TODO:
1507
1508 OUString aOrigRelStreamName = OUString::Concat(aOrigStreamName) + ".rels";
1509 OUString aNewRelStreamName = OUString::Concat(aNewStreamName) + ".rels";
1510
1511 bool bRenamed = aOrigRelStreamName != aNewRelStreamName;
1512 if ( m_nRelInfoStatus == RELINFO_CHANGED
1513 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1514 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1515 {
1516 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1517 xRelStorage->removeElement( aOrigRelStreamName );
1518
1519 if ( m_nRelInfoStatus == RELINFO_CHANGED )
1520 {
1521 if ( m_aNewRelInfo.hasElements() )
1522 {
1523 uno::Reference< io::XStream > xRelsStream =
1524 xRelStorage->openStreamElement( aNewRelStreamName,
1525 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1526
1527 uno::Reference< io::XOutputStream > xOutStream = xRelsStream->getOutputStream();
1528 if ( !xOutStream.is() )
1529 throw uno::RuntimeException();
1530
1531 ::comphelper::OFOPXMLHelper::WriteRelationsInfoSequence( xOutStream, m_aNewRelInfo, m_xContext );
1532
1533 // set the mediatype
1534 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1535 xPropSet->setPropertyValue(
1536 "MediaType",
1537 uno::makeAny( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1538
1539 m_nRelInfoStatus = RELINFO_READ;
1540 }
1541 }
1542 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1543 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1544 {
1545 uno::Reference< io::XStream > xRelsStream =
1546 xRelStorage->openStreamElement( aNewRelStreamName,
1547 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1548
1549 uno::Reference< io::XOutputStream > xOutputStream = xRelsStream->getOutputStream();
1550 if ( !xOutputStream.is() )
1551 throw uno::RuntimeException();
1552
1553 uno::Reference< io::XSeekable > xSeek( m_xNewRelInfoStream, uno::UNO_QUERY_THROW );
1554 xSeek->seek( 0 );
1555 ::comphelper::OStorageHelper::CopyInputToOutput( m_xNewRelInfoStream, xOutputStream );
1556 xSeek->seek( 0 );
1557
1558 // set the mediatype
1559 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1560 xPropSet->setPropertyValue("MediaType",
1561 uno::makeAny( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1562
1563 if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1564 m_nRelInfoStatus = RELINFO_NO_INIT;
1565 else
1566 {
1567 // the information is already parsed and the stream is stored, no need in temporary stream any more
1568 m_xNewRelInfoStream.clear();
1569 m_nRelInfoStatus = RELINFO_READ;
1570 }
1571 }
1572
1573 // the original stream makes no sense after this step
1574 m_xOrigRelInfoStream = m_xNewRelInfoStream;
1575 m_aOrigRelInfo = m_aNewRelInfo;
1576 m_bOrigRelInfoBroken = false;
1577 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1578 m_xNewRelInfoStream.clear();
1579 }
1580 else
1581 {
1582 // the stream is not changed but it might be renamed
1583 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1584 xRelStorage->renameElement( aOrigRelStreamName, aNewRelStreamName );
1585 }
1586 }
1587
1588 // OWriteStream implementation
1589
OWriteStream(OWriteStream_Impl * pImpl,bool bTransacted)1590 OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, bool bTransacted )
1591 : m_pImpl( pImpl )
1592 , m_bInStreamDisconnected( false )
1593 , m_bInitOnDemand( true )
1594 , m_nInitPosition( 0 )
1595 , m_bTransacted( bTransacted )
1596 {
1597 OSL_ENSURE( pImpl, "No base implementation!" );
1598 OSL_ENSURE( m_pImpl->m_xMutex.is(), "No mutex!" );
1599
1600 if ( !m_pImpl || !m_pImpl->m_xMutex.is() )
1601 throw uno::RuntimeException(); // just a disaster
1602
1603 m_pData.reset(new WSInternalData_Impl(pImpl->m_xMutex, m_pImpl->m_nStorageType));
1604 }
1605
OWriteStream(OWriteStream_Impl * pImpl,uno::Reference<io::XStream> const & xStream,bool bTransacted)1606 OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, uno::Reference< io::XStream > const & xStream, bool bTransacted )
1607 : m_pImpl( pImpl )
1608 , m_bInStreamDisconnected( false )
1609 , m_bInitOnDemand( false )
1610 , m_nInitPosition( 0 )
1611 , m_bTransacted( bTransacted )
1612 {
1613 OSL_ENSURE( pImpl && xStream.is(), "No base implementation!" );
1614 OSL_ENSURE( m_pImpl->m_xMutex.is(), "No mutex!" );
1615
1616 if ( !m_pImpl || !m_pImpl->m_xMutex.is() )
1617 throw uno::RuntimeException(); // just a disaster
1618
1619 m_pData.reset(new WSInternalData_Impl(pImpl->m_xMutex, m_pImpl->m_nStorageType));
1620
1621 if ( xStream.is() )
1622 {
1623 m_xInStream = xStream->getInputStream();
1624 m_xOutStream = xStream->getOutputStream();
1625 m_xSeekable.set( xStream, uno::UNO_QUERY );
1626 OSL_ENSURE( m_xInStream.is() && m_xOutStream.is() && m_xSeekable.is(), "Stream implementation is incomplete!" );
1627 }
1628 }
1629
~OWriteStream()1630 OWriteStream::~OWriteStream()
1631 {
1632 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1633 if ( m_pImpl )
1634 {
1635 osl_atomic_increment(&m_refCount);
1636 try {
1637 dispose();
1638 }
1639 catch( const uno::RuntimeException& )
1640 {
1641 TOOLS_INFO_EXCEPTION("package.xstor", "Quiet exception");
1642 }
1643 }
1644 }
1645
DeInit()1646 void OWriteStream::DeInit()
1647 {
1648 if ( !m_pImpl )
1649 return; // do nothing
1650
1651 if ( m_xSeekable.is() )
1652 m_nInitPosition = m_xSeekable->getPosition();
1653
1654 m_xInStream.clear();
1655 m_xOutStream.clear();
1656 m_xSeekable.clear();
1657 m_bInitOnDemand = true;
1658 }
1659
CheckInitOnDemand()1660 void OWriteStream::CheckInitOnDemand()
1661 {
1662 if ( !m_pImpl )
1663 {
1664 SAL_INFO("package.xstor", "Disposed!");
1665 throw lang::DisposedException();
1666 }
1667
1668 if ( !m_bInitOnDemand )
1669 return;
1670
1671 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
1672 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
1673 if ( xStream.is() )
1674 {
1675 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
1676 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
1677 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
1678 m_xSeekable->seek( m_nInitPosition );
1679
1680 m_nInitPosition = 0;
1681 m_bInitOnDemand = false;
1682 }
1683 }
1684
CopyToStreamInternally_Impl(const uno::Reference<io::XStream> & xDest)1685 void OWriteStream::CopyToStreamInternally_Impl( const uno::Reference< io::XStream >& xDest )
1686 {
1687 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1688
1689 CheckInitOnDemand();
1690
1691 if ( !m_xInStream.is() )
1692 throw uno::RuntimeException();
1693
1694 if ( !m_xSeekable.is() )
1695 throw uno::RuntimeException();
1696
1697 uno::Reference< beans::XPropertySet > xDestProps( xDest, uno::UNO_QUERY_THROW );
1698
1699 uno::Reference< io::XOutputStream > xDestOutStream = xDest->getOutputStream();
1700 if ( !xDestOutStream.is() )
1701 throw io::IOException(); // TODO
1702
1703 sal_Int64 nCurPos = m_xSeekable->getPosition();
1704 m_xSeekable->seek( 0 );
1705
1706 uno::Exception eThrown;
1707 bool bThrown = false;
1708 try {
1709 ::comphelper::OStorageHelper::CopyInputToOutput( m_xInStream, xDestOutStream );
1710 }
1711 catch ( const uno::Exception& e )
1712 {
1713 eThrown = e;
1714 bThrown = true;
1715 }
1716
1717 // position-related section below is critical
1718 // if it fails the stream will become invalid
1719 try {
1720 m_xSeekable->seek( nCurPos );
1721 }
1722 catch ( const uno::Exception& )
1723 {
1724 // TODO: set the stream in invalid state or dispose
1725 TOOLS_WARN_EXCEPTION( "package.xstor", "The stream become invalid during copying" );
1726 throw uno::RuntimeException();
1727 }
1728
1729 if ( bThrown )
1730 throw eThrown;
1731
1732 // now the properties can be copied
1733 // the order of the properties setting is not important for StorageStream API
1734 OUString aPropName ("Compressed");
1735 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1736 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1737 {
1738 aPropName = "MediaType";
1739 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1740
1741 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1742 {
1743 aPropName = "UseCommonStoragePasswordEncryption";
1744 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1745 }
1746 }
1747 }
1748
ModifyParentUnlockMutex_Impl(osl::ClearableMutexGuard & aGuard)1749 void OWriteStream::ModifyParentUnlockMutex_Impl(osl::ClearableMutexGuard& aGuard)
1750 {
1751 if ( m_pImpl->m_pParent )
1752 {
1753 if ( m_pImpl->m_pParent->HasModifiedListener() )
1754 {
1755 uno::Reference< util::XModifiable > xParentModif( static_cast<util::XModifiable*>(m_pImpl->m_pParent->m_pAntiImpl) );
1756 aGuard.clear();
1757 xParentModif->setModified( true );
1758 }
1759 else
1760 m_pImpl->m_pParent->m_bIsModified = true;
1761 }
1762 }
1763
queryInterface(const uno::Type & rType)1764 uno::Any SAL_CALL OWriteStream::queryInterface( const uno::Type& rType )
1765 {
1766 // common interfaces
1767 uno::Any aReturn = ::cppu::queryInterface
1768 ( rType
1769 , static_cast<lang::XTypeProvider*> ( this )
1770 , static_cast<io::XInputStream*> ( this )
1771 , static_cast<io::XOutputStream*> ( this )
1772 , static_cast<io::XStream*> ( this )
1773 , static_cast<embed::XExtendedStorageStream*> ( this )
1774 , static_cast<io::XSeekable*> ( this )
1775 , static_cast<io::XTruncate*> ( this )
1776 , static_cast<lang::XComponent*> ( this )
1777 , static_cast<beans::XPropertySet*> ( this ) );
1778
1779 if ( aReturn.hasValue() )
1780 return aReturn ;
1781
1782 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1783 {
1784 aReturn = ::cppu::queryInterface
1785 ( rType
1786 , static_cast<embed::XEncryptionProtectedSource2*> ( this )
1787 , static_cast<embed::XEncryptionProtectedSource*> ( this ) );
1788 }
1789 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1790 {
1791 aReturn = ::cppu::queryInterface
1792 ( rType
1793 , static_cast<embed::XRelationshipAccess*> ( this ) );
1794 }
1795
1796 if ( aReturn.hasValue() )
1797 return aReturn ;
1798
1799 if ( m_bTransacted )
1800 {
1801 aReturn = ::cppu::queryInterface
1802 ( rType
1803 , static_cast<embed::XTransactedObject*> ( this )
1804 , static_cast<embed::XTransactionBroadcaster*> ( this ) );
1805
1806 if ( aReturn.hasValue() )
1807 return aReturn ;
1808 }
1809
1810 return OWeakObject::queryInterface( rType );
1811 }
1812
acquire()1813 void SAL_CALL OWriteStream::acquire() noexcept
1814 {
1815 OWeakObject::acquire();
1816 }
1817
release()1818 void SAL_CALL OWriteStream::release() noexcept
1819 {
1820 OWeakObject::release();
1821 }
1822
getTypes()1823 uno::Sequence< uno::Type > SAL_CALL OWriteStream::getTypes()
1824 {
1825 if (! m_pData->m_pTypeCollection)
1826 {
1827 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1828
1829 if (! m_pData->m_pTypeCollection)
1830 {
1831 if ( m_bTransacted )
1832 {
1833 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1834 {
1835 ::cppu::OTypeCollection aTmpCollection
1836 ( cppu::UnoType<lang::XTypeProvider>::get()
1837 , cppu::UnoType<io::XInputStream>::get()
1838 , cppu::UnoType<io::XOutputStream>::get()
1839 , cppu::UnoType<io::XStream>::get()
1840 , cppu::UnoType<io::XSeekable>::get()
1841 , cppu::UnoType<io::XTruncate>::get()
1842 , cppu::UnoType<lang::XComponent>::get()
1843 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1844 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1845 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1846 , cppu::UnoType<embed::XTransactedObject>::get()
1847 , cppu::UnoType<embed::XTransactionBroadcaster>::get());
1848
1849 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1850 ( cppu::UnoType<beans::XPropertySet>::get()
1851 , aTmpCollection.getTypes()));
1852 }
1853 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1854 {
1855 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1856 ( cppu::UnoType<lang::XTypeProvider>::get()
1857 , cppu::UnoType<io::XInputStream>::get()
1858 , cppu::UnoType<io::XOutputStream>::get()
1859 , cppu::UnoType<io::XStream>::get()
1860 , cppu::UnoType<io::XSeekable>::get()
1861 , cppu::UnoType<io::XTruncate>::get()
1862 , cppu::UnoType<lang::XComponent>::get()
1863 , cppu::UnoType<embed::XRelationshipAccess>::get()
1864 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1865 , cppu::UnoType<embed::XTransactedObject>::get()
1866 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1867 , cppu::UnoType<beans::XPropertySet>::get()));
1868 }
1869 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1870 {
1871 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1872 ( cppu::UnoType<lang::XTypeProvider>::get()
1873 , cppu::UnoType<io::XInputStream>::get()
1874 , cppu::UnoType<io::XOutputStream>::get()
1875 , cppu::UnoType<io::XStream>::get()
1876 , cppu::UnoType<io::XSeekable>::get()
1877 , cppu::UnoType<io::XTruncate>::get()
1878 , cppu::UnoType<lang::XComponent>::get()
1879 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1880 , cppu::UnoType<embed::XTransactedObject>::get()
1881 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1882 , cppu::UnoType<beans::XPropertySet>::get()));
1883 }
1884 }
1885 else
1886 {
1887 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1888 {
1889 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1890 ( cppu::UnoType<lang::XTypeProvider>::get()
1891 , cppu::UnoType<io::XInputStream>::get()
1892 , cppu::UnoType<io::XOutputStream>::get()
1893 , cppu::UnoType<io::XStream>::get()
1894 , cppu::UnoType<io::XSeekable>::get()
1895 , cppu::UnoType<io::XTruncate>::get()
1896 , cppu::UnoType<lang::XComponent>::get()
1897 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1898 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1899 , cppu::UnoType<beans::XPropertySet>::get()));
1900 }
1901 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1902 {
1903 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1904 ( cppu::UnoType<lang::XTypeProvider>::get()
1905 , cppu::UnoType<io::XInputStream>::get()
1906 , cppu::UnoType<io::XOutputStream>::get()
1907 , cppu::UnoType<io::XStream>::get()
1908 , cppu::UnoType<io::XSeekable>::get()
1909 , cppu::UnoType<io::XTruncate>::get()
1910 , cppu::UnoType<lang::XComponent>::get()
1911 , cppu::UnoType<embed::XRelationshipAccess>::get()
1912 , cppu::UnoType<beans::XPropertySet>::get()));
1913 }
1914 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1915 {
1916 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1917 ( cppu::UnoType<lang::XTypeProvider>::get()
1918 , cppu::UnoType<io::XInputStream>::get()
1919 , cppu::UnoType<io::XOutputStream>::get()
1920 , cppu::UnoType<io::XStream>::get()
1921 , cppu::UnoType<io::XSeekable>::get()
1922 , cppu::UnoType<io::XTruncate>::get()
1923 , cppu::UnoType<lang::XComponent>::get()
1924 , cppu::UnoType<beans::XPropertySet>::get()));
1925 }
1926 }
1927 }
1928 }
1929
1930 return m_pData->m_pTypeCollection->getTypes() ;
1931 }
1932
1933 namespace { struct lcl_ImplId : public rtl::Static< ::cppu::OImplementationId, lcl_ImplId > {}; }
1934
getImplementationId()1935 uno::Sequence< sal_Int8 > SAL_CALL OWriteStream::getImplementationId()
1936 {
1937 return css::uno::Sequence<sal_Int8>();
1938 }
1939
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)1940 sal_Int32 SAL_CALL OWriteStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
1941 {
1942 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1943
1944 CheckInitOnDemand();
1945
1946 if ( !m_pImpl )
1947 {
1948 SAL_INFO("package.xstor", "Disposed!");
1949 throw lang::DisposedException();
1950 }
1951
1952 if ( !m_xInStream.is() )
1953 throw io::NotConnectedException();
1954
1955 return m_xInStream->readBytes( aData, nBytesToRead );
1956 }
1957
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)1958 sal_Int32 SAL_CALL OWriteStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
1959 {
1960 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1961
1962 CheckInitOnDemand();
1963
1964 if ( !m_pImpl )
1965 {
1966 SAL_INFO("package.xstor", "Disposed!");
1967 throw lang::DisposedException();
1968 }
1969
1970 if ( !m_xInStream.is() )
1971 throw io::NotConnectedException();
1972
1973 return m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
1974 }
1975
skipBytes(sal_Int32 nBytesToSkip)1976 void SAL_CALL OWriteStream::skipBytes( sal_Int32 nBytesToSkip )
1977 {
1978 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1979
1980 CheckInitOnDemand();
1981
1982 if ( !m_pImpl )
1983 {
1984 SAL_INFO("package.xstor", "Disposed!");
1985 throw lang::DisposedException();
1986 }
1987
1988 if ( !m_xInStream.is() )
1989 throw io::NotConnectedException();
1990
1991 m_xInStream->skipBytes( nBytesToSkip );
1992 }
1993
available()1994 sal_Int32 SAL_CALL OWriteStream::available( )
1995 {
1996 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1997
1998 CheckInitOnDemand();
1999
2000 if ( !m_pImpl )
2001 {
2002 SAL_INFO("package.xstor", "Disposed!");
2003 throw lang::DisposedException();
2004 }
2005
2006 if ( !m_xInStream.is() )
2007 throw io::NotConnectedException();
2008
2009 return m_xInStream->available();
2010
2011 }
2012
closeInput()2013 void SAL_CALL OWriteStream::closeInput( )
2014 {
2015 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2016
2017 if ( !m_pImpl )
2018 {
2019 SAL_INFO("package.xstor", "Disposed!");
2020 throw lang::DisposedException();
2021 }
2022
2023 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2024 throw io::NotConnectedException();
2025
2026 // the input part of the stream stays open for internal purposes (to allow reading during copying)
2027 // since it can not be reopened until output part is closed, it will be closed with output part.
2028 m_bInStreamDisconnected = true;
2029 // m_xInStream->closeInput();
2030 // m_xInStream.clear();
2031
2032 if ( !m_xOutStream.is() )
2033 dispose();
2034 }
2035
getInputStream()2036 uno::Reference< io::XInputStream > SAL_CALL OWriteStream::getInputStream()
2037 {
2038 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2039
2040 if ( !m_pImpl )
2041 {
2042 SAL_INFO("package.xstor", "Disposed!");
2043 throw lang::DisposedException();
2044 }
2045
2046 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2047 return uno::Reference< io::XInputStream >();
2048
2049 return this;
2050 }
2051
getOutputStream()2052 uno::Reference< io::XOutputStream > SAL_CALL OWriteStream::getOutputStream()
2053 {
2054 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2055
2056 try
2057 {
2058 CheckInitOnDemand();
2059 }
2060 catch( const io::IOException& r )
2061 {
2062 throw lang::WrappedTargetRuntimeException("OWriteStream::getOutputStream: Could not create backing temp file",
2063 static_cast < OWeakObject * > ( this ), makeAny ( r ) );
2064 }
2065
2066 if ( !m_pImpl )
2067 {
2068 SAL_INFO("package.xstor", "Disposed!");
2069 throw lang::DisposedException();
2070 }
2071
2072 if ( !m_xOutStream.is() )
2073 return uno::Reference< io::XOutputStream >();
2074
2075 return this;
2076 }
2077
writeBytes(const uno::Sequence<sal_Int8> & aData)2078 void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
2079 {
2080 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2081
2082 // the write method makes initialization itself, since it depends from the aData length
2083 // NO CheckInitOnDemand()!
2084
2085 if ( !m_pImpl )
2086 {
2087 SAL_INFO("package.xstor", "Disposed!");
2088 throw lang::DisposedException();
2089 }
2090
2091 if ( !m_bInitOnDemand )
2092 {
2093 if ( !m_xOutStream.is() || !m_xSeekable.is())
2094 throw io::NotConnectedException();
2095
2096 if ( m_pImpl->m_xCacheStream.is() )
2097 {
2098 // check whether the cache should be turned off
2099 sal_Int64 nPos = m_xSeekable->getPosition();
2100 if ( nPos + aData.getLength() > MAX_STORCACHE_SIZE )
2101 {
2102 // disconnect the cache and copy the data to the temporary file
2103 m_xSeekable->seek( 0 );
2104
2105 // it is enough to copy the cached stream, the cache should already contain everything
2106 if ( !m_pImpl->GetFilledTempFileIfNo( m_xInStream ).isEmpty() )
2107 {
2108 DeInit();
2109 // the last position is known and it is differs from the current stream position
2110 m_nInitPosition = nPos;
2111 }
2112 }
2113 }
2114 }
2115
2116 if ( m_bInitOnDemand )
2117 {
2118 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2119 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
2120 if ( xStream.is() )
2121 {
2122 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
2123 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
2124 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
2125 m_xSeekable->seek( m_nInitPosition );
2126
2127 m_nInitPosition = 0;
2128 m_bInitOnDemand = false;
2129 }
2130 }
2131
2132 if ( !m_xOutStream.is() )
2133 throw io::NotConnectedException();
2134
2135 m_xOutStream->writeBytes( aData );
2136 m_pImpl->m_bHasDataToFlush = true;
2137
2138 ModifyParentUnlockMutex_Impl( aGuard );
2139 }
2140
flush()2141 void SAL_CALL OWriteStream::flush()
2142 {
2143 // In case stream is flushed its current version becomes visible
2144 // to the parent storage. Usually parent storage flushes the stream
2145 // during own commit but a user can explicitly flush the stream
2146 // so the changes will be available through cloning functionality.
2147
2148 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2149
2150 if ( !m_pImpl )
2151 {
2152 SAL_INFO("package.xstor", "Disposed!");
2153 throw lang::DisposedException();
2154 }
2155
2156 if ( !m_bInitOnDemand )
2157 {
2158 if ( !m_xOutStream.is() )
2159 throw io::NotConnectedException();
2160
2161 m_xOutStream->flush();
2162 m_pImpl->Commit();
2163 }
2164 }
2165
CloseOutput_Impl()2166 void OWriteStream::CloseOutput_Impl()
2167 {
2168 // all the checks must be done in calling method
2169
2170 m_xOutStream->closeOutput();
2171 m_xOutStream.clear();
2172
2173 if ( m_bInitOnDemand )
2174 return;
2175
2176 // after the stream is disposed it can be committed
2177 // so transport correct size property
2178 if ( !m_xSeekable.is() )
2179 throw uno::RuntimeException();
2180
2181 for ( auto& rProp : m_pImpl->m_aProps )
2182 {
2183 if ( rProp.Name == "Size" )
2184 rProp.Value <<= m_xSeekable->getLength();
2185 }
2186 }
2187
closeOutput()2188 void SAL_CALL OWriteStream::closeOutput()
2189 {
2190 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2191
2192 CheckInitOnDemand();
2193
2194 if ( !m_pImpl )
2195 {
2196 SAL_INFO("package.xstor", "Disposed!");
2197 throw lang::DisposedException();
2198 }
2199
2200 if ( !m_xOutStream.is() )
2201 throw io::NotConnectedException();
2202
2203 CloseOutput_Impl();
2204
2205 if ( m_bInStreamDisconnected || !m_xInStream.is() )
2206 dispose();
2207 }
2208
seek(sal_Int64 location)2209 void SAL_CALL OWriteStream::seek( sal_Int64 location )
2210 {
2211 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2212
2213 CheckInitOnDemand();
2214
2215 if ( !m_pImpl )
2216 {
2217 SAL_INFO("package.xstor", "Disposed!");
2218 throw lang::DisposedException();
2219 }
2220
2221 if ( !m_xSeekable.is() )
2222 throw uno::RuntimeException();
2223
2224 m_xSeekable->seek( location );
2225 }
2226
getPosition()2227 sal_Int64 SAL_CALL OWriteStream::getPosition()
2228 {
2229 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2230
2231 CheckInitOnDemand();
2232
2233 if ( !m_pImpl )
2234 {
2235 SAL_INFO("package.xstor", "Disposed!");
2236 throw lang::DisposedException();
2237 }
2238
2239 if ( !m_xSeekable.is() )
2240 throw uno::RuntimeException();
2241
2242 return m_xSeekable->getPosition();
2243 }
2244
getLength()2245 sal_Int64 SAL_CALL OWriteStream::getLength()
2246 {
2247 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2248
2249 CheckInitOnDemand();
2250
2251 if ( !m_pImpl )
2252 {
2253 SAL_INFO("package.xstor", "Disposed!");
2254 throw lang::DisposedException();
2255 }
2256
2257 if ( !m_xSeekable.is() )
2258 throw uno::RuntimeException();
2259
2260 return m_xSeekable->getLength();
2261 }
2262
truncate()2263 void SAL_CALL OWriteStream::truncate()
2264 {
2265 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2266
2267 CheckInitOnDemand();
2268
2269 if ( !m_pImpl )
2270 {
2271 SAL_INFO("package.xstor", "Disposed!");
2272 throw lang::DisposedException();
2273 }
2274
2275 if ( !m_xOutStream.is() )
2276 throw uno::RuntimeException();
2277
2278 uno::Reference< io::XTruncate > xTruncate( m_xOutStream, uno::UNO_QUERY_THROW );
2279 xTruncate->truncate();
2280
2281 m_pImpl->m_bHasDataToFlush = true;
2282
2283 ModifyParentUnlockMutex_Impl( aGuard );
2284 }
2285
dispose()2286 void SAL_CALL OWriteStream::dispose()
2287 {
2288 // should be an internal method since it can be called only from parent storage
2289 {
2290 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2291
2292 if ( !m_pImpl )
2293 {
2294 SAL_INFO("package.xstor", "Disposed!");
2295 throw lang::DisposedException();
2296 }
2297
2298 if ( m_xOutStream.is() )
2299 CloseOutput_Impl();
2300
2301 if ( m_xInStream.is() )
2302 {
2303 m_xInStream->closeInput();
2304 m_xInStream.clear();
2305 }
2306
2307 m_xSeekable.clear();
2308
2309 m_pImpl->m_pAntiImpl = nullptr;
2310
2311 if ( !m_bInitOnDemand )
2312 {
2313 try
2314 {
2315 if ( !m_bTransacted )
2316 {
2317 m_pImpl->Commit();
2318 }
2319 else
2320 {
2321 // throw away all the changes
2322 m_pImpl->Revert();
2323 }
2324 }
2325 catch( const uno::Exception& )
2326 {
2327 uno::Any aCaught( ::cppu::getCaughtException() );
2328 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
2329 throw lang::WrappedTargetRuntimeException("Can not commit/revert the storage!",
2330 static_cast< OWeakObject* >( this ),
2331 aCaught );
2332 }
2333 }
2334
2335 m_pImpl = nullptr;
2336 }
2337
2338 // the listener might try to get rid of parent storage, and the storage would delete this object;
2339 // for now the listener is just notified at the end of the method to workaround the problem
2340 // in future a more elegant way should be found
2341
2342 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
2343 m_pData->m_aListenersContainer.disposeAndClear( aSource );
2344 }
2345
addEventListener(const uno::Reference<lang::XEventListener> & xListener)2346 void SAL_CALL OWriteStream::addEventListener(
2347 const uno::Reference< lang::XEventListener >& xListener )
2348 {
2349 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2350
2351 if ( !m_pImpl )
2352 {
2353 SAL_INFO("package.xstor", "Disposed!");
2354 throw lang::DisposedException();
2355 }
2356
2357 m_pData->m_aListenersContainer.addInterface( cppu::UnoType<lang::XEventListener>::get(),
2358 xListener );
2359 }
2360
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)2361 void SAL_CALL OWriteStream::removeEventListener(
2362 const uno::Reference< lang::XEventListener >& xListener )
2363 {
2364 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2365
2366 if ( !m_pImpl )
2367 {
2368 SAL_INFO("package.xstor", "Disposed!");
2369 throw lang::DisposedException();
2370 }
2371
2372 m_pData->m_aListenersContainer.removeInterface( cppu::UnoType<lang::XEventListener>::get(),
2373 xListener );
2374 }
2375
setEncryptionPassword(const OUString & aPass)2376 void SAL_CALL OWriteStream::setEncryptionPassword( const OUString& aPass )
2377 {
2378 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2379
2380 CheckInitOnDemand();
2381
2382 if ( !m_pImpl )
2383 {
2384 SAL_INFO("package.xstor", "Disposed!");
2385 throw lang::DisposedException();
2386 }
2387
2388 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2389
2390 m_pImpl->SetEncrypted( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aPass ) );
2391
2392 ModifyParentUnlockMutex_Impl( aGuard );
2393 }
2394
removeEncryption()2395 void SAL_CALL OWriteStream::removeEncryption()
2396 {
2397 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2398
2399 CheckInitOnDemand();
2400
2401 if ( !m_pImpl )
2402 {
2403 SAL_INFO("package.xstor", "Disposed!");
2404 throw lang::DisposedException();
2405 }
2406
2407 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2408
2409 m_pImpl->SetDecrypted();
2410
2411 ModifyParentUnlockMutex_Impl( aGuard );
2412 }
2413
setEncryptionData(const uno::Sequence<beans::NamedValue> & aEncryptionData)2414 void SAL_CALL OWriteStream::setEncryptionData( const uno::Sequence< beans::NamedValue >& aEncryptionData )
2415 {
2416 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2417
2418 CheckInitOnDemand();
2419
2420 if ( !m_pImpl )
2421 {
2422 SAL_INFO("package.xstor", "Disposed!");
2423 throw lang::DisposedException();
2424 }
2425
2426 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2427
2428 m_pImpl->SetEncrypted( aEncryptionData );
2429
2430 ModifyParentUnlockMutex_Impl( aGuard );
2431 }
2432
hasEncryptionData()2433 sal_Bool SAL_CALL OWriteStream::hasEncryptionData()
2434 {
2435 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2436
2437 if (!m_pImpl)
2438 return false;
2439
2440 bool bRet = false;
2441
2442 try
2443 {
2444 bRet = m_pImpl->IsEncrypted();
2445
2446 if (!bRet && m_pImpl->m_bUseCommonEncryption && m_pImpl->m_pParent)
2447 bRet = m_pImpl->m_pParent->m_bHasCommonEncryptionData;
2448 }
2449 catch( const uno::RuntimeException& )
2450 {
2451 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
2452 throw;
2453 }
2454 catch( const uno::Exception& )
2455 {
2456 uno::Any aCaught( ::cppu::getCaughtException() );
2457 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
2458 throw lang::WrappedTargetRuntimeException( "Problems on hasEncryptionData!",
2459 static_cast< ::cppu::OWeakObject* >( this ),
2460 aCaught );
2461 }
2462
2463 return bRet;
2464 }
2465
hasByID(const OUString & sID)2466 sal_Bool SAL_CALL OWriteStream::hasByID( const OUString& sID )
2467 {
2468 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2469
2470 if ( !m_pImpl )
2471 {
2472 SAL_INFO("package.xstor", "Disposed!");
2473 throw lang::DisposedException();
2474 }
2475
2476 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2477 throw uno::RuntimeException();
2478
2479 try
2480 {
2481 getRelationshipByID( sID );
2482 return true;
2483 }
2484 catch( const container::NoSuchElementException& )
2485 {
2486 TOOLS_INFO_EXCEPTION("package.xstor", "No Element");
2487 }
2488
2489 return false;
2490 }
2491
getTargetByID(const OUString & sID)2492 OUString SAL_CALL OWriteStream::getTargetByID( const OUString& sID )
2493 {
2494 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2495
2496 if ( !m_pImpl )
2497 {
2498 SAL_INFO("package.xstor", "Disposed!");
2499 throw lang::DisposedException();
2500 }
2501
2502 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2503 throw uno::RuntimeException();
2504
2505 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2506 auto pRel = lcl_findPairByName(aSeq, "Target");
2507 if (pRel != aSeq.end())
2508 return pRel->Second;
2509
2510 return OUString();
2511 }
2512
getTypeByID(const OUString & sID)2513 OUString SAL_CALL OWriteStream::getTypeByID( const OUString& sID )
2514 {
2515 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2516
2517 if ( !m_pImpl )
2518 {
2519 SAL_INFO("package.xstor", "Disposed!");
2520 throw lang::DisposedException();
2521 }
2522
2523 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2524 throw uno::RuntimeException();
2525
2526 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2527 auto pRel = lcl_findPairByName(aSeq, "Type");
2528 if (pRel != aSeq.end())
2529 return pRel->Second;
2530
2531 return OUString();
2532 }
2533
getRelationshipByID(const OUString & sID)2534 uno::Sequence< beans::StringPair > SAL_CALL OWriteStream::getRelationshipByID( const OUString& sID )
2535 {
2536 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2537
2538 if ( !m_pImpl )
2539 {
2540 SAL_INFO("package.xstor", "Disposed!");
2541 throw lang::DisposedException();
2542 }
2543
2544 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2545 throw uno::RuntimeException();
2546
2547 // TODO/LATER: in future the unification of the ID could be checked
2548 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2549 const beans::StringPair aIDRel("Id", sID);
2550 auto pRel = std::find_if(aSeq.begin(), aSeq.end(),
2551 [&aIDRel](const uno::Sequence<beans::StringPair>& rRel) {
2552 return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
2553 if (pRel != aSeq.end())
2554 return *pRel;
2555
2556 throw container::NoSuchElementException();
2557 }
2558
getRelationshipsByType(const OUString & sType)2559 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getRelationshipsByType( const OUString& sType )
2560 {
2561 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2562
2563 if ( !m_pImpl )
2564 {
2565 SAL_INFO("package.xstor", "Disposed!");
2566 throw lang::DisposedException();
2567 }
2568
2569 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2570 throw uno::RuntimeException();
2571
2572 // TODO/LATER: in future the unification of the ID could be checked
2573 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2574 const beans::StringPair aTypeRel("Type", sType);
2575 std::vector< uno::Sequence<beans::StringPair> > aResult;
2576 aResult.reserve(aSeq.getLength());
2577
2578 std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult),
2579 [&aTypeRel](const uno::Sequence<beans::StringPair>& rRel) {
2580 return std::find(rRel.begin(), rRel.end(), aTypeRel) != rRel.end(); });
2581
2582 return comphelper::containerToSequence(aResult);
2583 }
2584
getAllRelationships()2585 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getAllRelationships()
2586 {
2587 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2588
2589 if ( !m_pImpl )
2590 {
2591 SAL_INFO("package.xstor", "Disposed!");
2592 throw lang::DisposedException();
2593 }
2594
2595 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2596 throw uno::RuntimeException();
2597
2598 return m_pImpl->GetAllRelationshipsIfAny();
2599 }
2600
insertRelationshipByID(const OUString & sID,const uno::Sequence<beans::StringPair> & aEntry,sal_Bool bReplace)2601 void SAL_CALL OWriteStream::insertRelationshipByID( const OUString& sID, const uno::Sequence< beans::StringPair >& aEntry, sal_Bool bReplace )
2602 {
2603 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2604
2605 if ( !m_pImpl )
2606 {
2607 SAL_INFO("package.xstor", "Disposed!");
2608 throw lang::DisposedException();
2609 }
2610
2611 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2612 throw uno::RuntimeException();
2613
2614 const beans::StringPair aIDRel("Id", sID);
2615
2616 sal_Int32 nIDInd = -1;
2617
2618 // TODO/LATER: in future the unification of the ID could be checked
2619 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2620 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2621 {
2622 const auto& rRel = aSeq[nInd];
2623 if (std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end())
2624 nIDInd = nInd;
2625 }
2626
2627 if ( nIDInd != -1 && !bReplace )
2628 throw container::ElementExistException(); // TODO
2629
2630 if ( nIDInd == -1 )
2631 {
2632 nIDInd = aSeq.getLength();
2633 aSeq.realloc( nIDInd + 1 );
2634 }
2635
2636 std::vector<beans::StringPair> aResult;
2637 aResult.reserve(aEntry.getLength() + 1);
2638
2639 aResult.push_back(aIDRel);
2640 std::copy_if(aEntry.begin(), aEntry.end(), std::back_inserter(aResult),
2641 [](const beans::StringPair& rRel) { return rRel.First != "Id"; });
2642
2643 aSeq[nIDInd] = comphelper::containerToSequence(aResult);
2644
2645 m_pImpl->m_aNewRelInfo = aSeq;
2646 m_pImpl->m_xNewRelInfoStream.clear();
2647 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2648 }
2649
removeRelationshipByID(const OUString & sID)2650 void SAL_CALL OWriteStream::removeRelationshipByID( const OUString& sID )
2651 {
2652 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2653
2654 if ( !m_pImpl )
2655 {
2656 SAL_INFO("package.xstor", "Disposed!");
2657 throw lang::DisposedException();
2658 }
2659
2660 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2661 throw uno::RuntimeException();
2662
2663 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2664 const beans::StringPair aIDRel("Id", sID);
2665 auto pRel = std::find_if(std::cbegin(aSeq), std::cend(aSeq),
2666 [&aIDRel](const uno::Sequence< beans::StringPair >& rRel) {
2667 return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
2668 if (pRel != std::cend(aSeq))
2669 {
2670 auto nInd = static_cast<sal_Int32>(std::distance(std::cbegin(aSeq), pRel));
2671 comphelper::removeElementAt(aSeq, nInd);
2672
2673 m_pImpl->m_aNewRelInfo = aSeq;
2674 m_pImpl->m_xNewRelInfoStream.clear();
2675 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2676
2677 // TODO/LATER: in future the unification of the ID could be checked
2678 return;
2679 }
2680
2681 throw container::NoSuchElementException();
2682 }
2683
insertRelationships(const uno::Sequence<uno::Sequence<beans::StringPair>> & aEntries,sal_Bool bReplace)2684 void SAL_CALL OWriteStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& aEntries, sal_Bool bReplace )
2685 {
2686 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2687
2688 if ( !m_pImpl )
2689 {
2690 SAL_INFO("package.xstor", "Disposed!");
2691 throw lang::DisposedException();
2692 }
2693
2694 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2695 throw uno::RuntimeException();
2696
2697 OUString aIDTag( "Id" );
2698 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2699 std::vector< uno::Sequence<beans::StringPair> > aResultVec;
2700 aResultVec.reserve(aSeq.getLength() + aEntries.getLength());
2701
2702 std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec),
2703 [&aIDTag, &aEntries, bReplace](const uno::Sequence<beans::StringPair>& rTargetRel) {
2704 auto pTargetPair = lcl_findPairByName(rTargetRel, aIDTag);
2705 if (pTargetPair == rTargetRel.end())
2706 return false;
2707
2708 bool bIsSourceSame = std::any_of(aEntries.begin(), aEntries.end(),
2709 [&pTargetPair](const uno::Sequence<beans::StringPair>& rSourceEntry) {
2710 return std::find(rSourceEntry.begin(), rSourceEntry.end(), *pTargetPair) != rSourceEntry.end(); });
2711
2712 if ( bIsSourceSame && !bReplace )
2713 throw container::ElementExistException();
2714
2715 // if no such element in the provided sequence
2716 return !bIsSourceSame;
2717 });
2718
2719 std::transform(aEntries.begin(), aEntries.end(), std::back_inserter(aResultVec),
2720 [&aIDTag](const uno::Sequence<beans::StringPair>& rEntry) -> uno::Sequence<beans::StringPair> {
2721 auto pPair = lcl_findPairByName(rEntry, aIDTag);
2722 if (pPair == rEntry.end())
2723 throw io::IOException(); // TODO: illegal relation ( no ID )
2724
2725 auto aResult = comphelper::sequenceToContainer<std::vector<beans::StringPair>>(rEntry);
2726 auto nIDInd = std::distance(rEntry.begin(), pPair);
2727 std::rotate(aResult.begin(), std::next(aResult.begin(), nIDInd), std::next(aResult.begin(), nIDInd + 1));
2728
2729 return comphelper::containerToSequence(aResult);
2730 });
2731
2732 m_pImpl->m_aNewRelInfo = comphelper::containerToSequence(aResultVec);
2733 m_pImpl->m_xNewRelInfoStream.clear();
2734 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2735 }
2736
clearRelationships()2737 void SAL_CALL OWriteStream::clearRelationships()
2738 {
2739 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2740
2741 if ( !m_pImpl )
2742 {
2743 SAL_INFO("package.xstor", "Disposed!");
2744 throw lang::DisposedException();
2745 }
2746
2747 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2748 throw uno::RuntimeException();
2749
2750 m_pImpl->m_aNewRelInfo.realloc( 0 );
2751 m_pImpl->m_xNewRelInfoStream.clear();
2752 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2753 }
2754
getPropertySetInfo()2755 uno::Reference< beans::XPropertySetInfo > SAL_CALL OWriteStream::getPropertySetInfo()
2756 {
2757 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2758
2759 //TODO:
2760 return uno::Reference< beans::XPropertySetInfo >();
2761 }
2762
setPropertyValue(const OUString & aPropertyName,const uno::Any & aValue)2763 void SAL_CALL OWriteStream::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
2764 {
2765 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
2766
2767 if ( !m_pImpl )
2768 {
2769 SAL_INFO("package.xstor", "Disposed!");
2770 throw lang::DisposedException();
2771 }
2772
2773 m_pImpl->GetStreamProperties();
2774 OUString aCompressedString( "Compressed" );
2775 OUString aMediaTypeString( "MediaType" );
2776 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == aMediaTypeString )
2777 {
2778 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2779 bool bCompressedValueFromType = true;
2780 OUString aType;
2781 aValue >>= aType;
2782
2783 if ( !m_pImpl->m_bCompressedSetExplicit )
2784 {
2785 if ( aType == "image/jpeg" || aType == "image/png" || aType == "image/gif" )
2786 bCompressedValueFromType = false;
2787 }
2788
2789 for ( auto& rProp : m_pImpl->m_aProps )
2790 {
2791 if ( aPropertyName == rProp.Name )
2792 rProp.Value = aValue;
2793 else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString == rProp.Name )
2794 rProp.Value <<= bCompressedValueFromType;
2795 }
2796 }
2797 else if ( aPropertyName == aCompressedString )
2798 {
2799 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2800 m_pImpl->m_bCompressedSetExplicit = true;
2801 for ( auto& rProp : m_pImpl->m_aProps )
2802 {
2803 if ( aPropertyName == rProp.Name )
2804 rProp.Value = aValue;
2805 }
2806 }
2807 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2808 && aPropertyName == "UseCommonStoragePasswordEncryption" )
2809 {
2810 bool bUseCommonEncryption = false;
2811 if ( !(aValue >>= bUseCommonEncryption) )
2812 throw lang::IllegalArgumentException(); //TODO
2813
2814 if ( m_bInitOnDemand && m_pImpl->m_bHasInsertedStreamOptimization )
2815 {
2816 // the data stream is provided to the packagestream directly
2817 m_pImpl->m_bUseCommonEncryption = bUseCommonEncryption;
2818 }
2819 else if ( bUseCommonEncryption )
2820 {
2821 if ( !m_pImpl->m_bUseCommonEncryption )
2822 {
2823 m_pImpl->SetDecrypted();
2824 m_pImpl->m_bUseCommonEncryption = true;
2825 }
2826 }
2827 else
2828 m_pImpl->m_bUseCommonEncryption = false;
2829 }
2830 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == aMediaTypeString )
2831 {
2832 for ( auto& rProp : m_pImpl->m_aProps )
2833 {
2834 if ( aPropertyName == rProp.Name )
2835 rProp.Value = aValue;
2836 }
2837 }
2838 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfoStream" )
2839 {
2840 uno::Reference< io::XInputStream > xInRelStream;
2841 if ( !( aValue >>= xInRelStream ) || !xInRelStream.is() )
2842 throw lang::IllegalArgumentException(); // TODO
2843
2844 uno::Reference< io::XSeekable > xSeek( xInRelStream, uno::UNO_QUERY );
2845 if ( !xSeek.is() )
2846 {
2847 // currently this is an internal property that is used for optimization
2848 // and the stream must support XSeekable interface
2849 // TODO/LATER: in future it can be changed if property is used from outside
2850 throw lang::IllegalArgumentException(); // TODO
2851 }
2852
2853 m_pImpl->m_xNewRelInfoStream = xInRelStream;
2854 m_pImpl->m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
2855 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED_STREAM;
2856 }
2857 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfo" )
2858 {
2859 if ( !(aValue >>= m_pImpl->m_aNewRelInfo) )
2860 throw lang::IllegalArgumentException(); // TODO
2861 }
2862 else if ( aPropertyName == "Size" )
2863 throw beans::PropertyVetoException(); // TODO
2864 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2865 && ( aPropertyName == "IsEncrypted" || aPropertyName == "Encrypted" ) )
2866 throw beans::PropertyVetoException(); // TODO
2867 else if ( aPropertyName == "RelId" )
2868 {
2869 aValue >>= m_pImpl->m_nRelId;
2870 }
2871 else
2872 throw beans::UnknownPropertyException(aPropertyName); // TODO
2873
2874 m_pImpl->m_bHasDataToFlush = true;
2875 ModifyParentUnlockMutex_Impl( aGuard );
2876 }
2877
getPropertyValue(const OUString & aProp)2878 uno::Any SAL_CALL OWriteStream::getPropertyValue( const OUString& aProp )
2879 {
2880 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2881
2882 if ( !m_pImpl )
2883 {
2884 SAL_INFO("package.xstor", "Disposed!");
2885 throw lang::DisposedException();
2886 }
2887
2888 if ( aProp == "RelId" )
2889 {
2890 return uno::makeAny( m_pImpl->GetNewRelId() );
2891 }
2892
2893 OUString aPropertyName;
2894 if ( aProp == "IsEncrypted" )
2895 aPropertyName = "Encrypted";
2896 else
2897 aPropertyName = aProp;
2898
2899 if ( ( ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
2900 && aPropertyName == "MediaType" )
2901 || ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == "Encrypted" )
2902 || aPropertyName == "Compressed" )
2903 {
2904 m_pImpl->GetStreamProperties();
2905
2906 auto pProp = std::find_if(std::cbegin(m_pImpl->m_aProps), std::cend(m_pImpl->m_aProps),
2907 [&aPropertyName](const css::beans::PropertyValue& rProp){ return aPropertyName == rProp.Name; });
2908 if (pProp != std::cend(m_pImpl->m_aProps))
2909 return pProp->Value;
2910 }
2911 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2912 && aPropertyName == "UseCommonStoragePasswordEncryption" )
2913 return uno::makeAny( m_pImpl->m_bUseCommonEncryption );
2914 else if ( aPropertyName == "Size" )
2915 {
2916 bool bThrow = false;
2917 try
2918 {
2919 CheckInitOnDemand();
2920 }
2921 catch (const io::IOException&)
2922 {
2923 bThrow = true;
2924 }
2925 if (bThrow || !m_xSeekable.is())
2926 throw uno::RuntimeException();
2927
2928 return uno::makeAny( m_xSeekable->getLength() );
2929 }
2930
2931 throw beans::UnknownPropertyException(aPropertyName); // TODO
2932 }
2933
addPropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)2934 void SAL_CALL OWriteStream::addPropertyChangeListener(
2935 const OUString& /*aPropertyName*/,
2936 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
2937 {
2938 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2939
2940 if ( !m_pImpl )
2941 {
2942 SAL_INFO("package.xstor", "Disposed!");
2943 throw lang::DisposedException();
2944 }
2945
2946 //TODO:
2947 }
2948
removePropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)2949 void SAL_CALL OWriteStream::removePropertyChangeListener(
2950 const OUString& /*aPropertyName*/,
2951 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
2952 {
2953 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2954
2955 if ( !m_pImpl )
2956 {
2957 SAL_INFO("package.xstor", "Disposed!");
2958 throw lang::DisposedException();
2959 }
2960
2961 //TODO:
2962 }
2963
addVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)2964 void SAL_CALL OWriteStream::addVetoableChangeListener(
2965 const OUString& /*PropertyName*/,
2966 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
2967 {
2968 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2969
2970 if ( !m_pImpl )
2971 {
2972 SAL_INFO("package.xstor", "Disposed!");
2973 throw lang::DisposedException();
2974 }
2975
2976 //TODO:
2977 }
2978
removeVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)2979 void SAL_CALL OWriteStream::removeVetoableChangeListener(
2980 const OUString& /*PropertyName*/,
2981 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
2982 {
2983 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2984
2985 if ( !m_pImpl )
2986 {
2987 SAL_INFO("package.xstor", "Disposed!");
2988 throw lang::DisposedException();
2989 }
2990
2991 //TODO:
2992 }
2993
2994 // XTransactedObject
2995
BroadcastTransaction(sal_Int8 nMessage)2996 void OWriteStream::BroadcastTransaction( sal_Int8 nMessage )
2997 /*
2998 1 - preCommit
2999 2 - committed
3000 3 - preRevert
3001 4 - reverted
3002 */
3003 {
3004 // no need to lock mutex here for the checking of m_pImpl, and m_pData is alive until the object is destructed
3005 if ( !m_pImpl )
3006 {
3007 SAL_INFO("package.xstor", "Disposed!");
3008 throw lang::DisposedException();
3009 }
3010
3011 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
3012
3013 ::cppu::OInterfaceContainerHelper* pContainer =
3014 m_pData->m_aListenersContainer.getContainer(
3015 cppu::UnoType<embed::XTransactionListener>::get());
3016 if ( !pContainer )
3017 return;
3018
3019 ::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
3020 while ( pIterator.hasMoreElements( ) )
3021 {
3022 OSL_ENSURE( nMessage >= 1 && nMessage <= 4, "Wrong internal notification code is used!" );
3023
3024 switch( nMessage )
3025 {
3026 case STOR_MESS_PRECOMMIT:
3027 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preCommit( aSource );
3028 break;
3029 case STOR_MESS_COMMITTED:
3030 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->commited( aSource );
3031 break;
3032 case STOR_MESS_PREREVERT:
3033 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preRevert( aSource );
3034 break;
3035 case STOR_MESS_REVERTED:
3036 static_cast< embed::XTransactionListener*>( pIterator.next( ) )->reverted( aSource );
3037 break;
3038 }
3039 }
3040 }
commit()3041 void SAL_CALL OWriteStream::commit()
3042 {
3043 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::commit" );
3044
3045 if ( !m_pImpl )
3046 {
3047 SAL_INFO("package.xstor", "Disposed!");
3048 throw lang::DisposedException();
3049 }
3050
3051 if ( !m_bTransacted )
3052 throw uno::RuntimeException();
3053
3054 try {
3055 BroadcastTransaction( STOR_MESS_PRECOMMIT );
3056
3057 osl::ClearableMutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
3058
3059 if ( !m_pImpl )
3060 {
3061 SAL_INFO("package.xstor", "Disposed!");
3062 throw lang::DisposedException();
3063 }
3064
3065 m_pImpl->Commit();
3066
3067 // when the storage is committed the parent is modified
3068 ModifyParentUnlockMutex_Impl( aGuard );
3069 }
3070 catch( const io::IOException& )
3071 {
3072 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3073 throw;
3074 }
3075 catch( const embed::StorageWrappedTargetException& )
3076 {
3077 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3078 throw;
3079 }
3080 catch( const uno::RuntimeException& )
3081 {
3082 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3083 throw;
3084 }
3085 catch( const uno::Exception& )
3086 {
3087 uno::Any aCaught( ::cppu::getCaughtException() );
3088 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
3089 throw embed::StorageWrappedTargetException( "Problems on commit!",
3090 static_cast< ::cppu::OWeakObject* >( this ),
3091 aCaught );
3092 }
3093
3094 BroadcastTransaction( STOR_MESS_COMMITTED );
3095 }
3096
revert()3097 void SAL_CALL OWriteStream::revert()
3098 {
3099 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::revert" );
3100
3101 // the method removes all the changes done after last commit
3102
3103 if ( !m_pImpl )
3104 {
3105 SAL_INFO("package.xstor", "Disposed!");
3106 throw lang::DisposedException();
3107 }
3108
3109 if ( !m_bTransacted )
3110 throw uno::RuntimeException();
3111
3112 BroadcastTransaction( STOR_MESS_PREREVERT );
3113
3114 {
3115 osl::MutexGuard aGuard(m_pData->m_xSharedMutex->GetMutex());
3116
3117 if (!m_pImpl)
3118 {
3119 SAL_INFO("package.xstor", "Disposed!");
3120 throw lang::DisposedException();
3121 }
3122
3123 try {
3124 m_pImpl->Revert();
3125 }
3126 catch (const io::IOException&)
3127 {
3128 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3129 throw;
3130 }
3131 catch (const embed::StorageWrappedTargetException&)
3132 {
3133 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3134 throw;
3135 }
3136 catch (const uno::RuntimeException&)
3137 {
3138 TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow");
3139 throw;
3140 }
3141 catch (const uno::Exception&)
3142 {
3143 uno::Any aCaught(::cppu::getCaughtException());
3144 SAL_INFO("package.xstor", "Rethrow: " << exceptionToString(aCaught));
3145 throw embed::StorageWrappedTargetException("Problems on revert!",
3146 static_cast<::cppu::OWeakObject*>(this),
3147 aCaught);
3148 }
3149 }
3150
3151 BroadcastTransaction( STOR_MESS_REVERTED );
3152 }
3153
3154 // XTransactionBroadcaster
3155
addTransactionListener(const uno::Reference<embed::XTransactionListener> & aListener)3156 void SAL_CALL OWriteStream::addTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3157 {
3158 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3159
3160 if ( !m_pImpl )
3161 {
3162 SAL_INFO("package.xstor", "Disposed!");
3163 throw lang::DisposedException();
3164 }
3165
3166 if ( !m_bTransacted )
3167 throw uno::RuntimeException();
3168
3169 m_pData->m_aListenersContainer.addInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3170 aListener );
3171 }
3172
removeTransactionListener(const uno::Reference<embed::XTransactionListener> & aListener)3173 void SAL_CALL OWriteStream::removeTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3174 {
3175 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3176
3177 if ( !m_pImpl )
3178 {
3179 SAL_INFO("package.xstor", "Disposed!");
3180 throw lang::DisposedException();
3181 }
3182
3183 if ( !m_bTransacted )
3184 throw uno::RuntimeException();
3185
3186 m_pData->m_aListenersContainer.removeInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3187 aListener );
3188 }
3189
3190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
3191