From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id +NOYHdVHHWA8PwAA0tVLHw (envelope-from ) for ; Fri, 05 Feb 2021 13:27:49 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id SJRzGdVHHWCIIwAA1q6Kng (envelope-from ) for ; Fri, 05 Feb 2021 13:27:49 +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) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 20861940309 for ; Fri, 5 Feb 2021 13:27:49 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 3C0671FBC0; Fri, 5 Feb 2021 08:27:17 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 470B01FAEC for ; Fri, 5 Feb 2021 08:27:08 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 41C156067F; Fri, 5 Feb 2021 08:27:08 -0500 (EST) Received: (nullmailer pid 3258452 invoked by uid 1000); Fri, 05 Feb 2021 13:27:02 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 36/39] lib/open: set HOOK_DIR on open Date: Fri, 5 Feb 2021 09:26:51 -0400 Message-Id: <20210205132654.3258292-37-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205132654.3258292-1-david@tethera.net> References: <20210205132654.3258292-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: C7SOWGL2MQUT42WHDXTFYHE6IRY7JYF7 X-Message-ID-Hash: C7SOWGL2MQUT42WHDXTFYHE6IRY7JYF7 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 X-Migadu-Spam-Score: -1.04 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: 20861940309 X-Spam-Score: -1.04 X-Migadu-Scanner: scn0.migadu.com X-TUID: OF+URlokZdg6 This is a simple two step path search. Most error checking is deferred until running the hooks. --- lib/open.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ test/T590-libconfig.sh | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/lib/open.cc b/lib/open.cc index 577fc88a..b4637ec5 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -68,6 +68,44 @@ _xdg_dir (void *ctx, profile_name); } +static notmuch_status_t +_choose_hook_dir (notmuch_database_t *notmuch, + const char *profile, + char **message) +{ + const char *config; + const char *hook_dir; + struct stat st; + int err; + + hook_dir = notmuch_config_get (notmuch, NOTMUCH_CONFIG_HOOK_DIR); + + if (hook_dir) + return NOTMUCH_STATUS_SUCCESS; + + config = _xdg_dir (notmuch, "XDG_CONFIG_HOME", ".config", profile); + if (! config) + return NOTMUCH_STATUS_PATH_ERROR; + + hook_dir = talloc_asprintf (notmuch, "%s/hooks", config); + + err = stat (hook_dir, &st); + if (err) { + if (errno == ENOENT) { + const char *database_path = notmuch_database_get_path (notmuch); + hook_dir = talloc_asprintf (notmuch, "%s/.notmuch/hooks", database_path); + } else { + IGNORE_RESULT (asprintf (message, "Error: Cannot stat %s: %s.\n", + hook_dir, strerror (errno))); + return NOTMUCH_STATUS_FILE_ERROR; + } + } + + _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_HOOK_DIR, hook_dir); + + return NOTMUCH_STATUS_SUCCESS; +} + static notmuch_status_t _load_key_file (const char *path, const char *profile, @@ -301,6 +339,10 @@ notmuch_database_open_with_config (const char *database_path, if (status) goto DONE; + status = _choose_hook_dir (notmuch, profile, &message); + if (status) + goto DONE; + status = _notmuch_config_load_defaults (notmuch); if (status) goto DONE; diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index e2050b50..e7e6e08a 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -201,6 +201,37 @@ EOF test_expect_equal_file EXPECTED OUTPUT restore_database +test_begin_subtest "NOTMUCH_CONFIG_HOOK_DIR: traditional" +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% +{ + const char *val = notmuch_config_get (db, NOTMUCH_CONFIG_HOOK_DIR); + printf("database.hook_dir = %s\n", val); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +database.hook_dir = MAIL_DIR/.notmuch/hooks +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "NOTMUCH_CONFIG_HOOK_DIR: xdg" +dir="${HOME}/.config/notmuch/default/hooks" +mkdir -p $dir +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% +{ + const char *val = notmuch_config_get (db, NOTMUCH_CONFIG_HOOK_DIR); + printf("database.hook_dir = %s\n", val); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +database.hook_dir = CWD/home/.config/notmuch/default/hooks +== stderr == +EOF +rmdir $dir +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest "notmuch_config_get_values" cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% { @@ -333,7 +364,7 @@ EOF cat <<'EOF' >EXPECTED == stdout == MAIL_DIR -NULL +MAIL_DIR/.notmuch/hooks inbox;unread NULL -- 2.30.0