Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib-storage/mail-storage.c @ 903:fd8888f6f037 HEAD
Naming style changes, finally got tired of most of the typedefs. Also the
previous enum -> macro change reverted so that we don't use the highest bit
anymore, that's incompatible with old indexes so they will be rebuilt.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 05 Jan 2003 15:09:51 +0200 |
parents | 39e0b536e708 |
children | 340dea0150bf |
rev | line source |
---|---|
0 | 1 /* Copyright (C) 2002 Timo Sirainen */ |
2 | |
3 #include "lib.h" | |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
4 #include "ioloop.h" |
0 | 5 #include "mail-storage.h" |
6 | |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
7 #include <time.h> |
0 | 8 #include <ctype.h> |
9 | |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
10 /* Message to show to users when critical error occurs */ |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
11 #define CRITICAL_MSG "Internal error [%Y-%m-%d %H:%M:%S]" |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
12 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
13 struct mail_storage_list { |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
14 struct mail_storage_list *next; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
15 struct mail_storage *storage; |
0 | 16 }; |
17 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
18 static struct mail_storage_list *storages = NULL; |
0 | 19 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
20 void mail_storage_class_register(struct mail_storage *storage_class) |
0 | 21 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
22 struct mail_storage_list *list, **pos; |
0 | 23 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
24 list = i_new(struct mail_storage_list, 1); |
0 | 25 list->storage = storage_class; |
26 | |
27 /* append it after the list, so the autodetection order is correct */ | |
28 pos = &storages; | |
29 while (*pos != NULL) | |
30 pos = &(*pos)->next; | |
31 *pos = list; | |
32 } | |
33 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
34 void mail_storage_class_unregister(struct mail_storage *storage_class) |
0 | 35 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
36 struct mail_storage_list **list, *next; |
0 | 37 |
38 for (list = &storages; *list != NULL; list = &(*list)->next) { | |
39 if ((*list)->storage == storage_class) { | |
40 next = (*list)->next; | |
41 | |
42 (*list)->storage->free((*list)->storage); | |
43 i_free(*list); | |
44 | |
45 *list = next; | |
46 } | |
47 } | |
48 } | |
49 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
50 struct mail_storage *mail_storage_create(const char *name, const char *data, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
51 const char *user) |
0 | 52 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
53 struct mail_storage_list *list; |
0 | 54 |
55 i_assert(name != NULL); | |
56 | |
57 for (list = storages; list != NULL; list = list->next) { | |
58 if (strcasecmp(list->storage->name, name) == 0) | |
22
a946ce1f09b7
mbox fixes, not fully working yet but almost :)
Timo Sirainen <tss@iki.fi>
parents:
13
diff
changeset
|
59 return list->storage->create(data, user); |
0 | 60 } |
61 | |
62 return NULL; | |
63 } | |
64 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
65 struct mail_storage *mail_storage_create_default(const char *user) |
0 | 66 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
67 struct mail_storage_list *list; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
68 struct mail_storage *storage; |
0 | 69 |
70 for (list = storages; list != NULL; list = list->next) { | |
22
a946ce1f09b7
mbox fixes, not fully working yet but almost :)
Timo Sirainen <tss@iki.fi>
parents:
13
diff
changeset
|
71 storage = list->storage->create(NULL, user); |
0 | 72 if (storage != NULL) |
73 return storage; | |
74 } | |
75 | |
76 return NULL; | |
77 } | |
78 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
79 static struct mail_storage *mail_storage_autodetect(const char *data) |
0 | 80 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
81 struct mail_storage_list *list; |
0 | 82 |
83 for (list = storages; list != NULL; list = list->next) { | |
84 if (list->storage->autodetect(data)) | |
85 return list->storage; | |
86 } | |
87 | |
88 return NULL; | |
89 } | |
90 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
91 struct mail_storage *mail_storage_create_with_data(const char *data, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
92 const char *user) |
0 | 93 { |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
94 struct mail_storage *storage; |
0 | 95 const char *p, *name; |
96 | |
97 if (data == NULL || *data == '\0') | |
22
a946ce1f09b7
mbox fixes, not fully working yet but almost :)
Timo Sirainen <tss@iki.fi>
parents:
13
diff
changeset
|
98 return mail_storage_create_default(user); |
0 | 99 |
100 /* check if we're in the form of mailformat:data | |
101 (eg. maildir:Maildir) */ | |
102 p = data; | |
103 while (i_isalnum(*p)) p++; | |
104 | |
105 if (*p == ':') { | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
106 name = t_strdup_until(data, p); |
22
a946ce1f09b7
mbox fixes, not fully working yet but almost :)
Timo Sirainen <tss@iki.fi>
parents:
13
diff
changeset
|
107 storage = mail_storage_create(name, p+1, user); |
0 | 108 } else { |
109 storage = mail_storage_autodetect(data); | |
110 if (storage != NULL) | |
22
a946ce1f09b7
mbox fixes, not fully working yet but almost :)
Timo Sirainen <tss@iki.fi>
parents:
13
diff
changeset
|
111 storage = storage->create(data, user); |
0 | 112 } |
113 | |
114 return storage; | |
115 } | |
116 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
117 void mail_storage_destroy(struct mail_storage *storage) |
0 | 118 { |
119 i_assert(storage != NULL); | |
120 | |
121 i_free(storage->dir); | |
122 i_free(storage); | |
123 } | |
124 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
125 void mail_storage_clear_error(struct mail_storage *storage) |
0 | 126 { |
127 i_free(storage->error); | |
128 storage->error = NULL; | |
883
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
129 |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
130 storage->syntax_error = FALSE; |
0 | 131 } |
132 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
133 void mail_storage_set_error(struct mail_storage *storage, const char *fmt, ...) |
0 | 134 { |
135 va_list va; | |
136 | |
137 i_free(storage->error); | |
138 | |
139 if (fmt == NULL) | |
140 storage->error = NULL; | |
141 else { | |
142 va_start(va, fmt); | |
143 storage->error = i_strdup_vprintf(fmt, va); | |
883
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
144 storage->syntax_error = FALSE; |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
145 va_end(va); |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
146 } |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
147 } |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
148 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
149 void mail_storage_set_syntax_error(struct mail_storage *storage, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
150 const char *fmt, ...) |
883
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
151 { |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
152 va_list va; |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
153 |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
154 i_free(storage->error); |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
155 |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
156 if (fmt == NULL) |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
157 storage->error = NULL; |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
158 else { |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
159 va_start(va, fmt); |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
160 storage->error = i_strdup_vprintf(fmt, va); |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
161 storage->syntax_error = TRUE; |
0 | 162 va_end(va); |
163 } | |
164 } | |
165 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
166 void mail_storage_set_internal_error(struct mail_storage *storage) |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
167 { |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
168 struct tm *tm; |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
22
diff
changeset
|
169 char str[256]; |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
170 |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
171 tm = localtime(&ioloop_time); |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
172 |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
22
diff
changeset
|
173 storage->error = strftime(str, sizeof(str), CRITICAL_MSG, tm) > 0 ? |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
174 i_strdup(str) : i_strdup("Internal error"); |
883
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
175 storage->syntax_error = FALSE; |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
176 } |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
177 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
178 void mail_storage_set_critical(struct mail_storage *storage, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
179 const char *fmt, ...) |
0 | 180 { |
181 va_list va; | |
182 | |
183 i_free(storage->error); | |
184 if (fmt == NULL) | |
185 storage->error = NULL; | |
186 else { | |
187 va_start(va, fmt); | |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
188 i_error("%s", t_strdup_vprintf(fmt, va)); |
0 | 189 va_end(va); |
190 | |
13
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
191 /* critical errors may contain sensitive data, so let user |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
192 see only "Internal error" with a timestamp to make it |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
193 easier to look from log files the actual error message. */ |
bb294faf7379
"Critical errors" aren't displayed to users anymore, ie. anything that is
Timo Sirainen <tss@iki.fi>
parents:
5
diff
changeset
|
194 mail_storage_set_internal_error(storage); |
0 | 195 } |
196 } | |
197 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
198 const char *mail_storage_get_last_error(struct mail_storage *storage, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
199 int *syntax) |
0 | 200 { |
883
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
201 if (syntax != NULL) |
39e0b536e708
Storage can now specify if errors should be sent to user as BAD or NO.
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
202 *syntax = storage->syntax_error; |
0 | 203 return storage->error; |
204 } | |
205 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
883
diff
changeset
|
206 int mail_storage_is_inconsistency_error(struct mailbox *box) |
0 | 207 { |
208 return box->inconsistent; | |
209 } |