Use a tmp file when saving state and resume files

This commit is contained in:
Calum Lind 2014-01-18 15:54:43 +00:00
parent d12f0365d5
commit 3180bc7104
4 changed files with 15 additions and 5 deletions

View File

@ -195,6 +195,7 @@ class AuthManager(component.Component):
filename = "auth"
filepath = os.path.join(configmanager.get_config_dir(), filename)
filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp"
try:
if os.path.isfile(filepath):
@ -205,11 +206,12 @@ class AuthManager(component.Component):
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath, "wb") as _file:
with open(filepath_tmp, "wb") as _file:
for account in self.__auth.values():
_file.write("%(username)s:%(password)s:%(authlevel_int)s\n" % account.data())
_file.flush()
os.fsync(_file.fileno())
shutil.move(filepath_tmp, filepath)
except (IOError) as ex:
log.error("Unable to save %s: %s", filename, ex)
if os.path.isfile(filepath_bak):

View File

@ -157,6 +157,7 @@ class Core(component.Component):
filename = "session.state"
filepath = get_config_dir(filename)
filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp"
try:
if os.path.isfile(filepath):
@ -167,10 +168,11 @@ class Core(component.Component):
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath, "wb") as _file:
with open(filepath_tmp, "wb") as _file:
_file.write(lt.bencode(self.session.save_state()))
_file.flush()
os.fsync(_file.fileno())
shutil.move(filepath_tmp, filepath)
except (IOError, EOFError) as ex:
log.error("Unable to save %s: %s", filename, ex)
if os.path.isfile(filepath_bak):

View File

@ -698,6 +698,7 @@ class TorrentManager(component.Component):
filename = "torrents.state"
filepath = os.path.join(self.state_dir, filename)
filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp"
try:
if os.path.isfile(filepath):
@ -708,11 +709,12 @@ class TorrentManager(component.Component):
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath, "wb") as _file:
with open(filepath_tmp, "wb") as _file:
# Pickle the TorrentManagerState object
cPickle.dump(state, _file)
_file.flush()
os.fsync(_file.fileno())
shutil.move(filepath_tmp, filepath)
except (IOError, cPickle.PicklingError) as ex:
log.error("Unable to save %s: %s", filename, ex)
if os.path.isfile(filepath_bak):
@ -788,6 +790,7 @@ class TorrentManager(component.Component):
filename = "torrents.fastresume"
filepath = os.path.join(self.state_dir, filename)
filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp"
try:
if os.path.isfile(filepath):
@ -798,10 +801,11 @@ class TorrentManager(component.Component):
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath, "wb") as _file:
with open(filepath_tmp, "wb") as _file:
_file.write(lt.bencode(self.resume_data))
_file.flush()
os.fsync(_file.fileno())
shutil.move(filepath_tmp, filepath)
except (IOError, EOFError) as ex:
log.error("Unable to save %s: %s", filename, ex)
if os.path.isfile(filepath_bak):

View File

@ -275,6 +275,7 @@ def save_pickled_state_file(filename, state):
from deluge.configmanager import get_config_dir
filepath = os.path.join(get_config_dir(), "gtkui_state", filename)
filepath_bak = filepath + ".bak"
filepath_tmp = filepath + ".tmp"
try:
if os.path.isfile(filepath):
@ -285,11 +286,12 @@ def save_pickled_state_file(filename, state):
else:
log.info("Saving the %s at: %s", filename, filepath)
try:
with open(filepath, "wb") as _file:
with open(filepath_tmp, "wb") as _file:
# Pickle the state object
cPickle.dump(state, _file)
_file.flush()
os.fsync(_file.fileno())
shutil.move(filepath_tmp, filepath)
except (IOError, EOFError, cPickle.PicklingError) as ex:
log.error("Unable to save %s: %s", filename, ex)
if os.path.isfile(filepath_bak):