]> git.cworth.org Git - notmuch/commitdiff
Unbreak several notmuch commands after the addition of configuration.
authorCarl Worth <cworth@cworth.org>
Thu, 12 Nov 2009 04:29:30 +0000 (20:29 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 12 Nov 2009 04:29:30 +0000 (20:29 -0800)
All of the following commands:

notmuch dump
notmuch reply
notmuch restore
notmuch search
notmuch show
notmuch tag

were calling notmuch_database_open with an argument of NULL. This was
a legitimate call until the recent addition of configuration, after
which it is expected that all commands will lookup the correct path in
the configuration file. So fix all these commands to do that.

Also, while touching all of these commands, we fix them to use the
talloc context that is passed in rather than creating a local talloc
context. We also switch from using goto for return values, to doing
direct returns as soon as an error is detected, (which can be leak
free thanks to talloc).

notmuch-dump.c
notmuch-reply.c
notmuch-restore.c
notmuch-search.c
notmuch-show.c
notmuch-tag.c

index ae77338e68cd0dbc6131b1982c3f9b52aefd1819..e1fe72356dd09d1dfb4511c1685ad31928473ccf 100644 (file)
 int
 notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
 {
-    FILE *output = NULL;
-    notmuch_database_t *notmuch = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
     notmuch_query_t *query;
+    FILE *output;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     notmuch_tags_t *tags;
-    int ret = 0;
+
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
+
+    query = notmuch_query_create (notmuch, "");
+    if (query == NULL) {
+       fprintf (stderr, "Out of memory\n");
+       return 1;
+    }
+    notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
 
     if (argc) {
        output = fopen (argv[0], "w");
        if (output == NULL) {
            fprintf (stderr, "Error opening %s for writing: %s\n",
                     argv[0], strerror (errno));
-           ret = 1;
-           goto DONE;
+           return 1;
        }
     } else {
        output = stdout;
     }
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
-
-    query = notmuch_query_create (notmuch, "");
-    if (query == NULL) {
-       fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
-    }
-
-    notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
-
     for (messages = notmuch_query_search_messages (query);
         notmuch_messages_has_more (messages);
         notmuch_messages_advance (messages))
@@ -85,13 +84,11 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
        notmuch_message_destroy (message);
     }
 
-    notmuch_query_destroy (query);
-
-  DONE:
-    if (notmuch)
-       notmuch_database_close (notmuch);
-    if (output && output != stdout)
+    if (output != stdout)
        fclose (output);
 
-    return ret;
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
+
+    return 0;
 }
index 35a735f13f321924eec34ac9d0a2b26c74df04a9..fe8ee1784a7fd33c7975ef85adc74bf68033d46e 100644 (file)
@@ -123,10 +123,10 @@ add_recipients_for_string (GMimeMessage *message,
 int
 notmuch_reply_command (void *ctx, int argc, char *argv[])
 {
-    void *local = talloc_new (ctx);
-    notmuch_query_t *query = NULL;
-    notmuch_database_t *notmuch = NULL;
-    GMimeMessage *reply = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
+    GMimeMessage *reply;
     char *query_string;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
@@ -145,24 +145,24 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
     };
     unsigned int i;
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
 
-    query_string = query_string_from_args (local, argc, argv);
+    query_string = query_string_from_args (ctx, argc, argv);
     if (query_string == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     for (messages = notmuch_query_search_messages (query);
@@ -175,8 +175,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
        reply = g_mime_message_new (1);
        if (reply == NULL) {
            fprintf (stderr, "Out of memory\n");
-           ret = 1;
-           goto DONE;
+           return 1;
        }
 
        /* XXX: We need a configured email address (or addresses) for
@@ -230,18 +229,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
        notmuch_message_destroy (message);
     }
 
-  DONE:
-    if (local)
-       talloc_free (local);
-
-    if (query)
-       notmuch_query_destroy (query);
-
-    if (notmuch)
-       notmuch_database_close (notmuch);
-
-    if (reply)
-       g_object_unref (G_OBJECT (reply));
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
 
     return ret;
 }
index 87c68c1f1adaa109394b51409ca87145e966d25e..18cbece6442b3abbbeef636bdbbe164d8a949b6a 100644 (file)
 int
 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 {
-    FILE *input = NULL;
-    notmuch_database_t *notmuch = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    FILE *input;
     char *line = NULL;
     size_t line_size;
     ssize_t line_len;
     regex_t regex;
     int rerr;
-    int ret = 0;
+
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
 
     if (argc) {
        input = fopen (argv[0], "r");
        if (input == NULL) {
            fprintf (stderr, "Error opening %s for reading: %s\n",
                     argv[0], strerror (errno));
-           ret = 1;
-           goto DONE;
+           return 1;
        }
     } else {
        printf ("No filename given. Reading dump from stdin.\n");
        input = stdin;
     }
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
-
     /* Dump output is one line per message. We match a sequence of
      * non-space characters for the message-id, then one or more
      * spaces, then a list of space-separated tags as a sequence of
@@ -118,13 +119,12 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 
     regfree (&regex);
 
-  DONE:
     if (line)
        free (line);
-    if (notmuch)
-       notmuch_database_close (notmuch);
-    if (input && input != stdin)
+
+    notmuch_database_close (notmuch);
+    if (input != stdin)
        fclose (input);
 
-    return ret;
+    return 0;
 }
index 4d2c73616768fc58e955e87fd9d8ce818da5195e..3873a067cb59d5c21173ecdb23778848c6707a2b 100644 (file)
@@ -23,8 +23,8 @@
 int
 notmuch_search_command (void *ctx, int argc, char *argv[])
 {
-    void *local = talloc_new (ctx);
-    notmuch_database_t *notmuch = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
     notmuch_query_t *query;
     notmuch_threads_t *threads;
     notmuch_thread_t *thread;
@@ -32,21 +32,25 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     char *query_str;
     const char *relative_date;
     time_t date;
-    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
 
-    query_str = query_string_from_args (local, argc, argv);
+    query_str = query_string_from_args (ctx, argc, argv);
+    if (query_str == NULL) {
+       fprintf (stderr, "Out of moemory.\n");
+       return 1;
+    }
 
     query = notmuch_query_create (notmuch, query_str);
     if (query == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     for (threads = notmuch_query_search_threads (query);
@@ -58,7 +62,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
        thread = notmuch_threads_get (threads);
 
        date = notmuch_thread_get_oldest_date (thread);
-       relative_date = notmuch_time_relative_date (local, date);
+       relative_date = notmuch_time_relative_date (ctx, date);
 
        printf ("thread:%s %12s %s",
                notmuch_thread_get_thread_id (thread),
@@ -81,11 +85,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     }
 
     notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
 
-  DONE:
-    if (notmuch)
-       notmuch_database_close (notmuch);
-    talloc_free (local);
-
-    return ret;
+    return 0;
 }
index b5db3df95aa2652be9e1f5ffa0f0d7f241894696..19787feb2c17cd5dec629f0fc8b2ba35d1fe2dfc 100644 (file)
@@ -120,13 +120,12 @@ show_part(GMimeObject *part, int *part_count)
 int
 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 {
-    void *local = talloc_new (ctx);
-    char *query_string;
-    notmuch_database_t *notmuch = NULL;
-    notmuch_query_t *query = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
-    int ret = 0;
+    char *query_string;
 
     const char *headers[] = {
        "From", "To", "Cc", "Bcc", "Date"
@@ -134,24 +133,24 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     const char *name, *value;
     unsigned int i;
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
 
-    query_string = query_string_from_args (local, argc, argv);
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
+
+    query_string = query_string_from_args (ctx, argc, argv);
     if (query_string == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
        fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     for (messages = notmuch_query_search_messages (query);
@@ -166,7 +165,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 
        printf ("\fheader{\n");
 
-       printf ("%s\n", _get_one_line_summary (local, message));
+       printf ("%s\n", _get_one_line_summary (ctx, message));
 
        printf ("%s\n", notmuch_message_get_header (message, "subject"));
 
@@ -189,15 +188,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        notmuch_message_destroy (message);
     }
 
-  DONE:
-    if (local)
-       talloc_free (local);
-
-    if (query)
-       notmuch_query_destroy (query);
-
-    if (notmuch)
-       notmuch_database_close (notmuch);
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
 
-    return ret;
+    return 0;
 }
index b9cbb77d243f2a4b6534f8dc0e17ff9001c1c406..ada52e45d4295a1a6dffed64edea60bec411aa01 100644 (file)
 int
 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
 {
-    void *local;
     int *add_tags, *remove_tags;
     int add_tags_count = 0;
     int remove_tags_count = 0;
     char *query_string;
-    notmuch_database_t *notmuch = NULL;
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
     notmuch_query_t *query;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
-    int ret = 0;
     int i;
 
-    local = talloc_new (ctx);
-    if (local == NULL) {
-       ret = 1;
-       goto DONE;
-    }
-
-    add_tags = talloc_size (local, argc * sizeof (int));
+    add_tags = talloc_size (ctx, argc * sizeof (int));
     if (add_tags == NULL) {
-       ret = 1;
-       goto DONE;
+       fprintf (stderr, "Out of memory.\n");
+       return 1;
     }
 
-    remove_tags = talloc_size (local, argc * sizeof (int));
+    remove_tags = talloc_size (ctx, argc * sizeof (int));
     if (remove_tags == NULL) {
-       ret = 1;
-       goto DONE;
+       fprintf (stderr, "Out of memory.\n");
+       return 1;
     }
 
     for (i = 0; i < argc; i++) {
@@ -69,29 +62,28 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
 
     if (add_tags_count == 0 && remove_tags_count == 0) {
        fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     if (i == argc) {
        fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+       return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+       return 1;
 
-    query_string = query_string_from_args (local, argc - i, &argv[i]);
+    query_string = query_string_from_args (ctx, argc - i, &argv[i]);
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
        fprintf (stderr, "Out of memory.\n");
-       ret = 1;
-       goto DONE;
+       return 1;
     }
 
     for (messages = notmuch_query_search_messages (query);
@@ -115,12 +107,7 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
     }
 
     notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
 
-  DONE:
-    if (notmuch)
-       notmuch_database_close (notmuch);
-
-    talloc_free (local);
-
-    return ret;
+    return 0;
 }