diff --git a/src/mu-cmd-cfind.c b/src/mu-cmd-cfind.c index 502e5f84..8acb5337 100644 --- a/src/mu-cmd-cfind.c +++ b/src/mu-cmd-cfind.c @@ -36,7 +36,7 @@ print_header (MuConfigFormat format) { switch (format) { case MU_CONFIG_FORMAT_BBDB: - g_print (";; -*-coding: utf-8-emacs;-*-\n" + g_print (";; -*-coding: utf-8-emacs;-*-\n" ";;; file-version: 6\n"); break; case MU_CONFIG_FORMAT_MUTT_AB: @@ -56,7 +56,7 @@ each_contact_bbdb (const char *email, const char *name, time_t tstamp) lname = mu_str_guess_last_name (name); now = mu_date_str ("%Y-%m-%d", time(NULL)); timestamp = mu_date_str ("%Y-%m-%d", tstamp); - + g_print ("[\"%s\" \"%s\" nil nil nil nil (\"%s\") " "((creation-date . \"%s\") (time-stamp . \"%s\")) nil]\n", fname, lname, email, now, timestamp); @@ -64,7 +64,7 @@ each_contact_bbdb (const char *email, const char *name, time_t tstamp) g_free (now); g_free (timestamp); g_free (fname); - g_free (lname); + g_free (lname); } @@ -78,7 +78,7 @@ each_contact_mutt_alias (const char *email, const char *name) nick = mu_str_guess_nick (name); mu_util_print_encoded ("alias %s %s <%s>\n", - nick, name, email); + nick, name, email); g_free (nick); } @@ -155,7 +155,7 @@ each_contact (const char *email, const char *name, time_t tstamp, break; case MU_CONFIG_FORMAT_BBDB: each_contact_bbdb (email, name, tstamp); - break; + break; case MU_CONFIG_FORMAT_CSV: mu_util_print_encoded ("%s,%s\n", name ? name : "", email); break; @@ -173,7 +173,7 @@ run_cmd_cfind (const char* pattern, MuConfigFormat format, MuContacts *contacts; size_t num; ECData ecdata = {format, color}; - + contacts = mu_contacts_new (mu_runtime_path(MU_RUNTIME_PATH_CONTACTS)); if (!contacts) { g_warning ("could not retrieve contacts"); diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index 47b371a9..96bc87be 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -61,7 +61,7 @@ static void upgrade_warning (void) { g_warning ("the database needs to be updated to version %s\n", - MU_XAPIAN_DB_VERSION); + MU_STORE_SCHEMA_VERSION); g_message ("please run 'mu index --rebuild' (see the man page)"); } @@ -227,8 +227,20 @@ exec_cmd_on_query (MuQuery *xapian, const gchar *query, MuConfig *opts, for (rv = TRUE, *count = 0; !mu_msg_iter_is_done (iter); mu_msg_iter_next(iter)) { - rv = exec_cmd (mu_msg_get_path (mu_msg_iter_get_msg (iter, NULL)), - opts->exec); + const char *path; + MuMsg *msg; + + msg = mu_msg_iter_get_msg_floating (iter); + if (!msg) + continue; + + path = mu_msg_get_path (msg); + if (!msg) { + g_warning ("cannot get path for msg"); + continue; + } + + rv = exec_cmd (path, opts->exec); if (rv) ++*count; } @@ -346,15 +358,15 @@ get_query (MuConfig *opts) } static gboolean -db_is_ready (const char *xpath) +db_is_ready (MuStore *store) { - if (mu_store_database_is_empty (xpath)) { + if (mu_store_count (store) == 0) { g_warning ("database is empty; use 'mu index' to " "add messages"); return FALSE; } - if (mu_store_database_needs_upgrade (xpath)) { + if (mu_store_needs_upgrade (store)) { upgrade_warning (); return FALSE; } @@ -363,20 +375,18 @@ db_is_ready (const char *xpath) } static MuQuery* -get_query_obj (void) +get_query_obj (MuStore *store) { GError *err; - const char* xpath; MuQuery *mquery; - xpath = mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB); - if (!db_is_ready(xpath)) { - g_warning ("database '%s' is not ready", xpath); + if (!db_is_ready(store)) { + g_warning ("database is not ready"); return NULL; } err = NULL; - mquery = mu_query_new (xpath, &err); + mquery = mu_query_new (store, &err); if (!mquery) { g_warning ("error: %s", err->message); g_error_free (err); @@ -445,8 +455,7 @@ static gboolean output_links (MuMsgIter *iter, const char* linksdir, gboolean clearlinks, size_t *count) { - size_t mycount; - gboolean errseen; + size_t okcount, errcount; MuMsgIter *myiter; g_return_val_if_fail (iter, FALSE); @@ -456,31 +465,26 @@ output_links (MuMsgIter *iter, const char* linksdir, if (!create_linksdir_maybe (linksdir, clearlinks)) return FALSE; - for (myiter = iter, errseen = FALSE, mycount = 0; !mu_msg_iter_is_done (myiter); + for (myiter = iter, errcount = okcount = 0; !mu_msg_iter_is_done (myiter); mu_msg_iter_next (myiter)) { - MuMsg *msg; const char* path; + MuMsg *msg; - msg = mu_msg_iter_get_msg (iter, NULL); /* don't unref */ + msg = mu_msg_iter_get_msg_floating (iter); if (!msg) - return FALSE; - - path = mu_msg_get_path (msg); - if (!access (path, R_OK)) /* only link to readable */ continue; - if (!link_message (path, linksdir)) - errseen = TRUE; - else - ++mycount; + path = mu_msg_get_path (msg); + if (access (path, R_OK) == 0) /* only link to readable */ + link_message (path, linksdir) ? ++okcount : ++errcount; } - if (errseen) + if (errcount > 0) g_warning ("error linking some of the messages"); if (count) - *count = mycount; + *count = okcount; return TRUE; } @@ -594,8 +598,8 @@ thread_indent (MuMsgIter *iter) ti = mu_msg_iter_get_thread_info (iter); if (!ti) { - g_warning ("cannot get thread-info for %s", - mu_msg_get_subject(mu_msg_iter_get_msg(iter, NULL))); + g_warning ("cannot get thread-info for message %u", + mu_msg_iter_get_docid (iter)); return; } @@ -665,15 +669,12 @@ output_plain (MuMsgIter *iter, const char *fields, gboolean summary, for (myiter = iter, mycount = 0; !mu_msg_iter_is_done (myiter); mu_msg_iter_next (myiter)) { - size_t len; MuMsg *msg; - msg = mu_msg_iter_get_msg (iter, NULL); /* don't unref */ - if (!msg) { - g_warning ("can't get message"); + msg = mu_msg_iter_get_msg_floating (iter); /* don't unref */ + if (!msg) continue; - } /* only return messages if they're actually * readable (ie, live also outside the database) */ @@ -732,7 +733,7 @@ output_sexp (MuMsgIter *iter, gboolean threads, char *sexp; const MuMsgIterThreadInfo *ti; - msg = mu_msg_iter_get_msg (iter, NULL); /* don't unref */ + msg = mu_msg_iter_get_msg_floating (iter); if (!msg) return FALSE; @@ -790,8 +791,11 @@ output_xml (MuMsgIter *iter, gboolean include_unreadable, size_t *count) for (myiter = iter, mycount = 0; !mu_msg_iter_is_done (myiter); mu_msg_iter_next (myiter)) { + GError *err; MuMsg *msg; - msg = mu_msg_iter_get_msg (iter, NULL); /* don't unref */ + + err = NULL; + msg = mu_msg_iter_get_msg_floating (iter); /* don't unref */ if (!msg) return FALSE; @@ -801,7 +805,6 @@ output_xml (MuMsgIter *iter, gboolean include_unreadable, size_t *count) continue; output_xml_msg (msg); - ++mycount; } g_print ("\n"); @@ -814,7 +817,7 @@ output_xml (MuMsgIter *iter, gboolean include_unreadable, size_t *count) MuError -mu_cmd_find (MuConfig *opts) +mu_cmd_find (MuStore *store, MuConfig *opts) { MuQuery *xapian; gboolean rv; @@ -828,7 +831,7 @@ mu_cmd_find (MuConfig *opts) if (!query_params_valid (opts) || !format_params_valid(opts)) return MU_ERROR_IN_PARAMETERS; - xapian = get_query_obj(); + xapian = get_query_obj(store); query = get_query (opts); if (!xapian ||!query) diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index 157fc100..17f9462b 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -43,7 +43,7 @@ static void update_warning (void) { g_warning ("note: the database needs to be upgraded to version %s", - MU_XAPIAN_DB_VERSION); + MU_STORE_SCHEMA_VERSION); g_warning ("please run 'mu index --rebuild' (see the manpage)"); } @@ -195,33 +195,28 @@ index_msg_cb (MuIndexStats* stats, void *user_data) static gboolean -database_version_check_and_update (MuConfig *opts) +database_version_check_and_update (MuStore *store, MuConfig *opts) { - const gchar *xpath, *ccache; - - xpath = mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB); - ccache = mu_runtime_path (MU_RUNTIME_PATH_CONTACTS); - - if (mu_store_database_is_empty (xpath)) + if (mu_store_count (store) == 0) return TRUE; /* when rebuilding, we empty the database before doing * anything */ if (opts->rebuild) { opts->reindex = TRUE; - g_message ("clearing database [%s]", xpath); - g_message ("clearing contacts-cache [%s]", ccache); - return mu_store_database_clear (xpath, ccache); + g_message ("clearing database"); + g_message ("clearing contacts-cache"); + return mu_store_clear (store); } - if (!mu_store_database_needs_upgrade (xpath)) + if (!mu_store_needs_upgrade (store)) return TRUE; /* ok, nothing to do */ /* ok, database is not up to date */ if (opts->autoupgrade) { opts->reindex = TRUE; g_message ("auto-upgrade: clearing old database and cache"); - return mu_store_database_clear (xpath, ccache); + return mu_store_clear (store); } update_warning (); @@ -368,7 +363,7 @@ handle_index_error_and_free (GError *err) } static MuIndex* -init_mu_index (MuConfig *opts, MuError *code) +init_mu_index (MuStore *store, MuConfig *opts, MuError *code) { MuIndex *midx; GError *err; @@ -378,15 +373,13 @@ init_mu_index (MuConfig *opts, MuError *code) return NULL; } - if (!database_version_check_and_update(opts)) { + if (!database_version_check_and_update(store, opts)) { *code = MU_ERROR_XAPIAN_NOT_UP_TO_DATE; return NULL; } err = NULL; - midx = mu_index_new (mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB), - mu_runtime_path (MU_RUNTIME_PATH_CONTACTS), - &err); + midx = mu_index_new (store, &err); if (!midx) { *code = handle_index_error_and_free (err); return NULL; @@ -400,7 +393,7 @@ init_mu_index (MuConfig *opts, MuError *code) static MuError -cmd_index_or_cleanup (MuConfig *opts) +cmd_index_or_cleanup (MuStore *store, MuConfig *opts) { MuIndex *midx; MuIndexStats stats; @@ -408,7 +401,7 @@ cmd_index_or_cleanup (MuConfig *opts) MuError code; /* create, and do error handling if needed */ - midx = init_mu_index (opts, &code); + midx = init_mu_index (store, opts, &code); if (!midx) return code; @@ -442,23 +435,23 @@ cmd_index_or_cleanup (MuConfig *opts) MuError -mu_cmd_index (MuConfig *opts) +mu_cmd_index (MuStore *store, MuConfig *opts) { g_return_val_if_fail (opts, FALSE); g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_INDEX, FALSE); - return cmd_index_or_cleanup (opts); + return cmd_index_or_cleanup (store, opts); } MuError -mu_cmd_cleanup (MuConfig *opts) +mu_cmd_cleanup (MuStore *store, MuConfig *opts) { g_return_val_if_fail (opts, MU_ERROR_INTERNAL); g_return_val_if_fail (opts->cmd != MU_CONFIG_CMD_CLEANUP, MU_ERROR_INTERNAL); - return cmd_index_or_cleanup (opts); + return cmd_index_or_cleanup (store, opts); } diff --git a/src/mu-cmd.c b/src/mu-cmd.c index a63f01bb..52177ee8 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -395,36 +395,15 @@ check_file_okay (const char *path, gboolean cmd_add) } -static MuStore* -get_store (void) -{ - MuStore *store; - GError *err; - - err = NULL; - store = mu_store_new_writable - (mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), - mu_runtime_path(MU_RUNTIME_PATH_CONTACTS), - &err); - if (!store) { - if (err) { - g_warning ("store error: %s", err->message); - g_error_free (err); - } else - g_warning ("failed to create store object"); - } - - return store; -} MuError -mu_cmd_add (MuConfig *opts) +mu_cmd_add (MuStore *store, MuConfig *opts) { - MuStore *store; gboolean allok; int i; + g_return_val_if_fail (store, MU_ERROR_INTERNAL); g_return_val_if_fail (opts, MU_ERROR_INTERNAL); g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_ADD, MU_ERROR_INTERNAL); @@ -435,10 +414,6 @@ mu_cmd_add (MuConfig *opts) return MU_ERROR_IN_PARAMETERS; } - store = get_store (); - if (!store) - return MU_ERROR_INTERNAL; - for (i = 1, allok = TRUE; opts->params[i]; ++i) { const char* src; @@ -455,16 +430,13 @@ mu_cmd_add (MuConfig *opts) } } - mu_store_destroy (store); - return allok ? MU_OK : MU_ERROR; } MuError -mu_cmd_remove (MuConfig *opts) +mu_cmd_remove (MuStore *store, MuConfig *opts) { - MuStore *store; gboolean allok; int i; @@ -478,10 +450,6 @@ mu_cmd_remove (MuConfig *opts) return MU_ERROR_IN_PARAMETERS; } - store = get_store (); - if (!store) - return MU_ERROR_INTERNAL; - for (i = 1, allok = TRUE; opts->params[i]; ++i) { const char* src; @@ -498,10 +466,107 @@ mu_cmd_remove (MuConfig *opts) } } - mu_store_destroy (store); - return allok ? MU_OK : MU_ERROR; } +static void +show_usage (gboolean noerror) +{ + const char* usage= + "usage: mu command [options] [parameters]\n" + "where command is one of index, find, cfind, view, mkdir, cleanup, " + "extract, mv, add, remove or server\n\n" + "see the mu, mu- or mu-easy manpages for " + "more information\n"; + if (noerror) + g_print ("%s", usage); + else + g_printerr ("%s", usage); +} + +static void +show_version (void) +{ + g_print ("mu (mail indexer/searcher) version " VERSION "\n" + "Copyright (C) 2008-2011 Dirk-Jan C. Binnema (GPLv3+)\n"); +} + +typedef MuError (*store_func) (MuStore *, MuConfig *); + +MuError +with_store (store_func func, MuConfig *opts, gboolean read_only) +{ + MuStore *store; + GError *err; + + err = NULL; + + if (read_only) + store = mu_store_new_read_only + (mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), + &err); + else + store = mu_store_new_writable + (mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), + mu_runtime_path(MU_RUNTIME_PATH_CONTACTS), + &err); + if (!store) { + if (err) { + g_warning ("store error: %s", err->message); + g_error_free (err); + } + return MU_ERROR_XAPIAN; + + } else { + MuError merr; + + merr = func (store, opts); + mu_store_unref (store); + + return merr; + } +} + + +MuError +mu_cmd_execute (MuConfig *opts) +{ + g_return_val_if_fail (opts, MU_ERROR_INTERNAL); + + if (opts->version) { + show_version (); + return MU_OK; + } + + if (!opts->params||!opts->params[0]) {/* no command? */ + show_version (); + show_usage (TRUE); + return MU_ERROR_IN_PARAMETERS; + } + + switch (opts->cmd) { + case MU_CONFIG_CMD_CFIND: return mu_cmd_cfind (opts); + case MU_CONFIG_CMD_MKDIR: return mu_cmd_mkdir (opts); + case MU_CONFIG_CMD_MV: return mu_cmd_mv (opts); + case MU_CONFIG_CMD_VIEW: return mu_cmd_view (opts); + case MU_CONFIG_CMD_EXTRACT: return mu_cmd_extract (opts); + + case MU_CONFIG_CMD_CLEANUP: + return with_store (mu_cmd_cleanup, opts, FALSE); + case MU_CONFIG_CMD_FIND: + return with_store (mu_cmd_find, opts, TRUE); + case MU_CONFIG_CMD_INDEX: + return with_store (mu_cmd_index, opts, FALSE); + case MU_CONFIG_CMD_ADD: + return with_store (mu_cmd_add, opts, FALSE); + case MU_CONFIG_CMD_REMOVE: + return with_store (mu_cmd_remove, opts, FALSE); + case MU_CONFIG_CMD_SERVER: + return with_store (mu_cmd_server, opts, FALSE); + default: + show_usage (FALSE); + return MU_ERROR_IN_PARAMETERS; + } +} diff --git a/src/mu-cmd.h b/src/mu-cmd.h index 7ef017f8..12ec1ede 100644 --- a/src/mu-cmd.h +++ b/src/mu-cmd.h @@ -25,14 +25,15 @@ #include #include +#include G_BEGIN_DECLS /** * execute the 'mkdir' command - * + * * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeded, * some error code otherwise */ @@ -41,9 +42,9 @@ MuError mu_cmd_mkdir (MuConfig *opts); /** * execute the 'view' command - * + * * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeded, * some error code otherwise */ @@ -55,11 +56,11 @@ MuError mu_cmd_view (MuConfig *opts); * * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeded, * some error code otherwise */ -MuError mu_cmd_index (MuConfig *opts); +MuError mu_cmd_index (MuStore *store, MuConfig *opts); /** @@ -67,29 +68,30 @@ MuError mu_cmd_index (MuConfig *opts); * * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ -MuError mu_cmd_cleanup (MuConfig *opts); +MuError mu_cmd_cleanup (MuStore *store, MuConfig *opts); /** * execute the 'find' command - * + * + * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds and * >MU_OK (0) results, MU_EXITCODE_NO_MATCHES if the command * succeeds but there no matches, some error code for all other errors */ -MuError mu_cmd_find (MuConfig *opts); +MuError mu_cmd_find (MuStore *store, MuConfig *opts); /** * execute the 'extract' command - * + * * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ @@ -98,9 +100,9 @@ MuError mu_cmd_extract (MuConfig *opts); /** * execute the 'mv' command - * + * * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ @@ -108,9 +110,9 @@ MuError mu_cmd_mv (MuConfig *opts); /** * execute the cfind command - * + * * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ @@ -119,36 +121,47 @@ MuError mu_cmd_cfind (MuConfig *opts); /** * execute the add command - * + * + * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ -MuError mu_cmd_add (MuConfig *opts); +MuError mu_cmd_add (MuStore *store, MuConfig *opts); /** * execute the remove command - * + * + * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ -MuError mu_cmd_remove (MuConfig *opts); +MuError mu_cmd_remove (MuStore *store, MuConfig *opts); /** * execute the server command - * + * @param store store object to use * @param opts configuration options - * + * * @return MU_OK (0) if the command succeeds, * some error code otherwise */ -MuError mu_cmd_server (MuConfig *opts); +MuError mu_cmd_server (MuStore *store, MuConfig *opts); +/** + * execute some mu command, based on 'opts' + * + * @param opts configuration option + * + * @return MU_OK if all went wall, some error code otherwise + */ +MuError mu_cmd_execute (MuConfig *opts); + G_END_DECLS #endif /*__MU_CMD_H__*/