fuzz coverage

Coverage Report

Created: 2025-06-01 19:34

/Users/eugenesiegel/btc/bitcoin/src/flatfile.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <stdexcept>
7
8
#include <flatfile.h>
9
#include <logging.h>
10
#include <tinyformat.h>
11
#include <util/fs_helpers.h>
12
13
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
14
99.9k
    m_dir(std::move(dir)),
15
99.9k
    m_prefix(prefix),
16
99.9k
    m_chunk_size(chunk_size)
17
99.9k
{
18
99.9k
    if (chunk_size == 0) {
19
0
        throw std::invalid_argument("chunk_size must be positive");
20
0
    }
21
99.9k
}
22
23
std::string FlatFilePos::ToString() const
24
0
{
25
0
    return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
Line
Count
Source
1172
0
#define strprintf tfm::format
26
0
}
27
28
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
29
20.1M
{
30
20.1M
    return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
Line
Count
Source
1172
20.1M
#define strprintf tfm::format
31
20.1M
}
32
33
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
34
20.1M
{
35
20.1M
    if (pos.IsNull()) {
36
0
        return nullptr;
37
0
    }
38
20.1M
    fs::path path = FileName(pos);
39
20.1M
    fs::create_directories(path.parent_path());
40
20.1M
    FILE* file = fsbridge::fopen(path, read_only ? 
"rb"50.8k
:
"rb+"20.1M
);
41
20.1M
    if (!file && 
!read_only99.9k
)
42
99.9k
        file = fsbridge::fopen(path, "wb+");
43
20.1M
    if (!file) {
44
0
        LogPrintf("Unable to open file %s\n", fs::PathToString(path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
45
0
        return nullptr;
46
0
    }
47
20.1M
    if (pos.nPos && 
fseek(file, pos.nPos, SEEK_SET)19.9M
) {
48
0
        LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, fs::PathToString(path));
Line
Count
Source
266
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
261
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
49
0
        fclose(file);
50
0
        return nullptr;
51
0
    }
52
20.1M
    return file;
53
20.1M
}
54
55
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
56
20.0M
{
57
20.0M
    out_of_space = false;
58
59
20.0M
    unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
60
20.0M
    unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
61
20.0M
    if (n_new_chunks > n_old_chunks) {
62
99.9k
        size_t old_size = pos.nPos;
63
99.9k
        size_t new_size = n_new_chunks * m_chunk_size;
64
99.9k
        size_t inc_size = new_size - old_size;
65
66
99.9k
        if (CheckDiskSpace(m_dir, inc_size)) {
67
99.9k
            FILE *file = Open(pos);
68
99.9k
            if (file) {
69
99.9k
                LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
Line
Count
Source
280
99.9k
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
273
99.9k
    do {                                                  \
274
99.9k
        if (LogAcceptCategory((category), (level))) {     \
275
0
            LogPrintLevel_(category, level, __VA_ARGS__); \
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
276
0
        }                                                 \
277
99.9k
    } while (0)
70
99.9k
                AllocateFileRange(file, pos.nPos, inc_size);
71
99.9k
                fclose(file);
72
99.9k
                return inc_size;
73
99.9k
            }
74
99.9k
        } else {
75
0
            out_of_space = true;
76
0
        }
77
99.9k
    }
78
19.9M
    return 0;
79
20.0M
}
80
81
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
82
1.03k
{
83
1.03k
    FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
84
1.03k
    if (!file) {
85
0
        LogError("%s: failed to open file %d\n", __func__, pos.nFile);
Line
Count
Source
263
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
86
0
        return false;
87
0
    }
88
1.03k
    if (finalize && 
!TruncateFile(file, pos.nPos)0
) {
89
0
        fclose(file);
90
0
        LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
Line
Count
Source
263
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
91
0
        return false;
92
0
    }
93
1.03k
    if (!FileCommit(file)) {
94
0
        fclose(file);
95
0
        LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
Line
Count
Source
263
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
Line
Count
Source
255
0
#define LogPrintLevel_(category, level, ...) LogPrintFormatInternal(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
96
0
        return false;
97
0
    }
98
1.03k
    DirectoryCommit(m_dir);
99
100
1.03k
    fclose(file);
101
1.03k
    return true;
102
1.03k
}