Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/streams.cpp
Line
Count
Source
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
#include <util/obfuscation.h>
10
11
#include <array>
12
13
418k
AutoFile::AutoFile(std::FILE* file, const Obfuscation& obfuscation) : m_file{file}, m_obfuscation{obfuscation}
14
418k
{
15
418k
    if (!IsNull()) {
16
416k
        auto pos{std::ftell(m_file)};
17
416k
        if (pos >= 0) m_position = pos;
18
416k
    }
19
418k
}
20
21
std::size_t AutoFile::detail_fread(std::span<std::byte> dst)
22
23.0M
{
23
23.0M
    if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
24
23.0M
    const size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
25
23.0M
    if (m_obfuscation) {
26
555k
        if (!m_position) throw std::ios_base::failure("AutoFile::read: position unknown");
27
555k
        m_obfuscation(dst.subspan(0, ret), *m_position);
28
555k
    }
29
23.0M
    if (m_position) *m_position += ret;
30
23.0M
    return ret;
31
23.0M
}
32
33
void AutoFile::seek(int64_t offset, int origin)
34
3.36k
{
35
3.36k
    if (IsNull()) {
36
0
        throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
37
0
    }
38
3.36k
    if (std::fseek(m_file, offset, origin) != 0) {
39
0
        throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
40
0
    }
41
3.36k
    if (origin == SEEK_SET) {
42
471
        m_position = offset;
43
2.89k
    } else if (origin == SEEK_CUR && m_position.has_value()) {
44
2.82k
        *m_position += offset;
45
2.82k
    } else {
46
72
        int64_t r{std::ftell(m_file)};
47
72
        if (r < 0) {
48
0
            throw std::ios_base::failure("AutoFile::seek: ftell failed");
49
0
        }
50
72
        m_position = r;
51
72
    }
52
3.36k
}
53
54
int64_t AutoFile::tell()
55
142
{
56
142
    if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
57
142
    return *m_position;
58
142
}
59
60
int64_t AutoFile::size()
61
60
{
62
60
    if (IsNull()) {
63
1
        throw std::ios_base::failure("AutoFile::size: file handle is nullptr");
64
1
    }
65
    // Temporarily save the current position
66
59
    int64_t current_pos = tell();
67
59
    seek(0, SEEK_END);
68
59
    int64_t file_size = tell();
69
    // Restore the original position
70
59
    seek(current_pos, SEEK_SET);
71
59
    return file_size;
72
60
}
73
74
void AutoFile::read(std::span<std::byte> dst)
75
23.0M
{
76
23.0M
    if (detail_fread(dst) != dst.size()) {
77
39
        throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
78
39
    }
79
23.0M
}
80
81
void AutoFile::ignore(size_t nSize)
82
2.65k
{
83
2.65k
    if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
84
2.65k
    unsigned char data[4096];
85
5.30k
    while (nSize > 0) {
86
2.65k
        size_t nNow = std::min<size_t>(nSize, sizeof(data));
87
2.65k
        if (std::fread(data, 1, nNow, m_file) != nNow) {
88
2
            throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
89
2
        }
90
2.64k
        nSize -= nNow;
91
2.64k
        if (m_position.has_value()) *m_position += nNow;
92
2.64k
    }
93
2.65k
}
94
95
void AutoFile::write(std::span<const std::byte> src)
96
42.7M
{
97
42.7M
    if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
98
42.7M
    if (!m_obfuscation) {
99
42.7M
        if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
100
1
            throw std::ios_base::failure("AutoFile::write: write failed");
101
1
        }
102
42.7M
        m_was_written = true;
103
42.7M
        if (m_position.has_value()) *m_position += src.size();
104
42.7M
    } else {
105
40.5k
        std::array<std::byte, 4096> buf;
106
88.4k
        while (src.size()) {
107
47.8k
            auto buf_now{std::span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
108
47.8k
            std::copy_n(src.begin(), buf_now.size(), buf_now.begin());
109
47.8k
            write_buffer(buf_now);
110
47.8k
            src = src.subspan(buf_now.size());
111
47.8k
        }
112
40.5k
    }
113
42.7M
}
114
115
void AutoFile::write_buffer(std::span<std::byte> src)
116
271k
{
117
271k
    if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");
118
271k
    if (m_obfuscation) {
119
271k
        if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
120
271k
        m_obfuscation(src, *m_position); // obfuscate in-place
121
271k
    }
122
271k
    if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
123
0
        throw std::ios_base::failure("AutoFile::write_buffer: write failed");
124
0
    }
125
271k
    m_was_written = true;
126
271k
    if (m_position) *m_position += src.size();
127
271k
}
128
129
bool AutoFile::Commit()
130
2.50k
{
131
2.50k
    return ::FileCommit(m_file);
132
2.50k
}
133
134
bool AutoFile::Truncate(unsigned size)
135
0
{
136
0
    m_was_written = true;
137
0
    return ::TruncateFile(m_file, size);
138
0
}
139
140
size_t DataStream::GetMemoryUsage() const noexcept
141
320k
{
142
320k
    return sizeof(*this) + memusage::DynamicUsage(vch);
143
320k
}