diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index 4d9a31e1..4c2d9496 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -90,23 +90,32 @@ check_index_or_cleanup_params (MuConfig *opts) return FALSE; } - if (!opts->maildir || !g_path_is_absolute (opts->maildir)) { - g_warning ("maildir path '%s' is not valid", - opts->maildir ? opts->maildir : ""); - return FALSE; - } - if (opts->xbatchsize < 0) { g_warning ("the Xapian batch size must be non-negative"); return FALSE; } - if (!mu_util_check_dir (opts->maildir, TRUE, FALSE)) { - g_warning ("not a valid Maildir: %s", - opts->maildir ? opts->maildir : ""); + return TRUE; +} + +static gboolean +check_maildir (const char *maildir) +{ + if (!maildir) { + g_warning ("no maildir to work on"); return FALSE; } + if (!g_path_is_absolute (maildir)) { + g_warning ("maildir path '%s' is not absolute", maildir); + return FALSE; + } + + if (!mu_util_check_dir (maildir, TRUE, FALSE)) { + g_warning ("not a valid Maildir: %s", maildir); + return FALSE; + } + return TRUE; } @@ -161,7 +170,7 @@ database_version_check_and_update (MuConfig *opts) xpath = mu_runtime_xapian_dir (); - if (mu_util_db_is_empty (xpath)) + if (mu_util_xapian_is_empty (xpath)) return TRUE; /* when rebuilding, we empty the database before doing @@ -169,17 +178,17 @@ database_version_check_and_update (MuConfig *opts) if (opts->rebuild) { opts->reindex = TRUE; g_message ("clearing database %s", xpath); - return mu_util_clear_database (xpath); + return mu_util_xapian_clear (xpath); } - if (mu_util_db_version_up_to_date (xpath)) + if (!mu_util_xapian_needs_upgrade (xpath)) return TRUE; /* ok, nothing to do */ /* ok, database is not up to date */ if (opts->autoupgrade) { opts->reindex = TRUE; g_message ("auto-upgrade: clearing old database first"); - return mu_util_clear_database (xpath); + return mu_util_xapian_clear (xpath); } update_warning (); @@ -198,6 +207,36 @@ show_time (unsigned t, unsigned processed) } +static gboolean +update_maildir_path_maybe (MuIndex *idx, MuConfig *opts) +{ + gchar *exp; + + /* if 'maildir_guessed' is TRUE, we can override it later + * with mu_index_last_used_maildir (in mu-cmd-index.c) + */ + while (!opts->maildir) { + + const char *tmp; + + /* try the last used one */ + tmp = mu_index_last_used_maildir (idx); + if (tmp) + opts->maildir = g_strdup (tmp); + else + opts->maildir = mu_util_guess_maildir (); + } + + exp = mu_util_dir_expand(opts->maildir); + if (exp) { + g_free(opts->maildir); + opts->maildir = exp; + } + + return check_maildir (opts->maildir); +} + + static MuExitCode cmd_cleanup (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, gboolean show_progress) @@ -261,6 +300,36 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, return MU_EXITCODE_ERROR; } +static MuExitCode +handle_index_error_and_free (GError *err) +{ + MuExitCode code; + + if (!err) + return MU_EXITCODE_ERROR; + + switch (err->code) { + + case MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK: + g_warning ("cannot get Xapian writelock"); + g_warning ("maybe mu index is already running?"); + code = MU_EXITCODE_DB_LOCKED; + break; + + case MU_ERROR_XAPIAN_CORRUPTION: + g_warning ("xapian database seems to be corrupted"); + g_warning ("try 'mu index --rebuild"); + code = MU_EXITCODE_DB_CORRUPTED; + break; + default: + g_warning ("indexing error: %s", + err->message ? err->message : ""); + code = MU_EXITCODE_ERROR; + } + + g_error_free (err); + return code; +} static MuExitCode @@ -274,16 +343,19 @@ cmd_index_or_cleanup (MuConfig *opts) if (!check_index_or_cleanup_params (opts) || !database_version_check_and_update(opts)) - return MU_EXITCODE_ERROR; - + return MU_EXITCODE_ERROR; + err = NULL; - if (!(midx = mu_index_new (mu_runtime_xapian_dir(), - opts->xbatchsize, &err))) { - g_warning ("index/cleanup failed: %s", err->message); - g_error_free (err); - return MU_EXITCODE_ERROR; - } + if (!(midx = mu_index_new + (mu_runtime_xapian_dir(), opts->xbatchsize, &err))) + return handle_index_error_and_free (err); + /* we determine the maildir path only here, as it may depend on + * mu_index_last_used_maildir + */ + if (!update_maildir_path_maybe (midx, opts)) + return MU_EXITCODE_ERROR; + /* note, 'opts->quiet' already cause g_message output not to * be shown; here, we make sure we only print progress info if * opts->quiet is false case and when stdout is a tty */ diff --git a/src/mu-config.c b/src/mu-config.c index a6b0c985..f7a32828 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -72,17 +72,9 @@ config_options_group_mu (MuConfig *opts) static void set_group_index_defaults (MuConfig * opts) { - gchar *exp; - - if (!opts->maildir) - opts->maildir = mu_util_guess_maildir(); - - exp = mu_util_dir_expand(opts->maildir); - if (exp) { - g_free(opts->maildir); - opts->maildir = exp; - } - + /* note: opts->maildir is handled in mu-cmd, as we need to + * have a MuIndex entry for mu_index_last_used_maildir () + */ opts->xbatchsize = 0; } diff --git a/src/mu-index.c b/src/mu-index.c index d46ad49f..755de623 100644 --- a/src/mu-index.c +++ b/src/mu-index.c @@ -34,9 +34,12 @@ #include "mu-store.h" #include "mu-util.h" +#define MU_LAST_USED_MAILDIR_KEY "last_used_maildir" + struct _MuIndex { - MuStore *_xapian; + MuStore *_store; gboolean _needs_reindex; + gchar *_last_used_maildir; }; MuIndex* @@ -47,9 +50,9 @@ mu_index_new (const char *xpath, guint xbatchsize, GError **err) g_return_val_if_fail (xpath, NULL); index = g_new0 (MuIndex, 1); - index->_xapian = mu_store_new (xpath, xbatchsize, err); - - if (!index->_xapian) { + + index->_store = mu_store_new (xpath, xbatchsize, err); + if (!index->_store) { g_warning ("%s: failed to open xapian store (%s)", __FUNCTION__, xpath); g_free (index); @@ -60,11 +63,11 @@ mu_index_new (const char *xpath, guint xbatchsize, GError **err) * race-condition here, between mu_index_new and * mu_index_run. Maybe do the check in mu_index_run * instead? */ - if (mu_util_db_is_empty (xpath)) + if (mu_util_xapian_is_empty (xpath)) index->_needs_reindex = FALSE; else index->_needs_reindex = - mu_util_db_version_up_to_date (xpath) ? FALSE : TRUE; + mu_util_xapian_needs_upgrade (xpath); return index; } @@ -75,8 +78,10 @@ mu_index_destroy (MuIndex *index) { if (!index) return; - - mu_store_destroy (index->_xapian); + + g_free (index->_last_used_maildir); + mu_store_destroy (index->_store); + g_free (index); } @@ -84,7 +89,7 @@ mu_index_destroy (MuIndex *index) struct _MuIndexCallbackData { MuIndexMsgCallback _idx_msg_cb; MuIndexDirCallback _idx_dir_cb; - MuStore* _xapian; + MuStore* _store; void* _user_data; MuIndexStats* _stats; gboolean _reindex; @@ -107,7 +112,7 @@ needs_index (MuIndexCallbackData *data, const char *fullpath, return TRUE; /* it's not in the database yet */ - if (!mu_store_contains_message (data->_xapian, fullpath)) + if (!mu_store_contains_message (data->_store, fullpath)) return TRUE; /* it's there, but it's not up to date */ @@ -139,7 +144,7 @@ insert_or_update_maybe (const char* fullpath, const char* mdir, } /* we got a valid id; scan the message contents as well */ - if (G_UNLIKELY((mu_store_store (data->_xapian, msg) != MU_OK))) { + if (G_UNLIKELY((mu_store_store (data->_store, msg) != MU_OK))) { g_warning ("%s: storing content %s failed", __FUNCTION__, fullpath); return MU_ERROR; @@ -206,13 +211,13 @@ on_run_maildir_dir (const char* fullpath, gboolean enter, */ if (enter) { data->_dirstamp = - mu_store_get_timestamp (data->_xapian, + mu_store_get_timestamp (data->_store, fullpath); g_debug ("entering %s (ts==%u)", fullpath, (unsigned)data->_dirstamp); } else { time_t now = time (NULL); - mu_store_set_timestamp (data->_xapian, fullpath, + mu_store_set_timestamp (data->_store, fullpath, now); g_debug ("leaving %s (ts=%u)", fullpath, (unsigned)data->_dirstamp); @@ -255,7 +260,7 @@ init_cb_data (MuIndexCallbackData *cb_data, MuStore *xapian, cb_data->_idx_dir_cb = dir_cb; cb_data->_user_data = user_data; - cb_data->_xapian = xapian; + cb_data->_store = xapian; cb_data->_reindex = reindex; cb_data->_dirstamp = 0; @@ -265,6 +270,25 @@ init_cb_data (MuIndexCallbackData *cb_data, MuStore *xapian, memset (cb_data->_stats, 0, sizeof(MuIndexStats)); } +static void +update_last_used_maildir (MuIndex *index, const char *path) +{ + if (!mu_store_set_metadata (index->_store, + MU_LAST_USED_MAILDIR_KEY, + path)) + g_warning ("%s: failed to set metadata", __FUNCTION__); +} + +const char* +mu_index_last_used_maildir (MuIndex *index) +{ + g_return_val_if_fail (index, NULL); + g_free (index->_last_used_maildir); + + return index->_last_used_maildir = + mu_store_get_metadata (index->_store, + MU_LAST_USED_MAILDIR_KEY); +} MuResult @@ -276,7 +300,7 @@ mu_index_run (MuIndex *index, const char* path, MuIndexCallbackData cb_data; MuResult rv; - g_return_val_if_fail (index && index->_xapian, MU_ERROR); + g_return_val_if_fail (index && index->_store, MU_ERROR); g_return_val_if_fail (msg_cb, MU_ERROR); if (!check_path (path)) @@ -287,15 +311,17 @@ mu_index_run (MuIndex *index, const char* path, return MU_ERROR; } - init_cb_data (&cb_data, index->_xapian, reindex, stats, + init_cb_data (&cb_data, index->_store, reindex, stats, msg_cb, dir_cb, user_data); + update_last_used_maildir (index, path); + rv = mu_maildir_walk (path, (MuMaildirWalkMsgCallback)on_run_maildir_msg, (MuMaildirWalkDirCallback)on_run_maildir_dir, &cb_data); - mu_store_flush (index->_xapian); + mu_store_flush (index->_store); return rv; } @@ -354,7 +380,7 @@ mu_index_stats (MuIndex *index, const char* path, struct _CleanupData { - MuStore *_xapian; + MuStore *_store; MuIndexStats *_stats; MuIndexCleanupDeleteCallback _cb; void *_user_data; @@ -371,7 +397,7 @@ foreach_doc_cb (const char* path, CleanupData *cudata) if (access (path, R_OK) != 0) { g_debug ("not readable: %s; removing", path); - rv = mu_store_remove (cudata->_xapian, path); + rv = mu_store_remove (cudata->_store, path); if (rv != MU_OK) return rv; /* something went wrong... bail out */ @@ -400,15 +426,15 @@ mu_index_cleanup (MuIndex *index, MuIndexStats *stats, g_return_val_if_fail (index, MU_ERROR); g_return_val_if_fail (cb, MU_ERROR); - cudata._xapian = index->_xapian; + cudata._store = index->_store; cudata._stats = stats; cudata._cb = cb; cudata._user_data = user_data; - rv = mu_store_foreach (index->_xapian, + rv = mu_store_foreach (index->_store, (MuStoreForeachFunc)foreach_doc_cb, &cudata); - mu_store_flush (index->_xapian); + mu_store_flush (index->_store); return rv; } diff --git a/src/mu-index.h b/src/mu-index.h index 7224fa77..e2413bae 100644 --- a/src/mu-index.h +++ b/src/mu-index.h @@ -64,6 +64,17 @@ MuIndex* mu_index_new (const char* muhome, guint batchsize, GError **err) void mu_index_destroy (MuIndex *index); +/** + * get the maildir for the last run of indexing for the + * current database + * + * @param index MuIndex object + * + * @return the last used maildir, or NULL + */ +const char* mu_index_last_used_maildir (MuIndex *index); + + /** * callback function for mu_index_(run|stats|cleanup), for each message *