LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/svl/source/misc - lockfilecommon.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 70 93 75.3 %
Date: 2013-07-09 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      21             : #include <stdio.h>
      22             : 
      23             : #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
      24             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      25             : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
      26             : #include <com/sun/star/ucb/NameClashException.hpp>
      27             : #include <com/sun/star/io/WrongFormatException.hpp>
      28             : 
      29             : #include <osl/time.h>
      30             : #include <osl/security.hxx>
      31             : #include <osl/socket.hxx>
      32             : #include <osl/file.hxx>
      33             : 
      34             : #include <rtl/string.hxx>
      35             : #include <rtl/ustring.hxx>
      36             : #include <rtl/strbuf.hxx>
      37             : #include <rtl/ustrbuf.hxx>
      38             : 
      39             : #include <comphelper/processfactory.hxx>
      40             : 
      41             : #include <tools/urlobj.hxx>
      42             : #include <unotools/bootstrap.hxx>
      43             : 
      44             : #include <ucbhelper/content.hxx>
      45             : 
      46             : #include <unotools/useroptions.hxx>
      47             : 
      48             : #include <salhelper/linkhelper.hxx>
      49             : 
      50             : #include <svl/lockfilecommon.hxx>
      51             : 
      52             : using namespace ::com::sun::star;
      53             : 
      54             : namespace svt {
      55             : 
      56             : // ----------------------------------------------------------------------
      57         108 : LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
      58             : {
      59         108 :     INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
      60             : 
      61         216 :     OUString aShareURLString = aDocURL.GetPartBeforeLastName();
      62         108 :     aShareURLString += aPrefix;
      63         108 :     aShareURLString += aDocURL.GetName();
      64         108 :     aShareURLString += "%23"; // '#'
      65         216 :     m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE );
      66         108 : }
      67             : 
      68             : // ----------------------------------------------------------------------
      69         108 : LockFileCommon::~LockFileCommon()
      70             : {
      71         108 : }
      72             : 
      73             : // ----------------------------------------------------------------------
      74         108 : INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
      75             : {
      76         108 :     if ( aDocURL.HasError() )
      77           0 :         throw lang::IllegalArgumentException();
      78             : 
      79         108 :     OUString aURLToCheck = aDocURL.GetMainURL(INetURLObject::NO_DECODE);
      80             : 
      81             :     // there is currently no UCB functionality to resolve the symbolic links;
      82             :     // since the lock files are used only for local file systems the osl
      83             :     // functionality is used directly
      84         216 :     salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
      85         108 :     osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
      86         108 :     if (eStatus == osl::FileBase::E_None)
      87          92 :         aURLToCheck = aResolver.m_aStatus.getFileURL();
      88          16 :     else if (eStatus == osl::FileBase::E_MULTIHOP)
      89             :     {
      90             :         // do not allow too deep links
      91           0 :         throw io::IOException();
      92             :     }
      93             : 
      94         216 :     return INetURLObject( aURLToCheck );
      95             : }
      96             : 
      97             : // ----------------------------------------------------------------------
      98           0 : uno::Sequence< uno::Sequence< OUString > > LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer )
      99             : {
     100           0 :     sal_Int32 nCurPos = 0;
     101           0 :     sal_Int32 nCurEntry = 0;
     102           0 :     uno::Sequence< uno::Sequence< OUString > > aResult( 10 );
     103             : 
     104           0 :     while ( nCurPos < aBuffer.getLength() )
     105             :     {
     106           0 :         if ( nCurEntry >= aResult.getLength() )
     107           0 :             aResult.realloc( nCurEntry + 10 );
     108           0 :         aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos );
     109           0 :         nCurEntry++;
     110             :     }
     111             : 
     112           0 :     aResult.realloc( nCurEntry );
     113           0 :     return aResult;
     114             : }
     115             : 
     116             : // ----------------------------------------------------------------------
     117          54 : uno::Sequence< OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
     118             : {
     119          54 :     uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
     120             : 
     121         324 :     for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ )
     122             :     {
     123         270 :         aResult[nInd] = ParseName( aBuffer, io_nCurPos );
     124         540 :         if ( io_nCurPos >= aBuffer.getLength()
     125         270 :           || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' )
     126         540 :           || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) )
     127           0 :             throw io::WrongFormatException();
     128             :     }
     129             : 
     130          54 :     return aResult;
     131             : }
     132             : 
     133             : // ----------------------------------------------------------------------
     134         270 : OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
     135             : {
     136         270 :     OStringBuffer aResult;
     137         270 :     bool bHaveName = false;
     138         270 :     bool bEscape = false;
     139             : 
     140        6991 :     while( !bHaveName )
     141             :     {
     142        6451 :         if ( io_nCurPos >= aBuffer.getLength() )
     143           0 :             throw io::WrongFormatException();
     144             : 
     145        6451 :         if ( bEscape )
     146             :         {
     147           0 :             if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' )
     148           0 :                 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
     149             :             else
     150           0 :                 throw io::WrongFormatException();
     151             : 
     152           0 :             bEscape = false;
     153           0 :             io_nCurPos++;
     154             :         }
     155        6451 :         else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
     156         270 :             bHaveName = true;
     157             :         else
     158             :         {
     159        6181 :             if ( aBuffer[io_nCurPos] == '\\' )
     160           0 :                 bEscape = true;
     161             :             else
     162        6181 :                 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
     163             : 
     164        6181 :             io_nCurPos++;
     165             :         }
     166             :     }
     167             : 
     168         270 :     return OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
     169             : }
     170             : 
     171             : // ----------------------------------------------------------------------
     172         270 : OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
     173             : {
     174         270 :     OUStringBuffer aBuffer;
     175         270 :     const sal_Unicode* pStr = aSource.getStr();
     176        6451 :     for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
     177             :     {
     178        6181 :         if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
     179           0 :             aBuffer.append( (sal_Unicode)'\\' );
     180        6181 :         aBuffer.append( pStr[nInd] );
     181             :     }
     182             : 
     183         270 :     return aBuffer.makeStringAndClear();
     184             : }
     185             : 
     186             : // ----------------------------------------------------------------------
     187         108 : OUString LockFileCommon::GetOOOUserName()
     188             : {
     189         108 :     SvtUserOptions aUserOpt;
     190         108 :     OUString aName = aUserOpt.GetFirstName();
     191         108 :     if ( !aName.isEmpty() )
     192           0 :         aName += " ";
     193         108 :     aName += aUserOpt.GetLastName();
     194             : 
     195         108 :     return aName;
     196             : }
     197             : 
     198             : // ----------------------------------------------------------------------
     199         108 : OUString LockFileCommon::GetCurrentLocalTime()
     200             : {
     201         108 :     OUString aTime;
     202             : 
     203             :     TimeValue aSysTime;
     204         108 :     if ( osl_getSystemTime( &aSysTime ) )
     205             :     {
     206             :         TimeValue aLocTime;
     207         108 :         if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
     208             :         {
     209             :             oslDateTime aDateTime;
     210         108 :             if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
     211             :             {
     212             :                 char pDateTime[20];
     213         108 :                 sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes );
     214         108 :                 aTime = OUString::createFromAscii( pDateTime );
     215             :             }
     216             :         }
     217             :     }
     218             : 
     219         108 :     return aTime;
     220             : }
     221             : 
     222             : // ----------------------------------------------------------------------
     223         108 : uno::Sequence< OUString > LockFileCommon::GenerateOwnEntry()
     224             : {
     225         108 :     uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
     226             : 
     227         108 :     aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName();
     228             : 
     229         216 :     ::osl::Security aSecurity;
     230         108 :     aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] );
     231             : 
     232         108 :     aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname();
     233             : 
     234         108 :     aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime();
     235             : 
     236         108 :     ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] );
     237             : 
     238             : 
     239         216 :     return aResult;
     240             : }
     241             : 
     242             : } // namespace svt
     243             : 
     244             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10