Bitcoin Core 30.99.0
P2P Digital Currency
Public Member Functions | Protected Attributes | List of all members
AutoFile Class Reference

Non-refcounted RAII wrapper for FILE*. More...

#include <streams.h>

Collaboration diagram for AutoFile:
[legend]

Public Member Functions

 AutoFile (std::FILE *file, const Obfuscation &obfuscation={})
 
 ~AutoFile ()
 
 AutoFile (const AutoFile &)=delete
 
AutoFileoperator= (const AutoFile &)=delete
 
bool feof () const
 
int fclose ()
 
std::FILE * release ()
 Get wrapped FILE* with transfer of ownership. More...
 
bool IsNull () const
 Return true if the wrapped FILE* is nullptr, false otherwise. More...
 
void SetObfuscation (const Obfuscation &obfuscation)
 Continue with a different XOR key. More...
 
std::size_t detail_fread (std::span< std::byte > dst)
 Implementation detail, only used internally. More...
 
void seek (int64_t offset, int origin)
 Wrapper around fseek(). More...
 
int64_t tell ()
 Find position within the file. More...
 
bool Commit ()
 Wrapper around FileCommit(). More...
 
bool Truncate (unsigned size)
 Wrapper around TruncateFile(). More...
 
void write_buffer (std::span< std::byte > src)
 Write a mutable buffer more efficiently than write(), obfuscating the buffer in-place. More...
 
void read (std::span< std::byte > dst)
 
void ignore (size_t nSize)
 
void write (std::span< const std::byte > src)
 
template<typename T >
AutoFileoperator<< (const T &obj)
 
template<typename T >
AutoFileoperator>> (T &&obj)
 

Protected Attributes

std::FILE * m_file
 
Obfuscation m_obfuscation
 
std::optional< int64_t > m_position
 
bool m_was_written {false}
 

Detailed Description

Non-refcounted RAII wrapper for FILE*.

Will automatically close the file when it goes out of scope if not null. If you're returning the file pointer, return file.release(). If you need to close the file early, use autofile.fclose() instead of fclose(underlying_FILE).

Note
If the file has been written to, then the caller must close it explicitly with the fclose() method, check if it returns an error and treat such an error as if the write() method failed. The OS's fclose(3) may fail to flush to disk data that has been previously written, rendering the file corrupt.

Definition at line 370 of file streams.h.

Constructor & Destructor Documentation

◆ AutoFile() [1/2]

AutoFile::AutoFile ( std::FILE *  file,
const Obfuscation obfuscation = {} 
)
explicit

Definition at line 13 of file streams.cpp.

Here is the call graph for this function:

◆ ~AutoFile()

AutoFile::~AutoFile ( )
inline

Definition at line 381 of file streams.h.

Here is the call graph for this function:

◆ AutoFile() [2/2]

AutoFile::AutoFile ( const AutoFile )
delete

Member Function Documentation

◆ Commit()

bool AutoFile::Commit ( )

Wrapper around FileCommit().

Definition at line 115 of file streams.cpp.

Here is the call graph for this function:

◆ detail_fread()

std::size_t AutoFile::detail_fread ( std::span< std::byte >  dst)

Implementation detail, only used internally.

Definition at line 21 of file streams.cpp.

Here is the caller graph for this function:

◆ fclose()

int AutoFile::fclose ( )
inline

Definition at line 405 of file streams.h.

Here is the caller graph for this function:

◆ feof()

bool AutoFile::feof ( ) const
inline

Definition at line 403 of file streams.h.

Here is the caller graph for this function:

◆ ignore()

void AutoFile::ignore ( size_t  nSize)

Definition at line 67 of file streams.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsNull()

bool AutoFile::IsNull ( ) const
inline

Return true if the wrapped FILE* is nullptr, false otherwise.

Definition at line 424 of file streams.h.

Here is the caller graph for this function:

◆ operator<<()

template<typename T >
AutoFile & AutoFile::operator<< ( const T obj)
inline

Definition at line 455 of file streams.h.

Here is the call graph for this function:

◆ operator=()

AutoFile & AutoFile::operator= ( const AutoFile )
delete

◆ operator>>()

template<typename T >
AutoFile & AutoFile::operator>> ( T &&  obj)
inline

Definition at line 462 of file streams.h.

Here is the call graph for this function:

◆ read()

void AutoFile::read ( std::span< std::byte >  dst)

Definition at line 60 of file streams.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ release()

std::FILE * AutoFile::release ( )
inline

Get wrapped FILE* with transfer of ownership.

Note
This will invalidate the AutoFile object, and makes it the responsibility of the caller of this function to clean up the returned FILE*.

Definition at line 415 of file streams.h.

◆ seek()

void AutoFile::seek ( int64_t  offset,
int  origin 
)

Wrapper around fseek().

Will throw if seeking is not possible.

Definition at line 33 of file streams.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetObfuscation()

void AutoFile::SetObfuscation ( const Obfuscation obfuscation)
inline

Continue with a different XOR key.

Definition at line 427 of file streams.h.

◆ tell()

int64_t AutoFile::tell ( )

Find position within the file.

Will throw if unknown.

Definition at line 54 of file streams.cpp.

Here is the caller graph for this function:

◆ Truncate()

bool AutoFile::Truncate ( unsigned  size)

Wrapper around TruncateFile().

Definition at line 120 of file streams.cpp.

Here is the call graph for this function:

◆ write()

void AutoFile::write ( std::span< const std::byte >  src)

Definition at line 81 of file streams.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_buffer()

void AutoFile::write_buffer ( std::span< std::byte >  src)

Write a mutable buffer more efficiently than write(), obfuscating the buffer in-place.

Definition at line 101 of file streams.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ m_file

std::FILE* AutoFile::m_file
protected

Definition at line 373 of file streams.h.

◆ m_obfuscation

Obfuscation AutoFile::m_obfuscation
protected

Definition at line 374 of file streams.h.

◆ m_position

std::optional<int64_t> AutoFile::m_position
protected

Definition at line 375 of file streams.h.

◆ m_was_written

bool AutoFile::m_was_written {false}
protected

Definition at line 376 of file streams.h.


The documentation for this class was generated from the following files: