xref: /core/svl/source/misc/lockfilecommon.cxx (revision 3eee720c)
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 <com/sun/star/lang/IllegalArgumentException.hpp>
22 #include <com/sun/star/io/WrongFormatException.hpp>
23 
24 #include <osl/time.h>
25 #include <osl/security.hxx>
26 #include <osl/socket.hxx>
27 #include <osl/file.hxx>
28 #include <o3tl/enumrange.hxx>
29 #include <o3tl/sprintf.hxx>
30 
31 #include <rtl/ustring.hxx>
32 #include <rtl/strbuf.hxx>
33 #include <rtl/ustrbuf.hxx>
34 
35 #include <tools/urlobj.hxx>
36 #include <unotools/bootstrap.hxx>
37 
38 #include <unotools/useroptions.hxx>
39 
40 #include <salhelper/linkhelper.hxx>
41 
42 #include <svl/lockfilecommon.hxx>
43 #include <utility>
44 
45 using namespace ::com::sun::star;
46 
47 namespace svt {
48 
49 
LockFileCommon(OUString aLockFileURL)50 LockFileCommon::LockFileCommon(OUString aLockFileURL)
51     : m_aURL(std::move(aLockFileURL))
52 {
53 }
54 
~LockFileCommon()55 LockFileCommon::~LockFileCommon()
56 {
57 }
58 
59 
GetURL() const60 const OUString& LockFileCommon::GetURL() const
61 {
62     return m_aURL;
63 }
64 
65 
SetURL(const OUString & aURL)66 void LockFileCommon::SetURL(const OUString& aURL)
67 {
68     m_aURL = aURL;
69 }
70 
71 
GenerateOwnLockFileURL(std::u16string_view aOrigURL,std::u16string_view aPrefix)72 OUString LockFileCommon::GenerateOwnLockFileURL(
73     std::u16string_view aOrigURL, std::u16string_view aPrefix)
74 {
75     INetURLObject aURL = ResolveLinks(INetURLObject(aOrigURL));
76     aURL.setName(Concat2View(aPrefix + aURL.GetLastName() + "%23" /*'#'*/));
77     return aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
78 }
79 
80 
ResolveLinks(const INetURLObject & aDocURL)81 INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
82 {
83     if ( aDocURL.HasError() )
84         throw lang::IllegalArgumentException();
85 
86     OUString aURLToCheck = aDocURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
87 
88     // there is currently no UCB functionality to resolve the symbolic links;
89     // since the lock files are used only for local file systems the osl
90     // functionality is used directly
91     salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
92     osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
93     if (eStatus == osl::FileBase::E_None)
94         aURLToCheck = aResolver.m_aStatus.getFileURL();
95     else if (eStatus == osl::FileBase::E_MULTIHOP)
96     {
97         // do not allow too deep links
98         throw io::IOException();
99     }
100 
101     return INetURLObject( aURLToCheck );
102 }
103 
104 
ParseList(const uno::Sequence<sal_Int8> & aBuffer,std::vector<LockFileEntry> & aResult)105 void LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer, std::vector< LockFileEntry > & aResult )
106 {
107     sal_Int32 nCurPos = 0;
108     while ( nCurPos < aBuffer.getLength() )
109     {
110         aResult.push_back( ParseEntry( aBuffer, nCurPos ) );
111     }
112 }
113 
114 
ParseEntry(const uno::Sequence<sal_Int8> & aBuffer,sal_Int32 & io_nCurPos)115 LockFileEntry LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
116 {
117     LockFileEntry aResult;
118 
119     for ( LockFileComponent nInd : o3tl::enumrange<LockFileComponent>() )
120     {
121         aResult[nInd] = ParseName( aBuffer, io_nCurPos );
122         if ( io_nCurPos >= aBuffer.getLength()
123           || ( nInd < LockFileComponent::LAST && aBuffer[io_nCurPos++] != ',' )
124           || ( nInd == LockFileComponent::LAST && aBuffer[io_nCurPos++] != ';' ) )
125             throw io::WrongFormatException();
126     }
127 
128     return aResult;
129 }
130 
131 
ParseName(const uno::Sequence<sal_Int8> & aBuffer,sal_Int32 & io_nCurPos)132 OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
133 {
134     OStringBuffer aResult(128);
135     bool bHaveName = false;
136     bool bEscape = false;
137 
138     while( !bHaveName )
139     {
140         if ( io_nCurPos >= aBuffer.getLength() )
141             throw io::WrongFormatException();
142 
143         if ( bEscape )
144         {
145             if ( aBuffer[io_nCurPos] != ',' && aBuffer[io_nCurPos] != ';' && aBuffer[io_nCurPos] != '\\' )
146                 throw io::WrongFormatException();
147 
148             aResult.append( static_cast<char>(aBuffer[io_nCurPos]) );
149 
150             bEscape = false;
151             io_nCurPos++;
152         }
153         else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
154             bHaveName = true;
155         else
156         {
157             if ( aBuffer[io_nCurPos] == '\\' )
158                 bEscape = true;
159             else
160                 aResult.append( static_cast<char>(aBuffer[io_nCurPos]) );
161 
162             io_nCurPos++;
163         }
164     }
165 
166     return OStringToOUString( aResult, RTL_TEXTENCODING_UTF8 );
167 }
168 
169 
EscapeCharacters(const OUString & aSource)170 OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
171 {
172     OUStringBuffer aBuffer(aSource.getLength()*2);
173     const sal_Unicode* pStr = aSource.getStr();
174     for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
175     {
176         if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
177             aBuffer.append( '\\' );
178         aBuffer.append( pStr[nInd] );
179     }
180 
181     return aBuffer.makeStringAndClear();
182 }
183 
184 
GetOOOUserName()185 OUString LockFileCommon::GetOOOUserName()
186 {
187     SvtUserOptions aUserOpt;
188     OUString aName = aUserOpt.GetFirstName();
189     if ( !aName.isEmpty() )
190         aName += " ";
191     aName += aUserOpt.GetLastName();
192 
193     return aName;
194 }
195 
196 
GetCurrentLocalTime()197 OUString LockFileCommon::GetCurrentLocalTime()
198 {
199     OUString aTime;
200 
201     TimeValue aSysTime;
202     if ( osl_getSystemTime( &aSysTime ) )
203     {
204         TimeValue aLocTime;
205         if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
206         {
207             oslDateTime aDateTime;
208             if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
209             {
210                 char pDateTime[sizeof("65535.65535.-32768 65535:65535")];
211                     // reserve enough space for hypothetical max length
212                 o3tl::sprintf( pDateTime, "%02" SAL_PRIuUINT32 ".%02" SAL_PRIuUINT32 ".%4" SAL_PRIdINT32 " %02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32, sal_uInt32(aDateTime.Day), sal_uInt32(aDateTime.Month), sal_Int32(aDateTime.Year), sal_uInt32(aDateTime.Hours), sal_uInt32(aDateTime.Minutes) );
213                 aTime = OUString::createFromAscii( pDateTime );
214             }
215         }
216     }
217 
218     return aTime;
219 }
220 
221 
GenerateOwnEntry()222 LockFileEntry LockFileCommon::GenerateOwnEntry()
223 {
224     LockFileEntry aResult;
225 
226     aResult[LockFileComponent::OOOUSERNAME] = GetOOOUserName();
227 
228     ::osl::Security aSecurity;
229     aSecurity.getUserName( aResult[LockFileComponent::SYSUSERNAME] );
230 
231     aResult[LockFileComponent::LOCALHOST] = ::osl::SocketAddr::getLocalHostname();
232 
233     aResult[LockFileComponent::EDITTIME] = GetCurrentLocalTime();
234 
235     ::utl::Bootstrap::locateUserInstallation( aResult[LockFileComponent::USERURL] );
236 
237 
238     return aResult;
239 }
240 
241 } // namespace svt
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
244