diff options
-rw-r--r-- | comphelper/inc/comphelper/storagehelper.hxx | 2 | ||||
-rw-r--r-- | comphelper/source/misc/storagehelper.cxx | 12 | ||||
-rw-r--r-- | svtools/inc/svtools/documentlockfile.hxx | 23 | ||||
-rw-r--r-- | svtools/inc/svtools/lockfilecommon.hxx | 84 | ||||
-rw-r--r-- | svtools/inc/svtools/sharecontrolfile.hxx | 29 | ||||
-rw-r--r-- | svtools/prj/d.lst | 1 | ||||
-rw-r--r-- | svtools/source/misc/documentlockfile.cxx | 155 | ||||
-rw-r--r-- | svtools/source/misc/lockfilecommon.cxx | 276 | ||||
-rw-r--r-- | svtools/source/misc/makefile.mk | 1 | ||||
-rw-r--r-- | svtools/source/misc/sharecontrolfile.cxx | 161 |
10 files changed, 396 insertions, 348 deletions
diff --git a/comphelper/inc/comphelper/storagehelper.hxx b/comphelper/inc/comphelper/storagehelper.hxx index fb0e955d496c..b99f7e1233ca 100644 --- a/comphelper/inc/comphelper/storagehelper.hxx +++ b/comphelper/inc/comphelper/storagehelper.hxx @@ -162,6 +162,8 @@ public: = ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() ) throw ( ::com::sun::star::uno::Exception ); + static sal_Bool IsValidZipEntryFileName( const ::rtl::OUString& aName, sal_Bool bSlashAllowed ); + static sal_Bool IsValidZipEntryFileName( const sal_Unicode *pChar, sal_Int32 nLength, sal_Bool bSlashAllowed ); }; } diff --git a/comphelper/source/misc/storagehelper.cxx b/comphelper/source/misc/storagehelper.cxx index 667d1bdaa20a..7d63ed7a43f5 100644 --- a/comphelper/source/misc/storagehelper.cxx +++ b/comphelper/source/misc/storagehelper.cxx @@ -400,12 +400,17 @@ uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromStream( return xTempStorage; } +// ---------------------------------------------------------------------- +sal_Bool OStorageHelper::IsValidZipEntryFileName( const ::rtl::OUString& aName, sal_Bool bSlashAllowed ) +{ + return IsValidZipEntryFileName( aName.getStr(), aName.getLength(), bSlashAllowed ); +} // ---------------------------------------------------------------------- -sal_Bool IsValidZipEntryFileName( +sal_Bool OStorageHelper::IsValidZipEntryFileName( const sal_Unicode *pChar, sal_Int32 nLength, sal_Bool bSlashAllowed ) { - for ( sal_Int32 i = 0 ; i < nLength ; i++ ) + for ( sal_Int32 i = 0; i < nLength; i++ ) { switch ( pChar[i] ) { @@ -422,9 +427,8 @@ sal_Bool IsValidZipEntryFileName( return sal_False; break; default: - if ( pChar[i] < 32 || pChar[i] > 127 ) + if ( pChar[i] < 32 || pChar[i] >= 0xD800 && pChar[i] <= 0xDFFF ) return sal_False; -// Note: in case this ever supports unicode, watch out for surrogate pairs! } } return sal_True; diff --git a/svtools/inc/svtools/documentlockfile.hxx b/svtools/inc/svtools/documentlockfile.hxx index 73e64f4f04a5..f2625f7955bb 100644 --- a/svtools/inc/svtools/documentlockfile.hxx +++ b/svtools/inc/svtools/documentlockfile.hxx @@ -41,44 +41,25 @@ #include <com/sun/star/io/XTruncate.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> -#include <osl/mutex.hxx> - -// TODO/LATER: should be combined with sharecontrolfile -#define LOCKFILE_OOOUSERNAME_ID 0 -#define LOCKFILE_SYSUSERNAME_ID 1 -#define LOCKFILE_LOCALHOST_ID 2 -#define LOCKFILE_EDITTIME_ID 3 -#define LOCKFILE_USERURL_ID 4 -#define LOCKFILE_ENTRYSIZE 5 +#include <svtools/lockfilecommon.hxx> namespace svt { -class SVT_DLLPUBLIC DocumentLockFile +class SVT_DLLPUBLIC DocumentLockFile : public LockFileCommon { // the workaround for automated testing! static sal_Bool m_bAllowInteraction; - ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory; - ::rtl::OUString m_aURL; - - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > OpenStream(); void WriteEntryToStream( ::com::sun::star::uno::Sequence< ::rtl::OUString > aEntry, ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > xStream ); - ::com::sun::star::uno::Sequence< ::rtl::OUString > ParseEntry( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer ); - ::rtl::OUString ParseName( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ); - ::rtl::OUString EscapeCharacters( const ::rtl::OUString& aSource ); - ::rtl::OUString GetOOOUserName(); - ::rtl::OUString GetCurrentLocalTime(); - public: DocumentLockFile( const ::rtl::OUString& aOrigURL, const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory = ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() ); ~DocumentLockFile(); sal_Bool CreateOwnLockFile(); ::com::sun::star::uno::Sequence< ::rtl::OUString > GetLockData(); - ::com::sun::star::uno::Sequence< ::rtl::OUString > GenerateOwnEntry(); sal_Bool OverwriteOwnLockFile(); void RemoveFile(); diff --git a/svtools/inc/svtools/lockfilecommon.hxx b/svtools/inc/svtools/lockfilecommon.hxx new file mode 100644 index 000000000000..c8bd6251f9cb --- /dev/null +++ b/svtools/inc/svtools/lockfilecommon.hxx @@ -0,0 +1,84 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documentlockfile.hxx,v $ + * + * $Revision: 1.3 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _SVT_LOCKFILECOMMON_HXX +#define _SVT_LOCKFILECOMMON_HXX + +#include <svtools/svtdllapi.h> + +#include <com/sun/star/io/XStream.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XSeekable.hpp> +#include <com/sun/star/io/XTruncate.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> + +#include <osl/mutex.hxx> +#include <tools/urlobj.hxx> + +#define LOCKFILE_OOOUSERNAME_ID 0 +#define LOCKFILE_SYSUSERNAME_ID 1 +#define LOCKFILE_LOCALHOST_ID 2 +#define LOCKFILE_EDITTIME_ID 3 +#define LOCKFILE_USERURL_ID 4 +#define LOCKFILE_ENTRYSIZE 5 + +namespace svt { + +// This is a general implementation that is used in document lock file implementation and in sharing control file implementation +class SVT_DLLPUBLIC LockFileCommon +{ +protected: + ::osl::Mutex m_aMutex; + + ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory; + ::rtl::OUString m_aURL; + + + INetURLObject ResolveLinks( const INetURLObject& aDocURL ); + +public: + LockFileCommon( const ::rtl::OUString& aOrigURL, const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, const ::rtl::OUString& aPrefix ); + ~LockFileCommon(); + + static ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > > ParseList( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer ); + static ::com::sun::star::uno::Sequence< ::rtl::OUString > ParseEntry( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ); + static ::rtl::OUString ParseName( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ); + static ::rtl::OUString EscapeCharacters( const ::rtl::OUString& aSource ); + static ::rtl::OUString GetOOOUserName(); + static ::rtl::OUString GetCurrentLocalTime(); + static ::com::sun::star::uno::Sequence< ::rtl::OUString > GenerateOwnEntry(); +}; + +} + +#endif + diff --git a/svtools/inc/svtools/sharecontrolfile.hxx b/svtools/inc/svtools/sharecontrolfile.hxx index 2ab0d48a72f9..8735f8c5ccf7 100644 --- a/svtools/inc/svtools/sharecontrolfile.hxx +++ b/svtools/inc/svtools/sharecontrolfile.hxx @@ -40,24 +40,19 @@ #include <com/sun/star/io/XTruncate.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> -#include <osl/mutex.hxx> +#include <svtools/lockfilecommon.hxx> -#define SHARED_OOOUSERNAME_ID 0 -#define SHARED_SYSUSERNAME_ID 1 -#define SHARED_LOCALHOST_ID 2 -#define SHARED_EDITTIME_ID 3 -#define SHARED_USERURL_ID 4 -#define SHARED_ENTRYSIZE 5 +#define SHARED_OOOUSERNAME_ID LOCKFILE_OOOUSERNAME_ID +#define SHARED_SYSUSERNAME_ID LOCKFILE_SYSUSERNAME_ID +#define SHARED_LOCALHOST_ID LOCKFILE_LOCALHOST_ID +#define SHARED_EDITTIME_ID LOCKFILE_EDITTIME_ID +#define SHARED_USERURL_ID LOCKFILE_USERURL_ID +#define SHARED_ENTRYSIZE LOCKFILE_ENTRYSIZE namespace svt { -class SVT_DLLPUBLIC ShareControlFile +class SVT_DLLPUBLIC ShareControlFile : public LockFileCommon { - ::osl::Mutex m_aMutex; - ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory; - - ::rtl::OUString m_aURL; - ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > m_xStream; ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > m_xInputStream; ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > m_xOutputStream; @@ -73,14 +68,6 @@ class SVT_DLLPUBLIC ShareControlFile return ( m_xFactory.is() && m_xStream.is() && m_xInputStream.is() && m_xOutputStream.is() && m_xSeekable.is() && m_xTruncate.is() ); } - ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > > ParseList( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer ); - ::com::sun::star::uno::Sequence< ::rtl::OUString > ParseEntry( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ); - ::rtl::OUString ParseName( const ::com::sun::star::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ); - ::rtl::OUString EscapeCharacters( const ::rtl::OUString& aSource ); - ::rtl::OUString GetOOOUserName(); - ::rtl::OUString GetCurrentLocalTime(); - ::com::sun::star::uno::Sequence< ::rtl::OUString > GenerateOwnEntry(); - public: // The constructor will throw exception in case the stream can not be opened diff --git a/svtools/prj/d.lst b/svtools/prj/d.lst index bd3f87f0e2b3..6b85194db6c1 100644 --- a/svtools/prj/d.lst +++ b/svtools/prj/d.lst @@ -110,6 +110,7 @@ mkdir: %_DEST%\inc%_EXT%\svtools ..\inc\scrwin.hxx %_DEST%\inc%_EXT%\svtools\scrwin.hxx ..\inc\svtools\sharecontrolfile.hxx %_DEST%\inc%_EXT%\svtools\sharecontrolfile.hxx ..\inc\svtools\documentlockfile.hxx %_DEST%\inc%_EXT%\svtools\documentlockfile.hxx +..\inc\svtools\lockfilecommon.hxx %_DEST%\inc%_EXT%\svtools\lockfilecommon.hxx ..\inc\svtools\slstitm.hxx %_DEST%\inc%_EXT%\svtools\slstitm.hxx ..\inc\svtools\ilstitem.hxx %_DEST%\inc%_EXT%\svtools\ilstitem.hxx ..\inc\svtools\smplhint.hxx %_DEST%\inc%_EXT%\svtools\smplhint.hxx diff --git a/svtools/source/misc/documentlockfile.cxx b/svtools/source/misc/documentlockfile.cxx index c8904ef010a4..71b541cfe894 100644 --- a/svtools/source/misc/documentlockfile.cxx +++ b/svtools/source/misc/documentlockfile.cxx @@ -68,20 +68,8 @@ sal_Bool DocumentLockFile::m_bAllowInteraction = sal_True; // ---------------------------------------------------------------------- DocumentLockFile::DocumentLockFile( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory ) -: m_xFactory( xFactory ) +: LockFileCommon( aOrigURL, xFactory, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~lock." ) ) ) { - if ( !m_xFactory.is() ) - m_xFactory = ::comphelper::getProcessServiceFactory(); - - INetURLObject aDocURL( aOrigURL ); - if ( aDocURL.HasError() ) - throw lang::IllegalArgumentException(); - - ::rtl::OUString aShareURLString = aDocURL.GetPartBeforeLastName(); - aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~lock." ) ); - aShareURLString += aDocURL.GetName(); - aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "#" ) ); - m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE ); } // ---------------------------------------------------------------------- @@ -92,6 +80,8 @@ DocumentLockFile::~DocumentLockFile() // ---------------------------------------------------------------------- void DocumentLockFile::WriteEntryToStream( uno::Sequence< ::rtl::OUString > aEntry, uno::Reference< io::XOutputStream > xOutput ) { + ::osl::MutexGuard aGuard( m_aMutex ); + ::rtl::OUStringBuffer aBuffer; for ( sal_Int32 nEntryInd = 0; nEntryInd < aEntry.getLength(); nEntryInd++ ) @@ -111,6 +101,8 @@ void DocumentLockFile::WriteEntryToStream( uno::Sequence< ::rtl::OUString > aEnt // ---------------------------------------------------------------------- sal_Bool DocumentLockFile::CreateOwnLockFile() { + ::osl::MutexGuard aGuard( m_aMutex ); + try { uno::Reference< io::XStream > xTempFile( @@ -154,136 +146,10 @@ sal_Bool DocumentLockFile::CreateOwnLockFile() } // ---------------------------------------------------------------------- -uno::Sequence< ::rtl::OUString > DocumentLockFile::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer ) -{ - sal_Int32 nCurPos = 0; - - uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); - - for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ ) - { - aResult[nInd] = ParseName( aBuffer, nCurPos ); - if ( nCurPos >= aBuffer.getLength() - || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[nCurPos++] != ',' ) - || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[nCurPos++] != ';' ) ) - throw io::WrongFormatException(); - } - - return aResult; -} - -// ---------------------------------------------------------------------- -::rtl::OUString DocumentLockFile::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ) -{ - ::rtl::OStringBuffer aResult; - sal_Bool bHaveName = sal_False; - sal_Bool bEscape = sal_False; - - while( !bHaveName ) - { - if ( o_nCurPos >= aBuffer.getLength() ) - throw io::WrongFormatException(); - - if ( bEscape ) - { - if ( aBuffer[o_nCurPos] == ',' || aBuffer[o_nCurPos] == ';' || aBuffer[o_nCurPos] == '\\' ) - aResult.append( (sal_Char)aBuffer[o_nCurPos] ); - else - throw io::WrongFormatException(); - - bEscape = sal_False; - o_nCurPos++; - } - else if ( aBuffer[o_nCurPos] == ',' || aBuffer[o_nCurPos] == ';' ) - bHaveName = sal_True; - else - { - if ( aBuffer[o_nCurPos] == '\\' ) - bEscape = sal_True; - else - aResult.append( (sal_Char)aBuffer[o_nCurPos] ); - - o_nCurPos++; - } - } - - return ::rtl::OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ); -} - -// ---------------------------------------------------------------------- -::rtl::OUString DocumentLockFile::EscapeCharacters( const ::rtl::OUString& aSource ) -{ - ::rtl::OUStringBuffer aBuffer; - const sal_Unicode* pStr = aSource.getStr(); - for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ ) - { - if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' ) - aBuffer.append( (sal_Unicode)'\\' ); - aBuffer.append( pStr[nInd] ); - } - - return aBuffer.makeStringAndClear(); -} - -// ---------------------------------------------------------------------- -::rtl::OUString DocumentLockFile::GetOOOUserName() -{ - SvtUserOptions aUserOpt; - ::rtl::OUString aName = aUserOpt.GetFirstName(); - if ( aName.getLength() ) - aName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ); - aName += aUserOpt.GetLastName(); - - return aName; -} - -// ---------------------------------------------------------------------- -::rtl::OUString DocumentLockFile::GetCurrentLocalTime() -{ - ::rtl::OUString aTime; - - TimeValue aSysTime; - if ( osl_getSystemTime( &aSysTime ) ) - { - TimeValue aLocTime; - if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) ) - { - oslDateTime aDateTime; - if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) ) - { - char pDateTime[20]; - sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes ); - aTime = ::rtl::OUString::createFromAscii( pDateTime ); - } - } - } - - return aTime; -} - -// ---------------------------------------------------------------------- -uno::Sequence< ::rtl::OUString > DocumentLockFile::GenerateOwnEntry() -{ - uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); - - aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName(); - - ::osl::Security aSecurity; - aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] ); - - aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname(); - - aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime(); - - ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] ); - - - return aResult; -} - -// ---------------------------------------------------------------------- uno::Sequence< ::rtl::OUString > DocumentLockFile::GetLockData() { + ::osl::MutexGuard aGuard( m_aMutex ); + uno::Reference< io::XInputStream > xInput = OpenStream(); if ( !xInput.is() ) throw uno::RuntimeException(); @@ -299,12 +165,15 @@ uno::Sequence< ::rtl::OUString > DocumentLockFile::GetLockData() if ( nRead == nBufLen ) throw io::WrongFormatException(); - return ParseEntry( aBuffer ); + sal_Int32 nCurPos = 0; + return ParseEntry( aBuffer, nCurPos ); } // ---------------------------------------------------------------------- uno::Reference< io::XInputStream > DocumentLockFile::OpenStream() { + ::osl::MutexGuard aGuard( m_aMutex ); + uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory(); uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > xSimpleFileAccess( xFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ), @@ -344,6 +213,8 @@ sal_Bool DocumentLockFile::OverwriteOwnLockFile() // ---------------------------------------------------------------------- void DocumentLockFile::RemoveFile() { + ::osl::MutexGuard aGuard( m_aMutex ); + // TODO/LATER: the removing is not atomar, is it possible in general to make it atomar? uno::Sequence< ::rtl::OUString > aNewEntry = GenerateOwnEntry(); uno::Sequence< ::rtl::OUString > aFileData = GetLockData(); diff --git a/svtools/source/misc/lockfilecommon.cxx b/svtools/source/misc/lockfilecommon.cxx new file mode 100644 index 000000000000..ef2cf89d8572 --- /dev/null +++ b/svtools/source/misc/lockfilecommon.cxx @@ -0,0 +1,276 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: ,v $ + * + * $Revision: $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_svtools.hxx" + +#include <stdio.h> + +#include <com/sun/star/ucb/XSimpleFileAccess.hpp> +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/ucb/InsertCommandArgument.hpp> +#include <com/sun/star/ucb/NameClashException.hpp> +#include <com/sun/star/io/WrongFormatException.hpp> + +#include <osl/time.h> +#include <osl/security.hxx> +#include <osl/socket.hxx> +#include <osl/file.hxx> + +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include <rtl/strbuf.hxx> +#include <rtl/ustrbuf.hxx> + +#include <comphelper/processfactory.hxx> + +#include <tools/urlobj.hxx> +#include <unotools/bootstrap.hxx> + +#include <ucbhelper/content.hxx> + +#include <svtools/useroptions.hxx> + +#include <svtools/lockfilecommon.hxx> + +using namespace ::com::sun::star; + +namespace svt { + +// ---------------------------------------------------------------------- +LockFileCommon::LockFileCommon( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory, const ::rtl::OUString& aPrefix ) +: m_xFactory( xFactory ) +{ + if ( !m_xFactory.is() ) + m_xFactory = ::comphelper::getProcessServiceFactory(); + + INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) ); + + ::rtl::OUString aShareURLString = aDocURL.GetPartBeforeLastName(); + aShareURLString += aPrefix; + aShareURLString += aDocURL.GetName(); + aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "#" ) ); + m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE ); +} + +// ---------------------------------------------------------------------- +LockFileCommon::~LockFileCommon() +{ +} + +// ---------------------------------------------------------------------- +INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL ) +{ + if ( aDocURL.HasError() ) + throw lang::IllegalArgumentException(); + + ::rtl::OUString aURLToCheck = aDocURL.GetMainURL( INetURLObject::NO_DECODE ); + + sal_Bool bNeedsChecking = sal_True; + sal_Int32 nMaxLinkCount = 128; + sal_Int32 nCount = 0; + + while( bNeedsChecking ) + { + bNeedsChecking = sal_False; + + // do not allow too deep links + if ( nCount++ >= nMaxLinkCount ) + throw io::IOException(); + + // there is currently no UCB functionality to resolve the symbolic links; + // since the lock files are used only for local file systems the osl functionality is used directly + + ::osl::FileStatus aStatus( FileStatusMask_Type | FileStatusMask_LinkTargetURL ); + ::osl::DirectoryItem aItem; + if ( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aURLToCheck, aItem ) + && aItem.is() && ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) ) + { + if ( aStatus.isValid( FileStatusMask_Type ) + && aStatus.isValid( FileStatusMask_LinkTargetURL ) + && aStatus.getFileType() == ::osl::FileStatus::Link ) + { + aURLToCheck = aStatus.getLinkTargetURL(); + bNeedsChecking = sal_True; + } + } + } + + return INetURLObject( aURLToCheck ); +} + +// ---------------------------------------------------------------------- +uno::Sequence< uno::Sequence< ::rtl::OUString > > LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer ) +{ + sal_Int32 nCurPos = 0; + sal_Int32 nCurEntry = 0; + uno::Sequence< uno::Sequence< ::rtl::OUString > > aResult( 10 ); + + while ( nCurPos < aBuffer.getLength() ) + { + if ( nCurEntry >= aResult.getLength() ) + aResult.realloc( nCurEntry + 10 ); + aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos ); + nCurEntry++; + } + + aResult.realloc( nCurEntry ); + return aResult; +} + +// ---------------------------------------------------------------------- +uno::Sequence< ::rtl::OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos ) +{ + uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); + + for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ ) + { + aResult[nInd] = ParseName( aBuffer, io_nCurPos ); + if ( io_nCurPos >= aBuffer.getLength() + || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' ) + || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) ) + throw io::WrongFormatException(); + } + + return aResult; +} + +// ---------------------------------------------------------------------- +::rtl::OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos ) +{ + ::rtl::OStringBuffer aResult; + sal_Bool bHaveName = sal_False; + sal_Bool bEscape = sal_False; + + while( !bHaveName ) + { + if ( io_nCurPos >= aBuffer.getLength() ) + throw io::WrongFormatException(); + + if ( bEscape ) + { + if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' ) + aResult.append( (sal_Char)aBuffer[io_nCurPos] ); + else + throw io::WrongFormatException(); + + bEscape = sal_False; + io_nCurPos++; + } + else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' ) + bHaveName = sal_True; + else + { + if ( aBuffer[io_nCurPos] == '\\' ) + bEscape = sal_True; + else + aResult.append( (sal_Char)aBuffer[io_nCurPos] ); + + io_nCurPos++; + } + } + + return ::rtl::OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ); +} + +// ---------------------------------------------------------------------- +::rtl::OUString LockFileCommon::EscapeCharacters( const ::rtl::OUString& aSource ) +{ + ::rtl::OUStringBuffer aBuffer; + const sal_Unicode* pStr = aSource.getStr(); + for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ ) + { + if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' ) + aBuffer.append( (sal_Unicode)'\\' ); + aBuffer.append( pStr[nInd] ); + } + + return aBuffer.makeStringAndClear(); +} + +// ---------------------------------------------------------------------- +::rtl::OUString LockFileCommon::GetOOOUserName() +{ + SvtUserOptions aUserOpt; + ::rtl::OUString aName = aUserOpt.GetFirstName(); + if ( aName.getLength() ) + aName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ); + aName += aUserOpt.GetLastName(); + + return aName; +} + +// ---------------------------------------------------------------------- +::rtl::OUString LockFileCommon::GetCurrentLocalTime() +{ + ::rtl::OUString aTime; + + TimeValue aSysTime; + if ( osl_getSystemTime( &aSysTime ) ) + { + TimeValue aLocTime; + if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) ) + { + oslDateTime aDateTime; + if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) ) + { + char pDateTime[20]; + sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes ); + aTime = ::rtl::OUString::createFromAscii( pDateTime ); + } + } + } + + return aTime; +} + +// ---------------------------------------------------------------------- +uno::Sequence< ::rtl::OUString > LockFileCommon::GenerateOwnEntry() +{ + uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); + + aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName(); + + ::osl::Security aSecurity; + aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] ); + + aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname(); + + aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime(); + + ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] ); + + + return aResult; +} + +} // namespace svt + diff --git a/svtools/source/misc/makefile.mk b/svtools/source/misc/makefile.mk index a9f8c583e85b..a23092e120da 100644 --- a/svtools/source/misc/makefile.mk +++ b/svtools/source/misc/makefile.mk @@ -82,6 +82,7 @@ SLOFILES=\ $(SLO)$/dialogclosedlistener.obj\ $(SLO)$/dialogcontrolling.obj \ $(SLO)$/chartprettypainter.obj \ + $(SLO)$/lockfilecommon.obj \ $(SLO)$/sharecontrolfile.obj \ $(SLO)$/documentlockfile.obj \ $(SLO)$/langtab.obj diff --git a/svtools/source/misc/sharecontrolfile.cxx b/svtools/source/misc/sharecontrolfile.cxx index 0fd661f85135..dbea89f54bcc 100644 --- a/svtools/source/misc/sharecontrolfile.cxx +++ b/svtools/source/misc/sharecontrolfile.cxx @@ -67,21 +67,8 @@ namespace svt { // ---------------------------------------------------------------------- ShareControlFile::ShareControlFile( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory ) -: m_xFactory( xFactory ) +: LockFileCommon( aOrigURL, xFactory, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~sharing." ) ) ) { - if ( !m_xFactory.is() ) - m_xFactory = ::comphelper::getProcessServiceFactory(); - - INetURLObject aDocURL( aOrigURL ); - if ( aDocURL.HasError() ) - throw lang::IllegalArgumentException(); - - ::rtl::OUString aShareURLString = aDocURL.GetPartBeforeLastName(); - aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~sharing." ) ); - aShareURLString += aDocURL.GetName(); - aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "#" ) ); - m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE ); - OpenStream(); if ( !IsValid() ) @@ -182,152 +169,6 @@ void ShareControlFile::Close() } // ---------------------------------------------------------------------- -uno::Sequence< uno::Sequence< ::rtl::OUString > > ShareControlFile::ParseList( const uno::Sequence< sal_Int8 >& aBuffer ) -{ - sal_Int32 nCurPos = 0; - sal_Int32 nCurEntry = 0; - uno::Sequence< uno::Sequence< ::rtl::OUString > > aResult( 10 ); - - while ( nCurPos < aBuffer.getLength() ) - { - if ( nCurEntry >= aResult.getLength() ) - aResult.realloc( nCurEntry + 10 ); - aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos ); - nCurEntry++; - } - - aResult.realloc( nCurEntry ); - return aResult; -} - -// ---------------------------------------------------------------------- -uno::Sequence< ::rtl::OUString > ShareControlFile::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ) -{ - uno::Sequence< ::rtl::OUString > aResult( SHARED_ENTRYSIZE ); - - for ( int nInd = 0; nInd < SHARED_ENTRYSIZE; nInd++ ) - { - aResult[nInd] = ParseName( aBuffer, o_nCurPos ); - if ( o_nCurPos >= aBuffer.getLength() - || ( nInd < SHARED_ENTRYSIZE - 1 && aBuffer[o_nCurPos++] != ',' ) - || ( nInd == SHARED_ENTRYSIZE - 1 && aBuffer[o_nCurPos++] != ';' ) ) - throw io::WrongFormatException(); - } - - return aResult; -} - -// ---------------------------------------------------------------------- -::rtl::OUString ShareControlFile::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos ) -{ - ::rtl::OStringBuffer aResult; - sal_Bool bHaveName = sal_False; - sal_Bool bEscape = sal_False; - - while( !bHaveName ) - { - if ( o_nCurPos >= aBuffer.getLength() ) - throw io::WrongFormatException(); - - if ( bEscape ) - { - if ( aBuffer[o_nCurPos] == ',' || aBuffer[o_nCurPos] == ';' || aBuffer[o_nCurPos] == '\\' ) - aResult.append( (sal_Char)aBuffer[o_nCurPos] ); - else - throw io::WrongFormatException(); - - bEscape = sal_False; - o_nCurPos++; - } - else if ( aBuffer[o_nCurPos] == ',' || aBuffer[o_nCurPos] == ';' ) - bHaveName = sal_True; - else - { - if ( aBuffer[o_nCurPos] == '\\' ) - bEscape = sal_True; - else - aResult.append( (sal_Char)aBuffer[o_nCurPos] ); - - o_nCurPos++; - } - } - - return ::rtl::OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ); -} - -// ---------------------------------------------------------------------- -::rtl::OUString ShareControlFile::EscapeCharacters( const ::rtl::OUString& aSource ) -{ - ::rtl::OUStringBuffer aBuffer; - const sal_Unicode* pStr = aSource.getStr(); - for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ ) - { - if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' ) - aBuffer.append( (sal_Unicode)'\\' ); - aBuffer.append( pStr[nInd] ); - } - - return aBuffer.makeStringAndClear(); -} - -// ---------------------------------------------------------------------- -::rtl::OUString ShareControlFile::GetOOOUserName() -{ - SvtUserOptions aUserOpt; - ::rtl::OUString aName = aUserOpt.GetFirstName(); - if ( aName.getLength() ) - aName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ); - aName += aUserOpt.GetLastName(); - - return aName; -} - -// ---------------------------------------------------------------------- -::rtl::OUString ShareControlFile::GetCurrentLocalTime() -{ - ::rtl::OUString aTime; - - TimeValue aSysTime; - if ( osl_getSystemTime( &aSysTime ) ) - { - TimeValue aLocTime; - if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) ) - { - oslDateTime aDateTime; - if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) ) - { - char pDateTime[20]; - sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes ); - aTime = ::rtl::OUString::createFromAscii( pDateTime ); - } - } - } - - return aTime; -} - -// ---------------------------------------------------------------------- -uno::Sequence< ::rtl::OUString > ShareControlFile::GenerateOwnEntry() -{ - uno::Sequence< ::rtl::OUString > aResult( SHARED_ENTRYSIZE ); - - aResult[SHARED_OOOUSERNAME_ID] = GetOOOUserName(); - - ::osl::Security aSecurity; - aSecurity.getUserName( aResult[SHARED_SYSUSERNAME_ID] ); - - aResult[SHARED_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname(); - - aResult[SHARED_EDITTIME_ID] = GetCurrentLocalTime(); - - ::utl::Bootstrap::locateUserInstallation( aResult[SHARED_USERURL_ID] ); - - - return aResult; -} - - -// ---------------------------------------------------------------------- uno::Sequence< uno::Sequence< ::rtl::OUString > > ShareControlFile::GetUsersData() { ::osl::MutexGuard aGuard( m_aMutex ); |