From 13569ad6c96c3f3a653ac51274056614647ad7ef Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 11 Nov 2009 20:29:30 -0800 Subject: [PATCH] Unbreak several notmuch commands after the addition of configuration. 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 | 51 +++++++++++++++++++++---------------------- notmuch-reply.c | 45 +++++++++++++++----------------------- notmuch-restore.c | 32 +++++++++++++-------------- notmuch-search.c | 36 +++++++++++++++---------------- notmuch-show.c | 44 ++++++++++++++++--------------------- notmuch-tag.c | 55 ++++++++++++++++++----------------------------- 6 files changed, 114 insertions(+), 149 deletions(-) diff --git a/notmuch-dump.c b/notmuch-dump.c index ae77338e..e1fe7235 100644 --- a/notmuch-dump.c +++ b/notmuch-dump.c @@ -23,41 +23,40 @@ 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; } diff --git a/notmuch-reply.c b/notmuch-reply.c index 35a735f1..fe8ee178 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -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; } diff --git a/notmuch-restore.c b/notmuch-restore.c index 87c68c1f..18cbece6 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -23,34 +23,35 @@ 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 (®ex); - 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; } diff --git a/notmuch-search.c b/notmuch-search.c index 4d2c7361..3873a067 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -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; } diff --git a/notmuch-show.c b/notmuch-show.c index b5db3df9..19787feb 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -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; } diff --git a/notmuch-tag.c b/notmuch-tag.c index b9cbb77d..ada52e45 100644 --- a/notmuch-tag.c +++ b/notmuch-tag.c @@ -23,34 +23,27 @@ 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; } -- 2.26.2