xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
basics
libxrpl/basics/FileUtilities.cpp
1
#include <xrpl/basics/FileUtilities.h>
2
3
#include <xrpl/basics/contract.h>
4
5
#include <
cerrno
>
6
#include <
cstddef
>
7
#include <
filesystem
>
8
#include <format>
9
#include <
fstream
>
10
#include <
iomanip
>
11
#include <
ios
>
12
#include <
iostream
>
13
#include <
iterator
>
14
#include <
optional
>
15
#include <
random
>
16
#include <
sstream
>
17
#include <
stdexcept
>
18
#include <
string
>
19
#include <
system_error
>
20
21
namespace
xrpl
{
22
23
std::string
24
getFileContents
(
25
std::error_code
& ec,
26
std::filesystem::path
const
& sourcePath,
27
std::optional<std::size_t>
maxSize)
28
{
29
using namespace
std::filesystem
;
30
31
path
const
fullPath{
canonical
(sourcePath, ec)};
32
if
(ec)
33
return
{};
34
35
if
(maxSize && (
file_size
(fullPath, ec) > *maxSize || ec))
36
{
37
if
(!ec)
38
ec =
make_error_code
(std::errc::file_too_large);
39
return
{};
40
}
41
42
std::ifstream
fileStream(fullPath, std::ios::in);
43
44
if
(!fileStream)
45
{
46
ec.
assign
(errno,
std::generic_category
());
47
return
{};
48
}
49
50
std::string
result{
51
std::istreambuf_iterator<char>
{fileStream},
std::istreambuf_iterator<char>
{}};
52
53
if
(fileStream.
bad
())
54
{
55
ec.
assign
(errno,
std::generic_category
());
56
return
{};
57
}
58
59
return
result;
60
}
61
62
void
63
writeFileContents
(
64
std::error_code
& ec,
65
std::filesystem::path
const
& destPath,
66
std::string
const
& contents)
67
{
68
std::ofstream
fileStream(destPath, std::ios::out | std::ios::trunc);
69
70
if
(!fileStream)
71
{
72
ec.
assign
(errno,
std::generic_category
());
73
return
;
74
}
75
76
fileStream << contents;
77
78
if
(fileStream.
bad
())
79
{
80
ec.
assign
(errno,
std::generic_category
());
81
return
;
82
}
83
}
84
85
std::filesystem::path
86
uniqueRandomPath
(
87
std::filesystem::path
const
& base,
88
std::string
const
& prefix,
89
std::size_t
maxAttempts)
90
{
91
std::random_device
rd;
92
for
(
std::size_t
attempt = 0; attempt < maxAttempts; ++attempt)
93
{
94
std::ostringstream
oss;
95
oss << prefix <<
std::hex
<<
std::setfill
(
'0'
) <<
std::setw
(8) << rd() <<
std::setw
(8)
96
<< rd();
97
auto
candidate = base / oss.
str
();
98
std::error_code
ec;
99
bool
const
exists
=
std::filesystem::exists
(candidate, ec);
100
if
(ec)
101
{
102
Throw<std::runtime_error>
(
103
std::format
(
"Unable to check path '{}': {}"
, candidate.string(), ec.
message
()));
104
}
105
if
(!
exists
)
106
return
candidate;
107
}
108
Throw<std::runtime_error>
(
109
std::format
(
"Unable to generate a unique path under '{}'"
, base.
string
()));
110
}
111
112
TempDir::TempDir
() :
path_
(
uniqueRandomPath
(
std
::
filesystem
::
temp_directory_path
()))
113
{
114
std::filesystem::create_directory
(
path_
);
115
}
116
117
TempDir::~TempDir
()
118
{
119
// use non-throwing calls in the destructor
120
std::error_code
ec;
121
std::filesystem::remove_all
(
path_
, ec);
122
if
(ec)
123
{
124
std::cerr
<<
"Unable to remove temporary directory '"
<<
path_
.string()
125
<<
"': "
<< ec.
message
() <<
'\n'
;
126
}
127
}
128
129
std::string
130
TempDir::path
()
const
131
{
132
return
path_
.string();
133
}
134
135
std::string
136
TempDir::file
(
std::string
const
& name)
const
137
{
138
return
(
path_
/ name).
string
();
139
}
140
141
}
// namespace xrpl
std::error_code::assign
T assign(T... args)
std::ifstream::bad
T bad(T... args)
std::ifstream
std::ofstream
std::ostringstream
std::string
std::filesystem::canonical
T canonical(T... args)
std::cerr
cerrno
xrpl::TempDir::path_
std::filesystem::path path_
Definition
FileUtilities.h:45
xrpl::TempDir::TempDir
TempDir()
Construct a temporary directory.
Definition
libxrpl/basics/FileUtilities.cpp:112
xrpl::TempDir::path
std::string path() const
Get the native path for the temporary directory.
Definition
libxrpl/basics/FileUtilities.cpp:130
xrpl::TempDir::~TempDir
~TempDir()
Destroy a temporary directory.
Definition
libxrpl/basics/FileUtilities.cpp:117
xrpl::TempDir::file
std::string file(std::string const &name) const
Get the native path for a file.
Definition
libxrpl/basics/FileUtilities.cpp:136
std::filesystem::create_directory
T create_directory(T... args)
cstddef
std::error_code
std::filesystem::exists
T exists(T... args)
std::filesystem::file_size
T file_size(T... args)
filesystem
std::format
T format(T... args)
fstream
std::generic_category
T generic_category(T... args)
std::hex
T hex(T... args)
iomanip
ios
iostream
std::istreambuf_iterator
iterator
std::error_code::message
T message(T... args)
std::filesystem
std
STL namespace.
xrpl::path
Definition
FlowDebugInfo.h:21
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::make_error_code
std::error_code make_error_code(xrpl::TokenCodecErrc e)
Definition
token_errors.h:78
xrpl::uniqueRandomPath
std::filesystem::path uniqueRandomPath(std::filesystem::path const &base, std::string const &prefix="", std::size_t maxAttempts=100)
Generate a unique, non-existing path under base whose filename starts with prefix and ends with a ran...
Definition
libxrpl/basics/FileUtilities.cpp:86
xrpl::getFileContents
std::string getFileContents(std::error_code &ec, std::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
Definition
libxrpl/basics/FileUtilities.cpp:24
xrpl::writeFileContents
void writeFileContents(std::error_code &ec, std::filesystem::path const &destPath, std::string const &contents)
Definition
libxrpl/basics/FileUtilities.cpp:63
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
optional
std::filesystem::path
std::random_device
random
std::filesystem::remove_all
T remove_all(T... args)
std::setfill
T setfill(T... args)
std::setw
T setw(T... args)
std::size_t
sstream
stdexcept
std::ostringstream::str
T str(T... args)
string
system_error
std::filesystem::temp_directory_path
T temp_directory_path(T... args)
Generated by
1.17.0