aboutsummaryrefslogtreecommitdiff
path: root/notmuch.c
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2009-10-20 21:03:30 -0700
committerCarl Worth <cworth@cworth.org>2009-10-20 21:21:39 -0700
commit466a7bbf620e4bf1b57097a6d3c474159c475b6d (patch)
tree178d6175c4a70d6d5d3e005c6b9ec6464b99c1c9 /notmuch.c
parentcd4a8734d3bb151df70d51a84903bff994439b05 (diff)
Implement 'notmuch dump'.
This is a fairly big milestone for notmuch. It's our first command to do anything besides building the index, so it proves we can actually read valid results out from the index. It also puts in place almost all of the API and infrastructure we will need to allow searching of the database. Finally, with this change we are now using talloc inside of notmuch which is truly a delight to use. And now that I figured out how to use C++ objects with talloc allocation, (it requires grotty parts of C++ such as "placement new" and "explicit destructors"), we are valgrind-clean for "notmuch dump", (as in "no leaks are possible").
Diffstat (limited to 'notmuch.c')
-rw-r--r--notmuch.c74
1 files changed, 72 insertions, 2 deletions
diff --git a/notmuch.c b/notmuch.c
index 1ebd613..cedfebc 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -35,6 +35,8 @@
#include <dirent.h>
#include <errno.h>
+#include <talloc.h>
+
#include <glib.h> /* g_strdup_printf */
#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
@@ -370,8 +372,76 @@ show_command (int argc, char *argv[])
int
dump_command (int argc, char *argv[])
{
- fprintf (stderr, "Error: dump is not implemented yet.\n");
- return 1;
+ FILE *output;
+ notmuch_database_t *notmuch = NULL;
+ notmuch_query_t *query;
+ notmuch_results_t *results;
+ notmuch_message_t *message;
+ notmuch_tags_t *tags;
+ int ret = 0;
+
+ if (argc) {
+ output = fopen (argv[0], "w");
+ if (output == NULL) {
+ fprintf (stderr, "Error opening %s for writing: %s\n",
+ argv[1], strerror (errno));
+ ret = 1;
+ goto DONE;
+ }
+ } else {
+ output = stdout;
+ }
+
+ notmuch = notmuch_database_open (NULL);
+ if (notmuch == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ query = notmuch_query_create (notmuch, NOTMUCH_QUERY_ALL);
+ if (query == NULL) {
+ fprintf (stderr, "Out of memory\n");
+ ret = 1;
+ goto DONE;
+ }
+
+ notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
+
+ for (results = notmuch_query_search (query);
+ notmuch_results_has_more (results);
+ notmuch_results_advance (results))
+ {
+ message = notmuch_results_get (results);
+
+ fprintf (output,
+ "%s (", notmuch_message_get_message_id (message));
+
+ for (tags = notmuch_message_get_tags (message);
+ notmuch_tags_has_more (tags);
+ notmuch_tags_advance (tags))
+ {
+ int first = 1;
+
+ if (! first)
+ fprintf (output, " ");
+
+ fprintf (output, "%s", notmuch_tags_get (tags));
+
+ first = 0;
+ }
+
+ fprintf (output, ")\n");
+ }
+
+ notmuch_query_destroy (query);
+
+ DONE:
+ if (notmuch)
+ notmuch_database_close (notmuch);
+ if (output != stdout)
+ fclose (output);
+
+ return ret;
}
int