More reliable file load/save

pull/206/head
SChernykh 2022-09-13 18:26:01 +02:00
parent f044149004
commit ed78e9df6e
2 changed files with 10 additions and 6 deletions

View File

@ -477,6 +477,7 @@ void P2PServer::save_peer_list()
} }
} }
f.flush();
f.close(); f.close();
LOGINFO(5, "peer list saved (" << peer_list.size() << " peers)"); LOGINFO(5, "peer list saved (" << peer_list.size() << " peers)");
@ -557,7 +558,7 @@ void P2PServer::load_peer_list()
std::ifstream f(saved_peer_list_file_name); std::ifstream f(saved_peer_list_file_name);
if (f.is_open()) { if (f.is_open()) {
std::string address; std::string address;
while (!f.eof()) { while (f.good()) {
std::getline(f, address); std::getline(f, address);
if (!address.empty()) { if (!address.empty()) {
if (!saved_list.empty()) { if (!saved_list.empty()) {

View File

@ -826,25 +826,26 @@ void p2pool::load_found_blocks()
return; return;
} }
while (!f.eof()) { while (f.good()) {
time_t timestamp; time_t timestamp;
f >> timestamp; f >> timestamp;
if (f.eof()) break; if (!f.good()) break;
uint64_t height; uint64_t height;
f >> height; f >> height;
if (f.eof()) break; if (!f.good()) break;
hash id; hash id;
f >> id; f >> id;
if (f.eof()) break; if (!f.good()) break;
difficulty_type block_difficulty; difficulty_type block_difficulty;
f >> block_difficulty; f >> block_difficulty;
if (f.eof()) break; if (!f.good()) break;
difficulty_type cumulative_difficulty; difficulty_type cumulative_difficulty;
f >> cumulative_difficulty; f >> cumulative_difficulty;
if (!f.good() && !f.eof()) break;
m_foundBlocks.emplace_back(timestamp, height, id, block_difficulty, cumulative_difficulty); m_foundBlocks.emplace_back(timestamp, height, id, block_difficulty, cumulative_difficulty);
} }
@ -1333,6 +1334,8 @@ void p2pool::api_update_block_found(const ChainMain* data)
std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app); std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app);
if (f.is_open()) { if (f.is_open()) {
f << cur_time << ' ' << data->height << ' ' << data->id << ' ' << diff << ' ' << total_hashes << '\n'; f << cur_time << ' ' << data->height << ' ' << data->id << ' ' << diff << ' ' << total_hashes << '\n';
f.flush();
f.close();
} }
} }