forked from ttalvitie/browservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_dir.cpp
More file actions
54 lines (44 loc) · 1.17 KB
/
Copy pathtemp_dir.cpp
File metadata and controls
54 lines (44 loc) · 1.17 KB
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
48
49
50
51
52
53
54
#include "temp_dir.hpp"
#ifdef _WIN32
#include <fileapi.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace browservice {
TempDir::TempDir(CKey) {
#ifdef _WIN32
const DWORD bufSize = MAX_PATH + 2;
wchar_t baseBuf[bufSize];
DWORD baseLen = GetTempPathW(bufSize, baseBuf);
REQUIRE(baseLen > 0);
wstring path(baseBuf, (size_t)baseLen);
mt19937 rng = createRNG();
path += L"browservicetmp_";
wstring charPalette = L"abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV0123456789";
for(int i = 0; i < 16; ++i) {
wchar_t c = charPalette[uniform_int_distribution<size_t>(0, charPalette.size() - 1)(rng)];
path.push_back(c);
}
REQUIRE(CreateDirectoryW(path.c_str(), nullptr));
#else
char path[] = "/tmp/browservicetmp_XXXXXX";
REQUIRE(mkdtemp(path) != nullptr);
#endif
path_ = path;
}
TempDir::~TempDir() {
#ifdef _WIN32
if(!RemoveDirectoryW(path_.c_str())) {
WARNING_LOG("Deleting temporary directory ", path_, " failed");
}
#else
if(rmdir(path_.c_str())) {
WARNING_LOG("Deleting temporary directory ", path_, " failed");
}
#endif
}
const PathStr& TempDir::path() {
return path_;
}
}