From dd679f43aa9cf2a89ca7fe902d85c313d5aef333 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Mon, 14 May 2012 15:57:05 +2000 Subject: [PATCH] [PATCH 1/5] lib/cli: Make notmuch_database_get_directory return a status code --- e1/9720e70f6e08441a44516650017dbe1a6acf8e | 243 ++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 e1/9720e70f6e08441a44516650017dbe1a6acf8e diff --git a/e1/9720e70f6e08441a44516650017dbe1a6acf8e b/e1/9720e70f6e08441a44516650017dbe1a6acf8e new file mode 100644 index 000000000..937922e6a --- /dev/null +++ b/e1/9720e70f6e08441a44516650017dbe1a6acf8e @@ -0,0 +1,243 @@ +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 D95CE431FCB + for ; Sun, 13 May 2012 12:57:24 -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 ufcnFKicvW9i for ; + Sun, 13 May 2012 12:57:23 -0700 (PDT) +Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU + [18.7.68.34]) + by olra.theworths.org (Postfix) with ESMTP id EF138431FC3 + for ; Sun, 13 May 2012 12:57:22 -0700 (PDT) +X-AuditID: 12074422-b7fd66d0000008f9-a7-4fb012227241 +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP + id 75.24.02297.22210BF4; Sun, 13 May 2012 15:57:22 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q4DJvKsJ023343; + Sun, 13 May 2012 15:57:20 -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 q4DJvF49022907 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sun, 13 May 2012 15:57:16 -0400 (EDT) +Received: from amthrax by drake.mit.edu with local (Exim 4.77) + (envelope-from ) + id 1STeup-0003en-L8; Sun, 13 May 2012 15:57:15 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH 1/5] lib/cli: Make notmuch_database_get_directory return a + status code +Date: Sun, 13 May 2012 15:57:05 -0400 +Message-Id: <1336939029-13969-2-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1336939029-13969-1-git-send-email-amdragon@mit.edu> +References: <1336939029-13969-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFrrNIsWRmVeSWpSXmKPExsUixG6nrqsktMHfoPuTiMXs1h9MFjdauxkt + mqY7W1y/OZPZ4s3KeawOrB6Hvy5k8Zh4/jSbx637r9k9nq26xeyx5dB75gDWKC6blNSczLLU + In27BK6MxsNOBTcMK65ffMbUwNiq0cXIySEhYCKxoekHG4QtJnHh3nogm4tDSGAfo8SfyW9Z + IJwNjBILV05lgnDuM0lMvriBEcKZzygx89hBsH42AQ2JbfuXM4LYIgLSEjvvzmYFKWIW6GKU + +P2mmRkkISwQJrH8RwtYEYuAqkTvrf1gcV4BB4mTD9qZIQ6Rl3h6vw9sKKeAo8SyKwvB4kIg + NR//Mk9g5F/AyLCKUTYlt0o3NzEzpzg1Wbc4OTEvL7VI11QvN7NELzWldBMjOAxdlHYw/jyo + dIhRgINRiYd3geh6fyHWxLLiytxDjJIcTEqivGmCG/yF+JLyUyozEosz4otKc1KLDzFKcDAr + ifCufwhUzpuSWFmVWpQPk5LmYFES51XXeucnJJCeWJKanZpakFoEk5Xh4FCS4M0FGSpYlJqe + WpGWmVOCkGbi4AQZzgM0PAWkhre4IDG3ODMdIn+KUVFKHKJZACSRUZoH1wtLE68YxYFeEeZt + BKniAaYYuO5XQIOZgAbzC6wDGVySiJCSamCcpN9tbxvyYLLKsr4Vh8VOiaxz6p3JsePzZAcO + T9+1TPZ7Fmht+S+62+9Xy/s/V1lbnt1qmLukL9W5xmr2xuiXew7Wf3tXk8T5qu5nuP1URtav + CxOESme3lf65MUugZ+f5xX+0D4v2qT9NUWs5OOfkxaaKuHNyfe/np8sbP/Lh0FVsn12T+6VI + iaU4I9FQi7moOBEAKnU/+u4CAAA= +Cc: tomi.ollila@iki.fi +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: Sun, 13 May 2012 19:57:25 -0000 + +Previously, notmuch_database_get_directory had no way to indicate how +it had failed. This changes its prototype to return a status code and +set an out-argument to the retrieved directory, like similar functions +in the library API. This does *not* change its currently broken +behavior of creating directory objects when they don't exist, but it +does document it and paves the way for fixing this. Also, it can now +check for a read-only database and return +NOTMUCH_STATUS_READ_ONLY_DATABASE instead of crashing. + +In the interest of atomicity, this also updates calls from the CLI so +that notmuch still compiles. +--- + lib/database.cc | 22 ++++++++++++++++------ + lib/notmuch.h | 24 +++++++++++++++++++++--- + notmuch-new.c | 21 ++++++++++++++------- + 3 files changed, 51 insertions(+), 16 deletions(-) + +diff --git a/lib/database.cc b/lib/database.cc +index 07f186e..f8c4a7d 100644 +--- a/lib/database.cc ++++ b/lib/database.cc +@@ -955,8 +955,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, + mtime = Xapian::sortable_unserialise ( + document.get_value (NOTMUCH_VALUE_TIMESTAMP)); + +- directory = notmuch_database_get_directory (notmuch, +- term.c_str() + 10); ++ directory = _notmuch_directory_create (notmuch, term.c_str() + 10, ++ &status); + notmuch_directory_set_mtime (directory, mtime); + notmuch_directory_destroy (directory); + } +@@ -1304,20 +1304,30 @@ _notmuch_database_relative_path (notmuch_database_t *notmuch, + return relative; + } + +-notmuch_directory_t * ++notmuch_status_t + notmuch_database_get_directory (notmuch_database_t *notmuch, +- const char *path) ++ const char *path, ++ notmuch_directory_t **directory) + { + notmuch_status_t status; + ++ if (directory == NULL) ++ return NOTMUCH_STATUS_NULL_POINTER; ++ *directory = NULL; ++ ++ /* XXX Handle read-only databases properly */ ++ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) ++ return NOTMUCH_STATUS_READ_ONLY_DATABASE; ++ + try { +- return _notmuch_directory_create (notmuch, path, &status); ++ *directory = _notmuch_directory_create (notmuch, path, &status); + } catch (const Xapian::Error &error) { + fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n", + error.get_msg().c_str()); + notmuch->exception_reported = TRUE; +- return NULL; ++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + } ++ return status; + } + + /* Allocate a document ID that satisfies the following criteria: +diff --git a/lib/notmuch.h b/lib/notmuch.h +index e6e5cc2..d414198 100644 +--- a/lib/notmuch.h ++++ b/lib/notmuch.h +@@ -300,11 +300,29 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch); + * (see notmuch_database_get_path), or else should be an absolute path + * with initial components that match the path of 'database'. + * +- * Can return NULL if a Xapian exception occurs. ++ * Note: Currently this will create the directory object if it doesn't ++ * exist. This is not intentional behavior and is likely to change in ++ * the near future. To future-proof, when this returns ++ * NOTMUCH_STATUS_SUCCESS, callers should check that *directory is not ++ * NULL. ++ * ++ * Return value: ++ * ++ * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory. ++ * ++ * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL. ++ * ++ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; ++ * directory not retrieved. ++ * ++ * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only ++ * mode so the directory cannot be created (this case will be ++ * removed in the future). + */ +-notmuch_directory_t * ++notmuch_status_t + notmuch_database_get_directory (notmuch_database_t *database, +- const char *path); ++ const char *path, ++ notmuch_directory_t **directory); + + /* Add a new message to the given notmuch database or associate an + * additional filename with an existing message. +diff --git a/notmuch-new.c b/notmuch-new.c +index cb720cc..a3a8bec 100644 +--- a/notmuch-new.c ++++ b/notmuch-new.c +@@ -250,7 +250,7 @@ add_files_recursive (notmuch_database_t *notmuch, + notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS; + notmuch_message_t *message = NULL; + struct dirent **fs_entries = NULL; +- int i, num_fs_entries; ++ int i, num_fs_entries = 0; + notmuch_directory_t *directory; + notmuch_filenames_t *db_files = NULL; + notmuch_filenames_t *db_subdirs = NULL; +@@ -274,8 +274,12 @@ add_files_recursive (notmuch_database_t *notmuch, + + fs_mtime = st.st_mtime; + +- directory = notmuch_database_get_directory (notmuch, path); +- db_mtime = notmuch_directory_get_mtime (directory); ++ status = notmuch_database_get_directory (notmuch, path, &directory); ++ if (status) { ++ ret = status; ++ goto DONE; ++ } ++ db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0; + + new_directory = db_mtime ? FALSE : TRUE; + +@@ -295,7 +299,7 @@ add_files_recursive (notmuch_database_t *notmuch, + * by a new out-argument, or by recording this information and + * providing an accessor. + */ +- if (new_directory) ++ if (new_directory && directory) + notmuch_directory_set_mtime (directory, -1); + + /* If the database knows about this directory, then we sort based +@@ -810,7 +814,9 @@ _remove_directory (void *ctx, + notmuch_filenames_t *files, *subdirs; + char *absolute; + +- directory = notmuch_database_get_directory (notmuch, path); ++ status = notmuch_database_get_directory (notmuch, path, &directory); ++ if (status || !directory) ++ return status; + + for (files = notmuch_directory_get_child_files (directory); + notmuch_filenames_valid (files); +@@ -981,9 +987,10 @@ notmuch_new_command (void *ctx, int argc, char *argv[]) + } + + for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) { ++ notmuch_status_t status; + notmuch_directory_t *directory; +- directory = notmuch_database_get_directory (notmuch, f->filename); +- if (directory) { ++ status = notmuch_database_get_directory (notmuch, f->filename, &directory); ++ if (status == NOTMUCH_STATUS_SUCCESS && directory) { + notmuch_directory_set_mtime (directory, f->mtime); + notmuch_directory_destroy (directory); + } +-- +1.7.10 + -- 2.26.2