forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempFile.cpp
More file actions
47 lines (38 loc) · 901 Bytes
/
TempFile.cpp
File metadata and controls
47 lines (38 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "TempFile.h"
#include "../logging/logging.h"
#include <fstream>
namespace bf = boost::filesystem;
using std::ofstream;
using namespace cpputils::logging;
namespace cpputils {
TempFile::TempFile(const bf::path &path, bool create)
: _path(path) {
if (create) {
ofstream file(_path.string().c_str());
if (!file.good()) {
throw std::runtime_error("Could not create tempfile");
}
}
}
TempFile::TempFile(bool create)
: TempFile(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%"), create) {
}
TempFile::~TempFile() {
try {
if (exists()) {
remove();
}
} catch (const boost::filesystem::filesystem_error &e) {
LOG(ERR, "Could not delete tempfile.");
}
}
void TempFile::remove() {
bf::remove(_path);
}
bool TempFile::exists() const {
return bf::exists(_path);
}
const bf::path &TempFile::path() const {
return _path;
}
}