]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-config.c
notmuch: Abort if specified configuration file is not found.
[notmuch] / notmuch-config.c
index 7252a191373c5cc89f3f47df80ebd67766e8946f..cc05f6c414971019b9d799cd3fc81874079a57b4 100644 (file)
@@ -132,9 +132,10 @@ get_username_from_passwd_file (void *ctx)
     return name;
 }
 
     return name;
 }
 
-/* Open the named notmuch configuration file. A filename of NULL will
- * be interpreted as the default configuration file
- * ($HOME/.notmuch-config).
+/* Open the named notmuch configuration file. If the filename is NULL,
+ * the value of the environment variable $NOTMUCH_CONFIG will be used.
+ * If $NOTMUCH_CONFIG is unset, the default configuration file
+ * ($HOME/.notmuch-config) will be used.
  *
  * If any error occurs, (out of memory, or a permission-denied error,
  * etc.), this function will print a message to stderr and return
  *
  * If any error occurs, (out of memory, or a permission-denied error,
  * etc.), this function will print a message to stderr and return
@@ -168,6 +169,7 @@ notmuch_config_open (void *ctx,
 {
     GError *error = NULL;
     int is_new = 0;
 {
     GError *error = NULL;
     int is_new = 0;
+    char *notmuch_config_env = NULL;
 
     if (is_new_ret)
        *is_new_ret = 0;
 
     if (is_new_ret)
        *is_new_ret = 0;
@@ -180,11 +182,14 @@ notmuch_config_open (void *ctx,
     
     talloc_set_destructor (config, notmuch_config_destructor);
 
     
     talloc_set_destructor (config, notmuch_config_destructor);
 
-    if (filename)
+    if (filename) {
        config->filename = talloc_strdup (config, filename);
        config->filename = talloc_strdup (config, filename);
-    else
+    } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
+       config->filename = talloc_strdup (config, notmuch_config_env);
+    } else {
        config->filename = talloc_asprintf (config, "%s/.notmuch-config",
                                            getenv ("HOME"));
        config->filename = talloc_asprintf (config, "%s/.notmuch-config",
                                            getenv ("HOME"));
+    }
 
     config->key_file = g_key_file_new ();
 
 
     config->key_file = g_key_file_new ();
 
@@ -200,16 +205,22 @@ notmuch_config_open (void *ctx,
                                     &error))
     {
        /* We are capable of dealing with a non-existent configuration
                                     &error))
     {
        /* We are capable of dealing with a non-existent configuration
-        * file, so be silent about that. */
-       if (!(error->domain == G_FILE_ERROR &&
+        * file, so be silent about that (unless the user had set a
+        * non-default configuration file with the NOTMUCH_CONFIG
+        * variable)
+        */
+       if (notmuch_config_env ||
+           !(error->domain == G_FILE_ERROR &&
              error->code == G_FILE_ERROR_NOENT))
        {
            fprintf (stderr, "Error reading configuration file %s: %s\n",
                     config->filename, error->message);
            talloc_free (config);
              error->code == G_FILE_ERROR_NOENT))
        {
            fprintf (stderr, "Error reading configuration file %s: %s\n",
                     config->filename, error->message);
            talloc_free (config);
+           g_error_free (error);
            return NULL;
        }
 
            return NULL;
        }
 
+       g_error_free (error);
        is_new = 1;
     }
 
        is_new = 1;
     }
 
@@ -308,9 +319,12 @@ notmuch_config_save (notmuch_config_t *config)
     if (! g_file_set_contents (config->filename, data, length, &error)) {
        fprintf (stderr, "Error saving configuration to %s: %s\n",
                 config->filename, error->message);
     if (! g_file_set_contents (config->filename, data, length, &error)) {
        fprintf (stderr, "Error saving configuration to %s: %s\n",
                 config->filename, error->message);
+       g_error_free (error);
+       g_free (data);
        return 1;
     }
 
        return 1;
     }
 
+    g_free (data);
     return 0;
 }
 
     return 0;
 }