diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp index b1c1a9c3e0e8d5a8ffc7070b7599e0a76e80aa70..7ad69f6b5d884882f198f0f09b9e0f9448743af4 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 7ba0820d6fbaf21b2ef25e49f84063c5e8f6751e..6fa60a54e6d283c4e8852d25ca1dfeb4161c0652 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