diff --git a/deluge/core/authmanager.py b/deluge/core/authmanager.py index cc8eaf73a..12f06dacb 100644 --- a/deluge/core/authmanager.py +++ b/deluge/core/authmanager.py @@ -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): diff --git a/deluge/core/core.py b/deluge/core/core.py index 314f03fdd..d9866e1c1 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -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): diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 0606c0edf..2fd3be13c 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -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): diff --git a/deluge/ui/gtkui/common.py b/deluge/ui/gtkui/common.py index 1c31453ba..edab86683 100644 --- a/deluge/ui/gtkui/common.py +++ b/deluge/ui/gtkui/common.py @@ -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):