LCOV - code coverage report
Current view: top level - src - flatfile.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 81.8 % 11 9
Test Date: 2024-12-02 12:23:29 Functions: 100.0 % 2 2
Branches: 40.0 % 40 16

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2020 The Bitcoin Core 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                 :             : #ifndef BITCOIN_FLATFILE_H
       7                 :             : #define BITCOIN_FLATFILE_H
       8                 :             : 
       9                 :             : #include <string>
      10                 :             : 
      11                 :             : #include <serialize.h>
      12                 :             : #include <util/fs.h>
      13                 :             : 
      14                 :             : struct FlatFilePos
      15                 :             : {
      16                 :             :     int nFile{-1};
      17                 :             :     unsigned int nPos{0};
      18                 :             : 
      19                 :         316 :     SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
      20                 :             : 
      21         [ #  # ]:           0 :     FlatFilePos() = default;
      22                 :             : 
      23                 :      438916 :     FlatFilePos(int nFileIn, unsigned int nPosIn) :
      24                 :      438916 :         nFile(nFileIn),
      25   [ -  -  +  -  :      438916 :         nPos(nPosIn)
           -  - ][ #  # ]
      26                 :             :     {}
      27                 :             : 
      28                 :          38 :     friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
      29   [ +  -  +  - ]:          32 :         return (a.nFile == b.nFile && a.nPos == b.nPos);
           [ +  +  +  -  
                   +  - ]
      30                 :             :     }
      31                 :             : 
      32                 :          10 :     friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
      33   [ +  +  -  + ]:          20 :         return !(a == b);
      34                 :             :     }
      35                 :             : 
      36   [ -  +  -  -  :      671898 :     bool IsNull() const { return (nFile == -1); }
                   -  + ]
           [ -  +  +  - ]
           [ +  +  #  #  
                   #  # ]
      37                 :             : 
      38                 :             :     std::string ToString() const;
      39                 :             : };
      40                 :             : 
      41                 :             : /**
      42                 :             :  * FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
      43                 :             :  * access to and efficient management of these files.
      44                 :             :  */
      45                 :           0 : class FlatFileSeq
      46                 :             : {
      47                 :             : private:
      48                 :             :     const fs::path m_dir;
      49                 :             :     const char* const m_prefix;
      50                 :             :     const size_t m_chunk_size;
      51                 :             : 
      52                 :             : public:
      53                 :             :     /**
      54                 :             :      * Constructor
      55                 :             :      *
      56                 :             :      * @param dir The base directory that all files live in.
      57                 :             :      * @param prefix A short prefix given to all file names.
      58                 :             :      * @param chunk_size Disk space is pre-allocated in multiples of this amount.
      59                 :             :      */
      60                 :             :     FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
      61                 :             : 
      62                 :             :     /** Get the name of the file at the given position. */
      63                 :             :     fs::path FileName(const FlatFilePos& pos) const;
      64                 :             : 
      65                 :             :     /** Open a handle to the file at the given position. */
      66                 :             :     FILE* Open(const FlatFilePos& pos, bool read_only = false) const;
      67                 :             : 
      68                 :             :     /**
      69                 :             :      * Allocate additional space in a file after the given starting position. The amount allocated
      70                 :             :      * will be the minimum multiple of the sequence chunk size greater than add_size.
      71                 :             :      *
      72                 :             :      * @param[in] pos The starting position that bytes will be allocated after.
      73                 :             :      * @param[in] add_size The minimum number of bytes to be allocated.
      74                 :             :      * @param[out] out_of_space Whether the allocation failed due to insufficient disk space.
      75                 :             :      * @return The number of bytes successfully allocated.
      76                 :             :      */
      77                 :             :     size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const;
      78                 :             : 
      79                 :             :     /**
      80                 :             :      * Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
      81                 :             :      *
      82                 :             :      * @param[in] pos The first unwritten position in the file to be flushed.
      83                 :             :      * @param[in] finalize True if no more data will be written to this file.
      84                 :             :      * @return true on success, false on failure.
      85                 :             :      */
      86                 :             :     bool Flush(const FlatFilePos& pos, bool finalize = false) const;
      87                 :             : };
      88                 :             : 
      89                 :             : #endif // BITCOIN_FLATFILE_H
        

Generated by: LCOV version 2.0-1