libcryfs/tempfile/TempFile.cpp

44 lines
855 B
C++
Raw Normal View History

2015-04-25 02:40:02 +02:00
#include "TempFile.h"
#include "../logging/logging.h"
2015-04-25 02:40:02 +02:00
#include <fstream>
namespace bf = boost::filesystem;
using std::ofstream;
using namespace cpputils::logging;
2015-04-25 02:40:02 +02:00
namespace cpputils {
TempFile::TempFile(const bf::path &path, bool create)
: _path(path) {
if (create) {
ofstream file(_path.c_str());
2015-10-30 18:10:48 +01:00
if (!file.good()) {
throw std::runtime_error("Could not create tempfile");
}
2015-04-25 02:40:02 +02:00
}
}
TempFile::TempFile(bool create)
: TempFile(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%"), create) {
}
TempFile::~TempFile() {
try {
if (exists()) {
bf::remove(_path);
}
} catch (const boost::filesystem::filesystem_error &e) {
LOG(ERROR) << "Could not delete tempfile.";
2015-04-25 02:40:02 +02:00
}
}
2015-10-23 12:15:51 +02:00
bool TempFile::exists() const {
return bf::exists(_path);
}
2015-04-25 02:40:02 +02:00
const bf::path &TempFile::path() const {
return _path;
}
}