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

Generated by: LCOV version 1.10