]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
dbox: Code cleanups.
authorTimo Sirainen <tss@iki.fi>
Wed, 18 Mar 2009 19:24:46 +0000 (15:24 -0400)
committerTimo Sirainen <tss@iki.fi>
Wed, 18 Mar 2009 19:24:46 +0000 (15:24 -0400)
--HG--
branch : HEAD

src/lib-storage/index/dbox/dbox-file.c
src/lib-storage/index/dbox/dbox-file.h
src/lib-storage/index/dbox/dbox-mail.c
src/lib-storage/index/dbox/dbox-map-private.h [new file with mode: 0644]
src/lib-storage/index/dbox/dbox-map.c
src/lib-storage/index/dbox/dbox-map.h
src/lib-storage/index/dbox/dbox-storage.c
src/lib-storage/index/dbox/dbox-storage.h
src/lib-storage/index/dbox/dbox-sync-rebuild.c

index 030152c733125fb29ca6d859f225803366d88aba..47adb556f4c668c2fc7ee9288bd4895f881bb2c7 100644 (file)
@@ -652,7 +652,17 @@ int dbox_file_seek_next(struct dbox_file *file, uoff_t *offset, bool *last_r)
                return 1;
 
        i_stream_skip(file->input, size);
-       return dbox_file_seek_next_at_metadata(file, offset);
+       if ((ret = dbox_file_seek_next_at_metadata(file, offset)) <= 0)
+               return ret;
+
+       ret = dbox_file_get_mail_stream(file, *offset, &size, NULL, &expunged);
+       if (ret <= 0)
+               return ret;
+       if (expunged) {
+               *last_r = TRUE;
+               return 0;
+       }
+       return 1;
 }
 
 static int
@@ -827,27 +837,24 @@ dbox_file_metadata_read_at(struct dbox_file *file, uoff_t metadata_offset)
        return ret;
 }
 
-int dbox_file_metadata_read(struct dbox_file *file, uoff_t offset,
-                           bool *expunged_r)
+int dbox_file_metadata_read(struct dbox_file *file)
 {
-       uoff_t physical_size, metadata_offset;
+       uoff_t metadata_offset;
        int ret;
 
-       if (file->metadata_read_offset == offset || file->maildir_file)
-               return 1;
-       i_assert(offset != 0);
+       i_assert(file->cur_offset != 0 || file->maildir_file);
 
-       ret = dbox_file_get_mail_stream(file, offset, &physical_size,
-                                       NULL, expunged_r);
-       if (ret <= 0 || *expunged_r)
-               return ret;
+       if (file->metadata_read_offset == file->cur_offset ||
+           file->maildir_file)
+               return 1;
 
-       metadata_offset = offset + file->msg_header_size + physical_size;
+       metadata_offset = file->cur_offset + file->msg_header_size +
+               file->cur_physical_size;
        ret = dbox_file_metadata_read_at(file, metadata_offset);
        if (ret <= 0)
                return ret;
 
-       file->metadata_read_offset = offset;
+       file->metadata_read_offset = file->cur_offset;
        return 1;
 }
 
index 6d5c0486a1f88be6d4a1d16df5b4f9637ff9423b..1ff684dd8a7cf05898d765ca633e2fab0e6d9990 100644 (file)
@@ -144,15 +144,15 @@ int dbox_file_try_lock(struct dbox_file *file);
 void dbox_file_unlock(struct dbox_file *file);
 
 /* Seek to given offset in file and return the message's input stream
-   and physical size. Returns 1 if ok, 0 if file/offset is corrupted,
+   and physical size. Returns 1 if ok/expunged, 0 if file/offset is corrupted,
    -1 if I/O error. */
 int dbox_file_get_mail_stream(struct dbox_file *file, uoff_t offset,
                              uoff_t *physical_size_r,
                              struct istream **stream_r, bool *expunged_r);
-/* Seek to next message after given offset, or to first message if offset=0.
-   If there are no more messages, last_r is set to TRUE. Returns 1 if ok,
-   0 if file/offset is corrupted, -1 if I/O error. */
-int dbox_file_seek_next(struct dbox_file *file, uoff_t *offset, bool *last_r);
+/* Seek to next message after current one. If there are no more messages,
+   returns 0 and last_r is set to TRUE. Returns 1 if ok, 0 if file/offset is
+   corrupted, -1 if I/O error. */
+int dbox_file_seek_next(struct dbox_file *file, uoff_t *offset_r, bool *last_r);
 
 /* Returns TRUE if mail_size bytes can be appended to the file. */
 bool dbox_file_can_append(struct dbox_file *file, uoff_t mail_size);
@@ -171,12 +171,9 @@ void dbox_file_cancel_append(struct dbox_file *file, uoff_t append_offset);
 /* Flush writes to dbox file. */
 int dbox_file_flush_append(struct dbox_file *file);
 
-/* Read to given message's metadata. Returns 1 if ok, 0 if file/offset is
-   corrupted, -1 if I/O error. If message has already been expunged,
-   expunged_r=TRUE and 1 is returned. */
-int dbox_file_metadata_read(struct dbox_file *file, uoff_t offset,
-                           bool *expunged_r);
-
+/* Read current message's metadata. Returns 1 if ok, 0 if metadata is
+   corrupted, -1 if I/O error. */
+int dbox_file_metadata_read(struct dbox_file *file);
 /* Return wanted metadata value, or NULL if not found. */
 const char *dbox_file_metadata_get(struct dbox_file *file,
                                   enum dbox_metadata_key key);
index 9ae632c5c8e9350b31534381f2c17ce30671bd72..984fbae7df27f5ad3a39400762cc8fc0ac16f6d3 100644 (file)
@@ -91,18 +91,21 @@ static int
 dbox_mail_metadata_read(struct dbox_mail *mail, struct dbox_file **file_r)
 {
        struct mail *_mail = &mail->imail.mail.mail;
-       uoff_t offset;
+       uoff_t offset, size;
        bool expunged;
 
        if (dbox_mail_open(mail, &offset, file_r) < 0)
                return -1;
 
-       if (dbox_file_metadata_read(*file_r, offset, &expunged) <= 0)
+       if (dbox_file_get_mail_stream(*file_r, offset, &size,
+                                     NULL, &expunged) <= 0)
                return -1;
        if (expunged) {
                mail_set_expunged(_mail);
                return -1;
        }
+       if (dbox_file_metadata_read(*file_r) <= 0)
+               return -1;
        return 0;
 }
 
diff --git a/src/lib-storage/index/dbox/dbox-map-private.h b/src/lib-storage/index/dbox/dbox-map-private.h
new file mode 100644 (file)
index 0000000..e132439
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef DBOX_MAP_PRIVATE_H
+#define DBOX_MAP_PRIVATE_H
+
+#include "dbox-map.h"
+
+struct dbox_mail_lookup_rec {
+       uint32_t map_uid;
+       uint16_t refcount;
+       struct dbox_mail_index_map_record rec;
+};
+
+struct dbox_map {
+       struct dbox_storage *storage;
+       struct mail_index *index;
+       struct mail_index_view *view;
+
+       uint32_t map_ext_id, ref_ext_id;
+       ARRAY_TYPE(seq_range) ref0_file_ids;
+};
+
+struct dbox_map_append {
+       struct dbox_file *file;
+       uoff_t offset, size;
+};
+
+struct dbox_map_append_context {
+       struct dbox_mailbox *mbox;
+       struct dbox_map *map;
+
+       struct mail_index_sync_ctx *sync_ctx;
+       struct mail_index_view *sync_view;
+       struct mail_index_transaction *trans;
+
+       ARRAY_DEFINE(files, struct dbox_file *);
+       ARRAY_DEFINE(appends, struct dbox_map_append);
+
+       uint32_t first_new_file_id;
+       uint32_t orig_next_uid;
+
+       unsigned int files_nonappendable_count;
+
+       unsigned int failed:1;
+};
+
+int dbox_map_refresh(struct dbox_map *map);
+int dbox_map_view_lookup_rec(struct dbox_map *map, struct mail_index_view *view,
+                            uint32_t seq, struct dbox_mail_lookup_rec *rec_r);
+
+#endif
index a27d56d3349758ea8c8d9763caceeddcdbb76764..e3262d9138f6113b868df013582a3e46190d3bcb 100644 (file)
@@ -5,53 +5,10 @@
 #include "ostream.h"
 #include "dbox-storage.h"
 #include "dbox-file.h"
-#include "dbox-map.h"
+#include "dbox-map-private.h"
 
 #define MAX_BACKWARDS_LOOKUPS 10
 
-struct dbox_mail_index_map_header {
-       uint32_t highest_file_id;
-};
-
-struct dbox_mail_index_map_record {
-       uint32_t file_id;
-       uint32_t offset;
-       uint32_t size;
-};
-
-struct dbox_map {
-       struct dbox_storage *storage;
-       struct mail_index *index;
-       struct mail_index_view *view;
-
-       uint32_t map_ext_id, ref_ext_id;
-       ARRAY_TYPE(seq_range) ref0_file_ids;
-};
-
-struct dbox_map_append {
-       struct dbox_file *file;
-       uoff_t offset, size;
-};
-
-struct dbox_map_append_context {
-       struct dbox_mailbox *mbox;
-       struct dbox_map *map;
-
-       struct mail_index_sync_ctx *sync_ctx;
-       struct mail_index_view *sync_view;
-       struct mail_index_transaction *trans;
-
-       ARRAY_DEFINE(files, struct dbox_file *);
-       ARRAY_DEFINE(appends, struct dbox_map_append);
-
-       uint32_t first_new_file_id;
-       uint32_t orig_next_uid;
-
-       unsigned int files_nonappendable_count;
-
-       unsigned int failed:1;
-};
-
 void dbox_map_set_corrupted(struct dbox_map *map, const char *format, ...)
 {
        va_list args;
@@ -119,7 +76,7 @@ static int dbox_map_open(struct dbox_map *map)
        return 0;
 }
 
-static int dbox_map_refresh(struct dbox_map *map)
+int dbox_map_refresh(struct dbox_map *map)
 {
        struct mail_index_view_sync_ctx *ctx;
        bool delayed_expunges;
@@ -195,16 +152,40 @@ int dbox_map_lookup(struct dbox_map *map, uint32_t map_uid,
        return 1;
 }
 
+int dbox_map_view_lookup_rec(struct dbox_map *map, struct mail_index_view *view,
+                            uint32_t seq, struct dbox_mail_lookup_rec *rec_r)
+{
+       const uint16_t *ref16_p;
+       const void *data;
+       bool expunged;
+
+       memset(rec_r, 0, sizeof(*rec_r));
+       mail_index_lookup_uid(view, seq, &rec_r->map_uid);
+
+       mail_index_lookup_ext(view, seq, map->map_ext_id, &data, &expunged);
+       if (data == NULL) {
+               dbox_map_set_corrupted(map, "missing map extension");
+               return -1;
+       }
+       memcpy(&rec_r->rec, data, sizeof(rec_r->rec));
+
+       mail_index_lookup_ext(view, seq, map->ref_ext_id, &data, &expunged);
+       if (data == NULL) {
+               dbox_map_set_corrupted(map, "missing ref extension");
+               return -1;
+       }
+       ref16_p = data;
+       rec_r->refcount = *ref16_p;
+       return 0;
+}
+
 int dbox_map_get_file_msgs(struct dbox_map *map, uint32_t file_id,
                           ARRAY_TYPE(dbox_map_file_msg) *recs)
 {
        const struct mail_index_header *hdr;
+       struct dbox_mail_lookup_rec rec;
        struct dbox_map_file_msg msg;
-       const struct dbox_mail_index_map_record *rec;
-       const uint16_t *ref16_p;
-       unsigned int seq;
-       const void *data;
-       bool expunged;
+       uint32_t seq;
 
        if (dbox_map_refresh(map) < 0)
                return -1;
@@ -212,29 +193,15 @@ int dbox_map_get_file_msgs(struct dbox_map *map, uint32_t file_id,
 
        memset(&msg, 0, sizeof(msg));
        for (seq = 1; seq <= hdr->messages_count; seq++) {
-               mail_index_lookup_uid(map->view, seq, &msg.map_uid);
-
-               mail_index_lookup_ext(map->view, seq, map->map_ext_id,
-                                     &data, &expunged);
-               if (data == NULL) {
-                       dbox_map_set_corrupted(map, "missing map extension");
+               if (dbox_map_view_lookup_rec(map, map->view, seq, &rec) < 0)
                        return -1;
-               }
-               rec = data;
-               if (rec->file_id != file_id)
-                       continue;
 
-               msg.offset = rec->offset;
-               mail_index_lookup_ext(map->view, seq, map->ref_ext_id,
-                                     &data, &expunged);
-               if (data == NULL) {
-                       dbox_map_set_corrupted(map, "missing ref extension");
-                       return -1;
+               if (rec.rec.file_id == file_id) {
+                       msg.map_uid = rec.map_uid;
+                       msg.offset = rec.rec.offset;
+                       msg.refcount = rec.refcount;
+                       array_append(recs, &msg, 1);
                }
-               ref16_p = data;
-               msg.refcount = *ref16_p;
-
-               array_append(recs, &msg, 1);
        }
        return 0;
 }
index cea2a496a2f5f2308ea74a9b177b4c15de191e3d..7f5e6edc945d36345178515c23af27f23e763ac8 100644 (file)
@@ -1,10 +1,23 @@
 #ifndef DBOX_MAP_H
 #define DBOX_MAP_H
 
+#include "seq-range-array.h"
+
 struct dbox_storage;
 struct dbox_mailbox;
 struct dbox_file;
 struct dbox_map_append_context;
+struct dbox_mail_lookup_rec;
+
+struct dbox_mail_index_map_header {
+       uint32_t highest_file_id;
+};
+
+struct dbox_mail_index_map_record {
+       uint32_t file_id;
+       uint32_t offset;
+       uint32_t size;
+};
 
 struct dbox_map_file_msg {
        uint32_t map_uid;
@@ -27,7 +40,6 @@ int dbox_map_get_file_msgs(struct dbox_map *map, uint32_t file_id,
 
 int dbox_map_update_refcounts(struct dbox_map *map,
                              const ARRAY_TYPE(seq_range) *map_uids, int diff);
-/*  */
 int dbox_map_remove_file_id(struct dbox_map *map, uint32_t file_id);
 
 /* Return all files containing messages with zero refcount. */
index c19066845ea867708d8b8bc41c7c8b40e36e65bb..00916c48c281220d36dcb57942e6f913fc611c1c 100644 (file)
@@ -282,7 +282,7 @@ dbox_open(struct dbox_storage *storage, const char *name,
                mail_index_ext_register(index, "dbox-hdr",
                                        sizeof(struct dbox_index_header), 0, 0);
        mbox->guid_ext_id =
-               mail_index_ext_register(index, "guid", 0, 16, 1);
+               mail_index_ext_register(index, "guid", 0, DBOX_GUID_BIN_LEN, 1);
 
        index_storage_mailbox_init(&mbox->ibox, name, flags, FALSE);
        mbox->maildir_uidlist = maildir_uidlist_init_readonly(&mbox->ibox);
@@ -312,7 +312,7 @@ dbox_cleanup_if_exists(struct mail_storage *storage, const char *path)
        return TRUE;
 }
 
-static struct mailbox *
+struct mailbox *
 dbox_mailbox_open(struct mail_storage *_storage, const char *name,
                  struct istream *input, enum mailbox_open_flags flags)
 {
index def64fd78f96935a4474602e71ea94d556e0be2e..87e9699c917679ad60744ba68d63b31772229261 100644 (file)
@@ -16,6 +16,7 @@
 #define DBOX_MAIL_FILE_UID_PREFIX "u."
 #define DBOX_MAIL_FILE_MULTI_FORMAT DBOX_MAIL_FILE_MULTI_PREFIX"%u"
 #define DBOX_MAIL_FILE_UID_FORMAT DBOX_MAIL_FILE_UID_PREFIX"%u"
+#define DBOX_GUID_BIN_LEN (128/8)
 
 /* How often to scan for stale temp files (based on dir's atime) */
 #define DBOX_TMP_SCAN_SECS (8*60*60)
@@ -87,6 +88,10 @@ extern struct mail_vfuncs dbox_mail_vfuncs;
 void dbox_transaction_class_init(void);
 void dbox_transaction_class_deinit(void);
 
+struct mailbox *
+dbox_mailbox_open(struct mail_storage *storage, const char *name,
+                 struct istream *input, enum mailbox_open_flags flags);
+
 struct mail *
 dbox_mail_alloc(struct mailbox_transaction_context *t,
                enum mail_fetch_field wanted_fields,
index 033e1d20d63cbf9affa370cbcc179a64c24c80cb..91c113feaaed45b039ba7cf337cc239c362e527c 100644 (file)
@@ -153,7 +153,7 @@ static int dbox_sync_index_file_next(struct dbox_sync_rebuild_context *ctx,
                                     struct dbox_file *file, uoff_t *offset)
 {
        uint32_t seq;
-       bool expunged, last;
+       bool last;
        int ret;
 
        ret = dbox_file_seek_next(file, offset, &last);
@@ -166,7 +166,7 @@ static int dbox_sync_index_file_next(struct dbox_sync_rebuild_context *ctx,
                return 0;
        }
 
-       ret = dbox_file_metadata_read(file, *offset, &expunged);
+       ret = dbox_file_metadata_read(file);
        if (ret <= 0) {
                if (ret < 0)
                        return -1;
@@ -174,11 +174,10 @@ static int dbox_sync_index_file_next(struct dbox_sync_rebuild_context *ctx,
                          file->current_path);
                return 0;
        }
-       if (!expunged) {
-               /* FIXME: file->uid doesn't work for multi files */
-               mail_index_append(ctx->trans, file->uid, &seq);
-               dbox_sync_index_metadata(ctx, file, seq, file->uid);
-       }
+
+       /* FIXME: file->uid doesn't work for multi files */
+       mail_index_append(ctx->trans, file->uid, &seq);
+       dbox_sync_index_metadata(ctx, file, seq, file->uid);
        return 1;
 }
 
@@ -212,9 +211,8 @@ dbox_sync_index_uid_file(struct dbox_sync_rebuild_context *ctx,
 }
 
 static int
-dbox_sync_index_multi_file(struct dbox_sync_rebuild_context *ctx ATTR_UNUSED,
-                          const char *dir ATTR_UNUSED,
-                          const char *fname ATTR_UNUSED)
+dbox_sync_index_multi_file(struct dbox_sync_rebuild_context *ctx,
+                          const char *dir, const char *fname)
 {
        /* FIXME */
        return 0;