LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.9 % 66 60
Test Date: 2025-01-19 05:08:01 Functions: 90.0 % 10 9
Branches: 58.3 % 96 56

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/license/mit/.
       4                 :             : 
       5                 :             : #include <memusage.h>
       6                 :             : #include <span.h>
       7                 :             : #include <streams.h>
       8                 :             : #include <util/fs_helpers.h>
       9                 :             : 
      10                 :             : #include <array>
      11                 :             : 
      12                 :      490871 : AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
      13         [ +  + ]:      490871 :     : m_file{file}, m_xor{std::move(data_xor)}
      14                 :             : {
      15         [ +  + ]:      490871 :     if (!IsNull()) {
      16         [ +  - ]:      489339 :         auto pos{std::ftell(m_file)};
      17         [ +  - ]:      489339 :         if (pos >= 0) m_position = pos;
      18                 :             :     }
      19                 :      490871 : }
      20                 :             : 
      21                 :    20758875 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
      22                 :             : {
      23   [ +  +  +  - ]:    20758875 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      24         [ +  - ]:    20758874 :     size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
      25         [ +  + ]:    20758874 :     if (!m_xor.empty()) {
      26   [ -  +  -  - ]:     5037819 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
      27                 :     5037819 :         util::Xor(dst.subspan(0, ret), m_xor, *m_position);
      28                 :             :     }
      29         [ +  - ]:    20758874 :     if (m_position.has_value()) *m_position += ret;
      30                 :    20758874 :     return ret;
      31                 :             : }
      32                 :             : 
      33                 :        2117 : void AutoFile::seek(int64_t offset, int origin)
      34                 :             : {
      35         [ -  + ]:        2117 :     if (IsNull()) {
      36         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
      37                 :             :     }
      38         [ -  + ]:        2117 :     if (std::fseek(m_file, offset, origin) != 0) {
      39   [ #  #  #  # ]:           0 :         throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
      40                 :             :     }
      41         [ +  + ]:        2117 :     if (origin == SEEK_SET) {
      42                 :         276 :         m_position = offset;
      43   [ +  +  +  - ]:        1841 :     } else if (origin == SEEK_CUR && m_position.has_value()) {
      44                 :        1790 :         *m_position += offset;
      45                 :             :     } else {
      46                 :          51 :         int64_t r{std::ftell(m_file)};
      47         [ -  + ]:          51 :         if (r < 0) {
      48         [ #  # ]:           0 :             throw std::ios_base::failure("AutoFile::seek: ftell failed");
      49                 :             :         }
      50                 :          51 :         m_position = r;
      51                 :             :     }
      52                 :        2117 : }
      53                 :             : 
      54                 :      243874 : int64_t AutoFile::tell()
      55                 :             : {
      56   [ -  +  -  - ]:      243874 :     if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
      57                 :      243874 :     return *m_position;
      58                 :             : }
      59                 :             : 
      60                 :    20758469 : void AutoFile::read(Span<std::byte> dst)
      61                 :             : {
      62         [ +  + ]:    20758469 :     if (detail_fread(dst) != dst.size()) {
      63   [ -  +  +  - ]:          32 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      64                 :             :     }
      65                 :    20758436 : }
      66                 :             : 
      67                 :        1651 : void AutoFile::ignore(size_t nSize)
      68                 :             : {
      69   [ +  +  +  - ]:        1651 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      70                 :             :     unsigned char data[4096];
      71         [ +  + ]:        3298 :     while (nSize > 0) {
      72         [ +  - ]:        1650 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      73   [ -  +  +  + ]:        3300 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      74   [ -  +  +  - ]:           2 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      75                 :             :         }
      76                 :        1648 :         nSize -= nNow;
      77         [ +  - ]:        1648 :         if (m_position.has_value()) *m_position += nNow;
      78                 :             :     }
      79                 :        1648 : }
      80                 :             : 
      81                 :    38478712 : void AutoFile::write(Span<const std::byte> src)
      82                 :             : {
      83   [ +  +  +  - ]:    38478712 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      84         [ +  + ]:    38478711 :     if (m_xor.empty()) {
      85         [ +  + ]:    31815950 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
      86         [ +  - ]:           1 :             throw std::ios_base::failure("AutoFile::write: write failed");
      87                 :             :         }
      88         [ +  - ]:    31815949 :         if (m_position.has_value()) *m_position += src.size();
      89                 :             :     } else {
      90   [ -  +  -  - ]:     6662761 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
      91                 :             :         std::array<std::byte, 4096> buf;
      92         [ +  + ]:    14976547 :         while (src.size() > 0) {
      93         [ +  + ]:    14976545 :             auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
      94                 :     8313786 :             std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
      95                 :     8313786 :             util::Xor(buf_now, m_xor, *m_position);
      96         [ -  + ]:     8313786 :             if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
      97         [ #  # ]:           0 :                 throw std::ios_base::failure{"XorFile::write: failed"};
      98                 :             :             }
      99                 :     8313786 :             src = src.subspan(buf_now.size());
     100                 :     8313786 :             *m_position += buf_now.size();
     101                 :             :         }
     102                 :             :     }
     103                 :    38478710 : }
     104                 :             : 
     105                 :        2409 : bool AutoFile::Commit()
     106                 :             : {
     107                 :        2409 :     return ::FileCommit(m_file);
     108                 :             : }
     109                 :             : 
     110                 :           0 : bool AutoFile::Truncate(unsigned size)
     111                 :             : {
     112                 :           0 :     return ::TruncateFile(m_file, size);
     113                 :             : }
     114                 :             : 
     115                 :      319938 : size_t DataStream::GetMemoryUsage() const noexcept
     116                 :             : {
     117         [ +  + ]:      319938 :     return sizeof(*this) + memusage::DynamicUsage(vch);
     118                 :             : }
        

Generated by: LCOV version 2.0-1