forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_file.hpp
45 lines (40 loc) · 1.25 KB
/
write_file.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/filesystem/operations.hpp>
#include <fstream>
#include <qtils/bytestr.hpp>
#include <qtils/outcome.hpp>
namespace kagome {
inline outcome::result<void> writeFile(const std::filesystem::path &path,
std::string_view data) {
std::ofstream file{path, std::ios::binary};
if (file and file.write(data.data(), data.size()) and file.flush()) {
return outcome::success();
}
return std::errc{errno};
}
inline outcome::result<void> writeFile(const std::filesystem::path &path,
qtils::BytesIn data) {
return writeFile(path, qtils::byte2str(data));
}
outcome::result<void> writeFileTmp(const std::filesystem::path &path,
auto &&data) {
boost::system::error_code ec1;
auto tmp =
boost::filesystem::unique_path(path.native() + ".%%%%", ec1).native();
if (ec1) {
return ec1;
}
OUTCOME_TRY(writeFile(tmp, data));
std::error_code ec2;
std::filesystem::rename(tmp, path, ec2);
if (ec2) {
return ec2;
}
return outcome::success();
}
} // namespace kagome