Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / config / bootstrap.cxx
blobef7ba57000c12cf2ae50e8e3b072403682fe801c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <config_folders.h>
26 #include <unotools/bootstrap.hxx>
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <sal/log.hxx>
31 #include <osl/file.hxx>
32 #include <osl/diagnose.h>
34 #include <rtl/bootstrap.hxx>
35 #include <osl/process.h>
37 // #define this to true, if remembering defaults is not supported properly
38 #define RTL_BOOTSTRAP_DEFAULTS_BROKEN true
40 constexpr OUStringLiteral BOOTSTRAP_ITEM_PRODUCT_KEY = u"ProductKey";
41 constexpr OUStringLiteral BOOTSTRAP_ITEM_VERSIONFILE = u"Location";
42 constexpr OUStringLiteral BOOTSTRAP_ITEM_BUILDID = u"buildid";
44 constexpr OUStringLiteral BOOTSTRAP_ITEM_BASEINSTALLATION = u"BRAND_BASE_DIR";
45 constexpr OUStringLiteral BOOTSTRAP_ITEM_USERINSTALLATION = u"UserInstallation";
47 constexpr OUStringLiteral BOOTSTRAP_ITEM_USERDIR = u"UserDataDir";
49 constexpr OUStringLiteral BOOTSTRAP_DEFAULT_BASEINSTALL = u"$SYSBINDIR/..";
51 constexpr OUStringLiteral BOOTSTRAP_DIRNAME_USERDIR = u"user";
53 typedef char const * AsciiString;
55 namespace utl
58 // Implementation class: Bootstrap::Impl
60 static OUString makeImplName()
62 OUString uri;
63 rtl::Bootstrap::get( "BRAND_BASE_DIR", uri);
64 return uri + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap");
67 class Bootstrap::Impl
69 const OUString m_aImplName;
70 public: // struct to cache the result of a path lookup
71 struct PathData
73 OUString path;
74 PathStatus status;
76 PathData()
77 : status(DATA_UNKNOWN)
80 public: // data members
81 // base install data
82 PathData aBaseInstall_;
84 // user install data
85 PathData aUserInstall_;
87 // INI files
88 PathData aBootstrapINI_;
89 PathData aVersionINI_;
91 // overall status
92 Status status_;
94 public: // construction and initialization
95 Impl() : m_aImplName(makeImplName())
97 initialize();
100 void initialize();
102 // access helper
103 OUString getBootstrapValue(OUString const& _sName, OUString const& _sDefault) const;
105 const OUString& getImplName() const { return m_aImplName; }
107 private: // implementation
108 bool initBaseInstallationData(rtl::Bootstrap const & _rData);
109 bool initUserInstallationData(rtl::Bootstrap const & _rData);
112 namespace
114 Bootstrap::Impl& theImpl()
116 static Bootstrap::Impl SINGLETON;
117 return SINGLETON;
121 const Bootstrap::Impl& Bootstrap::data()
123 return theImpl();
126 bool Bootstrap::getProcessWorkingDir(OUString &rUrl)
128 rUrl.clear();
129 OUString s("$OOO_CWD");
130 rtl::Bootstrap::expandMacros(s);
131 if (s.isEmpty())
133 if (osl_getProcessWorkingDir(&rUrl.pData) == osl_Process_E_None)
134 return true;
136 else if (s[0] == '1')
138 rUrl = s.copy(1);
139 return true;
141 else if (s[0] == '2' &&
142 (osl::FileBase::getFileURLFromSystemPath(s.copy(1), rUrl) ==
143 osl::FileBase::E_None))
145 return true;
147 return false;
150 void Bootstrap::reloadData()
152 theImpl().initialize();
155 // helper
157 typedef Bootstrap::PathStatus PathStatus;
159 sal_Unicode const cURLSeparator = '/';
161 // path status utility function
162 static PathStatus implCheckStatusOfURL(OUString const& _sURL, osl::DirectoryItem& aDirItem)
164 using namespace osl;
166 PathStatus eStatus = Bootstrap::DATA_UNKNOWN;
168 if (!_sURL.isEmpty())
170 switch( DirectoryItem::get(_sURL, aDirItem) )
172 case DirectoryItem::E_None: // Success
173 eStatus = Bootstrap::PATH_EXISTS;
174 break;
176 case DirectoryItem::E_NOENT: // No such file or directory
177 eStatus = Bootstrap::PATH_VALID;
178 break;
180 case DirectoryItem::E_INVAL: // the format of the parameters was not valid
181 case DirectoryItem::E_NAMETOOLONG: // File name too long
182 case DirectoryItem::E_NOTDIR: // A component of the path prefix of path is not a directory
183 eStatus = Bootstrap::DATA_INVALID;
184 break;
186 // how to handle these ?
187 case DirectoryItem::E_LOOP: // Too many symbolic links encountered
188 case DirectoryItem::E_ACCES: // permission denied
189 // any other error - what to do ?
190 default:
191 eStatus = Bootstrap::DATA_UNKNOWN;
192 break;
195 else
197 eStatus = Bootstrap::DATA_MISSING;
200 return eStatus;
203 static bool implNormalizeURL(OUString & _sURL, osl::DirectoryItem& aDirItem)
205 using namespace osl;
207 OSL_PRECOND(aDirItem.is(), "Opened DirItem required");
209 static const sal_uInt32 cosl_FileStatus_Mask = osl_FileStatus_Mask_FileURL;
211 FileStatus aFileStatus(cosl_FileStatus_Mask);
213 if (aDirItem.getFileStatus(aFileStatus) != DirectoryItem::E_None)
214 return false;
216 OUString aNormalizedURL = aFileStatus.getFileURL();
218 if (aNormalizedURL.isEmpty())
219 return false;
221 // #109863# sal/osl returns final slash for file URLs contradicting
222 // the URL/URI RFCs.
223 if ( !aNormalizedURL.endsWith(OUStringChar(cURLSeparator)) )
224 _sURL = aNormalizedURL;
225 else
226 _sURL = aNormalizedURL.copy( 0, aNormalizedURL.getLength()-1 );
228 return true;
231 static bool implEnsureAbsolute(OUString & _rsURL) // also strips embedded dots !!
233 using osl::File;
235 OUString sBasePath;
236 OSL_VERIFY(Bootstrap::getProcessWorkingDir(sBasePath));
238 OUString sAbsolute;
239 if ( File::E_None == File::getAbsoluteFileURL(sBasePath, _rsURL, sAbsolute))
241 _rsURL = sAbsolute;
242 return true;
244 else
246 OSL_FAIL("Could not get absolute file URL for URL");
247 return false;
251 static bool implMakeAbsoluteURL(OUString & _rsPathOrURL)
253 using namespace osl;
255 bool bURL;
257 OUString sOther;
258 // check if it already was normalized
259 if ( File::E_None == File::getSystemPathFromFileURL(_rsPathOrURL, sOther) )
261 bURL = true;
264 else if ( File::E_None == File::getFileURLFromSystemPath(_rsPathOrURL, sOther) )
266 _rsPathOrURL = sOther;
267 bURL = true;
269 else
270 bURL = false;
272 return bURL && implEnsureAbsolute(_rsPathOrURL);
275 static PathStatus dbgCheckStatusOfURL(OUString const& _sURL)
277 using namespace osl;
279 DirectoryItem aDirItem;
281 return implCheckStatusOfURL(_sURL,aDirItem);
284 static PathStatus checkStatusAndNormalizeURL(OUString & _sURL)
286 using namespace osl;
288 PathStatus eStatus = Bootstrap::DATA_UNKNOWN;
290 if (_sURL.isEmpty())
291 eStatus = Bootstrap::DATA_MISSING;
293 else if ( !implMakeAbsoluteURL(_sURL) )
294 eStatus = Bootstrap::DATA_INVALID;
296 else
298 DirectoryItem aDirItem;
300 eStatus = implCheckStatusOfURL(_sURL,aDirItem);
302 if (eStatus == Bootstrap::PATH_EXISTS && !implNormalizeURL(_sURL,aDirItem))
303 OSL_FAIL("Unexpected failure getting actual URL for existing object");
305 return eStatus;
308 // helpers to build and check a nested URL
309 static PathStatus getDerivedPath(
310 OUString& _rURL,
311 OUString const& _aBaseURL, PathStatus _aBaseStatus,
312 std::u16string_view _sRelativeURL,
313 rtl::Bootstrap const & _rData, OUString const& _sBootstrapParameter
316 OUString sDerivedURL;
317 OSL_PRECOND(!_rData.getFrom(_sBootstrapParameter,sDerivedURL),"Setting for derived path is already defined");
318 OSL_PRECOND(!_sRelativeURL.empty() && _sRelativeURL[0] != cURLSeparator,"Invalid Relative URL");
320 PathStatus aStatus = _aBaseStatus;
322 // do we have a base path ?
323 if (!_aBaseURL.isEmpty())
325 OSL_PRECOND(!_aBaseURL.endsWith(OUStringChar(cURLSeparator)), "Unexpected: base URL ends in slash");
327 sDerivedURL = _aBaseURL + OUStringChar(cURLSeparator) + _sRelativeURL;
329 // a derived (nested) URL can only exist or have a lesser status, if the parent exists
330 if (aStatus == Bootstrap::PATH_EXISTS)
331 aStatus = checkStatusAndNormalizeURL(sDerivedURL);
333 else // the relative appendix must be valid
334 OSL_ASSERT(aStatus != Bootstrap::PATH_VALID || dbgCheckStatusOfURL(sDerivedURL) == Bootstrap::PATH_VALID);
336 _rData.getFrom(_sBootstrapParameter, _rURL, sDerivedURL);
338 OSL_ENSURE(sDerivedURL == _rURL,"Could not set derived URL via Bootstrap default parameter");
339 SAL_WARN_IF( !(RTL_BOOTSTRAP_DEFAULTS_BROKEN || (_rData.getFrom(_sBootstrapParameter,sDerivedURL) && sDerivedURL==_rURL)),
340 "unotools.config",
341 "Use of default did not affect bootstrap value");
343 else
345 // clear the result
346 _rURL = _aBaseURL;
348 // if we have no data it can't be a valid path
349 OSL_ASSERT( aStatus > Bootstrap::PATH_VALID );
352 return aStatus;
355 static PathStatus getDerivedPath(
356 OUString& _rURL,
357 Bootstrap::Impl::PathData const& _aBaseData,
358 std::u16string_view _sRelativeURL,
359 rtl::Bootstrap const & _rData, OUString const& _sBootstrapParameter
362 return getDerivedPath(_rURL,_aBaseData.path,_aBaseData.status,_sRelativeURL,_rData,_sBootstrapParameter);
365 static OUString getExecutableBaseName()
367 OUString sExecutable;
369 if (osl_Process_E_None == osl_getExecutableFile(&sExecutable.pData))
371 // split the executable name
372 sal_Int32 nSepIndex = sExecutable.lastIndexOf(cURLSeparator);
374 sExecutable = sExecutable.copy(nSepIndex + 1);
376 // ... and get the basename (strip the extension)
377 sal_Unicode const cExtensionSep = '.';
379 sal_Int32 const nExtIndex = sExecutable.lastIndexOf(cExtensionSep);
380 sal_Int32 const nExtLength = sExecutable.getLength() - nExtIndex - 1;
381 if (0 < nExtIndex && nExtLength < 4)
382 sExecutable = sExecutable.copy(0,nExtIndex);
384 else
385 SAL_WARN("unotools.config", "Cannot get executable name: osl_getExecutableFile failed");
387 return sExecutable;
390 static Bootstrap::PathStatus updateStatus(Bootstrap::Impl::PathData & _rResult)
392 _rResult.status = checkStatusAndNormalizeURL(_rResult.path);
393 return _rResult.status;
396 static Bootstrap::PathStatus implGetBootstrapFile(rtl::Bootstrap const & _rData, Bootstrap::Impl::PathData & _rBootstrapFile)
398 _rData.getIniName(_rBootstrapFile.path);
400 return updateStatus(_rBootstrapFile);
403 static Bootstrap::PathStatus implGetVersionFile(rtl::Bootstrap const & _rData, Bootstrap::Impl::PathData & _rVersionFile)
405 _rData.getFrom(BOOTSTRAP_ITEM_VERSIONFILE, _rVersionFile.path);
407 return updateStatus(_rVersionFile);
410 // Error reporting
412 char const IS_MISSING[] = "is missing";
413 char const IS_INVALID[] = "is corrupt";
414 char const PERIOD[] = ". ";
416 static void addFileError(OUStringBuffer& _rBuf, std::u16string_view _aPath, AsciiString _sWhat)
418 std::u16string_view sSimpleFileName = _aPath.substr(1 +_aPath.rfind(cURLSeparator));
420 _rBuf.append("The configuration file");
421 _rBuf.append(OUString::Concat(" '") + sSimpleFileName + "' ");
422 _rBuf.appendAscii(_sWhat).append(PERIOD);
425 static void addMissingDirectoryError(OUStringBuffer& _rBuf, std::u16string_view _aPath)
427 _rBuf.append(OUString::Concat("The configuration directory '") + _aPath + "' " +
428 IS_MISSING + PERIOD);
431 static void addUnexpectedError(OUStringBuffer& _rBuf, AsciiString _sExtraInfo = nullptr)
433 if (nullptr == _sExtraInfo)
434 _sExtraInfo = "An internal failure occurred";
436 _rBuf.appendAscii(_sExtraInfo).append(PERIOD);
439 static Bootstrap::FailureCode describeError(OUStringBuffer& _rBuf, Bootstrap::Impl const& _rData)
441 Bootstrap::FailureCode eErrCode = Bootstrap::INVALID_BOOTSTRAP_DATA;
443 _rBuf.append("The program cannot be started. ");
445 switch (_rData.aUserInstall_.status)
447 case Bootstrap::PATH_EXISTS:
448 switch (_rData.aBaseInstall_.status)
450 case Bootstrap::PATH_VALID:
451 addMissingDirectoryError(_rBuf, _rData.aBaseInstall_.path);
452 eErrCode = Bootstrap::MISSING_INSTALL_DIRECTORY;
453 break;
455 case Bootstrap::DATA_INVALID:
456 addUnexpectedError(_rBuf,"The installation path is invalid");
457 break;
459 case Bootstrap::DATA_MISSING:
460 addUnexpectedError(_rBuf,"The installation path is not available");
461 break;
463 case Bootstrap::PATH_EXISTS: // seems to be all fine (?)
464 addUnexpectedError(_rBuf,"");
465 break;
467 default: OSL_ASSERT(false);
468 addUnexpectedError(_rBuf);
469 break;
471 break;
473 case Bootstrap::PATH_VALID:
474 addMissingDirectoryError(_rBuf, _rData.aUserInstall_.path);
475 eErrCode = Bootstrap::MISSING_USER_DIRECTORY;
476 break;
478 // else fall through
479 case Bootstrap::DATA_INVALID:
480 if (_rData.aVersionINI_.status == Bootstrap::PATH_EXISTS)
482 addFileError(_rBuf, _rData.aVersionINI_.path, IS_INVALID);
483 eErrCode = Bootstrap::INVALID_VERSION_FILE_ENTRY;
484 break;
486 [[fallthrough]];
488 case Bootstrap::DATA_MISSING:
489 switch (_rData.aVersionINI_.status)
491 case Bootstrap::PATH_EXISTS:
492 addFileError(_rBuf, _rData.aVersionINI_.path, "does not support the current version");
493 eErrCode = Bootstrap::MISSING_VERSION_FILE_ENTRY;
494 break;
496 case Bootstrap::PATH_VALID:
497 addFileError(_rBuf, _rData.aVersionINI_.path, IS_MISSING);
498 eErrCode = Bootstrap::MISSING_VERSION_FILE;
499 break;
501 default:
502 switch (_rData.aBootstrapINI_.status)
504 case Bootstrap::PATH_EXISTS:
505 addFileError(_rBuf, _rData.aBootstrapINI_.path, IS_INVALID);
507 if (_rData.aVersionINI_.status == Bootstrap::DATA_MISSING)
508 eErrCode = Bootstrap::MISSING_BOOTSTRAP_FILE_ENTRY;
509 else
510 eErrCode = Bootstrap::INVALID_BOOTSTRAP_FILE_ENTRY;
511 break;
513 case Bootstrap::DATA_INVALID: OSL_ASSERT(false); [[fallthrough]];
514 case Bootstrap::PATH_VALID:
515 addFileError(_rBuf, _rData.aBootstrapINI_.path, IS_MISSING);
516 eErrCode = Bootstrap::MISSING_BOOTSTRAP_FILE;
517 break;
519 default:
520 addUnexpectedError(_rBuf);
521 break;
523 break;
525 break;
527 default: OSL_ASSERT(false);
528 addUnexpectedError(_rBuf);
529 break;
532 return eErrCode;
536 OUString Bootstrap::getProductKey()
538 OUString const sDefaultProductKey = getExecutableBaseName();
540 return data().getBootstrapValue( BOOTSTRAP_ITEM_PRODUCT_KEY, sDefaultProductKey );
543 OUString Bootstrap::getProductKey(OUString const& _sDefault)
545 return data().getBootstrapValue( BOOTSTRAP_ITEM_PRODUCT_KEY, _sDefault );
548 OUString Bootstrap::getBuildIdData(OUString const& _sDefault)
550 // try to open version.ini (versionrc)
551 OUString uri;
552 rtl::Bootstrap::get( "BRAND_BASE_DIR", uri);
553 rtl::Bootstrap aData( uri + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("version") );
554 if ( aData.getHandle() == nullptr )
555 // version.ini (versionrc) doesn't exist
556 return _sDefault;
558 // read value
559 OUString sBuildId;
560 aData.getFrom(BOOTSTRAP_ITEM_BUILDID,sBuildId,_sDefault);
561 return sBuildId;
564 Bootstrap::PathStatus Bootstrap::locateBaseInstallation(OUString& _rURL)
566 Impl::PathData const& aPathData = data().aBaseInstall_;
568 _rURL = aPathData.path;
569 return aPathData.status;
572 Bootstrap::PathStatus Bootstrap::locateUserInstallation(OUString& _rURL)
574 Impl::PathData const& aPathData = data().aUserInstall_;
576 _rURL = aPathData.path;
577 return aPathData.status;
580 Bootstrap::PathStatus Bootstrap::locateUserData(OUString& _rURL)
582 OUString const csUserDirItem(BOOTSTRAP_ITEM_USERDIR);
584 rtl::Bootstrap aData( data().getImplName() );
586 if ( aData.getFrom(csUserDirItem, _rURL) )
588 return checkStatusAndNormalizeURL(_rURL);
590 else
592 return getDerivedPath(_rURL, data().aUserInstall_ ,BOOTSTRAP_DIRNAME_USERDIR, aData, csUserDirItem);
596 Bootstrap::PathStatus Bootstrap::locateBootstrapFile(OUString& _rURL)
598 Impl::PathData const& aPathData = data().aBootstrapINI_;
600 _rURL = aPathData.path;
601 return aPathData.status;
604 Bootstrap::PathStatus Bootstrap::locateVersionFile(OUString& _rURL)
606 Impl::PathData const& aPathData = data().aVersionINI_;
608 _rURL = aPathData.path;
609 return aPathData.status;
612 Bootstrap::Status Bootstrap::checkBootstrapStatus(OUString& _rDiagnosticMessage, FailureCode& _rErrCode)
614 Impl const& aData = data();
616 Status result = aData.status_;
618 // maybe do further checks here
620 OUStringBuffer sErrorBuffer;
621 if (result != DATA_OK)
622 _rErrCode = describeError(sErrorBuffer,aData);
624 else
625 _rErrCode = NO_FAILURE;
627 _rDiagnosticMessage = sErrorBuffer.makeStringAndClear();
629 return result;
633 bool Bootstrap::Impl::initBaseInstallationData(rtl::Bootstrap const & _rData)
635 _rData.getFrom(BOOTSTRAP_ITEM_BASEINSTALLATION, aBaseInstall_.path, BOOTSTRAP_DEFAULT_BASEINSTALL);
637 bool bResult = (PATH_EXISTS == updateStatus(aBaseInstall_));
639 implGetBootstrapFile(_rData, aBootstrapINI_);
641 return bResult;
644 bool Bootstrap::Impl::initUserInstallationData(rtl::Bootstrap const & _rData)
646 if (_rData.getFrom(BOOTSTRAP_ITEM_USERINSTALLATION, aUserInstall_.path))
648 updateStatus(aUserInstall_);
650 else
652 // should we do just this
653 aUserInstall_.status = DATA_MISSING;
655 // ... or this - look for a single-user user directory ?
656 OUString const csUserDirItem(BOOTSTRAP_ITEM_USERDIR);
657 OUString sDummy;
658 // look for $BASEINSTALLATION/user only if default UserDir setting is used
659 if (! _rData.getFrom(csUserDirItem, sDummy))
661 if ( PATH_EXISTS == getDerivedPath(sDummy, aBaseInstall_, BOOTSTRAP_DIRNAME_USERDIR, _rData, csUserDirItem) )
662 aUserInstall_ = aBaseInstall_;
666 bool bResult = (PATH_EXISTS == aUserInstall_.status);
668 implGetVersionFile(_rData, aVersionINI_);
670 return bResult;
673 void Bootstrap::Impl::initialize()
675 rtl::Bootstrap aData( m_aImplName );
677 if (!initBaseInstallationData(aData))
679 status_ = INVALID_BASE_INSTALL;
681 else if (!initUserInstallationData(aData))
683 status_ = INVALID_USER_INSTALL;
685 if (aUserInstall_.status >= DATA_MISSING)
687 switch (aVersionINI_.status)
689 case PATH_EXISTS:
690 case PATH_VALID:
691 status_ = MISSING_USER_INSTALL;
692 break;
694 case DATA_INVALID:
695 case DATA_MISSING:
696 status_ = INVALID_BASE_INSTALL;
697 break;
698 default:
699 break;
703 else
705 status_ = DATA_OK;
709 OUString Bootstrap::Impl::getBootstrapValue(OUString const& _sName, OUString const& _sDefault) const
711 rtl::Bootstrap aData( m_aImplName );
713 OUString sResult;
714 aData.getFrom(_sName,sResult,_sDefault);
715 return sResult;
718 } // namespace utl
720 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */