LCOV - code coverage report
Current view: top level - src - flatfile.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 68.3 % 63 43
Test Date: 2026-03-16 04:49:17 Functions: 100.0 % 6 6
Branches: 47.2 % 72 34

             Branch data     Line data    Source code
       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                 :         416 : FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
      16                 :         416 :     m_dir(std::move(dir)),
      17                 :         416 :     m_prefix(prefix),
      18                 :         416 :     m_chunk_size(chunk_size)
      19                 :             : {
      20         [ -  + ]:         416 :     if (chunk_size == 0) {
      21         [ #  # ]:           0 :         throw std::invalid_argument("chunk_size must be positive");
      22                 :             :     }
      23                 :         416 : }
      24                 :             : 
      25                 :           9 : std::string FlatFilePos::ToString() const
      26                 :             : {
      27                 :           9 :     return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
      28                 :             : }
      29                 :             : 
      30                 :       23776 : fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
      31                 :             : {
      32   [ -  +  +  -  :       95104 :     return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
             +  -  +  - ]
      33                 :             : }
      34                 :             : 
      35                 :       23755 : FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
      36                 :             : {
      37         [ +  - ]:       23755 :     if (pos.IsNull()) {
      38                 :             :         return nullptr;
      39                 :             :     }
      40                 :       23755 :     fs::path path = FileName(pos);
      41   [ +  -  +  - ]:       23755 :     fs::create_directories(path.parent_path());
      42   [ +  +  +  - ]:       41031 :     FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
      43         [ +  + ]:       23755 :     if (!file && !read_only)
      44         [ +  - ]:         288 :         file = fsbridge::fopen(path, "wb+");
      45         [ +  + ]:       23755 :     if (!file) {
      46   [ -  +  +  - ]:           6 :         LogError("Unable to open file %s", fs::PathToString(path));
      47                 :           3 :         return nullptr;
      48                 :             :     }
      49   [ +  +  -  + ]:       23752 :     if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
      50   [ #  #  #  # ]:           0 :         LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));
      51   [ #  #  #  # ]:           0 :         if (fclose(file) != 0) {
      52   [ #  #  #  # ]:           0 :             LogError("Unable to close file %s", fs::PathToString(path));
      53                 :             :         }
      54                 :           0 :         return nullptr;
      55                 :             :     }
      56                 :             :     return file;
      57                 :       23755 : }
      58                 :             : 
      59                 :       16825 : size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
      60                 :             : {
      61                 :       16825 :     out_of_space = false;
      62                 :             : 
      63                 :       16825 :     unsigned int n_old_chunks = CeilDiv(pos.nPos, m_chunk_size);
      64                 :       16825 :     unsigned int n_new_chunks = CeilDiv(pos.nPos + add_size, m_chunk_size);
      65         [ +  + ]:       16825 :     if (n_new_chunks > n_old_chunks) {
      66                 :         279 :         size_t old_size = pos.nPos;
      67                 :         279 :         size_t new_size = n_new_chunks * m_chunk_size;
      68                 :         279 :         size_t inc_size = new_size - old_size;
      69                 :             : 
      70         [ +  - ]:         279 :         if (CheckDiskSpace(m_dir, inc_size)) {
      71                 :         279 :             FILE *file = Open(pos);
      72         [ +  - ]:         279 :             if (file) {
      73         [ +  + ]:         279 :                 LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
      74                 :         279 :                 AllocateFileRange(file, pos.nPos, inc_size);
      75         [ -  + ]:         279 :                 if (fclose(file) != 0) {
      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                 :             :                 }
      79                 :             :                 return inc_size;
      80                 :             :             }
      81                 :             :         } else {
      82                 :           0 :             out_of_space = true;
      83                 :             :         }
      84                 :             :     }
      85                 :             :     return 0;
      86                 :             : }
      87                 :             : 
      88                 :         168 : bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
      89                 :             : {
      90                 :         168 :     FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
      91         [ -  + ]:         168 :     if (!file) {
      92                 :           0 :         LogError("%s: failed to open file %d\n", __func__, pos.nFile);
      93                 :           0 :         return false;
      94                 :             :     }
      95   [ +  +  -  + ]:         168 :     if (finalize && !TruncateFile(file, pos.nPos)) {
      96                 :           0 :         LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
      97         [ #  # ]:           0 :         if (fclose(file) != 0) {
      98                 :           0 :             LogError("Failed to close file %d", pos.nFile);
      99                 :             :         }
     100                 :           0 :         return false;
     101                 :             :     }
     102         [ -  + ]:         168 :     if (!FileCommit(file)) {
     103                 :           0 :         LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
     104         [ #  # ]:           0 :         if (fclose(file) != 0) {
     105                 :           0 :             LogError("Failed to close file %d", pos.nFile);
     106                 :             :         }
     107                 :           0 :         return false;
     108                 :             :     }
     109                 :         168 :     DirectoryCommit(m_dir);
     110                 :             : 
     111         [ -  + ]:         168 :     if (fclose(file) != 0) {
     112                 :           0 :         LogError("Failed to close file %d after flush", pos.nFile);
     113                 :           0 :         return false;
     114                 :             :     }
     115                 :             :     return true;
     116                 :             : }
        

Generated by: LCOV version 2.0-1