#include <stdio.h>
-struct move_cmd_context {
+struct copy_cmd_context {
struct doveadm_mail_cmd_context ctx;
const char *destname;
+ bool move;
};
static int
-cmd_move_box(struct move_cmd_context *ctx, struct mailbox *destbox,
+cmd_copy_box(struct copy_cmd_context *ctx, struct mailbox *destbox,
const struct mailbox_info *info)
{
struct doveadm_mail_iter *iter;
while (doveadm_mail_iter_next(iter, &mail)) {
save_ctx = mailbox_save_alloc(desttrans);
mailbox_save_copy_flags(save_ctx, mail);
- if (mailbox_copy(&save_ctx, mail) == 0)
- mail_expunge(mail);
- else {
+ if (mailbox_copy(&save_ctx, mail) == 0) {
+ if (ctx->move)
+ mail_expunge(mail);
+ } else {
i_error("Copying message UID %u from '%s' failed: %s",
mail->uid, info->name,
mailbox_get_last_error(destbox, NULL));
}
if (mailbox_transaction_commit(&desttrans) < 0) {
- i_error("Committing moved mails failed: %s",
+ i_error("Committing %s mails failed: %s",
+ ctx->move ? "moved" : "copied",
mailbox_get_last_error(destbox, NULL));
doveadm_mail_failed_mailbox(&ctx->ctx, destbox);
/* rollback expunges */
}
static int
-cmd_move_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
+cmd_copy_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
{
- struct move_cmd_context *ctx = (struct move_cmd_context *)_ctx;
+ struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
const enum mailbox_list_iter_flags iter_flags =
MAILBOX_LIST_ITER_RAW_LIST |
MAILBOX_LIST_ITER_NO_AUTO_BOXES |
iter = doveadm_mailbox_list_iter_init(_ctx, user, _ctx->search_args,
iter_flags);
while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
- if (cmd_move_box(ctx, destbox, info) < 0)
+ if (cmd_copy_box(ctx, destbox, info) < 0)
ret = -1;
} T_END;
if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
}
-static void cmd_move_init(struct doveadm_mail_cmd_context *_ctx,
+static void cmd_copy_init(struct doveadm_mail_cmd_context *_ctx,
const char *const args[])
{
- struct move_cmd_context *ctx = (struct move_cmd_context *)_ctx;
- const char *destname = args[0];
+ struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
+ const char *destname = args[0], *cmdname = ctx->move ? "move" : "copy";
if (destname == NULL || args[1] == NULL)
- doveadm_mail_help_name("move");
+ doveadm_mail_help_name(cmdname);
ctx->destname = p_strdup(ctx->ctx.pool, destname);
ctx->ctx.search_args = doveadm_mail_build_search_args(args + 1);
- expunge_search_args_check(ctx->ctx.search_args, "move");
+ expunge_search_args_check(ctx->ctx.search_args, cmdname);
}
-static struct doveadm_mail_cmd_context *cmd_move_alloc(void)
+static struct doveadm_mail_cmd_context *cmd_copy_alloc(void)
{
- struct move_cmd_context *ctx;
+ struct copy_cmd_context *ctx;
- ctx = doveadm_mail_cmd_alloc(struct move_cmd_context);
- ctx->ctx.v.init = cmd_move_init;
- ctx->ctx.v.run = cmd_move_run;
+ ctx = doveadm_mail_cmd_alloc(struct copy_cmd_context);
+ ctx->ctx.v.init = cmd_copy_init;
+ ctx->ctx.v.run = cmd_copy_run;
doveadm_print_init(DOVEADM_PRINT_TYPE_FLOW);
return &ctx->ctx;
}
+static struct doveadm_mail_cmd_context *cmd_move_alloc(void)
+{
+ struct copy_cmd_context *ctx;
+
+ ctx = (struct copy_cmd_context *)cmd_copy_alloc();
+ ctx->move = TRUE;
+ return &ctx->ctx;
+}
+
+struct doveadm_mail_cmd cmd_copy = {
+ cmd_copy_alloc, "copy", "<destination> <search query>"
+};
struct doveadm_mail_cmd cmd_move = {
cmd_move_alloc, "move", "<destination> <search query>"
};