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 0 : LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
58 : {
59 0 : INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
60 :
61 0 : OUString aShareURLString = aDocURL.GetPartBeforeLastName();
62 0 : aShareURLString += aPrefix;
63 0 : aShareURLString += aDocURL.GetName();
64 0 : aShareURLString += "%23"; // '#'
65 0 : m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE );
66 0 : }
67 :
68 :
69 0 : LockFileCommon::~LockFileCommon()
70 : {
71 0 : }
72 :
73 :
74 0 : INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
75 : {
76 0 : if ( aDocURL.HasError() )
77 0 : throw lang::IllegalArgumentException();
78 :
79 0 : 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 0 : salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
85 0 : osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
86 0 : if (eStatus == osl::FileBase::E_None)
87 0 : aURLToCheck = aResolver.m_aStatus.getFileURL();
88 0 : else if (eStatus == osl::FileBase::E_MULTIHOP)
89 : {
90 : // do not allow too deep links
91 0 : throw io::IOException();
92 : }
93 :
94 0 : 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 0 : uno::Sequence< OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
118 : {
119 0 : uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
120 :
121 0 : for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ )
122 : {
123 0 : aResult[nInd] = ParseName( aBuffer, io_nCurPos );
124 0 : if ( io_nCurPos >= aBuffer.getLength()
125 0 : || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' )
126 0 : || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) )
127 0 : throw io::WrongFormatException();
128 : }
129 :
130 0 : return aResult;
131 : }
132 :
133 :
134 0 : OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
135 : {
136 0 : OStringBuffer aResult;
137 0 : bool bHaveName = false;
138 0 : bool bEscape = false;
139 :
140 0 : while( !bHaveName )
141 : {
142 0 : if ( io_nCurPos >= aBuffer.getLength() )
143 0 : throw io::WrongFormatException();
144 :
145 0 : 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 0 : else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
156 0 : bHaveName = true;
157 : else
158 : {
159 0 : if ( aBuffer[io_nCurPos] == '\\' )
160 0 : bEscape = true;
161 : else
162 0 : aResult.append( (sal_Char)aBuffer[io_nCurPos] );
163 :
164 0 : io_nCurPos++;
165 : }
166 : }
167 :
168 0 : return OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
169 : }
170 :
171 :
172 0 : OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
173 : {
174 0 : OUStringBuffer aBuffer;
175 0 : const sal_Unicode* pStr = aSource.getStr();
176 0 : for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
177 : {
178 0 : if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
179 0 : aBuffer.append( '\\' );
180 0 : aBuffer.append( pStr[nInd] );
181 : }
182 :
183 0 : return aBuffer.makeStringAndClear();
184 : }
185 :
186 :
187 0 : OUString LockFileCommon::GetOOOUserName()
188 : {
189 0 : SvtUserOptions aUserOpt;
190 0 : OUString aName = aUserOpt.GetFirstName();
191 0 : if ( !aName.isEmpty() )
192 0 : aName += " ";
193 0 : aName += aUserOpt.GetLastName();
194 :
195 0 : return aName;
196 : }
197 :
198 :
199 0 : OUString LockFileCommon::GetCurrentLocalTime()
200 : {
201 0 : OUString aTime;
202 :
203 : TimeValue aSysTime;
204 0 : if ( osl_getSystemTime( &aSysTime ) )
205 : {
206 : TimeValue aLocTime;
207 0 : if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
208 : {
209 : oslDateTime aDateTime;
210 0 : if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
211 : {
212 : char pDateTime[20];
213 0 : sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes );
214 0 : aTime = OUString::createFromAscii( pDateTime );
215 : }
216 : }
217 : }
218 :
219 0 : return aTime;
220 : }
221 :
222 :
223 0 : uno::Sequence< OUString > LockFileCommon::GenerateOwnEntry()
224 : {
225 0 : uno::Sequence< OUString > aResult( LOCKFILE_ENTRYSIZE );
226 :
227 0 : aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName();
228 :
229 0 : ::osl::Security aSecurity;
230 0 : aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] );
231 :
232 0 : aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname();
233 :
234 0 : aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime();
235 :
236 0 : ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] );
237 :
238 :
239 0 : return aResult;
240 : }
241 :
242 : } // namespace svt
243 :
244 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|