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