Make sure config files, resume data and state are fsync'd when saved. This should help prevent data

losses on crashes/improper shutdowns.
This commit is contained in:
Andrew Resch 2009-03-08 20:27:58 +00:00
parent fdf78bc7fd
commit c2de5ed93f
4 changed files with 19 additions and 1 deletions

View file

@ -3,6 +3,7 @@
* Fix displaying file errors when the torrent isn't paused
* Fix issue where torrents being check would get removed due to "stop at ratio" rules
* Fix #790 tracker hosts not correct for some .uk trackers
* Make sure config files, resume data and state are fsync'd when saved. This should help prevent data losses on crashes/improper shutdowns.
==== GtkUI ====
* Fix hiding bottom pane when no tabs are enabled upon restart

View file

@ -264,13 +264,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")
pickle.dump(self.__config, open(filename + ".new", "wb"))
f = open(filename + ".new", "w")
pickle.dump(self.__config, f)
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:

View file

@ -762,6 +762,8 @@ class Torrent:
log.debug("Saving fastresume file: %s", path)
fastresume = open(path, "wb")
fastresume.write(resume_data)
fastresume.flush()
os.fsync(fastresume.fileno())
fastresume.close()
except IOError:
log.warning("Error trying to save fastresume file")

View file

@ -540,6 +540,8 @@ class TorrentManager(component.Component):
os.path.join(self.config["state_location"], "torrents.state.new"),
"wb")
cPickle.dump(state, state_file)
state_file.flush()
os.fsync(state_file.fileno())
state_file.close()
except IOError:
log.warning("Unable to save state file.")