LibreOffice Module svl (master) 1
lockfilecommon.cxx
Go to the documentation of this file.
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>
37
39
41
43#include <utility>
44
45using namespace ::com::sun::star;
46
47namespace svt {
48
49
50LockFileCommon::LockFileCommon(OUString aLockFileURL)
51 : m_aURL(std::move(aLockFileURL))
52{
53}
54
56{
57}
58
59
60const OUString& LockFileCommon::GetURL() const
61{
62 return m_aURL;
63}
64
65
66void LockFileCommon::SetURL(const OUString& aURL)
67{
68 m_aURL = aURL;
69}
70
71
73 std::u16string_view aOrigURL, std::u16string_view aPrefix)
74{
76 aURL.setName(Concat2View(aPrefix + aURL.GetLastName() + "%23" /*'#'*/));
78}
79
80
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
105void 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
115LockFileEntry LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
116{
117 LockFileEntry aResult;
118
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
132OUString 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
170OUString 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
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
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
223{
224 LockFileEntry aResult;
225
227
228 ::osl::Security aSecurity;
229 aSecurity.getUserName( aResult[LockFileComponent::SYSUSERNAME] );
230
231 aResult[LockFileComponent::LOCALHOST] = ::osl::SocketAddr::getLocalHostname();
232
234
236
237
238 return aResult;
239}
240
241} // namespace svt
242
243/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::util::URL m_aURL
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool HasError() const
OUString GetFirstName() const
OUString GetLastName() const
osl::FileBase::RC fetchFileStatus(const rtl::OUString &rURL, int nDepth=128)
osl::FileStatus m_aStatus
static OUString ParseName(const css::uno::Sequence< sal_Int8 > &aBuffer, sal_Int32 &o_nCurPos)
static OUString GetOOOUserName()
LockFileCommon(OUString aLockFileURL)
static LockFileEntry GenerateOwnEntry()
static OUString EscapeCharacters(const OUString &aSource)
void SetURL(const OUString &aURL)
const OUString & GetURL() const
static INetURLObject ResolveLinks(const INetURLObject &aDocURL)
static void ParseList(const css::uno::Sequence< sal_Int8 > &aBuffer, std::vector< LockFileEntry > &rOutput)
static LockFileEntry ParseEntry(const css::uno::Sequence< sal_Int8 > &aBuffer, sal_Int32 &o_nCurPos)
static OUString GenerateOwnLockFileURL(std::u16string_view aOrigURL, std::u16string_view aPrefix)
This method generates the URL of the lock file based on the document URL and the specified prefix.
static OUString GetCurrentLocalTime()
static PathStatus locateUserInstallation(OUString &_rURL)
URL aURL
OUString aName
LockFileComponent
int sprintf(char(&s)[N], char const *format, T &&... arguments)
sal_uInt16 sal_Unicode
std::unique_ptr< char[]> aBuffer