Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/flatfile.cpp
Line
Count
Source
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
33.2k
    m_dir(std::move(dir)),
15
33.2k
    m_prefix(prefix),
16
33.2k
    m_chunk_size(chunk_size)
17
33.2k
{
18
33.2k
    if (chunk_size == 0) {
  Branch (18:9): [True: 0, False: 33.2k]
19
0
        throw std::invalid_argument("chunk_size must be positive");
20
0
    }
21
33.2k
}
22
23
std::string FlatFilePos::ToString() const
24
0
{
25
0
    return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
26
0
}
27
28
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
29
9.06M
{
30
9.06M
    return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
31
9.06M
}
32
33
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
34
9.06M
{
35
9.06M
    if (pos.IsNull()) {
  Branch (35:9): [True: 0, False: 9.06M]
36
0
        return nullptr;
37
0
    }
38
9.06M
    fs::path path = FileName(pos);
39
9.06M
    fs::create_directories(path.parent_path());
40
9.06M
    FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
  Branch (40:40): [True: 2.25M, False: 6.81M]
41
9.06M
    if (!file && !read_only)
  Branch (41:9): [True: 33.2k, False: 9.03M]
  Branch (41:18): [True: 33.2k, False: 0]
42
33.2k
        file = fsbridge::fopen(path, "wb+");
43
9.06M
    if (!file) {
  Branch (43:9): [True: 0, False: 9.06M]
44
0
        LogPrintf("Unable to open file %s\n", fs::PathToString(path));
45
0
        return nullptr;
46
0
    }
47
9.06M
    if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
  Branch (47:9): [True: 8.93M, False: 136k]
  Branch (47:21): [True: 0, False: 8.93M]
48
0
        LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, fs::PathToString(path));
49
0
        fclose(file);
50
0
        return nullptr;
51
0
    }
52
9.06M
    return file;
53
9.06M
}
54
55
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
56
6.70M
{
57
6.70M
    out_of_space = false;
58
59
6.70M
    unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
60
6.70M
    unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
61
6.70M
    if (n_new_chunks > n_old_chunks) {
  Branch (61:9): [True: 33.2k, False: 6.66M]
62
33.2k
        size_t old_size = pos.nPos;
63
33.2k
        size_t new_size = n_new_chunks * m_chunk_size;
64
33.2k
        size_t inc_size = new_size - old_size;
65
66
33.2k
        if (CheckDiskSpace(m_dir, inc_size)) {
  Branch (66:13): [True: 33.2k, False: 0]
67
33.2k
            FILE *file = Open(pos);
68
33.2k
            if (file) {
  Branch (68:17): [True: 33.2k, False: 0]
69
33.2k
                LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
70
33.2k
                AllocateFileRange(file, pos.nPos, inc_size);
71
33.2k
                fclose(file);
72
33.2k
                return inc_size;
73
33.2k
            }
74
33.2k
        } else {
75
0
            out_of_space = true;
76
0
        }
77
33.2k
    }
78
6.66M
    return 0;
79
6.70M
}
80
81
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
82
58.4k
{
83
58.4k
    FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
84
58.4k
    if (!file) {
  Branch (84:9): [True: 0, False: 58.4k]
85
0
        LogError("%s: failed to open file %d\n", __func__, pos.nFile);
86
0
        return false;
87
0
    }
88
58.4k
    if (finalize && !TruncateFile(file, pos.nPos)) {
  Branch (88:9): [True: 0, False: 58.4k]
  Branch (88:21): [True: 0, False: 0]
89
0
        fclose(file);
90
0
        LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
91
0
        return false;
92
0
    }
93
58.4k
    if (!FileCommit(file)) {
  Branch (93:9): [True: 0, False: 58.4k]
94
0
        fclose(file);
95
0
        LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
96
0
        return false;
97
0
    }
98
58.4k
    DirectoryCommit(m_dir);
99
100
58.4k
    fclose(file);
101
58.4k
    return true;
102
58.4k
}