Skip to content
Snippets Groups Projects
Commit 1dc820a7 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Unlink all the saved files before creating new ones. Avoids issues if the...

Unlink all the saved files before creating new ones. Avoids issues if the saves fail and we could potentially have three restarts present in various forms.
parent fa9e0e3a
No related branches found
No related tags found
1 merge request!501Backup of restart files
...@@ -4534,6 +4534,9 @@ void engine_dump_restarts(struct engine *e, int drifted_all, int force) { ...@@ -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); MPI_Bcast(&dump, 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif #endif
if (dump) { 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). */ /* Drift all particles first (may have just been done). */
if (!drifted_all) engine_drift_all(e); if (!drifted_all) engine_drift_all(e);
......
...@@ -273,7 +273,7 @@ void restart_write_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, ...@@ -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. * @result 1 if the file was found.
*/ */
int restart_stop_now(const char *dir, int cleanup) { int restart_stop_now(const char *dir, int cleanup) {
static struct stat buf; struct stat buf;
char filename[FNAMELEN]; char filename[FNAMELEN];
strcpy(filename, dir); strcpy(filename, dir);
strcat(filename, "/stop"); strcat(filename, "/stop");
...@@ -296,7 +296,7 @@ int restart_stop_now(const char *dir, int cleanup) { ...@@ -296,7 +296,7 @@ int restart_stop_now(const char *dir, int cleanup) {
* @param filename the name of the file to check. * @param filename the name of the file to check.
*/ */
void restart_save_previous(const char *filename) { void restart_save_previous(const char *filename) {
static struct stat buf; struct stat buf;
if (stat(filename, &buf) == 0) { if (stat(filename, &buf) == 0) {
char newname[FNAMELEN]; char newname[FNAMELEN];
strcpy(newname, filename); strcpy(newname, filename);
...@@ -308,3 +308,25 @@ void restart_save_previous(const char *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));
}
}
}
...@@ -39,5 +39,6 @@ void restart_write_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, ...@@ -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); int restart_stop_now(const char *dir, int cleanup);
void restart_save_previous(const char *filename); void restart_save_previous(const char *filename);
void restart_remove_previous(const char *filename);
#endif /* SWIFT_RESTART_H */ #endif /* SWIFT_RESTART_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment