diff --git a/src/restart.c b/src/restart.c index a507555fe62277f2bec117299f9bc12d87985153..0d248adda30bf2e4cd9eedb5f9159b1b1e5137ed 100644 --- a/src/restart.c +++ b/src/restart.c @@ -128,6 +128,9 @@ void restart_locate_free(int nfiles, char **files) { */ void restart_write(struct engine *e, const char *filename) { + /* Backup the existing restart file. XXX configure this. */ + restart_save_previous(filename); + FILE *stream = fopen(filename, "w"); if (stream == NULL) error("Failed to open restart file: %s (%s)", filename, strerror(errno)); @@ -283,3 +286,25 @@ int restart_stop_now(const char *dir, int cleanup) { } return 0; } + +/** + * @brief check if a file with the given name exists and rename to + * <filename>.prev. Used to move old restart files before overwriting. + * + * Does nothing if the file does not exist. + * + * @param filename the name of the file to check. + */ +void restart_save_previous(const char *filename) { + static struct stat buf; + if (stat(filename, &buf) == 0) { + char newname[FNAMELEN]; + strcpy(newname, filename); + strcat(newname, ".prev"); + if (rename(filename, newname) != 0) { + /* Worth a complaint, this should not happen. */ + message("Failed to rename file '%s' to '%s' (%s)", filename, newname, + strerror(errno)); + } + } +} diff --git a/src/restart.h b/src/restart.h index 80e6c6755c09be4d3bda84860774b7da9e1a07cd..00c5a6a1ec6c89e1607cef7c925ac7bf81f4fe5c 100644 --- a/src/restart.h +++ b/src/restart.h @@ -33,10 +33,11 @@ int restart_genname(const char *dir, const char *basename, int nodeID, void restart_read_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, char *label, const char *errstr); - void restart_write_blocks(void *ptr, size_t size, size_t nblocks, FILE *stream, const char *label, const char *errstr); int restart_stop_now(const char *dir, int cleanup); +void restart_save_previous(const char *filename); + #endif /* SWIFT_RESTART_H */