From 348d557d143745e1fd7b21ce17f6e13882ab8de8 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Sat, 19 May 2012 00:13:34 +2000 Subject: [PATCH] [PATCH 1/9] lib: Make directory document creation optional for _notmuch_directory_create --- 81/01a7ac639082fb5fd2a9b30f7f9c49d04bdc2e | 233 ++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 81/01a7ac639082fb5fd2a9b30f7f9c49d04bdc2e diff --git a/81/01a7ac639082fb5fd2a9b30f7f9c49d04bdc2e b/81/01a7ac639082fb5fd2a9b30f7f9c49d04bdc2e new file mode 100644 index 000000000..1a5001f8e --- /dev/null +++ b/81/01a7ac639082fb5fd2a9b30f7f9c49d04bdc2e @@ -0,0 +1,233 @@ +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 E435A429E38 + for ; Thu, 17 May 2012 21:13:57 -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 5QKL3f7IKhu0 for ; + Thu, 17 May 2012 21:13:55 -0700 (PDT) +Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU + [18.7.68.36]) + by olra.theworths.org (Postfix) with ESMTP id 367DB431FDB + for ; Thu, 17 May 2012 21:13:53 -0700 (PDT) +X-AuditID: 12074424-b7fae6d000000906-f7-4fb5cc809d4b +Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) + by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP + id 64.51.02310.08CC5BF4; Fri, 18 May 2012 00:13:52 -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 q4I4Dpb8021449; + Fri, 18 May 2012 00:13:51 -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 q4I4Dn14026781 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Fri, 18 May 2012 00:13:51 -0400 (EDT) +Received: from amthrax by drake.mit.edu with local (Exim 4.77) + (envelope-from ) + id 1SVEZZ-0000yx-OQ; Fri, 18 May 2012 00:13:49 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH 1/9] lib: Make directory document creation optional for + _notmuch_directory_create +Date: Fri, 18 May 2012 00:13:34 -0400 +Message-Id: <1337314423-3702-2-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1337314423-3702-1-git-send-email-amdragon@mit.edu> +References: <1337314423-3702-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrAIsWRmVeSWpSXmKPExsUixG6nottwZqu/wYSDohbXb85kdmD0eLbq + FnMAYxSXTUpqTmZZapG+XQJXxrQZy9kLbutV/Hx2hLWB8YRqFyMnh4SAicSrmR9YIGwxiQv3 + 1rN1MXJxCAnsY5S4+f0GK4SzgVHiyZ0FYFVCAveZJOZMyYBIzGKU+HXkOyNIgk1AQ2Lb/uVg + toiAtMTOu7OBujk4mAXUJP50qYCYwgKJEktfxYBUsAioSsyc/JUVxOYVsJe40LyHFeIIeYmn + 9/vYQMo5BRwkph2PhthqL/HvQjPLBEb+BYwMqxhlU3KrdHMTM3OKU5N1i5MT8/JSi3TN9XIz + S/RSU0o3MYIDxkVlB2PzIaVDjAIcjEo8vALzt/oLsSaWFVfmHmKU5GBSEuX9fhIoxJeUn1KZ + kVicEV9UmpNafIhRgoNZSYRXthsox5uSWFmVWpQPk5LmYFES59XQeucnJJCeWJKanZpakFoE + k5Xh4FCS4N10GqhRsCg1PbUiLTOnBCHNxMEJMpwHaHgmSA1vcUFibnFmOkT+FKOilDhvGUhC + ACSRUZoH1wuL6FeM4kCvCPNOAKniASYDuO5XQIOZgAaX5W4CGVySiJCSamA8cOzpK9bQpLsM + 7h8/S30L3fPyinThIv3aU+ynq8znrA8VFNXXznEu2KjsMuE/g9tZ3Z26Coe6c2K3dy06G10a + OpXjVtG6/+r2Ajvufnsak9TnZ+nmlyYT1WjxzLat7Ztz9O+6bzub7P48PNIT3nfv7JNdIjkz + CwsOMG89/P+D6YuNbUV+Hy8qsRRnJBpqMRcVJwIAiYfN1cMCAAA= +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: Fri, 18 May 2012 04:13:58 -0000 + +Previously this function would create directory documents if they +didn't exist. As a result, it could only be used on writable +databases. This adds an argument to make creation optional and to +make this function work on read-only databases. We use a flag +argument to avoid a bare boolean and to permit future expansion. + +Both callers have been updated, but currently retain the old behavior. +We'll take advantage of the new argument in the following patches. +--- + lib/database.cc | 6 +++--- + lib/directory.cc | 33 ++++++++++++++++++++++++++++----- + lib/notmuch-private.h | 8 ++++++++ + 3 files changed, 39 insertions(+), 8 deletions(-) + +diff --git a/lib/database.cc b/lib/database.cc +index f8c4a7d..df996a9 100644 +--- a/lib/database.cc ++++ b/lib/database.cc +@@ -956,7 +956,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, + document.get_value (NOTMUCH_VALUE_TIMESTAMP)); + + directory = _notmuch_directory_create (notmuch, term.c_str() + 10, +- &status); ++ NOTMUCH_FIND_CREATE, &status); + notmuch_directory_set_mtime (directory, mtime); + notmuch_directory_destroy (directory); + } +@@ -1210,7 +1210,7 @@ _notmuch_database_find_directory_id (notmuch_database_t *notmuch, + return NOTMUCH_STATUS_SUCCESS; + } + +- directory = _notmuch_directory_create (notmuch, path, &status); ++ directory = _notmuch_directory_create (notmuch, path, NOTMUCH_FIND_CREATE, &status); + if (status) { + *directory_id = -1; + return status; +@@ -1320,7 +1320,7 @@ notmuch_database_get_directory (notmuch_database_t *notmuch, + return NOTMUCH_STATUS_READ_ONLY_DATABASE; + + try { +- *directory = _notmuch_directory_create (notmuch, path, &status); ++ *directory = _notmuch_directory_create (notmuch, path, NOTMUCH_FIND_CREATE, &status); + } catch (const Xapian::Error &error) { + fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n", + error.get_msg().c_str()); +diff --git a/lib/directory.cc b/lib/directory.cc +index 70e1693..83bb19b 100644 +--- a/lib/directory.cc ++++ b/lib/directory.cc +@@ -82,28 +82,41 @@ find_directory_document (notmuch_database_t *notmuch, + return NOTMUCH_PRIVATE_STATUS_SUCCESS; + } + ++/* Find or create a directory document. ++ * ++ * 'path' should be a path relative to the path of 'database', or else ++ * should be an absolute path with initial components that match the ++ * path of 'database'. ++ * ++ * If (flags & NOTMUCH_FIND_CREATE), then the directory document will ++ * be created if it does not exist. Otherwise, if the directory ++ * document does not exist, *status_ret is set to ++ * NOTMUCH_STATUS_SUCCESS and this returns NULL. ++ */ + notmuch_directory_t * + _notmuch_directory_create (notmuch_database_t *notmuch, + const char *path, ++ notmuch_find_flags_t flags, + notmuch_status_t *status_ret) + { + Xapian::WritableDatabase *db; + notmuch_directory_t *directory; + notmuch_private_status_t private_status; + const char *db_path; ++ notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE); + + *status_ret = NOTMUCH_STATUS_SUCCESS; + + path = _notmuch_database_relative_path (notmuch, path); + +- if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) ++ if (create && notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) + INTERNAL_ERROR ("Failure to ensure database is writable"); + +- db = static_cast (notmuch->xapian_db); +- + directory = talloc (notmuch, notmuch_directory_t); +- if (unlikely (directory == NULL)) ++ if (unlikely (directory == NULL)) { ++ *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY; + return NULL; ++ } + + directory->notmuch = notmuch; + +@@ -122,6 +135,13 @@ _notmuch_directory_create (notmuch_database_t *notmuch, + directory->document_id = directory->doc.get_docid (); + + if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) { ++ if (!create) { ++ notmuch_directory_destroy (directory); ++ directory = NULL; ++ *status_ret = NOTMUCH_STATUS_SUCCESS; ++ goto DONE; ++ } ++ + void *local = talloc_new (directory); + const char *parent, *basename; + Xapian::docid parent_id; +@@ -145,6 +165,8 @@ _notmuch_directory_create (notmuch_database_t *notmuch, + directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP, + Xapian::sortable_serialise (0)); + ++ db = static_cast (notmuch->xapian_db); ++ + directory->document_id = _notmuch_database_generate_doc_id (notmuch); + db->replace_document (directory->document_id, directory->doc); + talloc_free (local); +@@ -158,10 +180,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch, + error.get_msg().c_str()); + notmuch->exception_reported = TRUE; + notmuch_directory_destroy (directory); ++ directory = NULL; + *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION; +- return NULL; + } + ++ DONE: + if (db_path != path) + free ((char *) db_path); + +diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h +index 3886e0c..538274f 100644 +--- a/lib/notmuch-private.h ++++ b/lib/notmuch-private.h +@@ -146,6 +146,13 @@ typedef enum _notmuch_private_status { + : \ + (notmuch_status_t) private_status) + ++/* Flags shared by various lookup functions. */ ++typedef enum _notmuch_find_flags { ++ /* If set, create the necessary document (or documents) if they ++ * are missing. Requires a read/write database. */ ++ NOTMUCH_FIND_CREATE = 1<<0, ++} notmuch_find_flags_t; ++ + typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t; + + typedef struct _notmuch_string_list notmuch_string_list_t; +@@ -206,6 +213,7 @@ _notmuch_database_filename_to_direntry (void *ctx, + notmuch_directory_t * + _notmuch_directory_create (notmuch_database_t *notmuch, + const char *path, ++ notmuch_find_flags_t flags, + notmuch_status_t *status_ret); + + unsigned int +-- +1.7.10 + -- 2.26.2