Skip to content

Commit

Permalink
Prevent initialization/destruction order fiasco
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Jan 28, 2022
1 parent 7acb8e0 commit 020abd3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
13 changes: 0 additions & 13 deletions include/ignition/common/SystemPaths.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@
#ifndef IGNITION_COMMON_SYSTEMPATHS_HH_
#define IGNITION_COMMON_SYSTEMPATHS_HH_

#include <stdio.h>

#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif

#include <functional>
#include <list>
#include <string>
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(fs::path::preferred_separator));
}

Expand All @@ -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<char>(fs::path::preferred_separator), '/');
}

Expand Down
26 changes: 10 additions & 16 deletions src/SystemPaths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
#include <string>
#include <vector>

#ifndef _WIN32
#include <dirent.h>
#else
#include "win_dirent.h"
#endif

#include "ignition/common/Console.hh"
#include "ignition/common/StringUtils.hh"
#include "ignition/common/SystemPaths.hh"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -465,9 +454,12 @@ std::string SystemPaths::FindFile(const std::string &_filename,
std::string SystemPaths::LocateLocalFile(const std::string &_filename,
const std::vector<std::string> &_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))
{
Expand Down Expand Up @@ -531,6 +523,8 @@ void SystemPaths::AddFindFileURICallback(
/////////////////////////////////////////////////
std::list<std::string> SystemPaths::PathsFromEnv(const std::string &_env)
{
std::cerr << "PathsFromEnv" << std::endl;

std::list<std::string> paths;

std::string envPathsStr;
Expand Down
1 change: 1 addition & 0 deletions src/TempDirectory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ignition/common/Console.hh>

#include <cstdlib>
#include <filesystem>

#ifdef _WIN32
Expand Down
29 changes: 21 additions & 8 deletions src/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <ignition/common/Util.hh>
#include <ignition/common/Uuid.hh>

#include <ignition/utils/NeverDestroyed.hh>

#ifndef _WIN32
#include <dirent.h>
#include <limits.h>
Expand All @@ -55,8 +57,19 @@
static const auto &ignstrtok = strtok_r;
#endif

static std::unique_ptr<ignition::common::SystemPaths> 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<ignition::common::SystemPaths*> paths{
new ignition::common::SystemPaths()
};
return paths.Access();
}

/////////////////////////////////////////////////
// Internal class for SHA1 computation
Expand Down Expand Up @@ -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);
}

/////////////////////////////////////////////////
Expand All @@ -328,13 +341,13 @@ std::string ignition::common::findFilePath(const std::string &_file)
void ignition::common::addFindFileURICallback(
std::function<std::string(const ignition::common::URI &)> _cb)
{
gSystemPaths->AddFindFileURICallback(_cb);
GetSystemPaths()->AddFindFileURICallback(_cb);
}

/////////////////////////////////////////////////
ignition::common::SystemPaths *ignition::common::systemPaths()
{
return gSystemPaths.get();
return GetSystemPaths();
}

/////////////////////////////////////////////////
Expand Down

0 comments on commit 020abd3

Please sign in to comment.