Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::persistence_util Namespace Reference

Functions

cfile open_cfile_for_read (const fc::path &dir, const std::string &filename)
 
uint32_t read_persistence_header (cfile &dat_content, const uint32_t magic_number, const uint32_t min_supported_version, const uint32_t max_supported_version)
 
cfile open_cfile_for_write (const fc::path &dir, const std::string &filename)
 
void write_persistence_header (cfile &dat_content, const uint32_t magic_number, const uint32_t current_version)
 

Detailed Description

This namespace provides functions related to reading and writing a persistence file with a header delineating the type of file and version.

Function Documentation

◆ open_cfile_for_read()

cfile fc::persistence_util::open_cfile_for_read ( const fc::path & dir,
const std::string & filename )

Definition at line 14 of file persistence_util.hpp.

14 {
15 if (!fc::is_directory(dir))
17
18 auto dat_file = dir / filename;
19
20 cfile dat_content;
21 dat_content.set_file_path(dat_file);
22 if( fc::exists( dat_file ) ) {
23 dat_content.open(cfile::create_or_update_rw_mode);
24 }
25 return dat_content;
26 }
void open(const char *mode)
Definition cfile.hpp:65
void set_file_path(fc::path file_path)
Definition cfile.hpp:37
bool exists(const path &p)
void create_directories(const path &p)
bool is_directory(const path &p)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_cfile_for_write()

cfile fc::persistence_util::open_cfile_for_write ( const fc::path & dir,
const std::string & filename )

Definition at line 58 of file persistence_util.hpp.

58 {
59 if (!fc::is_directory(dir))
61
62 auto dat_file = dir / filename;
63 cfile dat_content;
64 dat_content.set_file_path(dat_file.generic_string().c_str());
65 dat_content.open( cfile::truncate_rw_mode );
66 return dat_content;
67 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_persistence_header()

uint32_t fc::persistence_util::read_persistence_header ( cfile & dat_content,
const uint32_t magic_number,
const uint32_t min_supported_version,
const uint32_t max_supported_version )

Definition at line 28 of file persistence_util.hpp.

29 {
30 dat_content.seek(0); // needed on mac
31 auto ds = dat_content.create_datastream();
32
33 // validate totem
34 uint32_t totem = 0;
35 fc::raw::unpack( ds, totem );
36 if( totem != magic_number) {
37 FC_THROW_EXCEPTION(fc::parse_error_exception,
38 "File has unexpected magic number: ${actual_totem}. Expected ${expected_totem}",
39 ("actual_totem", totem)
40 ("expected_totem", magic_number));
41 }
42
43 // validate version
44 uint32_t version = 0;
45 fc::raw::unpack( ds, version );
46 if( version < min_supported_version || version > max_supported_version) {
47 FC_THROW_EXCEPTION(fc::parse_error_exception,
48 "Unsupported version for file. "
49 "Version is ${version} while code supports version(s) [${min},${max}]",
50 ("version", version)
51 ("min", min_supported_version)
52 ("max", max_supported_version));
53 }
54
55 return version;
56 }
void seek(long loc)
Definition cfile.hpp:87
cfile_datastream create_datastream()
Definition cfile.hpp:249
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
unsigned int uint32_t
Definition stdint.h:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_persistence_header()

void fc::persistence_util::write_persistence_header ( cfile & dat_content,
const uint32_t magic_number,
const uint32_t current_version )

Definition at line 69 of file persistence_util.hpp.

69 {
70 dat_content.write( reinterpret_cast<const char*>(&magic_number), sizeof(magic_number) );
71 dat_content.write( reinterpret_cast<const char*>(&current_version), sizeof(current_version) );
72 }
void write(const char *d, size_t n)
Definition cfile.hpp:127
Here is the call graph for this function:
Here is the caller graph for this function: