From 1cd004c9b77841089e6a3845165bdcc1302e6571 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 1 Jan 2010 15:37:39 +0200 Subject: [PATCH] * implement output-as-symlinks-to-messages functionality --- src/mu-config.c | 11 ++++---- src/mu-config.h | 23 ++++++++-------- src/mu-query.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- src/mu.c | 1 - 4 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/mu-config.c b/src/mu-config.c index 3cc590cd..e7977c1e 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -92,6 +92,8 @@ mu_config_options_group_query (MuConfigOptions *opts) "sort ascending (up)", NULL}, {"descending", 'd', 0, G_OPTION_ARG_NONE, &opts->descending, "sort descending (down)", NULL}, + {"linksdir", 't', 0, G_OPTION_ARG_STRING, &opts->linksdir, + "output as symbolic links to a target maildir", NULL }, { NULL, 0, 0, 0, NULL, NULL, NULL } }; @@ -131,6 +133,7 @@ mu_config_init (MuConfigOptions *opts) opts->sortfield = "d"; opts->ascending = FALSE; opts->descending = TRUE; + opts->linksdir = NULL; } @@ -139,11 +142,9 @@ mu_config_uninit (MuConfigOptions *opts) { g_return_if_fail (opts); - /* yes, this is fine -- they are only const - * for the 'outside world' */ - g_free ((gchar*)opts->muhome); - g_free ((gchar*)opts->maildir); - + g_free (opts->muhome); + g_free (opts->maildir); + g_free (opts->linksdir); g_strfreev (opts->params); } diff --git a/src/mu-config.h b/src/mu-config.h index 8a3b5688..e6d780b8 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -29,27 +29,28 @@ struct _MuConfigOptions { /* general options */ - gboolean quiet; /* don't give any output */ - gboolean debug; /* spew out debug info */ - const char *muhome;/* the House of Mu */ - gboolean version; /* request mu version */ - gboolean log_stderr; /*log to stderr (instead of logfile)*/ - gboolean log_append; /* append to log (instead of overwriting)*/ - gchar** params; /* parameters (for querying) */ + gboolean quiet; /* don't give any output */ + gboolean debug; /* spew out debug info */ + char *muhome;/* the House of Mu */ + gboolean version; /* request mu version */ + gboolean log_stderr; /*log to stderr (instead of logfile)*/ + gboolean log_append; /* append to log (don't overwrite)*/ + gchar** params; /* parameters (for querying) */ /* options for indexing */ - const char *maildir; /* where the mails are */ + char *maildir; /* where the mails are */ gboolean cleanup; /* cleanup deleted mails form db */ gboolean reindex; /* re-index existing mails */ /* options for querying */ gboolean xquery; /* give the Xapian query instead of search results */ - const char *fields; /* fields to show in output */ + char *fields; /* fields to show in output */ + + char *sortfield; /* field to sort by (string) */ + char *linksdir; /* maildir to output symlinks */ - const char *sortfield; /* field to sort by (string) */ - gboolean descending; /* sort descending? */ gboolean ascending; }; diff --git a/src/mu-query.c b/src/mu-query.c index ed7ba22d..d6d1ea87 100644 --- a/src/mu-query.c +++ b/src/mu-query.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "mu-util.h" #include "mu-result.h" @@ -28,6 +29,7 @@ #include "mu-msg-flags.h" #include "mu-query-xapian.h" #include "mu-query.h" +#include "mu-maildir.h" static gboolean print_query (MuQueryXapian *xapian, const gchar *query) @@ -153,7 +155,7 @@ print_rows (MuQueryXapian *xapian, const gchar *query, MuConfigOptions *opts) static gboolean -do_output (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params) +_do_output_text (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params) { gchar *query; gboolean retval = TRUE; @@ -172,6 +174,69 @@ do_output (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params) return retval; } +static gboolean +_create_linkdir_if_nonexistant (const gchar* linkdir) +{ + GError *err; + + if (access (linkdir, F_OK) != 0) { + err = NULL; + if (!mu_maildir_mkmdir (linkdir, 0700, TRUE, &err)) { + g_printerr ("error: %s", err->message); + g_error_free (err); + return FALSE; + } + } + + return TRUE; +} + +static gboolean +_do_output_links (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params) +{ + gchar *query; + gboolean retval = TRUE; + MuMsgXapian *row; + const MuMsgField *pathfield; + + if (!_create_linkdir_if_nonexistant (opts->linksdir)) + return FALSE; + + query = mu_query_xapian_combine (params, FALSE); + row = mu_query_xapian_run (xapian, query, NULL, FALSE); + if (!row) { + g_printerr ("error: running query failed\n"); + return FALSE; + } + + pathfield = mu_msg_field_from_id (MU_MSG_FIELD_ID_PATH); + + /* iterate over the found rows */ + while (!mu_msg_xapian_is_done (row)) { + const char *path; + path = mu_msg_xapian_get_field (row, pathfield); + if (path) { + GError *err = NULL; + if (!mu_maildir_link (path, opts->linksdir, &err)) { + if (err) { + g_printerr ("error: %s", err->message); + g_error_free (err); + } + return FALSE; + } + } + + mu_msg_xapian_next (row); + } + + mu_msg_xapian_destroy (row); + g_free (query); + + return retval; +} + + + MuResult @@ -188,7 +253,10 @@ mu_query_run (MuConfigOptions *opts, gchar **params) if (!xapian) return MU_ERROR; - rv = do_output (xapian, opts, params) ? 0 : 1; + if (opts->linksdir) + rv = _do_output_links (xapian, opts, params) ? 0 : 1; + else + rv = _do_output_text (xapian, opts, params) ? 0 : 1; mu_query_xapian_destroy (xapian); diff --git a/src/mu.c b/src/mu.c index cbb3296b..80e02067 100644 --- a/src/mu.c +++ b/src/mu.c @@ -297,7 +297,6 @@ main (int argc, char *argv[]) mu_index_destroy (midx); } else if (cmd == MU_CMD_QUERY) { - if (!config.params[1]) { g_printerr ("error: missing something to search for\n"); rv = 1;