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

Generated by: LCOV version 1.10