1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sot/storinfo.hxx>
22 #include <osl/file.hxx>
23 #include <unotools/tempfile.hxx>
24 #include <tools/stream.hxx>
25 #include <tools/debug.hxx>
27 #include <sot/stg.hxx>
28 #include "stgelem.hxx"
33 static long nTmpCount
= 0;
35 // The internal open mode is StreamMode::READ | StreamMode::TRUNC, which is silly
36 // by itself. It inhibits the checking of sharing modes and is used
37 // during CopyTo() and MoveTo() for opening a stream in read mode
38 // although it may be open in DENYALL mode
40 #define INTERNAL_MODE ( StreamMode::READ | StreamMode::TRUNC )
42 ///////////////////////// class StorageBase
45 StorageBase::StorageBase()
46 : m_bAutoCommit( false )
48 m_nMode
= StreamMode::READ
;
49 m_nError
= ERRCODE_NONE
;
52 StorageBase::~StorageBase()
56 // The following three methods are declared as const, since they
57 // may be called from within a const method.
59 ErrCode
StorageBase::GetError() const
61 const ErrCode n
= m_nError
;
62 m_nError
= ERRCODE_NONE
;
66 void StorageBase::SetError( ErrCode n
) const
72 void StorageBase::ResetError() const
74 m_nError
= ERRCODE_NONE
;
77 OLEStorageBase::OLEStorageBase( StgIo
* p
, StgDirEntry
* pe
, StreamMode
& nMode
)
78 : nStreamMode( nMode
), pIo( p
), pEntry( pe
)
86 OLEStorageBase::~OLEStorageBase()
90 DBG_ASSERT( pEntry
->m_nRefCnt
, "RefCount under 0" );
91 if( !--pEntry
->m_nRefCnt
)
93 if( pEntry
->m_bZombie
)
103 if( pIo
&& !pIo
->DecRef() )
110 // Validate the instance for I/O
112 bool OLEStorageBase::Validate_Impl( bool bWrite
) const
117 && !pEntry
->m_bInvalid
118 && ( !bWrite
|| !pEntry
->m_bDirect
|| ( nStreamMode
& StreamMode::WRITE
) );
121 bool OLEStorageBase::ValidateMode_Impl( StreamMode m
, StgDirEntry
const * p
)
123 if( m
== INTERNAL_MODE
)
125 StreamMode nCurMode
= ( p
&& p
->m_nRefCnt
) ? p
->m_nMode
: StreamMode::SHARE_DENYALL
;
126 if( ( m
& StreamMode::READWRITE
) == StreamMode::READ
)
128 // only SHARE_DENYWRITE or SHARE_DENYALL allowed
129 if( ( ( m
& StreamMode::SHARE_DENYWRITE
)
130 && ( nCurMode
& StreamMode::SHARE_DENYWRITE
) )
131 || ( ( m
& StreamMode::SHARE_DENYALL
)
132 && ( nCurMode
& StreamMode::SHARE_DENYALL
) ) )
137 // only SHARE_DENYALL allowed
138 // storages open in r/o mode are OK, since only
139 // the commit may fail
140 if( ( m
& StreamMode::SHARE_DENYALL
)
141 && ( nCurMode
& StreamMode::SHARE_DENYALL
) )
148 //////////////////////// class StorageStream
151 StorageStream::StorageStream( StgIo
* p
, StgDirEntry
* q
, StreamMode m
)
152 : OLEStorageBase( p
, q
, m_nMode
), nPos( 0 )
154 // The dir entry may be 0; this means that the stream is invalid.
157 if( q
->m_nRefCnt
== 1 )
164 m
&= ~StreamMode::READWRITE
;
168 StorageStream::~StorageStream()
170 // Do an auto-commit if the entry is open in direct mode
173 if( pEntry
&& pEntry
->m_nRefCnt
&& pEntry
->m_bDirect
&& (m_nMode
& StreamMode::WRITE
) )
177 bool StorageStream::Equals( const BaseStorageStream
& rStream
) const
179 const StorageStream
* pOther
= dynamic_cast<const StorageStream
*>( &rStream
);
180 return pOther
&& ( pOther
->pEntry
== pEntry
);
183 sal_uLong
StorageStream::Read( void* pData
, sal_uLong nSize
)
187 pEntry
->Seek( nPos
);
188 nSize
= pEntry
->Read( pData
, static_cast<sal_Int32
>(nSize
) );
189 pIo
->MoveError( *this );
197 sal_uLong
StorageStream::Write( const void* pData
, sal_uLong nSize
)
199 if( Validate( true ) )
201 pEntry
->Seek( nPos
);
202 nSize
= pEntry
->Write( pData
, static_cast<sal_Int32
>(nSize
) );
203 pIo
->MoveError( *this );
211 sal_uInt64
StorageStream::Seek( sal_uInt64 n
)
214 return nPos
= pEntry
->Seek( n
);
219 void StorageStream::Flush()
221 // Flushing means committing, since streams are never transacted
225 bool StorageStream::SetSize( sal_uLong nNewSize
)
227 if( Validate( true ) )
229 bool b
= pEntry
->SetSize( static_cast<sal_Int32
>(nNewSize
) );
230 pIo
->MoveError( *this );
237 sal_uLong
StorageStream::GetSize() const
240 return pEntry
->GetSize();
244 bool StorageStream::Commit()
248 if( !( m_nMode
& StreamMode::WRITE
) )
250 SetError( SVSTREAM_ACCESS_DENIED
);
256 pIo
->MoveError( *this );
261 void StorageStream::CopyTo( BaseStorageStream
* pDest
)
263 if( !Validate() || !pDest
|| !pDest
->Validate( true ) || Equals( *pDest
) )
265 pEntry
->Copy( *pDest
);
267 pIo
->MoveError( *this );
268 SetError( pDest
->GetError() );
271 bool StorageStream::Validate( bool bValidate
) const
273 bool bRet
= Validate_Impl( bValidate
);
275 SetError( SVSTREAM_ACCESS_DENIED
);
279 bool StorageStream::ValidateMode( StreamMode nMode
) const
281 bool bRet
= ValidateMode_Impl( nMode
);
283 SetError( SVSTREAM_ACCESS_DENIED
);
287 ///////////////////////// class SvStorageInfo
289 SvStorageInfo::SvStorageInfo( const StgDirEntry
& rE
)
291 rE
.m_aEntry
.GetName( aName
);
292 bStorage
= rE
.m_aEntry
.GetType() == STG_STORAGE
;
293 bStream
= rE
.m_aEntry
.GetType() == STG_STREAM
;
294 nSize
= bStorage
? 0 : rE
.m_aEntry
.GetSize();
297 /////////////////////////// class Storage
299 bool Storage::IsStorageFile( const OUString
& rFileName
)
302 if( aIo
.Open( rFileName
, StreamMode::STD_READ
) )
307 bool Storage::IsStorageFile( SvStream
* pStream
)
314 sal_uInt64 nPos
= pStream
->Tell();
315 bRet
= ( aHdr
.Load( *pStream
) && aHdr
.Check() );
317 // It's not a stream error if it is too small for an OLE storage header
318 if ( pStream
->GetErrorCode() == ERRCODE_IO_CANTSEEK
)
319 pStream
->ResetError();
320 pStream
->Seek( nPos
);
326 // Open the storage file. If writing is permitted and the file is not
327 // a storage file, initialize it.
330 Storage::Storage( const OUString
& rFile
, StreamMode m
, bool bDirect
)
331 : OLEStorageBase( new StgIo
, nullptr, m_nMode
)
332 , aName( rFile
), bIsRoot( false )
335 if( aName
.isEmpty() )
337 // no name = temporary name!
338 aName
= utl::TempFile::CreateTempName();
341 // the root storage creates the I/O system
343 if( pIo
->Open( aName
, m
) )
345 Init( ( m
& ( StreamMode::TRUNC
| StreamMode::NOCREATE
) ) == StreamMode::TRUNC
);
348 pEntry
->m_bDirect
= bDirect
;
350 pEntry
->m_bTemp
= bTemp
;
355 pIo
->MoveError( *this );
360 // Create a storage on a given stream.
362 Storage::Storage( SvStream
& r
, bool bDirect
)
363 : OLEStorageBase( new StgIo
, nullptr, m_nMode
)
366 m_nMode
= StreamMode::READ
;
368 m_nMode
= StreamMode::READ
| StreamMode::WRITE
;
369 if( r
.GetError() == ERRCODE_NONE
)
371 pIo
->SetStrm( &r
, false );
372 sal_uInt64 nSize
= r
.TellEnd();
374 // Initializing is OK if the stream is empty
378 pEntry
->m_bDirect
= bDirect
;
379 pEntry
->m_nMode
= m_nMode
;
381 pIo
->MoveError( *this );
385 SetError( r
.GetError() );
391 Storage::Storage( UCBStorageStream
& rStrm
, bool bDirect
)
392 : OLEStorageBase( new StgIo
, nullptr, m_nMode
), bIsRoot( false )
394 m_nMode
= StreamMode::READ
;
396 if ( rStrm
.GetError() != ERRCODE_NONE
)
398 SetError( rStrm
.GetError() );
403 SvStream
* pStream
= rStrm
.GetModifySvStream();
406 OSL_FAIL( "UCBStorageStream can not provide SvStream implementation!" );
407 SetError( SVSTREAM_GENERALERROR
);
412 if( pStream
->IsWritable() )
413 m_nMode
= StreamMode::READ
| StreamMode::WRITE
;
415 pIo
->SetStrm( &rStrm
);
417 sal_uInt64 nSize
= pStream
->TellEnd();
419 // Initializing is OK if the stream is empty
423 pEntry
->m_bDirect
= bDirect
;
424 pEntry
->m_nMode
= m_nMode
;
427 pIo
->MoveError( *this );
431 // Perform common code for both ctors above.
433 void Storage::Init( bool bCreate
)
436 bool bHdrLoaded
= false;
439 OSL_ENSURE( pIo
, "The pointer may not be empty at this point!" );
440 if( pIo
->Good() && pIo
->GetStrm() )
442 sal_uInt64 nSize
= pIo
->GetStrm()->TellEnd();
443 pIo
->GetStrm()->Seek( 0 );
446 bHdrLoaded
= pIo
->Load();
447 if( !bHdrLoaded
&& !bCreate
)
449 // File is not a storage and not empty; do not destroy!
450 SetError( SVSTREAM_FILEFORMAT_ERROR
);
455 // file is a storage, empty or should be overwritten
457 // we have to set up the data structures, since
461 if( pIo
->Good() && pIo
->m_pTOC
)
463 pEntry
= pIo
->m_pTOC
->GetRoot();
470 Storage::Storage( StgIo
* p
, StgDirEntry
* q
, StreamMode m
)
471 : OLEStorageBase( p
, q
, m_nMode
), bIsRoot( false )
474 q
->m_aEntry
.GetName( aName
);
476 m
&= ~StreamMode::READWRITE
;
478 if( q
&& q
->m_nRefCnt
== 1 )
484 // Invalidate all open substorages
489 // Do an auto-commit if the entry is open in direct mode
490 if( pEntry
->m_nRefCnt
&& pEntry
->m_bDirect
&& (m_nMode
& StreamMode::WRITE
) )
492 if( pEntry
->m_nRefCnt
== 1 )
493 pEntry
->Invalidate(false);
495 // close the stream is root storage
498 // remove the file if temporary root storage
499 if( bIsRoot
&& pEntry
&& pEntry
->m_bTemp
)
501 osl::File::remove( GetName() );
505 const OUString
& Storage::GetName() const
507 if( !bIsRoot
&& Validate() )
508 pEntry
->m_aEntry
.GetName( const_cast<Storage
*>(this)->aName
);
512 // Fill in the info list for this storage
514 void Storage::FillInfoList( SvStorageInfoList
* pList
) const
516 if( !(Validate() && pList
) )
519 StgIterator
aIter( *pEntry
);
520 StgDirEntry
* p
= aIter
.First();
525 SvStorageInfo
aInfo( *p
);
526 pList
->push_back( aInfo
);
532 // Open or create a substorage
534 BaseStorage
* Storage::OpenUCBStorage( const OUString
& rName
, StreamMode m
, bool bDirect
)
536 OSL_FAIL("Not supported!");
537 return OpenStorage( rName
, m
, bDirect
);
540 BaseStorage
* Storage::OpenOLEStorage( const OUString
& rName
, StreamMode m
, bool bDirect
)
542 return OpenStorage( rName
, m
, bDirect
);
545 BaseStorage
* Storage::OpenStorage( const OUString
& rName
, StreamMode m
, bool bDirect
)
547 if( !Validate() || !ValidateMode( m
) )
548 return new Storage( pIo
, nullptr, m
);
549 if( bDirect
&& !pEntry
->m_bDirect
)
552 StgDirEntry
* p
= StgDirStrm::Find( *pEntry
, rName
);
555 if( !( m
& StreamMode::NOCREATE
) )
558 // create a new storage
559 OUString aNewName
= rName
;
560 if( aNewName
.isEmpty() )
562 aNewName
= "Temp Stg " + OUString::number( ++nTmpCount
);
565 p
= pIo
->m_pTOC
->Create( *pEntry
, aNewName
, STG_STORAGE
);
570 pIo
->SetError( ( m
& StreamMode::WRITE
)
571 ? SVSTREAM_CANNOT_MAKE
: SVSTREAM_FILE_NOT_FOUND
);
573 else if( !ValidateMode( m
, p
) )
575 if( p
&& p
->m_aEntry
.GetType() != STG_STORAGE
)
577 pIo
->SetError( SVSTREAM_FILE_NOT_FOUND
);
581 // Either direct or transacted mode is supported
582 if( p
&& pEntry
->m_nRefCnt
== 1 )
583 p
->m_bDirect
= bDirect
;
585 // Don't check direct conflict if opening readonly
586 if( p
&& (m
& StreamMode::WRITE
))
588 if( p
->m_bDirect
!= bDirect
)
589 SetError( SVSTREAM_ACCESS_DENIED
);
591 Storage
* pStg
= new Storage( pIo
, p
, m
);
592 pIo
->MoveError( *pStg
);
593 if( m
& StreamMode::WRITE
) pStg
->m_bAutoCommit
= true;
599 BaseStorageStream
* Storage::OpenStream( const OUString
& rName
, StreamMode m
, bool )
601 if( !Validate() || !ValidateMode( m
) )
602 return new StorageStream( pIo
, nullptr, m
);
603 StgDirEntry
* p
= StgDirStrm::Find( *pEntry
, rName
);
607 if( !( m
& StreamMode::NOCREATE
) )
609 // create a new stream
610 // make a name if the stream is temporary (has no name)
611 OUString
aNewName( rName
);
612 if( aNewName
.isEmpty() )
614 aNewName
= "Temp Strm " + OUString::number( ++nTmpCount
);
617 p
= pIo
->m_pTOC
->Create( *pEntry
, aNewName
, STG_STREAM
);
620 pIo
->SetError( ( m
& StreamMode::WRITE
)
621 ? SVSTREAM_CANNOT_MAKE
: SVSTREAM_FILE_NOT_FOUND
);
623 else if( !ValidateMode( m
, p
) )
625 if( p
&& p
->m_aEntry
.GetType() != STG_STREAM
)
627 pIo
->SetError( SVSTREAM_FILE_NOT_FOUND
);
633 p
->m_bDirect
= pEntry
->m_bDirect
;
635 StorageStream
* pStm
= new StorageStream( pIo
, p
, m
);
636 if( p
&& !p
->m_bDirect
)
637 pStm
->SetAutoCommit( true );
638 pIo
->MoveError( *pStm
);
642 // Delete a stream or substorage by setting the temp bit.
644 void Storage::Remove( const OUString
& rName
)
646 if( !Validate( true ) )
648 StgDirEntry
* p
= StgDirStrm::Find( *pEntry
, rName
);
651 p
->Invalidate( true );
655 SetError( SVSTREAM_FILE_NOT_FOUND
);
661 bool Storage::CopyTo( const OUString
& rElem
, BaseStorage
* pDest
, const OUString
& rNew
)
663 if( !Validate() || !pDest
|| !pDest
->Validate( true ) )
665 StgDirEntry
* pElem
= StgDirStrm::Find( *pEntry
, rElem
);
668 if( pElem
->m_aEntry
.GetType() == STG_STORAGE
)
670 // copy the entire storage
671 tools::SvRef
<BaseStorage
> p1
= OpenStorage( rElem
, INTERNAL_MODE
);
672 tools::SvRef
<BaseStorage
> p2
= pDest
->OpenOLEStorage( rNew
, StreamMode::WRITE
| StreamMode::SHARE_DENYALL
, pEntry
->m_bDirect
);
676 ErrCode nTmpErr
= p2
->GetError();
679 p2
->SetClassId( p1
->GetClassId() );
680 p1
->CopyTo( p2
.get() );
681 SetError( p1
->GetError() );
683 nTmpErr
= p2
->GetError();
687 pDest
->SetError( nTmpErr
);
690 pDest
->SetError( nTmpErr
);
693 return Good() && pDest
->Good();
698 tools::SvRef
<BaseStorageStream
> p1
= OpenStream( rElem
, INTERNAL_MODE
);
699 tools::SvRef
<BaseStorageStream
> p2
= pDest
->OpenStream( rNew
, StreamMode::WRITE
| StreamMode::SHARE_DENYALL
, pEntry
->m_bDirect
);
703 ErrCode nTmpErr
= p2
->GetError();
706 p1
->CopyTo( p2
.get() );
707 SetError( p1
->GetError() );
709 nTmpErr
= p2
->GetError();
713 pDest
->SetError( nTmpErr
);
716 pDest
->SetError( nTmpErr
);
719 return Good() && pDest
->Good();
722 SetError( SVSTREAM_FILE_NOT_FOUND
);
726 bool Storage::CopyTo( BaseStorage
* pDest
) const
728 if( !Validate() || !pDest
|| !pDest
->Validate( true ) || Equals( *pDest
) )
730 SetError( SVSTREAM_ACCESS_DENIED
);
733 Storage
* pThis
= const_cast<Storage
*>(this);
734 pDest
->SetClassId( GetClassId() );
736 SvStorageInfoList aList
;
737 FillInfoList( &aList
);
739 for( size_t i
= 0; i
< aList
.size() && bRes
; i
++ )
741 SvStorageInfo
& rInfo
= aList
[ i
];
742 bRes
= pThis
->CopyTo( rInfo
.GetName(), pDest
, rInfo
.GetName() );
745 SetError( pDest
->GetError() );
746 return Good() && pDest
->Good();
749 bool Storage::IsStorage( const OUString
& rName
) const
753 StgDirEntry
* p
= StgDirStrm::Find( *pEntry
, rName
);
755 return p
->m_aEntry
.GetType() == STG_STORAGE
;
760 bool Storage::IsStream( const OUString
& rName
) const
764 StgDirEntry
* p
= StgDirStrm::Find( *pEntry
, rName
);
766 return p
->m_aEntry
.GetType() == STG_STREAM
;
771 bool Storage::IsContained( const OUString
& rName
) const
774 return StgDirStrm::Find( *pEntry
, rName
) != nullptr;
779 // Commit all sub-elements within this storage. If this is
780 // the root, commit the FAT, the TOC and the header as well.
782 bool Storage::Commit()
787 if( !( m_nMode
& StreamMode::WRITE
) )
789 SetError( SVSTREAM_ACCESS_DENIED
);
794 // Also commit the sub-streams and Storages
795 StgIterator
aIter( *pEntry
);
796 for( StgDirEntry
* p
= aIter
.First(); p
&& bRes
; p
= aIter
.Next() )
798 if( bRes
&& bIsRoot
)
800 bRes
= pEntry
->Commit();
802 bRes
= pIo
->CommitAll();
804 pIo
->MoveError( *this );
809 bool Storage::Revert()
814 ///////////////////////////// OLE Support
816 // Set the storage type
818 void Storage::SetClass( const SvGlobalName
& rClass
,
819 SotClipboardFormatId nOriginalClipFormat
,
820 const OUString
& rUserTypeName
)
822 if( Validate( true ) )
824 // set the class name in the root entry
825 pEntry
->m_aEntry
.SetClassId( rClass
.GetCLSID() );
827 // then create the streams
828 StgCompObjStream
aCompObj( *this, true );
829 aCompObj
.GetClsId() = rClass
.GetCLSID();
830 aCompObj
.GetCbFormat() = nOriginalClipFormat
;
831 aCompObj
.GetUserName() = rUserTypeName
;
832 if( !aCompObj
.Store() )
833 SetError( aCompObj
.GetError() );
836 StgOleStream
aOle(*this);
838 SetError( aOle
.GetError() );
842 SetError( SVSTREAM_ACCESS_DENIED
);
845 SvGlobalName
Storage::GetClassName()
847 StgCompObjStream
aCompObj( *this, false );
848 if( aCompObj
.Load() )
849 return SvGlobalName( aCompObj
.GetClsId() );
853 return SvGlobalName( pEntry
->m_aEntry
.GetClassId() );
855 return SvGlobalName();
858 SotClipboardFormatId
Storage::GetFormat()
860 StgCompObjStream
aCompObj( *this, false );
861 if( aCompObj
.Load() )
862 return aCompObj
.GetCbFormat();
864 return SotClipboardFormatId::NONE
;
867 OUString
Storage::GetUserName()
869 StgCompObjStream
aCompObj( *this, false );
870 if( aCompObj
.Load() )
871 return aCompObj
.GetUserName();
876 bool Storage::ValidateFAT()
878 FatError nErr
= pIo
->ValidateFATs();
879 return nErr
== FatError::Ok
;
882 void Storage::SetDirty()
888 void Storage::SetClassId( const ClsId
& rId
)
891 pEntry
->m_aEntry
.SetClassId( rId
);
894 const ClsId
& Storage::GetClassId() const
897 return pEntry
->m_aEntry
.GetClassId();
899 static const ClsId aDummyId
= {0,0,0,{0,0,0,0,0,0,0,0}};
903 bool Storage::Validate( bool bValidate
) const
905 bool bRet
= Validate_Impl( bValidate
);
907 SetError( SVSTREAM_ACCESS_DENIED
);
911 bool Storage::ValidateMode( StreamMode nMode
) const
913 bool bRet
= ValidateMode_Impl( nMode
);
915 SetError( SVSTREAM_ACCESS_DENIED
);
919 bool Storage::ValidateMode( StreamMode nMode
, StgDirEntry
const * p
) const
921 bool bRet
= ValidateMode_Impl( nMode
, p
);
923 SetError( SVSTREAM_ACCESS_DENIED
);
927 bool Storage::Equals( const BaseStorage
& rStorage
) const
929 const Storage
* pOther
= dynamic_cast<const Storage
*>( &rStorage
);
930 return pOther
&& ( pOther
->pEntry
== pEntry
);
933 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */