-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile.cpp
More file actions
239 lines (208 loc) · 6.44 KB
/
file.cpp
File metadata and controls
239 lines (208 loc) · 6.44 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <tmp/file>
#include "create.hpp"
#include <array>
#include <filesystem>
#include <fstream>
#include <ios>
#include <istream>
#include <string_view>
#include <system_error>
#include <utility>
#ifdef _WIN32
#define NOMINMAX
#define UNICODE
#include <Windows.h>
#include <corecrt_io.h>
#else
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#endif
namespace tmp {
namespace {
/// A block size for file reading
/// @note should always be less than INT_MAX
constexpr std::size_t block_size = 4096;
/// A type of buffer for I/O file operations
using buffer_type = std::array<std::byte, block_size>;
/// Opens a file and returns its handle
/// @param[in] path The path to the file to open
/// @param[in] readonly Whether to open file only for reading
/// @param[out] ec Parameter for error reporting
/// @returns A handle to the open file
file::native_handle_type open(const fs::path& path, bool readonly,
std::error_code& ec) noexcept {
ec.clear();
#ifdef _WIN32
DWORD access = readonly ? GENERIC_READ : GENERIC_WRITE;
DWORD creation_disposition = readonly ? OPEN_EXISTING : CREATE_ALWAYS;
DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
HANDLE handle =
CreateFile(path.c_str(), access, share_mode, nullptr,
creation_disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
ec = std::error_code(GetLastError(), std::system_category());
}
#else
constexpr mode_t mode = 0644;
int oflag = readonly ? O_RDONLY | O_NONBLOCK : O_RDWR | O_TRUNC | O_CREAT;
int handle = ::open(path.c_str(), oflag, mode);
if (handle == -1) {
ec = std::error_code(errno, std::system_category());
}
#endif
return handle;
}
/// Closes the given handle, ignoring any errors
/// @param[in] handle The handle to close
void close(file::native_handle_type handle) noexcept {
#ifdef _WIN32
CloseHandle(handle);
#else
::close(handle);
#endif
}
/// Copies file contents from one file descriptor to another
/// @param[in] from The source file descriptor
/// @param[in] to The target file descriptor
/// @param[out] ec Parameter for error reporting
void copy_file(file::native_handle_type from, file::native_handle_type to,
std::error_code& ec) noexcept {
// TODO: can be optimized using `sendfile`, `copyfile` or other system API
buffer_type buffer = buffer_type();
while (true) {
#ifdef _WIN32
DWORD bytes_read;
if (!ReadFile(from, buffer.data(), block_size, &bytes_read, nullptr)) {
ec = std::error_code(GetLastError(), std::system_category());
break;
}
#else
ssize_t bytes_read = read(from, buffer.data(), block_size);
if (bytes_read < 0) {
ec = std::error_code(errno, std::system_category());
break;
}
#endif
if (bytes_read == 0) {
break;
}
#ifdef _WIN32
DWORD written;
if (!WriteFile(to, buffer.data(), bytes_read, &written, nullptr)) {
ec = std::error_code(GetLastError(), std::system_category());
return;
}
#else
ssize_t written = write(to, buffer.data(), bytes_read);
if (written < 0) {
ec = std::error_code(errno, std::system_category());
break;
}
#endif
}
}
/// Copies file contents from the given path to the given file descriptor
/// @param[in] from The path to the source file
/// @param[in] to The target file descriptor
/// @param[out] ec Parameter for error reporting
void copy_file(const fs::path& from, file::native_handle_type to,
std::error_code& ec) noexcept {
file::native_handle_type source = open(from, /*readonly=*/true, ec);
if (!ec) {
copy_file(source, to, ec);
close(source);
}
}
/// Copies file contents from the given path to the given file descriptor
/// @param[in] from The source file descriptor
/// @param[in] to The path to the target file
/// @param[out] ec Parameter for error reporting
void copy_file(file::native_handle_type from, const fs::path& to,
std::error_code& ec) noexcept {
file::native_handle_type target = open(to, /*readonly=*/false, ec);
if (!ec) {
copy_file(from, target, ec);
close(target);
}
}
} // namespace
file::file(std::ios::openmode mode)
: std::iostream(std::addressof(sb))
#if defined(_MSC_VER)
,
underlying(nullptr, &std::fclose)
#endif
{
mode |= std::ios::in | std::ios::out;
#if defined(__GLIBCXX__)
int handle = create_file();
sb = __gnu_cxx::stdio_filebuf<char>(handle, mode);
if (!sb.is_open()) {
close(handle);
throw fs::filesystem_error("", std::make_error_code(std::io_errc::stream));
}
#elif defined(_LIBCPP_VERSION)
this->handle = create_file();
sb.__open(handle, mode);
if (!sb.is_open()) {
close(handle);
throw fs::filesystem_error("", std::make_error_code(std::io_errc::stream));
}
#else // MSVC
std::FILE* handle = create_file(mode);
underlying.reset(handle);
sb = std::filebuf(underlying.get());
#endif
}
file file::copy(const fs::path& path, std::ios::openmode mode) {
file tmpfile = file(mode);
std::error_code ec;
copy_file(path, tmpfile.native_handle(), ec);
if (ec) {
throw fs::filesystem_error("Cannot create a temporary copy", path, ec);
}
return tmpfile;
}
file::native_handle_type file::native_handle() const noexcept {
#if defined(__GLIBCXX__)
return sb.fd();
#elif defined(_LIBCPP_VERSION)
return handle;
#else // MSVC
intptr_t osfhandle = _get_osfhandle(_fileno(underlying.get()));
if (osfhandle == -1) {
return nullptr;
}
return reinterpret_cast<void*>(osfhandle);
#endif
}
void file::move(const fs::path& to) {
// TODO: I couldn't figure out how to create a hard link to a file without
// hard links, so I just copy it even within the same file system
seekg(0, std::ios::beg);
std::error_code ec;
copy_file(native_handle(), to, ec);
if (ec) {
throw fs::filesystem_error("Cannot move a temporary file", to, ec);
}
}
file::~file() noexcept = default;
// NOLINTBEGIN(*-use-after-move)
file::file(file&& other) noexcept
: std::iostream(std::move(other)),
#if defined(_MSC_VER)
underlying(std::move(other.underlying)),
#elif defined(_LIBCPP_VERSION)
handle(other.handle),
#endif
sb(std::move(other.sb)) {
set_rdbuf(std::addressof(sb));
}
// NOLINTEND(*-use-after-move)
file& file::operator=(file&& other) = default;
} // namespace tmp
std::size_t
std::hash<tmp::file>::operator()(const tmp::file& file) const noexcept {
return std::hash<tmp::file::native_handle_type>()(file.native_handle());
}