1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_svl.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <stdio.h> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/ucb/InsertCommandArgument.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/ucb/NameClashException.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/io/WrongFormatException.hpp> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <osl/time.h> 40*cdf0e10cSrcweir #include <osl/security.hxx> 41*cdf0e10cSrcweir #include <osl/socket.hxx> 42*cdf0e10cSrcweir #include <osl/file.hxx> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <rtl/string.hxx> 45*cdf0e10cSrcweir #include <rtl/ustring.hxx> 46*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 47*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir #include <tools/urlobj.hxx> 52*cdf0e10cSrcweir #include <unotools/bootstrap.hxx> 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir #include <ucbhelper/content.hxx> 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir #include <unotools/useroptions.hxx> 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir #include <svl/lockfilecommon.hxx> 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir using namespace ::com::sun::star; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir namespace svt { 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir // ---------------------------------------------------------------------- 65*cdf0e10cSrcweir LockFileCommon::LockFileCommon( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory, const ::rtl::OUString& aPrefix ) 66*cdf0e10cSrcweir : m_xFactory( xFactory ) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir if ( !m_xFactory.is() ) 69*cdf0e10cSrcweir m_xFactory = ::comphelper::getProcessServiceFactory(); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) ); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir ::rtl::OUString aShareURLString = aDocURL.GetPartBeforeLastName(); 74*cdf0e10cSrcweir aShareURLString += aPrefix; 75*cdf0e10cSrcweir aShareURLString += aDocURL.GetName(); 76*cdf0e10cSrcweir aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "#" ) ); 77*cdf0e10cSrcweir m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE ); 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir // ---------------------------------------------------------------------- 81*cdf0e10cSrcweir LockFileCommon::~LockFileCommon() 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir // ---------------------------------------------------------------------- 86*cdf0e10cSrcweir INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL ) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir if ( aDocURL.HasError() ) 89*cdf0e10cSrcweir throw lang::IllegalArgumentException(); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir ::rtl::OUString aURLToCheck = aDocURL.GetMainURL( INetURLObject::NO_DECODE ); 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir sal_Bool bNeedsChecking = sal_True; 94*cdf0e10cSrcweir sal_Int32 nMaxLinkCount = 128; 95*cdf0e10cSrcweir sal_Int32 nCount = 0; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir while( bNeedsChecking ) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir bNeedsChecking = sal_False; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir // do not allow too deep links 102*cdf0e10cSrcweir if ( nCount++ >= nMaxLinkCount ) 103*cdf0e10cSrcweir throw io::IOException(); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir // there is currently no UCB functionality to resolve the symbolic links; 106*cdf0e10cSrcweir // since the lock files are used only for local file systems the osl functionality is used directly 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir ::osl::FileStatus aStatus( FileStatusMask_Type | FileStatusMask_LinkTargetURL ); 109*cdf0e10cSrcweir ::osl::DirectoryItem aItem; 110*cdf0e10cSrcweir if ( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aURLToCheck, aItem ) 111*cdf0e10cSrcweir && aItem.is() && ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir if ( aStatus.isValid( FileStatusMask_Type ) 114*cdf0e10cSrcweir && aStatus.isValid( FileStatusMask_LinkTargetURL ) 115*cdf0e10cSrcweir && aStatus.getFileType() == ::osl::FileStatus::Link ) 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir aURLToCheck = aStatus.getLinkTargetURL(); 118*cdf0e10cSrcweir bNeedsChecking = sal_True; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir return INetURLObject( aURLToCheck ); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir // ---------------------------------------------------------------------- 127*cdf0e10cSrcweir uno::Sequence< uno::Sequence< ::rtl::OUString > > LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer ) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir sal_Int32 nCurPos = 0; 130*cdf0e10cSrcweir sal_Int32 nCurEntry = 0; 131*cdf0e10cSrcweir uno::Sequence< uno::Sequence< ::rtl::OUString > > aResult( 10 ); 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir while ( nCurPos < aBuffer.getLength() ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir if ( nCurEntry >= aResult.getLength() ) 136*cdf0e10cSrcweir aResult.realloc( nCurEntry + 10 ); 137*cdf0e10cSrcweir aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos ); 138*cdf0e10cSrcweir nCurEntry++; 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir aResult.realloc( nCurEntry ); 142*cdf0e10cSrcweir return aResult; 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir // ---------------------------------------------------------------------- 146*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir aResult[nInd] = ParseName( aBuffer, io_nCurPos ); 153*cdf0e10cSrcweir if ( io_nCurPos >= aBuffer.getLength() 154*cdf0e10cSrcweir || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' ) 155*cdf0e10cSrcweir || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) ) 156*cdf0e10cSrcweir throw io::WrongFormatException(); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir return aResult; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // ---------------------------------------------------------------------- 163*cdf0e10cSrcweir ::rtl::OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir ::rtl::OStringBuffer aResult; 166*cdf0e10cSrcweir sal_Bool bHaveName = sal_False; 167*cdf0e10cSrcweir sal_Bool bEscape = sal_False; 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir while( !bHaveName ) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir if ( io_nCurPos >= aBuffer.getLength() ) 172*cdf0e10cSrcweir throw io::WrongFormatException(); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir if ( bEscape ) 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' ) 177*cdf0e10cSrcweir aResult.append( (sal_Char)aBuffer[io_nCurPos] ); 178*cdf0e10cSrcweir else 179*cdf0e10cSrcweir throw io::WrongFormatException(); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir bEscape = sal_False; 182*cdf0e10cSrcweir io_nCurPos++; 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' ) 185*cdf0e10cSrcweir bHaveName = sal_True; 186*cdf0e10cSrcweir else 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir if ( aBuffer[io_nCurPos] == '\\' ) 189*cdf0e10cSrcweir bEscape = sal_True; 190*cdf0e10cSrcweir else 191*cdf0e10cSrcweir aResult.append( (sal_Char)aBuffer[io_nCurPos] ); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir io_nCurPos++; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir return ::rtl::OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ); 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // ---------------------------------------------------------------------- 201*cdf0e10cSrcweir ::rtl::OUString LockFileCommon::EscapeCharacters( const ::rtl::OUString& aSource ) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir ::rtl::OUStringBuffer aBuffer; 204*cdf0e10cSrcweir const sal_Unicode* pStr = aSource.getStr(); 205*cdf0e10cSrcweir for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' ) 208*cdf0e10cSrcweir aBuffer.append( (sal_Unicode)'\\' ); 209*cdf0e10cSrcweir aBuffer.append( pStr[nInd] ); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir return aBuffer.makeStringAndClear(); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir // ---------------------------------------------------------------------- 216*cdf0e10cSrcweir ::rtl::OUString LockFileCommon::GetOOOUserName() 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir SvtUserOptions aUserOpt; 219*cdf0e10cSrcweir ::rtl::OUString aName = aUserOpt.GetFirstName(); 220*cdf0e10cSrcweir if ( aName.getLength() ) 221*cdf0e10cSrcweir aName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ); 222*cdf0e10cSrcweir aName += aUserOpt.GetLastName(); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir return aName; 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir // ---------------------------------------------------------------------- 228*cdf0e10cSrcweir ::rtl::OUString LockFileCommon::GetCurrentLocalTime() 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir ::rtl::OUString aTime; 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir TimeValue aSysTime; 233*cdf0e10cSrcweir if ( osl_getSystemTime( &aSysTime ) ) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir TimeValue aLocTime; 236*cdf0e10cSrcweir if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) ) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir oslDateTime aDateTime; 239*cdf0e10cSrcweir if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) ) 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir char pDateTime[20]; 242*cdf0e10cSrcweir sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes ); 243*cdf0e10cSrcweir aTime = ::rtl::OUString::createFromAscii( pDateTime ); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir return aTime; 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir // ---------------------------------------------------------------------- 252*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LockFileCommon::GenerateOwnEntry() 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE ); 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName(); 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir ::osl::Security aSecurity; 259*cdf0e10cSrcweir aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] ); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname(); 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime(); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] ); 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir return aResult; 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir } // namespace svt 272*cdf0e10cSrcweir 273