mu: better error handling for opening database read-only

Be a bit clearer for the user.
This commit is contained in:
Dirk-Jan C. Binnema
2020-02-04 21:14:30 +02:00
parent 3fc2a5f3f8
commit 2575b2d0e3
4 changed files with 61 additions and 18 deletions

View File

@ -569,6 +569,33 @@ show_usage (void)
typedef MuError (*store_func) (MuStore *, MuConfig *, GError **err);
static MuError
with_readonly_store (store_func func, MuConfig *opts, GError **err)
{
MuError merr;
MuStore *store;
const char *path;
if (opts->rebuild) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR,
"cannot rebuild a read-only database");
return MU_G_ERROR_CODE(err);
}
path = mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB);
store = mu_store_new_readable (path, err);
if (!store)
return MU_G_ERROR_CODE(err);
merr = func (store, opts, err);
mu_store_unref (store);
return merr;
}
static MuStore*
get_store (MuConfig *opts, gboolean read_only, GError **err)
{
@ -672,9 +699,10 @@ mu_cmd_execute (MuConfig *opts, GError **err)
case MU_CONFIG_CMD_EXTRACT: merr = mu_cmd_extract (opts, err); break;
case MU_CONFIG_CMD_CFIND:
merr = with_store (mu_cmd_cfind, opts, TRUE, err); break;
merr = with_readonly_store (mu_cmd_cfind, opts, err); break;
case MU_CONFIG_CMD_FIND:
merr = with_store (mu_cmd_find, opts, TRUE, err); break;
merr = with_readonly_store (mu_cmd_find, opts, err); break;
case MU_CONFIG_CMD_INDEX:
merr = with_store (mu_cmd_index, opts, FALSE, err); break;
case MU_CONFIG_CMD_ADD:

View File

@ -51,9 +51,13 @@ show_version (void)
static void
handle_error (MuConfig *conf, MuError merr, GError **err)
{
const char *path;
if (!(err && *err))
return;
path = mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB);
switch ((*err)->code) {
case MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK:
g_printerr ("maybe mu is already running?\n");
@ -71,6 +75,17 @@ handle_error (MuConfig *conf, MuError merr, GError **err)
g_printerr ("see the mu manpage for commands, or "
"'mu script' for the scripts\n");
break;
case MU_ERROR_XAPIAN_CANNOT_OPEN:
g_printerr("Failed to open database @ %s, \n"
"Please (re)build the database, i.e., with\n"
"\tmu index --rebuild\n", path);
return;
case MU_ERROR_XAPIAN_SCHEMA_MISMATCH:
g_printerr("Failed to open database @ %s, \n"
"because the schema version does not match mu's.\n\n"
"Please rebuild the database, i.e., with\n"
"\tmu index --rebuild\n", path);
return;
default:
break; /* nothing to do */
}