mu: refactor add/remove commands

Most of their code is shared, so let's combine them.
This commit is contained in:
djcb
2017-01-14 12:32:20 +02:00
parent f40ffa2464
commit f91969e0b7

View File

@ -319,41 +319,52 @@ check_file_okay (const char *path, gboolean cmd_add)
} }
MuError typedef gboolean (*ForeachMsgFunc) (MuStore *store, const char *path,
mu_cmd_add (MuStore *store, MuConfig *opts, GError **err) GError **err);
{
gboolean allok;
int i;
g_return_val_if_fail (store, MU_ERROR_INTERNAL);
g_return_val_if_fail (opts, MU_ERROR_INTERNAL); static MuError
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_ADD, foreach_msg_file (MuStore *store, MuConfig *opts,
MU_ERROR_INTERNAL); ForeachMsgFunc foreach_func, GError **err)
{
unsigned u;
gboolean all_ok;
/* note: params[0] will be 'add' */ /* note: params[0] will be 'add' */
if (!opts->params[0] || !opts->params[1]) { if (!opts->params[0] || !opts->params[1]) {
g_print ("usage: mu add <file> [<files>]\n"); g_print ("usage: mu %s <file> [<files>]\n",
opts->params[0] ? opts->params[0] : "<cmd>");
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS, mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"missing source and/or target"); "missing parameters");
return MU_ERROR_IN_PARAMETERS; return MU_ERROR_IN_PARAMETERS;
} }
for (i = 1, allok = TRUE; opts->params[i]; ++i) { for (u = 1, all_ok = TRUE; opts->params[u]; ++u) {
const char* src; const char* path;
src = opts->params[i];
if (!check_file_okay (src, TRUE) || path = opts->params[u];
mu_store_add_path (store, src, NULL, err) ==
MU_STORE_INVALID_DOCID) { if (!check_file_okay (path, TRUE)) {
MU_WRITE_LOG ("failed to add %s", src); all_ok = FALSE;
allok = FALSE; MU_WRITE_LOG ("not a valid message file: %s", path);
continue;
}
if (!foreach_func (store, path, err)) {
all_ok = FALSE;
MU_WRITE_LOG ("error with %s: %s", path,
(err&&*err) ? (*err)->message :
"something went wrong");
g_clear_error (err);
continue;
} }
} }
if (!allok) { if (!all_ok) {
mu_util_g_set_error (err, MU_ERROR_XAPIAN_STORE_FAILED, mu_util_g_set_error (err, MU_ERROR_XAPIAN_STORE_FAILED,
"store failed for some message(s)"); "%s failed for some message(s)",
opts->params[0]);
return MU_ERROR_XAPIAN_STORE_FAILED; return MU_ERROR_XAPIAN_STORE_FAILED;
} }
@ -361,47 +372,46 @@ mu_cmd_add (MuStore *store, MuConfig *opts, GError **err)
} }
static gboolean
add_path_func (MuStore *store, const char *path, GError **err)
{
return mu_store_add_path (store, path, NULL, err);
}
MuError
mu_cmd_add (MuStore *store, MuConfig *opts, GError **err)
{
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);
return foreach_msg_file (store, opts, add_path_func, err);
}
static gboolean
remove_path_func (MuStore *store, const char *path, GError **err)
{
if (!mu_store_remove_path (store, path)) {
mu_util_g_set_error (err, MU_ERROR_XAPIAN_REMOVE_FAILED,
"failed to remove %s", path);
return FALSE;
}
return TRUE;
}
MuError MuError
mu_cmd_remove (MuStore *store, MuConfig *opts, GError **err) mu_cmd_remove (MuStore *store, MuConfig *opts, GError **err)
{ {
gboolean allok;
int i;
g_return_val_if_fail (opts, MU_ERROR_INTERNAL); g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_REMOVE, g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_REMOVE,
MU_ERROR_INTERNAL); MU_ERROR_INTERNAL);
/* note: params[0] will be 'remove' */ return foreach_msg_file (store, opts, remove_path_func, err);
if (!opts->params[0] || !opts->params[1]) {
g_warning ("usage: mu remove <file> [<files>]");
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"missing source and/or target");
return MU_ERROR_IN_PARAMETERS;
}
for (i = 1, allok = TRUE; opts->params[i]; ++i) {
const char* src;
src = opts->params[i];
if (!check_file_okay (src, FALSE) ||
!mu_store_remove_path (store, src)) {
allok = FALSE;
MU_WRITE_LOG ("failed to remove %s", src);
}
}
if (!allok) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_STORE_FAILED,
"remove failed for some message(s)");
return MU_ERROR_XAPIAN_REMOVE_FAILED;
}
return MU_OK;
} }
struct _VData { struct _VData {
MuMsgPartSigStatus combined_status; MuMsgPartSigStatus combined_status;
char *report; char *report;