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