From 9a4bbb771e3638f1c53079e89fcd7e2a6ac6b51a Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 8 Mar 2009 02:33:12 +0000 Subject: [PATCH] Make a backup of config files before overwriting and make sure the file is fsync'd without error --- deluge/config.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/deluge/config.py b/deluge/config.py index 52b22c6a4..510dbce08 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -270,13 +270,26 @@ class Config(object): self.__save_timer = None + # Save the new config and make sure it's written to disk try: log.debug("Saving new config file %s", filename + ".new") - json.dump(self.__config, open(filename + ".new", "w"), indent=2) + f = open(filename + ".new", "w") + json.dump(self.__config, f, indent=2) + f.flush() + os.fsync(f.fileno()) + f.close() except Exception, e: log.error("Error writing new config file: %s", e) return + # Make a backup of the old config + try: + log.debug("Backing up old config file to %s~", filename) + shutil.move(filename, filename + "~") + except Exception, e: + log.error("Error backing up old config..") + return + # The new config file has been written successfully, so let's move it over # the existing one. try: