diff --git a/src/engine.c b/src/engine.c index 9349aa7e4e9a32147b8a6c9790c77709091791d8..ffae3ef1de11f3714f8ab417b7cca6bb24e7a25e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -4534,6 +4534,9 @@ void engine_dump_restarts(struct engine *e, int drifted_all, int force) { MPI_Bcast(&dump, 1, MPI_INT, 0, MPI_COMM_WORLD); #endif if (dump) { + /* Clean out the previous saved files, if found. Do this now as we are + * MPI synchronized. XXX should we have a barrier XXX */ + restart_remove_previous(e->restart_file); /* Drift all particles first (may have just been done). */ if (!drifted_all) engine_drift_all(e); diff --git a/src/restart.c b/src/restart.c index 769a51c4a16b96f704b2c53bb0fc5c80becbf567..d3ec48724a0e97ea9bd40b243b17419387633559 100644 --- a/src/restart.c +++ b/src/restart.c @@ -273,7 +273,7 @@ void restart_write_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, * @result 1 if the file was found. */ int restart_stop_now(const char *dir, int cleanup) { - static struct stat buf; + struct stat buf; char filename[FNAMELEN]; strcpy(filename, dir); strcat(filename, "/stop"); @@ -296,7 +296,7 @@ int restart_stop_now(const char *dir, int cleanup) { * @param filename the name of the file to check. */ void restart_save_previous(const char *filename) { - static struct stat buf; + struct stat buf; if (stat(filename, &buf) == 0) { char newname[FNAMELEN]; strcpy(newname, filename); @@ -308,3 +308,25 @@ void restart_save_previous(const char *filename) { } } } + +/** + * @brief check if a saved file with the given prefix name exists and remove + * it. Used to remove old restart files before a save sequence + * so that old saved files are not mixed up with new ones. + * + * Does nothing if a saved file does not exist. + * + * @param filename the prefix used when the saved file was created. + */ +void restart_remove_previous(const char *filename) { + struct stat buf; + char newname[FNAMELEN]; + strcpy(newname, filename); + strcat(newname, ".prev"); + if (stat(newname, &buf) == 0) { + if (unlink(newname) != 0) { + /* Worth a complaint, this should not happen. */ + message("Failed to unlink file '%s' (%s)", newname, strerror(errno)); + } + } +} diff --git a/src/restart.h b/src/restart.h index 00c5a6a1ec6c89e1607cef7c925ac7bf81f4fe5c..49d127492255364cbf0f48653c560494e83a2920 100644 --- a/src/restart.h +++ b/src/restart.h @@ -39,5 +39,6 @@ void restart_write_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, int restart_stop_now(const char *dir, int cleanup); void restart_save_previous(const char *filename); +void restart_remove_previous(const char *filename); #endif /* SWIFT_RESTART_H */