Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault in case of no write access to log dir #546

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/gz/common/Filesystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ namespace ignition
/// \return True if directory creation was successful, false otherwise.
bool IGNITION_COMMON_VISIBLE createDirectory(const std::string &_path);

/// \brief Create directories for the given path
/// \brief Create directories for the given path errors are printed to the given stream
/// \param[in] _path Path to create directories from
/// \param[in] _errorOut Stream for error output
/// \return true on success
bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path, std::ostream &_errorOut);

/// \brief Create directories for the given path errors are printed on ignerr
/// \param[in] _path Path to create directories from
/// \return true on success
bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path);
Expand Down
11 changes: 8 additions & 3 deletions src/Console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ void FileLogger::Init(const std::string &_directory,
{
if (!env(IGN_HOMEDIR, logPath))
{
ignerr << "Missing HOME environment variable."
<< "No log file will be generated.";
std::cerr << "Missing HOME environment variable. "
<< "No log file will be generated.";
return;
}
logPath = joinPaths(logPath, _directory);
Expand All @@ -275,7 +275,12 @@ void FileLogger::Init(const std::string &_directory,
auto* buf = dynamic_cast<FileLogger::Buffer*>(this->rdbuf());

// Create the directory if it doesn't exist.
createDirectories(logPath);
if(!createDirectories(logPath, std::cerr))
{
std::cerr << "Failed to generate log directories. "
<< "No log file will be generated.";
return;
}

logPath = joinPaths(logPath, _filename);

Expand Down
2 changes: 1 addition & 1 deletion src/EnumIface_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ IGN_ENUM(myTypeIface, MyType, MY_TYPE_BEGIN, MY_TYPE_END,
/////////////////////////////////////////////////
TEST_F(EnumIfaceTest, StringCoversion)
{
MyType type;
MyType type = MyType::TYPE2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to the rest of the pull request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is a separate commit fixing a compile warning.


// Set value from string
myTypeIface.Set(type, "TYPE1");
Expand Down
10 changes: 8 additions & 2 deletions src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ bool common::copyDirectory(const std::string &_existingDirname,

/////////////////////////////////////////////////
bool common::createDirectories(const std::string &_path)
{
return createDirectories(_path, ignerr);
}

/////////////////////////////////////////////////
bool common::createDirectories(const std::string &_path, std::ostream &_errorOut)
{
size_t index = 0;
while (index < _path.size())
Expand All @@ -482,8 +488,8 @@ bool common::createDirectories(const std::string &_path)
if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
#endif
ignerr << "Failed to create directory [" + dir + "]: "
<< std::strerror(errno) << std::endl;
_errorOut << "Failed to create directory [" + dir + "]: "
<< std::strerror(errno) << std::endl;
return false;
}
}
Expand Down