| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | |
| 5 | <head> |
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 7 | <meta name="author" content="Bron Gondwana" /> |
| 8 | |
| 9 | <title>Mailbox API</title> |
| 10 | </head> |
| 11 | |
| 12 | <body> |
| 13 | <h1>Mailbox API</h1> |
| 14 | |
| 15 | <h2>Intro</h2> |
| 16 | |
| 17 | <p>The Mailbox API is implemented in <tt>imap/mailbox.h</tt> and |
| 18 | <tt>imap/mailbox.c</tt>. It wraps the data structures of the |
| 19 | <tt>cyrus.header</tt>, <tt>cyrus.index</tt> and <tt>cyrus.cache</tt> |
| 20 | files in a psuedo-object-oriented way, allowing easy changes to |
| 21 | the mailbox while keeping the internal cached data structures |
| 22 | consistent.</p> |
| 23 | |
| 24 | <h2>Opening and closing</h2> |
| 25 | |
| 26 | <pre> |
| 27 | struct mailbox *mailbox = NULL; |
| 28 | int r; |
| 29 | const char *mboxname = "user.brong"; |
| 30 | |
| 31 | r = mailbox_open_iwl(mboxname, &mailbox); |
| 32 | // or |
| 33 | r = mailbox_open_irl(mboxname, &mailbox); |
| 34 | // or |
| 35 | r = mailbox_open_exclusive(mboxname, &mailbox); |
| 36 | if (r) return r; |
| 37 | |
| 38 | do_stuff(mailbox); |
| 39 | |
| 40 | mailbox_close(&mailbox); |
| 41 | </pre> |
| 42 | |
| 43 | <p>It is always necessary to obtain an index lock when opening |
| 44 | a mailbox, because the index header read must be consistent. |
| 45 | The locks are as follows:</p> |
| 46 | |
| 47 | <table border="1"> |
| 48 | <tr> |
| 49 | <th>Function</th><th>Namelock</th><th>Index Lock</th> |
| 50 | </tr> |
| 51 | <tr> |
| 52 | <td>mailbox_open_iwl</td><td>Shared</td><td>Exclusive</td> |
| 53 | </tr> |
| 54 | <tr> |
| 55 | <td>mailbox_open_irl</td><td>Shared</td><td>Shared</td> |
| 56 | </tr> |
| 57 | <tr> |
| 58 | <td>mailbox_open_exclusive</td><td>Exclusive</td><td>Exclusive</td> |
| 59 | </tr> |
| 60 | </table> |
| 61 | |
| 62 | <p>It should never be necessary to call <tt>mailbox_open_exclusive</tt>, |
| 63 | but it's included for completeness. Use <tt>mailbox_open_iwl</tt> if |
| 64 | you expect to need to write to the index (or even if you're not sure) |
| 65 | and <tt>mailbox_open_irl</tt> when you know you're only reading from |
| 66 | the file and wish to allow other readers to work concurrently.</p> |
| 67 | |
| 68 | <p>Many actions are delayed until the mailbox is closed, or even until |
| 69 | the <em>last</em> mailbox is closed for things that require an |
| 70 | exclusive namelock to perform like deletion or repack. See below |
| 71 | under "delayed actions" for more detail.</p> |
| 72 | |
| 73 | <p>To avoid opening the same file multiple times, the mailbox API |
| 74 | refcounts open mailboxes. If you open the same mailbox again (i.e. |
| 75 | a URL fetch or status command on the currently select mailbox) then |
| 76 | the same mailbox will be returned. It must be unlocked (see below |
| 77 | or the open command will return IMAP_MAILBOX_LOCKED). The matching |
| 78 | close will reduce the refcount, and only the final close will do |
| 79 | the cleanup actions.</p> |
| 80 | |
| 81 | <h2>Locking and unlocking</h2> |
| 82 | |
| 83 | <p>You can keep a mailbox "open", maintaining the namelock, while |
| 84 | releasing the index lock to allow other processes to make changes |
| 85 | to the mailbox. By holding the namelock, you know that record |
| 86 | numbers won't change, and the underlying message files won't be |
| 87 | deleted.</p> |
| 88 | |
| 89 | <p><tt>mailbox_close</tt> will call <tt>mailbox_unlock_index</tt> |
| 90 | if the index is still locked, so it is not necessary to explicitly |
| 91 | unlock the index before closing.</p> |
| 92 | |
| 93 | <pre> |
| 94 | r = mailbox_unlock_index(mailbox, NULL); |
| 95 | |
| 96 | // sleep on user input... |
| 97 | |
| 98 | r = mailbox_lock_index(mailbox, LOCK_SHARED); |
| 99 | // or |
| 100 | r = mailbox_lock_index(mailbox, LOCK_EXCLUSIVE); |
| 101 | </pre> |
| 102 | |
| 103 | <p>For example, <tt>mailbox_unlock_index</tt> and |
| 104 | <tt>mailbox_lock_index</tt> are used extensively |
| 105 | by the index module, allowing an imap client to |
| 106 | maintain a long lived connection selected to a |
| 107 | mailbox and know that messages won't magically |
| 108 | disappear from under it - yet at the same time |
| 109 | allow new mail delivery to happen or other imap |
| 110 | connections to query the mailbox.</p> |
| 111 | |
| 112 | <p>If you have built an accurate statuscache item for |
| 113 | the locked mailbox, you can pass this as the second |
| 114 | parameter to mailbox_index_unlock. If there have been |
| 115 | any changes, mailbox_index_unlock will invalidated the |
| 116 | statuscache. If you give it the new value, then it will |
| 117 | store that value instead. For example:</p> |
| 118 | |
| 119 | <pre> |
| 120 | struct statusdata sdata; |
| 121 | index_status(state, &sdata); |
| 122 | /* RECENT is zero for everyone else because we wrote a new |
| 123 | * recentuid! */ |
| 124 | sdata.recent = 0; |
| 125 | mailbox_unlock_index(state->mailbox, &sdata); |
| 126 | </pre> |
| 127 | |
| 128 | <p>See "delayed actions" below for delayed actions performed |
| 129 | during an unlock.</p> |
| 130 | |
| 131 | <h2>Creating, renaming and deleting</h2> |
| 132 | |
| 133 | <p><b>WARNING:</b> These functions only change the mailbox |
| 134 | files on disk. They don't update the mailboxes.db records |
| 135 | or contact murder servers. In most cases you are probably |
| 136 | looking for the <tt>mboxlist_</tt> functions instead.</p> |
| 137 | |
| 138 | <p>Creating a mailbox is somewhat longwinded - as there are many |
| 139 | optional parameters.</p> |
| 140 | |
| 141 | <pre> |
| 142 | int mailbox_create(const char *name, const char *part, const char *acl, |
| 143 | const char *uniqueid, int options, unsigned uidvalidity, |
| 144 | struct mailbox **mailboxptr); |
| 145 | </pre> |
| 146 | |
| 147 | <p>Most interesting to note is that on success, <tt>mailboxptr</tt> will |
| 148 | contain the same mailbox that <tt>mailbox_open_exclusive</tt> above would |
| 149 | have returned, with an exclusive namelock and an exclusive index lock. |
| 150 | This allows you to perform other consistency operations after creating |
| 151 | the mailbox with a full guarantee that no other process will even be |
| 152 | able to know of the mailbox's existence! You can still roll-back by |
| 153 | deleting the mailbox and the next process will get the namelock and see |
| 154 | no mailbox with that name.</p> |
| 155 | |
| 156 | <pre> |
| 157 | int mailbox_rename_copy(struct mailbox *oldmailbox, |
| 158 | const char *newname, const char *newpart, |
| 159 | const char *userid, int ignorequota, |
| 160 | int silent, |
| 161 | struct mailbox **newmailboxptr); |
| 162 | </pre> |
| 163 | |
| 164 | <p>Very similar to mailbox_create - the new mailbox is created with |
| 165 | an exclusive name lock and returned. The old mailbox must be passed |
| 166 | in with an <b>exclusive index lock</b> but is fine with a shared |
| 167 | namelock, as it will be passed to <tt>mailbox_delete</tt>.</p> |
| 168 | |
| 169 | <pre> |
| 170 | int mailbox_delete(struct mailbox **mailboxptr); |
| 171 | </pre> |
| 172 | |
| 173 | <p>Just like <tt>mailbox_close</tt> above, this closes the mailbox. |
| 174 | Before it does so, it sets the OPT_MAILBOX_DELETED option flag in |
| 175 | the index header. The interesting work is actually done in |
| 176 | <tt>mailbox_close</tt>. See below under "delayed actions".</p> |
| 177 | |
| 178 | <p><tt>mailbox_delete</tt> requires an exclusive index lock, but |
| 179 | can complete quite happily with only a shared namelock.</p> |
| 180 | |
| 181 | <h2>Reading and writing records</h2> |
| 182 | |
| 183 | <p>Ok - so you have a mailbox, it's opened, and the index is |
| 184 | locked. Time to start reading and writing some records!</p> |
| 185 | |
| 186 | <p>At the mailbox level there is no concept of "message numbers" |
| 187 | from imap, only "record numbers". The canonical variable name |
| 188 | to refer to record numbers is <tt>recno</tt>. All records are |
| 189 | read and written using <tt>struct index_record</tt> values.</p> |
| 190 | |
| 191 | <p>Here at the API definitions used for reading and writing:</p> |
| 192 | |
| 193 | <pre> |
| 194 | int mailbox_read_index_record(struct mailbox *mailbox, |
| 195 | uint32_t recno, |
| 196 | struct index_record *record); |
| 197 | int mailbox_rewrite_index_record(struct mailbox *mailbox, |
| 198 | struct index_record *record); |
| 199 | int mailbox_append_index_record(struct mailbox *mailbox, |
| 200 | struct index_record *record); |
| 201 | int mailbox_commit(mailbox); |
| 202 | </pre> |
| 203 | |
| 204 | <p>An example of iterating through a mailbox</p> |
| 205 | |
| 206 | <pre> |
| 207 | uint32_t recno; |
| 208 | struct index_record record; |
| 209 | int make_changes; |
| 210 | |
| 211 | /* DEPRECATED */ |
| 212 | for (recno = 1; recno <= mailbox->i.num_records; recno++) { |
| 213 | if (mailbox_read_index_record(mailbox, recno, &record)) |
| 214 | fatal("invalid record", EX_SOFTWARE); // or return an error |
| 215 | if (record.internal_flags & FLAG_INTERNAL_EXPUNGED) |
| 216 | continue; // skip expunged records |
| 217 | make_changes = do_stuff(mailbox, &record); |
| 218 | if (make_changes) |
| 219 | mailbox_rewrite_index_record(mailbox, &record); |
| 220 | } |
| 221 | |
| 222 | /* the new way */ |
| 223 | int make_change; |
| 224 | const struct index_record *record; |
| 225 | struct mailbox_iter *iter; |
| 226 | |
| 227 | iter = mailbox_iter_init(mailbox, 0, ITER_SKIP_EXPUNGED); |
| 228 | while ((record = mailbox_iter_step(iter))) { |
| 229 | make_changes = do_stuff(mailbox, record); |
| 230 | if (make_changes) |
| 231 | mailbox_rewrite_index_record(mailbox, record); |
| 232 | } |
| 233 | mailbox_iter_done(&iter); |
| 234 | </pre> |
| 235 | |
| 236 | <p>NOTE: <tt>mailbox_rewrite_index_record</tt> doesn't need a recno, |
| 237 | as that's cached inside the index_record struct.</p> |
| 238 | |
| 239 | <p>NOTE: You need an exclusively locked index to use rewrite or append, |
| 240 | but only a shared index lock to use read.</p> |
| 241 | |
| 242 | <p>There are a range of consistency checks done to ensure that a |
| 243 | rewrite doesn't violate IMAP semantics (an expunged message can |
| 244 | never be unexpunged, UIDs can't change, etc) and the internal |
| 245 | tracking counts and quota data are updated as well. They will |
| 246 | be committed at unlock time, see "delayed actions"</p> |
| 247 | |
| 248 | <p>If you don't set the <tt>record.silent</tt> field to a true value |
| 249 | before rewriting or appending, the <tt>record.modseq</tt> and |
| 250 | <tt>record.last_updated</tt> values will be changed. This allows |
| 251 | condstore to work correctly.</p> |
| 252 | |
| 253 | <h3>Appending</h3> |
| 254 | |
| 255 | <p>To append a record, the file must have already been copied into |
| 256 | place (XXX - plan to move to a stage based system where the mailbox |
| 257 | API handles the staging, but that's not finished yet) and been parsed |
| 258 | into the record struct. The UID must be set already, and must be greater |
| 259 | than the UID of any existing record in the mailbox. There are a range |
| 260 | of consistency checks done.</p> |
| 261 | |
| 262 | <p>The internal consistency counts are updated by append as well.</p> |
| 263 | |
| 264 | <h3>Committing</h3> |
| 265 | |
| 266 | <p>When you have finished making any changes, you need to "commit". |
| 267 | This will write the updated values for any index header fields, |
| 268 | rewite the <tt>cyrus.header</tt> file if needed and fsync all |
| 269 | changes to disk.</p> |
| 270 | |
| 271 | <p>It is a fatal error to unlock (or close) a mailbox that has had |
| 272 | changes without committing, as it can leave the mailbox in a |
| 273 | corrupted state.</p> |
| 274 | |
| 275 | <h3>Cache records</h3> |
| 276 | |
| 277 | Cache records are accessed through <tt>record.crec</tt> which is not |
| 278 | filled by read_index_record. The cache file is only read and mapped |
| 279 | into memory as needed, so you if you want to access cache records, |
| 280 | the basic API is as follows: |
| 281 | |
| 282 | <pre> |
| 283 | int mailbox_cacherecord(struct mailbox *mailbox, |
| 284 | struct index_record *record); |
| 285 | const char *cacheitem_base(struct index_record *record, int field); |
| 286 | unsigned cacheitem_size(struct index_record *record, int field); |
| 287 | struct buf *cacheitem_buf(struct index_record *record, int field); |
| 288 | </pre> |
| 289 | |
| 290 | <p>You must always call <tt>mailbox_cacherecord</tt> on a record |
| 291 | before trying to access any of the cache items. "<tt>field</tt>" |
| 292 | above is the individual field (there are 10) in the cache record. |
| 293 | There's more information on those fields in the mailbox internal |
| 294 | format documentation.</p> |
| 295 | |
| 296 | <pre> |
| 297 | for (recno = 1; recno <= mailbox->i.num_records; recno++) { |
| 298 | if (mailbox_read_index_record(mailbox, recno, &record)) |
| 299 | fatal("invalid record", EX_SOFTWARE); // or return an error |
| 300 | if (record.internal_flags & FLAG_INTERNAL_EXPUNGED) |
| 301 | continue; // skip expunged records |
| 302 | if (mailbox_cacherecord(mailbox, &record)) |
| 303 | fatal("failed to read cache", EX_SOFTWARE); |
| 304 | ... |
| 305 | envelope_length = cacheitem_size(&record, CACHE_ENVELOPE); |
| 306 | } |
| 307 | </pre> |
| 308 | |
| 309 | <p>See <tt>imap/mailbox.h</tt> for the full list of constants.</p> |
| 310 | |
| 311 | <h2>Delayed Actions</h2> |
| 312 | |
| 313 | <p>Here's the bit you've been waiting for! What happens during |
| 314 | unlock and close</p> |
| 315 | |
| 316 | <h3>first, unlock</h3> |
| 317 | |
| 318 | <p>Anything that makes any changes sets the mailbox->has_changed |
| 319 | flag. If this is set, then before the index gets unlocked:</p> |
| 320 | |
| 321 | <ul> |
| 322 | <li>the updatenotifier (idle) is called</li> |
| 323 | <li><tt>sync_log_mailbox</tt> (replication) gets called</li> |
| 324 | <li>the statuscache value gets erased (or replaced if you passed |
| 325 | in an updated value).</li> |
| 326 | </ul> |
| 327 | |
| 328 | |
| 329 | <h3>then: close</h3> |
| 330 | |
| 331 | <p>next the index is unlocked (see above)</p> |
| 332 | |
| 333 | <p>third, any "unlink" commands scheduled for email files are |
| 334 | run. These can't be done until after the mailbox_commit to |
| 335 | ensure consistency - the file isn't deleted until the record |
| 336 | is written as unlinked! But we save the unlink until now so |
| 337 | that other tasks aren't waiting for the index lock while the |
| 338 | unlinks run. Unlink is expensive in IO and time.</p> |
| 339 | |
| 340 | <p>finally we check for MAILBOX_NEEDS_REPACK or MAILBOX_DELETED |
| 341 | option flags. If either is sets, then we make a non-blocking |
| 342 | attempt to get an exclusive namelock. If the non-blocking |
| 343 | attempt fails, then another process has the mailbox open, so |
| 344 | save the cleanup for them! If it succeeds, then go ahead |
| 345 | with either <tt>mailbox_delete_cleanup</tt> or |
| 346 | <tt>mailbox_index_repack</tt> as appropriate.</p> |
| 347 | |
| 348 | <p>After this it's just a matter of releasing malloc'd memory |
| 349 | and finally releasing the name lock.</p> |
| 350 | |
| 351 | |
| 352 | </body> |
| 353 | </html> |