From e53603a1fcf2dc90c7f5d90b73965e5deff9fbd0 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Wed, 28 Mar 2018 19:03:08 +0100 Subject: [PATCH] Check that backend exists before loading database --- gramps/cli/arghandler.py | 3 +++ gramps/cli/clidbman.py | 26 ++++++++++++-------------- gramps/gen/db/utils.py | 21 +++++++++++++-------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/gramps/cli/arghandler.py b/gramps/cli/arghandler.py index 5423e71ea..83788a6e0 100644 --- a/gramps/cli/arghandler.py +++ b/gramps/cli/arghandler.py @@ -554,6 +554,9 @@ class ArgHandler: if self.dbman.needs_recovery(dbpath): self.__error(_("Database needs recovery, cannot open it!")) return False + if self.dbman.backend_unavailable(dbpath): + self.__error(_("Database backend unavailable, cannot open it!")) + return False return True #------------------------------------------------------------------------- diff --git a/gramps/cli/clidbman.py b/gramps/cli/clidbman.py index 63335f1bd..7a110de82 100644 --- a/gramps/cli/clidbman.py +++ b/gramps/cli/clidbman.py @@ -48,8 +48,8 @@ import logging from gramps.gen.plug import BasePluginManager from gramps.gen.config import config from gramps.gen.constfunc import win -from gramps.gen.db.dbconst import DBLOGNAME -from gramps.gen.db.utils import make_database +from gramps.gen.db.dbconst import DBLOGNAME, DBBACKEND +from gramps.gen.db.utils import make_database, get_dbid_from_path from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -68,7 +68,6 @@ _LOG = logging.getLogger(DBLOGNAME) #------------------------------------------------------------------------- DEFAULT_TITLE = _("Family Tree") NAME_FILE = "name.txt" -BACKEND_FILE = "database.txt" META_NAME = "meta_data.db" UNAVAILABLE = _('Unavailable') @@ -156,11 +155,7 @@ class CLIDbManager: _("Version") _("Schema version") """ - dbid = "bsddb" - dbid_path = os.path.join(dirpath, "database.txt") - if os.path.isfile(dbid_path): - with open(dbid_path) as file: - dbid = file.read().strip() + dbid = get_dbid_from_path(dirpath) if not self.is_locked(dirpath): try: database = make_database(dbid) @@ -237,11 +232,7 @@ class CLIDbManager: for dpath in os.listdir(dbdir): dirpath = os.path.join(dbdir, dpath) path_name = os.path.join(dirpath, NAME_FILE) - try: - with open(os.path.join(dirpath, "database.txt")) as file: - backend_type = file.read() - except: - backend_type = "bsddb" + backend_type = get_dbid_from_path(dirpath) if os.path.isfile(path_name): with open(path_name, 'r', encoding='utf8') as file: name = file.readline().strip() @@ -309,7 +300,7 @@ class CLIDbManager: dbid = config.get('database.backend') newdb = make_database(dbid) - backend_path = os.path.join(new_path, BACKEND_FILE) + backend_path = os.path.join(new_path, DBBACKEND) with open(backend_path, "w", encoding='utf8') as backend_file: backend_file.write(dbid) @@ -403,6 +394,13 @@ class CLIDbManager: return True return False + def backend_unavailable(self, dbpath): + """ + Returns True if the database in dirpath has an unavailable backend + """ + dbid = get_dbid_from_path(dbpath) + return self.get_backend_name_from_dbid(dbid) == UNAVAILABLE + def remove_database(self, dbname, user=None): """ Deletes a database folder given a pattenr that matches diff --git a/gramps/gen/db/utils.py b/gramps/gen/db/utils.py index 1fb639fba..8adedabc7 100644 --- a/gramps/gen/db/utils.py +++ b/gramps/gen/db/utils.py @@ -41,7 +41,7 @@ from ..plug import BasePluginManager from ..const import PLUGINS_DIR, USER_PLUGINS from ..constfunc import win, get_env_var from ..config import config -from .dbconst import DBLOGNAME, DBLOCKFN +from .dbconst import DBLOGNAME, DBLOCKFN, DBBACKEND #------------------------------------------------------------------------- # @@ -109,13 +109,7 @@ def lookup_family_tree(dbname): if dbname == name: locked = False locked_by = None - backend = None - fname = os.path.join(dirpath, "database.txt") - if os.path.isfile(fname): - with open(fname, 'r', encoding='utf8') as ifile: - backend = ifile.read().strip() - else: - backend = "bsddb" + backend = get_dbid_from_path(dirpath) try: fname = os.path.join(dirpath, "lock") with open(fname, 'r', encoding='utf8') as ifile: @@ -126,6 +120,17 @@ def lookup_family_tree(dbname): return (dirpath, locked, locked_by, backend) return None +def get_dbid_from_path(dirpath): + """ + Return a database backend from a directory path. + """ + dbid = "bsddb" + dbid_path = os.path.join(dirpath, DBBACKEND) + if os.path.isfile(dbid_path): + with open(dbid_path) as file: + dbid = file.read().strip() + return dbid + def import_as_dict(filename, user, skp_imp_adds=True): """ Import the filename into a InMemoryDB and return it.