Make a backup of config files before overwriting and make sure the file is fsync'd without error

This commit is contained in:
Andrew Resch 2009-03-08 02:33:12 +00:00
parent e12faf5641
commit 9a4bbb771e
1 changed files with 14 additions and 1 deletions

View File

@ -270,13 +270,26 @@ class Config(object):
self.__save_timer = None self.__save_timer = None
# Save the new config and make sure it's written to disk
try: try:
log.debug("Saving new config file %s", filename + ".new") 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: except Exception, e:
log.error("Error writing new config file: %s", e) log.error("Error writing new config file: %s", e)
return 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 new config file has been written successfully, so let's move it over
# the existing one. # the existing one.
try: try: