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