Bitcoin Core  29.1.0
P2P Digital Currency
streams.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://opensource.org/license/mit/.
4 
5 #include <memusage.h>
6 #include <span.h>
7 #include <streams.h>
8 #include <util/fs_helpers.h>
9 
10 #include <array>
11 
12 AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
13  : m_file{file}, m_xor{std::move(data_xor)}
14 {
15  if (!IsNull()) {
16  auto pos{std::ftell(m_file)};
17  if (pos >= 0) m_position = pos;
18  }
19 }
20 
22 {
23  if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
24  size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
25  if (!m_xor.empty()) {
26  if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
27  util::Xor(dst.subspan(0, ret), m_xor, *m_position);
28  }
29  if (m_position.has_value()) *m_position += ret;
30  return ret;
31 }
32 
33 void AutoFile::seek(int64_t offset, int origin)
34 {
35  if (IsNull()) {
36  throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
37  }
38  if (std::fseek(m_file, offset, origin) != 0) {
39  throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
40  }
41  if (origin == SEEK_SET) {
42  m_position = offset;
43  } else if (origin == SEEK_CUR && m_position.has_value()) {
44  *m_position += offset;
45  } else {
46  int64_t r{std::ftell(m_file)};
47  if (r < 0) {
48  throw std::ios_base::failure("AutoFile::seek: ftell failed");
49  }
50  m_position = r;
51  }
52 }
53 
54 int64_t AutoFile::tell()
55 {
56  if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
57  return *m_position;
58 }
59 
61 {
62  if (detail_fread(dst) != dst.size()) {
63  throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
64  }
65 }
66 
67 void AutoFile::ignore(size_t nSize)
68 {
69  if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
70  unsigned char data[4096];
71  while (nSize > 0) {
72  size_t nNow = std::min<size_t>(nSize, sizeof(data));
73  if (std::fread(data, 1, nNow, m_file) != nNow) {
74  throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
75  }
76  nSize -= nNow;
77  if (m_position.has_value()) *m_position += nNow;
78  }
79 }
80 
82 {
83  if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
84  if (m_xor.empty()) {
85  if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
86  throw std::ios_base::failure("AutoFile::write: write failed");
87  }
88  if (m_position.has_value()) *m_position += src.size();
89  } else {
90  if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
91  std::array<std::byte, 4096> buf;
92  while (src.size() > 0) {
93  auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
94  std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
95  util::Xor(buf_now, m_xor, *m_position);
96  if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
97  throw std::ios_base::failure{"XorFile::write: failed"};
98  }
99  src = src.subspan(buf_now.size());
100  *m_position += buf_now.size();
101  }
102  }
103 }
104 
106 {
108 }
109 
110 bool AutoFile::Truncate(unsigned size)
111 {
113 }
114 
115 size_t DataStream::GetMemoryUsage() const noexcept
116 {
117  return sizeof(*this) + memusage::DynamicUsage(vch);
118 }
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:205
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:195
int ret
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:31
constexpr std::size_t size() const noexcept
Definition: span.h:187
bool feof() const
Definition: streams.h:407
std::optional< int64_t > m_position
Definition: streams.h:396
void Xor(Span< std::byte > write, Span< const std::byte > key, size_t key_offset=0)
Definition: streams.h:28
int64_t tell()
Find position within the file.
Definition: streams.cpp:54
std::FILE * m_file
Definition: streams.h:394
bool Commit()
Wrapper around FileCommit().
Definition: streams.cpp:105
std::vector< std::byte > m_xor
Definition: streams.h:395
void read(Span< std::byte > dst)
Definition: streams.cpp:60
constexpr C * begin() const noexcept
Definition: span.h:175
bool Truncate(unsigned size)
Wrapper around TruncateFile().
Definition: streams.cpp:110
bool TruncateFile(FILE *file, unsigned int length)
Definition: fs_helpers.cpp:150
constexpr C * data() const noexcept
Definition: span.h:174
AutoFile(std::FILE *file, std::vector< std::byte > data_xor={})
Definition: streams.cpp:12
size_t GetMemoryUsage() const noexcept
Compute total memory usage of this object (own memory + any dynamic memory).
Definition: streams.cpp:115
void seek(int64_t offset, int origin)
Wrapper around fseek().
Definition: streams.cpp:33
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
vector_type vch
Definition: streams.h:150
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:21
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:108
void write(Span< const std::byte > src)
Definition: streams.cpp:81
void ignore(size_t nSize)
Definition: streams.cpp:67
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:428