aboutsummaryrefslogtreecommitdiff
path: root/src/streams.cpp
diff options
context:
space:
mode:
authormerge-script <fanquake@gmail.com>2024-09-17 17:14:28 +0100
committermerge-script <fanquake@gmail.com>2024-09-17 17:14:28 +0100
commit1147e72953d1f262111a4b1d5a438a8394511bc7 (patch)
tree48061825c2dced44d438b01a8a8417ef03f217f9 /src/streams.cpp
parentfa460884406b35b0dee75af23f42e8b4c4acbebc (diff)
parent06a7df70df30879e0b691d1a252636f703b8cdfb (diff)
Merge bitcoin/bitcoin#30827: [28.x] Further backports and rc2v28.0rc2
06a7df70df30879e0b691d1a252636f703b8cdfb doc: Generate manpages (Ava Chow) 5315886bf5bb430ced1b23ebfcd0b8865b523675 build: Bump to 28.0rc2 (Ava Chow) ff95cb31da6ab6af70125b2c0b78aa976780c61e streams: remove AutoFile::Get() entirely (Pieter Wuille) 8229e981167f61e2d4c2e8b7761cba68374ea92f streams: cache file position within AutoFile (Pieter Wuille) 1b853fdb0a40064a5c75b2dae96dfe1d590de6e1 qt: Translations update (Hennadii Stepanov) 674dded8756ddf9b731f3149c66dd044090e4c8e gui: fix crash when closing wallet (furszy) d39262e5d41e92d22e020d283ddb6e4e406647b2 test: Wait for local services to update in feature_assumeutxo (Fabian Jahr) b329ed739b7311b3b47cae1ef8d576a90e0a36a1 test: add coverage for assumeUTXO honest peers disconnection (furszy) c6b5db1d591f0984cd0e6918970a9e4fc32595d3 assumeUTXO: fix peers disconnection during sync (furszy) 598415bcc1d1f8d516159a1501dda24e8d7c2ea9 test: Work around boost compilation error (MarcoFalke) Pull request description: * #30834 * #30807 * #30880 * https://github.com/bitcoin-core/gui/pull/835 * #30899 * #30884 ACKs for top commit: stickies-v: ACK 06a7df70df30879e0b691d1a252636f703b8cdfb hebasto: ACK 06a7df70df30879e0b691d1a252636f703b8cdfb, I've backported the listed PRs locally. The only merge conflict I faced was in https://github.com/bitcoin/bitcoin/pull/30807. It was trivial to resolve. Tree-SHA512: 779d734b50fdce379a20865ba30c969def028963ba51da0f497ddf1b5375e1f6166365295f226c1a07bab8be0c1aa0a6a3296fc6acd9fcf17bcc4874aac980a6
Diffstat (limited to 'src/streams.cpp')
-rw-r--r--src/streams.cpp69
1 files changed, 49 insertions, 20 deletions
diff --git a/src/streams.cpp b/src/streams.cpp
index cdd36a86fe..5f7baf92b9 100644
--- a/src/streams.cpp
+++ b/src/streams.cpp
@@ -4,21 +4,29 @@
#include <span.h>
#include <streams.h>
+#include <util/fs_helpers.h>
#include <array>
+AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
+ : m_file{file}, m_xor{std::move(data_xor)}
+{
+ if (!IsNull()) {
+ auto pos{std::ftell(m_file)};
+ if (pos >= 0) m_position = pos;
+ }
+}
+
std::size_t AutoFile::detail_fread(Span<std::byte> dst)
{
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
- if (m_xor.empty()) {
- return std::fread(dst.data(), 1, dst.size(), m_file);
- } else {
- const auto init_pos{std::ftell(m_file)};
- if (init_pos < 0) throw std::ios_base::failure("AutoFile::read: ftell failed");
- std::size_t ret{std::fread(dst.data(), 1, dst.size(), m_file)};
- util::Xor(dst.subspan(0, ret), m_xor, init_pos);
- return ret;
+ size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
+ if (!m_xor.empty()) {
+ if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
+ util::Xor(dst.subspan(0, ret), m_xor, *m_position);
}
+ if (m_position.has_value()) *m_position += ret;
+ return ret;
}
void AutoFile::seek(int64_t offset, int origin)
@@ -29,18 +37,23 @@ void AutoFile::seek(int64_t offset, int origin)
if (std::fseek(m_file, offset, origin) != 0) {
throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
}
+ if (origin == SEEK_SET) {
+ m_position = offset;
+ } else if (origin == SEEK_CUR && m_position.has_value()) {
+ *m_position += offset;
+ } else {
+ int64_t r{std::ftell(m_file)};
+ if (r < 0) {
+ throw std::ios_base::failure("AutoFile::seek: ftell failed");
+ }
+ m_position = r;
+ }
}
int64_t AutoFile::tell()
{
- if (IsNull()) {
- throw std::ios_base::failure("AutoFile::tell: file handle is nullptr");
- }
- int64_t r{std::ftell(m_file)};
- if (r < 0) {
- throw std::ios_base::failure("AutoFile::tell: ftell failed");
- }
- return r;
+ if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
+ return *m_position;
}
void AutoFile::read(Span<std::byte> dst)
@@ -60,6 +73,7 @@ void AutoFile::ignore(size_t nSize)
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
}
nSize -= nNow;
+ if (m_position.has_value()) *m_position += nNow;
}
}
@@ -70,19 +84,34 @@ void AutoFile::write(Span<const std::byte> src)
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure("AutoFile::write: write failed");
}
+ if (m_position.has_value()) *m_position += src.size();
} else {
- auto current_pos{std::ftell(m_file)};
- if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
+ if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
std::array<std::byte, 4096> buf;
while (src.size() > 0) {
auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
- util::Xor(buf_now, m_xor, current_pos);
+ util::Xor(buf_now, m_xor, *m_position);
if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
throw std::ios_base::failure{"XorFile::write: failed"};
}
src = src.subspan(buf_now.size());
- current_pos += buf_now.size();
+ *m_position += buf_now.size();
}
}
}
+
+bool AutoFile::Commit()
+{
+ return ::FileCommit(m_file);
+}
+
+bool AutoFile::IsError()
+{
+ return ferror(m_file);
+}
+
+bool AutoFile::Truncate(unsigned size)
+{
+ return ::TruncateFile(m_file, size);
+}