~ubuntu-branches/ubuntu/utopic/dovecot/utopic-proposed

« back to all changes in this revision

Viewing changes to src/imap/cmd-copy.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-08 09:35:49 UTC
  • mfrom: (4.1.35 sid)
  • Revision ID: package-import@ubuntu.com-20140108093549-i72o93pux8p0dlaf
Tags: 1:2.2.9-1ubuntu1
* Merge from Debian unstable, remaining changes:
  + Add mail-stack-delivery package:
    - Update d/rules
    - d/control: convert existing dovecot-postfix package to a dummy
      package and add new mail-stack-delivery package.
    - Update maintainer scripts.
    - Rename d/dovecot-postfix.* to debian/mail-stack-delivery.*
    - d/mail-stack-delivery.preinst: Move previously installed backups and
      config files to a new package namespace.
    - d/mail-stack-delivery.prerm: Added to handle downgrades.
  + Use Snakeoil SSL certificates by default:
    - d/control: Depend on ssl-cert.
    - d/dovecot-core.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - d/dovecot-core.ufw.profile: new ufw profile.
    - d/rules: install profile in dovecot-core.
    - d/control: dovecot-core - suggest ufw.
  + d/dovecot-core.dirs: Added usr/share/doc/dovecot-core
  + Add apport hook:
    - d/rules, d/source_dovecot.py
  + Add upstart job:
    - d/rules, d/dovecot-core.dovecot.upstart, d/control,
      d/dovecot-core.dirs, dovecot-imapd.{postrm, postinst, prerm},
      d/dovecot-pop3d.{postinst, postrm, prerm}.
      d/mail-stack-deliver.postinst: Convert init script to upstart.
  + Use the autotools-dev dh addon to update config.guess/config.sub for
    arm64.
* Dropped changes, included in Debian:
  - Update Dovecot name to reflect distribution in login greeting.
  - Update Drac plugin for >= 2.0.0 support.
* d/control: Drop dovecot-postfix package as its no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2002-2013 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "imap-common.h"
4
4
#include "str.h"
22
22
        now = time(NULL);
23
23
        last_io = I_MAX(client->last_input, client->last_output);
24
24
        if (now - last_io > MAIL_STORAGE_STAYALIVE_SECS) {
25
 
                o_stream_send_str(client->output, "* OK Hang in there..\r\n");
26
 
                o_stream_flush(client->output);
 
25
                o_stream_nsend_str(client->output, "* OK Hang in there..\r\n");
 
26
                o_stream_nflush(client->output);
27
27
                client->last_output = now;
28
28
        }
29
29
}
30
30
 
31
 
static int fetch_and_copy(struct client *client,
 
31
static int fetch_and_copy(struct client *client, bool move,
32
32
                          struct mailbox_transaction_context *t,
 
33
                          struct mailbox_transaction_context **src_trans_r,
33
34
                          struct mail_search_args *search_args,
34
35
                          const char **src_uidset_r,
35
36
                          unsigned int *copy_count_r)
62
63
                save_ctx = mailbox_save_alloc(t);
63
64
                mailbox_save_copy_flags(save_ctx, mail);
64
65
 
65
 
                if (mailbox_copy(&save_ctx, mail) < 0)
66
 
                        ret = mail->expunged ? 0 : -1;
 
66
                if (move) {
 
67
                        if (mailbox_move(&save_ctx, mail) < 0)
 
68
                                ret = -1;
 
69
                } else {
 
70
                        if (mailbox_copy(&save_ctx, mail) < 0)
 
71
                                ret = -1;
 
72
                }
 
73
                if (ret < 0 && mail->expunged)
 
74
                        ret = 0;
67
75
 
68
76
                msgset_generator_next(&srcset_ctx, mail->uid);
69
77
        }
72
80
        if (mailbox_search_deinit(&search_ctx) < 0)
73
81
                ret = -1;
74
82
 
75
 
        if (mailbox_transaction_commit(&src_trans) < 0)
76
 
                ret = -1;
77
 
 
 
83
        *src_trans_r = src_trans;
78
84
        *src_uidset_r = str_c(src_uidset);
79
85
        *copy_count_r = copy_count;
80
86
        return ret;
81
87
}
82
88
 
83
 
bool cmd_copy(struct client_command_context *cmd)
 
89
static bool cmd_copy_full(struct client_command_context *cmd, bool move)
84
90
{
85
91
        struct client *client = cmd->client;
86
92
        struct mail_storage *dest_storage;
87
93
        struct mailbox *destbox;
88
 
        struct mailbox_transaction_context *t;
 
94
        struct mailbox_transaction_context *t, *src_trans;
89
95
        struct mail_search_args *search_args;
90
96
        const char *messageset, *mailbox, *src_uidset;
91
97
        enum mailbox_sync_flags sync_flags = 0;
114
120
        t = mailbox_transaction_begin(destbox,
115
121
                                      MAILBOX_TRANSACTION_FLAG_EXTERNAL |
116
122
                                      MAILBOX_TRANSACTION_FLAG_ASSIGN_UIDS);
117
 
        ret = fetch_and_copy(client, t, search_args, &src_uidset, &copy_count);
 
123
        ret = fetch_and_copy(client, move, t, &src_trans, search_args,
 
124
                             &src_uidset, &copy_count);
118
125
        mail_search_args_unref(&search_args);
119
126
 
120
127
        msg = t_str_new(256);
121
128
        if (ret <= 0)
122
129
                mailbox_transaction_rollback(&t);
123
 
        else if (mailbox_transaction_commit_get_changes(&t, &changes) < 0)
124
 
                ret = -1;
125
 
        else if (copy_count == 0) {
126
 
                str_append(msg, "OK No messages copied.");
127
 
                pool_unref(&changes.pool);
128
 
        } else if (seq_range_count(&changes.saved_uids) == 0) {
129
 
                /* not supported by backend (virtual) */
130
 
                str_append(msg, "OK Copy completed.");
 
130
        else if (mailbox_transaction_commit_get_changes(&t, &changes) < 0) {
 
131
                if (mailbox_get_last_mail_error(destbox) == MAIL_ERROR_EXPUNGED) {
 
132
                        /* storage backend didn't notice the expunge until
 
133
                           at commit time. */
 
134
                        ret = 0;
 
135
                } else {
 
136
                        ret = -1;
 
137
                }
 
138
        } else if (copy_count == 0) {
 
139
                str_append(msg, "OK No messages found.");
 
140
                pool_unref(&changes.pool);
 
141
        } else if (seq_range_count(&changes.saved_uids) == 0 ||
 
142
                   changes.no_read_perm) {
 
143
                /* not supported by backend (virtual) or no read permissions
 
144
                   for mailbox */
 
145
                str_append(msg, move ? "OK Move completed." :
 
146
                           "OK Copy completed.");
 
147
                pool_unref(&changes.pool);
 
148
        } else if (move) {
 
149
                i_assert(copy_count == seq_range_count(&changes.saved_uids));
 
150
 
 
151
                str_printfa(msg, "* OK [COPYUID %u %s ",
 
152
                            changes.uid_validity, src_uidset);
 
153
                imap_write_seq_range(msg, &changes.saved_uids);
 
154
                str_append(msg, "] Moved UIDs.");
 
155
                client_send_line(client, str_c(msg));
 
156
 
 
157
                str_truncate(msg, 0);
 
158
                str_append(msg, "OK Move completed.");
131
159
                pool_unref(&changes.pool);
132
160
        } else {
133
161
                i_assert(copy_count == seq_range_count(&changes.saved_uids));
139
167
                pool_unref(&changes.pool);
140
168
        }
141
169
 
142
 
        dest_storage = mailbox_get_storage(destbox);
 
170
        if (ret <= 0 && move) {
 
171
                /* move failed, don't expunge anything */
 
172
                mailbox_transaction_rollback(&src_trans);
 
173
        } else {
 
174
                if (mailbox_transaction_commit(&src_trans) < 0)
 
175
                        ret = -1;
 
176
        }
 
177
 
 
178
        dest_storage = mailbox_get_storage(destbox);
143
179
        if (destbox != client->mailbox) {
144
 
                sync_flags |= MAILBOX_SYNC_FLAG_FAST;
 
180
                if (move)
 
181
                        sync_flags |= MAILBOX_SYNC_FLAG_EXPUNGE;
 
182
                else
 
183
                        sync_flags |= MAILBOX_SYNC_FLAG_FAST;
145
184
                imap_flags |= IMAP_SYNC_FLAG_SAFE;
146
185
                mailbox_free(&destbox);
 
186
        } else if (move) {
 
187
                sync_flags |= MAILBOX_SYNC_FLAG_EXPUNGE;
 
188
                imap_flags |= IMAP_SYNC_FLAG_SAFE;
147
189
        }
148
190
 
149
191
        if (ret > 0)
158
200
                return TRUE;
159
201
        }
160
202
}
 
203
 
 
204
bool cmd_copy(struct client_command_context *cmd)
 
205
{
 
206
        return cmd_copy_full(cmd, FALSE);
 
207
}
 
208
 
 
209
bool cmd_move(struct client_command_context *cmd)
 
210
{
 
211
        return cmd_copy_full(cmd, TRUE);
 
212
}