TempDir/TempFile don't fail if they can't delete themselves

This commit is contained in:
Sebastian Messmer 2015-10-30 18:27:40 +01:00
parent 6836a1bd40
commit 4ced78b07b
3 changed files with 17 additions and 8 deletions

View File

@ -1,11 +1,13 @@
#include "TempDir.h" #include "TempDir.h"
#include "../logging/logging.h"
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using namespace cpputils::logging;
namespace cpputils { namespace cpputils {
TempDir::TempDir() TempDir::TempDir()
: _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")), _existing(true) { : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")) {
bf::create_directory(_path); bf::create_directory(_path);
} }
@ -14,9 +16,12 @@ TempDir::~TempDir() {
} }
void TempDir::remove() { void TempDir::remove() {
if (_existing) { try {
if (bf::exists(_path)) {
bf::remove_all(_path); bf::remove_all(_path);
_existing = false; }
} catch (const boost::filesystem::filesystem_error &e) {
LOG(ERROR) << "Could not delete tempfile.";
} }
} }

View File

@ -16,7 +16,6 @@ public:
private: private:
const boost::filesystem::path _path; const boost::filesystem::path _path;
bool _existing;
DISALLOW_COPY_AND_ASSIGN(TempDir); DISALLOW_COPY_AND_ASSIGN(TempDir);
}; };

View File

@ -1,9 +1,10 @@
#include "TempFile.h" #include "TempFile.h"
#include "../logging/logging.h"
#include <fstream> #include <fstream>
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using std::ofstream; using std::ofstream;
using namespace cpputils::logging;
namespace cpputils { namespace cpputils {
@ -22,9 +23,13 @@ TempFile::TempFile(bool create)
} }
TempFile::~TempFile() { TempFile::~TempFile() {
try {
if (exists()) { if (exists()) {
bf::remove(_path); bf::remove(_path);
} }
} catch (const boost::filesystem::filesystem_error &e) {
LOG(ERROR) << "Could not delete tempfile.";
}
} }
bool TempFile::exists() const { bool TempFile::exists() const {