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 <ios>
32#include <iterator>
33#include <optional>
34#include <string>
35
36namespace ripple {
37
40 boost::system::error_code& ec,
41 boost::filesystem::path const& sourcePath,
43{
44 using namespace boost::filesystem;
45 using namespace boost::system::errc;
46
47 path fullPath{canonical(sourcePath, ec)};
48 if (ec)
49 return {};
50
51 if (maxSize && (file_size(fullPath, ec) > *maxSize || ec))
52 {
53 if (!ec)
54 ec = make_error_code(file_too_large);
55 return {};
56 }
57
58 ifstream fileStream(fullPath, std::ios::in);
59
60 if (!fileStream)
61 {
62 ec = make_error_code(static_cast<errc_t>(errno));
63 return {};
64 }
65
66 std::string const result{
69
70 if (fileStream.bad())
71 {
72 ec = make_error_code(static_cast<errc_t>(errno));
73 return {};
74 }
75
76 return result;
77}
78
79void
81 boost::system::error_code& ec,
82 boost::filesystem::path const& destPath,
83 std::string const& contents)
84{
85 using namespace boost::filesystem;
86 using namespace boost::system::errc;
87
88 ofstream fileStream(destPath, std::ios::out | std::ios::trunc);
89
90 if (!fileStream)
91 {
92 ec = make_error_code(static_cast<errc_t>(errno));
93 return;
94 }
95
96 fileStream << contents;
97
98 if (fileStream.bad())
99 {
100 ec = make_error_code(static_cast<errc_t>(errno));
101 return;
102 }
103}
104
105} // namespace ripple
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)
Definition: token_errors.h:97
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)