From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <notmuch-bounces@notmuchmail.org>
Received: from mp11.migadu.com ([2001:41d0:2:bcc0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms5.migadu.com with LMTPS
	id wAsNDqDQ42JVagAAbAwnHQ
	(envelope-from <notmuch-bounces@notmuchmail.org>)
	for <larch@yhetil.org>; Fri, 29 Jul 2022 14:20:48 +0200
Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp11.migadu.com with LMTPS
	id QHMbDqDQ42KoOgAA9RJhRA
	(envelope-from <notmuch-bounces@notmuchmail.org>)
	for <larch@yhetil.org>; Fri, 29 Jul 2022 14:20:48 +0200
Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1])
	(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 EE25B41AB1
	for <larch@yhetil.org>; Fri, 29 Jul 2022 14:20:47 +0200 (CEST)
Received: from yantan.tethera.net (localhost [127.0.0.1])
	by mail.notmuchmail.org (Postfix) with ESMTP id A82955F38A;
	Fri, 29 Jul 2022 12:20:22 +0000 (UTC)
Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])
	by mail.notmuchmail.org (Postfix) with ESMTP id 4D5D15F37E
	for <notmuch@notmuchmail.org>; Fri, 29 Jul 2022 12:20:18 +0000 (UTC)
Received: by fethera.tethera.net (Postfix, from userid 1001)
	id C808C5FC01; Fri, 29 Jul 2022 08:20:17 -0400 (EDT)
Received: (nullmailer pid 1557196 invoked by uid 1000);
	Fri, 29 Jul 2022 12:20:10 -0000
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [PATCH 6/6] lib/open: create database path in some cases
Date: Fri, 29 Jul 2022 09:20:01 -0300
Message-Id: <20220729122001.1556787-7-david@tethera.net>
X-Mailer: git-send-email 2.35.2
In-Reply-To: <20220729122001.1556787-1-david@tethera.net>
References: <20220729122001.1556787-1-david@tethera.net>
MIME-Version: 1.0
Message-ID-Hash: PVLL3SNOYIP3XERFR3CQTMFPHPTMGJL4
X-Message-ID-Hash: PVLL3SNOYIP3XERFR3CQTMFPHPTMGJL4
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; digests; suspicious-header
CC: spwhitton@spwhitton.name
X-Mailman-Version: 3.3.3
Precedence: list
List-Id: "Use and development of the notmuch mail system." <notmuch.notmuchmail.org>
List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
List-Owner: <mailto:notmuch-owner@notmuchmail.org>
List-Post: <mailto:notmuch@notmuchmail.org>
List-Subscribe: <mailto:notmuch-join@notmuchmail.org>
List-Unsubscribe: <mailto:notmuch-leave@notmuchmail.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Migadu-Flow: FLOW_IN
X-Migadu-To: larch@yhetil.org
X-Migadu-Country: DE
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org;
	s=key1; t=1659097248;
	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-owner:list-unsubscribe:list-subscribe:list-post;
	bh=wQMoAJ6igwA34h1c6aXhtJR8fu8zp3PBRyXU0CBeH9U=;
	b=bpepvSg2Mw3PhDAuIaqqxgufax0RQmL+6u2pkafaUe3U6EwaGm0qk6D/cZWUTdVNVZ2jTw
	vxmho4FhtfDKVj6P2VWJPfltcuXErshITP/+7Hz1XNZ7cDWXOD9mqz4vDDYJ7g75RjxscZ
	Oz51Sevk1xU4GmNewNQEx1aj+rqlZi9gRO0fXaz7uyjyjVuJWMSeh+7txENTJ552VL1BWB
	JRt1nygQLiBkmGafyNTEH8MXoz9oonyQNkqj4vfCG1XlCHJGa/8rlfGE4HTSH5YYB+GuRS
	gyBTQ12eOr2IR4cUAP0+LuiZcLueRZL024ZVoBojrjIBBur8SSuncpusNTW+Ng==
ARC-Seal: i=1; s=key1; d=yhetil.org; t=1659097248; a=rsa-sha256; cv=none;
	b=ecFyfjBg1ax4H/qKZuL/yCuzhd8wu5JtJNFe9TjRKj8gFUmV5GSVEQ2i5/QPHFAPr7XfwF
	zC0Fga3ce3wGLL7okISsu2svTXzStdw5wg4mF2mTXkgVVv0rvZVEn56N8iH8uxTytT9s+r
	LFYhjXyjwI7BGig2IhObmILFT0/tExaatn4dV9aRmnGEXFlhXnNp/JdKBeE03080DEkmjz
	MJWB8lKb20yB3Mu5AAdRKn8F/V36eRh4fNETfolVFYhtJsZlcozb5s1eWCRBWi6YI225Hl
	DMkUXH215DhpWdcLDl0tF2K/rAP4ziJnAcORm5iN8TP2DvCMV8VZRv/N0DVOiw==
ARC-Authentication-Results: i=1;
	aspmx1.migadu.com;
	dkim=none;
	dmarc=none;
	spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org
X-Migadu-Spam-Score: -2.10
Authentication-Results: aspmx1.migadu.com;
	dkim=none;
	dmarc=none;
	spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org
X-Migadu-Queue-Id: EE25B41AB1
X-Spam-Score: -2.10
X-Migadu-Scanner: scn0.migadu.com
X-TUID: aLkKnVKkSjx2

There is some duplication of code here, but not all of the locations
valid to find a database make sense to create. Furthermore we nead two
passes, so the control flow in _choose_database_path would get a bit
convoluted.
---
 lib/open.cc              | 53 ++++++++++++++++++++++++++++++++++++++--
 test/T055-path-config.sh |  1 -
 test/T560-lib-error.sh   | 10 +++++---
 3 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index bc450555..caa58ff6 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -262,6 +262,45 @@ _mkdir (const char *path, char **message)
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+static notmuch_status_t
+_create_database_path (notmuch_database_t *notmuch,
+		       const char *profile,
+		       GKeyFile *key_file,
+		       const char **database_path,
+		       char **message)
+{
+    notmuch_status_t status;
+
+    if (! *database_path) {
+	*database_path = getenv ("NOTMUCH_DATABASE");
+    }
+
+    if (! *database_path && key_file) {
+	char *path = g_key_file_get_string (key_file, "database", "path", NULL);
+	if (path) {
+	    if (path[0] == '/')
+		*database_path = talloc_strdup (notmuch, path);
+	    else
+		*database_path = talloc_asprintf (notmuch, "%s/%s", getenv ("HOME"), path);
+	    g_free (path);
+	}
+    }
+
+    if (! *database_path) {
+	*database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
+	notmuch->params |= NOTMUCH_PARAM_SPLIT;
+    }
+
+    if (*database_path[0] != '/') {
+	*message = strdup ("Error: Database path must be absolute.\n");
+	return NOTMUCH_STATUS_PATH_ERROR;
+    }
+
+    if ((status = _mkdir (*database_path, message)))
+	return status;
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
 
 static notmuch_database_t *
 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
@@ -641,9 +680,19 @@ notmuch_database_create_with_config (const char *database_path,
 	goto DONE;
     }
 
-    if ((status = _choose_database_path (notmuch, profile, key_file,
-					 &database_path, &message)))
+    status = _choose_database_path (notmuch, profile, key_file,
+				    &database_path, &message);
+    switch (status) {
+    case NOTMUCH_STATUS_SUCCESS:
+	break;
+    case NOTMUCH_STATUS_NO_DATABASE:
+	if ((status = _create_database_path (notmuch, profile, key_file,
+					     &database_path, &message)))
+	    goto DONE;
+	break;
+    default:
 	goto DONE;
+    }
 
     _set_database_path (notmuch, database_path);
 
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 149aa6a1..fe295324 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -369,7 +369,6 @@ EOF
 	   ;&
        mailroot_only)
 	   test_begin_subtest "create database parent dir ($config)"
-	   test_subtest_known_broken
 	   rm -r ${DATABASE_PATH}
 	   notmuch new
 	   test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3
diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh
index 30cce943..78cae1cd 100755
--- a/test/T560-lib-error.sh
+++ b/test/T560-lib-error.sh
@@ -102,16 +102,17 @@ test_C <<'EOF'
 int main (int argc, char** argv)
 {
     notmuch_status_t stat;
-    char *msg;
+    char *msg = NULL;
 
     stat = notmuch_database_create_with_config (NULL, "", NULL, NULL, &msg);
+    printf ("%s\n", notmuch_status_to_string (stat));
     if (msg) fputs (msg, stderr);
 }
 EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
+No mail root found
 == stderr ==
-Error: could not locate database.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
@@ -123,16 +124,17 @@ int main (int argc, char** argv)
 {
     notmuch_database_t *db;
     notmuch_status_t stat;
-    char *msg;
+    char *msg = NULL;
 
     stat = notmuch_database_create_with_config (argv[1], "", NULL, &db, &msg);
+    printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
     if (msg) fputs (msg, stderr);
 }
 EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
+1
 == stderr ==
-Error: database path 'CWD/nonexistent/foo' does not exist or is not a directory.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
-- 
2.35.2