2011-05-22 13:49:11 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
/*
|
2013-03-30 10:32:07 +01:00
|
|
|
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
1** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation; either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
2011-08-29 22:38:55 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** 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.
|
2011-08-29 22:38:55 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
2011-08-29 22:38:55 +02:00
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
*/
|
|
|
|
|
2010-09-26 16:29:54 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-11-25 21:55:06 +01:00
|
|
|
#include "config.h"
|
2010-09-26 16:29:54 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
|
|
|
#include "mu-index.h"
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
#include "mu-maildir.h"
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
#define MU_LAST_USED_MAILDIR_KEY "last_used_maildir"
|
2016-03-13 15:41:09 +01:00
|
|
|
#define MU_INDEX_MAX_FILE_SIZE (500*1000*1000) /* 500 Mb */
|
|
|
|
/* apparently, people are getting really big mails, so let us index those (by
|
|
|
|
* default)*/
|
2011-01-12 22:11:51 +01:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
struct _MuIndex {
|
2011-01-12 22:11:51 +01:00
|
|
|
MuStore *_store;
|
2010-09-12 20:19:02 +02:00
|
|
|
gboolean _needs_reindex;
|
2011-01-15 12:27:41 +01:00
|
|
|
guint _max_filesize;
|
2009-11-25 21:55:06 +01:00
|
|
|
};
|
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
MuIndex*
|
2011-08-30 21:00:52 +02:00
|
|
|
mu_index_new (MuStore *store, GError **err)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
|
|
|
MuIndex *index;
|
2011-09-03 09:45:54 +02:00
|
|
|
unsigned count;
|
2010-01-04 19:19:32 +01:00
|
|
|
|
2011-08-30 21:00:52 +02:00
|
|
|
g_return_val_if_fail (store, NULL);
|
|
|
|
g_return_val_if_fail (!mu_store_is_read_only(store), NULL);
|
2011-01-12 22:11:51 +01:00
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
index = g_new0 (MuIndex, 1);
|
|
|
|
|
2011-08-30 21:00:52 +02:00
|
|
|
index->_store = mu_store_ref (store);
|
2010-01-23 19:50:06 +01:00
|
|
|
|
2011-01-15 12:27:41 +01:00
|
|
|
/* set the default max file size */
|
|
|
|
index->_max_filesize = MU_INDEX_MAX_FILE_SIZE;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-09-03 09:45:54 +02:00
|
|
|
count = mu_store_count (store, err);
|
|
|
|
if (count == (unsigned)-1)
|
|
|
|
return NULL;
|
|
|
|
else if (count == 0)
|
2010-01-31 11:13:06 +01:00
|
|
|
index->_needs_reindex = FALSE;
|
2011-08-30 21:00:52 +02:00
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
/* else */
|
|
|
|
/* index->_needs_reindex = */
|
|
|
|
/* mu_store_database_needs_upgrade (xpath); */
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-12-02 22:16:57 +01:00
|
|
|
return index;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
void
|
2009-11-25 21:55:06 +01:00
|
|
|
mu_index_destroy (MuIndex *index)
|
|
|
|
{
|
|
|
|
if (!index)
|
|
|
|
return;
|
2011-01-12 22:11:51 +01:00
|
|
|
|
2011-08-30 21:00:52 +02:00
|
|
|
mu_store_unref (index->_store);
|
2009-11-25 21:55:06 +01:00
|
|
|
g_free (index);
|
|
|
|
}
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
struct _MuIndexCallbackData {
|
2016-07-23 18:18:09 +02:00
|
|
|
MuIndexMsgCallback _idx_msg_cb;
|
|
|
|
MuIndexDirCallback _idx_dir_cb;
|
|
|
|
MuStore* _store;
|
|
|
|
void* _user_data;
|
|
|
|
MuIndexStats* _stats;
|
|
|
|
gboolean _reindex;
|
|
|
|
gboolean _lazy_check;
|
|
|
|
time_t _dirstamp;
|
|
|
|
guint _max_filesize;
|
2009-11-25 21:55:06 +01:00
|
|
|
};
|
2010-09-12 20:19:02 +02:00
|
|
|
typedef struct _MuIndexCallbackData MuIndexCallbackData;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-01-04 21:50:26 +01:00
|
|
|
|
2011-07-18 08:05:56 +02:00
|
|
|
/* checks to determine if we need to (re)index this message note:
|
|
|
|
* simply checking timestamps is not good enough because message may
|
|
|
|
* be moved from other dirs (e.g. from 'new' to 'cur') and the time
|
|
|
|
* stamps won't change. */
|
2010-11-27 13:54:19 +01:00
|
|
|
static inline gboolean
|
|
|
|
needs_index (MuIndexCallbackData *data, const char *fullpath,
|
|
|
|
time_t filestamp)
|
|
|
|
{
|
|
|
|
/* unconditionally reindex */
|
|
|
|
if (data->_reindex)
|
|
|
|
return TRUE;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-09-03 09:45:54 +02:00
|
|
|
/* it's not in the database yet (FIXME: GError)*/
|
|
|
|
if (!mu_store_contains_message (data->_store, fullpath, NULL))
|
2010-11-27 13:54:19 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* it's there, but it's not up to date */
|
|
|
|
if ((unsigned)filestamp >= (unsigned)data->_dirstamp)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE; /* index not needed */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2012-09-20 20:25:55 +02:00
|
|
|
insert_or_update_maybe (const char *fullpath, const char *mdir,
|
2010-11-27 13:54:19 +01:00
|
|
|
time_t filestamp, MuIndexCallbackData *data,
|
|
|
|
gboolean *updated)
|
2011-08-29 22:38:55 +02:00
|
|
|
{
|
2013-10-13 18:58:25 +02:00
|
|
|
MuMsg *msg;
|
|
|
|
GError *err;
|
|
|
|
gboolean rv;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
*updated = FALSE;
|
2010-11-27 13:54:19 +01:00
|
|
|
if (!needs_index (data, fullpath, filestamp))
|
|
|
|
return MU_OK; /* nothing to do for this one */
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-11-25 20:49:25 +01:00
|
|
|
err = NULL;
|
2011-05-09 01:58:33 +02:00
|
|
|
msg = mu_msg_new_from_file (fullpath, mdir, &err);
|
2012-05-14 10:20:10 +02:00
|
|
|
if (!msg) {
|
2013-10-13 18:58:25 +02:00
|
|
|
if (!err)
|
|
|
|
g_warning ("error creating message object: %s",
|
|
|
|
fullpath);
|
|
|
|
else {
|
|
|
|
g_warning ("%s", err->message);
|
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
2012-05-14 10:20:10 +02:00
|
|
|
/* warn, then simply continue */
|
|
|
|
return MU_OK;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
/* we got a valid id; scan the message contents as well */
|
2012-05-14 10:20:10 +02:00
|
|
|
rv = mu_store_add_msg (data->_store, msg, &err);
|
2011-01-09 17:54:14 +01:00
|
|
|
mu_msg_unref (msg);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-05-14 10:20:10 +02:00
|
|
|
if (!rv) {
|
|
|
|
g_warning ("error storing message object: %s",
|
|
|
|
err ? err->message : "cause unknown");
|
2011-09-12 19:38:40 +02:00
|
|
|
g_clear_error (&err);
|
2012-05-14 10:20:10 +02:00
|
|
|
return MU_ERROR;
|
2011-09-12 19:38:40 +02:00
|
|
|
}
|
2012-05-14 10:20:10 +02:00
|
|
|
|
|
|
|
*updated = TRUE;
|
|
|
|
return MU_OK;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2012-01-01 12:53:18 +01:00
|
|
|
|
|
|
|
static MuError
|
2010-01-15 21:11:51 +01:00
|
|
|
run_msg_callback_maybe (MuIndexCallbackData *data)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError result;
|
2009-12-09 23:11:30 +01:00
|
|
|
|
2010-01-25 08:28:08 +01:00
|
|
|
if (!data || !data->_idx_msg_cb)
|
|
|
|
return MU_OK;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-25 08:28:08 +01:00
|
|
|
result = data->_idx_msg_cb (data->_stats, data->_user_data);
|
2011-08-29 22:38:55 +02:00
|
|
|
if (G_UNLIKELY(result != MU_OK && result != MU_STOP))
|
2010-11-30 21:20:27 +01:00
|
|
|
g_warning ("error in callback");
|
2010-01-25 08:28:08 +01:00
|
|
|
|
|
|
|
return result;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2012-09-20 20:25:55 +02:00
|
|
|
on_run_maildir_msg (const char *fullpath, const char *mdir,
|
2011-01-15 10:53:18 +01:00
|
|
|
struct stat *statbuf, MuIndexCallbackData *data)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError result;
|
2009-11-25 21:55:06 +01:00
|
|
|
gboolean updated;
|
2011-01-15 10:53:18 +01:00
|
|
|
|
|
|
|
/* protect against too big messages */
|
2011-01-15 12:27:41 +01:00
|
|
|
if (G_UNLIKELY(statbuf->st_size > data->_max_filesize)) {
|
|
|
|
g_warning ("ignoring because bigger than %u bytes: %s",
|
|
|
|
data->_max_filesize, fullpath);
|
2011-01-15 10:53:18 +01:00
|
|
|
return MU_OK; /* not an error */
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-15 21:11:51 +01:00
|
|
|
result = run_msg_callback_maybe (data);
|
2009-11-25 21:55:06 +01:00
|
|
|
if (result != MU_OK)
|
|
|
|
return result;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-15 10:53:18 +01:00
|
|
|
/* see if we need to update/insert anything...
|
|
|
|
* use the ctime, so any status change will be visible (perms,
|
|
|
|
* filename etc.)*/
|
|
|
|
result = insert_or_update_maybe (fullpath, mdir, statbuf->st_ctime,
|
|
|
|
data, &updated);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-15 10:53:18 +01:00
|
|
|
if (result == MU_OK && data && data->_stats) { /* update statistics */
|
2009-11-25 21:55:06 +01:00
|
|
|
++data->_stats->_processed;
|
2011-01-15 10:53:18 +01:00
|
|
|
updated ? ++data->_stats->_updated : ++data->_stats->_uptodate;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
static time_t
|
|
|
|
get_dir_timestamp (const char *path)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (stat (path, &statbuf) != 0) {
|
|
|
|
g_warning ("failed to stat %s: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statbuf.st_ctime;
|
|
|
|
}
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2011-08-29 22:38:55 +02:00
|
|
|
on_run_maildir_dir (const char* fullpath, gboolean enter,
|
2009-11-25 21:55:06 +01:00
|
|
|
MuIndexCallbackData *data)
|
|
|
|
{
|
2011-09-03 09:45:54 +02:00
|
|
|
GError *err;
|
2016-07-23 18:18:09 +02:00
|
|
|
|
2011-09-03 09:45:54 +02:00
|
|
|
err = NULL;
|
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
/* xapian stores a per-dir timestamp; we use this timestamp to determine
|
|
|
|
* whether a message is up-to-date
|
2009-11-25 21:55:06 +01:00
|
|
|
*/
|
2010-01-07 08:01:24 +01:00
|
|
|
if (enter) {
|
2009-11-25 21:55:06 +01:00
|
|
|
data->_dirstamp =
|
2011-09-03 09:45:54 +02:00
|
|
|
mu_store_get_timestamp (data->_store, fullpath, &err);
|
2016-07-23 18:18:09 +02:00
|
|
|
/* in 'lazy' mode, we only check the dir timestamp, and if it's
|
|
|
|
* up to date, we don't bother with this dir. This fails to
|
|
|
|
* account for messages below this dir that have merely
|
|
|
|
* _changed_ though */
|
|
|
|
if (data->_lazy_check && mu_maildir_is_leaf_dir(fullpath)) {
|
|
|
|
time_t dirstamp;
|
|
|
|
dirstamp = get_dir_timestamp (fullpath);
|
|
|
|
if (dirstamp <= data->_dirstamp) {
|
|
|
|
g_debug ("ignore %s (up-to-date)", fullpath);
|
|
|
|
return MU_IGNORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_debug ("entering %s", fullpath);
|
2010-01-07 08:01:24 +01:00
|
|
|
} else {
|
2011-01-12 22:11:51 +01:00
|
|
|
mu_store_set_timestamp (data->_store, fullpath,
|
2016-07-23 18:18:09 +02:00
|
|
|
time(NULL), &err);
|
|
|
|
g_debug ("leaving %s", fullpath);
|
2010-01-07 08:01:24 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-12-09 23:11:30 +01:00
|
|
|
if (data->_idx_dir_cb)
|
2011-08-29 22:38:55 +02:00
|
|
|
return data->_idx_dir_cb (fullpath, enter,
|
2009-11-25 21:55:06 +01:00
|
|
|
data->_user_data);
|
|
|
|
|
2011-09-03 09:45:54 +02:00
|
|
|
if (err) {
|
2015-04-22 20:06:31 +02:00
|
|
|
MU_WRITE_LOG ("%s: %s", __func__, err->message);
|
2011-09-03 09:45:54 +02:00
|
|
|
g_clear_error(&err);
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
return MU_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-20 20:25:55 +02:00
|
|
|
check_path (const char *path)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (path, FALSE);
|
2010-02-04 22:00:34 +01:00
|
|
|
|
|
|
|
if (!g_path_is_absolute (path)) {
|
|
|
|
g_warning ("%s: not an absolute path: %s",
|
2015-04-22 20:06:31 +02:00
|
|
|
__func__, path);
|
2010-02-04 22:00:34 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
if (access (path, R_OK) != 0) {
|
2011-08-29 22:38:55 +02:00
|
|
|
g_warning ("%s: cannot open '%s': %s",
|
2015-04-22 20:06:31 +02:00
|
|
|
__func__, path, strerror (errno));
|
2009-11-25 21:55:06 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
static void
|
2010-08-25 20:55:08 +02:00
|
|
|
init_cb_data (MuIndexCallbackData *cb_data, MuStore *xapian,
|
2016-07-23 18:18:09 +02:00
|
|
|
gboolean reindex, gboolean lazycheck,
|
|
|
|
guint max_filesize, MuIndexStats *stats,
|
2011-08-29 22:38:55 +02:00
|
|
|
MuIndexMsgCallback msg_cb, MuIndexDirCallback dir_cb,
|
2010-02-02 07:46:01 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
cb_data->_idx_msg_cb = msg_cb;
|
|
|
|
cb_data->_idx_dir_cb = dir_cb;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
cb_data->_user_data = user_data;
|
2011-01-15 12:27:41 +01:00
|
|
|
cb_data->_store = xapian;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
cb_data->_reindex = reindex;
|
|
|
|
cb_data->_lazy_check = lazycheck;
|
|
|
|
cb_data->_dirstamp = 0;
|
|
|
|
cb_data->_max_filesize = max_filesize;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
cb_data->_stats = stats;
|
|
|
|
if (cb_data->_stats)
|
|
|
|
memset (cb_data->_stats, 0, sizeof(MuIndexStats));
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
|
2011-01-15 12:27:41 +01:00
|
|
|
void
|
|
|
|
mu_index_set_max_msg_size (MuIndex *index, guint max_size)
|
|
|
|
{
|
|
|
|
g_return_if_fail (index);
|
|
|
|
|
|
|
|
if (max_size == 0)
|
2015-09-14 17:01:51 +02:00
|
|
|
index->_max_filesize = MU_INDEX_MAX_FILE_SIZE;
|
2011-01-15 12:27:41 +01:00
|
|
|
else
|
|
|
|
index->_max_filesize = max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2020-01-20 11:32:29 +01:00
|
|
|
mu_index_run (MuIndex *index, gboolean reindex, gboolean lazycheck,
|
2016-07-23 18:18:09 +02:00
|
|
|
MuIndexStats *stats,
|
2011-08-29 22:38:55 +02:00
|
|
|
MuIndexMsgCallback msg_cb, MuIndexDirCallback dir_cb,
|
2009-11-25 21:55:06 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
2020-01-20 11:32:29 +01:00
|
|
|
MuIndexCallbackData cb_data;
|
|
|
|
MuError rv;
|
|
|
|
const char *path;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
g_return_val_if_fail (index && index->_store, MU_ERROR);
|
2010-01-25 08:28:08 +01:00
|
|
|
g_return_val_if_fail (msg_cb, MU_ERROR);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2020-01-20 11:32:29 +01:00
|
|
|
path = mu_store_maildir (index->_store);
|
2010-01-15 21:11:51 +01:00
|
|
|
if (!check_path (path))
|
2010-01-04 19:19:32 +01:00
|
|
|
return MU_ERROR;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
if (!reindex && index->_needs_reindex) {
|
|
|
|
g_warning ("database not up-to-date; needs full reindex");
|
|
|
|
return MU_ERROR;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
init_cb_data (&cb_data, index->_store, reindex, lazycheck,
|
2011-01-15 12:27:41 +01:00
|
|
|
index->_max_filesize, stats,
|
2010-02-02 07:46:01 +01:00
|
|
|
msg_cb, dir_cb, user_data);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-01-06 00:25:19 +01:00
|
|
|
rv = mu_maildir_walk (path,
|
|
|
|
(MuMaildirWalkMsgCallback)on_run_maildir_msg,
|
|
|
|
(MuMaildirWalkDirCallback)on_run_maildir_dir,
|
2012-05-22 09:19:49 +02:00
|
|
|
reindex, /* re-index, ie. do a full update */
|
2010-01-06 00:25:19 +01:00
|
|
|
&cb_data);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
mu_store_flush (index->_store);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-06 00:25:19 +01:00
|
|
|
return rv;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2012-09-20 20:25:55 +02:00
|
|
|
on_stats_maildir_file (const char *fullpath, const char *mdir,
|
2011-08-29 22:38:55 +02:00
|
|
|
struct stat *statbuf,
|
2009-11-25 21:55:06 +01:00
|
|
|
MuIndexCallbackData *cb_data)
|
|
|
|
{
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError result;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
if (cb_data && cb_data->_idx_msg_cb)
|
2011-08-29 22:38:55 +02:00
|
|
|
result = cb_data->_idx_msg_cb (cb_data->_stats,
|
2009-11-25 21:55:06 +01:00
|
|
|
cb_data->_user_data);
|
|
|
|
else
|
|
|
|
result = MU_OK;
|
|
|
|
|
2009-12-09 23:11:30 +01:00
|
|
|
if (result == MU_OK) {
|
2009-11-25 21:55:06 +01:00
|
|
|
if (cb_data->_stats)
|
|
|
|
++cb_data->_stats->_processed;
|
|
|
|
return MU_OK;
|
2011-08-29 22:38:55 +02:00
|
|
|
}
|
2009-12-09 23:11:30 +01:00
|
|
|
|
|
|
|
return result; /* MU_STOP or MU_OK */
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2020-01-20 11:32:29 +01:00
|
|
|
mu_index_stats (MuIndex *index,
|
2009-11-25 21:55:06 +01:00
|
|
|
MuIndexStats *stats, MuIndexMsgCallback cb_msg,
|
|
|
|
MuIndexDirCallback cb_dir, void *user_data)
|
|
|
|
{
|
2020-01-20 11:32:29 +01:00
|
|
|
const char *path;
|
|
|
|
MuIndexCallbackData cb_data;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
g_return_val_if_fail (index, MU_ERROR);
|
2010-01-25 08:28:08 +01:00
|
|
|
g_return_val_if_fail (cb_msg, MU_ERROR);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2020-01-20 11:32:29 +01:00
|
|
|
path = mu_store_maildir (index->_store);
|
2010-01-15 21:11:51 +01:00
|
|
|
if (!check_path (path))
|
2010-01-04 19:19:32 +01:00
|
|
|
return MU_ERROR;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2009-12-05 15:38:47 +01:00
|
|
|
if (stats)
|
|
|
|
memset (stats, 0, sizeof(MuIndexStats));
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2015-09-14 17:01:51 +02:00
|
|
|
cb_data._idx_msg_cb = cb_msg;
|
|
|
|
cb_data._idx_dir_cb = cb_dir;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
cb_data._stats = stats;
|
|
|
|
cb_data._user_data = user_data;
|
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
cb_data._dirstamp = 0;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
return mu_maildir_walk (path,
|
|
|
|
(MuMaildirWalkMsgCallback)on_stats_maildir_file,
|
2012-05-22 09:19:49 +02:00
|
|
|
NULL, FALSE, &cb_data);
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
struct _CleanupData {
|
2016-07-23 18:18:09 +02:00
|
|
|
MuStore *_store;
|
|
|
|
MuIndexStats *_stats;
|
|
|
|
MuIndexCleanupDeleteCallback _cb;
|
|
|
|
void *_user_data;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
};
|
|
|
|
typedef struct _CleanupData CleanupData;
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2010-01-15 21:11:51 +01:00
|
|
|
foreach_doc_cb (const char* path, CleanupData *cudata)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2010-01-03 22:54:22 +01:00
|
|
|
if (access (path, R_OK) != 0) {
|
2011-01-15 12:27:41 +01:00
|
|
|
if (errno != EACCES)
|
2011-02-04 19:52:49 +01:00
|
|
|
g_debug ("cannot access %s: %s", path, strerror(errno));
|
2011-08-03 22:03:22 +02:00
|
|
|
if (!mu_store_remove_path (cudata->_store, path))
|
2011-01-15 12:27:41 +01:00
|
|
|
return MU_ERROR; /* something went wrong... bail out */
|
2010-01-04 19:19:32 +01:00
|
|
|
if (cudata->_stats)
|
|
|
|
++cudata->_stats->_cleaned_up;
|
2010-01-03 22:54:22 +01:00
|
|
|
}
|
|
|
|
|
2010-01-04 19:19:32 +01:00
|
|
|
if (cudata->_stats)
|
|
|
|
++cudata->_stats->_processed;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
if (!cudata->_cb)
|
|
|
|
return MU_OK;
|
2009-11-26 19:28:29 +01:00
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
return cudata->_cb (cudata->_stats, cudata->_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2010-01-03 22:54:22 +01:00
|
|
|
mu_index_cleanup (MuIndex *index, MuIndexStats *stats,
|
|
|
|
MuIndexCleanupDeleteCallback cb,
|
2011-09-03 09:45:54 +02:00
|
|
|
void *user_data, GError **err)
|
2010-01-03 22:54:22 +01:00
|
|
|
{
|
2016-07-23 18:18:09 +02:00
|
|
|
MuError rv;
|
|
|
|
CleanupData cudata;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
g_return_val_if_fail (index, MU_ERROR);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
cudata._store = index->_store;
|
2010-01-03 22:54:22 +01:00
|
|
|
cudata._stats = stats;
|
|
|
|
cudata._cb = cb;
|
|
|
|
cudata._user_data = user_data;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
rv = mu_store_foreach (index->_store,
|
2011-08-11 19:20:22 +02:00
|
|
|
(MuStoreForeachFunc)foreach_doc_cb,
|
2011-09-03 09:45:54 +02:00
|
|
|
&cudata, err);
|
|
|
|
|
2011-01-12 22:11:51 +01:00
|
|
|
mu_store_flush (index->_store);
|
2010-01-06 00:25:19 +01:00
|
|
|
|
2010-01-03 22:54:22 +01:00
|
|
|
return rv;
|
2010-01-03 12:41:32 +01:00
|
|
|
}
|
2010-01-04 19:19:32 +01:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_index_stats_clear (MuIndexStats *stats)
|
|
|
|
{
|
|
|
|
if (!stats)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
memset (stats, 0, sizeof(MuIndexStats));
|
|
|
|
return TRUE;
|
|
|
|
}
|