From a2e02d12cf2e9dcbc2c35fd6c1142f05412b53ac Mon Sep 17 00:00:00 2001 From: Joe Jordan Date: Wed, 27 Oct 2021 08:44:31 +0000 Subject: [PATCH] Throw on failure in gmx_file_rename --- src/gromacs/mdlib/mdoutf.cpp | 10 ++++++++-- src/gromacs/utility/futil.cpp | 14 ++++++++++---- src/gromacs/utility/futil.h | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/gromacs/mdlib/mdoutf.cpp b/src/gromacs/mdlib/mdoutf.cpp index ecf83d907e..5384aeaa83 100644 --- a/src/gromacs/mdlib/mdoutf.cpp +++ b/src/gromacs/mdlib/mdoutf.cpp @@ -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 */ diff --git a/src/gromacs/utility/futil.cpp b/src/gromacs/utility/futil.cpp index d1ae16a696..34467e1f1d 100644 --- a/src/gromacs/utility/futil.cpp +++ b/src/gromacs/utility/futil.cpp @@ -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) diff --git a/src/gromacs/utility/futil.h b/src/gromacs/utility/futil.h index ded880b5f6..fb57965007 100644 --- a/src/gromacs/utility/futil.h +++ b/src/gromacs/utility/futil.h @@ -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. -- 2.22.0