remove(newest_file);
}
catch (const std::runtime_error& e) {
- g_log<<Logger::Warning<<"NODDB init: Cannot parse file: " << filename << endl;
+ infile.close();
+ remove(newest_file);
+ g_log<<Logger::Warning<<"NODDB init: Cannot parse file: " << filename << ": " << e.what() << "; removed" << endl;
}
}
}
std::stringstream ss;
ss << d_prefix << "_" << tid;
f /= ss.str() + "_" + std::to_string(getpid()) + "." + bf_suffix;
+ path ftmp = f;
+ ftmp += ".tmp";
if (exists(p) && is_directory(p)) {
try {
std::ofstream ofile;
std::stringstream iss;
- ofile.open(f.string(), std::ios::out | std::ios::binary);
+ ofile.open(ftmp.string(), std::ios::out | std::ios::binary);
{
// only lock while dumping to a stringstream
std::lock_guard<std::mutex> lock(d_sbf_mutex);
// Now write it out to the file
ofile << iss.str();
- if (ofile.fail())
- throw std::runtime_error("Failed to write to file:" + f.string());
+ if (ofile.fail()) {
+ ofile.close();
+ remove(ftmp);
+ throw std::runtime_error("Failed to write to file:" + ftmp.string());
+ }
+ ofile.close();
+ rename(ftmp, f);
return true;
}
catch (const std::runtime_error& e) {
uint8_t k, p;
uint32_t num_cells, bitstr_len;
is.read((char*)&k, sizeof(k));
+ if (is.fail()) {
+ throw std::runtime_error("SBF: read failed (file too short?)");
+ }
is.read((char*)&num_cells, sizeof(num_cells));
+ if (is.fail()) {
+ throw std::runtime_error("SBF: read failed (file too short?)");
+ }
num_cells = ntohl(num_cells);
is.read((char*)&p, sizeof(p));
+ if (is.fail()) {
+ throw std::runtime_error("SBF: read failed (file too short?)");
+ }
is.read((char*)&bitstr_len, sizeof(bitstr_len));
+ if (is.fail()) {
+ throw std::runtime_error("SBF: read failed (file too short?)");
+ }
bitstr_len = ntohl(bitstr_len);
char* bitcstr = new char[bitstr_len];
is.read((char*)bitcstr, bitstr_len);
+ if (is.fail()) {
+ throw std::runtime_error("SBF: read failed (file too short?)");
+ }
std::string bitstr(bitcstr, bitstr_len);
delete[] bitcstr;
stableBF tempbf(k, num_cells, p, bitstr);