Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
persistence_util.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/io/cfile.hpp>
3#include <fc/io/raw.hpp>
4
5namespace fc {
6
12namespace persistence_util {
13
14 cfile open_cfile_for_read(const fc::path& dir, const std::string& filename) {
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 ) ) {
24 }
25 return dat_content;
26 }
27
28 uint32_t read_persistence_header(cfile& dat_content, const uint32_t magic_number, const uint32_t min_supported_version,
29 const uint32_t max_supported_version) {
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 }
57
58 cfile open_cfile_for_write(const fc::path& dir, const std::string& filename) {
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 }
68
69 void write_persistence_header(cfile& dat_content, const uint32_t magic_number, const uint32_t current_version) {
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 }
73} // namespace persistence_util
74
75} // namespace fc
static constexpr const char * truncate_rw_mode
Definition cfile.hpp:59
void seek(long loc)
Definition cfile.hpp:87
static constexpr const char * create_or_update_rw_mode
Definition cfile.hpp:57
cfile_datastream create_datastream()
Definition cfile.hpp:249
void open(const char *mode)
Definition cfile.hpp:65
void set_file_path(fc::path file_path)
Definition cfile.hpp:37
void write(const char *d, size_t n)
Definition cfile.hpp:127
wraps boost::filesystem::path to provide platform independent path manipulation.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
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)
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_read(const fc::path &dir, const std::string &filename)
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
namespace sysio::chain
Definition authority.cpp:3
bool exists(const path &p)
void create_directories(const path &p)
bool is_directory(const path &p)
unsigned int uint32_t
Definition stdint.h:126