From 4ced78b07b018127bceeb9217b5078f1d11fa41c Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 30 Oct 2015 18:27:40 +0100 Subject: [PATCH] TempDir/TempFile don't fail if they can't delete themselves --- tempfile/TempDir.cpp | 13 +++++++++---- tempfile/TempDir.h | 1 - tempfile/TempFile.cpp | 11 ++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/tempfile/TempDir.cpp b/tempfile/TempDir.cpp index f5545dc0..7d4d0838 100644 --- a/tempfile/TempDir.cpp +++ b/tempfile/TempDir.cpp @@ -1,11 +1,13 @@ #include "TempDir.h" +#include "../logging/logging.h" namespace bf = boost::filesystem; +using namespace cpputils::logging; namespace cpputils { TempDir::TempDir() - : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")), _existing(true) { + : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")) { bf::create_directory(_path); } @@ -14,9 +16,12 @@ TempDir::~TempDir() { } void TempDir::remove() { - if (_existing) { - bf::remove_all(_path); - _existing = false; + try { + if (bf::exists(_path)) { + bf::remove_all(_path); + } + } catch (const boost::filesystem::filesystem_error &e) { + LOG(ERROR) << "Could not delete tempfile."; } } diff --git a/tempfile/TempDir.h b/tempfile/TempDir.h index bde0b6b6..361788b1 100644 --- a/tempfile/TempDir.h +++ b/tempfile/TempDir.h @@ -16,7 +16,6 @@ public: private: const boost::filesystem::path _path; - bool _existing; DISALLOW_COPY_AND_ASSIGN(TempDir); }; diff --git a/tempfile/TempFile.cpp b/tempfile/TempFile.cpp index 14db0081..ea2e3cf4 100644 --- a/tempfile/TempFile.cpp +++ b/tempfile/TempFile.cpp @@ -1,9 +1,10 @@ #include "TempFile.h" - +#include "../logging/logging.h" #include namespace bf = boost::filesystem; using std::ofstream; +using namespace cpputils::logging; namespace cpputils { @@ -22,8 +23,12 @@ TempFile::TempFile(bool create) } TempFile::~TempFile() { - if (exists()) { - bf::remove(_path); + try { + if (exists()) { + bf::remove(_path); + } + } catch (const boost::filesystem::filesystem_error &e) { + LOG(ERROR) << "Could not delete tempfile."; } }