]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Added "copy" command.
authorTimo Sirainen <tss@iki.fi>
Thu, 23 Aug 2012 18:29:40 +0000 (21:29 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 23 Aug 2012 18:29:40 +0000 (21:29 +0300)
src/doveadm/Makefile.am
src/doveadm/doveadm-mail-copymove.c [moved from src/doveadm/doveadm-mail-move.c with 73% similarity]
src/doveadm/doveadm-mail.c
src/doveadm/doveadm-mail.h

index 22c54183149303989cd0318e258536d42ec4df49..7e8a31cb9e34ee19489df5f12f325f1331513d20 100644 (file)
@@ -74,7 +74,7 @@ common = \
        doveadm-mail-iter.c \
        doveadm-mail-mailbox.c \
        doveadm-mail-mailbox-status.c \
-       doveadm-mail-move.c \
+       doveadm-mail-copymove.c \
        doveadm-mailbox-list-iter.c \
        doveadm-mail-search.c \
        doveadm-mail-server.c \
similarity index 73%
rename from src/doveadm/doveadm-mail-move.c
rename to src/doveadm/doveadm-mail-copymove.c
index d0602f841b5e89ffe455733028bbb0de47580edd..0e5352f76af99017cb11539bbefa4743aa354726 100644 (file)
 
 #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;
@@ -40,9 +41,10 @@ cmd_move_box(struct move_cmd_context *ctx, struct mailbox *destbox,
        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));
@@ -52,7 +54,8 @@ cmd_move_box(struct move_cmd_context *ctx, struct mailbox *destbox,
        }
 
        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 */
@@ -66,9 +69,9 @@ cmd_move_box(struct move_cmd_context *ctx, struct mailbox *destbox,
 }
 
 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 |
@@ -97,7 +100,7 @@ cmd_move_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
        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)
@@ -114,31 +117,43 @@ cmd_move_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
 
 }
 
-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>"
 };
index e1f7157807378006b4ff559eb1bd259faa0b7ff4..1115ca89fcad18f7d3721d5c525b120a1ccb177e 100644 (file)
@@ -675,6 +675,7 @@ static struct doveadm_mail_cmd *mail_commands[] = {
        &cmd_import,
        &cmd_index,
        &cmd_altmove,
+       &cmd_copy,
        &cmd_move,
        &cmd_mailbox_list,
        &cmd_mailbox_create,
index 70c62471fe25261c980da00599aee7133c9cc1ec..612c10961bd8f89e7b49ed14c64a152d46b0aa14 100644 (file)
@@ -133,6 +133,7 @@ struct doveadm_mail_cmd cmd_fetch;
 struct doveadm_mail_cmd cmd_import;
 struct doveadm_mail_cmd cmd_index;
 struct doveadm_mail_cmd cmd_altmove;
+struct doveadm_mail_cmd cmd_copy;
 struct doveadm_mail_cmd cmd_move;
 struct doveadm_mail_cmd cmd_mailbox_list;
 struct doveadm_mail_cmd cmd_mailbox_create;