LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 89.1 % 64 57
Test Date: 2024-11-04 04:15:01 Functions: 77.8 % 9 7
Branches: 70.2 % 94 66

             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                 :             : #include <util/fs_helpers.h>
       8                 :             : 
       9                 :             : #include <array>
      10                 :             : 
      11                 :      189487 : AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
      12         [ +  + ]:      189487 :     : m_file{file}, m_xor{std::move(data_xor)}
      13                 :             : {
      14         [ +  + ]:      189487 :     if (!IsNull()) {
      15         [ +  - ]:      188506 :         auto pos{std::ftell(m_file)};
      16         [ +  + ]:      188506 :         if (pos >= 0) m_position = pos;
      17                 :             :     }
      18                 :      189487 : }
      19                 :             : 
      20                 :     8799005 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
      21                 :             : {
      22   [ +  +  +  - ]:     8799005 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      23         [ +  - ]:     8798523 :     size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
      24         [ +  + ]:     8798523 :     if (!m_xor.empty()) {
      25   [ +  +  +  - ]:      119146 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
      26                 :      118752 :         util::Xor(dst.subspan(0, ret), m_xor, *m_position);
      27                 :             :     }
      28         [ +  + ]:     8798129 :     if (m_position.has_value()) *m_position += ret;
      29                 :     8798129 :     return ret;
      30                 :             : }
      31                 :             : 
      32                 :         183 : void AutoFile::seek(int64_t offset, int origin)
      33                 :             : {
      34         [ -  + ]:         183 :     if (IsNull()) {
      35         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
      36                 :             :     }
      37         [ -  + ]:         183 :     if (std::fseek(m_file, offset, origin) != 0) {
      38   [ #  #  #  # ]:           0 :         throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
      39                 :             :     }
      40         [ +  + ]:         183 :     if (origin == SEEK_SET) {
      41                 :          63 :         m_position = offset;
      42   [ +  +  +  - ]:         120 :     } else if (origin == SEEK_CUR && m_position.has_value()) {
      43                 :          70 :         *m_position += offset;
      44                 :             :     } else {
      45                 :          50 :         int64_t r{std::ftell(m_file)};
      46         [ -  + ]:          50 :         if (r < 0) {
      47         [ #  # ]:           0 :             throw std::ios_base::failure("AutoFile::seek: ftell failed");
      48                 :             :         }
      49                 :          50 :         m_position = r;
      50                 :             :     }
      51                 :         183 : }
      52                 :             : 
      53                 :      174319 : int64_t AutoFile::tell()
      54                 :             : {
      55   [ -  +  -  - ]:      174319 :     if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
      56                 :      174319 :     return *m_position;
      57                 :             : }
      58                 :             : 
      59                 :     8796400 : void AutoFile::read(Span<std::byte> dst)
      60                 :             : {
      61         [ +  + ]:     8796400 :     if (detail_fread(dst) != dst.size()) {
      62   [ +  +  +  - ]:        2898 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      63                 :             :     }
      64                 :     8793425 : }
      65                 :             : 
      66                 :         673 : void AutoFile::ignore(size_t nSize)
      67                 :             : {
      68   [ +  +  +  - ]:         673 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      69                 :             :     unsigned char data[4096];
      70         [ +  + ]:        1368 :     while (nSize > 0) {
      71         [ +  + ]:        1102 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      72   [ -  +  +  + ]:        2204 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      73   [ +  +  +  - ]:         385 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      74                 :             :         }
      75                 :         816 :         nSize -= nNow;
      76         [ +  + ]:         816 :         if (m_position.has_value()) *m_position += nNow;
      77                 :             :     }
      78                 :         266 : }
      79                 :             : 
      80                 :     3791977 : void AutoFile::write(Span<const std::byte> src)
      81                 :             : {
      82   [ +  +  +  - ]:     3791977 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      83         [ +  + ]:     3791019 :     if (m_xor.empty()) {
      84         [ +  + ]:      633810 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
      85         [ +  - ]:        1887 :             throw std::ios_base::failure("AutoFile::write: write failed");
      86                 :             :         }
      87         [ +  + ]:      631923 :         if (m_position.has_value()) *m_position += src.size();
      88                 :             :     } else {
      89   [ +  +  +  - ]:     3157209 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
      90                 :             :         std::array<std::byte, 4096> buf;
      91         [ +  + ]:     6313425 :         while (src.size() > 0) {
      92         [ +  - ]:     6313554 :             auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
      93                 :     3156777 :             std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
      94                 :     3156777 :             util::Xor(buf_now, m_xor, *m_position);
      95         [ +  + ]:     3156777 :             if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
      96         [ +  - ]:         137 :                 throw std::ios_base::failure{"XorFile::write: failed"};
      97                 :             :             }
      98                 :     3156640 :             src = src.subspan(buf_now.size());
      99                 :     3156640 :             *m_position += buf_now.size();
     100                 :             :         }
     101                 :             :     }
     102                 :     3788571 : }
     103                 :             : 
     104                 :           0 : bool AutoFile::Commit()
     105                 :             : {
     106                 :           0 :     return ::FileCommit(m_file);
     107                 :             : }
     108                 :             : 
     109                 :           0 : bool AutoFile::Truncate(unsigned size)
     110                 :             : {
     111                 :           0 :     return ::TruncateFile(m_file, size);
     112                 :             : }
        

Generated by: LCOV version 2.0-1