Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 1A1A5429E2F for ; Sat, 11 Jun 2011 13:07:06 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YTonp0oUlZXf for ; Sat, 11 Jun 2011 13:07:05 -0700 (PDT) Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU [18.7.68.37]) by olra.theworths.org (Postfix) with ESMTP id 66688429E2D for ; Sat, 11 Jun 2011 13:07:05 -0700 (PDT) X-AuditID: 12074425-b7b82ae000000a2a-92-4df3cad1dd38 Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP id 51.75.02602.1DAC3FD4; Sat, 11 Jun 2011 16:06:41 -0400 (EDT) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id p5BK74c8030852; Sat, 11 Jun 2011 16:07:04 -0400 Received: from drake.mit.edu (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com [209.6.116.242]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id p5BK73aK006029 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sat, 11 Jun 2011 16:07:04 -0400 (EDT) Received: from amthrax by drake.mit.edu with local (Exim 4.76) (envelope-from ) id 1QVUSV-0000IS-PX; Sat, 11 Jun 2011 16:07:03 -0400 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH 11/17] lib: Add an API to find a message by filename. Date: Sat, 11 Jun 2011 16:04:37 -0400 Message-Id: <1307822683-848-12-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.5.1 In-Reply-To: <1307822683-848-1-git-send-email-amdragon@mit.edu> References: <87ei34rnc5.fsf@yoom.home.cworth.org> <1307822683-848-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrPIsWRmVeSWpSXmKPExsUixG6nonvx1Gdfgy8zxSyu35zJ7MDo8WzV LeYAxigum5TUnMyy1CJ9uwSujEebD7MXXFeo+HHwI3MD4zPJLkZODgkBE4mD3x4yQ9hiEhfu rWfrYuTiEBLYxyhx6P85dghnA6PE5llzWSCc+0wSH05PYIRw5jNKfNm0khGkn01AQ2Lb/uVg toiAtMTOu7NZuxg5OJgF1CT+dKmAhIUFXCXefAVZwcnBIqAqcWzuVBYQm1fAXmLJ/ntQZyhI XLkyDyzOCRSfd/AqmC0kkCax5NZu9gmM/AsYGVYxyqbkVunmJmbmFKcm6xYnJ+blpRbpWujl ZpbopaaUbmIEh42L6g7GCYeUDjEKcDAq8fAqrv3sK8SaWFZcmXuIUZKDSUmUV+cEUIgvKT+l MiOxOCO+qDQntfgQowQHs5II7/r2T75CvCmJlVWpRfkwKWkOFiVx3vmS6r5CAumJJanZqakF qUUwWRkODiUJXklgfAgJFqWmp1akZeaUIKSZODhBhvMADT96EqiGt7ggMbc4Mx0if4pRl+Px hk2HGIVY8vLzUqXEee+DFAmAFGWU5sHNgcX7K0ZxoLeEeX+AVPEAUwXcpFdAS5iAlgiUgi0p SURISTUwrp9wr6lF48HbJDf+qc08tmV/Dj387j+rj+nqvHRBLe+VXgV/dJzXrA+a0PTohL/E ntmz3pimn1BjvMz/aZbvxe2SZq6JJRcufnT3Eqsze2rcbNO//FQl9+7W95VtDCs9ZvTvaDOY 6jdj25+JzxNl/hRrBhxS/HsxanHv7riMH5N0nPZw5x/9pcRSnJFoqMVcVJwIAM6tA4rSAgAA Cc: Austin Clements X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 Jun 2011 20:07:06 -0000 notmuch_database_find_message_by_filename is mostly stolen from notmuch_database_remove_message, so this patch also vastly simplfies the latter using the former. This API is also useful in its own right and will be used in a later patch for eager maildir flag synchronization. --- lib/database.cc | 49 ++++++++++++++++++++++++++----------------------- lib/notmuch.h | 16 ++++++++++++++++ 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/database.cc b/lib/database.cc index 9886622..daa619d 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1747,57 +1747,60 @@ notmuch_status_t notmuch_database_remove_message (notmuch_database_t *notmuch, const char *filename) { + notmuch_message_t *message = + notmuch_database_find_message_by_filename (notmuch, filename); + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + + if (message) { + status = _notmuch_message_remove_filename (message, filename); + if (status == NOTMUCH_STATUS_SUCCESS) + _notmuch_message_delete (message); + else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) + _notmuch_message_sync (message); + } + + return status; +} + +notmuch_message_t * +notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, + const char *filename) +{ void *local; const char *prefix = _find_prefix ("file-direntry"); char *direntry, *term; Xapian::PostingIterator i, end; - Xapian::Document document; + notmuch_message_t *message = NULL; notmuch_status_t status; - status = _notmuch_database_ensure_writable (notmuch); - if (status) - return status; - local = talloc_new (notmuch); try { - status = _notmuch_database_filename_to_direntry (local, notmuch, filename, &direntry); if (status) - return status; + return NULL; term = talloc_asprintf (local, "%s%s", prefix, direntry); find_doc_ids_for_term (notmuch, term, &i, &end); - for ( ; i != end; i++) { - Xapian::TermIterator j; - notmuch_message_t *message; + if (i != end) { notmuch_private_status_t private_status; - message = _notmuch_message_create (local, notmuch, + message = _notmuch_message_create (notmuch, notmuch, *i, &private_status); - if (message == NULL) - return COERCE_STATUS (private_status, - "Inconsistent document ID in datbase."); - - status = _notmuch_message_remove_filename (message, filename); - if (status == NOTMUCH_STATUS_SUCCESS) - _notmuch_message_delete (message); - else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) - _notmuch_message_sync (message); } } catch (const Xapian::Error &error) { - fprintf (stderr, "Error: A Xapian exception occurred removing message: %s\n", + fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n", error.get_msg().c_str()); notmuch->exception_reported = TRUE; - status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + message = NULL; } talloc_free (local); - return status; + return message; } notmuch_string_list_t * diff --git a/lib/notmuch.h b/lib/notmuch.h index 830aa41..9ae260e 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -356,6 +356,22 @@ notmuch_message_t * notmuch_database_find_message (notmuch_database_t *database, const char *message_id); +/* Find a message with the given filename. + * + * If the database contains a message with the given filename, then a + * new notmuch_message_t object is returned. The caller should call + * notmuch_message_destroy when done with the message. + * + * This function returns NULL in the following situations: + * + * * No message is found with the given filename + * * An out-of-memory situation occurs + * * A Xapian exception occurs + */ +notmuch_message_t * +notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, + const char *filename); + /* Return a list of all tags found in the database. * * This function creates a list of all tags found in the database. The -- 1.7.5.1