LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 90.6 % 64 58
Test Date: 2024-11-04 04:45:35 Functions: 88.9 % 9 8
Branches: 56.4 % 94 53

             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                 :       18373 : AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
      12         [ +  + ]:       18373 :     : m_file{file}, m_xor{std::move(data_xor)}
      13                 :             : {
      14         [ +  + ]:       18373 :     if (!IsNull()) {
      15         [ +  - ]:       18266 :         auto pos{std::ftell(m_file)};
      16         [ +  - ]:       18266 :         if (pos >= 0) m_position = pos;
      17                 :             :     }
      18                 :       18373 : }
      19                 :             : 
      20                 :       76559 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
      21                 :             : {
      22   [ +  +  +  - ]:       76559 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      23         [ +  - ]:       76558 :     size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
      24         [ +  + ]:       76558 :     if (!m_xor.empty()) {
      25   [ -  +  -  - ]:       61219 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
      26                 :       61219 :         util::Xor(dst.subspan(0, ret), m_xor, *m_position);
      27                 :             :     }
      28         [ +  - ]:       76558 :     if (m_position.has_value()) *m_position += ret;
      29                 :       76558 :     return ret;
      30                 :             : }
      31                 :             : 
      32                 :         168 : void AutoFile::seek(int64_t offset, int origin)
      33                 :             : {
      34         [ -  + ]:         168 :     if (IsNull()) {
      35         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
      36                 :             :     }
      37         [ -  + ]:         168 :     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         [ +  + ]:         168 :     if (origin == SEEK_SET) {
      41                 :          52 :         m_position = offset;
      42   [ +  +  +  - ]:         116 :     } else if (origin == SEEK_CUR && m_position.has_value()) {
      43                 :         110 :         *m_position += offset;
      44                 :             :     } else {
      45                 :           6 :         int64_t r{std::ftell(m_file)};
      46         [ -  + ]:           6 :         if (r < 0) {
      47         [ #  # ]:           0 :             throw std::ios_base::failure("AutoFile::seek: ftell failed");
      48                 :             :         }
      49                 :           6 :         m_position = r;
      50                 :             :     }
      51                 :         168 : }
      52                 :             : 
      53                 :       14464 : int64_t AutoFile::tell()
      54                 :             : {
      55   [ -  +  -  - ]:       14464 :     if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
      56                 :       14464 :     return *m_position;
      57                 :             : }
      58                 :             : 
      59                 :       76275 : void AutoFile::read(Span<std::byte> dst)
      60                 :             : {
      61         [ +  + ]:       76275 :     if (detail_fread(dst) != dst.size()) {
      62   [ -  +  +  - ]:          16 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      63                 :             :     }
      64                 :       76258 : }
      65                 :             : 
      66                 :           4 : void AutoFile::ignore(size_t nSize)
      67                 :             : {
      68   [ +  +  +  - ]:           4 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      69                 :             :     unsigned char data[4096];
      70         [ +  + ]:           4 :     while (nSize > 0) {
      71         [ +  - ]:           3 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      72   [ -  +  +  + ]:           6 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      73   [ -  +  +  - ]:           2 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      74                 :             :         }
      75                 :           1 :         nSize -= nNow;
      76         [ +  - ]:           1 :         if (m_position.has_value()) *m_position += nNow;
      77                 :             :     }
      78                 :           1 : }
      79                 :             : 
      80                 :      274805 : void AutoFile::write(Span<const std::byte> src)
      81                 :             : {
      82   [ +  +  +  - ]:      274805 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      83         [ +  + ]:      274804 :     if (m_xor.empty()) {
      84         [ +  + ]:       33202 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
      85         [ +  - ]:           1 :             throw std::ios_base::failure("AutoFile::write: write failed");
      86                 :             :         }
      87         [ +  - ]:       33201 :         if (m_position.has_value()) *m_position += src.size();
      88                 :             :     } else {
      89   [ -  +  -  - ]:      241602 :         if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown");
      90                 :             :         std::array<std::byte, 4096> buf;
      91         [ +  + ]:      483204 :         while (src.size() > 0) {
      92         [ +  - ]:      483204 :             auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
      93                 :      241602 :             std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
      94                 :      241602 :             util::Xor(buf_now, m_xor, *m_position);
      95         [ -  + ]:      241602 :             if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
      96         [ #  # ]:           0 :                 throw std::ios_base::failure{"XorFile::write: failed"};
      97                 :             :             }
      98                 :      241602 :             src = src.subspan(buf_now.size());
      99                 :      241602 :             *m_position += buf_now.size();
     100                 :             :         }
     101                 :             :     }
     102                 :      274803 : }
     103                 :             : 
     104                 :           4 : bool AutoFile::Commit()
     105                 :             : {
     106                 :           4 :     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