catch write errors (eg unsanitzed strings)

This commit is contained in:
Green Sky 2024-04-05 18:18:21 +02:00
parent 7e285290fe
commit 3b010bd16f
No known key found for this signature in database

View File

@ -308,6 +308,7 @@ bool FragmentStore::syncToStorage(FragmentID fid, std::function<write_to_storage
return false;
}
try { // TODO: properly sanitize strings, so this cant throw
// sharing code between binary msgpack and text json for now
nlohmann::json meta_data_j = nlohmann::json::object(); // metadata needs to be an object, null not allowed
// metadata file
@ -380,6 +381,15 @@ bool FragmentStore::syncToStorage(FragmentID fid, std::function<write_to_storage
meta_file_stack.top()->write({reinterpret_cast<const uint8_t*>(meta_file_json_str.data()), meta_file_json_str.size()});
}
} catch (...) {
while (!meta_file_stack.empty()) { meta_file_stack.pop(); } // destroy stack // TODO: maybe work with scope?
std::filesystem::remove(meta_tmp_path);
while (!data_file_stack.empty()) { data_file_stack.pop(); } // destroy stack // TODO: maybe work with scope?
std::filesystem::remove(data_tmp_path);
std::cerr << "FS error: failed to serialize json data\n";
return false;
}
// now data
// for zstd compression, chunk size is frame size. (no cross frame referencing)
static constexpr int64_t chunk_size{1024*1024}; // 1MiB should be enough
@ -460,29 +470,6 @@ bool FragmentStore::loadFromStorage(FragmentID fid, std::function<read_from_stor
data_comp = _reg.get<FragComp::DataCompressionType>(fid).comp;
}
//std::stack<std::unique_ptr<File2I>> data_file_stack;
//data_file_stack.push(std::make_unique<File2RFile>(std::string_view{frag_path}));
//if (!data_file_stack.top()->isGood()) {
//std::cerr << "FS error: fragment data file failed to open '" << frag_path << "'\n";
//return false;
//}
//// TODO: decrypt here
//// add layer based on enum
//if (data_comp == Compression::ZSTD) {
//data_file_stack.push(std::make_unique<File2ZSTDR>(*data_file_stack.top().get()));
//} else {
//// TODO: make error instead
//assert(data_comp == Compression::NONE);
//}
//if (!data_file_stack.top()->isGood()) {
//std::cerr << "FS error: fragment data file failed to add " << (int)data_comp << " decompression layer '" << frag_path << "'\n";
//return false;
//}
auto data_file_stack = buildFileStackRead(std::string_view{frag_path}, Encryption::NONE, data_comp);
if (data_file_stack.empty()) {
return false;