Debian Codegraph @

ucb/source/ucp/tdoc/tdoc_storage.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/embed/ElementModes.hpp>
22 #include <com/sun/star/embed/InvalidStorageException.hpp>
23 #include <com/sun/star/embed/StorageFactory.hpp>
24 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
25 #include <com/sun/star/io/IOException.hpp>
26 #include <com/sun/star/packages/NoEncryptionException.hpp>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <utility>
29 #include <osl/diagnose.h>
30
31 #include "tdoc_uri.hxx"
32 #include "tdoc_docmgr.hxx"
33 #include "tdoc_stgelems.hxx"
34
35 #include "tdoc_storage.hxx"
36
37 using namespace com::sun::star;
38 using namespace tdoc_ucp;
39
40
41 // StorageElementFactory Implementation.
42
43
44 StorageElementFactory::StorageElementFactory(
45 uno::Reference< uno::XComponentContext > xContext,
46 rtl::Reference< OfficeDocumentsManager > xDocsMgr )
47 : m_xDocsMgr(std::move( xDocsMgr )),
48 m_xContext(std::move( xContext ))
49 {
50 }
51
52
53 StorageElementFactory::~StorageElementFactory()
54 {
55 OSL_ENSURE( m_aMap.empty(),
56 "StorageElementFactory::~StorageElementFactory - Dangling storages!" );
57 }
58
59
60 uno::Reference< embed::XStorage >
61 StorageElementFactory::createTemporaryStorage()
62 {
63 uno::Reference< embed::XStorage > xStorage;
64 uno::Reference< lang::XSingleServiceFactory > xStorageFac;
65 if ( m_xContext.is() )
66 {
67 xStorageFac = embed::StorageFactory::create( m_xContext );
68 }
69
70 OSL_ENSURE( xStorageFac.is(), "Can't create storage factory!" );
71 if ( xStorageFac.is() )
72 xStorage.set( xStorageFac->createInstance(), uno::UNO_QUERY );
73
74 if ( !xStorage.is() )
75 throw uno::RuntimeException();
76
77 return xStorage;
78 }
79
80
81 uno::Reference< embed::XStorage >
82 StorageElementFactory::createStorage( const OUString & rUri,
83 StorageAccessMode eMode )
84 {
85 osl::MutexGuard aGuard( m_aMutex );
86
87 if ( ( eMode != READ ) &&
88 ( eMode != READ_WRITE_NOCREATE ) &&
89 ( eMode != READ_WRITE_CREATE ) )
90 throw lang::IllegalArgumentException(
91 u"Invalid open mode!"_ustr,
92 uno::Reference< uno::XInterface >(),
93 sal_Int16( 2 ) );
94
95 Uri aUri( rUri );
96 if ( aUri.isRoot() )
97 {
98 throw lang::IllegalArgumentException(
99 u"Root never has a storage!"_ustr,
100 uno::Reference< uno::XInterface >(),
101 sal_Int16( 1 ) );
102 }
103
104 OUString aUriKey
105 ( rUri.endsWith("/")
106 ? rUri.copy( 0, rUri.getLength() - 1 )
107 : rUri );
108
109 StorageMap::iterator aIt ( m_aMap.begin() );
110 StorageMap::iterator aEnd( m_aMap.end() );
111
112 while ( aIt != aEnd )
113 {
114 if ( (*aIt).first.first == aUriKey )
115 {
116 // URI matches. Now, check open mode.
117 bool bMatch = true;
118 switch ( eMode )
119 {
120 case READ:
121 // No need to check; storage is at least readable.
122 bMatch = true;
123 break;
124
125 case READ_WRITE_NOCREATE:
126 case READ_WRITE_CREATE:
127 // If found storage is writable, it can be used.
128 // If not, a new one must be created.
129 bMatch = (*aIt).first.second;
130 break;
131 }
132
133 if ( bMatch )
134 break;
135 }
136 ++aIt;
137 }
138
139 if ( aIt == aEnd )
140 {
141 uno::Reference< embed::XStorage > xParentStorage;
142
143 // documents never have a parent storage.
144 if ( !aUri.isDocument() )
145 {
146 xParentStorage = queryParentStorage( aUriKey, eMode );
147
148 if ( !xParentStorage.is() )
149 {
150 // requested to create new storage, but failed?
151 OSL_ENSURE( eMode != READ_WRITE_CREATE,
152 "Unable to create parent storage!" );
153 return xParentStorage;
154 }
155 }
156
157 uno::Reference< embed::XStorage > xStorage
158 = queryStorage( xParentStorage, aUriKey, eMode );
159
160 if ( !xStorage.is() )
161 {
162 // requested to create new storage, but failed?
163 OSL_ENSURE( eMode != READ_WRITE_CREATE,
164 "Unable to create storage!" );
165 return xStorage;
166 }
167
168 bool bWritable = ( ( eMode == READ_WRITE_NOCREATE )
169 || ( eMode == READ_WRITE_CREATE ) );
170
171 rtl::Reference< Storage > xElement(
172 new Storage( m_xContext, this, aUriKey, xParentStorage, xStorage ) );
173
174 aIt = m_aMap.emplace(
175 std::pair< OUString, bool >( aUriKey, bWritable ),
176 xElement.get() ).first;
177
178 aIt->second->m_aContainerIt = aIt;
179 return aIt->second;
180 }
181 else if ( osl_atomic_increment( &aIt->second->m_refCount ) > 1 )
182 {
183 uno::Reference< embed::XStorage > xElement( aIt->second );
184 osl_atomic_decrement( &aIt->second->m_refCount );
185 return xElement;
186 }
187 else
188 {
189 osl_atomic_decrement( &aIt->second->m_refCount );
190 aIt->second->m_aContainerIt = m_aMap.end();
191
192 uno::Reference< embed::XStorage > xParentStorage;
193
194 // documents never have a parent storage.
195 if ( !aUri.isDocument() )
196 {
197 xParentStorage = queryParentStorage( aUriKey, eMode );
198
199 if ( !xParentStorage.is() )
200 {
201 // requested to create new storage, but failed?
202 OSL_ENSURE( eMode != READ_WRITE_CREATE,
203 "Unable to create parent storage!" );
204 return xParentStorage;
205 }
206 }
207
208 uno::Reference< embed::XStorage > xStorage
209 = queryStorage( xParentStorage, aUriKey, eMode );
210
211 if ( !xStorage.is() )
212 {
213 // requested to create new storage, but failed?
214 OSL_ENSURE( eMode != READ_WRITE_CREATE,
215 "Unable to create storage!" );
216 return xStorage;
217 }
218
219 rtl::Reference<Storage> pNewStorage = new Storage( m_xContext, this, aUriKey, xParentStorage, xStorage );
220 aIt->second = pNewStorage.get();
221 aIt->second->m_aContainerIt = aIt;
222 return pNewStorage;
223 }
224 }
225
226
227 uno::Reference< io::XInputStream >
228 StorageElementFactory::createInputStream( const OUString & rUri,
229 const OUString & rPassword )
230 {
231 osl::MutexGuard aGuard( m_aMutex );
232
233 uno::Reference< embed::XStorage > xParentStorage
234 = queryParentStorage( rUri, READ );
235
236 // Each stream must have a parent storage.
237 if ( !xParentStorage.is() )
238 return uno::Reference< io::XInputStream >();
239
240 uno::Reference< io::XStream > xStream
241 = queryStream( xParentStorage, rUri, rPassword, READ, false );
242
243 if ( !xStream.is() )
244 return uno::Reference< io::XInputStream >();
245
246 return xStream->getInputStream();
247 }
248
249
250 uno::Reference< io::XOutputStream >
251 StorageElementFactory::createOutputStream( const OUString & rUri,
252 const OUString & rPassword,
253 bool bTruncate )
254 {
255 osl::MutexGuard aGuard( m_aMutex );
256
257 uno::Reference< embed::XStorage > xParentStorage
258 = queryParentStorage( rUri, READ_WRITE_CREATE );
259
260 // Each stream must have a parent storage.
261 if ( !xParentStorage.is() )
262 {
263 OSL_FAIL( "StorageElementFactory::createOutputStream - "
264 "Unable to create parent storage!" );
265 return uno::Reference< io::XOutputStream >();
266 }
267
268 uno::Reference< io::XStream > xStream
269 = queryStream(
270 xParentStorage, rUri, rPassword, READ_WRITE_CREATE, bTruncate );
271
272 if ( !xStream.is() )
273 {
274 OSL_FAIL( "StorageElementFactory::createOutputStream - "
275 "Unable to create stream!" );
276 return uno::Reference< io::XOutputStream >();
277 }
278
279 // Note: We need a wrapper to hold a reference to the parent storage to
280 // ensure that nobody else owns it at the moment we want to commit
281 // our changes. (There can be only one writable instance at a time
282 // and even no writable instance if there is already another
283 // read-only instance!)
284 return uno::Reference< io::XOutputStream >(
285 new OutputStream( m_xContext, rUri, xParentStorage, xStream->getOutputStream() ) );
286 }
287
288
289 uno::Reference< io::XStream >
290 StorageElementFactory::createStream( const OUString & rUri,
291 const OUString & rPassword,
292 bool bTruncate )
293 {
294 osl::MutexGuard aGuard( m_aMutex );
295
296 uno::Reference< embed::XStorage > xParentStorage
297 = queryParentStorage( rUri, READ_WRITE_CREATE );
298
299 // Each stream must have a parent storage.
300 if ( !xParentStorage.is() )
301 {
302 OSL_FAIL( "StorageElementFactory::createStream - "
303 "Unable to create parent storage!" );
304 return uno::Reference< io::XStream >();
305 }
306
307 uno::Reference< io::XStream > xStream
308 = queryStream(
309 xParentStorage, rUri, rPassword, READ_WRITE_NOCREATE, bTruncate );
310
311 if ( !xStream.is() )
312 {
313 OSL_FAIL( "StorageElementFactory::createStream - "
314 "Unable to create stream!" );
315 return uno::Reference< io::XStream >();
316 }
317
318 return uno::Reference< io::XStream >(
319 new Stream( m_xContext, m_xDocsMgr, rUri, xParentStorage, xStream ) );
320 }
321
322 void StorageElementFactory::releaseElement( Storage const * pElement )
323 {
324 assert(pElement);
325 osl::MutexGuard aGuard( m_aMutex );
326 if ( pElement->m_aContainerIt != m_aMap.end() )
327 m_aMap.erase( pElement->m_aContainerIt );
328 }
329
330 // Non-UNO interface
331
332 uno::Reference< embed::XStorage > StorageElementFactory::queryParentStorage(
333 const OUString & rUri, StorageAccessMode eMode )
334 {
335 uno::Reference< embed::XStorage > xParentStorage;
336
337 Uri aUri( rUri );
338 Uri aParentUri( aUri.getParentUri() );
339 if ( !aParentUri.isRoot() )
340 {
341 xParentStorage = createStorage( aUri.getParentUri(), eMode );
342 OSL_ENSURE( xParentStorage.is()
343 // requested to create new storage, but failed?
344 || ( eMode != READ_WRITE_CREATE ),
345 "StorageElementFactory::queryParentStorage - No storage!" );
346 }
347 return xParentStorage;
348 }
349
350
351 uno::Reference< embed::XStorage > StorageElementFactory::queryStorage(
352 const uno::Reference< embed::XStorage > & xParentStorage,
353 const OUString & rUri,
354 StorageAccessMode eMode )
355 {
356 uno::Reference< embed::XStorage > xStorage;
357
358 Uri aUri( rUri );
359
360 if ( !xParentStorage.is() )
361 {
362 // document storage
363
364 xStorage = m_xDocsMgr->queryStorage( aUri.getDocumentId() );
365
366 if ( !xStorage.is() )
367 {
368 if ( eMode == READ_WRITE_CREATE )
369 throw lang::IllegalArgumentException(
370 u"Invalid open mode: document storages cannot be created!"_ustr,
371 uno::Reference< uno::XInterface >(),
372 sal_Int16( 2 ) );
373 else
374 throw embed::InvalidStorageException(
375 u"Invalid document id!"_ustr,
376 uno::Reference< uno::XInterface >() );
377 }
378
379 // match xStorage's open mode against requested open mode
380
381 uno::Reference< beans::XPropertySet > xPropSet(
382 xStorage, uno::UNO_QUERY );
383 OSL_ENSURE( xPropSet.is(),
384 "StorageElementFactory::queryStorage - "
385 "No XPropertySet interface!" );
386 try
387 {
388 uno::Any aPropValue = xPropSet->getPropertyValue(u"OpenMode"_ustr);
389
390 sal_Int32 nOpenMode = 0;
391 if ( aPropValue >>= nOpenMode )
392 {
393 switch ( eMode )
394 {
395 case READ:
396 if ( !( nOpenMode & embed::ElementModes::READ ) )
397 {
398 // document opened, but not readable.
399 throw embed::InvalidStorageException(
400 u"Storage is open, but not readable!"_ustr );
401 }
402 // storage okay
403 break;
404
405 case READ_WRITE_NOCREATE:
406 case READ_WRITE_CREATE:
407 if ( !( nOpenMode & embed::ElementModes::WRITE ) )
408 {
409 // document opened, but not writable.
410 throw embed::InvalidStorageException(
411 u"Storage is open, but not writable!"_ustr );
412 }
413 // storage okay
414 break;
415 }
416 }
417 else
418 {
419 OSL_FAIL(
420 "Bug! Value of property OpenMode has wrong type!" );
421
422 throw uno::RuntimeException(
423 u"Bug! Value of property OpenMode has wrong type!"_ustr );
424 }
425 }
426 catch ( beans::UnknownPropertyException const & )
427 {
428 css::uno::Any anyEx = cppu::getCaughtException();
429 OSL_FAIL( "Property OpenMode not supported!" );
430
431 throw embed::StorageWrappedTargetException(
432 u"Bug! Value of property OpenMode has wrong type!"_ustr,
433 uno::Reference< uno::XInterface >(),
434 anyEx );
435 }
436 catch ( lang::WrappedTargetException const & )
437 {
438 css::uno::Any anyEx = cppu::getCaughtException();
439 OSL_FAIL( "Caught WrappedTargetException!" );
440
441 throw embed::StorageWrappedTargetException(
442 u"WrappedTargetException during getPropertyValue!"_ustr,
443 uno::Reference< uno::XInterface >(),
444 anyEx );
445 }
446 }
447 else
448 {
449 // sub storage
450
451 const OUString & rName = aUri.getDecodedName();
452
453 if ( eMode == READ )
454 {
455 try
456 {
457 sal_Int32 const nOpenMode = embed::ElementModes::READ
458 | embed::ElementModes::NOCREATE;
459 xStorage
460 = xParentStorage->openStorageElement( rName, nOpenMode );
461 }
462 catch ( io::IOException const & )
463 {
464 // Another chance: Try to clone storage.
465 xStorage = createTemporaryStorage();
466 xParentStorage->copyStorageElementLastCommitTo( rName,
467 xStorage );
468 }
469 }
470 else
471 {
472 sal_Int32 nOpenMode = embed::ElementModes::READWRITE;
473 if ( eMode == READ_WRITE_NOCREATE )
474 nOpenMode |= embed::ElementModes::NOCREATE;
475
476 xStorage = xParentStorage->openStorageElement( rName, nOpenMode );
477 }
478 }
479
480 OSL_ENSURE( xStorage.is() || ( eMode != READ_WRITE_CREATE ),
481 "StorageElementFactory::queryStorage - No storage!" );
482 return xStorage;
483 }
484
485
486 uno::Reference< io::XStream >
487 StorageElementFactory::queryStream(
488 const uno::Reference< embed::XStorage > & xParentStorage,
489 const OUString & rUri,
490 const OUString & rPassword,
491 StorageAccessMode eMode,
492 bool bTruncate )
493 {
494 osl::MutexGuard aGuard( m_aMutex );
495
496 if ( !xParentStorage.is() )
497 {
498 throw lang::IllegalArgumentException(
499 u"No parent storage!"_ustr,
500 uno::Reference< uno::XInterface >(),
501 sal_Int16( 2 ) );
502 }
503
504 Uri aUri( rUri );
505 if ( aUri.isRoot() )
506 {
507 throw lang::IllegalArgumentException(
508 u"Root never is a stream!"_ustr,
509 uno::Reference< uno::XInterface >(),
510 sal_Int16( 2 ) );
511 }
512 else if ( aUri.isDocument() )
513 {
514 throw lang::IllegalArgumentException(
515 u"A document never is a stream!"_ustr,
516 uno::Reference< uno::XInterface >(),
517 sal_Int16( 2 ) );
518 }
519
520 sal_Int32 nOpenMode;
521 switch ( eMode )
522 {
523 case READ:
524 nOpenMode = embed::ElementModes::READ
525 | embed::ElementModes::NOCREATE
526 | embed::ElementModes::SEEKABLE;
527 break;
528
529 case READ_WRITE_NOCREATE:
530 nOpenMode = embed::ElementModes::READWRITE
531 | embed::ElementModes::NOCREATE
532 | embed::ElementModes::SEEKABLE;
533
534 if ( bTruncate )
535 nOpenMode |= embed::ElementModes::TRUNCATE;
536
537 break;
538
539 case READ_WRITE_CREATE:
540 nOpenMode = embed::ElementModes::READWRITE
541 | embed::ElementModes::SEEKABLE;
542
543 if ( bTruncate )
544 nOpenMode |= embed::ElementModes::TRUNCATE;
545
546 break;
547
548 default:
549 OSL_FAIL( "StorageElementFactory::queryStream : Unknown open mode!" );
550
551 throw embed::InvalidStorageException(
552 u"Unknown open mode!"_ustr,
553 uno::Reference< uno::XInterface >() );
554 }
555
556 // No object re-usage mechanism; streams are seekable => not stateless.
557
558 uno::Reference< io::XStream > xStream;
559 if ( !rPassword.isEmpty() )
560 {
561 if ( eMode == READ )
562 {
563 try
564 {
565 xStream = xParentStorage->cloneEncryptedStreamElement(
566 aUri.getDecodedName(),
567 rPassword );
568 }
569 catch ( packages::NoEncryptionException const & )
570 {
571 xStream
572 = xParentStorage->cloneStreamElement( aUri.getDecodedName() );
573 }
574 }
575 else
576 {
577 try
578 {
579 xStream = xParentStorage->openEncryptedStreamElement(
580 aUri.getDecodedName(),
581 nOpenMode,
582 rPassword );
583 }
584 catch ( packages::NoEncryptionException const & )
585 {
586 xStream
587 = xParentStorage->openStreamElement( aUri.getDecodedName(),
588 nOpenMode );
589 }
590 }
591 }
592 else
593 {
594 if ( eMode == READ )
595 {
596 xStream = xParentStorage->cloneStreamElement( aUri.getDecodedName() );
597 }
598 else
599 {
600 xStream = xParentStorage->openStreamElement( aUri.getDecodedName(),
601 nOpenMode );
602 }
603 }
604
605 if ( !xStream.is() )
606 {
607 throw embed::InvalidStorageException(
608 u"No stream!"_ustr,
609 uno::Reference< uno::XInterface >() );
610 }
611
612 return xStream;
613 }
614
615 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */