Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/flatfile.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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 <flatfile.h>
7
8
#include <tinyformat.h>
9
#include <util/fs_helpers.h>
10
#include <util/log.h>
11
#include <util/overflow.h>
12
13
#include <stdexcept>
14
15
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
16
6.28k
    m_dir(std::move(dir)),
17
6.28k
    m_prefix(prefix),
18
6.28k
    m_chunk_size(chunk_size)
19
6.28k
{
20
6.28k
    if (chunk_size == 0) {
  Branch (20:9): [True: 0, False: 6.28k]
21
0
        throw std::invalid_argument("chunk_size must be positive");
22
0
    }
23
6.28k
}
24
25
std::string FlatFilePos::ToString() const
26
14.8k
{
27
14.8k
    return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
28
14.8k
}
29
30
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
31
967k
{
32
967k
    return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
33
967k
}
34
35
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
36
982k
{
37
982k
    if (pos.IsNull()) {
  Branch (37:9): [True: 14.8k, False: 967k]
38
14.8k
        return nullptr;
39
14.8k
    }
40
967k
    fs::path path = FileName(pos);
41
967k
    fs::create_directories(path.parent_path());
42
967k
    FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
  Branch (42:40): [True: 6.25k, False: 961k]
43
967k
    if (!file && !read_only)
  Branch (43:9): [True: 2.75k, False: 964k]
  Branch (43:18): [True: 2.75k, False: 0]
44
2.75k
        file = fsbridge::fopen(path, "wb+");
45
967k
    if (!file) {
  Branch (45:9): [True: 0, False: 967k]
46
0
        LogError("Unable to open file %s", fs::PathToString(path));
47
0
        return nullptr;
48
0
    }
49
967k
    if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
  Branch (49:9): [True: 689k, False: 277k]
  Branch (49:21): [True: 0, False: 689k]
50
0
        LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));
51
0
        if (fclose(file) != 0) {
  Branch (51:13): [True: 0, False: 0]
52
0
            LogError("Unable to close file %s", fs::PathToString(path));
53
0
        }
54
0
        return nullptr;
55
0
    }
56
967k
    return file;
57
967k
}
58
59
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
60
695k
{
61
695k
    out_of_space = false;
62
63
695k
    unsigned int n_old_chunks = CeilDiv(pos.nPos, m_chunk_size);
64
695k
    unsigned int n_new_chunks = CeilDiv(pos.nPos + add_size, m_chunk_size);
65
695k
    if (n_new_chunks > n_old_chunks) {
  Branch (65:9): [True: 5.74k, False: 689k]
66
5.74k
        size_t old_size = pos.nPos;
67
5.74k
        size_t new_size = n_new_chunks * m_chunk_size;
68
5.74k
        size_t inc_size = new_size - old_size;
69
70
5.74k
        if (CheckDiskSpace(m_dir, inc_size)) {
  Branch (70:13): [True: 5.74k, False: 0]
71
5.74k
            FILE *file = Open(pos);
72
5.74k
            if (file) {
  Branch (72:17): [True: 5.74k, False: 0]
73
5.74k
                LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
74
5.74k
                AllocateFileRange(file, pos.nPos, inc_size);
75
5.74k
                if (fclose(file) != 0) {
  Branch (75:21): [True: 0, False: 5.74k]
76
0
                    LogError("Cannot close file %s%05u.dat after extending it with %u bytes", m_prefix, pos.nFile, new_size);
77
0
                    return 0;
78
0
                }
79
5.74k
                return inc_size;
80
5.74k
            }
81
5.74k
        } else {
82
0
            out_of_space = true;
83
0
        }
84
5.74k
    }
85
689k
    return 0;
86
695k
}
87
88
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
89
259k
{
90
259k
    FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
91
259k
    if (!file) {
  Branch (91:9): [True: 0, False: 259k]
92
0
        LogError("%s: failed to open file %d\n", __func__, pos.nFile);
93
0
        return false;
94
0
    }
95
259k
    if (finalize && !TruncateFile(file, pos.nPos)) {
  Branch (95:9): [True: 0, False: 259k]
  Branch (95:21): [True: 0, False: 0]
96
0
        LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
97
0
        if (fclose(file) != 0) {
  Branch (97:13): [True: 0, False: 0]
98
0
            LogError("Failed to close file %d", pos.nFile);
99
0
        }
100
0
        return false;
101
0
    }
102
259k
    if (!FileCommit(file)) {
  Branch (102:9): [True: 0, False: 259k]
103
0
        LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
104
0
        if (fclose(file) != 0) {
  Branch (104:13): [True: 0, False: 0]
105
0
            LogError("Failed to close file %d", pos.nFile);
106
0
        }
107
0
        return false;
108
0
    }
109
259k
    DirectoryCommit(m_dir);
110
111
259k
    if (fclose(file) != 0) {
  Branch (111:9): [True: 0, False: 259k]
112
0
        LogError("Failed to close file %d after flush", pos.nFile);
113
0
        return false;
114
0
    }
115
259k
    return true;
116
259k
}