2010-01-31 11:12:04 +01:00
|
|
|
/*
|
|
|
|
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2010-07-25 18:13:56 +02:00
|
|
|
#include <signal.h>
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
#include "mu-util.h"
|
|
|
|
#include "mu-util-xapian.h"
|
|
|
|
|
|
|
|
#include "mu-msg-gmime.h"
|
|
|
|
|
|
|
|
#include "mu-index.h"
|
|
|
|
#include "mu-cmd-index.h"
|
|
|
|
|
|
|
|
static gboolean MU_CAUGHT_SIGNAL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_warning (void)
|
|
|
|
{
|
2010-02-08 20:22:30 +01:00
|
|
|
g_warning ("Note: the database needs to be upgraded to version %s\n",
|
2010-01-31 11:12:04 +01:00
|
|
|
MU_XAPIAN_DB_VERSION);
|
2010-02-08 20:22:30 +01:00
|
|
|
g_warning ("Please run 'mu index --rebuild' (see the manpage)\n");
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sig_handler (int sig)
|
|
|
|
{
|
|
|
|
if (!MU_CAUGHT_SIGNAL && sig == SIGINT) /* Ctrl-C */
|
2010-02-04 22:00:34 +01:00
|
|
|
g_warning ("Shutting down gracefully, "
|
|
|
|
"press again to kill immediately\n");
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
MU_CAUGHT_SIGNAL = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
install_sig_handler (void)
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
int i, sigs[] = { SIGINT, SIGHUP, SIGTERM };
|
|
|
|
|
|
|
|
MU_CAUGHT_SIGNAL = FALSE;
|
|
|
|
|
|
|
|
action.sa_handler = sig_handler;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = SA_RESETHAND;
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS(sigs); ++i)
|
|
|
|
if (sigaction (sigs[i], &action, NULL) != 0)
|
|
|
|
g_warning ("error: set sigaction for %d failed: %s",
|
|
|
|
sigs[i], strerror (errno));;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
check_index_params (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
if (opts->linksdir || opts->xquery) {
|
2010-02-04 22:00:34 +01:00
|
|
|
g_warning ("Error: Invalid option(s) for command\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-07-26 11:12:43 +02:00
|
|
|
|
|
|
|
if (!opts->maildir || !g_path_is_absolute (opts->maildir)) {
|
|
|
|
g_warning ("Error: maildir path is not valid\n");
|
2010-01-31 11:12:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-02 20:53:28 +01:00
|
|
|
if (!mu_util_check_dir (opts->maildir, TRUE, FALSE)) {
|
2010-07-30 19:58:51 +02:00
|
|
|
g_warning ("Error: not a valid Maildir (%s)\n",
|
|
|
|
opts->maildir);
|
2010-01-31 11:12:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MuResult
|
|
|
|
index_msg_silent_cb (MuIndexStats* stats, void *user_data)
|
|
|
|
{
|
|
|
|
return MU_CAUGHT_SIGNAL ? MU_STOP: MU_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MuResult
|
|
|
|
index_msg_cb (MuIndexStats* stats, void *user_data)
|
|
|
|
{
|
|
|
|
char *kars="-\\|/";
|
|
|
|
char output[314];
|
|
|
|
|
|
|
|
static int i = 0;
|
|
|
|
static int len = 0;
|
|
|
|
|
|
|
|
while (len --> 0) /* note the --> operator :-) */
|
|
|
|
printf ("\b");
|
|
|
|
|
|
|
|
len = snprintf (output, sizeof(output),
|
2010-08-15 12:33:15 +02:00
|
|
|
"%c processing mail; processed: %u; "
|
|
|
|
"updated/new: %u, cleaned-up: %u",
|
|
|
|
(unsigned)kars[i % 4],
|
|
|
|
(unsigned)stats->_processed,
|
|
|
|
(unsigned)stats->_updated,
|
|
|
|
(unsigned)stats->_cleaned_up);
|
2010-01-31 11:12:04 +01:00
|
|
|
g_print ("%s", output);
|
|
|
|
++i;
|
|
|
|
|
|
|
|
return MU_CAUGHT_SIGNAL ? MU_STOP: MU_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
database_version_check_and_update (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
if (mu_util_xapian_db_is_empty (opts->xpath))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* we empty the database before doing anything */
|
2010-02-08 20:22:30 +01:00
|
|
|
if (opts->rebuild) {
|
2010-01-31 11:12:04 +01:00
|
|
|
opts->reindex = TRUE;
|
2010-02-08 20:22:30 +01:00
|
|
|
g_message ("Clearing database %s", opts->xpath);
|
2010-01-31 11:12:04 +01:00
|
|
|
return mu_util_xapian_clear_database (opts->xpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mu_util_xapian_db_version_up_to_date (opts->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_xapian_clear_database (opts->xpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
update_warning ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
static MuResult
|
|
|
|
run_cleanup (MuIndex *midx, MuIndexStats *stats, gboolean quiet)
|
|
|
|
{
|
|
|
|
return mu_index_cleanup (midx, stats,
|
|
|
|
quiet ? index_msg_silent_cb : index_msg_cb,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
gboolean
|
|
|
|
mu_cmd_cleanup (MuConfigOptions *opts)
|
|
|
|
{
|
2010-01-31 19:36:56 +01:00
|
|
|
MuResult rv;
|
2010-01-31 11:12:04 +01:00
|
|
|
MuIndex *midx;
|
|
|
|
MuIndexStats stats;
|
|
|
|
|
|
|
|
g_return_val_if_fail (opts, FALSE);
|
|
|
|
|
|
|
|
if (!check_index_params (opts))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
install_sig_handler ();
|
|
|
|
|
|
|
|
midx = mu_index_new (opts->xpath);
|
|
|
|
if (!midx) {
|
|
|
|
g_warning ("Cleanup failed");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_message ("Cleaning up removed messages from %s",
|
|
|
|
opts->xpath);
|
2010-02-13 13:08:45 +01:00
|
|
|
mu_index_stats_clear (&stats);
|
2010-01-31 19:36:56 +01:00
|
|
|
rv = run_cleanup (midx, &stats, opts->quiet);
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
mu_index_destroy (midx);
|
|
|
|
|
|
|
|
if (!opts->quiet)
|
|
|
|
g_print ("\n");
|
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
return (rv == MU_OK || rv == MU_STOP) ? TRUE: FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MuResult
|
|
|
|
run_index (MuIndex *midx, const char* maildir, MuIndexStats *stats,
|
|
|
|
gboolean reindex, gboolean quiet)
|
|
|
|
{
|
|
|
|
MuResult rv;
|
|
|
|
|
|
|
|
mu_index_stats_clear (stats);
|
|
|
|
mu_msg_gmime_init ();
|
|
|
|
|
|
|
|
rv = mu_index_run (midx, maildir, reindex, stats,
|
|
|
|
quiet ? index_msg_silent_cb :index_msg_cb,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
mu_msg_gmime_init ();
|
|
|
|
|
|
|
|
return rv;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_cmd_index (MuConfigOptions *opts)
|
|
|
|
{
|
2010-01-31 19:36:56 +01:00
|
|
|
gboolean rv;
|
|
|
|
MuIndex *midx;
|
|
|
|
MuIndexStats stats;
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
g_return_val_if_fail (opts, FALSE);
|
|
|
|
|
|
|
|
if (!check_index_params (opts))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!database_version_check_and_update(opts))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
install_sig_handler ();
|
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
midx = mu_index_new (opts->xpath);
|
|
|
|
if (!midx) {
|
|
|
|
g_warning ("Indexing failed");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-08-14 14:31:55 +02:00
|
|
|
g_message ("Indexing messages under %s", opts->maildir);
|
2010-01-31 19:36:56 +01:00
|
|
|
g_message ("Database: %s", opts->xpath);
|
|
|
|
|
|
|
|
rv = run_index (midx, opts->maildir, &stats, opts->reindex, opts->quiet);
|
2010-02-13 17:45:44 +01:00
|
|
|
if (rv == MU_OK && !opts->nocleanup) {
|
|
|
|
stats._processed = 0; /* restart processed at 0 */
|
2010-01-31 19:36:56 +01:00
|
|
|
rv = run_cleanup (midx, &stats, opts->quiet);
|
2010-02-13 17:45:44 +01:00
|
|
|
}
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
mu_index_destroy (midx);
|
|
|
|
|
2010-08-15 12:33:15 +02:00
|
|
|
MU_WRITE_LOG ("processed: %u; updated/new: %u, cleaned-up: %u",
|
|
|
|
(unsigned)stats._processed,
|
|
|
|
(unsigned)stats._updated,
|
|
|
|
(unsigned)stats._cleaned_up);
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
if (!opts->quiet)
|
|
|
|
g_print ("\n");
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
return (rv == MU_OK || rv == MU_STOP) ? TRUE: FALSE;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|