Check that backend exists before loading database

This commit is contained in:
Nick Hall 2018-03-28 19:03:08 +01:00
parent 04d6ca3a49
commit e53603a1fc
3 changed files with 28 additions and 22 deletions

View File

@ -554,6 +554,9 @@ class ArgHandler:
if self.dbman.needs_recovery(dbpath): if self.dbman.needs_recovery(dbpath):
self.__error(_("Database needs recovery, cannot open it!")) self.__error(_("Database needs recovery, cannot open it!"))
return False return False
if self.dbman.backend_unavailable(dbpath):
self.__error(_("Database backend unavailable, cannot open it!"))
return False
return True return True
#------------------------------------------------------------------------- #-------------------------------------------------------------------------

View File

@ -48,8 +48,8 @@ import logging
from gramps.gen.plug import BasePluginManager from gramps.gen.plug import BasePluginManager
from gramps.gen.config import config from gramps.gen.config import config
from gramps.gen.constfunc import win from gramps.gen.constfunc import win
from gramps.gen.db.dbconst import DBLOGNAME from gramps.gen.db.dbconst import DBLOGNAME, DBBACKEND
from gramps.gen.db.utils import make_database from gramps.gen.db.utils import make_database, get_dbid_from_path
from gramps.gen.const import GRAMPS_LOCALE as glocale from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
@ -68,7 +68,6 @@ _LOG = logging.getLogger(DBLOGNAME)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
DEFAULT_TITLE = _("Family Tree") DEFAULT_TITLE = _("Family Tree")
NAME_FILE = "name.txt" NAME_FILE = "name.txt"
BACKEND_FILE = "database.txt"
META_NAME = "meta_data.db" META_NAME = "meta_data.db"
UNAVAILABLE = _('Unavailable') UNAVAILABLE = _('Unavailable')
@ -156,11 +155,7 @@ class CLIDbManager:
_("Version") _("Version")
_("Schema version") _("Schema version")
""" """
dbid = "bsddb" dbid = get_dbid_from_path(dirpath)
dbid_path = os.path.join(dirpath, "database.txt")
if os.path.isfile(dbid_path):
with open(dbid_path) as file:
dbid = file.read().strip()
if not self.is_locked(dirpath): if not self.is_locked(dirpath):
try: try:
database = make_database(dbid) database = make_database(dbid)
@ -237,11 +232,7 @@ class CLIDbManager:
for dpath in os.listdir(dbdir): for dpath in os.listdir(dbdir):
dirpath = os.path.join(dbdir, dpath) dirpath = os.path.join(dbdir, dpath)
path_name = os.path.join(dirpath, NAME_FILE) path_name = os.path.join(dirpath, NAME_FILE)
try: backend_type = get_dbid_from_path(dirpath)
with open(os.path.join(dirpath, "database.txt")) as file:
backend_type = file.read()
except:
backend_type = "bsddb"
if os.path.isfile(path_name): if os.path.isfile(path_name):
with open(path_name, 'r', encoding='utf8') as file: with open(path_name, 'r', encoding='utf8') as file:
name = file.readline().strip() name = file.readline().strip()
@ -309,7 +300,7 @@ class CLIDbManager:
dbid = config.get('database.backend') dbid = config.get('database.backend')
newdb = make_database(dbid) 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: with open(backend_path, "w", encoding='utf8') as backend_file:
backend_file.write(dbid) backend_file.write(dbid)
@ -403,6 +394,13 @@ class CLIDbManager:
return True return True
return False 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): def remove_database(self, dbname, user=None):
""" """
Deletes a database folder given a pattenr that matches Deletes a database folder given a pattenr that matches

View File

@ -41,7 +41,7 @@ from ..plug import BasePluginManager
from ..const import PLUGINS_DIR, USER_PLUGINS from ..const import PLUGINS_DIR, USER_PLUGINS
from ..constfunc import win, get_env_var from ..constfunc import win, get_env_var
from ..config import config 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: if dbname == name:
locked = False locked = False
locked_by = None locked_by = None
backend = None backend = get_dbid_from_path(dirpath)
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"
try: try:
fname = os.path.join(dirpath, "lock") fname = os.path.join(dirpath, "lock")
with open(fname, 'r', encoding='utf8') as ifile: with open(fname, 'r', encoding='utf8') as ifile:
@ -126,6 +120,17 @@ def lookup_family_tree(dbname):
return (dirpath, locked, locked_by, backend) return (dirpath, locked, locked_by, backend)
return None 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): def import_as_dict(filename, user, skp_imp_adds=True):
""" """
Import the filename into a InMemoryDB and return it. Import the filename into a InMemoryDB and return it.