diff --git a/opm/io/eclipse/ExtSmryOutput.hpp b/opm/io/eclipse/ExtSmryOutput.hpp index e9db27f6c..a5341dd02 100644 --- a/opm/io/eclipse/ExtSmryOutput.hpp +++ b/opm/io/eclipse/ExtSmryOutput.hpp @@ -63,6 +63,7 @@ private: std::array ijk_from_global_index(const GridDims& dims, int globInd) const; std::vector make_modified_keys(const std::vector& valueKeys, const GridDims& dims); + bool rename_tmpfile(const std::string& tmp_fname); }; diff --git a/src/opm/io/eclipse/ExtSmryOutput.cpp b/src/opm/io/eclipse/ExtSmryOutput.cpp index 3d8119688..bf1e6d1cc 100644 --- a/src/opm/io/eclipse/ExtSmryOutput.cpp +++ b/src/opm/io/eclipse/ExtSmryOutput.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -29,7 +30,6 @@ #include #include - namespace Opm { namespace EclIO { @@ -99,8 +99,12 @@ void ExtSmryOutput::write(const std::vector& ts_data, int report_step, bo { const auto tp = std::chrono::system_clock::now(); auto sec_since_epoch = std::chrono::duration_cast(tp.time_since_epoch()).count(); - std::string tmp_file_name = "TMP_" + std::to_string(sec_since_epoch) + ".ESMRY"; + std::filesystem::path esmry_file(m_outputFileName); + std::filesystem::path rootName = esmry_file.parent_path() / esmry_file.stem(); + + std::string tmp_file_name = rootName.string() + "_TMP_" + std::to_string(sec_since_epoch) + ".ESMRY"; + { Opm::EclIO::EclOutput outFile(tmp_file_name, m_fmt, std::ios::out); @@ -123,16 +127,31 @@ void ExtSmryOutput::write(const std::vector& ts_data, int report_step, bo } } - const std::filesystem::path from_file = tmp_file_name; - const std::filesystem::path to_file = m_outputFileName; - std::filesystem::rename(from_file, to_file); - - m_last_write = std::chrono::system_clock::now(); + if (rename_tmpfile(tmp_file_name)){ + m_last_write = std::chrono::system_clock::now(); + } else { + Opm::OpmLog::warning("Not able to rename temporary ESMRY file " + tmp_file_name); + std::filesystem::path tmp_file(tmp_file_name); + std::filesystem::remove(tmp_file); + } } m_nTimeSteps++; } +bool ExtSmryOutput::rename_tmpfile(const std::string& tmp_fname) +{ + try { + std::filesystem::path from_file(tmp_fname); + std::filesystem::path to_file(m_outputFileName); + std::filesystem::rename(from_file, to_file); + } catch (...){ + return false; + } + + return true; +} + std::vector ExtSmryOutput::make_modified_keys(const std::vector& valueKeys, const GridDims& dims) {