From 28da0106e73098dc6116b6fb4740d6f9b3372544 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 9 Sep 2024 21:38:40 +0200 Subject: [PATCH 1/2] check that serial writes succeed, and throw a message if they don't --- src/output/dump.cpp | 36 +++++++++++++++++++++++++++--------- src/output/vtk.cpp | 15 +++++++++++++-- src/output/vtk.hpp | 9 +++++++-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/output/dump.cpp b/src/output/dump.cpp index 5a27ecc2..6b3767e0 100644 --- a/src/output/dump.cpp +++ b/src/output/dump.cpp @@ -248,7 +248,9 @@ void Dump::WriteString(IdfxFileHandler fileHdl, char *str, int size) { } offset=offset+size; #else - fwrite (str, sizeof(char), size, fileHdl); + if(fwrite (str, sizeof(char), size, fileHdl) != size) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } @@ -312,15 +314,23 @@ void Dump::WriteSerial(IdfxFileHandler fileHdl, int ndim, int *dim, #else // Write type of data - fwrite(&type, 1, sizeof(int), fileHdl); + if(fwrite(&type, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } // Write dimensions of array - fwrite(&ndim, 1, sizeof(int), fileHdl); + if(fwrite(&ndim, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } for(int n = 0 ; n < ndim ; n++) { - fwrite(dim+n, 1, sizeof(int), fileHdl); + if(fwrite(dim+n, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } ntot = ntot * dim[n]; } // Write raw data - fwrite(data, ntot, size, fileHdl); + if(fwrite(data, size, ntot, fileHdl) != ntot) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } @@ -384,18 +394,26 @@ void Dump::WriteDistributed(IdfxFileHandler fileHdl, int ndim, int *dim, int *gd #else // Write type of data - fwrite(&type, 1, sizeof(int), fileHdl); + if(fwrite(&type, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } // Write dimensions of array // (in serial, dim and gdim are identical, so no need to differentiate) - fwrite(&ndim, 1, sizeof(int), fileHdl); + if(fwrite(&ndim, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } for(int n = 0 ; n < ndim ; n++) { - fwrite(dim+n, 1, sizeof(int), fileHdl); + if(fwrite(dim+n, sizeof(int), 1, fileHdl) != 1) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } ntot = ntot * dim[n]; } // Write raw data - fwrite(data, ntot, sizeof(real), fileHdl); + if(fwrite(data, sizeof(real), ntot, fileHdl) != ntot) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } diff --git a/src/output/vtk.cpp b/src/output/vtk.cpp index 13b47d30..5dd250b7 100644 --- a/src/output/vtk.cpp +++ b/src/output/vtk.cpp @@ -53,7 +53,9 @@ void Vtk::WriteHeaderNodes(IdfxFileHandler fvtk) { MPI_FLOAT, MPI_STATUS_IGNORE)); this->offset += sizeof(float)*(nx1+ioffset)*(nx2+joffset)*(nx3+koffset)*3; #else - fwrite(node_coord.data(),sizeof(float),size,fvtk); + if(fwrite(node_coord.data(),sizeof(float),size,fvtk) != size) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } @@ -290,6 +292,13 @@ int Vtk::Write() { this->offset = 0; #else fileHdl = fopen(filename.c_str(),"wb"); + + if(fileHdl == NULL) { + std::stringstream msg; + msg << "Unable to open file " << filename << std::endl; + msg << "Check that you have write access and that you don't exceed your quota" << std::endl; + IDEFIX_ERROR(msg); + } #endif WriteHeader(fileHdl, this->data->t); @@ -502,6 +511,8 @@ void Vtk::WriteScalar(IdfxFileHandler fvtk, float* Vin, const std::string &var_ this->offset = this->offset + sizeof(float)*nx1*nx2*nx3; #else - fwrite(Vin,sizeof(float),nx1loc*nx2loc*nx3loc,fvtk); + if(fwrite(Vin,sizeof(float),nx1loc*nx2loc*nx3loc,fvtk) != nx1loc*nx2loc*nx3loc) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } diff --git a/src/output/vtk.hpp b/src/output/vtk.hpp index c33e0d05..9b5d715a 100644 --- a/src/output/vtk.hpp +++ b/src/output/vtk.hpp @@ -75,7 +75,10 @@ class BaseVtk { } offset=offset+strlen(header); #else - fprintf (fvtk, "%s", header); + int rc = fprintf (fvtk, "%s", header); + if(rc<0) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } @@ -90,7 +93,9 @@ class BaseVtk { } offset=offset+nelem*sizeof(T); #else - fwrite(buffer, sizeof(T), nelem, fvtk); + if(fwrite(buffer, sizeof(T), nelem, fvtk) != nelem) { + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + } #endif } }; From df6568fff29c540029cc4d44d11d8df56e1408b0 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 9 Sep 2024 21:46:33 +0200 Subject: [PATCH 2/2] fix typo add successful open check in dump write --- src/output/dump.cpp | 24 +++++++++++++++--------- src/output/vtk.cpp | 6 +++--- src/output/vtk.hpp | 4 ++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/output/dump.cpp b/src/output/dump.cpp index 6b3767e0..73b0df99 100644 --- a/src/output/dump.cpp +++ b/src/output/dump.cpp @@ -249,7 +249,7 @@ void Dump::WriteString(IdfxFileHandler fileHdl, char *str, int size) { offset=offset+size; #else if(fwrite (str, sizeof(char), size, fileHdl) != size) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } @@ -315,21 +315,21 @@ void Dump::WriteSerial(IdfxFileHandler fileHdl, int ndim, int *dim, #else // Write type of data if(fwrite(&type, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } // Write dimensions of array if(fwrite(&ndim, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } for(int n = 0 ; n < ndim ; n++) { if(fwrite(dim+n, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } ntot = ntot * dim[n]; } // Write raw data if(fwrite(data, size, ntot, fileHdl) != ntot) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } @@ -395,24 +395,24 @@ void Dump::WriteDistributed(IdfxFileHandler fileHdl, int ndim, int *dim, int *gd // Write type of data if(fwrite(&type, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } // Write dimensions of array // (in serial, dim and gdim are identical, so no need to differentiate) if(fwrite(&ndim, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } for(int n = 0 ; n < ndim ; n++) { if(fwrite(dim+n, sizeof(int), 1, fileHdl) != 1) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } ntot = ntot * dim[n]; } // Write raw data if(fwrite(data, sizeof(real), ntot, fileHdl) != ntot) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } @@ -854,6 +854,12 @@ int Dump::Write(Output& output) { this->offset = 0; #else fileHdl = fopen(filename.c_str(),"wb"); + if(fileHdl == NULL) { + std::stringstream msg; + msg << "Unable to open file " << filename << std::endl; + msg << "Check that you have write access and that you don't exceed your quota." << std::endl; + IDEFIX_ERROR(msg); + } #endif // File is open // First thing we need are coordinates: init a host mirror and sync it diff --git a/src/output/vtk.cpp b/src/output/vtk.cpp index 5dd250b7..0f52f37c 100644 --- a/src/output/vtk.cpp +++ b/src/output/vtk.cpp @@ -54,7 +54,7 @@ void Vtk::WriteHeaderNodes(IdfxFileHandler fvtk) { this->offset += sizeof(float)*(nx1+ioffset)*(nx2+joffset)*(nx3+koffset)*3; #else if(fwrite(node_coord.data(),sizeof(float),size,fvtk) != size) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } @@ -296,7 +296,7 @@ int Vtk::Write() { if(fileHdl == NULL) { std::stringstream msg; msg << "Unable to open file " << filename << std::endl; - msg << "Check that you have write access and that you don't exceed your quota" << std::endl; + msg << "Check that you have write access and that you don't exceed your quota." << std::endl; IDEFIX_ERROR(msg); } #endif @@ -512,7 +512,7 @@ void Vtk::WriteScalar(IdfxFileHandler fvtk, float* Vin, const std::string &var_ this->offset = this->offset + sizeof(float)*nx1*nx2*nx3; #else if(fwrite(Vin,sizeof(float),nx1loc*nx2loc*nx3loc,fvtk) != nx1loc*nx2loc*nx3loc) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } diff --git a/src/output/vtk.hpp b/src/output/vtk.hpp index 9b5d715a..dcda3618 100644 --- a/src/output/vtk.hpp +++ b/src/output/vtk.hpp @@ -77,7 +77,7 @@ class BaseVtk { #else int rc = fprintf (fvtk, "%s", header); if(rc<0) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif } @@ -94,7 +94,7 @@ class BaseVtk { offset=offset+nelem*sizeof(T); #else if(fwrite(buffer, sizeof(T), nelem, fvtk) != nelem) { - IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota"); + IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif }