]> git.notmuchmail.org Git - notmuch/commitdiff
lib/open: set HOOK_DIR on open
authorDavid Bremner <david@tethera.net>
Sat, 9 Jan 2021 19:05:58 +0000 (15:05 -0400)
committerDavid Bremner <david@tethera.net>
Sat, 6 Feb 2021 23:56:13 +0000 (19:56 -0400)
This is a simple two step path search.  Most error checking is
deferred until running the hooks.

lib/open.cc
test/T590-libconfig.sh

index 577fc88a40879267e9572e699ee904421cc959e6..b4637ec5445cba1ffbb93bd9cde237f0e0a3e327 100644 (file)
@@ -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;
index e2050b50c73b5945137aeb45a0fa213d23979aed..e7e6e08a6fd387ba2dfad2867bfb89d76f0247ec 100755 (executable)
@@ -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