Throw on failure in gmx_file_rename
authorJoe Jordan <ejjordan12@gmail.com>
Wed, 27 Oct 2021 08:44:31 +0000 (08:44 +0000)
committerMark Abraham <mark.j.abraham@gmail.com>
Wed, 27 Oct 2021 08:44:31 +0000 (08:44 +0000)
src/gromacs/mdlib/mdoutf.cpp
src/gromacs/utility/futil.cpp
src/gromacs/utility/futil.h

index ecf83d907e7e1780f1b009454f4bafd1cb06aa47..5384aeaa83f58cfe92668d130890149c7ceb7d3e 100644 (file)
@@ -464,9 +464,15 @@ static void write_checkpoint(const char*                     fn,
         /* Rename the checkpoint file from the temporary to the final name */
         mpiBarrierBeforeRename(applyMpiBarrierBeforeRename, mpiBarrierCommunicator);
 
-        if (gmx_file_rename(fntemp, fn) != 0)
+        try
         {
-            gmx_file("Cannot rename checkpoint file; maybe you are out of disk space?");
+            gmx_file_rename(fntemp, fn);
+        }
+        catch (gmx::FileIOError const&)
+        {
+            // In this case we can be more helpful than the generic message from gmx_file_rename
+            GMX_THROW(gmx::FileIOError(
+                    "Cannot rename checkpoint file; maybe you are out of disk space?"));
         }
     }
 #endif /* GMX_NO_RENAME */
index d1ae16a6962434d8600b352ae284ac9a2e189897..34467e1f1d120f3adfefee22f4a11fb9c06cc4ea 100644 (file)
@@ -592,11 +592,12 @@ FILE* gmx_fopen_temporary(char* buf)
     return fpout;
 }
 
-int gmx_file_rename(const char* oldname, const char* newname)
+void gmx_file_rename(const char* oldname, const char* newname)
 {
+    int code;
 #if !GMX_NATIVE_WINDOWS
     /* under unix, rename() is atomic (at least, it should be). */
-    return rename(oldname, newname);
+    code = rename(oldname, newname);
 #else
     if (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
     {
@@ -604,13 +605,18 @@ int gmx_file_rename(const char* oldname, const char* newname)
         /* This just lets the F@H checksumming system know about the rename */
         fcRename(oldname, newname);
 #    endif
-        return 0;
+        code = 0;
     }
     else
     {
-        return 1;
+        code = 1;
     }
 #endif
+    if (code != 0)
+    {
+        auto errorMsg = gmx::formatString("Failed to rename %s to %s.", oldname, newname);
+        GMX_THROW(gmx::FileIOError(errorMsg));
+    }
 }
 
 int gmx_file_copy(const char* oldname, const char* newname, gmx_bool copy_if_empty)
index ded880b5f6ce12ecb460f1e423c55bd12b32aeae..fb57965007baa3c6cf637aa99f2f47998b342963 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2018,2019,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -189,7 +189,7 @@ void gmx_tmpnam(char* buf);
  *
  * Renames/moves a file atomically, if the OS makes that available.
  */
-int gmx_file_rename(const char* oldname, const char* newname);
+void gmx_file_rename(const char* oldname, const char* newname);
 
 /*! \brief
  * Copies a file (data only) oldname to newname.