[#2775] Update state and fastresume save methods

* Issue introducted in a previous commit meant the state file is never
   saved when starting with a fresh config.
This commit is contained in:
Calum Lind 2015-10-30 14:34:58 +00:00
parent ed48c4a0c5
commit f500d78487

View File

@ -649,33 +649,42 @@ class TorrentManager(component.Component):
def save_state(self): def save_state(self):
"""Save the state of the TorrentManager to the torrents.state file.""" """Save the state of the TorrentManager to the torrents.state file."""
state = self.create_state() state = self.create_state()
if not state.torrents:
log.debug("Skipping saving state with no torrents loaded")
return
filename = "torrents.state" filename = "torrents.state"
filepath = os.path.join(self.state_dir, filename) filepath = os.path.join(self.state_dir, filename)
filepath_bak = filepath + ".bak" filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp" filepath_tmp = filepath + ".tmp"
try: try:
os.remove(filepath_bak) log.debug("Creating the temporary file: %s", filepath_tmp)
except OSError:
pass
try:
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
os.rename(filepath, filepath_bak)
except OSError as ex:
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath_tmp, "wb", 0) as _file: with open(filepath_tmp, "wb", 0) as _file:
# Pickle the TorrentManagerState object
cPickle.dump(state, _file) cPickle.dump(state, _file)
_file.flush() _file.flush()
os.fsync(_file.fileno()) os.fsync(_file.fileno())
os.rename(filepath_tmp, filepath)
except (OSError, cPickle.PicklingError) as ex: except (OSError, cPickle.PicklingError) as ex:
log.error("Unable to save %s: %s", filename, ex) log.error("Unable to save %s: %s", filename, ex)
return
try:
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
if os.path.isfile(filepath_bak): if os.path.isfile(filepath_bak):
log.info("Restoring backup of %s from: %s", filename, filepath_bak) os.remove(filepath_bak)
if os.path.isfile(filepath):
os.rename(filepath, filepath_bak)
except OSError as ex:
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
return
try:
log.debug("Saving %s to: %s", filename, filepath)
os.rename(filepath_tmp, filepath)
except OSError, ex:
log.error("Failed to set new state file %s: %s", filepath, ex)
if os.path.isfile(filepath_bak):
log.info("Restoring backup of state from: %s", filepath_bak)
os.rename(filepath_bak, filepath) os.rename(filepath_bak, filepath)
def save_resume_data(self, torrent_ids=None, flush_disk_cache=False): def save_resume_data(self, torrent_ids=None, flush_disk_cache=False):
@ -765,26 +774,32 @@ class TorrentManager(component.Component):
filepath_tmp = filepath + ".tmp" filepath_tmp = filepath + ".tmp"
try: try:
os.remove(filepath_bak) log.debug("Creating the temporary file: %s", filepath_tmp)
except OSError:
pass
try:
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
os.rename(filepath, filepath_bak)
except OSError as ex:
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath_tmp, "wb", 0) as _file: with open(filepath_tmp, "wb", 0) as _file:
_file.write(lt.bencode(self.resume_data)) _file.write(lt.bencode(self.resume_data))
_file.flush() _file.flush()
os.fsync(_file.fileno()) os.fsync(_file.fileno())
os.rename(filepath_tmp, filepath)
except (OSError, EOFError) as ex: except (OSError, EOFError) as ex:
log.error("Unable to save %s: %s", filename, ex) log.error("Unable to save %s: %s", filename, ex)
return False
try:
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
if os.path.isfile(filepath_bak): if os.path.isfile(filepath_bak):
log.info("Restoring backup of %s from: %s", filename, filepath_bak) os.remove(filepath_bak)
if os.path.isfile(filepath):
os.rename(filepath, filepath_bak)
except OSError as ex:
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
return False
try:
log.debug("Saving %s to: %s", filename, filepath)
os.rename(filepath_tmp, filepath)
except OSError, ex:
log.error("Failed to set new file %s: %s", filepath, ex)
if os.path.isfile(filepath_bak):
log.info("Restoring backup from: %s", filepath_bak)
os.rename(filepath_bak, filepath) os.rename(filepath_bak, filepath)
else: else:
# Sync the rename operations for the directory # Sync the rename operations for the directory