1016 {
1018 _file.reset(
FC_FOPEN(_block_file_name.c_str(),
"r"));
1019 SYS_ASSERT( _file, block_log_exception,
"Could not open Block log file at '${blocks_log}'", (
"blocks_log", _block_file_name) );
1020 _end_of_buffer_position = _unset_position;
1021
1022
1023 _version = 0;
1024 auto size = fread((char*)&_version, sizeof(_version), 1, _file.get());
1025 SYS_ASSERT( size == 1, block_log_exception,
"Block log file at '${blocks_log}' could not be read.", (
"file", _block_file_name) );
1026 const bool is_prune_log = is_pruned_log_and_mask_version(_version);
1028 "block log version ${v} is not supported", ("v", _version));
1029 if (_version == 1) {
1030 _first_block_num = 1;
1031 }
1032 else {
1033 size = fread((char*)&_first_block_num, sizeof(_first_block_num), 1, _file.get());
1034 SYS_ASSERT( size == 1, block_log_exception,
"Block log file at '${blocks_log}' not formatted consistently with version ${v}.", (
"file", _block_file_name)(
"v", _version) );
1035 }
1036
1037 auto status = fseek(_file.get(), 0, SEEK_END);
1038 SYS_ASSERT( status == 0, block_log_exception,
"Could not open Block log file at '${blocks_log}'. Returned status: ${status}", (
"blocks_log", _block_file_name)(
"status", status) );
1039
1040 auto eof_position_in_file = ftell(_file.get());
1041 SYS_ASSERT( eof_position_in_file > 0, block_log_exception,
"Block log file at '${blocks_log}' could not be read.", (
"blocks_log", _block_file_name) );
1042
1043 if(is_prune_log) {
1044 fseek(_file.get(), -
sizeof(
uint32_t), SEEK_CUR);
1046 size = fread((char*)&prune_count, sizeof(prune_count), 1, _file.get());
1047 SYS_ASSERT( size == 1, block_log_exception,
"Block log file at '${blocks_log}' not formatted consistently with pruned version ${v}.", (
"file", _block_file_name)(
"v", _version) );
1048 _prune_block_limit = prune_count;
1049 eof_position_in_file -= sizeof(prune_count);
1050 }
1051
1052 _current_position_in_file = eof_position_in_file - _position_size;
1053
1054 update_buffer();
1055
1056 _blocks_found = 0;
1057 char*
buf = _buffer_ptr.get();
1058 const uint32_t index_of_pos = _current_position_in_file - _start_of_buffer_position;
1060
1062 return 0;
1063 }
1064
1066 if (block_pos >= _start_of_buffer_position) {
1067 const uint32_t index_of_block = block_pos - _start_of_buffer_position;
1069 }
1070 else {
1072 auto status = fseek(_file.get(), blknum_offset_pos, SEEK_SET);
1073 SYS_ASSERT( status == 0, block_log_exception,
"Could not seek in '${blocks_log}' to position: ${pos}. Returned status: ${status}", (
"blocks_log", _block_file_name)(
"pos", blknum_offset_pos)(
"status", status) );
1074 auto size = fread((void*)&bnum, sizeof(bnum), 1, _file.get());
1075 SYS_ASSERT( size == 1, block_log_exception,
"Could not read in '${blocks_log}' at position: ${pos}", (
"blocks_log", _block_file_name)(
"pos", blknum_offset_pos) );
1076 }
1078 _blocks_expected = _last_block_num - _first_block_num + 1;
1079 return _blocks_expected;
1080 }
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
std::string generic_string() const
static const uint64_t npos
static bool is_supported_version(uint32_t version)
uint32_t endian_reverse_u32(uint32_t x)
unsigned __int64 uint64_t
static constexpr int blknum_offset