2
** Copyright (C) 2008 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 3 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software Foundation,
16
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
#include "app/mu-util.h"
28
#include "app/mu-app.h"
30
#include "msg/mu-msg-gmime.h"
31
#include "mu-index-app.h"
34
static gboolean _INITIALIZED = FALSE;
35
static char* _MAILDIR = NULL;
38
* this is set by signal handlers and checked by our
39
* callbacks, and allows us to terminate gracefully
40
* (close database etc.)
42
static gboolean _CAUGHT_SIGNAL = FALSE;
44
struct _IndexOptions {
49
unsigned int sqlite_transaction_size;
50
unsigned int synchronous;
51
unsigned int temp_store;
53
unsigned int xapian_transaction_size;
56
typedef struct _IndexOptions IndexOptions;
58
static IndexOptions INDEX_OPTIONS;
59
static GOptionEntry OPTION_ENTRIES[] = {
60
/* the tune- options are for performance tuning,
61
* but the defaults should be good for most */
62
{"tune-sqlite-transaction-size", 0, G_OPTION_FLAG_HIDDEN,
63
G_OPTION_ARG_INT, &INDEX_OPTIONS.sqlite_transaction_size},
64
{"tune-synchronous", 0, G_OPTION_FLAG_HIDDEN,
65
G_OPTION_ARG_INT, &INDEX_OPTIONS.synchronous},
66
{"tune-temp-store", 0, G_OPTION_FLAG_HIDDEN,
67
G_OPTION_ARG_INT, &INDEX_OPTIONS.temp_store},
68
{"tune-xapian-transaction-size", 0, G_OPTION_FLAG_HIDDEN,
69
G_OPTION_ARG_INT, &INDEX_OPTIONS.xapian_transaction_size},
70
{"tune-sort-inodes", 0, G_OPTION_FLAG_HIDDEN,
71
G_OPTION_ARG_INT, &INDEX_OPTIONS.sort_inodes},
72
{"maildir", 'm', 0, G_OPTION_ARG_FILENAME, &INDEX_OPTIONS.maildir,
73
"top of the maildir"},
74
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &INDEX_OPTIONS.quiet,
75
"don't show progress info during indexation"},
80
set_default_options (void)
82
memset (&INDEX_OPTIONS, 0, sizeof(IndexOptions));
85
INDEX_OPTIONS.sqlite_transaction_size = 100;
86
INDEX_OPTIONS.synchronous = 0; /* OFF */
87
INDEX_OPTIONS.temp_store = 2; /* MEMORY */
88
INDEX_OPTIONS.xapian_transaction_size = 1000;
89
INDEX_OPTIONS.sort_inodes = TRUE;
95
/* when pressed twice... */
99
g_print ("\ncaught signal %d; exiting loop\n", i);
100
_CAUGHT_SIGNAL = TRUE;
104
install_sig_handler (void)
106
struct sigaction action;
107
int i, sigs[] = { SIGINT, SIGHUP, SIGTERM };
109
_CAUGHT_SIGNAL = FALSE;
111
action.sa_handler = sig_handler;
112
sigemptyset(&action.sa_mask);
115
for (i = 0; i!= sizeof(sigs)/sizeof(sigs[0]); ++i)
116
if (sigaction (sigs[i], &action, NULL) != 0)
117
g_printerr ("error: installing sighandler %d failed\n",
124
get_default_maildir (void)
128
if (getenv("MAILDIR"))
129
return g_strdup (getenv("MAILDIR"));
131
home = getenv ("HOME");
133
home = g_get_home_dir ();
135
return g_strdup_printf ("%s%c%s", home, G_DIR_SEPARATOR,
141
handle_options (int *argcp, char ***argvp)
144
if (!mu_conf_handle_options (mu_app_conf(),OPTION_ENTRIES, argcp, argvp,
146
g_printerr ("option parsing failed: %s\n",
147
(err && err->message) ? err->message : "?" );
157
get_maildir_and_check (const char* maildir_or_null, GError **err)
161
if (!maildir_or_null) {
162
if (INDEX_OPTIONS.maildir)
163
maildir = mu_util_homedir_expand (INDEX_OPTIONS.maildir);
165
maildir = get_default_maildir ();
167
maildir = g_strdup(maildir_or_null);
169
if (!g_path_is_absolute (maildir)) {
170
g_set_error (err, 0, 0, "'%s' is not an absolute path",
176
if (access (maildir, R_OK) != 0) {
177
g_set_error (err, 0, 0, "'%s' is not a valid Maildir: %s",
178
maildir, strerror(errno));
188
mu_index_app_init (int *argcp, char***argvp, GError **err)
193
g_return_val_if_fail (argcp, FALSE);
194
g_return_val_if_fail (argvp, FALSE);
196
install_sig_handler ();
197
set_default_options ();
199
if (!mu_app_init (argcp, argvp, "mu-index")) {
200
g_set_error (err, 0, 0, "failed init mu");
203
if (!handle_options(argcp, argvp)) {
204
g_set_error (err, 0, 0, "failed to handle options");
208
_MAILDIR = get_maildir_and_check (*argcp > 1 ? (*argvp)[1] : NULL,
213
mu_msg_gmime_init ();
215
return _INITIALIZED = TRUE;
221
mu_index_app_uninit (void)
229
mu_msg_gmime_uninit ();
232
_INITIALIZED = FALSE;
239
tune_index_settings (MuIndex* index)
241
/* optimization tuning */
242
mu_index_tune (index,
243
INDEX_OPTIONS.sqlite_transaction_size,
244
INDEX_OPTIONS.synchronous,
245
INDEX_OPTIONS.temp_store,
246
INDEX_OPTIONS.xapian_transaction_size,
247
INDEX_OPTIONS.sort_inodes);
252
mu_index_app_get_index (GError **err)
256
g_return_val_if_fail (_INITIALIZED, NULL);
258
index = mu_index_new (mu_app_sqlite_path(), mu_app_xapian_path());
260
g_set_error (err, 0, 0, "cannot get index object");
264
tune_index_settings (index);
270
mu_index_app_quiet (void)
272
g_return_val_if_fail (_INITIALIZED, FALSE);
274
return INDEX_OPTIONS.quiet;
279
mu_index_app_get_maildir(void)
281
g_return_val_if_fail (_INITIALIZED, FALSE);
287
mu_index_app_caught_signal (void)
289
g_return_val_if_fail (_INITIALIZED, FALSE);
291
return _CAUGHT_SIGNAL;