Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::cfile Class Reference

#include <cfile.hpp>

Inheritance diagram for fc::cfile:

Public Member Functions

 cfile ()
 
void set_file_path (fc::path file_path)
 
fc::path get_file_path () const
 
bool is_open () const
 
auto fileno () const
 
void open (const char *mode)
 
size_t tellp () const
 
void seek (long loc)
 
void seek_end (long loc)
 
void skip (long loc)
 
void read (char *d, size_t n)
 
void write (const char *d, size_t n)
 
void flush ()
 
void sync ()
 
void punch_hole (size_t begin, size_t end)
 
size_t filesystem_block_size () const
 
bool eof () const
 
int getc ()
 
void close ()
 
boost::interprocess::mapping_handle_t get_mapping_handle () const
 
cfile_datastream create_datastream ()
 

Static Public Member Functions

static bool supports_hole_punching ()
 

Static Public Attributes

static constexpr const char * create_or_update_rw_mode = "ab+"
 
static constexpr const char * update_rw_mode = "rb+"
 
static constexpr const char * truncate_rw_mode = "wb+"
 

Detailed Description

Wrapper for c-file access that provides a similar interface as fstream without all the overhead of std streams. std::ios_base::failure exception thrown for errors.

Definition at line 31 of file cfile.hpp.

Constructor & Destructor Documentation

◆ cfile()

fc::cfile::cfile ( )
inline

Definition at line 33 of file cfile.hpp.

34 : _file(nullptr, &fclose)
35 {}

Member Function Documentation

◆ close()

void fc::cfile::close ( )
inline

Definition at line 202 of file cfile.hpp.

202 {
203 _file.reset();
204 _open = false;
205 }
Here is the caller graph for this function:

◆ create_datastream()

cfile_datastream fc::cfile::create_datastream ( )
inline

Definition at line 249 of file cfile.hpp.

249 {
250 return cfile_datastream(*this);
251}
Here is the caller graph for this function:

◆ eof()

bool fc::cfile::eof ( ) const
inline

Definition at line 191 of file cfile.hpp.

191{ return feof(_file.get()) != 0; }
Here is the caller graph for this function:

◆ fileno()

auto fc::cfile::fileno ( ) const
inline

Definition at line 47 of file cfile.hpp.

47 {
48 int fd = ::fileno(_file.get());
49 if( -1 == fd ) {
50 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
51 " unable to convert file pointer to file descriptor, error: " +
52 std::to_string( errno ) );
53 }
54 return fd;
55 }
auto fileno() const
Definition cfile.hpp:47
std::string generic_string() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ filesystem_block_size()

size_t fc::cfile::filesystem_block_size ( ) const
inline

Definition at line 189 of file cfile.hpp.

189{ return _file_blk_size; }

◆ flush()

void fc::cfile::flush ( )
inline

Definition at line 135 of file cfile.hpp.

135 {
136 if( 0 != fflush( _file.get() ) ) {
137 int err = ferror( _file.get() );
138 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
139 " unable to flush file, ferror: " + std::to_string( err ) );
140 }
141 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_file_path()

fc::path fc::cfile::get_file_path ( ) const
inline

Definition at line 41 of file cfile.hpp.

41 {
42 return _file_path;
43 }
Here is the caller graph for this function:

◆ get_mapping_handle()

boost::interprocess::mapping_handle_t fc::cfile::get_mapping_handle ( ) const
inline

Definition at line 207 of file cfile.hpp.

207 {
208 return {fileno(), false};
209 }
Here is the call graph for this function:

◆ getc()

int fc::cfile::getc ( )
inline

Definition at line 193 of file cfile.hpp.

193 {
194 int ret = fgetc(_file.get());
195 if (ret == EOF) {
196 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
197 " unable to read 1 byte");
198 }
199 return ret;
200 }
CK_RV ret
Here is the call graph for this function:

◆ is_open()

bool fc::cfile::is_open ( ) const
inline

Definition at line 45 of file cfile.hpp.

45{ return _open; }
Here is the caller graph for this function:

◆ open()

void fc::cfile::open ( const char * mode)
inline
Parameters
modeis any mode supported by fopen Tested with: "ab+" - open for binary update - create if does not exist "rb+" - open for binary update - file must exist

Definition at line 65 of file cfile.hpp.

65 {
66 _file.reset( FC_FOPEN( _file_path.generic_string().c_str(), mode ) );
67 if( !_file ) {
68 throw std::ios_base::failure( "cfile unable to open: " + _file_path.generic_string() + " in mode: " + std::string( mode ) );
69 }
70#ifndef _WIN32
71 struct stat st;
72 _file_blk_size = 4096;
73 if( fstat(fileno(), &st) == 0 )
74 _file_blk_size = st.st_blksize;
75#endif
76 _open = true;
77 }
#define FC_FOPEN(p, m)
Definition block_log.cpp:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ punch_hole()

void fc::cfile::punch_hole ( size_t begin,
size_t end )
inline

Definition at line 159 of file cfile.hpp.

159 {
160 if(begin % _file_blk_size) {
161 begin &= ~(_file_blk_size-1);
162 begin += _file_blk_size;
163 }
164 end &= ~(_file_blk_size-1);
165
166 if(begin >= end)
167 return;
168
169 int ret = 0;
170#if defined(__linux__)
171 ret = fallocate(fileno(), FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, begin, end-begin);
172#elif defined(__APPLE__)
173 struct fpunchhole puncher = {0, 0, static_cast<off_t>(begin), static_cast<off_t>(end-begin)};
174 ret = fcntl(fileno(), F_PUNCHHOLE, &puncher);
175#endif
176 if(ret == -1)
177 wlog("Failed to punch hole in file ${f}: ${e}", ("f", _file_path)("e", strerror(errno)));
178
179 flush();
180 }
void flush()
Definition cfile.hpp:135
#define wlog(FORMAT,...)
Definition logger.hpp:124
Here is the call graph for this function:

◆ read()

void fc::cfile::read ( char * d,
size_t n )
inline

Definition at line 114 of file cfile.hpp.

114 {
115 size_t result = fread( d, 1, n, _file.get() );
116 if( result != n ) {
117 int err = ferror(_file.get());
118 int eof = feof(_file.get());
119 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
120 " unable to read " + std::to_string( n ) + " bytes;"
121 " only read " + std::to_string( result ) +
122 ", eof: " + (eof == 0 ? "false" : "true") +
123 ", ferror: " + std::to_string(err) );
124 }
125 }
bool eof() const
Definition cfile.hpp:191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ seek()

void fc::cfile::seek ( long loc)
inline

Definition at line 87 of file cfile.hpp.

87 {
88 if( 0 != fseek( _file.get(), loc, SEEK_SET ) ) {
89 int err = ferror(_file.get());
90 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
91 " unable to SEEK_SET to: " + std::to_string(loc) +
92 ", ferror: " + std::to_string(err) );
93 }
94 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ seek_end()

void fc::cfile::seek_end ( long loc)
inline

Definition at line 96 of file cfile.hpp.

96 {
97 if( 0 != fseek( _file.get(), loc, SEEK_END ) ) {
98 int err = ferror(_file.get());
99 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
100 " unable to SEEK_END to: " + std::to_string(loc) +
101 ", ferror: " + std::to_string(err) );
102 }
103 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_file_path()

void fc::cfile::set_file_path ( fc::path file_path)
inline

Definition at line 37 of file cfile.hpp.

37 {
38 _file_path = std::move( file_path );
39 }
Here is the caller graph for this function:

◆ skip()

void fc::cfile::skip ( long loc)
inline

Definition at line 105 of file cfile.hpp.

105 {
106 if( 0 != fseek( _file.get(), loc, SEEK_CUR ) ) {
107 int err = ferror(_file.get());
108 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
109 " unable to SEEK_CUR to: " + std::to_string(loc) +
110 ", ferror: " + std::to_string(err) );
111 }
112 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ supports_hole_punching()

static bool fc::cfile::supports_hole_punching ( )
inlinestatic

Definition at line 182 of file cfile.hpp.

182 {
183#if defined(__linux__) || defined(__APPLE__)
184 return true;
185#endif
186 return false;
187 }
Here is the caller graph for this function:

◆ sync()

void fc::cfile::sync ( )
inline

Definition at line 143 of file cfile.hpp.

143 {
144 const int fd = fileno();
145 if( -1 == fsync( fd ) ) {
146 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
147 " unable to sync file, error: " + std::to_string( errno ) );
148 }
149#ifdef __APPLE__
150 if( -1 == fcntl( fd, F_FULLFSYNC ) ) {
151 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
152 " unable to F_FULLFSYNC file, error: " + std::to_string( errno ) );
153 }
154#endif
155 }
Here is the call graph for this function:

◆ tellp()

size_t fc::cfile::tellp ( ) const
inline

Definition at line 79 of file cfile.hpp.

79 {
80 long result = ftell( _file.get() );
81 if (result == -1)
82 throw std::ios_base::failure("cfile: " + get_file_path().generic_string() +
83 " unable to get the current position of the file, error: " + std::to_string( errno ));
84 return static_cast<size_t>(result);
85 }
fc::path get_file_path() const
Definition cfile.hpp:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

void fc::cfile::write ( const char * d,
size_t n )
inline

Definition at line 127 of file cfile.hpp.

127 {
128 size_t result = fwrite( d, 1, n, _file.get() );
129 if( result != n ) {
130 throw std::ios_base::failure( "cfile: " + _file_path.generic_string() +
131 " unable to write " + std::to_string( n ) + " bytes; only wrote " + std::to_string( result ) );
132 }
133 }
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ create_or_update_rw_mode

const char* fc::cfile::create_or_update_rw_mode = "ab+"
staticconstexpr

Definition at line 57 of file cfile.hpp.

◆ truncate_rw_mode

const char* fc::cfile::truncate_rw_mode = "wb+"
staticconstexpr

Definition at line 59 of file cfile.hpp.

◆ update_rw_mode

const char* fc::cfile::update_rw_mode = "rb+"
staticconstexpr

Definition at line 58 of file cfile.hpp.


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