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 "../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.";
}
}

View File

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

View File

@ -1,9 +1,10 @@
#include "TempFile.h"
#include "../logging/logging.h"
#include <fstream>
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.";
}
}