1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
31cdf0e10cSrcweir #include <com/sun/star/ucb/InsertCommandArgument.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ucb/NameClashException.hpp>
33cdf0e10cSrcweir #include <com/sun/star/io/WrongFormatException.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <osl/time.h>
36cdf0e10cSrcweir #include <osl/security.hxx>
37cdf0e10cSrcweir #include <osl/socket.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <rtl/string.hxx>
40cdf0e10cSrcweir #include <rtl/ustring.hxx>
41cdf0e10cSrcweir #include <rtl/strbuf.hxx>
42cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <tools/urlobj.hxx>
47cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <ucbhelper/content.hxx>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <unotools/useroptions.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <svl/documentlockfile.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir using namespace ::com::sun::star;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace svt {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir sal_Bool DocumentLockFile::m_bAllowInteraction = sal_True;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir // ----------------------------------------------------------------------
DocumentLockFile(const::rtl::OUString & aOrigURL,const uno::Reference<lang::XMultiServiceFactory> & xFactory)62cdf0e10cSrcweir DocumentLockFile::DocumentLockFile( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory )
63cdf0e10cSrcweir : LockFileCommon( aOrigURL, xFactory, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~lock." ) ) )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir // ----------------------------------------------------------------------
~DocumentLockFile()68cdf0e10cSrcweir DocumentLockFile::~DocumentLockFile()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir // ----------------------------------------------------------------------
WriteEntryToStream(uno::Sequence<::rtl::OUString> aEntry,uno::Reference<io::XOutputStream> xOutput)73cdf0e10cSrcweir void DocumentLockFile::WriteEntryToStream( uno::Sequence< ::rtl::OUString > aEntry, uno::Reference< io::XOutputStream > xOutput )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     ::rtl::OUStringBuffer aBuffer;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     for ( sal_Int32 nEntryInd = 0; nEntryInd < aEntry.getLength(); nEntryInd++ )
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         aBuffer.append( EscapeCharacters( aEntry[nEntryInd] ) );
82cdf0e10cSrcweir         if ( nEntryInd < aEntry.getLength() - 1 )
83cdf0e10cSrcweir             aBuffer.append( (sal_Unicode)',' );
84cdf0e10cSrcweir         else
85cdf0e10cSrcweir             aBuffer.append( (sal_Unicode)';' );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     ::rtl::OString aStringData( ::rtl::OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ) );
89cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aData( (sal_Int8*)aStringData.getStr(), aStringData.getLength() );
90cdf0e10cSrcweir     xOutput->writeBytes( aData );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // ----------------------------------------------------------------------
CreateOwnLockFile()94cdf0e10cSrcweir sal_Bool DocumentLockFile::CreateOwnLockFile()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     try
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         uno::Reference< io::XStream > xTempFile(
101cdf0e10cSrcweir             m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
102cdf0e10cSrcweir             uno::UNO_QUERY_THROW );
103cdf0e10cSrcweir         uno::Reference< io::XSeekable > xSeekable( xTempFile, uno::UNO_QUERY_THROW );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         uno::Reference< io::XInputStream > xInput = xTempFile->getInputStream();
106cdf0e10cSrcweir         uno::Reference< io::XOutputStream > xOutput = xTempFile->getOutputStream();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         if ( !xInput.is() || !xOutput.is() )
109cdf0e10cSrcweir             throw uno::RuntimeException();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         uno::Sequence< ::rtl::OUString > aNewEntry = GenerateOwnEntry();
112cdf0e10cSrcweir         WriteEntryToStream( aNewEntry, xOutput );
113cdf0e10cSrcweir         xOutput->closeOutput();
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         xSeekable->seek( 0 );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
118cdf0e10cSrcweir         ::ucbhelper::Content aTargetContent( m_aURL, xEnv );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         ucb::InsertCommandArgument aInsertArg;
121cdf0e10cSrcweir         aInsertArg.Data = xInput;
122cdf0e10cSrcweir         aInsertArg.ReplaceExisting = sal_False;
123cdf0e10cSrcweir         uno::Any aCmdArg;
124cdf0e10cSrcweir         aCmdArg <<= aInsertArg;
125cdf0e10cSrcweir         aTargetContent.executeCommand( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert" ) ), aCmdArg );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         // try to let the file be hidden if possible
128cdf0e10cSrcweir         try {
129cdf0e10cSrcweir             aTargetContent.setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ), uno::makeAny( sal_True ) );
130cdf0e10cSrcweir         } catch( uno::Exception& ) {}
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir     catch( ucb::NameClashException& )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         return sal_False;
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     return sal_True;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir // ----------------------------------------------------------------------
GetLockData()141cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > DocumentLockFile::GetLockData()
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     uno::Reference< io::XInputStream > xInput = OpenStream();
146cdf0e10cSrcweir     if ( !xInput.is() )
147cdf0e10cSrcweir         throw uno::RuntimeException();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     const sal_Int32 nBufLen = 32000;
150cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aBuffer( nBufLen );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     sal_Int32 nRead = 0;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     nRead = xInput->readBytes( aBuffer, nBufLen );
155cdf0e10cSrcweir     xInput->closeInput();
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     if ( nRead == nBufLen )
158cdf0e10cSrcweir         throw io::WrongFormatException();
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     sal_Int32 nCurPos = 0;
161cdf0e10cSrcweir     return ParseEntry( aBuffer, nCurPos );
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir // ----------------------------------------------------------------------
OpenStream()165cdf0e10cSrcweir uno::Reference< io::XInputStream > DocumentLockFile::OpenStream()
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
170cdf0e10cSrcweir         uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > xSimpleFileAccess(
171cdf0e10cSrcweir             xFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ),
172cdf0e10cSrcweir             uno::UNO_QUERY_THROW );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     // the file can be opened readonly, no locking will be done
175cdf0e10cSrcweir     return xSimpleFileAccess->openFileRead( m_aURL );
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // ----------------------------------------------------------------------
OverwriteOwnLockFile()179cdf0e10cSrcweir sal_Bool DocumentLockFile::OverwriteOwnLockFile()
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     // allows to overwrite the lock file with the current data
182cdf0e10cSrcweir     try
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
185cdf0e10cSrcweir         ::ucbhelper::Content aTargetContent( m_aURL, xEnv );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         uno::Sequence< ::rtl::OUString > aNewEntry = GenerateOwnEntry();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         uno::Reference< io::XStream > xStream = aTargetContent.openWriteableStreamNoLock();
190cdf0e10cSrcweir         uno::Reference< io::XOutputStream > xOutput = xStream->getOutputStream();
191cdf0e10cSrcweir         uno::Reference< io::XTruncate > xTruncate( xOutput, uno::UNO_QUERY_THROW );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         xTruncate->truncate();
194cdf0e10cSrcweir         WriteEntryToStream( aNewEntry, xOutput );
195cdf0e10cSrcweir         xOutput->closeOutput();
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir     catch( uno::Exception& )
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         return sal_False;
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     return sal_True;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir // ----------------------------------------------------------------------
RemoveFile()206cdf0e10cSrcweir void DocumentLockFile::RemoveFile()
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     // TODO/LATER: the removing is not atomar, is it possible in general to make it atomar?
211cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aNewEntry = GenerateOwnEntry();
212cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aFileData = GetLockData();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     if ( aFileData.getLength() < LOCKFILE_ENTRYSIZE )
215cdf0e10cSrcweir         throw io::WrongFormatException();
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     if ( !aFileData[LOCKFILE_SYSUSERNAME_ID].equals( aNewEntry[LOCKFILE_SYSUSERNAME_ID] )
218cdf0e10cSrcweir       || !aFileData[LOCKFILE_LOCALHOST_ID].equals( aNewEntry[LOCKFILE_LOCALHOST_ID] )
219cdf0e10cSrcweir       || !aFileData[LOCKFILE_USERURL_ID].equals( aNewEntry[LOCKFILE_USERURL_ID] ) )
220cdf0e10cSrcweir         throw io::IOException(); // not the owner, access denied
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
223cdf0e10cSrcweir     uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > xSimpleFileAccess(
224cdf0e10cSrcweir         xFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ),
225cdf0e10cSrcweir         uno::UNO_QUERY_THROW );
226cdf0e10cSrcweir     xSimpleFileAccess->kill( m_aURL );
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir } // namespace svt
230cdf0e10cSrcweir 
231