diff --git a/lib/mu-store-priv.hh b/lib/mu-store-priv.hh index 7c1d17ed..d9e36e68 100644 --- a/lib/mu-store-priv.hh +++ b/lib/mu-store-priv.hh @@ -177,7 +177,8 @@ public: mu_contacts_clear (_contacts); } - std::string get_uid_term (const char *path) const; + // not re-entrant; stays valid until called again + const char *get_uid_term (const char *path) const; MuContacts* contacts() { return _contacts; } diff --git a/lib/mu-store-read.cc b/lib/mu-store-read.cc index d2d8ed1b..191c4a2b 100644 --- a/lib/mu-store-read.cc +++ b/lib/mu-store-read.cc @@ -44,24 +44,16 @@ // note: not re-entrant -std::string +const char* _MuStore::get_uid_term (const char* path) const { - char real_path[PATH_MAX + 1]; + static char uid_term[64] = { '\0' }; + if (G_UNLIKELY(uid_term[0] == '\0')) + uid_term[0] = mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_UID); - static const std::string uid_prefix ( - 1, mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_UID)); + strncpy (uid_term + 1, mu_util_get_hash (path), sizeof(uid_term) - 1); - /* check profile to see if realpath is expensive; we need - * realpath here (and in mu-msg-file) to ensure that the same - * messages are only considered ones (ignore e.g. symlinks and - * '//' in paths) */ - - // note: realpath fails when there's no file at path - if (!realpath (path, real_path)) - strcpy (real_path, path); - - return std::string (uid_prefix + mu_util_get_hash (real_path)); + return uid_term; } @@ -72,7 +64,6 @@ mu_store_new_read_only (const char* xpath, GError **err) try { return new _MuStore (xpath); - } catch (const MuStoreError& merr) { mu_util_g_set_error (err, merr.mu_error(), "%s", merr.what().c_str()); diff --git a/lib/mu-util.c b/lib/mu-util.c index fd664b3d..fd05c9f7 100644 --- a/lib/mu-util.c +++ b/lib/mu-util.c @@ -559,25 +559,3 @@ mu_util_read_password (const char *prompt) } -const char* -mu_util_get_hash (const char* str) -{ - unsigned djbhash, bkdrhash, bkdrseed; - unsigned u; - static char hex[18]; - - g_return_val_if_fail (str, NULL); - - djbhash = 5381; - bkdrhash = 0; - bkdrseed = 1313; - - for(u = 0; str[u]; ++u) { - djbhash = ((djbhash << 5) + djbhash) + str[u]; - bkdrhash = bkdrhash * bkdrseed + str[u]; - } - - snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash); - - return hex; -} diff --git a/lib/mu-util.h b/lib/mu-util.h index 5e941321..87d44325 100644 --- a/lib/mu-util.h +++ b/lib/mu-util.h @@ -513,9 +513,26 @@ gboolean mu_util_g_set_error (GError **err, MuError errcode, const char *frm, .. * @return the hash as a static string, which stays valid until this * function is called again. */ -const char* mu_util_get_hash (const char* str); +static inline const char* +mu_util_get_hash (const char* str) +{ + unsigned djbhash, bkdrhash, bkdrseed; + unsigned u; + static char hex[18]; + djbhash = 5381; + bkdrhash = 0; + bkdrseed = 1313; + for(u = 0; str[u]; ++u) { + djbhash = ((djbhash << 5) + djbhash) + str[u]; + bkdrhash = bkdrhash * bkdrseed + str[u]; + } + + snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash); + + return hex; +} #define MU_COLOR_RED "\x1b[31m"