From 020abd3cefdb3f4fcc8b73fbf589c4240aee0440 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Fri, 28 Jan 2022 12:25:02 -0600 Subject: [PATCH] Prevent initialization/destruction order fiasco Signed-off-by: Michael Carroll --- include/ignition/common/SystemPaths.hh | 13 ------------ src/Filesystem.cc | 4 ++-- src/SystemPaths.cc | 26 +++++++++-------------- src/TempDirectory.cc | 1 + src/Util.cc | 29 +++++++++++++++++++------- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/include/ignition/common/SystemPaths.hh b/include/ignition/common/SystemPaths.hh index 9b8224525..d368dc737 100644 --- a/include/ignition/common/SystemPaths.hh +++ b/include/ignition/common/SystemPaths.hh @@ -17,16 +17,6 @@ #ifndef IGNITION_COMMON_SYSTEMPATHS_HH_ #define IGNITION_COMMON_SYSTEMPATHS_HH_ -#include - -#ifdef _WIN32 - #include - #define GetCurrentDir _getcwd -#else - #include - #define GetCurrentDir getcwd -#endif - #include #include #include @@ -41,9 +31,6 @@ namespace ignition { namespace common { - // Forward declare private data class - class SystemPathsPrivate; - /// \class SystemPaths SystemPaths.hh ignition/common/SystemPaths.hh /// \brief Functions to handle getting system paths, keeps track of: /// \li SystemPaths#pluginPaths - plugin library paths diff --git a/src/Filesystem.cc b/src/Filesystem.cc index b4b512ec3..50f8b9dd8 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -98,7 +98,7 @@ std::string const ignition::common::separator(std::string const &_s) ///////////////////////////////////////////////// void ignition::common::changeFromUnixPath(std::string &_path) { - std::replace(_path.begin(), _path.end(), '/', + std::replace(_path.begin(), _path.end(), '/', static_cast(fs::path::preferred_separator)); } @@ -112,7 +112,7 @@ std::string ignition::common::copyFromUnixPath(const std::string &_path) ///////////////////////////////////////////////// void ignition::common::changeToUnixPath(std::string &_path) { - std::replace(_path.begin(), _path.end(), + std::replace(_path.begin(), _path.end(), static_cast(fs::path::preferred_separator), '/'); } diff --git a/src/SystemPaths.cc b/src/SystemPaths.cc index 20ed8db4c..4358dac55 100644 --- a/src/SystemPaths.cc +++ b/src/SystemPaths.cc @@ -25,12 +25,6 @@ #include #include -#ifndef _WIN32 -#include -#else -#include "win_dirent.h" -#endif - #include "ignition/common/Console.hh" #include "ignition/common/StringUtils.hh" #include "ignition/common/SystemPaths.hh" @@ -101,18 +95,11 @@ SystemPaths::SystemPaths() else fullPath = path; - DIR *dir = opendir(fullPath.c_str()); - if (!dir) + if (!exists(fullPath)) { -#ifdef _WIN32 - mkdir(fullPath.c_str()); -#else - // cppcheck-suppress ConfigurationNotChecked - mkdir(fullPath.c_str(), S_IRWXU | S_IRGRP | S_IROTH); -#endif + createDirectories(fullPath); } - else - closedir(dir); + this->dataPtr->logPath = fullPath; // Populate this->dataPtr->filePaths with values from the default @@ -403,6 +390,8 @@ std::string SystemPaths::FindFile(const std::string &_filename, // Try appending to local paths else { + ignerr << "Here: " << cwd() << " " << filename << std::endl; + auto cwdPath = joinPaths(cwd(), filename); if (_searchLocalPath && exists(cwdPath)) { @@ -465,9 +454,12 @@ std::string SystemPaths::FindFile(const std::string &_filename, std::string SystemPaths::LocateLocalFile(const std::string &_filename, const std::vector &_paths) { + std::cerr << "LocateLocalFile: " << _filename << "\n"; + std::string foundPath = ""; for (auto const &path : _paths) { + std::cout << "\tPath: " << path << "\n"; std::string checkPath = NormalizeDirectoryPath(path) + _filename; if (exists(checkPath)) { @@ -531,6 +523,8 @@ void SystemPaths::AddFindFileURICallback( ///////////////////////////////////////////////// std::list SystemPaths::PathsFromEnv(const std::string &_env) { + std::cerr << "PathsFromEnv" << std::endl; + std::list paths; std::string envPathsStr; diff --git a/src/TempDirectory.cc b/src/TempDirectory.cc index da07f4d9b..13385b337 100644 --- a/src/TempDirectory.cc +++ b/src/TempDirectory.cc @@ -19,6 +19,7 @@ #include +#include #include #ifdef _WIN32 diff --git a/src/Util.cc b/src/Util.cc index de13ac071..71ff0340e 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -36,6 +36,8 @@ #include #include +#include + #ifndef _WIN32 #include #include @@ -55,8 +57,19 @@ static const auto &ignstrtok = strtok_r; #endif -static std::unique_ptr gSystemPaths( - new ignition::common::SystemPaths); +///////////////////////////////////////////////// +/// \brief Global instance of system paths for helper functions below +/// +/// This uses the NeverDestroyed pattern to prevent static initialization and +/// destruction order fiasco issues. +ignition::common::SystemPaths* GetSystemPaths() +{ + static const + ignition::utils::NeverDestroyed paths{ + new ignition::common::SystemPaths() + }; + return paths.Access(); +} ///////////////////////////////////////////////// // Internal class for SHA1 computation @@ -286,26 +299,26 @@ std::string ignition::common::timeToIso( ///////////////////////////////////////////////// std::string ignition::common::logPath() { - return gSystemPaths->LogPath(); + return GetSystemPaths()->LogPath(); } ///////////////////////////////////////////////// void ignition::common::addSearchPathSuffix(const std::string &_suffix) { - gSystemPaths->AddSearchPathSuffix(_suffix); + GetSystemPaths()->AddSearchPathSuffix(_suffix); } ///////////////////////////////////////////////// std::string ignition::common::findFile(const std::string &_file) { - return gSystemPaths->FindFile(_file, true); + return GetSystemPaths()->FindFile(_file, true); } ///////////////////////////////////////////////// std::string ignition::common::findFile(const std::string &_file, const bool _searchLocalPath) { - return gSystemPaths->FindFile(_file, _searchLocalPath); + return GetSystemPaths()->FindFile(_file, _searchLocalPath); } ///////////////////////////////////////////////// @@ -328,13 +341,13 @@ std::string ignition::common::findFilePath(const std::string &_file) void ignition::common::addFindFileURICallback( std::function _cb) { - gSystemPaths->AddFindFileURICallback(_cb); + GetSystemPaths()->AddFindFileURICallback(_cb); } ///////////////////////////////////////////////// ignition::common::SystemPaths *ignition::common::systemPaths() { - return gSystemPaths.get(); + return GetSystemPaths(); } /////////////////////////////////////////////////