LCOV - code coverage report
Current view: top level - src - flatfile.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 75.8 % 66 50
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 6 6
Branches: 45.3 % 64 29

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

Generated by: LCOV version 2.0-1