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

Generated by: LCOV version 1.10