From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:bcc0::]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id foBDKZARbWDYIAEAgWs5BA (envelope-from ) for ; Wed, 07 Apr 2021 03:57:36 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id 2Kw1IpARbWCORgAAB5/wlQ (envelope-from ) for ; Wed, 07 Apr 2021 01:57:36 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 18C9DAE88 for ; Wed, 7 Apr 2021 03:57:36 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id BC763290A4; Tue, 6 Apr 2021 21:56:30 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id DE96929052 for ; Tue, 6 Apr 2021 21:55:55 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id D18A05FA96; Tue, 6 Apr 2021 21:55:55 -0400 (EDT) Received: (nullmailer pid 2964252 invoked by uid 1000); Wed, 07 Apr 2021 01:55:35 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 23/28] CLI+lib: detect missing database in split configurations. Date: Tue, 6 Apr 2021 22:55:25 -0300 Message-Id: <20210407015530.2964017-24-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210407015530.2964017-1-david@tethera.net> References: <20210407015530.2964017-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: U5TL4YVM6OWD6IPWG5P5WRYS4FGAZVAI X-Message-ID-Hash: U5TL4YVM6OWD6IPWG5P5WRYS4FGAZVAI X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1617760656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=qwFt8H6qLO8ZpdsSwgbvzEClc+7RjsH901N9aR498bc=; b=UnCy9++hNOv7yIx0/MKNjKIsCRu+4dCOeoJQB0bdXBfTbinfsXfGIc8H/LjvfK4zBpJo/m hsQQ65N9YJhUx5I1Su4oSsa5piVBbJmm9neZ8wIwB45NYnPxJpfT4NDFK9VtaKbK0WNQPd Sv+Rtnwf6FF0X9/QXBYGe7c65WFx2LpEsQ1G+5X8UZjWDhZCoOaATtOEq4QH/JMvQCKKEb 8GKy3139Rck8Upl9cs2tD5S93oifTa+pMGpjl5VUuOK4/3vVdbNOluAFCr0jwFtq56dTgJ lSBFJHQ0SvKqXgpHM2w4BWBR0OhY19JmhPTll2gQlZPn3m9wnvEWf1oD5ua4TQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1617760656; a=rsa-sha256; cv=none; b=o+LJ4V1Sl7nt/32FjRsWMHaKX3c0i7xZ+bEynHWIKNpOCaC8VWOnwiCD46yABMe2dzjnVG GM2huS4Y8aUNM87fVfUN+tVKPuU25it+LCbWfRU1fYR6hcJPA0LUmkWbXAdW1F4GE6Ld2v H24XE0GXIumTKwjk9Yh/mtJkPeghmmd7TogNeAGVHkQlscs9dUxHns8ukIg8w4ai+/7T/D 2id0YxJ93bMDHd41sa65N65fN6VQOSl+QQPdSx81cP3MtQVmmQIY0/fa/Avt3TFxBtguAS ZF4QI77Ob3moCG5Mcahne/lkiP7DZsxkZC/sWoV8FUR8KnV7pk5t1MgOvg3eKw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -0.88 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 18C9DAE88 X-Spam-Score: -0.88 X-Migadu-Scanner: scn0.migadu.com X-TUID: TwMuZYYq47LL Eventually we want to do all opening of databases in the top level (main function). This means that detection of missing databases needs to move out of subcommands. It also requires updating the library to use the new NO_DATABASE status code. --- lib/open.cc | 24 +++++++++++++------- notmuch.c | 47 +++++++++++++++++----------------------- test/T040-setup.sh | 7 ++---- test/T055-path-config.sh | 16 ++++++++++++++ 4 files changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/open.cc b/lib/open.cc index d0493950..387e4ddb 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -209,8 +209,8 @@ _choose_database_path (void *ctx, } if (*database_path == NULL) { - *message = strdup ("Error: Cannot open a database for a NULL path.\n"); - return NOTMUCH_STATUS_NULL_POINTER; + *message = strdup ("Error: could not locate database.\n"); + return NOTMUCH_STATUS_NO_DATABASE; } if (*database_path[0] != '/') { @@ -750,12 +750,12 @@ _maybe_load_config_from_database (notmuch_database_t *notmuch, char *message; /* ignored */ if (_db_dir_exists (database_path, &message)) - return NOTMUCH_STATUS_SUCCESS; + return NOTMUCH_STATUS_NO_DATABASE; _set_database_path (notmuch, database_path); if (_notmuch_choose_xapian_path (notmuch, database_path, ¬much->xapian_path, &message)) - return NOTMUCH_STATUS_SUCCESS; + return NOTMUCH_STATUS_NO_DATABASE; (void) _finish_open (notmuch, profile, NOTMUCH_DATABASE_MODE_READ_ONLY, key_file, &message); @@ -798,19 +798,27 @@ notmuch_database_load_config (const char *database_path, status = _choose_database_path (local, profile, key_file, &database_path, &split, &message); switch (status) { - /* weirdly NULL_POINTER is what is returned if we fail to find - * a database */ - case NOTMUCH_STATUS_NULL_POINTER: + case NOTMUCH_STATUS_NO_DATABASE: case NOTMUCH_STATUS_SUCCESS: + if (! warning) + warning = status; break; default: goto DONE; } + if (database_path) { status = _maybe_load_config_from_database (notmuch, key_file, database_path, profile); - if (status) + switch (status) { + case NOTMUCH_STATUS_NO_DATABASE: + case NOTMUCH_STATUS_SUCCESS: + if (! warning) + warning = status; + break; + default: goto DONE; + } } if (key_file) { diff --git a/notmuch.c b/notmuch.c index ff9ad18b..bcb4b768 100644 --- a/notmuch.c +++ b/notmuch.c @@ -380,34 +380,12 @@ notmuch_command (notmuch_config_t *config, notmuch_database_t *notmuch, unused(int argc), unused(char **argv)) { - char *db_path; - struct stat st; - /* If the user has never configured notmuch, then run * notmuch_setup_command which will give a nice welcome message, * and interactively guide the user through the configuration. */ if (notmuch_config_is_new (config)) return notmuch_setup_command (config, notmuch, 0, NULL); - /* Notmuch is already configured, but is there a database? */ - db_path = talloc_asprintf (config, "%s/%s", - notmuch_config_get (notmuch, NOTMUCH_CONFIG_DATABASE_PATH), - ".notmuch"); - if (stat (db_path, &st)) { - if (errno != ENOENT) { - fprintf (stderr, "Error looking for notmuch database at %s: %s\n", - db_path, strerror (errno)); - return EXIT_FAILURE; - } - printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n", - db_path); - printf ("You probably want to run \"notmuch new\" now to create that database.\n\n" - "Note that the first run of \"notmuch new\" can take a very long time\n" - "and that the resulting database will use roughly the same amount of\n" - "storage space as the email being indexed.\n\n"); - return EXIT_SUCCESS; - } - printf ("Notmuch is configured and appears to have a database. Excellent!\n\n" "At this point you can start exploring the functionality of notmuch by\n" "using commands such as:\n\n" @@ -576,12 +554,27 @@ main (int argc, char *argv[]) fputs ("Try running 'notmuch setup' to create a configuration.", stderr); goto DONE; } - - if (status && (status != NOTMUCH_STATUS_NO_CONFIG)) { - if (status_string) { - fputs (status_string, stderr); - free (status_string); + switch (status) { + case NOTMUCH_STATUS_NO_CONFIG: + if (! (command->mode & NOTMUCH_COMMAND_CONFIG_CREATE)) { + fputs ("Try running 'notmuch setup' to create a configuration.", stderr); + goto DONE; + } + break; + case NOTMUCH_STATUS_NO_DATABASE: + if (! command_name) { + printf ("Notmuch is configured, but no database was found.\n"); + printf ("You probably want to run \"notmuch new\" now to create a database.\n\n" + "Note that the first run of \"notmuch new\" can take a very long time\n" + "and that the resulting database will use roughly the same amount of\n" + "storage space as the email being indexed.\n\n"); + status = NOTMUCH_STATUS_SUCCESS; + goto DONE; } + break; + case NOTMUCH_STATUS_SUCCESS: + break; + default: goto DONE; } diff --git a/test/T040-setup.sh b/test/T040-setup.sh index daeca3e4..42c621c8 100755 --- a/test/T040-setup.sh +++ b/test/T040-setup.sh @@ -26,11 +26,8 @@ test_expect_equal_file ${expected_dir}/config-with-comments new-notmuch-config test_begin_subtest "notmuch with a config but without a database suggests notmuch new" notmuch 2>&1 | notmuch_dir_sanitize > OUTPUT cat < EXPECTED -Notmuch is configured, but there's not yet a database at - - MAIL_DIR/.notmuch - -You probably want to run "notmuch new" now to create that database. +Notmuch is configured, but no database was found. +You probably want to run "notmuch new" now to create a database. Note that the first run of "notmuch new" can take a very long time and that the resulting database will use roughly the same amount of diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh index d8828342..0a34e67f 100755 --- a/test/T055-path-config.sh +++ b/test/T055-path-config.sh @@ -199,6 +199,22 @@ EOF "$output" \ "Welcome to a new version of notmuch! Your database will now be upgraded." + test_begin_subtest "notmuch +config -database suggests notmuch new ($config)" + mv "$XAPIAN_PATH" "${XAPIAN_PATH}.bak" + notmuch > OUTPUT +cat < EXPECTED +Notmuch is configured, but no database was found. +You probably want to run "notmuch new" now to create a database. + +Note that the first run of "notmuch new" can take a very long time +and that the resulting database will use roughly the same amount of +storage space as the email being indexed. + +EOF + mv "${XAPIAN_PATH}.bak" "$XAPIAN_PATH" + + test_expect_equal_file EXPECTED OUTPUT + restore_config done -- 2.30.2