From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id IMYmIIjaQGBvbgAA0tVLHw (envelope-from ) for ; Thu, 04 Mar 2021 13:03:04 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id UHkLHIjaQGCxQQAAB5/wlQ (envelope-from ) for ; Thu, 04 Mar 2021 13:03:04 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (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 E398215FB7 for ; Thu, 4 Mar 2021 14:03:03 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 6BE422757A; Thu, 4 Mar 2021 08:02:11 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 22DCC26B95 for ; Thu, 4 Mar 2021 08:01:55 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 197895FA96; Thu, 4 Mar 2021 08:01:55 -0500 (EST) Received: (nullmailer pid 4047208 invoked by uid 1000); Thu, 04 Mar 2021 13:01:45 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 22/27] CLI+lib: detect missing database in split configurations. Date: Thu, 4 Mar 2021 09:01:34 -0400 Message-Id: <20210304130139.4046183-23-david@tethera.net> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210304130139.4046183-1-david@tethera.net> References: <20210304130139.4046183-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 6UOO7BZZTENN3RHNRTMMKP5RZSPBEFYP X-Message-ID-Hash: 6UOO7BZZTENN3RHNRTMMKP5RZSPBEFYP 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=1614862984; 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=cgzH/aSqZO2Og5McoY8DZW5J20TcVuLrwLREWTfQ1aw=; b=FceFRzgJYVSBAwmcGcTs2Ej8PDJfhR0OyZLV0hM2hfZD6hSaMjap0UgPPcNthMofC6mSo5 fhb7Em19eB13M+mIGnyw1pSocBD7ad02iky426iK3DHxDRqRZJQiNMwvU/ewK2gz+sNao1 RnF23d0pKSTB8CjBsUYuurTzjrt38RgoD9LInRDANrZaRHKo5vwp2NAnDXE6Ts1glClwQe KUEwsG7qoI3ur/0YduR4yCYtx+wT1hioySkVwUepi4k4XFQV/DuWFiiJ8RnDSVcNVtY1C9 i25BVI8MM4oRG5aX2vVprlvvRgLzvZO1TTF0wEdjAaDZe8tMpVl/34vAqtimRQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1614862984; a=rsa-sha256; cv=none; b=H8agPJnGGN4lIc7bWUOgh6LSRvXLVlpbFTsIp4ezclmFXJK8ItNCw8BgzJHzNyjp8mnrBM NbGDAtB0gS0UMy3k7T/JMja1MkN527tawrQnVEFW3QZk0oRWdRWonFJjeX9Nh3P2OApGQF 0hFHdIGrEaWf/SxDFNy8cT50pKd6Y9lmqcqCT1RgTT12V+M71HeZYYhGZ7eh1hFKyAjeOr Ef5YMNAmxmjE4mB5Op+mfQDwYdh+OXP8or0OGnXU13GX+u/pG7e5l9ogFe4NXFNWkh1LKO Jmrp9YQcGTMy2xMv1JXi3+KsQASyEH2zjK5mIe6lssURgYX5fflXjBlwD2//AQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 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 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: E398215FB7 X-Spam-Score: -0.88 X-Migadu-Scanner: scn0.migadu.com X-TUID: mIsdrijMjrzO 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 | 52 ++++++++++++++++------------------------ test/T040-setup.sh | 7 ++---- test/T055-path-config.sh | 16 +++++++++++++ 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/lib/open.cc b/lib/open.cc index 737e79a7..3c88ff80 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] != '/') { @@ -726,12 +726,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); @@ -774,19 +774,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 (! status2) + status2 = 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 (! status2) + status2 = status; + break; + default: goto DONE; + } } if (key_file) { diff --git a/notmuch.c b/notmuch.c index 093af0a5..fa6b7eaa 100644 --- a/notmuch.c +++ b/notmuch.c @@ -369,34 +369,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" @@ -561,19 +539,29 @@ main (int argc, char *argv[]) ¬much, &status_string); - if (status == NOTMUCH_STATUS_NO_CONFIG && !(command->mode & NOTMUCH_COMMAND_CONFIG_CREATE)) { - 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; } - } if (command->mode & NOTMUCH_COMMAND_CONFIG_OPEN) { 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.1