From: Austin Clements Date: Sun, 13 May 2012 23:36:09 +0000 (+2000) Subject: [PATCH v2 1/5] lib/cli: Make notmuch_database_get_directory return a status code X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=ab79dfaeb16d3953f8df5fc1a28cfdf1e26f2ebf;p=notmuch-archives.git [PATCH v2 1/5] lib/cli: Make notmuch_database_get_directory return a status code --- diff --git a/fd/6fe48693c7a80334551ec31dcc712032e2d9e2 b/fd/6fe48693c7a80334551ec31dcc712032e2d9e2 new file mode 100644 index 000000000..f4efad433 --- /dev/null +++ b/fd/6fe48693c7a80334551ec31dcc712032e2d9e2 @@ -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 02E04431FC0 + for ; Sun, 13 May 2012 16:36: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 74Zp-v00edOG for ; + Sun, 13 May 2012 16:36:22 -0700 (PDT) +Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU + [18.9.25.13]) + by olra.theworths.org (Postfix) with ESMTP id 925BE431FAE + for ; Sun, 13 May 2012 16:36:22 -0700 (PDT) +X-AuditID: 1209190d-b7fbf6d0000008ba-27-4fb045761c29 +Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) + by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP + id 5A.DF.02234.67540BF4; Sun, 13 May 2012 19:36:22 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q4DNaLSx025782; + Sun, 13 May 2012 19:36:21 -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 q4DNaK2s001096 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sun, 13 May 2012 19:36:20 -0400 (EDT) +Received: from amthrax by drake.mit.edu with local (Exim 4.77) + (envelope-from ) + id 1STiKp-0004YJ-Sz; Sun, 13 May 2012 19:36:19 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v2 1/5] lib/cli: Make notmuch_database_get_directory return a + status code +Date: Sun, 13 May 2012 19:36:09 -0400 +Message-Id: <1336952173-17422-2-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1336952173-17422-1-git-send-email-amdragon@mit.edu> +References: <1336939029-13969-1-git-send-email-amdragon@mit.edu> + <1336952173-17422-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFrrNIsWRmVeSWpSXmKPExsUixCmqrFvmusHfYGU/t8Xs1h9MFjdauxkt + mqY7W1y/OZPZ4s3KeawOrB6Hvy5k8Zh4/jSbx637r9k9nq26xeyx5dB75gDWKC6blNSczLLU + In27BK6M5u0v2Au2GVZsXTybrYHxlXoXIyeHhICJxLo1fxkhbDGJC/fWs3UxcnEICexjlHhy + fxUjhLOBUaKn4z87hHOfSWJm51wmCGcWo8Tt7sWsIP1sAhoS2/YvB5slIiAtsfPubFaQImaB + LkaJ32+amUESwgKREk93zmQHsVkEVCVWLX0DtJCDg1fAQWLtEkOIO+Qlnt7vYwOxOQUcJfoe + fAQrFxIol5jRfp1lAiP/AkaGVYyyKblVurmJmTnFqcm6xcmJeXmpRbpGermZJXqpKaWbGMFh + KMm7g/HdQaVDjAIcjEo8vK9MN/gLsSaWFVfmHmKU5GBSEuVlcgIK8SXlp1RmJBZnxBeV5qQW + H2KU4GBWEuFd/3C9vxBvSmJlVWpRPkxKmoNFSZxXVeudn5BAemJJanZqakFqEUxWhoNDSYL3 + gQvQUMGi1PTUirTMnBKENBMHJ8hwHqDhZ0BqeIsLEnOLM9Mh8qcYFaXEea+AJARAEhmleXC9 + sDTxilEc6BVh3r0gVTzAFAPX/QpoMBPQYH6BdSCDSxIRUlINjFW9VdJPHiRWG+ZF6fBsnBA3 + 3WH5Tp7Qb0cy+3ad+NS/evYsOcb1z+e9TJ32ZccUy5+3/Z7syZpqPe3zHYGtLaXBbLHXj0w+ + 4iHqqh5TnJLasaqveIfgR9kpbhHaHV8eS8hpN5dfMS75Y+UUZ3szw/hrltFe5o6lT/T9tqh7 + 71825+oj+ZycViWW4oxEQy3mouJEAEREPaXuAgAA +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 23:36:24 -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 | 23 ++++++++++++++++++++--- + notmuch-new.c | 21 ++++++++++++++------- + 3 files changed, 50 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..bbb17e4 100644 +--- a/lib/notmuch.h ++++ b/lib/notmuch.h +@@ -300,11 +300,28 @@ 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. In the future, when a directory object does not exist this ++ * will return NOTMUCH_STATUS_SUCCESS and set *directory to NULL. ++ * Callers should be prepared for this. ++ * ++ * 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 +