LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.0 % 50 45
Test Date: 2024-08-28 05:13:07 Functions: 100.0 % 6 6
Branches: 53.9 % 76 41

             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 <span.h>
       6                 :             : #include <streams.h>
       7                 :             : 
       8                 :             : #include <array>
       9                 :             : 
      10                 :    24975848 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
      11                 :             : {
      12   [ +  +  +  - ]:    24975848 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      13         [ +  + ]:    24975847 :     if (m_xor.empty()) {
      14         [ +  - ]:    43166553 :         return std::fread(dst.data(), 1, dst.size(), m_file);
      15                 :             :     } else {
      16                 :     6785141 :         const auto init_pos{std::ftell(m_file)};
      17   [ -  +  -  - ]:     6785141 :         if (init_pos < 0) throw std::ios_base::failure("AutoFile::read: ftell failed");
      18         [ +  - ]:     6785141 :         std::size_t ret{std::fread(dst.data(), 1, dst.size(), m_file)};
      19                 :     6785141 :         util::Xor(dst.subspan(0, ret), m_xor, init_pos);
      20                 :     6785141 :         return ret;
      21                 :             :     }
      22                 :             : }
      23                 :             : 
      24                 :       16100 : void AutoFile::seek(int64_t offset, int origin)
      25                 :             : {
      26         [ -  + ]:       16100 :     if (IsNull()) {
      27         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
      28                 :             :     }
      29         [ -  + ]:       16100 :     if (std::fseek(m_file, offset, origin) != 0) {
      30   [ #  #  #  # ]:           0 :         throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
      31                 :             :     }
      32                 :       16100 : }
      33                 :             : 
      34                 :          49 : int64_t AutoFile::tell()
      35                 :             : {
      36         [ -  + ]:          49 :     if (IsNull()) {
      37         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::tell: file handle is nullptr");
      38                 :             :     }
      39                 :          49 :     int64_t r{std::ftell(m_file)};
      40         [ -  + ]:          49 :     if (r < 0) {
      41         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::tell: ftell failed");
      42                 :             :     }
      43                 :          49 :     return r;
      44                 :             : }
      45                 :             : 
      46                 :    24975418 : void AutoFile::read(Span<std::byte> dst)
      47                 :             : {
      48         [ +  + ]:    24975418 :     if (detail_fread(dst) != dst.size()) {
      49   [ -  +  +  - ]:          30 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      50                 :             :     }
      51                 :    24975387 : }
      52                 :             : 
      53                 :       15025 : void AutoFile::ignore(size_t nSize)
      54                 :             : {
      55   [ +  +  +  - ]:       15025 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      56                 :             :     unsigned char data[4096];
      57         [ +  + ]:       30046 :     while (nSize > 0) {
      58         [ +  - ]:       15024 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      59   [ -  +  +  + ]:       30048 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      60   [ -  +  +  - ]:           2 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      61                 :             :         }
      62                 :       15022 :         nSize -= nNow;
      63                 :             :     }
      64                 :       15022 : }
      65                 :             : 
      66                 :    45161913 : void AutoFile::write(Span<const std::byte> src)
      67                 :             : {
      68   [ +  +  +  - ]:    45161913 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      69         [ +  + ]:    45161912 :     if (m_xor.empty()) {
      70         [ +  + ]:    37661826 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
      71         [ +  - ]:           1 :             throw std::ios_base::failure("AutoFile::write: write failed");
      72                 :             :         }
      73                 :             :     } else {
      74                 :     7500086 :         auto current_pos{std::ftell(m_file)};
      75   [ -  +  -  - ]:     7500086 :         if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
      76                 :             :         std::array<std::byte, 4096> buf;
      77         [ +  + ]:    16953720 :         while (src.size() > 0) {
      78         [ +  + ]:    16953718 :             auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
      79                 :     9453634 :             std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
      80                 :     9453634 :             util::Xor(buf_now, m_xor, current_pos);
      81         [ -  + ]:     9453634 :             if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
      82         [ #  # ]:           0 :                 throw std::ios_base::failure{"XorFile::write: failed"};
      83                 :             :             }
      84                 :     9453634 :             src = src.subspan(buf_now.size());
      85                 :     9453634 :             current_pos += buf_now.size();
      86                 :             :         }
      87                 :             :     }
      88                 :    45161911 : }
        

Generated by: LCOV version 2.0-1