2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-02 10:15:23 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
#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>
|
2015-04-14 13:47:01 +02:00
|
|
|
#include <o3tl/enumrange.hxx>
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2009-10-06 07:38:24 +02:00
|
|
|
#include <unotools/useroptions.hxx>
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2011-05-25 08:53:31 +01:00
|
|
|
#include <salhelper/linkhelper.hxx>
|
|
|
|
|
2009-10-08 16:59:48 +02:00
|
|
|
#include <svl/lockfilecommon.hxx>
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
namespace svt {
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
|
|
|
INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aShareURLString = aDocURL.GetPartBeforeLastName();
|
2009-02-12 10:27:21 +00:00
|
|
|
aShareURLString += aPrefix;
|
|
|
|
aShareURLString += aDocURL.GetName();
|
2012-07-03 21:18:15 -03:00
|
|
|
aShareURLString += "%23"; // '#'
|
2016-12-05 08:47:18 +02:00
|
|
|
m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::DecodeMechanism::NONE );
|
2009-02-12 10:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2009-02-12 10:27:21 +00:00
|
|
|
LockFileCommon::~LockFileCommon()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2009-02-12 10:27:21 +00:00
|
|
|
INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
|
|
|
|
{
|
|
|
|
if ( aDocURL.HasError() )
|
|
|
|
throw lang::IllegalArgumentException();
|
|
|
|
|
2016-12-05 08:47:18 +02:00
|
|
|
OUString aURLToCheck = aDocURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2011-05-25 08:53:31 +01:00
|
|
|
// 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
|
|
|
|
salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
|
|
|
|
osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
|
|
|
|
if (eStatus == osl::FileBase::E_None)
|
|
|
|
aURLToCheck = aResolver.m_aStatus.getFileURL();
|
|
|
|
else if (eStatus == osl::FileBase::E_MULTIHOP)
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
|
|
|
// do not allow too deep links
|
2011-05-25 08:53:31 +01:00
|
|
|
throw io::IOException();
|
2009-02-12 10:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return INetURLObject( aURLToCheck );
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
void LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer, std::vector< LockFileEntry > & aResult )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nCurPos = 0;
|
|
|
|
while ( nCurPos < aBuffer.getLength() )
|
|
|
|
{
|
2015-04-14 13:47:01 +02:00
|
|
|
aResult.push_back( ParseEntry( aBuffer, nCurPos ) );
|
2009-02-12 10:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
LockFileEntry LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
2015-04-14 13:47:01 +02:00
|
|
|
LockFileEntry aResult;
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
for ( LockFileComponent nInd : o3tl::enumrange<LockFileComponent>() )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
|
|
|
aResult[nInd] = ParseName( aBuffer, io_nCurPos );
|
|
|
|
if ( io_nCurPos >= aBuffer.getLength()
|
2015-04-14 13:47:01 +02:00
|
|
|
|| ( nInd < LockFileComponent::LAST && aBuffer[io_nCurPos++] != ',' )
|
|
|
|
|| ( nInd == LockFileComponent::LAST && aBuffer[io_nCurPos++] != ';' ) )
|
2009-02-12 10:27:21 +00:00
|
|
|
throw io::WrongFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return aResult;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OStringBuffer aResult;
|
2013-04-25 15:00:54 +09:00
|
|
|
bool bHaveName = false;
|
|
|
|
bool bEscape = false;
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2013-04-25 15:00:54 +09:00
|
|
|
bEscape = false;
|
2009-02-12 10:27:21 +00:00
|
|
|
io_nCurPos++;
|
|
|
|
}
|
|
|
|
else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
|
2013-04-25 15:00:54 +09:00
|
|
|
bHaveName = true;
|
2009-02-12 10:27:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( aBuffer[io_nCurPos] == '\\' )
|
2013-04-25 15:00:54 +09:00
|
|
|
bEscape = true;
|
2009-02-12 10:27:21 +00:00
|
|
|
else
|
|
|
|
aResult.append( (sal_Char)aBuffer[io_nCurPos] );
|
|
|
|
|
|
|
|
io_nCurPos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 17:24:13 +01:00
|
|
|
return OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
|
2009-02-12 10:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUStringBuffer aBuffer;
|
2009-02-12 10:27:21 +00:00
|
|
|
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] == ',' )
|
2013-12-20 14:23:33 +02:00
|
|
|
aBuffer.append( '\\' );
|
2009-02-12 10:27:21 +00:00
|
|
|
aBuffer.append( pStr[nInd] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return aBuffer.makeStringAndClear();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString LockFileCommon::GetOOOUserName()
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
|
|
|
SvtUserOptions aUserOpt;
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aName = aUserOpt.GetFirstName();
|
2012-01-13 15:54:57 -02:00
|
|
|
if ( !aName.isEmpty() )
|
2012-07-03 21:18:15 -03:00
|
|
|
aName += " ";
|
2009-02-12 10:27:21 +00:00
|
|
|
aName += aUserOpt.GetLastName();
|
|
|
|
|
|
|
|
return aName;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString LockFileCommon::GetCurrentLocalTime()
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString aTime;
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
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 );
|
2013-04-07 12:06:47 +02:00
|
|
|
aTime = OUString::createFromAscii( pDateTime );
|
2009-02-12 10:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aTime;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
LockFileEntry LockFileCommon::GenerateOwnEntry()
|
2009-02-12 10:27:21 +00:00
|
|
|
{
|
2015-04-14 13:47:01 +02:00
|
|
|
LockFileEntry aResult;
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
aResult[LockFileComponent::OOOUSERNAME] = GetOOOUserName();
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
::osl::Security aSecurity;
|
2015-04-14 13:47:01 +02:00
|
|
|
aSecurity.getUserName( aResult[LockFileComponent::SYSUSERNAME] );
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
aResult[LockFileComponent::LOCALHOST] = ::osl::SocketAddr::getLocalHostname();
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
aResult[LockFileComponent::EDITTIME] = GetCurrentLocalTime();
|
2009-02-12 10:27:21 +00:00
|
|
|
|
2015-04-14 13:47:01 +02:00
|
|
|
::utl::Bootstrap::locateUserInstallation( aResult[LockFileComponent::USERURL] );
|
2009-02-12 10:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
return aResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace svt
|
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|