From 10409350070c0bf31ea0c66e833c14c8b342065f Mon Sep 17 00:00:00 2001 From: Austin Ray Date: Sat, 3 Nov 2018 21:11:50 -0400 Subject: [PATCH] notmuch: single point for closing database There are three points where a database is closed. Each point duplicates the code for different versions of notmuch. Implemented a new method for the version-specific notmuch code is handled. This method takes in a database and closes it. --- notmuch/mutt_notmuch.c | 12 ++---------- notmuch/nm_db.c | 34 ++++++++++++++++++++-------------- notmuch/notmuch_private.h | 1 + 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/notmuch/mutt_notmuch.c b/notmuch/mutt_notmuch.c index 505bfac5e..b130d3c8f 100644 --- a/notmuch/mutt_notmuch.c +++ b/notmuch/mutt_notmuch.c @@ -116,11 +116,7 @@ void nm_adata_free(void **ptr) struct NmAccountData *adata = *ptr; if (adata->db) { -#ifdef NOTMUCH_API_3 - notmuch_database_destroy(adata->db); -#else - notmuch_database_close(adata->db); -#endif + nm_db_free(adata->db); adata->db = NULL; } @@ -1962,11 +1958,7 @@ int nm_nonctx_get_count(struct Mailbox *m) done: if (db) { -#ifdef NOTMUCH_API_3 - notmuch_database_destroy(db); -#else - notmuch_database_close(db); -#endif + nm_db_free(db); mutt_debug(1, "nm: count close DB\n"); } url_free(&url); diff --git a/notmuch/nm_db.c b/notmuch/nm_db.c index ac8bf151f..8f0081006 100644 --- a/notmuch/nm_db.c +++ b/notmuch/nm_db.c @@ -47,10 +47,11 @@ const char *nm_db_get_filename(struct Mailbox *m) return NULL; char *db_filename = mdata->db_url.path ? mdata->db_url.path : NmDefaultUri; - if (!db_filename) - db_filename = Folder; - if (!db_filename) + if (!db_filename && !Folder) return NULL; + + db_filename = Folder; + if (nm_path_probe(db_filename, NULL) == MUTT_NOTMUCH) db_filename += NmUriProtocolLen; @@ -135,12 +136,8 @@ notmuch_database_t *nm_db_do_open(const char *filename, bool writable, bool verb */ notmuch_database_t *nm_db_get(struct Mailbox *m, bool writable) { - if (!m || (m->magic != MUTT_NOTMUCH)) - return NULL; - struct Account *a = m->account; - if (!a) - return NULL; - struct NmAccountData *adata = a->adata; + struct NmAccountData *adata = nm_adata_get(m); + if (!adata) return NULL; @@ -168,16 +165,25 @@ int nm_db_release(struct Mailbox *m) return -1; mutt_debug(1, "nm: db close\n"); -#ifdef NOTMUCH_API_3 - notmuch_database_destroy(adata->db); -#else - notmuch_database_close(adata->db); -#endif + nm_db_free(adata->db); adata->db = NULL; adata->longrun = false; return 0; } +/** + * nm_db_free - decoupled way to close a Notmuch database + * @param db Notmuch database + */ +void nm_db_free(notmuch_database_t *db) +{ +#ifdef NOTMUCH_API_3 + notmuch_database_destroy(db); +#else + notmuch_database_close(db); +#endif +} + /** * nm_db_trans_begin - Start a Notmuch database transaction * @param m Mailbox diff --git a/notmuch/notmuch_private.h b/notmuch/notmuch_private.h index 3d537d632..2cbc753f8 100644 --- a/notmuch/notmuch_private.h +++ b/notmuch/notmuch_private.h @@ -96,6 +96,7 @@ struct NmEmailData void nm_db_debug_check (struct Mailbox *m); notmuch_database_t *nm_db_do_open (const char *filename, bool writable, bool verbose); +void nm_db_free (notmuch_database_t *db); const char * nm_db_get_filename(struct Mailbox *m); int nm_db_get_mtime (struct Mailbox *m, time_t *mtime); notmuch_database_t *nm_db_get (struct Mailbox *m, bool writable); -- 2.40.0