LCOV - code coverage report
Current view: top level - src/util - readwritefile.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 21 30 70.0 %
Date: 2022-04-21 14:51:19 Functions: 2 2 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: 10 14 71.4 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2015-2020 The Bitcoin Core developers
#       2                 :            : // Copyright (c) 2017 The Zcash 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 <fs.h>
#       7                 :            : 
#       8                 :            : #include <limits>
#       9                 :            : #include <stdio.h>
#      10                 :            : #include <string>
#      11                 :            : #include <utility>
#      12                 :            : 
#      13                 :            : std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
#      14                 :          3 : {
#      15                 :          3 :     FILE *f = fsbridge::fopen(filename, "rb");
#      16         [ +  + ]:          3 :     if (f == nullptr)
#      17                 :          1 :         return std::make_pair(false,"");
#      18                 :          2 :     std::string retval;
#      19                 :          2 :     char buffer[128];
#      20                 :          5 :     do {
#      21                 :          5 :         const size_t n = fread(buffer, 1, std::min(sizeof(buffer), maxsize - retval.size()), f);
#      22                 :            :         // Check for reading errors so we don't return any data if we couldn't
#      23                 :            :         // read the entire file (or up to maxsize)
#      24         [ -  + ]:          5 :         if (ferror(f)) {
#      25                 :          0 :             fclose(f);
#      26                 :          0 :             return std::make_pair(false,"");
#      27                 :          0 :         }
#      28                 :          5 :         retval.append(buffer, buffer+n);
#      29 [ +  + ][ +  + ]:          5 :     } while (!feof(f) && retval.size() < maxsize);
#      30                 :          2 :     fclose(f);
#      31                 :          2 :     return std::make_pair(true,retval);
#      32                 :          2 : }
#      33                 :            : 
#      34                 :            : bool WriteBinaryFile(const fs::path &filename, const std::string &data)
#      35                 :          3 : {
#      36                 :          3 :     FILE *f = fsbridge::fopen(filename, "wb");
#      37         [ -  + ]:          3 :     if (f == nullptr)
#      38                 :          0 :         return false;
#      39         [ -  + ]:          3 :     if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
#      40                 :          0 :         fclose(f);
#      41                 :          0 :         return false;
#      42                 :          0 :     }
#      43         [ -  + ]:          3 :     if (fclose(f) != 0) {
#      44                 :          0 :         return false;
#      45                 :          0 :     }
#      46                 :          3 :     return true;
#      47                 :          3 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a