mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-13 04:54:23 +00:00
[#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:
parent
ed48c4a0c5
commit
f500d78487
@ -649,34 +649,43 @@ 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:
|
with open(filepath_tmp, "wb", 0) as _file:
|
||||||
pass
|
cPickle.dump(state, _file)
|
||||||
|
_file.flush()
|
||||||
|
os.fsync(_file.fileno())
|
||||||
|
except (OSError, cPickle.PicklingError) as ex:
|
||||||
|
log.error("Unable to save %s: %s", filename, ex)
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
||||||
os.rename(filepath, filepath_bak)
|
if os.path.isfile(filepath_bak):
|
||||||
|
os.remove(filepath_bak)
|
||||||
|
if os.path.isfile(filepath):
|
||||||
|
os.rename(filepath, filepath_bak)
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
||||||
else:
|
return
|
||||||
log.info("Saving the %s at: %s", filename, filepath)
|
|
||||||
try:
|
try:
|
||||||
with open(filepath_tmp, "wb", 0) as _file:
|
log.debug("Saving %s to: %s", filename, filepath)
|
||||||
# Pickle the TorrentManagerState object
|
os.rename(filepath_tmp, filepath)
|
||||||
cPickle.dump(state, _file)
|
except OSError, ex:
|
||||||
_file.flush()
|
log.error("Failed to set new state file %s: %s", filepath, ex)
|
||||||
os.fsync(_file.fileno())
|
if os.path.isfile(filepath_bak):
|
||||||
os.rename(filepath_tmp, filepath)
|
log.info("Restoring backup of state from: %s", filepath_bak)
|
||||||
except (OSError, cPickle.PicklingError) as ex:
|
os.rename(filepath_bak, filepath)
|
||||||
log.error("Unable to save %s: %s", filename, ex)
|
|
||||||
if os.path.isfile(filepath_bak):
|
|
||||||
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
|
||||||
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):
|
||||||
"""Saves torrents resume data.
|
"""Saves torrents resume data.
|
||||||
@ -765,34 +774,40 @@ 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:
|
with open(filepath_tmp, "wb", 0) as _file:
|
||||||
pass
|
_file.write(lt.bencode(self.resume_data))
|
||||||
|
_file.flush()
|
||||||
|
os.fsync(_file.fileno())
|
||||||
|
except (OSError, EOFError) as ex:
|
||||||
|
log.error("Unable to save %s: %s", filename, ex)
|
||||||
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
||||||
os.rename(filepath, filepath_bak)
|
if os.path.isfile(filepath_bak):
|
||||||
|
os.remove(filepath_bak)
|
||||||
|
if os.path.isfile(filepath):
|
||||||
|
os.rename(filepath, filepath_bak)
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, 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)
|
||||||
else:
|
else:
|
||||||
log.info("Saving the %s at: %s", filename, filepath)
|
# Sync the rename operations for the directory
|
||||||
try:
|
if hasattr(os, 'O_DIRECTORY'):
|
||||||
with open(filepath_tmp, "wb", 0) as _file:
|
dirfd = os.open(os.path.dirname(filepath), os.O_DIRECTORY)
|
||||||
_file.write(lt.bencode(self.resume_data))
|
os.fsync(dirfd)
|
||||||
_file.flush()
|
os.close(dirfd)
|
||||||
os.fsync(_file.fileno())
|
return True
|
||||||
os.rename(filepath_tmp, filepath)
|
|
||||||
except (OSError, EOFError) as ex:
|
|
||||||
log.error("Unable to save %s: %s", filename, ex)
|
|
||||||
if os.path.isfile(filepath_bak):
|
|
||||||
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
|
||||||
os.rename(filepath_bak, filepath)
|
|
||||||
else:
|
|
||||||
# Sync the rename operations for the directory
|
|
||||||
if hasattr(os, 'O_DIRECTORY'):
|
|
||||||
dirfd = os.open(os.path.dirname(filepath), os.O_DIRECTORY)
|
|
||||||
os.fsync(dirfd)
|
|
||||||
os.close(dirfd)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def get_queue_position(self, torrent_id):
|
def get_queue_position(self, torrent_id):
|
||||||
"""Get queue position of torrent"""
|
"""Get queue position of torrent"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user