[dovecot-cvs] dovecot/src/lib-storage/index/mbox mbox-file.c, 1.7, 1.8 mbox-lock.c, 1.13, 1.14 mbox-save.c, 1.74, 1.75 mbox-storage.c, 1.112, 1.113 mbox-sync.c, 1.141, 1.142

cras at dovecot.org cras at dovecot.org
Tue Mar 29 16:33:10 EEST 2005


Update of /var/lib/cvs/dovecot/src/lib-storage/index/mbox
In directory talvi:/tmp/cvs-serv3658/lib-storage/index/mbox

Modified Files:
	mbox-file.c mbox-lock.c mbox-save.c mbox-storage.c mbox-sync.c 
Log Message:
Added input stream parameter to mailbox_open(). With mbox it now allows
opening a read-only mbox using a stream.



Index: mbox-file.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-file.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- mbox-file.c	17 Dec 2004 00:05:54 -0000	1.7
+++ mbox-file.c	29 Mar 2005 13:33:08 -0000	1.8
@@ -16,6 +16,12 @@
 
 	i_assert(ibox->mbox_fd == -1);
 
+	if (ibox->mbox_file_stream != NULL) {
+		/* read-only mbox stream */
+		i_assert(ibox->mbox_readonly);
+		return 0;
+	}
+
 	fd = open(ibox->path, ibox->mbox_readonly ? O_RDONLY : O_RDWR);
 	if (fd == -1 && errno == EACCES && !ibox->mbox_readonly) {
                 ibox->mbox_readonly = TRUE;
@@ -56,7 +62,15 @@
 	if (ibox->mbox_stream != NULL)
 		return 0;
 
-	i_assert(ibox->mbox_file_stream == NULL);
+	if (ibox->mbox_file_stream != NULL) {
+		/* read-only mbox stream */
+		i_assert(ibox->mbox_fd == -1 && ibox->mbox_readonly);
+
+		ibox->mbox_stream =
+			i_stream_create_raw_mbox(default_pool,
+						 ibox->mbox_file_stream);
+		return 0;
+	}
 
 	if (ibox->mbox_fd == -1) {
 		if (mbox_file_open(ibox) < 0)
@@ -85,13 +99,20 @@
 void mbox_file_close_stream(struct index_mailbox *ibox)
 {
 	if (ibox->mbox_stream != NULL) {
-		i_stream_close(ibox->mbox_file_stream);
-		i_stream_unref(ibox->mbox_file_stream);
-		ibox->mbox_file_stream = NULL;
-
 		i_stream_unref(ibox->mbox_stream);
 		ibox->mbox_stream = NULL;
 	}
+
+	if (ibox->mbox_file_stream != NULL) {
+		if (ibox->mbox_fd == -1) {
+			/* read-only mbox stream */
+			i_assert(ibox->mbox_readonly);
+		} else {
+			i_stream_close(ibox->mbox_file_stream);
+			i_stream_unref(ibox->mbox_file_stream);
+			ibox->mbox_file_stream = NULL;
+		}
+	}
 }
 
 int mbox_file_seek(struct index_mailbox *ibox, struct mail_index_view *view,

Index: mbox-lock.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-lock.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- mbox-lock.c	16 Jan 2005 19:18:25 -0000	1.13
+++ mbox-lock.c	29 Mar 2005 13:33:08 -0000	1.14
@@ -455,6 +455,13 @@
 	if (!lock_settings_initialized)
                 mbox_init_lock_settings();
 
+	if (ibox->mbox_fd == -1 && ibox->mbox_file_stream != NULL) {
+		/* read-only mbox stream. no need to lock. */
+		i_assert(ibox->mbox_readonly);
+		ibox->mbox_lock_type = lock_type;
+		return TRUE;
+	}
+
 	max_wait_time = time(NULL) + lock_timeout;
 
 	memset(&ctx, 0, sizeof(ctx));

Index: mbox-save.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-save.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -d -r1.74 -r1.75
--- mbox-save.c	24 Mar 2005 20:17:00 -0000	1.74
+++ mbox-save.c	29 Mar 2005 13:33:08 -0000	1.75
@@ -251,6 +251,12 @@
 	struct index_mailbox *ibox = ctx->ibox;
 	int ret;
 
+	if (ctx->ibox->mbox_readonly || ctx->ibox->readonly) {
+		mail_storage_set_error(&ctx->ibox->storage->storage,
+				       "Read-only mbox");
+		return -1;
+	}
+
 	if (ctx->append_offset == (uoff_t)-1) {
 		/* first appended mail in this transaction */
 		if (ibox->mbox_lock_type != F_WRLCK) {

Index: mbox-storage.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-storage.c,v
retrieving revision 1.112
retrieving revision 1.113
diff -u -d -r1.112 -r1.113
--- mbox-storage.c	15 Mar 2005 19:01:53 -0000	1.112
+++ mbox-storage.c	29 Mar 2005 13:33:08 -0000	1.113
@@ -2,6 +2,7 @@
 
 #include "lib.h"
 #include "buffer.h"
+#include "istream.h"
 #include "home-expand.h"
 #include "mkdir-parents.h"
 #include "unlink-directory.h"
@@ -478,35 +479,13 @@
 	return FALSE;
 }
 
-static struct mailbox *
-mbox_open(struct index_storage *storage, const char *name,
-	  enum mailbox_open_flags flags)
+static struct index_mailbox *
+mbox_alloc(struct index_storage *storage, struct mail_index *index,
+	   const char *name, enum mailbox_open_flags flags)
 {
 	struct index_mailbox *ibox;
-	struct mail_index *index;
-	const char *path, *index_dir;
-	uint32_t mbox_ext_idx;
 	pool_t pool;
 
-	if (strcmp(name, "INBOX") == 0) {
-		/* name = "INBOX"
-		   path = "<inbox_file>/INBOX"
-		   index_dir = "/mail/.imap/INBOX" */
-		path = storage->inbox_path;
-		index_dir = mbox_get_index_dir(storage, "INBOX");
-	} else {
-		/* name = "foo/bar"
-		   path = "/mail/foo/bar"
-		   index_dir = "/mail/foo/.imap/bar" */
-		path = mbox_get_path(storage, name);
-		index_dir = mbox_get_index_dir(storage, name);
-	}
-
-	index = index_storage_alloc(index_dir, path, MBOX_INDEX_PREFIX);
-	mbox_ext_idx = mail_index_ext_register(index, "mbox", 0,
-					       sizeof(uint64_t),
-					       sizeof(uint64_t));
-
 	pool = pool_alloconly_create("mailbox", 256);
 	ibox = p_new(pool, struct index_mailbox, 1);
 	ibox->box = mbox_mailbox;
@@ -514,14 +493,15 @@
 	ibox->storage = storage;
 
 	if (index_storage_mailbox_init(ibox, index, name, flags) < 0) {
-		/* the memory was already freed */
+		/* the memory is already freed here, no need to deinit */
 		return NULL;
 	}
 
-	ibox->path = p_strdup(pool, path);
 	ibox->mbox_fd = -1;
 	ibox->mbox_lock_type = F_UNLCK;
-	ibox->mbox_ext_idx = mbox_ext_idx;
+	ibox->mbox_ext_idx =
+		mail_index_ext_register(index, "mbox", 0,
+					sizeof(uint64_t), sizeof(uint64_t));
 
 	ibox->is_recent = mbox_mail_is_recent;
 	ibox->mail_vfuncs = &mbox_mail_vfuncs;
@@ -533,6 +513,37 @@
 		mail_index_ext_register(ibox->index, "header-md5", 0, 16, 1);
 	if ((flags & MAILBOX_OPEN_KEEP_HEADER_MD5) != 0)
 		ibox->mbox_save_md5 = TRUE;
+	return ibox;
+}
+
+static struct mailbox *
+mbox_open(struct index_storage *storage, const char *name,
+	  enum mailbox_open_flags flags)
+{
+	struct index_mailbox *ibox;
+	struct mail_index *index;
+	const char *path, *index_dir;
+
+	if (strcmp(name, "INBOX") == 0) {
+		/* name = "INBOX"
+		   path = "<inbox_file>/INBOX"
+		   index_dir = "/mail/.imap/INBOX" */
+		path = storage->inbox_path;
+		index_dir = mbox_get_index_dir(storage, "INBOX");
+	} else {
+		/* name = "foo/bar"
+		   path = "/mail/foo/bar"
+		   index_dir = "/mail/foo/.imap/bar" */
+		path = mbox_get_path(storage, name);
+		index_dir = mbox_get_index_dir(storage, name);
+	}
+
+	index = index_storage_alloc(index_dir, path, MBOX_INDEX_PREFIX);
+	ibox = mbox_alloc(storage, index, name, flags);
+	if (ibox == NULL)
+		return NULL;
+
+	ibox->path = p_strdup(ibox->box.pool, path);
 
 	if (access(path, R_OK|W_OK) < 0) {
 		if (errno < EACCES)
@@ -547,8 +558,30 @@
 }
 
 static struct mailbox *
-mbox_mailbox_open(struct mail_storage *_storage,
-		  const char *name, enum mailbox_open_flags flags)
+mbox_mailbox_open_stream(struct index_storage *storage, const char *name,
+			 struct istream *input, enum mailbox_open_flags flags)
+{
+	struct mail_index *index;
+	struct index_mailbox *ibox;
+
+	flags |= MAILBOX_OPEN_READONLY;
+
+	index = mail_index_alloc(NULL, NULL);
+	ibox = mbox_alloc(storage, index, name, flags);
+	if (ibox == NULL)
+		return NULL;
+
+	i_stream_ref(input);
+	ibox->mbox_file_stream = input;
+	ibox->mbox_readonly = TRUE;
+
+	ibox->path = "(read-only mbox stream)";
+	return &ibox->box;
+}
+
+static struct mailbox *
+mbox_mailbox_open(struct mail_storage *_storage, const char *name,
+		  struct istream *input, enum mailbox_open_flags flags)
 {
 	struct index_storage *storage = (struct index_storage *)_storage;
 	const char *path;
@@ -556,6 +589,9 @@
 
 	mail_storage_clear_error(_storage);
 
+	if (input != NULL)
+		return mbox_mailbox_open_stream(storage, name, input, flags);
+
 	if (strcmp(name, "INBOX") == 0) {
 		/* make sure INBOX exists */
 		if (verify_inbox(storage) < 0)
@@ -916,7 +952,11 @@
 	}
 
         mbox_file_close(ibox);
-        index_storage_mailbox_free(box);
+	if (ibox->mbox_file_stream != NULL) {
+		i_stream_unref(ibox->mbox_file_stream);
+                ibox->mbox_file_stream = NULL;
+	}
+	index_storage_mailbox_free(box);
 	return ret;
 }
 

Index: mbox-sync.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-sync.c,v
retrieving revision 1.141
retrieving revision 1.142
diff -u -d -r1.141 -r1.142
--- mbox-sync.c	29 Mar 2005 10:30:20 -0000	1.141
+++ mbox-sync.c	29 Mar 2005 13:33:08 -0000	1.142
@@ -1257,17 +1257,28 @@
 int mbox_sync_has_changed(struct index_mailbox *ibox, int leave_dirty)
 {
 	const struct mail_index_header *hdr;
-	struct stat st;
-
-	hdr = mail_index_get_header(ibox->view);
+	const struct stat *st;
+	struct stat statbuf;
 
-	if (stat(ibox->path, &st) < 0) {
-		mbox_set_syscall_error(ibox, "stat()");
-		return -1;
+	if (ibox->mbox_file_stream != NULL && ibox->mbox_fd == -1) {
+		/* read-only stream */
+		st = i_stream_stat(ibox->mbox_file_stream);
+		if (st == NULL) {
+			mbox_set_syscall_error(ibox, "i_stream_stat()");
+			return -1;
+		}
+	} else {
+		if (stat(ibox->path, &statbuf) < 0) {
+			mbox_set_syscall_error(ibox, "stat()");
+			return -1;
+		}
+		st = &statbuf;
 	}
 
-	if ((uint32_t)st.st_mtime == hdr->sync_stamp &&
-	    (uint64_t)st.st_size == hdr->sync_size) {
+	hdr = mail_index_get_header(ibox->view);
+
+	if ((uint32_t)st->st_mtime == hdr->sync_stamp &&
+	    (uint64_t)st->st_size == hdr->sync_size) {
 		/* fully synced */
 		ibox->mbox_sync_dirty = FALSE;
 		return 0;
@@ -1276,8 +1287,8 @@
 	if (!ibox->mbox_sync_dirty || !leave_dirty)
 		return 1;
 
-	return st.st_mtime != ibox->mbox_dirty_stamp ||
-		st.st_size != ibox->mbox_dirty_size;
+	return st->st_mtime != ibox->mbox_dirty_stamp ||
+		st->st_size != ibox->mbox_dirty_size;
 }
 
 static int mbox_sync_update_imap_base(struct mbox_sync_context *sync_ctx)



More information about the dovecot-cvs mailing list