LCOV - code coverage report
Current view: top level - src - flatfile.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 53 70 75.7 %
Date: 2021-06-29 14:35:33 Functions: 6 6 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 24 32 75.0 %

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

Generated by: LCOV version 1.14