-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate.cpp
More file actions
223 lines (193 loc) · 6.01 KB
/
create.cpp
File metadata and controls
223 lines (193 loc) · 6.01 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "create.hpp"
#include <tmp/filebuf>
#include <filesystem>
#include <stdexcept>
#include <string_view>
#include <system_error>
#include <utility>
#ifdef _WIN32
#define UNICODE
#include <Windows.h>
#include <cwchar>
#else
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#endif
namespace tmp {
namespace {
/// Checks if the given prefix is valid to attach to a temporary directory name
/// @param[in] prefix The prefix to check validity for
/// @returns `true` if the prefix is valid, `false` otherwise
bool is_prefix_valid(const fs::path& prefix) {
return prefix.empty() ||
(++prefix.begin() == prefix.end() && prefix.is_relative() &&
!prefix.has_root_path() && prefix.filename() != "." &&
prefix.filename() != "..");
}
/// Checks that the given prefix is valid to attach to a temporary entry path
/// @param prefix The prefix to check validity for
/// @throws std::invalid_argument if the prefix cannot be attached to a path
void validate_prefix(const fs::path& prefix) {
if (!is_prefix_valid(prefix)) {
throw std::invalid_argument(
"Cannot create a temporary entry: prefix must be empty or a valid "
"single-segmented relative pathname");
}
}
#ifdef _WIN32
/// Creates a temporary path with the given prefix
/// @note prefix must be valid
/// @param[in] prefix A prefix to attach to the path pattern
/// @returns A unique temporary path
fs::path make_path(std::string_view prefix) {
constexpr static std::size_t CHARS_IN_GUID = 39;
GUID guid;
CoCreateGuid(&guid);
wchar_t name[CHARS_IN_GUID];
swprintf(name, CHARS_IN_GUID,
L"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", 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;
return path;
}
#else
/// Creates a temporary path pattern with the given prefix
/// @note prefix must be valid
/// @param[in] prefix A prefix to attach to the path pattern
/// @returns A path pattern for the unique temporary path
fs::path make_pattern(std::string_view prefix) {
fs::path path = fs::temp_directory_path() / prefix;
path += "XXXXXX"; // TODO: add '.', like `com.github.bugdea1er.tmp.yotR2k`?
return path;
}
#endif
#ifdef _WIN32
/// Makes a mode string for the `_wfdopen` function
/// @param mode The file opening mode
/// @returns A suitable mode string
const wchar_t* make_mdstring(std::ios::openmode mode) noexcept {
switch (mode & ~std::ios::ate) {
case std::ios::out:
case std::ios::out | std::ios::trunc:
return L"wx";
case std::ios::out | std::ios::app:
case std::ios::app:
return L"a";
case std::ios::in:
return L"r";
case std::ios::in | std::ios::out:
case std::ios::in | std::ios::out | std::ios::trunc:
return L"w+x";
case std::ios::in | std::ios::out | std::ios::app:
case std::ios::in | std::ios::app:
return L"a+";
case std::ios::out | std::ios::binary:
case std::ios::out | std::ios::trunc | std::ios::binary:
return L"wbx";
case std::ios::out | std::ios::app | std::ios::binary:
case std::ios::app | std::ios::binary:
return L"ab";
case std::ios::in | std::ios::binary:
return L"rb";
case std::ios::in | std::ios::out | std::ios::binary:
case std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary:
return L"w+bx";
case std::ios::in | std::ios::out | std::ios::app | std::ios::binary:
case std::ios::in | std::ios::app | std::ios::binary:
return L"a+b";
default:
return nullptr;
}
}
#endif
/// Closes the given handle, ignoring any errors
/// @param[in] handle The handle to close
void close(filebuf::open_handle_type handle) noexcept {
#ifdef _WIN32
fclose(handle);
#else
::close(handle);
#endif
}
} // namespace
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode) {
std::error_code ec;
std::pair<fs::path, filebuf> file = create_file(mode, ec);
if (ec) {
throw fs::filesystem_error("Cannot create a temporary file", ec);
}
return file;
}
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode,
std::error_code& ec) {
#ifdef _WIN32
fs::path::string_type path = make_path("");
#else
fs::path::string_type path = make_pattern("");
#endif
mode |= std::ios::in | std::ios::out;
#ifdef _WIN32
std::FILE* handle;
// FIXME: use _wfopen_s
if (const wchar_t* mdstr = make_mdstring(mode)) {
handle = _wfopen(path.c_str(), mdstr);
if (handle == nullptr) {
ec = std::error_code(errno, std::system_category());
return {};
}
} else {
ec = std::make_error_code(std::errc::invalid_argument);
return {};
}
#else
int handle = mkstemp(path.data());
if (handle == -1) {
ec = std::error_code(errno, std::system_category());
return {};
}
#endif
filebuf filebuf;
if (filebuf.open(handle, mode) == nullptr) {
close(handle);
ec = std::make_error_code(std::io_errc::stream);
fs::remove(path);
return {};
}
ec.clear();
return std::make_pair(path, std::move(filebuf));
}
fs::path create_directory(std::string_view prefix) {
validate_prefix(prefix); // throws std::invalid_argument with a proper text
std::error_code ec;
fs::path directory = create_directory(prefix, ec);
if (ec) {
throw fs::filesystem_error("Cannot create a temporary directory", ec);
}
return directory;
}
fs::path create_directory(std::string_view prefix, std::error_code& ec) {
if (!is_prefix_valid(prefix)) {
ec = std::make_error_code(std::errc::invalid_argument);
return fs::path();
}
#ifdef _WIN32
fs::path::string_type path = make_path(prefix);
#else
fs::path::string_type path = make_pattern(prefix);
#endif
#ifdef _WIN32
if (!CreateDirectory(path.c_str(), nullptr)) {
ec = std::error_code(GetLastError(), std::system_category());
}
#else
if (mkdtemp(path.data()) == nullptr) {
ec = std::error_code(errno, std::system_category());
}
#endif
return path;
}
} // namespace tmp