From b730f43bdd62ed54308f62c7a32d98862fa427a2 Mon Sep 17 00:00:00 2001 From: akva Date: Mon, 26 Sep 2011 08:58:02 +0000 Subject: [PATCH] added: sanity freespace limit in the HDF5 writer to avoid corrupting files git-svn-id: http://svn.sintef.no/trondheim/IFEM/trunk@1212 e10b68d5-8a6e-419e-a041-bce267b0401d --- src/Utility/HDF5Writer.C | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Utility/HDF5Writer.C b/src/Utility/HDF5Writer.C index 58731b05..b0d17b00 100644 --- a/src/Utility/HDF5Writer.C +++ b/src/Utility/HDF5Writer.C @@ -18,6 +18,8 @@ #include #include #include +#include +#include #ifdef HAS_HDF5 #include @@ -27,6 +29,8 @@ #include #endif +#define HDF5_SANITY_LIMIT 10*1024*1024LL // 10MB + HDF5Writer::HDF5Writer (const std::string& name, bool append, bool keepOpen) : DataWriter(name+".hdf5"), m_file(0), m_keepOpen(keepOpen) @@ -87,8 +91,21 @@ void HDF5Writer::openFile(int level) if (m_flag == H5F_ACC_TRUNC) m_file = H5Fcreate(m_name.c_str(),m_flag,H5P_DEFAULT,acc_tpl); - else + else { + // check free disk space - to protect against corrupting files + // due to out of space condition + if (m_flag == H5F_ACC_RDWR) { + char* cwd = get_current_dir_name(); + struct statvfs vfs; + statvfs(cwd,&vfs); + if (((int64_t)vfs.f_bavail)*vfs.f_bsize < HDF5_SANITY_LIMIT) { + std::cerr << "HDF5Writer: Low disk space detected, bailing" << std::endl; + exit(1); + } + free(cwd); + } m_file = H5Fopen(m_name.c_str(),m_flag,acc_tpl); + } std::stringstream str; str << '/' << level;