-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_util.hpp
More file actions
271 lines (238 loc) · 8.77 KB
/
fs_util.hpp
File metadata and controls
271 lines (238 loc) · 8.77 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma once
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <string>
#include <string_view>
#include <system_error>
#include <unistd.h>
#include <cfbox/error.hpp>
namespace cfbox::fs {
namespace detail {
inline auto make_error(int ec, std::string_view msg) -> base::Error {
return base::Error{ec, std::string{msg}};
}
} // namespace detail
inline auto exists(std::string_view path) -> bool {
return std::filesystem::exists(std::filesystem::path{path});
}
inline auto is_directory(std::string_view path) -> bool {
return std::filesystem::is_directory(std::filesystem::path{path});
}
inline auto file_size(std::string_view path) -> base::Result<std::uintmax_t> {
std::error_code ec;
auto sz = std::filesystem::file_size(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return sz;
}
inline auto mkdir_single(std::string_view path, std::filesystem::perms mode) -> base::Result<void> {
std::error_code ec;
std::filesystem::create_directory(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
std::filesystem::permissions(std::filesystem::path{path}, mode, ec);
if (ec) {
// non-fatal: directory created but permissions not set
}
return {};
}
inline auto mkdir_recursive(std::string_view path, std::filesystem::perms mode) -> base::Result<void> {
std::error_code ec;
std::filesystem::create_directories(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
std::filesystem::permissions(std::filesystem::path{path}, mode, ec);
return {};
}
inline auto remove_single(std::string_view path) -> base::Result<void> {
std::error_code ec;
std::filesystem::remove(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto remove_all(std::string_view path) -> base::Result<std::uintmax_t> {
std::error_code ec;
auto count = std::filesystem::remove_all(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return count;
}
inline auto copy_file(std::string_view src, std::string_view dst) -> base::Result<void> {
std::error_code ec;
std::filesystem::copy_file(
std::filesystem::path{src},
std::filesystem::path{dst},
std::filesystem::copy_options::overwrite_existing,
ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto copy_recursive(std::string_view src, std::string_view dst) -> base::Result<void> {
std::error_code ec;
std::filesystem::copy(
std::filesystem::path{src},
std::filesystem::path{dst},
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing,
ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto rename(std::string_view old_path, std::string_view new_path) -> base::Result<void> {
std::error_code ec;
std::filesystem::rename(std::filesystem::path{old_path}, std::filesystem::path{new_path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto create_hard_link(std::string_view target, std::string_view link_path) -> base::Result<void> {
std::error_code ec;
std::filesystem::create_hard_link(
std::filesystem::path{target},
std::filesystem::path{link_path},
ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto current_path() -> base::Result<std::string> {
std::error_code ec;
auto p = std::filesystem::current_path(ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return p.string();
}
inline auto directory_entries(std::string_view path) -> base::Result<std::vector<std::filesystem::directory_entry>> {
std::error_code ec;
std::filesystem::directory_iterator it{std::filesystem::path{path}, ec};
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
std::vector<std::filesystem::directory_entry> entries;
for (const auto& e : it) {
entries.push_back(e);
}
return entries;
}
inline auto status(std::string_view path) -> base::Result<std::filesystem::file_status> {
std::error_code ec;
auto st = std::filesystem::status(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return st;
}
inline auto symlink_status(std::string_view path) -> base::Result<std::filesystem::file_status> {
std::error_code ec;
auto st = std::filesystem::symlink_status(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return st;
}
inline auto last_write_time(std::string_view path) -> base::Result<std::filesystem::file_time_type> {
std::error_code ec;
auto t = std::filesystem::last_write_time(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return t;
}
inline auto permissions(std::string_view path, std::filesystem::perms prms) -> base::Result<void> {
std::error_code ec;
std::filesystem::permissions(std::filesystem::path{path}, prms, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto hard_link_count(std::string_view path) -> base::Result<std::uintmax_t> {
std::error_code ec;
auto count = std::filesystem::hard_link_count(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return count;
}
inline auto create_symlink(std::string_view target, std::string_view link_path) -> base::Result<void> {
std::error_code ec;
std::filesystem::create_symlink(
std::filesystem::path{target},
std::filesystem::path{link_path},
ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto read_symlink(std::string_view path) -> base::Result<std::string> {
std::error_code ec;
auto p = std::filesystem::read_symlink(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return p.string();
}
inline auto canonical(std::string_view path) -> base::Result<std::string> {
std::error_code ec;
auto p = std::filesystem::canonical(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return p.string();
}
inline auto resize_file(std::string_view path, std::uintmax_t new_size) -> base::Result<void> {
std::error_code ec;
std::filesystem::resize_file(std::filesystem::path{path}, new_size, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return {};
}
inline auto space(std::string_view path) -> base::Result<std::filesystem::space_info> {
std::error_code ec;
auto info = std::filesystem::space(std::filesystem::path{path}, ec);
if (ec) {
return std::unexpected(base::Error{static_cast<int>(ec.value()), ec.message()});
}
return info;
}
inline auto chown(std::string_view path, uid_t uid, gid_t gid) -> base::Result<void> {
if (::chown(std::string{path}.c_str(), uid, gid) != 0) {
return std::unexpected(base::Error{errno, std::strerror(errno)});
}
return {};
}
inline auto lchown(std::string_view path, uid_t uid, gid_t gid) -> base::Result<void> {
if (::lchown(std::string{path}.c_str(), uid, gid) != 0) {
return std::unexpected(base::Error{errno, std::strerror(errno)});
}
return {};
}
template <typename Func>
void for_each_entry(std::string_view path, bool recursive, Func&& fn) {
if (recursive && is_directory(path)) {
std::error_code ec;
for (const auto& entry : std::filesystem::recursive_directory_iterator(std::filesystem::path{path}, ec)) {
if (ec) continue;
fn(entry.path().string());
}
}
fn(std::string{path});
}
} // namespace cfbox::fs