annotate src/plugins/trash/trash-plugin.c @ 8294:8aa69e3d27ef HEAD

Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately. Also previous optimization change broke trash plugin when using multiple mail_users. This change fixes it to work again.
author Timo Sirainen <tss@iki.fi>
date Sat, 18 Oct 2008 21:31:18 +0300
parents 944f709797df
children d3cbd3494a8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7086
7ed926ed7aa4 Updated copyright notices to include year 2008.
Timo Sirainen <tss@iki.fi>
parents: 6512
diff changeset
1 /* Copyright (c) 2005-2008 Dovecot authors, see the included COPYING file */
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "array.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "istream.h"
5577
1fd9616157e4 Updated to support namespaces
Timo Sirainen <tss@iki.fi>
parents: 5297
diff changeset
6 #include "mail-namespace.h"
7642
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
7 #include "mail-search-build.h"
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "quota-private.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "quota-plugin.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "trash-plugin.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include <stdlib.h>
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include <unistd.h>
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include <fcntl.h>
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
5187
9dd18b285223 Minor memory allocation optimizations.
Timo Sirainen <tss@iki.fi>
parents: 5185
diff changeset
16 #define INIT_TRASH_MAILBOX_COUNT 4
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #define MAX_RETRY_COUNT 3
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
19 #define TRASH_USER_CONTEXT(obj) \
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
20 MODULE_CONTEXT(obj, trash_user_module)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
21
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 struct trash_mailbox {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 const char *name;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 int priority; /* lower number = higher priority */
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25
8287
944f709797df Trash plugin: Small optimization.
Timo Sirainen <tss@iki.fi>
parents: 8137
diff changeset
26 struct mail_storage *storage;
944f709797df Trash plugin: Small optimization.
Timo Sirainen <tss@iki.fi>
parents: 8137
diff changeset
27
8082
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 7642
diff changeset
28 /* temporarily set while cleaning: */
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 struct mailbox *box;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 struct mailbox_transaction_context *trans;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 struct mail_search_context *search_ctx;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 struct mail *mail;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 unsigned int mail_set:1;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 };
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
37 struct trash_user {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
38 union mail_user_module_context module_ctx;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
39
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
40 /* ordered by priority, highest first */
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
41 ARRAY_DEFINE(trash_boxes, struct trash_mailbox);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
42 };
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
43
5185
24f4a959a24c Added <plugin_name>_version string.
Timo Sirainen <tss@iki.fi>
parents: 5158
diff changeset
44 const char *trash_plugin_version = PACKAGE_VERSION;
24f4a959a24c Added <plugin_name>_version string.
Timo Sirainen <tss@iki.fi>
parents: 5158
diff changeset
45
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
46 static MODULE_CONTEXT_DEFINE_INIT(trash_user_module,
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
47 &mail_user_module_register);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
48 static void (*trash_next_hook_mail_namespaces_created)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
49 (struct mail_namespace *namespaces);
4514
511f1f5c76fa Changes to make trash plugin working again.
Timo Sirainen <tss@iki.fi>
parents: 4451
diff changeset
50 static int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
511f1f5c76fa Changes to make trash plugin working again.
Timo Sirainen <tss@iki.fi>
parents: 4451
diff changeset
51 uoff_t, bool *);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 static int trash_clean_mailbox_open(struct trash_mailbox *trash)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 {
7642
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
55 struct mail_search_args *search_args;
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
56
8287
944f709797df Trash plugin: Small optimization.
Timo Sirainen <tss@iki.fi>
parents: 8137
diff changeset
57 trash->box = mailbox_open(trash->storage, trash->name, NULL,
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 MAILBOX_OPEN_KEEP_RECENT);
5147
6b0032df7008 If trash mailbox can't be opened, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
59 if (trash->box == NULL)
5188
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
60 return 0;
5147
6b0032df7008 If trash mailbox can't be opened, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
61
6463
aeee5076f99f Use mailbox_sync() instead of our own implementation.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
62 if (mailbox_sync(trash->box, MAILBOX_SYNC_FLAG_FULL_READ, 0, NULL) < 0)
4748
5fb199ed254a Trash plugin was completely broken before.
Timo Sirainen <tss@iki.fi>
parents: 4602
diff changeset
63 return -1;
5fb199ed254a Trash plugin was completely broken before.
Timo Sirainen <tss@iki.fi>
parents: 4602
diff changeset
64
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 trash->trans = mailbox_transaction_begin(trash->box, 0);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 trash->mail = mail_alloc(trash->trans, MAIL_FETCH_PHYSICAL_SIZE |
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 MAIL_FETCH_RECEIVED_DATE, NULL);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68
7642
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
69 search_args = mail_search_build_init();
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
70 mail_search_build_add_all(search_args);
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
71 trash->search_ctx = mailbox_search_init(trash->trans,
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
72 search_args, NULL);
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
73 mail_search_args_unref(&search_args);
077bb84e9e77 Make mail_search_args an independent structure that can be used for multiple
Timo Sirainen <tss@iki.fi>
parents: 7287
diff changeset
74
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 return mailbox_search_next(trash->search_ctx, trash->mail);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 static int trash_clean_mailbox_get_next(struct trash_mailbox *trash,
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 time_t *received_time_r)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 int ret;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 if (!trash->mail_set) {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 if (trash->box == NULL)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 ret = trash_clean_mailbox_open(trash);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 else
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 ret = mailbox_search_next(trash->search_ctx,
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 trash->mail);
5188
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
89 if (ret <= 0) {
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
90 *received_time_r = 0;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 return ret;
5188
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
92 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 trash->mail_set = TRUE;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
96 if (mail_get_received_date(trash->mail, received_time_r) < 0)
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
97 return -1;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 return 1;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
101 static int trash_try_clean_mails(struct quota_transaction_context *ctx,
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
102 uint64_t size_needed)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 {
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
104 struct trash_user *tuser = TRASH_USER_CONTEXT(ctx->quota->user);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 struct trash_mailbox *trashes;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 unsigned int i, j, count, oldest_idx;
5297
67b8d61c988f compiler warning fix
Timo Sirainen <tss@iki.fi>
parents: 5189
diff changeset
107 time_t oldest, received = 0;
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
108 uint64_t size, size_expunged = 0, expunged_count = 0;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 int ret = 0;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
111 trashes = array_get_modifiable(&tuser->trash_boxes, &count);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 for (i = 0; i < count; ) {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 /* expunge oldest mails first in all trash boxes with
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 same priority */
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 oldest_idx = count;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 oldest = (time_t)-1;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 for (j = i; j < count; j++) {
5189
7ec060a7c29f Fixed priority handling.
Timo Sirainen <tss@iki.fi>
parents: 5188
diff changeset
118 if (trashes[j].priority != trashes[i].priority)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 break;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120
5188
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
121 ret = trash_clean_mailbox_get_next(&trashes[j],
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 &received);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 if (ret < 0)
6412
8ebc07d2eee9 Get rid of __ prefixes.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
124 goto err;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 if (ret > 0) {
5188
519ab252f1a3 Fixes for handling multiple storages.
Timo Sirainen <tss@iki.fi>
parents: 5187
diff changeset
126 if (oldest == (time_t)-1 || received < oldest) {
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 oldest = received;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 oldest_idx = j;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 if (oldest_idx < count) {
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
134 if (mail_get_physical_size(trashes[oldest_idx].mail,
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
135 &size) < 0) {
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
136 /* maybe expunged already? */
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
137 trashes[oldest_idx].mail_set = FALSE;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
138 continue;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
139 }
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
140
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6162
diff changeset
141 mail_expunge(trashes[oldest_idx].mail);
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
142 expunged_count++;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
143 size_expunged += size;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
144 if (size_expunged >= size_needed)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 break;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 trashes[oldest_idx].mail_set = FALSE;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 } else {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 /* find more mails from next priority's mailbox */
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 i = j;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152
6412
8ebc07d2eee9 Get rid of __ prefixes.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
153 err:
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 for (i = 0; i < count; i++) {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 struct trash_mailbox *trash = &trashes[i];
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156
5157
df22aec605b7 If trash mailbox couldn't be opened, don't crash later either.
Timo Sirainen <tss@iki.fi>
parents: 5147
diff changeset
157 if (trash->box == NULL)
df22aec605b7 If trash mailbox couldn't be opened, don't crash later either.
Timo Sirainen <tss@iki.fi>
parents: 5147
diff changeset
158 continue;
df22aec605b7 If trash mailbox couldn't be opened, don't crash later either.
Timo Sirainen <tss@iki.fi>
parents: 5147
diff changeset
159
4748
5fb199ed254a Trash plugin was completely broken before.
Timo Sirainen <tss@iki.fi>
parents: 4602
diff changeset
160 trash->mail_set = FALSE;
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
161 mail_free(&trash->mail);
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
162 (void)mailbox_search_deinit(&trash->search_ctx);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
6512
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
164 if (size_expunged >= size_needed)
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
165 (void)mailbox_transaction_commit(&trash->trans);
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
166 else {
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 /* couldn't get enough space, don't expunge anything */
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
168 mailbox_transaction_rollback(&trash->trans);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
171 mailbox_close(&trash->box);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 }
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
173
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
174 if (size_expunged < size_needed) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
175 if (getenv("DEBUG") != NULL) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
176 i_info("trash plugin: Failed to remove enough messages "
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
177 "(needed %llu bytes, expunged only %llu bytes)",
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
178 (unsigned long long)size_needed,
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
179 (unsigned long long)size_expunged);
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
180 }
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
181 return FALSE;
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
182 }
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
183
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
184 ctx->bytes_used = ctx->bytes_used > (int64_t)size_expunged ?
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
185 ctx->bytes_used - size_expunged : 0;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
186 ctx->count_used = ctx->count_used > (int64_t)expunged_count ?
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
187 ctx->count_used - expunged_count : 0;
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
188 return TRUE;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 static int
4514
511f1f5c76fa Changes to make trash plugin working again.
Timo Sirainen <tss@iki.fi>
parents: 4451
diff changeset
192 trash_quota_test_alloc(struct quota_transaction_context *ctx,
511f1f5c76fa Changes to make trash plugin working again.
Timo Sirainen <tss@iki.fi>
parents: 4451
diff changeset
193 uoff_t size, bool *too_large_r)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 int ret, i;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 for (i = 0; ; i++) {
4514
511f1f5c76fa Changes to make trash plugin working again.
Timo Sirainen <tss@iki.fi>
parents: 4451
diff changeset
198 ret = trash_next_quota_test_alloc(ctx, size, too_large_r);
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
199 if (ret != 0 || *too_large_r) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
200 if (getenv("DEBUG") != NULL && *too_large_r) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
201 i_info("trash plugin: Mail is larger than "
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
202 "quota, won't even try to handle");
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
203 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 return ret;
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
205 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207 if (i == MAX_RETRY_COUNT) {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 /* trash_try_clean_mails() should have returned 0 if
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 it couldn't get enough space, but allow retrying
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 it a couple of times if there was some extra space
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 that was needed.. */
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 break;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 /* not enough space. try deleting some from mailbox. */
5158
aa9b1fec1c21 Although messages were expunged, the expunges weren't counted in that same
Timo Sirainen <tss@iki.fi>
parents: 5157
diff changeset
216 ret = trash_try_clean_mails(ctx, size);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 if (ret <= 0)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 return 0;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 return 0;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
224 static bool trash_find_storage(struct mail_user *user,
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
225 struct trash_mailbox *trash)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
226 {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
227 struct mail_namespace *ns;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
228 const char *name;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
229
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
230 for (ns = user->namespaces; ns != NULL; ns = ns->next) {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
231 name = trash->name;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
232 if (mail_namespace_update_name(ns, &name)) {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
233 if (name != trash->name)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
234 trash->name = p_strdup(user->pool, name);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
235 trash->storage = ns->storage;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
236 return TRUE;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
237 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
238 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
239 return FALSE;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
240 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
241
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 static int trash_mailbox_priority_cmp(const void *p1, const void *p2)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243 {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244 const struct trash_mailbox *t1 = p1, *t2 = p2;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 return t1->priority - t2->priority;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
249 static int read_configuration(struct mail_user *user, const char *path)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 {
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
251 struct trash_user *tuser = TRASH_USER_CONTEXT(user);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 struct istream *input;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 const char *line, *name;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 struct trash_mailbox *trash;
4602
444edc8abef9 Don't allow giving NULL parameter to array_get() or array_get_modifiable().
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
255 unsigned int count;
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
256 int fd, ret = 0;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 fd = open(path, O_RDONLY);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259 if (fd == -1) {
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
260 i_error("trash plugin: open(%s) failed: %m", path);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 return -1;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
264 p_array_init(&tuser->trash_boxes, user->pool, INIT_TRASH_MAILBOX_COUNT);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265
6162
896cc473c1f0 Renamed i_stream_create_file() to i_stream_create_fd().
Timo Sirainen <tss@iki.fi>
parents: 6142
diff changeset
266 input = i_stream_create_fd(fd, (size_t)-1, FALSE);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 while ((line = i_stream_read_next_line(input)) != NULL) {
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 /* <priority> <mailbox name> */
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269 name = strchr(line, ' ');
7287
75526c605044 Allow #comments in trash config
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
270 if (name == NULL || name[1] == '\0' || *line == '#')
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 continue;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
273 trash = array_append_space(&tuser->trash_boxes);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
274 trash->name = p_strdup(user->pool, name+1);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 trash->priority = atoi(t_strdup_until(line, name));
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
276
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
277 if (!trash_find_storage(user, trash)) {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
278 i_error("trash: Namespace not found for mailbox '%s'",
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
279 trash->name);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
280 ret = -1;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
281 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
282
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
283 if (getenv("DEBUG") != NULL) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
284 i_info("trash plugin: Added '%s' with priority %d",
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
285 trash->name, trash->priority);
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
286 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 }
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
288 i_stream_destroy(&input);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289 (void)close(fd);
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
291 trash = array_get_modifiable(&tuser->trash_boxes, &count);
4602
444edc8abef9 Don't allow giving NULL parameter to array_get() or array_get_modifiable().
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
292 qsort(trash, count, sizeof(*trash), trash_mailbox_priority_cmp);
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
293 return ret;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
296 static void
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
297 trash_hook_mail_namespaces_created(struct mail_namespace *namespaces)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 {
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
299 struct mail_user *user = namespaces->user;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
300 struct trash_user *tuser;
4400
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
301 const char *env;
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
302
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
303 if (trash_next_hook_mail_namespaces_created != NULL)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
304 trash_next_hook_mail_namespaces_created(namespaces);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
305
4400
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
306 env = getenv("TRASH");
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
307 if (env == NULL) {
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
308 if (getenv("DEBUG") != NULL)
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
309 i_info("trash plugin: No trash setting, disabled");
4400
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
310 return;
6024
6074e9e6059d Added debug logging.
Timo Sirainen <tss@iki.fi>
parents: 5577
diff changeset
311 }
4400
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
312
4796
49c9e7588de4 s/quota/quota_set/ so we don't conflict with the quota symbol already
Timo Sirainen <tss@iki.fi>
parents: 4748
diff changeset
313 if (quota_set == NULL) {
4400
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
314 i_error("trash plugin: quota plugin not initialized");
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
315 return;
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
316 }
593523f53500 Removed hardcoded trash plugin configuration paths. Added information about
Timo Sirainen <tss@iki.fi>
parents: 4084
diff changeset
317
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
318 tuser = p_new(user->pool, struct trash_user, 1);
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
319 tuser->module_ctx.super = user->v;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
320 MODULE_CONTEXT_SET(user, trash_user_module, tuser);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
322 if (read_configuration(user, env) == 0) {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
323 trash_next_quota_test_alloc = quota_set->test_alloc;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
324 quota_set->test_alloc = trash_quota_test_alloc;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
325 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
326 }
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
327
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
328 void trash_plugin_init(void)
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
329 {
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
330 trash_next_hook_mail_namespaces_created = hook_mail_namespaces_created;
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
331 hook_mail_namespaces_created = trash_hook_mail_namespaces_created;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 void trash_plugin_deinit(void)
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335 {
8294
8aa69e3d27ef Trash plugin: Assign storage to all mailboxes at startup so errors are caught immediately.
Timo Sirainen <tss@iki.fi>
parents: 8287
diff changeset
336 hook_mail_namespaces_created = trash_hook_mail_namespaces_created;
4796
49c9e7588de4 s/quota/quota_set/ so we don't conflict with the quota symbol already
Timo Sirainen <tss@iki.fi>
parents: 4748
diff changeset
337 quota_set->test_alloc = trash_next_quota_test_alloc;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 }