2011-05-22 16:39:10 +02:00
|
|
|
/* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
/*
|
2011-01-05 19:38:33 +01:00
|
|
|
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-01-23 19:50:06 +01:00
|
|
|
**
|
|
|
|
** 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 of the License, 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
#if HAVE_CONFIG_H
|
2010-01-23 19:50:06 +01:00
|
|
|
#include "config.h"
|
2010-08-25 20:40:07 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
#include <cstring>
|
2010-01-23 20:57:57 +01:00
|
|
|
#include <errno.h>
|
2011-01-05 19:38:33 +01:00
|
|
|
#include <xapian.h>
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
#include "mu-util.h"
|
|
|
|
|
2011-01-12 22:13:03 +01:00
|
|
|
|
2011-05-22 16:39:10 +02:00
|
|
|
static char*
|
|
|
|
xapian_get_metadata (const gchar *xpath, const gchar *key)
|
2010-01-23 19:50:06 +01:00
|
|
|
{
|
2010-01-23 20:57:57 +01:00
|
|
|
g_return_val_if_fail (xpath, NULL);
|
2011-01-12 22:13:03 +01:00
|
|
|
g_return_val_if_fail (key, NULL);
|
|
|
|
|
2010-01-31 11:14:25 +01:00
|
|
|
if (!access(xpath, F_OK) == 0) {
|
|
|
|
g_warning ("cannot access %s: %s", xpath, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-23 20:57:57 +01:00
|
|
|
|
2010-01-31 11:14:25 +01:00
|
|
|
try {
|
2010-01-23 19:50:06 +01:00
|
|
|
Xapian::Database db (xpath);
|
2011-01-12 22:13:03 +01:00
|
|
|
const std::string val(db.get_metadata (key));
|
|
|
|
return val.empty() ? NULL : g_strdup (val.c_str());
|
2010-01-23 19:50:06 +01:00
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK;
|
2011-01-12 22:13:03 +01:00
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-12 22:13:03 +01:00
|
|
|
char*
|
|
|
|
mu_util_xapian_dbversion (const gchar *xpath)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (xpath, NULL);
|
|
|
|
|
2011-05-22 16:39:10 +02:00
|
|
|
return xapian_get_metadata (xpath, MU_STORE_VERSION_KEY);
|
2011-01-12 22:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_xapian_needs_upgrade (const gchar *xpath)
|
|
|
|
{
|
|
|
|
char *version;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (xpath, TRUE);
|
|
|
|
|
|
|
|
version = mu_util_xapian_dbversion (xpath);
|
|
|
|
|
|
|
|
if (g_strcmp0 (version, MU_XAPIAN_DB_VERSION) == 0)
|
|
|
|
rv = FALSE;
|
|
|
|
else
|
|
|
|
rv = TRUE;
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
g_free (version);
|
|
|
|
|
2011-01-12 22:13:03 +01:00
|
|
|
return rv;
|
2010-01-23 19:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 11:14:25 +01:00
|
|
|
gboolean
|
2011-01-12 22:13:03 +01:00
|
|
|
mu_util_xapian_is_empty (const gchar* xpath)
|
2010-01-31 11:14:25 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (xpath, TRUE);
|
|
|
|
|
|
|
|
/* it's 'empty' (non-existant) */
|
|
|
|
if (access(xpath, F_OK) != 0 && errno == ENOENT)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Xapian::Database db (xpath);
|
|
|
|
return db.get_doccount() == 0 ? TRUE : FALSE;
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-01-23 20:57:57 +01:00
|
|
|
gboolean
|
2011-05-24 20:36:55 +02:00
|
|
|
mu_util_xapian_clear (const gchar *xpath,
|
|
|
|
const char *ccache)
|
2010-01-23 20:57:57 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (xpath, FALSE);
|
2011-05-24 20:36:55 +02:00
|
|
|
g_return_val_if_fail (ccache, FALSE);
|
2010-01-23 20:57:57 +01:00
|
|
|
|
|
|
|
try {
|
2011-05-24 20:36:55 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* clear the database */
|
2010-01-23 20:57:57 +01:00
|
|
|
Xapian::WritableDatabase db
|
|
|
|
(xpath, Xapian::DB_CREATE_OR_OVERWRITE);
|
|
|
|
db.flush ();
|
2010-01-23 23:50:23 +01:00
|
|
|
MU_WRITE_LOG ("emptied database %s", xpath);
|
2011-05-24 20:36:55 +02:00
|
|
|
|
|
|
|
/* clear the contacts cache; this is not totally
|
|
|
|
* fail-safe, as some other process may still have it
|
|
|
|
* open... */
|
|
|
|
rv = unlink (ccache);
|
|
|
|
if (rv != 0 && errno != ENOENT) {
|
|
|
|
g_warning ("failed to remove contacts-cache: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-23 23:50:23 +01:00
|
|
|
|
2010-01-23 20:57:57 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-23 19:50:06 +01:00
|
|
|
|
2011-01-14 06:47:19 +01:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_xapian_is_locked (const gchar *xpath)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (xpath, FALSE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Xapian::WritableDatabase db (xpath, Xapian::DB_OPEN);
|
|
|
|
} catch (const Xapian::DatabaseLockError& xer) {
|
|
|
|
return TRUE;
|
|
|
|
} catch (const Xapian::Error &xer) {
|
2011-05-22 16:39:10 +02:00
|
|
|
g_warning ("%s: error: %s", __FUNCTION__,
|
|
|
|
xer.get_msg().c_str());
|
2011-01-14 06:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|