changeset 5904:62ceb6b2b20d HEAD

Renamed maildir_generate_tmp_filename() to maildir_filename_generate(). Also changed it to create the unique filename with time+usecs+pid, increasing usecs as needed. So there's no longer a process delivery counter.
author Timo Sirainen <tss@iki.fi>
date Sun, 08 Jul 2007 21:28:54 +0300
parents 5d0827cd9839
children 81b3a22e6ce1
files src/lib-storage/index/maildir/maildir-copy.c src/lib-storage/index/maildir/maildir-filename.c src/lib-storage/index/maildir/maildir-filename.h src/lib-storage/index/maildir/maildir-sync.c src/lib-storage/index/maildir/maildir-util.c
diffstat 5 files changed, 23 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/maildir/maildir-copy.c	Sun Jul 08 21:24:57 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-copy.c	Sun Jul 08 21:28:54 2007 +0300
@@ -140,8 +140,7 @@
 	if (filename == NULL) {
 		/* the generated filename is _always_ unique, so we don't
 		   bother trying to check if it already exists */
-		do_ctx.dest_fname =
-			maildir_generate_tmp_filename(&ioloop_timeval);
+		do_ctx.dest_fname = maildir_filename_generate();
 	} else {
 		do_ctx.dest_fname = filename;
 		do_ctx.preserve_filename = TRUE;
--- a/src/lib-storage/index/maildir/maildir-filename.c	Sun Jul 08 21:24:57 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-filename.c	Sun Jul 08 21:28:54 2007 +0300
@@ -11,26 +11,28 @@
 
 #include <stdlib.h>
 
-const char *maildir_generate_tmp_filename(const struct timeval *tv)
+const char *maildir_filename_generate(void)
 {
-	static unsigned int create_count = 0;
-	static time_t first_stamp = 0;
+	static struct timeval last_tv = { 0, 0 };
+	struct timeval tv;
 
-	if (first_stamp == 0 || first_stamp == ioloop_time) {
-		/* it's possible that within last second another process had
-		   the same PID as us. Use usecs to make sure we don't create
-		   duplicate base name. */
-		first_stamp = ioloop_time;
-		return t_strdup_printf("%s.P%sQ%uM%s.%s",
-				       dec2str(tv->tv_sec), my_pid,
-				       create_count++,
-				       dec2str(tv->tv_usec), my_hostname);
-	} else {
-		/* Don't bother with usecs. Saves a bit space :) */
-		return t_strdup_printf("%s.P%sQ%u.%s",
-				       dec2str(tv->tv_sec), my_pid,
-				       create_count++, my_hostname);
+	/* use secs + usecs to guarantee uniqueness within this process. */
+	if (ioloop_timeval.tv_sec > last_tv.tv_sec ||
+	    (ioloop_timeval.tv_sec == last_tv.tv_sec &&
+	     ioloop_timeval.tv_usec > last_tv.tv_usec))
+		tv = ioloop_timeval;
+	else {
+		tv = last_tv;
+		if (++tv.tv_usec == 1000000) {
+			tv.tv_sec++;
+			tv.tv_usec = 0;
+		}
 	}
+	last_tv = tv;
+
+	return t_strdup_printf("%s.M%sP%s.%s",
+			       dec2str(tv.tv_sec), dec2str(tv.tv_usec),
+			       my_pid, my_hostname);
 }
 
 void maildir_filename_get_flags(struct maildir_keywords_sync_ctx *ctx,
--- a/src/lib-storage/index/maildir/maildir-filename.h	Sun Jul 08 21:24:57 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-filename.h	Sun Jul 08 21:28:54 2007 +0300
@@ -3,7 +3,7 @@
 
 struct maildir_keywords_sync_ctx;
 
-const char *maildir_generate_tmp_filename(const struct timeval *tv);
+const char *maildir_filename_generate(void);
 
 void maildir_filename_get_flags(struct maildir_keywords_sync_ctx *ctx,
 			       const char *fname, enum mail_flags *flags_r,
--- a/src/lib-storage/index/maildir/maildir-sync.c	Sun Jul 08 21:24:57 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-sync.c	Sun Jul 08 21:28:54 2007 +0300
@@ -605,7 +605,7 @@
 		return 0;
 	}
 
-	new_fname = maildir_generate_tmp_filename(&ioloop_timeval);
+	new_fname = maildir_filename_generate();
 	new_path = t_strconcat(mbox->path, "/new/", new_fname, NULL);
 
 	if (rename(old_path, new_path) == 0)
--- a/src/lib-storage/index/maildir/maildir-util.c	Sun Jul 08 21:24:57 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-util.c	Sun Jul 08 21:28:54 2007 +0300
@@ -88,20 +88,18 @@
 		       mode_t mode, const char **fname_r)
 {
 	struct stat st;
-	struct timeval *tv, tv_now;
 	unsigned int prefix_len;
 	const char *tmp_fname = NULL;
 	string_t *path;
 	int fd;
 
-	tv = &ioloop_timeval;
 	path = t_str_new(256);
 	str_append(path, dir);
 	str_append_c(path, '/');
 	prefix_len = str_len(path);
 
 	for (;;) {
-		tmp_fname = maildir_generate_tmp_filename(tv);
+		tmp_fname = maildir_filename_generate();
 		str_truncate(path, prefix_len);
 		str_append(path, tmp_fname);
 
@@ -117,11 +115,6 @@
 			if (fd != -1 || errno != EEXIST)
 				break;
 		}
-
-		sleep(2);
-		tv = &tv_now;
-		if (gettimeofday(&tv_now, NULL) < 0)
-			i_fatal("gettimeofday(): %m");
 	}
 
 	*fname_r = tmp_fname;