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 1C210431FB6 for ; Mon, 25 Jun 2012 13:54:08 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 1.061 X-Spam-Level: * X-Spam-Status: No, score=1.061 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.246, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_SORBS_WEB=0.614] 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 1mh-z2GsTlVk for ; Mon, 25 Jun 2012 13:54:07 -0700 (PDT) Received: from mail-we0-f181.google.com (mail-we0-f181.google.com [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 09637431FAF for ; Mon, 25 Jun 2012 13:54:06 -0700 (PDT) Received: by werj55 with SMTP id j55so3649559wer.26 for ; Mon, 25 Jun 2012 13:54:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=nrU9nVmzJXJsXKqS8m11Z17v5gUa/3MWhEq27JiKUS0=; b=06M9rpHV51pq0hSDUnstkj43T96lpndJYsr7E4fkp9ggg47Cp1277Rs7O7Zu4gO0DC L6D7d1TdJztUdDfVcBItE86vII7fymd9GecWXTJ3JRBu87qq5ACbwnXBbGdrYkAA2FwX ef33s8OZ4w3CvknGX55pWvnQsgeewwGZzyr3ONT5sdV+qg69wgxEi575UFTOl8Y+1h9M WhfK3Z/jNHZx1dl4KWBRY8nPLKJr9ndIwvY8+EjOVUtI3HFK/DKPlqnylUrYPksaCU6c p3isfgN7zSTp9hCeQ90zLZRjfFN7TCWYmLuiOwZNMljTiWH0R7uPFaq1qVDiAR2s3/VW Ga0A== Received: by 10.180.24.39 with SMTP id r7mr27675594wif.9.1340657645740; Mon, 25 Jun 2012 13:54:05 -0700 (PDT) Received: from localhost ([195.24.209.21]) by mx.google.com with ESMTPS id t8sm34175wiy.3.2012.06.25.13.54.00 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 25 Jun 2012 13:54:04 -0700 (PDT) From: Ethan Glasser-Camp To: notmuch@notmuchmail.org Subject: [RFC PATCH 11/14] notmuch-new: pull out useful bits of add_files_recursive Date: Mon, 25 Jun 2012 16:51:54 -0400 Message-Id: <1340657517-6539-7-git-send-email-ethan@betacantrips.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1340657517-6539-1-git-send-email-ethan@betacantrips.com> References: <1340657517-6539-1-git-send-email-ethan@betacantrips.com> X-Mailman-Approved-At: Tue, 26 Jun 2012 03:51:54 -0700 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: Mon, 25 Jun 2012 20:54:08 -0000 This is part of notmuch-new refactor phase 1: make add_files stuff safe for other backends. add_files_recursive is essentially a maildir-crawling function that periodically adds files to the database or adds filenames to remove_files or remove_directory lists. I don't see an easy way to adapt add_files_recursive for other backends who might not have concepts of directories with other directories inside of them, so instead just provide an add_files method for each backend. This patch pulls some bits out of add_files_recursive which will be useful for other backends: two reporting functions _report_before_adding_file and _report_added_file, as well as _add_message, which actually does the message adding. Signed-off-by: Ethan Glasser-Camp --- notmuch-new.c | 192 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 73 deletions(-) diff --git a/notmuch-new.c b/notmuch-new.c index 57b27bf..1bf4e25 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -249,6 +249,122 @@ add_files_uri (unused(notmuch_database_t *notmuch), return NOTMUCH_STATUS_SUCCESS; } +/* Progress-reporting function. + * + * Can be used by any mailstore-crawling function that wants to alert + * users what message it's about to add. Subsequent errors will be due + * to this message ;) + */ +static void +_report_before_adding_file (add_files_state_t *state, const char *filename) +{ + state->processed_files++; + + if (state->verbose) { + if (state->output_is_a_tty) + printf("\r\033[K"); + + printf ("%i/%i: %s", + state->processed_files, + state->total_files, + filename); + + putchar((state->output_is_a_tty) ? '\r' : '\n'); + fflush (stdout); + } +} + +/* Progress-reporting function. + * + * Call this to respond to the signal handler for SIGALRM. + */ +static void +_report_added_file (add_files_state_t *state) +{ + if (do_print_progress) { + do_print_progress = 0; + generic_print_progress ("Processed", "files", state->tv_start, + state->processed_files, state->total_files); + } +} + + +/* Atomically handles adding a message to the database. + * + * Should be used by any mailstore-crawling function that finds a new + * message to add. + */ +static notmuch_status_t +_add_message (add_files_state_t *state, notmuch_database_t *notmuch, + const char *filename) +{ + notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS; + notmuch_message_t *message; + const char **tag; + + status = notmuch_database_begin_atomic (notmuch); + if (status) { + ret = status; + goto DONE; + } + + status = notmuch_database_add_message (notmuch, filename, &message); + + switch (status) { + /* success */ + case NOTMUCH_STATUS_SUCCESS: + state->added_messages++; + notmuch_message_freeze (message); + for (tag=state->new_tags; *tag != NULL; tag++) + notmuch_message_add_tag (message, *tag); + if (state->synchronize_flags == TRUE) + notmuch_message_maildir_flags_to_tags (message); + notmuch_message_thaw (message); + break; + /* Non-fatal issues (go on to next file) */ + case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: + if (state->synchronize_flags == TRUE) + notmuch_message_maildir_flags_to_tags (message); + break; + case NOTMUCH_STATUS_FILE_NOT_EMAIL: + fprintf (stderr, "Note: Ignoring non-mail file: %s\n", + filename); + break; + /* Fatal issues. Don't process anymore. */ + case NOTMUCH_STATUS_READ_ONLY_DATABASE: + case NOTMUCH_STATUS_XAPIAN_EXCEPTION: + case NOTMUCH_STATUS_OUT_OF_MEMORY: + fprintf (stderr, "Error: %s. Halting processing.\n", + notmuch_status_to_string (status)); + ret = status; + goto DONE; + default: + case NOTMUCH_STATUS_FILE_ERROR: + case NOTMUCH_STATUS_NULL_POINTER: + case NOTMUCH_STATUS_TAG_TOO_LONG: + case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: + case NOTMUCH_STATUS_UNBALANCED_ATOMIC: + case NOTMUCH_STATUS_LAST_STATUS: + INTERNAL_ERROR ("add_message returned unexpected value: %d", status); + ret = status; + goto DONE; + } + + status = notmuch_database_end_atomic (notmuch); + if (status) { + ret = status; + goto DONE; + } + + DONE: + if (message) { + notmuch_message_destroy (message); + message = NULL; + } + + return ret; +} + /* Examine 'path' recursively as follows: * * o Ask the filesystem for the mtime of 'path' (fs_mtime) @@ -300,7 +416,6 @@ add_files (notmuch_database_t *notmuch, char *next = NULL, *path_uri = NULL; time_t fs_mtime, db_mtime; notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS; - notmuch_message_t *message = NULL; struct dirent **fs_entries = NULL; int i, num_fs_entries = 0, entry_type; notmuch_directory_t *directory; @@ -309,7 +424,6 @@ add_files (notmuch_database_t *notmuch, time_t stat_time; struct stat st; notmuch_bool_t is_maildir; - const char **tag; if (stat (path, &st)) { fprintf (stderr, "Error reading directory %s: %s\n", @@ -488,83 +602,15 @@ add_files (notmuch_database_t *notmuch, * in the database, so add it. */ next = talloc_asprintf (notmuch, "%s/%s", path_uri, entry->d_name); - state->processed_files++; - - if (state->verbose) { - if (state->output_is_a_tty) - printf("\r\033[K"); - - printf ("%i/%i: %s", - state->processed_files, - state->total_files, - next); - - putchar((state->output_is_a_tty) ? '\r' : '\n'); - fflush (stdout); - } + _report_before_adding_file (state, next); - status = notmuch_database_begin_atomic (notmuch); + status = _add_message (state, notmuch, next); if (status) { ret = status; goto DONE; } - status = notmuch_database_add_message (notmuch, next, &message); - switch (status) { - /* success */ - case NOTMUCH_STATUS_SUCCESS: - state->added_messages++; - notmuch_message_freeze (message); - for (tag=state->new_tags; *tag != NULL; tag++) - notmuch_message_add_tag (message, *tag); - if (state->synchronize_flags == TRUE) - notmuch_message_maildir_flags_to_tags (message); - notmuch_message_thaw (message); - break; - /* Non-fatal issues (go on to next file) */ - case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: - if (state->synchronize_flags == TRUE) - notmuch_message_maildir_flags_to_tags (message); - break; - case NOTMUCH_STATUS_FILE_NOT_EMAIL: - fprintf (stderr, "Note: Ignoring non-mail file: %s\n", - next); - break; - /* Fatal issues. Don't process anymore. */ - case NOTMUCH_STATUS_READ_ONLY_DATABASE: - case NOTMUCH_STATUS_XAPIAN_EXCEPTION: - case NOTMUCH_STATUS_OUT_OF_MEMORY: - fprintf (stderr, "Error: %s. Halting processing.\n", - notmuch_status_to_string (status)); - ret = status; - goto DONE; - default: - case NOTMUCH_STATUS_FILE_ERROR: - case NOTMUCH_STATUS_NULL_POINTER: - case NOTMUCH_STATUS_TAG_TOO_LONG: - case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: - case NOTMUCH_STATUS_UNBALANCED_ATOMIC: - case NOTMUCH_STATUS_LAST_STATUS: - INTERNAL_ERROR ("add_message returned unexpected value: %d", status); - goto DONE; - } - - status = notmuch_database_end_atomic (notmuch); - if (status) { - ret = status; - goto DONE; - } - - if (message) { - notmuch_message_destroy (message); - message = NULL; - } - - if (do_print_progress) { - do_print_progress = 0; - generic_print_progress ("Processed", "files", state->tv_start, - state->processed_files, state->total_files); - } + _report_added_file (state); talloc_free (next); next = NULL; -- 1.7.9.5