-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate_directory.cpp
More file actions
95 lines (78 loc) · 2.92 KB
/
Copy pathcreate_directory.cpp
File metadata and controls
95 lines (78 loc) · 2.92 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-FileCopyrightText: (c) 2024 Ilya Andreev <bugdealer@icloud.com>
// SPDX-License-Identifier: MIT
#include "abi.hpp"
#include <filesystem>
#include <string_view>
#include <system_error>
#ifdef _WIN32
#define UNICODE
#include <Windows.h>
#include <array>
#include <cwchar>
#else
#include <cerrno>
#include <unistd.h>
#endif
namespace tmp::detail {
namespace {
namespace fs = std::filesystem;
/// Checks if the given prefix is valid to attach to a temporary directory name
/// @param prefix The prefix to check validity for
/// @returns `true` if the prefix is valid, `false` otherwise
bool is_prefix_valid(const fs::path& prefix) {
// We also need to check that the prefix does not contain a root path
// because of how path concatenation works in C++
return prefix.empty() || (++prefix.begin() == prefix.end() &&
prefix.is_relative() && !prefix.has_root_path());
}
#ifdef _WIN32
/// Creates a temporary path with the given prefix
/// @note prefix must be valid
/// @param prefix A prefix to attach to the path pattern
/// @returns A unique temporary path
fs::path make_path(std::string_view prefix) {
GUID guid;
CoCreateGuid(&guid);
constexpr static std::size_t CHARS_IN_GUID = 39;
std::array name = std::array<wchar_t, CHARS_IN_GUID>();
const wchar_t* format =
prefix.empty() ? L"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
: L".%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
swprintf(name.data(), name.size(), format, guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
fs::path path = fs::temp_directory_path() / prefix;
path += name.data();
return path;
}
#endif
} // namespace
/// Creates a temporary directory in the current user's temporary directory
/// @param prefix A prefix to attach to the temporary directory name
/// @returns A path to the created temporary directory
/// @throws fs::filesystem_error if cannot create a temporary directory
/// @throws std::invalid_argument if the prefix contains a directory separator
fs::path create_directory(std::string_view prefix) {
if (!is_prefix_valid(prefix)) {
throw std::invalid_argument("Cannot create a temporary directory: "
"prefix cannot contain a directory separator");
}
std::error_code ec;
#ifdef _WIN32
fs::path path = make_path(prefix);
if (!CreateDirectory(path.c_str(), nullptr)) {
ec = std::error_code(GetLastError(), std::system_category());
}
#else
std::string path = fs::temp_directory_path() / prefix;
path += prefix.empty() ? "XXXXXX" : ".XXXXXX";
if (mkdtemp(path.data()) == nullptr) {
ec = std::error_code(errno, std::system_category());
}
#endif
if (ec) {
throw fs::filesystem_error("Cannot create a temporary directory", ec);
}
return fs::canonical(path);
}
} // namespace tmp::detail