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