rippled
Loading...
Searching...
No Matches
FileUtilities.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/FileUtilities.h>
21
22#include <boost/filesystem/fstream.hpp>
23#include <boost/filesystem/operations.hpp>
24#include <boost/filesystem/path.hpp>
25#include <boost/system/detail/errc.hpp>
26#include <boost/system/detail/error_code.hpp>
27#include <boost/system/errc.hpp>
28
29#include <cerrno>
30#include <cstddef>
31#include <fstream>
32#include <ios>
33#include <iterator>
34#include <optional>
35#include <string>
36
37namespace ripple {
38
41 boost::system::error_code& ec,
42 boost::filesystem::path const& sourcePath,
44{
45 using namespace boost::filesystem;
46 using namespace boost::system::errc;
47
48 path fullPath{canonical(sourcePath, ec)};
49 if (ec)
50 return {};
51
52 if (maxSize && (file_size(fullPath, ec) > *maxSize || ec))
53 {
54 if (!ec)
55 ec = make_error_code(file_too_large);
56 return {};
57 }
58
59 std::ifstream fileStream(fullPath.string(), std::ios::in);
60
61 if (!fileStream)
62 {
63 ec = make_error_code(static_cast<errc_t>(errno));
64 return {};
65 }
66
67 std::string const result{
70
71 if (fileStream.bad())
72 {
73 ec = make_error_code(static_cast<errc_t>(errno));
74 return {};
75 }
76
77 return result;
78}
79
80void
82 boost::system::error_code& ec,
83 boost::filesystem::path const& destPath,
84 std::string const& contents)
85{
86 using namespace boost::filesystem;
87 using namespace boost::system::errc;
88
89 std::ofstream fileStream(
90 destPath.string(), std::ios::out | std::ios::trunc);
91
92 if (!fileStream)
93 {
94 ec = make_error_code(static_cast<errc_t>(errno));
95 return;
96 }
97
98 fileStream << contents;
99
100 if (fileStream.bad())
101 {
102 ec = make_error_code(static_cast<errc_t>(errno));
103 return;
104 }
105}
106
107} // namespace ripple
T bad(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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)