Branch data Line data Source code
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 <span.h>
6 : : #include <streams.h>
7 : : #include <util/fs_helpers.h>
8 : :
9 : : #include <array>
10 : :
11 : 0 : AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
12 [ # # ]: 0 : : m_file{file}, m_xor{std::move(data_xor)}
13 : : {
14 [ # # ]: 0 : if (!IsNull()) {
15 [ # # ]: 0 : auto pos{std::ftell(m_file)};
16 [ # # ]: 0 : if (pos >= 0) m_position = pos;
17 : : }
18 : 0 : }
19 : :
20 : 0 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
21 : : {
22 [ # # # # ]: 0 : if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
23 [ # # ]: 0 : size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
24 [ # # ]: 0 : if (!m_xor.empty()) {
25 [ # # # # ]: 0 : if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
26 : 0 : util::Xor(dst.subspan(0, ret), m_xor, *m_position);
27 : : }
28 [ # # ]: 0 : if (m_position.has_value()) *m_position += ret;
29 : 0 : return ret;
30 : : }
31 : :
32 : 0 : void AutoFile::seek(int64_t offset, int origin)
33 : : {
34 [ # # ]: 0 : if (IsNull()) {
35 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
36 : : }
37 [ # # ]: 0 : if (std::fseek(m_file, offset, origin) != 0) {
38 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
39 : : }
40 [ # # ]: 0 : if (origin == SEEK_SET) {
41 : 0 : m_position = offset;
42 [ # # # # ]: 0 : } else if (origin == SEEK_CUR && m_position.has_value()) {
43 : 0 : *m_position += offset;
44 : : } else {
45 : 0 : int64_t r{std::ftell(m_file)};
46 [ # # ]: 0 : if (r < 0) {
47 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::seek: ftell failed");
48 : : }
49 : 0 : m_position = r;
50 : : }
51 : 0 : }
52 : :
53 : 0 : int64_t AutoFile::tell()
54 : : {
55 [ # # # # ]: 0 : if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
56 : 0 : return *m_position;
57 : : }
58 : :
59 : 0 : void AutoFile::read(Span<std::byte> dst)
60 : : {
61 [ # # ]: 0 : if (detail_fread(dst) != dst.size()) {
62 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
63 : : }
64 : 0 : }
65 : :
66 : 0 : void AutoFile::ignore(size_t nSize)
67 : : {
68 [ # # # # ]: 0 : if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
69 : : unsigned char data[4096];
70 [ # # ]: 0 : while (nSize > 0) {
71 [ # # ]: 0 : size_t nNow = std::min<size_t>(nSize, sizeof(data));
72 [ # # # # ]: 0 : if (std::fread(data, 1, nNow, m_file) != nNow) {
73 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
74 : : }
75 : 0 : nSize -= nNow;
76 [ # # ]: 0 : if (m_position.has_value()) *m_position += nNow;
77 : : }
78 : 0 : }
79 : :
80 : 0 : void AutoFile::write(Span<const std::byte> src)
81 : : {
82 [ # # # # ]: 0 : if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
83 [ # # ]: 0 : if (m_xor.empty()) {
84 [ # # ]: 0 : if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
85 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::write: write failed");
86 : : }
87 [ # # ]: 0 : if (m_position.has_value()) *m_position += src.size();
88 : : } else {
89 [ # # # # ]: 0 : if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
90 : : std::array<std::byte, 4096> buf;
91 [ # # ]: 0 : while (src.size() > 0) {
92 [ # # ]: 0 : auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
93 : 0 : std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
94 : 0 : util::Xor(buf_now, m_xor, *m_position);
95 [ # # ]: 0 : if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
96 [ # # ]: 0 : throw std::ios_base::failure{"XorFile::write: failed"};
97 : : }
98 : 0 : src = src.subspan(buf_now.size());
99 : 0 : *m_position += buf_now.size();
100 : : }
101 : : }
102 : 0 : }
103 : :
104 : 0 : bool AutoFile::Commit()
105 : : {
106 : 0 : return ::FileCommit(m_file);
107 : : }
108 : :
109 : 0 : bool AutoFile::Truncate(unsigned size)
110 : : {
111 : 0 : return ::TruncateFile(m_file, size);
112 : : }
|