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 :
29 : #include <osl/time.h>
30 : #include <osl/security.hxx>
31 : #include <osl/socket.hxx>
32 : #include <osl/file.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 <tools/urlobj.hxx>
42 : #include <unotools/bootstrap.hxx>
43 :
44 : #include <ucbhelper/content.hxx>
45 :
46 : #include <unotools/useroptions.hxx>
47 :
48 : #include <salhelper/linkhelper.hxx>
49 :
50 : #include <svl/lockfilecommon.hxx>
51 :
52 : using namespace ::com::sun::star;
53 :
54 : namespace svt {
55 :
56 :
57 392 : LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
58 : {
59 392 : INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
60 :
61 784 : OUString aShareURLString = aDocURL.GetPartBeforeLastName();
62 392 : aShareURLString += aPrefix;
63 392 : aShareURLString += aDocURL.GetName();
64 392 : aShareURLString += "%23"; // '#'
65 784 : m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE );
66 392 : }
67 :
68 :
69 392 : LockFileCommon::~LockFileCommon()
70 : {
71 392 : }
72 :
73 :
74 392 : INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
75 : {
76 392 : if ( aDocURL.HasError() )
77 0 : throw lang::IllegalArgumentException();
78 :
79 392 : OUString aURLToCheck = aDocURL.GetMainURL(INetURLObject::NO_DECODE);
80 :
81 : // there is currently no UCB functionality to resolve the symbolic links;
82 : // since the lock files are used only for local file systems the osl
83 : // functionality is used directly
84 784 : salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
85 392 : osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
86 392 : if (eStatus == osl::FileBase::E_None)
87 368 : aURLToCheck = aResolver.m_aStatus.getFileURL();
88 24 : else if (eStatus == osl::FileBase::E_MULTIHOP)
89 : {
90 : // do not allow too deep links
91 0 : throw io::IOException();
92 : }
93 :
94 784 : return INetURLObject( aURLToCheck );
95 : }
96 :
97 :
98 0 : uno::Sequence< uno::Sequence< OUString > > LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer )
99 : {
100 0 : sal_Int32 nCurPos = 0;
101 0 : sal_Int32 nCurEntry = 0;
102 0 : uno::Sequence< uno::Sequence< OUString > > aResult( 10 );
103 :
104 0 : while ( nCurPos < aBuffer.getLength() )
105 : {
106 0 : if ( nCurEntry >= aResult.getLength() )
107 0 : aResult.realloc( nCurEntry + 10 );
108 0 : aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos );
109 0 : nCurEntry++;
110 : }
111 :
112 0 : aResult.realloc( nCurEntry );
113 0 : return aResult;
114 : }
115 :
116 :
117 196 : uno::Sequence< OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
118 : {
119 196 : uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
120 :
121 1176 : for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ )
122 : {
123 980 : aResult[nInd] = ParseName( aBuffer, io_nCurPos );
124 1960 : if ( io_nCurPos >= aBuffer.getLength()
125 980 : || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' )
126 1960 : || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) )
127 0 : throw io::WrongFormatException();
128 : }
129 :
130 196 : return aResult;
131 : }
132 :
133 :
134 980 : OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
135 : {
136 980 : OStringBuffer aResult;
137 980 : bool bHaveName = false;
138 980 : bool bEscape = false;
139 :
140 22002 : while( !bHaveName )
141 : {
142 20042 : if ( io_nCurPos >= aBuffer.getLength() )
143 0 : throw io::WrongFormatException();
144 :
145 20042 : if ( bEscape )
146 : {
147 0 : if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' )
148 0 : aResult.append( (sal_Char)aBuffer[io_nCurPos] );
149 : else
150 0 : throw io::WrongFormatException();
151 :
152 0 : bEscape = false;
153 0 : io_nCurPos++;
154 : }
155 20042 : else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
156 980 : bHaveName = true;
157 : else
158 : {
159 19062 : if ( aBuffer[io_nCurPos] == '\\' )
160 0 : bEscape = true;
161 : else
162 19062 : aResult.append( (sal_Char)aBuffer[io_nCurPos] );
163 :
164 19062 : io_nCurPos++;
165 : }
166 : }
167 :
168 980 : return OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
169 : }
170 :
171 :
172 980 : OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
173 : {
174 980 : OUStringBuffer aBuffer;
175 980 : const sal_Unicode* pStr = aSource.getStr();
176 20042 : for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
177 : {
178 19062 : if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
179 0 : aBuffer.append( '\\' );
180 19062 : aBuffer.append( pStr[nInd] );
181 : }
182 :
183 980 : return aBuffer.makeStringAndClear();
184 : }
185 :
186 :
187 394 : OUString LockFileCommon::GetOOOUserName()
188 : {
189 394 : SvtUserOptions aUserOpt;
190 394 : OUString aName = aUserOpt.GetFirstName();
191 394 : if ( !aName.isEmpty() )
192 52 : aName += " ";
193 394 : aName += aUserOpt.GetLastName();
194 :
195 394 : return aName;
196 : }
197 :
198 :
199 394 : OUString LockFileCommon::GetCurrentLocalTime()
200 : {
201 394 : OUString aTime;
202 :
203 : TimeValue aSysTime;
204 394 : if ( osl_getSystemTime( &aSysTime ) )
205 : {
206 : TimeValue aLocTime;
207 394 : if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
208 : {
209 : oslDateTime aDateTime;
210 394 : if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
211 : {
212 : char pDateTime[20];
213 394 : sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes );
214 394 : aTime = OUString::createFromAscii( pDateTime );
215 : }
216 : }
217 : }
218 :
219 394 : return aTime;
220 : }
221 :
222 :
223 394 : uno::Sequence< OUString > LockFileCommon::GenerateOwnEntry()
224 : {
225 394 : uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
226 :
227 394 : aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName();
228 :
229 788 : ::osl::Security aSecurity;
230 394 : aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] );
231 :
232 394 : aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname();
233 :
234 394 : aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime();
235 :
236 394 : ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] );
237 :
238 :
239 788 : return aResult;
240 : }
241 :
242 : } // namespace svt
243 :
244 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|