diff --git a/writer/HDF5DataFile.cpp b/writer/HDF5DataFile.cpp index 3117565c..49626a9f 100644 --- a/writer/HDF5DataFile.cpp +++ b/writer/HDF5DataFile.cpp @@ -78,11 +78,22 @@ std::optional HDF5DataFile::Close() { data_set->Close(); data_set.reset(); } - data_file->Close(); - data_file.reset(); - if (manage_file && (!std::filesystem::exists(filename.c_str()) || overwrite)) - std::rename(tmp_filename.c_str(), filename.c_str()); + if (manage_file ) { + data_file->Close(); + data_file.reset(); + + if (std::filesystem::exists(filename) && !overwrite) + throw JFJochException(JFJochExceptionCategory::FileWriteError, "File already exists"); + std::error_code ec; + std::filesystem::rename(tmp_filename, filename, ec); + if (ec) + throw JFJochException(JFJochExceptionCategory::FileWriteError, + "Cannot rename temporary HDF5 file " + tmp_filename + + " to " + filename + ": " + ec.message()); + } else { + data_file.reset(); + } closed = true; @@ -92,16 +103,19 @@ std::optional HDF5DataFile::Close() { ret.filename = filename; ret.file_number = file_number + 1; - return ret; } HDF5DataFile::~HDF5DataFile() { if (data_file) { try { - data_set->Close(); - data_file->Close(); - std::filesystem::remove(tmp_filename); + data_set.reset(); + data_set_image_number.reset(); + data_file.reset(); + if (manage_file) { + std::error_code ec; + std::filesystem::remove(tmp_filename, ec); + } } catch (const std::exception &e) { std::cerr << "HDF5DataFile::~HDF5DataFile: " << e.what() << std::endl; } catch (...) { diff --git a/writer/HDF5NXmx.cpp b/writer/HDF5NXmx.cpp index 605ffdae..64efcb90 100644 --- a/writer/HDF5NXmx.cpp +++ b/writer/HDF5NXmx.cpp @@ -52,8 +52,13 @@ NXmx::NXmx(const StartMessage &start) } NXmx::~NXmx() { - if (!std::filesystem::exists(filename.c_str()) || overwrite) - std::rename(tmp_filename.c_str(), filename.c_str()); + try { + if (hdf5_file) { + hdf5_file.reset(); + std::error_code ec; + std::filesystem::remove(tmp_filename, ec); + } + } catch (...) {} } @@ -643,6 +648,9 @@ void NXmx::Attenuator(const StartMessage &start) { } void NXmx::WriteCalibration(const CompressedImage &image) { + if (!hdf5_file) + throw JFJochException(JFJochExceptionCategory::FileWriteError, "HDF5 file already closed"); + if (!calibration_group_created) { calibration_group_created = true; HDF5Group(*hdf5_file, "/entry/instrument/detector/calibration").NXClass("NXcollection"); @@ -668,6 +676,8 @@ void NXmx::SaveCBORImage(const std::string &hdf5_path, const CompressedImage &im dataset->Write(data_type, image.GetCompressed()); else dataset->WriteDirectChunk(image.GetCompressed(), image.GetCompressedSize(), {0, 0}); + + dataset->Close(); } void NXmx::AzimuthalIntegration(const StartMessage &start, const EndMessage &end) { @@ -702,6 +712,8 @@ void NXmx::ADUHistogram(const EndMessage &end) { } void NXmx::Finalize(const EndMessage &end) { + if (!hdf5_file) + throw JFJochException(JFJochExceptionCategory::FileWriteError, "HDF5 file already closed"); if (end.end_date) { hdf5_file->Attr("file_time", end.end_date.value()); hdf5_file->SaveScalar("/entry/end_time", end.end_date.value()); @@ -746,6 +758,19 @@ void NXmx::Finalize(const EndMessage &end) { if (!end.scale_factor.empty()) SaveVector(*hdf5_file, "/entry/MX/imageScaleFactor", end.scale_factor); + + hdf5_file->Close(); + hdf5_file.reset(); + + if (std::filesystem::exists(filename) && !overwrite) + throw JFJochException(JFJochExceptionCategory::FileWriteError, "File already exists"); + + std::error_code ec; + std::filesystem::rename(tmp_filename, filename, ec); + if (ec) + throw JFJochException(JFJochExceptionCategory::FileWriteError, + "Cannot rename temporary HDF5 master file " + tmp_filename + + " to " + filename + ": " + ec.message()); } void NXmx::UserData(const StartMessage &start) {