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 xrpl {
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
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
60writeFileContents(boost::system::error_code& ec, boost::filesystem::path const& destPath, std::string const& contents)
61{
62 using namespace boost::filesystem;
63 using namespace boost::system::errc;
64
65 std::ofstream fileStream(destPath.string(), std::ios::out | std::ios::trunc);
66
67 if (!fileStream)
68 {
69 ec = make_error_code(static_cast<errc_t>(errno));
70 return;
71 }
72
73 fileStream << contents;
74
75 if (fileStream.bad())
76 {
77 ec = make_error_code(static_cast<errc_t>(errno));
78 return;
79 }
80}
81
82} // namespace xrpl
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(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)