LCOV - code coverage report
Current view: top level - svl/source/misc - documentlockfile.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 67 86 77.9 %
Date: 2015-06-13 12:38:46 Functions: 7 8 87.5 %
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 <com/sun/star/ucb/XSimpleFileAccess.hpp>
      22             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      23             : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
      24             : #include <com/sun/star/ucb/NameClashException.hpp>
      25             : #include <com/sun/star/io/WrongFormatException.hpp>
      26             : #include <com/sun/star/io/TempFile.hpp>
      27             : 
      28             : #include <osl/time.h>
      29             : #include <osl/security.hxx>
      30             : #include <osl/socket.hxx>
      31             : #include <o3tl/enumrange.hxx>
      32             : 
      33             : #include <rtl/string.hxx>
      34             : #include <rtl/ustring.hxx>
      35             : #include <rtl/strbuf.hxx>
      36             : #include <rtl/ustrbuf.hxx>
      37             : 
      38             : #include <comphelper/processfactory.hxx>
      39             : 
      40             : #include <unotools/bootstrap.hxx>
      41             : 
      42             : #include <ucbhelper/content.hxx>
      43             : 
      44             : #include <unotools/useroptions.hxx>
      45             : 
      46             : #include <svl/documentlockfile.hxx>
      47             : 
      48             : using namespace ::com::sun::star;
      49             : 
      50             : namespace svt {
      51             : 
      52             : bool DocumentLockFile::m_bAllowInteraction = true;
      53             : 
      54             : 
      55          63 : DocumentLockFile::DocumentLockFile( const OUString& aOrigURL )
      56          63 : : LockFileCommon( aOrigURL, OUString( ".~lock."  ) )
      57             : {
      58          63 : }
      59             : 
      60             : 
      61          63 : DocumentLockFile::~DocumentLockFile()
      62             : {
      63          63 : }
      64             : 
      65             : 
      66          32 : void DocumentLockFile::WriteEntryToStream( const LockFileEntry& aEntry, uno::Reference< io::XOutputStream > xOutput )
      67             : {
      68          32 :     ::osl::MutexGuard aGuard( m_aMutex );
      69             : 
      70          64 :     OUStringBuffer aBuffer;
      71             : 
      72         192 :     for ( LockFileComponent lft : o3tl::enumrange<LockFileComponent>() )
      73             :     {
      74         160 :         aBuffer.append( EscapeCharacters( aEntry[lft] ) );
      75         160 :         if ( lft < LockFileComponent::LAST )
      76         128 :             aBuffer.append( ',' );
      77             :         else
      78          32 :             aBuffer.append( ';' );
      79             :     }
      80             : 
      81          64 :     OString aStringData( OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ) );
      82          64 :     uno::Sequence< sal_Int8 > aData( reinterpret_cast<sal_Int8 const *>(aStringData.getStr()), aStringData.getLength() );
      83          64 :     xOutput->writeBytes( aData );
      84          32 : }
      85             : 
      86             : 
      87          32 : bool DocumentLockFile::CreateOwnLockFile()
      88             : {
      89          32 :     ::osl::MutexGuard aGuard( m_aMutex );
      90             : 
      91             :     try
      92             :     {
      93             :         uno::Reference< io::XStream > xTempFile(
      94             :             io::TempFile::create( comphelper::getProcessComponentContext() ),
      95          32 :             uno::UNO_QUERY_THROW );
      96          64 :         uno::Reference< io::XSeekable > xSeekable( xTempFile, uno::UNO_QUERY_THROW );
      97             : 
      98          64 :         uno::Reference< io::XInputStream > xInput = xTempFile->getInputStream();
      99          64 :         uno::Reference< io::XOutputStream > xOutput = xTempFile->getOutputStream();
     100             : 
     101          32 :         if ( !xInput.is() || !xOutput.is() )
     102           0 :             throw uno::RuntimeException();
     103             : 
     104          64 :         LockFileEntry aNewEntry = GenerateOwnEntry();
     105          32 :         WriteEntryToStream( aNewEntry, xOutput );
     106          32 :         xOutput->closeOutput();
     107             : 
     108          32 :         xSeekable->seek( 0 );
     109             : 
     110          64 :         uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
     111          64 :         ::ucbhelper::Content aTargetContent( m_aURL, xEnv, comphelper::getProcessComponentContext() );
     112             : 
     113          64 :         ucb::InsertCommandArgument aInsertArg;
     114          32 :         aInsertArg.Data = xInput;
     115          32 :         aInsertArg.ReplaceExisting = sal_False;
     116          64 :         uno::Any aCmdArg;
     117          32 :         aCmdArg <<= aInsertArg;
     118          32 :         aTargetContent.executeCommand( OUString( "insert"  ), aCmdArg );
     119             : 
     120             :         // try to let the file be hidden if possible
     121             :         try {
     122          32 :             aTargetContent.setPropertyValue("IsHidden", uno::makeAny( sal_True ) );
     123          32 :         } catch( uno::Exception& ) {}
     124             :     }
     125           0 :     catch( ucb::NameClashException& )
     126             :     {
     127           0 :         return false;
     128             :     }
     129             : 
     130          32 :     return true;
     131             : }
     132             : 
     133             : 
     134          31 : LockFileEntry DocumentLockFile::GetLockData()
     135             : {
     136          31 :     ::osl::MutexGuard aGuard( m_aMutex );
     137             : 
     138          62 :     uno::Reference< io::XInputStream > xInput = OpenStream();
     139          31 :     if ( !xInput.is() )
     140           0 :         throw uno::RuntimeException();
     141             : 
     142          31 :     const sal_Int32 nBufLen = 32000;
     143          62 :     uno::Sequence< sal_Int8 > aBuffer( nBufLen );
     144             : 
     145          31 :     sal_Int32 nRead = 0;
     146             : 
     147          31 :     nRead = xInput->readBytes( aBuffer, nBufLen );
     148          31 :     xInput->closeInput();
     149             : 
     150          31 :     if ( nRead == nBufLen )
     151           0 :         throw io::WrongFormatException();
     152             : 
     153          31 :     sal_Int32 nCurPos = 0;
     154          62 :     return ParseEntry( aBuffer, nCurPos );
     155             : }
     156             : 
     157             : 
     158          31 : uno::Reference< io::XInputStream > DocumentLockFile::OpenStream()
     159             : {
     160          31 :     ::osl::MutexGuard aGuard( m_aMutex );
     161             : 
     162          62 :     uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
     163          62 :     ::ucbhelper::Content aSourceContent( m_aURL, xEnv, comphelper::getProcessComponentContext() );
     164             : 
     165             :     // the file can be opened readonly, no locking will be done
     166          62 :     return aSourceContent.openStream();
     167             : }
     168             : 
     169             : 
     170           0 : bool DocumentLockFile::OverwriteOwnLockFile()
     171             : {
     172             :     // allows to overwrite the lock file with the current data
     173             :     try
     174             :     {
     175           0 :         uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
     176           0 :         ::ucbhelper::Content aTargetContent( m_aURL, xEnv, comphelper::getProcessComponentContext() );
     177             : 
     178           0 :         LockFileEntry aNewEntry = GenerateOwnEntry();
     179             : 
     180           0 :         uno::Reference< io::XStream > xStream = aTargetContent.openWriteableStreamNoLock();
     181           0 :         uno::Reference< io::XOutputStream > xOutput = xStream->getOutputStream();
     182           0 :         uno::Reference< io::XTruncate > xTruncate( xOutput, uno::UNO_QUERY_THROW );
     183             : 
     184           0 :         xTruncate->truncate();
     185           0 :         WriteEntryToStream( aNewEntry, xOutput );
     186           0 :         xOutput->closeOutput();
     187             :     }
     188           0 :     catch( uno::Exception& )
     189             :     {
     190           0 :         return false;
     191             :     }
     192             : 
     193           0 :     return true;
     194             : }
     195             : 
     196             : 
     197          31 : void DocumentLockFile::RemoveFile()
     198             : {
     199          31 :     ::osl::MutexGuard aGuard( m_aMutex );
     200             : 
     201             :     // TODO/LATER: the removing is not atomic, is it possible in general to make it atomic?
     202          62 :     LockFileEntry aNewEntry = GenerateOwnEntry();
     203          62 :     LockFileEntry aFileData = GetLockData();
     204             : 
     205          62 :     if ( !aFileData[LockFileComponent::SYSUSERNAME].equals( aNewEntry[LockFileComponent::SYSUSERNAME] )
     206          31 :       || !aFileData[LockFileComponent::LOCALHOST].equals( aNewEntry[LockFileComponent::LOCALHOST] )
     207          62 :       || !aFileData[LockFileComponent::USERURL].equals( aNewEntry[LockFileComponent::USERURL] ) )
     208           0 :         throw io::IOException(); // not the owner, access denied
     209             : 
     210          62 :     uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
     211          62 :     ::ucbhelper::Content aCnt(m_aURL, xEnv, comphelper::getProcessComponentContext());
     212             :     aCnt.executeCommand(OUString("delete"),
     213          62 :         uno::makeAny(true));
     214          31 : }
     215             : 
     216             : } // namespace svt
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11