mu-cmd: update for lib/mu-store changes

* some small API updates
 * in with_store, try to rebuild automatically when possible
This commit is contained in:
Dirk-Jan C. Binnema 2019-07-28 14:25:19 +03:00
parent 83d6484f86
commit afde8811b6
8 changed files with 111 additions and 122 deletions

View File

@ -229,17 +229,11 @@ get_query_obj (MuStore *store, GError **err)
return NULL;
if (count == 0) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_IS_EMPTY,
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_NEEDS_REINDEX,
"the database is empty");
return NULL;
}
if (!mu_store_versions_match (store)) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_VERSION_MISMATCH,
"the database needs a rebuild");
return NULL;
}
mquery = mu_query_new (store, err);
if (!mquery)
return NULL;

View File

@ -34,7 +34,7 @@
#include "mu-util.h"
#include "mu-msg.h"
#include "mu-index.h"
#include "mu-store.h"
#include "mu-store.hh"
#include "mu-runtime.h"
#include "mu-log.h"
@ -81,12 +81,6 @@ check_params (MuConfig *opts, GError **err)
return FALSE;
}
if (opts->xbatchsize < 0) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"the batch size must be >= 0");
return FALSE;
}
if (opts->max_msg_size < 0) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"the maximum message size must >= 0");
@ -185,36 +179,6 @@ index_msg_cb (MuIndexStats* stats, IndexData *idata)
return MU_CAUGHT_SIGNAL ? MU_STOP: MU_OK;
}
static gboolean
database_version_check_and_update (MuStore *store, MuConfig *opts,
GError **err)
{
if (mu_store_count (store, err) == 0)
return TRUE;
/* when rebuilding, we empty the database before doing
* anything */
if (opts->rebuild) {
g_debug ("clearing database");
g_debug ("clearing contacts-cache");
return mu_store_clear (store, err);
}
if (mu_store_versions_match (store))
return TRUE; /* ok, nothing to do */
/* ok, database is not up to date */
if (opts->autoupgrade) {
g_debug ("auto-upgrade: clearing old database and cache");
return mu_store_clear (store, err);
}
return FALSE;
}
static void
show_time (unsigned t, unsigned processed, gboolean color)
{
@ -288,17 +252,44 @@ cleanup_missing (MuIndex *midx, MuConfig *opts, MuIndexStats *stats,
static void
index_title (const char* maildir, const char* xapiandir, gboolean color)
index_title (MuStore *store, MuConfig *opts)
{
if (color)
g_print ("indexing messages under "
MU_COLOR_BLUE "%s" MU_COLOR_DEFAULT
" ["
MU_COLOR_BLUE "%s" MU_COLOR_DEFAULT
"]\n", maildir, xapiandir);
else
g_print ("indexing messages under %s [%s]\n",
maildir, xapiandir);
const char *blue, *green, *def;
char **addrs;
int i;
time_t created;
struct tm *tstamp;
char tbuf[40];
blue = opts->nocolor ? "" : MU_COLOR_BLUE;
green = opts->nocolor ? "" : MU_COLOR_GREEN;
def = opts->nocolor ? "" : MU_COLOR_DEFAULT;
g_print ("database : %s%s%s\n",
green, mu_store_database_path (store), def);
g_print ("schema-version : %s%s%s\n",
green, mu_store_schema_version(store), def);
created = mu_store_created (store);
tstamp = localtime (&created);
strftime (tbuf, sizeof(tbuf), "%c", tstamp);
g_print ("created : %s%s%s\n", green, tbuf, def);
g_print ("maildir : %s%s%s\n",
green, mu_store_maildir (store), def);
g_print ("personal-addresses : ");
addrs = mu_store_personal_addresses (store);
for (i = 0; addrs[i]; ++i) {
if (i != 0)
g_print (" ");
g_print ("%s%s%s\n", green, addrs[i], def);
}
g_strfreev(addrs);
g_print ("\n");
}
@ -309,11 +300,6 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, GError **err)
MuError rv;
gboolean show_progress;
if (!opts->quiet)
index_title (opts->maildir,
mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
!opts->nocolor);
show_progress = !opts->quiet && isatty(fileno(stdout));
idata.color = !opts->nocolor;
@ -345,10 +331,6 @@ init_mu_index (MuStore *store, MuConfig *opts, GError **err)
if (!check_params (opts, err))
return NULL;
if (!database_version_check_and_update(store, opts, err))
return NULL;
if (!check_maildir (opts->maildir, err))
return NULL;
@ -357,7 +339,6 @@ init_mu_index (MuStore *store, MuConfig *opts, GError **err)
return NULL;
mu_index_set_max_msg_size (midx, opts->max_msg_size);
mu_index_set_xbatch_size (midx, opts->xbatchsize);
return midx;
}
@ -366,10 +347,10 @@ init_mu_index (MuStore *store, MuConfig *opts, GError **err)
MuError
mu_cmd_index (MuStore *store, MuConfig *opts, GError **err)
{
MuIndex *midx;
MuIndexStats stats;
gboolean rv;
time_t t;
MuIndex *midx;
MuIndexStats stats;
gboolean rv;
time_t t;
g_return_val_if_fail (opts, FALSE);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_INDEX,
@ -383,6 +364,9 @@ mu_cmd_index (MuStore *store, MuConfig *opts, GError **err)
mu_index_stats_clear (&stats);
install_sig_handler ();
if (!opts->quiet)
index_title (store, opts);
t = time (NULL);
rv = cmd_index (midx, opts, &stats, err);

View File

@ -49,7 +49,7 @@
#include "mu-maildir.h"
#include "mu-query.h"
#include "mu-index.h"
#include "mu-store.h"
#include "mu-store.hh"
#include "mu-msg-part.h"
#include "mu-contacts.hh"
@ -624,7 +624,7 @@ each_contact_sexp (const char* full_address,
* @return the sexp
*/
static char*
contacts_to_sexp (MuContacts *contacts, gboolean personal,
contacts_to_sexp (const MuContacts *contacts, gboolean personal,
time_t last_seen, gint64 tstamp)
{
SexpData sdata;
@ -657,12 +657,12 @@ contacts_to_sexp (MuContacts *contacts, gboolean personal,
static MuError
cmd_contacts (ServerContext *ctx, GHashTable *args, GError **err)
{
MuContacts *contacts;
char *sexp;
gboolean personal;
time_t after;
const char *str;
gint64 tstamp;
const MuContacts *contacts;
char *sexp;
gboolean personal;
time_t after;
const char *str;
gint64 tstamp;
personal = get_bool_from_args (args, "personal", TRUE, NULL);
str = get_string_from_args (args, "after", TRUE, NULL);
@ -1012,7 +1012,7 @@ set_my_addresses (MuStore *store, const char *addrstr)
return;
my_addresses = g_strsplit (addrstr, ",", -1);
mu_store_set_my_addresses (store, (const char**)my_addresses);
mu_store_set_personal_addresses (store, (const char**)my_addresses);
g_strfreev (my_addresses);
}
@ -1040,7 +1040,7 @@ static MuError
index_and_maybe_cleanup (MuIndex *index, const char *path,
gboolean cleanup, gboolean lazy_check, GError **err)
{
MuError rv;
MuError rv;
MuIndexStats stats, stats2;
mu_index_stats_clear (&stats);
@ -1100,7 +1100,6 @@ cmd_index (ServerContext *ctx, GHashTable *args, GError **err)
index_and_maybe_cleanup (index, path,
cleanup, lazy_check,
err);
leave:
g_free (path);
@ -1531,10 +1530,6 @@ cmd_view (ServerContext *ctx, GHashTable *args, GError **err)
return MU_OK;
}
/*************************************************************************/
static MuError
@ -1578,12 +1573,11 @@ handle_args (ServerContext *ctx, GHashTable *args, GError **err)
}
MuError
mu_cmd_server (MuStore *store, MuConfig *opts/*unused*/, GError **err)
{
ServerContext ctx;
gboolean do_quit;
gboolean do_quit;
g_return_val_if_fail (store, MU_ERROR_INTERNAL);

View File

@ -38,7 +38,6 @@
#include "mu-contacts.hh"
#include "mu-runtime.h"
#include "mu-flags.h"
#include "mu-store.h"
#include "mu-log.h"
#define VIEW_TERMINATOR '\f' /* form-feed */
@ -569,31 +568,68 @@ show_usage (void)
typedef MuError (*store_func) (MuStore *, MuConfig *, GError **err);
static gboolean
needs_rebuild (MuStore *store, MuConfig *opts, GError **err)
{
if (store)
return mu_store_count(store, NULL) == 0;
else
return err &&
(*err)->code == MU_ERROR_XAPIAN_NEEDS_REINDEX &&
opts->maildir;
}
static MuError
with_store (store_func func, MuConfig *opts, gboolean read_only,
GError **err)
{
MuStore *store;
MuError merr;
MuError merr;
if (read_only)
store = mu_store_new_read_only
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
err);
else
if (opts->rebuild && read_only) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR,
"cannot rebuild a read-only database");
return MU_G_ERROR_CODE(err);
}
if (read_only) {
store = mu_store_new_readable (
mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), err);
} else if (!opts->rebuild) {
store = mu_store_new_writable
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), err);
if (needs_rebuild (store, opts, err)) {
if (store)
mu_store_unref(store);
opts->rebuild = TRUE;
g_clear_error (err);
return with_store(func, opts, read_only, err);
}
} else { /* rebuilding */
if (!opts->maildir) {
g_set_error (err, MU_ERROR_DOMAIN,
MU_ERROR_IN_PARAMETERS,
"missing --maildir parameter");
}
store = mu_store_new_create
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
opts->rebuild, err);
opts->maildir, err);
}
if (!store)
return MU_G_ERROR_CODE(err);
mu_store_set_my_addresses (store, (const char**)opts->my_addresses);
if (!read_only && opts->my_addresses)
mu_store_set_personal_addresses (
store, (const char**)opts->my_addresses);
merr = func (store, opts, err);
mu_store_unref (store);
return merr;
}
static gboolean
check_params (MuConfig *opts, GError **err)
{
@ -607,7 +643,6 @@ check_params (MuConfig *opts, GError **err)
return TRUE;
}
static void
set_log_options (MuConfig *opts)
{
@ -626,7 +661,6 @@ set_log_options (MuConfig *opts)
}
MuError
mu_cmd_execute (MuConfig *opts, GError **err)
{

View File

@ -19,13 +19,12 @@
**
*/
#ifndef __MU_CMD_H__
#define __MU_CMD_H__
#include <glib.h>
#include <mu-config.h>
#include <mu-store.h>
#include <mu-store.hh>
G_BEGIN_DECLS

View File

@ -132,8 +132,6 @@ config_options_group_mu (void)
} \
}
static void
set_group_index_defaults (void)
{
@ -156,16 +154,10 @@ config_options_group_index (void)
"only check dir-timestamps (false)", NULL},
{"my-address", 0, 0, G_OPTION_ARG_STRING_ARRAY,
&MU_CONFIG.my_addresses,
"my e-mail address (regexp); can be used multiple times",
"my e-mail address; can be used multiple times",
"<address>"},
{"autoupgrade", 0, 0, G_OPTION_ARG_NONE,
&MU_CONFIG.autoupgrade,
"auto-upgrade the database with new mu versions (false)",
NULL},
{"nocleanup", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.nocleanup,
"don't clean up the database after indexing (false)", NULL},
{"xbatchsize", 0, 0, G_OPTION_ARG_INT, &MU_CONFIG.xbatchsize,
"set transaction batchsize for xapian commits (0)", NULL},
{"max-msg-size", 0, 0, G_OPTION_ARG_INT,
&MU_CONFIG.max_msg_size,
"set the maximum size for message files", "<size>"},

View File

@ -114,13 +114,8 @@ struct _MuConfig {
gchar *maildir; /* where the mails are */
gboolean nocleanup; /* don't cleanup del'd mails from db */
gboolean rebuild; /* empty the database before indexing */
gboolean autoupgrade; /* automatically upgrade db
* when needed */
gboolean lazycheck; /* don't check dirs with up-to-date
* timestamps */
int xbatchsize; /* batchsize for xapian
* commits, or 0 for
* default */
int max_msg_size; /* maximum size for message files */
char** my_addresses; /* 'my e-mail address', for mu
* cfind; can be use multiple
@ -136,7 +131,7 @@ struct _MuConfig {
gboolean summary; /* OBSOLETE: use summary_len */
int summary_len; /* max # of lines for summary */
gchar *bookmark; /* use bookmark */
gchar *bookmark; /* use bookmark */
gchar *formatstr; /* output type for find
* (plain,links,xml,json,sexp)
* and view (plain, sexp) and cfind

View File

@ -58,14 +58,11 @@ handle_error (MuConfig *conf, MuError merr, GError **err)
case MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK:
g_printerr ("maybe mu is already running?\n");
break;
case MU_ERROR_XAPIAN_CORRUPTION:
case MU_ERROR_XAPIAN_VERSION_MISMATCH:
g_printerr ("database needs a rebuild; "
"try 'mu index --rebuild'\n");
break;
case MU_ERROR_XAPIAN_IS_EMPTY:
g_printerr ("database is empty; try 'mu index'\n");
break;
case MU_ERROR_XAPIAN_NEEDS_REINDEX:
g_printerr ("database needs (re)indexing.\n"
"try 'mu index' "
"(see mu-index(1) for details)\n");
return;
case MU_ERROR_IN_PARAMETERS:
if (conf && mu_config_cmd_is_valid(conf->cmd))
mu_config_show_help (conf->cmd);