doveadm-mail-copymove.c revision a18da4410dcd47ab8d9b40c09a76a54fa55b9c86
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen struct mail_storage_service_user *source_service_user;
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainencmd_copy_box(struct copy_cmd_context *ctx, struct mailbox *destbox,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct mailbox_transaction_context *desttrans;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (doveadm_mail_iter_init(&ctx->ctx, info, ctx->ctx.search_args, 0,
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen /* use a separately committed transaction for each mailbox.
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen this guarantees that mails aren't expunged without actually having
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen been copied. */
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen desttrans = mailbox_transaction_begin(destbox,
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_error("Copying message UID %u from '%s' failed: %s",
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen doveadm_mail_failed_mailbox(&ctx->ctx, destbox);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen if (mailbox_transaction_commit(&desttrans) < 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen doveadm_mail_failed_mailbox(&ctx->ctx, destbox);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* rollback expunges */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainencmd_copy_alloc_source_user(struct copy_cmd_context *ctx)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (mail_storage_service_lookup_next(ctx->ctx.storage_service, &input,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_fatal("Couldn't lookup user %s: %s", input.username, error);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainencmd_copy_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const enum mailbox_list_iter_flags iter_flags =
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (ctx->source_username != NULL && ctx->source_user == NULL)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen ns = mail_namespace_find(user->namespaces, ctx->destname);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen destbox = mailbox_alloc(ns->list, ctx->destname, MAILBOX_FLAG_SAVEONLY);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen i_error("Can't open mailbox '%s': %s", ctx->destname,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen doveadm_mail_failed_mailbox(&ctx->ctx, destbox);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen src_user = ctx->source_user != NULL ? ctx->source_user : user;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen iter = doveadm_mailbox_list_iter_init(_ctx, src_user, _ctx->search_args,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_error("Syncing mailbox '%s' failed: %s", ctx->destname,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen doveadm_mail_failed_mailbox(&ctx->ctx, destbox);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainenstatic void cmd_copy_init(struct doveadm_mail_cmd_context *_ctx,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen const char *const args[])
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen const char *destname = args[0], *cmdname = ctx->move ? "move" : "copy";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) == 0)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_fatal("Use -u parameter to specify destination user");
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen ctx->source_username = p_strdup(_ctx->pool, args[1]);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen ctx->destname = p_strdup(ctx->ctx.pool, destname);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen _ctx->search_args = doveadm_mail_build_search_args(args);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen expunge_search_args_check(ctx->ctx.search_args, cmdname);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainenstatic void cmd_copy_deinit(struct doveadm_mail_cmd_context *_ctx)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct copy_cmd_context *ctx = (struct copy_cmd_context *)_ctx;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mail_storage_service_user_free(&ctx->source_service_user);
27586e4785d56aeb76e1fd96af8db799688dc64aTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_copy_alloc(void)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct copy_cmd_context);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_move_alloc(void)
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk ctx = (struct copy_cmd_context *)cmd_copy_alloc();
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstruct doveadm_cmd_ver2 doveadm_cmd_copy_ver2 = {
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<destination> [user <source user>] <search query>",
1701e3f91107051b1704721bf1dc1e32491faaf9Timo SirainenDOVEADM_CMD_PARAM('\0', "destination-mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
b365bd121cdc87f63e1dd47c5085a27091118e00Timo SirainenDOVEADM_CMD_PARAM('\0', "source-type", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo SirainenDOVEADM_CMD_PARAM('\0', "source-user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
adb6413686e52e00dded4932babcc08ff041876bTimo SirainenDOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenstruct doveadm_cmd_ver2 doveadm_cmd_move_ver2 = {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<destination> [user <source user>] <search query>",
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo SirainenDOVEADM_CMD_PARAM('\0', "destination-mailbox", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo SirainenDOVEADM_CMD_PARAM('\0', "source-type", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo SirainenDOVEADM_CMD_PARAM('\0', "source-user", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)