~ubuntu-branches/ubuntu/trusty/dovecot/trusty-updates

« back to all changes in this revision

Viewing changes to src/lib-storage/index/dbox-multi/mdbox-deleted-storage.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-08 09:35:49 UTC
  • mfrom: (1.15.3) (96.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140108093549-814nkqdcxfbvgktg
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) 2013 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "array.h"
 
5
#include "ioloop.h"
 
6
#include "mkdir-parents.h"
 
7
#include "master-service.h"
 
8
#include "mail-index-modseq.h"
 
9
#include "mail-index-alloc-cache.h"
 
10
#include "mailbox-log.h"
 
11
#include "mailbox-list-private.h"
 
12
#include "mail-copy.h"
 
13
#include "dbox-mail.h"
 
14
#include "dbox-save.h"
 
15
#include "mdbox-map.h"
 
16
#include "mdbox-file.h"
 
17
#include "mdbox-sync.h"
 
18
#include "mdbox-storage-rebuild.h"
 
19
#include "mdbox-storage.h"
 
20
 
 
21
extern struct mail_storage mdbox_deleted_storage;
 
22
extern struct mailbox mdbox_deleted_mailbox;
 
23
extern struct dbox_storage_vfuncs mdbox_deleted_dbox_storage_vfuncs;
 
24
 
 
25
static struct mail_storage *mdbox_deleted_storage_alloc(void)
 
26
{
 
27
        struct mdbox_storage *storage;
 
28
        pool_t pool;
 
29
 
 
30
        pool = pool_alloconly_create("mdbox deleted storage", 2048);
 
31
        storage = p_new(pool, struct mdbox_storage, 1);
 
32
        storage->storage.v = mdbox_dbox_storage_vfuncs;
 
33
        storage->storage.storage = mdbox_deleted_storage;
 
34
        storage->storage.storage.pool = pool;
 
35
        return &storage->storage.storage;
 
36
}
 
37
 
 
38
static struct mailbox *
 
39
mdbox_deleted_mailbox_alloc(struct mail_storage *storage,
 
40
                            struct mailbox_list *list,
 
41
                            const char *vname, enum mailbox_flags flags)
 
42
{
 
43
        struct mdbox_mailbox *mbox;
 
44
        pool_t pool;
 
45
 
 
46
        flags |= MAILBOX_FLAG_READONLY | MAILBOX_FLAG_NO_INDEX_FILES;
 
47
 
 
48
        pool = pool_alloconly_create("mdbox deleted mailbox", 1024*3);
 
49
        mbox = p_new(pool, struct mdbox_mailbox, 1);
 
50
        mbox->box = mdbox_deleted_mailbox;
 
51
        mbox->box.pool = pool;
 
52
        mbox->box.storage = storage;
 
53
        mbox->box.list = list;
 
54
        mbox->box.mail_vfuncs = &mdbox_mail_vfuncs;
 
55
 
 
56
        index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX);
 
57
 
 
58
        mbox->storage = (struct mdbox_storage *)storage;
 
59
        return &mbox->box;
 
60
}
 
61
 
 
62
static int
 
63
mdbox_deleted_mailbox_create_indexes(struct mailbox *box,
 
64
                                     const struct mailbox_update *update,
 
65
                                     struct mail_index_transaction *trans)
 
66
{
 
67
        struct mdbox_mailbox *mbox = (struct mdbox_mailbox *)box;
 
68
        struct mail_index_transaction *new_trans = NULL;
 
69
        uint32_t uid_validity = ioloop_time;
 
70
        uint32_t uid_next = 1;
 
71
 
 
72
        if (update != NULL && update->uid_validity != 0)
 
73
                uid_validity = update->uid_validity;
 
74
 
 
75
        if (trans == NULL) {
 
76
                new_trans = mail_index_transaction_begin(box->view, 0);
 
77
                trans = new_trans;
 
78
        }
 
79
 
 
80
        mail_index_update_header(trans,
 
81
                offsetof(struct mail_index_header, uid_validity),
 
82
                &uid_validity, sizeof(uid_validity), TRUE);
 
83
        mail_index_update_header(trans,
 
84
                offsetof(struct mail_index_header, next_uid),
 
85
                &uid_next, sizeof(uid_next), TRUE);
 
86
        mbox->creating = TRUE;
 
87
        mdbox_update_header(mbox, trans, update);
 
88
        mbox->creating = FALSE;
 
89
 
 
90
        if (new_trans != NULL) {
 
91
                if (mail_index_transaction_commit(&new_trans) < 0) {
 
92
                        mailbox_set_index_error(box);
 
93
                        return -1;
 
94
                }
 
95
        }
 
96
        return 0;
 
97
}
 
98
 
 
99
static const char *
 
100
mdbox_get_attachment_path_suffix(struct dbox_file *file ATTR_UNUSED)
 
101
{
 
102
        return "";
 
103
}
 
104
 
 
105
static int
 
106
mdbox_deleted_mailbox_get_metadata(struct mailbox *box,
 
107
                                   enum mailbox_metadata_items items,
 
108
                                   struct mailbox_metadata *metadata_r)
 
109
{
 
110
        if (index_mailbox_get_metadata(box, items, metadata_r) < 0)
 
111
                return -1;
 
112
 
 
113
        if ((items & MAILBOX_METADATA_GUID) != 0)
 
114
                guid_128_generate(metadata_r->guid);
 
115
        return 0;
 
116
}
 
117
 
 
118
static struct mail_save_context *
 
119
mdbox_deleted_save_alloc(struct mailbox_transaction_context *t)
 
120
{
 
121
        struct mail_save_context *ctx;
 
122
 
 
123
        ctx = i_new(struct mail_save_context, 1);
 
124
        ctx->transaction = t;
 
125
        return ctx;
 
126
}
 
127
 
 
128
static int
 
129
mdbox_deleted_save_begin(struct mail_save_context *ctx,
 
130
                         struct istream *input ATTR_UNUSED)
 
131
{
 
132
        mail_storage_set_error(ctx->transaction->box->storage,
 
133
                MAIL_ERROR_NOTPOSSIBLE, "mdbox_deleted doesn't support saving mails");
 
134
        return -1;
 
135
}
 
136
 
 
137
static int
 
138
mdbox_deleted_save_continue(struct mail_save_context *ctx ATTR_UNUSED)
 
139
{
 
140
        return -1;
 
141
}
 
142
 
 
143
static int mdbox_deleted_save_finish(struct mail_save_context *ctx)
 
144
{
 
145
        index_save_context_free(ctx);
 
146
        return -1;
 
147
}
 
148
 
 
149
static void
 
150
mdbox_deleted_save_cancel(struct mail_save_context *ctx)
 
151
{
 
152
        index_save_context_free(ctx);
 
153
}
 
154
 
 
155
static int mdbox_deleted_sync(struct mdbox_mailbox *mbox,
 
156
                              enum mdbox_sync_flags flags ATTR_UNUSED)
 
157
{
 
158
        struct mail_index_sync_ctx *index_sync_ctx;
 
159
        struct mail_index_view *sync_view;
 
160
        struct mail_index_transaction *trans;
 
161
        struct mdbox_mail_index_record rec;
 
162
        struct mdbox_map_mail_index_record map_rec;
 
163
        enum mail_index_sync_flags sync_flags;
 
164
        uint16_t refcount;
 
165
        uint32_t map_seq, map_count, seq, uid = 0;
 
166
        int ret = 0;
 
167
 
 
168
        if (mbox->mdbox_deleted_synced) {
 
169
                /* don't bother supporting incremental syncs */
 
170
                return 0;
 
171
        }
 
172
        if (!mbox->box.inbox_user && mbox->box.name[0] != '\0') {
 
173
                /* since mailbox list currently shows all the existing
 
174
                   mailboxes, we don't want all of them to list the deleted
 
175
                   messages. only show messages in user's INBOX or the
 
176
                   namespace prefix. */
 
177
                return 0;
 
178
        }
 
179
 
 
180
        if (mdbox_map_open(mbox->storage->map) < 0)
 
181
                return -1;
 
182
 
 
183
        if (mdbox_deleted_mailbox_create_indexes(&mbox->box, NULL, NULL) < 0)
 
184
                return -1;
 
185
 
 
186
        memset(&rec, 0, sizeof(rec));
 
187
        rec.save_date = ioloop_time;
 
188
 
 
189
        sync_flags = index_storage_get_sync_flags(&mbox->box);
 
190
        if (mail_index_sync_begin(mbox->box.index, &index_sync_ctx,
 
191
                                  &sync_view, &trans, sync_flags) < 0) {
 
192
                mailbox_set_index_error(&mbox->box);
 
193
                return -1;
 
194
        }
 
195
 
 
196
        map_count = mdbox_map_get_messages_count(mbox->storage->map);
 
197
        for (map_seq = 1; map_seq <= map_count; map_seq++) {
 
198
                if (mdbox_map_lookup_seq_full(mbox->storage->map, map_seq,
 
199
                                              &map_rec, &refcount) < 0) {
 
200
                        ret = -1;
 
201
                        break;
 
202
                }
 
203
                if (refcount == 0) {
 
204
                        rec.map_uid = mdbox_map_lookup_uid(mbox->storage->map,
 
205
                                                           map_seq);
 
206
                        mail_index_append(trans, ++uid, &seq);
 
207
                        mail_index_update_ext(trans, seq,
 
208
                                              mbox->ext_id, &rec, NULL);
 
209
                }
 
210
        }
 
211
 
 
212
        if (ret < 0)
 
213
                mail_index_sync_rollback(&index_sync_ctx);
 
214
        else {
 
215
                if (mail_index_sync_commit(&index_sync_ctx) < 0) {
 
216
                        mailbox_set_index_error(&mbox->box);
 
217
                        ret = -1;
 
218
                } else {
 
219
                        mbox->mdbox_deleted_synced = TRUE;
 
220
                }
 
221
        }
 
222
        return ret;
 
223
}
 
224
 
 
225
static struct mailbox_sync_context *
 
226
mdbox_deleted_storage_sync_init(struct mailbox *box,
 
227
                                enum mailbox_sync_flags flags)
 
228
{
 
229
        struct mdbox_mailbox *mbox = (struct mdbox_mailbox *)box;
 
230
        enum mdbox_sync_flags mdbox_sync_flags = 0;
 
231
        int ret = 0;
 
232
 
 
233
        if (!box->opened) {
 
234
                if (mailbox_open(box) < 0)
 
235
                        ret = -1;
 
236
        }
 
237
 
 
238
        if (ret == 0 && (index_mailbox_want_full_sync(&mbox->box, flags) ||
 
239
                         mbox->storage->corrupted))
 
240
                ret = mdbox_deleted_sync(mbox, mdbox_sync_flags);
 
241
 
 
242
        return index_mailbox_sync_init(box, flags, ret < 0);
 
243
}
 
244
 
 
245
struct mail_storage mdbox_deleted_storage = {
 
246
        .name = MDBOX_DELETED_STORAGE_NAME,
 
247
        .class_flags = MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT |
 
248
                MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_GUIDS |
 
249
                MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_SAVE_GUIDS |
 
250
                MAIL_STORAGE_CLASS_FLAG_BINARY_DATA,
 
251
 
 
252
        .v = {
 
253
                mdbox_get_setting_parser_info,
 
254
                mdbox_deleted_storage_alloc,
 
255
                mdbox_storage_create,
 
256
                mdbox_storage_destroy,
 
257
                NULL,
 
258
                dbox_storage_get_list_settings,
 
259
                NULL,
 
260
                mdbox_deleted_mailbox_alloc,
 
261
                NULL
 
262
        }
 
263
};
 
264
 
 
265
struct mailbox mdbox_deleted_mailbox = {
 
266
        .v = {
 
267
                index_storage_is_readonly,
 
268
                index_storage_mailbox_enable,
 
269
                index_storage_mailbox_exists,
 
270
                mdbox_mailbox_open,
 
271
                index_storage_mailbox_close,
 
272
                index_storage_mailbox_free,
 
273
                dbox_mailbox_create,
 
274
                index_storage_mailbox_update,
 
275
                index_storage_mailbox_delete,
 
276
                index_storage_mailbox_rename,
 
277
                index_storage_get_status,
 
278
                mdbox_deleted_mailbox_get_metadata,
 
279
                index_storage_set_subscribed,
 
280
                index_storage_attribute_set,
 
281
                index_storage_attribute_get,
 
282
                index_storage_attribute_iter_init,
 
283
                index_storage_attribute_iter_next,
 
284
                index_storage_attribute_iter_deinit,
 
285
                index_storage_list_index_has_changed,
 
286
                index_storage_list_index_update_sync,
 
287
                mdbox_deleted_storage_sync_init,
 
288
                index_mailbox_sync_next,
 
289
                index_mailbox_sync_deinit,
 
290
                NULL,
 
291
                dbox_notify_changes,
 
292
                index_transaction_begin,
 
293
                index_transaction_commit,
 
294
                index_transaction_rollback,
 
295
                NULL,
 
296
                dbox_mail_alloc,
 
297
                index_storage_search_init,
 
298
                index_storage_search_deinit,
 
299
                index_storage_search_next_nonblock,
 
300
                index_storage_search_next_update_seq,
 
301
                mdbox_deleted_save_alloc,
 
302
                mdbox_deleted_save_begin,
 
303
                mdbox_deleted_save_continue,
 
304
                mdbox_deleted_save_finish,
 
305
                mdbox_deleted_save_cancel,
 
306
                mail_storage_copy,
 
307
                NULL,
 
308
                NULL,
 
309
                NULL,
 
310
                index_storage_is_inconsistent
 
311
        }
 
312
};
 
313
 
 
314
struct dbox_storage_vfuncs mdbox_deleted_dbox_storage_vfuncs = {
 
315
        mdbox_file_unrefed,
 
316
        mdbox_file_create_fd,
 
317
        mdbox_mail_open,
 
318
        mdbox_deleted_mailbox_create_indexes,
 
319
        mdbox_get_attachment_path_suffix,
 
320
        mdbox_set_mailbox_corrupted,
 
321
        mdbox_set_file_corrupted
 
322
};