1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: documentlockfile.cxx,v $
11 * $Revision: 1.3.82.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_svtools.hxx"
37 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
38 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
39 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
40 #include <com/sun/star/ucb/NameClashException.hpp>
41 #include <com/sun/star/io/WrongFormatException.hpp>
44 #include <osl/security.hxx>
45 #include <osl/socket.hxx>
47 #include <rtl/string.hxx>
48 #include <rtl/ustring.hxx>
49 #include <rtl/strbuf.hxx>
50 #include <rtl/ustrbuf.hxx>
52 #include <comphelper/processfactory.hxx>
54 #include <tools/urlobj.hxx>
55 #include <unotools/bootstrap.hxx>
57 #include <ucbhelper/content.hxx>
59 #include <svtools/useroptions.hxx>
61 #include <svtools/documentlockfile.hxx>
63 using namespace ::com::sun::star
;
67 sal_Bool
DocumentLockFile::m_bAllowInteraction
= sal_True
;
69 // ----------------------------------------------------------------------
70 DocumentLockFile::DocumentLockFile( const ::rtl::OUString
& aOrigURL
, const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
71 : LockFileCommon( aOrigURL
, xFactory
, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~lock." ) ) )
75 // ----------------------------------------------------------------------
76 DocumentLockFile::~DocumentLockFile()
80 // ----------------------------------------------------------------------
81 void DocumentLockFile::WriteEntryToStream( uno::Sequence
< ::rtl::OUString
> aEntry
, uno::Reference
< io::XOutputStream
> xOutput
)
83 ::osl::MutexGuard
aGuard( m_aMutex
);
85 ::rtl::OUStringBuffer aBuffer
;
87 for ( sal_Int32 nEntryInd
= 0; nEntryInd
< aEntry
.getLength(); nEntryInd
++ )
89 aBuffer
.append( EscapeCharacters( aEntry
[nEntryInd
] ) );
90 if ( nEntryInd
< aEntry
.getLength() - 1 )
91 aBuffer
.append( (sal_Unicode
)',' );
93 aBuffer
.append( (sal_Unicode
)';' );
96 ::rtl::OString
aStringData( ::rtl::OUStringToOString( aBuffer
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
) );
97 uno::Sequence
< sal_Int8
> aData( (sal_Int8
*)aStringData
.getStr(), aStringData
.getLength() );
98 xOutput
->writeBytes( aData
);
101 // ----------------------------------------------------------------------
102 sal_Bool
DocumentLockFile::CreateOwnLockFile()
104 ::osl::MutexGuard
aGuard( m_aMutex
);
108 uno::Reference
< io::XStream
> xTempFile(
109 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
110 uno::UNO_QUERY_THROW
);
111 uno::Reference
< io::XSeekable
> xSeekable( xTempFile
, uno::UNO_QUERY_THROW
);
113 uno::Reference
< io::XInputStream
> xInput
= xTempFile
->getInputStream();
114 uno::Reference
< io::XOutputStream
> xOutput
= xTempFile
->getOutputStream();
116 if ( !xInput
.is() || !xOutput
.is() )
117 throw uno::RuntimeException();
119 uno::Sequence
< ::rtl::OUString
> aNewEntry
= GenerateOwnEntry();
120 WriteEntryToStream( aNewEntry
, xOutput
);
121 xOutput
->closeOutput();
123 xSeekable
->seek( 0 );
125 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> xEnv
;
126 ::ucbhelper::Content
aTargetContent( m_aURL
, xEnv
);
128 ucb::InsertCommandArgument aInsertArg
;
129 aInsertArg
.Data
= xInput
;
130 aInsertArg
.ReplaceExisting
= sal_False
;
132 aCmdArg
<<= aInsertArg
;
133 aTargetContent
.executeCommand( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert" ) ), aCmdArg
);
135 // try to let the file be hidden if possible
137 aTargetContent
.setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ), uno::makeAny( sal_True
) );
138 } catch( uno::Exception
& ) {}
140 catch( ucb::NameClashException
& )
148 // ----------------------------------------------------------------------
149 uno::Sequence
< ::rtl::OUString
> DocumentLockFile::GetLockData()
151 ::osl::MutexGuard
aGuard( m_aMutex
);
153 uno::Reference
< io::XInputStream
> xInput
= OpenStream();
155 throw uno::RuntimeException();
157 const sal_Int32 nBufLen
= 32000;
158 uno::Sequence
< sal_Int8
> aBuffer( nBufLen
);
162 nRead
= xInput
->readBytes( aBuffer
, nBufLen
);
163 xInput
->closeInput();
165 if ( nRead
== nBufLen
)
166 throw io::WrongFormatException();
168 sal_Int32 nCurPos
= 0;
169 return ParseEntry( aBuffer
, nCurPos
);
172 // ----------------------------------------------------------------------
173 uno::Reference
< io::XInputStream
> DocumentLockFile::OpenStream()
175 ::osl::MutexGuard
aGuard( m_aMutex
);
177 uno::Reference
< lang::XMultiServiceFactory
> xFactory
= ::comphelper::getProcessServiceFactory();
178 uno::Reference
< ::com::sun::star::ucb::XSimpleFileAccess
> xSimpleFileAccess(
179 xFactory
->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ),
180 uno::UNO_QUERY_THROW
);
182 // the file can be opened readonly, no locking will be done
183 return xSimpleFileAccess
->openFileRead( m_aURL
);
186 // ----------------------------------------------------------------------
187 sal_Bool
DocumentLockFile::OverwriteOwnLockFile()
189 // allows to overwrite the lock file with the current data
192 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> xEnv
;
193 ::ucbhelper::Content
aTargetContent( m_aURL
, xEnv
);
195 uno::Sequence
< ::rtl::OUString
> aNewEntry
= GenerateOwnEntry();
197 uno::Reference
< io::XStream
> xStream
= aTargetContent
.openWriteableStreamNoLock();
198 uno::Reference
< io::XOutputStream
> xOutput
= xStream
->getOutputStream();
199 uno::Reference
< io::XTruncate
> xTruncate( xOutput
, uno::UNO_QUERY_THROW
);
201 xTruncate
->truncate();
202 WriteEntryToStream( aNewEntry
, xOutput
);
203 xOutput
->closeOutput();
205 catch( uno::Exception
& )
213 // ----------------------------------------------------------------------
214 void DocumentLockFile::RemoveFile()
216 ::osl::MutexGuard
aGuard( m_aMutex
);
218 // TODO/LATER: the removing is not atomar, is it possible in general to make it atomar?
219 uno::Sequence
< ::rtl::OUString
> aNewEntry
= GenerateOwnEntry();
220 uno::Sequence
< ::rtl::OUString
> aFileData
= GetLockData();
222 if ( aFileData
.getLength() < LOCKFILE_ENTRYSIZE
)
223 throw io::WrongFormatException();
225 if ( !aFileData
[LOCKFILE_SYSUSERNAME_ID
].equals( aNewEntry
[LOCKFILE_SYSUSERNAME_ID
] )
226 || !aFileData
[LOCKFILE_LOCALHOST_ID
].equals( aNewEntry
[LOCKFILE_LOCALHOST_ID
] )
227 || !aFileData
[LOCKFILE_USERURL_ID
].equals( aNewEntry
[LOCKFILE_USERURL_ID
] ) )
228 throw io::IOException(); // not the owner, access denied
230 uno::Reference
< lang::XMultiServiceFactory
> xFactory
= ::comphelper::getProcessServiceFactory();
231 uno::Reference
< ::com::sun::star::ucb::XSimpleFileAccess
> xSimpleFileAccess(
232 xFactory
->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ),
233 uno::UNO_QUERY_THROW
);
234 xSimpleFileAccess
->kill( m_aURL
);