~ubuntu-branches/ubuntu/quantal/maildir-utils/quantal

« back to all changes in this revision

Viewing changes to index/mu-index-app.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2010-01-19 20:12:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100119201243-d8qmzgxgexhy1bs0
Tags: 0.6~beta1-1
* new upstream release 0.6-beta
  - that merges the several different programs under one binary mu
  - no sqlite storage is used anymore
* debian packaging changes:
  - debian/patches
    . remove all patches
  - remove debian/HOWTO (upstream document) it is completely outdated
  - debian/control:
    . adjust build-dep for gmime-2.4
    . remove build-dep on quilt and sqlite
    . adjust the description to new reality
  - debian/rules:
    . do not try to install doc files that are not present anymore
    . disable quilt adaptions
  - add debian/NEWS that explains that the separate programs are gone

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 2008 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
3
 
**
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.
8
 
**
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.
13
 
**
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.
17
 
**
18
 
*/
19
 
#define _XOPEN_SOURCE
20
 
 
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
#include <unistd.h>
24
 
#include <errno.h>
25
 
#include <signal.h>
26
 
 
27
 
#include "app/mu-util.h"
28
 
#include "app/mu-app.h"
29
 
 
30
 
#include "msg/mu-msg-gmime.h"
31
 
#include "mu-index-app.h"
32
 
 
33
 
 
34
 
static gboolean _INITIALIZED = FALSE;
35
 
static char*    _MAILDIR     = NULL;
36
 
 
37
 
/*
38
 
 * this is set by signal handlers and checked by our
39
 
 * callbacks, and allows us to terminate gracefully
40
 
 * (close database etc.)
41
 
 */
42
 
static gboolean _CAUGHT_SIGNAL = FALSE;
43
 
 
44
 
struct _IndexOptions {
45
 
        gboolean quiet;
46
 
        gboolean cleanup;
47
 
        const char *maildir;
48
 
 
49
 
        unsigned int sqlite_transaction_size;
50
 
        unsigned int synchronous;
51
 
        unsigned int temp_store;
52
 
        
53
 
        unsigned int xapian_transaction_size;
54
 
        gboolean sort_inodes;
55
 
};
56
 
typedef struct _IndexOptions IndexOptions;
57
 
 
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"},
76
 
        {NULL}
77
 
};
78
 
 
79
 
static void
80
 
set_default_options (void)
81
 
{
82
 
        memset (&INDEX_OPTIONS, 0, sizeof(IndexOptions));
83
 
        
84
 
        /*defaults:*/
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;
90
 
}
91
 
 
92
 
static void
93
 
sig_handler (int i)
94
 
{
95
 
        /* when pressed twice... */
96
 
        if (_CAUGHT_SIGNAL)
97
 
                exit (0);
98
 
 
99
 
        g_print ("\ncaught signal %d; exiting loop\n", i);
100
 
        _CAUGHT_SIGNAL = TRUE;
101
 
}
102
 
 
103
 
static void
104
 
install_sig_handler (void)
105
 
{
106
 
        struct sigaction action;
107
 
        int i, sigs[] = { SIGINT, SIGHUP, SIGTERM };
108
 
 
109
 
        _CAUGHT_SIGNAL = FALSE;
110
 
 
111
 
        action.sa_handler = sig_handler;
112
 
        sigemptyset(&action.sa_mask);
113
 
        action.sa_flags = 0;
114
 
 
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",
118
 
                                    sigs[i]);
119
 
}
120
 
 
121
 
 
122
 
 
123
 
static char*
124
 
get_default_maildir (void)
125
 
{
126
 
        const char* home;
127
 
 
128
 
        if (getenv("MAILDIR")) 
129
 
                return g_strdup (getenv("MAILDIR"));
130
 
        
131
 
        home = getenv ("HOME");
132
 
        if (!home)
133
 
                home = g_get_home_dir ();
134
 
 
135
 
        return g_strdup_printf ("%s%c%s", home, G_DIR_SEPARATOR,
136
 
                                "Maildir");
137
 
}
138
 
 
139
 
 
140
 
static gboolean
141
 
handle_options (int *argcp, char ***argvp)
142
 
{
143
 
        GError *err = NULL;
144
 
        if (!mu_conf_handle_options (mu_app_conf(),OPTION_ENTRIES, argcp, argvp, 
145
 
                                     &err)) {
146
 
                g_printerr ("option parsing failed: %s\n", 
147
 
                            (err && err->message) ? err->message : "?" );
148
 
                if (err)
149
 
                        g_error_free (err);
150
 
                return FALSE;
151
 
        }
152
 
        return TRUE;
153
 
}
154
 
 
155
 
 
156
 
static gchar*
157
 
get_maildir_and_check (const char* maildir_or_null, GError **err)
158
 
{
159
 
        gchar* maildir; 
160
 
 
161
 
        if (!maildir_or_null) {
162
 
                if (INDEX_OPTIONS.maildir)
163
 
                        maildir = mu_util_homedir_expand (INDEX_OPTIONS.maildir);
164
 
                else
165
 
                        maildir = get_default_maildir ();
166
 
        } else
167
 
                maildir = g_strdup(maildir_or_null);
168
 
 
169
 
        if (!g_path_is_absolute (maildir)) {
170
 
                g_set_error (err, 0, 0, "'%s' is not an absolute path",
171
 
                             maildir);
172
 
                g_free (maildir);
173
 
                return NULL;
174
 
        }
175
 
        
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));
179
 
                g_free (maildir);
180
 
                return NULL;
181
 
        }
182
 
 
183
 
        return maildir;
184
 
}
185
 
 
186
 
 
187
 
gboolean
188
 
mu_index_app_init (int *argcp, char***argvp, GError **err)
189
 
{
190
 
        if (_INITIALIZED)
191
 
                return TRUE;
192
 
 
193
 
        g_return_val_if_fail (argcp, FALSE);
194
 
        g_return_val_if_fail (argvp, FALSE);
195
 
 
196
 
        install_sig_handler ();
197
 
        set_default_options ();
198
 
 
199
 
        if (!mu_app_init (argcp, argvp, "mu-index")) {
200
 
                g_set_error (err, 0, 0, "failed init mu");
201
 
                return FALSE;
202
 
        }
203
 
        if (!handle_options(argcp, argvp)) {
204
 
                g_set_error (err, 0, 0, "failed to handle options");
205
 
                return FALSE;
206
 
        }
207
 
        
208
 
        _MAILDIR = get_maildir_and_check (*argcp > 1 ? (*argvp)[1] : NULL, 
209
 
                                          err); 
210
 
        if (!_MAILDIR)
211
 
                return FALSE;
212
 
        
213
 
        mu_msg_gmime_init ();
214
 
 
215
 
        return _INITIALIZED = TRUE;
216
 
}
217
 
 
218
 
 
219
 
 
220
 
gboolean
221
 
mu_index_app_uninit (void)
222
 
{
223
 
        if (!_INITIALIZED)
224
 
                return TRUE;
225
 
 
226
 
        g_free (_MAILDIR);
227
 
        _MAILDIR = NULL;
228
 
 
229
 
        mu_msg_gmime_uninit ();
230
 
        mu_app_uninit ();
231
 
 
232
 
        _INITIALIZED = FALSE;
233
 
 
234
 
        return TRUE;
235
 
}
236
 
 
237
 
 
238
 
static void
239
 
tune_index_settings (MuIndex* index)
240
 
{
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);
248
 
}
249
 
 
250
 
 
251
 
MuIndex*
252
 
mu_index_app_get_index (GError **err)
253
 
{
254
 
        MuIndex *index;
255
 
        
256
 
        g_return_val_if_fail (_INITIALIZED, NULL);
257
 
        
258
 
        index = mu_index_new (mu_app_sqlite_path(), mu_app_xapian_path());
259
 
        if (!index) {
260
 
                g_set_error (err, 0, 0, "cannot get index object");
261
 
                return NULL;
262
 
        }
263
 
 
264
 
        tune_index_settings (index);
265
 
        return index;
266
 
}
267
 
 
268
 
 
269
 
gboolean
270
 
mu_index_app_quiet (void)
271
 
{
272
 
        g_return_val_if_fail (_INITIALIZED, FALSE);
273
 
        
274
 
        return INDEX_OPTIONS.quiet;
275
 
}
276
 
 
277
 
 
278
 
const char*
279
 
mu_index_app_get_maildir(void)
280
 
{
281
 
        g_return_val_if_fail (_INITIALIZED, FALSE);
282
 
        
283
 
        return _MAILDIR;
284
 
}
285
 
 
286
 
const gboolean
287
 
mu_index_app_caught_signal (void)
288
 
{
289
 
        g_return_val_if_fail (_INITIALIZED, FALSE);
290
 
        
291
 
        return _CAUGHT_SIGNAL;
292
 
 
293
 
}