-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscaffold_fs.cppm
More file actions
208 lines (195 loc) · 6.67 KB
/
Copy pathscaffold_fs.cppm
File metadata and controls
208 lines (195 loc) · 6.67 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
// Platform filesystem primitives used by transactional scaffolding. The
// public contract is stronger than std::filesystem::rename: an existing final
// name is never replaced, including a target that appears in the commit race.
module;
#if defined(_WIN32)
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
#elif defined(__linux__)
# include <cerrno>
# include <fcntl.h>
# include <linux/fs.h>
# include <sys/syscall.h>
# include <unistd.h>
#elif defined(__APPLE__)
# include <cerrno>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
#else
# include <cerrno>
# include <fcntl.h>
# include <unistd.h>
#endif
export module mcpp.platform.scaffold_fs;
import std;
namespace mcpp::platform::detail {
#if defined(_WIN32)
std::string win32_message(DWORD code) {
return std::system_category().message(static_cast<int>(code));
}
#else
std::string errno_message(int code) {
return std::generic_category().message(code);
}
bool sync_is_unsupported(int code) {
return code == EINVAL || code == EROFS
#ifdef ENOTSUP
|| code == ENOTSUP
#endif
#ifdef EOPNOTSUPP
|| code == EOPNOTSUPP
#endif
;
}
#endif
} // namespace mcpp::platform::detail
export namespace mcpp::platform {
std::expected<void, std::string> atomic_rename_directory_no_replace(
const std::filesystem::path& source,
const std::filesystem::path& target) {
#if defined(_WIN32)
if (::MoveFileExW(source.c_str(), target.c_str(),
MOVEFILE_WRITE_THROUGH) != 0) {
return {};
}
const auto error = ::GetLastError();
return std::unexpected(std::format(
"atomic no-replace rename '{}' -> '{}' failed: {}",
source.string(), target.string(), detail::win32_message(error)));
#elif defined(__linux__) && defined(SYS_renameat2)
if (::syscall(SYS_renameat2, AT_FDCWD, source.c_str(), AT_FDCWD,
target.c_str(), RENAME_NOREPLACE) == 0) {
return {};
}
const int error = errno;
return std::unexpected(std::format(
"atomic no-replace rename '{}' -> '{}' failed: {}",
source.string(), target.string(), detail::errno_message(error)));
#elif defined(__APPLE__)
if (::renamex_np(source.c_str(), target.c_str(), RENAME_EXCL) == 0) {
return {};
}
const int error = errno;
return std::unexpected(std::format(
"atomic no-replace rename '{}' -> '{}' failed: {}",
source.string(), target.string(), detail::errno_message(error)));
#else
// The fallback remains no-clobber in ordinary use, but platforms without
// a native exclusive rename cannot close the existence-check race. All
// release platforms use one of the branches above.
std::error_code ec;
if (std::filesystem::exists(target, ec) || ec) {
return std::unexpected(std::format(
"atomic scaffold target '{}' already exists", target.string()));
}
std::filesystem::rename(source, target, ec);
if (!ec) return {};
return std::unexpected(std::format(
"scaffold rename '{}' -> '{}' failed: {}",
source.string(), target.string(), ec.message()));
#endif
}
// Make a newly written regular file durable before its containing staging
// directory is published. Unsupported filesystem sync operations are treated
// as "best available"; genuine I/O/permission failures abort the transaction.
std::expected<void, std::string> sync_regular_file(
const std::filesystem::path& path) {
#if defined(_WIN32)
HANDLE handle = ::CreateFileW(
path.c_str(), GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
const auto error = ::GetLastError();
return std::unexpected(std::format(
"cannot open '{}' for durable sync: {}", path.string(),
detail::win32_message(error)));
}
if (::FlushFileBuffers(handle) == 0) {
const auto error = ::GetLastError();
::CloseHandle(handle);
return std::unexpected(std::format(
"durable sync '{}' failed: {}", path.string(),
detail::win32_message(error)));
}
if (::CloseHandle(handle) == 0) {
const auto error = ::GetLastError();
return std::unexpected(std::format(
"close synced file '{}' failed: {}", path.string(),
detail::win32_message(error)));
}
return {};
#else
int descriptor = ::open(path.c_str(), O_RDONLY
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
);
if (descriptor < 0) {
const int error = errno;
return std::unexpected(std::format(
"cannot open '{}' for durable sync: {}", path.string(),
detail::errno_message(error)));
}
if (::fsync(descriptor) != 0) {
const int error = errno;
::close(descriptor);
if (detail::sync_is_unsupported(error)) return {};
return std::unexpected(std::format(
"durable sync '{}' failed: {}", path.string(),
detail::errno_message(error)));
}
if (::close(descriptor) != 0) {
const int error = errno;
return std::unexpected(std::format(
"close synced file '{}' failed: {}", path.string(),
detail::errno_message(error)));
}
return {};
#endif
}
// Persist the directory entry after the exclusive rename where the host/filesystem
// offers directory fsync. MoveFileExW(MOVEFILE_WRITE_THROUGH) already supplies
// the corresponding Windows guarantee.
std::expected<void, std::string> sync_directory(
const std::filesystem::path& path) {
#if defined(_WIN32)
(void)path;
return {};
#else
int descriptor = ::open(path.c_str(), O_RDONLY
#ifdef O_DIRECTORY
| O_DIRECTORY
#endif
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
);
if (descriptor < 0) {
const int error = errno;
if (detail::sync_is_unsupported(error)) return {};
return std::unexpected(std::format(
"cannot open directory '{}' for durable sync: {}", path.string(),
detail::errno_message(error)));
}
if (::fsync(descriptor) != 0) {
const int error = errno;
::close(descriptor);
if (detail::sync_is_unsupported(error)) return {};
return std::unexpected(std::format(
"durable directory sync '{}' failed: {}", path.string(),
detail::errno_message(error)));
}
if (::close(descriptor) != 0) {
const int error = errno;
return std::unexpected(std::format(
"close synced directory '{}' failed: {}", path.string(),
detail::errno_message(error)));
}
return {};
#endif
}
} // namespace mcpp::platform