xrpld
Loading...
Searching...
No Matches
FileUtilities.cpp
1#include <xrpl/basics/FileUtilities.h>
2
3#include <boost/filesystem/operations.hpp>
4#include <boost/filesystem/path.hpp>
5#include <boost/system/detail/errc.hpp>
6#include <boost/system/detail/error_code.hpp>
7#include <boost/system/errc.hpp>
8
9#include <cerrno>
10#include <cstddef>
11#include <fstream>
12#include <ios>
13#include <iterator>
14#include <optional>
15#include <string>
16
17namespace xrpl {
18
19std::string
21 boost::system::error_code& ec,
22 boost::filesystem::path const& sourcePath,
24{
25 using namespace boost::filesystem;
26 using namespace boost::system::errc;
27
28 path const fullPath{canonical(sourcePath, ec)};
29 if (ec)
30 return {};
31
32 if (maxSize && (file_size(fullPath, ec) > *maxSize || ec))
33 {
34 if (!ec)
35 ec = make_error_code(file_too_large);
36 return {};
37 }
38
39 std::ifstream fileStream(fullPath.string(), std::ios::in);
40
41 if (!fileStream)
42 {
43 ec = make_error_code(static_cast<errc_t>(errno));
44 return {};
45 }
46
47 std::string result{
49
50 if (fileStream.bad())
51 {
52 ec = make_error_code(static_cast<errc_t>(errno));
53 return {};
54 }
55
56 return result;
57}
58
59void
61 boost::system::error_code& ec,
62 boost::filesystem::path const& destPath,
63 std::string const& contents)
64{
65 using namespace boost::filesystem;
66 using namespace boost::system::errc;
67
68 std::ofstream fileStream(destPath.string(), std::ios::out | std::ios::trunc);
69
70 if (!fileStream)
71 {
72 ec = make_error_code(static_cast<errc_t>(errno));
73 return;
74 }
75
76 fileStream << contents;
77
78 if (fileStream.bad())
79 {
80 ec = make_error_code(static_cast<errc_t>(errno));
81 return;
82 }
83}
84
85} // namespace xrpl
T bad(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::error_code make_error_code(xrpl::TokenCodecErrc e)
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
void writeFileContents(boost::system::error_code &ec, boost::filesystem::path const &destPath, std::string const &contents)