diff src/lib-storage/index/dbox/dbox-uidlist.c @ 3761:ef482c909771 HEAD

When index files (dovecot.index and dbox index) aren't synced with each others do a full sync.
author Timo Sirainen <tss@iki.fi>
date Wed, 21 Dec 2005 20:43:16 +0200
parents d216077076b8
children 2881f7e79098
line wrap: on
line diff
--- a/src/lib-storage/index/dbox/dbox-uidlist.c	Wed Dec 21 20:41:50 2005 +0200
+++ b/src/lib-storage/index/dbox/dbox-uidlist.c	Wed Dec 21 20:43:16 2005 +0200
@@ -157,7 +157,7 @@
 	const unsigned int *file_seq = key;
 	struct dbox_uidlist_entry *const *entry = p;
 
-	return (int)file_seq - (int)(*entry)->file_seq;
+	return (int)*file_seq - (int)(*entry)->file_seq;
 }
 
 static int dbox_uidlist_add_entry(struct dbox_uidlist *uidlist,
@@ -537,10 +537,14 @@
 
 	/* grow mtime by one if needed to make sure the last write is noticed */
 	lock_path = file_dotlock_get_lock_path(uidlist->dotlock);
-	if (stat(uidlist->path, &st) < 0 && errno != ENOENT) {
-		mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
-			"stat(%s) failed: %m", uidlist->path);
-		return -1;
+	if (stat(uidlist->path, &st) < 0) {
+		if (errno != ENOENT) {
+			mail_storage_set_critical(
+				STORAGE(uidlist->mbox->storage),
+				"stat(%s) failed: %m", uidlist->path);
+			return -1;
+		}
+		st.st_mtime = 0;
 	}
 	if (fstat(uidlist->lock_fd, &st2) < 0) {
 		mail_storage_set_critical(STORAGE(uidlist->mbox->storage),
@@ -551,9 +555,9 @@
 	if (st2.st_mtime <= st.st_mtime) {
 		struct utimbuf ut;
 
-		st.st_mtime++;
+		st2.st_mtime = ++st.st_mtime;
 		ut.actime = ioloop_time;
-		ut.modtime = st.st_mtime;
+		ut.modtime = st2.st_mtime;
 
 		if (utime(lock_path, &ut) < 0) {
 			mail_storage_set_critical(
@@ -563,8 +567,8 @@
 		}
 	}
 
-	uidlist->ino = st.st_ino;
-	uidlist->mtime = st.st_mtime;
+	uidlist->ino = st2.st_ino;
+	uidlist->mtime = st2.st_mtime;
 
 	/* now, finish the uidlist update by renaming the lock file to
 	   uidlist */
@@ -1022,10 +1026,12 @@
 }
 
 int dbox_uidlist_sync_init(struct dbox_uidlist *uidlist,
-			   struct dbox_uidlist_sync_ctx **ctx_r)
+			   struct dbox_uidlist_sync_ctx **ctx_r,
+			   time_t *mtime_r)
 {
 	int ret;
 
+	*mtime_r = -1;
 	if (dbox_uidlist_lock(uidlist) < 0)
 		return -1;
 
@@ -1037,6 +1043,8 @@
 	if (ret == 0) {
 		/* file is deleted */
 		uidlist->need_full_rewrite = TRUE;
+	} else {
+		*mtime_r = uidlist->mtime;
 	}
 
 	*ctx_r = i_new(struct dbox_uidlist_sync_ctx, 1);
@@ -1071,6 +1079,16 @@
 	i_free(ctx);
 }
 
+void dbox_uidlist_sync_from_scratch(struct dbox_uidlist_sync_ctx *ctx)
+{
+	array_clear(&ctx->uidlist->entries);
+	ctx->uidlist->ino = 0;
+	ctx->uidlist->mtime = 0;
+
+	ctx->modified = TRUE;
+	ctx->uidlist->need_full_rewrite = TRUE;
+}
+
 void dbox_uidlist_sync_set_modified(struct dbox_uidlist_sync_ctx *ctx)
 {
 	ctx->modified = TRUE;