diff --git a/src/filesystem.cpp b/src/filesystem.cpp index bf87767f0ac0..3f6bd19bd79f 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -346,6 +346,15 @@ bool delete_directory(const std::string& path, const bool keep_pbl) } return ret; } +bool delete_file(const std::string& path) +{ + bool ret = true; + if(remove(path.c_str()) != 0 && errno != ENOENT) { + ERR_FS << "remove(" << path << "): " << strerror(errno) << "\n"; + ret = false; + } + return ret; +} std::string get_cwd() { diff --git a/src/filesystem.hpp b/src/filesystem.hpp index 1fe0cd1fc1d9..038dd003df27 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -89,6 +89,7 @@ std::string get_exe_dir(); bool make_directory(const std::string& dirname); bool delete_directory(const std::string& dirname, const bool keep_pbl = false); +bool delete_file(const std::string &filename); bool looks_like_pbl(const std::string& file); diff --git a/src/filesystem_boost.cpp b/src/filesystem_boost.cpp index 1ebbae0bc9a9..3b7c7c65ddeb 100644 --- a/src/filesystem_boost.cpp +++ b/src/filesystem_boost.cpp @@ -553,6 +553,16 @@ bool delete_directory(const std::string& dirname, const bool keep_pbl) return ret; } +bool delete_file(const std::string &filename) +{ + error_code ec; + bool ret = bfs::remove(path(filename), ec); + if (ec) { + ERR_FS << "Could not delete file " << filename << ": " << ec.message() << '\n'; + } + return ret; +} + std::string read_file(const std::string &fname) { scoped_istream is = istream_file(fname);