* implement output-as-symlinks-to-messages functionality

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-01 15:37:39 +02:00
parent 366c1ea44f
commit 1cd004c9b7
4 changed files with 88 additions and 19 deletions

View File

@ -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);
}

View File

@ -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;
};

View File

@ -21,6 +21,7 @@
#include <glib/gstdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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);

View File

@ -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;