diff options
author | Calum Lind <calumlind+deluge@gmail.com> | 2013-05-21 23:45:26 +0100 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2013-05-22 01:25:25 +0100 |
commit | 2c4ef9dbb306ebefcb3314b4415c736fc3b263d2 (patch) | |
tree | 384c6a210dcfed9fdda260a5f839566883cbc96d /deluge/core/authmanager.py | |
parent | 2bbc1013be3a615fc29b998804f4e4bde639a9d4 (diff) | |
download | deluge-2c4ef9dbb306ebefcb3314b4415c736fc3b263d2.tar.gz deluge-2c4ef9dbb306ebefcb3314b4415c736fc3b263d2.tar.bz2 deluge-2c4ef9dbb306ebefcb3314b4415c736fc3b263d2.zip |
Fixup saving and loading state files
* All state files have a backup created before saving
* The backup will now be used if saving or loading fails
* GTKUI state files stored in new gtkui_state dir and common load/save functions created
* Detects bad shutdown and archives timestamped state files in separate config directory.
Diffstat (limited to 'deluge/core/authmanager.py')
-rw-r--r-- | deluge/core/authmanager.py | 66 |
1 files changed, 40 insertions, 26 deletions
diff --git a/deluge/core/authmanager.py b/deluge/core/authmanager.py index f8cffbb7c..768d9f852 100644 --- a/deluge/core/authmanager.py +++ b/deluge/core/authmanager.py @@ -100,7 +100,7 @@ class AuthManager(component.Component): def update(self): auth_file = configmanager.get_config_dir("auth") # Check for auth file and create if necessary - if not os.path.exists(auth_file): + if not os.path.isfile(auth_file): log.info("Authfile not found, recreating it.") self.__load_auth_file() return @@ -192,36 +192,40 @@ class AuthManager(component.Component): return True def write_auth_file(self): - old_auth_file = configmanager.get_config_dir("auth") - new_auth_file = old_auth_file + '.new' - bak_auth_file = old_auth_file + '.bak' - # Let's first create a backup - if os.path.exists(old_auth_file): - shutil.copy2(old_auth_file, bak_auth_file) + filename = "auth" + filepath = os.path.join(configmanager,get_config_dir(), filename) + filepath_bak = filepath + ".bak" try: - fd = open(new_auth_file, "w") - for account in self.__auth.values(): - fd.write( - "%(username)s:%(password)s:%(authlevel_int)s\n" % - account.data() - ) - fd.flush() - os.fsync(fd.fileno()) - fd.close() - os.rename(new_auth_file, old_auth_file) - except: - # Something failed, let's restore the previous file - if os.path.exists(bak_auth_file): - os.rename(bak_auth_file, old_auth_file) + if os.path.isfile(filepath): + log.info("Creating backup of %s at: %s", filename, filepath_bak) + shutil.copy2(filepath, filepath_bak) + except IOError 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, "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()) + except (IOError) 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) + shutil.move(filepath_bak, filepath) self.__load_auth_file() def __load_auth_file(self): save_and_reload = False - auth_file = configmanager.get_config_dir("auth") + filename = "auth" + auth_file = configmanager.get_config_dir(filename) + auth_file_bak = auth_file + ".bak" + # Check for auth file and create if necessary - if not os.path.exists(auth_file): + if not os.path.isfile(auth_file): create_localclient_account() return self.__load_auth_file() @@ -232,10 +236,20 @@ class AuthManager(component.Component): # File didn't change, no need for re-parsing's return - # Load the auth file into a dictionary: {username: Account(...)} - f = open(auth_file, "r").readlines() + for _filepath in (auth_file, auth_file_bak): + log.info("Opening %s for load: %s", filename, _filepath) + try: + with open(_filepath, "rb") as _file: + file_data = _file.readlines() + except (IOError), ex: + log.warning("Unable to load %s: %s", _filepath, ex) + file_data = [] + else: + log.info("Successfully loaded %s: %s", filename, _filepath) + break - for line in f: + # Load the auth file into a dictionary: {username: Account(...)} + for line in file_data: line = line.strip() if line.startswith("#") or not line: # This line is a comment or empty |