xref: /aoo41x/main/svl/source/misc/lockfilecommon.cxx (revision 40df464e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include <stdio.h>
28 
29 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
30 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
31 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
32 #include <com/sun/star/ucb/NameClashException.hpp>
33 #include <com/sun/star/io/WrongFormatException.hpp>
34 
35 #include <osl/time.h>
36 #include <osl/security.hxx>
37 #include <osl/socket.hxx>
38 #include <osl/file.hxx>
39 
40 #include <rtl/string.hxx>
41 #include <rtl/ustring.hxx>
42 #include <rtl/strbuf.hxx>
43 #include <rtl/ustrbuf.hxx>
44 
45 #include <comphelper/processfactory.hxx>
46 
47 #include <tools/urlobj.hxx>
48 #include <unotools/bootstrap.hxx>
49 
50 #include <ucbhelper/content.hxx>
51 
52 #include <unotools/useroptions.hxx>
53 
54 #include <svl/lockfilecommon.hxx>
55 
56 using namespace ::com::sun::star;
57 
58 namespace svt {
59 
60 // ----------------------------------------------------------------------
LockFileCommon(const::rtl::OUString & aOrigURL,const uno::Reference<lang::XMultiServiceFactory> & xFactory,const::rtl::OUString & aPrefix)61 LockFileCommon::LockFileCommon( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory, const ::rtl::OUString& aPrefix )
62 : m_xFactory( xFactory )
63 {
64     if ( !m_xFactory.is() )
65         m_xFactory = ::comphelper::getProcessServiceFactory();
66 
67     INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
68 
69     ::rtl::OUString aShareURLString = aDocURL.GetPartBeforeLastName();
70     aShareURLString += aPrefix;
71     aShareURLString += aDocURL.GetName();
72     aShareURLString += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "#" ) );
73     m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE );
74 }
75 
76 // ----------------------------------------------------------------------
~LockFileCommon()77 LockFileCommon::~LockFileCommon()
78 {
79 }
80 
81 // ----------------------------------------------------------------------
ResolveLinks(const INetURLObject & aDocURL)82 INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
83 {
84     if ( aDocURL.HasError() )
85         throw lang::IllegalArgumentException();
86 
87     ::rtl::OUString aURLToCheck = aDocURL.GetMainURL( INetURLObject::NO_DECODE );
88 
89     sal_Bool bNeedsChecking = sal_True;
90     sal_Int32 nMaxLinkCount = 128;
91     sal_Int32 nCount = 0;
92 
93     while( bNeedsChecking )
94     {
95         bNeedsChecking = sal_False;
96 
97         // do not allow too deep links
98         if ( nCount++ >= nMaxLinkCount )
99             throw io::IOException();
100 
101         // there is currently no UCB functionality to resolve the symbolic links;
102         // since the lock files are used only for local file systems the osl functionality is used directly
103 
104         ::osl::FileStatus aStatus( FileStatusMask_Type | FileStatusMask_LinkTargetURL );
105         ::osl::DirectoryItem aItem;
106         if ( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aURLToCheck, aItem )
107           && aItem.is() && ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
108         {
109             if ( aStatus.isValid( FileStatusMask_Type )
110               && aStatus.isValid( FileStatusMask_LinkTargetURL )
111               && aStatus.getFileType() == ::osl::FileStatus::Link )
112             {
113                 aURLToCheck = aStatus.getLinkTargetURL();
114                 bNeedsChecking = sal_True;
115             }
116         }
117     }
118 
119     return INetURLObject( aURLToCheck );
120 }
121 
122 // ----------------------------------------------------------------------
ParseList(const uno::Sequence<sal_Int8> & aBuffer)123 uno::Sequence< uno::Sequence< ::rtl::OUString > > LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer )
124 {
125     sal_Int32 nCurPos = 0;
126     sal_Int32 nCurEntry = 0;
127     uno::Sequence< uno::Sequence< ::rtl::OUString > > aResult( 10 );
128 
129     while ( nCurPos < aBuffer.getLength() )
130     {
131         if ( nCurEntry >= aResult.getLength() )
132             aResult.realloc( nCurEntry + 10 );
133         aResult[nCurEntry] = ParseEntry( aBuffer, nCurPos );
134         nCurEntry++;
135     }
136 
137     aResult.realloc( nCurEntry );
138     return aResult;
139 }
140 
141 // ----------------------------------------------------------------------
ParseEntry(const uno::Sequence<sal_Int8> & aBuffer,sal_Int32 & io_nCurPos)142 uno::Sequence< ::rtl::OUString > LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
143 {
144     uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE );
145 
146     for ( int nInd = 0; nInd < LOCKFILE_ENTRYSIZE; nInd++ )
147     {
148         aResult[nInd] = ParseName( aBuffer, io_nCurPos );
149         if ( io_nCurPos >= aBuffer.getLength()
150           || ( nInd < LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ',' )
151           || ( nInd == LOCKFILE_ENTRYSIZE - 1 && aBuffer[io_nCurPos++] != ';' ) )
152             throw io::WrongFormatException();
153     }
154 
155     return aResult;
156 }
157 
158 // ----------------------------------------------------------------------
ParseName(const uno::Sequence<sal_Int8> & aBuffer,sal_Int32 & io_nCurPos)159 ::rtl::OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
160 {
161     ::rtl::OStringBuffer aResult;
162     sal_Bool bHaveName = sal_False;
163     sal_Bool bEscape = sal_False;
164 
165     while( !bHaveName )
166     {
167         if ( io_nCurPos >= aBuffer.getLength() )
168             throw io::WrongFormatException();
169 
170         if ( bEscape )
171         {
172             if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' )
173                 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
174             else
175                 throw io::WrongFormatException();
176 
177             bEscape = sal_False;
178             io_nCurPos++;
179         }
180         else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
181             bHaveName = sal_True;
182         else
183         {
184             if ( aBuffer[io_nCurPos] == '\\' )
185                 bEscape = sal_True;
186             else
187                 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
188 
189             io_nCurPos++;
190         }
191     }
192 
193     return ::rtl::OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
194 }
195 
196 // ----------------------------------------------------------------------
EscapeCharacters(const::rtl::OUString & aSource)197 ::rtl::OUString LockFileCommon::EscapeCharacters( const ::rtl::OUString& aSource )
198 {
199     ::rtl::OUStringBuffer aBuffer;
200     const sal_Unicode* pStr = aSource.getStr();
201     for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
202     {
203         if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
204             aBuffer.append( (sal_Unicode)'\\' );
205         aBuffer.append( pStr[nInd] );
206     }
207 
208     return aBuffer.makeStringAndClear();
209 }
210 
211 // ----------------------------------------------------------------------
GetOOOUserName()212 ::rtl::OUString LockFileCommon::GetOOOUserName()
213 {
214     SvtUserOptions aUserOpt;
215     ::rtl::OUString aName = aUserOpt.GetFirstName();
216     if ( aName.getLength() )
217         aName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) );
218     aName += aUserOpt.GetLastName();
219 
220     return aName;
221 }
222 
223 // ----------------------------------------------------------------------
GetCurrentLocalTime()224 ::rtl::OUString LockFileCommon::GetCurrentLocalTime()
225 {
226     ::rtl::OUString aTime;
227 
228     TimeValue aSysTime;
229     if ( osl_getSystemTime( &aSysTime ) )
230     {
231         TimeValue aLocTime;
232         if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
233         {
234             oslDateTime aDateTime;
235             if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
236             {
237                 char pDateTime[20];
238                 sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes );
239                 aTime = ::rtl::OUString::createFromAscii( pDateTime );
240             }
241         }
242     }
243 
244     return aTime;
245 }
246 
247 // ----------------------------------------------------------------------
GenerateOwnEntry()248 uno::Sequence< ::rtl::OUString > LockFileCommon::GenerateOwnEntry()
249 {
250     uno::Sequence< ::rtl::OUString > aResult( LOCKFILE_ENTRYSIZE );
251 
252     aResult[LOCKFILE_OOOUSERNAME_ID] = GetOOOUserName();
253 
254     ::osl::Security aSecurity;
255     aSecurity.getUserName( aResult[LOCKFILE_SYSUSERNAME_ID] );
256 
257     aResult[LOCKFILE_LOCALHOST_ID] = ::osl::SocketAddr::getLocalHostname();
258 
259     aResult[LOCKFILE_EDITTIME_ID] = GetCurrentLocalTime();
260 
261     ::utl::Bootstrap::locateUserInstallation( aResult[LOCKFILE_USERURL_ID] );
262 
263 
264     return aResult;
265 }
266 
267 } // namespace svt
268 
269