From 7d00c92bc8fa93544b51e84bb5f2d2e4db3527d6 Mon Sep 17 00:00:00 2001 From: "Dmitry Yu. Naumov" <github@naumov.de> Date: Mon, 12 Nov 2018 19:12:00 +0100 Subject: [PATCH] [BL] Add removeFiles(). --- BaseLib/FileTools.cpp | 22 ++++++++++++++++++++++ BaseLib/FileTools.h | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp index b1c1a9c3e0e..7ad69f6b5d8 100644 --- a/BaseLib/FileTools.cpp +++ b/BaseLib/FileTools.cpp @@ -204,4 +204,26 @@ void setProjectDirectory(std::string const& dir) project_directory_is_set = true; } +void removeFiles(std::vector<std::string> const& files) +{ + for (auto const& file : files) + { + int const success = std::remove(file.c_str()); + if (success == 0) + { + DBUG("Removed '%s'", file.c_str()); + } + else + { + if (errno == ENOENT) // File does not exists + { + continue; + } + ERR("Removing file '%s' failed with error %d.", file.c_str(), + errno); + std::perror("Error: "); + OGS_FATAL("Unrecoverable error happened while removing a file."); + } + } +} } // end namespace BaseLib diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h index 7ba0820d6fb..6fa60a54e6d 100644 --- a/BaseLib/FileTools.h +++ b/BaseLib/FileTools.h @@ -167,4 +167,7 @@ std::string const& getProjectDirectory(); /// Sets the project directory. void setProjectDirectory(std::string const& dir); -} // end namespace BaseLib +/// Remove files. If a file does not exist nothing will happen, other errors +/// lead to OGS_FATAL call. +void removeFiles(std::vector<std::string> const& files); +} // end namespace BaseLib -- GitLab