Rework getting database version (#469)

It seems we don't get the correct database version in some case with
some compilers, optimization flags. Suspecting some stale ->c_str().
This commit is contained in:
djcb
2014-09-23 07:32:39 +03:00
parent 7888ba38f0
commit 3c7061338d
3 changed files with 17 additions and 17 deletions

View File

@ -111,6 +111,7 @@ public:
_processed = 0; _processed = 0;
_read_only = read_only; _read_only = read_only;
_ref_count = 1; _ref_count = 1;
_version = NULL;
} }
void set_my_addresses (const char **my_addresses) { void set_my_addresses (const char **my_addresses) {
@ -128,18 +129,17 @@ public:
} }
void check_set_version () { void check_set_version () {
/* check version...*/ if (_version)
gchar *version; return;
version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL); _version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL);
if (!version) if (!_version) {
mu_store_set_metadata (this, MU_STORE_VERSION_KEY, mu_store_set_metadata (this, MU_STORE_VERSION_KEY,
MU_STORE_SCHEMA_VERSION, NULL); MU_STORE_SCHEMA_VERSION, NULL);
else if (g_strcmp0 (version, MU_STORE_SCHEMA_VERSION) != 0) { _version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL);
g_free (version);
} else if (g_strcmp0 (_version, MU_STORE_SCHEMA_VERSION) != 0)
throw MuStoreError (MU_ERROR_XAPIAN_VERSION_MISMATCH, throw MuStoreError (MU_ERROR_XAPIAN_VERSION_MISMATCH,
"the database needs a rebuild"); "the database needs a rebuild");
} else
g_free (version);
} }
~_MuStore () { ~_MuStore () {
@ -151,6 +151,7 @@ public:
if (!_read_only) if (!_read_only)
mu_store_flush (this); mu_store_flush (this);
g_free (_version);
mu_str_free_list (_my_addresses); mu_str_free_list (_my_addresses);
MU_WRITE_LOG ("closing xapian database with %d document(s)", MU_WRITE_LOG ("closing xapian database with %d document(s)",
@ -180,14 +181,14 @@ public:
MuContacts* contacts() { return _contacts; } MuContacts* contacts() { return _contacts; }
const std::string version () const { const char *version () const {
char *v = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL); if (!_version)
_version = v; _version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL);
g_free (v);
return _version; return _version;
} }
void set_version (const char *version) { void set_version (const char *version) {
g_clear_pointer (&_version, g_free);
mu_store_set_metadata (this, MU_STORE_VERSION_KEY, version, NULL); mu_store_set_metadata (this, MU_STORE_VERSION_KEY, version, NULL);
} }
@ -245,7 +246,7 @@ private:
MuContacts *_contacts; MuContacts *_contacts;
std::string _path; std::string _path;
mutable std::string _version; mutable char *_version;
Xapian::Database *_db; Xapian::Database *_db;
bool _read_only; bool _read_only;

View File

@ -113,7 +113,8 @@ const char*
mu_store_version (const MuStore *store) mu_store_version (const MuStore *store)
{ {
g_return_val_if_fail (store, NULL); g_return_val_if_fail (store, NULL);
return store->version().c_str();
return store->version();
} }

View File

@ -98,8 +98,6 @@ mu_store_database_version (const gchar *xpath)
} }
gboolean gboolean
mu_store_database_is_locked (const gchar *xpath) mu_store_database_is_locked (const gchar *xpath)
{ {