From 91cbdd6b71d438b4ffa206dad7e023e5379195fb Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 14 Jan 2011 07:47:19 +0200 Subject: [PATCH] * add mu_util_xapian_is_locked and use it --- src/mu-util-db.cc | 16 ++++++++++++++++ src/mu-util.h | 16 +++++++++++++++- toys/mug2/mug.cc | 3 +++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/mu-util-db.cc b/src/mu-util-db.cc index 609770f5..c79f3967 100644 --- a/src/mu-util-db.cc +++ b/src/mu-util-db.cc @@ -140,3 +140,19 @@ mu_util_xapian_clear (const gchar *xpath) return FALSE; } + +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) { + g_warning ("%s: error: %s", __FUNCTION__, xer.get_msg().c_str()); + } + + return FALSE; +} diff --git a/src/mu-util.h b/src/mu-util.h index 90e61092..cb9d773a 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -184,6 +184,16 @@ gboolean mu_util_xapian_is_empty (const gchar *xpath); gboolean mu_util_xapian_clear (const gchar *xpath); +/** + * check if the database is locked for writing + * + * @param xpath path to a xapian database + * + * @return TRUE if it is locked, FALSE otherwise (or in case of error) + */ +gboolean mu_util_xapian_is_locked (const gchar *xpath); + + /** * convert a string array in to a string, with the elements separated * by ' ' @@ -252,10 +262,14 @@ unsigned char mu_util_get_dtype_with_lstat (const char *path); g_set_error ((GE),0,MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK, \ "%s: xapian error '%s'", \ __FUNCTION__, xerr.get_msg().c_str()); \ - } catch (const Xapian::DatabaseCorruptError &xerr ) { \ + } catch (const Xapian::DatabaseCorruptError &xerr) { \ g_set_error ((GE),0,MU_ERROR_XAPIAN_CORRUPTION, \ "%s: xapian error '%s'", \ __FUNCTION__, xerr.get_msg().c_str()); \ + } catch (const Xapian::DatabaseError &xerr) { \ + g_set_error ((GE),0,MU_ERROR_XAPIAN, \ + "%s: xapian error '%s'", \ + __FUNCTION__, xerr.get_msg().c_str()); \ } catch (const Xapian::Error &xerr) { \ g_set_error ((GE),0,(E), "%s: xapian error '%s'", \ __FUNCTION__, xerr.get_msg().c_str()); \ diff --git a/toys/mug2/mug.cc b/toys/mug2/mug.cc index c40878e9..63ffbecf 100644 --- a/toys/mug2/mug.cc +++ b/toys/mug2/mug.cc @@ -62,6 +62,9 @@ reindex (MugData *mugdata) MuIndex *midx; GError *err; + if (mu_util_xapian_is_locked (mu_runtime_xapian_dir())) + return; + err = NULL; midx = mu_index_new (mu_runtime_xapian_dir(), 0, &err); if (!midx) {