bump product version to 7.2.5.1
[LibreOffice.git] / configmgr / source / components.cxx
blob65bc00da951779c5283f7336bea122d3a45f1853
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 <cassert>
23 #include <chrono>
24 #include <vector>
25 #include <set>
27 #include <com/sun/star/beans/Optional.hpp>
28 #include <com/sun/star/beans/UnknownPropertyException.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/container/NoSuchElementException.hpp>
31 #include <com/sun/star/lang/WrappedTargetException.hpp>
32 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/uno/Exception.hpp>
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/uno/RuntimeException.hpp>
37 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <com/sun/star/uno/XInterface.hpp>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <config_dconf.h>
41 #include <config_folders.h>
42 #include <osl/conditn.hxx>
43 #include <osl/file.hxx>
44 #include <osl/mutex.hxx>
45 #include <rtl/bootstrap.hxx>
46 #include <rtl/ref.hxx>
47 #include <rtl/ustrbuf.hxx>
48 #include <rtl/ustring.hxx>
49 #include <sal/log.hxx>
50 #include <sal/types.h>
51 #include <salhelper/thread.hxx>
52 #include <tools/diagnose_ex.h>
53 #include <comphelper/backupfilehelper.hxx>
55 #include "additions.hxx"
56 #include "components.hxx"
57 #include "data.hxx"
58 #include "lock.hxx"
59 #include "modifications.hxx"
60 #include "node.hxx"
61 #include "nodemap.hxx"
62 #include "parsemanager.hxx"
63 #include "partial.hxx"
64 #include "rootaccess.hxx"
65 #include "writemodfile.hxx"
66 #include "xcdparser.hxx"
67 #include "xcuparser.hxx"
68 #include "xcsparser.hxx"
70 #if ENABLE_DCONF
71 #include "dconf.hxx"
72 #endif
74 #if defined(_WIN32)
75 #include "winreg.hxx"
76 #endif
78 namespace configmgr {
80 namespace {
82 struct UnresolvedVectorItem {
83 OUString name;
84 rtl::Reference< ParseManager > manager;
86 UnresolvedVectorItem(
87 OUString const & theName,
88 rtl::Reference< ParseManager > const & theManager):
89 name(theName), manager(theManager) {}
92 typedef std::vector< UnresolvedVectorItem > UnresolvedVector;
94 void parseXcsFile(
95 OUString const & url, int layer, Data & data, Partial const * partial,
96 Modifications * modifications, Additions * additions)
98 assert(partial == nullptr && modifications == nullptr && additions == nullptr);
99 (void) partial; (void) modifications; (void) additions;
100 bool ok = rtl::Reference< ParseManager >(
101 new ParseManager(url, new XcsParser(layer, data)))->parse(nullptr);
102 assert(ok);
103 (void) ok; // avoid warnings
106 void parseXcuFile(
107 OUString const & url, int layer, Data & data, Partial const * partial,
108 Modifications * modifications, Additions * additions)
110 bool ok = rtl::Reference< ParseManager >(
111 new ParseManager(
112 url,
113 new XcuParser(layer, data, partial, modifications, additions)))->
114 parse(nullptr);
115 assert(ok);
116 (void) ok; // avoid warnings
119 OUString expand(OUString const & str) {
120 OUString s(str);
121 rtl::Bootstrap::expandMacros(s); //TODO: detect failure
122 return s;
125 bool canRemoveFromLayer(int layer, rtl::Reference< Node > const & node) {
126 assert(node.is());
127 if (node->getLayer() > layer && node->getLayer() < Data::NO_LAYER) {
128 return false;
130 switch (node->kind()) {
131 case Node::KIND_LOCALIZED_PROPERTY:
132 case Node::KIND_GROUP:
133 for (auto const& member : node->getMembers())
135 if (!canRemoveFromLayer(layer, member.second)) {
136 return false;
139 return true;
140 case Node::KIND_SET:
141 return node->getMembers().empty();
142 default: // Node::KIND_PROPERTY, Node::KIND_LOCALIZED_VALUE
143 return true;
149 class Components::WriteThread: public salhelper::Thread {
150 public:
151 WriteThread(
152 rtl::Reference< WriteThread > * reference, Components & components,
153 OUString const & url, Data const & data);
155 void flush() { delay_.set(); }
157 private:
158 virtual ~WriteThread() override {}
160 virtual void execute() override;
162 rtl::Reference< WriteThread > * reference_;
163 Components & components_;
164 OUString url_;
165 Data const & data_;
166 osl::Condition delay_;
167 std::shared_ptr<osl::Mutex> lock_;
170 Components::WriteThread::WriteThread(
171 rtl::Reference< WriteThread > * reference, Components & components,
172 OUString const & url, Data const & data):
173 Thread("configmgrWriter"), reference_(reference), components_(components),
174 url_(url), data_(data),
175 lock_( lock() )
177 assert(reference != nullptr);
180 void Components::WriteThread::execute() {
181 delay_.wait(std::chrono::seconds(1)); // must not throw; result_error is harmless and ignored
182 osl::MutexGuard g(*lock_); // must not throw
183 try {
184 try {
185 writeModFile(components_, url_, data_);
186 } catch (css::uno::RuntimeException &) {
187 // Ignore write errors, instead of aborting:
188 TOOLS_WARN_EXCEPTION("configmgr", "error writing modifications");
190 } catch (...) {
191 reference_->clear();
192 throw;
194 reference_->clear();
197 Components & Components::getSingleton(
198 css::uno::Reference< css::uno::XComponentContext > const & context)
200 assert(context.is());
201 static Components singleton(context);
202 return singleton;
205 bool Components::allLocales(std::u16string_view locale) {
206 return locale == u"*";
209 rtl::Reference< Node > Components::resolvePathRepresentation(
210 OUString const & pathRepresentation,
211 OUString * canonicRepresentation, std::vector<OUString> * path, int * finalizedLayer)
212 const
214 return data_.resolvePathRepresentation(
215 pathRepresentation, canonicRepresentation, path, finalizedLayer);
218 rtl::Reference< Node > Components::getTemplate(OUString const & fullName) const
220 return data_.getTemplate(Data::NO_LAYER, fullName);
223 void Components::addRootAccess(rtl::Reference< RootAccess > const & access) {
224 roots_.insert(access.get());
227 void Components::removeRootAccess(RootAccess * access) {
228 roots_.erase(access);
231 void Components::initGlobalBroadcaster(
232 Modifications const & modifications,
233 rtl::Reference< RootAccess > const & exclude, Broadcaster * broadcaster)
235 //TODO: Iterate only over roots w/ listeners:
236 for (auto const& elemRoot : roots_)
238 rtl::Reference< RootAccess > root;
239 if (elemRoot->acquireCounting() > 1) {
240 root.set(elemRoot); // must not throw
242 elemRoot->releaseNondeleting();
243 if (root.is()) {
244 if (root != exclude) {
245 std::vector<OUString> path(root->getAbsolutePath());
246 Modifications::Node const * mods = &modifications.getRoot();
247 for (auto const& pathElem : path)
249 Modifications::Node::Children::const_iterator k(
250 mods->children.find(pathElem));
251 if (k == mods->children.end()) {
252 mods = nullptr;
253 break;
255 mods = &k->second;
257 //TODO: If the complete tree of which root is a part is deleted,
258 // or replaced, mods will be null, but some of the listeners
259 // from within root should probably fire nonetheless:
260 if (mods != nullptr) {
261 root->initBroadcaster(*mods, broadcaster);
268 void Components::addModification(std::vector<OUString> const & path) {
269 data_.modifications.add(path);
272 void Components::writeModifications() {
274 if (data_.modifications.empty())
275 return;
277 switch (modificationTarget_) {
278 case ModificationTarget::None:
279 break;
280 case ModificationTarget::File:
281 if (!writeThread_.is()) {
282 writeThread_ = new WriteThread(
283 &writeThread_, *this, modificationFileUrl_, data_);
284 writeThread_->launch();
286 break;
287 case ModificationTarget::Dconf:
288 #if ENABLE_DCONF
289 dconf::writeModifications(*this, data_);
290 #endif
291 break;
295 void Components::flushModifications() {
296 rtl::Reference< WriteThread > thread;
298 osl::MutexGuard g(*lock_);
299 thread = writeThread_;
301 if (thread.is()) {
302 thread->flush();
303 thread->join();
307 void Components::insertExtensionXcsFile(
308 bool shared, OUString const & fileUri)
310 int layer = getExtensionLayer(shared);
311 try {
312 parseXcsFile(fileUri, layer, data_, nullptr, nullptr, nullptr);
313 } catch (css::container::NoSuchElementException & e) {
314 throw css::uno::RuntimeException(
315 "insertExtensionXcsFile does not exist: " + e.Message);
319 void Components::insertExtensionXcuFile(
320 bool shared, OUString const & fileUri, Modifications * modifications)
322 assert(modifications != nullptr);
323 int layer = getExtensionLayer(shared) + 1;
324 Additions * adds = data_.addExtensionXcuAdditions(fileUri, layer);
325 try {
326 parseXcuFile(fileUri, layer, data_, nullptr, modifications, adds);
327 } catch (css::container::NoSuchElementException & e) {
328 data_.removeExtensionXcuAdditions(fileUri);
329 throw css::uno::RuntimeException(
330 "insertExtensionXcuFile does not exist: " + e.Message);
334 void Components::removeExtensionXcuFile(
335 OUString const & fileUri, Modifications * modifications)
337 //TODO: Ideally, exactly the data coming from the specified xcu file would
338 // be removed. However, not enough information is recorded in the in-memory
339 // data structures to do so. So, as a workaround, all those set elements
340 // that were freshly added by the xcu and have afterwards been left
341 // unchanged or have only had their properties changed in the user layer are
342 // removed (and nothing else). The heuristic to determine
343 // whether a node has been left unchanged is to check the layer ID (as
344 // usual) and additionally to check that the node does not recursively
345 // contain any non-empty sets (multiple extension xcu files are merged into
346 // one layer, so checking layer ID alone is not enough). Since
347 // item->additions records all additions of set members in textual order,
348 // the latter check works well when iterating through item->additions in
349 // reverse order.
350 assert(modifications != nullptr);
351 rtl::Reference< Data::ExtensionXcu > item(
352 data_.removeExtensionXcuAdditions(fileUri));
353 if (!item.is())
354 return;
356 for (Additions::reverse_iterator i(item->additions.rbegin());
357 i != item->additions.rend(); ++i)
359 rtl::Reference< Node > parent;
360 NodeMap const * map = &data_.getComponents();
361 rtl::Reference< Node > node;
362 for (auto const& j : *i)
364 parent = node;
365 node = map->findNode(Data::NO_LAYER, j);
366 if (!node.is()) {
367 break;
369 map = &node->getMembers();
371 if (node.is()) {
372 assert(parent.is());
373 if (parent->kind() == Node::KIND_SET) {
374 assert(
375 node->kind() == Node::KIND_GROUP ||
376 node->kind() == Node::KIND_SET);
377 if (canRemoveFromLayer(item->layer, node)) {
378 parent->getMembers().erase(i->back());
379 data_.modifications.remove(*i);
380 modifications->add(*i);
385 writeModifications();
388 void Components::insertModificationXcuFile(
389 OUString const & fileUri,
390 std::set< OUString > const & includedPaths,
391 std::set< OUString > const & excludedPaths,
392 Modifications * modifications)
394 assert(modifications != nullptr);
395 Partial part(includedPaths, excludedPaths);
396 try {
397 parseFileLeniently(
398 &parseXcuFile, fileUri, Data::NO_LAYER, &part, modifications, nullptr);
399 } catch (const css::container::NoSuchElementException &) {
400 TOOLS_WARN_EXCEPTION(
401 "configmgr",
402 "error inserting non-existing \"" << fileUri << "\"");
406 css::beans::Optional< css::uno::Any > Components::getExternalValue(
407 OUString const & descriptor)
409 sal_Int32 i = descriptor.indexOf(' ');
410 if (i <= 0) {
411 throw css::uno::RuntimeException(
412 "bad external value descriptor " + descriptor);
414 //TODO: Do not make calls with mutex locked:
415 OUString name(descriptor.copy(0, i));
416 ExternalServices::iterator j(externalServices_.find(name));
417 if (j == externalServices_.end()) {
418 css::uno::Reference< css::uno::XInterface > service;
419 try {
420 service = context_->getServiceManager()->createInstanceWithContext(
421 name, context_);
422 } catch (const css::uno::RuntimeException &) {
423 // Assuming these exceptions are real errors:
424 throw;
425 } catch (const css::uno::Exception &) {
426 // Assuming these exceptions indicate that the service is not
427 // installed:
428 TOOLS_WARN_EXCEPTION(
429 "configmgr",
430 "createInstance(" << name << ") failed");
432 css::uno::Reference< css::beans::XPropertySet > propset;
433 if (service.is()) {
434 propset.set( service, css::uno::UNO_QUERY_THROW);
436 j = externalServices_.emplace(name, propset).first;
438 css::beans::Optional< css::uno::Any > value;
439 if (j->second.is()) {
440 try {
441 if (!(j->second->getPropertyValue(descriptor.copy(i + 1)) >>=
442 value))
444 throw css::uno::RuntimeException(
445 "cannot obtain external value through " + descriptor);
447 } catch (css::beans::UnknownPropertyException & e) {
448 throw css::uno::RuntimeException(
449 "unknown external value descriptor ID: " + e.Message);
450 } catch (css::lang::WrappedTargetException & e) {
451 css::uno::Any anyEx = cppu::getCaughtException();
452 throw css::lang::WrappedTargetRuntimeException(
453 "cannot obtain external value: " + e.Message,
454 nullptr, anyEx );
457 return value;
460 Components::Components(
461 css::uno::Reference< css::uno::XComponentContext > const & context):
462 context_(context), sharedExtensionLayer_(-1), userExtensionLayer_(-1),
463 modificationTarget_(ModificationTarget::None)
465 assert(context.is());
466 lock_ = lock();
467 OUString conf(expand("${CONFIGURATION_LAYERS}"));
468 int layer = 0;
469 for (sal_Int32 i = 0;;) {
470 while (i != conf.getLength() && conf[i] == ' ') {
471 ++i;
473 if (i == conf.getLength()) {
474 break;
476 if (modificationTarget_ != ModificationTarget::None) {
477 throw css::uno::RuntimeException(
478 "CONFIGURATION_LAYERS: modification target layer followed by"
479 " further layers");
481 sal_Int32 c = i;
482 for (;; ++c) {
483 if (c == conf.getLength() || conf[c] == ' ') {
484 throw css::uno::RuntimeException(
485 "CONFIGURATION_LAYERS: missing ':' in \"" + conf + "\"");
487 if (conf[c] == ':') {
488 break;
491 sal_Int32 n = conf.indexOf(' ', c + 1);
492 if (n == -1) {
493 n = conf.getLength();
495 OUString type(conf.copy(i, c - i));
496 OUString url(conf.copy(c + 1, n - c - 1));
497 if (type == "xcsxcu") {
498 sal_uInt32 nStartTime = osl_getGlobalTimer();
499 parseXcsXcuLayer(layer, url);
500 SAL_INFO("configmgr", "parseXcsXcuLayer() took " << (osl_getGlobalTimer() - nStartTime) << " ms");
501 layer += 2; //TODO: overflow
502 } else if (type == "bundledext") {
503 parseXcsXcuIniLayer(layer, url, false);
504 layer += 2; //TODO: overflow
505 } else if (type == "sharedext") {
506 if (sharedExtensionLayer_ != -1) {
507 throw css::uno::RuntimeException(
508 "CONFIGURATION_LAYERS: multiple \"sharedext\" layers");
510 sharedExtensionLayer_ = layer;
511 parseXcsXcuIniLayer(layer, url, true);
512 layer += 2; //TODO: overflow
513 } else if (type == "userext") {
514 if (userExtensionLayer_ != -1) {
515 throw css::uno::RuntimeException(
516 "CONFIGURATION_LAYERS: multiple \"userext\" layers");
518 userExtensionLayer_ = layer;
519 parseXcsXcuIniLayer(layer, url, true);
520 layer += 2; //TODO: overflow
521 } else if (type == "res") {
522 sal_uInt32 nStartTime = osl_getGlobalTimer();
523 parseResLayer(layer, url);
524 SAL_INFO("configmgr", "parseResLayer() took " << (osl_getGlobalTimer() - nStartTime) << " ms");
525 ++layer; //TODO: overflow
526 #if ENABLE_DCONF
527 } else if (type == "dconf") {
528 if (url == "!") {
529 modificationTarget_ = ModificationTarget::Dconf;
530 dconf::readLayer(data_, Data::NO_LAYER);
531 } else if (url == "*") {
532 dconf::readLayer(data_, layer);
533 } else {
534 throw css::uno::RuntimeException(
535 "CONFIGURATION_LAYERS: unknown \"dconf\" kind \"" + url
536 + "\"");
538 ++layer; //TODO: overflow
539 #endif
540 #if defined(_WIN32)
541 } else if (type == "winreg") {
542 WinRegType eType;
543 if (url == "LOCAL_MACHINE" || url.isEmpty()/*backwards comp.*/) {
544 eType = WinRegType::LOCAL_MACHINE;
545 } else if (url == "CURRENT_USER") {
546 eType = WinRegType::CURRENT_USER;
547 } else {
548 throw css::uno::RuntimeException(
549 "CONFIGURATION_LAYERS: unknown \"winreg\" kind \"" + url
550 + "\"");
552 OUString aTempFileURL;
553 if (dumpWindowsRegistry(&aTempFileURL, eType)) {
554 parseFileLeniently(&parseXcuFile, aTempFileURL, layer, nullptr, nullptr, nullptr);
555 if (!getenv("SAL_CONFIG_WINREG_RETAIN_TMP"))
556 osl::File::remove(aTempFileURL);
558 ++layer; //TODO: overflow
559 #endif
560 } else if (type == "user") {
561 bool write;
562 if (url.startsWith("!", &url)) {
563 write = true;
564 } else if (url.startsWith("*", &url)) {
565 write = false;
566 } else {
567 write = true; // for backwards compatibility
569 if (url.isEmpty()) {
570 throw css::uno::RuntimeException(
571 "CONFIGURATION_LAYERS: empty \"user\" URL");
573 bool ignore = false;
574 #if ENABLE_DCONF
575 if (write) {
576 OUString token(
577 expand("${SYSUSERCONFIG}/libreoffice/dconfwrite"));
578 osl::DirectoryItem it;
579 osl::FileBase::RC e = osl::DirectoryItem::get(token, it);
580 ignore = e == osl::FileBase::E_None;
581 SAL_INFO(
582 "configmgr",
583 "dconf write (<" << token << "> " << +e << "): "
584 << int(ignore));
585 if (ignore) {
586 modificationTarget_ = ModificationTarget::Dconf;
589 #endif
590 if (!ignore) {
591 if (write) {
592 modificationTarget_ = ModificationTarget::File;
593 modificationFileUrl_ = url;
595 parseModificationLayer(write ? Data::NO_LAYER : layer, url);
597 ++layer; //TODO: overflow
598 } else {
599 throw css::uno::RuntimeException(
600 "CONFIGURATION_LAYERS: unknown layer type \"" + type + "\"");
602 i = n;
606 Components::~Components()
608 // get flag if _exit was already called which is a sign to not secure user config.
609 // this is used for win only currently where calling _exit() unfortunately still
610 // calls destructors (what is not wanted). May be needed for other systems, too
611 // (unknown yet) but can do no harm
612 const bool bExitWasCalled(comphelper::BackupFileHelper::getExitWasCalled());
614 #ifndef _WIN32
615 // we can add a SAL_WARN here for other systems where the destructor gets called after
616 // an _exit() call. Still safe - the getExitWasCalled() is used, but a hint that _exit
617 // behaves different on a system
618 SAL_WARN_IF(bExitWasCalled, "configmgr", "Components::~Components() called after _exit() call");
619 #endif
621 if (bExitWasCalled)
623 // do not write, re-join threads
624 osl::MutexGuard g(*lock_);
626 if (writeThread_.is())
628 writeThread_->join();
631 else
633 // write changes
634 flushModifications();
637 for (auto const& rootElem : roots_)
639 rootElem->setAlive(false);
643 void Components::parseFileLeniently(
644 FileParser * parseFile, OUString const & url, int layer,
645 Partial const * partial, Modifications * modifications,
646 Additions * additions)
648 assert(parseFile != nullptr);
649 try {
650 (*parseFile)(url, layer, data_, partial, modifications, additions);
651 } catch (const css::container::NoSuchElementException &) {
652 throw;
653 } catch (const css::uno::Exception &) { //TODO: more specific exception catching
654 // Ignore invalid XML files, instead of completely preventing OOo from
655 // starting:
656 TOOLS_WARN_EXCEPTION(
657 "configmgr",
658 "error reading \"" << url << "\"");
662 void Components::parseFiles(
663 int layer, OUString const & extension, FileParser * parseFile,
664 OUString const & url, bool recursive)
666 osl::Directory dir(url);
667 switch (dir.open()) {
668 case osl::FileBase::E_None:
669 break;
670 case osl::FileBase::E_NOENT:
671 if (!recursive) {
672 return;
674 [[fallthrough]];
675 default:
676 throw css::uno::RuntimeException(
677 "cannot open directory " + url);
679 for (;;) {
680 osl::DirectoryItem i;
681 osl::FileBase::RC rc = dir.getNextItem(i, SAL_MAX_UINT32);
682 if (rc == osl::FileBase::E_NOENT) {
683 break;
685 if (rc != osl::FileBase::E_None) {
686 throw css::uno::RuntimeException(
687 "cannot iterate directory " + url);
689 osl::FileStatus stat(
690 osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
691 osl_FileStatus_Mask_FileURL);
692 if (i.getFileStatus(stat) != osl::FileBase::E_None) {
693 throw css::uno::RuntimeException(
694 "cannot stat in directory " + url);
696 if (stat.getFileType() == osl::FileStatus::Directory) { //TODO: symlinks
697 parseFiles(layer, extension, parseFile, stat.getFileURL(), true);
698 } else {
699 OUString file(stat.getFileName());
700 if (file.endsWith(extension)) {
701 try {
702 parseFileLeniently(
703 parseFile, stat.getFileURL(), layer, nullptr, nullptr, nullptr);
704 } catch (css::container::NoSuchElementException & e) {
705 if (stat.getFileType() == osl::FileStatus::Link) {
706 SAL_WARN("configmgr", "dangling link <" << stat.getFileURL() << ">");
707 continue;
709 throw css::uno::RuntimeException(
710 "stat'ed file does not exist: " + e.Message);
717 void Components::parseFileList(
718 int layer, FileParser * parseFile, OUString const & urls,
719 bool recordAdditions)
721 for (sal_Int32 i = 0;;) {
722 OUString url(urls.getToken(0, ' ', i));
723 if (!url.isEmpty()) {
724 Additions * adds = nullptr;
725 if (recordAdditions) {
726 adds = data_.addExtensionXcuAdditions(url, layer);
728 try {
729 parseFileLeniently(parseFile, url, layer, nullptr, nullptr, adds);
730 } catch (const css::container::NoSuchElementException &) {
731 TOOLS_WARN_EXCEPTION("configmgr", "file does not exist");
732 if (adds != nullptr) {
733 data_.removeExtensionXcuAdditions(url);
737 if (i == -1) {
738 break;
743 void Components::parseXcdFiles(int layer, OUString const & url) {
744 osl::Directory dir(url);
745 switch (dir.open()) {
746 case osl::FileBase::E_None:
747 break;
748 case osl::FileBase::E_NOENT:
749 return;
750 default:
751 throw css::uno::RuntimeException(
752 "cannot open directory " + url);
754 UnresolvedVector unres;
755 std::set< OUString > existingDeps;
756 std::set< OUString > processedDeps;
757 for (;;) {
758 osl::DirectoryItem i;
759 osl::FileBase::RC rc = dir.getNextItem(i, SAL_MAX_UINT32);
760 if (rc == osl::FileBase::E_NOENT) {
761 break;
763 if (rc != osl::FileBase::E_None) {
764 throw css::uno::RuntimeException(
765 "cannot iterate directory " + url);
767 osl::FileStatus stat(
768 osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
769 osl_FileStatus_Mask_FileURL);
770 if (i.getFileStatus(stat) != osl::FileBase::E_None) {
771 throw css::uno::RuntimeException(
772 "cannot stat in directory " + url);
774 if (stat.getFileType() != osl::FileStatus::Directory) { //TODO: symlinks
775 OUString file(stat.getFileName());
776 OUString name;
777 if (file.endsWith(".xcd", &name)) {
778 existingDeps.insert(name);
779 rtl::Reference< ParseManager > manager;
780 try {
781 manager = new ParseManager(
782 stat.getFileURL(),
783 new XcdParser(layer, processedDeps, data_));
784 } catch (css::container::NoSuchElementException & e) {
785 if (stat.getFileType() == osl::FileStatus::Link) {
786 SAL_WARN("configmgr", "dangling link <" << stat.getFileURL() << ">");
787 continue;
789 throw css::uno::RuntimeException(
790 "stat'ed file does not exist: " + e.Message);
792 if (manager->parse(nullptr)) {
793 processedDeps.insert(name);
794 } else {
795 unres.emplace_back(name, manager);
800 while (!unres.empty()) {
801 bool resolved = false;
802 for (UnresolvedVector::iterator i(unres.begin()); i != unres.end();) {
803 if (i->manager->parse(&existingDeps)) {
804 processedDeps.insert(i->name);
805 i = unres.erase(i);
806 resolved = true;
807 } else {
808 ++i;
811 if (!resolved) {
812 throw css::uno::RuntimeException(
813 "xcd: unresolved dependencies in " + url);
818 void Components::parseXcsXcuLayer(int layer, OUString const & url) {
819 parseXcdFiles(layer, url);
820 parseFiles(layer, ".xcs", &parseXcsFile, url + "/schema", false);
821 parseFiles(layer + 1, ".xcu", &parseXcuFile, url + "/data", false);
824 void Components::parseXcsXcuIniLayer(
825 int layer, OUString const & url, bool recordAdditions)
827 // Check if ini file exists (otherwise .override would still read global
828 // SCHEMA/DATA variables, which could interfere with unrelated environment
829 // variables):
830 if (rtl::Bootstrap(url).getHandle() == nullptr) return;
832 OUStringBuffer prefix("${.override:");
833 for (sal_Int32 i = 0; i != url.getLength(); ++i) {
834 sal_Unicode c = url[i];
835 switch (c) {
836 case '$':
837 case ':':
838 case '\\':
839 prefix.append('\\');
840 [[fallthrough]];
841 default:
842 prefix.append(c);
845 prefix.append(':');
846 OUString urls(prefix.toString() + "SCHEMA}");
847 rtl::Bootstrap::expandMacros(urls);
848 if (!urls.isEmpty()) {
849 parseFileList(layer, &parseXcsFile, urls, false);
851 urls = prefix.makeStringAndClear() + "DATA}";
852 rtl::Bootstrap::expandMacros(urls);
853 if (!urls.isEmpty()) {
854 parseFileList(layer + 1, &parseXcuFile, urls, recordAdditions);
858 void Components::parseResLayer(int layer, std::u16string_view url) {
859 OUString resUrl(OUString::Concat(url) + "/res");
860 parseXcdFiles(layer, resUrl);
861 parseFiles(layer, ".xcu", &parseXcuFile, resUrl, false);
864 void Components::parseModificationLayer(int layer, OUString const & url) {
865 try {
866 parseFileLeniently(&parseXcuFile, url, layer, nullptr, nullptr, nullptr);
867 } catch (css::container::NoSuchElementException &) {
868 SAL_INFO(
869 "configmgr", "user registrymodifications.xcu does not (yet) exist");
870 // Migrate old user layer data (can be removed once migration is no
871 // longer relevant, probably OOo 4; also see hack for xsi namespace in
872 // xmlreader::XmlReader::registerNamespaceIri):
873 parseFiles(
874 layer, ".xcu", &parseXcuFile,
875 expand(
876 "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap")
877 ":UserInstallation}/user/registry/data"),
878 false);
882 int Components::getExtensionLayer(bool shared) const {
883 int layer = shared ? sharedExtensionLayer_ : userExtensionLayer_;
884 if (layer == -1) {
885 throw css::uno::RuntimeException(
886 "insert extension xcs/xcu file into undefined layer");
888 return layer;
893 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */